Files
tinyblocks/libraries/tinyblocks-editor/src/tb_editor_modules.c
T
mguinhosandClaude Opus 5.5 70c819393c Editor: ripple do M3, formas lisas e encaixes como no Scratch
- Ripple (gota que cresce do toque/clique) em todo componente com camada
  de estado; recortado no formato do componente (tb_ew_ripple).
- TB_DRAW_FILL_POLY: poligono convexo com antialias (pontos no espaco do
  text). Hexagono e encaixe deixam de ser faixas de 1 px (sumiam em linhas
  com zoom e na densidade do celular).
- Encaixe macho com cantos arredondados; encaixe femea recortado no topo
  dos comandos e dos blocos C (e nos bracos de baixo/"senao").
- Destaque ao soltar um reporter num campo segue a forma do campo; sombra
  de encaixe da pilha com a forma de bloco.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 22:23:32 -03:00

286 lines
12 KiB
C

/*
* 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.
*
* Com TB_ED_STATIC_MODULES (build Windows) os modulos vem linkados no
* proprio executavel: as tabelas apontam direto para as funcoes e nao ha
* hot reload.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <tb_editor_internal.h>
#ifdef TB_ED_STATIC_MODULES
# include <tb_editor_widgets.h>
# include <tb_editor_image.h>
# include <tb_editor_files.h>
#endif
/* 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;
TbEdFilesApi tb_ed_files;
/* Ordem = dependencia: widgets e a base (a imagem usa as primitivas dela) */
static const char* s_mod_name[3] = { "tinyblocks-editor-widgets",
"tinyblocks-editor-image",
"tinyblocks-editor-files" };
static const char* s_mod_file[3] = { "libtinyblocks-editor-widgets.so",
"libtinyblocks-editor-image.so",
"libtinyblocks-editor-files.so" };
static const int s_mod_n = 3;
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");
sym_fn(&w.text_f, "tinyblocks-editor-widgets", "tb_ew_text_f");
sym_fn(&w.icon, "tinyblocks-editor-widgets", "tb_ew_icon");
sym_fn(&w.shadow, "tinyblocks-editor-widgets", "tb_ew_shadow");
sym_fn(&w.anim_now, "tinyblocks-editor-widgets", "tb_ew_anim_now");
sym_fn(&w.ripple_down, "tinyblocks-editor-widgets", "tb_ew_ripple_down");
sym_fn(&w.ripple_up, "tinyblocks-editor-widgets", "tb_ew_ripple_up");
sym_fn(&w.ripple, "tinyblocks-editor-widgets", "tb_ew_ripple");
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 &&
w.text_f && w.icon && w.shadow && w.anim_now && w.ripple_down && w.ripple_up && w.ripple;
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");
sym_fn(&a.zoom, "tinyblocks-editor-image", "tb_editor_image_zoom");
bool ok = a.view && a.click && a.drag && a.release && a.load_file && a.save &&
a.ensure && a.sel_reset && a.sel_commit && a.zoom;
if (ok) tb_ed_img = a;
return ok;
}
/* Resolve as funcoes do explorador de arquivos */
static bool resolve_files(void) {
TbEdFilesApi a;
memset(&a, 0, sizeof(a));
sym_fn(&a.open, "tinyblocks-editor-files", "tb_editor_files_open");
sym_fn(&a.close, "tinyblocks-editor-files", "tb_editor_files_close");
sym_fn(&a.view, "tinyblocks-editor-files", "tb_editor_files_view");
sym_fn(&a.click, "tinyblocks-editor-files", "tb_editor_files_click");
sym_fn(&a.key, "tinyblocks-editor-files", "tb_editor_files_key");
sym_fn(&a.text, "tinyblocks-editor-files", "tb_editor_files_text");
sym_fn(&a.scroll, "tinyblocks-editor-files", "tb_editor_files_scroll");
bool ok = a.open && a.close && a.view && a.click && a.key && a.text && a.scroll;
if (ok) tb_ed_files = a;
return ok;
}
/* -- carga --- */
#ifdef TB_ED_STATIC_MODULES
bool tb_ed_modules_load(Ed* ed) {
tb_ed_w = (TbEdWidgetsApi){
.col_mul = tb_ew_col_mul, .fill_rect = tb_ew_fill_rect, .fill_rrect = tb_ew_fill_rrect,
.fill_circle = tb_ew_fill_circle, .stroke_rect = tb_ew_stroke_rect,
.stroke_circle = tb_ew_stroke_circle, .line = tb_ew_line, .text_c = tb_ew_text_c,
.text_width = tb_ew_text_width, .in_rect = tb_ew_in_rect,
.text_f = tb_ew_text_f, .icon = tb_ew_icon, .shadow = tb_ew_shadow,
.anim_now = tb_ew_anim_now, .ripple_down = tb_ew_ripple_down, .ripple_up = tb_ew_ripple_up,
.ripple = tb_ew_ripple };
tb_ed_img = (TbEdImageApi){
.view = tb_editor_image_view, .click = tb_editor_image_click, .drag = tb_editor_image_drag,
.release = tb_editor_image_release, .load_file = tb_editor_image_load_file,
.save = tb_editor_image_save, .ensure = tb_editor_image_ensure,
.sel_reset = tb_editor_image_sel_reset, .sel_commit = tb_editor_image_sel_commit,
.zoom = tb_editor_image_zoom };
tb_ed_files = (TbEdFilesApi){
.open = tb_editor_files_open, .close = tb_editor_files_close, .view = tb_editor_files_view,
.click = tb_editor_files_click, .key = tb_editor_files_key, .text = tb_editor_files_text,
.scroll = tb_editor_files_scroll };
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++;
}
ed->mods.loaded = true;
snprintf(ed->mods.info, sizeof(ed->mods.info), "Bundle do editor embutido no executável");
return true;
}
void tb_ed_modules_clear(void) {}
bool tb_ed_modules_check(Ed* ed) { (void)ed; return false; }
#else
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() && resolve_files();
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));
memset(&tb_ed_files, 0, sizeof(tb_ed_files));
}
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() && resolve_files();
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;
}
#endif /* TB_ED_STATIC_MODULES */