1a951da14c941aa38137c67f9f062963d4e6450f
[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.02"
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         int height;
77         int 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         box_size->width = -1;
198         box_size->height = -1;
199
200         AtspiComponent *component = atspi_accessible_get_component_iface(node);
201
202         if (component == NULL)
203                 return box_size;
204
205         AtspiRect *extent = atspi_component_get_extents(component, ATSPI_COORD_TYPE_SCREEN, NULL);
206         if (extent == NULL)
207                 return box_size;
208
209         box_size->width = extent->width;
210         box_size->height = extent->height;
211
212         g_object_unref(component);
213         g_free(extent);
214
215         return box_size;
216 }
217
218 static char *_get_states(AtspiAccessible *node)
219 {
220         AtspiStateSet *node_state_set = atspi_accessible_get_state_set(node);
221         GArray *states = atspi_state_set_get_states(node_state_set);
222         g_array_sort(states, _int_sort_function);
223
224         AtspiStateType state_type;
225         char *state_string = NULL;
226
227         for (int i = 0; states && (i < states->len); i++) {
228                 state_type = g_array_index(states, AtspiStateType, i);
229
230                 char node_state_str[8];
231                 sprintf(node_state_str, "(%d)", state_type);
232                 _combine_strings(&state_string, node_state_str);
233         }
234
235         g_array_free(states, 0);
236         g_object_unref(node_state_set);
237
238         return state_string;
239 }
240
241 static char *_get_attributes(AtspiAccessible *node)
242 {
243         GHashTable *attributes = NULL;
244         attributes = atspi_accessible_get_attributes(node, NULL);
245
246         if (!attributes)
247                 return NULL;
248
249         GHashTableIter attributes_iter;
250         gpointer attr_key;
251         gpointer attr_value;
252         g_hash_table_iter_init (&attributes_iter, attributes);
253         char *result = NULL;
254         while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
255                 char attributes_string[ATTR_WIDTH];
256                 int ret = snprintf(attributes_string, ATTR_WIDTH, "(%s=%s)", (char*)attr_key, (char*)attr_value);
257                 if (ret >= ATTR_WIDTH) {
258                         fprintf(stderr, "_get_attributes: generated string is too long. Buffer overflow");
259                         abort();
260                 }
261                 _combine_strings(&result, attributes_string);
262         }
263         g_hash_table_unref(attributes);
264
265         return result;
266 }
267
268 static char *_get_info(AtspiAccessible *node, int length_limit)
269 {
270         if (!node)
271                 return NULL;
272
273         char *node_name = atspi_accessible_get_name(node, NULL);
274         char *node_role_name = atspi_accessible_get_role_name(node, NULL);
275
276         char *attributes = _get_attributes(node);
277         _truncate_string(attributes, length_limit);
278         Box_Size *box_size = _get_box_size(node);
279         char *states = _get_states(node);
280         _truncate_string(states, length_limit);
281
282         char result[SAFE_BUFFER_SIZE];
283         int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%d,%d],[%s],[%s]]",
284                                                                                 node_role_name,
285                                                                                 attributes,
286                                                                                 box_size->width,
287                                                                                 box_size->height,
288                                                                                 node_name,
289                                                                                 states);
290
291         if (ret >= SAFE_BUFFER_SIZE) {
292                 fprintf(stderr, "_get_info: generated string is too long. Buffer overflow");
293                 abort();
294         }
295
296         free(node_name);
297         free(node_role_name);
298         free(attributes);
299         free(box_size);
300         free(states);
301
302         return _strdup(result);
303 }
304
305 static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
306 {
307         int parent_index = atspi_accessible_get_index_in_parent(obj, NULL);
308         AtspiAccessible *parent = atspi_accessible_get_parent(obj, NULL);
309
310         char output[CHECK_OUTPUT_WIDTH];
311         if (parent_index != parent_candidate_index || parent_candidate != parent) {
312                 char parent_status[NUMBER_WIDTH];
313
314                 if (parent == NULL)
315                         strcpy(parent_status, "_");
316                 else
317                         snprintf(parent_status, NUMBER_WIDTH, "%d", parent_index);
318
319                 snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%p,%p>]", parent_candidate_index, parent_status, parent_candidate, parent);
320
321         } else {
322                 snprintf(output, CHECK_OUTPUT_WIDTH, "[OK]");
323         }
324
325         printf("%-*s\t", CHECK_OUTPUT_WIDTH, output);
326 }
327
328 static int _print_atspi_tree_verify_maybe_r(int indent_number, AtspiAccessible *object, bool check_integrity, int length_limit)
329 {
330         char *indent = _multiply_string(' ', indent_number*indent_width);
331         if (indent != NULL) {
332                 printf("%s", indent);
333                 free(indent);
334         }
335
336         char *node_info = _get_info(object, length_limit);
337         if (node_info != NULL) {
338                 printf("%s\n", node_info);
339                 free(node_info);
340         }
341
342         int count = atspi_accessible_get_child_count(object, NULL);
343         for (int i = 0; i < count; i++) {
344                 AtspiAccessible *child = atspi_accessible_get_child_at_index(object, i, NULL);
345                 if (child) {
346                         if (check_integrity)
347                                 _test_atspi_parent_child_relation(child, object, i);
348
349                         _print_atspi_tree_verify_maybe_r(indent_number + 1, child, check_integrity, length_limit);
350                 }
351         }
352         return 0;
353 }
354
355 void _print_help()
356 {
357         printf("AT-SPI2-CORE-UTIL\n\n");
358         printf("USAGE: at_spi2_tool [OPTION] [PARAMETER] ...\n\n");
359         printf("OPTION LIST:\n");
360         printf("-h, --help\t\tshow this message\n");
361         printf("-v, --version\t\tshow actual version of tool\n");
362         printf("-g, --show-legend\tprint AT-SPI state legend\n");
363         printf("-l, --list-apps\t\tlist all applications of desktop\n");
364         printf("-d, --tree-dump\t\tdump tree for selected application\n");
365         printf("-c, --tree-check\tcheck tree for selected application\n");
366         printf("-f, --first-match\tperform dump or check only for first matching application\n");
367         printf("-i, --indent\t\tset indentation size, default value stands at 2\n");
368         printf("-t, --truncate\t\tset maximum single field size, default value stands at 30\n");
369         printf("\nEXAMPLE:\n");
370         printf("\tat_spi2_tool -i3 -d quickpanel\n");
371         printf("\t  show AT-SPI tree for node \"quickpanel\" using three-space indentation\n");
372         printf("\tat_spi2_tool -i1 -c starter\n");
373         printf("\t  show AT-SPI tree with integrity test for node \"starter\" using one-space indentation\n");
374 }
375 void _print_version()
376 {
377         printf("AT-SPI2-CORE-UTIL v%s\n", VERSION);
378 }
379
380 static void _atspi_tree_traverse(AtspiAccessible *desktop, const char *app_name, bool dump, bool check, bool first_match, int length_limit)
381 {
382         int count = atspi_accessible_get_child_count(desktop, NULL);
383         bool app_name_matched = false;
384         for (int i = 0; i < count; i++) {
385                 AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL);
386                 if (child) {
387                         char *name = atspi_accessible_get_name(child, NULL);
388
389                         if (!dump && !check) {
390                                 printf("%s\n", name);
391                         }
392
393                         if ((check || dump) && name && !strcmp(name, app_name)) {
394                                 app_name_matched = true;
395
396                                 _print_module_legend();
397
398                                 if (check)
399                                         _test_atspi_parent_child_relation(child, desktop, i);
400
401                                 _print_atspi_tree_verify_maybe_r(0, child, check, length_limit);
402
403                                 if (first_match) {
404                                         free(name);
405                                         break;
406                                 } else
407                                         printf("\n");
408                         }
409                         free(name);
410                 }
411         }
412
413         if (!app_name_matched && (dump || check))
414                 fprintf(stderr, "There is no application with name: %s. Try again.\n", app_name);
415 }
416
417 static void _run_command(int argc, char *argv[], AtspiAccessible *desktop)
418 {
419         struct option long_options[] = {
420                 {"help", no_argument, 0, 'h'},
421                 {"version", no_argument, 0, 'v'},
422                 {"show-legend", no_argument, 0, 'g'},
423                 {"list-apps", no_argument, 0, 'l'},
424                 {"tree-dump", required_argument, 0, 'd'},
425                 {"tree-check", required_argument, 0, 'c'},
426                 {"first-match", no_argument, 0, 'f'},
427                 {"indent", required_argument, 0, 'i'},
428                 {"truncate", required_argument, 0, 't'},
429         };
430
431         int command = 0;
432         int option_index = 0;
433         bool traverse_flags[FLAG_NO] = {false};
434         char *app_name = NULL;
435
436         while (TRUE) {
437                 command = getopt_long(argc, argv, "hvgld:c:ft:i:", long_options, &option_index);
438
439                 if (command == ERROR_STATE)
440                         break;
441
442                 switch (command) {
443                 case 'h':
444                         _print_help();
445                         break;
446
447                 case 'v':
448                         _print_version();
449                         break;
450
451                 case 'g':
452                         _print_atspi_states_legend();
453                         break;
454
455                 case 'l':
456                         _atspi_tree_traverse(desktop, NULL, false, false, false, module_name_limit);
457                         break;
458
459                 case 'd':
460                         traverse_flags[DUMP] = true;
461                         app_name = optarg;
462                         break;
463
464                 case 'c':
465                         traverse_flags[CHECK] = true;
466                         app_name = optarg;
467                         break;
468
469                 case 'f':
470                         traverse_flags[FIRST_MATCH] = true;
471                         break;
472
473                 case 'i':
474                         _set_indent(atoi(optarg));
475                         break;
476
477                 case 't':
478                         _set_module_length_limit(atoi(optarg));
479                         break;
480
481                 case '?':
482                         fprintf(stderr, "Invalid parameter. Use: \"--help\"\n");
483                         break;
484
485                 default:
486                         abort();
487                 }
488         }
489
490         if (traverse_flags[DUMP] || traverse_flags[CHECK])
491                 _atspi_tree_traverse(desktop, app_name, traverse_flags[DUMP], traverse_flags[CHECK], traverse_flags[FIRST_MATCH], module_name_limit);
492 }
493
494 int main(int argc, char *argv[])
495 {
496         if (argc < 2) {
497                 printf("Arguments required. Type %s --help.\n", argv[0]);
498                 return -1;
499         }
500
501         int result = atspi_init();
502         if (result != 0)
503         {
504                 fprintf(stderr, "Unable to initilize AT-SPI infrastructure, atspi_init failed\n");
505                 return -1;
506         }
507
508         AtspiAccessible *desktop = atspi_get_desktop(0);
509         if (!desktop) {
510                 fprintf(stderr, "atspi_get_desktop failed\n");
511                 return -1;
512         }
513
514         _run_command(argc, argv, desktop);
515
516         return 0;
517 }