1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
//! Mouse button related code
use crate::{config::BUTTON_BINDS, conn_info::Connection, debug, Error, Result, WM};
use xcb::{
x::{
self, Allow, AllowEvents, ButtonIndex, ButtonPressEvent, Cursor, EventMask, GrabButton,
GrabMode, GrabPointer, ModMask, MotionNotifyEvent, QueryPointer, UngrabButton,
UngrabPointer, Window,
},
Xid,
};
impl WM<'_> {
/// Dispatch the given button press event according to [`self::BUTTON_BINDS`]
pub fn handle_button_press(&mut self, e: &ButtonPressEvent) {
self.conn.send_request(&AllowEvents {
mode: Allow::ReplayPointer,
time: x::CURRENT_TIME,
});
let button = match e.detail() {
1 => ButtonIndex::N1,
2 => ButtonIndex::N2,
3 => ButtonIndex::N3,
4 => ButtonIndex::N4,
5 => ButtonIndex::N5,
_ => return,
};
BUTTON_BINDS.dispatch(
self,
button,
ModMask::from_bits_truncate(e.state().bits())
.difference(self.conn.keyboard_state.numlock_mask() | ModMask::LOCK),
);
}
/// Move the currently focused window until the mouse button is released.
/// This starts its own event loop, so won't exit till the user is done moving the window.
pub fn mouse_move(&mut self) -> Result<()> {
let Some(w) = self.clients.focused_mut().map(|c| c.window()) else {
return Ok(());
};
let reply = self
.conn
.wait_for_reply(self.conn.send_request(&QueryPointer { window: w }))?;
let (original_x, original_y) = (reply.win_x(), reply.win_y());
self.track_mouse_movement(w, |this, e| {
if let Some(c) = this.clients.focused_mut() {
c.set_geom(
&this.conn,
e.root_x() - original_x,
e.root_y() - original_y,
c.width(),
c.height(),
c.border_width(),
);
c.configure_notify(&this.conn);
}
Ok(())
})
}
/// Resize the currently focused window with the mouse.
/// This starts its own event loop, so won't exit till the user is done resizing the window.
pub fn mouse_resize(&mut self) -> Result<()> {
let Some((w, original_width, original_height)) = self
.clients
.focused_mut()
.map(|c| (c.window(), c.width(), c.height()))
else {
return Ok(());
};
let reply = self
.conn
.wait_for_reply(self.conn.send_request(&QueryPointer { window: w }))?;
let (original_x, original_y) = (reply.win_x(), reply.win_y());
self.track_mouse_movement(w, |this, e| {
if let Some(c) = this.clients.focused_mut() {
c.set_geom(
&this.conn,
c.x(),
c.y(),
original_width.saturating_add_signed(e.root_x() - original_x),
original_height.saturating_add_signed(e.root_y() - original_y),
c.border_width(),
);
c.configure_notify(&this.conn);
}
Ok(())
})
}
/// Start tracking pointer movement, calling `f` with each generated motion event.
/// This starts its own event loop, so won't exit till the user is done resizing the window.
pub fn track_mouse_movement(
&mut self,
w: Window,
mut f: impl FnMut(&mut Self, MotionNotifyEvent) -> Result<()>,
) -> Result<()> {
debug!("grabbing pointer for mouse movement tracking action");
self.conn
.wait_for_reply(self.conn.send_request(&GrabPointer {
owner_events: false,
grab_window: w,
event_mask: EventMask::BUTTON_PRESS
| EventMask::BUTTON_RELEASE
| EventMask::POINTER_MOTION,
pointer_mode: GrabMode::Async,
keyboard_mode: GrabMode::Async,
confine_to: Window::none(),
cursor: self.conn.cursors.moving(),
time: x::CURRENT_TIME,
}))?;
let mut last_time = 0;
let mut res = Ok(());
while res.is_ok() {
match self.conn.wait_for_event() {
Ok(xcb::Event::X(x::Event::MotionNotify(e))) => {
if e.time() - last_time <= (1000 / 60) {
continue;
}
last_time = e.time();
res = f(self, e);
res = res.and(self.conn.flush());
}
Ok(xcb::Event::X(x::Event::ButtonRelease(_))) => {
break;
}
Ok(e) => self.dispatch_event(e)?,
Err(Error::Xcb(xcb::Error::Protocol(e))) => {
eprintln!("protocol error in event loop: {e:#?}\ncontinuing anyway");
}
Err(e) => {
eprintln!("unrecoverable error: {e:#?}\nexiting event loop");
res = Err(e);
}
}
}
debug!("ending mouse movement tracking");
self.conn.send_request(&UngrabPointer {
time: x::CURRENT_TIME,
});
res
}
}
pub fn grab(conn: &Connection<'_>, window: Window, focused: bool) {
conn.send_request(&UngrabButton {
button: ButtonIndex::Any,
modifiers: ModMask::ANY,
grab_window: window,
});
if !focused {
conn.send_request(&GrabButton {
button: ButtonIndex::Any,
modifiers: ModMask::ANY,
grab_window: window,
owner_events: false,
event_mask: EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
pointer_mode: GrabMode::Sync,
keyboard_mode: GrabMode::Sync,
confine_to: Window::none(),
cursor: Cursor::none(),
});
}
for btn in BUTTON_BINDS.binds() {
for modifiers in [
ModMask::empty(),
ModMask::LOCK,
conn.keyboard_state.numlock_mask(),
conn.keyboard_state.numlock_mask() | ModMask::LOCK,
] {
conn.send_request(&GrabButton {
button: btn.button,
modifiers: btn.modifiers | modifiers,
grab_window: window,
owner_events: false,
event_mask: EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
pointer_mode: GrabMode::Async,
keyboard_mode: GrabMode::Sync,
confine_to: Window::none(),
cursor: Cursor::none(),
});
}
}
}
/// A button bound to some action
pub struct ButtonBind {
pub modifiers: ModMask,
pub button: ButtonIndex,
pub action: &'static dyn Fn(&mut WM<'_>),
}
/// A set of button binds. Currently, there is only one instance of this defined statically: [`crate::config::BUTTON_BINDS`].
pub struct ButtonBinds(pub &'static [ButtonBind]);
impl ButtonBinds {
/// Get an iterator over all bound buttons
pub fn binds(&self) -> impl Iterator<Item = &ButtonBind> {
self.0.iter()
}
/// Attempt to run the action for the matching buttonbind, if it exists.
pub fn dispatch(&self, wm: &mut WM<'_>, button: ButtonIndex, modifiers: ModMask) {
debug!("received {modifiers:?} {button:?}");
if let Some(bind) = self
.binds()
.find(|b| b.button == button && modifiers == b.modifiers)
{
debug!("found action");
(bind.action)(wm);
}
}
}
impl std::fmt::Debug for ButtonBind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ButtonBind")
.field("modifiers", &self.modifiers)
.field("button", &self.button)
.finish_non_exhaustive()
}
}
|