dynamic library - win virtual display

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-11-18 13:33:54 +08:00
parent 4a8578ee85
commit 26e8355528
10 changed files with 91 additions and 6 deletions

2
Cargo.lock generated
View File

@ -4391,6 +4391,7 @@ dependencies = [
"lazy_static",
"libappindicator",
"libc",
"libloading",
"libpulse-binding",
"libpulse-simple-binding",
"mac_address",
@ -4424,7 +4425,6 @@ dependencies = [
"trayicon",
"url",
"uuid",
"virtual_display",
"whoami",
"winapi 0.3.9",
"windows-service",

View File

@ -65,6 +65,7 @@ flutter_rust_bridge = { git = "https://github.com/SoLongAndThanksForAllThePizza/
errno = "0.2.8"
rdev = { git = "https://github.com/asur4s/rdev" }
url = { version = "2.1", features = ["serde"] }
libloading = "0.7"
reqwest = { version = "0.11", features = ["blocking", "json", "rustls-tls"], default-features=false }
chrono = "0.4.23"
@ -91,7 +92,6 @@ winit = "0.26"
winapi = { version = "0.3", features = ["winuser"] }
winreg = "0.10"
windows-service = "0.4"
virtual_display = { path = "libs/virtual_display" }
impersonate_system = { git = "https://github.com/21pages/impersonate-system" }
shared_memory = "0.12.4"
shutdown_hooks = "0.1.0"

View File

@ -280,6 +280,7 @@ def build_flutter_windows(version, features):
exit(-1)
os.chdir('flutter')
os.system('flutter build windows --release')
shutil.copy2('target/release/deps/virtual_display.dll', flutter_win_target_dir)
os.chdir('..')
os.chdir('libs/portable')
os.system('pip3 install -r requirements.txt')
@ -316,6 +317,11 @@ def main():
os.system('python3 res/inline-sciter.py')
portable = args.portable
if windows:
# build virtual display dynamic library
os.chdir('libs/virtual_display')
os.system('cargo build --release')
os.chdir('../..')
if flutter:
build_flutter_windows(version, features)
return

View File

@ -18,7 +18,7 @@ fn build_c_impl() {
if build.get_compiler().is_like_msvc() {
build.define("WIN32", "");
// build.define("_AMD64_", "");
build.flag("-Zi");
build.flag("-Z7");
build.flag("-GR-");
// build.flag("-std:c++11");
} else {

View File

@ -3,6 +3,10 @@ name = "virtual_display"
version = "0.1.0"
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
[build-dependencies]

View File

@ -13,7 +13,7 @@ fn build_c_impl() {
if build.get_compiler().is_like_msvc() {
build.define("WIN32", "");
build.flag("-Zi");
build.flag("-Z7");
build.flag("-GR-");
// build.flag("-std:c++11");
} else {
@ -24,7 +24,7 @@ fn build_c_impl() {
}
#[cfg(target_os = "windows")]
build.compile("xxx");
build.compile("win_virtual_display");
#[cfg(target_os = "windows")]
println!("cargo:rerun-if-changed=src/win10/IddController.c");

View File

@ -11,6 +11,7 @@ lazy_static::lazy_static! {
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;
@ -22,6 +23,7 @@ pub fn download_driver() -> ResultType<()> {
Ok(())
}
#[no_mangle]
pub fn install_update_driver(_reboot_required: &mut bool) -> ResultType<()> {
#[cfg(windows)]
let install_path = win10::DRIVER_INSTALL_PATH;
@ -62,6 +64,7 @@ pub fn install_update_driver(_reboot_required: &mut bool) -> ResultType<()> {
Ok(())
}
#[no_mangle]
pub fn uninstall_driver(_reboot_required: &mut bool) -> ResultType<()> {
#[cfg(windows)]
let install_path = win10::DRIVER_INSTALL_PATH;
@ -96,6 +99,7 @@ pub fn uninstall_driver(_reboot_required: &mut bool) -> ResultType<()> {
Ok(())
}
#[no_mangle]
pub fn is_device_created() -> bool {
#[cfg(windows)]
return *H_SW_DEVICE.lock().unwrap() != 0;
@ -103,6 +107,7 @@ pub fn is_device_created() -> bool {
return false;
}
#[no_mangle]
pub fn create_device() -> ResultType<()> {
if is_device_created() {
return Ok(());
@ -120,6 +125,7 @@ pub fn create_device() -> ResultType<()> {
Ok(())
}
#[no_mangle]
pub fn close_device() {
#[cfg(windows)]
unsafe {
@ -129,6 +135,7 @@ pub fn close_device() {
}
}
#[no_mangle]
pub fn plug_in_monitor() -> ResultType<()> {
#[cfg(windows)]
unsafe {
@ -149,6 +156,7 @@ pub fn plug_in_monitor() -> ResultType<()> {
Ok(())
}
#[no_mangle]
pub fn plug_out_monitor() -> ResultType<()> {
#[cfg(windows)]
unsafe {
@ -169,6 +177,7 @@ pub fn plug_out_monitor() -> ResultType<()> {
Ok(())
}
#[no_mangle]
pub fn update_monitor_modes() -> ResultType<()> {
#[cfg(windows)]
unsafe {

View File

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

View File

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

View File

@ -0,0 +1,64 @@
#![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");