-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplified the application by removing callback functions
The cost-benefit ratio of supporting callback functions was too low, leading to both code and API complexity. Therefore, callback functions have been removed to streamline the codebase. Changes: - Updated README.md to reflect the updated binding mechanism and simplified usage. - Removed callback function support from the main application logic. - Refactored `src/main.rs` to use `ScriptManager` instead of `LuaManager`. - Created a new `src/script_manager.rs` file to handle script management without callbacks. - Adjusted the key binding examples and roadmap in the documentation.
- Loading branch information
1 parent
0e7f726
commit bd8ec1d
Showing
3 changed files
with
92 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
use input::event::keyboard::KeyState; | ||
use mlua::Lua; | ||
use std::collections::HashMap; | ||
use std::process::{Command, Stdio}; | ||
use std::sync::{Arc, Mutex}; | ||
use std::u32; | ||
|
||
use crate::parser::parse_binding; | ||
|
||
#[derive(Debug)] | ||
enum Bindtype { | ||
Command(String), | ||
} | ||
|
||
pub struct ScriptManager { | ||
lua: &'static Lua, | ||
actions: Arc<Mutex<HashMap<Vec<u32>, Bindtype>>>, | ||
} | ||
|
||
impl ScriptManager { | ||
pub fn new() -> Self { | ||
let lua = Box::leak(Box::new(Lua::new())); | ||
let actions = Arc::new(Mutex::new(HashMap::new())); | ||
|
||
ScriptManager { lua, actions } | ||
} | ||
|
||
pub fn register_functions(&self) -> Result<(), mlua::Error> { | ||
let actions_str = Arc::clone(&self.actions); | ||
|
||
let basic_bind = | ||
self.lua | ||
.create_function(move |_, (binding, target): (String, String)| { | ||
println!("Binding key: {:?}", binding); | ||
println!("Target: {:?}", target); | ||
let mut actions_lock = actions_str.lock().unwrap(); | ||
let binding = parse_binding(&binding); | ||
let target = Bindtype::Command(target); | ||
actions_lock.insert(binding, target); | ||
Ok(()) | ||
})?; | ||
self.lua.globals().set("bind", basic_bind)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn load_script(&self, script: &str) -> Result<(), mlua::Error> { | ||
self.lua.load(script).exec() | ||
} | ||
|
||
pub fn handle_action(&self, total_combo: Vec<u32>, state: KeyState) { | ||
if let Some(action) = self.actions.lock().unwrap().get(&total_combo) { | ||
if state == KeyState::Pressed { | ||
println!("Action: {:?}", action); | ||
|
||
match action { | ||
Bindtype::Command(command) => { | ||
// run_command_as_user(command); | ||
Command::new("sh") | ||
.arg("-c") | ||
.arg(command) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.spawn() | ||
.expect("Failed to execute command"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |