diff options
| author | Teddy Wing | 2018-09-25 13:16:53 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2018-09-25 13:24:16 +0200 | 
| commit | 69343f6d6ae1524adeba78e31efa48dca78f0d2b (patch) | |
| tree | bb0c8ec28908eceaeecf0b93ff1c1678cb1dcacc | |
| parent | b0be366aa4e6eef93e4788e81c11cf481f90e556 (diff) | |
| download | dome-key-map-69343f6d6ae1524adeba78e31efa48dca78f0d2b.tar.bz2 | |
Add `state_load_map_group()` to get `MapGroup` from config file
Currently untested. This function is supposed to get a mapping file from
the XDG config directory, parse it, and load it into the state.
| -rw-r--r-- | dome_key_map.h | 2 | ||||
| -rw-r--r-- | src/cocoa_bridge.rs | 30 | ||||
| -rw-r--r-- | src/lib.rs | 1 | 
3 files changed, 33 insertions, 0 deletions
| diff --git a/dome_key_map.h b/dome_key_map.h index fc2e7fd..3b5aa04 100644 --- a/dome_key_map.h +++ b/dome_key_map.h @@ -40,4 +40,6 @@ const CKeyActionResult *c_run_key_action(State *state, Trigger trigger, const Tr  void state_free(State *ptr); +void state_load_map_group(State *ptr); +  State *state_new(void); diff --git a/src/cocoa_bridge.rs b/src/cocoa_bridge.rs index 6e1a41b..b941f7f 100644 --- a/src/cocoa_bridge.rs +++ b/src/cocoa_bridge.rs @@ -1,4 +1,5 @@  use std::ffi::{CStr, CString}; +use std::fs;  use std::mem;  use std::ptr;  use std::slice; @@ -7,6 +8,7 @@ use autopilot::key::type_string;  // use cocoa::base::nil;  // use cocoa::foundation::{NSArray, NSAutoreleasePool, NSDictionary};  use libc::{c_char, size_t}; +use xdg;  use {HeadphoneButton, MapGroup, MapKind}; @@ -96,6 +98,7 @@ pub struct CKeyActionResult {  #[derive(Default)]  pub struct State {      in_mode: Option<Vec<HeadphoneButton>>, +    map_group: Option<MapGroup>,  }  #[no_mangle] @@ -110,6 +113,33 @@ pub extern "C" fn state_free(ptr: *mut State) {  }  #[no_mangle] +pub extern "C" fn state_load_map_group(ptr: *mut State) { +    let xdg_dirs = xdg::BaseDirectories::with_prefix("dome-key").unwrap(); +    let mapping_file = xdg_dirs.find_config_file("mappings.dkmap") +        .expect( +            &format!( +                "No mapping file found at '{}{}'", +                xdg_dirs.get_config_home() +                    .to_str() +                    .expect("Config home path contains invalid unicode"), +                "/mappings.dkmap" +            ) +        ); + +    let state = unsafe { +        assert!(!ptr.is_null()); +        &mut *ptr +    }; + +    let dkmap = fs::read_to_string(mapping_file) +        .expect("Failed to read 'mappings.dkmap'"); +    state.map_group = Some( +        MapGroup::parse(&dkmap) +            .expect("Failed to parse 'mappings.dkmap'") +    ); +} + +#[no_mangle]  pub extern "C" fn c_run_key_action(      state: *mut State,      trigger: Trigger, @@ -4,6 +4,7 @@ extern crate autopilot;  #[macro_use]  extern crate combine;  extern crate libc; +extern crate xdg;  mod cocoa_bridge;  mod parser; | 
