diff options
| author | Teddy Wing | 2018-08-26 07:28:50 +0200 |
|---|---|---|
| committer | Teddy Wing | 2018-08-26 07:28:50 +0200 |
| commit | f09a074b3e96b960ee42dc484971e3fa192499d0 (patch) | |
| tree | fa2cbdfd19faa3724d37fc42ebcbdf1ceac52509 /src | |
| parent | 9b0e6e4207f5c44bef5d0b28ba036040845f82d8 (diff) | |
| download | dome-key-map-f09a074b3e96b960ee42dc484971e3fa192499d0.tar.bz2 | |
Make data types more C-like
Add a new `CKeyActionResult` that uses a `char *` instead of an
`Option<CString>`. Make our `c_run_key_action()` return this type
instead of `KeyActionResult`. Include some code to handle the
translation between the two types.
Names for now are just temporary so I can get something working. We'll
re-think proper names once I've tested this manually and have something
somewhat working.
Diffstat (limited to 'src')
| -rw-r--r-- | src/cocoa_bridge.rs | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index d98701f..f2d5f45 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -1,9 +1,10 @@ use std::ffi::CString; +use std::ptr; use std::slice; use cocoa::base::nil; use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary}; -use libc::size_t; +use libc::{c_char, size_t}; use {HeadphoneButton, MapGroup, MapKind}; @@ -46,19 +47,49 @@ pub struct KeyActionResult { pub kind: MapKind, } +#[repr(C)] +pub struct CKeyActionResult { + pub action: *const c_char, + pub kind: *const MapKind, +} + #[no_mangle] pub extern "C" fn c_run_key_action( trigger: *const HeadphoneButton, length: size_t, -) -> *const KeyActionResult { +) -> *const CKeyActionResult { 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 + let result = match run_key_action(trigger) { + Some(k) => { + match k.action { + Some(a) => { + CKeyActionResult { + action: a.as_ptr(), + kind: &k.kind, + } + }, + None => { + CKeyActionResult { + action: ptr::null(), + kind: &k.kind, + } + }, + } + }, + None => { + CKeyActionResult { + action: ptr::null(), + kind: ptr::null(), + } + } + }; + + &result as *const CKeyActionResult } #[no_mangle] |
