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:
1 parent
036162280d
commit
0143d3efd5
21 files changed
+3775
-557
No files matched your search
@@ -57,6 +57,28 @@ TB_API bool tb_api_unload(int idx);
|
||||
TB_API bool tb_api_reload(int idx);
|
||||
TB_API bool tb_api_is_loaded(const char* name);
|
||||
|
||||
/* -- Modulos de codigo (bundle de um app, ex: libraries privadas do editor)
|
||||
* Nao registram libraries/blocos: sao so codigo (.so) carregado por dlopen.
|
||||
* Usados para HOT RELOAD: recompile a parte que mudou e o app recarrega. --- */
|
||||
#define TB_API_MAX_CODE 32
|
||||
TB_API int tb_api_code_load(const char* name, const char* path); /* idx ou -1 */
|
||||
TB_API bool tb_api_code_unload(const char* name);
|
||||
TB_API void tb_api_code_unload_all(void);
|
||||
TB_API int tb_api_code_count(void);
|
||||
TB_API const char* tb_api_code_name(int i);
|
||||
TB_API const char* tb_api_code_path(int i);
|
||||
TB_API void* tb_api_code_handle(const char* name);
|
||||
TB_API void* tb_api_code_symbol(const char* name, const char* symbol);
|
||||
/* Recarrega TODOS os modulos de codigo na ordem de carga (unload inverso,
|
||||
* load direto) - resolve dependencias entre eles. Retorna quantos voltaram. */
|
||||
TB_API int tb_api_code_reload_all(void);
|
||||
/* true se algum modulo de codigo mudou no disco (usa watcher/mtime). */
|
||||
TB_API bool tb_api_code_changed(void);
|
||||
|
||||
/* -- Watcher de arquivos (inotify quando disponivel, senao mtime) --- */
|
||||
TB_API bool tb_api_watch(const char* path);
|
||||
TB_API int tb_api_poll_watch(void); /* eventos pendentes (0 = nada) */
|
||||
|
||||
/* Hot reload: true se algum .so mudou no disco desde o ultimo load. */
|
||||
TB_API bool tb_api_any_changed(void);
|
||||
/* Recarrega os .so que mudaram; retorna quantos. */
|
||||
|
||||
@@ -7,9 +7,11 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef __linux__
|
||||
# include <dlfcn.h>
|
||||
# include <sys/inotify.h>
|
||||
#endif
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
@@ -28,6 +30,230 @@ static time_t file_mtime(const char* p) {
|
||||
return (stat(p, &st) == 0) ? st.st_mtime : 0;
|
||||
}
|
||||
|
||||
static void set_err(const char* fmt, const char* a);
|
||||
static void path_dirname(const char* p, char* out, size_t cap);
|
||||
|
||||
static const char* path_basename(const char* p) {
|
||||
const char* s = strrchr(p, '/');
|
||||
return s ? s + 1 : p;
|
||||
}
|
||||
|
||||
/* -- Modulos de codigo (bundle) e watcher de arquivos ------------------- */
|
||||
|
||||
typedef struct {
|
||||
char name[64];
|
||||
char path[TB_API_PATH_LEN];
|
||||
void* handle;
|
||||
time_t mtime;
|
||||
bool dirty;
|
||||
} TbApiCode;
|
||||
|
||||
static TbApiCode s_code[TB_API_MAX_CODE];
|
||||
static int s_cn = 0;
|
||||
|
||||
static int code_find(const char* name) {
|
||||
if (!name) return -1;
|
||||
for (int i = 0; i < s_cn; i++) if (strcmp(s_code[i].name, name) == 0) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
# define TB_API_WATCH_MAX 64
|
||||
typedef struct {
|
||||
int wd;
|
||||
int idx; /* indice em s_code (-1 = so diretorio) */
|
||||
char base[128]; /* nome do arquivo vigiado */
|
||||
char dir[TB_API_PATH_LEN];
|
||||
} TbWatch;
|
||||
static TbWatch s_watch[TB_API_WATCH_MAX];
|
||||
static int s_wn = 0;
|
||||
static int s_inot = -1;
|
||||
|
||||
/* Vigia o DIRETORIO do arquivo: cobre tanto escrita no lugar quanto
|
||||
"make" (escreve num temp e renomeia por cima). */
|
||||
static int watch_add(const char* path) {
|
||||
if (s_inot < 0) return -1;
|
||||
char dir[TB_API_PATH_LEN];
|
||||
path_dirname(path, dir, sizeof(dir));
|
||||
if (s_wn > 0 && strcmp(s_watch[s_wn - 1].dir, dir) == 0) {
|
||||
/* mesmo diretorio: reaproveita o wd */
|
||||
snprintf(s_watch[s_wn - 1].base, sizeof(s_watch[s_wn-1].base), "%s", path_basename(path));
|
||||
return s_watch[s_wn - 1].wd;
|
||||
}
|
||||
if (s_wn >= TB_API_WATCH_MAX) return -1;
|
||||
int wd = inotify_add_watch(s_inot, dir,
|
||||
IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_ATTRIB);
|
||||
if (wd < 0) return -1;
|
||||
TbWatch* w = &s_watch[s_wn++];
|
||||
w->wd = wd;
|
||||
w->idx = -1;
|
||||
snprintf(w->dir, sizeof(w->dir), "%s", dir);
|
||||
snprintf(w->base, sizeof(w->base), "%s", path_basename(path));
|
||||
return wd;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool tb_api_watch(const char* path) {
|
||||
if (!path || !path[0]) return false;
|
||||
#ifdef __linux__
|
||||
if (s_inot < 0) {
|
||||
s_inot = inotify_init1(IN_NONBLOCK);
|
||||
if (s_inot < 0) return false;
|
||||
}
|
||||
return watch_add(path) >= 0;
|
||||
#else
|
||||
(void)path;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
int tb_api_poll_watch(void) {
|
||||
#ifdef __linux__
|
||||
if (s_inot < 0 || s_wn <= 0) return 0;
|
||||
char buf[4096] __attribute__((aligned(8)));
|
||||
ssize_t n;
|
||||
int changed = 0;
|
||||
while ((n = read(s_inot, buf, sizeof(buf))) > 0) {
|
||||
for (char* p = buf; p < buf + n; ) {
|
||||
struct inotify_event* ev = (struct inotify_event*)p;
|
||||
p += sizeof(*ev) + ev->len;
|
||||
if (!ev->len) continue;
|
||||
for (int i = 0; i < s_cn; i++) {
|
||||
if (strcmp(ev->name, path_basename(s_code[i].path)) != 0) continue;
|
||||
char d[TB_API_PATH_LEN];
|
||||
path_dirname(s_code[i].path, d, sizeof(d));
|
||||
for (int k = 0; k < s_wn; k++)
|
||||
if (s_watch[k].wd == ev->wd && strcmp(s_watch[k].dir, d) == 0) {
|
||||
s_code[i].dirty = true;
|
||||
changed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void path_dirname(const char* p, char* out, size_t cap) {
|
||||
snprintf(out, cap, "%s", p);
|
||||
char* sl = strrchr(out, '/');
|
||||
if (sl) *sl = 0; else snprintf(out, cap, ".");
|
||||
}
|
||||
|
||||
static bool code_changed_mtime(void) {
|
||||
bool any = false;
|
||||
for (int i = 0; i < s_cn; i++) {
|
||||
time_t mt = file_mtime(s_code[i].path);
|
||||
if (mt && mt != s_code[i].mtime) { s_code[i].dirty = true; any = true; }
|
||||
}
|
||||
return any;
|
||||
}
|
||||
|
||||
int tb_api_code_load(const char* name, const char* path) {
|
||||
if (!name || !name[0] || !path || !path[0]) {
|
||||
set_err("%s", "tb_api: nome/caminho invalido");
|
||||
return -1;
|
||||
}
|
||||
int ex = code_find(name);
|
||||
if (ex >= 0 && s_code[ex].handle && strcmp(s_code[ex].path, path) == 0)
|
||||
return ex;
|
||||
if (ex >= 0) tb_api_code_unload(name);
|
||||
if (s_cn >= TB_API_MAX_CODE) { set_err("%s", "tb_api: limite de modulos de codigo"); return -1; }
|
||||
#ifdef __linux__
|
||||
void* dl = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
|
||||
if (!dl) { set_err("dlopen: %s", dlerror()); return -1; }
|
||||
TbApiCode* c = &s_code[s_cn];
|
||||
memset(c, 0, sizeof(*c));
|
||||
snprintf(c->name, sizeof(c->name), "%s", name);
|
||||
snprintf(c->path, sizeof(c->path), "%s", path);
|
||||
c->handle = dl;
|
||||
c->mtime = file_mtime(path);
|
||||
c->dirty = false;
|
||||
/* watcher: registro por arquivo, apontando para o modulo */
|
||||
if (tb_api_watch(path)) {
|
||||
for (int k = s_wn - 1; k >= 0; k--) {
|
||||
if (strcmp(s_watch[k].base, path_basename(path)) == 0) { s_watch[k].idx = s_cn; break; }
|
||||
}
|
||||
}
|
||||
s_err[0] = 0;
|
||||
return s_cn++;
|
||||
#else
|
||||
(void)name; (void)path;
|
||||
set_err("%s", "tb_api: dlopen nao suportado nesta plataforma");
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool tb_api_code_unload(const char* name) {
|
||||
int i = code_find(name);
|
||||
if (i < 0) return false;
|
||||
#ifdef __linux__
|
||||
if (s_code[i].handle) dlclose(s_code[i].handle);
|
||||
#endif
|
||||
for (int k = i; k < s_cn - 1; k++) s_code[k] = s_code[k + 1];
|
||||
s_cn--;
|
||||
return true;
|
||||
}
|
||||
|
||||
void tb_api_code_unload_all(void) {
|
||||
for (int i = s_cn - 1; i >= 0; i--) {
|
||||
#ifdef __linux__
|
||||
if (s_code[i].handle) dlclose(s_code[i].handle);
|
||||
#endif
|
||||
s_code[i].handle = NULL;
|
||||
}
|
||||
s_cn = 0;
|
||||
}
|
||||
|
||||
int tb_api_code_count(void) { return s_cn; }
|
||||
const char* tb_api_code_name(int i) { return (i >= 0 && i < s_cn) ? s_code[i].name : NULL; }
|
||||
const char* tb_api_code_path(int i) { return (i >= 0 && i < s_cn) ? s_code[i].path : NULL; }
|
||||
|
||||
void* tb_api_code_handle(const char* name) {
|
||||
int i = code_find(name);
|
||||
return i >= 0 ? s_code[i].handle : NULL;
|
||||
}
|
||||
|
||||
void* tb_api_code_symbol(const char* name, const char* symbol) {
|
||||
void* h = tb_api_code_handle(name);
|
||||
#ifdef __linux__
|
||||
if (!h || !symbol) return NULL;
|
||||
return dlsym(h, symbol);
|
||||
#else
|
||||
(void)h; (void)symbol;
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
int tb_api_code_reload_all(void) {
|
||||
char names[TB_API_MAX_CODE][64];
|
||||
char paths[TB_API_MAX_CODE][TB_API_PATH_LEN];
|
||||
int n = s_cn;
|
||||
for (int i = 0; i < n; i++) {
|
||||
snprintf(names[i], sizeof(names[i]), "%s", s_code[i].name);
|
||||
snprintf(paths[i], sizeof(paths[i]), "%s", s_code[i].path);
|
||||
}
|
||||
tb_api_code_unload_all(); /* ordem inversa de dependencia */
|
||||
int ok = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
if (tb_api_code_load(names[i], paths[i]) >= 0) ok++;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool tb_api_code_changed(void) {
|
||||
bool any = false;
|
||||
#ifdef __linux__
|
||||
if (tb_api_poll_watch() > 0) any = true;
|
||||
#endif
|
||||
if (code_changed_mtime()) any = true;
|
||||
if (!any) {
|
||||
for (int i = 0; i < s_cn; i++) if (s_code[i].dirty) { any = true; break; }
|
||||
}
|
||||
return any;
|
||||
}
|
||||
|
||||
static void set_err(const char* fmt, const char* a) {
|
||||
snprintf(s_err, sizeof(s_err), fmt, a ? a : "");
|
||||
}
|
||||
@@ -53,6 +279,11 @@ bool tb_api_init(TinyBlocks* rt) {
|
||||
|
||||
void tb_api_shutdown(void) {
|
||||
/* O runtime e o dono dos blocos; aqui so esquecemos os modulos. */
|
||||
tb_api_code_unload_all();
|
||||
#ifdef __linux__
|
||||
if (s_inot >= 0) { close(s_inot); s_inot = -1; }
|
||||
s_wn = 0;
|
||||
#endif
|
||||
s_n = 0;
|
||||
s_rt = NULL;
|
||||
}
|
||||
@@ -170,6 +401,9 @@ bool tb_api_any_changed(void) {
|
||||
|
||||
int tb_api_poll_reload(void) {
|
||||
int n = 0;
|
||||
#ifdef __linux__
|
||||
tb_api_poll_watch();
|
||||
#endif
|
||||
for (int i = 0; i < s_n; i++) {
|
||||
TbApiModule* m = &s_mods[i];
|
||||
if (!m->path[0]) continue;
|
||||
|
||||
Reference in new issue
Block a user