editor: bundle com libraries privadas + hot reload (tinyblocks-api) e aba de imagem estilo Krita

- tinyblocks-editor agora e um bundle: libraries privadas em
  libraries/tinyblocks-editor-{widgets,image} (.a + .so), com library.toml
  (bundle = tinyblocks-editor, private = true -> nao aparecem no libman).
- O editor carrega os modulos por dlopen via tinyblocks-api e chama tudo por
  tabelas de ponteiros (tb_ed_w, tb_ed_img): da para recompilar so a parte que
  mudou e o editor recarrega ao vivo (make ed-modules), sem reiniciar.
- tinyblocks-api ganhou modulos de codigo (tb_api_code_*) e watcher de
  arquivos (inotify, com mtime como reserva).
- libman: entradas com bundle/private nao sao listadas como extensao.
- Aba de imagem: layout estilo Krita - ferramentas na esquerda, tela no meio,
  roda de cores + paleta + camadas na coluna da direita; miniatura por camada
  (como as fantasias do Scratch); rodape enxuto (Desfazer/Limpar/zoom).
- Ferramenta selecionar (mover/redimensionar/excluir pixels) e consertada a
  geometria da folha (doc = area de desenho, zoom puro).
- Makefile: alvo ed-modules; AppImage empacota os .so do bundle.
- harness: testes de imagem atualizados + testes de cores na lateral.
This commit is contained in:
mguinhos committed 2026-10-20 23:31:42 -03:00
1 parent 036162280d
commit 0143d3efd5
21 files changed
+3775 -557

No files matched your search

