From b620b210077291a644604f4377b5565ff7644385 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Mon, 24 Apr 2023 13:45:01 -0700 Subject: [PATCH] Add shortcuts inhibit protocol --- src/seat/mod.rs | 1 + src/seat/shortcuts_inhibit.rs | 126 ++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/seat/shortcuts_inhibit.rs diff --git a/src/seat/mod.rs b/src/seat/mod.rs index 0a1ffb3ea..b4891e5b3 100644 --- a/src/seat/mod.rs +++ b/src/seat/mod.rs @@ -3,6 +3,7 @@ pub mod keyboard; pub mod pointer; pub mod pointer_constraints; pub mod relative_pointer; +pub mod shortcuts_inhibit; pub mod touch; use std::{ diff --git a/src/seat/shortcuts_inhibit.rs b/src/seat/shortcuts_inhibit.rs new file mode 100644 index 000000000..216b1dcaa --- /dev/null +++ b/src/seat/shortcuts_inhibit.rs @@ -0,0 +1,126 @@ +use wayland_client::{ + globals::GlobalList, + protocol::{wl_seat, wl_surface}, + Connection, Dispatch, QueueHandle, +}; +use wayland_protocols::wp::keyboard_shortcuts_inhibit::zv1::client::{ + zwp_keyboard_shortcuts_inhibit_manager_v1, zwp_keyboard_shortcuts_inhibitor_v1, +}; + +use crate::{error::GlobalError, globals::GlobalData, registry::GlobalProxy}; + +#[derive(Debug)] +pub struct ShortcutsInhibitState { + shortcuts_inhibit_manager: GlobalProxy< + zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1, + >, +} + +impl ShortcutsInhibitState { + /// Bind `zwp_keyboard_shortcuts_inhibit_manager_v1` global, if it exists + pub fn bind(globals: &GlobalList, qh: &QueueHandle) -> Self + where + D: Dispatch< + zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1, + GlobalData, + > + 'static, + { + let shortcuts_inhibit_manager = GlobalProxy::from(globals.bind(qh, 1..=1, GlobalData)); + Self { shortcuts_inhibit_manager } + } + + /// Request that keyboard shortcuts are inhibited for surface on given seat. + /// + /// Raises protocol error if a shortcut inhibitor already exists for the seat and surface. + pub fn inhibit_shortcuts( + &self, + surface: &wl_surface::WlSurface, + seat: &wl_seat::WlSeat, + qh: &QueueHandle, + ) -> Result + where + D: Dispatch< + zwp_keyboard_shortcuts_inhibitor_v1::ZwpKeyboardShortcutsInhibitorV1, + GlobalData, + > + 'static, + { + Ok(self.shortcuts_inhibit_manager.get()?.inhibit_shortcuts(surface, seat, qh, GlobalData)) + } +} + +pub trait ShortcutsInhibitHandler: Sized { + fn active( + &mut self, + conn: &Connection, + qh: &QueueHandle, + shortcuts_inhibitor: &zwp_keyboard_shortcuts_inhibitor_v1::ZwpKeyboardShortcutsInhibitorV1, + ); + + fn inactive( + &mut self, + conn: &Connection, + qh: &QueueHandle, + shortcuts_inhibitor: &zwp_keyboard_shortcuts_inhibitor_v1::ZwpKeyboardShortcutsInhibitorV1, + ); +} + +impl + Dispatch< + zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1, + GlobalData, + D, + > for ShortcutsInhibitState +where + D: Dispatch< + zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1, + GlobalData, + >, +{ + fn event( + _data: &mut D, + _manager: &zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1, + _event: zwp_keyboard_shortcuts_inhibit_manager_v1::Event, + _: &GlobalData, + _conn: &Connection, + _qh: &QueueHandle, + ) { + unreachable!() + } +} + +impl + Dispatch + for ShortcutsInhibitState +where + D: Dispatch + + ShortcutsInhibitHandler, +{ + fn event( + data: &mut D, + inhibitor: &zwp_keyboard_shortcuts_inhibitor_v1::ZwpKeyboardShortcutsInhibitorV1, + event: zwp_keyboard_shortcuts_inhibitor_v1::Event, + _: &GlobalData, + conn: &Connection, + qh: &QueueHandle, + ) { + match event { + zwp_keyboard_shortcuts_inhibitor_v1::Event::Active => data.active(conn, qh, inhibitor), + zwp_keyboard_shortcuts_inhibitor_v1::Event::Inactive => { + data.inactive(conn, qh, inhibitor) + } + _ => unreachable!(), + } + } +} + +#[macro_export] +macro_rules! delegate_shortcuts_inhibit { + ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { + $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ + $crate::reexports::protocols::wp::keyboard_shortcuts_inhibit::zv1::client::zwp_keyboard_shortcuts_inhibit_manager_v1::ZwpKeyboardShortcutsInhibitManagerV1: $crate::globals::GlobalData + ] => $crate::seat::shortcuts_inhibit::ShortcutsInhibitState); + $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ + $crate::reexports::protocols::wp::keyboard_shortcuts_inhibit::zv1::client::zwp_keyboard_shortcuts_inhibitor_v1::ZwpKeyboardShortcutsInhibitorV1: $crate::globals::GlobalData + ] => $crate::seat::shortcuts_inhibit::ShortcutsInhibitState); + }; +}