tony / oxwm

Branch: 5db5675
Switch branches/tags
master
tonybtw added scrolling layout with animations. 5db5675 2025-12-20
156 lines · 4.6 KB Raw Blame History
1 use crate::client::TagMask;
2 use crate::errors::WmError;
3 use x11rb::protocol::xinerama::ConnectionExt as _;
4 use x11rb::protocol::xproto::{Screen, Window};
5 use x11rb::rust_connection::RustConnection;
6
7 type WmResult<T> = Result<T, WmError>;
8
9 #[derive(Debug, Clone)]
10 pub struct Monitor {
11 pub layout_symbol: String,
12 pub master_factor: f32,
13 pub num_master: i32,
14 pub monitor_number: usize,
15 pub bar_y_position: i32,
16 pub screen_x: i32,
17 pub screen_y: i32,
18 pub screen_width: i32,
19 pub screen_height: i32,
20 pub window_area_x: i32,
21 pub window_area_y: i32,
22 pub window_area_width: i32,
23 pub window_area_height: i32,
24 pub gap_inner_horizontal: i32,
25 pub gap_inner_vertical: i32,
26 pub gap_outer_horizontal: i32,
27 pub gap_outer_vertical: i32,
28 pub selected_tags_index: usize,
29 pub selected_layout_index: usize,
30 pub tagset: [u32; 2],
31 pub show_bar: bool,
32 pub top_bar: bool,
33 pub clients_head: Option<Window>,
34 pub selected_client: Option<Window>,
35 pub stack_head: Option<Window>,
36 pub bar_window: Option<Window>,
37 pub layout_indices: [usize; 2],
38 pub scroll_offset: i32,
39 }
40
41 impl Monitor {
42 pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
43 Self {
44 layout_symbol: String::from("[]"),
45 master_factor: 0.55,
46 num_master: 1,
47 monitor_number: 0,
48 bar_y_position: 0,
49 screen_x: x,
50 screen_y: y,
51 screen_width: width as i32,
52 screen_height: height as i32,
53 window_area_x: x,
54 window_area_y: y,
55 window_area_width: width as i32,
56 window_area_height: height as i32,
57 gap_inner_horizontal: 3,
58 gap_inner_vertical: 3,
59 gap_outer_horizontal: 3,
60 gap_outer_vertical: 3,
61 selected_tags_index: 0,
62 selected_layout_index: 0,
63 tagset: [1, 1],
64 show_bar: true,
65 top_bar: true,
66 clients_head: None,
67 selected_client: None,
68 stack_head: None,
69 bar_window: None,
70 layout_indices: [0, 1],
71 scroll_offset: 0,
72 }
73 }
74
75 pub fn contains_point(&self, x: i32, y: i32) -> bool {
76 x >= self.screen_x
77 && x < self.screen_x + self.screen_width
78 && y >= self.screen_y
79 && y < self.screen_y + self.screen_height
80 }
81
82 pub fn get_selected_tag(&self) -> TagMask {
83 self.tagset[self.selected_tags_index]
84 }
85 }
86
87 pub fn detect_monitors(
88 connection: &RustConnection,
89 screen: &Screen,
90 _root: Window,
91 ) -> WmResult<Vec<Monitor>> {
92 let fallback_monitors = || {
93 vec![Monitor::new(
94 0,
95 0,
96 screen.width_in_pixels as u32,
97 screen.height_in_pixels as u32,
98 )]
99 };
100
101 let mut monitors = Vec::<Monitor>::new();
102
103 let xinerama_active = connection
104 .xinerama_is_active()
105 .ok()
106 .and_then(|cookie| cookie.reply().ok())
107 .is_some_and(|reply| reply.state != 0);
108
109 if xinerama_active {
110 let Ok(xinerama_cookie) = connection.xinerama_query_screens() else {
111 return Ok(fallback_monitors());
112 };
113 let Ok(xinerama_reply) = xinerama_cookie.reply() else {
114 return Ok(fallback_monitors());
115 };
116
117 for screen_info in &xinerama_reply.screen_info {
118 let has_valid_dimensions = screen_info.width > 0 && screen_info.height > 0;
119 if !has_valid_dimensions {
120 continue;
121 }
122
123 let x_position = screen_info.x_org as i32;
124 let y_position = screen_info.y_org as i32;
125 let width_in_pixels = screen_info.width as u32;
126 let height_in_pixels = screen_info.height as u32;
127
128 let is_duplicate_monitor = monitors.iter().any(|monitor| {
129 monitor.screen_x == x_position
130 && monitor.screen_y == y_position
131 && monitor.screen_width == width_in_pixels as i32
132 && monitor.screen_height == height_in_pixels as i32
133 });
134
135 if !is_duplicate_monitor {
136 monitors.push(Monitor::new(
137 x_position,
138 y_position,
139 width_in_pixels,
140 height_in_pixels,
141 ));
142 }
143 }
144 }
145
146 if monitors.is_empty() {
147 monitors = fallback_monitors();
148 }
149
150 monitors.sort_by(|a, b| match a.screen_y.cmp(&b.screen_y) {
151 std::cmp::Ordering::Equal => a.screen_x.cmp(&b.screen_x),
152 other => other,
153 });
154
155 Ok(monitors)
156 }