badaaa3d03600578d24b0bbebf70da6e96c36d7d
[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
8 #define ERROR_STATE -1
9 #define SAFE_BUFFER_SIZE 2048
10 #define CHECK_OUTPUT_WIDTH 42
11 #define MINIMAL_MODULE_WIDTH 3
12 #define ATTR_WIDTH 50
13 #define NUMBER_WIDTH 6
14 #define ARRAY_SIZE(x)  (sizeof(x) / sizeof((x)[0]))
15 #define COLUMN_NO 3
16 #define FLAG_NO 3
17 #define DUMP 0
18 #define CHECK 1
19 #define FIRST_MATCH 2
20 #define VERSION "1.1"
21
22 static unsigned indent_width = 2;
23 static int module_name_limit = -1;
24
25 static const char* atspi_state_names[] = {
26         [ATSPI_STATE_INVALID] = "INVALID",
27         [ATSPI_STATE_ACTIVE] = "ACTIVE",
28         [ATSPI_STATE_ARMED] = "ARMED",
29         [ATSPI_STATE_BUSY] =  "BUSY",
30         [ATSPI_STATE_CHECKED] = "CHECKED",
31         [ATSPI_STATE_COLLAPSED] = "COLLAPSED",
32         [ATSPI_STATE_DEFUNCT] = "DEFUNCT",
33         [ATSPI_STATE_EDITABLE] = "EDITABLE",
34         [ATSPI_STATE_ENABLED] = "ENABLED",
35         [ATSPI_STATE_EXPANDABLE] = "EXPANDABLE",
36         [ATSPI_STATE_EXPANDED] = "EXPANDED",
37         [ATSPI_STATE_FOCUSABLE] = "FOCUSABLE",
38         [ATSPI_STATE_FOCUSED] = "FOCUSED",
39         [ATSPI_STATE_HAS_TOOLTIP] = "HAS_TOOLTIP",
40         [ATSPI_STATE_HORIZONTAL] = "HORIZONTAL",
41         [ATSPI_STATE_ICONIFIED] = "ICONIFIED",
42         [ATSPI_STATE_MULTI_LINE] = "MULTI_LINE",
43         [ATSPI_STATE_MULTISELECTABLE] = "MULTISELECTABLE",
44         [ATSPI_STATE_OPAQUE] = "OPAQUE",
45         [ATSPI_STATE_PRESSED] = "PRESSED",
46         [ATSPI_STATE_RESIZABLE] = "RESIZABLE",
47         [ATSPI_STATE_SELECTABLE] = "SELECTABLE",
48         [ATSPI_STATE_SELECTED] = "SELECTED",
49         [ATSPI_STATE_SENSITIVE] = "SENSITIVE",
50         [ATSPI_STATE_SHOWING] = "SHOWING",
51         [ATSPI_STATE_SINGLE_LINE] = "SINGLE_LINE",
52         [ATSPI_STATE_STALE] = "STALE",
53         [ATSPI_STATE_TRANSIENT] = "TRANSIENT",
54         [ATSPI_STATE_VERTICAL] = "VERTICAL",
55         [ATSPI_STATE_VISIBLE] = "VISIBLE",
56         [ATSPI_STATE_MANAGES_DESCENDANTS] = "MANAGES_DESCENDANTS",
57         [ATSPI_STATE_INDETERMINATE] = "INDETERMINATE",
58         [ATSPI_STATE_REQUIRED] = "REQUIRED",
59         [ATSPI_STATE_TRUNCATED] = "TRUNCATED",
60         [ATSPI_STATE_ANIMATED] = "ANIMATED",
61         [ATSPI_STATE_INVALID_ENTRY] = "INVALID_ENTRY",
62         [ATSPI_STATE_SUPPORTS_AUTOCOMPLETION] = "SUPPORTS_AUTOCOMPLETION",
63         [ATSPI_STATE_SELECTABLE_TEXT] = "SELECTABLE_TEXT",
64         [ATSPI_STATE_IS_DEFAULT] = "IS_DEFAULT",
65         [ATSPI_STATE_VISITED] = "VISITED",
66         [ATSPI_STATE_CHECKABLE] = "CHECKABLE",
67         [ATSPI_STATE_MODAL] = "MODAL",
68         [ATSPI_STATE_HIGHLIGHTED] = "HIGHLIGHTED",
69         [ATSPI_STATE_HIGHLIGHTABLE] = "HIGHLIGHTABLE",
70         [ATSPI_STATE_HAS_POPUP] = "HAS_POPUP",
71         [ATSPI_STATE_READ_ONLY] = "READ_ONLY",
72         [ATSPI_STATE_LAST_DEFINED] = "LAST_DEFINED"
73 };
74
75 typedef struct _Box_Size {
76         char *x;
77         char *y;
78         char *height;
79         char *width;
80 } Box_Size;
81
82 char *_strdup(const char *c)
83 {
84         char *result = (char *)calloc(1, strlen(c) + 1);
85         return result ? strcpy(result, c) : result;
86 }
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][flow to node, flow from node]\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         g_array_sort(states, _int_sort_function);
244
245         AtspiStateType state_type;
246         char *state_string = NULL;
247
248         for (int i = 0; states && (i < states->len); i++) {
249                 state_type = g_array_index(states, AtspiStateType, i);
250
251                 char node_state_str[8];
252                 sprintf(node_state_str, "(%d)", state_type);
253                 _combine_strings(&state_string, node_state_str);
254         }
255
256         g_array_free(states, 0);
257         g_object_unref(node_state_set);
258
259         _truncate_string(state_string, length_limit);
260
261         return state_string;
262 }
263
264 static char *_get_attributes(AtspiAccessible *node, int length_limit)
265 {
266         GHashTable *attributes = atspi_accessible_get_attributes(node, NULL);
267
268         if (!attributes)
269                 return NULL;
270
271         GHashTableIter attributes_iter;
272         gpointer attr_key;
273         gpointer attr_value;
274         g_hash_table_iter_init (&attributes_iter, attributes);
275         char *result = NULL;
276         while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
277                 char attributes_string[ATTR_WIDTH];
278                 int ret = snprintf(attributes_string, ATTR_WIDTH, "(%s=%s)", (char *)attr_key, (char *)attr_value);
279                 if (ret >= ATTR_WIDTH)
280                         fprintf(stderr, "\n_get_attributes: generated string is too long. Buffer overflow\n");
281
282                 _combine_strings(&result, attributes_string);
283         }
284         g_hash_table_unref(attributes);
285
286         _truncate_string(result, length_limit);
287         return result;
288 }
289
290 static char *_get_object_in_relation(AtspiAccessible *source, AtspiRelationType search_type)
291 {
292         if (source == NULL)
293                 return "";
294
295         GArray *relations = atspi_accessible_get_relation_set(source, NULL);
296
297         if (relations == NULL)
298                 return "";
299
300         AtspiAccessible *ret = NULL;
301         for (int i = 0; i < relations->len; ++i) {
302                 AtspiRelation *relation = g_array_index(relations, AtspiRelation *, i);
303                 AtspiRelationType type = atspi_relation_get_relation_type(relation);
304
305                 if (type == search_type) {
306                         ret = atspi_relation_get_target(relation, 0);
307                         break;
308                 }
309         }
310
311         g_array_free(relations, TRUE);
312
313         return ret ? atspi_accessible_get_path(ret, NULL) : g_strdup("");
314 }
315
316 static char *_get_info(AtspiAccessible *node, int length_limit)
317 {
318         if (!node)
319                 return NULL;
320
321         char *node_name = atspi_accessible_get_name(node, NULL);
322         char *node_role_name = atspi_accessible_get_role_name(node, NULL);
323         char *path = atspi_accessible_get_path(node, NULL);
324
325         char *attributes = _get_attributes(node, length_limit);
326         Box_Size *box_size = _get_box_size(node);
327         char *states = _get_states(node, length_limit);
328
329         char result[SAFE_BUFFER_SIZE];
330         int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%s,%s]]",
331                                                 path,
332                                                 node_role_name,
333                                                 attributes,
334                                                 box_size->x,
335                                                 box_size->y,
336                                                 box_size->width,
337                                                 box_size->height,
338                                                 node_name,
339                                                 states,
340                                                 _get_object_in_relation(node, ATSPI_RELATION_FLOWS_TO),
341                                                 _get_object_in_relation(node, ATSPI_RELATION_FLOWS_FROM));
342
343         if (ret >= SAFE_BUFFER_SIZE)
344                 fprintf(stderr, "\n%s, %s %s: generated string is too long. Buffer overflow\n", __FILE__, __FUNCTION__, __LINE__);
345
346         free(node_name);
347         free(node_role_name);
348         free(attributes);
349         if (box_size) {
350                 free(box_size->width);
351                 free(box_size->height);
352                 free(box_size);
353         }
354         free(states);
355
356         return _strdup(result);
357 }
358
359 static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
360 {
361         int parent_index = atspi_accessible_get_index_in_parent(obj, NULL);
362         AtspiAccessible *parent = atspi_accessible_get_parent(obj, NULL);
363
364         char output[CHECK_OUTPUT_WIDTH];
365         if (parent_index != parent_candidate_index || parent_candidate != parent) {
366                 char parent_status[NUMBER_WIDTH];
367
368                 if (parent == NULL)
369                         strcpy(parent_status, "_");
370                 else
371                         snprintf(parent_status, NUMBER_WIDTH, "%d", parent_index);
372
373                 snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%s,%s>]", parent_candidate_index, parent_status,
374                                 atspi_accessible_get_path(parent_candidate, NULL),
375                                 atspi_accessible_get_path(parent, NULL));
376
377         } else {
378                 snprintf(output, CHECK_OUTPUT_WIDTH, "[OK]");
379         }
380
381         printf("%-*s\t", CHECK_OUTPUT_WIDTH, output);
382 }
383
384 static int _print_atspi_tree_verify_maybe_r(int indent_number, AtspiAccessible *object, bool check_integrity, int length_limit)
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);
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);
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("-d, --tree-dump\t\tdump tree for selected application\n");
421         printf("-c, --tree-check\tcheck tree for selected application\n");
422         printf("-f, --first-match\tperform dump or check only for first matching application\n");
423         printf("-i, --indent\t\tset indentation size, default value stands at 2\n");
424         printf("-t, --truncate\t\tset maximum single field size, default value stands at 30\n");
425         printf("\nEXAMPLE:\n");
426         printf("\tat_spi2_tool -i3 -d quickpanel\n");
427         printf("\t  show AT-SPI tree for node \"quickpanel\" using three-space indentation\n");
428         printf("\tat_spi2_tool -i1 -c starter\n");
429         printf("\t  show AT-SPI tree with integrity test for node \"starter\" using one-space indentation\n");
430 }
431 void _print_version()
432 {
433         printf("AT-SPI2-CORE-UTIL v%s\n", VERSION);
434 }
435
436 static void _atspi_tree_traverse(AtspiAccessible *desktop, const char *app_name, bool dump, bool check, bool first_match, int length_limit)
437 {
438         int count = atspi_accessible_get_child_count(desktop, NULL);
439         bool app_name_matched = false;
440         for (int i = 0; i < count; i++) {
441                 AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL);
442                 if (child == NULL) {
443                         fprintf(stderr, "\n%s, %s %s: Null child occured. Results may be misleading.\n", __FILE__, __FUNCTION__, __LINE__);
444                         continue;
445                 }
446
447                 char *name = atspi_accessible_get_name(child, NULL);
448
449                 if (!dump && !check)
450                         printf("%s\n", name);
451
452                 if ((check || dump) && name && !strcmp(name, app_name)) {
453                         app_name_matched = true;
454
455                         _print_module_legend();
456
457                         if (check)
458                                 _test_atspi_parent_child_relation(child, desktop, i);
459
460                         _print_atspi_tree_verify_maybe_r(0, child, check, length_limit);
461
462                         if (first_match) {
463                                 free(name);
464                                 break;
465                         } else
466                                 printf("\n");
467
468                         free(name);
469                 }
470         }
471
472         if (!app_name_matched && (dump || check))
473                 fprintf(stderr, "There is no application with name: %s. Try again.\n", app_name);
474 }
475
476 static void _run_command(int argc, char *argv[], AtspiAccessible *desktop)
477 {
478         struct option long_options[] = {
479                 {"help", no_argument, 0, 'h'},
480                 {"version", no_argument, 0, 'v'},
481                 {"show-legend", no_argument, 0, 'g'},
482                 {"list-apps", no_argument, 0, 'l'},
483                 {"tree-dump", required_argument, 0, 'd'},
484                 {"tree-check", required_argument, 0, 'c'},
485                 {"first-match", no_argument, 0, 'f'},
486                 {"indent", required_argument, 0, 'i'},
487                 {"truncate", required_argument, 0, 't'},
488         };
489
490         int command = 0;
491         int option_index = 0;
492         bool traverse_flags[FLAG_NO] = {false};
493         char *app_name = NULL;
494
495         while (TRUE) {
496                 command = getopt_long(argc, argv, "hvgld:c:ft:i:", long_options, &option_index);
497
498                 if (command == ERROR_STATE)
499                         break;
500
501                 switch (command) {
502                 case 'h':
503                         _print_help();
504                         break;
505
506                 case 'v':
507                         _print_version();
508                         break;
509
510                 case 'g':
511                         _print_atspi_states_legend();
512                         break;
513
514                 case 'l':
515                         _atspi_tree_traverse(desktop, NULL, false, false, false, module_name_limit);
516                         break;
517
518                 case 'd':
519                         traverse_flags[DUMP] = true;
520                         app_name = optarg;
521                         break;
522
523                 case 'c':
524                         traverse_flags[CHECK] = true;
525                         app_name = optarg;
526                         break;
527
528                 case 'f':
529                         traverse_flags[FIRST_MATCH] = true;
530                         break;
531
532                 case 'i':
533                         _set_indent(atoi(optarg));
534                         break;
535
536                 case 't':
537                         _set_module_length_limit(atoi(optarg));
538                         break;
539
540                 case '?':
541                         fprintf(stderr, "Invalid parameter. Use: \"--help\"\n");
542                         break;
543
544                 default:
545                         abort();
546                 }
547         }
548
549         if (traverse_flags[DUMP] || traverse_flags[CHECK])
550                 _atspi_tree_traverse(desktop, app_name, traverse_flags[DUMP], traverse_flags[CHECK], traverse_flags[FIRST_MATCH], module_name_limit);
551 }
552
553 int main(int argc, char *argv[])
554 {
555         if (argc < 2) {
556                 printf("Arguments required. Type %s --help.\n", argv[0]);
557                 return -1;
558         }
559
560         int result = atspi_init();
561         if (result != 0)
562         {
563                 fprintf(stderr, "Unable to initilize AT-SPI infrastructure, atspi_init failed\n");
564                 return -1;
565         }
566
567         AtspiAccessible *desktop = atspi_get_desktop(0);
568         if (!desktop) {
569                 fprintf(stderr, "atspi_get_desktop failed\n");
570                 return -1;
571         }
572
573         _run_command(argc, argv, desktop);
574
575         return 0;
576 }