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