tony / oxwm

Branch: 8c774ac
Switch branches/tags
master
tonybtw Fixed border issue on focus, changed keymaps to be modular, added j/k functionality to iterate stack 8c774ac 2025-09-29
107 lines · 2.3 KB Raw Blame History
1 use crate::keyboard::keycodes;
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 None,
13 }
14
15 #[derive(Debug)]
16 pub enum Arg {
17 Str(&'static str),
18 Int(i32),
19 None,
20 }
21
22 pub struct Key {
23 pub(crate) modifiers: &'static [KeyButMask],
24 pub(crate) key: Keycode,
25 pub(crate) func: KeyAction,
26 pub(crate) arg: Arg,
27 }
28
29 impl Key {
30 pub const fn new(
31 modifiers: &'static [KeyButMask],
32 key: Keycode,
33 func: KeyAction,
34 arg: Arg,
35 ) -> Self {
36 Self {
37 modifiers,
38 key,
39 func,
40 arg,
41 }
42 }
43 }
44
45 const KEYBINDINGS: &[Key] = &[
46 Key::new(
47 &[KeyButMask::MOD1],
48 keycodes::RETURN,
49 KeyAction::Spawn,
50 Arg::Str("xclock"),
51 ),
52 Key::new(
53 &[KeyButMask::MOD1, KeyButMask::SHIFT],
54 keycodes::Q,
55 KeyAction::KillClient,
56 Arg::None,
57 ),
58 Key::new(&[KeyButMask::MOD1], keycodes::Q, KeyAction::Quit, Arg::None),
59 Key::new(
60 &[KeyButMask::MOD1],
61 keycodes::J,
62 KeyAction::FocusStack,
63 Arg::Int(1),
64 ),
65 Key::new(
66 &[KeyButMask::MOD1],
67 keycodes::K,
68 KeyAction::FocusStack,
69 Arg::Int(-1),
70 ),
71 ];
72
73 fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 {
74 modifiers
75 .iter()
76 .fold(0u16, |acc, &modifier| acc | u16::from(modifier))
77 }
78
79 pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> {
80 for keybinding in KEYBINDINGS {
81 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
82
83 connection.grab_key(
84 false,
85 root,
86 modifier_mask.into(),
87 keybinding.key,
88 GrabMode::ASYNC,
89 GrabMode::ASYNC,
90 )?;
91 }
92 Ok(())
93 }
94
95 pub fn handle_key_press(event: KeyPressEvent) -> Result<(KeyAction, &'static Arg)> {
96 println!("KeyPress: detail={}, state={:?}", event.detail, event.state);
97
98 for keybinding in KEYBINDINGS {
99 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
100
101 if event.detail == keybinding.key && event.state == modifier_mask.into() {
102 return Ok((keybinding.func, &keybinding.arg));
103 }
104 }
105
106 Ok((KeyAction::None, &Arg::None))
107 }