tony / oxwm

Branch: 2eed31f
Switch branches/tags
master
tonybtw Added tag functionality, fixed issue where tag was killing windows, added alacritty to devshell, added options in just file 2eed31f 2025-10-03
345 lines · 11.1 KB Raw Blame History
1 use crate::keyboard::{self, Arg, KeyAction};
2 use crate::layout::Layout;
3 use crate::layout::tiling::TilingLayout;
4
5 use anyhow::Result;
6
7 use x11rb::connection::Connection;
8 use x11rb::protocol::Event;
9 use x11rb::protocol::xproto::*;
10 use x11rb::rust_connection::RustConnection;
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
21 pub struct WindowManager {
22 connection: RustConnection,
23 screen_number: usize,
24 root: Window,
25 screen: Screen,
26 windows: Vec<Window>,
27 focused_window: Option<Window>,
28 layout: Box<dyn Layout>,
29 window_tags: std::collections::HashMap<Window, TagMask>,
30 selected_tags: TagMask,
31 }
32
33 impl WindowManager {
34 pub fn new() -> Result<Self> {
35 let (connection, screen_number) = x11rb::connect(None)?;
36 let root = connection.setup().roots[screen_number].root;
37 let screen = connection.setup().roots[screen_number].clone();
38
39 connection
40 .change_window_attributes(
41 root,
42 &ChangeWindowAttributesAux::new().event_mask(
43 EventMask::SUBSTRUCTURE_REDIRECT
44 | EventMask::SUBSTRUCTURE_NOTIFY
45 | EventMask::PROPERTY_CHANGE
46 | EventMask::KEY_PRESS,
47 ),
48 )?
49 .check()?;
50
51 return Ok(Self {
52 connection,
53 screen_number,
54 root,
55 screen,
56 windows: Vec::new(),
57 focused_window: None,
58 layout: Box::new(TilingLayout),
59 window_tags: std::collections::HashMap::new(),
60 selected_tags: tag_mask(0),
61 });
62 }
63
64 pub fn run(&mut self) -> Result<()> {
65 println!("oxwm started on display {}", self.screen_number);
66
67 keyboard::setup_keybinds(&self.connection, self.root)?;
68
69 loop {
70 let event = self.connection.wait_for_event()?;
71 println!("event: {:?}", event);
72 self.handle_event(event)?;
73 }
74 }
75
76 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
77 match action {
78 KeyAction::Spawn => {
79 if let Arg::Str(command) = arg {
80 println!("Spawning: {}", command);
81 std::process::Command::new(command).spawn()?;
82 }
83 }
84 KeyAction::KillClient => {
85 println!("Closing focused window");
86 if let Some(focused) = self.focused_window {
87 match self.connection.kill_client(focused) {
88 Ok(_) => {
89 self.connection.flush()?;
90 println!("Killed window {}", focused);
91 }
92 Err(e) => {
93 println!("Failed to kill window {}: {}", focused, e);
94 }
95 }
96 }
97 }
98 KeyAction::FocusStack => {
99 if let Arg::Int(direction) = arg {
100 println!("FocusStack called with direction: {}", direction);
101 println!("Windows: {:?}", self.windows);
102 println!("Current focused: {:?}", self.focused_window);
103 self.cycle_focus(*direction)?;
104 println!("New focused: {:?}", self.focused_window);
105 }
106 }
107 KeyAction::Quit => {
108 println!("Quitting window manager");
109 std::process::exit(0);
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 }
121 KeyAction::None => {
122 //no-op
123 }
124 }
125 Ok(())
126 }
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
191 pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
192 let visible = self.visible_windows();
193
194 if visible.is_empty() {
195 return Ok(());
196 }
197
198 let next_window = if let Some(current) = self.focused_window {
199 if let Some(current_index) = visible.iter().position(|&w| w == current) {
200 let next_index = if direction > 0 {
201 (current_index + 1) % visible.len()
202 } else {
203 (current_index + visible.len() - 1) % visible.len()
204 };
205 visible[next_index]
206 } else {
207 visible[0]
208 }
209 } else {
210 visible[0]
211 };
212
213 self.set_focus(Some(next_window))?;
214 Ok(())
215 }
216
217 pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
218 println!("set_focus called with: {:?}", window);
219 self.focused_window = window;
220
221 if let Some(win) = window {
222 self.connection
223 .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
224 self.connection.flush()?;
225 }
226
227 self.update_focus_visuals()?;
228 Ok(())
229 }
230
231 fn update_focus_visuals(&self) -> Result<()> {
232 println!("Updating focus visuals for {} windows", self.windows.len());
233 for &window in &self.windows {
234 let (border_color, border_width) = if self.focused_window == Some(window) {
235 println!("Window {} is FOCUSED (red border)", window);
236 (0xff0000, 2)
237 } else {
238 println!("Window {} is unfocused (gray border)", window);
239 (0x888888, 2)
240 };
241
242 self.connection.configure_window(
243 window,
244 &ConfigureWindowAux::new().border_width(border_width),
245 )?;
246
247 self.connection.change_window_attributes(
248 window,
249 &ChangeWindowAttributesAux::new().border_pixel(border_color),
250 )?;
251 }
252 self.connection.flush()?;
253 Ok(())
254 }
255
256 fn handle_event(&mut self, event: Event) -> Result<()> {
257 match event {
258 Event::MapRequest(event) => {
259 self.connection.map_window(event.window)?;
260 self.windows.push(event.window);
261 self.window_tags.insert(event.window, self.selected_tags);
262 self.apply_layout()?;
263 self.set_focus(Some(event.window))?;
264 }
265 Event::UnmapNotify(event) => {
266 if self.windows.contains(&event.window) {
267 if self.is_window_visible(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 }
276 }
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 // }
284 Event::DestroyNotify(event) => {
285 if self.windows.contains(&event.window) {
286 println!("Window {} destroyed, removing from layout", event.window);
287 self.remove_window(event.window)?;
288 }
289 }
290 Event::KeyPress(event) => {
291 let (action, arg) = keyboard::handle_key_press(event)?;
292 self.handle_key_action(action, arg)?;
293 }
294 _ => {}
295 }
296 Ok(())
297 }
298
299 fn apply_layout(&self) -> Result<()> {
300 let screen_width = self.screen.width_in_pixels as u32;
301 let screen_height = self.screen.height_in_pixels as u32;
302 let border_width = 2u32;
303
304 let visible = self.visible_windows();
305 let geometries = self.layout.arrange(&visible, screen_width, screen_height);
306
307 for (window, geometry) in visible.iter().zip(geometries.iter()) {
308 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
309 let adjusted_height = geometry.height.saturating_sub(2 * border_width);
310
311 self.connection.configure_window(
312 *window,
313 &ConfigureWindowAux::new()
314 .x(geometry.x_coordinate)
315 .y(geometry.y_coordinate)
316 .width(adjusted_width)
317 .height(adjusted_height),
318 )?;
319 }
320 self.connection.flush()?;
321 Ok(())
322 }
323
324 fn remove_window(&mut self, window: Window) -> Result<()> {
325 let initial_count = self.windows.len();
326 self.windows.retain(|&w| w != window);
327 self.window_tags.remove(&window);
328
329 if self.windows.len() < initial_count {
330 println!("Removed window {} from management", window);
331
332 if self.focused_window == Some(window) {
333 let new_focus = if self.windows.is_empty() {
334 None
335 } else {
336 Some(self.windows[self.windows.len() - 1])
337 };
338 self.set_focus(new_focus)?;
339 }
340
341 self.apply_layout()?;
342 }
343 Ok(())
344 }
345 }