Skip to content

Commit 3fa3b5d

Browse files
authored
Grab is sort of working (it's unstable anyway). (#160)
* Grab is sort of working (it's unstable anyway). * Hide this crappy display in unstable_grab. * Fmt.
1 parent 638fbab commit 3fa3b5d

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ license = "MIT"
1616
[dependencies]
1717
serde = {version = "1.0", features = ["derive"], optional=true}
1818
lazy_static = "1.4"
19+
serde_json = { version = "1.0", optional = true }
1920

2021
[features]
2122
default = []
2223
serialize = ["serde"]
23-
unstable_grab = ["evdev-rs", "epoll", "inotify"]
24+
unstable_grab = ["evdev-rs", "epoll", "inotify", "dep:serde_json", "serialize"]
2425
wayland = ["input", "input-linux", "xkbcommon"]
2526
x11 = ["dep:x11"]
2627

src/linux/wayland/grab.rs

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::linux::common::Display;
2-
use crate::linux::keyboard::Keyboard;
1+
use super::common::Display;
2+
use super::keyboard::Keyboard;
33
use crate::rdev::{Button, Event, EventType, GrabError, Key, KeyboardState};
44
use epoll::ControlOptions::{EPOLL_CTL_ADD, EPOLL_CTL_DEL};
55
use evdev_rs::{
@@ -13,13 +13,78 @@ use std::io;
1313
use std::os::unix::{
1414
ffi::OsStrExt,
1515
fs::FileTypeExt,
16-
io::{AsRawFd, IntoRawFd, RawFd},
16+
io::{AsRawFd, RawFd},
1717
};
1818
use std::path::Path;
1919
use std::time::SystemTime;
2020

2121
// TODO The x, y coordinates are currently wrong !! Is there mouse acceleration
2222
// to take into account ??
23+
use serde::Deserialize;
24+
use std::process::Command;
25+
26+
pub struct Display {}
27+
28+
#[derive(Debug, Deserialize)]
29+
struct SwayDisplay {
30+
rect: Rect,
31+
}
32+
33+
#[derive(Debug, Deserialize)]
34+
struct HyprDisplay {
35+
#[serde(rename = "activeWorkspace")]
36+
active_workspace: Rect,
37+
}
38+
39+
#[derive(Debug, Deserialize)]
40+
struct Rect {
41+
width: usize,
42+
height: usize,
43+
}
44+
45+
fn get_sway_size() -> Option<(usize, usize)> {
46+
let output = Command::new("swaymsg")
47+
.args(["-t", "get_outputs", "-r"])
48+
.output()
49+
.ok()?
50+
.stdout;
51+
52+
let displays: Vec<SwayDisplay> = serde_json::from_slice(&output).ok()?;
53+
let rect = &displays.get(0)?.rect;
54+
Some((rect.width, rect.height))
55+
}
56+
57+
fn get_hyprland_size() -> Option<(usize, usize)> {
58+
let output = Command::new("hyprctl")
59+
.args(["monitors", "-j"])
60+
.output()
61+
.ok()?
62+
.stdout;
63+
64+
let displays: Vec<HyprDisplay> = serde_json::from_slice(&output).ok()?;
65+
let rect = &displays.get(0)?.active_workspace;
66+
Some((rect.width, rect.height))
67+
}
68+
69+
impl Display {
70+
pub fn new() -> Option<Self> {
71+
Some(Self {})
72+
}
73+
74+
pub fn get_size(&self) -> Option<(usize, usize)> {
75+
if let Some(sway) = get_sway_size() {
76+
Some(sway)
77+
} else if let Some(hyprland) = get_hyprland_size() {
78+
Some(hyprland)
79+
} else {
80+
None
81+
}
82+
}
83+
84+
pub fn get_mouse_pos(&self) -> Option<(usize, usize)> {
85+
Some((0, 0))
86+
}
87+
}
2388

2489
macro_rules! convert_keys {
2590
($($ev_key:ident, $rdev_key:ident),*) => {
@@ -301,12 +366,11 @@ fn evdev_event_to_rdev_event(
301366
// }
302367
// }
303368

304-
pub fn grab<T>(callback: T) -> Result<(), GrabError>
369+
pub fn grab<T>(mut callback: T) -> Result<(), GrabError>
305370
where
306-
T: Fn(Event) -> Option<Event> + 'static,
371+
T: FnMut(Event) -> Option<Event> + 'static,
307372
{
308-
compile_error!("Wayland doesn't support grab yet");
309-
let mut kb = Keyboard::new().ok_or(GrabError::KeyboardError)?;
373+
let mut kb = Keyboard::new().map_err(|_| GrabError::KeyboardError)?;
310374
let display = Display::new().ok_or(GrabError::MissingDisplayError)?;
311375
let (width, height) = display.get_size().ok_or(GrabError::MissingDisplayError)?;
312376
let (current_x, current_y) = display

0 commit comments

Comments
 (0)