Too lazy to make propper edit but i changed the

polling system to a message one so we can have
more ways we can interract with plugins
(reload/ reset/ on click events)
This commit is contained in:
2024-06-26 04:00:44 +03:00
parent 9f53240807
commit 026a68364b
33 changed files with 694 additions and 271 deletions

View File

@@ -4,32 +4,83 @@
#include <stdio.h>
#define BUF_SZ 2048
#ifdef _WIN32
#define PATH_SEP "\\"
#else
#define PATH_SEP "/"
#endif
typedef enum log_level_t {
ERROR=1,
WARN=2,
INFO=3,
DEBUG=4,
} log_level_t;
typedef struct plug_info_t {
char* name;
char* version;
char* license;
} plug_info_t;
typedef struct plug_funcs_ctx_t {
void (*log)(char* module, log_level_t level, char* s);
} plug_funcs_ctx_t;
typedef struct plug_ctx_t {
char* config_dir;
plug_funcs_ctx_t funcs;
char* buf;
} plug_ctx_t;
#define PLUG_INFO(_name, _version, _license) \
typedef struct plug_msg_t {
char* name;
char* value;
} plug_msg_t;
// Functions defs
// User defined funcs
int plug_init(plug_ctx_t*); // Loads when DIM initialises
void* plug_pre_reload(); // Return a pointer to save state
int plug_post_reload(void*); // returns the same pointer after reload
int plug_on_msg(plug_msg_t*); // Write the message to `buf` with max `size` characters
int plug_free(); // Free everything before being killed
// Lib funcs
void setup_ctx(plug_ctx_t* ctx);
void _log(char* module, log_level_t level, char* s);
// Macros
#define PLUG_INFO(_name, _version, _license) \
static plug_info_t PLUG_INFO_VAR = { .name=(_name), .version=(_version), .license=(_license)}; \
void* plug_get_info() { \
return &PLUG_INFO_VAR; \
void* plug_get_info() { \
return &PLUG_INFO_VAR; \
}
#define log(level, ...) do { \
char* buf = (char*)malloc(BUF_SZ*sizeof(char)); \
char* mod = (char*)malloc(BUF_SZ*sizeof(char)); \
snprintf(mod, BUF_SZ*sizeof(char), \
"(plug) %s/%s", PLUG_INFO_VAR.name, __FILE__); \
snprintf(buf, BUF_SZ*sizeof(char), __VA_ARGS__); \
_log(mod, level, buf); \
free(buf); \
free(mod); \
} while (0)
#define PLUG_NAME(s) volatile char* PLUG_NAME = (s);
#define PLUG_VERSION(s) volatile char* PLUG_VERSION = (s);
#define PLUG_LICENSE(s) volatile char* PLUG_LICENSE = (s);
#ifdef PLUG_IMPL
static plug_ctx_t CTX = {0};
void setup_ctx(plug_ctx_t* ctx) {
CTX = *ctx;
}
void plug_init(plug_ctx_t*); // Loads when DIM initialises
void* plug_pre_reload(); // Return a pointer to save state
void plug_post_reload(void*); // returns the same pointer after reload
void plug_poll(char*, size_t); // Write the message to `buf` with max `size` characters
void plug_free(); // Free everything before being killed
void _log(char* module, log_level_t level, char* s) {
(CTX.funcs.log)(module, level, s);
}
#endif
#endif