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
|
use xcb::{
x::{
self, ChangeProperty, ChangeWindowAttributes, ConfigWindow, ConfigWindowMask,
ConfigureNotifyEvent, ConfigureWindow, Cw, EventMask, MapWindow, PropMode, SendEvent,
SendEventDest, StackMode, UnmapWindow, Window,
},
VoidCookieChecked, Xid,
};
use crate::{config::BORDER_WIDTH, conn_info::Connection};
use super::{hints, MonitorGeometry, Tag};
/// Information about a single client / window
#[derive(Debug)]
#[allow(clippy::struct_excessive_bools)]
pub struct Client {
/// The corresponding X11 window
window: Window,
x: i16,
y: i16,
width: u16,
height: u16,
border_width: u16,
mapped: bool,
urgent: bool,
never_focus: bool,
layout_mode: LayoutMode,
pub tag: Tag,
}
/// How this window's geometry is determined,
#[derive(Debug)]
pub enum LayoutMode {
/// By the tiling algorithm
Tiled,
/// By the user
Floating,
/// By the monitor its on
Fullscreen,
}
impl Client {
pub const fn new(window: Window, tag: Tag) -> Self {
Self {
window,
x: 0,
y: 0,
width: 0,
height: 0,
border_width: BORDER_WIDTH,
mapped: false,
urgent: false,
never_focus: false,
layout_mode: LayoutMode::Tiled,
tag,
}
}
/// Send a configure notify event with the current geometry.
pub fn configure_notify(&self, conn: &Connection<'_>) {
conn.send_request(&SendEvent {
destination: SendEventDest::Window(self.window),
event_mask: EventMask::STRUCTURE_NOTIFY,
event: &ConfigureNotifyEvent::new(
self.window,
self.window,
Window::none(),
self.x,
self.y,
self.width,
self.height,
self.border_width,
false,
),
propagate: false,
});
}
/// Set this client's geometry, also updating the X11 window if needed.
pub fn set_geom(
&mut self,
conn: &Connection<'_>,
x: i16,
y: i16,
width: u16,
height: u16,
border_width: u16,
) {
if (x, y, width, height, border_width)
== (self.x, self.y, self.width, self.height, self.border_width)
{
return;
}
self.x = x;
self.y = y;
self.width = width;
self.height = height;
self.border_width = border_width;
conn.send_request(&ConfigureWindow {
window: self.window,
value_list: &[
ConfigWindow::X(self.x.into()),
ConfigWindow::Y(self.y.into()),
ConfigWindow::Width(self.width.into()),
ConfigWindow::Height(self.height.into()),
ConfigWindow::BorderWidth(self.border_width.into()),
],
});
}
/// Set the client's geometry as requested by the given event.
/// Properties not specified in `e` are not changed.
pub fn set_geom_from(&mut self, conn: &Connection, e: &x::ConfigureRequestEvent) {
let Self {
mut x,
mut y,
mut width,
mut height,
mut border_width,
..
} = self;
if e.value_mask().contains(ConfigWindowMask::X) {
x = e.x();
}
if e.value_mask().contains(ConfigWindowMask::Y) {
y = e.y();
}
if e.value_mask().contains(ConfigWindowMask::HEIGHT) {
height = e.height();
}
if e.value_mask().contains(ConfigWindowMask::WIDTH) {
width = e.width();
}
if e.value_mask().contains(ConfigWindowMask::BORDER_WIDTH) {
border_width = e.border_width();
}
self.set_geom(conn, x, y, height, width, border_width);
}
/// Set the border colour of the X11 window to the given value (see `crate::colours::Colours`)
pub fn set_border(&self, conn: &Connection<'_>, colour: u32) {
conn.send_request(&ChangeWindowAttributes {
window: self.window(),
value_list: &[Cw::BorderPixel(colour)],
});
}
/// Whether the client is tiled
pub const fn tiled(&self) -> bool {
matches!(self.layout_mode, LayoutMode::Tiled)
}
/// Whether the client is fullscreen
pub const fn fullscreen(&self) -> bool {
matches!(self.layout_mode, LayoutMode::Fullscreen)
}
/// Whether the client is floating
pub const fn floating(&self) -> bool {
matches!(self.layout_mode, LayoutMode::Floating)
}
/// Set the window as tiled
pub fn set_tiled(&mut self, conn: &Connection<'_>) {
let dirty = !matches!(self.layout_mode, LayoutMode::Tiled);
self.layout_mode = LayoutMode::Tiled;
if dirty {
self.apply_net_wm_state(conn);
}
}
/// Set the window as floating
pub fn set_floating(&mut self, conn: &Connection<'_>) {
let dirty = !matches!(self.layout_mode, LayoutMode::Floating);
self.layout_mode = LayoutMode::Floating;
if dirty {
conn.send_request(&ConfigureWindow {
window: self.window,
value_list: &[ConfigWindow::StackMode(StackMode::Above)],
});
self.apply_net_wm_state(conn);
}
}
/// Set the window as fullscreen
pub fn set_fullscreen(&mut self, conn: &Connection<'_>, mon_geom: &MonitorGeometry) {
let dirty = !matches!(self.layout_mode, LayoutMode::Fullscreen);
self.layout_mode = LayoutMode::Fullscreen;
if dirty {
conn.send_request(&ConfigureWindow {
window: self.window,
value_list: &[ConfigWindow::StackMode(StackMode::Above)],
});
self.set_geom(
conn,
mon_geom.x_org,
mon_geom.y_org,
mon_geom.width,
mon_geom.height,
self.border_width,
);
self.apply_net_wm_state(conn);
}
}
fn apply_net_wm_state(&self, conn: &Connection<'_>) {
conn.send_request(&ChangeProperty {
mode: PropMode::Replace,
window: self.window,
property: conn.atoms.net_wm_state,
r#type: x::ATOM_ATOM,
data: &[match self.layout_mode {
LayoutMode::Tiled | LayoutMode::Floating => 0_u32,
LayoutMode::Fullscreen => conn.atoms.net_wm_fullscreen.resource_id(),
}],
});
}
/// Ensure this client is currently unmapped
pub fn ensure_unmapped(&mut self, conn: &Connection<'_>) {
if self.mapped {
conn.send_request(&UnmapWindow {
window: self.window,
});
self.mapped = false;
}
}
/// Ensure this client is currently mapped / visible
pub fn ensure_mapped(&mut self, conn: &Connection<'_>) {
if !self.mapped {
conn.send_request(&MapWindow {
window: self.window,
});
self.mapped = true;
}
}
/// Get the associated window
pub const fn window(&self) -> Window {
self.window
}
/// Set the event mask for this window
pub fn set_event_mask(
&self,
conn: &Connection<'_>,
event_mask: EventMask,
) -> VoidCookieChecked {
conn.send_request_checked(&ChangeWindowAttributes {
window: self.window(),
value_list: &[Cw::EventMask(event_mask)],
})
}
/// Sync the non-geometry related hints with EWMH hints
pub fn sync_hints(&mut self, conn: &Connection<'_>, focused: bool) {
let Some(mut hints) = hints::Ewm::get(conn, self.window) else {
return;
};
if focused && hints.is_urgent() {
hints.set_urgent(false);
hints.apply(conn, self.window);
} else {
self.urgent = hints.is_urgent();
}
self.never_focus = hints.input().is_some_and(|i| !i);
}
/// Apply any geometry-related EWM hints on the window
pub fn apply_geometry_hints(&mut self, conn: &Connection<'_>, mon_geom: &MonitorGeometry) {
if hints::is_fullscreen(conn, self.window) {
self.set_fullscreen(conn, mon_geom);
} else if hints::is_dialog(conn, self.window) {
self.set_floating(conn);
}
}
/// Set the given window as withdrawn / not withdrawn.
pub fn set_withdrawn(&self, conn: &Connection<'_>, withdrawn: bool) -> VoidCookieChecked {
conn.send_request_checked(&ChangeProperty {
mode: xcb::x::PropMode::Replace,
window: self.window,
property: conn.atoms.wm_state,
r#type: conn.atoms.wm_state,
data: &[u32::from(!withdrawn), 0_u32],
})
}
pub fn set_urgent(&mut self, urgent: bool) {
self.urgent = urgent;
}
pub const fn x(&self) -> i16 {
self.x
}
pub const fn y(&self) -> i16 {
self.y
}
pub const fn height(&self) -> u16 {
self.height
}
pub const fn width(&self) -> u16 {
self.width
}
pub const fn border_width(&self) -> u16 {
self.border_width
}
pub const fn never_focus(&self) -> bool {
self.never_focus
}
}
|