From aa3b8ca084babcb451ea73dc18843a49ff1d88eb Mon Sep 17 00:00:00 2001 From: fufesou Date: Fri, 18 Nov 2022 15:51:33 +0800 Subject: [PATCH] virtual display remove static links Signed-off-by: fufesou --- Cargo.lock | 1 - build.py | 5 -- libs/virtual_display/Cargo.toml | 1 - .../{ => dylib}/examples/idd_controller.rs | 2 +- libs/virtual_display/dylib/src/lib.rs | 6 ++ libs/virtual_display/src/lib.rs | 68 ++++++++++++++----- 6 files changed, 59 insertions(+), 24 deletions(-) rename libs/virtual_display/{ => dylib}/examples/idd_controller.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index 07cb346b8..9eb270683 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5567,7 +5567,6 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" name = "virtual_display" version = "0.1.0" dependencies = [ - "dylib_virtual_display", "hbb_common", "lazy_static", "libloading", diff --git a/build.py b/build.py index a95c64dc1..07c571b11 100755 --- a/build.py +++ b/build.py @@ -317,11 +317,6 @@ def main(): os.system('python3 res/inline-sciter.py') portable = args.portable if windows: - # build virtual display dynamic library - os.chdir('libs/virtual_display/dylib') - os.system('cargo build --release') - os.chdir('../../..') - if flutter: build_flutter_windows(version, features) return diff --git a/libs/virtual_display/Cargo.toml b/libs/virtual_display/Cargo.toml index 88e065206..c700bd12a 100644 --- a/libs/virtual_display/Cargo.toml +++ b/libs/virtual_display/Cargo.toml @@ -9,4 +9,3 @@ edition = "2021" lazy_static = "1.4" libloading = "0.7" hbb_common = { path = "../hbb_common" } -dylib_virtual_display = { path = "./dylib" } diff --git a/libs/virtual_display/examples/idd_controller.rs b/libs/virtual_display/dylib/examples/idd_controller.rs similarity index 98% rename from libs/virtual_display/examples/idd_controller.rs rename to libs/virtual_display/dylib/examples/idd_controller.rs index 7d5677724..c9a3fbbab 100644 --- a/libs/virtual_display/examples/idd_controller.rs +++ b/libs/virtual_display/dylib/examples/idd_controller.rs @@ -1,5 +1,5 @@ #[cfg(windows)] -use virtual_display::win10::{idd, DRIVER_INSTALL_PATH}; +use dylib_virtual_display::win10::{idd, DRIVER_INSTALL_PATH}; #[cfg(windows)] use std::{ diff --git a/libs/virtual_display/dylib/src/lib.rs b/libs/virtual_display/dylib/src/lib.rs index 7ffcc679f..4a95e3461 100644 --- a/libs/virtual_display/dylib/src/lib.rs +++ b/libs/virtual_display/dylib/src/lib.rs @@ -11,6 +11,12 @@ lazy_static::lazy_static! { static ref MONITOR_PLUGIN: Mutex> = Mutex::new(Vec::new()); } +#[no_mangle] +#[cfg(windows)] +pub fn get_dirver_install_path() -> &'static str { + win10::DRIVER_INSTALL_PATH +} + #[no_mangle] pub fn download_driver() -> ResultType<()> { #[cfg(windows)] diff --git a/libs/virtual_display/src/lib.rs b/libs/virtual_display/src/lib.rs index 47b11d74a..cd9402c69 100644 --- a/libs/virtual_display/src/lib.rs +++ b/libs/virtual_display/src/lib.rs @@ -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>> = { - #[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:: &'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:: 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::>(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:: 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:: ResultType<()>>>(b"install_update_driver") { + match lib.get:: 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:: ResultType<()>>>(b"uninstall_driver") { + match lib + .get:: 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),