Nova library privada do bundle do editor (dlopen + hot reload) com um diálogo modal no lugar da barra de caminho: locais, trilha clicável, lista filtrada por extensão, campo de nome e confirmação ao substituir. - start_path() abre o explorador em todos os modos (projeto, exportar, imagem, código, .so); sem o módulo volta para a barra de caminho. - O switch do Enter virou path_commit(), usado pelos dois caminhos. - Estado em ed->fdlg para sobreviver à recarga do módulo. - Empacotado no AppImage; 6 cenários novos no harness headless. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
231 lines
9.2 KiB
C
231 lines
9.2 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.
|
|
*/
|
|
#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;
|
|
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");
|
|
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");
|
|
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 --------------------------------------------------------------- */
|
|
|
|
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;
|
|
}
|