Skip to main content

wayland_sys/
egl.rs

1//! Bindings to the EGL library `libwayland-egl.so`
2//!
3//! This lib allows to create EGL surfaces out of wayland surfaces.
4//!
5//! The created handle is named `wayland_egl_handle()`.
6
7use crate::client::wl_proxy;
8use std::os::raw::c_int;
9#[cfg(feature = "dlopen")]
10use std::sync::LazyLock;
11
12pub enum wl_egl_window {}
13
14external_library!(WaylandEgl, "wayland-egl",
15    functions:
16        fn wl_egl_window_create(*mut wl_proxy, c_int, c_int) -> *mut wl_egl_window,
17        fn wl_egl_window_destroy(*mut wl_egl_window) -> (),
18        fn wl_egl_window_resize(*mut wl_egl_window, c_int, c_int, c_int, c_int) -> (),
19        fn wl_egl_window_get_attached_size(*mut wl_egl_window, *mut c_int, *mut c_int) -> (),
20);
21
22#[cfg(feature = "dlopen")]
23pub fn wayland_egl_option() -> Option<&'static WaylandEgl> {
24    static WAYLAND_EGL_OPTION: LazyLock<Option<WaylandEgl>> = LazyLock::new(|| {
25        let versions = ["libwayland-egl.so.1", "libwayland-egl.so"];
26
27        for ver in &versions {
28            match unsafe { WaylandEgl::open(ver) } {
29                Ok(h) => return Some(h),
30                Err(::dlib::DlError::CantOpen(_)) => continue,
31                Err(::dlib::DlError::MissingSymbol(s)) => {
32                    log::error!("Found library {ver} cannot be used: symbol {s} is missing.");
33                    return None;
34                }
35            }
36        }
37        None
38    });
39
40    WAYLAND_EGL_OPTION.as_ref()
41}
42
43#[cfg(feature = "dlopen")]
44pub fn wayland_egl_handle() -> &'static WaylandEgl {
45    static WAYLAND_EGL_HANDLE: LazyLock<&'static WaylandEgl> = LazyLock::new(|| {
46        wayland_egl_option().expect("Library libwayland-egl.so could not be loaded.")
47    });
48
49    &WAYLAND_EGL_HANDLE
50}
51
52#[cfg(not(feature = "dlopen"))]
53pub fn is_lib_available() -> bool {
54    true
55}
56#[cfg(feature = "dlopen")]
57pub fn is_lib_available() -> bool {
58    wayland_egl_option().is_some()
59}