tony / oxwm

Branch: 0335582
Switch branches/tags
master
tonybtw Updated for commit message requests and added floating (normie layout) and bind to toggle layouts. 0335582 2025-10-25
136 lines · 3.3 KB Raw Blame History
1 use std::io;
2 use std::process::Command;
3
4 use serde::Deserialize;
5 use x11rb::connection::Connection;
6 use x11rb::protocol::xproto::*;
7
8 use crate::errors::X11Error;
9
10 #[derive(Debug, Copy, Clone, Deserialize)]
11 pub enum KeyAction {
12 Spawn,
13 KillClient,
14 FocusStack,
15 Quit,
16 Restart,
17 Recompile,
18 ViewTag,
19 ToggleGaps,
20 ToggleFullScreen,
21 ToggleFloating,
22 ChangeLayout,
23 CycleLayout,
24 MoveToTag,
25 FocusMonitor,
26 None,
27 }
28
29 #[derive(Debug, Clone)]
30 pub enum Arg {
31 None,
32 Int(i32),
33 Str(String),
34 Array(Vec<String>),
35 }
36
37 impl Arg {
38 pub const fn none() -> Self {
39 Arg::None
40 }
41 }
42
43 #[derive(Clone)]
44 pub struct Key {
45 pub(crate) modifiers: Vec<KeyButMask>,
46 pub(crate) key: Keycode,
47 pub(crate) func: KeyAction,
48 pub(crate) arg: Arg,
49 }
50
51 impl Key {
52 pub fn new(modifiers: Vec<KeyButMask>, key: Keycode, func: KeyAction, arg: Arg) -> Self {
53 Self {
54 modifiers,
55 key,
56 func,
57 arg,
58 }
59 }
60 }
61
62 fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 {
63 modifiers
64 .iter()
65 .fold(0u16, |acc, &modifier| acc | u16::from(modifier))
66 }
67
68 pub fn setup_keybinds(
69 connection: &impl Connection,
70 root: Window,
71 keybindings: &[Key],
72 ) -> Result<(), X11Error> {
73 for keybinding in keybindings {
74 let modifier_mask = modifiers_to_mask(&keybinding.modifiers);
75
76 connection.grab_key(
77 false,
78 root,
79 modifier_mask.into(),
80 keybinding.key,
81 GrabMode::ASYNC,
82 GrabMode::ASYNC,
83 )?;
84 }
85 Ok(())
86 }
87
88 pub fn handle_key_press(event: KeyPressEvent, keybindings: &[Key]) -> (KeyAction, Arg) {
89 for keybinding in keybindings {
90 let modifier_mask = modifiers_to_mask(&keybinding.modifiers);
91
92 if event.detail == keybinding.key && event.state == modifier_mask.into() {
93 return (keybinding.func, keybinding.arg.clone());
94 }
95 }
96
97 (KeyAction::None, Arg::None)
98 }
99
100 pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> io::Result<()> {
101 use io::ErrorKind;
102 if let KeyAction::Spawn = action {
103 match arg {
104 Arg::Str(command) => match Command::new(command.as_str()).spawn() {
105 Err(err) if err.kind() == ErrorKind::NotFound => {
106 eprintln!(
107 "KeyAction::Spawn failed: could not spawn \"{}\", command not found",
108 command
109 );
110 }
111 Err(err) => Err(err)?,
112 _ => (),
113 },
114 Arg::Array(command) => {
115 let Some((cmd, args)) = command.split_first() else {
116 return Ok(());
117 };
118
119 let args_str: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
120 match Command::new(cmd.as_str()).args(&args_str).spawn() {
121 Err(err) if err.kind() == ErrorKind::NotFound => {
122 eprintln!(
123 "KeyAction::Spawn failed: could not spawn \"{}\", command not found",
124 cmd
125 );
126 }
127 Err(err) => Err(err)?,
128 _ => (),
129 }
130 }
131 _ => {}
132 }
133 }
134
135 Ok(())
136 }