wayland_sys/
cursor.rs

1//! Bindings to the `wayland-cursor.so` library
2//!
3//! The created handle is named `wayland_cursor_handle()`.
4
5use crate::client::wl_proxy;
6#[cfg(feature = "dlopen")]
7use once_cell::sync::Lazy;
8use std::os::raw::{c_char, c_int, c_uint};
9
10pub enum wl_cursor_theme {}
11
12#[repr(C)]
13pub struct wl_cursor_image {
14    /// actual width
15    pub width: u32,
16    /// actual height
17    pub height: u32,
18    /// hot spot x (must be inside image)
19    pub hotspot_x: u32,
20    /// hot spot y (must be inside image)
21    pub hotspot_y: u32,
22    /// animation delay to next frame
23    pub delay: u32,
24}
25
26#[repr(C)]
27pub struct wl_cursor {
28    pub image_count: c_uint,
29    pub images: *mut *mut wl_cursor_image,
30    pub name: *mut c_char,
31}
32
33external_library!(WaylandCursor, "wayland-cursor",
34    functions:
35        fn wl_cursor_theme_load(*const c_char, c_int, *mut wl_proxy) -> *mut wl_cursor_theme,
36        fn wl_cursor_theme_destroy(*mut wl_cursor_theme) -> (),
37        fn wl_cursor_theme_get_cursor(*mut wl_cursor_theme, *const c_char) -> *mut wl_cursor,
38        fn wl_cursor_image_get_buffer(*mut wl_cursor_image) -> *mut wl_proxy,
39        fn wl_cursor_frame(*mut wl_cursor, u32) -> c_int,
40        fn wl_cursor_frame_and_duration(*mut wl_cursor, u32, *mut u32) -> c_int,
41);
42
43#[cfg(feature = "dlopen")]
44pub fn wayland_cursor_option() -> Option<&'static WaylandCursor> {
45    static WAYLAND_CURSOR_OPTION: Lazy<Option<WaylandCursor>> = Lazy::new(|| {
46        let versions = ["libwayland-cursor.so.0", "libwayland-cursor.so"];
47
48        for ver in &versions {
49            match unsafe { WaylandCursor::open(ver) } {
50                Ok(h) => return Some(h),
51                Err(::dlib::DlError::CantOpen(_)) => continue,
52                Err(::dlib::DlError::MissingSymbol(s)) => {
53                    log::error!("Found library {} cannot be used: symbol {} is missing.", ver, s);
54                    return None;
55                }
56            }
57        }
58        None
59    });
60
61    WAYLAND_CURSOR_OPTION.as_ref()
62}
63
64#[cfg(feature = "dlopen")]
65pub fn wayland_cursor_handle() -> &'static WaylandCursor {
66    static WAYLAND_CURSOR_HANDLE: Lazy<&'static WaylandCursor> = Lazy::new(|| {
67        wayland_cursor_option().expect("Library libwayland-cursor.so could not be loaded.")
68    });
69
70    &WAYLAND_CURSOR_HANDLE
71}
72
73#[cfg(not(feature = "dlopen"))]
74pub fn is_lib_available() -> bool {
75    true
76}
77#[cfg(feature = "dlopen")]
78pub fn is_lib_available() -> bool {
79    wayland_cursor_option().is_some()
80}