From 9277da7b29ca3dd15676cefe706d9aeb52524dee Mon Sep 17 00:00:00 2001 From: Benjamin Klum Date: Wed, 29 Nov 2023 01:06:32 +0100 Subject: [PATCH] Helgobox runtime API mechanism: Support multiple API functions --- api/src/runtime/mod.rs | 39 ++++++++++++++-------- extension/src/lib.rs | 4 +-- main/src/infrastructure/plugin/api_impl.rs | 4 +++ 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/api/src/runtime/mod.rs b/api/src/runtime/mod.rs index 9298dbb5e..77300226f 100644 --- a/api/src/runtime/mod.rs +++ b/api/src/runtime/mod.rs @@ -4,18 +4,22 @@ use std::ffi::{c_char, c_long, c_void, CStr}; use std::mem::transmute; macro_rules! api { - ($func_name:ident ($( $param_name:ident: $param_type:ty ),*) -> $ret_type:ty) => { + ($( $func_name:ident ($( $param_name:ident: $param_type:ty ),*) -> $ret_type:ty, )+) => { pub struct HelgoboxApiPointers { - $func_name: Option $ret_type>, + $( + $func_name: Option $ret_type> + ),+ } impl HelgoboxApiPointers { pub fn load(plugin_context: &PluginContext) -> Self { unsafe { Self { - $func_name: transmute(plugin_context.GetFunc( - concat!(stringify!($func_name), "\0").as_ptr() as *const c_char, - )), + $( + $func_name: transmute(plugin_context.GetFunc( + concat!(stringify!($func_name), "\0").as_ptr() as *const c_char, + )) + ),+ } } } @@ -32,26 +36,33 @@ macro_rules! api { } } - pub fn $func_name(&self, $( $param_name: $param_type ),*) -> $ret_type { - self.pointers.$func_name.unwrap()($( $param_name ),*) - } + $( + pub fn $func_name(&self, $( $param_name: $param_type ),*) -> $ret_type { + self.pointers.$func_name.unwrap()($( $param_name ),*) + } + )+ } pub trait HelgoboxApi { - extern "C" fn $func_name($( $param_name: $param_type ),*) -> $ret_type; + $( + extern "C" fn $func_name($( $param_name: $param_type ),*) -> $ret_type; + )+ } pub fn register_helgobox_api(mut register_api_fn: impl FnMut(&CStr, *mut c_void)) { unsafe { - register_api_fn( - CStr::from_ptr(concat!(stringify!($func_name), "\0").as_ptr() as *const c_char), - T::$func_name as *mut c_void, - ); + $( + register_api_fn( + CStr::from_ptr(concat!(stringify!($func_name), "\0").as_ptr() as *const c_char), + T::$func_name as *mut c_void, + ); + )+ } } }; } api![ - HB_FindFirstInstanceInProject(project: *const ReaProject) -> c_long + HB_FindFirstInstanceInProject(project: *const ReaProject) -> c_long, + HB_ShowOrHidePlaytime(instance_id: c_long) -> (), ]; diff --git a/extension/src/lib.rs b/extension/src/lib.rs index 0f0c806c0..b9d7139c3 100644 --- a/extension/src/lib.rs +++ b/extension/src/lib.rs @@ -60,8 +60,8 @@ impl HookCommand for MyHookCommand { .plugin_context(); let pointers = HelgoboxApiPointers::load(&plugin_context); let session = HelgoboxApiSession::new(pointers); - let res = session.HB_FindFirstInstanceInProject(null()); - println!("Executing my command: {res}!"); + session.HB_ShowOrHidePlaytime(45); + // println!("Executing my command: {res}!"); true } } diff --git a/main/src/infrastructure/plugin/api_impl.rs b/main/src/infrastructure/plugin/api_impl.rs index 094c71ec6..c011835c3 100644 --- a/main/src/infrastructure/plugin/api_impl.rs +++ b/main/src/infrastructure/plugin/api_impl.rs @@ -11,6 +11,10 @@ impl HelgoboxApi for HelgoboxApiImpl { extern "C" fn HB_FindFirstInstanceInProject(project: *const ReaProject) -> c_long { 42 } + + extern "C" fn HB_ShowOrHidePlaytime(instance_id: c_long) -> () { + Reaper::get().show_console_msg("Show or hide playtime"); + } } pub fn register_api() {