forked from YaLTeR/niri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rs
536 lines (476 loc) · 15.8 KB
/
client.rs
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
use std::cmp::min;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt::Write as _;
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use std::{env, fmt};
use calloop::EventLoop;
use calloop_wayland_source::WaylandSource;
use single_pixel_buffer::v1::client::wp_single_pixel_buffer_manager_v1::WpSinglePixelBufferManagerV1;
use smithay::reexports::wayland_protocols::wp::single_pixel_buffer;
use smithay::reexports::wayland_protocols::wp::viewporter::client::wp_viewport::WpViewport;
use smithay::reexports::wayland_protocols::wp::viewporter::client::wp_viewporter::WpViewporter;
use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_surface::{self, XdgSurface};
use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_toplevel::{self, XdgToplevel};
use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_wm_base::{self, XdgWmBase};
use wayland_backend::client::Backend;
use wayland_client::globals::Global;
use wayland_client::protocol::wl_buffer::{self, WlBuffer};
use wayland_client::protocol::wl_callback::{self, WlCallback};
use wayland_client::protocol::wl_compositor::WlCompositor;
use wayland_client::protocol::wl_display::WlDisplay;
use wayland_client::protocol::wl_output::{self, WlOutput};
use wayland_client::protocol::wl_registry::{self, WlRegistry};
use wayland_client::protocol::wl_surface::{self, WlSurface};
use wayland_client::{Connection, Dispatch, Proxy as _, QueueHandle};
use crate::utils::id::IdCounter;
pub struct Client {
pub id: ClientId,
pub event_loop: EventLoop<'static, State>,
pub connection: Connection,
pub qh: QueueHandle<State>,
pub display: WlDisplay,
pub state: State,
}
pub struct State {
pub qh: QueueHandle<State>,
pub globals: Vec<Global>,
pub outputs: HashMap<WlOutput, String>,
pub compositor: Option<WlCompositor>,
pub xdg_wm_base: Option<XdgWmBase>,
pub spbm: Option<WpSinglePixelBufferManagerV1>,
pub viewporter: Option<WpViewporter>,
pub windows: Vec<Window>,
}
pub struct Window {
pub qh: QueueHandle<State>,
pub spbm: WpSinglePixelBufferManagerV1,
pub surface: WlSurface,
pub xdg_surface: XdgSurface,
pub xdg_toplevel: XdgToplevel,
pub viewport: WpViewport,
pub pending_configure: Configure,
pub configures_received: Vec<(u32, Configure)>,
pub close_requsted: bool,
pub configures_looked_at: usize,
}
#[derive(Debug, Clone, Default)]
pub struct Configure {
pub size: (i32, i32),
pub bounds: Option<(i32, i32)>,
pub states: Vec<xdg_toplevel::State>,
}
#[derive(Default)]
pub struct SyncData {
pub done: AtomicBool,
}
static CLIENT_ID_COUNTER: IdCounter = IdCounter::new();
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClientId(u64);
impl ClientId {
fn next() -> ClientId {
ClientId(CLIENT_ID_COUNTER.next())
}
}
impl fmt::Display for Configure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "size: {} × {}, ", self.size.0, self.size.1)?;
if let Some(bounds) = self.bounds {
write!(f, "bounds: {} × {}, ", bounds.0, bounds.1)?;
} else {
write!(f, "bounds: none, ")?;
}
write!(f, "states: {:?}", self.states)?;
Ok(())
}
}
fn connect(socket_name: &OsStr) -> Connection {
let mut socket_path = PathBuf::from(env::var_os("XDG_RUNTIME_DIR").unwrap());
socket_path.push(socket_name);
let stream = UnixStream::connect(socket_path).unwrap();
let backend = Backend::connect(stream).unwrap();
Connection::from_backend(backend)
}
impl Client {
pub fn new(socket_name: &OsStr) -> Self {
let id = ClientId::next();
let event_loop = EventLoop::try_new().unwrap();
let connection = connect(socket_name);
let queue = connection.new_event_queue();
let qh = queue.handle();
WaylandSource::new(connection.clone(), queue)
.insert(event_loop.handle())
.unwrap();
let display = connection.display();
let _registry = display.get_registry(&qh, ());
connection.flush().unwrap();
let state = State {
qh: qh.clone(),
globals: Vec::new(),
outputs: HashMap::new(),
compositor: None,
xdg_wm_base: None,
spbm: None,
viewporter: None,
windows: Vec::new(),
};
Self {
id,
event_loop,
connection,
qh,
display,
state,
}
}
pub fn dispatch(&mut self) {
self.event_loop
.dispatch(Duration::ZERO, &mut self.state)
.unwrap();
}
pub fn send_sync(&self) -> Arc<SyncData> {
let data = Arc::new(SyncData::default());
self.display.sync(&self.qh, data.clone());
self.connection.flush().unwrap();
data
}
pub fn create_window(&mut self) -> &mut Window {
self.state.create_window()
}
pub fn window(&mut self, surface: &WlSurface) -> &mut Window {
self.state.window(surface)
}
pub fn output(&mut self, name: &str) -> WlOutput {
self.state
.outputs
.iter()
.find(|(_, v)| *v == name)
.unwrap()
.0
.clone()
}
}
impl State {
pub fn create_window(&mut self) -> &mut Window {
let compositor = self.compositor.as_ref().unwrap();
let xdg_wm_base = self.xdg_wm_base.as_ref().unwrap();
let viewporter = self.viewporter.as_ref().unwrap();
let surface = compositor.create_surface(&self.qh, ());
let xdg_surface = xdg_wm_base.get_xdg_surface(&surface, &self.qh, ());
let xdg_toplevel = xdg_surface.get_toplevel(&self.qh, ());
let viewport = viewporter.get_viewport(&surface, &self.qh, ());
let window = Window {
qh: self.qh.clone(),
spbm: self.spbm.clone().unwrap(),
surface,
xdg_surface,
xdg_toplevel,
viewport,
pending_configure: Configure::default(),
configures_received: Vec::new(),
close_requsted: false,
configures_looked_at: 0,
};
self.windows.push(window);
self.windows.last_mut().unwrap()
}
pub fn window(&mut self, surface: &WlSurface) -> &mut Window {
self.windows
.iter_mut()
.find(|w| w.surface == *surface)
.unwrap()
}
}
impl Window {
pub fn commit(&self) {
self.surface.commit();
}
pub fn ack_last(&self) {
let serial = self.configures_received.last().unwrap().0;
self.xdg_surface.ack_configure(serial);
}
pub fn ack_last_and_commit(&self) {
self.ack_last();
self.commit();
}
pub fn attach_new_buffer(&self) {
let buffer = self.spbm.create_u32_rgba_buffer(0, 0, 0, 0, &self.qh, ());
self.surface.attach(Some(&buffer), 0, 0);
}
pub fn set_size(&self, w: u16, h: u16) {
self.viewport.set_destination(i32::from(w), i32::from(h));
}
pub fn set_fullscreen(&self, output: Option<&WlOutput>) {
self.xdg_toplevel.set_fullscreen(output);
}
pub fn unset_fullscreen(&self) {
self.xdg_toplevel.unset_fullscreen();
}
pub fn set_parent(&self, parent: Option<&XdgToplevel>) {
self.xdg_toplevel.set_parent(parent);
}
pub fn set_title(&self, title: &str) {
self.xdg_toplevel.set_title(title.to_owned());
}
pub fn recent_configures(&mut self) -> impl Iterator<Item = &Configure> {
let start = self.configures_looked_at;
self.configures_looked_at = self.configures_received.len();
self.configures_received[start..].iter().map(|(_, c)| c)
}
pub fn format_recent_configures(&mut self) -> String {
let mut buf = String::new();
for configure in self.recent_configures() {
if !buf.is_empty() {
buf.push('\n');
}
write!(buf, "{configure}").unwrap();
}
buf
}
}
impl Dispatch<WlCallback, Arc<SyncData>> for State {
fn event(
_state: &mut Self,
_proxy: &WlCallback,
event: <WlCallback as wayland_client::Proxy>::Event,
data: &Arc<SyncData>,
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
match event {
wl_callback::Event::Done { .. } => data.done.store(true, Ordering::Relaxed),
_ => unreachable!(),
}
}
}
impl Dispatch<WlRegistry, ()> for State {
fn event(
state: &mut Self,
registry: &WlRegistry,
event: <WlRegistry as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
qh: &QueueHandle<Self>,
) {
match event {
wl_registry::Event::Global {
name,
interface,
version,
} => {
if interface == WlCompositor::interface().name {
let version = min(version, WlCompositor::interface().version);
state.compositor = Some(registry.bind(name, version, qh, ()));
} else if interface == XdgWmBase::interface().name {
let version = min(version, XdgWmBase::interface().version);
state.xdg_wm_base = Some(registry.bind(name, version, qh, ()));
} else if interface == WpSinglePixelBufferManagerV1::interface().name {
let version = min(version, WpSinglePixelBufferManagerV1::interface().version);
state.spbm = Some(registry.bind(name, version, qh, ()));
} else if interface == WpViewporter::interface().name {
let version = min(version, WpViewporter::interface().version);
state.viewporter = Some(registry.bind(name, version, qh, ()));
} else if interface == WlOutput::interface().name {
let version = min(version, WlOutput::interface().version);
let output = registry.bind(name, version, qh, ());
state.outputs.insert(output, String::new());
}
let global = Global {
name,
interface,
version,
};
state.globals.push(global);
}
wl_registry::Event::GlobalRemove { .. } => (),
_ => unreachable!(),
}
}
}
impl Dispatch<WlOutput, ()> for State {
fn event(
state: &mut Self,
output: &WlOutput,
event: <WlOutput as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
match event {
wl_output::Event::Geometry { .. } => (),
wl_output::Event::Mode { .. } => (),
wl_output::Event::Done => (),
wl_output::Event::Scale { .. } => (),
wl_output::Event::Name { name } => {
*state.outputs.get_mut(output).unwrap() = name;
}
wl_output::Event::Description { .. } => (),
_ => unreachable!(),
}
}
}
impl Dispatch<WlCompositor, ()> for State {
fn event(
_state: &mut Self,
_proxy: &WlCompositor,
_event: <WlCompositor as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
unreachable!()
}
}
impl Dispatch<XdgWmBase, ()> for State {
fn event(
_state: &mut Self,
xdg_wm_base: &XdgWmBase,
event: <XdgWmBase as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
match event {
xdg_wm_base::Event::Ping { serial } => {
xdg_wm_base.pong(serial);
}
_ => unreachable!(),
}
}
}
impl Dispatch<WlSurface, ()> for State {
fn event(
_state: &mut Self,
_proxy: &WlSurface,
event: <WlSurface as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
match event {
wl_surface::Event::Enter { .. } => (),
wl_surface::Event::Leave { .. } => (),
wl_surface::Event::PreferredBufferScale { .. } => (),
wl_surface::Event::PreferredBufferTransform { .. } => (),
_ => unreachable!(),
}
}
}
impl Dispatch<XdgSurface, ()> for State {
fn event(
state: &mut Self,
xdg_surface: &XdgSurface,
event: <XdgSurface as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
match event {
xdg_surface::Event::Configure { serial } => {
let window = state
.windows
.iter_mut()
.find(|w| w.xdg_surface == *xdg_surface)
.unwrap();
let configure = window.pending_configure.clone();
window.configures_received.push((serial, configure));
}
_ => unreachable!(),
}
}
}
impl Dispatch<XdgToplevel, ()> for State {
fn event(
state: &mut Self,
xdg_toplevel: &XdgToplevel,
event: <XdgToplevel as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
let window = state
.windows
.iter_mut()
.find(|w| w.xdg_toplevel == *xdg_toplevel)
.unwrap();
match event {
xdg_toplevel::Event::Configure {
width,
height,
states,
} => {
let configure = &mut window.pending_configure;
configure.size = (width, height);
configure.states = states
.chunks_exact(4)
.flat_map(TryInto::<[u8; 4]>::try_into)
.map(u32::from_ne_bytes)
.flat_map(xdg_toplevel::State::try_from)
.collect();
}
xdg_toplevel::Event::Close => {
window.close_requsted = true;
}
xdg_toplevel::Event::ConfigureBounds { width, height } => {
window.pending_configure.bounds = Some((width, height));
}
xdg_toplevel::Event::WmCapabilities { .. } => (),
_ => unreachable!(),
}
}
}
impl Dispatch<WlBuffer, ()> for State {
fn event(
_state: &mut Self,
_proxy: &WlBuffer,
event: <WlBuffer as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
match event {
wl_buffer::Event::Release => (),
_ => unreachable!(),
}
}
}
impl Dispatch<WpSinglePixelBufferManagerV1, ()> for State {
fn event(
_state: &mut Self,
_proxy: &WpSinglePixelBufferManagerV1,
_event: <WpSinglePixelBufferManagerV1 as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
unreachable!()
}
}
impl Dispatch<WpViewporter, ()> for State {
fn event(
_state: &mut Self,
_proxy: &WpViewporter,
_event: <WpViewporter as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
unreachable!()
}
}
impl Dispatch<WpViewport, ()> for State {
fn event(
_state: &mut Self,
_proxy: &WpViewport,
_event: <WpViewport as wayland_client::Proxy>::Event,
_data: &(),
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
unreachable!()
}
}