tony / oxwm

Branch: 7443210
Switch branches/tags
master
tonybtw Finished killing windows dymanically and correctly b5923a9 2025-09-29
216 lines · 6.9 KB Raw Blame History
1 use crate::keyboard::{self, KeyAction}; // Add KeyAction to the import
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 pub struct WindowManager {
13 connection: RustConnection,
14 screen_number: usize,
15 root: Window,
16 screen: Screen,
17 windows: Vec<Window>,
18 pub focused_window: Option<Window>,
19 layout: Box<dyn Layout>,
20 }
21
22 impl WindowManager {
23 pub fn new() -> Result<Self> {
24 let (connection, screen_number) = x11rb::connect(None)?;
25 let root = connection.setup().roots[screen_number].root;
26 let screen = connection.setup().roots[screen_number].clone();
27
28 connection
29 .change_window_attributes(
30 root,
31 &ChangeWindowAttributesAux::new().event_mask(
32 EventMask::SUBSTRUCTURE_REDIRECT
33 | EventMask::SUBSTRUCTURE_NOTIFY
34 | EventMask::PROPERTY_CHANGE
35 | EventMask::KEY_PRESS,
36 ),
37 )?
38 .check()?;
39
40 return Ok(Self {
41 connection,
42 screen_number,
43 root,
44 screen,
45 windows: Vec::new(),
46 focused_window: None,
47 layout: Box::new(TilingLayout),
48 });
49 }
50
51 pub fn run(&mut self) -> Result<()> {
52 println!("oxwm started on display {}", self.screen_number);
53
54 keyboard::setup_keybinds(&self.connection, self.root)?;
55
56 loop {
57 let event = self.connection.wait_for_event()?;
58 println!("event: {:?}", event);
59 self.handle_event(event)?;
60 }
61 }
62
63 fn handle_key_action(&mut self, action: keyboard::KeyAction) -> Result<()> {
64 match action {
65 keyboard::KeyAction::SpawnTerminal => {
66 println!("Spawning terminal");
67 std::process::Command::new("xclock").spawn()?;
68 }
69 keyboard::KeyAction::CloseWindow => {
70 println!("Closing focused window");
71 if let Some(focused) = self.focused_window {
72 match self.connection.kill_client(focused) {
73 Ok(_) => {
74 self.connection.flush()?;
75 println!("Killed window {}", focused);
76 }
77 Err(e) => {
78 println!("Failed to kill window {}: {}", focused, e);
79 }
80 }
81 }
82 }
83 keyboard::KeyAction::CycleWindow => {
84 println!("Cycling focus");
85 self.cycle_focus()?;
86 }
87 keyboard::KeyAction::Quit => {
88 println!("Quitting window manager");
89 std::process::exit(0);
90 }
91 keyboard::KeyAction::None => {
92 //no-op
93 }
94 }
95 Ok(())
96 }
97 pub fn cycle_focus(&mut self) -> Result<()> {
98 if self.windows.is_empty() {
99 return Ok(());
100 }
101
102 let next_window = if let Some(current) = self.focused_window {
103 if let Some(current_index) = self.windows.iter().position(|&w| w == current) {
104 let next_index = (current_index + 1) % self.windows.len();
105 self.windows[next_index]
106 } else {
107 self.windows[0]
108 }
109 } else {
110 self.windows[0]
111 };
112
113 self.set_focus(Some(next_window))?;
114 Ok(())
115 }
116
117 pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
118 self.focused_window = window;
119
120 if let Some(win) = window {
121 self.connection
122 .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
123 }
124
125 self.update_focus_visuals()?;
126 Ok(())
127 }
128
129 fn update_focus_visuals(&self) -> Result<()> {
130 for &window in &self.windows {
131 let border_color = if self.focused_window == Some(window) {
132 0xff0000
133 } else {
134 0x888888
135 };
136
137 self.connection.change_window_attributes(
138 window,
139 &ChangeWindowAttributesAux::new().border_pixel(border_color),
140 )?;
141 }
142 Ok(())
143 }
144 fn handle_event(&mut self, event: Event) -> Result<()> {
145 match event {
146 Event::MapRequest(event) => {
147 self.connection.map_window(event.window)?;
148 self.windows.push(event.window);
149 self.apply_layout()?;
150 self.set_focus(Some(event.window))?;
151 self.connection.flush()?;
152 }
153 Event::UnmapNotify(event) => {
154 if self.windows.contains(&event.window) {
155 println!("Window {} unmapped, removing from layout", event.window);
156 self.remove_window(event.window)?;
157 }
158 }
159 Event::DestroyNotify(event) => {
160 if self.windows.contains(&event.window) {
161 println!("Window {} destroyed, removing from layout", event.window);
162 self.remove_window(event.window)?;
163 }
164 }
165 Event::KeyPress(event) => {
166 let action = keyboard::handle_key_press(event)?; // Remove &self.connection
167 self.handle_key_action(action)?;
168 }
169 _ => {}
170 }
171 Ok(())
172 }
173 fn apply_layout(&self) -> Result<()> {
174 let screen_width = self.screen.width_in_pixels as u32;
175 let screen_height = self.screen.height_in_pixels as u32;
176
177 let geometries = self
178 .layout
179 .arrange(&self.windows, screen_width, screen_height);
180
181 for (window, geometry) in self.windows.iter().zip(geometries.iter()) {
182 self.connection.configure_window(
183 *window,
184 &ConfigureWindowAux::new()
185 .x(geometry.x_coordinate)
186 .y(geometry.y_coordinate)
187 .width(geometry.width)
188 .height(geometry.height)
189 .border_width(1),
190 )?;
191 }
192 return Ok(());
193 }
194
195 fn remove_window(&mut self, window: Window) -> Result<()> {
196 let initial_count = self.windows.len();
197 self.windows.retain(|&w| w != window);
198
199 if self.windows.len() < initial_count {
200 println!("Removed window {} from management", window);
201
202 if self.focused_window == Some(window) {
203 self.focused_window = if self.windows.is_empty() {
204 None
205 } else {
206 Some(self.windows[self.windows.len() - 1])
207 };
208 }
209
210 self.apply_layout()?;
211 self.connection.flush()?;
212 }
213
214 Ok(())
215 }
216 }