Adiciona tinyblocks-api, tinyblocks-libman e a tela "Escolha uma Extensão"

tinyblocks-api (gestor publico de modulos/libraries):
- registry de modulos (embutidos + .so), load/unload/reload via dlopen,
  hot reload por mtime e introspeccao de tipos de bloco.
- substitui a logica de hotlib duplicada no editor.

tinyblocks-libman (gestor/buscador de libraries):
- varre library.toml (libraries/ e ~/.tinyblocks/libraries), le metadados
  de forma RECURSIVA e resolve o caminho do .so/.a.
- busca por texto e filtro por categoria; consulta o estado de carga na api.

Editor: tela cheia "Escolha uma Extensão" (UI desenhada pelo editor, como no
Scratch): botao Bibliotecas / Ctrl+L, busca, chips de categoria que quebram
linha, grade de cartoes com faixa colorida e selo "carregada"; clicar carrega
o .so via api.

Respeito a interfaces publicas entre libraries:
- libman deixa de incluir o ast.h (interno) do tinyblocks-toml e passa a usar
  uma API publica e opaca: tb_toml_open/close/str/int/int_array.
- core ganha accessors publicos: tb_rt_type_count/at, tb_rt_library_count/at,
  tb_library_name/category/block_count; api e editor usam-nos em vez de ler
  campos de struct do core diretamente.

Testes: make test-libman (novo) + step no harness que abre o libman, busca,
carrega um .so e confirma a categoria na paleta. test-editor/toml/blueprint/
plugin seguem OK.
This commit is contained in:
mguinhos committed 2026-10-20 20:22:37 -03:00
1 parent f616503caa
commit dcc20985c5
20 files changed
+1143 -112

No files matched your search

+188
View File
@@ -0,0 +1,188 @@
/*
* 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 <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", ""));
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);
}
int tb_libman_scan_defaults(void) {
int n = 0;
n += tb_libman_scan("libraries", 3);
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(const char* query, const char* category,
int* out, int max) {
int n = 0;
bool allcat = !category || !category[0] || strcmp(category, "Tudo") == 0;
for (int i = 0; i < s_n && n < max; i++) {
const TbLibmanEntry* e = &s_e[i];
if (!allcat && strcmp(e->category, category) != 0) continue;
if (!contains_ci(e->name, query) &&
!contains_ci(e->category, query) &&
!contains_ci(e->description, query)) continue;
out[n++] = i;
}
return n;
}
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;
}