tony / oxwm

Branch: 95fc4c9
Switch branches/tags
master
tonybtw Added fonts.. (bloatware), updated devshell accordingly fc9864b 2025-10-03
353 lines · 10.9 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, screen_number)?;
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 self.update_bar()?;
71
72 loop {
73 let event = self.connection.wait_for_event()?;
74 self.handle_event(event)?;
75 }
76 }
77
78 fn update_bar(&mut self) -> Result<()> {
79 let mut occupied_tags: TagMask = 0;
80 for &tags in self.window_tags.values() {
81 occupied_tags |= tags;
82 }
83
84 self.bar.invalidate();
85 self.bar
86 .draw(&self.connection, self.selected_tags, occupied_tags)?;
87 Ok(())
88 }
89
90 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
91 match action {
92 KeyAction::Spawn => match arg {
93 Arg::Str(command) => {
94 std::process::Command::new(command).spawn()?;
95 }
96 Arg::Array(cmd) => {
97 if let Some((program, args)) = cmd.split_first() {
98 std::process::Command::new(program).args(args).spawn()?;
99 }
100 }
101 _ => {}
102 },
103 KeyAction::KillClient => {
104 if let Some(focused) = self.focused_window {
105 match self.connection.kill_client(focused) {
106 Ok(_) => {
107 self.connection.flush()?;
108 }
109 Err(e) => {
110 eprintln!("Failed to kill window {}: {}", focused, e);
111 }
112 }
113 }
114 }
115 KeyAction::FocusStack => {
116 if let Arg::Int(direction) = arg {
117 self.cycle_focus(*direction)?;
118 }
119 }
120 KeyAction::Quit => {
121 std::process::exit(0);
122 }
123 KeyAction::ViewTag => {
124 if let Arg::Int(tag_index) = arg {
125 self.view_tag(*tag_index as usize)?;
126 }
127 }
128 KeyAction::MoveToTag => {
129 if let Arg::Int(tag_index) = arg {
130 self.move_to_tag(*tag_index as usize)?;
131 }
132 }
133 KeyAction::None => {
134 //no-op
135 }
136 }
137 Ok(())
138 }
139
140 fn is_window_visible(&self, window: Window) -> bool {
141 if let Some(&tags) = self.window_tags.get(&window) {
142 (tags & self.selected_tags) != 0
143 } else {
144 false
145 }
146 }
147
148 fn visible_windows(&self) -> Vec<Window> {
149 self.windows
150 .iter()
151 .filter(|&&w| self.is_window_visible(w))
152 .copied()
153 .collect()
154 }
155
156 fn update_window_visibility(&self) -> Result<()> {
157 for &window in &self.windows {
158 if self.is_window_visible(window) {
159 self.connection.map_window(window)?;
160 } else {
161 self.connection.unmap_window(window)?;
162 }
163 }
164 self.connection.flush()?;
165 Ok(())
166 }
167
168 pub fn view_tag(&mut self, tag_index: usize) -> Result<()> {
169 if tag_index >= TAG_COUNT {
170 return Ok(());
171 }
172
173 self.selected_tags = tag_mask(tag_index);
174 self.update_window_visibility()?;
175 self.apply_layout()?;
176 self.update_bar()?;
177
178 let visible = self.visible_windows();
179 self.set_focus(visible.first().copied())?;
180
181 Ok(())
182 }
183
184 pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> {
185 if tag_index >= TAG_COUNT {
186 return Ok(());
187 }
188
189 if let Some(focused) = self.focused_window {
190 let mask = tag_mask(tag_index);
191 self.window_tags.insert(focused, mask);
192 self.update_window_visibility()?;
193 self.apply_layout()?;
194 self.update_bar()?;
195 }
196
197 Ok(())
198 }
199
200 pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
201 let visible = self.visible_windows();
202
203 if visible.is_empty() {
204 return Ok(());
205 }
206
207 let next_window = if let Some(current) = self.focused_window {
208 if let Some(current_index) = visible.iter().position(|&w| w == current) {
209 let next_index = if direction > 0 {
210 (current_index + 1) % visible.len()
211 } else {
212 (current_index + visible.len() - 1) % visible.len()
213 };
214 visible[next_index]
215 } else {
216 visible[0]
217 }
218 } else {
219 visible[0]
220 };
221
222 self.set_focus(Some(next_window))?;
223 Ok(())
224 }
225
226 pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
227 self.focused_window = window;
228
229 if let Some(win) = window {
230 self.connection
231 .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
232 self.connection.flush()?;
233 }
234
235 self.update_focus_visuals()?;
236 Ok(())
237 }
238
239 fn update_focus_visuals(&self) -> Result<()> {
240 for &window in &self.windows {
241 let (border_color, border_width) = if self.focused_window == Some(window) {
242 (BORDER_FOCUSED, BORDER_WIDTH)
243 } else {
244 (BORDER_UNFOCUSED, BORDER_WIDTH)
245 };
246
247 self.connection.configure_window(
248 window,
249 &ConfigureWindowAux::new().border_width(border_width),
250 )?;
251
252 self.connection.change_window_attributes(
253 window,
254 &ChangeWindowAttributesAux::new().border_pixel(border_color),
255 )?;
256 }
257 self.connection.flush()?;
258 Ok(())
259 }
260
261 fn handle_event(&mut self, event: Event) -> Result<()> {
262 match event {
263 Event::MapRequest(event) => {
264 self.connection.map_window(event.window)?;
265 self.windows.push(event.window);
266 self.window_tags.insert(event.window, self.selected_tags);
267 self.apply_layout()?;
268 self.update_bar()?;
269 self.set_focus(Some(event.window))?;
270 }
271 Event::UnmapNotify(event) => {
272 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
273 self.remove_window(event.window)?;
274 }
275 }
276 Event::DestroyNotify(event) => {
277 if self.windows.contains(&event.window) {
278 self.remove_window(event.window)?;
279 }
280 }
281 Event::KeyPress(event) => {
282 let (action, arg) = keyboard::handle_key_press(event)?;
283 self.handle_key_action(action, arg)?;
284 }
285 Event::ButtonPress(event) => {
286 if event.event == self.bar.window() {
287 if let Some(tag_index) = self.bar.handle_click(event.event_x) {
288 self.view_tag(tag_index)?;
289 }
290 }
291 }
292 Event::Expose(event) => {
293 if event.window == self.bar.window() {
294 self.bar.invalidate();
295 self.update_bar()?;
296 }
297 }
298 _ => {}
299 }
300 Ok(())
301 }
302
303 fn apply_layout(&self) -> Result<()> {
304 let screen_width = self.screen.width_in_pixels as u32;
305 let screen_height = self.screen.height_in_pixels as u32;
306 let border_width = BORDER_WIDTH;
307
308 let bar_height = self.bar.height() as u32;
309 let usable_height = screen_height.saturating_sub(bar_height);
310
311 let visible = self.visible_windows();
312 let geometries = self.layout.arrange(&visible, screen_width, usable_height);
313
314 for (window, geometry) in visible.iter().zip(geometries.iter()) {
315 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
316 let adjusted_height = geometry.height.saturating_sub(2 * border_width);
317
318 let adjusted_y = geometry.y_coordinate + bar_height as i32;
319
320 self.connection.configure_window(
321 *window,
322 &ConfigureWindowAux::new()
323 .x(geometry.x_coordinate)
324 .y(adjusted_y)
325 .width(adjusted_width)
326 .height(adjusted_height),
327 )?;
328 }
329 self.connection.flush()?;
330 Ok(())
331 }
332
333 fn remove_window(&mut self, window: Window) -> Result<()> {
334 let initial_count = self.windows.len();
335 self.windows.retain(|&w| w != window);
336 self.window_tags.remove(&window);
337
338 if self.windows.len() < initial_count {
339 if self.focused_window == Some(window) {
340 let new_focus = if self.windows.is_empty() {
341 None
342 } else {
343 Some(self.windows[self.windows.len() - 1])
344 };
345 self.set_focus(new_focus)?;
346 }
347
348 self.apply_layout()?;
349 self.update_bar()?;
350 }
351 Ok(())
352 }
353 }