Skip to content

Commit

Permalink
Parse and execute
Browse files Browse the repository at this point in the history
Commands are now executed from bound functions. Albiet unsuccessfully due to user privalege issues
  • Loading branch information
divanvisagie committed Jul 9, 2024
1 parent ba1296c commit 007c543
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::os::unix::{fs::OpenOptionsExt, io::OwnedFd};
use std::path::Path;
use std::process::Command;
use std::sync::{Arc, Mutex};
use std::u32;

struct Interface;

Expand All @@ -32,16 +34,30 @@ impl LibinputInterface for Interface {
enum Keys {
A = 30,
B = 31,
C = ,
D = 32,
LeftMod = 125,
LeftAlt = 56,
}
fn parse_binding(binding: &str) -> Vec<u32> {
let strings: Vec<String> = binding.split('+').map(|s| s.to_string()).collect();

let mut keys = Vec::new();
for string in strings {
match string.as_str() {
"Alt" => keys.push(Keys::LeftAlt as u32),
"A" => keys.push(Keys::A as u32),
_ => {}
}
}
keys
}
fn main() {
let mut input = Libinput::new_with_udev(Interface);
input.udev_assign_seat("seat0").unwrap();

let lua = Lua::new();
let actions: HashMap<String, String> = HashMap::new();
let actions: HashMap<Vec<u32>, String> = HashMap::new();
let lua = Arc::new(Mutex::new(lua));
let actions = Arc::new(Mutex::new(actions));

Expand All @@ -55,6 +71,7 @@ fn main() {
println!("Binding key: {:?}", binding);
println!("Target: {:?}", target);
let mut actions_lock = actions.lock().unwrap();
let binding = parse_binding(&binding);
actions_lock.insert(binding, target);
Ok(())
})
Expand All @@ -66,7 +83,7 @@ fn main() {
let lua = lua.clone();
let actions = actions.clone();
let script = r#"
bind("Alt+A", "test")
bind("Alt+A", "gnome-terminal")
print("lol")
"#;
let lock = lua.lock().unwrap();
Expand Down Expand Up @@ -95,15 +112,26 @@ fn main() {
active_keys.clear();
}

if active_keys.contains(&(Keys::LeftAlt as u32))
&& key == Keys::A as u32
&& state == KeyState::Pressed
{
// A merge of active keys and the current key pressed
let total_combo = active_keys
.iter()
.chain(std::iter::once(&key))
.copied()
.collect::<Vec<u32>>();

if let Some(action) = actions.lock().unwrap().get("Alt+A") {
// is there something in the actions map that contains all of
// the keys in the total_combo
if let Some(action) = actions.lock().unwrap().get(&total_combo) {
if state == KeyState::Pressed {
println!("Action: {:?}", action);

// execute action as command
Command::new(action)
.spawn()
.expect("Failed to execute command");
}
}

}
_ => {} // Ignore non-keyboard events
}
Expand Down

0 comments on commit 007c543

Please sign in to comment.