aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2018-10-27 17:32:52 +0200
committerTeddy Wing2018-10-27 17:32:52 +0200
commit0ffb9b4e26d4539ac8e3c4755e20237b72c6a6a9 (patch)
tree30c3adb09cd808b979992633cc3d519919628bfd /src
parent45a1bc469a312c9cdc875ee8fd5ed6c25a217d40 (diff)
downloaddome-key-map-0ffb9b4e26d4539ac8e3c4755e20237b72c6a6a9.tar.bz2
Move non-FFI functions from `ffi.rs` to `map.rs`
Keep FFI functions separate from the rest.
Diffstat (limited to 'src')
-rw-r--r--src/ffi.rs83
-rw-r--r--src/lib.rs1
-rw-r--r--src/map.rs77
3 files changed, 83 insertions, 78 deletions
diff --git a/src/ffi.rs b/src/ffi.rs
index f3175b7..91a48c3 100644
--- a/src/ffi.rs
+++ b/src/ffi.rs
@@ -1,7 +1,5 @@
-use std::env;
-use std::ffi::{CStr, CString, OsString};
+use std::ffi::{CStr, CString};
use std::fs;
-use std::process::Command;
use std::ptr;
use std::slice;
@@ -9,8 +7,9 @@ use libc::{c_char, size_t};
use stderrlog;
use xdg;
-use {Action, HeadphoneButton, MapAction, MapGroup, MapKind};
+use {HeadphoneButton, MapGroup};
use config::{self, Config};
+use map::run_key_action_for_mode;
use trial;
#[repr(C)]
@@ -29,8 +28,8 @@ pub enum ActionKind {
#[derive(Default)]
pub struct State {
- in_mode: Option<Vec<HeadphoneButton>>,
- map_group: Option<MapGroup>,
+ pub in_mode: Option<Vec<HeadphoneButton>>,
+ pub map_group: Option<MapGroup>,
}
#[no_mangle]
@@ -113,78 +112,6 @@ pub extern "C" fn dome_key_run_key_action(
run_key_action_for_mode(&mut state, trigger);
}
-// TODO: un-extern
-#[no_mangle]
-pub extern "C" fn run_key_action_for_mode<'a>(
- state: &mut State,
- trigger: &'a [HeadphoneButton],
-) {
- match state.map_group {
- Some(ref map_group) => {
- let map = map_group.maps.get(trigger);
- let mode = map_group.modes.get(trigger);
-
- if let Some(in_mode) = state.in_mode.clone() {
- if let Some(mode) = map_group.modes.get(&in_mode) {
- // Deactivate mode by pressing current mode trigger
- if &in_mode[..] == trigger {
- state.in_mode = None;
-
- return;
- }
-
- if let Some(map) = mode.get(trigger) {
- run_action(&map);
- }
- }
- }
-
- // TODO: make sure this doesn't run when in_mode
- if state.in_mode.is_none() {
- if let Some(map) = map {
- run_action(&map);
- }
- }
-
- if mode.is_some() {
- state.in_mode = Some(trigger.to_vec());
- }
- },
- None => (),
- }
-}
-
-fn run_action(map_action: &MapAction) {
- match map_action.kind {
- MapKind::Map => {
- if let Action::Map(action) = &map_action.action {
- for key in action {
- key.tap()
- }
- }
- },
- MapKind::Command => {
- if let Action::String(action) = &map_action.action {
- let shell = match env::var_os("SHELL") {
- Some(s) => s,
- None => OsString::from("/bin/sh"),
- };
-
- match Command::new(shell)
- .arg("-c")
- .arg(action)
- .spawn() {
- Ok(_) => (),
- Err(e) => error!(
- "Command failed to start: `{}'",
- e
- ),
- }
- }
- },
- }
-}
-
#[no_mangle]
pub extern "C" fn dome_key_parse_args(
args: *const *const c_char,
diff --git a/src/lib.rs b/src/lib.rs
index 674758f..0e96ff0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -36,6 +36,7 @@ mod config;
mod errors;
mod ffi;
mod key_code;
+mod map;
mod parser;
mod trial;
diff --git a/src/map.rs b/src/map.rs
new file mode 100644
index 0000000..0591db9
--- /dev/null
+++ b/src/map.rs
@@ -0,0 +1,77 @@
+use std::env;
+use std::ffi::OsString;
+use std::process::Command;
+
+use {Action, HeadphoneButton, MapAction, MapKind};
+use ffi::State;
+
+pub fn run_key_action_for_mode<'a>(
+ state: &mut State,
+ trigger: &'a [HeadphoneButton],
+) {
+ match state.map_group {
+ Some(ref map_group) => {
+ let map = map_group.maps.get(trigger);
+ let mode = map_group.modes.get(trigger);
+
+ if let Some(in_mode) = state.in_mode.clone() {
+ if let Some(mode) = map_group.modes.get(&in_mode) {
+ // Deactivate mode by pressing current mode trigger
+ if &in_mode[..] == trigger {
+ state.in_mode = None;
+
+ return;
+ }
+
+ if let Some(map) = mode.get(trigger) {
+ run_action(&map);
+ }
+ }
+ }
+
+ // TODO: make sure this doesn't run when in_mode
+ if state.in_mode.is_none() {
+ if let Some(map) = map {
+ run_action(&map);
+ }
+ }
+
+ if mode.is_some() {
+ state.in_mode = Some(trigger.to_vec());
+ }
+ },
+ None => (),
+ }
+}
+
+fn run_action(map_action: &MapAction) {
+ match map_action.kind {
+ MapKind::Map => {
+ if let Action::Map(action) = &map_action.action {
+ for key in action {
+ key.tap()
+ }
+ }
+ },
+ MapKind::Command => {
+ if let Action::String(action) = &map_action.action {
+ let shell = match env::var_os("SHELL") {
+ Some(s) => s,
+ None => OsString::from("/bin/sh"),
+ };
+
+ match Command::new(shell)
+ .arg("-c")
+ .arg(action)
+ .spawn() {
+ Ok(_) => (),
+ Err(e) => error!(
+ "Command failed to start: `{}'",
+ e
+ ),
+ }
+ }
+ },
+ }
+}
+