tony / oxwm

Branch: b9062ae
Switch branches/tags
master
tonybtw added fallback font b9062ae 2025-10-06
83 lines · 1.8 KB Raw Blame History
1 use crate::config::KEYBINDINGS;
2 use anyhow::Result;
3 use x11rb::connection::Connection;
4 use x11rb::protocol::xproto::*;
5
6 #[derive(Debug, Copy, Clone)]
7 pub enum KeyAction {
8 Spawn,
9 KillClient,
10 FocusStack,
11 Quit,
12 Restart,
13 ViewTag,
14 MoveToTag,
15 None,
16 }
17
18 #[derive(Debug)]
19 pub enum Arg {
20 None,
21 Int(i32),
22 Str(&'static str),
23 Array(&'static [&'static str]),
24 }
25
26 pub struct Key {
27 pub(crate) modifiers: &'static [KeyButMask],
28 pub(crate) key: Keycode,
29 pub(crate) func: KeyAction,
30 pub(crate) arg: Arg,
31 }
32
33 impl Key {
34 pub const fn new(
35 modifiers: &'static [KeyButMask],
36 key: Keycode,
37 func: KeyAction,
38 arg: Arg,
39 ) -> Self {
40 Self {
41 modifiers,
42 key,
43 func,
44 arg,
45 }
46 }
47 }
48
49 fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 {
50 modifiers
51 .iter()
52 .fold(0u16, |acc, &modifier| acc | u16::from(modifier))
53 }
54
55 pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> {
56 for keybinding in KEYBINDINGS {
57 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
58
59 println!("Grabbing key {} with mod {}", keybinding.key, modifier_mask);
60
61 connection.grab_key(
62 false,
63 root,
64 modifier_mask.into(),
65 keybinding.key,
66 GrabMode::ASYNC,
67 GrabMode::ASYNC,
68 )?;
69 }
70 Ok(())
71 }
72
73 pub fn handle_key_press(event: KeyPressEvent) -> Result<(KeyAction, &'static Arg)> {
74 for keybinding in KEYBINDINGS {
75 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
76
77 if event.detail == keybinding.key && event.state == modifier_mask.into() {
78 return Ok((keybinding.func, &keybinding.arg));
79 }
80 }
81
82 Ok((KeyAction::None, &Arg::None))
83 }