move virtual display to lib workspace

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-11-18 14:52:01 +08:00
parent 26e8355528
commit 27e7b57222
19 changed files with 1702 additions and 297 deletions

20
Cargo.lock generated
View File

@ -1392,6 +1392,18 @@ version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0"
[[package]]
name = "dylib_virtual_display"
version = "0.1.0"
dependencies = [
"cc",
"hbb_common",
"lazy_static",
"serde 1.0.144",
"serde_derive",
"thiserror",
]
[[package]] [[package]]
name = "ed25519" name = "ed25519"
version = "1.5.2" version = "1.5.2"
@ -4391,7 +4403,6 @@ dependencies = [
"lazy_static", "lazy_static",
"libappindicator", "libappindicator",
"libc", "libc",
"libloading",
"libpulse-binding", "libpulse-binding",
"libpulse-simple-binding", "libpulse-simple-binding",
"mac_address", "mac_address",
@ -4425,6 +4436,7 @@ dependencies = [
"trayicon", "trayicon",
"url", "url",
"uuid", "uuid",
"virtual_display",
"whoami", "whoami",
"winapi 0.3.9", "winapi 0.3.9",
"windows-service", "windows-service",
@ -5555,12 +5567,10 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
name = "virtual_display" name = "virtual_display"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cc", "dylib_virtual_display",
"hbb_common", "hbb_common",
"lazy_static", "lazy_static",
"serde 1.0.144", "libloading",
"serde_derive",
"thiserror",
] ]
[[package]] [[package]]

View File

