niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use std::cmp::min; |
| 2 | use std::collections::HashMap; |
| 3 | use std::fmt; |
| 4 | use std::fmt::Write as _; |
| 5 | use std::os::unix::net::UnixStream; |
| 6 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 7 | use std::sync::Arc; |
| 8 | use std::time::Duration; |
| 9 | |
| 10 | use calloop::EventLoop; |
| 11 | use calloop_wayland_source::WaylandSource; |
| 12 | use single_pixel_buffer::v1::client::wp_single_pixel_buffer_manager_v1::WpSinglePixelBufferManagerV1; |
| 13 | use smithay::reexports::wayland_protocols::wp::keyboard_shortcuts_inhibit::zv1::client::zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1; |
| 14 | use smithay::reexports::wayland_protocols::wp::keyboard_shortcuts_inhibit::zv1::client::zwp_keyboard_shortcuts_inhibitor_v1::{self, ZwpKeyboardShortcutsInhibitorV1}; |
| 15 | use smithay::reexports::wayland_protocols::wp::single_pixel_buffer; |
| 16 | use smithay::reexports::wayland_protocols::wp::viewporter::client::wp_viewport::WpViewport; |
| 17 | use smithay::reexports::wayland_protocols::wp::viewporter::client::wp_viewporter::WpViewporter; |
| 18 | use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_surface::{self, XdgSurface}; |
| 19 | use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_toplevel::{self, XdgToplevel}; |
| 20 | use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_wm_base::{self, XdgWmBase}; |
| 21 | use smithay::reexports::wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_shell_v1::{ |
| 22 | self, ZwlrLayerShellV1, |
| 23 | }; |
| 24 | use smithay::reexports::wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1::{ |
| 25 | self, ZwlrLayerSurfaceV1, |
| 26 | }; |
| 27 | use smithay::reexports::wayland_protocols_wlr::virtual_pointer::v1::client::zwlr_virtual_pointer_manager_v1::ZwlrVirtualPointerManagerV1; |
| 28 | use smithay::reexports::wayland_protocols_wlr::virtual_pointer::v1::client::zwlr_virtual_pointer_v1::ZwlrVirtualPointerV1; |
| 29 | use wayland_backend::client::Backend; |
| 30 | use wayland_client::globals::Global; |
| 31 | use wayland_client::protocol::wl_buffer::{self, WlBuffer}; |
| 32 | use wayland_client::protocol::wl_callback::{self, WlCallback}; |
| 33 | use wayland_client::protocol::wl_compositor::WlCompositor; |
| 34 | use wayland_client::protocol::wl_display::WlDisplay; |
| 35 | use wayland_client::protocol::wl_keyboard::{self, WlKeyboard}; |
| 36 | use wayland_client::protocol::wl_output::{self, WlOutput}; |
| 37 | use wayland_client::protocol::wl_registry::{self, WlRegistry}; |
| 38 | use wayland_client::protocol::wl_seat::{self, WlSeat}; |
| 39 | use wayland_client::protocol::wl_surface::{self, WlSurface}; |
| 40 | use wayland_client::{Connection, Dispatch, Proxy as _, QueueHandle}; |
| 41 | |
| 42 | use crate::utils::id::IdCounter; |
| 43 | |
| 44 | pub struct Client { |
| 45 | pub id: ClientId, |
| 46 | pub event_loop: EventLoop<'static, State>, |
| 47 | pub connection: Connection, |
| 48 | pub qh: QueueHandle<State>, |
| 49 | pub display: WlDisplay, |
| 50 | pub state: State, |
| 51 | } |
| 52 | |
| 53 | pub struct State { |
| 54 | pub qh: QueueHandle<State>, |
| 55 | |
| 56 | pub globals: Vec<Global>, |
| 57 | pub outputs: HashMap<WlOutput, String>, |
| 58 | pub surfaces: HashMap<WlSurface, Surface>, |
| 59 | pub seats: HashMap<WlSeat, Seat>, |
| 60 | |
| 61 | pub compositor: Option<WlCompositor>, |
| 62 | pub xdg_wm_base: Option<XdgWmBase>, |
| 63 | pub layer_shell: Option<ZwlrLayerShellV1>, |
| 64 | pub virtual_pointer_manager: Option<ZwlrVirtualPointerManagerV1>, |
| 65 | pub virtual_pointer: Option<ZwlrVirtualPointerV1>, |
| 66 | pub spbm: Option<WpSinglePixelBufferManagerV1>, |
| 67 | pub viewporter: Option<WpViewporter>, |
| 68 | pub ksim: Option<ZwpKeyboardShortcutsInhibitManagerV1>, |
| 69 | |
| 70 | pub windows: Vec<Window>, |
| 71 | pub layers: Vec<LayerSurface>, |
| 72 | } |
| 73 | |
| 74 | pub struct Window { |
| 75 | pub qh: QueueHandle<State>, |
| 76 | pub spbm: WpSinglePixelBufferManagerV1, |
| 77 | |
| 78 | pub surface: WlSurface, |
| 79 | pub xdg_surface: XdgSurface, |
| 80 | pub xdg_toplevel: XdgToplevel, |
| 81 | pub viewport: WpViewport, |
| 82 | pub pending_configure: Configure, |
| 83 | pub configures_received: Vec<(u32, Configure)>, |
| 84 | pub close_requested: bool, |
| 85 | |
| 86 | pub configures_looked_at: usize, |
| 87 | } |
| 88 | |
| 89 | pub struct LayerSurface { |
| 90 | pub qh: QueueHandle<State>, |
| 91 | pub spbm: WpSinglePixelBufferManagerV1, |
| 92 | |
| 93 | pub surface: WlSurface, |
| 94 | pub layer_surface: ZwlrLayerSurfaceV1, |
| 95 | pub viewport: WpViewport, |
| 96 | pub configures_received: Vec<(u32, LayerConfigure)>, |
| 97 | pub close_requested: bool, |
| 98 | |
| 99 | pub configures_looked_at: usize, |
| 100 | } |
| 101 | |
| 102 | #[derive(Debug, Default)] |
| 103 | pub struct Seat { |
| 104 | pub keyboard: Option<Keyboard>, |
| 105 | } |
| 106 | |
| 107 | #[derive(Debug)] |
| 108 | pub struct Surface { |
| 109 | pub keyboard_events_received: Vec<KeyboardEvent>, |
| 110 | pub keyboard_events_looked_at: usize, |
| 111 | } |
| 112 | |
| 113 | #[derive(Debug)] |
| 114 | pub struct Keyboard { |
| 115 | pub proxy: WlKeyboard, |
| 116 | pub surface: Option<WlSurface>, |
| 117 | } |
| 118 | |
| 119 | #[derive(Debug)] |
| 120 | pub enum KeyboardEvent { |
| 121 | Enter { |
| 122 | keys: Vec<u32>, |
| 123 | }, |
| 124 | Leave, |
| 125 | Key { |
| 126 | key: u32, |
| 127 | state: wl_keyboard::KeyState, |
| 128 | }, |
| 129 | Modifiers { |
| 130 | depressed: u32, |
| 131 | latched: u32, |
| 132 | locked: u32, |
| 133 | group: u32, |
| 134 | }, |
| 135 | } |
| 136 | |
| 137 | #[derive(Debug, Clone, Default)] |
| 138 | pub struct Configure { |
| 139 | pub size: (i32, i32), |
| 140 | pub bounds: Option<(i32, i32)>, |
| 141 | pub states: Vec<xdg_toplevel::State>, |
| 142 | } |
| 143 | |
| 144 | #[derive(Debug, Clone, Copy)] |
| 145 | pub struct LayerConfigure { |
| 146 | pub size: (u32, u32), |
| 147 | } |
| 148 | |
| 149 | #[derive(Clone, Copy, Default)] |
| 150 | pub struct LayerMargin { |
| 151 | pub top: i32, |
| 152 | pub right: i32, |
| 153 | pub bottom: i32, |
| 154 | pub left: i32, |
| 155 | } |
| 156 | |
| 157 | #[derive(Clone, Copy, Default)] |
| 158 | pub struct LayerConfigureProps { |
| 159 | pub size: Option<(u32, u32)>, |
| 160 | pub anchor: Option<zwlr_layer_surface_v1::Anchor>, |
| 161 | pub exclusive_zone: Option<i32>, |
| 162 | pub margin: Option<LayerMargin>, |
| 163 | pub kb_interactivity: Option<zwlr_layer_surface_v1::KeyboardInteractivity>, |
| 164 | pub layer: Option<zwlr_layer_shell_v1::Layer>, |
| 165 | pub exclusive_edge: Option<zwlr_layer_surface_v1::Anchor>, |
| 166 | } |
| 167 | |
| 168 | #[derive(Default)] |
| 169 | pub struct SyncData { |
| 170 | pub done: AtomicBool, |
| 171 | } |
| 172 | |
| 173 | static CLIENT_ID_COUNTER: IdCounter = IdCounter::new(); |
| 174 | |
| 175 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 176 | pub struct ClientId(u64); |
| 177 | |
| 178 | impl ClientId { |
| 179 | fn next() -> ClientId { |
| 180 | ClientId(CLIENT_ID_COUNTER.next()) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | impl fmt::Display for KeyboardEvent { |
| 185 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 186 | match self { |
| 187 | KeyboardEvent::Enter { keys } => { |
| 188 | write!(f, "enter: {keys:?}")?; |
| 189 | } |
| 190 | KeyboardEvent::Leave => { |
| 191 | write!(f, "leave")?; |
| 192 | } |
| 193 | KeyboardEvent::Key { key, state } => { |
| 194 | let action = match state { |
| 195 | wl_keyboard::KeyState::Released => "released", |
| 196 | wl_keyboard::KeyState::Pressed => "pressed", |
| 197 | wl_keyboard::KeyState::Repeated => "repeated", |
| 198 | _ => unreachable!(), |
| 199 | }; |
| 200 | write!(f, "key {action}: {key}")?; |
| 201 | } |
| 202 | KeyboardEvent::Modifiers { |
| 203 | depressed, |
| 204 | latched, |
| 205 | locked, |
| 206 | group, |
| 207 | } => { |
| 208 | write!( |
| 209 | f, |
| 210 | "modifiers: depressed={depressed}, latched={latched}, \ |
| 211 | locked={locked}, group={group}" |
| 212 | )?; |
| 213 | } |
| 214 | } |
| 215 | Ok(()) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | impl fmt::Display for Configure { |
| 220 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 221 | write!(f, "size: {} × {}, ", self.size.0, self.size.1)?; |
| 222 | if let Some(bounds) = self.bounds { |
| 223 | write!(f, "bounds: {} × {}, ", bounds.0, bounds.1)?; |
| 224 | } else { |
| 225 | write!(f, "bounds: none, ")?; |
| 226 | } |
| 227 | write!(f, "states: {:?}", self.states)?; |
| 228 | Ok(()) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | impl fmt::Display for LayerConfigure { |
| 233 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 234 | write!(f, "size: {} × {}", self.size.0, self.size.1)?; |
| 235 | Ok(()) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | impl Client { |
| 240 | pub fn new(stream: UnixStream) -> Self { |
| 241 | let id = ClientId::next(); |
| 242 | |
| 243 | let event_loop = EventLoop::try_new().unwrap(); |
| 244 | let backend = Backend::connect(stream).unwrap(); |
| 245 | let connection = Connection::from_backend(backend); |
| 246 | let queue = connection.new_event_queue(); |
| 247 | let qh = queue.handle(); |
| 248 | WaylandSource::new(connection.clone(), queue) |
| 249 | .insert(event_loop.handle()) |
| 250 | .unwrap(); |
| 251 | |
| 252 | let display = connection.display(); |
| 253 | let _registry = display.get_registry(&qh, ()); |
| 254 | connection.flush().unwrap(); |
| 255 | |
| 256 | let state = State { |
| 257 | qh: qh.clone(), |
| 258 | globals: Vec::new(), |
| 259 | outputs: HashMap::new(), |
| 260 | surfaces: HashMap::new(), |
| 261 | seats: HashMap::new(), |
| 262 | compositor: None, |
| 263 | xdg_wm_base: None, |
| 264 | layer_shell: None, |
| 265 | virtual_pointer_manager: None, |
| 266 | virtual_pointer: None, |
| 267 | spbm: None, |
| 268 | viewporter: None, |
| 269 | ksim: None, |
| 270 | windows: Vec::new(), |
| 271 | layers: Vec::new(), |
| 272 | }; |
| 273 | |
| 274 | Self { |
| 275 | id, |
| 276 | event_loop, |
| 277 | connection, |
| 278 | qh, |
| 279 | display, |
| 280 | state, |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | pub fn dispatch(&mut self) { |
| 285 | self.event_loop |
| 286 | .dispatch(Duration::ZERO, &mut self.state) |
| 287 | .unwrap(); |
| 288 | |
| 289 | if let Some(error) = self.connection.protocol_error() { |
| 290 | panic!("{error}"); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | pub fn send_sync(&self) -> Arc<SyncData> { |
| 295 | let data = Arc::new(SyncData::default()); |
| 296 | self.display.sync(&self.qh, data.clone()); |
| 297 | self.connection.flush().unwrap(); |
| 298 | data |
| 299 | } |
| 300 | |
| 301 | pub fn create_window(&mut self) -> &mut Window { |
| 302 | self.state.create_window() |
| 303 | } |
| 304 | |
| 305 | pub fn window(&mut self, surface: &WlSurface) -> &mut Window { |
| 306 | self.state.window(surface) |
| 307 | } |
| 308 | |
| 309 | pub fn create_layer( |
| 310 | &mut self, |
| 311 | output: Option<&WlOutput>, |
| 312 | layer: zwlr_layer_shell_v1::Layer, |
| 313 | namespace: &str, |
| 314 | ) -> &mut LayerSurface { |
| 315 | self.state.create_layer(output, layer, namespace.to_owned()) |
| 316 | } |
| 317 | |
| 318 | pub fn layer(&mut self, surface: &WlSurface) -> &mut LayerSurface { |
| 319 | self.state.layer(surface) |
| 320 | } |
| 321 | |
| 322 | pub fn output(&mut self, name: &str) -> WlOutput { |
| 323 | self.state |
| 324 | .outputs |
| 325 | .iter() |
| 326 | .find(|(_, v)| *v == name) |
| 327 | .unwrap() |
| 328 | .0 |
| 329 | .clone() |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | impl State { |
| 334 | pub fn create_surface(&mut self) -> WlSurface { |
| 335 | let compositor = self.compositor.as_ref().unwrap(); |
| 336 | let surface = compositor.create_surface(&self.qh, ()); |
| 337 | self.surfaces.insert( |
| 338 | surface.clone(), |
| 339 | Surface { |
| 340 | keyboard_events_received: Vec::new(), |
| 341 | keyboard_events_looked_at: 0, |
| 342 | }, |
| 343 | ); |
| 344 | surface |
| 345 | } |
| 346 | |
| 347 | pub fn create_window(&mut self) -> &mut Window { |
| 348 | let surface = self.create_surface(); |
| 349 | |
| 350 | let xdg_wm_base = self.xdg_wm_base.as_ref().unwrap(); |
| 351 | let viewporter = self.viewporter.as_ref().unwrap(); |
| 352 | |
| 353 | let xdg_surface = xdg_wm_base.get_xdg_surface(&surface, &self.qh, ()); |
| 354 | let xdg_toplevel = xdg_surface.get_toplevel(&self.qh, ()); |
| 355 | let viewport = viewporter.get_viewport(&surface, &self.qh, ()); |
| 356 | |
| 357 | let window = Window { |
| 358 | qh: self.qh.clone(), |
| 359 | spbm: self.spbm.clone().unwrap(), |
| 360 | |
| 361 | surface, |
| 362 | xdg_surface, |
| 363 | xdg_toplevel, |
| 364 | viewport, |
| 365 | pending_configure: Configure::default(), |
| 366 | configures_received: Vec::new(), |
| 367 | close_requested: false, |
| 368 | |
| 369 | configures_looked_at: 0, |
| 370 | }; |
| 371 | |
| 372 | self.windows.push(window); |
| 373 | self.windows.last_mut().unwrap() |
| 374 | } |
| 375 | |
| 376 | pub fn window(&mut self, surface: &WlSurface) -> &mut Window { |
| 377 | self.windows |
| 378 | .iter_mut() |
| 379 | .find(|w| w.surface == *surface) |
| 380 | .unwrap() |
| 381 | } |
| 382 | |
| 383 | pub fn create_layer( |
| 384 | &mut self, |
| 385 | output: Option<&WlOutput>, |
| 386 | layer: zwlr_layer_shell_v1::Layer, |
| 387 | namespace: String, |
| 388 | ) -> &mut LayerSurface { |
| 389 | let surface = self.create_surface(); |
| 390 | |
| 391 | let layer_shell = self.layer_shell.as_ref().unwrap(); |
| 392 | let viewporter = self.viewporter.as_ref().unwrap(); |
| 393 | |
| 394 | let layer_surface = |
| 395 | layer_shell.get_layer_surface(&surface, output, layer, namespace, &self.qh, ()); |
| 396 | let viewport = viewporter.get_viewport(&surface, &self.qh, ()); |
| 397 | |
| 398 | let layer_surface = LayerSurface { |
| 399 | qh: self.qh.clone(), |
| 400 | spbm: self.spbm.clone().unwrap(), |
| 401 | |
| 402 | surface, |
| 403 | layer_surface, |
| 404 | viewport, |
| 405 | configures_received: Vec::new(), |
| 406 | close_requested: false, |
| 407 | |
| 408 | configures_looked_at: 0, |
| 409 | }; |
| 410 | |
| 411 | self.layers.push(layer_surface); |
| 412 | self.layers.last_mut().unwrap() |
| 413 | } |
| 414 | |
| 415 | pub fn layer(&mut self, surface: &WlSurface) -> &mut LayerSurface { |
| 416 | self.layers |
| 417 | .iter_mut() |
| 418 | .find(|w| w.surface == *surface) |
| 419 | .unwrap() |
| 420 | } |
| 421 | |
| 422 | pub fn recent_keyboard_events( |
| 423 | &mut self, |
| 424 | surface: &WlSurface, |
| 425 | ) -> impl Iterator<Item = &KeyboardEvent> + '_ { |
| 426 | let surface = self.surfaces.get_mut(surface).unwrap(); |
| 427 | surface.recent_keyboard_events() |
| 428 | } |
| 429 | |
| 430 | pub fn inhibit_shortcuts(&self, surface: &WlSurface) -> ZwpKeyboardShortcutsInhibitorV1 { |
| 431 | let ksim = self.ksim.as_ref().unwrap(); |
| 432 | let seat = self.seats.keys().next().unwrap(); |
| 433 | ksim.inhibit_shortcuts(surface, seat, &self.qh, ()) |
| 434 | } |
| 435 | |
| 436 | pub fn virtual_pointer(&mut self) -> ZwlrVirtualPointerV1 { |
| 437 | if let Some(pointer) = &self.virtual_pointer { |
| 438 | return pointer.clone(); |
| 439 | } |
| 440 | |
| 441 | let manager = self.virtual_pointer_manager.as_ref().unwrap(); |
| 442 | let pointer = manager.create_virtual_pointer(None, &self.qh, ()); |
| 443 | self.virtual_pointer = Some(pointer.clone()); |
| 444 | pointer |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | impl Window { |
| 449 | pub fn commit(&self) { |
| 450 | self.surface.commit(); |
| 451 | } |
| 452 | |
| 453 | pub fn ack_last(&self) { |
| 454 | let serial = self.configures_received.last().unwrap().0; |
| 455 | self.xdg_surface.ack_configure(serial); |
| 456 | } |
| 457 | |
| 458 | pub fn ack_last_and_commit(&self) { |
| 459 | self.ack_last(); |
| 460 | self.commit(); |
| 461 | } |
| 462 | |
| 463 | pub fn attach_new_buffer(&self) { |
| 464 | let buffer = self.spbm.create_u32_rgba_buffer(0, 0, 0, 0, &self.qh, ()); |
| 465 | self.surface.attach(Some(&buffer), 0, 0); |
| 466 | } |
| 467 | |
| 468 | pub fn attach_null(&self) { |
| 469 | self.surface.attach(None, 0, 0); |
| 470 | } |
| 471 | |
| 472 | pub fn set_size(&self, w: u16, h: u16) { |
| 473 | self.viewport.set_destination(i32::from(w), i32::from(h)); |
| 474 | } |
| 475 | |
| 476 | pub fn set_fullscreen(&self, output: Option<&WlOutput>) { |
| 477 | self.xdg_toplevel.set_fullscreen(output); |
| 478 | } |
| 479 | |
| 480 | pub fn unset_fullscreen(&self) { |
| 481 | self.xdg_toplevel.unset_fullscreen(); |
| 482 | } |
| 483 | |
| 484 | pub fn set_maximized(&self) { |
| 485 | self.xdg_toplevel.set_maximized(); |
| 486 | } |
| 487 | |
| 488 | pub fn unset_maximized(&self) { |
| 489 | self.xdg_toplevel.unset_maximized(); |
| 490 | } |
| 491 | |
| 492 | pub fn set_parent(&self, parent: Option<&XdgToplevel>) { |
| 493 | self.xdg_toplevel.set_parent(parent); |
| 494 | } |
| 495 | |
| 496 | pub fn set_title(&self, title: &str) { |
| 497 | self.xdg_toplevel.set_title(title.to_owned()); |
| 498 | } |
| 499 | |
| 500 | pub fn recent_configures(&mut self) -> impl Iterator<Item = &Configure> { |
| 501 | let start = self.configures_looked_at; |
| 502 | self.configures_looked_at = self.configures_received.len(); |
| 503 | self.configures_received[start..].iter().map(|(_, c)| c) |
| 504 | } |
| 505 | |
| 506 | pub fn format_recent_configures(&mut self) -> String { |
| 507 | let mut buf = String::new(); |
| 508 | for configure in self.recent_configures() { |
| 509 | if !buf.is_empty() { |
| 510 | buf.push('\n'); |
| 511 | } |
| 512 | write!(buf, "{configure}").unwrap(); |
| 513 | } |
| 514 | buf |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | impl LayerSurface { |
| 519 | pub fn commit(&self) { |
| 520 | self.surface.commit(); |
| 521 | } |
| 522 | |
| 523 | pub fn ack_last(&self) { |
| 524 | let serial = self.configures_received.last().unwrap().0; |
| 525 | self.layer_surface.ack_configure(serial); |
| 526 | } |
| 527 | |
| 528 | pub fn ack_last_and_commit(&self) { |
| 529 | self.ack_last(); |
| 530 | self.commit(); |
| 531 | } |
| 532 | |
| 533 | pub fn set_configure_props(&self, props: LayerConfigureProps) { |
| 534 | let LayerConfigureProps { |
| 535 | size, |
| 536 | anchor, |
| 537 | exclusive_zone, |
| 538 | margin, |
| 539 | kb_interactivity, |
| 540 | layer, |
| 541 | exclusive_edge, |
| 542 | } = props; |
| 543 | |
| 544 | if let Some(x) = size { |
| 545 | self.layer_surface.set_size(x.0, x.1); |
| 546 | } |
| 547 | if let Some(x) = anchor { |
| 548 | self.layer_surface.set_anchor(x); |
| 549 | } |
| 550 | if let Some(x) = exclusive_zone { |
| 551 | self.layer_surface.set_exclusive_zone(x); |
| 552 | } |
| 553 | if let Some(x) = margin { |
| 554 | self.layer_surface |
| 555 | .set_margin(x.top, x.right, x.bottom, x.left); |
| 556 | } |
| 557 | if let Some(x) = kb_interactivity { |
| 558 | self.layer_surface.set_keyboard_interactivity(x); |
| 559 | } |
| 560 | if let Some(x) = layer { |
| 561 | self.layer_surface.set_layer(x); |
| 562 | } |
| 563 | if let Some(x) = exclusive_edge { |
| 564 | self.layer_surface.set_exclusive_edge(x); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | pub fn attach_new_buffer(&self) { |
| 569 | let buffer = self.spbm.create_u32_rgba_buffer(0, 0, 0, 0, &self.qh, ()); |
| 570 | self.surface.attach(Some(&buffer), 0, 0); |
| 571 | } |
| 572 | |
| 573 | pub fn attach_null(&self) { |
| 574 | self.surface.attach(None, 0, 0); |
| 575 | } |
| 576 | |
| 577 | pub fn set_size(&self, w: u16, h: u16) { |
| 578 | self.viewport.set_destination(i32::from(w), i32::from(h)); |
| 579 | } |
| 580 | |
| 581 | pub fn recent_configures(&mut self) -> impl Iterator<Item = &LayerConfigure> { |
| 582 | let start = self.configures_looked_at; |
| 583 | self.configures_looked_at = self.configures_received.len(); |
| 584 | self.configures_received[start..].iter().map(|(_, c)| c) |
| 585 | } |
| 586 | |
| 587 | pub fn format_recent_configures(&mut self) -> String { |
| 588 | let mut buf = String::new(); |
| 589 | for configure in self.recent_configures() { |
| 590 | if !buf.is_empty() { |
| 591 | buf.push('\n'); |
| 592 | } |
| 593 | write!(buf, "{configure}").unwrap(); |
| 594 | } |
| 595 | buf |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | impl Surface { |
| 600 | pub fn recent_keyboard_events(&mut self) -> impl Iterator<Item = &KeyboardEvent> { |
| 601 | let start = self.keyboard_events_looked_at; |
| 602 | self.keyboard_events_looked_at = self.keyboard_events_received.len(); |
| 603 | self.keyboard_events_received[start..].iter() |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | impl Dispatch<WlCallback, Arc<SyncData>> for State { |
| 608 | fn event( |
| 609 | _state: &mut Self, |
| 610 | _proxy: &WlCallback, |
| 611 | event: <WlCallback as wayland_client::Proxy>::Event, |
| 612 | data: &Arc<SyncData>, |
| 613 | _conn: &Connection, |
| 614 | _qhandle: &QueueHandle<Self>, |
| 615 | ) { |
| 616 | match event { |
| 617 | wl_callback::Event::Done { .. } => data.done.store(true, Ordering::Relaxed), |
| 618 | _ => unreachable!(), |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | impl Dispatch<WlRegistry, ()> for State { |
| 624 | fn event( |
| 625 | state: &mut Self, |
| 626 | registry: &WlRegistry, |
| 627 | event: <WlRegistry as wayland_client::Proxy>::Event, |
| 628 | _data: &(), |
| 629 | _conn: &Connection, |
| 630 | qh: &QueueHandle<Self>, |
| 631 | ) { |
| 632 | match event { |
| 633 | wl_registry::Event::Global { |
| 634 | name, |
| 635 | interface, |
| 636 | version, |
| 637 | } => { |
| 638 | if interface == WlCompositor::interface().name { |
| 639 | let version = min(version, WlCompositor::interface().version); |
| 640 | state.compositor = Some(registry.bind(name, version, qh, ())); |
| 641 | } else if interface == XdgWmBase::interface().name { |
| 642 | let version = min(version, XdgWmBase::interface().version); |
| 643 | state.xdg_wm_base = Some(registry.bind(name, version, qh, ())); |
| 644 | } else if interface == ZwlrLayerShellV1::interface().name { |
| 645 | let version = min(version, ZwlrLayerShellV1::interface().version); |
| 646 | state.layer_shell = Some(registry.bind(name, version, qh, ())); |
| 647 | } else if interface == ZwlrVirtualPointerManagerV1::interface().name { |
| 648 | let version = min(version, ZwlrVirtualPointerManagerV1::interface().version); |
| 649 | state.virtual_pointer_manager = Some(registry.bind(name, version, qh, ())); |
| 650 | } else if interface == WpSinglePixelBufferManagerV1::interface().name { |
| 651 | let version = min(version, WpSinglePixelBufferManagerV1::interface().version); |
| 652 | state.spbm = Some(registry.bind(name, version, qh, ())); |
| 653 | } else if interface == WpViewporter::interface().name { |
| 654 | let version = min(version, WpViewporter::interface().version); |
| 655 | state.viewporter = Some(registry.bind(name, version, qh, ())); |
| 656 | } else if interface == WlOutput::interface().name { |
| 657 | let version = min(version, WlOutput::interface().version); |
| 658 | let output = registry.bind(name, version, qh, ()); |
| 659 | state.outputs.insert(output, String::new()); |
| 660 | } else if interface == WlSeat::interface().name { |
| 661 | let version = min(version, WlSeat::interface().version); |
| 662 | state |
| 663 | .seats |
| 664 | .insert(registry.bind(name, version, qh, ()), Seat::default()); |
| 665 | } else if interface == ZwpKeyboardShortcutsInhibitManagerV1::interface().name { |
| 666 | let version = min( |
| 667 | version, |
| 668 | ZwpKeyboardShortcutsInhibitManagerV1::interface().version, |
| 669 | ); |
| 670 | state.ksim = Some(registry.bind(name, version, qh, ())); |
| 671 | } |
| 672 | |
| 673 | let global = Global { |
| 674 | name, |
| 675 | interface, |
| 676 | version, |
| 677 | }; |
| 678 | state.globals.push(global); |
| 679 | } |
| 680 | wl_registry::Event::GlobalRemove { .. } => (), |
| 681 | _ => unreachable!(), |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | impl Dispatch<WlOutput, ()> for State { |
| 687 | fn event( |
| 688 | state: &mut Self, |
| 689 | output: &WlOutput, |
| 690 | event: <WlOutput as wayland_client::Proxy>::Event, |
| 691 | _data: &(), |
| 692 | _conn: &Connection, |
| 693 | _qhandle: &QueueHandle<Self>, |
| 694 | ) { |
| 695 | match event { |
| 696 | wl_output::Event::Geometry { .. } => (), |
| 697 | wl_output::Event::Mode { .. } => (), |
| 698 | wl_output::Event::Done => (), |
| 699 | wl_output::Event::Scale { .. } => (), |
| 700 | wl_output::Event::Name { name } => { |
| 701 | *state.outputs.get_mut(output).unwrap() = name; |
| 702 | } |
| 703 | wl_output::Event::Description { .. } => (), |
| 704 | _ => unreachable!(), |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | impl Dispatch<WlSeat, ()> for State { |
| 710 | fn event( |
| 711 | state: &mut Self, |
| 712 | seat: &WlSeat, |
| 713 | event: <WlSeat as wayland_client::Proxy>::Event, |
| 714 | _data: &(), |
| 715 | _conn: &Connection, |
| 716 | qh: &QueueHandle<Self>, |
| 717 | ) { |
| 718 | match event { |
| 719 | wl_seat::Event::Capabilities { capabilities } => { |
| 720 | let capabilities = capabilities.into_result().unwrap(); |
| 721 | let data = state.seats.get_mut(seat).unwrap(); |
| 722 | |
| 723 | if capabilities.contains(wl_seat::Capability::Keyboard) { |
| 724 | if data.keyboard.is_none() { |
| 725 | let keyboard = Keyboard { |
| 726 | proxy: seat.get_keyboard(qh, seat.clone()), |
| 727 | surface: None, |
| 728 | }; |
| 729 | data.keyboard = Some(keyboard); |
| 730 | } |
| 731 | } else { |
| 732 | if let Some(keyboard) = data.keyboard.take() { |
| 733 | keyboard.proxy.release(); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | wl_seat::Event::Name { .. } => (), |
| 738 | _ => unreachable!(), |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | impl Dispatch<WlKeyboard, WlSeat> for State { |
| 744 | fn event( |
| 745 | state: &mut Self, |
| 746 | _keyboard: &WlKeyboard, |
| 747 | event: <WlKeyboard as wayland_client::Proxy>::Event, |
| 748 | seat: &WlSeat, |
| 749 | _conn: &Connection, |
| 750 | _qhandle: &QueueHandle<Self>, |
| 751 | ) { |
| 752 | let seat = state.seats.get_mut(seat).unwrap(); |
| 753 | let keyboard = seat.keyboard.as_mut().unwrap(); |
| 754 | |
| 755 | match event { |
| 756 | wl_keyboard::Event::Keymap { .. } => (), |
| 757 | wl_keyboard::Event::Enter { surface, keys, .. } => { |
| 758 | keyboard.surface = Some(surface.clone()); |
| 759 | |
| 760 | // The order is arbitrary, but we need a fixed order for our tests. |
| 761 | let mut keys: Vec<u32> = bytemuck::pod_collect_to_vec(&keys); |
| 762 | keys.sort_unstable(); |
| 763 | |
| 764 | let event = KeyboardEvent::Enter { keys }; |
| 765 | let surface = state.surfaces.get_mut(&surface).unwrap(); |
| 766 | surface.keyboard_events_received.push(event); |
| 767 | } |
| 768 | wl_keyboard::Event::Leave { .. } => { |
| 769 | let surface = keyboard.surface.take().unwrap(); |
| 770 | |
| 771 | let event = KeyboardEvent::Leave; |
| 772 | let surface = state.surfaces.get_mut(&surface).unwrap(); |
| 773 | surface.keyboard_events_received.push(event); |
| 774 | } |
| 775 | wl_keyboard::Event::Key { |
| 776 | key, |
| 777 | state: key_state, |
| 778 | .. |
| 779 | } => { |
| 780 | let key_state = key_state.into_result().unwrap(); |
| 781 | let surface = keyboard.surface.clone().unwrap(); |
| 782 | |
| 783 | let event = KeyboardEvent::Key { |
| 784 | key, |
| 785 | state: key_state, |
| 786 | }; |
| 787 | let surface = state.surfaces.get_mut(&surface).unwrap(); |
| 788 | surface.keyboard_events_received.push(event); |
| 789 | } |
| 790 | wl_keyboard::Event::Modifiers { |
| 791 | mods_depressed, |
| 792 | mods_latched, |
| 793 | mods_locked, |
| 794 | group, |
| 795 | .. |
| 796 | } => { |
| 797 | let surface = keyboard.surface.clone().unwrap(); |
| 798 | |
| 799 | let event = KeyboardEvent::Modifiers { |
| 800 | depressed: mods_depressed, |
| 801 | latched: mods_latched, |
| 802 | locked: mods_locked, |
| 803 | group, |
| 804 | }; |
| 805 | let surface = state.surfaces.get_mut(&surface).unwrap(); |
| 806 | surface.keyboard_events_received.push(event); |
| 807 | } |
| 808 | wl_keyboard::Event::RepeatInfo { .. } => (), |
| 809 | _ => unreachable!(), |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | impl Dispatch<WlCompositor, ()> for State { |
| 815 | fn event( |
| 816 | _state: &mut Self, |
| 817 | _proxy: &WlCompositor, |
| 818 | _event: <WlCompositor as wayland_client::Proxy>::Event, |
| 819 | _data: &(), |
| 820 | _conn: &Connection, |
| 821 | _qhandle: &QueueHandle<Self>, |
| 822 | ) { |
| 823 | unreachable!() |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | impl Dispatch<XdgWmBase, ()> for State { |
| 828 | fn event( |
| 829 | _state: &mut Self, |
| 830 | xdg_wm_base: &XdgWmBase, |
| 831 | event: <XdgWmBase as wayland_client::Proxy>::Event, |
| 832 | _data: &(), |
| 833 | _conn: &Connection, |
| 834 | _qhandle: &QueueHandle<Self>, |
| 835 | ) { |
| 836 | match event { |
| 837 | xdg_wm_base::Event::Ping { serial } => { |
| 838 | xdg_wm_base.pong(serial); |
| 839 | } |
| 840 | _ => unreachable!(), |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | impl Dispatch<ZwlrLayerShellV1, ()> for State { |
| 846 | fn event( |
| 847 | _state: &mut Self, |
| 848 | _proxy: &ZwlrLayerShellV1, |
| 849 | _event: <ZwlrLayerShellV1 as wayland_client::Proxy>::Event, |
| 850 | _data: &(), |
| 851 | _conn: &Connection, |
| 852 | _qhandle: &QueueHandle<Self>, |
| 853 | ) { |
| 854 | unreachable!() |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | wayland_client::delegate_noop!(State: ZwlrVirtualPointerManagerV1); |
| 859 | wayland_client::delegate_noop!(State: ZwlrVirtualPointerV1); |
| 860 | |
| 861 | impl Dispatch<WlSurface, ()> for State { |
| 862 | fn event( |
| 863 | _state: &mut Self, |
| 864 | _proxy: &WlSurface, |
| 865 | event: <WlSurface as wayland_client::Proxy>::Event, |
| 866 | _data: &(), |
| 867 | _conn: &Connection, |
| 868 | _qhandle: &QueueHandle<Self>, |
| 869 | ) { |
| 870 | match event { |
| 871 | wl_surface::Event::Enter { .. } => (), |
| 872 | wl_surface::Event::Leave { .. } => (), |
| 873 | wl_surface::Event::PreferredBufferScale { .. } => (), |
| 874 | wl_surface::Event::PreferredBufferTransform { .. } => (), |
| 875 | _ => unreachable!(), |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | impl Dispatch<XdgSurface, ()> for State { |
| 881 | fn event( |
| 882 | state: &mut Self, |
| 883 | xdg_surface: &XdgSurface, |
| 884 | event: <XdgSurface as wayland_client::Proxy>::Event, |
| 885 | _data: &(), |
| 886 | _conn: &Connection, |
| 887 | _qhandle: &QueueHandle<Self>, |
| 888 | ) { |
| 889 | match event { |
| 890 | xdg_surface::Event::Configure { serial } => { |
| 891 | let window = state |
| 892 | .windows |
| 893 | .iter_mut() |
| 894 | .find(|w| w.xdg_surface == *xdg_surface) |
| 895 | .unwrap(); |
| 896 | let configure = window.pending_configure.clone(); |
| 897 | window.configures_received.push((serial, configure)); |
| 898 | } |
| 899 | _ => unreachable!(), |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | impl Dispatch<XdgToplevel, ()> for State { |
| 905 | fn event( |
| 906 | state: &mut Self, |
| 907 | xdg_toplevel: &XdgToplevel, |
| 908 | event: <XdgToplevel as wayland_client::Proxy>::Event, |
| 909 | _data: &(), |
| 910 | _conn: &Connection, |
| 911 | _qhandle: &QueueHandle<Self>, |
| 912 | ) { |
| 913 | let window = state |
| 914 | .windows |
| 915 | .iter_mut() |
| 916 | .find(|w| w.xdg_toplevel == *xdg_toplevel) |
| 917 | .unwrap(); |
| 918 | |
| 919 | match event { |
| 920 | xdg_toplevel::Event::Configure { |
| 921 | width, |
| 922 | height, |
| 923 | states, |
| 924 | } => { |
| 925 | let configure = &mut window.pending_configure; |
| 926 | configure.size = (width, height); |
| 927 | configure.states = states |
| 928 | .chunks_exact(4) |
| 929 | .flat_map(TryInto::<[u8; 4]>::try_into) |
| 930 | .map(u32::from_ne_bytes) |
| 931 | .flat_map(xdg_toplevel::State::try_from) |
| 932 | .collect(); |
| 933 | } |
| 934 | xdg_toplevel::Event::Close => { |
| 935 | window.close_requested = true; |
| 936 | } |
| 937 | xdg_toplevel::Event::ConfigureBounds { width, height } => { |
| 938 | window.pending_configure.bounds = Some((width, height)); |
| 939 | } |
| 940 | xdg_toplevel::Event::WmCapabilities { .. } => (), |
| 941 | _ => unreachable!(), |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | impl Dispatch<ZwlrLayerSurfaceV1, ()> for State { |
| 947 | fn event( |
| 948 | state: &mut Self, |
| 949 | layer_surface: &ZwlrLayerSurfaceV1, |
| 950 | event: <ZwlrLayerSurfaceV1 as wayland_client::Proxy>::Event, |
| 951 | _data: &(), |
| 952 | _conn: &Connection, |
| 953 | _qhandle: &QueueHandle<Self>, |
| 954 | ) { |
| 955 | let layer_surface = state |
| 956 | .layers |
| 957 | .iter_mut() |
| 958 | .find(|w| w.layer_surface == *layer_surface) |
| 959 | .unwrap(); |
| 960 | |
| 961 | match event { |
| 962 | zwlr_layer_surface_v1::Event::Configure { |
| 963 | serial, |
| 964 | width, |
| 965 | height, |
| 966 | } => { |
| 967 | let configure = LayerConfigure { |
| 968 | size: (width, height), |
| 969 | }; |
| 970 | layer_surface.configures_received.push((serial, configure)); |
| 971 | } |
| 972 | zwlr_layer_surface_v1::Event::Closed => layer_surface.close_requested = true, |
| 973 | _ => unreachable!(), |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | impl Dispatch<WlBuffer, ()> for State { |
| 979 | fn event( |
| 980 | _state: &mut Self, |
| 981 | _proxy: &WlBuffer, |
| 982 | event: <WlBuffer as wayland_client::Proxy>::Event, |
| 983 | _data: &(), |
| 984 | _conn: &Connection, |
| 985 | _qhandle: &QueueHandle<Self>, |
| 986 | ) { |
| 987 | match event { |
| 988 | wl_buffer::Event::Release => (), |
| 989 | _ => unreachable!(), |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | impl Dispatch<WpSinglePixelBufferManagerV1, ()> for State { |
| 995 | fn event( |
| 996 | _state: &mut Self, |
| 997 | _proxy: &WpSinglePixelBufferManagerV1, |
| 998 | _event: <WpSinglePixelBufferManagerV1 as wayland_client::Proxy>::Event, |
| 999 | _data: &(), |
| 1000 | _conn: &Connection, |
| 1001 | _qhandle: &QueueHandle<Self>, |
| 1002 | ) { |
| 1003 | unreachable!() |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | impl Dispatch<WpViewporter, ()> for State { |
| 1008 | fn event( |
| 1009 | _state: &mut Self, |
| 1010 | _proxy: &WpViewporter, |
| 1011 | _event: <WpViewporter as wayland_client::Proxy>::Event, |
| 1012 | _data: &(), |
| 1013 | _conn: &Connection, |
| 1014 | _qhandle: &QueueHandle<Self>, |
| 1015 | ) { |
| 1016 | unreachable!() |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | impl Dispatch<WpViewport, ()> for State { |
| 1021 | fn event( |
| 1022 | _state: &mut Self, |
| 1023 | _proxy: &WpViewport, |
| 1024 | _event: <WpViewport as wayland_client::Proxy>::Event, |
| 1025 | _data: &(), |
| 1026 | _conn: &Connection, |
| 1027 | _qhandle: &QueueHandle<Self>, |
| 1028 | ) { |
| 1029 | unreachable!() |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | impl Dispatch<ZwpKeyboardShortcutsInhibitManagerV1, ()> for State { |
| 1034 | fn event( |
| 1035 | _state: &mut Self, |
| 1036 | _proxy: &ZwpKeyboardShortcutsInhibitManagerV1, |
| 1037 | _event: <ZwpKeyboardShortcutsInhibitManagerV1 as wayland_client::Proxy>::Event, |
| 1038 | _data: &(), |
| 1039 | _conn: &Connection, |
| 1040 | _qhandle: &QueueHandle<Self>, |
| 1041 | ) { |
| 1042 | unreachable!() |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | impl Dispatch<ZwpKeyboardShortcutsInhibitorV1, ()> for State { |
| 1047 | fn event( |
| 1048 | _state: &mut Self, |
| 1049 | _proxy: &ZwpKeyboardShortcutsInhibitorV1, |
| 1050 | event: <ZwpKeyboardShortcutsInhibitorV1 as wayland_client::Proxy>::Event, |
| 1051 | _data: &(), |
| 1052 | _conn: &Connection, |
| 1053 | _qhandle: &QueueHandle<Self>, |
| 1054 | ) { |
| 1055 | match event { |
| 1056 | zwp_keyboard_shortcuts_inhibitor_v1::Event::Active => (), |
| 1057 | zwp_keyboard_shortcuts_inhibitor_v1::Event::Inactive => (), |
| 1058 | _ => unreachable!(), |
| 1059 | } |
| 1060 | } |
| 1061 | } |