test : enhance accessible state information
[platform/upstream/at-spi2-core.git] / test / at_spi2_tool.c
1 #include "atspi/atspi.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <getopt.h>
6 #include <stdbool.h>
7 #include <gio/gio.h>
8 #include <assert.h>
9
10 #define ERROR_STATE -1
11 #define SAFE_BUFFER_SIZE 2048
12 #define CHECK_OUTPUT_WIDTH 42
13 #define MINIMAL_MODULE_WIDTH 3
14 #define ATTR_WIDTH 50
15 #define NUMBER_WIDTH 6
16 #define ARRAY_SIZE(x)  (sizeof(x) / sizeof((x)[0]))
17 #define COLUMN_NO 3
18 #define FLAG_NO 3
19 #define DUMP 0
20 #define CHECK 1
21 #define FIRST_MATCH 2
22 #define RELATION_TABLE_COLUMN_COUNT 7
23 #define ATTRIBUTE_TABLE_COLUMN_COUNT 3
24 #define RELATION_TABLE_COLUMN_WIDTH 20
25 #define ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH 20
26 #define VERSION "1.1"
27
28 static unsigned indent_width = 2;
29 static int module_name_limit = -1;
30
31 static const char* atspi_state_names[] = {
32         [ATSPI_STATE_INVALID] = "INVALID",
33         [ATSPI_STATE_ACTIVE] = "ACTIVE",
34         [ATSPI_STATE_ARMED] = "ARMED",
35         [ATSPI_STATE_BUSY] =  "BUSY",
36         [ATSPI_STATE_CHECKED] = "CHECKED",
37         [ATSPI_STATE_COLLAPSED] = "COLLAPSED",
38         [ATSPI_STATE_DEFUNCT] = "DEFUNCT",
39         [ATSPI_STATE_EDITABLE] = "EDITABLE",
40         [ATSPI_STATE_ENABLED] = "ENABLED",
41         [ATSPI_STATE_EXPANDABLE] = "EXPANDABLE",
42         [ATSPI_STATE_EXPANDED] = "EXPANDED",
43         [ATSPI_STATE_FOCUSABLE] = "FOCUSABLE",
44         [ATSPI_STATE_FOCUSED] = "FOCUSED",
45         [ATSPI_STATE_HAS_TOOLTIP] = "HAS_TOOLTIP",
46         [ATSPI_STATE_HORIZONTAL] = "HORIZONTAL",
47         [ATSPI_STATE_ICONIFIED] = "ICONIFIED",
48         [ATSPI_STATE_MULTI_LINE] = "MULTI_LINE",
49         [ATSPI_STATE_MULTISELECTABLE] = "MULTISELECTABLE",
50         [ATSPI_STATE_OPAQUE] = "OPAQUE",
51         [ATSPI_STATE_PRESSED] = "PRESSED",
52         [ATSPI_STATE_RESIZABLE] = "RESIZABLE",
53         [ATSPI_STATE_SELECTABLE] = "SELECTABLE",
54         [ATSPI_STATE_SELECTED] = "SELECTED",
55         [ATSPI_STATE_SENSITIVE] = "SENSITIVE",
56         [ATSPI_STATE_SHOWING] = "SHOWING",
57         [ATSPI_STATE_SINGLE_LINE] = "SINGLE_LINE",
58         [ATSPI_STATE_STALE] = "STALE",
59         [ATSPI_STATE_TRANSIENT] = "TRANSIENT",
60         [ATSPI_STATE_VERTICAL] = "VERTICAL",
61         [ATSPI_STATE_VISIBLE] = "VISIBLE",
62         [ATSPI_STATE_MANAGES_DESCENDANTS] = "MANAGES_DESCENDANTS",
63         [ATSPI_STATE_INDETERMINATE] = "INDETERMINATE",
64         [ATSPI_STATE_REQUIRED] = "REQUIRED",
65         [ATSPI_STATE_TRUNCATED] = "TRUNCATED",
66         [ATSPI_STATE_ANIMATED] = "ANIMATED",
67         [ATSPI_STATE_INVALID_ENTRY] = "INVALID_ENTRY",
68         [ATSPI_STATE_SUPPORTS_AUTOCOMPLETION] = "SUPPORTS_AUTOCOMPLETION",
69         [ATSPI_STATE_SELECTABLE_TEXT] = "SELECTABLE_TEXT",
70         [ATSPI_STATE_IS_DEFAULT] = "IS_DEFAULT",
71         [ATSPI_STATE_VISITED] = "VISITED",
72         [ATSPI_STATE_CHECKABLE] = "CHECKABLE",
73         [ATSPI_STATE_MODAL] = "MODAL",
74         [ATSPI_STATE_HIGHLIGHTED] = "HIGHLIGHTED",
75         [ATSPI_STATE_HIGHLIGHTABLE] = "HIGHLIGHTABLE",
76         [ATSPI_STATE_HAS_POPUP] = "HAS_POPUP",
77         [ATSPI_STATE_READ_ONLY] = "READ_ONLY",
78         [ATSPI_STATE_LAST_DEFINED] = "LAST_DEFINED"
79 };
80
81 typedef struct _Box_Size {
82         char *x;
83         char *y;
84         char *height;
85         char *width;
86 } Box_Size;
87
88 static char *_multiply_string(char c, unsigned number)
89 {
90         char *result = (char *)calloc(1, number + 1);
91
92         if (result == NULL)
93                 return result;
94
95         memset(result, c, number);
96
97         return result;
98 }
99
100 static void _print_module_legend()
101 {
102         printf("\n[[node],[node role name],[attributes list],[x,y,width,height],[node name],[states list][relations]\n\n");
103 }
104
105 static void _print_atspi_states_legend()
106 {
107         _print_module_legend();
108
109         int array_len = ARRAY_SIZE(atspi_state_names);
110         int line_count = (array_len + COLUMN_NO - 1)/COLUMN_NO;
111         for (int line_idx = 0; line_idx < line_count; line_idx++) {
112                 if ((line_idx < line_count - 1) || (array_len % COLUMN_NO == 0)) {
113                         printf("[%d]\t%-24s[%d]\t%-24s[%d]\t%s\n",
114                                         line_idx * COLUMN_NO,
115                                         atspi_state_names[line_idx * COLUMN_NO],
116                                         (line_idx * COLUMN_NO) + 1,
117                                         atspi_state_names[(line_idx * COLUMN_NO) + 1],
118                                         (line_idx * COLUMN_NO) + 2,
119                                         atspi_state_names[(line_idx * COLUMN_NO) + 2]);
120                 } else if (array_len % COLUMN_NO == 2) {
121                         printf("[%d]\t%-24s[%d]\t%s\n",
122                                         line_idx * COLUMN_NO,
123                                         atspi_state_names[line_idx * COLUMN_NO],
124                                         (line_idx * COLUMN_NO) + 1,
125                                         atspi_state_names[(line_idx * COLUMN_NO) + 1]);
126                 } else {
127                         printf("[%d]\t%s\n", line_idx * COLUMN_NO, atspi_state_names[line_idx * COLUMN_NO]);
128                 }
129         }
130 }
131
132 static void _set_indent(int indent_size)
133 {
134         if (indent_size < 0) {
135                 fprintf(stderr, "WARNING: Invalid indent size. Default value retained.\n");
136                 return;
137         }
138
139         indent_width = indent_size;
140 }
141
142 static void _set_module_length_limit(int limit)
143 {
144         if (limit < MINIMAL_MODULE_WIDTH) {
145                 fprintf(stderr, "WARNING: Invalid maximum field size. Default value retained.\n");
146                 return;
147         }
148
149         module_name_limit = limit;
150 }
151
152 static int _int_sort_function(gconstpointer a, gconstpointer b)
153 {
154         int int_a = GPOINTER_TO_INT (a);
155         int int_b = GPOINTER_TO_INT (b);
156
157         return int_a - int_b;
158 }
159
160 static void _truncate_string(char *string, int length_limit)
161 {
162         if (length_limit < MINIMAL_MODULE_WIDTH || !string)
163                 return;
164
165         int len = strlen(string);
166
167         if (len > length_limit)
168                 memcpy(string + length_limit - 3, "...", 4);
169 }
170
171 static size_t _combine_strings(char **destination, const char *source)
172 {
173         if (!source || !*source || !destination)
174                 return 0;
175
176         size_t old_len = *destination ? strlen(*destination) : 0;
177         size_t source_len = strlen(source);
178         size_t len = old_len + source_len;
179         char *buf = realloc(*destination, (len + 1) * sizeof(char));
180
181         if (!buf) {
182                 fprintf(stderr, "_combine_strings: allocation failed");
183                 return old_len;
184         }
185
186         memcpy(buf + old_len, source, source_len + 1);
187
188         *destination = buf;
189
190         return len;
191 }
192
193 static Box_Size *_get_box_size(AtspiAccessible *node)
194 {
195         Box_Size *box_size = (Box_Size *)malloc(sizeof(Box_Size));
196         if (box_size == NULL)
197                 return NULL;
198
199         char *x = NULL;
200         char *y = NULL;
201         char *width = NULL;
202         char *height = NULL;
203
204         AtspiComponent *component = atspi_accessible_get_component_iface(node);
205         AtspiRect *extent = component ? atspi_component_get_extents(component, ATSPI_COORD_TYPE_SCREEN, NULL) : NULL;
206
207         if (extent == NULL) {
208                 _combine_strings(&x, "_");
209                 _combine_strings(&y, "_");
210                 _combine_strings(&width, "_");
211                 _combine_strings(&height, "_");
212         } else {
213                 char temp_buff[NUMBER_WIDTH];
214
215                 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->x);
216                 _combine_strings(&x, temp_buff);
217
218                 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->y);
219                 _combine_strings(&y, temp_buff);
220
221                 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->width);
222                 _combine_strings(&width, temp_buff);
223
224                 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->height);
225                 _combine_strings(&height, temp_buff);
226
227                 g_object_unref(component);
228                 g_free(extent);
229         }
230
231         box_size->x = x;
232         box_size->y = y;
233         box_size->width = width;
234         box_size->height = height;
235
236         return box_size;
237 }
238
239 static char *_get_states(AtspiAccessible *node, int length_limit)
240 {
241         AtspiStateSet *node_state_set = atspi_accessible_get_state_set(node);
242         GArray *states = atspi_state_set_get_states(node_state_set);
243         if (!states) return NULL;
244         g_array_sort(states, _int_sort_function);
245
246         AtspiStateType state_type;
247         char *state_string = NULL;
248
249         for (int i = 0; i < states->len; i++) {
250                 state_type = g_array_index(states, AtspiStateType, i);
251
252                 char node_state_str[27] = "";
253                 strncat(node_state_str, "(", sizeof(node_state_str) - strlen(node_state_str) - 1);
254                 strncat(node_state_str, atspi_state_names[state_type], sizeof(node_state_str) - strlen(node_state_str) - 1);
255                 strncat(node_state_str, ")", sizeof(node_state_str) - strlen(node_state_str) - 1);
256
257                 _combine_strings(&state_string, node_state_str);
258         }
259
260         g_array_free(states, 0);
261         g_object_unref(node_state_set);
262
263         _truncate_string(state_string, length_limit);
264
265         return state_string;
266 }
267
268 static char *_get_attributes(AtspiAccessible *node, int length_limit, bool *attributes_are_too_long)
269 {
270         GHashTable *attributes = atspi_accessible_get_attributes(node, NULL);
271
272         if (!attributes)
273                 return NULL;
274
275         GHashTableIter attributes_iter;
276         gpointer attr_key;
277         gpointer attr_value;
278         g_hash_table_iter_init (&attributes_iter, attributes);
279         char *result = NULL;
280         while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
281                 char attributes_string[SAFE_BUFFER_SIZE];
282                 int ret = snprintf(attributes_string, SAFE_BUFFER_SIZE, "(%s=%s)", (char *)attr_key, (char *)attr_value);
283                 if (ret >= SAFE_BUFFER_SIZE)
284                         fprintf(stderr, "\n%s, %s %d: generated string is too long. Buffer overflow\n", __FILE__, __FUNCTION__, __LINE__);
285
286                 _combine_strings(&result, attributes_string);
287         }
288         g_hash_table_unref(attributes);
289
290         int real_truncate_size = (length_limit < ATTR_WIDTH && length_limit > MINIMAL_MODULE_WIDTH) ? length_limit : ATTR_WIDTH;
291         if (result && attributes_are_too_long && strlen(result) > real_truncate_size)
292                 *attributes_are_too_long = true;
293
294         _truncate_string(result, real_truncate_size);
295         return result;
296 }
297
298 static char *_get_info(AtspiAccessible *node, int length_limit, bool *attributes_are_too_long, bool *app_has_relations)
299 {
300         if (!node)
301                 return NULL;
302
303         char *node_name = atspi_accessible_get_name(node, NULL);
304         char *node_role_name = atspi_accessible_get_role_name(node, NULL);
305         char *unique_id = atspi_accessible_get_unique_id(node, NULL);
306
307         char *attributes = _get_attributes(node, length_limit, attributes_are_too_long);
308         Box_Size *box_size = _get_box_size(node);
309         char *states = _get_states(node, length_limit);
310
311         GArray *relations = atspi_accessible_get_relation_set(node, NULL);
312         bool current_node_has_relations = (relations && relations->len);
313
314         char result[SAFE_BUFFER_SIZE];
315         int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%s]]",
316                                                 unique_id,
317                                                 node_role_name,
318                                                 attributes,
319                                                 box_size->x,
320                                                 box_size->y,
321                                                 box_size->width,
322                                                 box_size->height,
323                                                 node_name,
324                                                 states,
325                                                 current_node_has_relations ? "*" : "");
326
327         if (ret >= SAFE_BUFFER_SIZE)
328                 fprintf(stderr, "\n%s, %s %d: generated string is too long. Buffer overflow\n", __FILE__, __FUNCTION__, __LINE__);
329
330         if (current_node_has_relations)
331                 *app_has_relations = true;
332
333         free(node_name);
334         free(node_role_name);
335         free(unique_id);
336         free(attributes);
337         if (box_size) {
338                 free(box_size->width);
339                 free(box_size->height);
340                 free(box_size);
341         }
342         free(states);
343         g_array_free(relations, TRUE);
344
345         return g_strdup(result);
346 }
347
348 static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
349 {
350         int parent_index = atspi_accessible_get_index_in_parent(obj, NULL);
351         AtspiAccessible *parent = atspi_accessible_get_parent(obj, NULL);
352
353         char output[CHECK_OUTPUT_WIDTH];
354         if (parent_index != parent_candidate_index || parent_candidate != parent) {
355                 char parent_status[NUMBER_WIDTH];
356
357                 if (parent == NULL)
358                         strncpy(parent_status, "_", NUMBER_WIDTH);
359                 else
360                         snprintf(parent_status, NUMBER_WIDTH, "%d", parent_index);
361
362                 char *parent_unique_id = atspi_accessible_get_unique_id(parent, NULL);
363                 char *parent_candidate_unique_id = atspi_accessible_get_unique_id(parent_candidate, NULL);
364                 snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%s,%s>]", parent_candidate_index, parent_status,
365                                 parent_candidate_unique_id, parent_unique_id);
366                 free(parent_unique_id);
367                 free(parent_candidate_unique_id);
368
369         } else {
370                 snprintf(output, CHECK_OUTPUT_WIDTH, "[OK]");
371         }
372
373         printf("%-*s\t", CHECK_OUTPUT_WIDTH, output);
374 }
375
376 static int _print_atspi_tree_verify_maybe_r(int indent_number, AtspiAccessible *object, bool check_integrity, int length_limit,
377                                                                                         bool *attributes_are_too_long, bool *app_has_relations)
378 {
379         char *indent = _multiply_string(' ', indent_number*indent_width);
380         if (indent != NULL) {
381                 printf("%s", indent);
382                 free(indent);
383         }
384
385         char *node_info = _get_info(object, length_limit, attributes_are_too_long, app_has_relations);
386         if (node_info != NULL) {
387                 printf("%s\n", node_info);
388                 free(node_info);
389         }
390
391         int count = atspi_accessible_get_child_count(object, NULL);
392         for (int i = 0; i < count; i++) {
393                 AtspiAccessible *child = atspi_accessible_get_child_at_index(object, i, NULL);
394                 if (child) {
395                         if (check_integrity)
396                                 _test_atspi_parent_child_relation(child, object, i);
397
398                         _print_atspi_tree_verify_maybe_r(indent_number + 1, child, check_integrity, length_limit, attributes_are_too_long, app_has_relations);
399                 }
400         }
401         return 0;
402 }
403
404 void _print_help()
405 {
406         printf("AT-SPI2-CORE-UTIL\n\n");
407         printf("USAGE: at_spi2_tool [OPTION] [PARAMETER] ...\n\n");
408         printf("OPTION LIST:\n");
409         printf("-h, --help\t\tshow this message\n");
410         printf("-v, --version\t\tshow actual version of tool\n");
411         printf("-g, --show-legend\tprint AT-SPI state legend\n");
412         printf("-l, --list-apps\t\tlist all applications of desktop\n");
413         printf("-a, --at-spi-client <true|false>\tenable/disable org.a11y.Status.IsEnabled property\n");
414         printf("-s, --sleep <N>\tsleep N seconds\n");
415         printf("-d, --tree-dump\t\tdump tree for selected application\n");
416         printf("-c, --tree-check\tcheck tree for selected application\n");
417         printf("-f, --first-match\tperform dump or check only for first matching application\n");
418         printf("-i, --indent\t\tset indentation size, default value stands at 2\n");
419         printf("-t, --truncate\t\tset maximum single field size, default value stands at 30\n");
420         printf("\nEXAMPLE:\n");
421         printf("\tat_spi2_tool -i3 -d quickpanel\n");
422         printf("\t  show AT-SPI tree for node \"quickpanel\" using three-space indentation\n");
423         printf("\tat_spi2_tool -i1 -c starter\n");
424         printf("\t  show AT-SPI tree with integrity test for node \"starter\" using one-space indentation\n");
425 }
426
427 void _print_version()
428 {
429         printf("AT-SPI2-CORE-UTIL v%s\n", VERSION);
430 }
431
432 static void _print_horizontal_line_in_relations_table() {
433         for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++) {
434                 for (int j = 0; j < RELATION_TABLE_COLUMN_WIDTH; j++)
435                         printf("-");
436                 printf("+");
437         }
438         printf("\n");
439 }
440
441 static char *_get_unique_id_of_object_in_relation(AtspiRelation *relation) {
442         if (!relation)
443                 return NULL;
444
445         gint last_index = atspi_relation_get_n_targets(relation) - 1;
446         AtspiAccessible *target = atspi_relation_get_target(relation, last_index);
447         return atspi_accessible_get_unique_id(target, NULL);
448 }
449
450 static void _print_relations_for_object(AtspiAccessible *node) {
451         GArray *relations = atspi_accessible_get_relation_set(node, NULL);
452         if (!relations)
453                 return;
454
455         if (!relations->len) {
456                 g_array_free(relations, FALSE);
457                 return;
458         }
459
460         char **table = calloc(RELATION_TABLE_COLUMN_COUNT, sizeof(char *));
461         if (!table) {
462                 fprintf(stderr, "Calloc failed. Can't alloc memory for object relations string\n");
463                 return;
464         }
465
466         table[0] = atspi_accessible_get_unique_id(node, NULL);
467         for (int i = 0; i < relations->len; i++) {
468                 AtspiRelation *relation = g_array_index(relations, AtspiRelation *, i);
469                 AtspiRelationType type = atspi_relation_get_relation_type(relation);
470                 int idx;
471                 switch (type) {
472                         case ATSPI_RELATION_CONTROLLER_FOR:     idx = 1; break;
473                         case ATSPI_RELATION_CONTROLLED_BY:      idx = 2; break;
474                         case ATSPI_RELATION_FLOWS_TO:           idx = 3; break;
475                         case ATSPI_RELATION_FLOWS_FROM:         idx = 4; break;
476                         case ATSPI_RELATION_DESCRIBED_BY:       idx = 5; break;
477                         default: idx = 0;
478                 }
479
480                 if (idx > 0)
481                         table[idx] = _get_unique_id_of_object_in_relation(relation);
482                 else {
483                         char buf[16];
484                         snprintf(buf, sizeof(buf), " [%d]", type);
485                         _combine_strings(&table[RELATION_TABLE_COLUMN_COUNT - 1], buf);
486                 }
487         }
488
489         for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++) {
490                 printf("%*s|", RELATION_TABLE_COLUMN_WIDTH, table[i] ? table[i] : "");
491                 free(table[i]);
492         }
493         free(table);
494
495         printf("\n");
496         _print_horizontal_line_in_relations_table();
497         g_array_free(relations, TRUE);
498 }
499
500 typedef void (*print_information_about_object_function)(AtspiAccessible *);
501
502 static void _iterate_over_tree(print_information_about_object_function func, AtspiAccessible *node) {
503         func(node);
504
505         int count = atspi_accessible_get_child_count(node, NULL);
506         for (int i = 0; i < count; i++) {
507                 AtspiAccessible *child = atspi_accessible_get_child_at_index(node, i, NULL);
508                 if (child)
509                         _iterate_over_tree(func, child);
510         }
511 }
512
513 static void _print_relations_for_objects_in_tree(AtspiAccessible *node) {
514         _iterate_over_tree(_print_relations_for_object, node);
515 }
516
517 static void _print_header_for_relation_table() {
518         char *table[] = {"OBJECT", "CONTROLLER_FOR", "CONTROLLED_BY", "FLOWS_TO", "FLOWS_FROM", "DESCRIBED_BY", "OTHER RELATION [ID]"};
519         assert(ARRAY_SIZE(table) == RELATION_TABLE_COLUMN_COUNT);
520
521         _print_horizontal_line_in_relations_table();
522
523         for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++)
524                 printf("%*s|", RELATION_TABLE_COLUMN_WIDTH , table[i]);
525
526         printf("\n");
527         _print_horizontal_line_in_relations_table();
528 }
529
530 static void _print_relations_table(AtspiAccessible *node) {
531         printf("\nRELATIONS TABLE\n");
532         _print_header_for_relation_table();
533         _print_relations_for_objects_in_tree(node);
534 }
535
536 static void _print_horizontal_line_in_attributes_table() {
537         int size_factor = 1;
538         for (int i = 0; i < ATTRIBUTE_TABLE_COLUMN_COUNT; i++) {
539                 if (i == ATTRIBUTE_TABLE_COLUMN_COUNT - 1)
540                         size_factor = 4;
541                 for (int j = 0; j < ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH * size_factor; j++)
542                         printf("-");
543                 printf("+");
544         }
545         printf("\n");
546 }
547
548 static void _print_attributes_for_object(AtspiAccessible *node) {
549         GHashTable *attributes = atspi_accessible_get_attributes(node, NULL);
550
551         if (!attributes)
552                 return;
553
554         char *unique_id = atspi_accessible_get_unique_id(node, NULL);
555         GHashTableIter attributes_iter;
556         gpointer attr_key;
557         gpointer attr_value;
558
559         g_hash_table_iter_init (&attributes_iter, attributes);
560         while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
561                 printf("%*s|", ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH, unique_id ? unique_id : "");
562                 printf("%*s|", ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH, attr_key ? (char *) attr_key : "");
563                 printf("%*s|\n", ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH * 4, attr_value ? (char *) attr_value : "");
564         }
565
566         if (g_hash_table_size (attributes))
567                 _print_horizontal_line_in_attributes_table();
568
569         g_hash_table_unref(attributes);
570         free(unique_id);
571 }
572
573 static void _print_attributes_for_objects_in_tree(AtspiAccessible *node) {
574         _iterate_over_tree(_print_attributes_for_object, node);
575 }
576
577 static void _print_header_for_attributes_table() {
578         char *table[] = {"OBJECT", "ATTRIBUTE KEY", "ATTRIBUTE VALUE"};
579         assert(ARRAY_SIZE(table) == ATTRIBUTE_TABLE_COLUMN_COUNT);
580
581         _print_horizontal_line_in_attributes_table();
582
583         int size_factor = 1;
584         for (int i = 0; i < ATTRIBUTE_TABLE_COLUMN_COUNT; i++) {
585                 if (i == ATTRIBUTE_TABLE_COLUMN_COUNT - 1)
586                         size_factor = 4;
587                 printf("%*s|", ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH * size_factor , table[i]);
588         }
589
590         printf("\n");
591         _print_horizontal_line_in_attributes_table();
592 }
593
594 static void _print_attributes_table(AtspiAccessible *node) {
595         printf("\nATTRIBUTES TABLE\n");
596         _print_header_for_attributes_table();
597         _print_attributes_for_objects_in_tree(node);
598 }
599
600 static void _atspi_tree_traverse(const char *app_name, bool dump, bool check, bool first_match, int length_limit)
601 {
602
603         AtspiAccessible *desktop = atspi_get_desktop(0);
604         if (!desktop) {
605                 fprintf(stderr, "atspi_get_desktop failed\n");
606                 return;
607         }
608
609         int count = atspi_accessible_get_child_count(desktop, NULL);
610         bool app_name_matched = false;
611
612         for (int i = 0; i < count; i++) {
613                 AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL);
614                 if (child == NULL) {
615                         fprintf(stderr, "\n%s, %s %d: Null application occured. Results may be misleading.\n", __FILE__, __FUNCTION__, __LINE__);
616                         continue;
617                 }
618
619                 char *name = atspi_accessible_get_name(child, NULL);
620                 bool attributes_are_too_long = false;
621                 bool app_has_relations = false;
622
623                 if (!dump && !check)
624                         printf("%s\n", name);
625
626                 if ((check || dump) && name && !strcmp(name, app_name)) {
627                         app_name_matched = true;
628
629                         _print_module_legend();
630
631                         if (check)
632                                 _test_atspi_parent_child_relation(child, desktop, i);
633
634                         _print_atspi_tree_verify_maybe_r(0, child, check, length_limit, &attributes_are_too_long, &app_has_relations);
635
636                         if (app_has_relations)
637                                 _print_relations_table(child);
638
639                         if (attributes_are_too_long)
640                                 _print_attributes_table(child);
641
642                         if (first_match) {
643                                 free(name);
644                                 break;
645                         } else {
646                                 printf("\n");
647                         }
648
649                         free(name);
650                 }
651         }
652
653         if (!app_name_matched && (dump || check))
654                 fprintf(stderr, "There is no application with name: %s. Try again.\n", app_name);
655
656         g_object_unref(desktop);
657 }
658
659 static void _at_spi_client_enable(gboolean enabled)
660 {
661         static GDBusProxy *proxy = NULL; //we keep proxy (dbus connection) until program exits
662         GVariant *result;
663         GError *error = NULL;
664         GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_NONE;
665
666
667         if (!proxy) {
668                 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
669                                         flags,
670                                         NULL, /* GDBusInterfaceInfo */
671                                         "org.a11y.Bus",
672                                         "/org/a11y/bus",
673                                         "org.freedesktop.DBus.Properties",
674                                         NULL, /* GCancellable */
675                                         &error);
676                 if (error) {
677                         fprintf(stderr, "Failed to create proxy object for '/org/a11y/bus': %s\n", error->message);
678                         g_error_free(error);
679                         return;
680                 }
681         }
682
683         result = g_dbus_proxy_call_sync(proxy,
684                                         "Set",
685                                         g_variant_new ("(ssv)",  "org.a11y.Status", "IsEnabled", g_variant_new_boolean(enabled)),
686                                         G_DBUS_CALL_FLAGS_NONE,
687                                         -1,
688                                         NULL,
689                                         &error);
690         if (result)
691                 g_variant_unref(result);
692
693         if (error) {
694                 fprintf(stderr, "Fail to call org.freedesktop.DBus.Properties.Set: %s\n", error->message);
695                 g_error_free(error);
696         }
697 }
698
699 static void _run_command(int argc, char *argv[])
700 {
701         struct option long_options[] = {
702                 {"help", no_argument, 0, 'h'},
703                 {"version", no_argument, 0, 'v'},
704                 {"show-legend", no_argument, 0, 'g'},
705                 {"list-apps", no_argument, 0, 'l'},
706                 {"at-spi-client", no_argument, 0, 'a'},
707                 {"sleep", required_argument, 0, 's'},
708                 {"tree-dump", required_argument, 0, 'd'},
709                 {"tree-check", required_argument, 0, 'c'},
710                 {"first-match", no_argument, 0, 'f'},
711                 {"indent", required_argument, 0, 'i'},
712                 {"truncate", required_argument, 0, 't'},
713         };
714
715         int command = 0;
716         int option_index = 0;
717         bool traverse_flags[FLAG_NO] = {false};
718         char *app_name = NULL;
719         gboolean enable_at_spi_client;
720
721         while (TRUE) {
722                 command = getopt_long(argc, argv, "hvgla:s:d:c:ft:i:", long_options, &option_index);
723
724                 if (command == ERROR_STATE)
725                         break;
726
727                 switch (command) {
728                 case 'h':
729                         _print_help();
730                         break;
731
732                 case 'v':
733                         _print_version();
734                         break;
735
736                 case 'g':
737                         _print_atspi_states_legend();
738                         break;
739
740                 case 'l':
741                         _atspi_tree_traverse(NULL, false, false, false, module_name_limit);
742                         break;
743
744                 case 'a':
745                         enable_at_spi_client = TRUE;
746                         if (optarg[0] == 'f' || optarg[0] == '0')
747                                 enable_at_spi_client = FALSE;
748
749                         _at_spi_client_enable(enable_at_spi_client);
750                         break;
751
752                 case 's':
753                         sleep(atoi(optarg));
754                         break;
755
756                 case 'd':
757                         traverse_flags[DUMP] = true;
758                         app_name = optarg;
759                         break;
760
761                 case 'c':
762                         traverse_flags[CHECK] = true;
763                         app_name = optarg;
764                         break;
765
766                 case 'f':
767                         traverse_flags[FIRST_MATCH] = true;
768                         break;
769
770                 case 'i':
771                         _set_indent(atoi(optarg));
772                         break;
773
774                 case 't':
775                         _set_module_length_limit(atoi(optarg));
776                         break;
777
778                 case '?':
779                         fprintf(stderr, "Invalid parameter. Use: \"--help\"\n");
780                         break;
781
782                 default:
783                         abort();
784                 }
785         }
786
787         if (traverse_flags[DUMP] || traverse_flags[CHECK])
788                 _atspi_tree_traverse(app_name, traverse_flags[DUMP], traverse_flags[CHECK], traverse_flags[FIRST_MATCH], module_name_limit);
789 }
790
791 int main(int argc, char *argv[])
792 {
793         if (argc < 2) {
794                 printf("Arguments required. Type %s --help.\n", argv[0]);
795                 return -1;
796         }
797
798         int result = atspi_init();
799         if (result != 0)
800         {
801                 fprintf(stderr, "Unable to initilize AT-SPI infrastructure, atspi_init failed\n");
802                 return -1;
803         }
804
805         _run_command(argc, argv);
806
807         return 0;
808 }