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
|
use std::fmt::Debug;
use xcb::{
x::{
self, ChangeProperty, ChangeWindowAttributes, CreateWindow, DeleteProperty, DestroyWindow,
Window, WindowClass,
},
Connection as RawConnection, Cookie,
};
mod atoms;
mod colours;
mod cursors;
mod keys;
pub use self::{
atoms::Atoms,
colours::{Colour, Colours},
cursors::Cursors,
keys::KeyboardInfo,
};
use crate::error::{Error, Result};
/// The connection, along with some cached resources required for WM operations.
pub struct Connection<'a> {
/// The open connection to an X server
conn: &'a RawConnection,
/// The 'screen' number on the X server. Note this isn’t what you think it is on multi-monitor setups
screen_num: usize,
/// The root window
root: Window,
/// A window used to prove we're actually EWMH compliant.
/// See [the EWMH spec](https://specifications.freedesktop.org/wm-spec/wm-spec-1.3.html#idm46187912047344)
// TODO: Destroy this properly
check_window: Window,
/// Cached colours,
pub colours: Colours,
/// Cached cursors
pub cursors: Cursors,
/// Cached atoms
pub atoms: Atoms,
/// Cached keyboard layout information
pub keyboard_state: KeyboardInfo,
}
impl<'a> Connection<'a> {
/// Prepare the window manager to run on the given connection and screen number.
/// This will fail if another WM is running.
pub fn new(conn: &'a RawConnection, screen_num: usize) -> Result<Self> {
// Fetch root window
let setup = conn.get_setup();
let screen = setup.roots().nth(screen_num).ok_or(Error::NoSuchScreen)?;
// Check no other WM is running
conn.check_request(conn.send_request_checked(&ChangeWindowAttributes {
window: screen.root(),
value_list: &[
x::Cw::BackPixel(screen.white_pixel()),
x::Cw::EventMask(x::EventMask::SUBSTRUCTURE_REDIRECT),
],
}))
.map_err(|_| Error::OtherWMRunning)?;
// Create check window
let check_window = conn.generate_id();
let root = screen.root();
let atoms = Atoms::intern_all(conn)?;
conn.send_request(&CreateWindow {
wid: check_window,
parent: root,
depth: 0,
x: 0,
y: 0,
width: 0,
height: 0,
border_width: 0,
class: WindowClass::InputOutput,
visual: 0,
value_list: &[],
});
conn.send_request(&ChangeProperty {
mode: x::PropMode::Replace,
window: root,
property: atoms.net_wm_check,
r#type: x::ATOM_WINDOW,
data: &[check_window],
});
conn.send_request(&ChangeProperty {
mode: x::PropMode::Replace,
window: check_window,
property: atoms.net_wm_check,
r#type: x::ATOM_WINDOW,
data: &[check_window],
});
conn.send_request(&ChangeProperty {
mode: x::PropMode::Replace,
window: check_window,
property: atoms.net_wm_name,
r#type: x::ATOM_STRING,
data: b"blow",
});
// Supported flag
conn.send_request(&ChangeProperty {
mode: x::PropMode::Replace,
window: root,
property: atoms.net_supported,
r#type: x::ATOM_ATOM,
data: &[
atoms.net_active_window,
atoms.net_wm_name,
atoms.net_wm_state,
atoms.net_wm_check,
atoms.net_wm_fullscreen,
atoms.net_wm_window_type,
atoms.net_wm_window_type_dialog,
atoms.net_client_list,
],
});
// Cleanup state
conn.send_request(&DeleteProperty {
window: root,
property: atoms.net_client_list,
});
// Get the right events
let cursors = Cursors::new_with(conn)?;
conn.send_request(&ChangeWindowAttributes {
window: root,
value_list: &[
x::Cw::EventMask(
x::EventMask::SUBSTRUCTURE_REDIRECT
| x::EventMask::SUBSTRUCTURE_NOTIFY
| x::EventMask::BUTTON_PRESS
| x::EventMask::ENTER_WINDOW
| x::EventMask::FOCUS_CHANGE
| x::EventMask::STRUCTURE_NOTIFY
| x::EventMask::PROPERTY_CHANGE,
),
x::Cw::Cursor(cursors.normal()),
],
});
Ok(Self {
colours: Colours::new_with(conn, screen.default_colormap())?,
atoms: Atoms::intern_all(conn)?,
cursors: Cursors::new_with(conn)?,
keyboard_state: KeyboardInfo::new_with(conn)?,
check_window: conn.generate_id(),
conn,
screen_num,
root: screen.root(),
})
}
/// Refresh cached info about keyboard layout
pub fn refresh_keyboard_info(&mut self) -> Result<()> {
self.keyboard_state = KeyboardInfo::new_with(self.conn)?;
Ok(())
}
/// Delegate for [`RawConnection::send_request`]
pub fn send_request<R>(&self, req: &R) -> R::Cookie
where
R: xcb::Request,
{
self.conn.send_request(req)
}
/// Delegate for [`RawConnection::send_and_check_request`]
pub fn send_and_check_request<R>(&self, req: &R) -> Result<()>
where
R: xcb::RequestWithoutReply,
{
self.conn.send_and_check_request(req).map_err(Into::into)
}
/// Delegate for [`RawConnection::flush`]
pub fn flush(&self) -> Result<()> {
self.conn.flush().map_err(Into::into)
}
/// Delegate for [`RawConnection::wait_for_event`]
pub fn wait_for_event(&self) -> Result<xcb::Event> {
self.conn.wait_for_event().map_err(Into::into)
}
/// Delegate for [`RawConnection::active_extensions`]
pub fn active_extensions(&self) -> impl Iterator<Item = xcb::Extension> + '_ {
self.conn.active_extensions()
}
/// Delegate for [`RawConnection::wait_for_reply`]
pub fn wait_for_reply<C>(&self, cookie: C) -> xcb::Result<C::Reply>
where
C: xcb::CookieWithReplyChecked,
{
self.conn.wait_for_reply(cookie)
}
/// Delegate for [`RawConnection::get_setup`]
pub fn get_setup(&self) -> &x::Setup {
self.conn.get_setup()
}
/// The root window
pub const fn root(&self) -> Window {
self.root
}
/// The 'screen' number on the X server. Note this isn’t what you think it is on multi-monitor setups
pub const fn screen_num(&self) -> usize {
self.screen_num
}
}
impl Drop for Connection<'_> {
fn drop(&mut self) {
self.send_request(&DestroyWindow {
window: self.check_window,
});
// Unset attributes of root window
self.conn.send_request(&DeleteProperty {
window: self.root,
property: self.atoms.net_wm_check,
});
self.conn.send_request(&DeleteProperty {
window: self.root,
property: self.atoms.net_client_list,
});
self.conn.send_request(&DeleteProperty {
window: self.root,
property: self.atoms.net_supported,
});
self.conn.send_request(&ChangeWindowAttributes {
window: self.root,
value_list: &[x::Cw::EventMask(x::EventMask::NO_EVENT)],
});
// Safety: These methods only require that the object is not used again, which will be true since we're in the destructor.
unsafe {
self.cursors.free(self.conn);
self.colours.free(self.conn);
}
}
}
|