| @@ -3,8 +3,9 @@ use crate::bar::Bar; |
| 3 |
use crate::errors::WmError; |
3 |
use crate::errors::WmError; |
| 4 |
use crate::keyboard::{self, Arg, KeyAction, handlers}; |
4 |
use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 5 |
use crate::layout::GapConfig; |
5 |
use crate::layout::GapConfig; |
| 6 |
-use crate::layout::Layout; |
|
|
| 7 |
use crate::layout::tiling::TilingLayout; |
6 |
use crate::layout::tiling::TilingLayout; |
|
|
7 |
+use crate::layout::{Layout, layout_from_str, next_layout}; |
|
|
8 |
+use crate::monitor::{Monitor, detect_monitors}; |
| 8 |
use std::collections::HashSet; |
9 |
use std::collections::HashSet; |
| 9 |
use x11rb::cursor::Handle as CursorHandle; |
10 |
use x11rb::cursor::Handle as CursorHandle; |
| 10 |
|
11 |
|
| @@ -18,6 +19,34 @@ pub fn tag_mask(tag: usize) -> TagMask { |
| 18 |
1 << tag |
19 |
1 << tag |
| 19 |
} |
20 |
} |
| 20 |
|
21 |
|
|
|
22 |
+struct AtomCache { |
|
|
23 |
+ net_current_desktop: Atom, |
|
|
24 |
+ net_client_info: Atom, |
|
|
25 |
+ wm_state: Atom, |
|
|
26 |
+} |
|
|
27 |
+ |
|
|
28 |
+impl AtomCache { |
|
|
29 |
+ fn new(connection: &RustConnection) -> WmResult<Self> { |
|
|
30 |
+ let net_current_desktop = connection |
|
|
31 |
+ .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
|
|
32 |
+ .reply()? |
|
|
33 |
+ .atom; |
|
|
34 |
+ |
|
|
35 |
+ let net_client_info = connection |
|
|
36 |
+ .intern_atom(false, b"_NET_CLIENT_INFO")? |
|
|
37 |
+ .reply()? |
|
|
38 |
+ .atom; |
|
|
39 |
+ |
|
|
40 |
+ let wm_state = connection.intern_atom(false, b"WM_STATE")?.reply()?.atom; |
|
|
41 |
+ |
|
|
42 |
+ Ok(Self { |
|
|
43 |
+ net_current_desktop, |
|
|
44 |
+ net_client_info, |
|
|
45 |
+ wm_state, |
|
|
46 |
+ }) |
|
|
47 |
+ } |
|
|
48 |
+} |
|
|
49 |
+ |
| 21 |
pub struct WindowManager { |
50 |
pub struct WindowManager { |
| 22 |
config: Config, |
51 |
config: Config, |
| 23 |
connection: RustConnection, |
52 |
connection: RustConnection, |
| @@ -25,14 +54,20 @@ pub struct WindowManager { |
| 25 |
root: Window, |
54 |
root: Window, |
| 26 |
screen: Screen, |
55 |
screen: Screen, |
| 27 |
windows: Vec<Window>, |
56 |
windows: Vec<Window>, |
| 28 |
- focused_window: Option<Window>, |
|
|
| 29 |
layout: Box<dyn Layout>, |
57 |
layout: Box<dyn Layout>, |
| 30 |
window_tags: std::collections::HashMap<Window, TagMask>, |
58 |
window_tags: std::collections::HashMap<Window, TagMask>, |
| 31 |
- selected_tags: TagMask, |
59 |
+ window_monitor: std::collections::HashMap<Window, usize>, |
|
|
60 |
+ window_geometries: std::collections::HashMap<Window, (i16, i16, u16, u16)>, |
| 32 |
gaps_enabled: bool, |
61 |
gaps_enabled: bool, |
| 33 |
fullscreen_window: Option<Window>, |
62 |
fullscreen_window: Option<Window>, |
| 34 |
floating_windows: HashSet<Window>, |
63 |
floating_windows: HashSet<Window>, |
| 35 |
- bar: Bar, |
64 |
+ bars: Vec<Bar>, |
|
|
65 |
+ monitors: Vec<Monitor>, |
|
|
66 |
+ selected_monitor: usize, |
|
|
67 |
+ atoms: AtomCache, |
|
|
68 |
+ previous_focused: Option<Window>, |
|
|
69 |
+ display: *mut x11::xlib::Display, |
|
|
70 |
+ font: crate::bar::font::Font, |
| 36 |
} |
71 |
} |
| 37 |
|
72 |
|
| 38 |
type WmResult<T> = Result<T, WmError>; |
73 |
type WmResult<T> = Result<T, WmError>; |
| @@ -61,7 +96,8 @@ impl WindowManager { |
| 61 |
| EventMask::SUBSTRUCTURE_NOTIFY |
96 |
| EventMask::SUBSTRUCTURE_NOTIFY |
| 62 |
| EventMask::PROPERTY_CHANGE |
97 |
| EventMask::PROPERTY_CHANGE |
| 63 |
| EventMask::KEY_PRESS |
98 |
| EventMask::KEY_PRESS |
| 64 |
- | EventMask::BUTTON_PRESS, |
99 |
+ | EventMask::BUTTON_PRESS |
|
|
100 |
+ | EventMask::POINTER_MOTION, |
| 65 |
), |
101 |
), |
| 66 |
)? |
102 |
)? |
| 67 |
.check()?; |
103 |
.check()?; |
| @@ -90,11 +126,35 @@ impl WindowManager { |
| 90 |
u16::from(config.modkey).into(), |
126 |
u16::from(config.modkey).into(), |
| 91 |
)?; |
127 |
)?; |
| 92 |
|
128 |
|
| 93 |
- let bar = Bar::new(&connection, &screen, screen_number, &config)?; |
129 |
+ let monitors = detect_monitors(&connection, &screen, root)?; |
|
|
130 |
+ |
|
|
131 |
+ let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
|
|
132 |
+ if display.is_null() { |
|
|
133 |
+ return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed)); |
|
|
134 |
+ } |
|
|
135 |
+ |
|
|
136 |
+ let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?; |
|
|
137 |
+ |
|
|
138 |
+ let mut bars = Vec::new(); |
|
|
139 |
+ for monitor in monitors.iter() { |
|
|
140 |
+ let bar = Bar::new( |
|
|
141 |
+ &connection, |
|
|
142 |
+ &screen, |
|
|
143 |
+ screen_number, |
|
|
144 |
+ &config, |
|
|
145 |
+ display, |
|
|
146 |
+ &font, |
|
|
147 |
+ monitor.x as i16, |
|
|
148 |
+ monitor.y as i16, |
|
|
149 |
+ monitor.width as u16, |
|
|
150 |
+ )?; |
|
|
151 |
+ bars.push(bar); |
|
|
152 |
+ } |
| 94 |
|
153 |
|
| 95 |
- let selected_tags = Self::get_saved_selected_tags(&connection, root, config.tags.len())?; |
|
|
| 96 |
let gaps_enabled = config.gaps_enabled; |
154 |
let gaps_enabled = config.gaps_enabled; |
| 97 |
|
155 |
|
|
|
156 |
+ let atoms = AtomCache::new(&connection)?; |
|
|
157 |
+ |
| 98 |
let mut window_manager = Self { |
158 |
let mut window_manager = Self { |
| 99 |
config, |
159 |
config, |
| 100 |
connection, |
160 |
connection, |
| @@ -102,14 +162,20 @@ impl WindowManager { |
| 102 |
root, |
162 |
root, |
| 103 |
screen, |
163 |
screen, |
| 104 |
windows: Vec::new(), |
164 |
windows: Vec::new(), |
| 105 |
- focused_window: None, |
|
|
| 106 |
layout: Box::new(TilingLayout), |
165 |
layout: Box::new(TilingLayout), |
| 107 |
window_tags: std::collections::HashMap::new(), |
166 |
window_tags: std::collections::HashMap::new(), |
| 108 |
- selected_tags, |
167 |
+ window_monitor: std::collections::HashMap::new(), |
|
|
168 |
+ window_geometries: std::collections::HashMap::new(), |
| 109 |
gaps_enabled, |
169 |
gaps_enabled, |
| 110 |
fullscreen_window: None, |
170 |
fullscreen_window: None, |
| 111 |
floating_windows: HashSet::new(), |
171 |
floating_windows: HashSet::new(), |
| 112 |
- bar, |
172 |
+ bars, |
|
|
173 |
+ monitors, |
|
|
174 |
+ selected_monitor: 0, |
|
|
175 |
+ atoms, |
|
|
176 |
+ previous_focused: None, |
|
|
177 |
+ display, |
|
|
178 |
+ font, |
| 113 |
}; |
179 |
}; |
| 114 |
|
180 |
|
| 115 |
window_manager.scan_existing_windows()?; |
181 |
window_manager.scan_existing_windows()?; |
| @@ -118,7 +184,12 @@ impl WindowManager { |
| 118 |
Ok(window_manager) |
184 |
Ok(window_manager) |
| 119 |
} |
185 |
} |
| 120 |
|
186 |
|
| 121 |
- fn get_saved_selected_tags( |
187 |
+ /** |
|
|
188 |
+ * |
|
|
189 |
+ * This function is deprecated for now, but will potentially be used in the future. |
|
|
190 |
+ * |
|
|
191 |
+ */ |
|
|
192 |
+ fn _get_saved_selected_tags( |
| 122 |
connection: &RustConnection, |
193 |
connection: &RustConnection, |
| 123 |
root: Window, |
194 |
root: Window, |
| 124 |
tag_count: usize, |
195 |
tag_count: usize, |
| @@ -154,20 +225,11 @@ impl WindowManager { |
| 154 |
|
225 |
|
| 155 |
fn scan_existing_windows(&mut self) -> WmResult<()> { |
226 |
fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 156 |
let tree = self.connection.query_tree(self.root)?.reply()?; |
227 |
let tree = self.connection.query_tree(self.root)?.reply()?; |
| 157 |
- let net_client_info = self |
228 |
+ let net_client_info = self.atoms.net_client_info; |
| 158 |
- .connection |
229 |
+ let wm_state_atom = self.atoms.wm_state; |
| 159 |
- .intern_atom(false, b"_NET_CLIENT_INFO")? |
|
|
| 160 |
- .reply()? |
|
|
| 161 |
- .atom; |
|
|
| 162 |
- |
|
|
| 163 |
- let wm_state_atom = self |
|
|
| 164 |
- .connection |
|
|
| 165 |
- .intern_atom(false, b"WM_STATE")? |
|
|
| 166 |
- .reply()? |
|
|
| 167 |
- .atom; |
|
|
| 168 |
|
230 |
|
| 169 |
for &window in &tree.children { |
231 |
for &window in &tree.children { |
| 170 |
- if window == self.bar.window() { |
232 |
+ if self.bars.iter().any(|bar| bar.window() == window) { |
| 171 |
continue; |
233 |
continue; |
| 172 |
} |
234 |
} |
| 173 |
|
235 |
|
| @@ -183,6 +245,7 @@ impl WindowManager { |
| 183 |
let tag = self.get_saved_tag(window, net_client_info)?; |
245 |
let tag = self.get_saved_tag(window, net_client_info)?; |
| 184 |
self.windows.push(window); |
246 |
self.windows.push(window); |
| 185 |
self.window_tags.insert(window, tag); |
247 |
self.window_tags.insert(window, tag); |
|
|
248 |
+ self.window_monitor.insert(window, self.selected_monitor); |
| 186 |
continue; |
249 |
continue; |
| 187 |
} |
250 |
} |
| 188 |
|
251 |
|
| @@ -207,12 +270,13 @@ impl WindowManager { |
| 207 |
let tag = self.get_saved_tag(window, net_client_info)?; |
270 |
let tag = self.get_saved_tag(window, net_client_info)?; |
| 208 |
self.windows.push(window); |
271 |
self.windows.push(window); |
| 209 |
self.window_tags.insert(window, tag); |
272 |
self.window_tags.insert(window, tag); |
|
|
273 |
+ self.window_monitor.insert(window, self.selected_monitor); |
| 210 |
} |
274 |
} |
| 211 |
} |
275 |
} |
| 212 |
} |
276 |
} |
| 213 |
|
277 |
|
| 214 |
if let Some(&first) = self.windows.first() { |
278 |
if let Some(&first) = self.windows.first() { |
| 215 |
- self.set_focus(Some(first))?; |
279 |
+ self.set_focus(first)?; |
| 216 |
} |
280 |
} |
| 217 |
|
281 |
|
| 218 |
self.apply_layout()?; |
282 |
self.apply_layout()?; |
| @@ -243,15 +307,15 @@ impl WindowManager { |
| 243 |
} |
307 |
} |
| 244 |
} |
308 |
} |
| 245 |
|
309 |
|
| 246 |
- Ok(self.selected_tags) |
310 |
+ Ok(self |
|
|
311 |
+ .monitors |
|
|
312 |
+ .get(self.selected_monitor) |
|
|
313 |
+ .map(|m| m.selected_tags) |
|
|
314 |
+ .unwrap_or(tag_mask(0))) |
| 247 |
} |
315 |
} |
| 248 |
|
316 |
|
| 249 |
fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
317 |
fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| 250 |
- let net_client_info = self |
318 |
+ let net_client_info = self.atoms.net_client_info; |
| 251 |
- .connection |
|
|
| 252 |
- .intern_atom(false, b"_NET_CLIENT_INFO")? |
|
|
| 253 |
- .reply()? |
|
|
| 254 |
- .atom; |
|
|
| 255 |
|
319 |
|
| 256 |
let bytes = tag.to_ne_bytes().to_vec(); |
320 |
let bytes = tag.to_ne_bytes().to_vec(); |
| 257 |
|
321 |
|
| @@ -270,11 +334,7 @@ impl WindowManager { |
| 270 |
} |
334 |
} |
| 271 |
|
335 |
|
| 272 |
fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
336 |
fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
| 273 |
- let wm_state_atom = self |
337 |
+ let wm_state_atom = self.atoms.wm_state; |
| 274 |
- .connection |
|
|
| 275 |
- .intern_atom(false, b"WM_STATE")? |
|
|
| 276 |
- .reply()? |
|
|
| 277 |
- .atom; |
|
|
| 278 |
|
338 |
|
| 279 |
let data = [state, 0u32]; |
339 |
let data = [state, 0u32]; |
| 280 |
let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
340 |
let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| @@ -300,24 +360,30 @@ impl WindowManager { |
| 300 |
self.update_bar()?; |
360 |
self.update_bar()?; |
| 301 |
|
361 |
|
| 302 |
loop { |
362 |
loop { |
| 303 |
- self.bar.update_blocks(); |
363 |
+ while let Some(event) = self.connection.poll_for_event()? { |
| 304 |
- |
|
|
| 305 |
- if let Ok(Some(event)) = self.connection.poll_for_event() { |
|
|
| 306 |
if let Some(should_restart) = self.handle_event(event)? { |
364 |
if let Some(should_restart) = self.handle_event(event)? { |
| 307 |
return Ok(should_restart); |
365 |
return Ok(should_restart); |
| 308 |
} |
366 |
} |
| 309 |
} |
367 |
} |
| 310 |
|
368 |
|
| 311 |
- if self.bar.needs_redraw() { |
369 |
+ if let Some(bar) = self.bars.get_mut(self.selected_monitor) { |
|
|
370 |
+ bar.update_blocks(); |
|
|
371 |
+ } |
|
|
372 |
+ |
|
|
373 |
+ if self.bars.iter().any(|bar| bar.needs_redraw()) { |
| 312 |
self.update_bar()?; |
374 |
self.update_bar()?; |
| 313 |
} |
375 |
} |
| 314 |
|
376 |
|
| 315 |
- std::thread::sleep(std::time::Duration::from_millis(10)); |
377 |
+ std::thread::sleep(std::time::Duration::from_millis(100)); |
| 316 |
} |
378 |
} |
| 317 |
} |
379 |
} |
| 318 |
|
380 |
|
| 319 |
fn toggle_floating(&mut self) -> WmResult<()> { |
381 |
fn toggle_floating(&mut self) -> WmResult<()> { |
| 320 |
- if let Some(focused) = self.focused_window { |
382 |
+ if let Some(focused) = self |
|
|
383 |
+ .monitors |
|
|
384 |
+ .get(self.selected_monitor) |
|
|
385 |
+ .and_then(|m| m.focused_window) |
|
|
386 |
+ { |
| 321 |
if self.floating_windows.contains(&focused) { |
387 |
if self.floating_windows.contains(&focused) { |
| 322 |
self.floating_windows.remove(&focused); |
388 |
self.floating_windows.remove(&focused); |
| 323 |
self.apply_layout()?; |
389 |
self.apply_layout()?; |
| @@ -326,9 +392,10 @@ impl WindowManager { |
| 326 |
let float_height = (self.screen.height_in_pixels / 2) as u32; |
392 |
let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 327 |
|
393 |
|
| 328 |
let border_width = self.config.border_width; |
394 |
let border_width = self.config.border_width; |
| 329 |
- |
395 |
+ |
| 330 |
- let center_width = ((self.screen.width_in_pixels - float_width as u16)/2) as i32; |
396 |
+ let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 331 |
- let center_height = ((self.screen.height_in_pixels - float_height as u16)/2) as i32; |
397 |
+ let center_height = |
|
|
398 |
+ ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 332 |
|
399 |
|
| 333 |
self.connection.configure_window( |
400 |
self.connection.configure_window( |
| 334 |
focused, |
401 |
focused, |
| @@ -349,28 +416,367 @@ impl WindowManager { |
| 349 |
Ok(()) |
416 |
Ok(()) |
| 350 |
} |
417 |
} |
| 351 |
|
418 |
|
|
|
419 |
+ fn smart_move_window(&mut self, direction: i32) -> WmResult<()> { |
|
|
420 |
+ let focused = match self |
|
|
421 |
+ .monitors |
|
|
422 |
+ .get(self.selected_monitor) |
|
|
423 |
+ .and_then(|m| m.focused_window) |
|
|
424 |
+ { |
|
|
425 |
+ Some(win) => win, |
|
|
426 |
+ None => return Ok(()), |
|
|
427 |
+ }; |
|
|
428 |
+ |
|
|
429 |
+ if self.fullscreen_window == Some(focused) { |
|
|
430 |
+ return Ok(()); |
|
|
431 |
+ } |
|
|
432 |
+ |
|
|
433 |
+ if !self.floating_windows.contains(&focused) { |
|
|
434 |
+ let float_width = (self.screen.width_in_pixels / 2) as u32; |
|
|
435 |
+ let float_height = (self.screen.height_in_pixels / 2) as u32; |
|
|
436 |
+ let border_width = self.config.border_width; |
|
|
437 |
+ let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
|
|
438 |
+ let center_height = ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
|
|
439 |
+ |
|
|
440 |
+ self.connection.configure_window( |
|
|
441 |
+ focused, |
|
|
442 |
+ &ConfigureWindowAux::new() |
|
|
443 |
+ .x(center_width) |
|
|
444 |
+ .y(center_height) |
|
|
445 |
+ .width(float_width) |
|
|
446 |
+ .height(float_height) |
|
|
447 |
+ .border_width(border_width) |
|
|
448 |
+ .stack_mode(StackMode::ABOVE), |
|
|
449 |
+ )?; |
|
|
450 |
+ self.floating_windows.insert(focused); |
|
|
451 |
+ } |
|
|
452 |
+ |
|
|
453 |
+ let current_geom = match self.connection.get_geometry(focused)?.reply() { |
|
|
454 |
+ Ok(geom) => geom, |
|
|
455 |
+ Err(_) => return Ok(()), |
|
|
456 |
+ }; |
|
|
457 |
+ |
|
|
458 |
+ let c_x = current_geom.x as i32; |
|
|
459 |
+ let c_y = current_geom.y as i32; |
|
|
460 |
+ let c_width = current_geom.width as i32; |
|
|
461 |
+ let c_height = current_geom.height as i32; |
|
|
462 |
+ |
|
|
463 |
+ let monitor = match self.monitors.get(self.selected_monitor) { |
|
|
464 |
+ Some(m) => m, |
|
|
465 |
+ None => return Ok(()), |
|
|
466 |
+ }; |
|
|
467 |
+ |
|
|
468 |
+ let (gap_ih, gap_iv, gap_oh, gap_ov) = if self.gaps_enabled { |
|
|
469 |
+ ( |
|
|
470 |
+ self.config.gap_inner_horizontal as i32, |
|
|
471 |
+ self.config.gap_inner_vertical as i32, |
|
|
472 |
+ self.config.gap_outer_horizontal as i32, |
|
|
473 |
+ self.config.gap_outer_vertical as i32, |
|
|
474 |
+ ) |
|
|
475 |
+ } else { |
|
|
476 |
+ (0, 0, 0, 0) |
|
|
477 |
+ }; |
|
|
478 |
+ |
|
|
479 |
+ let (new_x, new_y) = match direction { |
|
|
480 |
+ 0 => { |
|
|
481 |
+ // UP |
|
|
482 |
+ let mut target = i32::MIN; |
|
|
483 |
+ let top = c_y; |
|
|
484 |
+ let mut ny = c_y - (monitor.height as i32 / 4); |
|
|
485 |
+ |
|
|
486 |
+ for &other_window in &self.windows { |
|
|
487 |
+ if other_window == focused { |
|
|
488 |
+ continue; |
|
|
489 |
+ } |
|
|
490 |
+ if !self.floating_windows.contains(&other_window) { |
|
|
491 |
+ continue; |
|
|
492 |
+ } |
|
|
493 |
+ if !self.is_window_visible(other_window) { |
|
|
494 |
+ continue; |
|
|
495 |
+ } |
|
|
496 |
+ let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
|
|
497 |
+ if other_mon != self.selected_monitor { |
|
|
498 |
+ continue; |
|
|
499 |
+ } |
|
|
500 |
+ |
|
|
501 |
+ let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
|
|
502 |
+ Ok(geom) => geom, |
|
|
503 |
+ Err(_) => continue, |
|
|
504 |
+ }; |
|
|
505 |
+ |
|
|
506 |
+ let o_x = other_geom.x as i32; |
|
|
507 |
+ let o_y = other_geom.y as i32; |
|
|
508 |
+ let o_width = other_geom.width as i32; |
|
|
509 |
+ let o_height = other_geom.height as i32; |
|
|
510 |
+ |
|
|
511 |
+ if c_x + c_width < o_x || c_x > o_x + o_width { |
|
|
512 |
+ continue; |
|
|
513 |
+ } |
|
|
514 |
+ |
|
|
515 |
+ let bottom = o_y + o_height + gap_iv; |
|
|
516 |
+ if top > bottom && ny < bottom { |
|
|
517 |
+ target = target.max(bottom); |
|
|
518 |
+ } |
|
|
519 |
+ } |
|
|
520 |
+ |
|
|
521 |
+ if target != i32::MIN { |
|
|
522 |
+ ny = target; |
|
|
523 |
+ } |
|
|
524 |
+ ny = ny.max(monitor.y as i32 + gap_ov); |
|
|
525 |
+ (c_x, ny) |
|
|
526 |
+ } |
|
|
527 |
+ 1 => { |
|
|
528 |
+ // DOWN |
|
|
529 |
+ let mut target = i32::MAX; |
|
|
530 |
+ let bottom = c_y + c_height; |
|
|
531 |
+ let mut ny = c_y + (monitor.height as i32 / 4); |
|
|
532 |
+ |
|
|
533 |
+ for &other_window in &self.windows { |
|
|
534 |
+ if other_window == focused { |
|
|
535 |
+ continue; |
|
|
536 |
+ } |
|
|
537 |
+ if !self.floating_windows.contains(&other_window) { |
|
|
538 |
+ continue; |
|
|
539 |
+ } |
|
|
540 |
+ if !self.is_window_visible(other_window) { |
|
|
541 |
+ continue; |
|
|
542 |
+ } |
|
|
543 |
+ let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
|
|
544 |
+ if other_mon != self.selected_monitor { |
|
|
545 |
+ continue; |
|
|
546 |
+ } |
|
|
547 |
+ |
|
|
548 |
+ let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
|
|
549 |
+ Ok(geom) => geom, |
|
|
550 |
+ Err(_) => continue, |
|
|
551 |
+ }; |
|
|
552 |
+ |
|
|
553 |
+ let o_x = other_geom.x as i32; |
|
|
554 |
+ let o_y = other_geom.y as i32; |
|
|
555 |
+ let o_width = other_geom.width as i32; |
|
|
556 |
+ |
|
|
557 |
+ if c_x + c_width < o_x || c_x > o_x + o_width { |
|
|
558 |
+ continue; |
|
|
559 |
+ } |
|
|
560 |
+ |
|
|
561 |
+ let top = o_y - gap_iv; |
|
|
562 |
+ if bottom < top && (ny + c_height) > top { |
|
|
563 |
+ target = target.min(top - c_height); |
|
|
564 |
+ } |
|
|
565 |
+ } |
|
|
566 |
+ |
|
|
567 |
+ if target != i32::MAX { |
|
|
568 |
+ ny = target; |
|
|
569 |
+ } |
|
|
570 |
+ ny = ny.min(monitor.y as i32 + monitor.height as i32 - c_height - gap_ov); |
|
|
571 |
+ (c_x, ny) |
|
|
572 |
+ } |
|
|
573 |
+ 2 => { |
|
|
574 |
+ // LEFT |
|
|
575 |
+ let mut target = i32::MIN; |
|
|
576 |
+ let left = c_x; |
|
|
577 |
+ let mut nx = c_x - (monitor.width as i32 / 6); |
|
|
578 |
+ |
|
|
579 |
+ for &other_window in &self.windows { |
|
|
580 |
+ if other_window == focused { |
|
|
581 |
+ continue; |
|
|
582 |
+ } |
|
|
583 |
+ if !self.floating_windows.contains(&other_window) { |
|
|
584 |
+ continue; |
|
|
585 |
+ } |
|
|
586 |
+ if !self.is_window_visible(other_window) { |
|
|
587 |
+ continue; |
|
|
588 |
+ } |
|
|
589 |
+ let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
|
|
590 |
+ if other_mon != self.selected_monitor { |
|
|
591 |
+ continue; |
|
|
592 |
+ } |
|
|
593 |
+ |
|
|
594 |
+ let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
|
|
595 |
+ Ok(geom) => geom, |
|
|
596 |
+ Err(_) => continue, |
|
|
597 |
+ }; |
|
|
598 |
+ |
|
|
599 |
+ let o_x = other_geom.x as i32; |
|
|
600 |
+ let o_y = other_geom.y as i32; |
|
|
601 |
+ let o_width = other_geom.width as i32; |
|
|
602 |
+ let o_height = other_geom.height as i32; |
|
|
603 |
+ |
|
|
604 |
+ if c_y + c_height < o_y || c_y > o_y + o_height { |
|
|
605 |
+ continue; |
|
|
606 |
+ } |
|
|
607 |
+ |
|
|
608 |
+ let right = o_x + o_width + gap_ih; |
|
|
609 |
+ if left > right && nx < right { |
|
|
610 |
+ target = target.max(right); |
|
|
611 |
+ } |
|
|
612 |
+ } |
|
|
613 |
+ |
|
|
614 |
+ if target != i32::MIN { |
|
|
615 |
+ nx = target; |
|
|
616 |
+ } |
|
|
617 |
+ nx = nx.max(monitor.x as i32 + gap_oh); |
|
|
618 |
+ (nx, c_y) |
|
|
619 |
+ } |
|
|
620 |
+ 3 => { |
|
|
621 |
+ // RIGHT |
|
|
622 |
+ let mut target = i32::MAX; |
|
|
623 |
+ let right = c_x + c_width; |
|
|
624 |
+ let mut nx = c_x + (monitor.width as i32 / 6); |
|
|
625 |
+ |
|
|
626 |
+ for &other_window in &self.windows { |
|
|
627 |
+ if other_window == focused { |
|
|
628 |
+ continue; |
|
|
629 |
+ } |
|
|
630 |
+ if !self.floating_windows.contains(&other_window) { |
|
|
631 |
+ continue; |
|
|
632 |
+ } |
|
|
633 |
+ if !self.is_window_visible(other_window) { |
|
|
634 |
+ continue; |
|
|
635 |
+ } |
|
|
636 |
+ let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
|
|
637 |
+ if other_mon != self.selected_monitor { |
|
|
638 |
+ continue; |
|
|
639 |
+ } |
|
|
640 |
+ |
|
|
641 |
+ let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
|
|
642 |
+ Ok(geom) => geom, |
|
|
643 |
+ Err(_) => continue, |
|
|
644 |
+ }; |
|
|
645 |
+ |
|
|
646 |
+ let o_x = other_geom.x as i32; |
|
|
647 |
+ let o_y = other_geom.y as i32; |
|
|
648 |
+ let o_height = other_geom.height as i32; |
|
|
649 |
+ |
|
|
650 |
+ if c_y + c_height < o_y || c_y > o_y + o_height { |
|
|
651 |
+ continue; |
|
|
652 |
+ } |
|
|
653 |
+ |
|
|
654 |
+ let left = o_x - gap_ih; |
|
|
655 |
+ if right < left && (nx + c_width) > left { |
|
|
656 |
+ target = target.min(left - c_width); |
|
|
657 |
+ } |
|
|
658 |
+ } |
|
|
659 |
+ |
|
|
660 |
+ if target != i32::MAX { |
|
|
661 |
+ nx = target; |
|
|
662 |
+ } |
|
|
663 |
+ nx = nx.min(monitor.x as i32 + monitor.width as i32 - c_width - gap_oh); |
|
|
664 |
+ (nx, c_y) |
|
|
665 |
+ } |
|
|
666 |
+ _ => return Ok(()), |
|
|
667 |
+ }; |
|
|
668 |
+ |
|
|
669 |
+ self.connection.configure_window( |
|
|
670 |
+ focused, |
|
|
671 |
+ &ConfigureWindowAux::new() |
|
|
672 |
+ .x(new_x) |
|
|
673 |
+ .y(new_y) |
|
|
674 |
+ .stack_mode(StackMode::ABOVE), |
|
|
675 |
+ )?; |
|
|
676 |
+ |
|
|
677 |
+ self.connection.flush()?; |
|
|
678 |
+ Ok(()) |
|
|
679 |
+ } |
|
|
680 |
+ |
|
|
681 |
+ fn exchange_client(&mut self, direction: i32) -> WmResult<()> { |
|
|
682 |
+ let focused = match self |
|
|
683 |
+ .monitors |
|
|
684 |
+ .get(self.selected_monitor) |
|
|
685 |
+ .and_then(|m| m.focused_window) |
|
|
686 |
+ { |
|
|
687 |
+ Some(win) => win, |
|
|
688 |
+ None => return Ok(()), |
|
|
689 |
+ }; |
|
|
690 |
+ |
|
|
691 |
+ if self.fullscreen_window == Some(focused) || self.floating_windows.contains(&focused) { |
|
|
692 |
+ return Ok(()); |
|
|
693 |
+ } |
|
|
694 |
+ |
|
|
695 |
+ let visible = self.visible_windows(); |
|
|
696 |
+ if visible.len() < 2 { |
|
|
697 |
+ return Ok(()); |
|
|
698 |
+ } |
|
|
699 |
+ |
|
|
700 |
+ let current_idx = match visible.iter().position(|&w| w == focused) { |
|
|
701 |
+ Some(idx) => idx, |
|
|
702 |
+ None => return Ok(()), |
|
|
703 |
+ }; |
|
|
704 |
+ |
|
|
705 |
+ let target_idx = match direction { |
|
|
706 |
+ 0 | 2 => { |
|
|
707 |
+ // UP or LEFT - previous in stack |
|
|
708 |
+ if current_idx == 0 { |
|
|
709 |
+ visible.len() - 1 |
|
|
710 |
+ } else { |
|
|
711 |
+ current_idx - 1 |
|
|
712 |
+ } |
|
|
713 |
+ } |
|
|
714 |
+ 1 | 3 => { |
|
|
715 |
+ // DOWN or RIGHT - next in stack |
|
|
716 |
+ (current_idx + 1) % visible.len() |
|
|
717 |
+ } |
|
|
718 |
+ _ => return Ok(()), |
|
|
719 |
+ }; |
|
|
720 |
+ |
|
|
721 |
+ let target = visible[target_idx]; |
|
|
722 |
+ |
|
|
723 |
+ let focused_pos = self.windows.iter().position(|&w| w == focused); |
|
|
724 |
+ let target_pos = self.windows.iter().position(|&w| w == target); |
|
|
725 |
+ |
|
|
726 |
+ if let (Some(f_pos), Some(t_pos)) = (focused_pos, target_pos) { |
|
|
727 |
+ self.windows.swap(f_pos, t_pos); |
|
|
728 |
+ |
|
|
729 |
+ self.apply_layout()?; |
|
|
730 |
+ |
|
|
731 |
+ self.set_focus(focused)?; |
|
|
732 |
+ |
|
|
733 |
+ if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() { |
|
|
734 |
+ self.connection.warp_pointer( |
|
|
735 |
+ x11rb::NONE, |
|
|
736 |
+ focused, |
|
|
737 |
+ 0, |
|
|
738 |
+ 0, |
|
|
739 |
+ 0, |
|
|
740 |
+ 0, |
|
|
741 |
+ geometry.width as i16 / 2, |
|
|
742 |
+ geometry.height as i16 / 2, |
|
|
743 |
+ )?; |
|
|
744 |
+ } |
|
|
745 |
+ } |
|
|
746 |
+ |
|
|
747 |
+ Ok(()) |
|
|
748 |
+ } |
|
|
749 |
+ |
| 352 |
fn toggle_fullscreen(&mut self) -> WmResult<()> { |
750 |
fn toggle_fullscreen(&mut self) -> WmResult<()> { |
| 353 |
- if let Some(focused) = self.focused_window { |
751 |
+ if let Some(focused) = self |
|
|
752 |
+ .monitors |
|
|
753 |
+ .get(self.selected_monitor) |
|
|
754 |
+ .and_then(|m| m.focused_window) |
|
|
755 |
+ { |
| 354 |
if self.fullscreen_window == Some(focused) { |
756 |
if self.fullscreen_window == Some(focused) { |
| 355 |
self.fullscreen_window = None; |
757 |
self.fullscreen_window = None; |
| 356 |
|
758 |
|
| 357 |
- self.connection.map_window(self.bar.window())?; |
759 |
+ for bar in &self.bars { |
|
|
760 |
+ self.connection.map_window(bar.window())?; |
|
|
761 |
+ } |
| 358 |
|
762 |
|
| 359 |
self.apply_layout()?; |
763 |
self.apply_layout()?; |
| 360 |
- self.update_focus_visuals()?; |
|
|
| 361 |
} else { |
764 |
} else { |
| 362 |
self.fullscreen_window = Some(focused); |
765 |
self.fullscreen_window = Some(focused); |
| 363 |
|
766 |
|
| 364 |
- self.connection.unmap_window(self.bar.window())?; |
767 |
+ if let Some(bar) = self.bars.get(self.selected_monitor) { |
|
|
768 |
+ self.connection.unmap_window(bar.window())?; |
|
|
769 |
+ } |
| 365 |
|
770 |
|
| 366 |
- let screen_width = self.screen.width_in_pixels as u32; |
771 |
+ let monitor = &self.monitors[self.selected_monitor]; |
| 367 |
- let screen_height = self.screen.height_in_pixels as u32; |
772 |
+ let screen_width = monitor.width; |
|
|
773 |
+ let screen_height = monitor.height; |
| 368 |
|
774 |
|
| 369 |
self.connection.configure_window( |
775 |
self.connection.configure_window( |
| 370 |
focused, |
776 |
focused, |
| 371 |
&ConfigureWindowAux::new() |
777 |
&ConfigureWindowAux::new() |
| 372 |
- .x(0) |
778 |
+ .x(monitor.x) |
| 373 |
- .y(0) |
779 |
+ .y(monitor.y) |
| 374 |
.width(screen_width) |
780 |
.width(screen_width) |
| 375 |
.height(screen_height) |
781 |
.height(screen_height) |
| 376 |
.border_width(0) |
782 |
.border_width(0) |
| @@ -384,14 +790,27 @@ impl WindowManager { |
| 384 |
} |
790 |
} |
| 385 |
|
791 |
|
| 386 |
fn update_bar(&mut self) -> WmResult<()> { |
792 |
fn update_bar(&mut self) -> WmResult<()> { |
| 387 |
- let mut occupied_tags: TagMask = 0; |
793 |
+ for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 388 |
- for &tags in self.window_tags.values() { |
794 |
+ if let Some(bar) = self.bars.get_mut(monitor_index) { |
| 389 |
- occupied_tags |= tags; |
795 |
+ let mut occupied_tags: TagMask = 0; |
| 390 |
- } |
796 |
+ for (&window, &tags) in &self.window_tags { |
|
|
797 |
+ if self.window_monitor.get(&window).copied().unwrap_or(0) == monitor_index { |
|
|
798 |
+ occupied_tags |= tags; |
|
|
799 |
+ } |
|
|
800 |
+ } |
| 391 |
|
801 |
|
| 392 |
- self.bar.invalidate(); |
802 |
+ let draw_blocks = monitor_index == self.selected_monitor; |
| 393 |
- self.bar |
803 |
+ bar.invalidate(); |
| 394 |
- .draw(&self.connection, self.selected_tags, occupied_tags)?; |
804 |
+ bar.draw( |
|
|
805 |
+ &self.connection, |
|
|
806 |
+ &self.font, |
|
|
807 |
+ self.display, |
|
|
808 |
+ monitor.selected_tags, |
|
|
809 |
+ occupied_tags, |
|
|
810 |
+ draw_blocks, |
|
|
811 |
+ )?; |
|
|
812 |
+ } |
|
|
813 |
+ } |
| 395 |
Ok(()) |
814 |
Ok(()) |
| 396 |
} |
815 |
} |
| 397 |
|
816 |
|
| @@ -399,7 +818,11 @@ impl WindowManager { |
| 399 |
match action { |
818 |
match action { |
| 400 |
KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
819 |
KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 401 |
KeyAction::KillClient => { |
820 |
KeyAction::KillClient => { |
| 402 |
- if let Some(focused) = self.focused_window { |
821 |
+ if let Some(focused) = self |
|
|
822 |
+ .monitors |
|
|
823 |
+ .get(self.selected_monitor) |
|
|
824 |
+ .and_then(|m| m.focused_window) |
|
|
825 |
+ { |
| 403 |
match self.connection.kill_client(focused) { |
826 |
match self.connection.kill_client(focused) { |
| 404 |
Ok(_) => { |
827 |
Ok(_) => { |
| 405 |
self.connection.flush()?; |
828 |
self.connection.flush()?; |
| @@ -413,10 +836,44 @@ impl WindowManager { |
| 413 |
KeyAction::ToggleFullScreen => { |
836 |
KeyAction::ToggleFullScreen => { |
| 414 |
self.toggle_fullscreen()?; |
837 |
self.toggle_fullscreen()?; |
| 415 |
} |
838 |
} |
|
|
839 |
+ KeyAction::ChangeLayout => { |
|
|
840 |
+ if let Arg::Str(layout_name) = arg { |
|
|
841 |
+ match layout_from_str(layout_name) { |
|
|
842 |
+ Ok(layout) => { |
|
|
843 |
+ self.layout = layout; |
|
|
844 |
+ self.apply_layout()?; |
|
|
845 |
+ } |
|
|
846 |
+ Err(e) => eprintln!("Failed to change layout: {}", e), |
|
|
847 |
+ } |
|
|
848 |
+ } |
|
|
849 |
+ } |
|
|
850 |
+ KeyAction::CycleLayout => { |
|
|
851 |
+ let current_name = self.layout.name(); |
|
|
852 |
+ let next_name = next_layout(current_name); |
|
|
853 |
+ match layout_from_str(next_name) { |
|
|
854 |
+ Ok(layout) => { |
|
|
855 |
+ self.layout = layout; |
|
|
856 |
+ self.apply_layout()?; |
|
|
857 |
+ } |
|
|
858 |
+ Err(e) => eprintln!("Failed to cycle layout: {}", e), |
|
|
859 |
+ } |
|
|
860 |
+ } |
| 416 |
KeyAction::ToggleFloating => { |
861 |
KeyAction::ToggleFloating => { |
| 417 |
self.toggle_floating()?; |
862 |
self.toggle_floating()?; |
| 418 |
} |
863 |
} |
| 419 |
|
864 |
|
|
|
865 |
+ KeyAction::SmartMoveWin => { |
|
|
866 |
+ if let Arg::Int(direction) = arg { |
|
|
867 |
+ self.smart_move_window(*direction)?; |
|
|
868 |
+ } |
|
|
869 |
+ } |
|
|
870 |
+ |
|
|
871 |
+ KeyAction::ExchangeClient => { |
|
|
872 |
+ if let Arg::Int(direction) = arg { |
|
|
873 |
+ self.exchange_client(*direction)?; |
|
|
874 |
+ } |
|
|
875 |
+ } |
|
|
876 |
+ |
| 420 |
KeyAction::FocusStack => { |
877 |
KeyAction::FocusStack => { |
| 421 |
if let Arg::Int(direction) = arg { |
878 |
if let Arg::Int(direction) = arg { |
| 422 |
self.cycle_focus(*direction)?; |
879 |
self.cycle_focus(*direction)?; |
| @@ -448,14 +905,50 @@ impl WindowManager { |
| 448 |
self.gaps_enabled = !self.gaps_enabled; |
905 |
self.gaps_enabled = !self.gaps_enabled; |
| 449 |
self.apply_layout()?; |
906 |
self.apply_layout()?; |
| 450 |
} |
907 |
} |
|
|
908 |
+ KeyAction::FocusMonitor => { |
|
|
909 |
+ if let Arg::Int(direction) = arg { |
|
|
910 |
+ self.focus_monitor(*direction)?; |
|
|
911 |
+ } |
|
|
912 |
+ } |
| 451 |
KeyAction::None => {} |
913 |
KeyAction::None => {} |
| 452 |
} |
914 |
} |
| 453 |
Ok(()) |
915 |
Ok(()) |
| 454 |
} |
916 |
} |
| 455 |
|
917 |
|
|
|
918 |
+ fn focus_monitor(&mut self, direction: i32) -> WmResult<()> { |
|
|
919 |
+ if self.monitors.is_empty() { |
|
|
920 |
+ return Ok(()); |
|
|
921 |
+ } |
|
|
922 |
+ |
|
|
923 |
+ let new_monitor = if direction > 0 { |
|
|
924 |
+ (self.selected_monitor + 1) % self.monitors.len() |
|
|
925 |
+ } else { |
|
|
926 |
+ (self.selected_monitor + self.monitors.len() - 1) % self.monitors.len() |
|
|
927 |
+ }; |
|
|
928 |
+ |
|
|
929 |
+ if new_monitor == self.selected_monitor { |
|
|
930 |
+ return Ok(()); |
|
|
931 |
+ } |
|
|
932 |
+ |
|
|
933 |
+ self.selected_monitor = new_monitor; |
|
|
934 |
+ |
|
|
935 |
+ self.update_bar()?; |
|
|
936 |
+ |
|
|
937 |
+ let visible = self.visible_windows_on_monitor(new_monitor); |
|
|
938 |
+ if let Some(&win) = visible.first() { |
|
|
939 |
+ self.set_focus(win)?; |
|
|
940 |
+ } |
|
|
941 |
+ |
|
|
942 |
+ Ok(()) |
|
|
943 |
+ } |
|
|
944 |
+ |
| 456 |
fn is_window_visible(&self, window: Window) -> bool { |
945 |
fn is_window_visible(&self, window: Window) -> bool { |
|
|
946 |
+ let window_mon = self.window_monitor.get(&window).copied().unwrap_or(0); |
|
|
947 |
+ |
| 457 |
if let Some(&tags) = self.window_tags.get(&window) { |
948 |
if let Some(&tags) = self.window_tags.get(&window) { |
| 458 |
- (tags & self.selected_tags) != 0 |
949 |
+ let monitor = self.monitors.get(window_mon); |
|
|
950 |
+ let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
|
|
951 |
+ (tags & selected_tags) != 0 |
| 459 |
} else { |
952 |
} else { |
| 460 |
false |
953 |
false |
| 461 |
} |
954 |
} |
| @@ -469,6 +962,32 @@ impl WindowManager { |
| 469 |
.collect() |
962 |
.collect() |
| 470 |
} |
963 |
} |
| 471 |
|
964 |
|
|
|
965 |
+ fn visible_windows_on_monitor(&self, monitor_index: usize) -> Vec<Window> { |
|
|
966 |
+ self.windows |
|
|
967 |
+ .iter() |
|
|
968 |
+ .filter(|&&w| { |
|
|
969 |
+ let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
|
|
970 |
+ if window_mon != monitor_index { |
|
|
971 |
+ return false; |
|
|
972 |
+ } |
|
|
973 |
+ if let Some(&tags) = self.window_tags.get(&w) { |
|
|
974 |
+ let monitor = self.monitors.get(monitor_index); |
|
|
975 |
+ let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
|
|
976 |
+ (tags & selected_tags) != 0 |
|
|
977 |
+ } else { |
|
|
978 |
+ false |
|
|
979 |
+ } |
|
|
980 |
+ }) |
|
|
981 |
+ .copied() |
|
|
982 |
+ .collect() |
|
|
983 |
+ } |
|
|
984 |
+ |
|
|
985 |
+ fn get_monitor_at_point(&self, x: i32, y: i32) -> Option<usize> { |
|
|
986 |
+ self.monitors |
|
|
987 |
+ .iter() |
|
|
988 |
+ .position(|mon| mon.contains_point(x, y)) |
|
|
989 |
+ } |
|
|
990 |
+ |
| 472 |
fn update_window_visibility(&self) -> WmResult<()> { |
991 |
fn update_window_visibility(&self) -> WmResult<()> { |
| 473 |
for &window in &self.windows { |
992 |
for &window in &self.windows { |
| 474 |
if self.is_window_visible(window) { |
993 |
if self.is_window_visible(window) { |
| @@ -488,10 +1007,14 @@ impl WindowManager { |
| 488 |
|
1007 |
|
| 489 |
if self.fullscreen_window.is_some() { |
1008 |
if self.fullscreen_window.is_some() { |
| 490 |
self.fullscreen_window = None; |
1009 |
self.fullscreen_window = None; |
| 491 |
- self.connection.map_window(self.bar.window())?; |
1010 |
+ for bar in &self.bars { |
|
|
1011 |
+ self.connection.map_window(bar.window())?; |
|
|
1012 |
+ } |
| 492 |
} |
1013 |
} |
| 493 |
|
1014 |
|
| 494 |
- self.selected_tags = tag_mask(tag_index); |
1015 |
+ if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
|
|
1016 |
+ monitor.selected_tags = tag_mask(tag_index); |
|
|
1017 |
+ } |
| 495 |
|
1018 |
|
| 496 |
self.save_selected_tags()?; |
1019 |
self.save_selected_tags()?; |
| 497 |
|
1020 |
|
| @@ -499,20 +1022,23 @@ impl WindowManager { |
| 499 |
self.apply_layout()?; |
1022 |
self.apply_layout()?; |
| 500 |
self.update_bar()?; |
1023 |
self.update_bar()?; |
| 501 |
|
1024 |
|
| 502 |
- let visible = self.visible_windows(); |
1025 |
+ let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 503 |
- self.set_focus(visible.first().copied())?; |
1026 |
+ if let Some(&win) = visible.first() { |
|
|
1027 |
+ self.set_focus(win)?; |
|
|
1028 |
+ } |
| 504 |
|
1029 |
|
| 505 |
Ok(()) |
1030 |
Ok(()) |
| 506 |
} |
1031 |
} |
| 507 |
|
1032 |
|
| 508 |
fn save_selected_tags(&self) -> WmResult<()> { |
1033 |
fn save_selected_tags(&self) -> WmResult<()> { |
| 509 |
- let net_current_desktop = self |
1034 |
+ let net_current_desktop = self.atoms.net_current_desktop; |
| 510 |
- .connection |
|
|
| 511 |
- .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
|
|
| 512 |
- .reply()? |
|
|
| 513 |
- .atom; |
|
|
| 514 |
|
1035 |
|
| 515 |
- let desktop = self.selected_tags.trailing_zeros(); |
1036 |
+ let selected_tags = self |
|
|
1037 |
+ .monitors |
|
|
1038 |
+ .get(self.selected_monitor) |
|
|
1039 |
+ .map(|m| m.selected_tags) |
|
|
1040 |
+ .unwrap_or(tag_mask(0)); |
|
|
1041 |
+ let desktop = selected_tags.trailing_zeros(); |
| 516 |
|
1042 |
|
| 517 |
let bytes = (desktop as u32).to_ne_bytes(); |
1043 |
let bytes = (desktop as u32).to_ne_bytes(); |
| 518 |
self.connection.change_property( |
1044 |
self.connection.change_property( |
| @@ -534,7 +1060,11 @@ impl WindowManager { |
| 534 |
return Ok(()); |
1060 |
return Ok(()); |
| 535 |
} |
1061 |
} |
| 536 |
|
1062 |
|
| 537 |
- if let Some(focused) = self.focused_window { |
1063 |
+ if let Some(focused) = self |
|
|
1064 |
+ .monitors |
|
|
1065 |
+ .get(self.selected_monitor) |
|
|
1066 |
+ .and_then(|m| m.focused_window) |
|
|
1067 |
+ { |
| 538 |
let mask = tag_mask(tag_index); |
1068 |
let mask = tag_mask(tag_index); |
| 539 |
self.window_tags.insert(focused, mask); |
1069 |
self.window_tags.insert(focused, mask); |
| 540 |
|
1070 |
|
| @@ -555,7 +1085,12 @@ impl WindowManager { |
| 555 |
return Ok(()); |
1085 |
return Ok(()); |
| 556 |
} |
1086 |
} |
| 557 |
|
1087 |
|
| 558 |
- let next_window = if let Some(current) = self.focused_window { |
1088 |
+ let current = self |
|
|
1089 |
+ .monitors |
|
|
1090 |
+ .get(self.selected_monitor) |
|
|
1091 |
+ .and_then(|m| m.focused_window); |
|
|
1092 |
+ |
|
|
1093 |
+ let next_window = if let Some(current) = current { |
| 559 |
if let Some(current_index) = visible.iter().position(|&w| w == current) { |
1094 |
if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 560 |
let next_index = if direction > 0 { |
1095 |
let next_index = if direction > 0 { |
| 561 |
(current_index + 1) % visible.len() |
1096 |
(current_index + 1) % visible.len() |
| @@ -570,41 +1105,55 @@ impl WindowManager { |
| 570 |
visible[0] |
1105 |
visible[0] |
| 571 |
}; |
1106 |
}; |
| 572 |
|
1107 |
|
| 573 |
- self.set_focus(Some(next_window))?; |
1108 |
+ self.set_focus(next_window)?; |
| 574 |
Ok(()) |
1109 |
Ok(()) |
| 575 |
} |
1110 |
} |
| 576 |
|
1111 |
|
| 577 |
- pub fn set_focus(&mut self, window: Option<Window>) -> WmResult<()> { |
1112 |
+ pub fn set_focus(&mut self, window: Window) -> WmResult<()> { |
| 578 |
- self.focused_window = window; |
1113 |
+ let old_focused = self.previous_focused; |
| 579 |
|
1114 |
|
| 580 |
- if let Some(win) = window { |
1115 |
+ if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 581 |
- self.connection |
1116 |
+ monitor.focused_window = Some(window); |
| 582 |
- .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?; |
|
|
| 583 |
- self.connection.flush()?; |
|
|
| 584 |
} |
1117 |
} |
| 585 |
|
1118 |
|
| 586 |
- self.update_focus_visuals()?; |
1119 |
+ self.connection |
|
|
1120 |
+ .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; |
|
|
1121 |
+ self.connection.flush()?; |
|
|
1122 |
+ |
|
|
1123 |
+ self.update_focus_visuals(old_focused, window)?; |
|
|
1124 |
+ self.previous_focused = Some(window); |
| 587 |
Ok(()) |
1125 |
Ok(()) |
| 588 |
} |
1126 |
} |
| 589 |
|
1127 |
|
| 590 |
- fn update_focus_visuals(&self) -> WmResult<()> { |
1128 |
+ fn update_focus_visuals( |
| 591 |
- for &window in &self.windows { |
1129 |
+ &self, |
| 592 |
- let (border_color, border_width) = if self.focused_window == Some(window) { |
1130 |
+ old_focused: Option<Window>, |
| 593 |
- (self.config.border_focused, self.config.border_width) |
1131 |
+ new_focused: Window, |
| 594 |
- } else { |
1132 |
+ ) -> WmResult<()> { |
| 595 |
- (self.config.border_unfocused, self.config.border_width) |
1133 |
+ if let Some(old_win) = old_focused { |
| 596 |
- }; |
1134 |
+ if old_win != new_focused { |
| 597 |
- |
1135 |
+ self.connection.configure_window( |
| 598 |
- self.connection.configure_window( |
1136 |
+ old_win, |
| 599 |
- window, |
1137 |
+ &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 600 |
- &ConfigureWindowAux::new().border_width(border_width), |
1138 |
+ )?; |
| 601 |
- )?; |
|
|
| 602 |
|
1139 |
|
| 603 |
- self.connection.change_window_attributes( |
1140 |
+ self.connection.change_window_attributes( |
| 604 |
- window, |
1141 |
+ old_win, |
| 605 |
- &ChangeWindowAttributesAux::new().border_pixel(border_color), |
1142 |
+ &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), |
| 606 |
- )?; |
1143 |
+ )?; |
|
|
1144 |
+ } |
| 607 |
} |
1145 |
} |
|
|
1146 |
+ |
|
|
1147 |
+ self.connection.configure_window( |
|
|
1148 |
+ new_focused, |
|
|
1149 |
+ &ConfigureWindowAux::new().border_width(self.config.border_width), |
|
|
1150 |
+ )?; |
|
|
1151 |
+ |
|
|
1152 |
+ self.connection.change_window_attributes( |
|
|
1153 |
+ new_focused, |
|
|
1154 |
+ &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), |
|
|
1155 |
+ )?; |
|
|
1156 |
+ |
| 608 |
self.connection.flush()?; |
1157 |
self.connection.flush()?; |
| 609 |
Ok(()) |
1158 |
Ok(()) |
| 610 |
} |
1159 |
} |
| @@ -742,20 +1291,33 @@ impl WindowManager { |
| 742 |
return Ok(None); |
1291 |
return Ok(None); |
| 743 |
} |
1292 |
} |
| 744 |
|
1293 |
|
|
|
1294 |
+ if let Ok(geom) = self.connection.get_geometry(event.window)?.reply() { |
|
|
1295 |
+ self.window_geometries |
|
|
1296 |
+ .insert(event.window, (geom.x, geom.y, geom.width, geom.height)); |
|
|
1297 |
+ } |
|
|
1298 |
+ |
| 745 |
self.connection.map_window(event.window)?; |
1299 |
self.connection.map_window(event.window)?; |
| 746 |
self.connection.change_window_attributes( |
1300 |
self.connection.change_window_attributes( |
| 747 |
event.window, |
1301 |
event.window, |
| 748 |
&ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), |
1302 |
&ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), |
| 749 |
)?; |
1303 |
)?; |
| 750 |
|
1304 |
|
|
|
1305 |
+ let selected_tags = self |
|
|
1306 |
+ .monitors |
|
|
1307 |
+ .get(self.selected_monitor) |
|
|
1308 |
+ .map(|m| m.selected_tags) |
|
|
1309 |
+ .unwrap_or(tag_mask(0)); |
|
|
1310 |
+ |
| 751 |
self.windows.push(event.window); |
1311 |
self.windows.push(event.window); |
| 752 |
- self.window_tags.insert(event.window, self.selected_tags); |
1312 |
+ self.window_tags.insert(event.window, selected_tags); |
|
|
1313 |
+ self.window_monitor |
|
|
1314 |
+ .insert(event.window, self.selected_monitor); |
| 753 |
self.set_wm_state(event.window, 1)?; |
1315 |
self.set_wm_state(event.window, 1)?; |
| 754 |
- let _ = self.save_client_tag(event.window, self.selected_tags); |
1316 |
+ let _ = self.save_client_tag(event.window, selected_tags); |
| 755 |
|
1317 |
|
| 756 |
self.apply_layout()?; |
1318 |
self.apply_layout()?; |
| 757 |
self.update_bar()?; |
1319 |
self.update_bar()?; |
| 758 |
- self.set_focus(Some(event.window))?; |
1320 |
+ self.set_focus(event.window)?; |
| 759 |
} |
1321 |
} |
| 760 |
Event::UnmapNotify(event) => { |
1322 |
Event::UnmapNotify(event) => { |
| 761 |
if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
1323 |
if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
| @@ -772,7 +1334,26 @@ impl WindowManager { |
| 772 |
return Ok(None); |
1334 |
return Ok(None); |
| 773 |
} |
1335 |
} |
| 774 |
if self.windows.contains(&event.event) { |
1336 |
if self.windows.contains(&event.event) { |
| 775 |
- self.set_focus(Some(event.event))?; |
1337 |
+ self.set_focus(event.event)?; |
|
|
1338 |
+ } |
|
|
1339 |
+ } |
|
|
1340 |
+ Event::MotionNotify(event) => { |
|
|
1341 |
+ if event.event != self.root { |
|
|
1342 |
+ return Ok(None); |
|
|
1343 |
+ } |
|
|
1344 |
+ |
|
|
1345 |
+ if let Some(monitor_index) = |
|
|
1346 |
+ self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) |
|
|
1347 |
+ { |
|
|
1348 |
+ if monitor_index != self.selected_monitor { |
|
|
1349 |
+ self.selected_monitor = monitor_index; |
|
|
1350 |
+ self.update_bar()?; |
|
|
1351 |
+ |
|
|
1352 |
+ let visible = self.visible_windows_on_monitor(monitor_index); |
|
|
1353 |
+ if let Some(&win) = visible.first() { |
|
|
1354 |
+ self.set_focus(win)?; |
|
|
1355 |
+ } |
|
|
1356 |
+ } |
| 776 |
} |
1357 |
} |
| 777 |
} |
1358 |
} |
| 778 |
Event::KeyPress(event) => { |
1359 |
Event::KeyPress(event) => { |
| @@ -784,12 +1365,21 @@ impl WindowManager { |
| 784 |
} |
1365 |
} |
| 785 |
} |
1366 |
} |
| 786 |
Event::ButtonPress(event) => { |
1367 |
Event::ButtonPress(event) => { |
| 787 |
- if event.event == self.bar.window() { |
1368 |
+ let is_bar_click = self |
| 788 |
- if let Some(tag_index) = self.bar.handle_click(event.event_x) { |
1369 |
+ .bars |
|
|
1370 |
+ .iter() |
|
|
1371 |
+ .enumerate() |
|
|
1372 |
+ .find(|(_, bar)| bar.window() == event.event); |
|
|
1373 |
+ |
|
|
1374 |
+ if let Some((monitor_index, bar)) = is_bar_click { |
|
|
1375 |
+ if let Some(tag_index) = bar.handle_click(event.event_x) { |
|
|
1376 |
+ if monitor_index != self.selected_monitor { |
|
|
1377 |
+ self.selected_monitor = monitor_index; |
|
|
1378 |
+ } |
| 789 |
self.view_tag(tag_index)?; |
1379 |
self.view_tag(tag_index)?; |
| 790 |
} |
1380 |
} |
| 791 |
} else if event.child != x11rb::NONE { |
1381 |
} else if event.child != x11rb::NONE { |
| 792 |
- self.set_focus(Some(event.child))?; |
1382 |
+ self.set_focus(event.child)?; |
| 793 |
|
1383 |
|
| 794 |
if event.detail == ButtonIndex::M1.into() { |
1384 |
if event.detail == ButtonIndex::M1.into() { |
| 795 |
self.move_mouse(event.child)?; |
1385 |
self.move_mouse(event.child)?; |
| @@ -799,9 +1389,12 @@ impl WindowManager { |
| 799 |
} |
1389 |
} |
| 800 |
} |
1390 |
} |
| 801 |
Event::Expose(event) => { |
1391 |
Event::Expose(event) => { |
| 802 |
- if event.window == self.bar.window() { |
1392 |
+ for bar in &mut self.bars { |
| 803 |
- self.bar.invalidate(); |
1393 |
+ if event.window == bar.window() { |
| 804 |
- self.update_bar()?; |
1394 |
+ bar.invalidate(); |
|
|
1395 |
+ self.update_bar()?; |
|
|
1396 |
+ break; |
|
|
1397 |
+ } |
| 805 |
} |
1398 |
} |
| 806 |
} |
1399 |
} |
| 807 |
_ => {} |
1400 |
_ => {} |
| @@ -813,12 +1406,12 @@ impl WindowManager { |
| 813 |
if self.fullscreen_window.is_some() { |
1406 |
if self.fullscreen_window.is_some() { |
| 814 |
return Ok(()); |
1407 |
return Ok(()); |
| 815 |
} |
1408 |
} |
| 816 |
- let screen_width = self.screen.width_in_pixels as u32; |
|
|
| 817 |
- let screen_height = self.screen.height_in_pixels as u32; |
|
|
| 818 |
- let border_width = self.config.border_width; |
|
|
| 819 |
|
1409 |
|
| 820 |
- let bar_height = self.bar.height() as u32; |
1410 |
+ if self.layout.name() == crate::layout::NORMIE { |
| 821 |
- let usable_height = screen_height.saturating_sub(bar_height); |
1411 |
+ return Ok(()); |
|
|
1412 |
+ } |
|
|
1413 |
+ |
|
|
1414 |
+ let border_width = self.config.border_width; |
| 822 |
|
1415 |
|
| 823 |
let gaps = if self.gaps_enabled { |
1416 |
let gaps = if self.gaps_enabled { |
| 824 |
GapConfig { |
1417 |
GapConfig { |
| @@ -836,54 +1429,93 @@ impl WindowManager { |
| 836 |
} |
1429 |
} |
| 837 |
}; |
1430 |
}; |
| 838 |
|
1431 |
|
| 839 |
- let visible: Vec<Window> = self |
1432 |
+ for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 840 |
- .visible_windows() |
1433 |
+ let visible: Vec<Window> = self |
| 841 |
- .into_iter() |
1434 |
+ .windows |
| 842 |
- .filter(|w| !self.floating_windows.contains(w)) |
1435 |
+ .iter() |
| 843 |
- .collect(); |
1436 |
+ .filter(|&&w| { |
|
|
1437 |
+ let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
|
|
1438 |
+ if window_mon != monitor_index { |
|
|
1439 |
+ return false; |
|
|
1440 |
+ } |
|
|
1441 |
+ if self.floating_windows.contains(&w) { |
|
|
1442 |
+ return false; |
|
|
1443 |
+ } |
|
|
1444 |
+ if let Some(&tags) = self.window_tags.get(&w) { |
|
|
1445 |
+ (tags & monitor.selected_tags) != 0 |
|
|
1446 |
+ } else { |
|
|
1447 |
+ false |
|
|
1448 |
+ } |
|
|
1449 |
+ }) |
|
|
1450 |
+ .copied() |
|
|
1451 |
+ .collect(); |
|
|
1452 |
+ |
|
|
1453 |
+ let bar_height = self |
|
|
1454 |
+ .bars |
|
|
1455 |
+ .get(monitor_index) |
|
|
1456 |
+ .map(|b| b.height() as u32) |
|
|
1457 |
+ .unwrap_or(0); |
|
|
1458 |
+ let usable_height = monitor.height.saturating_sub(bar_height); |
| 844 |
|
1459 |
|
| 845 |
- let geometries = self |
1460 |
+ let geometries = self |
| 846 |
- .layout |
1461 |
+ .layout |
| 847 |
- .arrange(&visible, screen_width, usable_height, &gaps); |
1462 |
+ .arrange(&visible, monitor.width, usable_height, &gaps); |
| 848 |
|
1463 |
|
| 849 |
- for (window, geometry) in visible.iter().zip(geometries.iter()) { |
1464 |
+ for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 850 |
- let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
1465 |
+ let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 851 |
- let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
1466 |
+ let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 852 |
|
1467 |
|
| 853 |
- let adjusted_y = geometry.y_coordinate + bar_height as i32; |
1468 |
+ let adjusted_x = geometry.x_coordinate + monitor.x; |
|
|
1469 |
+ let adjusted_y = geometry.y_coordinate + monitor.y + bar_height as i32; |
| 854 |
|
1470 |
|
| 855 |
- self.connection.configure_window( |
1471 |
+ self.connection.configure_window( |
| 856 |
- *window, |
1472 |
+ *window, |
| 857 |
- &ConfigureWindowAux::new() |
1473 |
+ &ConfigureWindowAux::new() |
| 858 |
- .x(geometry.x_coordinate) |
1474 |
+ .x(adjusted_x) |
| 859 |
- .y(adjusted_y) |
1475 |
+ .y(adjusted_y) |
| 860 |
- .width(adjusted_width) |
1476 |
+ .width(adjusted_width) |
| 861 |
- .height(adjusted_height), |
1477 |
+ .height(adjusted_height), |
| 862 |
- )?; |
1478 |
+ )?; |
|
|
1479 |
+ } |
| 863 |
} |
1480 |
} |
|
|
1481 |
+ |
| 864 |
self.connection.flush()?; |
1482 |
self.connection.flush()?; |
| 865 |
Ok(()) |
1483 |
Ok(()) |
| 866 |
} |
1484 |
} |
| 867 |
|
1485 |
|
|
|
1486 |
+ pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> { |
|
|
1487 |
+ self.layout = Box::new(new_layout); |
|
|
1488 |
+ self.apply_layout()?; |
|
|
1489 |
+ Ok(()) |
|
|
1490 |
+ } |
|
|
1491 |
+ |
| 868 |
fn remove_window(&mut self, window: Window) -> WmResult<()> { |
1492 |
fn remove_window(&mut self, window: Window) -> WmResult<()> { |
| 869 |
let initial_count = self.windows.len(); |
1493 |
let initial_count = self.windows.len(); |
| 870 |
self.windows.retain(|&w| w != window); |
1494 |
self.windows.retain(|&w| w != window); |
| 871 |
self.window_tags.remove(&window); |
1495 |
self.window_tags.remove(&window); |
|
|
1496 |
+ self.window_monitor.remove(&window); |
|
|
1497 |
+ self.window_geometries.remove(&window); |
| 872 |
self.floating_windows.remove(&window); |
1498 |
self.floating_windows.remove(&window); |
| 873 |
|
1499 |
|
| 874 |
if self.fullscreen_window == Some(window) { |
1500 |
if self.fullscreen_window == Some(window) { |
| 875 |
self.fullscreen_window = None; |
1501 |
self.fullscreen_window = None; |
| 876 |
- self.connection.map_window(self.bar.window())?; |
1502 |
+ for bar in &self.bars { |
|
|
1503 |
+ self.connection.map_window(bar.window())?; |
|
|
1504 |
+ } |
| 877 |
} |
1505 |
} |
| 878 |
|
1506 |
|
| 879 |
if self.windows.len() < initial_count { |
1507 |
if self.windows.len() < initial_count { |
| 880 |
- if self.focused_window == Some(window) { |
1508 |
+ let focused = self |
| 881 |
- let new_focus = if self.windows.is_empty() { |
1509 |
+ .monitors |
| 882 |
- None |
1510 |
+ .get(self.selected_monitor) |
| 883 |
- } else { |
1511 |
+ .and_then(|m| m.focused_window); |
| 884 |
- Some(self.windows[self.windows.len() - 1]) |
1512 |
+ if focused == Some(window) { |
| 885 |
- }; |
1513 |
+ let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 886 |
- self.set_focus(new_focus)?; |
1514 |
+ if let Some(&new_win) = visible.last() { |
|
|
1515 |
+ self.set_focus(new_win)?; |
|
|
1516 |
+ } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
|
|
1517 |
+ monitor.focused_window = None; |
|
|
1518 |
+ } |
| 887 |
} |
1519 |
} |
| 888 |
|
1520 |
|
| 889 |
self.apply_layout()?; |
1521 |
self.apply_layout()?; |