| 
									
										
										
										
											2022-03-11 19:51:57 +08:00
										 |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | pub mod win10;
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use hbb_common::{bail, lazy_static, ResultType};
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  | use std::{ffi::CString, path::Path, sync::Mutex};
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | lazy_static::lazy_static! {
 | 
					
						
							|  |  |  |     static ref H_SW_DEVICE: Mutex<u64> = Mutex::new(0);
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  | pub fn install_update_driver(_reboot_required: &mut bool) -> ResultType<()> {
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |     #[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)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |     let _full_install_path = match abs_path.to_str() {
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |         Some(p) => CString::new(p)?.into_raw(),
 | 
					
						
							|  |  |  |         None => bail!("{} not exists", install_path),
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |     #[cfg(windows)]
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |     unsafe {
 | 
					
						
							|  |  |  |         {
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:18:57 +08:00
										 |  |  |             let mut reboot_required_tmp = win10::idd::FALSE;
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |             if win10::idd::InstallUpdate(_full_install_path, &mut reboot_required_tmp)
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |                 == win10::idd::FALSE
 | 
					
						
							|  |  |  |             {
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |                 bail!("{}", win10::get_last_msg()?);
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |             *_reboot_required = reboot_required_tmp == win10::idd::TRUE;
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:18:57 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  | pub fn uninstall_driver(_reboot_required: &mut bool) -> ResultType<()> {
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:18:57 +08:00
										 |  |  |     #[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)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |     let _full_install_path = match abs_path.to_str() {
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:18:57 +08:00
										 |  |  |         Some(p) => CString::new(p)?.into_raw(),
 | 
					
						
							|  |  |  |         None => bail!("{} not exists", install_path),
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |     #[cfg(windows)]
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:18:57 +08:00
										 |  |  |     unsafe {
 | 
					
						
							|  |  |  |         {
 | 
					
						
							|  |  |  |             let mut reboot_required_tmp = win10::idd::FALSE;
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |             if win10::idd::Uninstall(_full_install_path, &mut reboot_required_tmp)
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:18:57 +08:00
										 |  |  |                 == win10::idd::FALSE
 | 
					
						
							|  |  |  |             {
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |                 bail!("{}", win10::get_last_msg()?);
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:18:57 +08:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |             *_reboot_required = reboot_required_tmp == win10::idd::TRUE;
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn is_device_created() -> bool {
 | 
					
						
							|  |  |  |     #[cfg(windows)]
 | 
					
						
							|  |  |  |     return *H_SW_DEVICE.lock().unwrap() != 0;
 | 
					
						
							|  |  |  |     #[cfg(not(windows))]
 | 
					
						
							|  |  |  |     return false;
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | pub fn create_device() -> ResultType<()> {
 | 
					
						
							| 
									
										
										
										
											2022-03-13 16:42:31 +08:00
										 |  |  |     if is_device_created() {
 | 
					
						
							|  |  |  |         return Ok(());
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |     unsafe {
 | 
					
						
							|  |  |  |         let mut h_sw_device = *H_SW_DEVICE.lock().unwrap() as win10::idd::HSWDEVICE;
 | 
					
						
							|  |  |  |         if win10::idd::DeviceCreate(&mut h_sw_device) == win10::idd::FALSE {
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |             bail!("{}", win10::get_last_msg()?);
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |         } else {
 | 
					
						
							|  |  |  |             *H_SW_DEVICE.lock().unwrap() = h_sw_device as u64;
 | 
					
						
							|  |  |  |             Ok(())
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | pub fn close_device() {
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							|  |  |  |         win10::idd::DeviceClose(*H_SW_DEVICE.lock().unwrap() as win10::idd::HSWDEVICE);
 | 
					
						
							| 
									
										
										
										
											2022-03-12 20:18:57 +08:00
										 |  |  |         *H_SW_DEVICE.lock().unwrap() = 0;
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | pub fn plug_in_monitor() -> ResultType<()> {
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							|  |  |  |         if win10::idd::MonitorPlugIn(0, 0, 30) == win10::idd::FALSE {
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |             bail!("{}", win10::get_last_msg()?);
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |         Ok(())
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[cfg(windows)]
 | 
					
						
							|  |  |  | pub fn plug_out_monitor() -> ResultType<()> {
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							|  |  |  |         if win10::idd::MonitorPlugOut(0) == win10::idd::FALSE {
 | 
					
						
							| 
									
										
										
										
											2022-03-13 17:08:33 +08:00
										 |  |  |             bail!("{}", win10::get_last_msg()?);
 | 
					
						
							| 
									
										
										
										
											2022-03-12 19:51:55 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |         Ok(())
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |