diff --git a/Cargo.toml b/Cargo.toml index 0e52006..bd68b9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rdev = "0.5.3" +rdev = { version = "0.5.3", features = ["unstable_grab"]} serde_json = "1.0.120" serde = { version = "1.0.203", features = ["derive"] } libc = "0.2.155" diff --git a/README.md b/README.md index 5d3f581..b0b032d 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ sudo dnf install alsa-lib-devel ``` - # Usage ``` @@ -34,6 +33,23 @@ rustyvibes -v (0-100 | optional) ### Download Soundpacks: [Here](https://drive.google.com/file/d/1LQEQ9aOVQAs_wgVecXkjaA9K4LXnCdp_/view?usp=sharing) +## Wayland Support +Global Wayland support has limitations: + - Will not work if you use any key remapper (like keyd, kmonad, etc) + - Latency is increased by noticeable amount + +To use it, first add the user to `input` group using this command: +``` +sudo usermod -a -G input $USER +``` + +Then **Login and Logout** to apply the changes + +Then run the command with the `--wayland` flag +``` +rustyvibes -v (0-100 | optional) --wayland +``` + --- ### Mechvibes vs. Rustyvibes diff --git a/src/args.rs b/src/args.rs index 2b0a476..3ac2eeb 100644 --- a/src/args.rs +++ b/src/args.rs @@ -17,5 +17,10 @@ pub struct ArgParser { /// The volume to be set. /// Default: 100 #[arg(short, long)] - pub volume: Option + pub volume: Option, + + /// Enable wayland support (Experimental). + /// Default: false + #[arg(long, default_value_t = false)] + pub wayland: bool } diff --git a/src/main.rs b/src/main.rs index 8dc437e..49d6fb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ fn main() { let a = ArgParser::parse(); let soundpack = a.soundpack; let vol = a.volume.or(Some(100)).unwrap(); + let using_wayland = a.wayland; - start::rustyvibes::start_rustyvibes(soundpack, vol); + start::rustyvibes::start_rustyvibes(soundpack, vol, using_wayland); } diff --git a/src/start.rs b/src/start.rs index 5eb2f86..8dc644c 100644 --- a/src/start.rs +++ b/src/start.rs @@ -1,6 +1,6 @@ pub mod rustyvibes { - use rdev::{listen, Event}; + use rdev::{listen, Event, grab}; use serde_json; use serde_json::{Map, Value}; use std::error::Error; @@ -37,7 +37,7 @@ pub mod rustyvibes { } } - pub fn start_rustyvibes(args: String, vol: u16) { + pub fn start_rustyvibes(args: String, vol: u16, using_wayland: bool) { { #[cfg(any(target_os = "macos", target_os = "linux"))] unsafe { @@ -60,22 +60,35 @@ pub mod rustyvibes { println!("Soundpack configuration loaded"); println!("Rustyvibes is running"); + + + if using_wayland { + println!("using 'unstable_grab' for wayland"); + let event_handler = move |event: Event| -> Option { + json_file.event_handler(event.clone(), args.clone(), vol); + Some(event.clone()) + }; + if let Err(error) = grab(event_handler) { + println!("Error: {:?}", error) + } + }else{ let event_handler = move |event: Event| { json_file.event_handler(event, args.clone(), vol); }; - if let Err(error) = listen(event_handler) { println!("Error: {:?}", error) } } + } + use once_cell::sync::Lazy; use std::collections::HashSet; use std::sync::Mutex; static KEY_DEPRESSED: Lazy>> = Lazy::new(|| Mutex::new(HashSet::new())); - fn callback(event: Event, json_file: serde_json::Map, directory: String, vol: u16) { + fn callback(event: Event, json_file: serde_json::Map, directory: String, vol: u16) -> Option { match event.event_type { rdev::EventType::KeyPress(key) => { let key_code = key_code::code_from_key(key); @@ -107,5 +120,8 @@ pub mod rustyvibes { } _ => (), } + + // Return the event to avoid blocking + Some(event) } -} \ No newline at end of file +}