@@ -0,0 +1,209 @@
/*
* TinyBlocks - tb_editor_modules.c
* Carrega as libraries privadas do editor (bundle) por dlopen, usando o
* tinyblocks-api como gestor de modulos, para permitir HOT RELOAD:
*
* 1. edite uma library privada (ex: libraries/tinyblocks-editor-image)
* 2. rode "make" (so a parte que mudou e recompilada)
* 3. o editor em execucao detecta (inotify/mtime) e recarrega sozinho
*
* O estado da interface vive em Ed/Tab (no editor), entao trocar o codigo
* nao perde nada. As chamadas passam por tabelas de ponteiros (tb_ed_w,
* tb_ed_img), por isso a troca vale imediatamente para o editor inteiro.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "../internal/tb_editor_internal.h"
/* Este arquivo nomeia os campos das tabelas (w.fill_rect, ...), entao os
apelidos curtos do editor atrapalham: remove-os aqui. */
#undef col_mul
#undef rb_fill_rect
#undef rb_fill_rrect
#undef rb_fill_circle
#undef rb_stroke_rect
#undef rb_stroke_circle
#undef rb_line
#undef rb_text_c
#undef tw
#undef in_rect
/* Tabelas vivas (preenchidas aqui, usadas pelo editor) */
TbEdImageApi tb_ed_img;
TbEdWidgetsApi tb_ed_w;
/* Ordem = dependencia: widgets e a base (a imagem usa as primitivas dela) */
static const char* s_mod_name[2] = { "tinyblocks-editor-widgets",
"tinyblocks-editor-image" };
static const char* s_mod_file[2] = { "libtinyblocks-editor-widgets.so",
"libtinyblocks-editor-image.so" };
static const int s_mod_n = 2;
static char s_dir[512] = "";
const char* tb_ed_modules_dir(void) { return s_dir; }
/* -- utilidades ---------------------------------------------------------- */
static bool file_exists(const char* p) {
struct stat st;
return p && stat(p, &st) == 0 && S_ISREG(st.st_mode);
}
static void dir_of_exe(char* out, size_t cap) {
char buf[512];
ssize_t n = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
if (n <= 0) { snprintf(out, cap, "."); return; }
buf[n] = 0;
char* slash = strrchr(buf, '/');
if (slash) *slash = 0;
snprintf(out, cap, "%s", buf);
}
/* Procura o .so nos lugares conhecidos (arvore do projeto, AppImage, cwd) */
static bool find_module(const char* name, const char* file, char* out, size_t cap) {
char exe[512];
dir_of_exe(exe, sizeof(exe));
const char* env = getenv("TINYBLOCKS_ED_MODULES");
static char c[6][1024];
snprintf(c[0], sizeof(c[0]), "libraries/tinyblocks-editor/libraries/%s/build/%s", name, file);
snprintf(c[1], sizeof(c[1]), "%s/../libraries/tinyblocks-editor/libraries/%s/build/%s",
exe, name, file);
snprintf(c[2], sizeof(c[2]), "%s/../lib/tinyblocks-editor/%s", exe, file);
snprintf(c[3], sizeof(c[3]), "%s/%s", exe, file);
snprintf(c[4], sizeof(c[4]), "%s/%s", s_dir[0] ? s_dir : ".", file);
if (env && env[0]) snprintf(c[5], sizeof(c[5]), "%s/%s", env, file);
else c[5][0] = 0;
for (int i = 0; i < 6; i++) {
if (!c[i][0]) continue;
if (file_exists(c[i])) {
snprintf(out, cap, "%s", c[i]);
char* sl = strrchr(out, '/');
if (sl) { *sl = 0; snprintf(s_dir, sizeof(s_dir), "%s", out); *sl = '/'; }
return true;
}
}
return false;
}
/* Copia o endereco do simbolo para o ponteiro de funcao (sem casts fora de
padrao: dlsym devolve void*, o ponteiro de funcao tem o mesmo tamanho). */
static void sym_fn(void* dst, const char* mod, const char* name) {
void* p = tb_api_code_symbol(mod, name);
memcpy(dst, &p, sizeof(p));
}
/* Resolve as funcoes da library de widgets */
static bool resolve_widgets(void) {
TbEdWidgetsApi w;
memset(&w, 0, sizeof(w));
sym_fn(&w.col_mul, "tinyblocks-editor-widgets", "tb_ew_col_mul");
sym_fn(&w.fill_rect, "tinyblocks-editor-widgets", "tb_ew_fill_rect");
sym_fn(&w.fill_rrect, "tinyblocks-editor-widgets", "tb_ew_fill_rrect");
sym_fn(&w.fill_circle, "tinyblocks-editor-widgets", "tb_ew_fill_circle");
sym_fn(&w.stroke_rect, "tinyblocks-editor-widgets", "tb_ew_stroke_rect");
sym_fn(&w.stroke_circle, "tinyblocks-editor-widgets", "tb_ew_stroke_circle");
sym_fn(&w.line, "tinyblocks-editor-widgets", "tb_ew_line");
sym_fn(&w.text_c, "tinyblocks-editor-widgets", "tb_ew_text_c");
sym_fn(&w.text_width, "tinyblocks-editor-widgets", "tb_ew_text_width");
sym_fn(&w.in_rect, "tinyblocks-editor-widgets", "tb_ew_in_rect");
bool ok = w.col_mul && w.fill_rect && w.fill_rrect && w.fill_circle && w.stroke_rect &&
w.stroke_circle && w.line && w.text_c && w.text_width && w.in_rect;
if (ok) tb_ed_w = w;
return ok;
}
/* Resolve as funcoes da aba Imagem */
static bool resolve_image(void) {
TbEdImageApi a;
memset(&a, 0, sizeof(a));
sym_fn(&a.view, "tinyblocks-editor-image", "tb_editor_image_view");
sym_fn(&a.click, "tinyblocks-editor-image", "tb_editor_image_click");
sym_fn(&a.drag, "tinyblocks-editor-image", "tb_editor_image_drag");
sym_fn(&a.release, "tinyblocks-editor-image", "tb_editor_image_release");
sym_fn(&a.load_file, "tinyblocks-editor-image", "tb_editor_image_load_file");
sym_fn(&a.save, "tinyblocks-editor-image", "tb_editor_image_save");
sym_fn(&a.ensure, "tinyblocks-editor-image", "tb_editor_image_ensure");
sym_fn(&a.sel_reset, "tinyblocks-editor-image", "tb_editor_image_sel_reset");
sym_fn(&a.sel_commit, "tinyblocks-editor-image", "tb_editor_image_sel_commit");
bool ok = a.view && a.click && a.drag && a.release && a.load_file && a.save &&
a.ensure && a.sel_reset && a.sel_commit;
if (ok) tb_ed_img = a;
return ok;
}
/* -- carga --------------------------------------------------------------- */
bool tb_ed_modules_load(Ed* ed) {
if (!tb_api_runtime()) tb_api_init(ed->rt);
tb_ed_modules_clear();
/* ordem de dependencia: widgets antes da imagem */
for (int i = 0; i < s_mod_n; i++) {
char path[1024];
if (!find_module(s_mod_name[i], s_mod_file[i], path, sizeof(path))) {
snprintf(ed->mods.info, sizeof(ed->mods.info),
"modulo nao encontrado: %s", s_mod_file[i]);
return false;
}
if (tb_api_code_load(s_mod_name[i], path) < 0) {
snprintf(ed->mods.info, sizeof(ed->mods.info), "%.120s", tb_api_last_error());
return false;
}
ed->mods.list[ed->mods.n].name = s_mod_name[i];
ed->mods.list[ed->mods.n].file = s_mod_file[i];
ed->mods.list[ed->mods.n].ok = 1;
ed->mods.n++;
}
bool ok = resolve_widgets() && resolve_image();
ed->mods.loaded = ok;
if (ok) {
snprintf(ed->mods.info, sizeof(ed->mods.info),
"Bundle do editor carregado (%.100s)", tb_api_code_count() ? s_dir : "?");
}
return ok;
}
void tb_ed_modules_clear(void) {
tb_api_code_unload_all();
memset(&tb_ed_img, 0, sizeof(tb_ed_img));
memset(&tb_ed_w, 0, sizeof(tb_ed_w));
}
bool tb_ed_modules_check(Ed* ed) {
if (!ed->mods.loaded) return false;
if (!tb_api_code_changed()) return false;
/* Recarrega o bundle inteiro, em ordem: descarrega na ordem inversa
(para as dependencias soltarem) e carrega na ordem de dependencia. */
int n = tb_api_code_count();
char names[8][64], paths[8][512];
int keep = n < 8 ? n : 8;
for (int i = 0; i < keep; i++) {
snprintf(names[i], sizeof(names[i]), "%s", tb_api_code_name(i));
snprintf(paths[i], sizeof(paths[i]), "%s", tb_api_code_path(i));
}
tb_api_code_reload_all();
(void)names; (void)paths;
ed->mods.n = 0;
for (int i = 0; i < s_mod_n; i++) {
ed->mods.list[ed->mods.n].name = s_mod_name[i];
ed->mods.list[ed->mods.n].file = s_mod_file[i];
ed->mods.list[ed->mods.n].ok = 1;
ed->mods.n++;
}
bool ok = resolve_widgets() && resolve_image();
ed->mods.loaded = ok;
ed->mods.reloads++;
snprintf(ed->mods.info, sizeof(ed->mods.info), "Editor recarregado (%d)%s",
ed->mods.reloads, ok ? "" : " - FALHOU");
snprintf(ed->hot_status, sizeof(ed->hot_status), "%s", ed->mods.info);
ed->hot_status_ok = ok;
return true;
}