b47d432a9342ebdfbdc31559ff58d0bd1f4dce77
[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 255
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 *height;
77         char *width;
78 } Box_Size;
79
80 char *_strdup(const char *c)
81 {
82         char *result = (char *)calloc(1, strlen(c) + 1);
83         return result ? strcpy(result, c) : result;
84 }
85
86 static char *_multiply_string(char c, unsigned number)
87 {
88         char *result = (char *)calloc(1, number + 1);
89
90         if (result == NULL)
91                 return result;
92
93         memset(result, c, number);
94
95         return result;
96 }
97
98 static void _print_module_legend()
99 {
100         printf("\n[[node role name],[attributes list],[width,height],[node name],[states list]]\n\n");
101 }
102
103 static void _print_atspi_states_legend()
104 {
105         _print_module_legend();
106
107         int array_len = ARRAY_SIZE(atspi_state_names);
108         int line_count = (array_len + COLUMN_NO - 1)/COLUMN_NO;
109         for (int line_idx = 0; line_idx < line_count; line_idx++) {
110                 if ((line_idx < line_count - 1) || (array_len % COLUMN_NO == 0)) {
111                         printf("[%d]\t%-24s[%d]\t%-24s[%d]\t%s\n",
112                                         line_idx * COLUMN_NO,
113                                         atspi_state_names[line_idx * COLUMN_NO],
114                                         (line_idx * COLUMN_NO) + 1,
115                                         atspi_state_names[(line_idx * COLUMN_NO) + 1],
116                                         (line_idx * COLUMN_NO) + 2,
117                                         atspi_state_names[(line_idx * COLUMN_NO) + 2]);
118                 } else if (array_len % COLUMN_NO == 2) {
119                         printf("[%d]\t%-24s[%d]\t%s\n",
120                                         line_idx * COLUMN_NO,
121                                         atspi_state_names[line_idx * COLUMN_NO],
122                                         (line_idx * COLUMN_NO) + 1,
123                                         atspi_state_names[(line_idx * COLUMN_NO) + 1]);
124                 } else {
125                         printf("[%d]\t%s\n", line_idx * COLUMN_NO, atspi_state_names[line_idx * COLUMN_NO]);
126                 }
127         }
128 }
129
130 static void _set_indent(int indent_size)
131 {
132         if (indent_size < 0) {
133                 fprintf(stderr, "WARNING: Invalid indent size. Default value retained.\n");
134                 return;
135         }
136
137         indent_width = indent_size;
138 }
139
140 static void _set_module_length_limit(int limit)
141 {
142         if (limit < MINIMAL_MODULE_WIDTH) {
143                 fprintf(stderr, "WARNING: Invalid maximum field size. Default value retained.\n");
144                 return;
145         }
146
147         module_name_limit = limit;
148 }
149
150 static int _int_sort_function(gconstpointer a, gconstpointer b)
151 {
152         int int_a = GPOINTER_TO_INT (a);
153         int int_b = GPOINTER_TO_INT (b);
154
155         return int_a - int_b;
156 }
157
158 static void _truncate_string(char *string, int length_limit)
159 {
160         if (length_limit < MINIMAL_MODULE_WIDTH || !string)
161                 return;
162
163         int len = strlen(string);
164
165         if (len > length_limit)
166                 memcpy(string + length_limit - 3, "...", 4);
167 }
168
169 static size_t _combine_strings(char **destination, const char *source)
170 {
171         if (!source || !*source || !destination)
172                 return 0;
173
174         size_t old_len = *destination ? strlen(*destination) : 0;
175         size_t source_len = strlen(source);
176         size_t len = old_len + source_len;
177         char *buf = realloc(*destination, (len + 1) * sizeof(char));
178
179         if (!buf) {
180                 fprintf(stderr, "_combine_strings: allocation failed");
181                 return old_len;
182         }
183
184         memcpy(buf + old_len, source, source_len + 1);
185
186         *destination = buf;
187
188         return len;
189 }
190
191 static Box_Size *_get_box_size(AtspiAccessible *node)
192 {
193         Box_Size *box_size = (Box_Size *)malloc(sizeof(Box_Size));
194         if (box_size == NULL)
195                 return NULL;
196
197         char *width = NULL;
198         char *height = NULL;
199
200         _combine_strings(&width, "_");
201         _combine_strings(&height, "_");
202
203         box_size->width = width;
204         box_size->height = height;
205
206         AtspiComponent *component = atspi_accessible_get_component_iface(node);
207
208         if (component == NULL)
209                 return box_size;
210
211         AtspiRect *extent = atspi_component_get_extents(component, ATSPI_COORD_TYPE_SCREEN, NULL);
212         if (extent == NULL)
213                 return box_size;
214
215         *width = '\0';
216         *height = '\0';
217
218         char temp_buff[NUMBER_WIDTH];
219
220         snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->width);
221         _combine_strings(&width, temp_buff);
222
223         snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->height);
224         _combine_strings(&height, temp_buff);
225
226         g_object_unref(component);
227         g_free(extent);
228
229         box_size->width = width;
230         box_size->height = height;
231
232         return box_size;
233 }
234
235 static char *_get_states(AtspiAccessible *node)
236 {
237         AtspiStateSet *node_state_set = atspi_accessible_get_state_set(node);
238         GArray *states = atspi_state_set_get_states(node_state_set);
239         g_array_sort(states, _int_sort_function);
240
241         AtspiStateType state_type;
242         char *state_string = NULL;
243
244         for (int i = 0; states && (i < states->len); i++) {
245                 state_type = g_array_index(states, AtspiStateType, i);
246
247                 char node_state_str[8];
248                 sprintf(node_state_str, "(%d)", state_type);
249                 _combine_strings(&state_string, node_state_str);
250         }
251
252         g_array_free(states, 0);
253         g_object_unref(node_state_set);
254
255         return state_string;
256 }
257
258 static char *_get_attributes(AtspiAccessible *node)
259 {
260         GHashTable *attributes = NULL;
261         attributes = atspi_accessible_get_attributes(node, NULL);
262
263         if (!attributes)
264                 return NULL;
265
266         GHashTableIter attributes_iter;
267         gpointer attr_key;
268         gpointer attr_value;
269         g_hash_table_iter_init (&attributes_iter, attributes);
270         char *result = NULL;
271         while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
272                 char attributes_string[ATTR_WIDTH];
273                 int ret = snprintf(attributes_string, ATTR_WIDTH, "(%s=%s)", (char*)attr_key, (char*)attr_value);
274                 if (ret >= ATTR_WIDTH) {
275                         fprintf(stderr, "_get_attributes: generated string is too long. Buffer overflow");
276                         abort();
277                 }
278                 _combine_strings(&result, attributes_string);
279         }
280         g_hash_table_unref(attributes);
281
282         return result;
283 }
284
285 static char *_get_info(AtspiAccessible *node, int length_limit)
286 {
287         if (!node)
288                 return NULL;
289
290         char *node_name = atspi_accessible_get_name(node, NULL);
291         char *node_role_name = atspi_accessible_get_role_name(node, NULL);
292
293         char *attributes = _get_attributes(node);
294         _truncate_string(attributes, length_limit);
295         Box_Size *box_size = _get_box_size(node);
296         char *states = _get_states(node);
297         _truncate_string(states, length_limit);
298
299         char result[SAFE_BUFFER_SIZE];
300         int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%s,%s],[%s],[%s]]",
301                                                                                 node_role_name,
302                                                                                 attributes,
303                                                                                 box_size->width,
304                                                                                 box_size->height,
305                                                                                 node_name,
306                                                                                 states);
307
308         if (ret >= SAFE_BUFFER_SIZE) {
309                 fprintf(stderr, "_get_info: generated string is too long. Buffer overflow");
310                 abort();
311         }
312
313         free(node_name);
314         free(node_role_name);
315         free(attributes);
316         if (box_size) {
317                 free(box_size->width);
318                 free(box_size->height);
319                 free(box_size);
320         }
321         free(states);
322
323         return _strdup(result);
324 }
325
326 static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
327 {
328         int parent_index = atspi_accessible_get_index_in_parent(obj, NULL);
329         AtspiAccessible *parent = atspi_accessible_get_parent(obj, NULL);
330
331         char output[CHECK_OUTPUT_WIDTH];
332         if (parent_index != parent_candidate_index || parent_candidate != parent) {
333                 char parent_status[NUMBER_WIDTH];
334
335                 if (parent == NULL)
336                         strcpy(parent_status, "_");
337                 else
338                         snprintf(parent_status, NUMBER_WIDTH, "%d", parent_index);
339
340                 snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%p,%p>]", parent_candidate_index, parent_status, parent_candidate, parent);
341
342         } else {
343                 snprintf(output, CHECK_OUTPUT_WIDTH, "[OK]");
344         }
345
346         printf("%-*s\t", CHECK_OUTPUT_WIDTH, output);
347 }
348
349 static int _print_atspi_tree_verify_maybe_r(int indent_number, AtspiAccessible *object, bool check_integrity, int length_limit)
350 {
351         char *indent = _multiply_string(' ', indent_number*indent_width);
352         if (indent != NULL) {
353                 printf("%s", indent);
354                 free(indent);
355         }
356
357         char *node_info = _get_info(object, length_limit);
358         if (node_info != NULL) {
359                 printf("%s\n", node_info);
360                 free(node_info);
361         }
362
363         int count = atspi_accessible_get_child_count(object, NULL);
364         for (int i = 0; i < count; i++) {
365                 AtspiAccessible *child = atspi_accessible_get_child_at_index(object, i, NULL);
366                 if (child) {
367                         if (check_integrity)
368                                 _test_atspi_parent_child_relation(child, object, i);
369
370                         _print_atspi_tree_verify_maybe_r(indent_number + 1, child, check_integrity, length_limit);
371                 }
372         }
373         return 0;
374 }
375
376 void _print_help()
377 {
378         printf("AT-SPI2-CORE-UTIL\n\n");
379         printf("USAGE: at_spi2_tool [OPTION] [PARAMETER] ...\n\n");
380         printf("OPTION LIST:\n");
381         printf("-h, --help\t\tshow this message\n");
382         printf("-v, --version\t\tshow actual version of tool\n");
383         printf("-g, --show-legend\tprint AT-SPI state legend\n");
384         printf("-l, --list-apps\t\tlist all applications of desktop\n");
385         printf("-d, --tree-dump\t\tdump tree for selected application\n");
386         printf("-c, --tree-check\tcheck tree for selected application\n");
387         printf("-f, --first-match\tperform dump or check only for first matching application\n");
388         printf("-i, --indent\t\tset indentation size, default value stands at 2\n");
389         printf("-t, --truncate\t\tset maximum single field size, default value stands at 30\n");
390         printf("\nEXAMPLE:\n");
391         printf("\tat_spi2_tool -i3 -d quickpanel\n");
392         printf("\t  show AT-SPI tree for node \"quickpanel\" using three-space indentation\n");
393         printf("\tat_spi2_tool -i1 -c starter\n");
394         printf("\t  show AT-SPI tree with integrity test for node \"starter\" using one-space indentation\n");
395 }
396 void _print_version()
397 {
398         printf("AT-SPI2-CORE-UTIL v%s\n", VERSION);
399 }
400
401 static void _atspi_tree_traverse(AtspiAccessible *desktop, const char *app_name, bool dump, bool check, bool first_match, int length_limit)
402 {
403         int count = atspi_accessible_get_child_count(desktop, NULL);
404         bool app_name_matched = false;
405         for (int i = 0; i < count; i++) {
406                 AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL);
407                 if (child) {
408                         char *name = atspi_accessible_get_name(child, NULL);
409
410                         if (!dump && !check) {
411                                 printf("%s\n", name);
412                         }
413
414                         if ((check || dump) && name && !strcmp(name, app_name)) {
415                                 app_name_matched = true;
416
417                                 _print_module_legend();
418
419                                 if (check)
420                                         _test_atspi_parent_child_relation(child, desktop, i);
421
422                                 _print_atspi_tree_verify_maybe_r(0, child, check, length_limit);
423
424                                 if (first_match) {
425                                         free(name);
426                                         break;
427                                 } else
428                                         printf("\n");
429                         }
430                         free(name);
431                 }
432         }
433
434         if (!app_name_matched && (dump || check))
435                 fprintf(stderr, "There is no application with name: %s. Try again.\n", app_name);
436 }
437
438 static void _run_command(int argc, char *argv[], AtspiAccessible *desktop)
439 {
440         struct option long_options[] = {
441                 {"help", no_argument, 0, 'h'},
442                 {"version", no_argument, 0, 'v'},
443                 {"show-legend", no_argument, 0, 'g'},
444                 {"list-apps", no_argument, 0, 'l'},
445                 {"tree-dump", required_argument, 0, 'd'},
446                 {"tree-check", required_argument, 0, 'c'},
447                 {"first-match", no_argument, 0, 'f'},
448                 {"indent", required_argument, 0, 'i'},
449                 {"truncate", required_argument, 0, 't'},
450         };
451
452         int command = 0;
453         int option_index = 0;
454         bool traverse_flags[FLAG_NO] = {false};
455         char *app_name = NULL;
456
457         while (TRUE) {
458                 command = getopt_long(argc, argv, "hvgld:c:ft:i:", long_options, &option_index);
459
460                 if (command == ERROR_STATE)
461                         break;
462
463                 switch (command) {
464                 case 'h':
465                         _print_help();
466                         break;
467
468                 case 'v':
469                         _print_version();
470                         break;
471
472                 case 'g':
473                         _print_atspi_states_legend();
474                         break;
475
476                 case 'l':
477                         _atspi_tree_traverse(desktop, NULL, false, false, false, module_name_limit);
478                         break;
479
480                 case 'd':
481                         traverse_flags[DUMP] = true;
482                         app_name = optarg;
483                         break;
484
485                 case 'c':
486                         traverse_flags[CHECK] = true;
487                         app_name = optarg;
488                         break;
489
490                 case 'f':
491                         traverse_flags[FIRST_MATCH] = true;
492                         break;
493
494                 case 'i':
495                         _set_indent(atoi(optarg));
496                         break;
497
498                 case 't':
499                         _set_module_length_limit(atoi(optarg));
500                         break;
501
502                 case '?':
503                         fprintf(stderr, "Invalid parameter. Use: \"--help\"\n");
504                         break;
505
506                 default:
507                         abort();
508                 }
509         }
510
511         if (traverse_flags[DUMP] || traverse_flags[CHECK])
512                 _atspi_tree_traverse(desktop, app_name, traverse_flags[DUMP], traverse_flags[CHECK], traverse_flags[FIRST_MATCH], module_name_limit);
513 }
514
515 int main(int argc, char *argv[])
516 {
517         if (argc < 2) {
518                 printf("Arguments required. Type %s --help.\n", argv[0]);
519                 return -1;
520         }
521
522         int result = atspi_init();
523         if (result != 0)
524         {
525                 fprintf(stderr, "Unable to initilize AT-SPI infrastructure, atspi_init failed\n");
526                 return -1;
527         }
528
529         AtspiAccessible *desktop = atspi_get_desktop(0);
530         if (!desktop) {
531                 fprintf(stderr, "atspi_get_desktop failed\n");
532                 return -1;
533         }
534
535         _run_command(argc, argv, desktop);
536
537         return 0;
538 }