|
| 1 | +// https://gitlab.gnome.org/GNOME/mutter/-/blob/main/data/dbus-interfaces/org.freedesktop.a11y.xml |
| 2 | +// |
| 3 | +// TODO: Restrict protocol acccess? |
| 4 | +// TODO remove client when not connected to server |
| 5 | + |
| 6 | +use futures_executor::ThreadPool; |
| 7 | +use smithay::backend::input::KeyState; |
| 8 | +use std::collections::HashMap; |
| 9 | +use std::collections::HashSet; |
| 10 | +use std::sync::OnceLock; |
| 11 | +use std::sync::{Arc, Mutex}; |
| 12 | +use xkbcommon::xkb::{self, Keysym}; |
| 13 | +use zbus::message::Header; |
| 14 | +use zbus::names::UniqueName; |
| 15 | +use zbus::object_server::SignalEmitter; |
| 16 | + |
| 17 | +// As defined in at-spi2-core |
| 18 | +const ATSPI_DEVICE_A11Y_MANAGER_VIRTUAL_MOD_START: u32 = 15; |
| 19 | + |
| 20 | +#[derive(PartialEq, Eq, Debug)] |
| 21 | +struct KeyGrab { |
| 22 | + pub mods: u32, |
| 23 | + pub virtual_mods: HashSet<Keysym>, |
| 24 | + pub key: Keysym, |
| 25 | +} |
| 26 | + |
| 27 | +impl KeyGrab { |
| 28 | + fn new(virtual_mods: &[Keysym], key: Keysym, raw_mods: u32) -> Self { |
| 29 | + let mods = raw_mods & ((1 << ATSPI_DEVICE_A11Y_MANAGER_VIRTUAL_MOD_START) - 1); |
| 30 | + let virtual_mods = virtual_mods |
| 31 | + .iter() |
| 32 | + .copied() |
| 33 | + .enumerate() |
| 34 | + .filter(|(i, _)| { |
| 35 | + raw_mods & (1 << (ATSPI_DEVICE_A11Y_MANAGER_VIRTUAL_MOD_START + *i as u32)) != 0 |
| 36 | + }) |
| 37 | + .map(|(_, x)| x) |
| 38 | + .collect(); |
| 39 | + Self { |
| 40 | + mods, |
| 41 | + virtual_mods, |
| 42 | + key, |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Debug, Default)] |
| 48 | +struct Client { |
| 49 | + grabbed: bool, |
| 50 | + watched: bool, |
| 51 | + virtual_mods: HashSet<Keysym>, |
| 52 | + key_grabs: Vec<KeyGrab>, |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug, Default)] |
| 56 | +struct Clients(HashMap<UniqueName<'static>, Client>); |
| 57 | + |
| 58 | +impl Clients { |
| 59 | + fn get(&mut self, name: &UniqueName<'_>) -> &mut Client { |
| 60 | + self.0.entry(name.to_owned()).or_default() |
| 61 | + } |
| 62 | + |
| 63 | + fn remove(&mut self, name: &UniqueName<'_>) -> bool { |
| 64 | + self.0.remove(&name.to_owned()).is_some() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug)] |
| 69 | +pub struct A11yKeyboardMonitorState { |
| 70 | + executor: ThreadPool, |
| 71 | + clients: Arc<Mutex<Clients>>, |
| 72 | + active_virtual_mods: HashSet<Keysym>, |
| 73 | + conn: Arc<OnceLock<zbus::Connection>>, |
| 74 | +} |
| 75 | + |
| 76 | +impl A11yKeyboardMonitorState { |
| 77 | + pub fn new(executor: &ThreadPool) -> Self { |
| 78 | + let clients = Arc::new(Mutex::new(Clients::default())); |
| 79 | + let clients_clone = clients.clone(); |
| 80 | + let conn_cell = Arc::new(OnceLock::new()); |
| 81 | + let conn_cell_clone = conn_cell.clone(); |
| 82 | + executor.spawn_ok(async move { |
| 83 | + match serve(clients_clone).await { |
| 84 | + Ok(conn) => { |
| 85 | + conn_cell_clone.set(conn).unwrap(); |
| 86 | + } |
| 87 | + Err(err) => { |
| 88 | + tracing::error!("Failed to serve `org.freedesktop.a11y.Manager`: {err}"); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + Self { |
| 93 | + executor: executor.clone(), |
| 94 | + clients, |
| 95 | + active_virtual_mods: HashSet::new(), |
| 96 | + conn: conn_cell, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + pub fn has_virtual_mod(&self, keysym: Keysym) -> bool { |
| 101 | + self.clients |
| 102 | + .lock() |
| 103 | + .unwrap() |
| 104 | + .0 |
| 105 | + .values() |
| 106 | + .any(|client| client.virtual_mods.contains(&keysym)) |
| 107 | + } |
| 108 | + |
| 109 | + pub fn add_active_virtual_mod(&mut self, keysym: Keysym) { |
| 110 | + self.active_virtual_mods.insert(keysym); |
| 111 | + } |
| 112 | + |
| 113 | + pub fn remove_active_virtual_mod(&mut self, keysym: Keysym) -> bool { |
| 114 | + self.active_virtual_mods.remove(&keysym) |
| 115 | + } |
| 116 | + |
| 117 | + pub fn has_keyboard_grab(&self) -> bool { |
| 118 | + self.clients |
| 119 | + .lock() |
| 120 | + .unwrap() |
| 121 | + .0 |
| 122 | + .values() |
| 123 | + .any(|client| client.grabbed) |
| 124 | + } |
| 125 | + |
| 126 | + /// Key grab exists for mods, key, with active virtual mods |
| 127 | + pub fn has_key_grab(&self, mods: u32, key: Keysym) -> bool { |
| 128 | + self.clients |
| 129 | + .lock() |
| 130 | + .unwrap() |
| 131 | + .0 |
| 132 | + .values() |
| 133 | + .flat_map(|client| &client.key_grabs) |
| 134 | + .any(|grab| { |
| 135 | + grab.mods == mods |
| 136 | + && grab.virtual_mods == self.active_virtual_mods |
| 137 | + && grab.key == key |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + pub fn key_event( |
| 142 | + &self, |
| 143 | + modifiers: &smithay::input::keyboard::ModifiersState, |
| 144 | + keysym: &smithay::input::keyboard::KeysymHandle, |
| 145 | + state: smithay::backend::input::KeyState, |
| 146 | + ) { |
| 147 | + let Some(conn) = self.conn.get() else { |
| 148 | + return; |
| 149 | + }; |
| 150 | + |
| 151 | + // Test if any client is watching key input |
| 152 | + if !self |
| 153 | + .clients |
| 154 | + .lock() |
| 155 | + .unwrap() |
| 156 | + .0 |
| 157 | + .values() |
| 158 | + .any(|client| client.watched) |
| 159 | + { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + let signal_context = SignalEmitter::new(conn, "/org/freedesktop/a11y/Manager").unwrap(); |
| 164 | + let released = match state { |
| 165 | + KeyState::Pressed => false, |
| 166 | + KeyState::Released => true, |
| 167 | + }; |
| 168 | + let unichar = { |
| 169 | + let xkb = keysym.xkb().lock().unwrap(); |
| 170 | + unsafe { xkb.state() }.key_get_utf32(keysym.raw_code()) |
| 171 | + }; |
| 172 | + let future = KeyboardMonitor::key_event( |
| 173 | + signal_context, |
| 174 | + released, |
| 175 | + modifiers.serialized.layout_effective, |
| 176 | + keysym.modified_sym().raw(), |
| 177 | + unichar, |
| 178 | + keysym.raw_code().raw() as u16, |
| 179 | + ); |
| 180 | + self.executor.spawn_ok(async { |
| 181 | + future.await; |
| 182 | + }); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +struct KeyboardMonitor { |
| 187 | + clients: Arc<Mutex<Clients>>, |
| 188 | +} |
| 189 | + |
| 190 | +#[zbus::interface(name = "org.freedesktop.a11y.KeyboardMonitor")] |
| 191 | +impl KeyboardMonitor { |
| 192 | + fn grab_keyboard(&mut self, #[zbus(header)] header: Header<'_>) { |
| 193 | + if let Some(sender) = header.sender() { |
| 194 | + let mut clients = self.clients.lock().unwrap(); |
| 195 | + clients.get(sender).grabbed = true; |
| 196 | + eprintln!("grab keyboard by {}", sender); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + fn ungrab_keyboard(&mut self, #[zbus(header)] header: Header<'_>) { |
| 201 | + if let Some(sender) = header.sender() { |
| 202 | + let mut clients = self.clients.lock().unwrap(); |
| 203 | + clients.get(sender).grabbed = false; |
| 204 | + eprintln!("ungrab keyboard by {}", sender); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + fn watch_keyboard(&mut self, #[zbus(header)] header: Header<'_>) { |
| 209 | + if let Some(sender) = header.sender() { |
| 210 | + let mut clients = self.clients.lock().unwrap(); |
| 211 | + clients.get(sender).watched = true; |
| 212 | + eprintln!("watch keyboard by {}", sender); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + fn unwatch_keyboard(&mut self, #[zbus(header)] header: Header<'_>) { |
| 217 | + if let Some(sender) = header.sender() { |
| 218 | + let mut clients = self.clients.lock().unwrap(); |
| 219 | + clients.get(sender).watched = false; |
| 220 | + eprintln!("unwatch keyboard by {}", sender); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + fn set_key_grabs( |
| 225 | + &self, |
| 226 | + #[zbus(header)] header: Header<'_>, |
| 227 | + virtual_mods: Vec<u32>, |
| 228 | + keystrokes: Vec<(u32, u32)>, |
| 229 | + ) { |
| 230 | + let virtual_mods = virtual_mods |
| 231 | + .into_iter() |
| 232 | + .map(Keysym::from) |
| 233 | + .collect::<Vec<_>>(); |
| 234 | + let key_grabs = keystrokes |
| 235 | + .into_iter() |
| 236 | + .map(|(k, mods)| KeyGrab::new(&virtual_mods, Keysym::from(k), mods)) |
| 237 | + .collect::<Vec<_>>(); |
| 238 | + |
| 239 | + if let Some(sender) = header.sender() { |
| 240 | + let mut clients = self.clients.lock().unwrap(); |
| 241 | + let client = clients.get(sender); |
| 242 | + eprintln!( |
| 243 | + "key grabs set by {}: {:?}", |
| 244 | + sender, |
| 245 | + (&virtual_mods, &key_grabs) |
| 246 | + ); |
| 247 | + client.virtual_mods = virtual_mods.into_iter().collect::<HashSet<_>>(); |
| 248 | + client.key_grabs = key_grabs; |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + // TODO signal |
| 253 | + #[zbus(signal)] |
| 254 | + async fn key_event( |
| 255 | + ctx: SignalEmitter<'_>, |
| 256 | + released: bool, |
| 257 | + state: u32, |
| 258 | + keysym: u32, |
| 259 | + unichar: u32, |
| 260 | + keycode: u16, |
| 261 | + ) -> zbus::Result<()>; |
| 262 | +} |
| 263 | + |
| 264 | +async fn serve(clients: Arc<Mutex<Clients>>) -> zbus::Result<zbus::Connection> { |
| 265 | + let keyboard_monitor = KeyboardMonitor { clients }; |
| 266 | + zbus::connection::Builder::session()? |
| 267 | + .name("org.freedesktop.a11y.Manager")? |
| 268 | + .serve_at("/org/freedesktop/a11y/Manager", keyboard_monitor)? |
| 269 | + .build() |
| 270 | + .await |
| 271 | +} |
0 commit comments