tony / oxwm

Branch: e9ab027
Switch branches/tags
master
tonybtw Added hot reload functionality, and it works.' 6f540a5 2025-10-04
81 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 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 connection.grab_key(
60 false,
61 root,
62 modifier_mask.into(),
63 keybinding.key,
64 GrabMode::ASYNC,
65 GrabMode::ASYNC,
66 )?;
67 }
68 Ok(())
69 }
70
71 pub fn handle_key_press(event: KeyPressEvent) -> Result<(KeyAction, &'static Arg)> {
72 for keybinding in KEYBINDINGS {
73 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
74
75 if event.detail == keybinding.key && event.state == modifier_mask.into() {
76 return Ok((keybinding.func, &keybinding.arg));
77 }
78 }
79
80 Ok((KeyAction::None, &Arg::None))
81 }