Interface de acessibilidade do editor (tinyblocks-a11y)

Library nova, no papel do ATK/GtkAccessible: cada quadro o editor monta
uma árvore acessível (papel, nome, descrição, valor, estados, ações,
retângulo), com ids estáveis, eventos entre quadros (foco, estado, nome,
valor, componente novo, aviso) e uma ponte para o narrador
(TINYBLOCKS_A11Y=log liga uma ponte de texto no stderr).

Cada parte da tela se descreve onde é desenhada: menu, áreas, categorias,
paleta, abas, blocos (rótulo falado, campos, bocas, parâmetros do
defina), grupos, palco, saída, menus e diálogos. As ações agem pelo
mesmo caminho do mouse e rolam até o componente se ele estiver fora da
vista. API: tb_editor_accessible(scene).

O harness acha componentes pela árvore (categorias, cenários 8 e 27) e
tem um cenário só de acessibilidade; test-a11y testa a library.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-23 07:24:19 -03:00
1 parent 80bd330766
commit 600c4fc520
12 files changed
+1557 -103

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
# Makefile da library a11y (interface de acessibilidade: arvore acessivel)
# Constroi build/libtinyblocks-a11y.a e build/libtinyblocks-a11y.so
CC := gcc
BUILD := build
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
OBJ := $(BUILD)/tb_a11y.o
LIB_A := $(BUILD)/libtinyblocks-a11y.a
LIB_SO := $(BUILD)/libtinyblocks-a11y.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_a11y.c include/tb_a11y.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ)
$(CC) -shared -o $@ $^
clean:
$(RM) -r $(BUILD)
+203
View File
@@ -0,0 +1,203 @@
/*
* TinyBlocks - tb_a11y.h
* Interface de acessibilidade (no espirito do ATK/GtkAccessible do GTK).
*
* Uma interface grafica descreve a cada quadro uma ARVORE ACESSIVEL: cada
* componente (botao, aba, bloco, campo...) vira um no com papel, nome,
* descricao, valor, estados, acoes e retangulo na tela. Quem consome:
* - um narrador / leitor de tela, por uma ponte (TinyBlocks_A11yBridge)
* que recebe os eventos (foco mudou, estado mudou, aviso...);
* - a automacao de testes, que acha componentes por papel + nome e os
* aciona por tb_a11y_do_action, sem depender de texto desenhado nem de
* coordenadas fixas.
*
* Construcao (modo imediato, a cada quadro):
*
* tb_a11y_begin(tree, "TinyBlocks", w, h);
* int bar = tb_a11y_push(tree, TB_A11Y_TOOLBAR, "Menu", 0, 0, w, 44);
* int b = tb_a11y_add(tree, TB_A11Y_BUTTON, "Abrir", x, y, bw, bh);
* tb_a11y_set_actions(tree, b, TB_A11Y_ACT_ACTIVATE);
* tb_a11y_pop(tree);
* tb_a11y_end(tree); // compara com o quadro anterior e gera eventos
*
* Os ids dos nos sao estaveis entre quadros (derivados do caminho: papel +
* nome + ordem entre irmaos iguais), entao foco e selecao podem ser seguidos.
*/
#ifndef TINYBLOCKS_TB_A11Y_H
#define TINYBLOCKS_TB_A11Y_H
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <tinyblocks/tb_defs.h>
/* == Papeis (o "tipo" do componente, como ATK_ROLE_* / GTK_ACCESSIBLE_ROLE_*) */
typedef enum TinyBlocks_A11yRole {
TB_A11Y_NONE = 0,
TB_A11Y_APPLICATION, /* raiz */
TB_A11Y_PANE, /* area da janela (paleta, scripts, palco) */
TB_A11Y_TOOLBAR, /* barra de botoes */
TB_A11Y_BUTTON,
TB_A11Y_CHECKBOX,
TB_A11Y_RADIO, /* uma opcao entre varias (ex.: cor) */
TB_A11Y_TEXT_FIELD, /* campo de texto editavel */
TB_A11Y_LABEL, /* texto estatico */
TB_A11Y_LIST,
TB_A11Y_LIST_ITEM,
TB_A11Y_TAB_LIST,
TB_A11Y_TAB,
TB_A11Y_TAB_PANEL,
TB_A11Y_MENU,
TB_A11Y_MENU_ITEM,
TB_A11Y_DIALOG,
TB_A11Y_GROUP, /* agrupamento (ex.: grupo colorido) */
TB_A11Y_CANVAS, /* area de desenho (palco, imagem) */
TB_A11Y_STATUS, /* aviso / mensagem (regiao "viva") */
TB_A11Y_BLOCK, /* um bloco de programacao */
TB_A11Y_SLOT, /* encaixe vazio de um bloco (ex.: booleano) */
TB_A11Y_ROLE_COUNT
} TinyBlocks_A11yRole;
/* == Estados (bits) */
enum {
TB_A11Y_FOCUSED = 1u << 0, /* recebe o teclado */
TB_A11Y_SELECTED = 1u << 1, /* aba/categoria/opcao atual */
TB_A11Y_CHECKED = 1u << 2,
TB_A11Y_EXPANDED = 1u << 3, /* menu aberto */
TB_A11Y_DISABLED = 1u << 4,
TB_A11Y_EDITABLE = 1u << 5,
TB_A11Y_EDITING = 1u << 6, /* texto sendo digitado agora */
TB_A11Y_MODAL = 1u << 7,
TB_A11Y_HOVERED = 1u << 8,
TB_A11Y_BUSY = 1u << 9, /* rodando */
TB_A11Y_OFFSCREEN = 1u << 10, /* existe, mas fora da area visivel */
};
/* == Acoes (bits), executadas por tb_a11y_do_action */
typedef enum TinyBlocks_A11yAction {
TB_A11Y_ACT_ACTIVATE = 1u << 0, /* clicar / pressionar */
TB_A11Y_ACT_MENU = 1u << 1, /* abrir o menu de contexto */
TB_A11Y_ACT_EDIT = 1u << 2, /* comecar a editar o texto */
TB_A11Y_ACT_DRAG = 1u << 3, /* pode ser arrastado */
TB_A11Y_ACT_DROP = 1u << 4, /* aceita algo solto em cima */
} TinyBlocks_A11yAction;
#define TB_A11Y_NAME_MAX 128
#define TB_A11Y_DESC_MAX 192
#define TB_A11Y_VALUE_MAX 128
typedef struct TinyBlocks_A11yNode {
uint32_t id; /* estavel entre quadros */
uint32_t key; /* hash da chave (o nome, se nao *
* houver set_key); entra no id */
int parent; /* indice do pai (-1 na raiz) */
int first_child, last_child, next_sibling;
int child_count;
int depth;
TinyBlocks_A11yRole role;
uint32_t states;
uint32_t actions;
float x, y, w, h; /* retangulo na janela */
float px, py; /* onde agir (clique); centro se nao *
* houver set_point */
char name[TB_A11Y_NAME_MAX];
char description[TB_A11Y_DESC_MAX];
char value[TB_A11Y_VALUE_MAX];
} TinyBlocks_A11yNode;
/* == Eventos (o que muda entre quadros), para o narrador */
typedef enum TinyBlocks_A11yEventKind {
TB_A11Y_EV_FOCUS, /* `node` ganhou o foco */
TB_A11Y_EV_STATE, /* estados de `node` mudaram (old_states) */
TB_A11Y_EV_NAME, /* nome mudou */
TB_A11Y_EV_VALUE, /* valor mudou */
TB_A11Y_EV_SHOWN, /* no novo (dialogos, menus, avisos) */
TB_A11Y_EV_ANNOUNCE, /* aviso para ler em voz alta (`text`) */
} TinyBlocks_A11yEventKind;
typedef struct TinyBlocks_A11yEvent {
TinyBlocks_A11yEventKind kind;
int node; /* indice no quadro atual (-1 em ANNOUNCE) */
uint32_t id;
uint32_t old_states;
char text[TB_A11Y_DESC_MAX];
} TinyBlocks_A11yEvent;
typedef struct TinyBlocks_A11yTree TinyBlocks_A11yTree;
/* == Ponte para um narrador (interface implementada por cada plataforma:
* AT-SPI no Linux, UI Automation no Windows, um sintetizador de voz...) */
typedef struct TinyBlocks_A11yBridge {
const char* name;
void (*on_event)(void* user, const TinyBlocks_A11yTree* tree, const TinyBlocks_A11yEvent* ev);
void (*on_frame)(void* user, const TinyBlocks_A11yTree* tree); /* arvore nova (pode ser NULL) */
void* user;
} TinyBlocks_A11yBridge;
/* == Quem desenha a interface executa as acoes (ex.: clicar no centro) */
typedef bool (*TinyBlocks_A11yPerform)(void* user, const TinyBlocks_A11yTree* tree,
int node, TinyBlocks_A11yAction action);
/* -- Ciclo de vida --- */
TB_API TinyBlocks_A11yTree* tb_a11y_tree_new(void);
TB_API void tb_a11y_tree_free(TinyBlocks_A11yTree* tree);
TB_API void tb_a11y_set_perform(TinyBlocks_A11yTree* tree, TinyBlocks_A11yPerform fn, void* user);
TB_API void tb_a11y_set_bridge(TinyBlocks_A11yTree* tree, const TinyBlocks_A11yBridge* bridge);
/* -- Construcao (a cada quadro) --- */
TB_API void tb_a11y_begin(TinyBlocks_A11yTree* tree, const char* app_name, float w, float h);
TB_API int tb_a11y_push(TinyBlocks_A11yTree* tree, TinyBlocks_A11yRole role, const char* name,
float x, float y, float w, float h); /* abre um container */
TB_API int tb_a11y_add (TinyBlocks_A11yTree* tree, TinyBlocks_A11yRole role, const char* name,
float x, float y, float w, float h); /* folha */
TB_API void tb_a11y_pop (TinyBlocks_A11yTree* tree);
TB_API int tb_a11y_current(const TinyBlocks_A11yTree* tree); /* container aberto */
TB_API void tb_a11y_end (TinyBlocks_A11yTree* tree);
TB_API void tb_a11y_set_states (TinyBlocks_A11yTree* tree, int node, uint32_t states);
TB_API void tb_a11y_add_states (TinyBlocks_A11yTree* tree, int node, uint32_t states);
TB_API void tb_a11y_set_actions (TinyBlocks_A11yTree* tree, int node, uint32_t actions);
TB_API void tb_a11y_set_value (TinyBlocks_A11yTree* tree, int node, const char* value);
TB_API void tb_a11y_set_description(TinyBlocks_A11yTree* tree, int node, const char* desc);
TB_API void tb_a11y_set_name (TinyBlocks_A11yTree* tree, int node, const char* name);
/* Chave estavel para o id quando o nome muda com o conteudo (ex.: um bloco
* "repita (10) vezes" usa o tipo como chave). Chame antes dos filhos. */
TB_API void tb_a11y_set_key (TinyBlocks_A11yTree* tree, int node, const char* key);
/* Ponto onde as acoes agem (o centro pode cair num filho) */
TB_API void tb_a11y_set_point (TinyBlocks_A11yTree* tree, int node, float x, float y);
/* Aviso para o narrador ler (mensagens de status, erros) */
TB_API void tb_a11y_announce(TinyBlocks_A11yTree* tree, const char* text);
/* -- Consulta --- */
TB_API int tb_a11y_count(const TinyBlocks_A11yTree* tree);
TB_API const TinyBlocks_A11yNode* tb_a11y_node(const TinyBlocks_A11yTree* tree, int node);
TB_API int tb_a11y_root(const TinyBlocks_A11yTree* tree);
TB_API int tb_a11y_focus(const TinyBlocks_A11yTree* tree); /* -1 se nenhum */
TB_API int tb_a11y_by_id(const TinyBlocks_A11yTree* tree, uint32_t id);
/* n-esimo descendente de `from` com o papel (NONE = qualquer) e o nome
* (NULL = qualquer). Devolve o indice ou -1. */
TB_API int tb_a11y_find(const TinyBlocks_A11yTree* tree, int from, TinyBlocks_A11yRole role,
const char* name, int nth);
/* Como find, mas o nome so precisa conter `part` */
TB_API int tb_a11y_find_part(const TinyBlocks_A11yTree* tree, int from, TinyBlocks_A11yRole role,
const char* part, int nth);
/* Caminho separado por " > " (ex.: "Paleta > Meus Blocos > Criar um Bloco") */
TB_API char* tb_a11y_path(const TinyBlocks_A11yTree* tree, int node, char* out, size_t cap);
TB_API void tb_a11y_center(const TinyBlocks_A11yTree* tree, int node, float* x, float* y);
TB_API void tb_a11y_point (const TinyBlocks_A11yTree* tree, int node, float* x, float* y);
TB_API const char* tb_a11y_role_name(TinyBlocks_A11yRole role); /* "botão", "aba"... */
/* Arvore em texto, um no por linha (para depuracao e testes) */
TB_API void tb_a11y_dump(const TinyBlocks_A11yTree* tree, int from, FILE* out);
/* -- Acoes e eventos --- */
TB_API bool tb_a11y_do_action(TinyBlocks_A11yTree* tree, int node, TinyBlocks_A11yAction action);
/* Eventos do ultimo tb_a11y_end (tambem enviados a ponte) */
TB_API int tb_a11y_event_count(const TinyBlocks_A11yTree* tree);
TB_API const TinyBlocks_A11yEvent* tb_a11y_event(const TinyBlocks_A11yTree* tree, int i);
/* Ponte simples que escreve os eventos num FILE* (um "narrador" de texto) */
TB_API TinyBlocks_A11yBridge tb_a11y_log_bridge(FILE* out);
/* Frase que um narrador diria para o no: "Abrir, botão" */
TB_API char* tb_a11y_describe(const TinyBlocks_A11yTree* tree, int node, char* out, size_t cap);
#endif /* TINYBLOCKS_TB_A11Y_H */
+18
View File
@@ -0,0 +1,18 @@
# Metadados da library a11y. Lido pela UI/build sem recompilar o .so.
# Nao tem blocos: e a interface de acessibilidade usada pelo editor (narrador
# e automacao de testes); por isso e privada (fora da lista de extensoes).
[library]
name = "tinyblocks-a11y"
private = true
version = "1.0.0"
category = "Acessibilidade"
description = "Interface de acessibilidade: arvore acessivel, acoes e eventos"
color = [76, 151, 255, 255]
dark = [51, 115, 230, 255]
[build]
sources = ["src/tb_a11y.c"]
includes = ["include"]
shared = "build/libtinyblocks-a11y.so"
static = "build/libtinyblocks-a11y.a"
depends = []
+458
View File
@@ -0,0 +1,458 @@
/*
* TinyBlocks - tb_a11y.c
* Arvore acessivel em modo imediato (ver tb_a11y.h).
*/
#include <stdlib.h>
#include <string.h>
#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",
};
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;
return t->perform(t->perform_user, t, node, a);
}
/* -- 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;
}