|
@@ -1,8 +1,9 @@
|
|
|
//! The title's life cycle. A console title is never told it is closing:
|
|
//! 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.
|
|
|
|
|
|
|
+//! quitting it or launching a new game first constrains it (its
|
|
|
|
|
+//! constrained-change routines run), then 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(non_snake_case)]
|
|
|
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
|
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
#![allow(clippy::too_many_arguments)]
|
|
@@ -19,21 +20,35 @@ use windows_sys::core::{PCSTR, PCWSTR, w};
|
|
|
|
|
|
|
|
use crate::{HleModule, hle_export};
|
|
use crate::{HleModule, hle_export};
|
|
|
|
|
|
|
|
-type Routine = extern "system" fn(quiesced: u8, context: *mut c_void);
|
|
|
|
|
|
|
+/// A suspend routine (`quiesced`) or a constrained-change routine
|
|
|
|
|
+/// (`constrained`): both take a flag and the title's context.
|
|
|
|
|
+type Routine = extern "system" fn(flag: 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());
|
|
|
|
|
|
|
+/// Registration id, routine and context.
|
|
|
|
|
+type Registration = (usize, Routine, usize);
|
|
|
|
|
|
|
|
-fn routines() -> MutexGuard<'static, Vec<(usize, Routine, usize)>> {
|
|
|
|
|
|
|
+/// Each suspend routine.
|
|
|
|
|
+static ROUTINES: Mutex<Vec<Registration>> = Mutex::new(Vec::new());
|
|
|
|
|
+
|
|
|
|
|
+/// Each constrained-change routine.
|
|
|
|
|
+static CONSTRAINED: Mutex<Vec<Registration>> = Mutex::new(Vec::new());
|
|
|
|
|
+
|
|
|
|
|
+fn routines() -> MutexGuard<'static, Vec<Registration>> {
|
|
|
ROUTINES.lock().unwrap_or_else(|e| e.into_inner())
|
|
ROUTINES.lock().unwrap_or_else(|e| e.into_inner())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+fn constrained() -> MutexGuard<'static, Vec<Registration>> {
|
|
|
|
|
+ CONSTRAINED.lock().unwrap_or_else(|e| e.into_inner())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/// How long the console lets suspend routines run before it ends the title.
|
|
/// How long the console lets suspend routines run before it ends the title.
|
|
|
const SUSPEND_DEADLINE: Duration = Duration::from_secs(1);
|
|
const SUSPEND_DEADLINE: Duration = Duration::from_secs(1);
|
|
|
|
|
|
|
|
const ERROR_INVALID_PARAMETER: u32 = 87;
|
|
const ERROR_INVALID_PARAMETER: u32 = 87;
|
|
|
|
|
|
|
|
-pub extern "system" fn RegisterAppStateChangeNotification(
|
|
|
|
|
|
|
+/// Adds `routine` to `list` and writes its registration id.
|
|
|
|
|
+fn register(
|
|
|
|
|
+ mut list: MutexGuard<'static, Vec<Registration>>,
|
|
|
routine: Option<Routine>,
|
|
routine: Option<Routine>,
|
|
|
context: *mut c_void,
|
|
context: *mut c_void,
|
|
|
registration: *mut usize,
|
|
registration: *mut usize,
|
|
@@ -46,16 +61,38 @@ pub extern "system" fn RegisterAppStateChangeNotification(
|
|
|
}
|
|
}
|
|
|
static NEXT: AtomicUsize = AtomicUsize::new(1);
|
|
static NEXT: AtomicUsize = AtomicUsize::new(1);
|
|
|
let id = NEXT.fetch_add(1, Ordering::Relaxed);
|
|
let id = NEXT.fetch_add(1, Ordering::Relaxed);
|
|
|
- routines().push((id, routine, context as usize));
|
|
|
|
|
|
|
+ list.push((id, routine, context as usize));
|
|
|
// SAFETY: checked non-null; the title passes room for its registration.
|
|
// SAFETY: checked non-null; the title passes room for its registration.
|
|
|
unsafe { registration.write(id) };
|
|
unsafe { registration.write(id) };
|
|
|
0
|
|
0
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+pub extern "system" fn RegisterAppStateChangeNotification(
|
|
|
|
|
+ routine: Option<Routine>,
|
|
|
|
|
+ context: *mut c_void,
|
|
|
|
|
+ registration: *mut usize,
|
|
|
|
|
+) -> u32 {
|
|
|
|
|
+ register(routines(), routine, context, registration)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
pub extern "system" fn UnregisterAppStateChangeNotification(registration: usize) {
|
|
pub extern "system" fn UnregisterAppStateChangeNotification(registration: usize) {
|
|
|
routines().retain(|&(id, ..)| id != registration);
|
|
routines().retain(|&(id, ..)| id != registration);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// The host's own version never calls the routine (a desktop app is never
|
|
|
|
|
+/// constrained), so a title waiting for it would never suspend.
|
|
|
|
|
+pub extern "system" fn RegisterAppConstrainedChangeNotification(
|
|
|
|
|
+ routine: Option<Routine>,
|
|
|
|
|
+ context: *mut c_void,
|
|
|
|
|
+ registration: *mut usize,
|
|
|
|
|
+) -> u32 {
|
|
|
|
|
+ register(constrained(), routine, context, registration)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+pub extern "system" fn UnregisterAppConstrainedChangeNotification(registration: usize) {
|
|
|
|
|
+ constrained().retain(|&(id, ..)| id != registration);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/// The window procedure a title window had before ours.
|
|
/// The window procedure a title window had before ours.
|
|
|
const TITLE_PROC: PCWSTR = w!("victory.wndproc");
|
|
const TITLE_PROC: PCWSTR = w!("victory.wndproc");
|
|
|
|
|
|
|
@@ -201,8 +238,13 @@ pub(crate) fn end_title(why: &'static str, then: Box<dyn FnOnce() + Send>) {
|
|
|
if ENDING.swap(true, Ordering::SeqCst) {
|
|
if ENDING.swap(true, Ordering::SeqCst) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ let constrain = constrained().clone();
|
|
|
let suspend = routines().clone();
|
|
let suspend = routines().clone();
|
|
|
- tracing::info!(routines = suspend.len(), "{why}: suspending the title");
|
|
|
|
|
|
|
+ tracing::info!(
|
|
|
|
|
+ constrained = constrain.len(),
|
|
|
|
|
+ routines = suspend.len(),
|
|
|
|
|
+ "{why}: suspending the title"
|
|
|
|
|
+ );
|
|
|
// Whoever comes first (the routines or the deadline) runs `then`; the
|
|
// Whoever comes first (the routines or the deadline) runs `then`; the
|
|
|
// other waits for it, so the process never ends halfway through.
|
|
// other waits for it, so the process never ends halfway through.
|
|
|
let then = std::sync::Arc::new(Mutex::new(Some(then)));
|
|
let then = std::sync::Arc::new(Mutex::new(Some(then)));
|
|
@@ -218,9 +260,7 @@ pub(crate) fn end_title(why: &'static str, then: Box<dyn FnOnce() + Send>) {
|
|
|
// Not on the calling thread: titles hand the suspend to their window
|
|
// Not on the calling thread: titles hand the suspend to their window
|
|
|
// thread and wait for it inside the routine.
|
|
// thread and wait for it inside the routine.
|
|
|
std::thread::spawn(move || {
|
|
std::thread::spawn(move || {
|
|
|
- for (_, routine, context) in suspend {
|
|
|
|
|
- routine(1, context as *mut c_void);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ constrain_then_suspend(&constrain, &suspend);
|
|
|
finish("title suspended");
|
|
finish("title suspended");
|
|
|
});
|
|
});
|
|
|
std::thread::spawn(move || {
|
|
std::thread::spawn(move || {
|
|
@@ -229,6 +269,16 @@ pub(crate) fn end_title(why: &'static str, then: Box<dyn FnOnce() + Send>) {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// Runs the constrained-change routines (constrained), then the suspend
|
|
|
|
|
+/// routines (quiesced), as the console does when it suspends a title.
|
|
|
|
|
+/// Grand Theft Auto VI's suspend routine waits for a state that only its
|
|
|
|
|
+/// constrained routine brings about; called alone it never returns.
|
|
|
|
|
+fn constrain_then_suspend(constrain: &[Registration], suspend: &[Registration]) {
|
|
|
|
|
+ for &(_, routine, context) in constrain.iter().chain(suspend) {
|
|
|
|
|
+ routine(1, context as *mut c_void);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/// Set in the environment of a relaunched `victory`: an inherited handle
|
|
/// Set in the environment of a relaunched `victory`: an inherited handle
|
|
|
/// of the process it replaces.
|
|
/// of the process it replaces.
|
|
|
const RELAUNCHED_FROM: &str = "VICTORY_RELAUNCHED_FROM";
|
|
const RELAUNCHED_FROM: &str = "VICTORY_RELAUNCHED_FROM";
|
|
@@ -303,6 +353,8 @@ pub(crate) fn module() -> HleModule {
|
|
|
exports: vec![
|
|
exports: vec![
|
|
|
hle_export!(RegisterAppStateChangeNotification),
|
|
hle_export!(RegisterAppStateChangeNotification),
|
|
|
hle_export!(UnregisterAppStateChangeNotification),
|
|
hle_export!(UnregisterAppStateChangeNotification),
|
|
|
|
|
+ hle_export!(RegisterAppConstrainedChangeNotification),
|
|
|
|
|
+ hle_export!(UnregisterAppConstrainedChangeNotification),
|
|
|
hle_export!(CreateWindowExW),
|
|
hle_export!(CreateWindowExW),
|
|
|
hle_export!(CreateWindowExA),
|
|
hle_export!(CreateWindowExA),
|
|
|
],
|
|
],
|
|
@@ -331,6 +383,40 @@ mod tests {
|
|
|
assert_eq!(left, [b]);
|
|
assert_eq!(left, [b]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn constrained_registrations_come_and_go() {
|
|
|
|
|
+ let (mut a, mut b) = (0, 0);
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ RegisterAppConstrainedChangeNotification(Some(noop), std::ptr::null_mut(), &mut a),
|
|
|
|
|
+ 0
|
|
|
|
|
+ );
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ RegisterAppConstrainedChangeNotification(Some(noop), std::ptr::null_mut(), &mut b),
|
|
|
|
|
+ 0
|
|
|
|
|
+ );
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ RegisterAppConstrainedChangeNotification(None, std::ptr::null_mut(), &mut a),
|
|
|
|
|
+ ERROR_INVALID_PARAMETER
|
|
|
|
|
+ );
|
|
|
|
|
+ UnregisterAppConstrainedChangeNotification(a);
|
|
|
|
|
+ let left: Vec<_> = constrained().iter().map(|r| r.0).collect();
|
|
|
|
|
+ assert_eq!(left, [b]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// What `recording_routine` saw: (context, flag) per call, in order.
|
|
|
|
|
+ static CALLS: Mutex<Vec<(usize, u8)>> = Mutex::new(Vec::new());
|
|
|
|
|
+
|
|
|
|
|
+ extern "system" fn recording_routine(flag: u8, context: *mut c_void) {
|
|
|
|
|
+ CALLS.lock().unwrap().push((context as usize, flag));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn constrained_routines_run_before_suspend_routines() {
|
|
|
|
|
+ let r: Routine = recording_routine;
|
|
|
|
|
+ constrain_then_suspend(&[(1, r, 10), (2, r, 11)], &[(3, r, 20), (4, r, 21)]);
|
|
|
|
|
+ assert_eq!(*CALLS.lock().unwrap(), [(10, 1), (11, 1), (20, 1), (21, 1)]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
#[test]
|
|
#[test]
|
|
|
fn oversized_windows_shrink_to_the_screen() {
|
|
fn oversized_windows_shrink_to_the_screen() {
|
|
|
let area = RECT {
|
|
let area = RECT {
|