tony / oxwm

Added tag functionality, fixed issue where tag was killing windows, added alacritty to devshell, added options in just file Browse files
tonybtw committed 2025-10-03 parent 8c774ac commit 2eed31f6247b7f6adee04acebae6c5ef5cd0cdb2
Showing 5 changed files with 192 additions and 30 deletions. UnifiedSplit
+1 −1 flake.nix
@@ -14,9 +14,9 @@
14 buildInputs = [ 14 buildInputs = [
15 pkgs.rustc 15 pkgs.rustc
16 pkgs.cargo 16 pkgs.cargo
17 - pkgs.xorg.xorgserver
18 pkgs.xorg.xclock 17 pkgs.xorg.xclock
19 pkgs.xterm 18 pkgs.xterm
19 + pkgs.alacritty
20 pkgs.just 20 pkgs.just
21 ]; 21 ];
22 shellHook = '' 22 shellHook = ''
+13 −0 justfile
@@ -1,3 +1,16 @@
1 +build:
2 + cargo build --release
3 +
4 +install: build
5 + sudo cp target/release/oxwm /usr/local/bin/oxwm
6 + @echo "✓ oxwm installed. Restart X or hit Mod+Shift+R (when implemented)"
7 +
8 +uninstall:
9 + sudo rm -f /usr/local/bin/oxwm
10 +
11 +clean:
12 + cargo clean
13 +
1 test: 14 test:
2 pkill Xephyr || true 15 pkill Xephyr || true
3 Xephyr -screen 1280x800 :1 & sleep 1 16 Xephyr -screen 1280x800 :1 & sleep 1
+53 −5 src/keyboard/handlers.rs
@@ -9,6 +9,8 @@ pub enum KeyAction {
9 KillClient, 9 KillClient,
10 FocusStack, 10 FocusStack,
11 Quit, 11 Quit,
12 + ViewTag,
13 + MoveToTag,
12 None, 14 None,
13 } 15 }
14 16
@@ -47,27 +49,73 @@ const KEYBINDINGS: &[Key] = &[
47 &[KeyButMask::MOD1], 49 &[KeyButMask::MOD1],
48 keycodes::RETURN, 50 keycodes::RETURN,
49 KeyAction::Spawn, 51 KeyAction::Spawn,
50 - Arg::Str("xclock"), 52 + Arg::Str("alacritty"),
51 ), 53 ),
52 Key::new( 54 Key::new(
53 - &[KeyButMask::MOD1, KeyButMask::SHIFT], 55 + &[KeyButMask::MOD1],
54 keycodes::Q, 56 keycodes::Q,
55 KeyAction::KillClient, 57 KeyAction::KillClient,
56 Arg::None, 58 Arg::None,
57 ), 59 ),
58 - Key::new(&[KeyButMask::MOD1], keycodes::Q, KeyAction::Quit, Arg::None), 60 + Key::new(
61 + &[KeyButMask::MOD1, KeyButMask::SHIFT],
62 + keycodes::Q,
63 + KeyAction::Quit,
64 + Arg::None,
65 + ),
59 Key::new( 66 Key::new(
60 &[KeyButMask::MOD1], 67 &[KeyButMask::MOD1],
61 - keycodes::J, 68 + keycodes::K,
62 KeyAction::FocusStack, 69 KeyAction::FocusStack,
63 Arg::Int(1), 70 Arg::Int(1),
64 ), 71 ),
65 Key::new( 72 Key::new(
66 &[KeyButMask::MOD1], 73 &[KeyButMask::MOD1],
67 - keycodes::K, 74 + keycodes::J,
68 KeyAction::FocusStack, 75 KeyAction::FocusStack,
69 Arg::Int(-1), 76 Arg::Int(-1),
70 ), 77 ),
78 + // NEW: Tag bindings (Mod+1 through Mod+9)
79 + Key::new(
80 + &[KeyButMask::MOD1],
81 + keycodes::KEY_1,
82 + KeyAction::ViewTag,
83 + Arg::Int(0),
84 + ),
85 + Key::new(
86 + &[KeyButMask::MOD1],
87 + keycodes::KEY_2,
88 + KeyAction::ViewTag,
89 + Arg::Int(1),
90 + ),
91 + Key::new(
92 + &[KeyButMask::MOD1],
93 + keycodes::KEY_3,
94 + KeyAction::ViewTag,
95 + Arg::Int(2),
96 + ),
97 + // ... add KEY_4 through KEY_9
98 +
99 + // NEW: Move window to tag (Mod+Shift+1 through Mod+Shift+9)
100 + Key::new(
101 + &[KeyButMask::MOD1, KeyButMask::SHIFT],
102 + keycodes::KEY_1,
103 + KeyAction::MoveToTag,
104 + Arg::Int(0),
105 + ),
106 + Key::new(
107 + &[KeyButMask::MOD1, KeyButMask::SHIFT],
108 + keycodes::KEY_2,
109 + KeyAction::MoveToTag,
110 + Arg::Int(1),
111 + ),
112 + Key::new(
113 + &[KeyButMask::MOD1, KeyButMask::SHIFT],
114 + keycodes::KEY_3,
115 + KeyAction::MoveToTag,
116 + Arg::Int(2),
117 + ),
118 + // ... etc
71 ]; 119 ];
72 120
73 fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 { 121 fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 {
+9 −4 src/keyboard/keycodes.rs
@@ -1,4 +1,6 @@
1 -// wip 1 +#![allow(dead_code)]
2 +// Allowing dead code here because its just a file for potential keybinds
3 +
2 pub const RETURN: u8 = 36; 4 pub const RETURN: u8 = 36;
3 pub const Q: u8 = 24; 5 pub const Q: u8 = 24;
4 pub const ESCAPE: u8 = 9; 6 pub const ESCAPE: u8 = 9;
@@ -7,13 +9,11 @@ pub const TAB: u8 = 23;
7 pub const BACKSPACE: u8 = 22; 9 pub const BACKSPACE: u8 = 22;
8 pub const DELETE: u8 = 119; 10 pub const DELETE: u8 = 119;
9 11
10 -// Function keys
11 pub const F1: u8 = 67; 12 pub const F1: u8 = 67;
12 pub const F2: u8 = 68; 13 pub const F2: u8 = 68;
13 pub const F3: u8 = 69; 14 pub const F3: u8 = 69;
14 pub const F4: u8 = 70; 15 pub const F4: u8 = 70;
15 16
16 -// Letters (assuming QWERTY)
17 pub const A: u8 = 38; 17 pub const A: u8 = 38;
18 pub const S: u8 = 39; 18 pub const S: u8 = 39;
19 pub const D: u8 = 40; 19 pub const D: u8 = 40;
@@ -22,7 +22,12 @@ pub const J: u8 = 44;
22 pub const K: u8 = 45; 22 pub const K: u8 = 45;
23 pub const L: u8 = 46; 23 pub const L: u8 = 46;
24 24
25 -// Numbers
26 pub const KEY_1: u8 = 10; 25 pub const KEY_1: u8 = 10;
27 pub const KEY_2: u8 = 11; 26 pub const KEY_2: u8 = 11;
28 pub const KEY_3: u8 = 12; 27 pub const KEY_3: u8 = 12;
28 +pub const KEY_4: u8 = 13;
29 +pub const KEY_5: u8 = 14;
30 +pub const KEY_6: u8 = 15;
31 +pub const KEY_7: u8 = 16;
32 +pub const KEY_8: u8 = 17;
33 +pub const KEY_9: u8 = 18;
+116 −20 src/window_manager.rs
@@ -9,6 +9,15 @@ use x11rb::protocol::Event;
9 use x11rb::protocol::xproto::*; 9 use x11rb::protocol::xproto::*;
10 use x11rb::rust_connection::RustConnection; 10 use x11rb::rust_connection::RustConnection;
11 11
12 +// TODO: move this to its own file? (tag.rs?)
13 +pub type TagMask = u32;
14 +
15 +pub const TAG_COUNT: usize = 9;
16 +
17 +pub fn tag_mask(tag: usize) -> TagMask {
18 + 1 << tag
19 +}
20 +
12 pub struct WindowManager { 21 pub struct WindowManager {
13 connection: RustConnection, 22 connection: RustConnection,
14 screen_number: usize, 23 screen_number: usize,
@@ -17,6 +26,8 @@ pub struct WindowManager {
17 windows: Vec<Window>, 26 windows: Vec<Window>,
18 focused_window: Option<Window>, 27 focused_window: Option<Window>,
19 layout: Box<dyn Layout>, 28 layout: Box<dyn Layout>,
29 + window_tags: std::collections::HashMap<Window, TagMask>,
30 + selected_tags: TagMask,
20 } 31 }
21 32
22 impl WindowManager { 33 impl WindowManager {
@@ -45,6 +56,8 @@ impl WindowManager {
45 windows: Vec::new(), 56 windows: Vec::new(),
46 focused_window: None, 57 focused_window: None,
47 layout: Box::new(TilingLayout), 58 layout: Box::new(TilingLayout),
59 + window_tags: std::collections::HashMap::new(),
60 + selected_tags: tag_mask(0),
48 }); 61 });
49 } 62 }
50 63
@@ -95,6 +108,16 @@ impl WindowManager {
95 println!("Quitting window manager"); 108 println!("Quitting window manager");
96 std::process::exit(0); 109 std::process::exit(0);
97 } 110 }
111 + KeyAction::ViewTag => {
112 + if let Arg::Int(tag_index) = arg {
113 + self.view_tag(*tag_index as usize)?;
114 + }
115 + }
116 + KeyAction::MoveToTag => {
117 + if let Arg::Int(tag_index) = arg {
118 + self.move_to_tag(*tag_index as usize)?;
119 + }
120 + }
98 KeyAction::None => { 121 KeyAction::None => {
99 //no-op 122 //no-op
100 } 123 }
@@ -102,28 +125,89 @@ impl WindowManager {
102 Ok(()) 125 Ok(())
103 } 126 }
104 127
128 + fn is_window_visible(&self, window: Window) -> bool {
129 + if let Some(&tags) = self.window_tags.get(&window) {
130 + (tags & self.selected_tags) != 0
131 + } else {
132 + false
133 + }
134 + }
135 +
136 + fn visible_windows(&self) -> Vec<Window> {
137 + self.windows
138 + .iter()
139 + .filter(|&&w| self.is_window_visible(w))
140 + .copied()
141 + .collect()
142 + }
143 +
144 + fn update_window_visibility(&self) -> Result<()> {
145 + for &window in &self.windows {
146 + if self.is_window_visible(window) {
147 + self.connection.map_window(window)?;
148 + } else {
149 + self.connection.unmap_window(window)?;
150 + }
151 + }
152 + self.connection.flush()?;
153 + Ok(())
154 + }
155 +
156 + pub fn view_tag(&mut self, tag_index: usize) -> Result<()> {
157 + if tag_index >= TAG_COUNT {
158 + return Ok(());
159 + }
160 +
161 + self.selected_tags = tag_mask(tag_index);
162 + println!("Viewing tag {}", tag_index + 1);
163 +
164 + self.update_window_visibility()?;
165 + self.apply_layout()?;
166 +
167 + let visible = self.visible_windows();
168 + self.set_focus(visible.first().copied())?;
169 +
170 + Ok(())
171 + }
172 +
173 + pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> {
174 + if tag_index >= TAG_COUNT {
175 + return Ok(());
176 + }
177 +
178 + if let Some(focused) = self.focused_window {
179 + let mask = tag_mask(tag_index);
180 + self.window_tags.insert(focused, mask);
181 +
182 + println!("Moved window {} to tag {}", focused, tag_index + 1);
183 +
184 + self.update_window_visibility()?;
185 + self.apply_layout()?;
186 + }
187 +
188 + Ok(())
189 + }
190 +
105 pub fn cycle_focus(&mut self, direction: i32) -> Result<()> { 191 pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
106 - if self.windows.is_empty() { 192 + let visible = self.visible_windows();
193 +
194 + if visible.is_empty() {
107 return Ok(()); 195 return Ok(());
108 } 196 }
109 197
110 let next_window = if let Some(current) = self.focused_window { 198 let next_window = if let Some(current) = self.focused_window {
111 - if let Some(current_index) = self.windows.iter().position(|&w| w == current) { 199 + if let Some(current_index) = visible.iter().position(|&w| w == current) {
112 let next_index = if direction > 0 { 200 let next_index = if direction > 0 {
113 - (current_index + 1) % self.windows.len() 201 + (current_index + 1) % visible.len()
114 } else { 202 } else {
115 - if current_index == 0 { 203 + (current_index + visible.len() - 1) % visible.len()
116 - self.windows.len() - 1
117 - } else {
118 - current_index - 1
119 - }
120 }; 204 };
121 - self.windows[next_index] 205 + visible[next_index]
122 } else { 206 } else {
123 - self.windows[0] 207 + visible[0]
124 } 208 }
125 } else { 209 } else {
126 - self.windows[0] 210 + visible[0]
127 }; 211 };
128 212
129 self.set_focus(Some(next_window))?; 213 self.set_focus(Some(next_window))?;
@@ -174,15 +258,29 @@ impl WindowManager {
174 Event::MapRequest(event) => { 258 Event::MapRequest(event) => {
175 self.connection.map_window(event.window)?; 259 self.connection.map_window(event.window)?;
176 self.windows.push(event.window); 260 self.windows.push(event.window);
261 + self.window_tags.insert(event.window, self.selected_tags);
177 self.apply_layout()?; 262 self.apply_layout()?;
178 self.set_focus(Some(event.window))?; 263 self.set_focus(Some(event.window))?;
179 } 264 }
180 Event::UnmapNotify(event) => { 265 Event::UnmapNotify(event) => {
181 if self.windows.contains(&event.window) { 266 if self.windows.contains(&event.window) {
182 - println!("Window {} unmapped, removing from layout", event.window); 267 + if self.is_window_visible(event.window) {
183 - self.remove_window(event.window)?; 268 + println!("Visible window {} unmapped, removing", event.window);
269 + self.remove_window(event.window)?;
270 + } else {
271 + println!(
272 + "Hidden window {} unmapped (expected), ignoring",
273 + event.window
274 + );
275 + }
184 } 276 }
185 } 277 }
278 + // Event::UnmapNotify(event) => {
279 + // if self.windows.contains(&event.window) {
280 + // println!("Window {} unmapped, removing from layout", event.window);
281 + // self.remove_window(event.window)?;
282 + // }
283 + // }
186 Event::DestroyNotify(event) => { 284 Event::DestroyNotify(event) => {
187 if self.windows.contains(&event.window) { 285 if self.windows.contains(&event.window) {
188 println!("Window {} destroyed, removing from layout", event.window); 286 println!("Window {} destroyed, removing from layout", event.window);
@@ -203,12 +301,10 @@ impl WindowManager {
203 let screen_height = self.screen.height_in_pixels as u32; 301 let screen_height = self.screen.height_in_pixels as u32;
204 let border_width = 2u32; 302 let border_width = 2u32;
205 303
206 - let geometries = self 304 + let visible = self.visible_windows();
207 - .layout 305 + let geometries = self.layout.arrange(&visible, screen_width, screen_height);
208 - .arrange(&self.windows, screen_width, screen_height);
209 306
210 - for (window, geometry) in self.windows.iter().zip(geometries.iter()) { 307 + for (window, geometry) in visible.iter().zip(geometries.iter()) {
211 - // Adjust for full borders
212 let adjusted_width = geometry.width.saturating_sub(2 * border_width); 308 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
213 let adjusted_height = geometry.height.saturating_sub(2 * border_width); 309 let adjusted_height = geometry.height.saturating_sub(2 * border_width);
214 310
@@ -222,12 +318,13 @@ impl WindowManager {
222 )?; 318 )?;
223 } 319 }
224 self.connection.flush()?; 320 self.connection.flush()?;
225 - return Ok(()); 321 + Ok(())
226 } 322 }
227 323
228 fn remove_window(&mut self, window: Window) -> Result<()> { 324 fn remove_window(&mut self, window: Window) -> Result<()> {
229 let initial_count = self.windows.len(); 325 let initial_count = self.windows.len();
230 self.windows.retain(|&w| w != window); 326 self.windows.retain(|&w| w != window);
327 + self.window_tags.remove(&window);
231 328
232 if self.windows.len() < initial_count { 329 if self.windows.len() < initial_count {
233 println!("Removed window {} from management", window); 330 println!("Removed window {} from management", window);
@@ -243,7 +340,6 @@ impl WindowManager {
243 340
244 self.apply_layout()?; 341 self.apply_layout()?;
245 } 342 }
246 -
247 Ok(()) 343 Ok(())
248 } 344 }
249 } 345 }