From 2cbdf4ac6dccea7a9a4c8dd6c96bb722510d9c75 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Tue, 25 Jan 2022 23:39:12 -0800 Subject: [PATCH] attach and detach the windows console (#1178) * attach and detach the windows console * remove commented code * fix formatting * fix build issues and consolidate windows utils * formatting fix Co-authored-by: Keith Simmons --- src/main.rs | 24 ++++++------------------ src/windows_utils.rs | 30 ++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8cc8eb0..04db52b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,8 @@ mod renderer; mod running_tracker; mod settings; mod window; + +#[cfg(target_os = "windows")] mod windows_utils; #[macro_use] @@ -45,6 +47,7 @@ use window::{create_window, KeyboardSettings, WindowSettings}; pub use channel_utils::*; pub use event_aggregator::*; pub use running_tracker::*; +#[cfg(target_os = "windows")] pub use windows_utils::*; fn main() { @@ -171,6 +174,9 @@ fn maybe_disown() { return; } + #[cfg(target_os = "windows")] + windows_detach_from_console(); + if let Ok(current_exe) = env::current_exe() { assert!(process::Command::new(current_exe) .stdin(process::Stdio::null()) @@ -185,21 +191,3 @@ fn maybe_disown() { eprintln!("error in disowning process, cannot obtain the path for the current executable, continuing without disowning..."); } } - -#[cfg(target_os = "windows")] -fn windows_fix_dpi() { - use winapi::shared::windef::DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2; - use winapi::um::winuser::SetProcessDpiAwarenessContext; - unsafe { - SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); - } -} - -#[cfg(target_os = "windows")] -fn windows_attach_to_console() { - // Attach to parent console tip found here: https://github.com/rust-lang/rust/issues/67159#issuecomment-987882771 - use winapi::um::wincon::{AttachConsole, ATTACH_PARENT_PROCESS}; - unsafe { - AttachConsole(ATTACH_PARENT_PROCESS); - } -} diff --git a/src/windows_utils.rs b/src/windows_utils.rs index 5811461..951bc61 100644 --- a/src/windows_utils.rs +++ b/src/windows_utils.rs @@ -1,21 +1,22 @@ -#[cfg(target_os = "windows")] use std::{ ffi::CString, ptr::{null, null_mut}, }; -#[cfg(windows)] use winapi::{ - shared::minwindef::{DWORD, HKEY, MAX_PATH}, + shared::{ + minwindef::{DWORD, HKEY, MAX_PATH}, + windef::DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, + }, um::{ libloaderapi::GetModuleFileNameA, - wincon::{AttachConsole, ATTACH_PARENT_PROCESS}, + wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS}, winnt::{KEY_WRITE, REG_OPTION_NON_VOLATILE, REG_SZ}, winreg::{RegCloseKey, RegCreateKeyExA, RegDeleteTreeA, RegSetValueExA, HKEY_CURRENT_USER}, + winuser::SetProcessDpiAwarenessContext, }, }; -#[cfg(target_os = "windows")] fn get_binary_path() -> String { let mut buffer = vec![0u8; MAX_PATH]; unsafe { @@ -32,7 +33,6 @@ fn get_binary_path() -> String { } } -#[cfg(target_os = "windows")] pub fn unregister_rightclick() -> bool { let str_registry_path_1 = CString::new("Software\\Classes\\Directory\\Background\\shell\\Neovide").unwrap(); @@ -44,7 +44,6 @@ pub fn unregister_rightclick() -> bool { } } -#[cfg(target_os = "windows")] pub fn register_rightclick_directory() -> bool { let neovide_path = get_binary_path(); let mut registry_key: HKEY = null_mut(); @@ -133,7 +132,6 @@ pub fn register_rightclick_directory() -> bool { true } -#[cfg(target_os = "windows")] pub fn register_rightclick_file() -> bool { let neovide_path = get_binary_path(); let mut registry_key: HKEY = null_mut(); @@ -221,9 +219,21 @@ pub fn register_rightclick_file() -> bool { true } -pub fn attach_parent_console() { - #[cfg(target_os = "windows")] +pub fn windows_fix_dpi() { + unsafe { + SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + } +} + +pub fn windows_attach_to_console() { + // Attach to parent console tip found here: https://github.com/rust-lang/rust/issues/67159#issuecomment-987882771 unsafe { AttachConsole(ATTACH_PARENT_PROCESS); } } + +pub fn windows_detach_from_console() { + unsafe { + FreeConsole(); + } +}