/* * TinyBlocks - tb_a11y.c * Arvore acessivel em modo imediato (ver tb_a11y.h). */ #include #include #include "tb_a11y.h" #define STACK_MAX 64 typedef struct PrevNode { /* o que importa do quadro anterior, por id */ uint32_t id; uint32_t states; uint32_t name_h, value_h; } PrevNode; struct TinyBlocks_A11yTree { TinyBlocks_A11yNode* nodes; int n, cap; int stack[STACK_MAX]; int sp; int focus; PrevNode* prev; /* tabela hash (aberta) do quadro anterior */ int prev_cap; uint32_t prev_focus; bool has_prev; TinyBlocks_A11yEvent* events; int ev_n, ev_cap; char announce[8][TB_A11Y_DESC_MAX]; int announce_n; TinyBlocks_A11yPerform perform; void* perform_user; TinyBlocks_A11yBridge bridge; bool has_bridge; }; /* -- Utilitarios --- */ static uint32_t fnv(uint32_t h, const void* data, size_t n) { const unsigned char* p = data; for (size_t i = 0; i < n; i++) { h ^= p[i]; h *= 16777619u; } return h; } static uint32_t str_hash(const char* s) { return fnv(2166136261u, s, strlen(s)); } static void scopy(char* dst, size_t cap, const char* src) { if (!src) src = ""; size_t n = strlen(src); if (n >= cap) { /* corta sem partir letra UTF-8 */ n = cap - 1; while (n > 0 && ((unsigned char)src[n] & 0xC0) == 0x80) n--; } memcpy(dst, src, n); dst[n] = 0; } static bool valid(const TinyBlocks_A11yTree* t, int node) { return t && node >= 0 && node < t->n; } /* -- Ciclo de vida --- */ TinyBlocks_A11yTree* tb_a11y_tree_new(void) { TinyBlocks_A11yTree* t = calloc(1, sizeof(*t)); if (t) t->focus = -1; return t; } void tb_a11y_tree_free(TinyBlocks_A11yTree* t) { if (!t) return; free(t->nodes); free(t->prev); free(t->events); free(t); } void tb_a11y_set_perform(TinyBlocks_A11yTree* t, TinyBlocks_A11yPerform fn, void* user) { if (!t) return; t->perform = fn; t->perform_user = user; } void tb_a11y_set_bridge(TinyBlocks_A11yTree* t, const TinyBlocks_A11yBridge* b) { if (!t) return; t->has_bridge = b != NULL; if (b) t->bridge = *b; } /* -- Construcao --- */ /* id = hash(pai, papel, chave, ordem entre os irmaos com o mesmo papel e chave) */ static uint32_t node_id(const TinyBlocks_A11yTree* t, int parent, TinyBlocks_A11yRole role, uint32_t key, int self) { uint32_t h = parent >= 0 ? t->nodes[parent].id : 2166136261u; int ord = 0; if (parent >= 0) for (int c = t->nodes[parent].first_child; c >= 0 && c != self; c = t->nodes[c].next_sibling) if (t->nodes[c].role == role && t->nodes[c].key == key) ord++; h = fnv(h, &role, sizeof(role)); h = fnv(h, &key, sizeof(key)); h = fnv(h, &ord, sizeof(ord)); return h ? h : 1; } static int new_node(TinyBlocks_A11yTree* t, TinyBlocks_A11yRole role, const char* name, float x, float y, float w, float h) { if (t->n == t->cap) { int cap = t->cap ? t->cap * 2 : 256; TinyBlocks_A11yNode* nn = realloc(t->nodes, (size_t)cap * sizeof(*nn)); if (!nn) return -1; t->nodes = nn; t->cap = cap; } int i = t->n++; TinyBlocks_A11yNode* nd = &t->nodes[i]; memset(nd, 0, sizeof(*nd)); int parent = t->sp > 0 ? t->stack[t->sp - 1] : -1; nd->parent = parent; nd->first_child = nd->last_child = nd->next_sibling = -1; nd->role = role; nd->x = x; nd->y = y; nd->w = w; nd->h = h; nd->px = x + w * 0.5f; nd->py = y + h * 0.5f; scopy(nd->name, sizeof(nd->name), name); if (parent >= 0) { TinyBlocks_A11yNode* p = &t->nodes[parent]; nd->depth = p->depth + 1; if (p->last_child >= 0) t->nodes[p->last_child].next_sibling = i; else p->first_child = i; p->last_child = i; p->child_count++; } nd->key = str_hash(nd->name); nd->id = node_id(t, parent, role, nd->key, i); return i; } void tb_a11y_begin(TinyBlocks_A11yTree* t, const char* app_name, float w, float h) { if (!t) return; t->n = 0; t->sp = 0; t->focus = -1; t->announce_n = 0; int r = new_node(t, TB_A11Y_APPLICATION, app_name, 0, 0, w, h); if (r >= 0) t->stack[t->sp++] = r; } int tb_a11y_push(TinyBlocks_A11yTree* t, TinyBlocks_A11yRole role, const char* name, float x, float y, float w, float h) { if (!t) return -1; int i = new_node(t, role, name, x, y, w, h); if (i >= 0 && t->sp < STACK_MAX) t->stack[t->sp++] = i; return i; } int tb_a11y_add(TinyBlocks_A11yTree* t, TinyBlocks_A11yRole role, const char* name, float x, float y, float w, float h) { return t ? new_node(t, role, name, x, y, w, h) : -1; } void tb_a11y_pop(TinyBlocks_A11yTree* t) { if (t && t->sp > 1) t->sp--; /* a raiz fica */ } int tb_a11y_current(const TinyBlocks_A11yTree* t) { return t && t->sp > 0 ? t->stack[t->sp - 1] : -1; } void tb_a11y_set_states(TinyBlocks_A11yTree* t, int node, uint32_t s) { if (!valid(t, node)) return; t->nodes[node].states = s; if (s & TB_A11Y_FOCUSED) t->focus = node; } void tb_a11y_add_states(TinyBlocks_A11yTree* t, int node, uint32_t s) { if (valid(t, node)) tb_a11y_set_states(t, node, t->nodes[node].states | s); } void tb_a11y_set_actions(TinyBlocks_A11yTree* t, int node, uint32_t a) { if (valid(t, node)) t->nodes[node].actions = a; } void tb_a11y_set_value(TinyBlocks_A11yTree* t, int node, const char* v) { if (valid(t, node)) scopy(t->nodes[node].value, sizeof(t->nodes[node].value), v); } void tb_a11y_set_description(TinyBlocks_A11yTree* t, int node, const char* d) { if (valid(t, node)) scopy(t->nodes[node].description, sizeof(t->nodes[node].description), d); } void tb_a11y_set_name(TinyBlocks_A11yTree* t, int node, const char* name) { if (valid(t, node)) scopy(t->nodes[node].name, sizeof(t->nodes[node].name), name); } /* Muda o id: so antes de o no ganhar filhos (o id deles depende do dele) */ void tb_a11y_set_key(TinyBlocks_A11yTree* t, int node, const char* key) { if (!valid(t, node) || !key) return; TinyBlocks_A11yNode* nd = &t->nodes[node]; nd->key = str_hash(key); if (nd->child_count == 0) nd->id = node_id(t, nd->parent, nd->role, nd->key, node); } void tb_a11y_set_point(TinyBlocks_A11yTree* t, int node, float x, float y) { if (!valid(t, node)) return; t->nodes[node].px = x; t->nodes[node].py = y; } void tb_a11y_announce(TinyBlocks_A11yTree* t, const char* text) { if (!t || !text || !text[0] || t->announce_n >= 8) return; scopy(t->announce[t->announce_n++], TB_A11Y_DESC_MAX, text); } /* -- Eventos (diferenca para o quadro anterior) --- */ static void ev_push(TinyBlocks_A11yTree* t, TinyBlocks_A11yEventKind kind, int node, uint32_t old_states, const char* text) { if (t->ev_n == t->ev_cap) { int cap = t->ev_cap ? t->ev_cap * 2 : 32; TinyBlocks_A11yEvent* ne = realloc(t->events, (size_t)cap * sizeof(*ne)); if (!ne) return; t->events = ne; t->ev_cap = cap; } TinyBlocks_A11yEvent* e = &t->events[t->ev_n++]; memset(e, 0, sizeof(*e)); e->kind = kind; e->node = node; e->id = node >= 0 ? t->nodes[node].id : 0; e->old_states = old_states; if (text) scopy(e->text, sizeof(e->text), text); } static const PrevNode* prev_find(const TinyBlocks_A11yTree* t, uint32_t id) { if (!t->prev_cap) return NULL; for (int k = 0, i = (int)(id & (uint32_t)(t->prev_cap - 1)); k < t->prev_cap; k++, i = (i + 1) & (t->prev_cap - 1)) { if (t->prev[i].id == 0) return NULL; if (t->prev[i].id == id) return &t->prev[i]; } return NULL; } static void prev_store(TinyBlocks_A11yTree* t) { int cap = 64; while (cap < t->n * 2) cap *= 2; if (cap != t->prev_cap) { PrevNode* np = realloc(t->prev, (size_t)cap * sizeof(*np)); if (!np) { t->has_prev = false; return; } t->prev = np; t->prev_cap = cap; } memset(t->prev, 0, (size_t)t->prev_cap * sizeof(*t->prev)); for (int n = 0; n < t->n; n++) { const TinyBlocks_A11yNode* nd = &t->nodes[n]; int i = (int)(nd->id & (uint32_t)(cap - 1)); while (t->prev[i].id != 0 && t->prev[i].id != nd->id) i = (i + 1) & (cap - 1); t->prev[i].id = nd->id; t->prev[i].states = nd->states; t->prev[i].name_h = str_hash(nd->name); t->prev[i].value_h = str_hash(nd->value); } t->prev_focus = t->focus >= 0 ? t->nodes[t->focus].id : 0; t->has_prev = true; } /* estados que interessam ao narrador (passar o mouse nao) */ #define STATE_EV_MASK (TB_A11Y_SELECTED | TB_A11Y_CHECKED | TB_A11Y_EXPANDED | TB_A11Y_DISABLED | \ TB_A11Y_EDITING | TB_A11Y_BUSY) static bool shows_up(TinyBlocks_A11yRole r) { return r == TB_A11Y_DIALOG || r == TB_A11Y_MENU || r == TB_A11Y_STATUS; } void tb_a11y_end(TinyBlocks_A11yTree* t) { if (!t) return; t->ev_n = 0; if (t->has_prev) { uint32_t fid = t->focus >= 0 ? t->nodes[t->focus].id : 0; if (fid && fid != t->prev_focus) ev_push(t, TB_A11Y_EV_FOCUS, t->focus, 0, NULL); for (int n = 0; n < t->n; n++) { const TinyBlocks_A11yNode* nd = &t->nodes[n]; const PrevNode* p = prev_find(t, nd->id); if (!p) { if (shows_up(nd->role)) ev_push(t, TB_A11Y_EV_SHOWN, n, 0, nd->name); continue; } if ((p->states ^ nd->states) & STATE_EV_MASK) ev_push(t, TB_A11Y_EV_STATE, n, p->states, NULL); if (p->name_h != str_hash(nd->name)) ev_push(t, TB_A11Y_EV_NAME, n, 0, nd->name); if (p->value_h != str_hash(nd->value)) ev_push(t, TB_A11Y_EV_VALUE, n, 0, nd->value); } } for (int i = 0; i < t->announce_n; i++) ev_push(t, TB_A11Y_EV_ANNOUNCE, -1, 0, t->announce[i]); prev_store(t); if (t->has_bridge) { if (t->bridge.on_frame) t->bridge.on_frame(t->bridge.user, t); if (t->bridge.on_event) for (int i = 0; i < t->ev_n; i++) t->bridge.on_event(t->bridge.user, t, &t->events[i]); } } int tb_a11y_event_count(const TinyBlocks_A11yTree* t) { return t ? t->ev_n : 0; } const TinyBlocks_A11yEvent* tb_a11y_event(const TinyBlocks_A11yTree* t, int i) { return t && i >= 0 && i < t->ev_n ? &t->events[i] : NULL; } /* -- Consulta --- */ int tb_a11y_count(const TinyBlocks_A11yTree* t) { return t ? t->n : 0; } const TinyBlocks_A11yNode* tb_a11y_node(const TinyBlocks_A11yTree* t, int node) { return valid(t, node) ? &t->nodes[node] : NULL; } int tb_a11y_root(const TinyBlocks_A11yTree* t) { return t && t->n > 0 ? 0 : -1; } int tb_a11y_focus(const TinyBlocks_A11yTree* t) { return t ? t->focus : -1; } int tb_a11y_by_id(const TinyBlocks_A11yTree* t, uint32_t id) { for (int i = 0; t && i < t->n; i++) if (t->nodes[i].id == id) return i; return -1; } /* Os nos sao gravados em pre-ordem: os descendentes de `from` vem depois dele */ static int find_impl(const TinyBlocks_A11yTree* t, int from, TinyBlocks_A11yRole role, const char* name, bool part, int nth) { if (!t || t->n == 0 || from >= t->n) return -1; if (from < 0) from = 0; for (int i = from + 1; i < t->n; i++) { const TinyBlocks_A11yNode* nd = &t->nodes[i]; if (nd->depth <= t->nodes[from].depth) break; /* saiu da subarvore */ if (role != TB_A11Y_NONE && nd->role != role) continue; if (name && (part ? !strstr(nd->name, name) : strcmp(nd->name, name) != 0)) continue; if (nth-- > 0) continue; return i; } return -1; } int tb_a11y_find(const TinyBlocks_A11yTree* t, int from, TinyBlocks_A11yRole role, const char* name, int nth) { return find_impl(t, from, role, name, false, nth); } int tb_a11y_find_part(const TinyBlocks_A11yTree* t, int from, TinyBlocks_A11yRole role, const char* part, int nth) { return find_impl(t, from, role, part, true, nth); } void tb_a11y_center(const TinyBlocks_A11yTree* t, int node, float* x, float* y) { if (!valid(t, node)) { *x = *y = 0; return; } const TinyBlocks_A11yNode* nd = &t->nodes[node]; *x = nd->x + nd->w * 0.5f; *y = nd->y + nd->h * 0.5f; } void tb_a11y_point(const TinyBlocks_A11yTree* t, int node, float* x, float* y) { if (!valid(t, node)) { *x = *y = 0; return; } *x = t->nodes[node].px; *y = t->nodes[node].py; } char* tb_a11y_path(const TinyBlocks_A11yTree* t, int node, char* out, size_t cap) { if (!cap) return out; out[0] = 0; if (!valid(t, node)) return out; int chain[64], n = 0; for (int p = node; p > 0 && n < 64; p = t->nodes[p].parent) chain[n++] = p; /* sem a raiz */ size_t len = 0; for (int i = n - 1; i >= 0 && len + 1 < cap; i--) { const char* nm = t->nodes[chain[i]].name[0] ? t->nodes[chain[i]].name : tb_a11y_role_name(t->nodes[chain[i]].role); int w = snprintf(out + len, cap - len, "%s%s", i == n - 1 ? "" : " > ", nm); if (w < 0) break; len += (size_t)w; } return out; } const char* tb_a11y_role_name(TinyBlocks_A11yRole r) { static const char* names[TB_A11Y_ROLE_COUNT] = { [TB_A11Y_NONE] = "", [TB_A11Y_APPLICATION] = "aplicação", [TB_A11Y_PANE] = "área", [TB_A11Y_TOOLBAR] = "barra de ferramentas", [TB_A11Y_BUTTON] = "botão", [TB_A11Y_CHECKBOX] = "caixa de seleção", [TB_A11Y_RADIO] = "opção", [TB_A11Y_TEXT_FIELD] = "campo de texto", [TB_A11Y_LABEL] = "texto", [TB_A11Y_LIST] = "lista", [TB_A11Y_LIST_ITEM] = "item", [TB_A11Y_TAB_LIST] = "lista de abas", [TB_A11Y_TAB] = "aba", [TB_A11Y_TAB_PANEL] = "painel", [TB_A11Y_MENU] = "menu", [TB_A11Y_MENU_ITEM] = "item de menu", [TB_A11Y_DIALOG] = "diálogo", [TB_A11Y_GROUP] = "grupo", [TB_A11Y_CANVAS] = "tela de desenho", [TB_A11Y_STATUS] = "aviso", [TB_A11Y_BLOCK] = "bloco", [TB_A11Y_SLOT] = "encaixe", [TB_A11Y_SEPARATOR] = "divisória", [TB_A11Y_TOOLTIP] = "dica", }; return r >= 0 && r < TB_A11Y_ROLE_COUNT ? names[r] : ""; } /* "Abrir, botão" / "x, campo de texto, 10, editando" */ char* tb_a11y_describe(const TinyBlocks_A11yTree* t, int node, char* out, size_t cap) { if (!cap) return out; out[0] = 0; if (!valid(t, node)) return out; const TinyBlocks_A11yNode* nd = &t->nodes[node]; size_t len = 0; #define ADD(...) do { int w_ = snprintf(out + len, cap - len, __VA_ARGS__); \ if (w_ > 0) len = len + (size_t)w_ < cap ? len + (size_t)w_ : cap - 1; } while (0) if (nd->name[0]) ADD("%s, ", nd->name); ADD("%s", tb_a11y_role_name(nd->role)); if (nd->value[0]) ADD(", %s", nd->value); static const struct { uint32_t bit; const char* txt; } st[] = { { TB_A11Y_SELECTED, "selecionado" }, { TB_A11Y_CHECKED, "marcado" }, { TB_A11Y_EXPANDED, "aberto" }, { TB_A11Y_DISABLED, "desativado" }, { TB_A11Y_EDITING, "editando" }, { TB_A11Y_BUSY, "rodando" }, }; for (size_t i = 0; i < sizeof(st) / sizeof(st[0]); i++) if (nd->states & st[i].bit) ADD(", %s", st[i].txt); if (nd->description[0]) ADD(". %s", nd->description); #undef ADD return out; } void tb_a11y_dump(const TinyBlocks_A11yTree* t, int from, FILE* out) { if (!t || t->n == 0 || !out || from >= t->n) return; if (from < 0) from = 0; int base = t->nodes[from].depth; for (int i = from; i < t->n; i++) { const TinyBlocks_A11yNode* nd = &t->nodes[i]; if (i > from && nd->depth <= base) break; char d[400]; fprintf(out, "%*s%s [%.0f,%.0f %.0fx%.0f]\n", (nd->depth - base) * 2, "", tb_a11y_describe(t, i, d, sizeof(d)), nd->x, nd->y, nd->w, nd->h); } } /* -- Acoes --- */ bool tb_a11y_do_action(TinyBlocks_A11yTree* t, int node, TinyBlocks_A11yAction a) { if (!valid(t, node) || !t->perform || !(t->nodes[node].actions & (uint32_t)a)) return false; TinyBlocks_A11yRequest req = { a, -1, false, 0, 0 }; return t->perform(t->perform_user, t, node, &req); } static bool drop_impl(TinyBlocks_A11yTree* t, int node, int target, bool at, float x, float y) { if (!valid(t, node) || !valid(t, target) || !t->perform) return false; if (!(t->nodes[node].actions & TB_A11Y_ACT_DRAG) || !(t->nodes[target].actions & TB_A11Y_ACT_DROP)) return false; TinyBlocks_A11yRequest req = { TB_A11Y_ACT_DRAG, target, at, x, y }; return t->perform(t->perform_user, t, node, &req); } bool tb_a11y_drop(TinyBlocks_A11yTree* t, int node, int target) { return drop_impl(t, node, target, false, 0, 0); } bool tb_a11y_drop_at(TinyBlocks_A11yTree* t, int node, int target, float x, float y) { return drop_impl(t, node, target, true, x, y); } /* -- Ponte de texto --- */ static void log_event(void* user, const TinyBlocks_A11yTree* t, const TinyBlocks_A11yEvent* ev) { FILE* out = user; char d[400]; switch (ev->kind) { case TB_A11Y_EV_FOCUS: fprintf(out, "[a11y] foco: %s\n", tb_a11y_describe(t, ev->node, d, sizeof(d))); break; case TB_A11Y_EV_STATE: fprintf(out, "[a11y] estado: %s\n", tb_a11y_describe(t, ev->node, d, sizeof(d))); break; case TB_A11Y_EV_NAME: fprintf(out, "[a11y] nome: %s\n", ev->text); break; case TB_A11Y_EV_VALUE: fprintf(out, "[a11y] valor: %s\n", tb_a11y_describe(t, ev->node, d, sizeof(d))); break; case TB_A11Y_EV_SHOWN: fprintf(out, "[a11y] abriu: %s\n", tb_a11y_describe(t, ev->node, d, sizeof(d))); break; case TB_A11Y_EV_ANNOUNCE: fprintf(out, "[a11y] aviso: %s\n", ev->text); break; } fflush(out); } TinyBlocks_A11yBridge tb_a11y_log_bridge(FILE* out) { TinyBlocks_A11yBridge b = { "texto", log_event, NULL, out }; return b; }