Skip to content

Commit 4040198

Browse files
committed
KeyGrab
1 parent 63e2a9f commit 4040198

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

src/dbus/a11y_keyboard_monitor.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::collections::HashMap;
88
use std::collections::HashSet;
99
use std::sync::OnceLock;
1010
use std::sync::{Arc, Mutex};
11-
use std::{error::Error, future::pending};
1211
use xkbcommon::xkb::{self, Keysym};
1312
use zbus::message::Header;
1413
use zbus::names::UniqueName;
@@ -17,12 +16,39 @@ use zbus::SignalContext;
1716
// As defined in at-spi2-core
1817
const ATSPI_DEVICE_A11Y_MANAGER_VIRTUAL_MOD_START: u32 = 15;
1918

19+
#[derive(PartialEq, Eq, Debug)]
20+
struct KeyGrab {
21+
pub mods: u32,
22+
pub virtual_modifiers: HashSet<Keysym>,
23+
pub key: Keysym,
24+
}
25+
26+
impl KeyGrab {
27+
fn new(virtual_modifiers: &[Keysym], key: Keysym, raw_mods: u32) -> Self {
28+
let mods = raw_mods & ((1 << ATSPI_DEVICE_A11Y_MANAGER_VIRTUAL_MOD_START) - 1);
29+
let virtual_modifiers = virtual_modifiers
30+
.iter()
31+
.copied()
32+
.enumerate()
33+
.filter(|(i, _)| {
34+
raw_mods & (1 << (ATSPI_DEVICE_A11Y_MANAGER_VIRTUAL_MOD_START + *i as u32)) != 0
35+
})
36+
.map(|(_, x)| x)
37+
.collect();
38+
Self {
39+
mods,
40+
virtual_modifiers,
41+
key,
42+
}
43+
}
44+
}
45+
2046
#[derive(Debug, Default)]
2147
struct Client {
2248
grabbed: bool,
2349
watched: bool,
24-
virtual_modifiers: Vec<Keysym>,
25-
keystrokes: Vec<(Keysym, Modifiers)>,
50+
virtual_modifiers: HashSet<Keysym>,
51+
key_grabs: Vec<KeyGrab>,
2652
}
2753

2854
#[derive(Debug, Default)]
@@ -124,8 +150,6 @@ impl A11yKeyboardMonitorState {
124150
}
125151
}
126152

127-
trait A11yKeyboardMonitorHandler {}
128-
129153
struct KeyboardMonitor {
130154
clients: Arc<Mutex<Clients>>,
131155
}
@@ -174,21 +198,21 @@ impl KeyboardMonitor {
174198
.into_iter()
175199
.map(Keysym::from)
176200
.collect::<Vec<_>>();
177-
let keystrokes = keystrokes
201+
let key_grabs = keystrokes
178202
.into_iter()
179-
.map(|(k, mods)| (Keysym::from(k), Modifiers(mods)))
203+
.map(|(k, mods)| KeyGrab::new(&virtual_modifiers, Keysym::from(k), mods))
180204
.collect::<Vec<_>>();
181205

182206
if let Some(sender) = header.sender() {
183207
let mut clients = self.clients.lock().unwrap();
184-
let mut client = clients.get(sender);
208+
let client = clients.get(sender);
185209
eprintln!(
186210
"key grabs set by {}: {:?}",
187211
sender,
188-
(&virtual_modifiers, &keystrokes)
212+
(&virtual_modifiers, &key_grabs)
189213
);
190-
client.virtual_modifiers = virtual_modifiers;
191-
client.keystrokes = keystrokes;
214+
client.virtual_modifiers = virtual_modifiers.into_iter().collect::<HashSet<_>>();
215+
client.key_grabs = key_grabs;
192216
}
193217
}
194218

@@ -204,9 +228,6 @@ impl KeyboardMonitor {
204228
) -> zbus::Result<()>;
205229
}
206230

207-
#[derive(Debug)]
208-
struct Modifiers(u32);
209-
210231
async fn serve(clients: Arc<Mutex<Clients>>) -> zbus::Result<zbus::Connection> {
211232
let keyboard_monitor = KeyboardMonitor { clients };
212233
zbus::connection::Builder::session()?

0 commit comments

Comments
 (0)