@ -65,7 +65,6 @@ flutter_rust_bridge = { git = "https://github.com/SoLongAndThanksForAllThePizza/
errno = "0.2.8" errno = "0.2.8"
rdev = { git = "https://github.com/asur4s/rdev" } rdev = { git = "https://github.com/asur4s/rdev" }
url = { version = "2.1", features = ["serde"] } url = { version = "2.1", features = ["serde"] }
libloading = "0.7"
reqwest = { version = "0.11", features = ["blocking", "json", "rustls-tls"], default-features=false } reqwest = { version = "0.11", features = ["blocking", "json", "rustls-tls"], default-features=false }
chrono = "0.4.23" chrono = "0.4.23"
@ -92,6 +91,7 @@ winit = "0.26"
winapi = { version = "0.3", features = ["winuser"] } winapi = { version = "0.3", features = ["winuser"] }
winreg = "0.10" winreg = "0.10"
windows-service = "0.4" windows-service = "0.4"
virtual_display = { path = "libs/virtual_display" }
impersonate_system = { git = "https://github.com/21pages/impersonate-system" } impersonate_system = { git = "https://github.com/21pages/impersonate-system" }
shared_memory = "0.12.4" shared_memory = "0.12.4"
shutdown_hooks = "0.1.0" shutdown_hooks = "0.1.0"
@ -126,7 +126,7 @@ jni = "0.19"
flutter_rust_bridge = { git = "https://github.com/SoLongAndThanksForAllThePizza/flutter_rust_bridge" } flutter_rust_bridge = { git = "https://github.com/SoLongAndThanksForAllThePizza/flutter_rust_bridge" }
[workspace] [workspace]
members = ["libs/scrap", "libs/hbb_common", "libs/enigo", "libs/clipboard", "libs/virtual_display", "libs/simple_rc", "libs/portable"] members = ["libs/scrap", "libs/hbb_common", "libs/enigo", "libs/clipboard", "libs/virtual_display", "libs/virtual_display/dylib", "libs/simple_rc", "libs/portable"]
[package.metadata.winres] [package.metadata.winres]
LegalCopyright = "Copyright © 2022 Purslane, Inc." LegalCopyright = "Copyright © 2022 Purslane, Inc."

View File

@ -280,8 +280,8 @@ def build_flutter_windows(version, features):
exit(-1) exit(-1)
os.chdir('flutter') os.chdir('flutter')
os.system('flutter build windows --release') os.system('flutter build windows --release')
shutil.copy2('target/release/deps/virtual_display.dll', flutter_win_target_dir)
os.chdir('..') os.chdir('..')
shutil.copy2('target/release/deps/dylib_virtual_display.dll', flutter_win_target_dir)
os.chdir('libs/portable') os.chdir('libs/portable')
os.system('pip3 install -r requirements.txt') os.system('pip3 install -r requirements.txt')
os.system( os.system(
@ -318,9 +318,9 @@ def main():
portable = args.portable portable = args.portable
if windows: if windows:
# build virtual display dynamic library # build virtual display dynamic library
os.chdir('libs/virtual_display') os.chdir('libs/virtual_display/dylib')
os.system('cargo build --release') os.system('cargo build --release')
os.chdir('../..') os.chdir('../../..')
if flutter: if flutter:
build_flutter_windows(version, features) build_flutter_windows(version, features)

1358
libs/virtual_display/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,18 +3,10 @@ name = "virtual_display"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[lib]
name = "virtual_display"
crate-type = ["cdylib", "staticlib", "rlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
cc = "1.0"
[dependencies] [dependencies]
thiserror = "1.0.30"
lazy_static = "1.4" lazy_static = "1.4"
serde = "1.0" libloading = "0.7"
serde_derive = "1.0"
hbb_common = { path = "../hbb_common" } hbb_common = { path = "../hbb_common" }
dylib_virtual_display = { path = "./dylib" }

View File

@ -1,32 +1,3 @@
# virtual display # virtual display
Virtual display may be used on computers that do not have a monitor. [doc](./dylib/README.md)
[Development reference](https://github.com/pavlobu/deskreen/discussions/86)
## windows
### win10
Win10 provides [Indirect Display Driver Model](https://msdn.microsoft.com/en-us/library/windows/hardware/mt761968(v=vs.85).aspx).
This lib uses [this project](https://github.com/fufesou/RustDeskIddDriver) as the driver.
**NOTE**: Versions before Win10 1607. Try follow [this method](https://github.com/fanxiushu/xdisp_virt/tree/master/indirect_display).
#### tested platforms
- [x] 19041
- [x] 19043
### win7
TODO
[WDDM](https://docs.microsoft.com/en-us/windows-hardware/drivers/display/windows-vista-display-driver-model-design-guide).
## X11
## OSX

View File

@ -0,0 +1,19 @@
[package]
name = "dylib_virtual_display"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "staticlib", "rlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
cc = "1.0"
[dependencies]
thiserror = "1.0.30"
lazy_static = "1.4"
serde = "1.0"
serde_derive = "1.0"
hbb_common = { path = "../../hbb_common" }

View File

@ -0,0 +1,32 @@
# virtual display
Virtual display may be used on computers that do not have a monitor.
[Development reference](https://github.com/pavlobu/deskreen/discussions/86)
## windows
### win10
Win10 provides [Indirect Display Driver Model](https://msdn.microsoft.com/en-us/library/windows/hardware/mt761968(v=vs.85).aspx).
This lib uses [this project](https://github.com/fufesou/RustDeskIddDriver) as the driver.
**NOTE**: Versions before Win10 1607. Try follow [this method](https://github.com/fanxiushu/xdisp_virt/tree/master/indirect_display).
#### tested platforms
- [x] 19041
- [x] 19043
### win7
TODO
[WDDM](https://docs.microsoft.com/en-us/windows-hardware/drivers/display/windows-vista-display-driver-model-design-guide).
## X11
## OSX

View File

@ -0,0 +1,201 @@
#[cfg(windows)]
pub mod win10;
use hbb_common::{bail, lazy_static, ResultType};
use std::{path::Path, sync::Mutex};
lazy_static::lazy_static! {
// If device is uninstalled though "Device Manager" Window.
// Rustdesk is unable to handle device any more...
static ref H_SW_DEVICE: Mutex<u64> = Mutex::new(0);
static ref MONITOR_PLUGIN: Mutex<Vec<u32>> = Mutex::new(Vec::new());
}
#[no_mangle]
pub fn download_driver() -> ResultType<()> {
#[cfg(windows)]
let _download_url = win10::DRIVER_DOWNLOAD_URL;
#[cfg(target_os = "linux")]
let _download_url = "";
// process download and report progress
Ok(())
}
#[no_mangle]
pub fn install_update_driver(_reboot_required: &mut bool) -> ResultType<()> {
#[cfg(windows)]
let install_path = win10::DRIVER_INSTALL_PATH;
#[cfg(not(windows))]
let install_path = "";
let abs_path = Path::new(install_path).canonicalize()?;
if !abs_path.exists() {
bail!("{} not exists", install_path)
}
#[cfg(windows)]
unsafe {
{
// Device must be created before install driver.
// https://github.com/fufesou/RustDeskIddDriver/issues/1
if let Err(e) = create_device() {
bail!("{}", e);
}
let full_install_path: Vec<u16> = abs_path
.to_string_lossy()
.as_ref()
.encode_utf16()
.chain(Some(0).into_iter())
.collect();
let mut reboot_required_tmp = win10::idd::FALSE;
if win10::idd::InstallUpdate(full_install_path.as_ptr() as _, &mut reboot_required_tmp)
== win10::idd::FALSE
{
bail!("{}", win10::get_last_msg()?);
}
*_reboot_required = reboot_required_tmp == win10::idd::TRUE;
}
}
Ok(())
}
#[no_mangle]
pub fn uninstall_driver(_reboot_required: &mut bool) -> ResultType<()> {
#[cfg(windows)]
let install_path = win10::DRIVER_INSTALL_PATH;
#[cfg(not(windows))]
let install_path = "";
let abs_path = Path::new(install_path).canonicalize()?;
if !abs_path.exists() {
bail!("{} not exists", install_path)
}
#[cfg(windows)]
unsafe {
{
let full_install_path: Vec<u16> = abs_path
.to_string_lossy()
.as_ref()
.encode_utf16()
.chain(Some(0).into_iter())
.collect();
let mut reboot_required_tmp = win10::idd::FALSE;
if win10::idd::Uninstall(full_install_path.as_ptr() as _, &mut reboot_required_tmp)
== win10::idd::FALSE
{
bail!("{}", win10::get_last_msg()?);
}
*_reboot_required = reboot_required_tmp == win10::idd::TRUE;
}
}
Ok(())
}
#[no_mangle]
pub fn is_device_created() -> bool {
#[cfg(windows)]
return *H_SW_DEVICE.lock().unwrap() != 0;
#[cfg(not(windows))]
return false;
}
#[no_mangle]
pub fn create_device() -> ResultType<()> {
if is_device_created() {
return Ok(());
}
#[cfg(windows)]
unsafe {
let mut lock_device = H_SW_DEVICE.lock().unwrap();
let mut h_sw_device = *lock_device as win10::idd::HSWDEVICE;
if win10::idd::DeviceCreate(&mut h_sw_device) == win10::idd::FALSE {
bail!("{}", win10::get_last_msg()?);
} else {
*lock_device = h_sw_device as u64;
}
}
Ok(())
}
#[no_mangle]
pub fn close_device() {
#[cfg(windows)]
unsafe {
win10::idd::DeviceClose(*H_SW_DEVICE.lock().unwrap() as win10::idd::HSWDEVICE);
*H_SW_DEVICE.lock().unwrap() = 0;
MONITOR_PLUGIN.lock().unwrap().clear();
}
}
#[no_mangle]
pub fn plug_in_monitor() -> ResultType<()> {
#[cfg(windows)]
unsafe {
let monitor_index = 0 as u32;
let mut plug_in_monitors = MONITOR_PLUGIN.lock().unwrap();
for i in 0..plug_in_monitors.len() {
if let Some(d) = plug_in_monitors.get(i) {
if *d == monitor_index {
return Ok(());
}
};
}
if win10::idd::MonitorPlugIn(monitor_index, 0, 30) == win10::idd::FALSE {
bail!("{}", win10::get_last_msg()?);
}
(*plug_in_monitors).push(monitor_index);
}
Ok(())
}
#[no_mangle]
pub fn plug_out_monitor() -> ResultType<()> {
#[cfg(windows)]
unsafe {
let monitor_index = 0 as u32;
if win10::idd::MonitorPlugOut(monitor_index) == win10::idd::FALSE {
bail!("{}", win10::get_last_msg()?);
}
let mut plug_in_monitors = MONITOR_PLUGIN.lock().unwrap();
for i in 0..plug_in_monitors.len() {
if let Some(d) = plug_in_monitors.get(i) {
if *d == monitor_index {
plug_in_monitors.remove(i);
break;
}
};
}
}
Ok(())
}
#[no_mangle]
pub fn update_monitor_modes() -> ResultType<()> {
#[cfg(windows)]
unsafe {
let monitor_index = 0 as u32;
let mut modes = vec![win10::idd::MonitorMode {
width: 1920,
height: 1080,
sync: 60,
}];
if win10::idd::FALSE
== win10::idd::MonitorModesUpdate(
monitor_index as win10::idd::UINT,
modes.len() as win10::idd::UINT,
modes.as_mut_ptr(),
)
{
bail!("{}", win10::get_last_msg()?);
}
}
Ok(())
}

View File

@ -1,201 +1,89 @@
#[cfg(windows)] #[cfg(windows)]
pub mod win10; pub use dylib_virtual_display::win10;
use hbb_common::{bail, lazy_static, ResultType}; use hbb_common::{bail, ResultType};
use std::{path::Path, sync::Mutex}; use std::sync::{Arc, Mutex};
const LIB_NAME_VIRTUAL_DISPLAY: &str = "virtual_display";
lazy_static::lazy_static! { lazy_static::lazy_static! {
// If device is uninstalled though "Device Manager" Window. static ref LIB_VIRTUAL_DISPLAY: Arc<Mutex<Result<libloading::Library, libloading::Error>>> = {
// Rustdesk is unable to handle device any more... #[cfg(target_os = "windows")]
static ref H_SW_DEVICE: Mutex<u64> = Mutex::new(0); let libname = format!("{}.dll", LIB_NAME_VIRTUAL_DISPLAY);
static ref MONITOR_PLUGIN: Mutex<Vec<u32>> = Mutex::new(Vec::new());
}
#[no_mangle]
pub fn download_driver() -> ResultType<()> {
#[cfg(windows)]
let _download_url = win10::DRIVER_DOWNLOAD_URL;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
let _download_url = ""; let libname = format!("lib{}.so", LIB_NAME_VIRTUAL_DISPLAY);
#[cfg(target_os = "macos")]
// process download and report progress let libname = format!("lib{}.dylib", LIB_NAME_VIRTUAL_DISPLAY);
Arc::new(Mutex::new(unsafe { libloading::Library::new(libname) }))
Ok(()) };
} }
#[no_mangle]
pub fn install_update_driver(_reboot_required: &mut bool) -> ResultType<()> {
#[cfg(windows)]
let install_path = win10::DRIVER_INSTALL_PATH;
#[cfg(not(windows))]
let install_path = "";
let abs_path = Path::new(install_path).canonicalize()?;
if !abs_path.exists() {
bail!("{} not exists", install_path)
}
#[cfg(windows)]
unsafe {
{
// Device must be created before install driver.
// https://github.com/fufesou/RustDeskIddDriver/issues/1
if let Err(e) = create_device() {
bail!("{}", e);
}
let full_install_path: Vec<u16> = abs_path
.to_string_lossy()
.as_ref()
.encode_utf16()
.chain(Some(0).into_iter())
.collect();
let mut reboot_required_tmp = win10::idd::FALSE;
if win10::idd::InstallUpdate(full_install_path.as_ptr() as _, &mut reboot_required_tmp)
== win10::idd::FALSE
{
bail!("{}", win10::get_last_msg()?);
}
*_reboot_required = reboot_required_tmp == win10::idd::TRUE;
}
}
Ok(())
}
#[no_mangle]
pub fn uninstall_driver(_reboot_required: &mut bool) -> ResultType<()> {
#[cfg(windows)]
let install_path = win10::DRIVER_INSTALL_PATH;
#[cfg(not(windows))]
let install_path = "";
let abs_path = Path::new(install_path).canonicalize()?;
if !abs_path.exists() {
bail!("{} not exists", install_path)
}
#[cfg(windows)]
unsafe {
{
let full_install_path: Vec<u16> = abs_path
.to_string_lossy()
.as_ref()
.encode_utf16()
.chain(Some(0).into_iter())
.collect();
let mut reboot_required_tmp = win10::idd::FALSE;
if win10::idd::Uninstall(full_install_path.as_ptr() as _, &mut reboot_required_tmp)
== win10::idd::FALSE
{
bail!("{}", win10::get_last_msg()?);
}
*_reboot_required = reboot_required_tmp == win10::idd::TRUE;
}
}
Ok(())
}
#[no_mangle]
pub fn is_device_created() -> bool { pub fn is_device_created() -> bool {
#[cfg(windows)] match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
return *H_SW_DEVICE.lock().unwrap() != 0; Ok(lib) => unsafe {
#[cfg(not(windows))] match lib.get::<libloading::Symbol<fn() -> bool>>(b"is_device_created") {
return false; Ok(func) => func(),
Err(..) => false,
}
},
Err(..) => false,
}
} }
#[no_mangle]
pub fn create_device() -> ResultType<()> {
if is_device_created() {
return Ok(());
}
#[cfg(windows)]
unsafe {
let mut lock_device = H_SW_DEVICE.lock().unwrap();
let mut h_sw_device = *lock_device as win10::idd::HSWDEVICE;
if win10::idd::DeviceCreate(&mut h_sw_device) == win10::idd::FALSE {
bail!("{}", win10::get_last_msg()?);
} else {
*lock_device = h_sw_device as u64;
}
}
Ok(())
}
#[no_mangle]
pub fn close_device() { pub fn close_device() {
#[cfg(windows)] match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
unsafe { Ok(lib) => unsafe {
win10::idd::DeviceClose(*H_SW_DEVICE.lock().unwrap() as win10::idd::HSWDEVICE); match lib.get::<libloading::Symbol<fn()>>(b"close_device") {
*H_SW_DEVICE.lock().unwrap() = 0; Ok(func) => func(),
MONITOR_PLUGIN.lock().unwrap().clear(); Err(..) => {}
}
},
Err(..) => {}
} }
} }
#[no_mangle] macro_rules! def_func_result {
pub fn plug_in_monitor() -> ResultType<()> { ($func:ident, $name: tt) => {
#[cfg(windows)] pub fn $func() -> ResultType<()> {
unsafe { match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
let monitor_index = 0 as u32; Ok(lib) => unsafe {
let mut plug_in_monitors = MONITOR_PLUGIN.lock().unwrap(); match lib.get::<libloading::Symbol<fn() -> ResultType<()>>>($name.as_bytes()) {
for i in 0..plug_in_monitors.len() { Ok(func) => func(),
if let Some(d) = plug_in_monitors.get(i) { Err(..) => bail!("Failed to load func {}", $name),
if *d == monitor_index { }
return Ok(()); },
Err(e) => bail!("Failed to load library {}, {}", LIB_NAME_VIRTUAL_DISPLAY, e),
}
} }
}; };
} }
if win10::idd::MonitorPlugIn(monitor_index, 0, 30) == win10::idd::FALSE {
bail!("{}", win10::get_last_msg()?); pub fn install_update_driver(reboot_required: &mut bool) -> ResultType<()> {
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
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"),
} }
(*plug_in_monitors).push(monitor_index); },
Err(e) => bail!("Failed to load library {}, {}", LIB_NAME_VIRTUAL_DISPLAY, e),
} }
Ok(())
} }
#[no_mangle] pub fn uninstall_driver(reboot_required: &mut bool) -> ResultType<()> {
pub fn plug_out_monitor() -> ResultType<()> { match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
#[cfg(windows)] Ok(lib) => unsafe {
unsafe { match lib.get::<libloading::Symbol<fn(&mut bool) -> ResultType<()>>>(b"uninstall_driver") {
let monitor_index = 0 as u32; Ok(func) => func(reboot_required),
if win10::idd::MonitorPlugOut(monitor_index) == win10::idd::FALSE { Err(..) => bail!("Failed to load func uninstall_driver"),
bail!("{}", win10::get_last_msg()?);
} }
let mut plug_in_monitors = MONITOR_PLUGIN.lock().unwrap(); },
for i in 0..plug_in_monitors.len() { Err(e) => bail!("Failed to load library {}, {}", LIB_NAME_VIRTUAL_DISPLAY, e),
if let Some(d) = plug_in_monitors.get(i) {
if *d == monitor_index {
plug_in_monitors.remove(i);
break;
} }
};
}
}
Ok(())
} }
#[no_mangle] def_func_result!(download_driver, "download_driver");
pub fn update_monitor_modes() -> ResultType<()> { def_func_result!(create_device, "create_device");
#[cfg(windows)] def_func_result!(plug_in_monitor, "plug_in_monitor");
unsafe { def_func_result!(plug_out_monitor, "plug_out_monitor");
let monitor_index = 0 as u32; def_func_result!(update_monitor_modes, "update_monitor_modes");
let mut modes = vec![win10::idd::MonitorMode {
width: 1920,
height: 1080,
sync: 60,
}];
if win10::idd::FALSE
== win10::idd::MonitorModesUpdate(
monitor_index as win10::idd::UINT,
modes.len() as win10::idd::UINT,
modes.as_mut_ptr(),
)
{
bail!("{}", win10::get_last_msg()?);
}
}
Ok(())
}

View File

@ -53,8 +53,6 @@ mod connection;
pub mod portable_service; pub mod portable_service;
mod service; mod service;
mod video_qos; mod video_qos;
#[cfg(windows)]
mod virtual_display;
pub mod video_service; pub mod video_service;
use hbb_common::tcp::new_listener; use hbb_common::tcp::new_listener;

View File

@ -42,7 +42,7 @@ use std::{
time::{self, Duration, Instant}, time::{self, Duration, Instant},
}; };
#[cfg(windows)] #[cfg(windows)]
use super::virtual_display; use virtual_display;
pub const SCRAP_UBUNTU_HIGHER_REQUIRED: &str = "Wayland requires Ubuntu 21.04 or higher version."; pub const SCRAP_UBUNTU_HIGHER_REQUIRED: &str = "Wayland requires Ubuntu 21.04 or higher version.";
pub const SCRAP_OTHER_VERSION_OR_X11_REQUIRED: &str = pub const SCRAP_OTHER_VERSION_OR_X11_REQUIRED: &str =

View File

@ -1,64 +0,0 @@
#![allow(dead_code)]
use hbb_common::{bail, ResultType};
use std::sync::{Arc, Mutex};
const LIB_NAME_VIRTUAL_DISPLAY: &str = "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) }))
};
}
pub(super) fn is_device_created() -> bool {
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
match lib.get::<libloading::Symbol<fn() -> bool>>(b"is_device_created") {
Ok(func) => func(),
Err(..) => false,
}
},
Err(..) => false,
}
}
macro_rules! def_func_result {
($func:ident, $name: tt) => {
pub(super) fn $func() -> ResultType<()> {
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 library {}, {}", LIB_NAME_VIRTUAL_DISPLAY, e),
}
}
};
}
def_func_result!(create_device, "create_device");
pub(super) fn close_device() {
match &*LIB_VIRTUAL_DISPLAY.lock().unwrap() {
Ok(lib) => unsafe {
match lib.get::<libloading::Symbol<fn()>>(b"close_device") {
Ok(func) => func(),
Err(..) => {},
}
},
Err(..) => {},
}
}
def_func_result!(plug_in_monitor, "plug_in_monitor");
def_func_result!(plug_out_monitor, "plug_out_monitor");
def_func_result!(update_monitor_modes, "update_monitor_modes");