Skip to content

Commit

Permalink
Merge pull request #10 from bigmstone/issue-9/first-mapping
Browse files Browse the repository at this point in the history
Fix Loading only first Mapping
  • Loading branch information
bigmstone authored Jun 26, 2023
2 parents e83f479 + c205084 commit acd8942
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scriptkeys"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
description = "ScriptKeys allows you to easily build macros (in Lua) on every key press for the supported devices."
documentation = "https://github.com/bigmstone/scriptkeys/blob/main/README.md"
Expand Down
5 changes: 3 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ pub enum ConfigEvent {
Script,
}

#[derive(Deserialize, PartialEq)]
#[derive(Deserialize, PartialEq, Debug)]
pub struct Mapping {
pub key: u32,
pub script: String,
}

#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
pub struct Config {
pub device: Devices,
pub mappings: Vec<Mapping>,
Expand Down Expand Up @@ -157,3 +157,4 @@ async fn config_watcher(
}
}
}

3 changes: 2 additions & 1 deletion src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Event {
pub action: Action,
}

#[derive(Deserialize, PartialEq)]
#[derive(Deserialize, PartialEq, Debug)]
pub enum Devices {
XK68JS,
Dummy,
Expand Down Expand Up @@ -55,3 +55,4 @@ impl Device for Dummy {
}
}
}

3 changes: 3 additions & 0 deletions src/device/xkeys/xk68js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::collections::HashMap;
use {
anyhow::{Error, Result},
hidapi::{HidApi, HidDevice},
log::trace,
serde::Deserialize,
tokio::sync::mpsc::Sender,
};
Expand Down Expand Up @@ -153,6 +154,7 @@ impl Device for XK68JS {
let txc = tx.clone();
tokio::spawn(async move {
for event in events {
trace!("Sending event: {:?}", event);
txc.send(event).await.unwrap();
}
});
Expand Down Expand Up @@ -189,3 +191,4 @@ mod test {
}
}
}

9 changes: 8 additions & 1 deletion src/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,21 @@ impl Script {

pub fn load_mapping(&mut self, conf: &Config) -> Result<()> {
for mapping in &conf.mappings {
trace!("Loading mapping: {:?}", mapping);
if let Some(full_path) = find_script(&mapping.script) {
match self.load_script_mapping(&full_path, mapping) {
Ok(()) => break,
Ok(()) => continue,
Err(err) => {
if err.downcast_ref::<ScriptNotFound>().is_some() {
continue;
} else {
error!("Error loading mapping: {:?}, {}", mapping, err);
return Err(err);
}
}
};
} else {
error!("Script not found: {:?}", mapping);
return Err(Error::new(ScriptNotFound));
}
}
Expand All @@ -98,6 +101,7 @@ impl Script {
pub fn load_script_mapping(&mut self, path: &Path, mapping: &Mapping) -> Result<()> {
if path.exists() {
if let Ok(script) = fs::read_to_string(path) {
trace!("Loading script: {}", path.display());
self.lua.load(&script).exec()?;
let name = Path::new(&mapping.script).file_stem().unwrap();
self.script_map
Expand Down Expand Up @@ -138,6 +142,8 @@ pub async fn script_loop(script: Arc<Mutex<Script>>, mut rx: Receiver<Event>) {
Action::Release => format!("{}.Release", table_name),
};

trace!("Executing script: {}", method);

script.lua.load(&format!("{}()", method)).exec().unwrap();
}
}
Expand Down Expand Up @@ -272,3 +278,4 @@ async fn script_watcher(
}
}
}

0 comments on commit acd8942

Please sign in to comment.