Skip to content

Commit

Permalink
log from the helper functions instead of while loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Adjective-Object committed Dec 30, 2024
1 parent cf5a0ce commit 882bf08
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 77 deletions.
62 changes: 33 additions & 29 deletions src/native/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,36 +214,40 @@ impl LibEgl {
pub fn try_load() -> Option<LibEgl> {
module::Module::load("libEGL.so")
.or_else(|_| module::Module::load("libEGL.so.1"))
.map(|module| LibEgl {
eglChooseConfig: module.get_symbol("eglChooseConfig").ok(),
eglCopyBuffers: module.get_symbol("eglCopyBuffers").ok(),
eglCreateContext: module.get_symbol("eglCreateContext").ok(),
eglCreatePbufferSurface: module.get_symbol("eglCreatePbufferSurface").ok(),
eglCreatePixmapSurface: module.get_symbol("eglCreatePixmapSurface").ok(),
eglCreateWindowSurface: module.get_symbol("eglCreateWindowSurface").ok(),
eglDestroyContext: module.get_symbol("eglDestroyContext").ok(),
eglDestroySurface: module.get_symbol("eglDestroySurface").ok(),
eglGetConfigAttrib: module.get_symbol("eglGetConfigAttrib").ok(),
eglGetConfigs: module.get_symbol("eglGetConfigs").ok(),
eglGetCurrentDisplay: module.get_symbol("eglGetCurrentDisplay").ok(),
eglGetCurrentSurface: module.get_symbol("eglGetCurrentSurface").ok(),
eglGetDisplay: module.get_symbol("eglGetDisplay").ok(),
eglGetError: module.get_symbol("eglGetError").ok(),
eglGetProcAddress: module.get_symbol("eglGetProcAddress").ok(),
eglInitialize: module.get_symbol("eglInitialize").ok(),
eglMakeCurrent: module.get_symbol("eglMakeCurrent").ok(),
eglQueryContext: module.get_symbol("eglQueryContext").ok(),
eglQueryString: module.get_symbol("eglQueryString").ok(),
eglQuerySurface: module.get_symbol("eglQuerySurface").ok(),
eglSwapBuffers: module.get_symbol("eglSwapBuffers").ok(),
eglTerminate: module.get_symbol("eglTerminate").ok(),
eglWaitGL: module.get_symbol("eglWaitGL").ok(),
eglWaitNative: module.get_symbol("eglWaitNative").ok(),
eglBindTexImage: module.get_symbol("eglBindTexImage").ok(),
eglReleaseTexImage: module.get_symbol("eglReleaseTexImage").ok(),
eglSurfaceAttrib: module.get_symbol("eglSurfaceAttrib").ok(),
eglSwapInterval: module.get_symbol("eglSwapInterval").ok(),
.and_then(|module| Ok(LibEgl {
eglChooseConfig: module.get_symbol("eglChooseConfig")?,
eglCopyBuffers: module.get_symbol("eglCopyBuffers")?,
eglCreateContext: module.get_symbol("eglCreateContext")?,
eglCreatePbufferSurface: module.get_symbol("eglCreatePbufferSurface")?,
eglCreatePixmapSurface: module.get_symbol("eglCreatePixmapSurface")?,
eglCreateWindowSurface: module.get_symbol("eglCreateWindowSurface")?,
eglDestroyContext: module.get_symbol("eglDestroyContext")?,
eglDestroySurface: module.get_symbol("eglDestroySurface")?,
eglGetConfigAttrib: module.get_symbol("eglGetConfigAttrib")?,
eglGetConfigs: module.get_symbol("eglGetConfigs")?,
eglGetCurrentDisplay: module.get_symbol("eglGetCurrentDisplay")?,
eglGetCurrentSurface: module.get_symbol("eglGetCurrentSurface")?,
eglGetDisplay: module.get_symbol("eglGetDisplay")?,
eglGetError: module.get_symbol("eglGetError")?,
eglGetProcAddress: module.get_symbol("eglGetProcAddress")?,
eglInitialize: module.get_symbol("eglInitialize")?,
eglMakeCurrent: module.get_symbol("eglMakeCurrent")?,
eglQueryContext: module.get_symbol("eglQueryContext")?,
eglQueryString: module.get_symbol("eglQueryString")?,
eglQuerySurface: module.get_symbol("eglQuerySurface")?,
eglSwapBuffers: module.get_symbol("eglSwapBuffers")?,
eglTerminate: module.get_symbol("eglTerminate")?,
eglWaitGL: module.get_symbol("eglWaitGL")?,
eglWaitNative: module.get_symbol("eglWaitNative")?,
eglBindTexImage: module.get_symbol("eglBindTexImage")?,
eglReleaseTexImage: module.get_symbol("eglReleaseTexImage")?,
eglSurfaceAttrib: module.get_symbol("eglSurfaceAttrib")?,
eglSwapInterval: module.get_symbol("eglSwapInterval")?,
module,
}))
.map_err(|err| {
eprintln!("failed loading libEGL: {err}");
err
})
.ok()
}
Expand Down
16 changes: 4 additions & 12 deletions src/native/linux_wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,22 +533,14 @@ impl crate::native::Clipboard for WaylandClipboard {
fn set(&mut self, _data: &str) {}
}


pub fn eprint_iferr<T>(lib_name: &'static str, opt_lib: Option<T>) -> Option<T> {
if opt_lib.is_none() {
eprintln!("failed to load library {}", lib_name);
}
return opt_lib
}

pub fn run<F>(conf: &crate::conf::Conf, f: &mut Option<F>) -> Option<()>
where
F: 'static + FnOnce() -> Box<dyn EventHandler>,
{
unsafe {
let client = eprint_iferr("libwayland-client", LibWaylandClient::try_load())?;
let egl = eprint_iferr("libwayland-egl", LibWaylandEgl::try_load())?;
let xkb = eprint_iferr("libxkbcommon", LibXkbCommon::try_load())?;
let client = LibWaylandClient::try_load()?;
let egl = LibWaylandEgl::try_load()?;
let xkb = LibXkbCommon::try_load()?;

let wdisplay = (client.wl_display_connect)(std::ptr::null_mut());
if wdisplay.is_null() {
Expand Down Expand Up @@ -630,7 +622,7 @@ where
eprintln!("Decoration manager not found, will draw fallback decorations");
}

let mut libegl = eprint_iferr("libEGL", egl::LibEgl::try_load())?;
let mut libegl = egl::LibEgl::try_load()?;
let (context, config, egl_display) = egl::create_egl_context(
&mut libegl,
wdisplay as *mut _,
Expand Down
44 changes: 24 additions & 20 deletions src/native/linux_wayland/libwayland_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,38 +621,42 @@ impl LibWaylandClient {
pub fn try_load() -> Option<LibWaylandClient> {
crate::native::module::Module::load("libwayland-client.so")
.or_else(|_| crate::native::module::Module::load("libwayland-client.so.0"))
.map(|module| LibWaylandClient {
wl_display_connect: module.get_symbol("wl_display_connect").unwrap(),
wl_proxy_add_listener: module.get_symbol("wl_proxy_add_listener").unwrap(),
.and_then(|module| Ok(LibWaylandClient {
wl_display_connect: module.get_symbol("wl_display_connect")?,
wl_proxy_add_listener: module.get_symbol("wl_proxy_add_listener")?,
wl_display_dispatch_pending: module
.get_symbol("wl_display_dispatch_pending")
.unwrap(),
?,

wl_proxy_destroy: module.get_symbol("wl_proxy_destroy").unwrap(),
wl_proxy_marshal: module.get_symbol("wl_proxy_marshal").unwrap(),
wl_proxy_destroy: module.get_symbol("wl_proxy_destroy")?,
wl_proxy_marshal: module.get_symbol("wl_proxy_marshal")?,
wl_proxy_marshal_constructor: module
.get_symbol("wl_proxy_marshal_constructor")
.unwrap(),
?,
wl_proxy_marshal_constructor_versioned: module
.get_symbol("wl_proxy_marshal_constructor_versioned")
.unwrap(),
wl_display_roundtrip: module.get_symbol("wl_display_roundtrip").unwrap(),
?,
wl_display_roundtrip: module.get_symbol("wl_display_roundtrip")?,

wl_registry_interface: module.get_symbol("wl_registry_interface").unwrap(),
wl_compositor_interface: module.get_symbol("wl_compositor_interface").unwrap(),
wl_registry_interface: module.get_symbol("wl_registry_interface")?,
wl_compositor_interface: module.get_symbol("wl_compositor_interface")?,
wl_subcompositor_interface: module
.get_symbol("wl_subcompositor_interface")
.unwrap(),
wl_surface_interface: module.get_symbol("wl_surface_interface").unwrap(),
wl_subsurface_interface: module.get_symbol("wl_subsurface_interface").unwrap(),
wl_buffer_interface: module.get_symbol("wl_buffer_interface").unwrap(),
wl_seat_interface: module.get_symbol("wl_seat_interface").unwrap(),
wl_shm_interface: module.get_symbol("wl_shm_interface").unwrap(),
wl_shm_pool_interface: module.get_symbol("wl_shm_pool_interface").unwrap(),
wl_keyboard_interface: module.get_symbol("wl_keyboard_interface").unwrap(),
wl_pointer_interface: module.get_symbol("wl_pointer_interface").unwrap(),
?,
wl_surface_interface: module.get_symbol("wl_surface_interface")?,
wl_subsurface_interface: module.get_symbol("wl_subsurface_interface")?,
wl_buffer_interface: module.get_symbol("wl_buffer_interface")?,
wl_seat_interface: module.get_symbol("wl_seat_interface")?,
wl_shm_interface: module.get_symbol("wl_shm_interface")?,
wl_shm_pool_interface: module.get_symbol("wl_shm_pool_interface")?,
wl_keyboard_interface: module.get_symbol("wl_keyboard_interface")?,
wl_pointer_interface: module.get_symbol("wl_pointer_interface")?,

_module: std::rc::Rc::new(module),
}))
.map_err(|err| {
eprintln!("failed loading libwayland-client: {err}");
err
})
.ok()
}
Expand Down
14 changes: 9 additions & 5 deletions src/native/linux_wayland/libwayland_egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ impl LibWaylandEgl {
pub fn try_load() -> Option<LibWaylandEgl> {
crate::native::module::Module::load("libwayland-egl.so")
.or_else(|_| crate::native::module::Module::load("libwayland-egl.so.1"))
.map(|module| LibWaylandEgl {
wl_egl_window_create: module.get_symbol("wl_egl_window_create").unwrap(),
wl_egl_window_destroy: module.get_symbol("wl_egl_window_destroy").unwrap(),
wl_egl_window_resize: module.get_symbol("wl_egl_window_resize").unwrap(),
.and_then(|module| Ok(LibWaylandEgl {
wl_egl_window_create: module.get_symbol("wl_egl_window_create")?,
wl_egl_window_destroy: module.get_symbol("wl_egl_window_destroy")?,
wl_egl_window_resize: module.get_symbol("wl_egl_window_resize")?,
wl_egl_window_get_attached_size: module
.get_symbol("wl_egl_window_get_attached_size")
.unwrap(),
?,
_module: module,
}))
.map_err(|err| {
eprintln!("failed loading libwayland-egl: {err}");
err
})
.ok()
}
Expand Down
26 changes: 15 additions & 11 deletions src/native/linux_wayland/libxkbcommon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,25 @@ impl LibXkbCommon {
.or_else(|_| crate::native::module::Module::load("libxkbcommon.so.0"))
.or_else(|_| crate::native::module::Module::load("libxkbcommon.so.0.0.0"))
.or_else(|_| crate::native::module::Module::load("libxkbcommon.so.0.0.0.0"))
.map(|module| LibXkbCommon {
xkb_context_new: module.get_symbol("xkb_context_new").unwrap(),
xkb_context_unref: module.get_symbol("xkb_context_unref").unwrap(),
.and_then(|module| Ok(LibXkbCommon {
xkb_context_new: module.get_symbol("xkb_context_new")?,
xkb_context_unref: module.get_symbol("xkb_context_unref")?,
xkb_keymap_new_from_string: module
.get_symbol("xkb_keymap_new_from_string")
.unwrap(),
xkb_keymap_unref: module.get_symbol("xkb_keymap_unref").unwrap(),
xkb_state_new: module.get_symbol("xkb_state_new").unwrap(),
xkb_state_unref: module.get_symbol("xkb_state_unref").unwrap(),
xkb_state_key_get_one_sym: module.get_symbol("xkb_state_key_get_one_sym").unwrap(),
xkb_state_update_mask: module.get_symbol("xkb_state_update_mask").unwrap(),
xkb_keysym_to_utf32: module.get_symbol("xkb_keysym_to_utf32").unwrap(),
?,
xkb_keymap_unref: module.get_symbol("xkb_keymap_unref")?,
xkb_state_new: module.get_symbol("xkb_state_new")?,
xkb_state_unref: module.get_symbol("xkb_state_unref")?,
xkb_state_key_get_one_sym: module.get_symbol("xkb_state_key_get_one_sym")?,
xkb_state_update_mask: module.get_symbol("xkb_state_update_mask")?,
xkb_keysym_to_utf32: module.get_symbol("xkb_keysym_to_utf32")?,

_module: std::rc::Rc::new(module),
}))
.map_err(|err| {
eprintln!("failed loading libxkbcommon: {err}");
err
})
.ok()
}
}
}
9 changes: 9 additions & 0 deletions src/native/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ pub enum Error {
DlSymError(String),
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
Self::DlOpenError(dll_name) => write!(f, "failed opening library {}", dll_name),
Self::DlSymError(dll_symbol) => write!(f, "missing symbol {}", dll_symbol),
}
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub mod linux {
use super::Error;
Expand Down

0 comments on commit 882bf08

Please sign in to comment.