tony / oxwm

Finished killing windows dymanically and correctly Browse files
tonybtw committed 2025-09-29 parent 7ed4196 commit b5923a9d80843926e1e90c170cbd916d2ebd95e2
Showing 4 changed files with 238 additions and 41 deletions. UnifiedSplit
+83 −12 readme.org
@@ -2,13 +2,84 @@
2 2 #+STARTUP: overview
3 3
4 4 * OXWM β€” DWM but Better (and oxidized)
5 -A dynamic window manager written in /RustπŸ¦€/, inspired by dwm but designed to evolve
5 +A dynamic window manager written in Rust, inspired by dwm but designed to evolve
6 6 on its own. Configuration is done in Rust source code, keeping with the suckless
7 7 philosophy of *"edit + recompile."*
8 8
9 9 This project is still in its early stages. Currently, it can claim ownership of
10 10 an X display and log incoming events.
11 11
12 +* Project Structure
13 +
14 +** src/
15 +*** main.rs
16 +- main() :: Creates WindowManager and calls .run()
17 +
18 +*** window_manager.rs (CORE)
19 +Handles all X11 events and window management
20 +
21 +**** struct WindowManager
22 +- connection :: X11 connection
23 +- windows :: Vec<Window> (all managed windows)
24 +- focused_window :: Option<Window>
25 +- layout :: Box<dyn Layout>
26 +
27 +**** Methods
28 +- new() :: Initialize WM, grab root window
29 +- run() :: Main event loop
30 +- handle_event() :: Routes X11 events
31 + - MapRequest β†’ add window, apply layout
32 + - UnmapNotify β†’ remove window
33 + - DestroyNotify β†’ remove window
34 + - KeyPress β†’ get action, handle it
35 +- handle_key_action() :: Execute keyboard actions
36 +- remove_window() :: Remove from Vec, reapply layout
37 +- set_focus() :: Focus window, update visuals
38 +- cycle_focus() :: Move focus to next window
39 +- update_focus_visuals() :: Set border colors
40 +- apply_layout() :: Position all windows
41 +
42 +*** keyboard/
43 +**** mod.rs
44 +- Re-exports public interface
45 +
46 +**** keycodes.rs
47 +- Key constants (Q, J, RETURN, etc)
48 +
49 +**** handlers.rs
50 +- enum KeyAction :: SpawnTerminal, CloseWindow, CycleWindow, Quit, None
51 +- setup_keybinds() :: Register keys with X11
52 +- handle_key_press() :: Parse key β†’ return KeyAction
53 +
54 +*** layout/
55 +**** mod.rs
56 +- Layout trait definition
57 +
58 +**** tiling.rs
59 +- TilingLayout :: Implementation
60 + - arrange() :: Calculate window positions
61 +
62 +* Event Flow
63 +
64 +1. X11 event arrives β†’ run() receives it
65 +2. handle_event() matches event type
66 +3. For KeyPress:
67 + - keyboard::handle_key_press() β†’ KeyAction
68 + - handle_key_action() executes action
69 +4. For Map/Unmap:
70 + - Modify windows Vec
71 + - apply_layout() repositions everything
72 + - update_focus_visuals() redraws borders
73 +
74 +* Key Bindings
75 +
76 +| Binding | Action |
77 +|---------------+----------------------|
78 +| Alt+Return | Spawn terminal |
79 +| Alt+J | Cycle focus |
80 +| Alt+Shift+Q | Close focused window |
81 +| Alt+Shift+Q | Quit WM |
82 +
12 83 * βš™ Installation β€” Running with Nix Flakes
13 84 You can set up a reproducible development environment with Rust, Cargo, Xephyr, xterm, and
14 85 just by using the flake.
@@ -37,19 +108,19 @@ This should open a new Xephyr window. oxwm will attach to it and log X11
37 108 events in your host terminal. Clients like xterm/xclock will appear inside Xephyr.
38 109
39 110 * OXWM Todo List:
40 -** Reorganization Tasks
41 -- [ ] Move keyboard module to folder structure:
42 - - [ ] Create =src/keyboard/mod.rs= with re-exports
43 - - [ ] Move constants to =src/keyboard/keycodes.rs=
44 - - [ ] Move key handlers to =src/keyboard/handlers.rs=
45 - - [ ] Update imports in main.rs and window_manager.rs
111 +** TODO Reorganization Tasks [1/2]
112 +- [X] Move keyboard module to folder structure:
113 + - [X] Create =src/keyboard/mod.rs= with re-exports
114 + - [X] Move constants to =src/keyboard/keycodes.rs=
115 + - [X] Move key handlers to =src/keyboard/handlers.rs=
116 + - [X] Update imports in main.rs and window_manager.rs
46 117 - [ ] Create =src/config.rs= in root directory for future configuration system
47 118
48 -** Core Window Management
49 -- [ ] Fix layout after program is closed (handle UnmapNotify events)
50 - - [ ] Add UnmapNotify to event handling
51 - - [ ] Remove closed windows from windows vector
52 - - [ ] Re-apply layout after window removal
119 +** TODO Core Window Management [1/2]
120 +- [X] Fix layout after program is closed (handle UnmapNotify events)
121 + - [X] Add UnmapNotify to event handling
122 + - [X] Remove closed windows from windows vector
123 + - [X] Re-apply layout after window removal
53 124 - [ ] Add keybind to swap focus between windows
54 125 - [ ] Track focused window in WindowManager struct
55 126 - [ ] Implement focus cycling logic
+32 −17 src/keyboard/handlers.rs
@@ -3,15 +3,25 @@ use anyhow::Result;
3 3 use x11rb::connection::Connection;
4 4 use x11rb::protocol::xproto::*;
5 5
6 +#[derive(Debug)]
7 +pub enum KeyAction {
8 + SpawnTerminal,
9 + CloseWindow,
10 + CycleWindow,
11 + Quit,
12 + None,
13 +}
14 +
6 15 pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()> {
7 16 connection.grab_key(
8 17 false,
9 18 root,
10 - ModMask::M4.into(),
19 + ModMask::M1.into(),
11 20 keycodes::RETURN,
12 21 GrabMode::ASYNC,
13 22 GrabMode::ASYNC,
14 23 )?;
24 +
15 25 connection.grab_key(
16 26 false,
17 27 root,
@@ -20,33 +30,38 @@ pub fn setup_keybinds(connection: &impl Connection, root: Window) -> Result<()>
20 30 GrabMode::ASYNC,
21 31 GrabMode::ASYNC,
22 32 )?;
33 +
23 34 connection.grab_key(
24 35 false,
25 36 root,
26 - ModMask::M4.into(),
37 + ModMask::M1.into(),
27 38 keycodes::Q,
28 39 GrabMode::ASYNC,
29 40 GrabMode::ASYNC,
30 41 )?;
42 +
43 + connection.grab_key(
44 + false,
45 + root,
46 + ModMask::M1.into(),
47 + keycodes::J,
48 + GrabMode::ASYNC,
49 + GrabMode::ASYNC,
50 + )?;
51 +
31 52 Ok(())
32 53 }
33 54
34 -pub fn handle_key_press(connection: &impl Connection, event: KeyPressEvent) -> Result<()> {
55 +pub fn handle_key_press(event: KeyPressEvent) -> Result<KeyAction> {
35 56 println!("KeyPress: detail={}, state={:?}", event.detail, event.state);
36 - match (event.detail, event.state) {
37 - (keycodes::RETURN, state) if state.contains(KeyButMask::MOD1) => {
38 - println!("Spawning terminal");
39 - std::process::Command::new("xterm").spawn()?;
40 - }
57 + let action = match (event.detail, event.state) {
58 + (keycodes::RETURN, state) if state.contains(KeyButMask::MOD1) => KeyAction::SpawnTerminal,
41 59 (keycodes::Q, state) if state.contains(KeyButMask::MOD1 | KeyButMask::SHIFT) => {
42 - println!("Closing focused window");
43 - let focus_reply = connection.get_input_focus()?.reply()?;
44 - if focus_reply.focus != x11rb::NONE && focus_reply.focus != event.root {
45 - connection.kill_client(focus_reply.focus)?;
46 - connection.flush()?;
47 - }
60 + KeyAction::CloseWindow
48 61 }
49 - _ => {}
50 - }
51 - Ok(())
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)
52 67 }
+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::{handle_key_press, setup_keybinds};
6 +pub use handlers::{KeyAction, handle_key_press, setup_keybinds}; // Add KeyAction here
7 7 pub use keycodes::*;
+122 −11 src/window_manager.rs
@@ -1,5 +1,4 @@
1 -use crate::keyboard;
2 -// use crate::keys;
1 +use crate::keyboard::{self, KeyAction}; // Add KeyAction to the import
3 2 use crate::layout::Layout;
4 3 use crate::layout::tiling::TilingLayout;
5 4
@@ -16,6 +15,7 @@ pub struct WindowManager {
16 15 root: Window,
17 16 screen: Screen,
18 17 windows: Vec<Window>,
18 + pub focused_window: Option<Window>,
19 19 layout: Box<dyn Layout>,
20 20 }
21 21
@@ -43,6 +43,7 @@ impl WindowManager {
43 43 root,
44 44 screen,
45 45 windows: Vec::new(),
46 + focused_window: None,
46 47 layout: Box::new(TilingLayout),
47 48 });
48 49 }
@@ -59,28 +60,116 @@ impl WindowManager {
59 60 }
60 61 }
61 62
63 + fn handle_key_action(&mut self, action: keyboard::KeyAction) -> Result<()> {
64 + match action {
65 + keyboard::KeyAction::SpawnTerminal => {
66 + println!("Spawning terminal");
67 + std::process::Command::new("xclock").spawn()?;
68 + }
69 + keyboard::KeyAction::CloseWindow => {
70 + println!("Closing focused window");
71 + if let Some(focused) = self.focused_window {
72 + match self.connection.kill_client(focused) {
73 + Ok(_) => {
74 + self.connection.flush()?;
75 + println!("Killed window {}", focused);
76 + }
77 + Err(e) => {
78 + println!("Failed to kill window {}: {}", focused, e);
79 + }
80 + }
81 + }
82 + }
83 + keyboard::KeyAction::CycleWindow => {
84 + println!("Cycling focus");
85 + self.cycle_focus()?;
86 + }
87 + keyboard::KeyAction::Quit => {
88 + println!("Quitting window manager");
89 + std::process::exit(0);
90 + }
91 + keyboard::KeyAction::None => {
92 + //no-op
93 + }
94 + }
95 + Ok(())
96 + }
97 + pub fn cycle_focus(&mut self) -> Result<()> {
98 + if self.windows.is_empty() {
99 + return Ok(());
100 + }
101 +
102 + let next_window = if let Some(current) = self.focused_window {
103 + if let Some(current_index) = self.windows.iter().position(|&w| w == current) {
104 + let next_index = (current_index + 1) % self.windows.len();
105 + self.windows[next_index]
106 + } else {
107 + self.windows[0]
108 + }
109 + } else {
110 + self.windows[0]
111 + };
112 +
113 + self.set_focus(Some(next_window))?;
114 + Ok(())
115 + }
116 +
117 + pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
118 + self.focused_window = window;
119 +
120 + if let Some(win) = window {
121 + self.connection
122 + .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
123 + }
124 +
125 + self.update_focus_visuals()?;
126 + Ok(())
127 + }
128 +
129 + fn update_focus_visuals(&self) -> Result<()> {
130 + for &window in &self.windows {
131 + let border_color = if self.focused_window == Some(window) {
132 + 0xff0000
133 + } else {
134 + 0x888888
135 + };
136 +
137 + self.connection.change_window_attributes(
138 + window,
139 + &ChangeWindowAttributesAux::new().border_pixel(border_color),
140 + )?;
141 + }
142 + Ok(())
143 + }
62 144 fn handle_event(&mut self, event: Event) -> Result<()> {
63 145 match event {
64 146 Event::MapRequest(event) => {
65 147 self.connection.map_window(event.window)?;
66 148 self.windows.push(event.window);
67 149 self.apply_layout()?;
68 - self.connection.set_input_focus(
69 - InputFocus::POINTER_ROOT,
70 - event.window,
71 - x11rb::CURRENT_TIME,
72 - )?;
150 + self.set_focus(Some(event.window))?;
73 151 self.connection.flush()?;
74 152 }
153 + Event::UnmapNotify(event) => {
154 + if self.windows.contains(&event.window) {
155 + println!("Window {} unmapped, removing from layout", event.window);
156 + self.remove_window(event.window)?;
157 + }
158 + }
159 + Event::DestroyNotify(event) => {
160 + if self.windows.contains(&event.window) {
161 + println!("Window {} destroyed, removing from layout", event.window);
162 + self.remove_window(event.window)?;
163 + }
164 + }
75 165 Event::KeyPress(event) => {
76 - println!("KeyPress event received!");
77 - keyboard::handle_key_press(&self.connection, event)?;
166 + let action = keyboard::handle_key_press(event)?; // Remove &self.connection
167 + self.handle_key_action(action)?;
78 168 }
79 169 _ => {}
80 170 }
81 - return Ok(());
171 + Ok(())
82 172 }
83 -
84 173 fn apply_layout(&self) -> Result<()> {
85 174 let screen_width = self.screen.width_in_pixels as u32;
86 175 let screen_height = self.screen.height_in_pixels as u32;
@@ -102,4 +191,26 @@ impl WindowManager {
102 191 }
103 192 return Ok(());
104 193 }
194 +
195 + fn remove_window(&mut self, window: Window) -> Result<()> {
196 + let initial_count = self.windows.len();
197 + self.windows.retain(|&w| w != window);
198 +
199 + if self.windows.len() < initial_count {
200 + println!("Removed window {} from management", window);
201 +
202 + if self.focused_window == Some(window) {
203 + self.focused_window = if self.windows.is_empty() {
204 + None
205 + } else {
206 + Some(self.windows[self.windows.len() - 1])
207 + };
208 + }
209 +
210 + self.apply_layout()?;
211 + self.connection.flush()?;
212 + }
213 +
214 + Ok(())
215 + }
105 216 }