From c78d505f4848654f1e4d9207b077a78c219f8a92 Mon Sep 17 00:00:00 2001 From: sahaj-b Date: Sun, 17 Nov 2024 17:27:51 +0530 Subject: [PATCH 1/2] Added wayland support --- Cargo.toml | 2 +- README.md | 6 ++++++ src/args.rs | 7 ++++++- src/main.rs | 3 ++- src/start.rs | 26 +++++++++++++++++++++----- 5 files changed, 36 insertions(+), 8 deletions(-) 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..991818b 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,12 @@ Fedora sudo dnf install alsa-lib-devel ``` +### 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 # Usage diff --git a/src/args.rs b/src/args.rs index 2b0a476..32c48c3 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, + + /// Use unstable_grab for wayland + /// 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 +} From 2d091d12530e48a0ce5622f90df1785f15ca3cae Mon Sep 17 00:00:00 2001 From: sahaj-b Date: Sun, 17 Nov 2024 17:31:56 +0530 Subject: [PATCH 2/2] Fixed args and readme --- README.md | 24 +++++++++++++++++------- src/args.rs | 2 +- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 991818b..b0b032d 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,6 @@ Fedora sudo dnf install alsa-lib-devel ``` -### 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 - # Usage @@ -40,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 32c48c3..3ac2eeb 100644 --- a/src/args.rs +++ b/src/args.rs @@ -19,7 +19,7 @@ pub struct ArgParser { #[arg(short, long)] pub volume: Option, - /// Use unstable_grab for wayland + /// Enable wayland support (Experimental). /// Default: false #[arg(long, default_value_t = false)] pub wayland: bool