tony / oxwm

Branch: 35a51b2
Switch branches/tags
master
xsoder feat: Implemntation of full screen mode 2342897 2025-10-09
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 ToggleGaps,
15 ToggleFullScreen,
16 MoveToTag,
17 None,
18 }
19
20 #[derive(Debug)]
21 pub enum Arg {
22 None,
23 Int(i32),
24 Str(&'static str),
25 Array(&'static [&'static str]),
26 }
27
28 pub struct Key {
29 pub(crate) modifiers: &'static [KeyButMask],
30 pub(crate) key: Keycode,
31 pub(crate) func: KeyAction,
32 pub(crate) arg: Arg,
33 }
34
35 impl Key {
36 pub const fn new(
37 modifiers: &'static [KeyButMask],
38 key: Keycode,
39 func: KeyAction,
40 arg: Arg,
41 ) -> Self {
42 Self {
43 modifiers,
44 key,
45 func,
46 arg,
47 }
48 }
49 }
50
51 fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 {
52 modifiers
53 .iter()
54 .fold(0u16, |acc, &modifier| acc | u16::from(modifier))
55 }
56
57 pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> {
58 for keybinding in KEYBINDINGS {
59 let modifier_mask = modifiers_to_mask(keybinding.modifiers);
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 }