mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
refact virtual display
Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
@@ -69,3 +69,6 @@ pub mod rc;
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
pub mod privacy_win_mag;
|
||||
|
||||
#[cfg(all(windows, feature = "virtual_display_driver"))]
|
||||
pub mod virtual_display_manager;
|
||||
|
||||
@@ -19,10 +19,10 @@
|
||||
// https://slhck.info/video/2017/03/01/rate-control.html
|
||||
|
||||
use super::{video_qos::VideoQoS, *};
|
||||
#[cfg(all(windows, feature = "virtual_display_driver"))]
|
||||
use crate::virtual_display_manager;
|
||||
#[cfg(windows)]
|
||||
use crate::{platform::windows::is_process_consent_running, privacy_win_mag};
|
||||
#[cfg(all(windows, feature = "virtual_display_driver"))]
|
||||
use hbb_common::config::LocalConfig;
|
||||
#[cfg(windows)]
|
||||
use hbb_common::get_version_number;
|
||||
use hbb_common::tokio::sync::{
|
||||
@@ -45,11 +45,6 @@ use std::{
|
||||
ops::{Deref, DerefMut},
|
||||
time::{self, Duration, Instant},
|
||||
};
|
||||
#[cfg(all(windows, feature = "virtual_display_driver"))]
|
||||
use virtual_display;
|
||||
|
||||
#[cfg(all(windows, feature = "virtual_display_driver"))]
|
||||
const VIRTUAL_DISPLAY_INDEX_FOR_HEADLESS: u32 = 0;
|
||||
|
||||
pub const SCRAP_UBUNTU_HIGHER_REQUIRED: &str = "Wayland requires Ubuntu 21.04 or higher version.";
|
||||
pub const SCRAP_OTHER_VERSION_OR_X11_REQUIRED: &str =
|
||||
@@ -284,15 +279,8 @@ fn create_capturer(
|
||||
#[cfg(all(windows, feature = "virtual_display_driver"))]
|
||||
fn ensure_close_virtual_device() -> ResultType<()> {
|
||||
let num_displays = Display::all()?.len();
|
||||
if num_displays == 0 {
|
||||
// Device may sometimes be uninstalled by user in "Device Manager" Window.
|
||||
// Closing device will clear the instance data.
|
||||
virtual_display::close_device();
|
||||
} else if num_displays > 1 {
|
||||
// Try close device, if display device changed.
|
||||
if virtual_display::is_device_created() {
|
||||
virtual_display::close_device();
|
||||
}
|
||||
if num_displays > 1 {
|
||||
virtual_display_manager::plug_out_headless();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -938,46 +926,14 @@ fn try_get_displays() -> ResultType<Vec<Display>> {
|
||||
let mut displays = Display::all()?;
|
||||
if displays.len() == 0 {
|
||||
log::debug!("no displays, create virtual display");
|
||||
// Try plugin monitor
|
||||
if LocalConfig::get_virtual_display_num() > 0 {
|
||||
let mut device_already_created = false;
|
||||
if let Err(e) = virtual_display::create_device() {
|
||||
if e.to_string().contains("Device is already created") {
|
||||
device_already_created = true;
|
||||
} else {
|
||||
log::debug!("Create device failed {}", e);
|
||||
}
|
||||
}
|
||||
if device_already_created || virtual_display::is_device_created() {
|
||||
// Reboot is not required for this case.
|
||||
let mut _reboot_required = false;
|
||||
virtual_display::install_update_driver(&mut _reboot_required)?;
|
||||
if let Err(e) = virtual_display::plug_in_monitor(VIRTUAL_DISPLAY_INDEX_FOR_HEADLESS)
|
||||
{
|
||||
log::debug!("Plug in monitor failed {}", e);
|
||||
} else {
|
||||
let modes = [virtual_display::MonitorMode {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
sync: 60,
|
||||
}];
|
||||
if let Err(e) = virtual_display::update_monitor_modes(
|
||||
VIRTUAL_DISPLAY_INDEX_FOR_HEADLESS,
|
||||
&modes,
|
||||
) {
|
||||
log::debug!("Update monitor modes failed {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Err(e) = virtual_display_manager::plug_in_headless() {
|
||||
log::error!("plug in headless failed {}", e);
|
||||
} else {
|
||||
displays = Display::all()?;
|
||||
}
|
||||
} else if displays.len() > 1 {
|
||||
// to-do: do not close if in idd privacy mode.
|
||||
|
||||
// If more than one displays exists, close RustDeskVirtualDisplay
|
||||
if virtual_display::is_device_created() {
|
||||
virtual_display::close_device()
|
||||
}
|
||||
let _res = virtual_display_manager::plug_in_headless();
|
||||
}
|
||||
Ok(displays)
|
||||
}
|
||||
|
||||
106
src/virtual_display_manager.rs
Normal file
106
src/virtual_display_manager.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
use hbb_common::{allow_err, bail, lazy_static, log, ResultType};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
// virtual display index range: 0 - 2 are reserved for headless and other special uses.
|
||||
const VIRTUAL_DISPLAY_INDEX_FOR_HEADLESS: u32 = 0;
|
||||
const VIRTUAL_DISPLAY_START_FOR_PEER: u32 = 3;
|
||||
const VIRTUAL_DISPLAY_MAX_COUNT: u32 = 10;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref VIRTUAL_DISPLAY_MANAGER: Arc<Mutex<VirtualDisplayManager>> =
|
||||
Arc::new(Mutex::new(VirtualDisplayManager::default()));
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct VirtualDisplayManager {
|
||||
headless_index: Option<u32>,
|
||||
peer_required_indices: HashSet<u32>,
|
||||
}
|
||||
|
||||
impl VirtualDisplayManager {
|
||||
fn prepare_driver() -> ResultType<()> {
|
||||
if let Err(e) = virtual_display::create_device() {
|
||||
if !e.to_string().contains("Device is already created") {
|
||||
bail!("Create device failed {}", e);
|
||||
}
|
||||
}
|
||||
// Reboot is not required for this case.
|
||||
let mut _reboot_required = false;
|
||||
allow_err!(virtual_display::install_update_driver(
|
||||
&mut _reboot_required
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn plug_in_monitor(index: u32, modes: &[virtual_display::MonitorMode]) -> ResultType<()> {
|
||||
if let Err(e) = virtual_display::plug_in_monitor(index) {
|
||||
bail!("Plug in monitor failed {}", e);
|
||||
}
|
||||
if let Err(e) = virtual_display::update_monitor_modes(index, &modes) {
|
||||
log::error!("Update monitor modes failed {}", e);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plug_in_headless() -> ResultType<()> {
|
||||
let mut manager = VIRTUAL_DISPLAY_MANAGER.lock().unwrap();
|
||||
VirtualDisplayManager::prepare_driver()?;
|
||||
let modes = [virtual_display::MonitorMode {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
sync: 60,
|
||||
}];
|
||||
VirtualDisplayManager::plug_in_monitor(VIRTUAL_DISPLAY_INDEX_FOR_HEADLESS, &modes)?;
|
||||
manager.headless_index = Some(VIRTUAL_DISPLAY_INDEX_FOR_HEADLESS);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn plug_out_headless() {
|
||||
let mut manager = VIRTUAL_DISPLAY_MANAGER.lock().unwrap();
|
||||
if let Some(index) = manager.headless_index.take() {
|
||||
if let Err(e) = virtual_display::plug_out_monitor(index) {
|
||||
log::error!("Plug out monitor failed {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plug_in_peer_required(
|
||||
modes: Vec<Vec<virtual_display::MonitorMode>>,
|
||||
) -> ResultType<Vec<u32>> {
|
||||
let mut manager = VIRTUAL_DISPLAY_MANAGER.lock().unwrap();
|
||||
VirtualDisplayManager::prepare_driver()?;
|
||||
|
||||
let mut indices: Vec<u32> = Vec::new();
|
||||
for m in modes.iter() {
|
||||
for idx in VIRTUAL_DISPLAY_START_FOR_PEER..VIRTUAL_DISPLAY_MAX_COUNT {
|
||||
if !manager.peer_required_indices.contains(&idx) {
|
||||
match VirtualDisplayManager::plug_in_monitor(idx, m) {
|
||||
Ok(_) => {
|
||||
manager.peer_required_indices.insert(idx);
|
||||
indices.push(idx);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Plug in monitor failed {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(indices)
|
||||
}
|
||||
|
||||
pub fn plug_out_peer_required(modes: &[u32]) -> ResultType<()> {
|
||||
let mut manager = VIRTUAL_DISPLAY_MANAGER.lock().unwrap();
|
||||
for idx in modes.iter() {
|
||||
if manager.peer_required_indices.contains(idx) {
|
||||
allow_err!(virtual_display::plug_out_monitor(*idx));
|
||||
manager.peer_required_indices.remove(idx);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user