From dddf2b896adc701095e2615bbd82c1396bb9bcc8 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Tue, 20 Sep 2022 21:55:39 +0200 Subject: [PATCH] refactor using new global inits helper --- src/bin/server.rs | 345 ++++++++++++++++++++-------------------------- 1 file changed, 153 insertions(+), 192 deletions(-) diff --git a/src/bin/server.rs b/src/bin/server.rs index 3e2ad49..fe7a1c9 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -18,6 +18,7 @@ use wayland_protocols::wp::{ use wayland_protocols_wlr::layer_shell::v1::client::{zwlr_layer_shell_v1, zwlr_layer_surface_v1}; use wayland_client::{ + globals::{registry_queue_init, GlobalListContents}, protocol::{ wl_buffer, wl_compositor, wl_keyboard, wl_pointer, wl_region, wl_registry, wl_seat, wl_shm, wl_shm_pool, wl_surface, @@ -27,23 +28,68 @@ use wayland_client::{ use tempfile; +struct Globals { + compositor: wl_compositor::WlCompositor, + pointer_constraints: zwp_pointer_constraints_v1::ZwpPointerConstraintsV1, + relative_pointer_manager: zwp_relative_pointer_manager_v1::ZwpRelativePointerManagerV1, + shortcut_inhibit_manager: zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1, + seat: wl_seat::WlSeat, + shm: wl_shm::WlShm, + layer_shell: zwlr_layer_shell_v1::ZwlrLayerShellV1, +} + struct App { running: bool, - compositor: Option, - buffer: Option, - surface: Option, - layer_shell: Option, - layer_surface: Option, - pointer_constraints: Option, - rel_pointer_manager: Option, + windows: Windows, pointer_lock: Option, rel_pointer: Option, - shortcut_inhibit_manager: - Option, - shortcut_inhibitor: - Option, + shortcut_inhibitor: Option, connection: protocol::Connection, - seat: Option, + globals: Globals, +} + +struct Windows { + _left: Option, + right: Option, + _top: Option, + _bottom: Option, +} + +struct Window { + buffer: wl_buffer::WlBuffer, + surface: wl_surface::WlSurface, + layer_surface: zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, +} + +impl Window { + fn new(g: &Globals, qh: QueueHandle) -> Window { + let (width, height) = (1, 1440); + let mut file = tempfile::tempfile().unwrap(); + draw(&mut file, (width, height)); + let pool = g.shm.create_pool(file.as_raw_fd(), (width * height * 4) as i32, &qh, ()); + let buffer = pool.create_buffer(0, + width as i32, height as i32, (width * 4) as i32, + wl_shm::Format::Argb8888, &qh, (), + ); + let surface = g.compositor.create_surface(&qh, ()); + + let layer_surface = g.layer_shell.get_layer_surface( + &surface, + None, + zwlr_layer_shell_v1::Layer::Top, + "LAN Mouse Sharing".into(), + &qh, + (), + ); + + layer_surface.set_anchor(zwlr_layer_surface_v1::Anchor::Right); + layer_surface.set_size(1, 1440); + layer_surface.set_exclusive_zone(1); + layer_surface.set_margin(0, 0, 0, 0); + surface.set_input_region(None); + surface.commit(); + Window { buffer, surface, layer_surface } + } } fn main() { @@ -51,59 +97,48 @@ fn main() { let connection = protocol::Connection::new(config); // establish connection via environment-provided configuration. let conn = Connection::connect_to_env().unwrap(); + let (globals, mut queue) = registry_queue_init::(&conn).unwrap(); + let qh = queue.handle(); - // Retrieve the wayland display object - let display = conn.display(); - // Create an event queue for our event processing - let mut event_queue = conn.new_event_queue(); - let qh = event_queue.handle(); + let compositor: wl_compositor::WlCompositor = globals.bind(&qh, 4..=5, ()).unwrap(); + let shm: wl_shm::WlShm = globals.bind::(&qh, 1..=1, ()).unwrap(); + let layer_shell: zwlr_layer_shell_v1::ZwlrLayerShellV1 = globals.bind(&qh, 3..=4, ()).unwrap(); + let seat: wl_seat::WlSeat = globals.bind(&qh, 7..=8, ()).unwrap(); + let pointer_constraints: zwp_pointer_constraints_v1::ZwpPointerConstraintsV1 = globals.bind(&qh, 1..=1, ()).unwrap(); + let relative_pointer_manager: zwp_relative_pointer_manager_v1::ZwpRelativePointerManagerV1 = globals.bind(&qh, 1..=1, ()).unwrap(); + let shortcut_inhibit_manager: zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1 = globals.bind(&qh, 1..=1, ()).unwrap(); - // Create a wl_registry object by sending the wl_display.get_registry request - display.get_registry(&qh, ()); + let globals = Globals { + compositor, + shm, + layer_shell, + seat, + pointer_constraints, + relative_pointer_manager, + shortcut_inhibit_manager, + }; + + let windows: Windows = Windows { + _left: None, + right: Some(Window::new(&globals, qh)), + _top: None, + _bottom: None, + }; let mut app = App { running: true, - compositor: None, - buffer: None, - surface: None, - layer_shell: None, - layer_surface: None, - pointer_constraints: None, - rel_pointer_manager: None, + globals, + windows, pointer_lock: None, rel_pointer: None, - connection, - shortcut_inhibit_manager: None, shortcut_inhibitor: None, - seat: None, + connection, }; - // use roundtrip to process this event synchronously - event_queue.roundtrip(&mut app).unwrap(); - let compositor = app.compositor.as_ref().unwrap(); - app.surface = Some(compositor.create_surface(&qh, ())); - - let layer_shell = app.layer_shell.as_ref().unwrap(); - let layer_surface = layer_shell.get_layer_surface( - &app.surface.as_ref().unwrap(), - None, - zwlr_layer_shell_v1::Layer::Top, - "LAN Mouse Sharing".into(), - &qh, - (), - ); - app.layer_surface = Some(layer_surface); - let layer_surface = app.layer_surface.as_ref().unwrap(); - layer_surface.set_anchor(zwlr_layer_surface_v1::Anchor::Right); - layer_surface.set_size(1, 1440); - layer_surface.set_exclusive_zone(1); - layer_surface.set_margin(0, 0, 0, 0); - app.surface.as_ref().unwrap().set_input_region(None); - app.surface.as_ref().unwrap().commit(); while app.running { - event_queue.blocking_dispatch(&mut app).unwrap(); + queue.blocking_dispatch(&mut app).unwrap(); } } @@ -116,83 +151,50 @@ fn draw(f: &mut File, (width, height): (u32, u32)) { } } -impl Dispatch for App { - fn event( - app: &mut Self, - registry: &wl_registry::WlRegistry, - event: wl_registry::Event, - _: &(), - _: &Connection, - qh: &QueueHandle, - ) { - // Match global event to get globals after requesting them in main - if let wl_registry::Event::Global { - name, interface, .. - } = event - { - match &interface[..] { - "wl_compositor" => { - app.compositor = - Some(registry.bind::(name, 4, qh, ())); - } - "wl_shm" => { - let shm = registry.bind::(name, 1, qh, ()); - let (width, height) = (1, 1440); - let mut file = tempfile::tempfile().unwrap(); - draw(&mut file, (width, height)); - let pool = - shm.create_pool(file.as_raw_fd(), (width * height * 4) as i32, &qh, ()); - let buffer = pool.create_buffer( - 0, - width as i32, - height as i32, - (width * 4) as i32, - wl_shm::Format::Argb8888, +impl App { + fn grab(&mut self, pointer: &wl_pointer::WlPointer, serial: u32, qh: &QueueHandle) { + pointer.set_cursor(serial, None, 0, 0); + let layer_surface = &self.windows.right.as_ref().unwrap().layer_surface; + layer_surface.set_keyboard_interactivity(zwlr_layer_surface_v1::KeyboardInteractivity::Exclusive); + let surface = &self.windows.right.as_ref().unwrap().surface; + surface.commit(); + if self.pointer_lock.is_none() { + self.pointer_lock = + Some(self.globals.pointer_constraints.lock_pointer(&surface, pointer, None, zwp_pointer_constraints_v1::Lifetime::Oneshot, qh, ())); + } + if self.rel_pointer.is_none() { + self.rel_pointer = Some( + self.globals.relative_pointer_manager.get_relative_pointer(pointer, qh, ()), + ); + } + if self.shortcut_inhibitor.is_none() { + self.shortcut_inhibitor = Some( + self.globals.shortcut_inhibit_manager.inhibit_shortcuts( + &surface, + &self.globals.seat, qh, (), - ); - app.buffer = Some(buffer); - } - "wl_seat" => { - app.seat = Some(registry.bind::(name, 7, qh, ())); - } - "zwp_pointer_constraints_v1" => { - app.pointer_constraints = Some( - registry.bind::( - name, - 1, - &qh, - (), - ), - ); - } - "zwp_relative_pointer_manager_v1" => { - app.rel_pointer_manager = Some( - registry.bind::( - name, - 1, - &qh, - (), - ), - ); - } - "zwlr_layer_shell_v1" => { - app.layer_shell = Some( - registry.bind::( - name, - 3, - &qh, - (), - ), - ); - } - "zwp_keyboard_shortcuts_inhibit_manager_v1" => { - app.shortcut_inhibit_manager = Some(registry.bind::( - name, 1, &qh, (), - )); - } - _ => {} - } + ), + ); + } + } + + fn ungrab(&mut self) { + let layer_surface = &self.windows.right.as_ref().unwrap().layer_surface; + layer_surface.set_keyboard_interactivity(zwlr_layer_surface_v1::KeyboardInteractivity::None); + let surface = &self.windows.right.as_ref().unwrap().surface; + surface.commit(); + if let Some(pointer_lock) = &self.pointer_lock { + pointer_lock.destroy(); + self.pointer_lock = None; + } + if let Some(rel_pointer) = &self.rel_pointer { + rel_pointer.destroy(); + self.rel_pointer = None; + } + if let Some(shortcut_inhibitor) = &self.shortcut_inhibitor { + shortcut_inhibitor.destroy(); + self.shortcut_inhibitor = None; } } } @@ -205,9 +207,7 @@ impl Dispatch for App { _: &(), _: &Connection, _: &QueueHandle, - ) { - todo!() - } + ) { } } impl Dispatch for App { @@ -218,9 +218,7 @@ impl Dispatch for App { _: &(), _: &Connection, _: &QueueHandle, - ) { - // - } + ) { } } impl Dispatch for App { @@ -231,9 +229,7 @@ impl Dispatch for App { _: &(), _: &Connection, _: &QueueHandle, - ) { - // ignore - } + ) { } } impl Dispatch for App { @@ -257,9 +253,7 @@ impl Dispatch for App { _: &(), _: &Connection, _: &QueueHandle, - ) { - // - } + ) { } } impl Dispatch for App { @@ -301,53 +295,10 @@ impl Dispatch for App { surface_x: _, surface_y: _, } => { - pointer.set_cursor(serial, None, 0, 0); - if let Some(s) = app.layer_surface.as_ref() { - s.set_keyboard_interactivity( - zwlr_layer_surface_v1::KeyboardInteractivity::Exclusive, - ); - app.surface.as_ref().unwrap().commit(); - } - if app.pointer_lock.is_none() { - app.pointer_lock = - Some(app.pointer_constraints.as_ref().unwrap().lock_pointer( - &app.surface.as_ref().unwrap(), - pointer, - None, - zwp_pointer_constraints_v1::Lifetime::Oneshot, - qh, - (), - )); - } - if app.rel_pointer.is_none() { - app.rel_pointer = Some( - app.rel_pointer_manager - .as_ref() - .unwrap() - .get_relative_pointer(pointer, qh, ()), - ); - } - if app.shortcut_inhibitor.is_none() { - app.shortcut_inhibitor = Some( - app.shortcut_inhibit_manager - .as_ref() - .unwrap() - .inhibit_shortcuts( - app.surface.as_ref().unwrap(), - app.seat.as_ref().unwrap(), - qh, - (), - ), - ); - } + app.grab(pointer, serial, qh); } wl_pointer::Event::Leave { .. } => { - if let Some(s) = app.layer_surface.as_ref() { - s.set_keyboard_interactivity( - zwlr_layer_surface_v1::KeyboardInteractivity::None, - ); - app.surface.as_ref().unwrap().commit(); - } + app.ungrab(); } wl_pointer::Event::Button { .. } => { app.connection.send_event(event); @@ -487,20 +438,18 @@ impl Dispatch for App { impl Dispatch for App { fn event( app: &mut Self, - surface: &zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, + layer_surface: &zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, event: ::Event, _: &(), _: &Connection, _: &QueueHandle, ) { if let zwlr_layer_surface_v1::Event::Configure { serial, .. } = event { - app.surface.as_ref().unwrap().commit(); - surface.ack_configure(serial); - app.surface - .as_ref() - .unwrap() - .attach(Some(app.buffer.as_ref().unwrap()), 0, 0); - app.surface.as_ref().unwrap().commit(); + let surface = &app.windows.right.as_ref().unwrap().surface; + surface.commit(); + layer_surface.ack_configure(serial); + surface.attach(Some(&app.windows.right.as_ref().unwrap().buffer), 0, 0); + surface.commit(); } } } @@ -542,3 +491,15 @@ impl Dispatch for App { + fn event( + _: &mut App, + _: &wl_registry::WlRegistry, + _: wl_registry::Event, + _: &GlobalListContents, + _: &Connection, + _: &QueueHandle, + ) { } + +}