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

+31
View File
@@ -0,0 +1,31 @@
# Makefile da library libman (gestor/buscador de libraries)
# Constroi build/libtinyblocks-libman.a e build/libtinyblocks-libman.so
CC := gcc
BUILD := build
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude \
-I../tinyblocks-toml/include -I../tinyblocks-api/include
OBJ := $(BUILD)/tb_libman.o
LIB_A := $(BUILD)/libtinyblocks-libman.a
LIB_SO := $(BUILD)/libtinyblocks-libman.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_libman.c include/tb_libman.h \
../tinyblocks-toml/include/tb_toml.h ../tinyblocks-api/include/tb_api.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ)
$(CC) -shared -o $@ $^
clean:
$(RM) -r $(BUILD)
@@ -0,0 +1,73 @@
/*
* TinyBlocks - tb_libman.h
* Library tinyblocks-libman: gestor/buscador de libraries.
*
* Varre diretorios em busca de arquivos "library.toml", le os metadados
* (usando tinyblocks-toml) e resolve os caminhos dos .so/.a. Sabe tambem
* se uma library ja esta carregada, consultando tinyblocks-api.
*
* NAO desenha UI: o editor e quem desenha a tela de escolha de extensao.
*/
#ifndef TINYBLOCKS_LIBMAN_H
#define TINYBLOCKS_LIBMAN_H
#include <stdbool.h>
#include <tinyblocks/tinyblocks.h>
#ifdef __cplusplus
extern "C" {
#endif
#define TBLM_MAX_ENTRIES 128
#define TBLM_NAME_LEN 64
#define TBLM_DESC_LEN 200
#define TBLM_PATH_LEN 512
typedef struct TbLibmanEntry {
char name[TBLM_NAME_LEN];
char version[24];
char category[48];
char description[TBLM_DESC_LEN];
unsigned char color[4];
unsigned char dark[4];
char toml[TBLM_PATH_LEN]; /* caminho do library.toml */
char dir[TBLM_PATH_LEN]; /* diretorio da library */
char shared[TBLM_PATH_LEN]; /* caminho do .so (pode nao existir) */
char staticlib[TBLM_PATH_LEN]; /* caminho do .a */
char depends[160];
} TbLibmanEntry;
/* -- Ciclo de vida --- */
TB_API bool tb_libman_init(TinyBlocks* rt); /* liga ao tinyblocks-api */
TB_API void tb_libman_shutdown(void);
TB_API const char* tb_libman_last_error(void);
/* -- Varredura --- */
TB_API void tb_libman_clear(void);
/* Varre 'root' recursivamente (ate max_depth niveis) procurando library.toml. */
TB_API int tb_libman_scan(const char* root, int max_depth);
/* Varre os diretorios padrao: "libraries", "./libraries" e
* "$HOME/.tinyblocks/libraries". */
TB_API int tb_libman_scan_defaults(void);
TB_API int tb_libman_count(void);
TB_API const TbLibmanEntry* tb_libman_entry(int i);
/* -- Estado / instalacao (via tinyblocks-api) --- */
TB_API bool tb_libman_loaded(int i);
TB_API int tb_libman_install(int i); /* tb_api_load(entry->shared) */
/* -- Filtros --- */
/* Preenche out[] com os indices cujo nome/categoria/descricao contem 'query'
* (case-insensitive) e cuja categoria casa com 'category' (NULL ou "" = todas).
* Retorna quantos indices foram escritos (ate 'max'). */
TB_API int tb_libman_search(const char* query, const char* category,
int* out, int max);
/* Categorias distintas encontradas (sem repeticao). Retorna n. */
TB_API int tb_libman_categories(char out[][48], int max);
#ifdef __cplusplus
}
#endif
#endif /* TINYBLOCKS_LIBMAN_H */
+15
View File
@@ -0,0 +1,15 @@
# Metadados da library libman (gestor/buscador de libraries).
[library]
name = "tinyblocks-libman"
version = "1.0.0"
category = "Bibliotecas"
description = "Procura e carrega libraries por library.toml"
color = [255, 87, 34, 255]
dark = [216, 67, 21, 255]
[build]
sources = ["src/tb_libman.c"]
includes = ["include"]
shared = "build/libtinyblocks-libman.so"
static = "build/libtinyblocks-libman.a"
depends = ["tinyblocks-toml", "tinyblocks-api"]
+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;
}