|
|
@@ -1,13 +1,13 @@ |
| 1 |
1 |
use crate::bar::Bar; |
| 2 |
2 |
use crate::config::{BORDER_FOCUSED, BORDER_UNFOCUSED, BORDER_WIDTH, TAG_COUNT}; |
| 3 |
3 |
use crate::keyboard::{self, Arg, KeyAction}; |
| 4 |
|
-use crate::layout::tiling::TilingLayout; |
| 5 |
4 |
use crate::layout::Layout; |
|
5 |
+use crate::layout::tiling::TilingLayout; |
| 6 |
6 |
use anyhow::Result; |
| 7 |
7 |
|
| 8 |
8 |
use x11rb::connection::Connection; |
| 9 |
|
-use x11rb::protocol::xproto::*; |
| 10 |
9 |
use x11rb::protocol::Event; |
|
10 |
+use x11rb::protocol::xproto::*; |
| 11 |
11 |
use x11rb::rust_connection::RustConnection; |
| 12 |
12 |
|
| 13 |
13 |
pub type TagMask = u32; |
|
|
@@ -48,7 +48,7 @@ impl WindowManager { |
| 48 |
48 |
|
| 49 |
49 |
let bar = Bar::new(&connection, &screen, screen_number)?; |
| 50 |
50 |
|
| 51 |
|
- return Ok(Self { |
|
51 |
+ let mut window_manger = Self { |
| 52 |
52 |
connection, |
| 53 |
53 |
screen_number, |
| 54 |
54 |
root, |
|
|
@@ -59,21 +59,46 @@ impl WindowManager { |
| 59 |
59 |
window_tags: std::collections::HashMap::new(), |
| 60 |
60 |
selected_tags: tag_mask(0), |
| 61 |
61 |
bar, |
| 62 |
|
- }); |
|
62 |
+ }; |
|
63 |
+ |
|
64 |
+ window_manger.scan_existing_windows()?; |
|
65 |
+ |
|
66 |
+ Ok(window_manger) |
|
67 |
+ } |
|
68 |
+ |
|
69 |
+ fn scan_existing_windows(&mut self) -> Result<()> { |
|
70 |
+ let tree = self.connection.query_tree(self.root)?.reply()?; |
|
71 |
+ |
|
72 |
+ for &window in &tree.children { |
|
73 |
+ if let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() { |
|
74 |
+ if window != self.bar.window() |
|
75 |
+ && attrs.map_state == MapState::VIEWABLE |
|
76 |
+ && !attrs.override_redirect |
|
77 |
+ { |
|
78 |
+ self.windows.push(window); |
|
79 |
+ self.window_tags.insert(window, self.selected_tags); |
|
80 |
+ } |
|
81 |
+ } |
|
82 |
+ } |
|
83 |
+ if let Some(&first) = self.windows.first() { |
|
84 |
+ self.set_focus(Some(first))?; |
|
85 |
+ } |
|
86 |
+ Ok(()) |
| 63 |
87 |
} |
| 64 |
88 |
|
| 65 |
|
- pub fn run(&mut self) -> Result<()> { |
|
89 |
+ pub fn run(&mut self) -> Result<bool> { |
| 66 |
90 |
println!("oxwm started on display {}", self.screen_number); |
| 67 |
91 |
|
| 68 |
92 |
keyboard::setup_keybinds(&self.connection, self.root)?; |
| 69 |
|
- |
| 70 |
93 |
self.update_bar()?; |
| 71 |
94 |
|
| 72 |
95 |
loop { |
| 73 |
96 |
self.bar.update_blocks()?; |
| 74 |
97 |
|
| 75 |
98 |
if let Ok(Some(event)) = self.connection.poll_for_event() { |
| 76 |
|
- self.handle_event(event)?; |
|
99 |
+ if let Some(should_restart) = self.handle_event(event)? { |
|
100 |
+ return Ok(should_restart); |
|
101 |
+ } |
| 77 |
102 |
} |
| 78 |
103 |
|
| 79 |
104 |
if self.bar.needs_redraw() { |
|
|
@@ -84,19 +109,6 @@ impl WindowManager { |
| 84 |
109 |
} |
| 85 |
110 |
} |
| 86 |
111 |
|
| 87 |
|
- // pub fn run(&mut self) -> Result<()> { |
| 88 |
|
- // println!("oxwm started on display {}", self.screen_number); |
| 89 |
|
- // |
| 90 |
|
- // keyboard::setup_keybinds(&self.connection, self.root)?; |
| 91 |
|
- // |
| 92 |
|
- // self.update_bar()?; |
| 93 |
|
- // |
| 94 |
|
- // loop { |
| 95 |
|
- // let event = self.connection.wait_for_event()?; |
| 96 |
|
- // self.handle_event(event)?; |
| 97 |
|
- // } |
| 98 |
|
- // } |
| 99 |
|
- |
| 100 |
112 |
fn update_bar(&mut self) -> Result<()> { |
| 101 |
113 |
let mut occupied_tags: TagMask = 0; |
| 102 |
114 |
for &tags in self.window_tags.values() { |
|
|
@@ -139,8 +151,8 @@ impl WindowManager { |
| 139 |
151 |
self.cycle_focus(*direction)?; |
| 140 |
152 |
} |
| 141 |
153 |
} |
| 142 |
|
- KeyAction::Quit => { |
| 143 |
|
- std::process::exit(0); |
|
154 |
+ KeyAction::Quit | KeyAction::Restart => { |
|
155 |
+ //no-op |
| 144 |
156 |
} |
| 145 |
157 |
KeyAction::ViewTag => { |
| 146 |
158 |
if let Arg::Int(tag_index) = arg { |
|
|
@@ -280,7 +292,7 @@ impl WindowManager { |
| 280 |
292 |
Ok(()) |
| 281 |
293 |
} |
| 282 |
294 |
|
| 283 |
|
- fn handle_event(&mut self, event: Event) -> Result<()> { |
|
295 |
+ fn handle_event(&mut self, event: Event) -> Result<Option<bool>> { |
| 284 |
296 |
match event { |
| 285 |
297 |
Event::MapRequest(event) => { |
| 286 |
298 |
self.connection.map_window(event.window)?; |
|
|
@@ -302,7 +314,12 @@ impl WindowManager { |
| 302 |
314 |
} |
| 303 |
315 |
Event::KeyPress(event) => { |
| 304 |
316 |
let (action, arg) = keyboard::handle_key_press(event)?; |
| 305 |
|
- self.handle_key_action(action, arg)?; |
|
317 |
+ |
|
318 |
+ match action { |
|
319 |
+ KeyAction::Quit => return Ok(Some(false)), |
|
320 |
+ KeyAction::Restart => return Ok(Some(true)), |
|
321 |
+ _ => self.handle_key_action(action, arg)?, |
|
322 |
+ } |
| 306 |
323 |
} |
| 307 |
324 |
Event::ButtonPress(event) => { |
| 308 |
325 |
if event.event == self.bar.window() { |
|
|
@@ -319,9 +336,8 @@ impl WindowManager { |
| 319 |
336 |
} |
| 320 |
337 |
_ => {} |
| 321 |
338 |
} |
| 322 |
|
- Ok(()) |
|
339 |
+ Ok(None) |
| 323 |
340 |
} |
| 324 |
|
- |
| 325 |
341 |
fn apply_layout(&self) -> Result<()> { |
| 326 |
342 |
let screen_width = self.screen.width_in_pixels as u32; |
| 327 |
343 |
let screen_height = self.screen.height_in_pixels as u32; |