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