aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-10-05 19:16:23 +0200
committerTeddy Wing2018-10-05 19:16:23 +0200
commit961b3b0b2d33a2c632fbc093b61e2c2d4dc07f70 (patch)
tree8c9cde38a296b6509780b1200472490465df485f
parent42fa76b3472c2dae8fd6db6649058e54e9f67003 (diff)
downloaddome-key-map-961b3b0b2d33a2c632fbc093b61e2c2d4dc07f70.tar.bz2
Add an FFI bridge to the `parse_args()` function
This will allow us to parse command line arguments starting from a C argv array (hopefully).
-rw-r--r--dome_key_map.h6
-rw-r--r--src/cocoa_bridge.rs31
2 files changed, 37 insertions, 0 deletions
diff --git a/dome_key_map.h b/dome_key_map.h
index e3dcf60..39ba298 100644
--- a/dome_key_map.h
+++ b/dome_key_map.h
@@ -17,6 +17,8 @@ typedef enum {
MapKind_Command,
} MapKind;
+typedef struct Config Config;
+
typedef struct State State;
typedef struct {
@@ -26,8 +28,12 @@ typedef struct {
void c_run_key_action(State *state, Trigger trigger, const Trigger *mode);
+void config_free(Config *ptr);
+
void logger_init(void);
+const Config *parse_args(const char *args, size_t length);
+
void state_free(State *ptr);
void state_load_map_group(State *ptr);
diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs
index 1b07ed0..f572c37 100644
--- a/src/cocoa_bridge.rs
+++ b/src/cocoa_bridge.rs
@@ -14,6 +14,7 @@ use stderrlog;
use xdg;
use {Action, HeadphoneButton, MapAction, MapGroup, MapKind};
+use config::{self, Config};
#[repr(C)]
struct renameMeMapGroup {
@@ -297,6 +298,36 @@ fn run_action(map_action: &MapAction) {
// fn run_command(command: Action) -> Result {
// }
+#[no_mangle]
+pub extern "C" fn parse_args(
+ args: *const c_char,
+ length: size_t,
+) -> *const Config {
+ let args = unsafe {
+ assert!(!args.is_null());
+
+ let args = slice::from_raw_parts(args, length as usize);
+
+ args
+ .iter()
+ .map(|s|
+ CStr::from_ptr(s)
+ .to_string_lossy()
+ .into_owned())
+ .collect::<Vec<String>>()
+ };
+
+ let config = config::parse_args(&args);
+
+ Box::into_raw(Box::new(config)) as *const Config
+}
+
+#[no_mangle]
+pub extern "C" fn config_free(ptr: *mut Config) {
+ if ptr.is_null() { return }
+ unsafe { Box::from_raw(ptr); }
+}
+
mod tests {
use super::*;