From 9b0e6e4207f5c44bef5d0b28ba036040845f82d8 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 26 Aug 2018 01:32:43 +0200 Subject: Get `run_key_action` to export correctly to C Add a new wrapper function for `run_key_action` that uses C appropriate inputs & outputs and calls into our Rusty `run_key_action`. This new function now correctly gets a header generated for it by 'cbindgen'. Immense thanks to Jake Goulding on the Rust FFI Omnibus for showing me how to pass a slice argument from C: http://jakegoulding.com/rust-ffi-omnibus/slice_arguments/ In order to pass the slice from C, we need to pass a C array and its length to the function. Cool. --- src/cocoa_bridge.rs | 17 +++++++++++++++++ src/lib.rs | 1 + 2 files changed, 18 insertions(+) (limited to 'src') diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index d4749f0..d98701f 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -1,7 +1,9 @@ use std::ffi::CString; +use std::slice; use cocoa::base::nil; use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary}; +use libc::size_t; use {HeadphoneButton, MapGroup, MapKind}; @@ -44,6 +46,21 @@ pub struct KeyActionResult { pub kind: MapKind, } +#[no_mangle] +pub extern "C" fn c_run_key_action( + trigger: *const HeadphoneButton, + length: size_t, +) -> *const KeyActionResult { + let trigger = unsafe { + assert!(!trigger.is_null()); + + slice::from_raw_parts(trigger, length as usize) + }; + + let result = run_key_action(trigger).unwrap(); + &result as *const KeyActionResult +} + #[no_mangle] pub extern "C" fn run_key_action( trigger: &[HeadphoneButton] diff --git a/src/lib.rs b/src/lib.rs index db113dc..cf7dd1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ extern crate cocoa; #[macro_use] extern crate combine; +extern crate libc; mod cocoa_bridge; mod parser; -- cgit v1.2.3