tony / oxwm

C has better C interop than rust. Browse files
tonybanters committed 2026-01-15 parent fc65b0e commit 51a0f9dde0eccd72e2d84091108b0325548ba774
Showing 1 changed file with 61 additions and 40 deletions. UnifiedSplit
+61 −40 src/window_manager.rs
@@ -10,7 +10,6 @@ use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout}
10 10 use crate::monitor::{Monitor, detect_monitors};
11 11 use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay};
12 12 use std::collections::{HashMap, HashSet};
13 -use x11rb::cursor::Handle as CursorHandle;
14 13
15 14 use x11rb::connection::Connection;
16 15 use x11rb::protocol::Event;
@@ -187,27 +186,17 @@ impl WindowManager {
187 186 let root = connection.setup().roots[screen_number].root;
188 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 189 connection
199 190 .change_window_attributes(
200 191 root,
201 - &ChangeWindowAttributesAux::new()
202 - .cursor(normal_cursor)
203 - .event_mask(
204 - EventMask::SUBSTRUCTURE_REDIRECT
205 - | EventMask::SUBSTRUCTURE_NOTIFY
206 - | EventMask::PROPERTY_CHANGE
207 - | EventMask::KEY_PRESS
208 - | EventMask::BUTTON_PRESS
209 - | EventMask::POINTER_MOTION,
210 - ),
192 + &ChangeWindowAttributesAux::new().event_mask(
193 + EventMask::SUBSTRUCTURE_REDIRECT
194 + | EventMask::SUBSTRUCTURE_NOTIFY
195 + | EventMask::PROPERTY_CHANGE
196 + | EventMask::KEY_PRESS
197 + | EventMask::BUTTON_PRESS
198 + | EventMask::POINTER_MOTION,
199 + ),
211 200 )?
212 201 .check()?;
213 202
@@ -256,6 +245,13 @@ impl WindowManager {
256 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 255 let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?;
260 256
261 257 let mut bars = Vec::new();
@@ -270,7 +266,7 @@ impl WindowManager {
270 266 monitor.screen_x as i16,
271 267 monitor.screen_y as i16,
272 268 monitor.screen_width as u16,
273 - normal_cursor,
269 + normal_cursor as u32,
274 270 )?;
275 271 bars.push(bar);
276 272 }
@@ -291,7 +287,7 @@ impl WindowManager {
291 287 .saturating_sub(2 * config.gap_outer_horizontal as i32) as u16,
292 288 config.scheme_occupied,
293 289 config.scheme_selected,
294 - normal_cursor,
290 + normal_cursor as u32,
295 291 )?;
296 292 tab_bars.push(tab_bar);
297 293 }
@@ -806,7 +802,8 @@ impl WindowManager {
806 802 let window_width = (available_width - total_inner_gaps) / visible_count as i32;
807 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 807 let max_scroll = (total_width - available_width).max(0);
811 808
812 809 let current_offset = monitor.scroll_offset;
@@ -817,7 +814,8 @@ impl WindowManager {
817 814 };
818 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 820 Ok(())
823 821 }
@@ -870,7 +868,8 @@ impl WindowManager {
870 868 let tiled_count = tiled_windows.len();
871 869 if tiled_count <= visible_count {
872 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 873 } else if let Some(m) = self.monitors.get_mut(monitor_index) {
875 874 m.scroll_offset = 0;
876 875 }
@@ -882,7 +881,8 @@ impl WindowManager {
882 881 let window_width = (available_width - total_inner_gaps) / visible_count as i32;
883 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 886 let max_scroll = (total_width - available_width).max(0);
887 887
888 888 let target_scroll = (target_idx as i32) * scroll_step;
@@ -891,7 +891,8 @@ impl WindowManager {
891 891 let current_offset = monitor.scroll_offset;
892 892 if current_offset != new_offset {
893 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 896 } else {
896 897 if let Some(m) = self.monitors.get_mut(monitor_index) {
897 898 m.scroll_offset = new_offset;
@@ -950,11 +951,13 @@ impl WindowManager {
950 951 };
951 952
952 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 956 let window_width = if tiled_count <= visible_count {
955 957 (available_width - total_inner_gaps) / tiled_count as i32
956 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 963 let scroll_step = window_width + inner_gap as i32;
@@ -963,7 +966,8 @@ impl WindowManager {
963 966 } else {
964 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 972 return format!("[{}-{}/{}]", first_visible, last_visible, tiled_count);
969 973 }
@@ -1979,7 +1983,14 @@ impl WindowManager {
1979 1983
1980 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 1994 .clients
1984 1995 .get(&window)
1985 1996 .map(|client| {
@@ -2276,7 +2287,11 @@ impl WindowManager {
2276 2287 }
2277 2288
2278 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 2295 self.attach_after(window, selected, client_monitor);
2281 2296 } else {
2282 2297 self.attach_aside(window, client_monitor);
@@ -2340,8 +2355,11 @@ impl WindowManager {
2340 2355 .unwrap_or(false);
2341 2356
2342 2357 if !never_focus {
2343 - self.connection
2344 - .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?;
2358 + self.connection.set_input_focus(
2359 + InputFocus::POINTER_ROOT,
2360 + window,
2361 + x11rb::CURRENT_TIME,
2362 + )?;
2345 2363
2346 2364 self.connection.change_property(
2347 2365 PropMode::REPLACE,
@@ -2361,7 +2379,8 @@ impl WindowManager {
2361 2379 }
2362 2380
2363 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 2385 if !focused {
2367 2386 self.connection.grab_button(
@@ -2433,7 +2452,8 @@ impl WindowManager {
2433 2452 self.root,
2434 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 2459 Ok(())
@@ -2446,9 +2466,7 @@ impl WindowManager {
2446 2466 .and_then(|m| m.selected_client);
2447 2467
2448 2468 let mut focus_client = window;
2449 - if focus_client.is_none()
2450 - || focus_client.is_some_and(|w| !self.is_visible(w))
2451 - {
2469 + if focus_client.is_none() || focus_client.is_some_and(|w| !self.is_visible(w)) {
2452 2470 let mut current = self
2453 2471 .monitors
2454 2472 .get(self.selected_monitor)
@@ -2502,8 +2520,11 @@ impl WindowManager {
2502 2520 .unwrap_or(false);
2503 2521
2504 2522 if !never_focus {
2505 - self.connection
2506 - .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
2523 + self.connection.set_input_focus(
2524 + InputFocus::POINTER_ROOT,
2525 + win,
2526 + x11rb::CURRENT_TIME,
2527 + )?;
2507 2528
2508 2529 self.connection.change_property(
2509 2530 PropMode::REPLACE,