From 7e0b19d4d962087b8c9be85c4f6bcd217ced7b71 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 30 Oct 2018 00:15:38 +0100 Subject: If no 'mappings.dkmap' file is found, use default mappings If a mappings file is found but is empty, no mappings will be set. Hmm, that sounds wrong. We still want to keep the defaults even if only some button triggers are mapped. Making buttons do nothing is what `` is for, not this behaviour. Darn. --- src/ffi.rs | 15 +++++++++------ src/parser.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ffi.rs b/src/ffi.rs index 55b0998..4d71334 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -50,13 +50,13 @@ pub extern "C" fn dome_key_state_free(ptr: *mut State) { pub extern "C" fn dome_key_state_load_map_group(ptr: *mut State) { match xdg::BaseDirectories::with_prefix("dome-key") { Ok(xdg_dirs) => { + let state = unsafe { + assert!(!ptr.is_null()); + &mut *ptr + }; + match xdg_dirs.find_config_file("mappings.dkmap") { Some(mapping_file) => { - let state = unsafe { - assert!(!ptr.is_null()); - &mut *ptr - }; - let dkmap = fs::read_to_string(mapping_file) .expect("Failed to read 'mappings.dkmap'"); @@ -67,10 +67,13 @@ pub extern "C" fn dome_key_state_load_map_group(ptr: *mut State) { state.map_group = Some(map_group); }, None => { + state.map_group = Some(MapGroup::default()); + match xdg_dirs.get_config_home().to_str() { Some(config_home) => { error!( - "No mapping file found at '{}{}'", + "No mapping file found at '{}{}'. + Using default mappings.", config_home, "mappings.dkmap" ) diff --git a/src/parser.rs b/src/parser.rs index fe56fca..8314e0d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -203,6 +203,58 @@ impl MapGroup { } } +/// Default headphone button mappings: +/// +/// * Up → Volume up +/// * Middle → Play +/// * Down → Volume down +impl Default for MapGroup { + fn default() -> Self { + let mut default_maps: MapCollection = HashMap::new(); + default_maps.insert( + vec![HeadphoneButton::Up], + MapAction { + action: Action::Map( + vec![KeyboardKeyWithModifiers::new( + KeyboardKey::NXKey(key_code::NX_KEYTYPE_SOUND_UP), + vec![], + )] + ), + kind: MapKind::Map, + }, + ); + default_maps.insert( + vec![HeadphoneButton::Play], + MapAction { + action: Action::Map( + vec![KeyboardKeyWithModifiers::new( + KeyboardKey::NXKey(key_code::NX_KEYTYPE_PLAY), + vec![], + )] + ), + kind: MapKind::Map, + }, + ); + default_maps.insert( + vec![HeadphoneButton::Down], + MapAction { + action: Action::Map( + vec![KeyboardKeyWithModifiers::new( + KeyboardKey::NXKey(key_code::NX_KEYTYPE_SOUND_DOWN), + vec![], + )] + ), + kind: MapKind::Map, + }, + ); + + MapGroup { + maps: default_maps, + modes: HashMap::new(), + } + } +} + fn string_case_insensitive( s: &'static str -- cgit v1.2.3