tony / oxwm

Branch: 251382c
Switch branches/tags
master
tonybtw Added bar. 2mb! 8347143 2025-10-03
355 lines · 11 KB Raw Blame History
1 use crate::bar::Bar;
2 use crate::config::{BORDER_FOCUSED, BORDER_UNFOCUSED, BORDER_WIDTH, TAG_COUNT};
3 use crate::keyboard::{self, Arg, KeyAction};
4 use crate::layout::Layout;
5 use crate::layout::tiling::TilingLayout;
6 use anyhow::Result;
7
8 use x11rb::connection::Connection;
9 use x11rb::protocol::Event;
10 use x11rb::protocol::xproto::*;
11 use x11rb::rust_connection::RustConnection;
12
13 pub type TagMask = u32;
14 pub fn tag_mask(tag: usize) -> TagMask {
15 1 << tag
16 }
17
18 pub struct WindowManager {
19 connection: RustConnection,
20 screen_number: usize,
21 root: Window,
22 screen: Screen,
23 windows: Vec<Window>,
24 focused_window: Option<Window>,
25 layout: Box<dyn Layout>,
26 window_tags: std::collections::HashMap<Window, TagMask>,
27 selected_tags: TagMask,
28 bar: Bar,
29 }
30
31 impl WindowManager {
32 pub fn new() -> Result<Self> {
33 let (connection, screen_number) = x11rb::connect(None)?;
34 let root = connection.setup().roots[screen_number].root;
35 let screen = connection.setup().roots[screen_number].clone();
36
37 connection
38 .change_window_attributes(
39 root,
40 &ChangeWindowAttributesAux::new().event_mask(
41 EventMask::SUBSTRUCTURE_REDIRECT
42 | EventMask::SUBSTRUCTURE_NOTIFY
43 | EventMask::PROPERTY_CHANGE
44 | EventMask::KEY_PRESS,
45 ),
46 )?
47 .check()?;
48
49 let bar = Bar::new(&connection, &screen)?;
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 bar,
62 });
63 }
64
65 pub fn run(&mut self) -> Result<()> {
66 println!("oxwm started on display {}", self.screen_number);
67
68 keyboard::setup_keybinds(&self.connection, self.root)?;
69
70 // Initial bar draw
71 self.update_bar()?;
72
73 loop {
74 let event = self.connection.wait_for_event()?;
75 self.handle_event(event)?;
76 }
77 }
78
79 fn update_bar(&mut self) -> Result<()> {
80 let mut occupied_tags: TagMask = 0;
81 for &tags in self.window_tags.values() {
82 occupied_tags |= tags;
83 }
84
85 self.bar.invalidate();
86 self.bar
87 .draw(&self.connection, self.selected_tags, occupied_tags)?;
88 Ok(())
89 }
90
91 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
92 match action {
93 KeyAction::Spawn => match arg {
94 Arg::Str(command) => {
95 std::process::Command::new(command).spawn()?;
96 }
97 Arg::Array(cmd) => {
98 if let Some((program, args)) = cmd.split_first() {
99 std::process::Command::new(program).args(args).spawn()?;
100 }
101 }
102 _ => {}
103 },
104 KeyAction::KillClient => {
105 if let Some(focused) = self.focused_window {
106 match self.connection.kill_client(focused) {
107 Ok(_) => {
108 self.connection.flush()?;
109 }
110 Err(e) => {
111 eprintln!("Failed to kill window {}: {}", focused, e);
112 }
113 }
114 }
115 }
116 KeyAction::FocusStack => {
117 if let Arg::Int(direction) = arg {
118 self.cycle_focus(*direction)?;
119 }
120 }
121 KeyAction::Quit => {
122 std::process::exit(0);
123 }
124 KeyAction::ViewTag => {
125 if let Arg::Int(tag_index) = arg {
126 self.view_tag(*tag_index as usize)?;
127 }
128 }
129 KeyAction::MoveToTag => {
130 if let Arg::Int(tag_index) = arg {
131 self.move_to_tag(*tag_index as usize)?;
132 }
133 }
134 KeyAction::None => {
135 //no-op
136 }
137 }
138 Ok(())
139 }
140
141 fn is_window_visible(&self, window: Window) -> bool {
142 if let Some(&tags) = self.window_tags.get(&window) {
143 (tags & self.selected_tags) != 0
144 } else {
145 false
146 }
147 }
148
149 fn visible_windows(&self) -> Vec<Window> {
150 self.windows
151 .iter()
152 .filter(|&&w| self.is_window_visible(w))
153 .copied()
154 .collect()
155 }
156
157 fn update_window_visibility(&self) -> Result<()> {
158 for &window in &self.windows {
159 if self.is_window_visible(window) {
160 self.connection.map_window(window)?;
161 } else {
162 self.connection.unmap_window(window)?;
163 }
164 }
165 self.connection.flush()?;
166 Ok(())
167 }
168
169 pub fn view_tag(&mut self, tag_index: usize) -> Result<()> {
170 if tag_index >= TAG_COUNT {
171 return Ok(());
172 }
173
174 self.selected_tags = tag_mask(tag_index);
175 self.update_window_visibility()?;
176 self.apply_layout()?;
177 self.update_bar()?; // Update bar to show new tag
178
179 let visible = self.visible_windows();
180 self.set_focus(visible.first().copied())?;
181
182 Ok(())
183 }
184
185 pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> {
186 if tag_index >= TAG_COUNT {
187 return Ok(());
188 }
189
190 if let Some(focused) = self.focused_window {
191 let mask = tag_mask(tag_index);
192 self.window_tags.insert(focused, mask);
193 self.update_window_visibility()?;
194 self.apply_layout()?;
195 self.update_bar()?; // Update bar to show occupied tags changed
196 }
197
198 Ok(())
199 }
200
201 pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
202 let visible = self.visible_windows();
203
204 if visible.is_empty() {
205 return Ok(());
206 }
207
208 let next_window = if let Some(current) = self.focused_window {
209 if let Some(current_index) = visible.iter().position(|&w| w == current) {
210 let next_index = if direction > 0 {
211 (current_index + 1) % visible.len()
212 } else {
213 (current_index + visible.len() - 1) % visible.len()
214 };
215 visible[next_index]
216 } else {
217 visible[0]
218 }
219 } else {
220 visible[0]
221 };
222
223 self.set_focus(Some(next_window))?;
224 Ok(())
225 }
226
227 pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
228 self.focused_window = window;
229
230 if let Some(win) = window {
231 self.connection
232 .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
233 self.connection.flush()?;
234 }
235
236 self.update_focus_visuals()?;
237 Ok(())
238 }
239
240 fn update_focus_visuals(&self) -> Result<()> {
241 for &window in &self.windows {
242 let (border_color, border_width) = if self.focused_window == Some(window) {
243 (BORDER_FOCUSED, BORDER_WIDTH)
244 } else {
245 (BORDER_UNFOCUSED, BORDER_WIDTH)
246 };
247
248 self.connection.configure_window(
249 window,
250 &ConfigureWindowAux::new().border_width(border_width),
251 )?;
252
253 self.connection.change_window_attributes(
254 window,
255 &ChangeWindowAttributesAux::new().border_pixel(border_color),
256 )?;
257 }
258 self.connection.flush()?;
259 Ok(())
260 }
261
262 fn handle_event(&mut self, event: Event) -> Result<()> {
263 match event {
264 Event::MapRequest(event) => {
265 self.connection.map_window(event.window)?;
266 self.windows.push(event.window);
267 self.window_tags.insert(event.window, self.selected_tags);
268 self.apply_layout()?;
269 self.update_bar()?;
270 self.set_focus(Some(event.window))?;
271 }
272 Event::UnmapNotify(event) => {
273 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
274 self.remove_window(event.window)?;
275 }
276 }
277 Event::DestroyNotify(event) => {
278 if self.windows.contains(&event.window) {
279 self.remove_window(event.window)?;
280 }
281 }
282 Event::KeyPress(event) => {
283 let (action, arg) = keyboard::handle_key_press(event)?;
284 self.handle_key_action(action, arg)?;
285 }
286 Event::ButtonPress(event) => {
287 // Check if click was on the bar
288 if event.event == self.bar.window() {
289 if let Some(tag_index) = self.bar.handle_click(event.event_x) {
290 self.view_tag(tag_index)?;
291 }
292 }
293 }
294 Event::Expose(event) => {
295 if event.window == self.bar.window() {
296 self.bar.invalidate();
297 self.update_bar()?;
298 }
299 }
300 _ => {}
301 }
302 Ok(())
303 }
304
305 fn apply_layout(&self) -> Result<()> {
306 let screen_width = self.screen.width_in_pixels as u32;
307 let screen_height = self.screen.height_in_pixels as u32;
308 let border_width = BORDER_WIDTH;
309
310 let bar_height = self.bar.height() as u32;
311 let usable_height = screen_height.saturating_sub(bar_height);
312
313 let visible = self.visible_windows();
314 let geometries = self.layout.arrange(&visible, screen_width, usable_height);
315
316 for (window, geometry) in visible.iter().zip(geometries.iter()) {
317 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
318 let adjusted_height = geometry.height.saturating_sub(2 * border_width);
319
320 let adjusted_y = geometry.y_coordinate + bar_height as i32;
321
322 self.connection.configure_window(
323 *window,
324 &ConfigureWindowAux::new()
325 .x(geometry.x_coordinate)
326 .y(adjusted_y)
327 .width(adjusted_width)
328 .height(adjusted_height),
329 )?;
330 }
331 self.connection.flush()?;
332 Ok(())
333 }
334
335 fn remove_window(&mut self, window: Window) -> Result<()> {
336 let initial_count = self.windows.len();
337 self.windows.retain(|&w| w != window);
338 self.window_tags.remove(&window);
339
340 if self.windows.len() < initial_count {
341 if self.focused_window == Some(window) {
342 let new_focus = if self.windows.is_empty() {
343 None
344 } else {
345 Some(self.windows[self.windows.len() - 1])
346 };
347 self.set_focus(new_focus)?;
348 }
349
350 self.apply_layout()?;
351 self.update_bar()?;
352 }
353 Ok(())
354 }
355 }