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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
//! Tracking and managing windows.
use std::{
cmp::{max, min, Ordering},
mem,
};
use crate::{
buttons,
config::BORDER_WIDTH,
conn_info::Connection,
debug,
error::{Error, Result},
WM,
};
use xcb::{
x::{
self, ChangeProperty, CloseDown, ConfigWindow, ConfigWindowMask, ConfigureNotifyEvent,
ConfigureRequestEvent, ConfigureWindow, DeleteProperty, DestroyNotifyEvent, Drawable,
EventMask, GetGeometry, GetWindowAttributes, InputFocus, KillClient, MapRequestEvent,
PropMode, SetCloseDownMode, SetInputFocus, UnmapNotifyEvent, Window,
},
xinerama, BaseEvent, Extension, Xid,
};
pub use client::*;
pub use hints::*;
pub use monitors::*;
#[doc(hidden)]
mod client;
#[doc(hidden)]
mod hints;
#[doc(hidden)]
mod monitors;
mod tile;
/// The tag a client has, similar to a workspace in most WMs.
pub type Tag = u8;
pub type MonitorIdx = usize;
pub type ClientIdx = usize;
impl WM<'_> {
/// Perform configure requests if we're happy with them, or they're for an unmanaged window.
pub(crate) fn handle_configure_request(&mut self, e: &ConfigureRequestEvent) {
if let Some(c) = self.clients.find_client_mut(e.window()) {
// Always allow setting border width
if c.floating() {
c.set_geom_from(&self.conn, e);
} else if e.value_mask().contains(ConfigWindowMask::BORDER_WIDTH) {
c.set_geom(
&self.conn,
c.x(),
c.y(),
c.height(),
c.width(),
e.border_width(),
);
}
c.configure_notify(&self.conn);
} else {
// Configure it as requested, and sort the rest when we actually map the window
// According to spec, we should check the property mask and only set properties that are specified, setting defaults
// for other ones.
// Unfortunately, some clients (such as Audacity) will not set the mask correctly.
// So instead we just use all the properties, and the ones that can be invalid (width, height > 0) we clamp.
self.conn.send_request(&ConfigureWindow {
window: e.window(),
value_list: &[
ConfigWindow::X(e.x().into()),
ConfigWindow::Y(e.y().into()),
ConfigWindow::Width(max(e.width().into(), 1)),
ConfigWindow::Height(max(e.height().into(), 1)),
ConfigWindow::BorderWidth(e.border_width().into()),
ConfigWindow::StackMode(e.stack_mode()),
],
});
}
}
/// Update our monitor geometry if the root window is reconfigured
pub(crate) fn handle_configure_notify(&mut self, e: &ConfigureNotifyEvent) -> Result<()> {
if e.window() == self.conn.root() {
self.clients.update_geometry(&self.conn)?;
}
Ok(())
}
/// Removing destroyed windows from the client list and rearrange.
pub(crate) fn handle_destroy_notify(&mut self, e: &DestroyNotifyEvent) {
// destroynotify gets fired twice: one with event = e.window(), and one with event = root
// so only listen to the second one
if e.event() == self.conn.root() {
self.clients.unmanage_destroyed(&self.conn, e.window());
}
}
/// Map a window, starting to manage it if needed.
pub(crate) fn handle_map_request(&mut self, e: &MapRequestEvent) -> Result<()> {
// Ignore already managed windows
if self.clients.find_client_mut(e.window()).is_some() {
return Ok(());
}
let attrs = self.conn.wait_for_reply(
self.conn
.send_request(&GetWindowAttributes { window: e.window() }),
)?;
if attrs.override_redirect() {
// Something special, or us doing actual mapping. Don't manage it just let it do its thing.
return Ok(());
}
// Start managing, and map window
self.clients.manage(&self.conn, e.window());
Ok(())
}
/// When a window is unmapped, update its state.
pub(crate) fn handle_unmap_notify(&mut self, e: &UnmapNotifyEvent) {
if !e.is_from_send_event() {
if let Some(c) = self.clients.find_client_mut(e.window()) {
let cookie = c.set_withdrawn(&self.conn, true);
// The above may fail if the window has already been destroyed - just discard the error here.
let _ = self.conn.check_request(cookie);
}
if self
.clients
.focused()
.is_some_and(|c| c.window() == e.window())
{
self.clients.unfocus_destroyed();
}
}
}
}
/// Holds state related to the window manager's clients
/// This contains a list of clients per monitor, alongside info on that monitor's screen size.
pub struct ClientState {
/// The current arranging function.
/// This function is expected to ensure that all clients are the correct size, reconfigure them if needed, and map/unmap as needed.
arrange: &'static dyn Fn(&Connection<'_>, &mut [Client], &MonitorInfo),
/// Clients attached to that monitor
pub clients: Vec<Client>,
/// Information for each monitor.
mons: Vec<MonitorInfo>,
/// Index of the currently focused client
focused_client: Option<ClientIdx>,
}
impl ClientState {
/// Start managing the given window, adding it to the client list and ensuring its configuration is valid.
pub fn manage(&mut self, conn: &Connection<'_>, window: Window) {
let mut tag = self.focused_mon().focused_tag.create_tag();
let mut floating = false;
if let Some(parent) = hints::transient_for(conn, window) {
floating = true;
if let Some(c) = self.find_client_mut(parent) {
tag = c.tag;
}
}
let is_visible = self.focused_mon().focused_tag.matches(tag);
let new_idx = self.clients.len();
self.clients.push(Client::new(window, tag));
let Ok(geom) = conn.wait_for_reply(conn.send_request(&GetGeometry {
drawable: Drawable::Window(window),
})) else {
return; // window stopped existing, so we can't manage it
};
let mon_geom = self.focused_mon().screen_info;
let c = &mut self.clients[new_idx];
#[allow(clippy::cast_sign_loss)]
c.set_geom(
conn,
geom.x(),
geom.y(),
min(geom.width(), mon_geom.width.saturating_sub(geom.x() as u16)),
min(
geom.height(),
mon_geom.height.saturating_sub(geom.y() as u16),
),
BORDER_WIDTH,
);
c.set_border(conn, conn.colours.border_normal());
if floating {
c.set_floating(conn);
}
c.sync_hints(conn, true);
c.apply_geometry_hints(conn, &mon_geom);
c.set_event_mask(
conn,
EventMask::ENTER_WINDOW
| EventMask::FOCUS_CHANGE
| EventMask::PROPERTY_CHANGE
| EventMask::STRUCTURE_NOTIFY
| EventMask::BUTTON_PRESS
| EventMask::KEY_PRESS,
);
// Add to net_client_list
conn.send_request(&ChangeProperty {
mode: xcb::x::PropMode::Append,
window: conn.root(),
property: conn.atoms.net_client_list,
r#type: x::ATOM_WINDOW,
data: &[window],
});
if is_visible {
c.ensure_mapped(conn);
self.rearrange(conn);
self.refocus(conn, self.clients.len() - 1);
}
}
/// Stop managing the given destroyed window
pub fn unmanage_destroyed(&mut self, conn: &Connection<'_>, window: Window) {
let Some(i) = self.find_client_pos(window) else {
return;
};
let focused_mon = self.focused_mon().screen_info;
self.clients.remove(i);
self.focused_client = self.focused_client.and_then(|f| match f.cmp(&i) {
Ordering::Less => Some(f),
Ordering::Greater => Some(f - 1),
Ordering::Equal => None,
});
if self.focused_client.is_none() {
if let Some(i) = self
.clients
.iter()
.position(|c| focused_mon.contains(c.x(), c.y()))
{
self.refocus(conn, i);
}
}
self.rearrange(conn);
}
/// Refocus on the client with the given co-ordinates, setting X11 properties as required.
/// If the given index is invalid, focus on the root instead.
pub fn refocus(&mut self, conn: &Connection<'_>, i: ClientIdx) {
self.unfocus(conn);
if self.set_focused(i).is_some() {
let mon_idx = self.client_mon_idx(i);
let tag = self.client(i).tag;
if !self.mons[mon_idx].focused_tag.matches(tag) {
self.set_mon_tag_focus(conn, mon_idx, TagFocus::Tag(tag));
}
let new = self.client_mut(i);
new.set_border(conn, conn.colours.border_focused());
new.sync_hints(conn, true);
buttons::grab(conn, new.window(), true);
if !new.never_focus() {
conn.send_request(&SetInputFocus {
revert_to: InputFocus::PointerRoot,
focus: new.window(),
time: x::CURRENT_TIME,
});
conn.send_request(&ChangeProperty {
window: conn.root(),
mode: PropMode::Replace,
property: conn.atoms.net_active_window,
r#type: x::ATOM_WINDOW,
data: &[new.window()],
});
conn.send_event(new.window(), conn.atoms.wm_take_focus);
}
} else {
conn.send_request(&SetInputFocus {
revert_to: InputFocus::PointerRoot,
focus: conn.root(),
time: x::CURRENT_TIME,
});
conn.send_request(&DeleteProperty {
window: conn.root(),
property: conn.atoms.net_active_window,
});
}
}
/// Unfocus the currently focused window, if it exists.
pub fn unfocus(&mut self, conn: &Connection<'_>) {
if let Some(old) = self.focused_mut() {
old.set_border(conn, conn.colours.border_normal());
buttons::grab(conn, old.window(), false);
}
self.focused_client = None;
}
/// Unfocus the currently focused window, if it exists, without doing any of the X11 parts.
/// This is used when a focused window is destroyed or unmapped
pub fn unfocus_destroyed(&mut self) {
self.focused_client = None;
}
/// Go to the next or previous window in the current monitor, looping around if needed
pub fn change_focus(&mut self, conn: &Connection<'_>, increase: bool) {
if self.clients.is_empty() {
return;
}
let tag_focus = self.focused_mon().focused_tag;
let curr_focused = self.focused_client.unwrap_or(0);
let look_through = self
.clients
.iter()
.enumerate()
.filter(|(_, c)| tag_focus.matches(c.tag))
.filter(|(_, c)| self.focused_mon().screen_info.contains(c.x(), c.y()))
.map(|(i, _)| i);
let new_idx = if increase {
look_through
.cycle()
.skip_while(|i| *i != curr_focused)
.nth(1)
.unwrap_or(0)
} else {
look_through
.rev()
.cycle()
.skip_while(|i| *i != curr_focused)
.nth(1)
.unwrap_or(0)
};
self.refocus(conn, new_idx);
}
/// Shift the focused client up (increase = false) or down (increase = true) the client list
pub fn shift_focused_client(&mut self, conn: &Connection<'_>, increase: bool) {
// We always want to place it after/before the next/previous one on the focused tag
let Some(target) = self.focused_client else {
return;
};
let mon_idx = self.client_mon_idx(target);
let tag_focus = self.mons[mon_idx].focused_tag;
let look_through = self
.clients
.iter()
.enumerate()
.filter(|(_, c)| tag_focus.matches(c.tag))
.filter(|(_, c)| c.tiled())
.map(|(i, _)| i);
let new_idx = if increase {
look_through
.cycle()
.skip_while(|i| *i != target)
.nth(1)
.unwrap_or(0)
} else {
look_through
.rev()
.cycle()
.skip_while(|i| *i != target)
.nth(1)
.unwrap_or(0)
};
// Need to do split it so that the borrow checker is happy
match new_idx.cmp(&target) {
Ordering::Less => {
let (before, after) = self.clients.split_at_mut(target);
mem::swap(&mut before[new_idx], &mut after[0]);
}
Ordering::Greater => {
let (before, after) = self.clients.split_at_mut(new_idx);
mem::swap(&mut before[target], &mut after[0]);
}
Ordering::Equal => return,
}
self.focused_client = Some(new_idx); // Doesn't need any actual X operations done
self.rearrange_mon(conn, mon_idx);
}
/// Shift the focused client to the top of the stack
pub fn shift_focused_to_top(&mut self, conn: &Connection<'_>) {
if self.clients.len() == 1 {
return; // Already at the top
}
let Some(target) = self.focused_client else {
return;
};
// Need to do split it so that the borrow checker is happy
let (before, after) = self.clients.split_at_mut(1);
mem::swap(&mut before[0], &mut after[target - 1]);
self.focused_client = Some(0); // Doesn't need any actual X operations done
self.rearrange_mon(conn, self.focused_mon_idx());
}
/// Toggle whether the client with the given position is floating
pub fn toggle_floating(&mut self, conn: &Connection<'_>, pos: ClientIdx) {
let c = &mut self.clients[pos];
if c.tiled() {
c.set_floating(conn);
} else {
c.set_tiled(conn);
}
self.rearrange_mon(conn, self.client_mon_idx(pos));
}
/// Toggle whether the client with the given position is fullscreen
pub fn toggle_fullscreen(&mut self, conn: &Connection<'_>, pos: ClientIdx) {
let mon = self.client_mon_idx(pos);
let mon_info = &self.mons[mon];
let c = &mut self.clients[pos];
if c.tiled() {
c.set_fullscreen(conn, &mon_info.screen_info);
} else {
c.set_tiled(conn);
}
self.rearrange_mon(conn, mon);
}
/// Set the focused tag for the given monitor
pub fn set_mon_tag_focus(
&mut self,
conn: &Connection<'_>,
mon: MonitorIdx,
tag_focus: TagFocus,
) {
// Hide windows from currently focused tag filter
let mon_info = &self.mons[mon];
let curr_focus = mon_info.focused_tag;
self.clients
.iter_mut()
.filter(|c| curr_focus.matches(c.tag))
.filter(|c| mon_info.screen_info.contains(c.x(), c.y()))
.for_each(|c| c.ensure_unmapped(conn));
debug!("setting tag focus to {:?} on mon {}", tag_focus, mon);
self.mons[mon].last_focused_tag = curr_focus;
self.mons[mon].focused_tag = tag_focus;
self.rearrange_mon(conn, mon);
self.unfocus(conn);
self.focused_client = self.clients.iter().position(|c| tag_focus.matches(c.tag));
}
/// Set the given monitor's focused tag to its previous value
pub fn mon_prev_tag_focus(&mut self, conn: &Connection<'_>, mon: MonitorIdx) {
self.set_mon_tag_focus(conn, mon, self.mons[mon].last_focused_tag);
}
/// Set the tag for the given client
pub fn set_client_tag(&mut self, conn: &Connection<'_>, pos: ClientIdx, tag: Tag) {
let c = self.client_mut(pos);
if c.tag == tag {
return;
}
debug!("moving client with window {:?} to tag {}", c.window(), tag);
c.tag = tag;
c.ensure_unmapped(conn);
self.unfocus(conn);
self.rearrange_mon(conn, self.client_mon_idx(pos));
}
/// Update the recorded monitors and monitor sizes, retiling if necessary.
pub fn update_geometry(&mut self, conn: &Connection<'_>) -> Result<()> {
let mut dirty = false;
if conn.active_extensions().any(|e| e == Extension::Xinerama) {
let reply = conn.wait_for_reply(conn.send_request(&xinerama::QueryScreens {}))?;
// Monitor removed, move its clients away
if reply.screen_info().len() > self.mons.len() {
dirty = true;
self.truncate_screens(reply.screen_info().len());
}
// Update screen info & add new client lists if needed
for (i, monitor) in reply.screen_info().iter().enumerate() {
dirty |= self.set_monitor_geometry(i, (*monitor).into());
}
} else {
// Only one screen
if self.mons.len() > 1 {
dirty = true;
self.truncate_screens(1);
}
// TODO: it looks like this won't actually update when the screen size changes?
let setup = conn.get_setup();
let screen = setup
.roots()
.nth(conn.screen_num())
.ok_or(Error::NoSuchScreen)?;
dirty |= self.set_monitor_geometry(
0,
MonitorGeometry {
x_org: 0,
y_org: 0,
width: screen.width_in_pixels(),
height: screen.height_in_pixels(),
},
);
}
if dirty {
self.rearrange(conn);
}
Ok(())
}
/// Set the new amount of screens
fn truncate_screens(&mut self, new_size: usize) {
match new_size.cmp(&self.mons.len()) {
Ordering::Greater => {
for _ in 0..new_size - self.mons.len() {
self.mons.push(MonitorInfo::default());
}
}
Ordering::Less => {
self.mons.drain(new_size - self.mons.len()..self.mons.len());
}
Ordering::Equal => (),
}
}
/// Set the given screen's geometry, resizing the monitor list if necessary.
/// Returns true if the new info is different from the old one.
fn set_monitor_geometry(&mut self, i: MonitorIdx, info: MonitorGeometry) -> bool {
while i >= self.mons.len() {
self.mons.push(MonitorInfo::default());
}
let dirty = self.mons[i].screen_info != info;
self.mons[i].screen_info = info;
dirty
}
/// Find the [`Client`] corresponding to the given window
pub fn find_client_mut(&mut self, window: Window) -> Option<&mut Client> {
let i = self.find_client_pos(window)?;
Some(&mut self.clients[i])
}
/// Find the position of the client with the given window, returning an index
pub fn find_client_pos(&self, window: Window) -> Option<ClientIdx> {
self.clients.iter().position(|c| c.window() == window)
}
/// Get the index of the currently focused client, if it exists
pub const fn focused_pos(&self) -> Option<ClientIdx> {
self.focused_client
}
/// Get a reference to the currently focused client, if it exists.
pub fn focused(&self) -> Option<&Client> {
self.focused_client.map(|i| self.client(i))
}
/// Get a mutable reference to the currently focused client, if it exists.
pub fn focused_mut(&mut self) -> Option<&mut Client> {
self.focused_client.map(|i| self.client_mut(i))
}
pub fn is_focused(&self, e: Window) -> bool {
self.focused().is_some_and(|c| c.window() == e)
}
/// Set the currently focused client, returning a mutable reference to the client.
pub fn set_focused(&mut self, mut i: ClientIdx) -> Option<&mut Client> {
if self.clients.is_empty() {
return None;
}
if i >= self.clients.len() {
i = self.clients.len() - 1;
}
self.focused_client = Some(i);
Some(&mut self.clients[i])
}
/// Get a reference to the client at the given index
pub fn client(&self, i: ClientIdx) -> &Client {
&self.clients[i]
}
/// Get a mutable reference to the client at the given index
pub fn client_mut(&mut self, i: ClientIdx) -> &mut Client {
&mut self.clients[i]
}
/// Rearrange all clients, reconfiguring them as needed.
pub fn rearrange(&mut self, conn: &Connection<'_>) {
for mon in 0..self.mons.len() {
self.rearrange_mon(conn, mon);
}
}
/// Rearrange a specific monitor
pub fn rearrange_mon(&mut self, conn: &Connection<'_>, mon: MonitorIdx) {
(self.arrange)(conn, &mut self.clients, &self.mons[mon]);
}
/// Get the index of the currently focused monitor
pub fn focused_mon_idx(&self) -> MonitorIdx {
self.focused_client.map_or(0, |i| self.client_mon_idx(i))
}
/// Get the info for the currently focused monitor
pub fn focused_mon(&self) -> &MonitorInfo {
&self.mons[self.focused_mon_idx()]
}
/// Get the monitor info for the client with the given index
pub fn client_mon_idx(&self, pos: ClientIdx) -> MonitorIdx {
let c = self.client(pos);
self.mons
.iter()
.position(|m| m.screen_info.contains(c.x(), c.y()))
.unwrap_or_else(|| self.mons.len() - 1)
}
/// Get the monitor info for the client with the given index
pub fn client_mon(&self, pos: ClientIdx) -> &MonitorInfo {
&self.mons[self.client_mon_idx(pos)]
}
pub fn kill_client(&self, conn: &Connection, pos: usize) {
let c = self.client(pos);
// Modern clients respond to the WM_DELETE event
if !conn.send_event(c.window(), conn.atoms.wm_delete) {
// Fallback to the old fashioned way
// Using checked requests so we can ignore errors here without waiting for them to go to
// the event loop
let cookie1 = conn.send_request_checked(&SetCloseDownMode {
mode: CloseDown::DestroyAll,
});
let cookie2 = conn.send_request_checked(&KillClient {
resource: c.window().resource_id(),
});
let _ = conn.check_request(cookie1);
let _ = conn.check_request(cookie2);
}
}
}
impl Default for ClientState {
fn default() -> Self {
Self {
arrange: &tile::tile,
mons: vec![],
clients: vec![],
focused_client: None,
}
}
}
impl std::fmt::Debug for ClientState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ClientState")
.field("focused_client", &self.focused_client)
.field("mons", &self.mons)
.field("clients", &self.clients)
.finish_non_exhaustive()
}
}
|