Skip to content

Instantly share code, notes, and snippets.

@saivert
Last active February 13, 2022 13:30
Show Gist options
  • Save saivert/05b4978e47fd9b01dc3907c000e54ea2 to your computer and use it in GitHub Desktop.
Save saivert/05b4978e47fd9b01dc3907c000e54ea2 to your computer and use it in GitHub Desktop.
Boilerplate rust plugin for deadbeef
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
pub mod db;
use core::ffi::c_void;
use db::db::*;
use std::ffi::CStr;
use std::os::raw::c_char;
use once_cell::sync::Lazy;
use std::sync::Mutex;
const PLUGIN_ID: &str = "rustplugin\0";
const PLUGIN_NAME: &str = "A rust plugin\0";
const PLUGIN_DESCR: &str = "An example of a plugin written in rust\0";
const PLUGIN_COPYRIGHT: &str = "Copyright 2021\0";
const PLUGIN_WEBSITE: &str = "https://saivert.com\0";
const PLUGIN_CONFIG: &str = "\0";
// The global pointer variable I have been looking for!
static mut DBFUNCTIONS: *const DB_functions_t = 0 as *const DB_functions_t;
#[derive(Default)]
struct MyPlugin {
a: i32,
s: String,
}
static PLUGINDATA: Lazy<Mutex<MyPlugin>> = Lazy::new(|| Mutex::new(Default::default()));
#[no_mangle]
pub unsafe extern "C" fn start() -> i32 {
println!("Rust Plugin started!");
let confdir: &CStr = CStr::from_ptr( (*DBFUNCTIONS).get_config_dir.unwrap()() );
println!("From rust: Deadbeef config dir is {}", confdir.to_str().unwrap());
let mut plugdata = PLUGINDATA.lock().unwrap();
plugdata.s = String::from("Cool");
plugdata.a = 2;
0
}
#[no_mangle]
pub unsafe extern "C" fn stop() -> i32 {
println!("Rust Plugin stopped!");
let plugdata = PLUGINDATA.lock().unwrap();
println!("Rust Plugin: Our data is {} and {}", plugdata.s, plugdata.a);
0
}
#[no_mangle]
pub unsafe extern "C" fn libsampleplugin_load(functions: *const DB_functions_t ) -> *mut c_void {
DBFUNCTIONS = functions;
let plugin_struct = DB_plugin_s {
type_: DB_PLUGIN_OUTPUT as i32,
api_vmajor: 1,
api_vminor: 0,
version_major: 1,
version_minor: 0,
flags: 0,
reserved1: 0,
reserved2: 0,
reserved3: 0,
id: PLUGIN_ID.as_ptr() as *const c_char,
name: CStr::from_bytes_with_nul_unchecked(PLUGIN_NAME.as_bytes()).as_ptr(),
descr: PLUGIN_DESCR.as_ptr() as *const c_char,
copyright: PLUGIN_COPYRIGHT.as_ptr() as *const c_char,
website: PLUGIN_WEBSITE.as_ptr() as *const c_char,
command: None,
start: Some(start),
stop: Some(stop),
connect: None,
disconnect: None,
exec_cmdline: None,
get_actions: None,
message: None,
configdialog: PLUGIN_CONFIG.as_ptr() as *const c_char,
};
let plugin_misc = DB_misc_t {
plugin: plugin_struct,
};
//let i: f32;
// match (*functions).streamer_get_playpos {
// Some(x) => i = x(),
// None => i = 0.
// }
let data = Box::new(plugin_misc);
Box::into_raw(data) as *mut c_void
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment