#include <string.h>
#include <getopt.h>
#include <stdbool.h>
+#include <gio/gio.h>
+#include <assert.h>
+#include <stdint.h>
#define ERROR_STATE -1
-#define SAFE_BUFFER_SIZE 255
+#define SAFE_BUFFER_SIZE 2048
+#define CHECK_OUTPUT_WIDTH 42
+#define MINIMAL_MODULE_WIDTH 3
+#define ATTR_WIDTH 50
+#define NUMBER_WIDTH 6
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define COLUMN_NO 3
+#define FLAG_NO 3
+#define DUMP 0
+#define CHECK 1
+#define FIRST_MATCH 2
+#define RELATION_TABLE_COLUMN_COUNT 7
+#define ATTRIBUTE_TABLE_COLUMN_COUNT 3
+#define RELATION_TABLE_COLUMN_WIDTH 20
+#define ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH 20
+#define VERSION "1.1"
static unsigned indent_width = 2;
+static int module_name_limit = -1;
static const char* atspi_state_names[] = {
[ATSPI_STATE_INVALID] = "INVALID",
[ATSPI_STATE_LAST_DEFINED] = "LAST_DEFINED"
};
-typedef struct _StringAccumulator {
- char *string;
- int length;
-} StringAccumulator;
+typedef struct _Box_Size {
+ char *x;
+ char *y;
+ char *height;
+ char *width;
+} Box_Size;
-char *strdup(const char *c) {
- char *result = (char *)calloc(1, strlen(c) + 1);
- return result ? strcpy(result, c) : result;
-}
-
-static char *multiply_string(char c, unsigned number) {
+static char *_multiply_string(char c, unsigned number)
+{
char *result = (char *)calloc(1, number + 1);
if (result == NULL)
return result;
}
-static void _print_atspi_states_legend() {
+static void _print_module_legend()
+{
+ printf("\n[[node],[node role name],[attributes list],[x,y,width,height],[node name],[states list][relations]\n\n");
+}
+
+static void _print_atspi_states_legend()
+{
+ _print_module_legend();
+
int array_len = ARRAY_SIZE(atspi_state_names);
int line_count = (array_len + COLUMN_NO - 1)/COLUMN_NO;
for (int line_idx = 0; line_idx < line_count; line_idx++) {
- if ((line_idx < line_count - 1) || (array_len % 3 == 0)) {
- printf("[%d]\t%-24s[%d]\t%-24s[%d]\t%s\n", line_idx * 3, atspi_state_names[line_idx * 3], (line_idx * 3) + 1, atspi_state_names[(line_idx * 3) + 1], (line_idx * 3) + 2, atspi_state_names[(line_idx * 3) + 2]);
- } else if (array_len % 3 == 2) {
- printf("[%d]\t%-24s[%d]\t%s\n", line_idx * 3, atspi_state_names[line_idx * 3], (line_idx * 3) + 1, atspi_state_names[(line_idx * 3) + 1]);
+ if ((line_idx < line_count - 1) || (array_len % COLUMN_NO == 0)) {
+ printf("[%d]\t%-24s[%d]\t%-24s[%d]\t%s\n",
+ line_idx * COLUMN_NO,
+ atspi_state_names[line_idx * COLUMN_NO],
+ (line_idx * COLUMN_NO) + 1,
+ atspi_state_names[(line_idx * COLUMN_NO) + 1],
+ (line_idx * COLUMN_NO) + 2,
+ atspi_state_names[(line_idx * COLUMN_NO) + 2]);
+ } else if (array_len % COLUMN_NO == 2) {
+ printf("[%d]\t%-24s[%d]\t%s\n",
+ line_idx * COLUMN_NO,
+ atspi_state_names[line_idx * COLUMN_NO],
+ (line_idx * COLUMN_NO) + 1,
+ atspi_state_names[(line_idx * COLUMN_NO) + 1]);
} else {
- printf("[%d]\t%s\n", line_idx * 3, atspi_state_names[line_idx * 3]);
+ printf("[%d]\t%s\n", line_idx * COLUMN_NO, atspi_state_names[line_idx * COLUMN_NO]);
}
}
}
-static void set_indent(int indent_size)
+static void _set_indent(int indent_size)
{
if (indent_size < 0) {
fprintf(stderr, "WARNING: Invalid indent size. Default value retained.\n");
indent_width = indent_size;
}
+static void _set_module_length_limit(int limit)
+{
+ if (limit < MINIMAL_MODULE_WIDTH) {
+ fprintf(stderr, "WARNING: Invalid maximum field size. Default value retained.\n");
+ return;
+ }
+
+ module_name_limit = limit;
+}
+
static int _int_sort_function(gconstpointer a, gconstpointer b)
{
int int_a = GPOINTER_TO_INT (a);
return int_a - int_b;
}
-static int _accumulate_string(const char *string, StringAccumulator *accumulator) {
- if (!accumulator)
- return FALSE;
+static void _truncate_string(char *string, int length_limit)
+{
+ if (length_limit < MINIMAL_MODULE_WIDTH || !string)
+ return;
- if (string) {
- int length = accumulator->length + strlen(string);
- char *rebuffer = realloc(accumulator->string, length + 1);
- if (rebuffer) {
- strcpy(rebuffer + accumulator->length, string);
- accumulator->string = rebuffer;
- accumulator->length = length;
- } else {
- return FALSE;
- }
+ int len = strlen(string);
+
+ if (len > length_limit)
+ memcpy(string + length_limit - 3, "...", 4);
+}
+
+static size_t _combine_strings(char **destination, const char *source)
+{
+ if (!source || !*source || !destination)
+ return 0;
+
+ size_t old_len = *destination ? strlen(*destination) : 0;
+ size_t source_len = strlen(source);
+ size_t len = old_len + source_len;
+ char *buf = realloc(*destination, (len + 1) * sizeof(char));
+
+ if (!buf) {
+ fprintf(stderr, "_combine_strings: allocation failed");
+ return old_len;
}
- return TRUE;
+
+ memcpy(buf + old_len, source, source_len + 1);
+
+ *destination = buf;
+
+ return len;
}
-static char *get_info(AtspiAccessible *node) {
- if (!node)
+static Box_Size *_get_box_size(AtspiAccessible *node)
+{
+ Box_Size *box_size = (Box_Size *)malloc(sizeof(Box_Size));
+ if (box_size == NULL)
return NULL;
- int node_width = -1;
- int node_height = -1;
- char *node_name = atspi_accessible_get_name(node, NULL);
- char *node_role_name = atspi_accessible_get_role_name(node, NULL);
+ char *x = NULL;
+ char *y = NULL;
+ char *width = NULL;
+ char *height = NULL;
+
AtspiComponent *component = atspi_accessible_get_component_iface(node);
+ AtspiRect *extent = component ? atspi_component_get_extents(component, ATSPI_COORD_TYPE_SCREEN, NULL) : NULL;
- AtspiRect *extent = NULL;
- if (component != NULL){
- extent = atspi_component_get_extents(component, ATSPI_COORD_TYPE_SCREEN, NULL);
+ if (extent == NULL) {
+ _combine_strings(&x, "_");
+ _combine_strings(&y, "_");
+ _combine_strings(&width, "_");
+ _combine_strings(&height, "_");
+ } else {
+ char temp_buff[NUMBER_WIDTH];
- if (extent != NULL) {
- node_width = extent->width;
- node_height = extent->height;
- }
+ snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->x);
+ _combine_strings(&x, temp_buff);
+
+ snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->y);
+ _combine_strings(&y, temp_buff);
+
+ snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->width);
+ _combine_strings(&width, temp_buff);
+
+ snprintf(temp_buff, NUMBER_WIDTH, "%d", extent->height);
+ _combine_strings(&height, temp_buff);
+
+ g_object_unref(component);
+ g_free(extent);
}
+ box_size->x = x;
+ box_size->y = y;
+ box_size->width = width;
+ box_size->height = height;
+
+ return box_size;
+}
+
+static char *_get_states(AtspiAccessible *node, int length_limit)
+{
AtspiStateSet *node_state_set = atspi_accessible_get_state_set(node);
GArray *states = atspi_state_set_get_states(node_state_set);
+ if (!states) {
+ g_clear_object(&node_state_set);
+ return NULL;
+ }
g_array_sort(states, _int_sort_function);
- AtspiStateType state;
- StringAccumulator state_accumulator = {};
- for (int i = 0; states && (i < states->len); i++) {
- state = g_array_index(states, AtspiStateType, i);
- char node_state_str[8];
- sprintf(node_state_str, "[%d]", state);
- _accumulate_string(node_state_str, &state_accumulator);
+ AtspiStateType state_type;
+ char *state_string = NULL;
+
+ for (int i = 0; i < states->len; i++) {
+ state_type = g_array_index(states, AtspiStateType, i);
+
+ char node_state_str[27] = "";
+ strncat(node_state_str, "(", sizeof(node_state_str) - strlen(node_state_str) - 1);
+ strncat(node_state_str, atspi_state_names[state_type], sizeof(node_state_str) - strlen(node_state_str) - 1);
+ strncat(node_state_str, ")", sizeof(node_state_str) - strlen(node_state_str) - 1);
+
+ _combine_strings(&state_string, node_state_str);
}
+
g_array_free(states, 0);
+ g_clear_object(&node_state_set);
- StringAccumulator attribute_accumulator = {};
- GHashTable *attributes = NULL;
- attributes = atspi_accessible_get_attributes(node, NULL);
- if (attributes) {
- GHashTableIter attributes_iter;
- gpointer attr_key, attr_value;
- g_hash_table_iter_init (&attributes_iter, attributes);
- while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
- _accumulate_string("[", &attribute_accumulator);
- _accumulate_string((char*)attr_key, &attribute_accumulator);
- _accumulate_string("=", &attribute_accumulator);
- _accumulate_string((char*)attr_value, &attribute_accumulator);
- _accumulate_string("]", &attribute_accumulator);
- }
- g_hash_table_unref(attributes);
- }
+ _truncate_string(state_string, length_limit);
- char result[SAFE_BUFFER_SIZE + 1];
- snprintf(result, SAFE_BUFFER_SIZE, "[[%s],[%s],[%d,%d],[%s],[%s]]\n", node_role_name, attribute_accumulator.string, node_width, node_height, node_name, state_accumulator.string);
+ return state_string;
+}
- free(node_name);
- free(node_role_name);
+static char *_get_attributes(AtspiAccessible *node, int length_limit, bool *attributes_are_too_long)
+{
+ GHashTable *attributes = atspi_accessible_get_attributes(node, NULL);
- if (state_accumulator.string)
- g_free(state_accumulator.string);
+ if (!attributes)
+ return NULL;
- if (attribute_accumulator.string)
- g_free(attribute_accumulator.string);
+ GHashTableIter attributes_iter;
+ gpointer attr_key;
+ gpointer attr_value;
+ g_hash_table_iter_init (&attributes_iter, attributes);
+ char *result = NULL;
+ while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
+ char attributes_string[SAFE_BUFFER_SIZE];
+ int ret = snprintf(attributes_string, SAFE_BUFFER_SIZE, "(%s=%s)", (char *)attr_key, (char *)attr_value);
+ if (ret >= SAFE_BUFFER_SIZE)
+ fprintf(stderr, "\n%s, %s %d: generated string is too long. Buffer overflow\n", __FILE__, __FUNCTION__, __LINE__);
+
+ _combine_strings(&result, attributes_string);
+ }
+ g_hash_table_unref(attributes);
- if (component)
- g_object_unref(component);
+ int real_truncate_size = (length_limit < ATTR_WIDTH && length_limit > MINIMAL_MODULE_WIDTH) ? length_limit : ATTR_WIDTH;
+ if (result && attributes_are_too_long && strlen(result) > real_truncate_size)
+ *attributes_are_too_long = true;
- if (extent)
- g_free(extent);
+ _truncate_string(result, real_truncate_size);
+ return result;
+}
+
+static char *_get_info(AtspiAccessible *node, int length_limit, bool *attributes_are_too_long, bool *app_has_relations)
+{
+ if (!node)
+ return NULL;
+
+ char *node_name = atspi_accessible_get_name(node, NULL);
+ char *node_role_name = atspi_accessible_get_role_name(node, NULL);
+ char *unique_id = atspi_accessible_get_unique_id(node, NULL);
+ char *path = atspi_accessible_get_path(node, NULL);
+ unsigned long long eo_ptr = 0;
+ sscanf(path, "%llu", &eo_ptr);
+
+ char *attributes = _get_attributes(node, length_limit, attributes_are_too_long);
+ Box_Size *box_size = _get_box_size(node);
+ char *states = _get_states(node, length_limit);
+
+ GArray *relations = atspi_accessible_get_relation_set(node, NULL);
+ bool current_node_has_relations = (relations && relations->len);
+
+ char result[SAFE_BUFFER_SIZE];
+ int ret = snprintf(result, SAFE_BUFFER_SIZE, "[[%s(%p)],[%s],[%s],[%s,%s,%s,%s],[%s],[%s],[%s]]",
+ unique_id, (uintptr_t)eo_ptr,
+ node_role_name,
+ attributes,
+ box_size ? box_size->x : "nil",
+ box_size ? box_size->y : "nil",
+ box_size ? box_size->width : "nil",
+ box_size ? box_size->height : "nil",
+ node_name,
+ states,
+ current_node_has_relations ? "*" : "");
+
+ if (ret >= SAFE_BUFFER_SIZE)
+ fprintf(stderr, "\n%s, %s %d: generated string is too long. Buffer overflow\n", __FILE__, __FUNCTION__, __LINE__);
+
+ if (current_node_has_relations)
+ *app_has_relations = true;
- if (node_state_set)
- g_object_unref(node_state_set);
+ free(node_name);
+ free(node_role_name);
+ free(unique_id);
+ free(path);
+ free(attributes);
+ if (box_size) {
+ free(box_size->width);
+ free(box_size->height);
+ free(box_size);
+ }
+ free(states);
+ if (relations)
+ g_array_free(relations, TRUE);
- return strdup(result);
+ return g_strdup(result);
}
-static void test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
+static void _test_atspi_parent_child_relation(AtspiAccessible *obj, AtspiAccessible *parent_candidate, int parent_candidate_index)
{
int parent_index = atspi_accessible_get_index_in_parent(obj, NULL);
AtspiAccessible *parent = atspi_accessible_get_parent(obj, NULL);
+
+ char output[CHECK_OUTPUT_WIDTH];
if (parent_index != parent_candidate_index || parent_candidate != parent) {
- printf("[FAIL<%d,", parent_candidate_index);
+ char parent_status[NUMBER_WIDTH];
if (parent == NULL)
- printf("_");
+ strncpy(parent_status, "_", NUMBER_WIDTH);
else
- printf("%d", parent_index);
+ snprintf(parent_status, NUMBER_WIDTH, "%d", parent_index);
- printf(">]\t");
+ char *parent_unique_id = atspi_accessible_get_unique_id(parent, NULL);
+ char *parent_candidate_unique_id = atspi_accessible_get_unique_id(parent_candidate, NULL);
+ snprintf(output, CHECK_OUTPUT_WIDTH, "[FAIL<%d,%s><%s,%s>]", parent_candidate_index, parent_status,
+ parent_candidate_unique_id, parent_unique_id);
+ free(parent_unique_id);
+ free(parent_candidate_unique_id);
} else {
- printf("[OK]\t");
+ snprintf(output, CHECK_OUTPUT_WIDTH, "[OK]");
}
+
+ printf("%-*s\t", CHECK_OUTPUT_WIDTH, output);
}
-static int expand_r(int indent_number, AtspiAccessible *object, bool check_integrity)
+static int _print_atspi_tree_verify_maybe_r(int indent_number, AtspiAccessible *object, bool check_integrity, int length_limit,
+ bool *attributes_are_too_long, bool *app_has_relations)
{
- char *indent = multiply_string(' ', indent_number*indent_width);
+ char *indent = _multiply_string(' ', indent_number*indent_width);
if (indent != NULL) {
printf("%s", indent);
free(indent);
}
- char *node_info = get_info(object);
+ char *node_info = _get_info(object, length_limit, attributes_are_too_long, app_has_relations);
if (node_info != NULL) {
- printf("%s", node_info);
+ printf("%s\n", node_info);
free(node_info);
}
AtspiAccessible *child = atspi_accessible_get_child_at_index(object, i, NULL);
if (child) {
if (check_integrity)
- test_atspi_parent_child_relation(child, object, i);
+ _test_atspi_parent_child_relation(child, object, i);
- expand_r(indent_number + 1, child, check_integrity);
+ _print_atspi_tree_verify_maybe_r(indent_number + 1, child, check_integrity, length_limit, attributes_are_too_long, app_has_relations);
}
}
return 0;
}
-void print_help()
+void _print_help()
{
printf("AT-SPI2-CORE-UTIL\n\n");
printf("USAGE: at_spi2_tool [OPTION] [PARAMETER] ...\n\n");
printf("OPTION LIST:\n");
printf("-h, --help\t\tshow this message\n");
+ printf("-v, --version\t\tshow actual version of tool\n");
printf("-g, --show-legend\tprint AT-SPI state legend\n");
printf("-l, --list-apps\t\tlist all applications of desktop\n");
+ printf("-a, --at-spi-client <true|false>\tenable/disable org.a11y.Status.IsEnabled property\n");
+ printf("-s, --sleep <N>\tsleep N seconds\n");
printf("-d, --tree-dump\t\tdump tree for selected application\n");
printf("-c, --tree-check\tcheck tree for selected application\n");
- printf("-i, --indent\t\tset indentation size\n");
+ printf("-f, --first-match\tperform dump or check only for first matching application\n");
+ printf("-i, --indent\t\tset indentation size, default value stands at 2\n");
+ printf("-t, --truncate\t\tset maximum single field size, default value stands at 30\n");
printf("\nEXAMPLE:\n");
printf("\tat_spi2_tool -i3 -d quickpanel\n");
printf("\t show AT-SPI tree for node \"quickpanel\" using three-space indentation\n");
printf("\t show AT-SPI tree with integrity test for node \"starter\" using one-space indentation\n");
}
-static void atspi_tree_traverse(AtspiAccessible *desktop, const char *app_name, bool dump, bool check)
+void _print_version()
+{
+ printf("AT-SPI2-CORE-UTIL v%s\n", VERSION);
+}
+
+static void _print_horizontal_line_in_relations_table() {
+ for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++) {
+ for (int j = 0; j < RELATION_TABLE_COLUMN_WIDTH; j++)
+ printf("-");
+ printf("+");
+ }
+ printf("\n");
+}
+
+static char *_get_unique_id_of_object_in_relation(AtspiRelation *relation) {
+ if (!relation)
+ return NULL;
+
+ gint last_index = atspi_relation_get_n_targets(relation) - 1;
+ AtspiAccessible *target = atspi_relation_get_target(relation, last_index);
+ return atspi_accessible_get_unique_id(target, NULL);
+}
+
+static void _print_relations_for_object(AtspiAccessible *node) {
+ GArray *relations = atspi_accessible_get_relation_set(node, NULL);
+ if (!relations)
+ return;
+
+ if (!relations->len) {
+ g_array_free(relations, FALSE);
+ return;
+ }
+
+ char **table = calloc(RELATION_TABLE_COLUMN_COUNT, sizeof(char *));
+ if (!table) {
+ fprintf(stderr, "Calloc failed. Can't alloc memory for object relations string\n");
+ return;
+ }
+
+ table[0] = atspi_accessible_get_unique_id(node, NULL);
+ for (int i = 0; i < relations->len; i++) {
+ AtspiRelation *relation = g_array_index(relations, AtspiRelation *, i);
+ AtspiRelationType type = atspi_relation_get_relation_type(relation);
+ int idx;
+ switch (type) {
+ case ATSPI_RELATION_CONTROLLER_FOR: idx = 1; break;
+ case ATSPI_RELATION_CONTROLLED_BY: idx = 2; break;
+ case ATSPI_RELATION_FLOWS_TO: idx = 3; break;
+ case ATSPI_RELATION_FLOWS_FROM: idx = 4; break;
+ case ATSPI_RELATION_DESCRIBED_BY: idx = 5; break;
+ default: idx = 0;
+ }
+
+ if (idx > 0)
+ table[idx] = _get_unique_id_of_object_in_relation(relation);
+ else {
+ char buf[16];
+ snprintf(buf, sizeof(buf), " [%d]", type);
+ _combine_strings(&table[RELATION_TABLE_COLUMN_COUNT - 1], buf);
+ }
+ }
+
+ for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++) {
+ printf("%*s|", RELATION_TABLE_COLUMN_WIDTH, table[i] ? table[i] : "");
+ free(table[i]);
+ }
+ free(table);
+
+ printf("\n");
+ _print_horizontal_line_in_relations_table();
+ if (relations)
+ g_array_free(relations, TRUE);
+}
+
+typedef void (*print_information_about_object_function)(AtspiAccessible *);
+
+static void _iterate_over_tree(print_information_about_object_function func, AtspiAccessible *node) {
+ func(node);
+
+ int count = atspi_accessible_get_child_count(node, NULL);
+ for (int i = 0; i < count; i++) {
+ AtspiAccessible *child = atspi_accessible_get_child_at_index(node, i, NULL);
+ if (child)
+ _iterate_over_tree(func, child);
+ }
+}
+
+static void _print_relations_for_objects_in_tree(AtspiAccessible *node) {
+ _iterate_over_tree(_print_relations_for_object, node);
+}
+
+static void _print_header_for_relation_table() {
+ char *table[] = {"OBJECT", "CONTROLLER_FOR", "CONTROLLED_BY", "FLOWS_TO", "FLOWS_FROM", "DESCRIBED_BY", "OTHER RELATION [ID]"};
+ assert(ARRAY_SIZE(table) == RELATION_TABLE_COLUMN_COUNT);
+
+ _print_horizontal_line_in_relations_table();
+
+ for (int i = 0; i < RELATION_TABLE_COLUMN_COUNT; i++)
+ printf("%*s|", RELATION_TABLE_COLUMN_WIDTH , table[i]);
+
+ printf("\n");
+ _print_horizontal_line_in_relations_table();
+}
+
+static void _print_relations_table(AtspiAccessible *node) {
+ printf("\nRELATIONS TABLE\n");
+ _print_header_for_relation_table();
+ _print_relations_for_objects_in_tree(node);
+}
+
+static void _print_horizontal_line_in_attributes_table() {
+ int size_factor = 1;
+ for (int i = 0; i < ATTRIBUTE_TABLE_COLUMN_COUNT; i++) {
+ if (i == ATTRIBUTE_TABLE_COLUMN_COUNT - 1)
+ size_factor = 4;
+ for (int j = 0; j < ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH * size_factor; j++)
+ printf("-");
+ printf("+");
+ }
+ printf("\n");
+}
+
+static void _print_attributes_for_object(AtspiAccessible *node) {
+ GHashTable *attributes = atspi_accessible_get_attributes(node, NULL);
+
+ if (!attributes)
+ return;
+
+ char *unique_id = atspi_accessible_get_unique_id(node, NULL);
+ GHashTableIter attributes_iter;
+ gpointer attr_key;
+ gpointer attr_value;
+
+ g_hash_table_iter_init (&attributes_iter, attributes);
+ while (g_hash_table_iter_next (&attributes_iter, &attr_key, &attr_value)) {
+ printf("%*s|", ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH, unique_id ? unique_id : "");
+ printf("%*s|", ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH, attr_key ? (char *) attr_key : "");
+ printf("%*s|\n", ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH * 4, attr_value ? (char *) attr_value : "");
+ }
+
+ if (g_hash_table_size (attributes))
+ _print_horizontal_line_in_attributes_table();
+
+ g_hash_table_unref(attributes);
+ free(unique_id);
+}
+
+static void _print_attributes_for_objects_in_tree(AtspiAccessible *node) {
+ _iterate_over_tree(_print_attributes_for_object, node);
+}
+
+static void _print_header_for_attributes_table() {
+ char *table[] = {"OBJECT", "ATTRIBUTE KEY", "ATTRIBUTE VALUE"};
+ assert(ARRAY_SIZE(table) == ATTRIBUTE_TABLE_COLUMN_COUNT);
+
+ _print_horizontal_line_in_attributes_table();
+
+ int size_factor = 1;
+ for (int i = 0; i < ATTRIBUTE_TABLE_COLUMN_COUNT; i++) {
+ if (i == ATTRIBUTE_TABLE_COLUMN_COUNT - 1)
+ size_factor = 4;
+ printf("%*s|", ATTRIBUTE_TABLE_BASE_COLUMN_WIDTH * size_factor , table[i]);
+ }
+
+ printf("\n");
+ _print_horizontal_line_in_attributes_table();
+}
+
+static void _print_attributes_table(AtspiAccessible *node) {
+ printf("\nATTRIBUTES TABLE\n");
+ _print_header_for_attributes_table();
+ _print_attributes_for_objects_in_tree(node);
+}
+
+static void _atspi_tree_traverse(const char *app_name, bool dump, bool check, bool first_match, int length_limit)
{
+
+ AtspiAccessible *desktop = atspi_get_desktop(0);
+ if (!desktop) {
+ fprintf(stderr, "atspi_get_desktop failed\n");
+ return;
+ }
+
int count = atspi_accessible_get_child_count(desktop, NULL);
+ bool app_name_matched = false;
+
for (int i = 0; i < count; i++) {
AtspiAccessible *child = atspi_accessible_get_child_at_index(desktop, i, NULL);
- if (child) {
- char *name = atspi_accessible_get_name(child, NULL);
+ if (child == NULL) {
+ fprintf(stderr, "\n%s, %s %d: Null application occured. Results may be misleading.\n", __FILE__, __FUNCTION__, __LINE__);
+ continue;
+ }
- if (!dump && !check)
- printf("%s\n", name);
+ char *name = atspi_accessible_get_name(child, NULL);
+ bool attributes_are_too_long = false;
+ bool app_has_relations = false;
- if (dump && name && !strcmp(name, app_name)) {
- expand_r(0, child, false);
- break;
- }
+ if (!dump && !check)
+ printf("%s\n", name);
- if (check && name && !strcmp(name, app_name)) {
- test_atspi_parent_child_relation(child, desktop, i);
- expand_r(0, child, true);
+ if ((check || dump) && name && !strcmp(name, app_name)) {
+ app_name_matched = true;
+
+ _print_module_legend();
+
+ if (check)
+ _test_atspi_parent_child_relation(child, desktop, i);
+
+ _print_atspi_tree_verify_maybe_r(0, child, check, length_limit, &attributes_are_too_long, &app_has_relations);
+
+ if (app_has_relations)
+ _print_relations_table(child);
+
+ if (attributes_are_too_long)
+ _print_attributes_table(child);
+
+ if (first_match) {
+ free(name);
break;
+ } else {
+ printf("\n");
}
+
+ free(name);
+ }
+ }
+
+ if (!app_name_matched && (dump || check))
+ fprintf(stderr, "There is no application with name: %s. Try again.\n", app_name);
+
+ g_object_unref(desktop);
+}
+
+static void _at_spi_client_enable(gboolean enabled)
+{
+ static GDBusProxy *proxy = NULL; //we keep proxy (dbus connection) until program exits
+ GVariant *result;
+ GError *error = NULL;
+ GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_NONE;
+
+
+ if (!proxy) {
+ proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+ flags,
+ NULL, /* GDBusInterfaceInfo */
+ "org.a11y.Bus",
+ "/org/a11y/bus",
+ "org.freedesktop.DBus.Properties",
+ NULL, /* GCancellable */
+ &error);
+ if (error) {
+ fprintf(stderr, "Failed to create proxy object for '/org/a11y/bus': %s\n", error->message);
+ g_error_free(error);
+ return;
}
}
+
+ result = g_dbus_proxy_call_sync(proxy,
+ "Set",
+ g_variant_new ("(ssv)", "org.a11y.Status", "IsEnabled", g_variant_new_boolean(enabled)),
+ G_DBUS_CALL_FLAGS_NONE,
+ -1,
+ NULL,
+ &error);
+ if (result)
+ g_variant_unref(result);
+
+ if (error) {
+ fprintf(stderr, "Fail to call org.freedesktop.DBus.Properties.Set: %s\n", error->message);
+ g_error_free(error);
+ }
}
-static void run_command(int argc, char *argv[], AtspiAccessible *desktop)
+static void _run_command(int argc, char *argv[])
{
struct option long_options[] = {
{"help", no_argument, 0, 'h'},
+ {"version", no_argument, 0, 'v'},
{"show-legend", no_argument, 0, 'g'},
{"list-apps", no_argument, 0, 'l'},
+ {"at-spi-client", no_argument, 0, 'a'},
+ {"sleep", required_argument, 0, 's'},
{"tree-dump", required_argument, 0, 'd'},
{"tree-check", required_argument, 0, 'c'},
+ {"first-match", no_argument, 0, 'f'},
{"indent", required_argument, 0, 'i'},
+ {"truncate", required_argument, 0, 't'},
};
int command = 0;
int option_index = 0;
+ bool traverse_flags[FLAG_NO] = {false};
+ char *app_name = NULL;
+ gboolean enable_at_spi_client;
while (TRUE) {
- command = getopt_long(argc, argv, "hgld:c:i:", long_options, &option_index);
+ command = getopt_long(argc, argv, "hvgla:s:d:c:ft:i:", long_options, &option_index);
if (command == ERROR_STATE)
break;
switch (command) {
case 'h':
- print_help();
+ _print_help();
+ break;
+
+ case 'v':
+ _print_version();
break;
case 'g':
break;
case 'l':
- atspi_tree_traverse(desktop, NULL, false, false);
+ _atspi_tree_traverse(NULL, false, false, false, module_name_limit);
+ break;
+
+ case 'a':
+ enable_at_spi_client = TRUE;
+ if (optarg[0] == 'f' || optarg[0] == '0')
+ enable_at_spi_client = FALSE;
+
+ _at_spi_client_enable(enable_at_spi_client);
+ break;
+
+ case 's':
+ sleep(atoi(optarg));
break;
case 'd':
- atspi_tree_traverse(desktop, optarg, true, false);
+ traverse_flags[DUMP] = true;
+ app_name = optarg;
break;
case 'c':
- atspi_tree_traverse(desktop, optarg, false, true);
+ traverse_flags[CHECK] = true;
+ app_name = optarg;
+ break;
+
+ case 'f':
+ traverse_flags[FIRST_MATCH] = true;
break;
case 'i':
- set_indent(atoi(optarg));
+ _set_indent(atoi(optarg));
+ break;
+
+ case 't':
+ _set_module_length_limit(atoi(optarg));
break;
case '?':
+ fprintf(stderr, "Invalid parameter. Use: \"--help\"\n");
break;
default:
abort();
}
}
+
+ if (traverse_flags[DUMP] || traverse_flags[CHECK])
+ _atspi_tree_traverse(app_name, traverse_flags[DUMP], traverse_flags[CHECK], traverse_flags[FIRST_MATCH], module_name_limit);
}
int main(int argc, char *argv[])
return -1;
}
- AtspiAccessible *desktop = atspi_get_desktop(0);
-
- if (!desktop) {
- fprintf(stderr, "atspi_get_desktop failed\n");
- return 1;
+ int result = atspi_init();
+ if (result != 0)
+ {
+ fprintf(stderr, "Unable to initilize AT-SPI infrastructure, atspi_init failed\n");
+ return -1;
}
- atspi_init();
-
- run_command(argc, argv, desktop);
+ _run_command(argc, argv);
return 0;
}