tony / oxwm

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