diff --git a/libs/virtual_display/examples/idd_controller.rs b/libs/virtual_display/examples/idd_controller.rs index 0bcb79d66..98e2c2b44 100644 --- a/libs/virtual_display/examples/idd_controller.rs +++ b/libs/virtual_display/examples/idd_controller.rs @@ -104,7 +104,7 @@ fn main() { println!("Create device begin"); if h_sw_device != invalid_device { println!("Device created before"); - break; + continue; } if idd::FALSE == idd::DeviceCreate(&mut h_sw_device) { println!("{}", CStr::from_ptr(idd::GetLastMsg()).to_str().unwrap()); diff --git a/libs/virtual_display/examples/virtual_display_1.rs b/libs/virtual_display/examples/virtual_display_1.rs new file mode 100644 index 000000000..31fdbe06e --- /dev/null +++ b/libs/virtual_display/examples/virtual_display_1.rs @@ -0,0 +1,89 @@ +use std::io::{self, Read}; +use virtual_display; + +fn prompt_input() -> u8 { + println!("Press key execute:"); + println!(" 1. 'x' 1. exit"); + println!(" 2. 'i' 2. install or update driver"); + println!(" 3. 'u' 3. uninstall driver"); + println!(" 4. 'c' 4. create device"); + println!(" 5. 'd' 5. destroy device"); + println!(" 6. '1' 6. plug in monitor 0,1,2"); + println!(" 7. '4' 7. plug out monitor 0,1,2"); + + io::stdin() + .bytes() + .next() + .and_then(|result| result.ok()) + .unwrap_or(0) +} + +fn plug_in() { + println!("Plug in monitor begin"); + if let Err(e) = virtual_display::plug_in_monitor() { + println!("{}", e); + } else { + println!("Plug in monitor done"); + } +} + +fn plug_out() { + println!("Plug out monitor begin"); + if let Err(e) = virtual_display::plug_out_monitor() { + println!("{}", e); + } else { + println!("Plug out monitor done"); + } +} + +fn main() { + loop { + match prompt_input() as char { + 'x' => break, + 'i' => { + println!("Install or update driver begin"); + let mut reboot_required = false; + if let Err(e) = virtual_display::install_update_driver(&mut reboot_required) { + println!("{}", e); + } else { + println!( + "Install or update driver done, reboot is {} required", + if reboot_required { "" } else { "not" } + ); + } + } + 'u' => { + println!("Uninstall driver begin"); + let mut reboot_required = false; + if let Err(e) = virtual_display::uninstall_driver(&mut reboot_required) { + println!("{}", e); + } else { + println!( + "Uninstall driver done, reboot is {} required", + if reboot_required { "" } else { "not" } + ); + } + } + 'c' => { + println!("Create device begin"); + if virtual_display::is_device_created() { + println!("Device created before"); + continue; + } + if let Err(e) = virtual_display::create_device() { + println!("{}", e); + } else { + println!("Create device done"); + } + } + 'd' => { + println!("Close device begin"); + virtual_display::close_device(); + println!("Close device done"); + } + '1' => plug_in(), + '4' => plug_out(), + _ => {} + } + } +} diff --git a/libs/virtual_display/src/lib.rs b/libs/virtual_display/src/lib.rs index 2a37fb586..03b3b59e7 100644 --- a/libs/virtual_display/src/lib.rs +++ b/libs/virtual_display/src/lib.rs @@ -24,7 +24,7 @@ pub fn download_driver() -> ResultType<()> { Ok(()) } -pub fn install_update_driver() -> ResultType<()> { +pub fn install_update_driver(reboot_required: &mut bool) -> ResultType<()> { #[cfg(windows)] let install_path = win10::DRIVER_INSTALL_PATH; #[cfg(not(windows))] @@ -43,12 +43,45 @@ pub fn install_update_driver() -> ResultType<()> { unsafe { #[cfg(windows)] { - let mut reboot_required = win10::idd::FALSE; - if win10::idd::InstallUpdate(full_install_path, &mut reboot_required) + let mut reboot_required_tmp = win10::idd::FALSE; + if win10::idd::InstallUpdate(full_install_path, &mut reboot_required_tmp) == win10::idd::FALSE { bail!("{}", CStr::from_ptr(win10::idd::GetLastMsg()).to_str()?); } + *reboot_required = reboot_required_tmp == win10::idd::TRUE; + } + } + + Ok(()) +} + +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) + } + + let full_install_path = match abs_path.to_str() { + Some(p) => CString::new(p)?.into_raw(), + None => bail!("{} not exists", install_path), + }; + + unsafe { + #[cfg(windows)] + { + let mut reboot_required_tmp = win10::idd::FALSE; + if win10::idd::Uninstall(full_install_path, &mut reboot_required_tmp) + == win10::idd::FALSE + { + bail!("{}", CStr::from_ptr(win10::idd::GetLastMsg()).to_str()?); + } + *reboot_required = reboot_required_tmp == win10::idd::TRUE; } } @@ -79,6 +112,7 @@ pub fn create_device() -> ResultType<()> { pub fn close_device() { unsafe { win10::idd::DeviceClose(*H_SW_DEVICE.lock().unwrap() as win10::idd::HSWDEVICE); + *H_SW_DEVICE.lock().unwrap() = 0; } } diff --git a/src/ui.rs b/src/ui.rs index 6c69f14b5..cc6347b84 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -367,9 +367,13 @@ impl UI { // TODO: ui prompt fn install_virtual_display(&self) { - match virtual_display::install_update_driver() { + let mut reboot_required = false; + match virtual_display::install_update_driver(&mut reboot_required) { Ok(_) => { - log::info!("Virtual Display: install virtual display done"); + log::info!( + "Virtual Display: install virtual display done, reboot is {} required", + if reboot_required { "" } else { "not" } + ); } Err(e) => { log::error!("Virtual Display: install virtual display failed {}", e);