From a6f5f275e781bfa443e69b74a6c776eb6d970ded Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 29 Sep 2018 01:19:45 +0200 Subject: Revert "Try to propagate KeyCodeConvertible from Action to everywhere" This reverts commit 16cd3895f7b111544927d71904aab912d9abbf59. See that commit message for details. --- dome_key_map.h | 10 +++---- src/cocoa_bridge.rs | 30 +++++++++------------ src/parser.rs | 78 +++++++++++++++++++---------------------------------- 3 files changed, 44 insertions(+), 74 deletions(-) diff --git a/dome_key_map.h b/dome_key_map.h index b8c4b79..dd61a38 100644 --- a/dome_key_map.h +++ b/dome_key_map.h @@ -23,7 +23,7 @@ typedef enum { MapKind_Command, } MapKind; -typedef struct State_K State_K; +typedef struct State State; typedef struct { const HeadphoneButton *buttons; @@ -36,12 +36,12 @@ typedef struct { const Trigger *in_mode; } CKeyActionResult; -const CKeyActionResult *c_run_key_action(State_K *state, Trigger trigger, const Trigger *mode); +const CKeyActionResult *c_run_key_action(State *state, Trigger trigger, const Trigger *mode); void logger_init(void); -void state_free(State_K *ptr); +void state_free(State *ptr); -void state_load_map_group(State_K *ptr); +void state_load_map_group(State *ptr); -State_K *state_new(void); +State *state_new(void); diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index a4a072f..8718e58 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -4,7 +4,7 @@ use std::mem; use std::ptr; use std::slice; -use autopilot::key::{KeyCodeConvertible, type_string}; +use autopilot::key::type_string; // use cocoa::base::nil; // use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary}; use libc::{c_char, size_t}; @@ -97,10 +97,9 @@ pub struct CKeyActionResult { } #[derive(Default)] -pub struct State -where K: Default { +pub struct State { in_mode: Option>, - map_group: Option>, + map_group: Option, } #[no_mangle] @@ -114,21 +113,18 @@ pub extern "C" fn logger_init() { } #[no_mangle] -pub extern "C" fn state_new() -> *mut State -where K: KeyCodeConvertible { +pub extern "C" fn state_new() -> *mut State { Box::into_raw(Box::new(State::default())) } #[no_mangle] -pub extern "C" fn state_free(ptr: *mut State) -where K: KeyCodeConvertible { +pub extern "C" fn state_free(ptr: *mut State) { if ptr.is_null() { return } unsafe { Box::from_raw(ptr); } } #[no_mangle] -pub extern "C" fn state_load_map_group(ptr: *mut State) -where K: KeyCodeConvertible { +pub extern "C" fn state_load_map_group(ptr: *mut State) { match xdg::BaseDirectories::with_prefix("dome-key") { Ok(xdg_dirs) => { match xdg_dirs.find_config_file("mappings.dkmap") { @@ -166,12 +162,11 @@ where K: KeyCodeConvertible { } #[no_mangle] -pub extern "C" fn c_run_key_action( - state: *mut State, +pub extern "C" fn c_run_key_action( + state: *mut State, trigger: Trigger, mode: *const Trigger, -) -> *const CKeyActionResult -where K: KeyCodeConvertible { +) -> *const CKeyActionResult { let trigger = unsafe { assert!(!trigger.buttons.is_null()); @@ -263,12 +258,11 @@ where K: KeyCodeConvertible { } #[no_mangle] -pub extern "C" fn run_key_action_for_mode<'a, K>( - state: &mut State, +pub extern "C" fn run_key_action_for_mode<'a>( + state: &mut State, trigger: &'a [HeadphoneButton], in_mode: Option<&[HeadphoneButton]> -) -> Option> -where K: KeyCodeConvertible { +) -> Option> { let sample_maps = "map k map j map works! diff --git a/src/parser.rs b/src/parser.rs index 7fcea71..57abbbb 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -22,28 +22,11 @@ pub enum HeadphoneButton { Down, } type Trigger = Vec; -// type Action = String; - -// enum Action<'a, T: 'a + KeyCodeConvertible> { -// String(String), -// Map(&'a [(T, &'a [Flag])]), -// Command(&'a [&'a str]), -// } - -// struct KeyboardKey { -// key: , -// flags: &[Flags], -// } - -// enum Action { -// String(String), -// Map(Vec<(T, Vec)>), -// Command(Vec), -// } -enum Action { - String(String), - Map(Vec<(K, Vec)>), - Command(Vec), +type Action = String; + +enum Action2<'a, T: 'a + KeyCodeConvertible> { + Map(&'a [(T, &'a [Flag])]), + Command(&'a [&'a str]), } #[repr(C)] @@ -54,42 +37,42 @@ pub enum MapKind { } #[derive(Debug, PartialEq)] -pub struct MapAction { - pub action: Action, +pub struct MapAction { + pub action: Action, pub kind: MapKind, } #[derive(Debug, PartialEq)] -struct Map { +struct Map { trigger: Trigger, - action: Action, + action: Action, kind: MapKind, } -type MapCollection = HashMap>; +type MapCollection = HashMap; #[derive(Debug, PartialEq)] -struct Mode { +struct Mode { trigger: Trigger, - maps: MapCollection, + maps: MapCollection, } #[derive(Debug, PartialEq)] -pub struct MapGroup { - pub maps: MapCollection, - pub modes: HashMap>, +pub struct MapGroup { + pub maps: MapCollection, + pub modes: HashMap, } #[derive(Debug, PartialEq)] -enum Definition { - Map(Map), - Mode(Mode), +enum Definition { + Map(Map), + Mode(Mode), } -impl MapGroup { +impl MapGroup { pub fn parse( mappings: &str - ) -> Result, CombineErrors> { + ) -> Result> { let input = State::new(mappings); map_group().easy_parse(input).map(|t| t.0) } @@ -134,14 +117,12 @@ where many1(headphone_button()) } -fn action() -> impl Parser> +fn action() -> impl Parser where I: Stream, I::Error: ParseError, - K: KeyCodeConvertible, { take_until(newline()) - .map(|action| Action::String(action)) } fn whitespace_separator() -> impl Parser @@ -152,11 +133,10 @@ where skip_many1(space().or(tab())) } -fn map() -> impl Parser> +fn map() -> impl Parser where I: Stream, I::Error: ParseError, - K: KeyCodeConvertible, { ( map_kind(), @@ -173,15 +153,14 @@ where ) } -fn map_collection() -> impl Parser> +fn map_collection() -> impl Parser where I: Stream, I::Error: ParseError, - K: KeyCodeConvertible, { ( blank(), - many::>, _>(map().skip(blank())), + many::, _>(map().skip(blank())), ).map(|(_, collection)| { let mut maps = HashMap::new(); @@ -199,11 +178,10 @@ where }) } -fn mode() -> impl Parser> +fn mode() -> impl Parser where I: Stream, I::Error: ParseError, - K: KeyCodeConvertible, { ( string("mode"), @@ -221,11 +199,10 @@ where ) } -fn definitions() -> impl Parser>> +fn definitions() -> impl Parser> where I: Stream, I::Error: ParseError, - K: KeyCodeConvertible, { ( blank(), @@ -238,11 +215,10 @@ where ).map(|(_, definitions)| definitions) } -fn map_group() -> impl Parser> +fn map_group() -> impl Parser where I: Stream, I::Error: ParseError, - K: KeyCodeConvertible, { definitions() .map(|definitions| { -- cgit v1.2.3