tony / oxwm

Branch: 7b4ea87
Switch branches/tags
master
tonybtw Added bar. 2mb! 8347143 2025-10-03
80 lines · 1.7 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 ViewTag,
13 MoveToTag,
14 None,
15 }
16
17 #[derive(Debug)]
18 pub enum Arg {
19 None,
20 Int(i32),
21 Str(&'static str),
22 Array(&'static [&'static str]),
23 }
24
25 pub struct Key {
26 pub(crate) modifiers: &'static [KeyButMask],
27 pub(crate) key: Keycode,
28 pub(crate) func: KeyAction,
29 pub(crate) arg: Arg,
30 }
31
32 impl Key {
33 pub const fn new(
34 modifiers: &'static [KeyButMask],
35 key: Keycode,
36 func: KeyAction,
37 arg: Arg,
38 ) -> Self {
39 Self {
40 modifiers,
41 key,
42 func,
43 arg,
44 }
45 }
46 }
47
48 fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 {
49 modifiers
50 .iter()
51 .fold(0u16, |acc, &modifier| acc | u16::from(modifier))
52 }
53
54 pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> {
55 for keybinding in KEYBINDINGS {
56 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
57
58 connection.grab_key(
59 false,
60 root,
61 modifier_mask.into(),
62 keybinding.key,
63 GrabMode::ASYNC,
64 GrabMode::ASYNC,
65 )?;
66 }
67 Ok(())
68 }
69
70 pub fn handle_key_press(event: KeyPressEvent) -> Result<(KeyAction, &'static Arg)> {
71 for keybinding in KEYBINDINGS {
72 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
73
74 if event.detail == keybinding.key && event.state == modifier_mask.into() {
75 return Ok((keybinding.func, &keybinding.arg));
76 }
77 }
78
79 Ok((KeyAction::None, &Arg::None))
80 }