add reload

This commit is contained in:
Nishi 2025-12-28 17:29:00 +09:00
commit ef89c2dac9
Signed by: nishi
GPG key ID: 27EF69B208EB9343
11 changed files with 283 additions and 121 deletions

View file

@ -1,5 +1,14 @@
#include "mpanel.h"
#include <stb_ds.h>
typedef struct module {
char* key;
void* value;
MwWidget user;
} module_t;
module_t* modules = NULL;
static void read_config(xemil_t** cfg, const char* path) {
xemil_t* tmp;
@ -13,44 +22,115 @@ static void read_config(xemil_t** cfg, const char* path) {
}
}
static void init_module(xl_node_t* node) {
char* p = MDEStringConcatenate("lib", node->name);
char* p2 = MDEStringConcatenate(p, "Module.so");
char* s = MwDirectoryJoin(LIBDIR "/mpanel", p2);
void* lib;
char* id = NULL;
xl_attribute_t* a = node->first_attribute;
int ind;
while(a != NULL) {
if(strcmp(a->key, "ID") == 0 && a->value != NULL) {
id = a->value;
}
a = a->next;
}
if(id == NULL) return;
lib = dlopen(s, RTLD_LAZY | RTLD_LOCAL);
if(lib != NULL) {
if((ind = shgeti(modules, id)) == -1) {
MwWidget (*module)(MwWidget box, xl_node_t* node) = dlsym(lib, "module");
void* ret = NULL;
if(module != NULL) {
ret = module(box, node);
shput(modules, id, lib);
ind = shgeti(modules, id);
modules[ind].user = ret;
}
} else {
void (*module)(MwWidget box, MwWidget user, xl_node_t* node);
void* ret = NULL;
dlclose(lib);
lib = modules[ind].value;
module = dlsym(lib, "module_reload");
if(module != NULL) {
module(box, modules[ind].user, node);
}
}
}
free(s);
free(p2);
free(p);
}
static void module_scan(xl_node_t* node) {
xl_node_t* n = node->first_child;
while(n != NULL) {
if(n->type == XL_NODE_NODE) {
char* p = MDEStringConcatenate("lib", n->name);
char* p2 = MDEStringConcatenate(p, "Module.so");
char* s = MwDirectoryJoin(LIBDIR "/mpanel", p2);
void* lib = dlopen(s, RTLD_LAZY | RTLD_LOCAL);
if(lib != NULL) {
void (*module)(MwWidget box, xl_node_t* node) = dlsym(lib, "module");
if(module != NULL) module(box, n);
}
free(s);
free(p2);
free(p);
init_module(n);
}
n = n->next;
}
}
void load_modules(void) {
xemil_t* cfg = NULL;
char* conf = MDEDirectoryConfigPath();
char* dir = MwDirectoryJoin(conf, "mpanel");
char* path = MwDirectoryJoin(dir, "mpanelrc");
static void prepare_config(xemil_t** cfg) {
char* conf = MDEDirectoryConfigPath();
char* dir = MwDirectoryJoin(conf, "mpanel");
char* path = MwDirectoryJoin(dir, "mpanelrc");
free(dir);
free(conf);
read_config(&cfg, SYSCONFDIR "/mpanel/mpanelrc");
read_config(&cfg, path);
read_config(cfg, SYSCONFDIR "/mpanel/mpanelrc");
read_config(cfg, path);
free(path);
}
void load_modules(void) {
xemil_t* cfg = NULL;
prepare_config(&cfg);
sh_new_strdup(modules);
if(cfg != NULL && cfg->root != NULL) {
xl_node_t* n = cfg->root->first_child;
if(strcmp(cfg->root->name, "Panel") != 0) {
fprintf(stderr, "MPanel config error: Root element has to be Panel\n");
return;
}
while(n != NULL) {
if(n->type == XL_NODE_NODE && strcmp(n->name, "Module") == 0) {
module_scan(n);
}
n = n->next;
}
xl_close(cfg);
}
}
void reload_modules(void) {
xemil_t* cfg = NULL;
prepare_config(&cfg);
if(cfg != NULL && cfg->root != NULL) {
xl_node_t* n = cfg->root->first_child;