1 #include "atspi/atspi.h"
9 #define SAFE_BUFFER_SIZE 2048
10 #define CHECK_OUTPUT_WIDTH 42
11 #define MINIMAL_MODULE_WIDTH 3
13 #define NUMBER_WIDTH 6
14 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
22 static unsigned indent_width = 2;
23 static int module_name_limit = -1;
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"
75 typedef struct _Box_Size {
82 char *_strdup(const char *c)
84 char *result = (char *)calloc(1, strlen(c) + 1);
85 return result ? strcpy(result, c) : result;
88 static char *_multiply_string(char c, unsigned number)
90 char *result = (char *)calloc(1, number + 1);
95 memset(result, c, number);
100 static void _print_module_legend()
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");
105 static void _print_atspi_states_legend()
107 _print_module_legend();
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]);
127 printf("[%d]\t%s\n", line_idx * COLUMN_NO, atspi_state_names[line_idx * COLUMN_NO]);
132 static void _set_indent(int indent_size)
134 if (indent_size < 0) {
135 fprintf(stderr, "WARNING: Invalid indent size. Default value retained.\n");
139 indent_width = indent_size;
142 static void _set_module_length_limit(int limit)
144 if (limit < MINIMAL_MODULE_WIDTH) {
145 fprintf(stderr, "WARNING: Invalid maximum field size. Default value retained.\n");
149 module_name_limit = limit;
152 static int _int_sort_function(gconstpointer a, gconstpointer b)
154 int int_a = GPOINTER_TO_INT (a);
155 int int_b = GPOINTER_TO_INT (b);
157 return int_a - int_b;
160 static void _truncate_string(char *string, int length_limit)
162 if (length_limit < MINIMAL_MODULE_WIDTH || !string)
165 int len = strlen(string);
167 if (len > length_limit)
168 memcpy(string + length_limit - 3, "...", 4);
171 static size_t _combine_strings(char **destination, const char *source)
173 if (!source || !*source || !destination)
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));
182 fprintf(stderr, "_combine_strings: allocation failed");
186 memcpy(buf + old_len, source, source_len + 1);
193 static Box_Size *_get_box_size(AtspiAccessible *node)
195 Box_Size *box_size = (Box_Size *)malloc(sizeof(Box_Size));
196 if (box_size == NULL)
204 AtspiComponent *component = atspi_accessible_get_component_iface(node);
205 AtspiRect *extent = component ? atspi_component_get_extents(component, ATSPI_COORD_TYPE_SCREEN, NULL) : NULL;
207 if (extent == NULL) {
208 _combine_strings(&x, "_");
209 _combine_strings(&y, "_");
210 _combine_strings(&width, "_");
211 _combine_strings(&height, "_");
213 char temp_buff[NUMBER_WIDTH];
215 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->x);
216 _combine_strings(&x, temp_buff);
218 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->y);
219 _combine_strings(&y, temp_buff);
221 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->width);
222 _combine_strings(&width, temp_buff);
224 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->height);
225 _combine_strings(&height, temp_buff);
227 g_object_unref(component);
233 box_size->width = width;
234 box_size->height = height;
239 static char *_get_states(AtspiAccessible *node, int length_limit)
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);
245 AtspiStateType state_type;
246 char *state_string = NULL;
248 for (int i = 0; states && (i < states->len); i++) {
249 state_type = g_array_index(states, AtspiStateType, i);
251 char node_state_str[8];
252 sprintf(node_state_str, "(%d)", state_type);
253 _combine_strings(&state_string, node_state_str);
256 g_array_free(states, 0);
257 g_object_unref(node_state_set);
259 _truncate_string(state_string, length_limit);
264 static char *_get_attributes(AtspiAccessible *node, int length_limit)
266 GHashTable *attributes = atspi_accessible_get_attributes(node, NULL);
271 GHashTableIter attributes_iter;
274 g_hash_table_iter_init (&attributes_iter, attributes);
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");
282 _combine_strings(&result, attributes_string);
284 g_hash_table_unref(attributes);
286 _truncate_string(result, length_limit);
290 static AtspiAccessible *_get_object_in_relation(AtspiAccessible *source, AtspiRelationType search_type)
295 GArray *relations = atspi_accessible_get_relation_set(source, NULL);
297 if (relations == NULL)
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);
305 if (type == search_type) {
306 ret = atspi_relation_get_target(relation, 0);
311 g_array_free(relations, TRUE);
316 static char *_get_info(AtspiAccessible *node, int length_limit)
321 char *node_name = atspi_accessible_get_name(node, NULL);
322 char *node_role_name = atspi_accessible_get_role_name(node, NULL);
324 char *attributes = _get_attributes(node, length_limit);
325 Box_Size *box_size = _get_box_size(node);
326 char *states = _get_states(node, length_limit);
328 char result[SAFE_BUFFER_SIZE];
329 int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%p],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%p,%p]]",
339 _get_object_in_relation(node, ATSPI_RELATION_FLOWS_TO),
340 _get_object_in_relation(node, ATSPI_RELATION_FLOWS_FROM));
342 if (ret >= SAFE_BUFFER_SIZE)
343 fprintf(stderr, "\n_get_info: generated string is too long. Buffer overflow\n");
346 free(node_role_name);
349 free(box_size->width);
350 free(box_size->height);
355 return _strdup(result);
358 static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
360 int parent_index = atspi_accessible_get_index_in_parent(obj, NULL);
361 AtspiAccessible *parent = atspi_accessible_get_parent(obj, NULL);
363 char output[CHECK_OUTPUT_WIDTH];
364 if (parent_index != parent_candidate_index || parent_candidate != parent) {
365 char parent_status[NUMBER_WIDTH];
368 strcpy(parent_status, "_");
370 snprintf(parent_status, NUMBER_WIDTH, "%d", parent_index);
372 snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%p,%p>]", parent_candidate_index, parent_status, parent_candidate, parent);
375 snprintf(output, CHECK_OUTPUT_WIDTH, "[OK]");
378 printf("%-*s\t", CHECK_OUTPUT_WIDTH, output);
381 static int _print_atspi_tree_verify_maybe_r(int indent_number, AtspiAccessible *object, bool check_integrity, int length_limit)
383 char *indent = _multiply_string(' ', indent_number*indent_width);
384 if (indent != NULL) {
385 printf("%s", indent);
389 char *node_info = _get_info(object, length_limit);
390 if (node_info != NULL) {
391 printf("%s\n", node_info);
395 int count = atspi_accessible_get_child_count(object, NULL);
396 for (int i = 0; i < count; i++) {
397 AtspiAccessible *child = atspi_accessible_get_child_at_index(object, i, NULL);
400 _test_atspi_parent_child_relation(child, object, i);
402 _print_atspi_tree_verify_maybe_r(indent_number + 1, child, check_integrity, length_limit);
410 printf("AT-SPI2-CORE-UTIL\n\n");
411 printf("USAGE: at_spi2_tool [OPTION] [PARAMETER] ...\n\n");
412 printf("OPTION LIST:\n");
413 printf("-h, --help\t\tshow this message\n");
414 printf("-v, --version\t\tshow actual version of tool\n");
415 printf("-g, --show-legend\tprint AT-SPI state legend\n");
416 printf("-l, --list-apps\t\tlist all applications of desktop\n");
417 printf("-d, --tree-dump\t\tdump tree for selected application\n");
418 printf("-c, --tree-check\tcheck tree for selected application\n");
419 printf("-f, --first-match\tperform dump or check only for first matching application\n");
420 printf("-i, --indent\t\tset indentation size, default value stands at 2\n");
421 printf("-t, --truncate\t\tset maximum single field size, default value stands at 30\n");
422 printf("\nEXAMPLE:\n");
423 printf("\tat_spi2_tool -i3 -d quickpanel\n");
424 printf("\t show AT-SPI tree for node \"quickpanel\" using three-space indentation\n");
425 printf("\tat_spi2_tool -i1 -c starter\n");
426 printf("\t show AT-SPI tree with integrity test for node \"starter\" using one-space indentation\n");
428 void _print_version()
430 printf("AT-SPI2-CORE-UTIL v%s\n", VERSION);
433 static void _atspi_tree_traverse(AtspiAccessible *desktop, const char *app_name, bool dump, bool check, bool first_match, int length_limit)
435 int count = atspi_accessible_get_child_count(desktop, NULL);
436 bool app_name_matched = false;
437 for (int i = 0; i < count; i++) {
438 AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL);
442 char *name = atspi_accessible_get_name(child, NULL);
445 printf("%s\n", name);
447 if ((check || dump) && name && !strcmp(name, app_name)) {
448 app_name_matched = true;
450 _print_module_legend();
453 _test_atspi_parent_child_relation(child, desktop, i);
455 _print_atspi_tree_verify_maybe_r(0, child, check, length_limit);
467 if (!app_name_matched && (dump || check))
468 fprintf(stderr, "There is no application with name: %s. Try again.\n", app_name);
471 static void _run_command(int argc, char *argv[], AtspiAccessible *desktop)
473 struct option long_options[] = {
474 {"help", no_argument, 0, 'h'},
475 {"version", no_argument, 0, 'v'},
476 {"show-legend", no_argument, 0, 'g'},
477 {"list-apps", no_argument, 0, 'l'},
478 {"tree-dump", required_argument, 0, 'd'},
479 {"tree-check", required_argument, 0, 'c'},
480 {"first-match", no_argument, 0, 'f'},
481 {"indent", required_argument, 0, 'i'},
482 {"truncate", required_argument, 0, 't'},
486 int option_index = 0;
487 bool traverse_flags[FLAG_NO] = {false};
488 char *app_name = NULL;
491 command = getopt_long(argc, argv, "hvgld:c:ft:i:", long_options, &option_index);
493 if (command == ERROR_STATE)
506 _print_atspi_states_legend();
510 _atspi_tree_traverse(desktop, NULL, false, false, false, module_name_limit);
514 traverse_flags[DUMP] = true;
519 traverse_flags[CHECK] = true;
524 traverse_flags[FIRST_MATCH] = true;
528 _set_indent(atoi(optarg));
532 _set_module_length_limit(atoi(optarg));
536 fprintf(stderr, "Invalid parameter. Use: \"--help\"\n");
544 if (traverse_flags[DUMP] || traverse_flags[CHECK])
545 _atspi_tree_traverse(desktop, app_name, traverse_flags[DUMP], traverse_flags[CHECK], traverse_flags[FIRST_MATCH], module_name_limit);
548 int main(int argc, char *argv[])
551 printf("Arguments required. Type %s --help.\n", argv[0]);
555 int result = atspi_init();
558 fprintf(stderr, "Unable to initilize AT-SPI infrastructure, atspi_init failed\n");
562 AtspiAccessible *desktop = atspi_get_desktop(0);
564 fprintf(stderr, "atspi_get_desktop failed\n");
568 _run_command(argc, argv, desktop);