tony / oxwm

Added support for multiple monitors using randr library and dwm.c logic. Applied bar updates for multi monitor support. Browse files
tonybtw committed 2025-10-17 parent 6225625 commit 84c9fb03a6aa38e6760ed4a2fcb59c1c6bcaa6a3
Showing 7 changed files with 361 additions and 92 deletions. UnifiedSplit
+1 −1 Cargo.toml
@@ -13,7 +13,7 @@ path = "src/bin/main.rs"
13 13
14 [dependencies] 14 [dependencies]
15 x11 = { version = "2.21", features = ["xlib", "xft"] } 15 x11 = { version = "2.21", features = ["xlib", "xft"] }
16 -x11rb = { version = "0.13", features = ["cursor"] } 16 +x11rb = { version = "0.13", features = ["cursor", "randr"] }
17 anyhow = "1" 17 anyhow = "1"
18 chrono = "0.4" 18 chrono = "0.4"
19 dirs = "5.0" 19 dirs = "5.0"
+5 −4 src/bar/bar.rs
@@ -38,12 +38,13 @@ impl Bar {
38 screen: &Screen, 38 screen: &Screen,
39 screen_num: usize, 39 screen_num: usize,
40 config: &Config, 40 config: &Config,
41 + x: i16,
42 + y: i16,
43 + width: u16,
41 ) -> Result<Self, X11Error> { 44 ) -> Result<Self, X11Error> {
42 let window = connection.generate_id()?; 45 let window = connection.generate_id()?;
43 let graphics_context = connection.generate_id()?; 46 let graphics_context = connection.generate_id()?;
44 47
45 - let width = screen.width_in_pixels;
46 -
47 let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; 48 let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
48 if display.is_null() { 49 if display.is_null() {
49 return Err(X11Error::DisplayOpenFailed.into()); 50 return Err(X11Error::DisplayOpenFailed.into());
@@ -57,8 +58,8 @@ impl Bar {
57 COPY_DEPTH_FROM_PARENT, 58 COPY_DEPTH_FROM_PARENT,
58 window, 59 window,
59 screen.root, 60 screen.root,
60 - 0, 61 + x,
61 - 0, 62 + y,
62 width, 63 width,
63 height, 64 height,
64 0, 65 0,
+1 −0 src/keyboard/handlers.rs
@@ -20,6 +20,7 @@ pub enum KeyAction {
20 ToggleFullScreen, 20 ToggleFullScreen,
21 ToggleFloating, 21 ToggleFloating,
22 MoveToTag, 22 MoveToTag,
23 + FocusMonitor,
23 None, 24 None,
24 } 25 }
25 26
+1 −0 src/lib.rs
@@ -3,6 +3,7 @@ pub mod config;
3 pub mod errors; 3 pub mod errors;
4 pub mod keyboard; 4 pub mod keyboard;
5 pub mod layout; 5 pub mod layout;
6 +pub mod monitor;
6 pub mod window_manager; 7 pub mod window_manager;
7 8
8 pub mod prelude { 9 pub mod prelude {
+144 −0 src/monitor.rs new file mode 100644
@@ -0,0 +1,144 @@
1 +use crate::errors::WmError;
2 +use x11rb::protocol::randr::{self, ConnectionExt as _};
3 +use x11rb::protocol::xproto::{Screen, Window};
4 +use x11rb::rust_connection::RustConnection;
5 +
6 +type WmResult<T> = Result<T, WmError>;
7 +
8 +#[derive(Debug, Clone)]
9 +pub struct Monitor {
10 + pub x: i32,
11 + pub y: i32,
12 + pub width: u32,
13 + pub height: u32,
14 + pub selected_tags: u32,
15 + pub focused_window: Option<Window>,
16 +}
17 +
18 +impl Monitor {
19 + pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
20 + Self {
21 + x,
22 + y,
23 + width,
24 + height,
25 + selected_tags: 1,
26 + focused_window: None,
27 + }
28 + }
29 +
30 + pub fn contains_point(&self, x: i32, y: i32) -> bool {
31 + x >= self.x
32 + && x < self.x + self.width as i32
33 + && y >= self.y
34 + && y < self.y + self.height as i32
35 + }
36 +}
37 +
38 +pub fn detect_monitors(
39 + connection: &RustConnection,
40 + screen: &Screen,
41 + root: Window,
42 +) -> WmResult<Vec<Monitor>> {
43 + let randr_version = match connection.randr_query_version(1, 2) {
44 + Ok(cookie) => match cookie.reply() {
45 + Ok(reply) => reply.major_version >= 1 && reply.minor_version >= 2,
46 + Err(_) => false,
47 + },
48 + Err(_) => false,
49 + };
50 +
51 + if !randr_version {
52 + eprintln!("RandR 1.2+ not available, using single monitor");
53 + return Ok(vec![Monitor::new(
54 + 0,
55 + 0,
56 + screen.width_in_pixels as u32,
57 + screen.height_in_pixels as u32,
58 + )]);
59 + }
60 +
61 + let resources = match connection.randr_get_screen_resources(root) {
62 + Ok(cookie) => match cookie.reply() {
63 + Ok(res) => res,
64 + Err(_) => {
65 + eprintln!("Failed to get screen resources, using single monitor");
66 + return Ok(vec![Monitor::new(
67 + 0,
68 + 0,
69 + screen.width_in_pixels as u32,
70 + screen.height_in_pixels as u32,
71 + )]);
72 + }
73 + },
74 + Err(_) => {
75 + eprintln!("Failed to query screen resources, using single monitor");
76 + return Ok(vec![Monitor::new(
77 + 0,
78 + 0,
79 + screen.width_in_pixels as u32,
80 + screen.height_in_pixels as u32,
81 + )]);
82 + }
83 + };
84 +
85 + let mut monitors = Vec::new();
86 +
87 + for &output in &resources.outputs {
88 + let output_info = match connection.randr_get_output_info(output, 0) {
89 + Ok(cookie) => match cookie.reply() {
90 + Ok(info) => info,
91 + Err(_) => continue,
92 + },
93 + Err(_) => continue,
94 + };
95 +
96 + if output_info.connection != randr::Connection::CONNECTED {
97 + continue;
98 + }
99 +
100 + if output_info.crtc == 0 {
101 + continue;
102 + }
103 +
104 + let crtc_info = match connection.randr_get_crtc_info(output_info.crtc, 0) {
105 + Ok(cookie) => match cookie.reply() {
106 + Ok(info) => info,
107 + Err(_) => continue,
108 + },
109 + Err(_) => continue,
110 + };
111 +
112 + if crtc_info.width == 0 || crtc_info.height == 0 {
113 + continue;
114 + }
115 +
116 + monitors.push(Monitor::new(
117 + crtc_info.x as i32,
118 + crtc_info.y as i32,
119 + crtc_info.width as u32,
120 + crtc_info.height as u32,
121 + ));
122 + }
123 +
124 + if monitors.is_empty() {
125 + eprintln!("No monitors detected via RandR, using full screen");
126 + monitors.push(Monitor::new(
127 + 0,
128 + 0,
129 + screen.width_in_pixels as u32,
130 + screen.height_in_pixels as u32,
131 + ));
132 + }
133 +
134 + monitors.sort_by(|a, b| {
135 + let y_cmp = a.y.cmp(&b.y);
136 + if y_cmp == std::cmp::Ordering::Equal {
137 + a.x.cmp(&b.x)
138 + } else {
139 + y_cmp
140 + }
141 + });
142 +
143 + Ok(monitors)
144 +}
+207 −87 src/window_manager.rs Large diff collapsed, click to expand
@@ -5,6 +5,7 @@ use crate::keyboard::{self, Arg, KeyAction, handlers};
5 use crate::layout::GapConfig; 5 use crate::layout::GapConfig;
6 use crate::layout::Layout; 6 use crate::layout::Layout;
7 use crate::layout::tiling::TilingLayout; 7 use crate::layout::tiling::TilingLayout;
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
@@ -25,14 +26,15 @@ pub struct WindowManager {
25 root: Window, 26 root: Window,
26 screen: Screen, 27 screen: Screen,
27 windows: Vec<Window>, 28 windows: Vec<Window>,
28 - focused_window: Option<Window>,
29 layout: Box<dyn Layout>, 29 layout: Box<dyn Layout>,
30 window_tags: std::collections::HashMap<Window, TagMask>, 30 window_tags: std::collections::HashMap<Window, TagMask>,
31 - selected_tags: TagMask, 31 + window_monitor: std::collections::HashMap<Window, usize>,
32 gaps_enabled: bool, 32 gaps_enabled: bool,
33 fullscreen_window: Option<Window>, 33 fullscreen_window: Option<Window>,
34 floating_windows: HashSet<Window>, 34 floating_windows: HashSet<Window>,
35 - bar: Bar, 35 + bars: Vec<Bar>,
36 + monitors: Vec<Monitor>,
37 + selected_monitor: usize,
36 } 38 }
37 39
38 type WmResult<T> = Result<T, WmError>; 40 type WmResult<T> = Result<T, WmError>;
@@ -90,9 +92,27 @@ impl WindowManager {
90 u16::from(config.modkey).into(), 92 u16::from(config.modkey).into(),
91 )?; 93 )?;
92 94
93 - let bar = Bar::new(&connection, &screen, screen_number, &config)?; 95 + let mut monitors = detect_monitors(&connection, &screen, root)?;
94 96
95 let selected_tags = Self::get_saved_selected_tags(&connection, root, config.tags.len())?; 97 let selected_tags = Self::get_saved_selected_tags(&connection, root, config.tags.len())?;
98 + if !monitors.is_empty() {
99 + monitors[0].selected_tags = selected_tags;
100 + }
101 +
102 + let mut bars = Vec::new();
103 + for monitor in &monitors {
104 + let bar = Bar::new(
105 + &connection,
106 + &screen,
107 + screen_number,
108 + &config,
109 + monitor.x as i16,
110 + monitor.y as i16,
111 + monitor.width as u16,
112 + )?;
113 + bars.push(bar);
114 + }
115 +
96 let gaps_enabled = config.gaps_enabled; 116 let gaps_enabled = config.gaps_enabled;
97 117
98 let mut window_manager = Self { 118 let mut window_manager = Self {
@@ -102,14 +122,15 @@ impl WindowManager {
102 root, 122 root,
103 screen, 123 screen,
104 windows: Vec::new(), 124 windows: Vec::new(),
105 - focused_window: None,
106 layout: Box::new(TilingLayout), 125 layout: Box::new(TilingLayout),
107 window_tags: std::collections::HashMap::new(), 126 window_tags: std::collections::HashMap::new(),
108 - selected_tags, 127 + window_monitor: std::collections::HashMap::new(),
109 gaps_enabled, 128 gaps_enabled,
110 fullscreen_window: None, 129 fullscreen_window: None,
111 floating_windows: HashSet::new(), 130 floating_windows: HashSet::new(),
112 - bar, 131 + bars,
132 + monitors,
133 + selected_monitor: 0,
113 }; 134 };
114 135
115 window_manager.scan_existing_windows()?; 136 window_manager.scan_existing_windows()?;
@@ -167,7 +188,7 @@ impl WindowManager {
167 .atom; 188 .atom;
168 189
169 for &window in &tree.children { 190 for &window in &tree.children {
170 - if window == self.bar.window() { 191 + if self.bars.iter().any(|bar| bar.window() == window) {
171 continue; 192 continue;
172 } 193 }
173 194
@@ -183,6 +204,7 @@ impl WindowManager {
183 let tag = self.get_saved_tag(window, net_client_info)?; 204 let tag = self.get_saved_tag(window, net_client_info)?;
184 self.windows.push(window); 205 self.windows.push(window);
185 self.window_tags.insert(window, tag); 206 self.window_tags.insert(window, tag);
207 + self.window_monitor.insert(window, self.selected_monitor);
186 continue; 208 continue;
187 } 209 }
188 210
@@ -207,12 +229,13 @@ impl WindowManager {
207 let tag = self.get_saved_tag(window, net_client_info)?; 229 let tag = self.get_saved_tag(window, net_client_info)?;
208 self.windows.push(window); 230 self.windows.push(window);
209 self.window_tags.insert(window, tag); 231 self.window_tags.insert(window, tag);
232 + self.window_monitor.insert(window, self.selected_monitor);
210 } 233 }
211 } 234 }
212 } 235 }
213 236
214 if let Some(&first) = self.windows.first() { 237 if let Some(&first) = self.windows.first() {
215 - self.set_focus(Some(first))?; 238 + self.set_focus(first)?;
216 } 239 }
217 240
218 self.apply_layout()?; 241 self.apply_layout()?;
@@ -243,7 +266,7 @@ impl WindowManager {
243 } 266 }
244 } 267 }
245 268
246 - Ok(self.selected_tags) 269 + Ok(self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0)))
247 } 270 }
248 271
249 fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { 272 fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> {
@@ -300,7 +323,9 @@ impl WindowManager {
300 self.update_bar()?; 323 self.update_bar()?;
301 324
302 loop { 325 loop {
303 - self.bar.update_blocks(); 326 + for bar in &mut self.bars {
327 + bar.update_blocks();
328 + }
304 329
305 if let Ok(Some(event)) = self.connection.poll_for_event() { 330 if let Ok(Some(event)) = self.connection.poll_for_event() {
306 if let Some(should_restart) = self.handle_event(event)? { 331 if let Some(should_restart) = self.handle_event(event)? {
@@ -308,7 +333,7 @@ impl WindowManager {
308 } 333 }
309 } 334 }
310 335
311 - if self.bar.needs_redraw() { 336 + if self.bars.iter().any(|bar| bar.needs_redraw()) {
312 self.update_bar()?; 337 self.update_bar()?;
313 } 338 }
314 339
@@ -317,7 +342,7 @@ impl WindowManager {
317 } 342 }
318 343
319 fn toggle_floating(&mut self) -> WmResult<()> { 344 fn toggle_floating(&mut self) -> WmResult<()> {
320 - if let Some(focused) = self.focused_window { 345 + if let Some(focused) = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window) {
321 if self.floating_windows.contains(&focused) { 346 if self.floating_windows.contains(&focused) {
322 self.floating_windows.remove(&focused); 347 self.floating_windows.remove(&focused);
323 self.apply_layout()?; 348 self.apply_layout()?;
@@ -335,27 +360,32 @@ impl WindowManager {
335 } 360 }
336 361
337 fn toggle_fullscreen(&mut self) -> WmResult<()> { 362 fn toggle_fullscreen(&mut self) -> WmResult<()> {
338 - if let Some(focused) = self.focused_window { 363 + if let Some(focused) = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window) {
339 if self.fullscreen_window == Some(focused) { 364 if self.fullscreen_window == Some(focused) {
340 self.fullscreen_window = None; 365 self.fullscreen_window = None;
341 366
342 - self.connection.map_window(self.bar.window())?; 367 + for bar in &self.bars {
368 + self.connection.map_window(bar.window())?;
369 + }
343 370
344 self.apply_layout()?; 371 self.apply_layout()?;
345 self.update_focus_visuals()?; 372 self.update_focus_visuals()?;
346 } else { 373 } else {
347 self.fullscreen_window = Some(focused); 374 self.fullscreen_window = Some(focused);
348 375
349 - self.connection.unmap_window(self.bar.window())?; 376 + if let Some(bar) = self.bars.get(self.selected_monitor) {
377 + self.connection.unmap_window(bar.window())?;
378 + }
350 379
351 - let screen_width = self.screen.width_in_pixels as u32; 380 + let monitor = &self.monitors[self.selected_monitor];
352 - let screen_height = self.screen.height_in_pixels as u32; 381 + let screen_width = monitor.width;
382 + let screen_height = monitor.height;
353 383
354 self.connection.configure_window( 384 self.connection.configure_window(
355 focused, 385 focused,
356 &ConfigureWindowAux::new() 386 &ConfigureWindowAux::new()
357 - .x(0) 387 + .x(monitor.x)
358 - .y(0) 388 + .y(monitor.y)
359 .width(screen_width) 389 .width(screen_width)
360 .height(screen_height) 390 .height(screen_height)
361 .border_width(0) 391 .border_width(0)
@@ -369,14 +399,19 @@ impl WindowManager {
369 } 399 }
370 400
371 fn update_bar(&mut self) -> WmResult<()> { 401 fn update_bar(&mut self) -> WmResult<()> {
372 - let mut occupied_tags: TagMask = 0; 402 + for (mon_idx, monitor) in self.monitors.iter().enumerate() {
373 - for &tags in self.window_tags.values() { 403 + if let Some(bar) = self.bars.get_mut(mon_idx) {
374 - occupied_tags |= tags; 404 + let mut occupied_tags: TagMask = 0;
375 - } 405 + for (&window, &tags) in &self.window_tags {
406 + if self.window_monitor.get(&window).copied().unwrap_or(0) == mon_idx {
407 + occupied_tags |= tags;
408 + }
409 + }
376 410
377 - self.bar.invalidate(); 411 + bar.invalidate();
378 - self.bar 412 + bar.draw(&self.connection, monitor.selected_tags, occupied_tags)?;
379 - .draw(&self.connection, self.selected_tags, occupied_tags)?; 413 + }
414 + }
380 Ok(()) 415 Ok(())
381 } 416 }
382 417
@@ -384,7 +419,7 @@ impl WindowManager {
384 match action { 419 match action {
385 KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, 420 KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?,
386 KeyAction::KillClient => { 421 KeyAction::KillClient => {
387 - if let Some(focused) = self.focused_window { 422 + if let Some(focused) = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window) {
388 match self.connection.kill_client(focused) { 423 match self.connection.kill_client(focused) {
389 Ok(_) => { 424 Ok(_) => {
390 self.connection.flush()?; 425 self.connection.flush()?;
@@ -433,14 +468,54 @@ impl WindowManager {
433 self.gaps_enabled = !self.gaps_enabled; 468 self.gaps_enabled = !self.gaps_enabled;
434 self.apply_layout()?; 469 self.apply_layout()?;
435 } 470 }
471 + KeyAction::FocusMonitor => {
472 + if let Arg::Int(direction) = arg {
473 + self.focus_monitor(*direction)?;
474 + }
475 + }
436 KeyAction::None => {} 476 KeyAction::None => {}
437 } 477 }
438 Ok(()) 478 Ok(())
439 } 479 }
440 480
481 + fn focus_monitor(&mut self, direction: i32) -> WmResult<()> {
482 + if self.monitors.is_empty() {
483 + return Ok(());
484 + }
485 +
486 + let new_monitor = if direction > 0 {
487 + (self.selected_monitor + 1) % self.monitors.len()
488 + } else {
489 + (self.selected_monitor + self.monitors.len() - 1) % self.monitors.len()
490 + };
491 +
492 + if new_monitor == self.selected_monitor {
493 + return Ok(());
494 + }
495 +
496 + self.selected_monitor = new_monitor;
497 +
498 + self.update_window_visibility()?;
499 + self.apply_layout()?;
500 + self.update_bar()?;
501 +
502 + let visible = self.visible_windows();
503 + if let Some(&win) = visible.first() {
504 + self.set_focus(win)?;
505 + }
506 +
507 + Ok(())
508 + }
509 +
441 fn is_window_visible(&self, window: Window) -> bool { 510 fn is_window_visible(&self, window: Window) -> bool {
511 + let window_mon = self.window_monitor.get(&window).copied().unwrap_or(0);
512 + if window_mon != self.selected_monitor {
513 + return false;
514 + }
515 +
442 if let Some(&tags) = self.window_tags.get(&window) { 516 if let Some(&tags) = self.window_tags.get(&window) {
443 - (tags & self.selected_tags) != 0 517 + let selected_tags = self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(0);
518 + (tags & selected_tags) != 0
444 } else { 519 } else {
445 false 520 false
446 } 521 }
@@ -473,10 +548,14 @@ impl WindowManager {
473 548
474 if self.fullscreen_window.is_some() { 549 if self.fullscreen_window.is_some() {
475 self.fullscreen_window = None; 550 self.fullscreen_window = None;
476 - self.connection.map_window(self.bar.window())?; 551 + for bar in &self.bars {
552 + self.connection.map_window(bar.window())?;
553 + }
477 } 554 }
478 555
479 - self.selected_tags = tag_mask(tag_index); 556 + if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
557 + monitor.selected_tags = tag_mask(tag_index);
558 + }
480 559
481 self.save_selected_tags()?; 560 self.save_selected_tags()?;
482 561
@@ -485,7 +564,9 @@ impl WindowManager {
485 self.update_bar()?; 564 self.update_bar()?;
486 565
487 let visible = self.visible_windows(); 566 let visible = self.visible_windows();
488 - self.set_focus(visible.first().copied())?; 567 + if let Some(&win) = visible.first() {
568 + self.set_focus(win)?;
569 + }
489 570
490 Ok(()) 571 Ok(())
491 } 572 }
@@ -497,7 +578,8 @@ impl WindowManager {
497 .reply()? 578 .reply()?
498 .atom; 579 .atom;
499 580
500 - let desktop = self.selected_tags.trailing_zeros(); 581 + let selected_tags = self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0));
582 + let desktop = selected_tags.trailing_zeros();
501 583
502 let bytes = (desktop as u32).to_ne_bytes(); 584 let bytes = (desktop as u32).to_ne_bytes();
503 self.connection.change_property( 585 self.connection.change_property(
@@ -519,7 +601,7 @@ impl WindowManager {
519 return Ok(()); 601 return Ok(());
520 } 602 }
521 603
522 - if let Some(focused) = self.focused_window { 604 + if let Some(focused) = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window) {
523 let mask = tag_mask(tag_index); 605 let mask = tag_mask(tag_index);
524 self.window_tags.insert(focused, mask); 606 self.window_tags.insert(focused, mask);
525 607
@@ -540,7 +622,9 @@ impl WindowManager {
540 return Ok(()); 622 return Ok(());
541 } 623 }
542 624
543 - let next_window = if let Some(current) = self.focused_window { 625 + let current = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window);
626 +
627 + let next_window = if let Some(current) = current {
544 if let Some(current_index) = visible.iter().position(|&w| w == current) { 628 if let Some(current_index) = visible.iter().position(|&w| w == current) {
545 let next_index = if direction > 0 { 629 let next_index = if direction > 0 {
546 (current_index + 1) % visible.len() 630 (current_index + 1) % visible.len()
@@ -555,26 +639,28 @@ impl WindowManager {
555 visible[0] 639 visible[0]
556 }; 640 };
557 641
558 - self.set_focus(Some(next_window))?; 642 + self.set_focus(next_window)?;
559 Ok(()) 643 Ok(())
560 } 644 }
561 645
562 - pub fn set_focus(&mut self, window: Option<Window>) -> WmResult<()> { 646 + pub fn set_focus(&mut self, window: Window) -> WmResult<()> {
563 - self.focused_window = window; 647 + if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
564 - 648 + monitor.focused_window = Some(window);
565 - if let Some(win) = window {
566 - self.connection
567 - .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
568 - self.connection.flush()?;
569 } 649 }
570 650
651 + self.connection
652 + .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?;
653 + self.connection.flush()?;
654 +
571 self.update_focus_visuals()?; 655 self.update_focus_visuals()?;
572 Ok(()) 656 Ok(())
573 } 657 }
574 658
575 fn update_focus_visuals(&self) -> WmResult<()> { 659 fn update_focus_visuals(&self) -> WmResult<()> {
660 + let focused = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window);
661 +
576 for &window in &self.windows { 662 for &window in &self.windows {
577 - let (border_color, border_width) = if self.focused_window == Some(window) { 663 + let (border_color, border_width) = if focused == Some(window) {
578 (self.config.border_focused, self.config.border_width) 664 (self.config.border_focused, self.config.border_width)
579 } else { 665 } else {
580 (self.config.border_unfocused, self.config.border_width) 666 (self.config.border_unfocused, self.config.border_width)
@@ -733,14 +819,17 @@ impl WindowManager {
733 &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), 819 &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW),
734 )?; 820 )?;
735 821
822 + let selected_tags = self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0));
823 +
736 self.windows.push(event.window); 824 self.windows.push(event.window);
737 - self.window_tags.insert(event.window, self.selected_tags); 825 + self.window_tags.insert(event.window, selected_tags);
826 + self.window_monitor.insert(event.window, self.selected_monitor);
738 self.set_wm_state(event.window, 1)?; 827 self.set_wm_state(event.window, 1)?;
739 - let _ = self.save_client_tag(event.window, self.selected_tags); 828 + let _ = self.save_client_tag(event.window, selected_tags);
740 829
741 self.apply_layout()?; 830 self.apply_layout()?;
742 self.update_bar()?; 831 self.update_bar()?;
743 - self.set_focus(Some(event.window))?; 832 + self.set_focus(event.window)?;
744 } 833 }
745 Event::UnmapNotify(event) => { 834 Event::UnmapNotify(event) => {
746 if self.windows.contains(&event.window) && self.is_window_visible(event.window) { 835 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
@@ -757,7 +846,7 @@ impl WindowManager {
757 return Ok(None); 846 return Ok(None);
758 } 847 }
759 if self.windows.contains(&event.event) { 848 if self.windows.contains(&event.event) {
760 - self.set_focus(Some(event.event))?; 849 + self.set_focus(event.event)?;
761 } 850 }
762 } 851 }
763 Event::KeyPress(event) => { 852 Event::KeyPress(event) => {
@@ -769,12 +858,19 @@ impl WindowManager {
769 } 858 }
770 } 859 }
771 Event::ButtonPress(event) => { 860 Event::ButtonPress(event) => {
772 - if event.event == self.bar.window() { 861 + let is_bar_click = self.bars.iter()
773 - if let Some(tag_index) = self.bar.handle_click(event.event_x) { 862 + .enumerate()
863 + .find(|(_, bar)| bar.window() == event.event);
864 +
865 + if let Some((mon_idx, bar)) = is_bar_click {
866 + if let Some(tag_index) = bar.handle_click(event.event_x) {
867 + if mon_idx != self.selected_monitor {
868 + self.selected_monitor = mon_idx;
869 + }
774 self.view_tag(tag_index)?; 870 self.view_tag(tag_index)?;
775 } 871 }
776 } else if event.child != x11rb::NONE { 872 } else if event.child != x11rb::NONE {
777 - self.set_focus(Some(event.child))?; 873 + self.set_focus(event.child)?;
778 874
779 if event.detail == ButtonIndex::M1.into() { 875 if event.detail == ButtonIndex::M1.into() {
780 self.move_mouse(event.child)?; 876 self.move_mouse(event.child)?;
@@ -784,9 +880,12 @@ impl WindowManager {
784 } 880 }
785 } 881 }
786 Event::Expose(event) => { 882 Event::Expose(event) => {
787 - if event.window == self.bar.window() { 883 + for bar in &mut self.bars {
788 - self.bar.invalidate(); 884 + if event.window == bar.window() {
789 - self.update_bar()?; 885 + bar.invalidate();
886 + self.update_bar()?;
887 + break;
888 + }
790 } 889 }
791 } 890 }
792 _ => {} 891 _ => {}
@@ -798,12 +897,8 @@ impl WindowManager {
798 if self.fullscreen_window.is_some() { 897 if self.fullscreen_window.is_some() {
799 return Ok(()); 898 return Ok(());
800 } 899 }
801 - let screen_width = self.screen.width_in_pixels as u32;
802 - let screen_height = self.screen.height_in_pixels as u32;
803 - let border_width = self.config.border_width;
804 900
805 - let bar_height = self.bar.height() as u32; 901 + let border_width = self.config.border_width;
806 - let usable_height = screen_height.saturating_sub(bar_height);
807 902
808 let gaps = if self.gaps_enabled { 903 let gaps = if self.gaps_enabled {
809 GapConfig { 904 GapConfig {
@@ -821,31 +916,52 @@ impl WindowManager {
821 } 916 }
822 }; 917 };
823 918
824 - let visible: Vec<Window> = self 919 + for (mon_idx, monitor) in self.monitors.iter().enumerate() {
825 - .visible_windows() 920 + let visible: Vec<Window> = self
826 - .into_iter() 921 + .windows
827 - .filter(|w| !self.floating_windows.contains(w)) 922 + .iter()
828 - .collect(); 923 + .filter(|&&w| {
924 + let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0);
925 + if window_mon != mon_idx {
926 + return false;
927 + }
928 + if self.floating_windows.contains(&w) {
929 + return false;
930 + }
931 + if let Some(&tags) = self.window_tags.get(&w) {
932 + (tags & monitor.selected_tags) != 0
933 + } else {
934 + false
935 + }
936 + })
937 + .copied()
938 + .collect();
939 +
940 + let bar_height = self.bars.get(mon_idx).map(|b| b.height() as u32).unwrap_or(0);
941 + let usable_height = monitor.height.saturating_sub(bar_height);
829 942
830 - let geometries = self 943 + let geometries = self
831 - .layout 944 + .layout
832 - .arrange(&visible, screen_width, usable_height, &gaps); 945 + .arrange(&visible, monitor.width, usable_height, &gaps);
833 946
834 - for (window, geometry) in visible.iter().zip(geometries.iter()) { 947 + for (window, geometry) in visible.iter().zip(geometries.iter()) {
835 - let adjusted_width = geometry.width.saturating_sub(2 * border_width); 948 + let adjusted_width = geometry.width.saturating_sub(2 * border_width);
836 - let adjusted_height = geometry.height.saturating_sub(2 * border_width); 949 + let adjusted_height = geometry.height.saturating_sub(2 * border_width);
837 950
838 - let adjusted_y = geometry.y_coordinate + bar_height as i32; 951 + let adjusted_x = geometry.x_coordinate + monitor.x;
952 + let adjusted_y = geometry.y_coordinate + monitor.y + bar_height as i32;
839 953
840 - self.connection.configure_window( 954 + self.connection.configure_window(
841 - *window, 955 + *window,
842 - &ConfigureWindowAux::new() 956 + &ConfigureWindowAux::new()
843 - .x(geometry.x_coordinate) 957 + .x(adjusted_x)
844 - .y(adjusted_y) 958 + .y(adjusted_y)
845 - .width(adjusted_width) 959 + .width(adjusted_width)
846 - .height(adjusted_height), 960 + .height(adjusted_height),
847 - )?; 961 + )?;
962 + }
848 } 963 }
964 +
849 self.connection.flush()?; 965 self.connection.flush()?;
850 Ok(()) 966 Ok(())
851 } 967 }
@@ -854,21 +970,25 @@ impl WindowManager {
854 let initial_count = self.windows.len(); 970 let initial_count = self.windows.len();
855 self.windows.retain(|&w| w != window); 971 self.windows.retain(|&w| w != window);
856 self.window_tags.remove(&window); 972 self.window_tags.remove(&window);
973 + self.window_monitor.remove(&window);
857 self.floating_windows.remove(&window); 974 self.floating_windows.remove(&window);
858 975
859 if self.fullscreen_window == Some(window) { 976 if self.fullscreen_window == Some(window) {
860 self.fullscreen_window = None; 977 self.fullscreen_window = None;
861 - self.connection.map_window(self.bar.window())?; 978 + for bar in &self.bars {
979 + self.connection.map_window(bar.window())?;
980 + }
862 } 981 }
863 982
864 if self.windows.len() < initial_count { 983 if self.windows.len() < initial_count {
865 - if self.focused_window == Some(window) { 984 + let focused = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window);
866 - let new_focus = if self.windows.is_empty() { 985 + if focused == Some(window) {
867 - None 986 + let visible = self.visible_windows();
868 - } else { 987 + if let Some(&new_win) = visible.last() {
869 - Some(self.windows[self.windows.len() - 1]) 988 + self.set_focus(new_win)?;
870 - }; 989 + } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
871 - self.set_focus(new_focus)?; 990 + monitor.focused_window = None;
991 + }
872 } 992 }
873 993
874 self.apply_layout()?; 994 self.apply_layout()?;
+2 −0 templates/config.ron
@@ -31,6 +31,8 @@
31 (modifiers: [Mod4, Shift], key: R, action: Restart), 31 (modifiers: [Mod4, Shift], key: R, action: Restart),
32 (modifiers: [Mod4], key: J, action: FocusStack, arg: -1), 32 (modifiers: [Mod4], key: J, action: FocusStack, arg: -1),
33 (modifiers: [Mod4], key: K, action: FocusStack, arg: 1), 33 (modifiers: [Mod4], key: K, action: FocusStack, arg: 1),
34 + (modifiers: [Mod4], key: Comma, action: FocusMonitor, arg: -1),
35 + (modifiers: [Mod4], key: Period, action: FocusMonitor, arg: 1),
34 (modifiers: [Mod4], key: Key1, action: ViewTag, arg: 0), 36 (modifiers: [Mod4], key: Key1, action: ViewTag, arg: 0),
35 (modifiers: [Mod4], key: Key2, action: ViewTag, arg: 1), 37 (modifiers: [Mod4], key: Key2, action: ViewTag, arg: 1),
36 (modifiers: [Mod4], key: Key3, action: ViewTag, arg: 2), 38 (modifiers: [Mod4], key: Key3, action: ViewTag, arg: 2),