fix(app): only a reading made with no game running is kept as other programs' memory

- a normal placement keeps its reading only when no game runs: the perf loop passes whether a
  game runs to every switch, so the way back from a failed game stage while the game runs keeps
  the reading from before the game
- game detection also looks at every running program once as the services start: a program in
  a launcher's game folder or the person's games list counts, also behind other windows
- checked on the win-ci VM: a minimised program in a Steam game folder is found at the start
  while nothing is in front

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 10:52:48 +09:00
parent 4a81b88d41
commit 5316f4e9b0
6 changed files with 175 additions and 41 deletions

View File

@@ -150,6 +150,16 @@ pub fn classify(fg: &Foreground, d3d_fullscreen: bool, s: &GameSettings, install
(fg.covers_monitor && fg.no_frame && fg.gpu_3d >= MIN_GPU_3D).then_some(Reason::Borderless)
}
/// Whether one of these programs (full paths) is a game by what it is: the person's list or a
/// launcher's game folder. Used once as the services start, for games behind other windows (how a
/// window looks is not known there, so those rules do not apply).
pub fn game_running_among(paths: &[String], s: &GameSettings, installed: &[InstalledGame]) -> bool {
paths.iter().any(|p| {
let fg = Foreground { pid: 0, path: p.clone(), covers_monitor: false, no_frame: false, gpu_3d: 0.0 };
classify(&fg, false, s, installed).is_some()
})
}
/// A game's name for the dashboard: its name from the launcher (the folder under a launcher's game
/// folder, or an installed game's name), else the program's name without ".exe".
pub fn game_name(path: &str, installed: &[InstalledGame]) -> String {
@@ -200,6 +210,8 @@ impl GameState {
/// sees one).
pub struct GameWatch {
state: Arc<Mutex<GameState>>,
/// A game process ran (in front or behind other windows) when the watch started.
background: bool,
stop: Arc<AtomicBool>,
thread: Mutex<Option<std::thread::JoinHandle<()>>>,
}
@@ -209,9 +221,11 @@ impl GameWatch {
/// known at once (`on`).
pub fn start(settings: GameSettings) -> Self {
let (state, stop) = (Arc::new(Mutex::new(GameState::default())), Arc::new(AtomicBool::new(false)));
let mut background = false;
if cfg!(windows) && settings.detect {
let snap = probe::Probe::new().snapshot();
state.lock().unwrap().update(&snap, &settings, probe::alive);
background = game_running_among(&probe::process_paths(), &settings, &snap.installed);
}
let thread = (cfg!(windows) && settings.detect).then(|| {
let (state, stop) = (state.clone(), stop.clone());
@@ -239,7 +253,13 @@ impl GameWatch {
}
})
});
Self { state, stop, thread: Mutex::new(thread) }
Self { state, background, stop, thread: Mutex::new(thread) }
}
/// Whether a game process ran when the watch started, also one behind other windows (found by
/// what the program is: the person's list or a launcher's game folder).
pub fn in_background_at_start(&self) -> bool {
self.background
}
/// Whether game mode is on.
@@ -286,6 +306,10 @@ mod probe {
pub fn alive(_pid: u32) -> bool {
false
}
pub fn process_paths() -> Vec<String> {
vec![]
}
}
#[cfg(windows)]
@@ -453,18 +477,7 @@ mod probe {
}
let mut pid = 0;
GetWindowThreadProcessId(hwnd, &mut pid);
let h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
if h.is_null() {
return None;
}
let mut buf = [0u16; 1024];
let mut len = buf.len() as u32;
let ok = QueryFullProcessImageNameW(h, PROCESS_NAME_WIN32, buf.as_mut_ptr(), &mut len) != 0;
CloseHandle(h);
if !ok {
return None;
}
let path = String::from_utf16_lossy(&buf[..len as usize]);
let path = image_path(pid)?;
let mut r: RECT = std::mem::zeroed();
GetWindowRect(hwnd, &mut r);
let mut mi = MONITORINFO { cbSize: std::mem::size_of::<MONITORINFO>() as u32, ..Default::default() };
@@ -477,6 +490,48 @@ mod probe {
}
}
/// Process `pid`'s program path.
fn image_path(pid: u32) -> Option<String> {
// SAFETY: a query-only handle, closed at once; the buffer is sized as passed.
unsafe {
let h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
if h.is_null() {
return None;
}
let mut buf = [0u16; 1024];
let mut len = buf.len() as u32;
let ok = QueryFullProcessImageNameW(h, PROCESS_NAME_WIN32, buf.as_mut_ptr(), &mut len) != 0;
CloseHandle(h);
ok.then(|| String::from_utf16_lossy(&buf[..len as usize]))
}
}
/// Every running program's path this user may read.
pub fn process_paths() -> Vec<String> {
use windows_sys::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
};
let mut out = vec![];
// SAFETY: a process snapshot walked with a sized entry, then closed.
unsafe {
let snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if snap == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE {
return out;
}
let mut e: PROCESSENTRY32W = std::mem::zeroed();
e.dwSize = std::mem::size_of::<PROCESSENTRY32W>() as u32;
let mut more = Process32FirstW(snap, &mut e) != 0;
while more {
if let Some(p) = image_path(e.th32ProcessID) {
out.push(p);
}
more = Process32NextW(snap, &mut e) != 0;
}
CloseHandle(snap);
}
out
}
/// Whether process `pid` still runs.
pub fn alive(pid: u32) -> bool {
// SAFETY: a handle only to wait on, closed at once.
@@ -562,6 +617,22 @@ mod tests {
assert_eq!(entry("World of Warcraft", "Blizzard Entertainment", "x", ""), None, "no folder, nothing to match");
}
#[test]
fn a_game_running_in_the_background_is_found_by_its_program() {
let s = settings();
let wow = InstalledGame { dir: r"D:\Games\World of Warcraft".into(), name: "World of Warcraft".into(), launcher: "Battle.net" };
// Behind other windows: only what the program is (where it lives, or the person's list).
assert!(game_running_among(&[r"C:\Windows\explorer.exe".into(), r"C:\Riot Games\VALORANT\live\VALORANT.exe".into()], &s, &[]));
assert!(game_running_among(&[r"D:\Games\World of Warcraft\_retail_\Wow.exe".into()], &s, &[wow]));
assert!(!game_running_among(&[r"C:\Windows\explorer.exe".into(), r"C:\Riot Games\Riot Client\RiotClientServices.exe".into(),
r"C:\Program Files\Google\Chrome\Application\chrome.exe".into()], &s, &[]), "launchers and browsers are not games");
let mut mine = settings();
mine.games = vec!["mygame".into()];
assert!(game_running_among(&[r"D:\x\MyGame.exe".into()], &mine, &[]));
mine.not_games = vec!["VALORANT.exe".into()];
assert!(!game_running_among(&[r"C:\Riot Games\VALORANT\live\VALORANT.exe".into()], &mine, &[]));
}
#[test]
fn the_persons_lists_come_first() {
let mut s = settings();