Files
tinyblocks/libraries/tinyblocks-libman/src/tb_libman.c
T
mguinhos 0143d3efd5 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.
2026-10-20 23:31:42 -03:00

223 lines
7.1 KiB
C

/*
* TinyBlocks - tb_libman.c
* Gestor/buscador de libraries: varre library.toml, le metadados com
* tinyblocks-toml e consulta o estado de carga em tinyblocks-api.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_libman.h"
#include "tb_api.h"
#include "tb_toml.h" /* interface publica do parser TOML */
static TbLibmanEntry s_e[TBLM_MAX_ENTRIES];
static int s_n = 0;
static char s_err[400];
static TinyBlocks* s_rt = NULL;
static void copy_str(char* dst, size_t cap, const char* s) {
snprintf(dst, cap, "%s", s ? s : "");
}
static void resolve(char* dst, size_t cap, const char* dir, const char* rel) {
if (!rel || !rel[0]) { dst[0] = 0; return; }
if (rel[0] == '/') snprintf(dst, cap, "%s", rel);
else snprintf(dst, cap, "%s/%s", dir, rel);
}
static bool already(const char* tomlpath) {
for (int i = 0; i < s_n; i++)
if (strcmp(s_e[i].toml, tomlpath) == 0) return true;
return false;
}
static void entry_from_toml(const char* tomlpath, const char* dir) {
TbTomlDoc* doc = tb_toml_open(tomlpath);
if (!doc) {
snprintf(s_err, sizeof(s_err), "toml: falha ao abrir %s", tomlpath);
return;
}
TbLibmanEntry* e = &s_e[s_n];
memset(e, 0, sizeof(*e));
copy_str(e->name, sizeof(e->name), tb_toml_str(doc, "library.name", ""));
if (!e->name[0]) { tb_toml_close(doc); return; } /* sem nome: ignora */
copy_str(e->version, sizeof(e->version), tb_toml_str(doc, "library.version", ""));
copy_str(e->category, sizeof(e->category), tb_toml_str(doc, "library.category", ""));
copy_str(e->description, sizeof(e->description),
tb_toml_str(doc, "library.description", ""));
copy_str(e->depends, sizeof(e->depends), tb_toml_str(doc, "build.depends", ""));
copy_str(e->bundle, sizeof(e->bundle), tb_toml_str(doc, "library.bundle", ""));
e->private_lib = tb_toml_int(doc, "library.private", 0) != 0 ||
strcmp(tb_toml_str(doc, "library.private", "0"), "true") == 0;
int col[4];
tb_toml_int_array(doc, "library.color", col, 4, 255);
for (int i = 0; i < 4; i++) e->color[i] = (unsigned char)col[i];
tb_toml_int_array(doc, "library.dark", col, 4, 255);
for (int i = 0; i < 4; i++) e->dark[i] = (unsigned char)col[i];
copy_str(e->toml, sizeof(e->toml), tomlpath);
copy_str(e->dir, sizeof(e->dir), dir);
resolve(e->shared, sizeof(e->shared), dir, tb_toml_str(doc, "build.shared", ""));
resolve(e->staticlib, sizeof(e->staticlib), dir, tb_toml_str(doc, "build.static", ""));
tb_toml_close(doc);
s_n++;
}
static int scan_dir(const char* root, int depth) {
DIR* d = opendir(root);
if (!d) return 0;
int added = 0;
struct dirent* de;
while ((de = readdir(d))) {
if (de->d_name[0] == '.') continue;
if (strcmp(de->d_name, "build") == 0) continue;
char path[TBLM_PATH_LEN];
snprintf(path, sizeof(path), "%s/%s", root, de->d_name);
struct stat st;
if (stat(path, &st) != 0) continue;
if (S_ISDIR(st.st_mode)) {
if (depth > 0) added += scan_dir(path, depth - 1);
} else if (strcmp(de->d_name, "library.toml") == 0) {
if (s_n < TBLM_MAX_ENTRIES && !already(path)) {
int before = s_n;
entry_from_toml(path, root);
if (s_n > before) added++;
}
}
}
closedir(d);
return added;
}
bool tb_libman_init(TinyBlocks* rt) {
s_rt = rt;
s_n = 0;
s_err[0] = 0;
if (!tb_api_init(rt)) return false;
tb_api_adopt_all(); /* conta as libraries embutidas ja registradas */
return true;
}
void tb_libman_shutdown(void) {
s_n = 0;
tb_api_shutdown();
}
const char* tb_libman_last_error(void) { return s_err; }
void tb_libman_clear(void) { s_n = 0; }
int tb_libman_count(void) { return s_n; }
const TbLibmanEntry* tb_libman_entry(int i) {
return (i >= 0 && i < s_n) ? &s_e[i] : NULL;
}
int tb_libman_scan(const char* root, int max_depth) {
if (!root || !root[0]) return 0;
return scan_dir(root, max_depth);
}
/* Varre libraries ao lado do executavel (ex.: AppImage em
* usr/bin/tinyblocks -> usr/share/tinyblocks/libraries). */
static int scan_exe_roots(void) {
char exe[TBLM_PATH_LEN];
ssize_t k = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
if (k <= 0) return 0;
exe[k] = 0;
char* slash = strrchr(exe, '/');
if (!slash) return 0;
*slash = 0; /* diretorio do executavel */
static const char* rels[] = {
"/../share/tinyblocks/libraries",
"/../lib/tinyblocks/libraries",
};
int n = 0;
for (int i = 0; i < 2; i++) {
char buf[TBLM_PATH_LEN];
snprintf(buf, sizeof(buf), "%s%s", exe, rels[i]);
n += tb_libman_scan(buf, 3);
}
return n;
}
int tb_libman_scan_defaults(void) {
int n = 0;
n += tb_libman_scan("libraries", 3);
n += scan_exe_roots();
const char* home = getenv("HOME");
if (home && home[0]) {
char buf[TBLM_PATH_LEN];
snprintf(buf, sizeof(buf), "%s/.tinyblocks/libraries", home);
n += tb_libman_scan(buf, 3);
}
return n;
}
bool tb_libman_loaded(int i) {
if (i < 0 || i >= s_n) return false;
return tb_api_is_loaded(s_e[i].name);
}
int tb_libman_install(int i) {
if (i < 0 || i >= s_n) return -1;
int r = tb_api_load(s_e[i].shared);
snprintf(s_err, sizeof(s_err), "%s", r < 0 ? tb_api_last_error() : "");
return r;
}
static bool contains_ci(const char* hay, const char* needle) {
if (!needle || !needle[0]) return true;
if (!hay) return false;
for (; *hay; hay++) {
const char* h = hay;
const char* n = needle;
while (*h && *n && tolower((unsigned char)*h) == tolower((unsigned char)*n)) { h++; n++; }
if (!*n) return true;
}
return false;
}
int tb_libman_search_ex(const char* query, const char* category,
bool include_private, int* out, int max) {
bool allcat = !category || !category[0] || strcmp(category, "Tudo") == 0;
int n = 0;
for (int i = 0; i < s_n && n < max; i++) {
const TbLibmanEntry* e = &s_e[i];
if (e->private_lib && !include_private) continue;
if (!allcat && strcmp(e->category, category) != 0) continue;
if (query && query[0] &&
!contains_ci(e->name, query) &&
!contains_ci(e->category, query) &&
!contains_ci(e->description, query)) continue;
out[n++] = i;
}
return n;
}
int tb_libman_search(const char* query, const char* category, int* out, int max) {
return tb_libman_search_ex(query, category, false, out, max);
}
int tb_libman_categories(char out[][48], int max) {
int n = 0;
for (int i = 0; i < s_n && n < max; i++) {
const char* c = s_e[i].category;
if (!c[0]) continue;
bool dup = false;
for (int j = 0; j < n; j++) if (strcmp(out[j], c) == 0) { dup = true; break; }
if (!dup) snprintf(out[n++], 48, "%s", c);
}
return n;
}