| @@ -3,8 +3,8 @@ 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 crate::monitor::{Monitor, detect_monitors}; |
| 9 |
use std::collections::HashSet; |
9 |
use std::collections::HashSet; |
| 10 |
use x11rb::cursor::Handle as CursorHandle; |
10 |
use x11rb::cursor::Handle as CursorHandle; |
| @@ -37,10 +37,7 @@ impl AtomCache { |
| 37 |
.reply()? |
37 |
.reply()? |
| 38 |
.atom; |
38 |
.atom; |
| 39 |
|
39 |
|
| 40 |
- let wm_state = connection |
40 |
+ let wm_state = connection.intern_atom(false, b"WM_STATE")?.reply()?.atom; |
| 41 |
- .intern_atom(false, b"WM_STATE")? |
|
|
| 42 |
- .reply()? |
|
|
| 43 |
- .atom; |
|
|
| 44 |
|
41 |
|
| 45 |
Ok(Self { |
42 |
Ok(Self { |
| 46 |
net_current_desktop, |
43 |
net_current_desktop, |
| @@ -128,12 +125,7 @@ impl WindowManager { |
| 128 |
u16::from(config.modkey).into(), |
125 |
u16::from(config.modkey).into(), |
| 129 |
)?; |
126 |
)?; |
| 130 |
|
127 |
|
| 131 |
- let mut monitors = detect_monitors(&connection, &screen, root)?; |
128 |
+ let monitors = detect_monitors(&connection, &screen, root)?; |
| 132 |
- |
|
|
| 133 |
- let selected_tags = Self::get_saved_selected_tags(&connection, root, config.tags.len())?; |
|
|
| 134 |
- if !monitors.is_empty() { |
|
|
| 135 |
- monitors[0].selected_tags = selected_tags; |
|
|
| 136 |
- } |
|
|
| 137 |
|
129 |
|
| 138 |
let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
130 |
let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
| 139 |
if display.is_null() { |
131 |
if display.is_null() { |
| @@ -308,7 +300,11 @@ impl WindowManager { |
| 308 |
} |
300 |
} |
| 309 |
} |
301 |
} |
| 310 |
|
302 |
|
| 311 |
- Ok(self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0))) |
303 |
+ Ok(self |
|
|
304 |
+ .monitors |
|
|
305 |
+ .get(self.selected_monitor) |
|
|
306 |
+ .map(|m| m.selected_tags) |
|
|
307 |
+ .unwrap_or(tag_mask(0))) |
| 312 |
} |
308 |
} |
| 313 |
|
309 |
|
| 314 |
fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
310 |
fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| @@ -376,7 +372,11 @@ impl WindowManager { |
| 376 |
} |
372 |
} |
| 377 |
|
373 |
|
| 378 |
fn toggle_floating(&mut self) -> WmResult<()> { |
374 |
fn toggle_floating(&mut self) -> WmResult<()> { |
| 379 |
- if let Some(focused) = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window) { |
375 |
+ if let Some(focused) = self |
|
|
376 |
+ .monitors |
|
|
377 |
+ .get(self.selected_monitor) |
|
|
378 |
+ .and_then(|m| m.focused_window) |
|
|
379 |
+ { |
| 380 |
if self.floating_windows.contains(&focused) { |
380 |
if self.floating_windows.contains(&focused) { |
| 381 |
self.floating_windows.remove(&focused); |
381 |
self.floating_windows.remove(&focused); |
| 382 |
self.apply_layout()?; |
382 |
self.apply_layout()?; |
| @@ -385,9 +385,10 @@ impl WindowManager { |
| 385 |
let float_height = (self.screen.height_in_pixels / 2) as u32; |
385 |
let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 386 |
|
386 |
|
| 387 |
let border_width = self.config.border_width; |
387 |
let border_width = self.config.border_width; |
| 388 |
- |
388 |
+ |
| 389 |
- let center_width = ((self.screen.width_in_pixels - float_width as u16)/2) as i32; |
389 |
+ let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 390 |
- let center_height = ((self.screen.height_in_pixels - float_height as u16)/2) as i32; |
390 |
+ let center_height = |
|
|
391 |
+ ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 391 |
|
392 |
|
| 392 |
self.connection.configure_window( |
393 |
self.connection.configure_window( |
| 393 |
focused, |
394 |
focused, |
| @@ -409,7 +410,11 @@ impl WindowManager { |
| 409 |
} |
410 |
} |
| 410 |
|
411 |
|
| 411 |
fn toggle_fullscreen(&mut self) -> WmResult<()> { |
412 |
fn toggle_fullscreen(&mut self) -> WmResult<()> { |
| 412 |
- if let Some(focused) = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window) { |
413 |
+ if let Some(focused) = self |
|
|
414 |
+ .monitors |
|
|
415 |
+ .get(self.selected_monitor) |
|
|
416 |
+ .and_then(|m| m.focused_window) |
|
|
417 |
+ { |
| 413 |
if self.fullscreen_window == Some(focused) { |
418 |
if self.fullscreen_window == Some(focused) { |
| 414 |
self.fullscreen_window = None; |
419 |
self.fullscreen_window = None; |
| 415 |
|
420 |
|
| @@ -458,7 +463,14 @@ impl WindowManager { |
| 458 |
|
463 |
|
| 459 |
let draw_blocks = monitor_index == self.selected_monitor; |
464 |
let draw_blocks = monitor_index == self.selected_monitor; |
| 460 |
bar.invalidate(); |
465 |
bar.invalidate(); |
| 461 |
- bar.draw(&self.connection, &self.font, self.display, monitor.selected_tags, occupied_tags, draw_blocks)?; |
466 |
+ bar.draw( |
|
|
467 |
+ &self.connection, |
|
|
468 |
+ &self.font, |
|
|
469 |
+ self.display, |
|
|
470 |
+ monitor.selected_tags, |
|
|
471 |
+ occupied_tags, |
|
|
472 |
+ draw_blocks, |
|
|
473 |
+ )?; |
| 462 |
} |
474 |
} |
| 463 |
} |
475 |
} |
| 464 |
Ok(()) |
476 |
Ok(()) |
| @@ -468,7 +480,11 @@ impl WindowManager { |
| 468 |
match action { |
480 |
match action { |
| 469 |
KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
481 |
KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 470 |
KeyAction::KillClient => { |
482 |
KeyAction::KillClient => { |
| 471 |
- if let Some(focused) = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window) { |
483 |
+ if let Some(focused) = self |
|
|
484 |
+ .monitors |
|
|
485 |
+ .get(self.selected_monitor) |
|
|
486 |
+ .and_then(|m| m.focused_window) |
|
|
487 |
+ { |
| 472 |
match self.connection.kill_client(focused) { |
488 |
match self.connection.kill_client(focused) { |
| 473 |
Ok(_) => { |
489 |
Ok(_) => { |
| 474 |
self.connection.flush()?; |
490 |
self.connection.flush()?; |
| @@ -482,6 +498,28 @@ impl WindowManager { |
| 482 |
KeyAction::ToggleFullScreen => { |
498 |
KeyAction::ToggleFullScreen => { |
| 483 |
self.toggle_fullscreen()?; |
499 |
self.toggle_fullscreen()?; |
| 484 |
} |
500 |
} |
|
|
501 |
+ KeyAction::ChangeLayout => { |
|
|
502 |
+ if let Arg::Str(layout_name) = arg { |
|
|
503 |
+ match layout_from_str(layout_name) { |
|
|
504 |
+ Ok(layout) => { |
|
|
505 |
+ self.layout = layout; |
|
|
506 |
+ self.apply_layout()?; |
|
|
507 |
+ } |
|
|
508 |
+ Err(e) => eprintln!("Failed to change layout: {}", e), |
|
|
509 |
+ } |
|
|
510 |
+ } |
|
|
511 |
+ } |
|
|
512 |
+ KeyAction::CycleLayout => { |
|
|
513 |
+ let current_name = self.layout.name(); |
|
|
514 |
+ let next_name = next_layout(current_name); |
|
|
515 |
+ match layout_from_str(next_name) { |
|
|
516 |
+ Ok(layout) => { |
|
|
517 |
+ self.layout = layout; |
|
|
518 |
+ self.apply_layout()?; |
|
|
519 |
+ } |
|
|
520 |
+ Err(e) => eprintln!("Failed to cycle layout: {}", e), |
|
|
521 |
+ } |
|
|
522 |
+ } |
| 485 |
KeyAction::ToggleFloating => { |
523 |
KeyAction::ToggleFloating => { |
| 486 |
self.toggle_floating()?; |
524 |
self.toggle_floating()?; |
| 487 |
} |
525 |
} |
| @@ -645,7 +683,11 @@ impl WindowManager { |
| 645 |
fn save_selected_tags(&self) -> WmResult<()> { |
683 |
fn save_selected_tags(&self) -> WmResult<()> { |
| 646 |
let net_current_desktop = self.atoms.net_current_desktop; |
684 |
let net_current_desktop = self.atoms.net_current_desktop; |
| 647 |
|
685 |
|
| 648 |
- let selected_tags = self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0)); |
686 |
+ let selected_tags = self |
|
|
687 |
+ .monitors |
|
|
688 |
+ .get(self.selected_monitor) |
|
|
689 |
+ .map(|m| m.selected_tags) |
|
|
690 |
+ .unwrap_or(tag_mask(0)); |
| 649 |
let desktop = selected_tags.trailing_zeros(); |
691 |
let desktop = selected_tags.trailing_zeros(); |
| 650 |
|
692 |
|
| 651 |
let bytes = (desktop as u32).to_ne_bytes(); |
693 |
let bytes = (desktop as u32).to_ne_bytes(); |
| @@ -668,7 +710,11 @@ impl WindowManager { |
| 668 |
return Ok(()); |
710 |
return Ok(()); |
| 669 |
} |
711 |
} |
| 670 |
|
712 |
|
| 671 |
- if let Some(focused) = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window) { |
713 |
+ if let Some(focused) = self |
|
|
714 |
+ .monitors |
|
|
715 |
+ .get(self.selected_monitor) |
|
|
716 |
+ .and_then(|m| m.focused_window) |
|
|
717 |
+ { |
| 672 |
let mask = tag_mask(tag_index); |
718 |
let mask = tag_mask(tag_index); |
| 673 |
self.window_tags.insert(focused, mask); |
719 |
self.window_tags.insert(focused, mask); |
| 674 |
|
720 |
|
| @@ -689,7 +735,10 @@ impl WindowManager { |
| 689 |
return Ok(()); |
735 |
return Ok(()); |
| 690 |
} |
736 |
} |
| 691 |
|
737 |
|
| 692 |
- let current = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window); |
738 |
+ let current = self |
|
|
739 |
+ .monitors |
|
|
740 |
+ .get(self.selected_monitor) |
|
|
741 |
+ .and_then(|m| m.focused_window); |
| 693 |
|
742 |
|
| 694 |
let next_window = if let Some(current) = current { |
743 |
let next_window = if let Some(current) = current { |
| 695 |
if let Some(current_index) = visible.iter().position(|&w| w == current) { |
744 |
if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| @@ -726,7 +775,11 @@ impl WindowManager { |
| 726 |
Ok(()) |
775 |
Ok(()) |
| 727 |
} |
776 |
} |
| 728 |
|
777 |
|
| 729 |
- fn update_focus_visuals(&self, old_focused: Option<Window>, new_focused: Window) -> WmResult<()> { |
778 |
+ fn update_focus_visuals( |
|
|
779 |
+ &self, |
|
|
780 |
+ old_focused: Option<Window>, |
|
|
781 |
+ new_focused: Window, |
|
|
782 |
+ ) -> WmResult<()> { |
| 730 |
if let Some(old_win) = old_focused { |
783 |
if let Some(old_win) = old_focused { |
| 731 |
if old_win != new_focused { |
784 |
if old_win != new_focused { |
| 732 |
self.connection.configure_window( |
785 |
self.connection.configure_window( |
| @@ -894,11 +947,16 @@ impl WindowManager { |
| 894 |
&ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), |
947 |
&ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), |
| 895 |
)?; |
948 |
)?; |
| 896 |
|
949 |
|
| 897 |
- let selected_tags = self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0)); |
950 |
+ let selected_tags = self |
|
|
951 |
+ .monitors |
|
|
952 |
+ .get(self.selected_monitor) |
|
|
953 |
+ .map(|m| m.selected_tags) |
|
|
954 |
+ .unwrap_or(tag_mask(0)); |
| 898 |
|
955 |
|
| 899 |
self.windows.push(event.window); |
956 |
self.windows.push(event.window); |
| 900 |
self.window_tags.insert(event.window, selected_tags); |
957 |
self.window_tags.insert(event.window, selected_tags); |
| 901 |
- self.window_monitor.insert(event.window, self.selected_monitor); |
958 |
+ self.window_monitor |
|
|
959 |
+ .insert(event.window, self.selected_monitor); |
| 902 |
self.set_wm_state(event.window, 1)?; |
960 |
self.set_wm_state(event.window, 1)?; |
| 903 |
let _ = self.save_client_tag(event.window, selected_tags); |
961 |
let _ = self.save_client_tag(event.window, selected_tags); |
| 904 |
|
962 |
|
| @@ -929,7 +987,9 @@ impl WindowManager { |
| 929 |
return Ok(None); |
987 |
return Ok(None); |
| 930 |
} |
988 |
} |
| 931 |
|
989 |
|
| 932 |
- if let Some(monitor_index) = self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) { |
990 |
+ if let Some(monitor_index) = |
|
|
991 |
+ self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) |
|
|
992 |
+ { |
| 933 |
if monitor_index != self.selected_monitor { |
993 |
if monitor_index != self.selected_monitor { |
| 934 |
self.selected_monitor = monitor_index; |
994 |
self.selected_monitor = monitor_index; |
| 935 |
self.update_bar()?; |
995 |
self.update_bar()?; |
| @@ -950,7 +1010,9 @@ impl WindowManager { |
| 950 |
} |
1010 |
} |
| 951 |
} |
1011 |
} |
| 952 |
Event::ButtonPress(event) => { |
1012 |
Event::ButtonPress(event) => { |
| 953 |
- let is_bar_click = self.bars.iter() |
1013 |
+ let is_bar_click = self |
|
|
1014 |
+ .bars |
|
|
1015 |
+ .iter() |
| 954 |
.enumerate() |
1016 |
.enumerate() |
| 955 |
.find(|(_, bar)| bar.window() == event.event); |
1017 |
.find(|(_, bar)| bar.window() == event.event); |
| 956 |
|
1018 |
|
| @@ -1029,7 +1091,11 @@ impl WindowManager { |
| 1029 |
.copied() |
1091 |
.copied() |
| 1030 |
.collect(); |
1092 |
.collect(); |
| 1031 |
|
1093 |
|
| 1032 |
- let bar_height = self.bars.get(monitor_index).map(|b| b.height() as u32).unwrap_or(0); |
1094 |
+ let bar_height = self |
|
|
1095 |
+ .bars |
|
|
1096 |
+ .get(monitor_index) |
|
|
1097 |
+ .map(|b| b.height() as u32) |
|
|
1098 |
+ .unwrap_or(0); |
| 1033 |
let usable_height = monitor.height.saturating_sub(bar_height); |
1099 |
let usable_height = monitor.height.saturating_sub(bar_height); |
| 1034 |
|
1100 |
|
| 1035 |
let geometries = self |
1101 |
let geometries = self |
| @@ -1058,6 +1124,12 @@ impl WindowManager { |
| 1058 |
Ok(()) |
1124 |
Ok(()) |
| 1059 |
} |
1125 |
} |
| 1060 |
|
1126 |
|
|
|
1127 |
+ pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> { |
|
|
1128 |
+ self.layout = Box::new(new_layout); |
|
|
1129 |
+ self.apply_layout()?; |
|
|
1130 |
+ Ok(()) |
|
|
1131 |
+ } |
|
|
1132 |
+ |
| 1061 |
fn remove_window(&mut self, window: Window) -> WmResult<()> { |
1133 |
fn remove_window(&mut self, window: Window) -> WmResult<()> { |
| 1062 |
let initial_count = self.windows.len(); |
1134 |
let initial_count = self.windows.len(); |
| 1063 |
self.windows.retain(|&w| w != window); |
1135 |
self.windows.retain(|&w| w != window); |
| @@ -1073,7 +1145,10 @@ impl WindowManager { |
| 1073 |
} |
1145 |
} |
| 1074 |
|
1146 |
|
| 1075 |
if self.windows.len() < initial_count { |
1147 |
if self.windows.len() < initial_count { |
| 1076 |
- let focused = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window); |
1148 |
+ let focused = self |
|
|
1149 |
+ .monitors |
|
|
1150 |
+ .get(self.selected_monitor) |
|
|
1151 |
+ .and_then(|m| m.focused_window); |
| 1077 |
if focused == Some(window) { |
1152 |
if focused == Some(window) { |
| 1078 |
let visible = self.visible_windows_on_monitor(self.selected_monitor); |
1153 |
let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 1079 |
if let Some(&new_win) = visible.last() { |
1154 |
if let Some(&new_win) = visible.last() { |