C has better C interop than rust.
Browse files
+61 −40 src/window_manager.rs
| @@ -10,7 +10,6 @@ use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout} | |||
| 10 | use crate::monitor::{Monitor, detect_monitors}; | 10 | use crate::monitor::{Monitor, detect_monitors}; |
| 11 | use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay}; | 11 | use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay}; |
| 12 | use std::collections::{HashMap, HashSet}; | 12 | use std::collections::{HashMap, HashSet}; |
| 13 | -use x11rb::cursor::Handle as CursorHandle; | ||
| 14 | 13 | ||
| 15 | use x11rb::connection::Connection; | 14 | use x11rb::connection::Connection; |
| 16 | use x11rb::protocol::Event; | 15 | use x11rb::protocol::Event; |
| @@ -187,27 +186,17 @@ impl WindowManager { | |||
| 187 | let root = connection.setup().roots[screen_number].root; | 186 | let root = connection.setup().roots[screen_number].root; |
| 188 | let screen = connection.setup().roots[screen_number].clone(); | 187 | let screen = connection.setup().roots[screen_number].clone(); |
| 189 | 188 | ||
| 190 | - let normal_cursor = CursorHandle::new( | ||
| 191 | - &connection, | ||
| 192 | - screen_number, | ||
| 193 | - &x11rb::resource_manager::new_from_default(&connection)?, | ||
| 194 | - )? | ||
| 195 | - .reply()? | ||
| 196 | - .load_cursor(&connection, "left_ptr")?; | ||
| 197 | - | ||
| 198 | connection | 189 | connection |
| 199 | .change_window_attributes( | 190 | .change_window_attributes( |
| 200 | root, | 191 | root, |
| 201 | - &ChangeWindowAttributesAux::new() | 192 | + &ChangeWindowAttributesAux::new().event_mask( |
| 202 | - .cursor(normal_cursor) | 193 | + EventMask::SUBSTRUCTURE_REDIRECT |
| 203 | - .event_mask( | 194 | + | EventMask::SUBSTRUCTURE_NOTIFY |
| 204 | - EventMask::SUBSTRUCTURE_REDIRECT | 195 | + | EventMask::PROPERTY_CHANGE |
| 205 | - | EventMask::SUBSTRUCTURE_NOTIFY | 196 | + | EventMask::KEY_PRESS |
| 206 | - | EventMask::PROPERTY_CHANGE | 197 | + | EventMask::BUTTON_PRESS |
| 207 | - | EventMask::KEY_PRESS | 198 | + | EventMask::POINTER_MOTION, |
| 208 | - | EventMask::BUTTON_PRESS | 199 | + ), |
| 209 | - | EventMask::POINTER_MOTION, | ||
| 210 | - ), | ||
| 211 | )? | 200 | )? |
| 212 | .check()?; | 201 | .check()?; |
| 213 | 202 | ||
| @@ -256,6 +245,13 @@ impl WindowManager { | |||
| 256 | return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed)); | 245 | return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed)); |
| 257 | } | 246 | } |
| 258 | 247 | ||
| 248 | + // C has better C interop than rust. | ||
| 249 | + let normal_cursor = unsafe { x11::xlib::XCreateFontCursor(display, 68) }; | ||
| 250 | + | ||
| 251 | + unsafe { | ||
| 252 | + x11::xlib::XDefineCursor(display, root as u64, normal_cursor); | ||
| 253 | + } | ||
| 254 | + | ||
| 259 | let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?; | 255 | let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?; |
| 260 | 256 | ||
| 261 | let mut bars = Vec::new(); | 257 | let mut bars = Vec::new(); |
| @@ -270,7 +266,7 @@ impl WindowManager { | |||
| 270 | monitor.screen_x as i16, | 266 | monitor.screen_x as i16, |
| 271 | monitor.screen_y as i16, | 267 | monitor.screen_y as i16, |
| 272 | monitor.screen_width as u16, | 268 | monitor.screen_width as u16, |
| 273 | - normal_cursor, | 269 | + normal_cursor as u32, |
| 274 | )?; | 270 | )?; |
| 275 | bars.push(bar); | 271 | bars.push(bar); |
| 276 | } | 272 | } |
| @@ -291,7 +287,7 @@ impl WindowManager { | |||
| 291 | .saturating_sub(2 * config.gap_outer_horizontal as i32) as u16, | 287 | .saturating_sub(2 * config.gap_outer_horizontal as i32) as u16, |
| 292 | config.scheme_occupied, | 288 | config.scheme_occupied, |
| 293 | config.scheme_selected, | 289 | config.scheme_selected, |
| 294 | - normal_cursor, | 290 | + normal_cursor as u32, |
| 295 | )?; | 291 | )?; |
| 296 | tab_bars.push(tab_bar); | 292 | tab_bars.push(tab_bar); |
| 297 | } | 293 | } |
| @@ -806,7 +802,8 @@ impl WindowManager { | |||
| 806 | let window_width = (available_width - total_inner_gaps) / visible_count as i32; | 802 | let window_width = (available_width - total_inner_gaps) / visible_count as i32; |
| 807 | let scroll_amount = window_width + inner_gap as i32; | 803 | let scroll_amount = window_width + inner_gap as i32; |
| 808 | 804 | ||
| 809 | - let total_width = tiled_count as i32 * window_width + (tiled_count - 1) as i32 * inner_gap as i32; | 805 | + let total_width = |
| 806 | + tiled_count as i32 * window_width + (tiled_count - 1) as i32 * inner_gap as i32; | ||
| 810 | let max_scroll = (total_width - available_width).max(0); | 807 | let max_scroll = (total_width - available_width).max(0); |
| 811 | 808 | ||
| 812 | let current_offset = monitor.scroll_offset; | 809 | let current_offset = monitor.scroll_offset; |
| @@ -817,7 +814,8 @@ impl WindowManager { | |||
| 817 | }; | 814 | }; |
| 818 | let target_offset = target_offset.clamp(0, max_scroll); | 815 | let target_offset = target_offset.clamp(0, max_scroll); |
| 819 | 816 | ||
| 820 | - self.scroll_animation.start(current_offset, target_offset, &self.animation_config); | 817 | + self.scroll_animation |
| 818 | + .start(current_offset, target_offset, &self.animation_config); | ||
| 821 | 819 | ||
| 822 | Ok(()) | 820 | Ok(()) |
| 823 | } | 821 | } |
| @@ -870,7 +868,8 @@ impl WindowManager { | |||
| 870 | let tiled_count = tiled_windows.len(); | 868 | let tiled_count = tiled_windows.len(); |
| 871 | if tiled_count <= visible_count { | 869 | if tiled_count <= visible_count { |
| 872 | if animate && monitor.scroll_offset != 0 { | 870 | if animate && monitor.scroll_offset != 0 { |
| 873 | - self.scroll_animation.start(monitor.scroll_offset, 0, &self.animation_config); | 871 | + self.scroll_animation |
| 872 | + .start(monitor.scroll_offset, 0, &self.animation_config); | ||
| 874 | } else if let Some(m) = self.monitors.get_mut(monitor_index) { | 873 | } else if let Some(m) = self.monitors.get_mut(monitor_index) { |
| 875 | m.scroll_offset = 0; | 874 | m.scroll_offset = 0; |
| 876 | } | 875 | } |
| @@ -882,7 +881,8 @@ impl WindowManager { | |||
| 882 | let window_width = (available_width - total_inner_gaps) / visible_count as i32; | 881 | let window_width = (available_width - total_inner_gaps) / visible_count as i32; |
| 883 | let scroll_step = window_width + inner_gap as i32; | 882 | let scroll_step = window_width + inner_gap as i32; |
| 884 | 883 | ||
| 885 | - let total_width = tiled_count as i32 * window_width + (tiled_count - 1) as i32 * inner_gap as i32; | 884 | + let total_width = |
| 885 | + tiled_count as i32 * window_width + (tiled_count - 1) as i32 * inner_gap as i32; | ||
| 886 | let max_scroll = (total_width - available_width).max(0); | 886 | let max_scroll = (total_width - available_width).max(0); |
| 887 | 887 | ||
| 888 | let target_scroll = (target_idx as i32) * scroll_step; | 888 | let target_scroll = (target_idx as i32) * scroll_step; |
| @@ -891,7 +891,8 @@ impl WindowManager { | |||
| 891 | let current_offset = monitor.scroll_offset; | 891 | let current_offset = monitor.scroll_offset; |
| 892 | if current_offset != new_offset { | 892 | if current_offset != new_offset { |
| 893 | if animate { | 893 | if animate { |
| 894 | - self.scroll_animation.start(current_offset, new_offset, &self.animation_config); | 894 | + self.scroll_animation |
| 895 | + .start(current_offset, new_offset, &self.animation_config); | ||
| 895 | } else { | 896 | } else { |
| 896 | if let Some(m) = self.monitors.get_mut(monitor_index) { | 897 | if let Some(m) = self.monitors.get_mut(monitor_index) { |
| 897 | m.scroll_offset = new_offset; | 898 | m.scroll_offset = new_offset; |
| @@ -950,11 +951,13 @@ impl WindowManager { | |||
| 950 | }; | 951 | }; |
| 951 | 952 | ||
| 952 | let available_width = monitor.screen_width - 2 * outer_gap as i32; | 953 | let available_width = monitor.screen_width - 2 * outer_gap as i32; |
| 953 | - let total_inner_gaps = inner_gap as i32 * (visible_count.min(tiled_count) - 1) as i32; | 954 | + let total_inner_gaps = |
| 955 | + inner_gap as i32 * (visible_count.min(tiled_count) - 1) as i32; | ||
| 954 | let window_width = if tiled_count <= visible_count { | 956 | let window_width = if tiled_count <= visible_count { |
| 955 | (available_width - total_inner_gaps) / tiled_count as i32 | 957 | (available_width - total_inner_gaps) / tiled_count as i32 |
| 956 | } else { | 958 | } else { |
| 957 | - (available_width - inner_gap as i32 * (visible_count - 1) as i32) / visible_count as i32 | 959 | + (available_width - inner_gap as i32 * (visible_count - 1) as i32) |
| 960 | + / visible_count as i32 | ||
| 958 | }; | 961 | }; |
| 959 | 962 | ||
| 960 | let scroll_step = window_width + inner_gap as i32; | 963 | let scroll_step = window_width + inner_gap as i32; |
| @@ -963,7 +966,8 @@ impl WindowManager { | |||
| 963 | } else { | 966 | } else { |
| 964 | 1 | 967 | 1 |
| 965 | }; | 968 | }; |
| 966 | - let last_visible = (first_visible + visible_count as i32 - 1).min(tiled_count as i32); | 969 | + let last_visible = |
| 970 | + (first_visible + visible_count as i32 - 1).min(tiled_count as i32); | ||
| 967 | 971 | ||
| 968 | return format!("[{}-{}/{}]", first_visible, last_visible, tiled_count); | 972 | return format!("[{}-{}/{}]", first_visible, last_visible, tiled_count); |
| 969 | } | 973 | } |
| @@ -1979,7 +1983,14 @@ impl WindowManager { | |||
| 1979 | 1983 | ||
| 1980 | self.fullscreen_windows.remove(&window); | 1984 | self.fullscreen_windows.remove(&window); |
| 1981 | 1985 | ||
| 1982 | - let (was_floating, restored_x, restored_y, restored_width, restored_height, restored_border) = self | 1986 | + let ( |
| 1987 | + was_floating, | ||
| 1988 | + restored_x, | ||
| 1989 | + restored_y, | ||
| 1990 | + restored_width, | ||
| 1991 | + restored_height, | ||
| 1992 | + restored_border, | ||
| 1993 | + ) = self | ||
| 1983 | .clients | 1994 | .clients |
| 1984 | .get(&window) | 1995 | .get(&window) |
| 1985 | .map(|client| { | 1996 | .map(|client| { |
| @@ -2276,7 +2287,11 @@ impl WindowManager { | |||
| 2276 | } | 2287 | } |
| 2277 | 2288 | ||
| 2278 | if self.layout.name() == "scrolling" { | 2289 | if self.layout.name() == "scrolling" { |
| 2279 | - if let Some(selected) = self.monitors.get(client_monitor).and_then(|m| m.selected_client) { | 2290 | + if let Some(selected) = self |
| 2291 | + .monitors | ||
| 2292 | + .get(client_monitor) | ||
| 2293 | + .and_then(|m| m.selected_client) | ||
| 2294 | + { | ||
| 2280 | self.attach_after(window, selected, client_monitor); | 2295 | self.attach_after(window, selected, client_monitor); |
| 2281 | } else { | 2296 | } else { |
| 2282 | self.attach_aside(window, client_monitor); | 2297 | self.attach_aside(window, client_monitor); |
| @@ -2340,8 +2355,11 @@ impl WindowManager { | |||
| 2340 | .unwrap_or(false); | 2355 | .unwrap_or(false); |
| 2341 | 2356 | ||
| 2342 | if !never_focus { | 2357 | if !never_focus { |
| 2343 | - self.connection | 2358 | + self.connection.set_input_focus( |
| 2344 | - .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; | 2359 | + InputFocus::POINTER_ROOT, |
| 2360 | + window, | ||
| 2361 | + x11rb::CURRENT_TIME, | ||
| 2362 | + )?; | ||
| 2345 | 2363 | ||
| 2346 | self.connection.change_property( | 2364 | self.connection.change_property( |
| 2347 | PropMode::REPLACE, | 2365 | PropMode::REPLACE, |
| @@ -2361,7 +2379,8 @@ impl WindowManager { | |||
| 2361 | } | 2379 | } |
| 2362 | 2380 | ||
| 2363 | fn grabbuttons(&self, window: Window, focused: bool) -> WmResult<()> { | 2381 | fn grabbuttons(&self, window: Window, focused: bool) -> WmResult<()> { |
| 2364 | - self.connection.ungrab_button(ButtonIndex::ANY, window, ModMask::ANY)?; | 2382 | + self.connection |
| 2383 | + .ungrab_button(ButtonIndex::ANY, window, ModMask::ANY)?; | ||
| 2365 | 2384 | ||
| 2366 | if !focused { | 2385 | if !focused { |
| 2367 | self.connection.grab_button( | 2386 | self.connection.grab_button( |
| @@ -2433,7 +2452,8 @@ impl WindowManager { | |||
| 2433 | self.root, | 2452 | self.root, |
| 2434 | x11rb::CURRENT_TIME, | 2453 | x11rb::CURRENT_TIME, |
| 2435 | )?; | 2454 | )?; |
| 2436 | - self.connection.delete_property(self.root, self.atoms.net_active_window)?; | 2455 | + self.connection |
| 2456 | + .delete_property(self.root, self.atoms.net_active_window)?; | ||
| 2437 | } | 2457 | } |
| 2438 | 2458 | ||
| 2439 | Ok(()) | 2459 | Ok(()) |
| @@ -2446,9 +2466,7 @@ impl WindowManager { | |||
| 2446 | .and_then(|m| m.selected_client); | 2466 | .and_then(|m| m.selected_client); |
| 2447 | 2467 | ||
| 2448 | let mut focus_client = window; | 2468 | let mut focus_client = window; |
| 2449 | - if focus_client.is_none() | 2469 | + if focus_client.is_none() || focus_client.is_some_and(|w| !self.is_visible(w)) { |
| 2450 | - || focus_client.is_some_and(|w| !self.is_visible(w)) | ||
| 2451 | - { | ||
| 2452 | let mut current = self | 2470 | let mut current = self |
| 2453 | .monitors | 2471 | .monitors |
| 2454 | .get(self.selected_monitor) | 2472 | .get(self.selected_monitor) |
| @@ -2502,8 +2520,11 @@ impl WindowManager { | |||
| 2502 | .unwrap_or(false); | 2520 | .unwrap_or(false); |
| 2503 | 2521 | ||
| 2504 | if !never_focus { | 2522 | if !never_focus { |
| 2505 | - self.connection | 2523 | + self.connection.set_input_focus( |
| 2506 | - .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?; | 2524 | + InputFocus::POINTER_ROOT, |
| 2525 | + win, | ||
| 2526 | + x11rb::CURRENT_TIME, | ||
| 2527 | + )?; | ||
| 2507 | 2528 | ||
| 2508 | self.connection.change_property( | 2529 | self.connection.change_property( |
| 2509 | PropMode::REPLACE, | 2530 | PropMode::REPLACE, |