Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wayland support using 'unstable grab' in rdev #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ sudo dnf install alsa-lib-devel
```



# Usage

```
Expand All @@ -34,6 +33,23 @@ rustyvibes <soundpack_path> -v <volume> (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 <soundpack_path> -v <volume> (0-100 | optional) --wayland
```

---

### Mechvibes vs. Rustyvibes
Expand Down
7 changes: 6 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ pub struct ArgParser {
/// The volume to be set.
/// Default: 100
#[arg(short, long)]
pub volume: Option<u16>
pub volume: Option<u16>,

/// Enable wayland support (Experimental).
/// Default: false
#[arg(long, default_value_t = false)]
pub wayland: bool
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
26 changes: 21 additions & 5 deletions src/start.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Event> {
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<Mutex<HashSet<i32>>> = Lazy::new(|| Mutex::new(HashSet::new()));

fn callback(event: Event, json_file: serde_json::Map<std::string::String, serde_json::Value>, directory: String, vol: u16) {
fn callback(event: Event, json_file: serde_json::Map<std::string::String, serde_json::Value>, directory: String, vol: u16) -> Option<Event> {
match event.event_type {
rdev::EventType::KeyPress(key) => {
let key_code = key_code::code_from_key(key);
Expand Down Expand Up @@ -107,5 +120,8 @@ pub mod rustyvibes {
}
_ => (),
}

// Return the event to avoid blocking
Some(event)
}
}
}