Files
EonaCat.XSBox/crates/engine/src/hle/console_window.rs
T
EonaCat 1b0d534e6c
Rust validation / Formatting (push) Failing after 27s
EonaCat/ci-workflows: SonarQube / sonar (push) Successful in 47s
Rust validation / Windows x64 build and tests (push) Canceled after 0s
Initial
2026-12-04 21:03:25 +01:00

83 lines
2.2 KiB
Rust

use windows_sys::Win32::Foundation::{GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE};
use windows_sys::Win32::Storage::FileSystem::{CreateFileW, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING};
use windows_sys::Win32::System::Console::{
AllocConsole, FreeConsole, GetConsoleWindow, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
SetStdHandle,
};
use windows_sys::Win32::UI::WindowsAndMessaging::{
SW_HIDE, SW_RESTORE, SW_SHOW, SetForegroundWindow, ShowWindow,
};
use windows_sys::core::w;
pub fn hide() {
if std::env::var_os("EONACAT_XS_CONSOLE").is_some_and(|v| v == "1") {
return;
}
unsafe {
let hwnd = GetConsoleWindow();
if hwnd.is_null() {
return;
}
ShowWindow(hwnd, SW_HIDE);
}
tracing::info!("console hidden (F5 shows it)");
}
pub fn show() {
unsafe {
let mut hwnd = GetConsoleWindow();
if hwnd.is_null() {
FreeConsole();
if AllocConsole() == 0 {
tracing::warn!("F5: could not open a console window");
return;
}
attach_std_handles();
hwnd = GetConsoleWindow();
if hwnd.is_null() {
return;
}
}
ShowWindow(hwnd, SW_SHOW);
ShowWindow(hwnd, SW_RESTORE);
SetForegroundWindow(hwnd);
}
tracing::info!("console shown (F5)");
}
unsafe fn attach_std_handles() {
unsafe {
let out = CreateFileW(
w!("CONOUT$"),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
std::ptr::null(),
OPEN_EXISTING,
0,
std::ptr::null_mut(),
);
if out != INVALID_HANDLE_VALUE {
SetStdHandle(STD_OUTPUT_HANDLE, out);
SetStdHandle(STD_ERROR_HANDLE, out);
}
let input = CreateFileW(
w!("CONIN$"),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
std::ptr::null(),
OPEN_EXISTING,
0,
std::ptr::null_mut(),
);
if input != INVALID_HANDLE_VALUE {
SetStdHandle(STD_INPUT_HANDLE, input);
}
}
}