tony / oxwm

Fixed border issue on focus, changed keymaps to be modular, added j/k functionality to iterate stack Browse files
tonybtw committed 2025-09-29 parent f198c24 commit 8c774ace1ea367d7d290895a8df02a2d0706798a
Showing 3 changed files with 145 additions and 124 deletions. UnifiedSplit
+86 −98 src/keyboard/handlers.rs
@@ -3,117 +3,105 @@ use anyhow::Result;
3 3 use x11rb::connection::Connection;
4 4 use x11rb::protocol::xproto::*;
5 5
6 -#[derive(Debug)]
6 +#[derive(Debug, Copy, Clone)]
7 7 pub enum KeyAction {
8 - SpawnTerminal,
9 - CloseWindow,
10 - CycleWindow,
8 + Spawn,
9 + KillClient,
10 + FocusStack,
11 11 Quit,
12 12 None,
13 13 }
14 14
15 -pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> {
16 - connection.grab_key(
17 - false,
18 - root,
19 - ModMask::M1.into(),
20 - keycodes::RETURN,
21 - GrabMode::ASYNC,
22 - GrabMode::ASYNC,
23 - )?;
15 +#[derive(Debug)]
16 +pub enum Arg {
17 + Str(&'static str),
18 + Int(i32),
19 + None,
20 +}
24 21
25 - connection.grab_key(
26 - false,
27 - root,
28 - (ModMask::M1 | ModMask::SHIFT).into(),
29 - keycodes::Q,
30 - GrabMode::ASYNC,
31 - GrabMode::ASYNC,
32 - )?;
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 +}
33 28
34 - connection.grab_key(
35 - false,
36 - root,
37 - ModMask::M1.into(),
38 - keycodes::Q,
39 - GrabMode::ASYNC,
40 - GrabMode::ASYNC,
41 - )?;
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 +}
42 44
43 - connection.grab_key(
44 - false,
45 - root,
46 - ModMask::M1.into(),
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],
47 61 keycodes::J,
48 - GrabMode::ASYNC,
49 - GrabMode::ASYNC,
50 - )?;
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);
51 82
83 + connection.grab_key(
84 + false,
85 + root,
86 + modifier_mask.into(),
87 + keybinding.key,
88 + GrabMode::ASYNC,
89 + GrabMode::ASYNC,
90 + )?;
91 + }
52 92 Ok(())
53 93 }
54 94
55 -pub fn handle_key_press(event: KeyPressEvent) -> Result<KeyAction> {
95 +pub fn handle_key_press(event: KeyPressEvent) -> Result<(KeyAction, &'static Arg)> {
56 96 println!("KeyPress: detail={}, state={:?}", event.detail, event.state);
57 - let action = match (event.detail, event.state) {
58 - (keycodes::RETURN, state) if state.contains(KeyButMask::MOD1) => KeyAction::SpawnTerminal,
59 - (keycodes::Q, state) if state.contains(KeyButMask::MOD1 | KeyButMask::SHIFT) => {
60 - KeyAction::CloseWindow
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));
61 103 }
62 - (keycodes::Q, state) if state.contains(KeyButMask::MOD1) => KeyAction::Quit,
63 - (keycodes::J, state) if state.contains(KeyButMask::MOD1) => KeyAction::CycleWindow,
64 - _ => KeyAction::None,
65 - };
66 - Ok(action)
67 -}
104 + }
68 105
69 -// pub struct Key {
70 -// pub(crate) modifiers: &'static [KeyButMask], // List of modifiers
71 -// pub(crate) key: Keycode,
72 -// pub(crate) func: KeyAction,
73 -// pub(crate) arg: Arg,
74 -// }
75 -//
76 -// impl Key {
77 -// pub const fn new(
78 -// modifiers: &'static [KeyButMask],
79 -// key: Keycode,
80 -// func: KeyAction,
81 -// arg: Arg,
82 -// ) -> Self {
83 -// Self {
84 -// modifiers,
85 -// key,
86 -// func,
87 -// arg,
88 -// }
89 -// }
90 -// }
91 -//
92 -// const KEYBINDINGS: &[Key] = &[
93 -// Key::new(
94 -// &[KeyButMask::MOD1],
95 -// keycodes::RETURN,
96 -// KeyAction::Spawn,
97 -// Arg::Str("xclock"),
98 -// ),
99 -// Key::new(
100 -// &[KeyButMask::MOD1, KeyButMask::SHIFT],
101 -// keycodes::Q,
102 -// KeyAction::KillClient,
103 -// Arg::None,
104 -// ),
105 -// Key::new(&[KeyButMask::MOD1], keycodes::Q, KeyAction::Quit, Arg::None),
106 -// Key::new(
107 -// &[KeyButMask::MOD1],
108 -// keycodes::J,
109 -// KeyAction::FocusStack,
110 -// Arg::Int(1),
111 -// ),
112 -// Key::new(
113 -// &[KeyButMask::MOD1],
114 -// keycodes::K,
115 -// KeyAction::FocusStack,
116 -// Arg::Int(-1),
117 -// ),
118 -// ];
119 -//
106 + Ok((KeyAction::None, &Arg::None))
107 +}
+1 −1 src/keyboard/mod.rs
@@ -3,5 +3,5 @@ pub mod handlers;
3 3 pub mod keycodes;
4 4
5 5 // Re-export commonly used items for convenience
6 -pub use handlers::{KeyAction, handle_key_press, setup_keybinds}; // Add KeyAction here
6 +pub use handlers::{Arg, KeyAction, handle_key_press, setup_keybinds}; // Add KeyAction here
7 7 pub use keycodes::*;
+58 −25 src/window_manager.rs
@@ -1,4 +1,4 @@
1 -use crate::keyboard::{self, KeyAction}; // Add KeyAction to the import
1 +use crate::keyboard::{self, Arg, KeyAction};
2 2 use crate::layout::Layout;
3 3 use crate::layout::tiling::TilingLayout;
4 4
@@ -15,7 +15,7 @@ pub struct WindowManager {
15 15 root: Window,
16 16 screen: Screen,
17 17 windows: Vec<Window>,
18 - pub focused_window: Option<Window>,
18 + focused_window: Option<Window>,
19 19 layout: Box<dyn Layout>,
20 20 }
21 21
@@ -60,13 +60,15 @@ impl WindowManager {
60 60 }
61 61 }
62 62
63 - fn handle_key_action(&mut self, action: keyboard::KeyAction) -> Result<()> {
63 + fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
64 64 match action {
65 - keyboard::KeyAction::SpawnTerminal => {
66 - println!("Spawning terminal");
67 - std::process::Command::new("xclock").spawn()?;
65 + KeyAction::Spawn => {
66 + if let Arg::Str(command) = arg {
67 + println!("Spawning: {}", command);
68 + std::process::Command::new(command).spawn()?;
69 + }
68 70 }
69 - keyboard::KeyAction::CloseWindow => {
71 + KeyAction::KillClient => {
70 72 println!("Closing focused window");
71 73 if let Some(focused) = self.focused_window {
72 74 match self.connection.kill_client(focused) {
@@ -80,28 +82,42 @@ impl WindowManager {
80 82 }
81 83 }
82 84 }
83 - keyboard::KeyAction::CycleWindow => {
84 - println!("Cycling focus");
85 - self.cycle_focus()?;
85 + KeyAction::FocusStack => {
86 + if let Arg::Int(direction) = arg {
87 + println!("FocusStack called with direction: {}", direction);
88 + println!("Windows: {:?}", self.windows);
89 + println!("Current focused: {:?}", self.focused_window);
90 + self.cycle_focus(*direction)?;
91 + println!("New focused: {:?}", self.focused_window);
92 + }
86 93 }
87 - keyboard::KeyAction::Quit => {
94 + KeyAction::Quit => {
88 95 println!("Quitting window manager");
89 96 std::process::exit(0);
90 97 }
91 - keyboard::KeyAction::None => {
98 + KeyAction::None => {
92 99 //no-op
93 100 }
94 101 }
95 102 Ok(())
96 103 }
97 - pub fn cycle_focus(&mut self) -> Result<()> {
104 +
105 + pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
98 106 if self.windows.is_empty() {
99 107 return Ok(());
100 108 }
101 109
102 110 let next_window = if let Some(current) = self.focused_window {
103 111 if let Some(current_index) = self.windows.iter().position(|&w| w == current) {
104 - let next_index = (current_index + 1) % self.windows.len();
112 + let next_index = if direction > 0 {
113 + (current_index + 1) % self.windows.len()
114 + } else {
115 + if current_index == 0 {
116 + self.windows.len() - 1
117 + } else {
118 + current_index - 1
119 + }
120 + };
105 121 self.windows[next_index]
106 122 } else {
107 123 self.windows[0]
@@ -115,11 +131,13 @@ impl WindowManager {
115 131 }
116 132
117 133 pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
134 + println!("set_focus called with: {:?}", window);
118 135 self.focused_window = window;
119 136
120 137 if let Some(win) = window {
121 138 self.connection
122 139 .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
140 + self.connection.flush()?;
123 141 }
124 142
125 143 self.update_focus_visuals()?;
@@ -127,20 +145,30 @@ impl WindowManager {
127 145 }
128 146
129 147 fn update_focus_visuals(&self) -> Result<()> {
148 + println!("Updating focus visuals for {} windows", self.windows.len());
130 149 for &window in &self.windows {
131 - let border_color = if self.focused_window == Some(window) {
132 - 0xff0000
150 + let (border_color, border_width) = if self.focused_window == Some(window) {
151 + println!("Window {} is FOCUSED (red border)", window);
152 + (0xff0000, 2)
133 153 } else {
134 - 0x888888
154 + println!("Window {} is unfocused (gray border)", window);
155 + (0x888888, 2)
135 156 };
136 157
158 + self.connection.configure_window(
159 + window,
160 + &ConfigureWindowAux::new().border_width(border_width),
161 + )?;
162 +
137 163 self.connection.change_window_attributes(
138 164 window,
139 165 &ChangeWindowAttributesAux::new().border_pixel(border_color),
140 166 )?;
141 167 }
168 + self.connection.flush()?;
142 169 Ok(())
143 170 }
171 +
144 172 fn handle_event(&mut self, event: Event) -> Result<()> {
145 173 match event {
146 174 Event::MapRequest(event) => {
@@ -148,7 +176,6 @@ impl WindowManager {
148 176 self.windows.push(event.window);
149 177 self.apply_layout()?;
150 178 self.set_focus(Some(event.window))?;
151 - self.connection.flush()?;
152 179 }
153 180 Event::UnmapNotify(event) => {
154 181 if self.windows.contains(&event.window) {
@@ -163,32 +190,38 @@ impl WindowManager {
163 190 }
164 191 }
165 192 Event::KeyPress(event) => {
166 - let action = keyboard::handle_key_press(event)?; // Remove &self.connection
167 - self.handle_key_action(action)?;
193 + let (action, arg) = keyboard::handle_key_press(event)?;
194 + self.handle_key_action(action, arg)?;
168 195 }
169 196 _ => {}
170 197 }
171 198 Ok(())
172 199 }
200 +
173 201 fn apply_layout(&self) -> Result<()> {
174 202 let screen_width = self.screen.width_in_pixels as u32;
175 203 let screen_height = self.screen.height_in_pixels as u32;
204 + let border_width = 2u32;
176 205
177 206 let geometries = self
178 207 .layout
179 208 .arrange(&self.windows, screen_width, screen_height);
180 209
181 210 for (window, geometry) in self.windows.iter().zip(geometries.iter()) {
211 + // Adjust for full borders
212 + let adjusted_width = geometry.width.saturating_sub(2 * border_width);
213 + let adjusted_height = geometry.height.saturating_sub(2 * border_width);
214 +
182 215 self.connection.configure_window(
183 216 *window,
184 217 &ConfigureWindowAux::new()
185 218 .x(geometry.x_coordinate)
186 219 .y(geometry.y_coordinate)
187 - .width(geometry.width)
188 - .height(geometry.height)
189 - .border_width(1),
220 + .width(adjusted_width)
221 + .height(adjusted_height),
190 222 )?;
191 223 }
224 + self.connection.flush()?;
192 225 return Ok(());
193 226 }
194 227
@@ -200,15 +233,15 @@ impl WindowManager {
200 233 println!("Removed window {} from management", window);
201 234
202 235 if self.focused_window == Some(window) {
203 - self.focused_window = if self.windows.is_empty() {
236 + let new_focus = if self.windows.is_empty() {
204 237 None
205 238 } else {
206 239 Some(self.windows[self.windows.len() - 1])
207 240 };
241 + self.set_focus(new_focus)?;
208 242 }
209 243
210 244 self.apply_layout()?;
211 - self.connection.flush()?;
212 245 }
213 246
214 247 Ok(())