tony / oxwm

Branch: 3f51577
Switch branches/tags
master
tonybtw Added support for multiple monitors using randr library and dwm.c logic. Applied bar updates for multi monitor support. 84c9fb0 2025-10-17
134 lines · 3.2 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 MoveToTag,
23 FocusMonitor,
24 None,
25 }
26
27 #[derive(Debug, Clone)]
28 pub enum Arg {
29 None,
30 Int(i32),
31 Str(String),
32 Array(Vec<String>),
33 }
34
35 impl Arg {
36 pub const fn none() -> Self {
37 Arg::None
38 }
39 }
40
41 #[derive(Clone)]
42 pub struct Key {
43 pub(crate) modifiers: Vec<KeyButMask>,
44 pub(crate) key: Keycode,
45 pub(crate) func: KeyAction,
46 pub(crate) arg: Arg,
47 }
48
49 impl Key {
50 pub fn new(modifiers: Vec<KeyButMask>, key: Keycode, func: KeyAction, arg: Arg) -> Self {
51 Self {
52 modifiers,
53 key,
54 func,
55 arg,
56 }
57 }
58 }
59
60 fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 {
61 modifiers
62 .iter()
63 .fold(0u16, |acc, &modifier| acc | u16::from(modifier))
64 }
65
66 pub fn setup_keybinds(
67 connection: &impl Connection,
68 root: Window,
69 keybindings: &[Key],
70 ) -> Result<(), X11Error> {
71 for keybinding in keybindings {
72 let modifier_mask = modifiers_to_mask(&keybinding.modifiers);
73
74 connection.grab_key(
75 false,
76 root,
77 modifier_mask.into(),
78 keybinding.key,
79 GrabMode::ASYNC,
80 GrabMode::ASYNC,
81 )?;
82 }
83 Ok(())
84 }
85
86 pub fn handle_key_press(event: KeyPressEvent, keybindings: &[Key]) -> (KeyAction, Arg) {
87 for keybinding in keybindings {
88 let modifier_mask = modifiers_to_mask(&keybinding.modifiers);
89
90 if event.detail == keybinding.key && event.state == modifier_mask.into() {
91 return (keybinding.func, keybinding.arg.clone());
92 }
93 }
94
95 (KeyAction::None, Arg::None)
96 }
97
98 pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> io::Result<()> {
99 use io::ErrorKind;
100 if let KeyAction::Spawn = action {
101 match arg {
102 Arg::Str(command) => match Command::new(command.as_str()).spawn() {
103 Err(err) if err.kind() == ErrorKind::NotFound => {
104 eprintln!(
105 "KeyAction::Spawn failed: could not spawn \"{}\", command not found",
106 command
107 );
108 }
109 Err(err) => Err(err)?,
110 _ => (),
111 },
112 Arg::Array(command) => {
113 let Some((cmd, args)) = command.split_first() else {
114 return Ok(());
115 };
116
117 let args_str: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
118 match Command::new(cmd.as_str()).args(&args_str).spawn() {
119 Err(err) if err.kind() == ErrorKind::NotFound => {
120 eprintln!(
121 "KeyAction::Spawn failed: could not spawn \"{}\", command not found",
122 cmd
123 );
124 }
125 Err(err) => Err(err)?,
126 _ => (),
127 }
128 }
129 _ => {}
130 }
131 }
132
133 Ok(())
134 }