diff options
| author | Teddy Wing | 2018-08-26 08:34:44 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2018-08-26 08:34:44 +0200 | 
| commit | 076af0f0145b7f924eb32be010311545d039850f (patch) | |
| tree | 40740fd0de1180a407bcea0137b0195defb4232a | |
| parent | f09a074b3e96b960ee42dc484971e3fa192499d0 (diff) | |
| download | dome-key-map-076af0f0145b7f924eb32be010311545d039850f.tar.bz2 | |
Link library with a test C program
Make a test `includer.c` program that includes the Rust library and
calls our `c_run_key_action()` to see if it actually works. Currently it
doesn't, we get `(null)` printed to stdout.
Add a Makefile with the build command for the C program.
cbindgen.toml:
Remove `KeyActionResult` from exported types, as the `Option` field it
contains caused `gcc` to complain.
cocoa_bridge.rs:
* Comment out all 'cocoa' crate related code as the 'cocoa' code was
  interfering with GCC compilation as a result of my not linking to
  Cocoa frameworks.
* Add a new test map definition that corresponds with the one we look
  for in `includer.c`.
parser.rs:
Add `#[repr(C)]` to `MapKind` because it needs to be exported in the
library and generated into our C header file.
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | cbindgen.toml | 2 | ||||
| -rw-r--r-- | dome_key_map.h | 12 | ||||
| -rw-r--r-- | includer.c | 12 | ||||
| -rw-r--r-- | src/cocoa_bridge.rs | 51 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/parser.rs | 1 | 
7 files changed, 47 insertions, 35 deletions
| diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1434afa --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +includer: +	gcc -o $@ includer.c target/debug/libdome_key_map.a diff --git a/cbindgen.toml b/cbindgen.toml index 22e0269..e2a124a 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -6,7 +6,7 @@ include_version = true  # include = ["dome-key-map"]  [export] -include = ["KeyActionResult", "HeadphoneButton"] +include = ["HeadphoneButton", "MapKind"]  # item_types = ["enums", "structs", "functions"]  item_types = ["constants", "globals", "enums", "structs", "unions", "typedefs", "opaque", "functions"] diff --git a/dome_key_map.h b/dome_key_map.h index 1206d1d..647c6e5 100644 --- a/dome_key_map.h +++ b/dome_key_map.h @@ -12,18 +12,14 @@ typedef enum {    Down,  } HeadphoneButton; -typedef struct MapKind MapKind; - -typedef struct Option_CString Option_CString; +typedef enum { +  Map, +  Command, +} MapKind;  typedef struct {    const char *action;    const MapKind *kind;  } CKeyActionResult; -typedef struct { -  Option_CString action; -  MapKind kind; -} KeyActionResult; -  const CKeyActionResult *c_run_key_action(const HeadphoneButton *trigger, size_t length); diff --git a/includer.c b/includer.c new file mode 100644 index 0000000..9a02d22 --- /dev/null +++ b/includer.c @@ -0,0 +1,12 @@ +#include <stdio.h> +#include "dome_key_map.h" + +#define SIZE 2 + +int main() { +	HeadphoneButton trigger[SIZE] = {Play, Down}; +	const CKeyActionResult *result = c_run_key_action(trigger, SIZE); +	printf("%s", result->action); + +	return 0; +} diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index f2d5f45..fe65533 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -2,8 +2,8 @@ use std::ffi::CString;  use std::ptr;  use std::slice; -use cocoa::base::nil; -use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary}; +// use cocoa::base::nil; +// use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary};  use libc::{c_char, size_t};  use {HeadphoneButton, MapGroup, MapKind}; @@ -12,28 +12,28 @@ use {HeadphoneButton, MapGroup, MapKind};  struct renameMeMapGroup {  } -pub extern "C" fn parse_mappings() { -    let sample_maps = "map <up> k -map <down> j"; - -    let map_group = MapGroup::parse(sample_maps).unwrap(); - -    unsafe { -        let _pool = NSAutoreleasePool::new(nil); - -        let maps = NSDictionary::init(nil).autorelease(); -        let modes = NSDictionary::init(nil).autorelease(); - -        for (trigger, action) in map_group.maps { -            // let t = NSArray::arrayWithObjects(nil, &trigger).autorelease(); - -            // maps. -        } - -        for (trigger, modes) in map_group.modes { -        } -    } -} +// pub extern "C" fn parse_mappings() { +//     let sample_maps = "map <up> k +// map <down> j"; +// +//     let map_group = MapGroup::parse(sample_maps).unwrap(); +// +//     unsafe { +//         let _pool = NSAutoreleasePool::new(nil); +// +//         let maps = NSDictionary::init(nil).autorelease(); +//         let modes = NSDictionary::init(nil).autorelease(); +// +//         for (trigger, action) in map_group.maps { +//             // let t = NSArray::arrayWithObjects(nil, &trigger).autorelease(); +// +//             // maps. +//         } +// +//         for (trigger, modes) in map_group.modes { +//         } +//     } +// }  // Different method:  // Call Rust function with trigger @@ -97,7 +97,8 @@ pub extern "C" fn run_key_action(      trigger: &[HeadphoneButton]  ) -> Option<KeyActionResult> {      let sample_maps = "map <up> k -map <down> j"; +map <down> j +map <play><down> works!";      // Figure out how to persist this without re-parsing      let map_group = MapGroup::parse(sample_maps).unwrap(); @@ -1,4 +1,4 @@ -extern crate cocoa; +// extern crate cocoa;  #[macro_use]  extern crate combine; diff --git a/src/parser.rs b/src/parser.rs index c6c8cce..1d9aa56 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -23,6 +23,7 @@ pub enum HeadphoneButton {  type Trigger = Vec<HeadphoneButton>;  type Action = String; +#[repr(C)]  #[derive(Debug, PartialEq)]  pub enum MapKind {      Map, | 
