tony / oxwm

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