| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | pub mod win10;
 | 
					
						
							| 
									
										
										
										
											2023-10-27 20:57:35 +08:00
										 |  |  | use hbb_common::ResultType;
 | 
					
						
							|  |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | use hbb_common::{bail, lazy_static};
 | 
					
						
							| 
									
										
										
										
											2023-06-24 21:09:45 +08:00
										 |  |  | #[cfg(windows)]
 | 
					
						
							| 
									
										
										
										
											2023-10-27 16:19:42 +08:00
										 |  |  | use std::path::PathBuf;
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | use std::sync::Mutex;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[cfg(windows)]
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  | lazy_static::lazy_static! {
 | 
					
						
							|  |  |  |     // If device is uninstalled though "Device Manager" Window.
 | 
					
						
							| 
									
										
										
										
											2023-11-21 10:55:46 +08:00
										 |  |  |     // RustDesk is unable to handle device any more...
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |     static ref H_SW_DEVICE: Mutex<u64> = Mutex::new(0);
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-18 15:51:33 +08:00
										 |  |  | #[no_mangle]
 | 
					
						
							|  |  |  | #[cfg(windows)]
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  | pub fn get_driver_install_path() -> &'static str {
 | 
					
						
							| 
									
										
										
										
											2022-11-18 15:51:33 +08:00
										 |  |  |     win10::DRIVER_INSTALL_PATH
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  | #[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(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-27 20:57:35 +08:00
										 |  |  | #[cfg(windows)]
 | 
					
						
							| 
									
										
										
										
											2023-10-27 16:19:42 +08:00
										 |  |  | fn get_driver_install_abs_path() -> ResultType<PathBuf> {
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |     let install_path = win10::DRIVER_INSTALL_PATH;
 | 
					
						
							| 
									
										
										
										
											2023-10-27 16:19:42 +08:00
										 |  |  |     let exe_file = std::env::current_exe()?;
 | 
					
						
							|  |  |  |     let abs_path = match exe_file.parent() {
 | 
					
						
							|  |  |  |         Some(cur_dir) => cur_dir.join(install_path),
 | 
					
						
							|  |  |  |         None => bail!(
 | 
					
						
							|  |  |  |             "Invalid exe parent for {}",
 | 
					
						
							|  |  |  |             exe_file.to_string_lossy().as_ref()
 | 
					
						
							|  |  |  |         ),
 | 
					
						
							|  |  |  |     };
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |     if !abs_path.exists() {
 | 
					
						
							|  |  |  |         bail!("{} not exists", install_path)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2023-10-27 16:19:42 +08:00
										 |  |  |     Ok(abs_path)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[no_mangle]
 | 
					
						
							|  |  |  | pub fn install_update_driver(_reboot_required: &mut bool) -> ResultType<()> {
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |     #[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);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-27 20:57:35 +08:00
										 |  |  |             let abs_path = get_driver_install_abs_path()?;
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |             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)]
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							|  |  |  |         {
 | 
					
						
							| 
									
										
										
										
											2023-10-27 20:57:35 +08:00
										 |  |  |             let abs_path = get_driver_install_abs_path()?;
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |             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;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[no_mangle]
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  | pub fn plug_in_monitor(_monitor_index: u32, _edid: u32, _retries: u32) -> ResultType<()> {
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |     #[cfg(windows)]
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  |         if win10::idd::MonitorPlugIn(_monitor_index as _, _edid as _, _retries as _)
 | 
					
						
							|  |  |  |             == win10::idd::FALSE
 | 
					
						
							|  |  |  |         {
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |             bail!("{}", win10::get_last_msg()?);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[no_mangle]
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  | pub fn plug_out_monitor(_monitor_index: u32) -> ResultType<()> {
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |     #[cfg(windows)]
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  |         if win10::idd::MonitorPlugOut(_monitor_index) == win10::idd::FALSE {
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |             bail!("{}", win10::get_last_msg()?);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | type PMonitorMode = win10::idd::PMonitorMode;
 | 
					
						
							|  |  |  | #[cfg(not(windows))]
 | 
					
						
							|  |  |  | type PMonitorMode = *mut std::ffi::c_void;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  | #[no_mangle]
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  | pub fn update_monitor_modes(
 | 
					
						
							|  |  |  |     _monitor_index: u32,
 | 
					
						
							|  |  |  |     _mode_count: u32,
 | 
					
						
							|  |  |  |     _modes: PMonitorMode,
 | 
					
						
							|  |  |  | ) -> ResultType<()> {
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |     #[cfg(windows)]
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							|  |  |  |         if win10::idd::FALSE
 | 
					
						
							| 
									
										
										
										
											2023-04-14 21:31:12 +08:00
										 |  |  |             == win10::idd::MonitorModesUpdate(_monitor_index as _, _mode_count as _, _modes)
 | 
					
						
							| 
									
										
										
										
											2022-11-18 14:52:01 +08:00
										 |  |  |         {
 | 
					
						
							|  |  |  |             bail!("{}", win10::get_last_msg()?);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 |