Retire ATK nodes without accessing freed children or retaining dangling parent and ID pointers. Treat failed and malformed MSAA state queries as protected, and initialize worker logging before argument validation. Add publisher lifetime and worker privacy regressions, correct the publisher build target dependency, and remove both browser artifacts during uninstall. Validation: build, all 12 CTests, updated startup-error regression, AddressSanitizer lifetime check, shellcheck, and temporary-prefix removal check passed. Live speech and Braille acceptance remains separate.
2548 lines
105 KiB
C
2548 lines
105 KiB
C
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
// Prototype AT-SPI publisher for Wine Access browser support (milestone 1).
|
|
//
|
|
// Wine Access browser publisher.
|
|
//
|
|
// Publishes a Windows Chromium window's accessibility tree on the
|
|
// accessibility bus through ATK and atk-bridge, matching what native Linux
|
|
// Chromium publishes, so readers use their own browser support. The design,
|
|
// mapping rules and verification are in docs/browser.md.
|
|
//
|
|
// Live mode (started by the wine-access helper): --worker COMMAND... spawns
|
|
// wine-access-browser-worker.exe through the prefix's Wine loader and keeps
|
|
// the tree current from its messages, forwarding actions, focus, caret,
|
|
// selection and scrolling. Once published it prints "published" on stdout so
|
|
// the helper can retire legacy announcements for that window.
|
|
// Snapshot mode (tests): publishes a recorded wine-access-browser-snapshot-1
|
|
// file, static.
|
|
//
|
|
// Usage: wine-access-browser-publisher (SNAPSHOT.json | --worker COMMAND...)
|
|
// [--app-name NAME] [--active] [--announce] [--quit-after SECONDS]
|
|
// --worker must be the last option: everything after it is the command.
|
|
// WINE_ACCESS_BROWSER_TRACE=1 logs every worker message to stderr.
|
|
|
|
#include <atk-bridge.h>
|
|
#include <X11/Xlib.h>
|
|
#include <atk/atk.h>
|
|
#include <errno.h>
|
|
#include <glib-unix.h>
|
|
#include <poll.h>
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
#include <json-glib/json-glib.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// MSAA roles and states used by the mapping (values from oleacc.h).
|
|
enum {
|
|
MsaaRoleMenuBar = 2, MsaaRoleAlert = 8, MsaaRoleWindow = 9, MsaaRoleClient = 10, MsaaRoleMenuPopup = 11,
|
|
MsaaRoleMenuItem = 12, MsaaRoleToolTip = 13, MsaaRoleDocument = 15, MsaaRolePane = 16, MsaaRoleDialog = 18,
|
|
MsaaRoleGrouping = 20, MsaaRoleSeparator = 21, MsaaRoleToolBar = 22, MsaaRoleStatusBar = 23, MsaaRoleTable = 24,
|
|
MsaaRoleColumnHeader = 25, MsaaRoleRowHeader = 26, MsaaRoleRow = 28, MsaaRoleCell = 29, MsaaRoleLink = 30,
|
|
MsaaRoleList = 33, MsaaRoleListItem = 34, MsaaRoleOutline = 35, MsaaRoleOutlineItem = 36, MsaaRolePageTab = 37,
|
|
MsaaRoleGraphic = 40, MsaaRoleStaticText = 41, MsaaRoleText = 42, MsaaRolePushButton = 43,
|
|
MsaaRoleCheckButton = 44, MsaaRoleRadioButton = 45, MsaaRoleComboBox = 46, MsaaRoleProgressBar = 48,
|
|
MsaaRoleSlider = 51, MsaaRoleSpinButton = 52, MsaaRoleButtonMenu = 57, MsaaRolePageTabList = 60,
|
|
};
|
|
enum {
|
|
MsaaStateUnavailable = 0x1, MsaaStateSelected = 0x2, MsaaStateFocused = 0x4, MsaaStatePressed = 0x8,
|
|
MsaaStateChecked = 0x10, MsaaStateReadOnly = 0x40, MsaaStateExpanded = 0x200, MsaaStateCollapsed = 0x400,
|
|
MsaaStateBusy = 0x800, MsaaStateInvisible = 0x8000, MsaaStateOffscreen = 0x10000, MsaaStateFocusable = 0x100000,
|
|
MsaaStateSelectable = 0x200000, MsaaStateMultiSelectable = 0x1000000, MsaaStateProtected = 0x20000000,
|
|
};
|
|
// IA2 roles and states (AccessibleRole.idl, AccessibleStates.idl).
|
|
enum {
|
|
Ia2RoleCanvas = 0x401, Ia2RoleCaption = 0x402, Ia2RoleEmbeddedObject = 0x40a, Ia2RoleFooter = 0x40e,
|
|
Ia2RoleForm = 0x410, Ia2RoleHeader = 0x413, Ia2RoleHeading = 0x414, Ia2RoleInternalFrame = 0x418,
|
|
Ia2RoleLabel = 0x419, Ia2RoleNote = 0x41b, Ia2RolePage = 0x41d, Ia2RoleParagraph = 0x41e, Ia2RoleSection = 0x424,
|
|
Ia2RoleToggleButton = 0x42a, Ia2RoleComplementary = 0x42c, Ia2RoleLandmark = 0x42d, Ia2RoleBlockQuote = 0x431,
|
|
Ia2RoleMark = 0x432,
|
|
};
|
|
enum {
|
|
Ia2StateDefunct = 0x4, Ia2StateEditable = 0x8, Ia2StateInvalidEntry = 0x40, Ia2StateModal = 0x100,
|
|
Ia2StateMultiLine = 0x200, Ia2StateRequired = 0x800, Ia2StateSelectableText = 0x1000,
|
|
Ia2StateSingleLine = 0x2000, Ia2StateSupportsAutocompletion = 0x8000, Ia2StateCheckable = 0x40000,
|
|
};
|
|
|
|
typedef struct {
|
|
long start; // character offsets in the parent's text
|
|
long end;
|
|
long id; // IA2 uniqueID of the embedded child
|
|
} WaLink;
|
|
|
|
typedef struct {
|
|
char *type;
|
|
GArray *targets; // long uniqueIDs
|
|
} WaRelation;
|
|
|
|
typedef struct WaNode WaNode;
|
|
struct WaNode {
|
|
long id;
|
|
long msaaRole;
|
|
long msaaStates;
|
|
long ia2Role;
|
|
long ia2States;
|
|
char *name;
|
|
char *description;
|
|
char *value;
|
|
char *defaultAction; // MSAA default action, used when IA2 Action is absent
|
|
gboolean hasLocation;
|
|
int x, y, width, height;
|
|
GPtrArray *attributeKeys; // parallel arrays, provider order
|
|
GPtrArray *attributeValues;
|
|
gboolean hasText;
|
|
gunichar *characters;
|
|
long characterCount;
|
|
long caret; // character offset, -1 when none
|
|
GArray *boundaries[4]; // per UTF-16 offset: pairs of longs (start, end) from IA2, or NULL
|
|
GArray *links; // WaLink
|
|
GPtrArray *relations; // WaRelation *
|
|
GPtrArray *actions; // char *
|
|
gboolean isTable;
|
|
long rows, columns;
|
|
gboolean isCell;
|
|
long row, column, rowSpan, columnSpan;
|
|
GArray *columnHeaders; // long uniqueIDs
|
|
GArray *rowHeaders;
|
|
WaNode *parent;
|
|
GPtrArray *children; // WaNode *
|
|
AtkRole atkRole;
|
|
gboolean isFrame;
|
|
gboolean isApplication;
|
|
AtkObject *accessible;
|
|
AtkHyperlink *hyperlink;
|
|
// Live mode only.
|
|
gboolean defunct;
|
|
long selectionStart, selectionEnd; // characters, -1 when none
|
|
GHashTable *boundaryCache[4]; // UTF-16 offset -> packed segment from the worker
|
|
gboolean liveExtentsValid;
|
|
GArray *characterExtents; // 4 ints per UTF-16 offset, screen coordinates; NULL when unknown
|
|
};
|
|
|
|
static GHashTable *nodesById; // long id -> WaNode *
|
|
static WaNode *applicationNode;
|
|
static WaNode *frameNode;
|
|
static gboolean frameActive;
|
|
static const char *applicationName = "electron";
|
|
static long syntheticIds = -1000000000L;
|
|
static long focusedId;
|
|
static gboolean liveMode;
|
|
static unsigned long long x11Window; // browser's top-level X11 window, from the worker
|
|
static const int requestTimeoutMs = 300; // synchronous worker requests from ATK callbacks
|
|
|
|
static gboolean worker_request_async(const char *op, long id, const char *extraFields);
|
|
static JsonObject *worker_request_sync_timeout(const char *op, long id, const char *extraFields, int timeoutMs);
|
|
static JsonObject *worker_request_sync(const char *op, long id, const char *extraFields);
|
|
|
|
// ---------------------------------------------------------------- loading
|
|
|
|
static char *dup_member_string(JsonObject *object, const char *member) {
|
|
if (!json_object_has_member(object, member)) return NULL;
|
|
JsonNode *node = json_object_get_member(object, member);
|
|
if (JSON_NODE_HOLDS_NULL(node)) return NULL;
|
|
return g_strdup(json_node_get_string(node));
|
|
}
|
|
|
|
static long member_long(JsonObject *object, const char *member, long fallback) {
|
|
return json_object_has_member(object, member) ? (long)json_object_get_int_member(object, member) : fallback;
|
|
}
|
|
|
|
static GArray *id_array(JsonArray *array) {
|
|
GArray *result = g_array_new(FALSE, FALSE, sizeof(long));
|
|
for (guint i = 0; array && i < json_array_get_length(array); i++) {
|
|
long id = (long)json_array_get_int_element(array, i);
|
|
g_array_append_val(result, id);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// IA2 object attributes: "key:value;" pairs, with \ escaping : ; = , and \.
|
|
static void parse_attributes(WaNode *node, const char *text) {
|
|
node->attributeKeys = g_ptr_array_new_with_free_func(g_free);
|
|
node->attributeValues = g_ptr_array_new_with_free_func(g_free);
|
|
if (!text) return;
|
|
GString *key = g_string_new(NULL);
|
|
GString *value = g_string_new(NULL);
|
|
gboolean inValue = FALSE;
|
|
for (const char *cursor = text; *cursor; cursor++) {
|
|
char c = *cursor;
|
|
if (c == '\\' && cursor[1]) {
|
|
cursor++;
|
|
g_string_append_c(inValue ? value : key, *cursor);
|
|
} else if (c == ':' && !inValue) {
|
|
inValue = TRUE;
|
|
} else if (c == ';') {
|
|
if (key->len) {
|
|
g_ptr_array_add(node->attributeKeys, g_strdup(key->str));
|
|
g_ptr_array_add(node->attributeValues, g_strdup(value->str));
|
|
}
|
|
g_string_truncate(key, 0);
|
|
g_string_truncate(value, 0);
|
|
inValue = FALSE;
|
|
} else {
|
|
g_string_append_c(inValue ? value : key, c);
|
|
}
|
|
}
|
|
if (key->len) {
|
|
g_ptr_array_add(node->attributeKeys, g_strdup(key->str));
|
|
g_ptr_array_add(node->attributeValues, g_strdup(value->str));
|
|
}
|
|
g_string_free(key, TRUE);
|
|
g_string_free(value, TRUE);
|
|
}
|
|
|
|
static const char *attribute(WaNode *node, const char *key) {
|
|
for (guint i = 0; node->attributeKeys && i < node->attributeKeys->len; i++)
|
|
if (strcmp(g_ptr_array_index(node->attributeKeys, i), key) == 0) return g_ptr_array_index(node->attributeValues, i);
|
|
return NULL;
|
|
}
|
|
|
|
// Map a UTF-16 offset (Windows/IA2) to a character offset (AT-SPI). Returns
|
|
// -1 for an offset inside a surrogate pair or past the end.
|
|
static long utf16_to_character(const WaNode *node, long utf16Offset) {
|
|
long position = 0;
|
|
for (long i = 0; i <= node->characterCount; i++) {
|
|
if (position == utf16Offset) return i;
|
|
if (position > utf16Offset || i == node->characterCount) break;
|
|
position += node->characters[i] > 0xFFFF ? 2 : 1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Map a UTF-16 boundary to a character boundary, rounding an offset that falls
|
|
// inside a surrogate pair down (for a start) or up (for an end). Chromium's own
|
|
// word boundaries can end mid-pair next to an emoji; native ATK then reports
|
|
// end -1, which is a malformed range, so the publisher rounds instead.
|
|
static long utf16_boundary_to_character(const WaNode *node, long utf16Offset, gboolean roundUp) {
|
|
long position = 0;
|
|
for (long i = 0; i <= node->characterCount; i++) {
|
|
if (position == utf16Offset) return i;
|
|
if (i == node->characterCount) break;
|
|
long next = position + (node->characters[i] > 0xFFFF ? 2 : 1);
|
|
if (utf16Offset > position && utf16Offset < next) return roundUp ? i + 1 : i;
|
|
position = next;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static long character_to_utf16(const WaNode *node, long characterOffset) {
|
|
if (characterOffset < 0 || characterOffset > node->characterCount) return -1;
|
|
long position = 0;
|
|
for (long i = 0; i < characterOffset; i++) position += node->characters[i] > 0xFFFF ? 2 : 1;
|
|
return position;
|
|
}
|
|
|
|
static long utf16_length(const WaNode *node) {
|
|
long length = 0;
|
|
for (long i = 0; i < node->characterCount; i++) length += node->characters[i] > 0xFFFF ? 2 : 1;
|
|
return length;
|
|
}
|
|
|
|
// Properties only: identity, tree links and live state are not touched.
|
|
static void fill_node(WaNode *node, JsonObject *object) {
|
|
node->msaaRole = member_long(object, "msaaRole", 0);
|
|
node->msaaStates = member_long(object, "msaaStates", 0);
|
|
node->ia2Role = member_long(object, "ia2Role", node->msaaRole);
|
|
node->ia2States = member_long(object, "ia2States", 0);
|
|
node->name = dup_member_string(object, "name");
|
|
node->description = dup_member_string(object, "description");
|
|
gboolean isProtected = (node->msaaStates & MsaaStateProtected) != 0;
|
|
// The probe never records protected values; refuse them here as well.
|
|
node->value = isProtected ? NULL : dup_member_string(object, "value");
|
|
node->defaultAction = dup_member_string(object, "defaultAction");
|
|
if (json_object_has_member(object, "location")) {
|
|
JsonArray *location = json_object_get_array_member(object, "location");
|
|
node->hasLocation = json_array_get_length(location) == 4;
|
|
if (node->hasLocation) {
|
|
node->x = (int)json_array_get_int_element(location, 0);
|
|
node->y = (int)json_array_get_int_element(location, 1);
|
|
node->width = (int)json_array_get_int_element(location, 2);
|
|
node->height = (int)json_array_get_int_element(location, 3);
|
|
}
|
|
}
|
|
char *attributes = dup_member_string(object, "attributes");
|
|
parse_attributes(node, attributes);
|
|
g_free(attributes);
|
|
|
|
node->caret = -1;
|
|
long textLength = member_long(object, "textLength", -1);
|
|
if (textLength >= 0) {
|
|
node->hasText = TRUE;
|
|
if (isProtected) {
|
|
// Native Chromium presents password text as bullets; publish only the
|
|
// length, never the content.
|
|
node->characterCount = textLength;
|
|
node->characters = g_new(gunichar, textLength + 1);
|
|
for (long i = 0; i < textLength; i++) node->characters[i] = 0x2022;
|
|
} else {
|
|
char *text = dup_member_string(object, "text");
|
|
glong count = 0;
|
|
node->characters = g_utf8_to_ucs4(text ? text : "", -1, NULL, &count, NULL);
|
|
node->characterCount = count;
|
|
if (utf16_length(node) != textLength)
|
|
g_warning("node %ld: text is %ld UTF-16 units, provider reported %ld", node->id, utf16_length(node), textLength);
|
|
g_free(text);
|
|
long caret = member_long(object, "caret", -1);
|
|
if (caret >= 0) node->caret = utf16_to_character(node, caret);
|
|
// Layout-based boundaries recorded from IA2 get_textAtOffset, in UTF-16
|
|
// offsets, one segment per offset including the end of the text.
|
|
static const char *boundaryNames[4] = {"word", "sentence", "line", "paragraph"};
|
|
if (json_object_has_member(object, "boundaries")) {
|
|
JsonObject *boundaries = json_object_get_object_member(object, "boundaries");
|
|
for (int b = 0; b < 4; b++) {
|
|
if (!json_object_has_member(boundaries, boundaryNames[b])) continue;
|
|
JsonArray *segments = json_object_get_array_member(boundaries, boundaryNames[b]);
|
|
if ((long)json_array_get_length(segments) != textLength + 1) {
|
|
g_warning("node %ld: %s boundaries cover %u offsets, expected %ld; ignored", node->id, boundaryNames[b],
|
|
json_array_get_length(segments), textLength + 1);
|
|
continue;
|
|
}
|
|
node->boundaries[b] = g_array_new(FALSE, FALSE, sizeof(long));
|
|
for (guint i = 0; i < json_array_get_length(segments); i++) {
|
|
JsonArray *segment = json_array_get_array_element(segments, i);
|
|
long pair[2] = {(long)json_array_get_int_element(segment, 0), (long)json_array_get_int_element(segment, 1)};
|
|
g_array_append_vals(node->boundaries[b], pair, 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
node->links = g_array_new(FALSE, FALSE, sizeof(WaLink));
|
|
if (json_object_has_member(object, "hyperlinks")) {
|
|
JsonArray *links = json_object_get_array_member(object, "hyperlinks");
|
|
for (guint i = 0; i < json_array_get_length(links); i++) {
|
|
JsonNode *element = json_array_get_element(links, i);
|
|
if (JSON_NODE_HOLDS_NULL(element)) continue;
|
|
JsonArray *entry = json_node_get_array(element);
|
|
WaLink link = {
|
|
.start = utf16_to_character(node, (long)json_array_get_int_element(entry, 0)),
|
|
.end = utf16_to_character(node, (long)json_array_get_int_element(entry, 1)),
|
|
.id = (long)json_array_get_int_element(entry, 2),
|
|
};
|
|
if (link.start < 0 || link.end < 0) {
|
|
g_warning("node %ld: hyperlink %u has an offset outside the text; dropped", node->id, i);
|
|
continue;
|
|
}
|
|
g_array_append_val(node->links, link);
|
|
}
|
|
}
|
|
|
|
node->relations = g_ptr_array_new();
|
|
if (json_object_has_member(object, "relations")) {
|
|
JsonArray *relations = json_object_get_array_member(object, "relations");
|
|
for (guint i = 0; i < json_array_get_length(relations); i++) {
|
|
JsonObject *entry = json_array_get_object_element(relations, i);
|
|
WaRelation *relation = g_new0(WaRelation, 1);
|
|
relation->type = dup_member_string(entry, "type");
|
|
relation->targets = id_array(json_object_get_array_member(entry, "targets"));
|
|
g_ptr_array_add(node->relations, relation);
|
|
}
|
|
}
|
|
|
|
node->actions = g_ptr_array_new_with_free_func(g_free);
|
|
if (json_object_has_member(object, "actions")) {
|
|
JsonArray *actions = json_object_get_array_member(object, "actions");
|
|
for (guint i = 0; i < json_array_get_length(actions); i++) {
|
|
JsonNode *element = json_array_get_element(actions, i);
|
|
g_ptr_array_add(node->actions, g_strdup(JSON_NODE_HOLDS_NULL(element) ? "" : json_node_get_string(element)));
|
|
}
|
|
}
|
|
|
|
if (json_object_has_member(object, "table")) {
|
|
JsonObject *table = json_object_get_object_member(object, "table");
|
|
node->isTable = TRUE;
|
|
node->rows = member_long(table, "rows", 0);
|
|
node->columns = member_long(table, "columns", 0);
|
|
}
|
|
if (json_object_has_member(object, "cell")) {
|
|
JsonObject *cell = json_object_get_object_member(object, "cell");
|
|
node->isCell = TRUE;
|
|
node->row = member_long(cell, "row", 0);
|
|
node->column = member_long(cell, "column", 0);
|
|
node->rowSpan = member_long(cell, "rowSpan", 1);
|
|
node->columnSpan = member_long(cell, "columnSpan", 1);
|
|
node->columnHeaders = id_array(json_object_has_member(cell, "columnHeaders")
|
|
? json_object_get_array_member(cell, "columnHeaders") : NULL);
|
|
node->rowHeaders = id_array(json_object_has_member(cell, "rowHeaders")
|
|
? json_object_get_array_member(cell, "rowHeaders") : NULL);
|
|
}
|
|
|
|
}
|
|
|
|
static WaNode *new_node(JsonObject *object) {
|
|
WaNode *node = g_new0(WaNode, 1);
|
|
node->id = member_long(object, "id", 0);
|
|
if (node->id == 0) node->id = syntheticIds--;
|
|
node->selectionStart = node->selectionEnd = -1;
|
|
fill_node(node, object);
|
|
return node;
|
|
}
|
|
|
|
static void free_node_properties(WaNode *node) {
|
|
g_clear_pointer(&node->name, g_free);
|
|
g_clear_pointer(&node->description, g_free);
|
|
g_clear_pointer(&node->value, g_free);
|
|
g_clear_pointer(&node->defaultAction, g_free);
|
|
g_clear_pointer(&node->attributeKeys, g_ptr_array_unref);
|
|
g_clear_pointer(&node->attributeValues, g_ptr_array_unref);
|
|
g_clear_pointer(&node->characters, g_free);
|
|
for (int b = 0; b < 4; b++) g_clear_pointer(&node->boundaries[b], g_array_unref);
|
|
g_clear_pointer(&node->links, g_array_unref);
|
|
if (node->relations) {
|
|
for (guint i = 0; i < node->relations->len; i++) {
|
|
WaRelation *relation = g_ptr_array_index(node->relations, i);
|
|
g_free(relation->type);
|
|
g_array_unref(relation->targets);
|
|
g_free(relation);
|
|
}
|
|
g_clear_pointer(&node->relations, g_ptr_array_unref);
|
|
}
|
|
g_clear_pointer(&node->actions, g_ptr_array_unref);
|
|
g_clear_pointer(&node->columnHeaders, g_array_unref);
|
|
g_clear_pointer(&node->rowHeaders, g_array_unref);
|
|
}
|
|
|
|
static WaNode *node_for_id(long id);
|
|
|
|
static WaNode *load_node(JsonObject *object, WaNode *parent) {
|
|
WaNode *node = new_node(object);
|
|
node->parent = parent;
|
|
WaNode *previous = node_for_id(node->id);
|
|
if (previous && !previous->defunct) g_warning("duplicate uniqueID %ld", node->id);
|
|
// The key lives inside the node. Replacing just the value would retain
|
|
// the old node's key after that node is retired and freed.
|
|
g_hash_table_replace(nodesById, &node->id, node);
|
|
|
|
node->children = g_ptr_array_new();
|
|
JsonArray *children = json_object_get_array_member(object, "children");
|
|
for (guint i = 0; children && i < json_array_get_length(children); i++)
|
|
g_ptr_array_add(node->children, load_node(json_array_get_object_element(children, i), node));
|
|
return node;
|
|
}
|
|
|
|
static WaNode *node_for_id(long id) {
|
|
return g_hash_table_lookup(nodesById, &id);
|
|
}
|
|
|
|
// "click ancestor" -> "clickAncestor", matching native action names.
|
|
static char *camel_case(const char *text) {
|
|
GString *result = g_string_new(NULL);
|
|
gboolean upperNext = FALSE;
|
|
for (const char *cursor = text; *cursor; cursor++) {
|
|
if (*cursor == ' ') {
|
|
upperNext = result->len > 0;
|
|
continue;
|
|
}
|
|
g_string_append_c(result, upperNext ? g_ascii_toupper(*cursor) : g_ascii_tolower(*cursor));
|
|
upperNext = FALSE;
|
|
}
|
|
return g_string_free(result, FALSE);
|
|
}
|
|
|
|
// Objects outside any web document belong to Chromium's views layer (browser
|
|
// UI). Windows omits their IA2 actions and adds internal "view_N" ids; native
|
|
// Linux Chromium gives them the MSAA default action plus showContextMenu and
|
|
// no internal id. Never applied inside web content.
|
|
static void adapt_views_node(WaNode *node) {
|
|
if (node->actions->len == 0 && !node->isApplication) {
|
|
g_ptr_array_add(node->actions, node->defaultAction ? camel_case(node->defaultAction) : g_strdup("doDefault"));
|
|
g_ptr_array_add(node->actions, g_strdup("showContextMenu"));
|
|
}
|
|
for (guint i = 0; i < node->attributeKeys->len; i++) {
|
|
const char *value = g_ptr_array_index(node->attributeValues, i);
|
|
if (strcmp(g_ptr_array_index(node->attributeKeys, i), "id") == 0 && g_str_has_prefix(value, "view_")) {
|
|
g_ptr_array_remove_index(node->attributeKeys, i);
|
|
g_ptr_array_remove_index(node->attributeValues, i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void adapt_views_layer(WaNode *node) {
|
|
if (node->msaaRole == MsaaRoleDocument) return;
|
|
adapt_views_node(node);
|
|
for (guint i = 0; i < node->children->len; i++) adapt_views_layer(g_ptr_array_index(node->children, i));
|
|
}
|
|
|
|
static gboolean inside_document(WaNode *node) {
|
|
for (WaNode *current = node; current; current = current->parent)
|
|
if (current->msaaRole == MsaaRoleDocument) return TRUE;
|
|
return FALSE;
|
|
}
|
|
|
|
// Chromium's views layer (browser UI above the web document) answers
|
|
// IAccessibleHypertext::get_nHyperlinks with an error, yet its text is one
|
|
// embedded-object character per child, and native Chromium exposes those as
|
|
// links. Rebuild them only when that shape is exact.
|
|
static void synthesize_view_links_node(WaNode *node) {
|
|
if (node->links->len == 0 && node->hasText && node->characterCount > 0 &&
|
|
node->characterCount == (long)node->children->len) {
|
|
gboolean allEmbedded = TRUE;
|
|
for (long i = 0; i < node->characterCount; i++)
|
|
if (node->characters[i] != 0xFFFC) allEmbedded = FALSE;
|
|
if (allEmbedded) {
|
|
for (guint i = 0; i < node->children->len; i++) {
|
|
WaNode *child = g_ptr_array_index(node->children, i);
|
|
WaLink link = {.start = i, .end = i + 1, .id = child->id};
|
|
g_array_append_val(node->links, link);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void synthesize_view_links(WaNode *node) {
|
|
synthesize_view_links_node(node);
|
|
for (guint i = 0; i < node->children->len; i++) synthesize_view_links(g_ptr_array_index(node->children, i));
|
|
}
|
|
|
|
// ---------------------------------------------------------------- mapping
|
|
|
|
// Roles follow native Linux Chromium for the same page. Pairings confirmed by
|
|
// the fixture comparison are marked "verified"; the rest come from Chromium's
|
|
// documented mappings and still need a fixture case.
|
|
static AtkRole map_role(WaNode *node) {
|
|
const char *xmlRoles = attribute(node, "xml-roles");
|
|
switch (node->ia2Role) {
|
|
case Ia2RoleHeading: return ATK_ROLE_HEADING; // verified
|
|
case Ia2RoleParagraph: return ATK_ROLE_PARAGRAPH; // verified
|
|
case Ia2RoleLabel: return ATK_ROLE_LABEL; // verified
|
|
case Ia2RoleCaption: return ATK_ROLE_CAPTION; // verified
|
|
case Ia2RoleLandmark: return ATK_ROLE_LANDMARK; // verified (main)
|
|
case Ia2RoleSection: // verified (div, form)
|
|
return xmlRoles && strcmp(xmlRoles, "form") == 0 ? ATK_ROLE_FORM : ATK_ROLE_SECTION;
|
|
case Ia2RoleForm: return ATK_ROLE_FORM;
|
|
case Ia2RoleFooter: return ATK_ROLE_FOOTER;
|
|
case Ia2RoleHeader: return ATK_ROLE_HEADER;
|
|
case Ia2RoleBlockQuote: return ATK_ROLE_BLOCK_QUOTE;
|
|
case Ia2RoleMark: return ATK_ROLE_MARK;
|
|
case Ia2RoleComplementary: return ATK_ROLE_LANDMARK;
|
|
case Ia2RoleEmbeddedObject: return ATK_ROLE_EMBEDDED;
|
|
case Ia2RoleInternalFrame: return ATK_ROLE_INTERNAL_FRAME;
|
|
case Ia2RoleToggleButton: return ATK_ROLE_TOGGLE_BUTTON;
|
|
case Ia2RoleCanvas: return ATK_ROLE_CANVAS;
|
|
case Ia2RolePage: return ATK_ROLE_PAGE;
|
|
case Ia2RoleNote: return ATK_ROLE_COMMENT;
|
|
default: break;
|
|
}
|
|
switch (node->msaaRole) {
|
|
case MsaaRoleDocument: return ATK_ROLE_DOCUMENT_WEB; // verified
|
|
case MsaaRolePane: // verified (views)
|
|
case MsaaRoleClient: return ATK_ROLE_PANEL; // verified (WebView)
|
|
case MsaaRoleMenuBar: return ATK_ROLE_MENU_BAR; // verified
|
|
case MsaaRoleButtonMenu: return ATK_ROLE_PUSH_BUTTON; // verified (SubmenuButton)
|
|
case MsaaRolePushButton: return ATK_ROLE_PUSH_BUTTON; // verified
|
|
case MsaaRoleCheckButton: return ATK_ROLE_CHECK_BOX; // verified
|
|
case MsaaRoleLink: return ATK_ROLE_LINK; // verified
|
|
case MsaaRoleStaticText: return ATK_ROLE_STATIC; // verified
|
|
case MsaaRoleText: // verified
|
|
return (node->msaaStates & MsaaStateProtected) ? ATK_ROLE_PASSWORD_TEXT : ATK_ROLE_ENTRY;
|
|
case MsaaRoleList: return ATK_ROLE_LIST; // verified
|
|
case MsaaRoleListItem: return ATK_ROLE_LIST_ITEM; // verified
|
|
case MsaaRoleTable: return ATK_ROLE_TABLE; // verified
|
|
case MsaaRoleRow: return ATK_ROLE_TABLE_ROW; // verified
|
|
case MsaaRoleColumnHeader: return ATK_ROLE_COLUMN_HEADER; // verified
|
|
case MsaaRoleRowHeader: return ATK_ROLE_ROW_HEADER; // verified
|
|
case MsaaRoleCell: return ATK_ROLE_TABLE_CELL; // verified
|
|
case MsaaRoleRadioButton: return ATK_ROLE_RADIO_BUTTON;
|
|
case MsaaRoleComboBox: return ATK_ROLE_COMBO_BOX;
|
|
case MsaaRoleMenuPopup: return ATK_ROLE_MENU;
|
|
case MsaaRoleMenuItem: return ATK_ROLE_MENU_ITEM;
|
|
case MsaaRoleGraphic: return ATK_ROLE_IMAGE;
|
|
case MsaaRoleSeparator: return ATK_ROLE_SEPARATOR;
|
|
case MsaaRoleToolBar: return ATK_ROLE_TOOL_BAR;
|
|
case MsaaRoleStatusBar: return ATK_ROLE_STATUSBAR;
|
|
case MsaaRoleDialog: return ATK_ROLE_DIALOG;
|
|
case MsaaRoleAlert: return ATK_ROLE_ALERT;
|
|
case MsaaRoleToolTip: return ATK_ROLE_TOOL_TIP;
|
|
case MsaaRoleWindow: return ATK_ROLE_WINDOW;
|
|
case MsaaRoleOutline: return ATK_ROLE_TREE;
|
|
case MsaaRoleOutlineItem: return ATK_ROLE_TREE_ITEM;
|
|
case MsaaRolePageTab: return ATK_ROLE_PAGE_TAB;
|
|
case MsaaRolePageTabList: return ATK_ROLE_PAGE_TAB_LIST;
|
|
case MsaaRoleProgressBar: return ATK_ROLE_PROGRESS_BAR;
|
|
case MsaaRoleSlider: return ATK_ROLE_SLIDER;
|
|
case MsaaRoleSpinButton: return ATK_ROLE_SPIN_BUTTON;
|
|
case MsaaRoleGrouping: return ATK_ROLE_PANEL;
|
|
default: break;
|
|
}
|
|
g_warning("node %ld: no AT-SPI role for MSAA role %ld / IA2 role 0x%lx", node->id, node->msaaRole, node->ia2Role);
|
|
return ATK_ROLE_UNKNOWN;
|
|
}
|
|
|
|
static gboolean is_text_field(AtkRole role) {
|
|
return role == ATK_ROLE_ENTRY || role == ATK_ROLE_PASSWORD_TEXT;
|
|
}
|
|
|
|
// Text leaves (DOM text nodes) are static text without an element tag.
|
|
// Verified for text nodes and the ::marker pseudo-element; <br> still needs a
|
|
// fixture case.
|
|
static gboolean is_text_leaf(WaNode *node) {
|
|
return node->msaaRole == MsaaRoleStaticText && !attribute(node, "tag");
|
|
}
|
|
|
|
// ---------------------------------------------------------------- GObject types
|
|
|
|
enum {
|
|
IfaceComponent = 1 << 0, IfaceAction = 1 << 1, IfaceText = 1 << 2, IfaceHypertext = 1 << 3,
|
|
IfaceHyperlinkImpl = 1 << 4, IfaceTable = 1 << 5, IfaceTableCell = 1 << 6, IfaceDocument = 1 << 7,
|
|
IfaceSelection = 1 << 8, IfaceWindow = 1 << 9,
|
|
};
|
|
|
|
typedef struct {
|
|
AtkObject parent;
|
|
WaNode *node;
|
|
} WaAccessible;
|
|
|
|
typedef struct {
|
|
AtkObjectClass parentClass;
|
|
} WaAccessibleClass;
|
|
|
|
static WaNode *node_of(gpointer object) {
|
|
return ((WaAccessible *)object)->node;
|
|
}
|
|
|
|
static const gchar *wa_get_name(AtkObject *object) {
|
|
WaNode *node = node_of(object);
|
|
return node->name ? node->name : "";
|
|
}
|
|
|
|
static const gchar *wa_get_description(AtkObject *object) {
|
|
WaNode *node = node_of(object);
|
|
return node->description ? node->description : "";
|
|
}
|
|
|
|
static gint wa_get_n_children(AtkObject *object) {
|
|
return (gint)node_of(object)->children->len;
|
|
}
|
|
|
|
static AtkObject *wa_ref_child(AtkObject *object, gint index) {
|
|
WaNode *node = node_of(object);
|
|
if (index < 0 || (guint)index >= node->children->len) return NULL;
|
|
WaNode *child = g_ptr_array_index(node->children, index);
|
|
return g_object_ref(child->accessible);
|
|
}
|
|
|
|
static gint wa_get_index_in_parent(AtkObject *object) {
|
|
WaNode *node = node_of(object);
|
|
if (!node->parent) return -1;
|
|
for (guint i = 0; i < node->parent->children->len; i++)
|
|
if (g_ptr_array_index(node->parent->children, i) == node) return (gint)i;
|
|
return -1;
|
|
}
|
|
|
|
static AtkStateSet *wa_ref_state_set(AtkObject *object) {
|
|
WaNode *node = node_of(object);
|
|
AtkStateSet *states = atk_state_set_new();
|
|
if (node->defunct) {
|
|
atk_state_set_add_state(states, ATK_STATE_DEFUNCT);
|
|
return states;
|
|
}
|
|
long msaa = node->msaaStates, ia2 = node->ia2States;
|
|
if (!(msaa & MsaaStateUnavailable)) {
|
|
atk_state_set_add_state(states, ATK_STATE_ENABLED);
|
|
atk_state_set_add_state(states, ATK_STATE_SENSITIVE);
|
|
}
|
|
if (!(msaa & MsaaStateInvisible)) atk_state_set_add_state(states, ATK_STATE_VISIBLE);
|
|
if (!(msaa & (MsaaStateInvisible | MsaaStateOffscreen))) atk_state_set_add_state(states, ATK_STATE_SHOWING);
|
|
if (msaa & MsaaStateFocusable) atk_state_set_add_state(states, ATK_STATE_FOCUSABLE);
|
|
// Windows Chromium can report focus on both a views object and the web
|
|
// document; AT-SPI needs exactly one, the end of the window's accFocus chain.
|
|
if (node->id == focusedId) atk_state_set_add_state(states, ATK_STATE_FOCUSED);
|
|
if (msaa & MsaaStateChecked) atk_state_set_add_state(states, ATK_STATE_CHECKED);
|
|
if (msaa & MsaaStatePressed) atk_state_set_add_state(states, ATK_STATE_PRESSED);
|
|
if (msaa & MsaaStateSelected) atk_state_set_add_state(states, ATK_STATE_SELECTED);
|
|
if (msaa & MsaaStateSelectable) atk_state_set_add_state(states, ATK_STATE_SELECTABLE);
|
|
if (msaa & MsaaStateExpanded) atk_state_set_add_state(states, ATK_STATE_EXPANDED);
|
|
if (msaa & MsaaStateCollapsed) atk_state_set_add_state(states, ATK_STATE_COLLAPSED);
|
|
if (msaa & MsaaStateBusy) atk_state_set_add_state(states, ATK_STATE_BUSY);
|
|
if (msaa & MsaaStateMultiSelectable) atk_state_set_add_state(states, ATK_STATE_MULTISELECTABLE);
|
|
// Windows Chromium marks every non-editable document and list read-only;
|
|
// native Linux Chromium only reports it for read-only form fields.
|
|
if ((msaa & MsaaStateReadOnly) && is_text_field(node->atkRole)) atk_state_set_add_state(states, ATK_STATE_READ_ONLY);
|
|
if (ia2 & Ia2StateEditable) atk_state_set_add_state(states, ATK_STATE_EDITABLE);
|
|
if (ia2 & Ia2StateMultiLine) atk_state_set_add_state(states, ATK_STATE_MULTI_LINE);
|
|
if (ia2 & Ia2StateSingleLine) atk_state_set_add_state(states, ATK_STATE_SINGLE_LINE);
|
|
if (ia2 & Ia2StateSelectableText) atk_state_set_add_state(states, ATK_STATE_SELECTABLE_TEXT);
|
|
if (ia2 & Ia2StateRequired) atk_state_set_add_state(states, ATK_STATE_REQUIRED);
|
|
if (ia2 & Ia2StateInvalidEntry) atk_state_set_add_state(states, ATK_STATE_INVALID_ENTRY);
|
|
if (ia2 & Ia2StateCheckable) atk_state_set_add_state(states, ATK_STATE_CHECKABLE);
|
|
if (ia2 & Ia2StateModal) atk_state_set_add_state(states, ATK_STATE_MODAL);
|
|
if (ia2 & Ia2StateSupportsAutocompletion) atk_state_set_add_state(states, ATK_STATE_SUPPORTS_AUTOCOMPLETION);
|
|
if (ia2 & Ia2StateDefunct) atk_state_set_add_state(states, ATK_STATE_DEFUNCT);
|
|
const char *hasPopup = attribute(node, "haspopup");
|
|
if (hasPopup && strcmp(hasPopup, "false") != 0) atk_state_set_add_state(states, ATK_STATE_HAS_POPUP);
|
|
if (node->isFrame && frameActive) atk_state_set_add_state(states, ATK_STATE_ACTIVE);
|
|
if (node->isApplication) atk_state_set_add_state(states, ATK_STATE_FOCUSABLE);
|
|
return states;
|
|
}
|
|
|
|
static AtkAttributeSet *wa_get_attributes(AtkObject *object) {
|
|
WaNode *node = node_of(object);
|
|
AtkAttributeSet *set = NULL;
|
|
for (guint i = 0; node->attributeKeys && i < node->attributeKeys->len; i++) {
|
|
AtkAttribute *entry = g_new(AtkAttribute, 1);
|
|
entry->name = g_strdup(g_ptr_array_index(node->attributeKeys, i));
|
|
entry->value = g_strdup(g_ptr_array_index(node->attributeValues, i));
|
|
set = g_slist_append(set, entry);
|
|
}
|
|
// Lets the reader tie this frame to the browser's X11 window (see the
|
|
// Cthulhu ownership rule in browser.md).
|
|
if (node->isFrame && x11Window) {
|
|
AtkAttribute *entry = g_new(AtkAttribute, 1);
|
|
entry->name = g_strdup("wine-access-x11-window");
|
|
entry->value = g_strdup_printf("%llu", x11Window);
|
|
set = g_slist_append(set, entry);
|
|
}
|
|
return set;
|
|
}
|
|
|
|
static AtkRelationType map_relation(const char *type) {
|
|
static const struct { const char *ia2; AtkRelationType atk; } table[] = {
|
|
{"labelledBy", ATK_RELATION_LABELLED_BY}, {"labelFor", ATK_RELATION_LABEL_FOR},
|
|
{"describedBy", ATK_RELATION_DESCRIBED_BY}, {"descriptionFor", ATK_RELATION_DESCRIPTION_FOR},
|
|
{"controllerFor", ATK_RELATION_CONTROLLER_FOR}, {"controlledBy", ATK_RELATION_CONTROLLED_BY},
|
|
{"flowsTo", ATK_RELATION_FLOWS_TO}, {"flowsFrom", ATK_RELATION_FLOWS_FROM},
|
|
{"memberOf", ATK_RELATION_MEMBER_OF}, {"nodeChildOf", ATK_RELATION_NODE_CHILD_OF},
|
|
{"nodeParentOf", ATK_RELATION_NODE_PARENT_OF}, {"embeds", ATK_RELATION_EMBEDS},
|
|
{"embeddedBy", ATK_RELATION_EMBEDDED_BY}, {"popupFor", ATK_RELATION_POPUP_FOR},
|
|
{"subwindowOf", ATK_RELATION_SUBWINDOW_OF}, {"details", ATK_RELATION_DETAILS},
|
|
{"detailsFor", ATK_RELATION_DETAILS_FOR}, {"error", ATK_RELATION_ERROR_MESSAGE},
|
|
{"errorFor", ATK_RELATION_ERROR_FOR},
|
|
};
|
|
for (size_t i = 0; type && i < G_N_ELEMENTS(table); i++)
|
|
if (strcmp(type, table[i].ia2) == 0) return table[i].atk;
|
|
return ATK_RELATION_NULL;
|
|
}
|
|
|
|
static void add_relation(AtkRelationSet *set, AtkRelationType type, WaNode *target) {
|
|
if (target && target->accessible) atk_relation_set_add_relation_by_type(set, type, target->accessible);
|
|
}
|
|
|
|
static AtkRelationSet *wa_ref_relation_set(AtkObject *object) {
|
|
WaNode *node = node_of(object);
|
|
AtkRelationSet *set = atk_relation_set_new();
|
|
for (guint i = 0; i < node->relations->len; i++) {
|
|
WaRelation *relation = g_ptr_array_index(node->relations, i);
|
|
AtkRelationType type = map_relation(relation->type);
|
|
if (type == ATK_RELATION_NULL) {
|
|
g_warning("node %ld: unmapped relation %s", node->id, relation->type ? relation->type : "(null)");
|
|
continue;
|
|
}
|
|
for (guint j = 0; j < relation->targets->len; j++)
|
|
add_relation(set, type, node_for_id(g_array_index(relation->targets, long, j)));
|
|
}
|
|
// Native Chromium relates the frame and the top document, which IA2 does
|
|
// not report across that boundary.
|
|
if (node->isFrame) {
|
|
GHashTableIter iter;
|
|
gpointer key, value;
|
|
g_hash_table_iter_init(&iter, nodesById);
|
|
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
|
WaNode *candidate = value;
|
|
if (candidate->atkRole == ATK_ROLE_DOCUMENT_WEB) add_relation(set, ATK_RELATION_EMBEDS, candidate);
|
|
}
|
|
}
|
|
if (node->atkRole == ATK_ROLE_DOCUMENT_WEB) add_relation(set, ATK_RELATION_EMBEDDED_BY, frameNode);
|
|
return set;
|
|
}
|
|
|
|
static gpointer accessibleParentClass;
|
|
|
|
// Defunct nodes are owned by their accessible from then on: free them when the
|
|
// last reference (ours or atk-bridge's) goes away.
|
|
static void wa_accessible_finalize(GObject *object) {
|
|
WaNode *node = node_of(object);
|
|
if (node && node->defunct) {
|
|
free_node_properties(node);
|
|
g_clear_pointer(&node->children, g_ptr_array_unref);
|
|
for (int b = 0; b < 4; b++) g_clear_pointer(&node->boundaryCache[b], g_hash_table_unref);
|
|
g_clear_pointer(&node->characterExtents, g_array_unref);
|
|
g_free(node);
|
|
}
|
|
G_OBJECT_CLASS(accessibleParentClass)->finalize(object);
|
|
}
|
|
|
|
static void wa_accessible_class_init(gpointer klass, gpointer data) {
|
|
(void)data;
|
|
if (!accessibleParentClass) accessibleParentClass = g_type_class_peek_parent(klass);
|
|
G_OBJECT_CLASS(klass)->finalize = wa_accessible_finalize;
|
|
AtkObjectClass *objectClass = ATK_OBJECT_CLASS(klass);
|
|
objectClass->get_name = wa_get_name;
|
|
objectClass->get_description = wa_get_description;
|
|
objectClass->get_n_children = wa_get_n_children;
|
|
objectClass->ref_child = wa_ref_child;
|
|
objectClass->get_index_in_parent = wa_get_index_in_parent;
|
|
objectClass->ref_state_set = wa_ref_state_set;
|
|
objectClass->get_attributes = wa_get_attributes;
|
|
objectClass->ref_relation_set = wa_ref_relation_set;
|
|
}
|
|
|
|
// ---- AtkComponent
|
|
|
|
// The frame's extents must be the browser's X11 window geometry: readers
|
|
// compare them with the window manager's view of the foreground window, and
|
|
// Wine's client area can differ from it.
|
|
static Display *xDisplay;
|
|
static gboolean xDisplayTried;
|
|
|
|
static int ignore_x_error(Display *display, XErrorEvent *event) {
|
|
(void)display;
|
|
(void)event;
|
|
return 0; // the window may vanish at any time; fall back to Windows geometry
|
|
}
|
|
|
|
static gboolean x11_window_extents(int *x, int *y, int *width, int *height) {
|
|
if (!x11Window) return FALSE;
|
|
if (!xDisplayTried) {
|
|
xDisplayTried = TRUE;
|
|
xDisplay = XOpenDisplay(NULL);
|
|
if (xDisplay) XSetErrorHandler(ignore_x_error);
|
|
}
|
|
if (!xDisplay) return FALSE;
|
|
XWindowAttributes attributes;
|
|
Window child;
|
|
if (!XGetWindowAttributes(xDisplay, (Window)x11Window, &attributes)) return FALSE;
|
|
if (!XTranslateCoordinates(xDisplay, (Window)x11Window, attributes.root, 0, 0, x, y, &child)) return FALSE;
|
|
*width = attributes.width;
|
|
*height = attributes.height;
|
|
return *width > 0 && *height > 0;
|
|
}
|
|
|
|
// Positions change with scrolling and layout; in live mode fetch them on
|
|
// demand and keep them until the worker reports a layout change.
|
|
static void refresh_live_extents(WaNode *node) {
|
|
if (!liveMode || node->defunct || node->isApplication || node->liveExtentsValid) return;
|
|
if (node->isFrame && x11_window_extents(&node->x, &node->y, &node->width, &node->height)) {
|
|
node->hasLocation = TRUE;
|
|
node->liveExtentsValid = TRUE;
|
|
return;
|
|
}
|
|
JsonObject *reply = worker_request_sync("extents", node->id, NULL);
|
|
if (!reply) return;
|
|
if (json_object_get_boolean_member_with_default(reply, "ok", FALSE) && json_object_has_member(reply, "extents")) {
|
|
JsonArray *extents = json_object_get_array_member(reply, "extents");
|
|
if (json_array_get_length(extents) == 4) {
|
|
node->x = (int)json_array_get_int_element(extents, 0);
|
|
node->y = (int)json_array_get_int_element(extents, 1);
|
|
node->width = (int)json_array_get_int_element(extents, 2);
|
|
node->height = (int)json_array_get_int_element(extents, 3);
|
|
node->hasLocation = TRUE;
|
|
node->liveExtentsValid = TRUE;
|
|
}
|
|
}
|
|
json_object_unref(reply);
|
|
}
|
|
|
|
static void wa_get_extents(AtkComponent *component, gint *x, gint *y, gint *width, gint *height,
|
|
AtkCoordType coordType) {
|
|
WaNode *node = node_of(component);
|
|
refresh_live_extents(node);
|
|
if (coordType != ATK_XY_SCREEN && frameNode) refresh_live_extents(frameNode);
|
|
if (coordType == ATK_XY_PARENT && node->parent) refresh_live_extents(node->parent);
|
|
if (!node->hasLocation) {
|
|
*x = *y = *width = *height = -1;
|
|
return;
|
|
}
|
|
*x = node->x;
|
|
*y = node->y;
|
|
*width = node->width;
|
|
*height = node->height;
|
|
if (coordType == ATK_XY_WINDOW && frameNode && frameNode->hasLocation) {
|
|
*x -= frameNode->x;
|
|
*y -= frameNode->y;
|
|
} else if (coordType == ATK_XY_PARENT && node->parent && node->parent->hasLocation) {
|
|
*x -= node->parent->x;
|
|
*y -= node->parent->y;
|
|
}
|
|
}
|
|
|
|
static gboolean wa_component_grab_focus(AtkComponent *component) {
|
|
WaNode *node = node_of(component);
|
|
if (liveMode && !node->defunct) return worker_request_async("focus", node->id, NULL);
|
|
g_message("grab_focus requested for node %ld (snapshot mode)", node->id);
|
|
return FALSE;
|
|
}
|
|
|
|
// ATK and IA2 scroll types have the same values and order.
|
|
static gboolean wa_component_scroll_to(AtkComponent *component, AtkScrollType type) {
|
|
WaNode *node = node_of(component);
|
|
if (!liveMode || node->defunct) return FALSE;
|
|
char fields[48];
|
|
snprintf(fields, sizeof(fields), ",\"scrollType\":%d", (int)type);
|
|
return worker_request_async("scrollTo", node->id, fields);
|
|
}
|
|
|
|
static void wa_component_init(gpointer ifacePointer, gpointer data) {
|
|
AtkComponentIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->get_extents = wa_get_extents;
|
|
iface->grab_focus = wa_component_grab_focus;
|
|
iface->scroll_to = wa_component_scroll_to;
|
|
}
|
|
|
|
// ---- AtkAction
|
|
|
|
static gint wa_get_n_actions(AtkAction *action) {
|
|
return (gint)node_of(action)->actions->len;
|
|
}
|
|
|
|
static const gchar *wa_action_get_name(AtkAction *action, gint index) {
|
|
WaNode *node = node_of(action);
|
|
return index >= 0 && (guint)index < node->actions->len ? g_ptr_array_index(node->actions, index) : NULL;
|
|
}
|
|
|
|
// Actions run asynchronously, as in native Chromium: report acceptance now and
|
|
// let the resulting events describe the outcome.
|
|
static gboolean wa_do_action(AtkAction *action, gint index) {
|
|
WaNode *node = node_of(action);
|
|
if (index < 0 || (guint)index >= node->actions->len) return FALSE;
|
|
if (liveMode && !node->defunct) {
|
|
char fields[32];
|
|
snprintf(fields, sizeof(fields), ",\"index\":%d", index);
|
|
return worker_request_async("action", node->id, fields);
|
|
}
|
|
g_message("do_action %d (%s) requested for node %ld (snapshot mode)", index, wa_action_get_name(action, index),
|
|
node->id);
|
|
return FALSE;
|
|
}
|
|
|
|
static void wa_action_init(gpointer ifacePointer, gpointer data) {
|
|
AtkActionIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->get_n_actions = wa_get_n_actions;
|
|
iface->get_name = wa_action_get_name;
|
|
iface->get_localized_name = wa_action_get_name;
|
|
iface->do_action = wa_do_action;
|
|
}
|
|
|
|
// ---- AtkText
|
|
|
|
static gchar *slice(WaNode *node, long start, long end) {
|
|
if (end < 0 || end > node->characterCount) end = node->characterCount;
|
|
if (start < 0) start = 0;
|
|
if (start > end) start = end;
|
|
return g_ucs4_to_utf8(node->characters + start, end - start, NULL, NULL, NULL);
|
|
}
|
|
|
|
static gchar *wa_get_text(AtkText *text, gint start, gint end) {
|
|
return slice(node_of(text), start, end);
|
|
}
|
|
|
|
static gint wa_get_character_count(AtkText *text) {
|
|
return (gint)node_of(text)->characterCount;
|
|
}
|
|
|
|
static gint wa_get_caret_offset(AtkText *text) {
|
|
WaNode *node = node_of(text);
|
|
return (gint)MIN(node->caret, node->characterCount);
|
|
}
|
|
|
|
static gunichar wa_get_character_at_offset(AtkText *text, gint offset) {
|
|
WaNode *node = node_of(text);
|
|
return offset >= 0 && offset < node->characterCount ? node->characters[offset] : 0;
|
|
}
|
|
|
|
static gboolean is_space(gunichar c) {
|
|
return g_unichar_isspace(c);
|
|
}
|
|
|
|
// Ask the worker for one IA2 boundary segment, cached per granularity until the
|
|
// text changes (and, for lines, until layout changes). Offsets are UTF-16.
|
|
static const long ia2BoundaryTypes[4] = {1, 2, 4, 3}; // word, sentence, line, paragraph
|
|
|
|
static gboolean live_boundary(WaNode *node, long utf16Offset, int index, long *start16, long *end16) {
|
|
if (!liveMode || node->defunct || (node->msaaStates & MsaaStateProtected)) return FALSE;
|
|
if (!node->boundaryCache[index]) node->boundaryCache[index] = g_hash_table_new(g_direct_hash, g_direct_equal);
|
|
gpointer packed;
|
|
if (g_hash_table_lookup_extended(node->boundaryCache[index], GSIZE_TO_POINTER(utf16Offset), NULL, &packed)) {
|
|
guint64 value = GPOINTER_TO_SIZE(packed);
|
|
*start16 = (long)(gint32)(guint32)(value >> 32) - 1;
|
|
*end16 = (long)(gint32)(guint32)(value & 0xffffffffu) - 1;
|
|
return TRUE;
|
|
}
|
|
char fields[80];
|
|
snprintf(fields, sizeof(fields), ",\"offset\":%ld,\"granularity\":%ld", utf16Offset, ia2BoundaryTypes[index]);
|
|
JsonObject *reply = worker_request_sync("boundary", node->id, fields);
|
|
if (!reply) return FALSE;
|
|
gboolean ok = json_object_get_boolean_member_with_default(reply, "ok", FALSE);
|
|
if (ok) {
|
|
*start16 = (long)json_object_get_int_member_with_default(reply, "start", -1);
|
|
*end16 = (long)json_object_get_int_member_with_default(reply, "end", -1);
|
|
guint64 value = ((guint64)(guint32)(*start16 + 1) << 32) | (guint32)(*end16 + 1);
|
|
g_hash_table_insert(node->boundaryCache[index], GSIZE_TO_POINTER(utf16Offset), GSIZE_TO_POINTER(value));
|
|
}
|
|
json_object_unref(reply);
|
|
return ok;
|
|
}
|
|
|
|
static int boundary_index(AtkTextGranularity granularity) {
|
|
switch (granularity) {
|
|
case ATK_TEXT_GRANULARITY_WORD: return 0;
|
|
case ATK_TEXT_GRANULARITY_SENTENCE: return 1;
|
|
case ATK_TEXT_GRANULARITY_LINE: return 2;
|
|
case ATK_TEXT_GRANULARITY_PARAGRAPH: return 3;
|
|
default: return -1;
|
|
}
|
|
}
|
|
|
|
// Word, sentence, line and paragraph boundaries come from the browser's own
|
|
// layout through IA2 when recorded; the fallback below is only a
|
|
// newline-based approximation for objects without that data.
|
|
static gchar *wa_get_string_at_offset(AtkText *text, gint offset, AtkTextGranularity granularity,
|
|
gint *startOffset, gint *endOffset) {
|
|
WaNode *node = node_of(text);
|
|
long count = node->characterCount;
|
|
int index = boundary_index(granularity);
|
|
// At or past the end of the text native ATK reports no segment for any
|
|
// granularity; IA2 may fail there or still report the last line.
|
|
if (index >= 0 && (offset < 0 || offset >= count)) {
|
|
*startOffset = *endOffset = -1;
|
|
return NULL;
|
|
}
|
|
long utf16Offset = character_to_utf16(node, offset);
|
|
long start16 = -1, end16 = -1;
|
|
gboolean haveSegment = FALSE;
|
|
if (index >= 0 && node->boundaries[index]) {
|
|
GArray *segments = node->boundaries[index];
|
|
if (utf16Offset < 0 || (guint)(utf16Offset * 2 + 1) >= segments->len) {
|
|
*startOffset = *endOffset = -1;
|
|
return NULL;
|
|
}
|
|
start16 = g_array_index(segments, long, utf16Offset * 2);
|
|
end16 = g_array_index(segments, long, utf16Offset * 2 + 1);
|
|
haveSegment = TRUE;
|
|
} else if (index >= 0 && utf16Offset >= 0 && liveMode && !(node->msaaStates & MsaaStateProtected)) {
|
|
haveSegment = live_boundary(node, utf16Offset, index, &start16, &end16);
|
|
if (!haveSegment)
|
|
g_warning("node %ld: no layout boundary from the browser at offset %d; using an approximation", node->id,
|
|
offset);
|
|
}
|
|
if (haveSegment) {
|
|
long start = start16 < 0 ? -1 : utf16_boundary_to_character(node, start16, FALSE);
|
|
long end = end16 < 0 ? -1 : utf16_boundary_to_character(node, end16, TRUE);
|
|
// At the end of the text native ATK reports no segment for any
|
|
// granularity; IA2 still reports the last line there.
|
|
if (start < 0 || end < 0 || offset >= count) {
|
|
*startOffset = *endOffset = -1;
|
|
return NULL;
|
|
}
|
|
*startOffset = (gint)start;
|
|
*endOffset = (gint)end;
|
|
return slice(node, start, end);
|
|
}
|
|
if (offset < 0 || offset > count) {
|
|
*startOffset = *endOffset = -1;
|
|
return NULL;
|
|
}
|
|
long start = offset, end = offset;
|
|
switch (granularity) {
|
|
case ATK_TEXT_GRANULARITY_CHAR:
|
|
end = offset < count ? offset + 1 : offset;
|
|
break;
|
|
case ATK_TEXT_GRANULARITY_WORD:
|
|
while (start > 0 && !(is_space(node->characters[start - 1]) && !is_space(node->characters[start]))) start--;
|
|
while (end < count && !is_space(node->characters[end])) end++;
|
|
while (end < count && is_space(node->characters[end])) end++;
|
|
break;
|
|
case ATK_TEXT_GRANULARITY_SENTENCE:
|
|
while (start > 0 && !(node->characters[start - 1] == '.' || node->characters[start - 1] == '\n')) start--;
|
|
while (end < count && node->characters[end] != '.' && node->characters[end] != '\n') end++;
|
|
while (end < count && (node->characters[end] == '.' || is_space(node->characters[end]))) end++;
|
|
break;
|
|
case ATK_TEXT_GRANULARITY_LINE:
|
|
case ATK_TEXT_GRANULARITY_PARAGRAPH:
|
|
while (start > 0 && node->characters[start - 1] != '\n') start--;
|
|
while (end < count && node->characters[end] != '\n') end++;
|
|
if (end < count) end++;
|
|
break;
|
|
default:
|
|
*startOffset = *endOffset = -1;
|
|
return NULL;
|
|
}
|
|
*startOffset = (gint)start;
|
|
*endOffset = (gint)end;
|
|
return slice(node, start, end);
|
|
}
|
|
|
|
static gboolean forward_text_range(WaNode *node, const char *op, gint start, gint end, int scrollType) {
|
|
long start16 = character_to_utf16(node, start), end16 = character_to_utf16(node, end);
|
|
if (!liveMode || node->defunct || start16 < 0 || end16 < 0) return FALSE;
|
|
char fields[96];
|
|
if (scrollType >= 0)
|
|
snprintf(fields, sizeof(fields), ",\"start\":%ld,\"end\":%ld,\"scrollType\":%d", start16, end16, scrollType);
|
|
else
|
|
snprintf(fields, sizeof(fields), ",\"start\":%ld,\"end\":%ld", start16, end16);
|
|
return worker_request_async(op, node->id, fields);
|
|
}
|
|
|
|
static gboolean wa_set_caret_offset(AtkText *text, gint offset) {
|
|
WaNode *node = node_of(text);
|
|
long offset16 = character_to_utf16(node, offset);
|
|
if (liveMode && !node->defunct && offset16 >= 0) {
|
|
char fields[40];
|
|
snprintf(fields, sizeof(fields), ",\"offset\":%ld", offset16);
|
|
return worker_request_async("setCaret", node->id, fields);
|
|
}
|
|
g_message("set_caret_offset %d requested for node %ld (snapshot mode)", offset, node->id);
|
|
return FALSE;
|
|
}
|
|
|
|
static gint wa_get_n_selections(AtkText *text) {
|
|
WaNode *node = node_of(text);
|
|
return node->selectionStart >= 0 && node->selectionEnd > node->selectionStart ? 1 : 0;
|
|
}
|
|
|
|
static gchar *wa_get_selection(AtkText *text, gint selectionNumber, gint *startOffset, gint *endOffset) {
|
|
WaNode *node = node_of(text);
|
|
if (selectionNumber != 0 || wa_get_n_selections(text) == 0) {
|
|
*startOffset = *endOffset = 0;
|
|
return NULL;
|
|
}
|
|
*startOffset = (gint)node->selectionStart;
|
|
*endOffset = (gint)node->selectionEnd;
|
|
return slice(node, node->selectionStart, node->selectionEnd);
|
|
}
|
|
|
|
static gboolean wa_add_selection(AtkText *text, gint start, gint end) {
|
|
return forward_text_range(node_of(text), "setSelection", start, end, -1);
|
|
}
|
|
|
|
static gboolean wa_set_selection(AtkText *text, gint selectionNumber, gint start, gint end) {
|
|
return selectionNumber == 0 && forward_text_range(node_of(text), "setSelection", start, end, -1);
|
|
}
|
|
|
|
static gboolean wa_remove_selection(AtkText *text, gint selectionNumber) {
|
|
WaNode *node = node_of(text);
|
|
long caret = MAX(node->caret, 0);
|
|
return selectionNumber == 0 && forward_text_range(node, "setSelection", (gint)caret, (gint)caret, -1);
|
|
}
|
|
|
|
static gboolean wa_scroll_substring_to(AtkText *text, gint start, gint end, AtkScrollType type) {
|
|
return forward_text_range(node_of(text), "scrollSubstring", start, end, (int)type);
|
|
}
|
|
|
|
static AtkAttributeSet *wa_get_run_attributes(AtkText *text, gint offset, gint *startOffset, gint *endOffset) {
|
|
WaNode *node = node_of(text);
|
|
(void)offset;
|
|
*startOffset = 0;
|
|
*endOffset = (gint)node->characterCount;
|
|
return NULL;
|
|
}
|
|
|
|
// All of an object's character rectangles in one worker request, cached
|
|
// until its text or the layout changes. Returns FALSE when unavailable.
|
|
static gboolean ensure_character_extents(WaNode *node) {
|
|
if (node->characterExtents) return TRUE;
|
|
if (!liveMode || node->defunct || !node->hasText || node->characterCount == 0 || utf16_length(node) > 4096)
|
|
return FALSE;
|
|
// One IA2 call per character in the worker: allow time in proportion.
|
|
int timeoutMs = (int)MIN(3000, requestTimeoutMs + utf16_length(node) * 2);
|
|
JsonObject *reply = worker_request_sync_timeout("characterExtentsAll", node->id, NULL, timeoutMs);
|
|
gboolean ok = reply && json_object_get_boolean_member_with_default(reply, "ok", FALSE) &&
|
|
json_object_has_member(reply, "extents");
|
|
if (ok) {
|
|
JsonArray *rectangles = json_object_get_array_member(reply, "extents");
|
|
guint count = json_array_get_length(rectangles);
|
|
ok = (long)count == utf16_length(node);
|
|
if (ok) {
|
|
node->characterExtents = g_array_sized_new(FALSE, FALSE, sizeof(int), count * 4);
|
|
for (guint i = 0; i < count; i++) {
|
|
JsonArray *rectangle = json_array_get_array_element(rectangles, i);
|
|
for (guint j = 0; j < 4; j++) {
|
|
int value = (int)json_array_get_int_element(rectangle, j);
|
|
g_array_append_val(node->characterExtents, value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (reply) json_object_unref(reply);
|
|
return ok;
|
|
}
|
|
|
|
// Screen rectangle of one character, from the cache.
|
|
static gboolean cached_character_rectangle(WaNode *node, gint offset, int *rectangle) {
|
|
long offset16 = character_to_utf16(node, offset);
|
|
if (offset16 < 0 || offset >= node->characterCount || !ensure_character_extents(node)) return FALSE;
|
|
for (int j = 0; j < 4; j++) rectangle[j] = g_array_index(node->characterExtents, int, offset16 * 4 + j);
|
|
return TRUE;
|
|
}
|
|
|
|
static void to_coordinate_type(WaNode *node, gint *x, gint *y, AtkCoordType coordType) {
|
|
if (coordType == ATK_XY_SCREEN) return;
|
|
WaNode *origin = coordType == ATK_XY_PARENT ? node->parent : frameNode;
|
|
if (!origin) return;
|
|
refresh_live_extents(origin);
|
|
*x -= origin->x;
|
|
*y -= origin->y;
|
|
}
|
|
|
|
// Native Chromium answers range extents itself; ATK's default would ask for
|
|
// every character separately, which here means one worker round trip each.
|
|
static void wa_get_range_extents(AtkText *text, gint start, gint end, AtkCoordType coordType,
|
|
AtkTextRectangle *rectangle) {
|
|
WaNode *node = node_of(text);
|
|
rectangle->x = rectangle->y = rectangle->width = rectangle->height = 0;
|
|
if (end < 0 || end > node->characterCount) end = (gint)node->characterCount;
|
|
int left = G_MAXINT, top = G_MAXINT, right = G_MININT, bottom = G_MININT;
|
|
for (gint offset = MAX(start, 0); offset < end; offset++) {
|
|
int character[4];
|
|
if (!cached_character_rectangle(node, offset, character)) break;
|
|
if (character[2] <= 0 && character[3] <= 0) continue;
|
|
left = MIN(left, character[0]);
|
|
top = MIN(top, character[1]);
|
|
right = MAX(right, character[0] + character[2]);
|
|
bottom = MAX(bottom, character[1] + character[3]);
|
|
}
|
|
if (left > right || top > bottom) return;
|
|
rectangle->x = left;
|
|
rectangle->y = top;
|
|
rectangle->width = right - left;
|
|
rectangle->height = bottom - top;
|
|
to_coordinate_type(node, &rectangle->x, &rectangle->y, coordType);
|
|
}
|
|
|
|
static void wa_get_character_extents(AtkText *text, gint offset, gint *x, gint *y, gint *width, gint *height,
|
|
AtkCoordType coordType) {
|
|
WaNode *node = node_of(text);
|
|
int character[4];
|
|
if (cached_character_rectangle(node, offset, character)) {
|
|
*x = character[0];
|
|
*y = character[1];
|
|
*width = character[2];
|
|
*height = character[3];
|
|
to_coordinate_type(node, x, y, coordType);
|
|
return;
|
|
}
|
|
long offset16 = character_to_utf16(node, offset);
|
|
if (liveMode && !node->defunct && offset16 >= 0) {
|
|
char fields[40];
|
|
snprintf(fields, sizeof(fields), ",\"offset\":%ld", offset16);
|
|
JsonObject *reply = worker_request_sync("characterExtents", node->id, fields);
|
|
gboolean ok = reply && json_object_get_boolean_member_with_default(reply, "ok", FALSE) &&
|
|
json_object_has_member(reply, "extents");
|
|
if (ok) {
|
|
JsonArray *extents = json_object_get_array_member(reply, "extents");
|
|
*x = (gint)json_array_get_int_element(extents, 0);
|
|
*y = (gint)json_array_get_int_element(extents, 1);
|
|
*width = (gint)json_array_get_int_element(extents, 2);
|
|
*height = (gint)json_array_get_int_element(extents, 3);
|
|
if (coordType != ATK_XY_SCREEN && frameNode) {
|
|
refresh_live_extents(frameNode);
|
|
*x -= frameNode->x;
|
|
*y -= frameNode->y;
|
|
}
|
|
}
|
|
if (reply) json_object_unref(reply);
|
|
if (ok) return;
|
|
}
|
|
wa_get_extents(ATK_COMPONENT(text), x, y, width, height, coordType);
|
|
}
|
|
|
|
static void wa_text_init(gpointer ifacePointer, gpointer data) {
|
|
AtkTextIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->get_text = wa_get_text;
|
|
iface->get_character_count = wa_get_character_count;
|
|
iface->get_caret_offset = wa_get_caret_offset;
|
|
iface->set_caret_offset = wa_set_caret_offset;
|
|
iface->get_character_at_offset = wa_get_character_at_offset;
|
|
iface->get_string_at_offset = wa_get_string_at_offset;
|
|
iface->get_n_selections = wa_get_n_selections;
|
|
iface->get_selection = wa_get_selection;
|
|
iface->add_selection = wa_add_selection;
|
|
iface->set_selection = wa_set_selection;
|
|
iface->remove_selection = wa_remove_selection;
|
|
iface->scroll_substring_to = wa_scroll_substring_to;
|
|
iface->get_run_attributes = wa_get_run_attributes;
|
|
iface->get_character_extents = wa_get_character_extents;
|
|
iface->get_range_extents = wa_get_range_extents;
|
|
}
|
|
|
|
// ---- AtkHyperlink (one per embedded child) and AtkHypertext
|
|
|
|
// Holds a reference to its target accessible so a hyperlink that atk-bridge
|
|
// still exports never points at freed memory. The node drops its reference to
|
|
// the hyperlink when it becomes defunct, breaking the cycle.
|
|
typedef struct {
|
|
AtkHyperlink parent;
|
|
AtkObject *accessible;
|
|
} WaHyperlink;
|
|
|
|
typedef struct {
|
|
AtkHyperlinkClass parentClass;
|
|
} WaHyperlinkClass;
|
|
|
|
G_DEFINE_TYPE(WaHyperlink, wa_hyperlink, ATK_TYPE_HYPERLINK)
|
|
|
|
static WaNode *hyperlink_node(AtkHyperlink *hyperlink) {
|
|
return node_of(((WaHyperlink *)hyperlink)->accessible);
|
|
}
|
|
|
|
static const WaLink *link_entry(WaNode *node) {
|
|
if (!node->parent) return NULL;
|
|
for (guint i = 0; i < node->parent->links->len; i++) {
|
|
const WaLink *link = &g_array_index(node->parent->links, WaLink, i);
|
|
if (link->id == node->id) return link;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static gint wa_hyperlink_get_start_index(AtkHyperlink *hyperlink) {
|
|
const WaLink *link = link_entry(hyperlink_node(hyperlink));
|
|
return link ? (gint)link->start : -1;
|
|
}
|
|
|
|
static gint wa_hyperlink_get_end_index(AtkHyperlink *hyperlink) {
|
|
const WaLink *link = link_entry(hyperlink_node(hyperlink));
|
|
return link ? (gint)link->end : -1;
|
|
}
|
|
|
|
static AtkObject *wa_hyperlink_get_object(AtkHyperlink *hyperlink, gint index) {
|
|
return index == 0 ? ((WaHyperlink *)hyperlink)->accessible : NULL;
|
|
}
|
|
|
|
static gchar *wa_hyperlink_get_uri(AtkHyperlink *hyperlink, gint index) {
|
|
WaNode *node = hyperlink_node(hyperlink);
|
|
return index == 0 && node->atkRole == ATK_ROLE_LINK && node->value ? g_strdup(node->value) : NULL;
|
|
}
|
|
|
|
static gint wa_hyperlink_get_n_anchors(AtkHyperlink *hyperlink) {
|
|
(void)hyperlink;
|
|
return 1;
|
|
}
|
|
|
|
static gboolean wa_hyperlink_is_valid(AtkHyperlink *hyperlink) {
|
|
return !hyperlink_node(hyperlink)->defunct;
|
|
}
|
|
|
|
static void wa_hyperlink_finalize(GObject *object) {
|
|
g_clear_object(&((WaHyperlink *)object)->accessible);
|
|
G_OBJECT_CLASS(wa_hyperlink_parent_class)->finalize(object);
|
|
}
|
|
|
|
static void wa_hyperlink_class_init(WaHyperlinkClass *klass) {
|
|
G_OBJECT_CLASS(klass)->finalize = wa_hyperlink_finalize;
|
|
AtkHyperlinkClass *hyperlinkClass = ATK_HYPERLINK_CLASS(klass);
|
|
hyperlinkClass->get_start_index = wa_hyperlink_get_start_index;
|
|
hyperlinkClass->get_end_index = wa_hyperlink_get_end_index;
|
|
hyperlinkClass->get_object = wa_hyperlink_get_object;
|
|
hyperlinkClass->get_uri = wa_hyperlink_get_uri;
|
|
hyperlinkClass->get_n_anchors = wa_hyperlink_get_n_anchors;
|
|
hyperlinkClass->is_valid = wa_hyperlink_is_valid;
|
|
}
|
|
|
|
static void wa_hyperlink_init(WaHyperlink *hyperlink) {
|
|
(void)hyperlink;
|
|
}
|
|
|
|
static AtkHyperlink *wa_get_hyperlink(AtkHyperlinkImpl *impl) {
|
|
WaNode *node = node_of(impl);
|
|
if (!node->hyperlink) {
|
|
WaHyperlink *hyperlink = g_object_new(wa_hyperlink_get_type(), NULL);
|
|
hyperlink->accessible = g_object_ref(node->accessible);
|
|
node->hyperlink = ATK_HYPERLINK(hyperlink);
|
|
}
|
|
return g_object_ref(node->hyperlink);
|
|
}
|
|
|
|
static void wa_hyperlink_impl_init(gpointer ifacePointer, gpointer data) {
|
|
AtkHyperlinkImplIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->get_hyperlink = wa_get_hyperlink;
|
|
}
|
|
|
|
static gint wa_get_n_links(AtkHypertext *hypertext) {
|
|
return (gint)node_of(hypertext)->links->len;
|
|
}
|
|
|
|
static AtkHyperlink *wa_get_link(AtkHypertext *hypertext, gint index) {
|
|
WaNode *node = node_of(hypertext);
|
|
if (index < 0 || (guint)index >= node->links->len) return NULL;
|
|
WaNode *child = node_for_id(g_array_index(node->links, WaLink, index).id);
|
|
if (!child || !ATK_IS_HYPERLINK_IMPL(child->accessible)) return NULL;
|
|
AtkHyperlink *hyperlink = atk_hyperlink_impl_get_hyperlink(ATK_HYPERLINK_IMPL(child->accessible));
|
|
g_object_unref(hyperlink); // the child keeps its own reference; get_link is transfer none
|
|
return hyperlink;
|
|
}
|
|
|
|
static gint wa_get_link_index(AtkHypertext *hypertext, gint characterOffset) {
|
|
WaNode *node = node_of(hypertext);
|
|
for (guint i = 0; i < node->links->len; i++) {
|
|
const WaLink *link = &g_array_index(node->links, WaLink, i);
|
|
if (characterOffset >= link->start && characterOffset < link->end) return (gint)i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static void wa_hypertext_init(gpointer ifacePointer, gpointer data) {
|
|
AtkHypertextIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->get_n_links = wa_get_n_links;
|
|
iface->get_link = wa_get_link;
|
|
iface->get_link_index = wa_get_link_index;
|
|
}
|
|
|
|
// ---- AtkTable and AtkTableCell
|
|
|
|
static WaNode *find_cell(WaNode *node, long row, long column) {
|
|
if (node->isCell && row >= node->row && row < node->row + node->rowSpan && column >= node->column &&
|
|
column < node->column + node->columnSpan)
|
|
return node;
|
|
for (guint i = 0; i < node->children->len; i++) {
|
|
WaNode *child = g_ptr_array_index(node->children, i);
|
|
if (child->isTable) continue; // do not descend into nested tables
|
|
WaNode *found = find_cell(child, row, column);
|
|
if (found) return found;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static AtkObject *wa_table_ref_at(AtkTable *table, gint row, gint column) {
|
|
WaNode *cell = find_cell(node_of(table), row, column);
|
|
return cell ? g_object_ref(cell->accessible) : NULL;
|
|
}
|
|
|
|
static gint wa_table_get_n_rows(AtkTable *table) {
|
|
return (gint)node_of(table)->rows;
|
|
}
|
|
|
|
static gint wa_table_get_n_columns(AtkTable *table) {
|
|
return (gint)node_of(table)->columns;
|
|
}
|
|
|
|
static AtkObject *wa_table_get_caption(AtkTable *table) {
|
|
WaNode *node = node_of(table);
|
|
for (guint i = 0; i < node->children->len; i++) {
|
|
WaNode *child = g_ptr_array_index(node->children, i);
|
|
if (child->atkRole == ATK_ROLE_CAPTION) return child->accessible;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void wa_table_init(gpointer ifacePointer, gpointer data) {
|
|
AtkTableIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->ref_at = wa_table_ref_at;
|
|
iface->get_n_rows = wa_table_get_n_rows;
|
|
iface->get_n_columns = wa_table_get_n_columns;
|
|
iface->get_caption = wa_table_get_caption;
|
|
}
|
|
|
|
// Native Chromium reports no header cells for a header cell; IA2 lists the
|
|
// header cell itself or its column header. Verified with th scope=col/row.
|
|
static GPtrArray *header_cells(WaNode *node, GArray *ids) {
|
|
GPtrArray *result = g_ptr_array_new_with_free_func(g_object_unref);
|
|
if (node->atkRole == ATK_ROLE_COLUMN_HEADER || node->atkRole == ATK_ROLE_ROW_HEADER) return result;
|
|
for (guint i = 0; ids && i < ids->len; i++) {
|
|
WaNode *header = node_for_id(g_array_index(ids, long, i));
|
|
if (header && header != node && header->accessible) g_ptr_array_add(result, g_object_ref(header->accessible));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static GPtrArray *wa_cell_get_column_header_cells(AtkTableCell *cell) {
|
|
WaNode *node = node_of(cell);
|
|
return header_cells(node, node->columnHeaders);
|
|
}
|
|
|
|
static GPtrArray *wa_cell_get_row_header_cells(AtkTableCell *cell) {
|
|
WaNode *node = node_of(cell);
|
|
return header_cells(node, node->rowHeaders);
|
|
}
|
|
|
|
static gint wa_cell_get_column_span(AtkTableCell *cell) {
|
|
return (gint)node_of(cell)->columnSpan;
|
|
}
|
|
|
|
static gint wa_cell_get_row_span(AtkTableCell *cell) {
|
|
return (gint)node_of(cell)->rowSpan;
|
|
}
|
|
|
|
static gboolean wa_cell_get_position(AtkTableCell *cell, gint *row, gint *column) {
|
|
WaNode *node = node_of(cell);
|
|
*row = (gint)node->row;
|
|
*column = (gint)node->column;
|
|
return TRUE;
|
|
}
|
|
|
|
static gboolean wa_cell_get_row_column_span(AtkTableCell *cell, gint *row, gint *column, gint *rowSpan,
|
|
gint *columnSpan) {
|
|
WaNode *node = node_of(cell);
|
|
*row = (gint)node->row;
|
|
*column = (gint)node->column;
|
|
*rowSpan = (gint)node->rowSpan;
|
|
*columnSpan = (gint)node->columnSpan;
|
|
return TRUE;
|
|
}
|
|
|
|
static AtkObject *wa_cell_get_table(AtkTableCell *cell) {
|
|
for (WaNode *node = node_of(cell)->parent; node; node = node->parent)
|
|
if (node->isTable) return g_object_ref(node->accessible);
|
|
return NULL;
|
|
}
|
|
|
|
static void wa_table_cell_init(gpointer ifacePointer, gpointer data) {
|
|
AtkTableCellIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->get_column_header_cells = wa_cell_get_column_header_cells;
|
|
iface->get_row_header_cells = wa_cell_get_row_header_cells;
|
|
iface->get_column_span = wa_cell_get_column_span;
|
|
iface->get_row_span = wa_cell_get_row_span;
|
|
iface->get_position = wa_cell_get_position;
|
|
iface->get_row_column_span = wa_cell_get_row_column_span;
|
|
iface->get_table = wa_cell_get_table;
|
|
}
|
|
|
|
// ---- AtkSelection (selection containers; changes need the backend)
|
|
|
|
static WaNode *selected_child(WaNode *node, gint index) {
|
|
gint seen = 0;
|
|
for (guint i = 0; i < node->children->len; i++) {
|
|
WaNode *child = g_ptr_array_index(node->children, i);
|
|
if ((child->msaaStates & MsaaStateSelected) && seen++ == index) return child;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static gint wa_get_selection_count(AtkSelection *selection) {
|
|
WaNode *node = node_of(selection);
|
|
gint count = 0;
|
|
for (guint i = 0; i < node->children->len; i++)
|
|
if (((WaNode *)g_ptr_array_index(node->children, i))->msaaStates & MsaaStateSelected) count++;
|
|
return count;
|
|
}
|
|
|
|
static AtkObject *wa_ref_selection(AtkSelection *selection, gint index) {
|
|
WaNode *child = selected_child(node_of(selection), index);
|
|
return child ? g_object_ref(child->accessible) : NULL;
|
|
}
|
|
|
|
static gboolean wa_is_child_selected(AtkSelection *selection, gint index) {
|
|
WaNode *node = node_of(selection);
|
|
if (index < 0 || (guint)index >= node->children->len) return FALSE;
|
|
return (((WaNode *)g_ptr_array_index(node->children, index))->msaaStates & MsaaStateSelected) != 0;
|
|
}
|
|
|
|
static gboolean wa_selection_change(AtkSelection *selection, gint index) {
|
|
g_message("selection change at %d requested for node %ld (no backend in prototype)", index,
|
|
node_of(selection)->id);
|
|
return FALSE;
|
|
}
|
|
|
|
static gboolean wa_selection_clear(AtkSelection *selection) {
|
|
return wa_selection_change(selection, -1);
|
|
}
|
|
|
|
static void wa_selection_init(gpointer ifacePointer, gpointer data) {
|
|
AtkSelectionIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->get_selection_count = wa_get_selection_count;
|
|
iface->ref_selection = wa_ref_selection;
|
|
iface->is_child_selected = wa_is_child_selected;
|
|
iface->add_selection = wa_selection_change;
|
|
iface->remove_selection = wa_selection_change;
|
|
iface->clear_selection = wa_selection_clear;
|
|
iface->select_all_selection = wa_selection_clear;
|
|
}
|
|
|
|
// ---- AtkWindow: no methods; its activate/deactivate signals become the
|
|
// window:activate and window:deactivate events readers use.
|
|
|
|
static void wa_window_init(gpointer ifacePointer, gpointer data) {
|
|
(void)ifacePointer;
|
|
(void)data;
|
|
}
|
|
|
|
// ---- AtkDocument
|
|
|
|
static const gchar *wa_document_get_locale(AtkDocument *document) {
|
|
(void)document;
|
|
return "";
|
|
}
|
|
|
|
static void wa_document_init(gpointer ifacePointer, gpointer data) {
|
|
AtkDocumentIface *iface = ifacePointer;
|
|
(void)data;
|
|
iface->get_document_locale = wa_document_get_locale;
|
|
}
|
|
|
|
// One GType per interface combination, registered on first use.
|
|
static GType type_for_mask(guint mask) {
|
|
static GHashTable *types;
|
|
if (!types) types = g_hash_table_new(g_direct_hash, g_direct_equal);
|
|
gpointer cached = g_hash_table_lookup(types, GUINT_TO_POINTER(mask + 1));
|
|
if (cached) return (GType)GPOINTER_TO_SIZE(cached);
|
|
GTypeInfo info = {
|
|
.class_size = sizeof(WaAccessibleClass),
|
|
.class_init = wa_accessible_class_init,
|
|
.instance_size = sizeof(WaAccessible),
|
|
};
|
|
char typeName[64];
|
|
snprintf(typeName, sizeof(typeName), "WaAccessible%u", mask);
|
|
GType type = g_type_register_static(ATK_TYPE_OBJECT, typeName, &info, 0);
|
|
static const struct {
|
|
guint flag;
|
|
GType (*type)(void);
|
|
GInterfaceInitFunc init;
|
|
} interfaces[] = {
|
|
{IfaceComponent, atk_component_get_type, wa_component_init},
|
|
{IfaceAction, atk_action_get_type, wa_action_init},
|
|
{IfaceText, atk_text_get_type, wa_text_init},
|
|
{IfaceHypertext, atk_hypertext_get_type, wa_hypertext_init},
|
|
{IfaceHyperlinkImpl, atk_hyperlink_impl_get_type, wa_hyperlink_impl_init},
|
|
{IfaceTable, atk_table_get_type, wa_table_init},
|
|
{IfaceTableCell, atk_table_cell_get_type, wa_table_cell_init},
|
|
{IfaceDocument, atk_document_get_type, wa_document_init},
|
|
{IfaceSelection, atk_selection_get_type, wa_selection_init},
|
|
{IfaceWindow, atk_window_get_type, wa_window_init},
|
|
};
|
|
for (size_t i = 0; i < G_N_ELEMENTS(interfaces); i++) {
|
|
if (!(mask & interfaces[i].flag)) continue;
|
|
GInterfaceInfo interfaceInfo = {.interface_init = interfaces[i].init};
|
|
g_type_add_interface_static(type, interfaces[i].type(), &interfaceInfo);
|
|
}
|
|
g_hash_table_insert(types, GUINT_TO_POINTER(mask + 1), GSIZE_TO_POINTER(type));
|
|
return type;
|
|
}
|
|
|
|
// Interface sets follow native Chromium: every object has Action, Component,
|
|
// Document and Text; text fields lack Hypertext; everything except text
|
|
// leaves is a hyperlink implementation.
|
|
static guint interface_mask(WaNode *node) {
|
|
guint mask = IfaceComponent | IfaceAction | IfaceText | IfaceDocument;
|
|
if (!is_text_field(node->atkRole)) mask |= IfaceHypertext;
|
|
if (!is_text_leaf(node)) mask |= IfaceHyperlinkImpl;
|
|
if (node->isTable) mask |= IfaceTable;
|
|
if (node->isCell) mask |= IfaceTableCell;
|
|
if (node->isFrame) mask |= IfaceWindow;
|
|
// Selection containers; the menu bar is verified, the rest follow Chromium.
|
|
switch (node->atkRole) {
|
|
case ATK_ROLE_MENU_BAR: case ATK_ROLE_MENU: case ATK_ROLE_LIST_BOX: case ATK_ROLE_PAGE_TAB_LIST:
|
|
case ATK_ROLE_TREE: case ATK_ROLE_TREE_TABLE:
|
|
mask |= IfaceSelection;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return mask;
|
|
}
|
|
|
|
static void create_accessibles(WaNode *node) {
|
|
if (!node->atkRole) node->atkRole = map_role(node);
|
|
WaAccessible *accessible = g_object_new(type_for_mask(interface_mask(node)), NULL);
|
|
accessible->node = node;
|
|
node->accessible = ATK_OBJECT(accessible);
|
|
atk_object_set_role(node->accessible, node->atkRole);
|
|
if (node->parent) atk_object_set_parent(node->accessible, node->parent->accessible);
|
|
for (guint i = 0; i < node->children->len; i++) create_accessibles(g_ptr_array_index(node->children, i));
|
|
}
|
|
|
|
// ---------------------------------------------------------------- AtkUtil
|
|
|
|
static AtkObject *wa_util_get_root(void) {
|
|
return applicationNode ? applicationNode->accessible : NULL;
|
|
}
|
|
|
|
static const gchar *wa_util_get_toolkit_name(void) {
|
|
return "Chromium";
|
|
}
|
|
|
|
static const gchar *wa_util_get_toolkit_version(void) {
|
|
return "1.0";
|
|
}
|
|
|
|
static void install_util(void) {
|
|
AtkUtilClass *utilClass = ATK_UTIL_CLASS(g_type_class_ref(ATK_TYPE_UTIL));
|
|
utilClass->get_root = wa_util_get_root;
|
|
utilClass->get_toolkit_name = wa_util_get_toolkit_name;
|
|
utilClass->get_toolkit_version = wa_util_get_toolkit_version;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- live mode: worker connection
|
|
|
|
static GPid workerPid;
|
|
static int workerInput = -1;
|
|
static int workerOutput = -1;
|
|
static GString *workerBuffer;
|
|
static GQueue *pendingMessages; // JsonObject *, received while answering a synchronous request
|
|
static gboolean workerClosed;
|
|
static long nextRequestId = 1;
|
|
static guint pendingIdleSource;
|
|
static GMainLoop *mainLoop;
|
|
static int exitStatus;
|
|
static const gsize maxBufferedBytes = 256u << 20;
|
|
|
|
static void handle_message(JsonObject *message);
|
|
|
|
static void write_worker(const char *line) {
|
|
if (workerInput < 0) return;
|
|
GString *data = g_string_new(line);
|
|
g_string_append_c(data, '\n');
|
|
gsize written = 0;
|
|
while (written < data->len) {
|
|
ssize_t result = write(workerInput, data->str + written, data->len - written);
|
|
if (result < 0 && errno == EINTR) continue;
|
|
if (result <= 0) {
|
|
g_warning("worker input closed: %s", g_strerror(errno));
|
|
close(workerInput);
|
|
workerInput = -1;
|
|
break;
|
|
}
|
|
written += (gsize)result;
|
|
}
|
|
g_string_free(data, TRUE);
|
|
}
|
|
|
|
// Read whatever the worker has sent, waiting at most timeoutMs.
|
|
static void read_worker(int timeoutMs) {
|
|
if (workerOutput < 0 || workerClosed) return;
|
|
struct pollfd descriptor = {.fd = workerOutput, .events = POLLIN};
|
|
if (poll(&descriptor, 1, timeoutMs) <= 0) return;
|
|
char chunk[65536];
|
|
ssize_t count = read(workerOutput, chunk, sizeof(chunk));
|
|
if (count > 0) {
|
|
g_string_append_len(workerBuffer, chunk, count);
|
|
if (workerBuffer->len > maxBufferedBytes) {
|
|
g_warning("worker sent an oversized message; disconnecting");
|
|
workerClosed = TRUE;
|
|
}
|
|
} else if (count == 0 || (errno != EINTR && errno != EAGAIN)) {
|
|
workerClosed = TRUE;
|
|
}
|
|
}
|
|
|
|
static JsonObject *parse_line(const char *line) {
|
|
JsonParser *parser = json_parser_new();
|
|
JsonObject *object = NULL;
|
|
if (json_parser_load_from_data(parser, line, -1, NULL)) {
|
|
JsonNode *root = json_parser_get_root(parser);
|
|
if (root && JSON_NODE_HOLDS_OBJECT(root)) object = json_object_ref(json_node_get_object(root));
|
|
}
|
|
if (!object) g_warning("ignored a malformed worker message");
|
|
g_object_unref(parser);
|
|
return object;
|
|
}
|
|
|
|
static JsonObject *take_message(void) {
|
|
for (;;) {
|
|
char *newline = memchr(workerBuffer->str, '\n', workerBuffer->len);
|
|
if (!newline) return NULL;
|
|
gsize length = (gsize)(newline - workerBuffer->str);
|
|
char *line = g_strndup(workerBuffer->str, length);
|
|
g_string_erase(workerBuffer, 0, (gssize)length + 1);
|
|
JsonObject *object = parse_line(line);
|
|
g_free(line);
|
|
if (object) return object;
|
|
}
|
|
}
|
|
|
|
static gboolean process_pending(gpointer data) {
|
|
(void)data;
|
|
pendingIdleSource = 0;
|
|
JsonObject *message;
|
|
while ((message = g_queue_pop_head(pendingMessages))) {
|
|
handle_message(message);
|
|
json_object_unref(message);
|
|
}
|
|
return G_SOURCE_REMOVE;
|
|
}
|
|
|
|
static void queue_pending(JsonObject *message) {
|
|
g_queue_push_tail(pendingMessages, message);
|
|
if (!pendingIdleSource) pendingIdleSource = g_idle_add(process_pending, NULL);
|
|
}
|
|
|
|
static long send_request(const char *op, long id, const char *extraFields) {
|
|
long requestId = nextRequestId++;
|
|
char *line = g_strdup_printf("{\"type\":\"request\",\"requestId\":%ld,\"op\":\"%s\",\"id\":%ld%s}", requestId, op,
|
|
id, extraFields ? extraFields : "");
|
|
write_worker(line);
|
|
g_free(line);
|
|
return requestId;
|
|
}
|
|
|
|
static gboolean worker_request_async(const char *op, long id, const char *extraFields) {
|
|
if (workerInput < 0 || workerClosed) return FALSE;
|
|
send_request(op, id, extraFields);
|
|
return TRUE;
|
|
}
|
|
|
|
// Called from ATK callbacks, which must answer synchronously. Waits a bounded
|
|
// time for the reply; every other message is queued and applied later from
|
|
// the main loop, so the tree never changes under the caller.
|
|
// Synchronous request statistics, logged every few seconds while requests
|
|
// happen, so live tests show where reader queries spend their time.
|
|
typedef struct {
|
|
guint count;
|
|
guint timeouts;
|
|
double totalMs;
|
|
double maxMs;
|
|
} WaRequestStats;
|
|
|
|
static GHashTable *requestStats; // op name -> WaRequestStats *
|
|
static guint statsSource;
|
|
|
|
static gboolean log_request_stats(gpointer data) {
|
|
(void)data;
|
|
statsSource = 0;
|
|
GString *summary = g_string_new("synchronous requests in the last 3 s:");
|
|
GHashTableIter iter;
|
|
gpointer key, value;
|
|
g_hash_table_iter_init(&iter, requestStats);
|
|
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
|
WaRequestStats *stats = value;
|
|
g_string_append_printf(summary, " %s n=%u avg=%.1fms max=%.1fms", (const char *)key, stats->count,
|
|
stats->totalMs / stats->count, stats->maxMs);
|
|
if (stats->timeouts) g_string_append_printf(summary, " timeouts=%u", stats->timeouts);
|
|
}
|
|
g_message("%s", summary->str);
|
|
g_string_free(summary, TRUE);
|
|
g_hash_table_remove_all(requestStats);
|
|
return G_SOURCE_REMOVE;
|
|
}
|
|
|
|
static void record_request(const char *op, double elapsedMs, gboolean timedOut) {
|
|
if (!requestStats) requestStats = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
|
WaRequestStats *stats = g_hash_table_lookup(requestStats, op);
|
|
if (!stats) {
|
|
stats = g_new0(WaRequestStats, 1);
|
|
g_hash_table_insert(requestStats, g_strdup(op), stats);
|
|
}
|
|
stats->count++;
|
|
stats->totalMs += elapsedMs;
|
|
stats->maxMs = MAX(stats->maxMs, elapsedMs);
|
|
if (timedOut) stats->timeouts++;
|
|
if (!statsSource) statsSource = g_timeout_add_seconds(3, log_request_stats, NULL);
|
|
}
|
|
|
|
static JsonObject *worker_request_sync_timeout(const char *op, long id, const char *extraFields, int timeoutMs) {
|
|
if (workerInput < 0 || workerClosed) return NULL;
|
|
gint64 startUs = g_get_monotonic_time();
|
|
long requestId = send_request(op, id, extraFields);
|
|
gint64 deadline = g_get_monotonic_time() + (gint64)timeoutMs * 1000;
|
|
for (;;) {
|
|
JsonObject *message;
|
|
while ((message = take_message())) {
|
|
if (g_strcmp0(json_object_get_string_member_with_default(message, "type", ""), "reply") == 0 &&
|
|
json_object_get_int_member_with_default(message, "requestId", 0) == requestId) {
|
|
record_request(op, (g_get_monotonic_time() - startUs) / 1000.0, FALSE);
|
|
return message;
|
|
}
|
|
queue_pending(message);
|
|
}
|
|
gint64 remainingMs = (deadline - g_get_monotonic_time()) / 1000;
|
|
if (remainingMs <= 0 || workerClosed) break;
|
|
read_worker((int)remainingMs);
|
|
}
|
|
g_warning("worker request %ld (%s on %ld) got no reply within %d ms", requestId, op, id, timeoutMs);
|
|
record_request(op, (g_get_monotonic_time() - startUs) / 1000.0, TRUE);
|
|
return NULL;
|
|
}
|
|
|
|
static JsonObject *worker_request_sync(const char *op, long id, const char *extraFields) {
|
|
return worker_request_sync_timeout(op, id, extraFields, requestTimeoutMs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- live mode: applying updates
|
|
|
|
static guint interface_mask(WaNode *node);
|
|
static void create_accessibles(WaNode *node);
|
|
|
|
static gboolean traceMessages;
|
|
|
|
static void defunct_subtree(WaNode *node) {
|
|
if (node->defunct) return;
|
|
if (traceMessages) g_printerr("trace: defunct %ld %s\n", node->id, atk_role_get_name(node->atkRole));
|
|
node->defunct = TRUE;
|
|
node->parent = NULL;
|
|
atk_object_set_parent(node->accessible, NULL);
|
|
if (node->id == focusedId) focusedId = 0;
|
|
if (node_for_id(node->id) == node) g_hash_table_remove(nodesById, &node->id);
|
|
atk_object_notify_state_change(node->accessible, ATK_STATE_DEFUNCT, TRUE);
|
|
for (guint i = 0; i < node->children->len; i++) {
|
|
WaNode *child = g_ptr_array_index(node->children, i);
|
|
defunct_subtree(child);
|
|
}
|
|
g_ptr_array_set_size(node->children, 0);
|
|
g_clear_object(&node->hyperlink);
|
|
for (int b = 0; b < 4; b++) g_clear_pointer(&node->boundaryCache[b], g_hash_table_unref);
|
|
g_clear_pointer(&node->characterExtents, g_array_unref);
|
|
// Drop the tree's reference last; the node may be freed from here on.
|
|
g_object_unref(node->accessible);
|
|
}
|
|
|
|
static void clear_boundary_caches(WaNode *node, gboolean linesOnly) {
|
|
for (int b = 0; b < 4; b++)
|
|
if (!linesOnly || b == 2) g_clear_pointer(&node->boundaryCache[b], g_hash_table_unref);
|
|
g_clear_pointer(&node->characterExtents, g_array_unref); // positions follow both text and layout
|
|
}
|
|
|
|
// Replace existing's properties with fresh's, keeping identity, tree links,
|
|
// the accessible object and live state. Frees fresh.
|
|
static void adopt_properties(WaNode *existing, WaNode *fresh) {
|
|
WaNode old = *existing;
|
|
*existing = *fresh;
|
|
existing->id = old.id;
|
|
existing->parent = old.parent;
|
|
existing->children = old.children;
|
|
existing->isFrame = old.isFrame;
|
|
existing->isApplication = old.isApplication;
|
|
existing->accessible = old.accessible;
|
|
existing->hyperlink = old.hyperlink;
|
|
existing->defunct = old.defunct;
|
|
existing->caret = old.caret; // the caret changes only through caret messages
|
|
existing->selectionStart = old.selectionStart;
|
|
existing->selectionEnd = old.selectionEnd;
|
|
for (int b = 0; b < 4; b++) existing->boundaryCache[b] = old.boundaryCache[b];
|
|
existing->characterExtents = old.characterExtents; // cleared below if the text changed
|
|
existing->liveExtentsValid = FALSE;
|
|
if (existing->isFrame) existing->atkRole = ATK_ROLE_FRAME;
|
|
free_node_properties(&old);
|
|
g_free(fresh);
|
|
}
|
|
|
|
static void emit_text_diff(WaNode *node, const gunichar *oldText, long oldCount) {
|
|
const gunichar *newText = node->characters;
|
|
long newCount = node->characterCount;
|
|
long prefix = 0;
|
|
while (prefix < oldCount && prefix < newCount && oldText[prefix] == newText[prefix]) prefix++;
|
|
long suffix = 0;
|
|
while (suffix < oldCount - prefix && suffix < newCount - prefix &&
|
|
oldText[oldCount - 1 - suffix] == newText[newCount - 1 - suffix])
|
|
suffix++;
|
|
long removed = oldCount - prefix - suffix, inserted = newCount - prefix - suffix;
|
|
if (removed > 0) {
|
|
gchar *text = g_ucs4_to_utf8(oldText + prefix, removed, NULL, NULL, NULL);
|
|
g_signal_emit_by_name(node->accessible, "text-remove", (gint)prefix, (gint)removed, text);
|
|
g_free(text);
|
|
}
|
|
if (inserted > 0) {
|
|
gchar *text = g_ucs4_to_utf8(newText + prefix, inserted, NULL, NULL, NULL);
|
|
g_signal_emit_by_name(node->accessible, "text-insert", (gint)prefix, (gint)inserted, text);
|
|
g_free(text);
|
|
}
|
|
}
|
|
|
|
typedef struct {
|
|
char *name;
|
|
char *description;
|
|
AtkStateSet *states;
|
|
gunichar *text;
|
|
long textCount;
|
|
AtkRole role;
|
|
} WaObservable;
|
|
|
|
static void capture(WaNode *node, WaObservable *observable) {
|
|
observable->name = g_strdup(node->name);
|
|
observable->description = g_strdup(node->description);
|
|
observable->states = atk_object_ref_state_set(node->accessible);
|
|
observable->textCount = node->characterCount;
|
|
observable->text = node->characterCount ? g_memdup2(node->characters, sizeof(gunichar) * node->characterCount) : NULL;
|
|
observable->role = node->atkRole;
|
|
}
|
|
|
|
// children-changed:add events wait for the end of the worker's batch and go
|
|
// out in document order, after focus changes, as native Chromium sends them.
|
|
static GPtrArray *deferredAdditions; // AtkObject *, referenced
|
|
|
|
static void document_order(WaNode *node, GHashTable *order, guint *next) {
|
|
g_hash_table_insert(order, node, GUINT_TO_POINTER(++*next));
|
|
for (guint i = 0; i < node->children->len; i++) document_order(g_ptr_array_index(node->children, i), order, next);
|
|
}
|
|
|
|
static GHashTable *flushOrder;
|
|
|
|
static gint compare_document_order(gconstpointer first, gconstpointer second) {
|
|
guint a = GPOINTER_TO_UINT(g_hash_table_lookup(flushOrder, node_of(*(AtkObject *const *)first)));
|
|
guint b = GPOINTER_TO_UINT(g_hash_table_lookup(flushOrder, node_of(*(AtkObject *const *)second)));
|
|
return a < b ? -1 : a > b;
|
|
}
|
|
|
|
static void flush_additions(void) {
|
|
if (!deferredAdditions || deferredAdditions->len == 0) return;
|
|
flushOrder = g_hash_table_new(g_direct_hash, g_direct_equal);
|
|
guint next = 0;
|
|
document_order(applicationNode, flushOrder, &next);
|
|
g_ptr_array_sort(deferredAdditions, compare_document_order);
|
|
for (guint i = 0; i < deferredAdditions->len; i++) {
|
|
AtkObject *child = g_ptr_array_index(deferredAdditions, i);
|
|
WaNode *childNode = node_of(child);
|
|
WaNode *parent = childNode->parent;
|
|
if (childNode->defunct || !parent || parent->defunct) continue;
|
|
gint index = -1;
|
|
for (guint j = 0; j < parent->children->len; j++)
|
|
if (g_ptr_array_index(parent->children, j) == childNode) index = (gint)j;
|
|
if (index >= 0) g_signal_emit_by_name(parent->accessible, "children-changed::add", index, child);
|
|
}
|
|
g_ptr_array_set_size(deferredAdditions, 0);
|
|
g_clear_pointer(&flushOrder, g_hash_table_unref);
|
|
}
|
|
|
|
static gboolean only_embedded_objects(const gunichar *text, long count) {
|
|
for (long i = 0; i < count; i++)
|
|
if (text[i] != 0xFFFC) return FALSE;
|
|
return TRUE;
|
|
}
|
|
|
|
// Emit the AT-SPI events for what changed since capture(), then free it.
|
|
// When the node's children changed, native Chromium reports its text as wholly
|
|
// replaced, and reports nothing for text made only of embedded objects; edits
|
|
// within one text object are reported as the minimal changed range.
|
|
static void emit_changes(WaNode *node, WaObservable *before, gboolean childrenChanged) {
|
|
AtkObject *accessible = node->accessible;
|
|
if (before->role != node->atkRole) atk_object_set_role(accessible, node->atkRole);
|
|
if (g_strcmp0(before->name ? before->name : "", node->name ? node->name : "") != 0)
|
|
g_object_notify(G_OBJECT(accessible), "accessible-name");
|
|
if (g_strcmp0(before->description ? before->description : "", node->description ? node->description : "") != 0)
|
|
g_object_notify(G_OBJECT(accessible), "accessible-description");
|
|
gboolean textChanged = before->textCount != node->characterCount ||
|
|
(node->characterCount &&
|
|
memcmp(before->text, node->characters, sizeof(gunichar) * node->characterCount) != 0);
|
|
if (textChanged) {
|
|
clear_boundary_caches(node, FALSE);
|
|
if (!childrenChanged) {
|
|
emit_text_diff(node, before->text, before->textCount);
|
|
} else if (!(only_embedded_objects(before->text, before->textCount) &&
|
|
only_embedded_objects(node->characters, node->characterCount))) {
|
|
if (before->textCount > 0) {
|
|
gchar *text = g_ucs4_to_utf8(before->text, before->textCount, NULL, NULL, NULL);
|
|
g_signal_emit_by_name(node->accessible, "text-remove", 0, (gint)before->textCount, text);
|
|
g_free(text);
|
|
}
|
|
if (node->characterCount > 0) {
|
|
gchar *text = g_ucs4_to_utf8(node->characters, node->characterCount, NULL, NULL, NULL);
|
|
g_signal_emit_by_name(node->accessible, "text-insert", 0, (gint)node->characterCount, text);
|
|
g_free(text);
|
|
}
|
|
}
|
|
}
|
|
AtkStateSet *after = atk_object_ref_state_set(accessible);
|
|
for (int state = ATK_STATE_INVALID + 1; state < ATK_STATE_LAST_DEFINED; state++) {
|
|
if (state == ATK_STATE_FOCUSED || state == ATK_STATE_DEFUNCT) continue; // focus comes from focus messages
|
|
gboolean was = atk_state_set_contains_state(before->states, state);
|
|
gboolean is = atk_state_set_contains_state(after, state);
|
|
if (was != is) atk_object_notify_state_change(accessible, state, is);
|
|
}
|
|
g_object_unref(after);
|
|
g_object_unref(before->states);
|
|
g_free(before->name);
|
|
g_free(before->description);
|
|
g_free(before->text);
|
|
}
|
|
|
|
static gboolean is_ancestor(WaNode *candidate, WaNode *node) {
|
|
for (WaNode *current = node; current; current = current->parent)
|
|
if (current == candidate) return TRUE;
|
|
return FALSE;
|
|
}
|
|
|
|
// A fresh node whose role and interfaces are computed as they would be in
|
|
// place, so it can be compared with an existing one.
|
|
static WaNode *prepare_fresh(JsonObject *object, WaNode *parent, gboolean isFrame) {
|
|
WaNode *fresh = new_node(object);
|
|
fresh->parent = parent;
|
|
fresh->isFrame = isFrame;
|
|
fresh->atkRole = isFrame ? ATK_ROLE_FRAME : map_role(fresh);
|
|
return fresh;
|
|
}
|
|
|
|
static void discard_fresh(WaNode *fresh) {
|
|
free_node_properties(fresh);
|
|
g_free(fresh);
|
|
}
|
|
|
|
static void postprocess_node(WaNode *node) {
|
|
synthesize_view_links_node(node);
|
|
if (!inside_document(node)) adapt_views_node(node);
|
|
}
|
|
|
|
static void postprocess_subtree(WaNode *node) {
|
|
synthesize_view_links(node);
|
|
if (!inside_document(node)) adapt_views_layer(node);
|
|
}
|
|
|
|
static void reconcile_existing(WaNode *node, WaNode *fresh, JsonObject *object, gboolean withChildren);
|
|
|
|
// Make node's children match the provider's list: keep objects whose IDs
|
|
// survive (their identity matters to readers), create new ones, and retire
|
|
// the rest. Removals are emitted here; additions are returned in `added` for
|
|
// the caller to emit after text changes, in native order. Returns whether the
|
|
// child list changed.
|
|
static gboolean reconcile_children(WaNode *node, JsonArray *childArray, GPtrArray *added) {
|
|
gboolean changed = FALSE;
|
|
GPtrArray *oldChildren = node->children;
|
|
GPtrArray *newChildren = g_ptr_array_new();
|
|
node->children = newChildren;
|
|
for (guint i = 0; childArray && i < json_array_get_length(childArray); i++) {
|
|
JsonObject *childObject = json_array_get_object_element(childArray, i);
|
|
long id = member_long(childObject, "id", 0);
|
|
WaNode *existing = id ? node_for_id(id) : NULL;
|
|
WaNode *placed = NULL;
|
|
if (existing && !existing->defunct && !is_ancestor(existing, node)) {
|
|
WaNode *fresh = prepare_fresh(childObject, node, existing->isFrame);
|
|
if (interface_mask(fresh) == interface_mask(existing)) {
|
|
if (existing->parent != node && existing->parent) {
|
|
WaNode *oldParent = existing->parent;
|
|
gint oldIndex = -1;
|
|
for (guint j = 0; j < oldParent->children->len; j++)
|
|
if (g_ptr_array_index(oldParent->children, j) == existing) oldIndex = (gint)j;
|
|
if (oldIndex >= 0) {
|
|
g_ptr_array_remove_index(oldParent->children, oldIndex);
|
|
g_signal_emit_by_name(oldParent->accessible, "children-changed::remove", oldIndex, existing->accessible);
|
|
}
|
|
}
|
|
existing->parent = node;
|
|
atk_object_set_parent(existing->accessible, node->accessible);
|
|
g_ptr_array_add(newChildren, existing);
|
|
reconcile_existing(existing, fresh, childObject, TRUE);
|
|
placed = existing;
|
|
} else {
|
|
if (traceMessages)
|
|
g_printerr("trace: node %ld interfaces changed 0x%x -> 0x%x (role %s -> %s)\n", existing->id,
|
|
interface_mask(existing), interface_mask(fresh), atk_role_get_name(existing->atkRole),
|
|
atk_role_get_name(fresh->atkRole));
|
|
discard_fresh(fresh); // interfaces changed: the old object is retired below
|
|
if (existing->parent != node) {
|
|
WaNode *oldParent = existing->parent;
|
|
if (oldParent) {
|
|
g_ptr_array_remove(oldParent->children, existing);
|
|
g_signal_emit_by_name(oldParent->accessible, "children-changed::remove", -1, existing->accessible);
|
|
}
|
|
defunct_subtree(existing);
|
|
}
|
|
}
|
|
}
|
|
if (!placed) {
|
|
if (existing && !existing->defunct && is_ancestor(existing, node)) {
|
|
g_warning("node %ld would become its own descendant; ignored", id);
|
|
continue;
|
|
}
|
|
WaNode *created = load_node(childObject, node);
|
|
created->atkRole = map_role(created);
|
|
g_ptr_array_add(newChildren, created);
|
|
postprocess_subtree(created);
|
|
create_accessibles(created);
|
|
}
|
|
}
|
|
for (guint i = 0; i < oldChildren->len; i++) {
|
|
WaNode *old = g_ptr_array_index(oldChildren, i);
|
|
gboolean kept = FALSE;
|
|
for (guint j = 0; j < newChildren->len && !kept; j++) kept = g_ptr_array_index(newChildren, j) == old;
|
|
if (kept) continue;
|
|
changed = TRUE;
|
|
if (old->parent != node) continue;
|
|
g_signal_emit_by_name(node->accessible, "children-changed::remove", (gint)i, old->accessible);
|
|
defunct_subtree(old);
|
|
}
|
|
for (guint i = 0; i < newChildren->len; i++) {
|
|
WaNode *child = g_ptr_array_index(newChildren, i);
|
|
gboolean existed = FALSE;
|
|
for (guint j = 0; j < oldChildren->len && !existed; j++) existed = g_ptr_array_index(oldChildren, j) == child;
|
|
if (!existed) {
|
|
g_ptr_array_add(added, g_object_ref(child->accessible));
|
|
changed = TRUE;
|
|
}
|
|
}
|
|
g_ptr_array_unref(oldChildren);
|
|
return changed;
|
|
}
|
|
|
|
static void reconcile_existing(WaNode *node, WaNode *fresh, JsonObject *object, gboolean withChildren) {
|
|
WaObservable before;
|
|
capture(node, &before);
|
|
adopt_properties(node, fresh);
|
|
GPtrArray *added = g_ptr_array_new_with_free_func(g_object_unref);
|
|
gboolean childrenChanged = FALSE;
|
|
if (withChildren && json_object_has_member(object, "children"))
|
|
childrenChanged = reconcile_children(node, json_object_get_array_member(object, "children"), added);
|
|
postprocess_node(node);
|
|
emit_changes(node, &before, childrenChanged);
|
|
if (!deferredAdditions) deferredAdditions = g_ptr_array_new_with_free_func(g_object_unref);
|
|
for (guint i = 0; i < added->len; i++) g_ptr_array_add(deferredAdditions, g_object_ref(g_ptr_array_index(added, i)));
|
|
g_ptr_array_unref(added);
|
|
}
|
|
|
|
// Apply a node or subtree record to the node with the same ID. When its
|
|
// interfaces change the object must be replaced; ask for a full subtree.
|
|
static void apply_record(JsonObject *object, gboolean withChildren) {
|
|
long id = member_long(object, "id", 0);
|
|
WaNode *node = node_for_id(id);
|
|
if (!node || node->defunct) {
|
|
g_debug("update for unknown node %ld ignored", id);
|
|
return;
|
|
}
|
|
WaNode *fresh = prepare_fresh(object, node->parent, node->isFrame);
|
|
if (!node->isFrame && interface_mask(fresh) != interface_mask(node)) {
|
|
discard_fresh(fresh);
|
|
WaNode *parent = node->parent;
|
|
if (parent) worker_request_async("refresh", parent->id, NULL);
|
|
return;
|
|
}
|
|
reconcile_existing(node, fresh, object, withChildren);
|
|
}
|
|
|
|
static void apply_focus(long id) {
|
|
if (id == focusedId) return;
|
|
WaNode *previous = focusedId ? node_for_id(focusedId) : NULL;
|
|
focusedId = id;
|
|
if (previous && !previous->defunct) atk_object_notify_state_change(previous->accessible, ATK_STATE_FOCUSED, FALSE);
|
|
WaNode *node = node_for_id(id);
|
|
if (node && !node->defunct) atk_object_notify_state_change(node->accessible, ATK_STATE_FOCUSED, TRUE);
|
|
}
|
|
|
|
// The object holding the document's current range selection, if any.
|
|
static long selectionOwnerId;
|
|
|
|
static void apply_selection(WaNode *node, JsonObject *message) {
|
|
long start = -1, end = -1;
|
|
if (json_object_has_member(message, "selection") &&
|
|
!JSON_NODE_HOLDS_NULL(json_object_get_member(message, "selection"))) {
|
|
JsonArray *range = json_object_get_array_member(message, "selection");
|
|
if (json_array_get_length(range) == 2) {
|
|
start = utf16_boundary_to_character(node, (long)json_array_get_int_element(range, 0), FALSE);
|
|
end = utf16_boundary_to_character(node, (long)json_array_get_int_element(range, 1), TRUE);
|
|
}
|
|
}
|
|
if (start == end) start = end = -1; // a collapsed selection is no selection
|
|
if (start != node->selectionStart || end != node->selectionEnd) {
|
|
node->selectionStart = start;
|
|
node->selectionEnd = end;
|
|
g_signal_emit_by_name(node->accessible, "text-selection-changed");
|
|
}
|
|
if (start >= 0) selectionOwnerId = node->id;
|
|
else if (selectionOwnerId == node->id) selectionOwnerId = 0;
|
|
}
|
|
|
|
static void apply_caret(JsonObject *message) {
|
|
WaNode *node = node_for_id(member_long(message, "id", 0));
|
|
if (!node || node->defunct || !node->hasText) return;
|
|
// A caret arriving for an object that is not yet focused means focus is
|
|
// moving there. If another object held a range selection, the document
|
|
// selection collapses: native Chromium reports that on the new object.
|
|
if (node->id != focusedId && selectionOwnerId && selectionOwnerId != node->id) {
|
|
WaNode *owner = node_for_id(selectionOwnerId);
|
|
if (owner && !owner->defunct) owner->selectionStart = owner->selectionEnd = -1;
|
|
selectionOwnerId = 0;
|
|
g_signal_emit_by_name(node->accessible, "text-selection-changed");
|
|
}
|
|
// Native order: selection change, then caret move.
|
|
apply_selection(node, message);
|
|
long caret16 = member_long(message, "caret", -1);
|
|
long caret = caret16 < 0 ? -1 : utf16_boundary_to_character(node, caret16, FALSE);
|
|
if (caret != node->caret) {
|
|
node->caret = caret;
|
|
if (caret >= 0) g_signal_emit_by_name(node->accessible, "text-caret-moved", (gint)caret);
|
|
}
|
|
}
|
|
|
|
// Windows Chromium reports scrolling to an anchor (an in-page link target) as
|
|
// EVENT_SYSTEM_SCROLLINGSTART on the target; native Linux Chromium moves the
|
|
// caret to the start of the target instead, which is how readers continue
|
|
// reading from there.
|
|
static void apply_anchor(JsonObject *message) {
|
|
WaNode *node = node_for_id(member_long(message, "id", 0));
|
|
if (!node || node->defunct || !node->hasText) return;
|
|
node->caret = 0;
|
|
g_signal_emit_by_name(node->accessible, "text-caret-moved", 0);
|
|
}
|
|
|
|
static void invalidate_layout(void) {
|
|
GHashTableIter iter;
|
|
gpointer key, value;
|
|
g_hash_table_iter_init(&iter, nodesById);
|
|
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
|
WaNode *node = value;
|
|
node->liveExtentsValid = FALSE;
|
|
clear_boundary_caches(node, TRUE);
|
|
}
|
|
}
|
|
|
|
static void apply_foreground(gboolean active) {
|
|
if (active == frameActive || !frameNode) return;
|
|
frameActive = active;
|
|
atk_object_notify_state_change(frameNode->accessible, ATK_STATE_ACTIVE, active);
|
|
g_signal_emit_by_name(frameNode->accessible, active ? "activate" : "deactivate");
|
|
}
|
|
|
|
static void worker_gone(void) {
|
|
if (exitStatus) return;
|
|
g_message("browser worker ended; retiring the published window");
|
|
exitStatus = 3;
|
|
if (frameNode && !frameNode->defunct) {
|
|
g_ptr_array_remove(applicationNode->children, frameNode);
|
|
g_signal_emit_by_name(applicationNode->accessible, "children-changed::remove", 0, frameNode->accessible);
|
|
defunct_subtree(frameNode);
|
|
frameNode = NULL;
|
|
}
|
|
if (mainLoop) g_main_loop_quit(mainLoop);
|
|
}
|
|
|
|
static void trace_message(const char *type, JsonObject *message) {
|
|
long id = member_long(message, "id", 0);
|
|
if (!id && json_object_has_member(message, "node")) id = member_long(json_object_get_object_member(message, "node"), "id", 0);
|
|
long children = -1;
|
|
if (json_object_has_member(message, "node")) {
|
|
JsonObject *node = json_object_get_object_member(message, "node");
|
|
if (json_object_has_member(node, "children")) children = json_array_get_length(json_object_get_array_member(node, "children"));
|
|
}
|
|
g_printerr("trace: %s id=%ld children=%ld\n", type, id, children);
|
|
}
|
|
|
|
static void handle_message(JsonObject *message) {
|
|
const char *type = json_object_get_string_member_with_default(message, "type", "");
|
|
if (traceMessages) trace_message(type, message);
|
|
if (strcmp(type, "tree") == 0) {
|
|
if (frameNode && json_object_has_member(message, "root")) {
|
|
JsonObject *root = json_object_get_object_member(message, "root");
|
|
WaNode *fresh = prepare_fresh(root, applicationNode, TRUE);
|
|
reconcile_existing(frameNode, fresh, root, TRUE);
|
|
}
|
|
apply_focus(member_long(message, "focusedId", 0));
|
|
} else if (strcmp(type, "subtree") == 0 || strcmp(type, "node") == 0) {
|
|
if (json_object_has_member(message, "node"))
|
|
apply_record(json_object_get_object_member(message, "node"), strcmp(type, "subtree") == 0);
|
|
} else if (strcmp(type, "focus") == 0) {
|
|
apply_focus(member_long(message, "id", 0));
|
|
} else if (strcmp(type, "caret") == 0) {
|
|
apply_caret(message);
|
|
} else if (strcmp(type, "selection") == 0) {
|
|
WaNode *node = node_for_id(member_long(message, "id", 0));
|
|
if (node && !node->defunct && node->hasText) apply_selection(node, message);
|
|
} else if (strcmp(type, "layout") == 0) {
|
|
invalidate_layout();
|
|
} else if (strcmp(type, "anchor") == 0) {
|
|
apply_anchor(message);
|
|
} else if (strcmp(type, "flush") == 0) {
|
|
flush_additions();
|
|
} else if (strcmp(type, "foreground") == 0) {
|
|
apply_foreground(json_object_get_boolean_member_with_default(message, "active", FALSE));
|
|
} else if (strcmp(type, "reply") == 0) {
|
|
if (!json_object_get_boolean_member_with_default(message, "ok", FALSE))
|
|
g_debug("worker request %ld failed: hr=%ld", member_long(message, "requestId", 0), member_long(message, "hr", 0));
|
|
} else if (strcmp(type, "gone") == 0) {
|
|
worker_gone();
|
|
} else if (strcmp(type, "hello") != 0) {
|
|
g_warning("unknown worker message type %s", type);
|
|
}
|
|
}
|
|
|
|
static gboolean on_worker_output(gint fd, GIOCondition condition, gpointer data) {
|
|
(void)fd;
|
|
(void)condition;
|
|
(void)data;
|
|
read_worker(0);
|
|
JsonObject *message;
|
|
while ((message = g_queue_pop_head(pendingMessages))) { // keep arrival order
|
|
handle_message(message);
|
|
json_object_unref(message);
|
|
}
|
|
while (!exitStatus && (message = take_message())) {
|
|
handle_message(message);
|
|
json_object_unref(message);
|
|
}
|
|
if (workerClosed && !exitStatus) worker_gone();
|
|
return exitStatus ? G_SOURCE_REMOVE : G_SOURCE_CONTINUE;
|
|
}
|
|
|
|
static gboolean spawn_worker(char **command) {
|
|
GError *error = NULL;
|
|
if (!g_spawn_async_with_pipes(NULL, command, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
|
|
&workerPid, &workerInput, &workerOutput, NULL, &error)) {
|
|
fprintf(stderr, "Cannot start worker: %s\n", error->message);
|
|
g_error_free(error);
|
|
return FALSE;
|
|
}
|
|
workerBuffer = g_string_new(NULL);
|
|
pendingMessages = g_queue_new();
|
|
return TRUE;
|
|
}
|
|
|
|
// Wait for the worker's first tree, applying hello on the way.
|
|
static JsonObject *wait_for_first_tree(int seconds) {
|
|
gint64 deadline = g_get_monotonic_time() + (gint64)seconds * G_USEC_PER_SEC;
|
|
while (g_get_monotonic_time() < deadline && !workerClosed) {
|
|
JsonObject *message;
|
|
while ((message = take_message())) {
|
|
const char *type = json_object_get_string_member_with_default(message, "type", "");
|
|
if (strcmp(type, "tree") == 0) return message;
|
|
if (strcmp(type, "hello") == 0) {
|
|
x11Window = (unsigned long long)json_object_get_int_member_with_default(message, "x11Window", 0);
|
|
if (json_object_get_boolean_member_with_default(message, "foreground", FALSE)) frameActive = TRUE;
|
|
g_message("worker connected to window \"%s\"", json_object_get_string_member_with_default(message, "title", ""));
|
|
json_object_unref(message);
|
|
} else if (strcmp(type, "gone") == 0) {
|
|
json_object_unref(message);
|
|
return NULL;
|
|
} else {
|
|
queue_pending(message);
|
|
}
|
|
}
|
|
read_worker(250);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
// ---------------------------------------------------------------- main
|
|
|
|
|
|
static gboolean announce(gpointer data) {
|
|
(void)data;
|
|
if (frameActive) {
|
|
g_signal_emit_by_name(frameNode->accessible, "activate");
|
|
atk_object_notify_state_change(frameNode->accessible, ATK_STATE_ACTIVE, TRUE);
|
|
}
|
|
WaNode *focused = node_for_id(focusedId);
|
|
if (focused) {
|
|
atk_object_notify_state_change(focused->accessible, ATK_STATE_FOCUSED, TRUE);
|
|
g_message("announced focus on node %ld (%s)", focused->id, atk_role_get_name(focused->atkRole));
|
|
}
|
|
return G_SOURCE_REMOVE;
|
|
}
|
|
|
|
static gboolean quit_loop(gpointer loop) {
|
|
g_main_loop_quit(loop);
|
|
return G_SOURCE_REMOVE;
|
|
}
|
|
|
|
// Build the published tree: the provider's window root becomes the frame, under
|
|
// a synthesized application root like the one native Chromium publishes.
|
|
static void build_initial_tree(JsonObject *rootObject, long initialFocus) {
|
|
nodesById = g_hash_table_new(g_int64_hash, g_int64_equal);
|
|
G_STATIC_ASSERT(sizeof(long) == sizeof(gint64));
|
|
frameNode = load_node(rootObject, NULL);
|
|
frameNode->isFrame = TRUE;
|
|
frameNode->atkRole = ATK_ROLE_FRAME;
|
|
focusedId = initialFocus;
|
|
synthesize_view_links(frameNode);
|
|
adapt_views_layer(frameNode);
|
|
|
|
applicationNode = g_new0(WaNode, 1);
|
|
applicationNode->id = syntheticIds--;
|
|
applicationNode->isApplication = TRUE;
|
|
applicationNode->atkRole = ATK_ROLE_APPLICATION;
|
|
applicationNode->name = g_strdup(applicationName);
|
|
parse_attributes(applicationNode, "explicit-name:true;name-from:attribute;");
|
|
applicationNode->hasText = TRUE;
|
|
applicationNode->characters = g_new(gunichar, 2);
|
|
applicationNode->characters[0] = 0xFFFC;
|
|
applicationNode->characterCount = 1;
|
|
applicationNode->caret = -1;
|
|
applicationNode->selectionStart = applicationNode->selectionEnd = -1;
|
|
applicationNode->links = g_array_new(FALSE, FALSE, sizeof(WaLink));
|
|
WaLink frameLink = {.start = 0, .end = 1, .id = frameNode->id};
|
|
g_array_append_val(applicationNode->links, frameLink);
|
|
applicationNode->relations = g_ptr_array_new();
|
|
applicationNode->actions = g_ptr_array_new_with_free_func(g_free);
|
|
g_ptr_array_add(applicationNode->actions, g_strdup("doDefault"));
|
|
g_ptr_array_add(applicationNode->actions, g_strdup("showContextMenu"));
|
|
applicationNode->children = g_ptr_array_new();
|
|
g_ptr_array_add(applicationNode->children, frameNode);
|
|
frameNode->parent = applicationNode;
|
|
g_hash_table_insert(nodesById, &applicationNode->id, applicationNode);
|
|
create_accessibles(applicationNode);
|
|
}
|
|
|
|
static void usage(const char *program) {
|
|
fprintf(stderr,
|
|
"Usage: %s (SNAPSHOT.json | --worker COMMAND...) [--app-name NAME] [--active] [--announce] "
|
|
"[--quit-after SECONDS]\n--worker must come last; everything after it is the command.\n",
|
|
program);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const char *snapshotPath = NULL;
|
|
char **workerCommand = NULL;
|
|
gboolean announceFocus = FALSE;
|
|
int quitAfterSeconds = 0;
|
|
for (int i = 1; i < argc; i++) {
|
|
if (strcmp(argv[i], "--app-name") == 0 && i + 1 < argc) applicationName = argv[++i];
|
|
else if (strcmp(argv[i], "--active") == 0) frameActive = TRUE;
|
|
else if (strcmp(argv[i], "--announce") == 0) announceFocus = TRUE;
|
|
else if (strcmp(argv[i], "--quit-after") == 0 && i + 1 < argc) quitAfterSeconds = atoi(argv[++i]);
|
|
else if (strcmp(argv[i], "--worker") == 0 && i + 1 < argc) {
|
|
workerCommand = argv + i + 1;
|
|
break;
|
|
} else if (!snapshotPath && argv[i][0] != '-') snapshotPath = argv[i];
|
|
else {
|
|
fprintf(stderr, "Unknown argument: %s\n", argv[i]);
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
}
|
|
if (!snapshotPath == !workerCommand) {
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
|
|
JsonParser *parser = NULL;
|
|
JsonObject *firstTree = NULL;
|
|
if (workerCommand) {
|
|
liveMode = TRUE;
|
|
const char *trace = g_getenv("WINE_ACCESS_BROWSER_TRACE");
|
|
traceMessages = trace && *trace;
|
|
signal(SIGPIPE, SIG_IGN);
|
|
// Identifies this process to the reader's Wine ownership check. Readers
|
|
// read /proc/PID/environ, which only shows the environment the process
|
|
// started with, so restart once with the marker instead of setenv alone.
|
|
if (g_strcmp0(g_getenv("WINE_ACCESS_BROWSER_PUBLISHER"), "1") != 0) {
|
|
g_setenv("WINE_ACCESS_BROWSER_PUBLISHER", "1", TRUE);
|
|
// Exec the real path so the process keeps its own name.
|
|
char *self = g_file_read_link("/proc/self/exe", NULL);
|
|
if (self) execv(self, argv);
|
|
fprintf(stderr, "Cannot restart with the publisher marker: %s\n", g_strerror(errno));
|
|
return 1;
|
|
}
|
|
if (!spawn_worker(workerCommand)) return 1;
|
|
firstTree = wait_for_first_tree(90);
|
|
if (!firstTree) {
|
|
fprintf(stderr, "The worker sent no tree\n");
|
|
return 3;
|
|
}
|
|
build_initial_tree(json_object_get_object_member(firstTree, "root"), member_long(firstTree, "focusedId", 0));
|
|
} else {
|
|
parser = json_parser_new();
|
|
GError *error = NULL;
|
|
if (!json_parser_load_from_file(parser, snapshotPath, &error)) {
|
|
fprintf(stderr, "Cannot read %s: %s\n", snapshotPath, error->message);
|
|
return 1;
|
|
}
|
|
JsonObject *document = json_node_get_object(json_parser_get_root(parser));
|
|
const char *format = json_object_get_string_member_with_default(document, "format", "");
|
|
if (strcmp(format, "wine-access-browser-snapshot-1") != 0) {
|
|
fprintf(stderr, "Unsupported snapshot format: %s\n", format);
|
|
return 1;
|
|
}
|
|
build_initial_tree(json_object_get_object_member(document, "root"), member_long(document, "focusedId", 0));
|
|
}
|
|
|
|
install_util();
|
|
if (atk_bridge_adaptor_init(&argc, &argv) != 0) {
|
|
fprintf(stderr, "atk-bridge initialization failed; is the accessibility bus available?\n");
|
|
return 1;
|
|
}
|
|
g_message("published %u objects as application \"%s\" (toolkit Chromium, %s mode)", g_hash_table_size(nodesById),
|
|
applicationName, liveMode ? "live" : "snapshot");
|
|
// The helper waits for this line before retiring legacy announcements.
|
|
fputs("published\n", stdout);
|
|
fflush(stdout);
|
|
|
|
mainLoop = g_main_loop_new(NULL, FALSE);
|
|
g_unix_signal_add(SIGINT, quit_loop, mainLoop);
|
|
g_unix_signal_add(SIGTERM, quit_loop, mainLoop);
|
|
if (liveMode) {
|
|
g_unix_fd_add(workerOutput, G_IO_IN | G_IO_HUP | G_IO_ERR, on_worker_output, NULL);
|
|
if (!g_queue_is_empty(pendingMessages) && !pendingIdleSource) pendingIdleSource = g_idle_add(process_pending, NULL);
|
|
}
|
|
if (announceFocus) g_timeout_add(1000, announce, NULL);
|
|
if (quitAfterSeconds > 0) g_timeout_add_seconds(quitAfterSeconds, quit_loop, mainLoop);
|
|
g_main_loop_run(mainLoop);
|
|
atk_bridge_adaptor_cleanup();
|
|
g_main_loop_unref(mainLoop);
|
|
if (workerInput >= 0) close(workerInput); // the worker exits when its input closes
|
|
if (firstTree) json_object_unref(firstTree);
|
|
if (parser) g_object_unref(parser);
|
|
return exitStatus;
|
|
}
|