Added PluginContext

This commit is contained in:
2024-06-20 23:43:16 +03:00
parent 43a07debde
commit 19aee664c2
25 changed files with 172 additions and 83 deletions

View File

@@ -10,6 +10,10 @@ typedef struct plug_info_t {
char* license;
} plug_info_t;
typedef struct plug_ctx_t {
char* config_dir;
} plug_ctx_t;
#define PLUG_INFO(_name, _version, _license) \
static plug_info_t PLUG_INFO_VAR = { .name=(_name), .version=(_version), .license=(_license)}; \
void* plug_get_info() { \
@@ -22,7 +26,7 @@ void* plug_get_info() { \
#define PLUG_LICENSE(s) volatile char* PLUG_LICENSE = (s);
void plug_init(); // Loads when DIM initialises
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

View File

@@ -0,0 +1,25 @@
use std::{ffi::{c_char, CStr}, path::PathBuf};
#[repr(C)]
pub struct ContextRaw {
pub config_dir: *const c_char
}
#[derive(Debug, Clone)]
pub struct Context {
pub config_dir: PathBuf
}
impl Context {
pub fn new(cr: *const ContextRaw) -> Self {
let config_dir = unsafe {
let v = CStr::from_ptr((*cr).config_dir);
PathBuf::from(v.to_string_lossy().to_string())
};
Self {
config_dir
}
}
}

View File

@@ -4,14 +4,15 @@
mod magic;
mod c_buffer;
mod plugin_info;
mod context;
pub use c_buffer::*;
pub use plugin_info::*;
pub use anyhow::Result;
pub use context::*;
pub trait DimPlugin {
fn init(&mut self);
fn init(&mut self, ctx: Context);
fn pre_reload(&mut self);
fn post_reload(&mut self);
fn poll(&mut self, f: &mut CBuffer) -> Result<()>;
@@ -23,3 +24,4 @@ pub trait DimPlugin {

View File

@@ -15,10 +15,12 @@ macro_rules! plugin_info {
}
#[no_mangle]
unsafe extern "C" fn plug_init() {
unsafe extern "C" fn plug_init(ctx: *const $crate::ContextRaw) {
PLUG = std::alloc::alloc(std::alloc::Layout::new::<$typ>()) as *mut $typ;
*PLUG = Plug::new();
(&mut *PLUG).init();
let ctx = $crate::Context::new(ctx);
(&mut *PLUG).init(ctx);
}
#[no_mangle]