| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- //! The title's life cycle. A console title is never told it is closing:
- //! quitting it or launching a new game first suspends it (its suspend
- //! routines run, and it has a second to save) and then ends the process.
- //! Closing the title's window does the same here; a new launch then starts
- //! `victory` again, which waits for this process to be gone.
- #![allow(non_snake_case)]
- #![allow(clippy::not_unsafe_ptr_arg_deref)]
- #![allow(clippy::too_many_arguments)]
- use std::ffi::c_void;
- use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
- use std::sync::{Mutex, MutexGuard};
- use std::time::Duration;
- use windows_sys::Win32::Foundation::{HINSTANCE, HWND, LPARAM, LRESULT, RECT, WPARAM};
- use windows_sys::Win32::System::Threading::{GetCurrentProcess, TerminateProcess};
- use windows_sys::Win32::UI::WindowsAndMessaging::{self as wm, HMENU};
- use windows_sys::core::{PCSTR, PCWSTR, w};
- use crate::{HleModule, hle_export};
- type Routine = extern "system" fn(quiesced: u8, context: *mut c_void);
- /// Registration id, routine and context of each suspend routine.
- static ROUTINES: Mutex<Vec<(usize, Routine, usize)>> = Mutex::new(Vec::new());
- fn routines() -> MutexGuard<'static, Vec<(usize, Routine, usize)>> {
- ROUTINES.lock().unwrap_or_else(|e| e.into_inner())
- }
- /// How long the console lets suspend routines run before it ends the title.
- const SUSPEND_DEADLINE: Duration = Duration::from_secs(1);
- const ERROR_INVALID_PARAMETER: u32 = 87;
- pub extern "system" fn RegisterAppStateChangeNotification(
- routine: Option<Routine>,
- context: *mut c_void,
- registration: *mut usize,
- ) -> u32 {
- let Some(routine) = routine else {
- return ERROR_INVALID_PARAMETER;
- };
- if registration.is_null() {
- return ERROR_INVALID_PARAMETER;
- }
- static NEXT: AtomicUsize = AtomicUsize::new(1);
- let id = NEXT.fetch_add(1, Ordering::Relaxed);
- routines().push((id, routine, context as usize));
- // SAFETY: checked non-null; the title passes room for its registration.
- unsafe { registration.write(id) };
- 0
- }
- pub extern "system" fn UnregisterAppStateChangeNotification(registration: usize) {
- routines().retain(|&(id, ..)| id != registration);
- }
- /// The window procedure a title window had before ours.
- const TITLE_PROC: PCWSTR = w!("victory.wndproc");
- pub extern "system" fn CreateWindowExW(
- ex_style: u32,
- class: PCWSTR,
- name: PCWSTR,
- style: u32,
- x: i32,
- y: i32,
- width: i32,
- height: i32,
- parent: HWND,
- menu: HMENU,
- instance: HINSTANCE,
- param: *const c_void,
- ) -> HWND {
- let (x, y, width, height) = fit(parent, x, y, width, height);
- // SAFETY: the title's own arguments, forwarded (the size fitted).
- let hwnd = unsafe {
- wm::CreateWindowExW(
- ex_style, class, name, style, x, y, width, height, parent, menu, instance, param,
- )
- };
- hook(hwnd);
- hwnd
- }
- /// See [`CreateWindowExW`].
- pub extern "system" fn CreateWindowExA(
- ex_style: u32,
- class: PCSTR,
- name: PCSTR,
- style: u32,
- x: i32,
- y: i32,
- width: i32,
- height: i32,
- parent: HWND,
- menu: HMENU,
- instance: HINSTANCE,
- param: *const c_void,
- ) -> HWND {
- let (x, y, width, height) = fit(parent, x, y, width, height);
- // SAFETY: the title's own arguments, forwarded (the size fitted).
- let hwnd = unsafe {
- wm::CreateWindowExA(
- ex_style, class, name, style, x, y, width, height, parent, menu, instance, param,
- )
- };
- hook(hwnd);
- hwnd
- }
- /// A console title sizes its window to its render target (Grand Theft
- /// Auto VI: 3840x2160 on a Series X), which can exceed the host's screen.
- /// A top-level window larger than the work area is shrunk to fit, keeping
- /// its proportions, and centred; what it shows is scaled to it (the swap
- /// chain stretches).
- fn fit(parent: HWND, x: i32, y: i32, width: i32, height: i32) -> (i32, i32, i32, i32) {
- let mut area = RECT::default();
- // SAFETY: `area` receives the primary monitor's work area.
- let known =
- unsafe { wm::SystemParametersInfoW(wm::SPI_GETWORKAREA, 0, (&raw mut area).cast(), 0) };
- let fitted = fit_into(&area, width, height);
- if !parent.is_null() || known == 0 || fitted == (width, height) {
- return (x, y, width, height);
- }
- tracing::info!(width, height, to = ?fitted, "title window fitted to the screen");
- let (w, h) = fitted;
- let left = area.left + (area.right - area.left - w) / 2;
- let top = area.top + (area.bottom - area.top - h) / 2;
- (left, top, w, h)
- }
- /// `width` x `height` scaled down to fit `area`, or unchanged if it fits
- /// (or is `CW_USEDEFAULT`).
- fn fit_into(area: &RECT, width: i32, height: i32) -> (i32, i32) {
- let (aw, ah) = (area.right - area.left, area.bottom - area.top);
- if width <= 0 || height <= 0 || aw <= 0 || ah <= 0 || (width <= aw && height <= ah) {
- return (width, height);
- }
- let scale = f64::min(
- f64::from(aw) / f64::from(width),
- f64::from(ah) / f64::from(height),
- );
- (
- (f64::from(width) * scale) as i32,
- (f64::from(height) * scale) as i32,
- )
- }
- /// Puts our procedure in front of the title's.
- fn hook(hwnd: HWND) {
- if hwnd.is_null() {
- return;
- }
- // SAFETY: a window this thread just created. The title's procedure is
- // stored before ours can run.
- unsafe {
- let title = wm::GetWindowLongPtrW(hwnd, wm::GWLP_WNDPROC);
- wm::SetPropW(hwnd, TITLE_PROC, title as *mut c_void);
- wm::SetWindowLongPtrW(hwnd, wm::GWLP_WNDPROC, window_proc as *const () as isize);
- }
- }
- extern "system" fn window_proc(
- hwnd: HWND,
- message: u32,
- wparam: WPARAM,
- lparam: LPARAM,
- ) -> LRESULT {
- // A user close (close button, Alt+F4, window or taskbar menu); a
- // console title never sees one, nor a WM_CLOSE from it.
- if message == wm::WM_SYSCOMMAND && wparam & 0xFFF0 == wm::SC_CLOSE as usize {
- end_title("window closed", Box::new(|| {}));
- return 0;
- }
- // SAFETY: the procedure `hook` stored, called the way the system would.
- unsafe {
- let title = wm::GetPropW(hwnd, TITLE_PROC);
- if message == wm::WM_NCDESTROY {
- wm::RemovePropW(hwnd, TITLE_PROC);
- }
- let title = std::mem::transmute::<*mut c_void, wm::WNDPROC>(title);
- wm::CallWindowProcW(title, hwnd, message, wparam, lparam)
- }
- }
- /// Suspends the title as the console does when it ends it, runs `then`
- /// (once), then ends the process. Returns at once; later calls do nothing.
- pub(crate) fn end_title(why: &'static str, then: Box<dyn FnOnce() + Send>) {
- static ENDING: AtomicBool = AtomicBool::new(false);
- if ENDING.swap(true, Ordering::SeqCst) {
- return;
- }
- let suspend = routines().clone();
- tracing::info!(routines = suspend.len(), "{why}: suspending the title");
- // Whoever comes first (the routines or the deadline) runs `then`; the
- // other waits for it, so the process never ends halfway through.
- let then = std::sync::Arc::new(Mutex::new(Some(then)));
- let finish = move |how: &str| {
- let mut then = then.lock().unwrap_or_else(|e| e.into_inner());
- tracing::info!("{how}; ending the title");
- if let Some(then) = then.take() {
- then();
- }
- end();
- };
- let late = finish.clone();
- // Not on the calling thread: titles hand the suspend to their window
- // thread and wait for it inside the routine.
- std::thread::spawn(move || {
- for (_, routine, context) in suspend {
- routine(1, context as *mut c_void);
- }
- finish("title suspended");
- });
- std::thread::spawn(move || {
- std::thread::sleep(SUSPEND_DEADLINE);
- late("title did not finish suspending in time");
- });
- }
- /// Set in the environment of a relaunched `victory`: an inherited handle
- /// of the process it replaces.
- const RELAUNCHED_FROM: &str = "VICTORY_RELAUNCHED_FROM";
- /// Starts `victory` again with `args` (after the title has suspended); the
- /// new process waits until this one has ended.
- pub(crate) fn relaunch(args: Vec<std::ffi::OsString>) {
- use windows_sys::Win32::Foundation::{DUPLICATE_SAME_ACCESS, DuplicateHandle, HANDLE};
- let mut me: HANDLE = std::ptr::null_mut();
- // SAFETY: an inheritable duplicate of our own process handle.
- let duplicated = unsafe {
- DuplicateHandle(
- GetCurrentProcess(),
- GetCurrentProcess(),
- GetCurrentProcess(),
- &mut me,
- 0,
- 1,
- DUPLICATE_SAME_ACCESS,
- )
- } != 0;
- let spawned = std::env::current_exe().and_then(|exe| {
- let mut command = std::process::Command::new(exe);
- command.args(&args);
- if duplicated {
- command.env(RELAUNCHED_FROM, (me as usize).to_string());
- }
- command.spawn()
- });
- match spawned {
- Ok(child) => tracing::info!(pid = child.id(), ?args, "relaunched"),
- Err(e) => tracing::warn!(%e, "relaunch failed; the title just ends"),
- }
- }
- /// In a relaunched `victory`: waits (30 s at most) for the process it
- /// replaces to be gone, so the two never share the title's files.
- pub fn wait_for_previous() {
- use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};
- use windows_sys::Win32::System::Threading::WaitForSingleObject;
- let Some(handle) = std::env::var(RELAUNCHED_FROM)
- .ok()
- .and_then(|h| h.parse::<usize>().ok())
- else {
- return;
- };
- // SAFETY: removing our own variable before any other thread reads it.
- unsafe { std::env::remove_var(RELAUNCHED_FROM) };
- let handle = handle as HANDLE;
- // SAFETY: the inherited process handle the previous process gave us.
- unsafe {
- WaitForSingleObject(handle, 30_000);
- CloseHandle(handle);
- }
- }
- fn end() {
- // Compiled host pipelines kept for the next run.
- #[cfg(windows)]
- victory_gpu::pipeline::save_pipeline_cache();
- // SAFETY: ends this process, as the console ends a suspended title.
- unsafe { TerminateProcess(GetCurrentProcess(), 0) };
- }
- pub(crate) fn module() -> HleModule {
- HleModule {
- name: "lifecycle",
- dlls: &[
- "api-ms-win-core-psm-appnotify-*",
- "ext-ms-win-rtcore-ntuser-window-*",
- ],
- exports: vec![
- hle_export!(RegisterAppStateChangeNotification),
- hle_export!(UnregisterAppStateChangeNotification),
- hle_export!(CreateWindowExW),
- hle_export!(CreateWindowExA),
- ],
- }
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- extern "system" fn noop(_: u8, _: *mut c_void) {}
- #[test]
- fn registrations_come_and_go() {
- let (mut a, mut b) = (0, 0);
- assert_eq!(
- RegisterAppStateChangeNotification(Some(noop), std::ptr::null_mut(), &mut a),
- 0
- );
- assert_eq!(
- RegisterAppStateChangeNotification(Some(noop), std::ptr::null_mut(), &mut b),
- 0
- );
- UnregisterAppStateChangeNotification(a);
- let left: Vec<_> = routines().iter().map(|r| r.0).collect();
- assert_eq!(left, [b]);
- }
- #[test]
- fn oversized_windows_shrink_to_the_screen() {
- let area = RECT {
- left: 0,
- top: 0,
- right: 1920,
- bottom: 1040,
- };
- assert_eq!(fit_into(&area, 3840, 2160), (1848, 1040));
- assert_eq!(fit_into(&area, 1280, 720), (1280, 720));
- assert_eq!(
- fit_into(&area, wm::CW_USEDEFAULT, 0),
- (wm::CW_USEDEFAULT, 0)
- );
- }
- #[test]
- fn hooked_windows_still_reach_the_title_procedure() {
- let null = std::ptr::null_mut();
- let hwnd = CreateWindowExW(
- 0,
- w!("STATIC"),
- w!("before"),
- 0,
- 0,
- 0,
- 10,
- 10,
- null,
- null,
- null,
- std::ptr::null(),
- );
- assert!(!hwnd.is_null());
- let mut text = [0u16; 16];
- // SAFETY: our window; the buffer holds 16 characters.
- unsafe {
- wm::SetWindowTextW(hwnd, w!("after"));
- let n = wm::GetWindowTextW(hwnd, text.as_mut_ptr(), 16);
- assert_eq!(String::from_utf16_lossy(&text[..n as usize]), "after");
- assert!(!wm::GetPropW(hwnd, TITLE_PROC).is_null());
- wm::DestroyWindow(hwnd);
- }
- }
- }
|