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 <keithsim@microsoft.com>
macos-click-through
Keith Simmons 3 years ago committed by GitHub
parent cac8d279b1
commit 2cbdf4ac6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,6 +22,8 @@ mod renderer;
mod running_tracker; mod running_tracker;
mod settings; mod settings;
mod window; mod window;
#[cfg(target_os = "windows")]
mod windows_utils; mod windows_utils;
#[macro_use] #[macro_use]
@ -45,6 +47,7 @@ use window::{create_window, KeyboardSettings, WindowSettings};
pub use channel_utils::*; pub use channel_utils::*;
pub use event_aggregator::*; pub use event_aggregator::*;
pub use running_tracker::*; pub use running_tracker::*;
#[cfg(target_os = "windows")]
pub use windows_utils::*; pub use windows_utils::*;
fn main() { fn main() {
@ -171,6 +174,9 @@ fn maybe_disown() {
return; return;
} }
#[cfg(target_os = "windows")]
windows_detach_from_console();
if let Ok(current_exe) = env::current_exe() { if let Ok(current_exe) = env::current_exe() {
assert!(process::Command::new(current_exe) assert!(process::Command::new(current_exe)
.stdin(process::Stdio::null()) .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..."); 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);
}
}

@ -1,21 +1,22 @@
#[cfg(target_os = "windows")]
use std::{ use std::{
ffi::CString, ffi::CString,
ptr::{null, null_mut}, ptr::{null, null_mut},
}; };
#[cfg(windows)]
use winapi::{ use winapi::{
shared::minwindef::{DWORD, HKEY, MAX_PATH}, shared::{
minwindef::{DWORD, HKEY, MAX_PATH},
windef::DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
},
um::{ um::{
libloaderapi::GetModuleFileNameA, libloaderapi::GetModuleFileNameA,
wincon::{AttachConsole, ATTACH_PARENT_PROCESS}, wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS},
winnt::{KEY_WRITE, REG_OPTION_NON_VOLATILE, REG_SZ}, winnt::{KEY_WRITE, REG_OPTION_NON_VOLATILE, REG_SZ},
winreg::{RegCloseKey, RegCreateKeyExA, RegDeleteTreeA, RegSetValueExA, HKEY_CURRENT_USER}, winreg::{RegCloseKey, RegCreateKeyExA, RegDeleteTreeA, RegSetValueExA, HKEY_CURRENT_USER},
winuser::SetProcessDpiAwarenessContext,
}, },
}; };
#[cfg(target_os = "windows")]
fn get_binary_path() -> String { fn get_binary_path() -> String {
let mut buffer = vec![0u8; MAX_PATH]; let mut buffer = vec![0u8; MAX_PATH];
unsafe { unsafe {
@ -32,7 +33,6 @@ fn get_binary_path() -> String {
} }
} }
#[cfg(target_os = "windows")]
pub fn unregister_rightclick() -> bool { pub fn unregister_rightclick() -> bool {
let str_registry_path_1 = let str_registry_path_1 =
CString::new("Software\\Classes\\Directory\\Background\\shell\\Neovide").unwrap(); 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 { pub fn register_rightclick_directory() -> bool {
let neovide_path = get_binary_path(); let neovide_path = get_binary_path();
let mut registry_key: HKEY = null_mut(); let mut registry_key: HKEY = null_mut();
@ -133,7 +132,6 @@ pub fn register_rightclick_directory() -> bool {
true true
} }
#[cfg(target_os = "windows")]
pub fn register_rightclick_file() -> bool { pub fn register_rightclick_file() -> bool {
let neovide_path = get_binary_path(); let neovide_path = get_binary_path();
let mut registry_key: HKEY = null_mut(); let mut registry_key: HKEY = null_mut();
@ -221,9 +219,21 @@ pub fn register_rightclick_file() -> bool {
true true
} }
pub fn attach_parent_console() { pub fn windows_fix_dpi() {
#[cfg(target_os = "windows")] 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 { unsafe {
AttachConsole(ATTACH_PARENT_PROCESS); AttachConsole(ATTACH_PARENT_PROCESS);
} }
} }
pub fn windows_detach_from_console() {
unsafe {
FreeConsole();
}
}

Loading…
Cancel
Save