1- use crate :: linux :: common:: Display ;
2- use crate :: linux :: keyboard:: Keyboard ;
1+ use super :: common:: Display ;
2+ use super :: keyboard:: Keyboard ;
33use crate :: rdev:: { Button , Event , EventType , GrabError , Key , KeyboardState } ;
44use epoll:: ControlOptions :: { EPOLL_CTL_ADD , EPOLL_CTL_DEL } ;
55use evdev_rs:: {
@@ -13,13 +13,78 @@ use std::io;
1313use std:: os:: unix:: {
1414 ffi:: OsStrExt ,
1515 fs:: FileTypeExt ,
16- io:: { AsRawFd , IntoRawFd , RawFd } ,
16+ io:: { AsRawFd , RawFd } ,
1717} ;
1818use std:: path:: Path ;
1919use 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
2489macro_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 >
305370where
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