virtual display remove static links

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2022-11-18 15:51:33 +08:00
parent 27e7b57222
commit aa3b8ca084
6 changed files with 59 additions and 24 deletions

View File

@@ -1,24 +1,52 @@
#[cfg(windows)]
pub use dylib_virtual_display::win10;
use hbb_common::{bail, ResultType};
use std::sync::{Arc, Mutex};
const LIB_NAME_VIRTUAL_DISPLAY: &str = "virtual_display";
const LIB_NAME_VIRTUAL_DISPLAY: &str = "dylib_virtual_display";
lazy_static::lazy_static! {
static ref LIB_VIRTUAL_DISPLAY: Arc<Mutex<Result<libloading::Library, libloading::Error>>> = {
#[cfg(target_os = "windows")]
let libname = format!("{}.dll", LIB_NAME_VIRTUAL_DISPLAY);
#[cfg(target_os = "linux")]
let libname = format!("lib{}.so", LIB_NAME_VIRTUAL_DISPLAY);
#[cfg(target_os = "macos")]
let libname = format!("lib{}.dylib", LIB_NAME_VIRTUAL_DISPLAY);
Arc::new(Mutex::new(unsafe { libloading::Library::new(libname) }))
Arc::new(Mutex::new(unsafe { libloading::Library::new(get_lib_name()) }))
};
}
#[cfg(target_os = "windows")]
fn get_lib_name() -> String {
format!("{}.dll", LIB_NAME_VIRTUAL_DISPLAY)
}
#[cfg(target_os = "linux")]
fn get_lib_name() -> String {
format!("lib{}.so", LIB_NAME_VIRTUAL_DISPLAY)
}
#[cfg(target_os = "macos")]
fn get_lib_name() -> String {
format!("lib{}.dylib", LIB_NAME_VIRTUAL_DISPLAY)
}
fn try_reload_lib() {
let mut lock = LIB_VIRTUAL_DISPLAY.lock().unwrap();
if lock.is_err() {
*lock = unsafe { libloading::Library::new(get_lib_name()) };
}
}
#[cfg(windows)]
pub fn get_dirver_install_path() -> ResultType<&'static str> {
try_reload_lib();
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
match lib.get::<libloading::Symbol<fn() -> &'static str>>(b"get_dirver_install_path") {
Ok(func) => Ok(func()),
Err(e) => bail!("Failed to load func get_dirver_install_path, {}", e),
}
},
Err(e) => bail!("Failed to load library {}, {}", LIB_NAME_VIRTUAL_DISPLAY, e),
}
}
pub fn is_device_created() -> bool {
try_reload_lib();
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
match lib.get::<libloading::Symbol<fn() -> bool>>(b"is_device_created") {
@@ -31,6 +59,7 @@ pub fn is_device_created() -> bool {
}
pub fn close_device() {
try_reload_lib();
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
match lib.get::<libloading::Symbol<fn()>>(b"close_device") {
@@ -45,11 +74,12 @@ pub fn close_device() {
macro_rules! def_func_result {
($func:ident, $name: tt) => {
pub fn $func() -> ResultType<()> {
try_reload_lib();
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
match lib.get::<libloading::Symbol<fn() -> ResultType<()>>>($name.as_bytes()) {
Ok(func) => func(),
Err(..) => bail!("Failed to load func {}", $name),
Err(e) => bail!("Failed to load func {}, {}", $name, e),
}
},
Err(e) => bail!("Failed to load library {}, {}", LIB_NAME_VIRTUAL_DISPLAY, e),
@@ -59,11 +89,14 @@ macro_rules! def_func_result {
}
pub fn install_update_driver(reboot_required: &mut bool) -> ResultType<()> {
try_reload_lib();
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
match lib.get::<libloading::Symbol<fn(&mut bool) -> ResultType<()>>>(b"install_update_driver") {
match lib.get::<libloading::Symbol<fn(&mut bool) -> ResultType<()>>>(
b"install_update_driver",
) {
Ok(func) => func(reboot_required),
Err(..) => bail!("Failed to load func install_update_driver"),
Err(e) => bail!("Failed to load func install_update_driver, {}", e),
}
},
Err(e) => bail!("Failed to load library {}, {}", LIB_NAME_VIRTUAL_DISPLAY, e),
@@ -71,11 +104,14 @@ pub fn install_update_driver(reboot_required: &mut bool) -> ResultType<()> {
}
pub fn uninstall_driver(reboot_required: &mut bool) -> ResultType<()> {
try_reload_lib();
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
match lib.get::<libloading::Symbol<fn(&mut bool) -> ResultType<()>>>(b"uninstall_driver") {
match lib
.get::<libloading::Symbol<fn(&mut bool) -> ResultType<()>>>(b"uninstall_driver")
{
Ok(func) => func(reboot_required),
Err(..) => bail!("Failed to load func uninstall_driver"),
Err(e) => bail!("Failed to load func uninstall_driver, {}", e),
}
},
Err(e) => bail!("Failed to load library {}, {}", LIB_NAME_VIRTUAL_DISPLAY, e),