1 #include "atspi/atspi.h"
10 #define ERROR_STATE -1
11 #define SAFE_BUFFER_SIZE 2048
12 #define CHECK_OUTPUT_WIDTH 42
13 #define MINIMAL_MODULE_WIDTH 3
15 #define NUMBER_WIDTH 6
16 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
22 #define RELATION_TABLE_COLUMN_COUNT 7
23 #define RELATION_COLUMN_WIDTH 20
26 static unsigned indent_width = 2;
27 static int module_name_limit = -1;
29 static const char* atspi_state_names[] = {
30 [ATSPI_STATE_INVALID] = "INVALID",
31 [ATSPI_STATE_ACTIVE] = "ACTIVE",
32 [ATSPI_STATE_ARMED] = "ARMED",
33 [ATSPI_STATE_BUSY] = "BUSY",
34 [ATSPI_STATE_CHECKED] = "CHECKED",
35 [ATSPI_STATE_COLLAPSED] = "COLLAPSED",
36 [ATSPI_STATE_DEFUNCT] = "DEFUNCT",
37 [ATSPI_STATE_EDITABLE] = "EDITABLE",
38 [ATSPI_STATE_ENABLED] = "ENABLED",
39 [ATSPI_STATE_EXPANDABLE] = "EXPANDABLE",
40 [ATSPI_STATE_EXPANDED] = "EXPANDED",
41 [ATSPI_STATE_FOCUSABLE] = "FOCUSABLE",
42 [ATSPI_STATE_FOCUSED] = "FOCUSED",
43 [ATSPI_STATE_HAS_TOOLTIP] = "HAS_TOOLTIP",
44 [ATSPI_STATE_HORIZONTAL] = "HORIZONTAL",
45 [ATSPI_STATE_ICONIFIED] = "ICONIFIED",
46 [ATSPI_STATE_MULTI_LINE] = "MULTI_LINE",
47 [ATSPI_STATE_MULTISELECTABLE] = "MULTISELECTABLE",
48 [ATSPI_STATE_OPAQUE] = "OPAQUE",
49 [ATSPI_STATE_PRESSED] = "PRESSED",
50 [ATSPI_STATE_RESIZABLE] = "RESIZABLE",
51 [ATSPI_STATE_SELECTABLE] = "SELECTABLE",
52 [ATSPI_STATE_SELECTED] = "SELECTED",
53 [ATSPI_STATE_SENSITIVE] = "SENSITIVE",
54 [ATSPI_STATE_SHOWING] = "SHOWING",
55 [ATSPI_STATE_SINGLE_LINE] = "SINGLE_LINE",
56 [ATSPI_STATE_STALE] = "STALE",
57 [ATSPI_STATE_TRANSIENT] = "TRANSIENT",
58 [ATSPI_STATE_VERTICAL] = "VERTICAL",
59 [ATSPI_STATE_VISIBLE] = "VISIBLE",
60 [ATSPI_STATE_MANAGES_DESCENDANTS] = "MANAGES_DESCENDANTS",
61 [ATSPI_STATE_INDETERMINATE] = "INDETERMINATE",
62 [ATSPI_STATE_REQUIRED] = "REQUIRED",
63 [ATSPI_STATE_TRUNCATED] = "TRUNCATED",
64 [ATSPI_STATE_ANIMATED] = "ANIMATED",
65 [ATSPI_STATE_INVALID_ENTRY] = "INVALID_ENTRY",
66 [ATSPI_STATE_SUPPORTS_AUTOCOMPLETION] = "SUPPORTS_AUTOCOMPLETION",
67 [ATSPI_STATE_SELECTABLE_TEXT] = "SELECTABLE_TEXT",
68 [ATSPI_STATE_IS_DEFAULT] = "IS_DEFAULT",
69 [ATSPI_STATE_VISITED] = "VISITED",
70 [ATSPI_STATE_CHECKABLE] = "CHECKABLE",
71 [ATSPI_STATE_MODAL] = "MODAL",
72 [ATSPI_STATE_HIGHLIGHTED] = "HIGHLIGHTED",
73 [ATSPI_STATE_HIGHLIGHTABLE] = "HIGHLIGHTABLE",
74 [ATSPI_STATE_HAS_POPUP] = "HAS_POPUP",
75 [ATSPI_STATE_READ_ONLY] = "READ_ONLY",
76 [ATSPI_STATE_LAST_DEFINED] = "LAST_DEFINED"
79 typedef struct _Box_Size {
86 static char *_multiply_string(char c, unsigned number)
88 char *result = (char *)calloc(1, number + 1);
93 memset(result, c, number);
98 static void _print_module_legend()
100 printf("\n[[node],[node role name],[attributes list],[x,y,width,height],[node name],[states list][relations]\n\n");
103 static void _print_atspi_states_legend()
105 _print_module_legend();
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]);
125 printf("[%d]\t%s\n", line_idx * COLUMN_NO, atspi_state_names[line_idx * COLUMN_NO]);
130 static void _set_indent(int indent_size)
132 if (indent_size < 0) {
133 fprintf(stderr, "WARNING: Invalid indent size. Default value retained.\n");
137 indent_width = indent_size;
140 static void _set_module_length_limit(int limit)
142 if (limit < MINIMAL_MODULE_WIDTH) {
143 fprintf(stderr, "WARNING: Invalid maximum field size. Default value retained.\n");
147 module_name_limit = limit;
150 static int _int_sort_function(gconstpointer a, gconstpointer b)
152 int int_a = GPOINTER_TO_INT (a);
153 int int_b = GPOINTER_TO_INT (b);
155 return int_a - int_b;
158 static void _truncate_string(char *string, int length_limit)
160 if (length_limit < MINIMAL_MODULE_WIDTH || !string)
163 int len = strlen(string);
165 if (len > length_limit)
166 memcpy(string + length_limit - 3, "...", 4);
169 static size_t _combine_strings(char **destination, const char *source)
171 if (!source || !*source || !destination)
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));
180 fprintf(stderr, "_combine_strings: allocation failed");
184 memcpy(buf + old_len, source, source_len + 1);
191 static Box_Size *_get_box_size(AtspiAccessible *node)
193 Box_Size *box_size = (Box_Size *)malloc(sizeof(Box_Size));
194 if (box_size == NULL)
202 AtspiComponent *component = atspi_accessible_get_component_iface(node);
203 AtspiRect *extent = component ? atspi_component_get_extents(component, ATSPI_COORD_TYPE_SCREEN, NULL) : NULL;
205 if (extent == NULL) {
206 _combine_strings(&x, "_");
207 _combine_strings(&y, "_");
208 _combine_strings(&width, "_");
209 _combine_strings(&height, "_");
211 char temp_buff[NUMBER_WIDTH];
213 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->x);
214 _combine_strings(&x, temp_buff);
216 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->y);
217 _combine_strings(&y, temp_buff);
219 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->width);
220 _combine_strings(&width, temp_buff);
222 snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->height);
223 _combine_strings(&height, temp_buff);
225 g_object_unref(component);
231 box_size->width = width;
232 box_size->height = height;
237 static char *_get_states(AtspiAccessible *node, int length_limit)
239 AtspiStateSet *node_state_set = atspi_accessible_get_state_set(node);
240 GArray *states = atspi_state_set_get_states(node_state_set);
241 if (!states) return NULL;
242 g_array_sort(states, _int_sort_function);
244 AtspiStateType state_type;
245 char *state_string = NULL;
247 for (int i = 0; i < states->len; i++) {
248 state_type = g_array_index(states, AtspiStateType, i);
250 char node_state_str[8];
251 snprintf(node_state_str, 8, "(%d)", state_type);
252 _combine_strings(&state_string, node_state_str);
255 g_array_free(states, 0);
256 g_object_unref(node_state_set);
258 _truncate_string(state_string, length_limit);
263 static char *_get_attributes(AtspiAccessible *node, int length_limit)
265 GHashTable *attributes = atspi_accessible_get_attributes(node, NULL);
270 GHashTableIter attributes_iter;
273 g_hash_table_iter_init (&attributes_iter, attributes);
275 while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
276 char attributes_string[ATTR_WIDTH];
277 int ret = snprintf(attributes_string, ATTR_WIDTH, "(%s=%s)", (char *)attr_key, (char *)attr_value);
278 if (ret >= ATTR_WIDTH)
279 fprintf(stderr, "\n_get_attributes: generated string is too long. Buffer overflow\n");
281 _combine_strings(&result, attributes_string);
283 g_hash_table_unref(attributes);
285 _truncate_string(result, length_limit);
289 static char *_get_info(AtspiAccessible *node, int length_limit)
294 char *node_name = atspi_accessible_get_name(node, NULL);
295 char *node_role_name = atspi_accessible_get_role_name(node, NULL);
296 char *unique_id = atspi_accessible_get_unique_id(node, NULL);
298 char *attributes = _get_attributes(node, length_limit);
299 Box_Size *box_size = _get_box_size(node);
300 char *states = _get_states(node, length_limit);
302 GArray *relations = atspi_accessible_get_relation_set(node, NULL);
304 char result[SAFE_BUFFER_SIZE];
305 int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%s]]",
315 (relations && relations->len) ? "*" : "");
317 if (ret >= SAFE_BUFFER_SIZE)
318 fprintf(stderr, "\n%s, %s %d: generated string is too long. Buffer overflow\n", __FILE__, __FUNCTION__, __LINE__);
321 free(node_role_name);
325 free(box_size->width);
326 free(box_size->height);
330 g_array_free(relations, TRUE);
332 return g_strdup(result);
335 static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
337 int parent_index = atspi_accessible_get_index_in_parent(obj, NULL);
338 AtspiAccessible *parent = atspi_accessible_get_parent(obj, NULL);
340 char output[CHECK_OUTPUT_WIDTH];
341 if (parent_index != parent_candidate_index || parent_candidate != parent) {
342 char parent_status[NUMBER_WIDTH];
345 strncpy(parent_status, "_", NUMBER_WIDTH);
347 snprintf(parent_status, NUMBER_WIDTH, "%d", parent_index);
349 char *parent_unique_id = atspi_accessible_get_unique_id(parent, NULL);
350 char *parent_candidate_unique_id = atspi_accessible_get_unique_id(parent_candidate, NULL);
351 snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%s,%s>]", parent_candidate_index, parent_status,
352 parent_candidate_unique_id, parent_unique_id);
353 free(parent_unique_id);
354 free(parent_candidate_unique_id);
357 snprintf(output, CHECK_OUTPUT_WIDTH, "[OK]");
360 printf("%-*s\t", CHECK_OUTPUT_WIDTH, output);
363 static int _print_atspi_tree_verify_maybe_r(int indent_number, AtspiAccessible *object, bool check_integrity, int length_limit)
365 char *indent = _multiply_string(' ', indent_number*indent_width);
366 if (indent != NULL) {
367 printf("%s", indent);
371 char *node_info = _get_info(object, length_limit);
372 if (node_info != NULL) {
373 printf("%s\n", node_info);
377 int count = atspi_accessible_get_child_count(object, NULL);
378 for (int i = 0; i < count; i++) {
379 AtspiAccessible *child = atspi_accessible_get_child_at_index(object, i, NULL);
382 _test_atspi_parent_child_relation(child, object, i);
384 _print_atspi_tree_verify_maybe_r(indent_number + 1, child, check_integrity, length_limit);
392 printf("AT-SPI2-CORE-UTIL\n\n");
393 printf("USAGE: at_spi2_tool [OPTION] [PARAMETER] ...\n\n");
394 printf("OPTION LIST:\n");
395 printf("-h, --help\t\tshow this message\n");
396 printf("-v, --version\t\tshow actual version of tool\n");
397 printf("-g, --show-legend\tprint AT-SPI state legend\n");
398 printf("-l, --list-apps\t\tlist all applications of desktop\n");
399 printf("-a, --at-spi-client <true|false>\tenable/disable org.a11y.Status.IsEnabled property\n");
400 printf("-s, --sleep <N>\tsleep N seconds\n");
401 printf("-d, --tree-dump\t\tdump tree for selected application\n");
402 printf("-c, --tree-check\tcheck tree for selected application\n");
403 printf("-f, --first-match\tperform dump or check only for first matching application\n");
404 printf("-i, --indent\t\tset indentation size, default value stands at 2\n");
405 printf("-t, --truncate\t\tset maximum single field size, default value stands at 30\n");
406 printf("\nEXAMPLE:\n");
407 printf("\tat_spi2_tool -i3 -d quickpanel\n");
408 printf("\t show AT-SPI tree for node \"quickpanel\" using three-space indentation\n");
409 printf("\tat_spi2_tool -i1 -c starter\n");
410 printf("\t show AT-SPI tree with integrity test for node \"starter\" using one-space indentation\n");
413 void _print_version()
415 printf("AT-SPI2-CORE-UTIL v%s\n", VERSION);
418 static void _print_horizontal_line_in_relation_table() {
419 for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++) {
420 for (int j = 0; j < RELATION_COLUMN_WIDTH; j++)
427 static void _print_legend_for_relation_table() {
428 char *table[] = {"OBJECT", "CONTROLLER_FOR", "CONTROLLED_BY", "FLOWS_TO", "FLOWS_FROM", "DESCRIBED_BY", "OTHER RELATION [ID]"};
429 assert(ARRAY_SIZE(table) == RELATION_TABLE_COLUMN_COUNT);
430 for(int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++)
431 printf("%*s|", RELATION_COLUMN_WIDTH , table[i]);
433 _print_horizontal_line_in_relation_table();
436 static char *_get_unique_id_of_object_in_relation(AtspiRelation *relation) {
440 gint last_index = atspi_relation_get_n_targets(relation) - 1;
441 AtspiAccessible *target = atspi_relation_get_target(relation, last_index);
442 return atspi_accessible_get_unique_id(target, NULL);
445 static void _print_relations_for_object(AtspiAccessible *node) {
446 GArray *relations = atspi_accessible_get_relation_set(node, NULL);
450 if (!relations->len) {
451 g_array_free(relations, FALSE);
455 char **table = calloc(RELATION_TABLE_COLUMN_COUNT, sizeof(char *));
457 fprintf(stderr, "Calloc failed. Can't alloc memory for object relations string\n");
461 table[0] = atspi_accessible_get_unique_id(node, NULL);
462 for (int i = 0; i < relations->len; i++) {
463 AtspiRelation *relation = g_array_index(relations, AtspiRelation *, i);
464 AtspiRelationType type = atspi_relation_get_relation_type(relation);
467 case ATSPI_RELATION_CONTROLLER_FOR: idx = 1; break;
468 case ATSPI_RELATION_CONTROLLED_BY: idx = 2; break;
469 case ATSPI_RELATION_FLOWS_TO: idx = 3; break;
470 case ATSPI_RELATION_FLOWS_FROM: idx = 4; break;
471 case ATSPI_RELATION_DESCRIBED_BY: idx = 5; break;
476 table[idx] = _get_unique_id_of_object_in_relation(relation);
479 snprintf(buf, sizeof(buf), " [%d]", type);
480 _combine_strings(&table[RELATION_TABLE_COLUMN_COUNT - 1], buf);
484 for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++) {
485 printf("%*s|", RELATION_COLUMN_WIDTH, table[i] ? table[i] : "");
491 _print_horizontal_line_in_relation_table();
492 g_array_free(relations, TRUE);
495 static void _print_relations_for_objects_in_tree(AtspiAccessible *node) {
496 _print_relations_for_object(node);
498 int count = atspi_accessible_get_child_count(node, NULL);
499 for (int i = 0; i < count; i++) {
500 AtspiAccessible *child = atspi_accessible_get_child_at_index(node, i, NULL);
502 _print_relations_for_objects_in_tree(child);
506 static void _print_relations_table(AtspiAccessible *node) {
507 _print_legend_for_relation_table();
508 _print_relations_for_objects_in_tree(node);
511 static void _atspi_tree_traverse(const char *app_name, bool dump, bool check, bool first_match, int length_limit)
514 AtspiAccessible *desktop = atspi_get_desktop(0);
516 fprintf(stderr, "atspi_get_desktop failed\n");
520 int count = atspi_accessible_get_child_count(desktop, NULL);
521 bool app_name_matched = false;
522 for (int i = 0; i < count; i++) {
523 AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL);
525 fprintf(stderr, "\n%s, %s %d: Null application occured. Results may be misleading.\n", __FILE__, __FUNCTION__, __LINE__);
529 char *name = atspi_accessible_get_name(child, NULL);
532 printf("%s\n", name);
534 if ((check || dump) && name && !strcmp(name, app_name)) {
535 app_name_matched = true;
537 _print_module_legend();
540 _test_atspi_parent_child_relation(child, desktop, i);
542 _print_atspi_tree_verify_maybe_r(0, child, check, length_limit);
544 _print_relations_table(child);
557 if (!app_name_matched && (dump || check))
558 fprintf(stderr, "There is no application with name: %s. Try again.\n", app_name);
560 g_object_unref(desktop);
563 static void _at_spi_client_enable(gboolean enabled)
565 static GDBusProxy *proxy = NULL; //we keep proxy (dbus connection) until program exits
567 GError *error = NULL;
568 GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_NONE;
572 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
574 NULL, /* GDBusInterfaceInfo */
577 "org.freedesktop.DBus.Properties",
578 NULL, /* GCancellable */
581 fprintf(stderr, "Failed to create proxy object for '/org/a11y/bus': %s\n", error->message);
587 result = g_dbus_proxy_call_sync(proxy,
589 g_variant_new ("(ssv)", "org.a11y.Status", "IsEnabled", g_variant_new_boolean(enabled)),
590 G_DBUS_CALL_FLAGS_NONE,
595 g_variant_unref(result);
598 fprintf(stderr, "Fail to call org.freedesktop.DBus.Properties.Set: %s\n", error->message);
603 static void _run_command(int argc, char *argv[])
605 struct option long_options[] = {
606 {"help", no_argument, 0, 'h'},
607 {"version", no_argument, 0, 'v'},
608 {"show-legend", no_argument, 0, 'g'},
609 {"list-apps", no_argument, 0, 'l'},
610 {"at-spi-client", no_argument, 0, 'a'},
611 {"sleep", required_argument, 0, 's'},
612 {"tree-dump", required_argument, 0, 'd'},
613 {"tree-check", required_argument, 0, 'c'},
614 {"first-match", no_argument, 0, 'f'},
615 {"indent", required_argument, 0, 'i'},
616 {"truncate", required_argument, 0, 't'},
620 int option_index = 0;
621 bool traverse_flags[FLAG_NO] = {false};
622 char *app_name = NULL;
623 gboolean enable_at_spi_client;
626 command = getopt_long(argc, argv, "hvgla:s:d:c:ft:i:", long_options, &option_index);
628 if (command == ERROR_STATE)
641 _print_atspi_states_legend();
645 _atspi_tree_traverse(NULL, false, false, false, module_name_limit);
649 enable_at_spi_client = TRUE;
650 if(optarg[0] == 'f' || optarg[0] == '0')
651 enable_at_spi_client = FALSE;
653 _at_spi_client_enable(enable_at_spi_client);
661 traverse_flags[DUMP] = true;
666 traverse_flags[CHECK] = true;
671 traverse_flags[FIRST_MATCH] = true;
675 _set_indent(atoi(optarg));
679 _set_module_length_limit(atoi(optarg));
683 fprintf(stderr, "Invalid parameter. Use: \"--help\"\n");
691 if (traverse_flags[DUMP] || traverse_flags[CHECK])
692 _atspi_tree_traverse(app_name, traverse_flags[DUMP], traverse_flags[CHECK], traverse_flags[FIRST_MATCH], module_name_limit);
695 int main(int argc, char *argv[])
698 printf("Arguments required. Type %s --help.\n", argv[0]);
702 int result = atspi_init();
705 fprintf(stderr, "Unable to initilize AT-SPI infrastructure, atspi_init failed\n");
709 _run_command(argc, argv);