feat(app): game detection for the performance stages

- while the services run the app looks at the foreground window every 2 s (Windows): the
  person's notGames and games lists first, never a browser, video player, the desktop, Discord,
  a launcher's own window or this app; a program under a Steam, Epic, Riot or Battle.net game
  folder, exclusive full screen, or a window covering a monitor without title bar or border whose
  process uses at least 10% of the GPU's 3D engine (the M0 rule) is a game
- game mode stays until that game's process ends, also behind other windows; the dashboard shows
  "게임 모드: 켜짐 (name, reason)" and the game log tab its changes
- a 게임 모드 settings screen (detection on/off, the two lists)
- checked on the win-ci VM: a program in a Steam game folder turned it on after 1.4 s and off
  2.0 s after it ended

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-14 09:08:50 +09:00
parent 9b53af169d
commit c9101e0d6a
12 changed files with 588 additions and 9 deletions

456
app/src/game.rs Normal file
View File

@@ -0,0 +1,456 @@
//! Game detection (`docs/v2/pc-features.md`, "게임 감지 기준"; admin.spec.md, "Game detection"):
//! whether a game runs, from the foreground window, Windows' full-screen state and where the
//! program lives. It drives the performance stages (M7). Nothing here looks at the game's screen.
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use serde::Serialize;
use crate::settings::GameSettings;
/// How often the foreground is looked at.
const EVERY: Duration = Duration::from_secs(2);
/// A borderless window counts as a game when its process uses at least this much of the GPU's 3D
/// engine (the M0 `games` check's threshold).
const MIN_GPU_3D: f64 = 10.0;
/// Programs that are never games: browsers (also the bot's Chrome), video players, the desktop,
/// Discord, launchers' own windows and this app.
const NOT_GAMES: &[&str] = &[
"chrome.exe", "msedge.exe", "firefox.exe", "opera.exe", "brave.exe", "whale.exe", "vivaldi.exe",
"msedgewebview2.exe", "vlc.exe", "potplayer.exe", "potplayermini.exe", "potplayermini64.exe", "mpc-hc.exe",
"mpc-hc64.exe", "mpc-be64.exe", "wmplayer.exe", "video.ui.exe", "explorer.exe", "discord.exe", "jarvis.exe",
"steam.exe", "steamwebhelper.exe", "epicgameslauncher.exe", "epicwebhelper.exe", "riotclientservices.exe",
"riot client.exe", "riotclientux.exe", "riotclientuxrender.exe", "leagueclient.exe", "leagueclientux.exe",
"leagueclientuxrender.exe", "battle.net.exe", "agent.exe",
];
/// Launcher game folders: a program under one counts as a game whatever its window looks like.
const LAUNCHERS: &[(&str, &str)] = &[
("\\steamapps\\common\\", "Steam"),
("\\epic games\\", "Epic"),
("\\riot games\\", "Riot"),
("\\battle.net\\", "Battle.net"),
];
/// The foreground window as Windows reports it.
#[derive(Debug, Clone, PartialEq)]
pub struct Foreground {
pub pid: u32,
/// The program's full path.
pub path: String,
/// The window covers one monitor completely.
pub covers_monitor: bool,
/// No title bar and no sizing border.
pub no_frame: bool,
/// The process's use of the GPU's 3D engine, in percent.
pub gpu_3d: f64,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Snapshot {
pub foreground: Option<Foreground>,
/// Windows says a full-screen Direct3D program runs (exclusive full screen).
pub d3d_fullscreen: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", content = "launcher", rename_all = "camelCase")]
pub enum Reason {
Fullscreen,
Borderless,
Launcher(&'static str),
Registered,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Detected {
#[serde(skip)]
pub pid: u32,
pub name: String,
pub reason: Reason,
}
fn file_name(path: &str) -> String {
path.rsplit(['\\', '/']).next().unwrap_or(path).to_lowercase()
}
/// Whether `name` (a program's file name, lower case) is in `list`; list entries may leave out ".exe".
fn listed(name: &str, list: &[String]) -> bool {
list.iter().map(|e| e.trim().to_lowercase()).any(|e| e == name || format!("{e}.exe") == name)
}
/// Why the foreground program is a game, or `None`.
pub fn classify(fg: &Foreground, d3d_fullscreen: bool, s: &GameSettings) -> Option<Reason> {
let name = file_name(&fg.path);
if listed(&name, &s.not_games) {
return None;
}
if listed(&name, &s.games) {
return Some(Reason::Registered);
}
if NOT_GAMES.contains(&name.as_str()) {
return None;
}
let path = fg.path.to_lowercase().replace('/', "\\");
if let Some((_, launcher)) = LAUNCHERS.iter().find(|(dir, _)| path.contains(dir)) {
return Some(Reason::Launcher(launcher));
}
if d3d_fullscreen {
return Some(Reason::Fullscreen);
}
(fg.covers_monitor && fg.no_frame && fg.gpu_3d >= MIN_GPU_3D).then_some(Reason::Borderless)
}
/// A game's name for the dashboard: its folder under a launcher's game folder, else the program's
/// name without ".exe".
pub fn display_name(path: &str) -> String {
let lower = path.to_lowercase().replace('/', "\\");
for (dir, _) in LAUNCHERS {
if let Some(i) = lower.find(dir) {
let rest = &path[i + dir.len()..];
if let Some(folder) = rest.split(['\\', '/']).next().filter(|f| !f.is_empty()) {
return folder.to_owned();
}
}
}
let name = path.rsplit(['\\', '/']).next().unwrap_or(path);
name.strip_suffix(".exe").or_else(|| name.strip_suffix(".EXE")).unwrap_or(name).to_owned()
}
/// The game mode: once a game is seen it stays until its process ends, also while another window
/// is in front (the game still holds its video memory).
#[derive(Default)]
pub struct GameState {
current: Option<Detected>,
}
impl GameState {
/// Look at `snap`; `alive(pid)` says whether a process still runs. Returns the new state when
/// it changed.
pub fn update(&mut self, snap: &Snapshot, s: &GameSettings, alive: impl Fn(u32) -> bool) -> Option<Option<Detected>> {
if !s.detect {
return self.current.take().map(|_| None);
}
if self.current.as_ref().is_some_and(|d| alive(d.pid)) {
return None;
}
let next = snap.foreground.as_ref().and_then(|fg| {
classify(fg, snap.d3d_fullscreen, s).map(|reason| Detected { pid: fg.pid, name: display_name(&fg.path), reason })
});
if next == self.current {
return None;
}
self.current = next.clone();
Some(next)
}
pub fn current(&self) -> Option<&Detected> {
self.current.as_ref()
}
}
/// Watches for games every two seconds while the services run (Windows only; elsewhere it never
/// sees one).
pub struct GameWatch {
state: Arc<Mutex<GameState>>,
stop: Arc<AtomicBool>,
thread: Mutex<Option<std::thread::JoinHandle<()>>>,
}
impl GameWatch {
pub fn start(settings: GameSettings) -> Self {
let (state, stop) = (Arc::new(Mutex::new(GameState::default())), Arc::new(AtomicBool::new(false)));
let thread = (cfg!(windows) && settings.detect).then(|| {
let (state, stop) = (state.clone(), stop.clone());
std::thread::spawn(move || {
let mut probe = probe::Probe::new();
while !stop.load(Ordering::SeqCst) {
let snap = probe.snapshot();
let changed = state.lock().unwrap().update(&snap, &settings, probe::alive);
match changed {
Some(Some(d)) => {
crate::info_log!("game", "game mode on: {} ({:?})", d.name, d.reason);
}
Some(None) => {
crate::info_log!("game", "game mode off");
}
None => {}
}
// Short sleeps, so a stop does not wait for the whole interval.
for _ in 0..(EVERY.as_millis() / 100) {
if stop.load(Ordering::SeqCst) {
break;
}
std::thread::sleep(Duration::from_millis(100));
}
}
})
});
Self { state, stop, thread: Mutex::new(thread) }
}
/// The game for the dashboard: `{name, reason}` or null.
pub fn view(&self) -> serde_json::Value {
serde_json::json!(self.state.lock().unwrap().current())
}
pub fn stop(&self) {
self.stop.store(true, Ordering::SeqCst);
if let Some(t) = self.thread.lock().unwrap().take() {
let _ = t.join();
}
}
}
#[cfg(not(windows))]
mod probe {
use super::Snapshot;
pub struct Probe;
impl Probe {
pub(super) fn new() -> Self {
Probe
}
pub fn snapshot(&mut self) -> Snapshot {
Snapshot::default()
}
}
pub fn alive(_pid: u32) -> bool {
false
}
}
#[cfg(windows)]
mod probe {
use super::{Foreground, Snapshot};
use windows_sys::Win32::Foundation::{CloseHandle, HWND, RECT, WAIT_TIMEOUT};
use windows_sys::Win32::Graphics::Gdi::{GetMonitorInfoW, MonitorFromWindow, MONITORINFO, MONITOR_DEFAULTTONEAREST};
use windows_sys::Win32::System::Performance::*;
use windows_sys::Win32::System::Threading::{
OpenProcess, QueryFullProcessImageNameW, WaitForSingleObject, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION,
PROCESS_SYNCHRONIZE,
};
use windows_sys::Win32::UI::Shell::{SHQueryUserNotificationState, QUNS_RUNNING_D3D_FULL_SCREEN};
use windows_sys::Win32::UI::WindowsAndMessaging::*;
/// The GPU 3D engine counters (PDH), kept open: the rate needs two samples.
pub struct Probe {
query: PDH_HQUERY,
counter: PDH_HCOUNTER,
}
impl Probe {
pub(super) fn new() -> Self {
let mut query: PDH_HQUERY = std::ptr::null_mut();
let mut counter: PDH_HCOUNTER = std::ptr::null_mut();
let path: Vec<u16> = "\\GPU Engine(*engtype_3D)\\Utilization Percentage".encode_utf16().chain(Some(0)).collect();
// SAFETY: plain PDH calls with out pointers to locals; a failure leaves zeros, which
// `gpu_3d` treats as "no counters".
unsafe {
if PdhOpenQueryW(std::ptr::null(), 0, &mut query) == 0 {
PdhAddEnglishCounterW(query, path.as_ptr(), 0, &mut counter);
PdhCollectQueryData(query);
}
}
Self { query, counter }
}
/// 3D engine use of process `pid` in percent, summed over its engines.
fn gpu_3d(&mut self, pid: u32) -> f64 {
if self.query.is_null() || self.counter.is_null() {
return 0.0;
}
let (mut size, mut count) = (0u32, 0u32);
// SAFETY: the documented two-call pattern: the first call asks for the buffer size,
// the second fills a buffer of that many bytes, aligned for the items.
unsafe {
if PdhCollectQueryData(self.query) != 0 {
return 0.0;
}
PdhGetFormattedCounterArrayW(self.counter, PDH_FMT_DOUBLE, &mut size, &mut count, std::ptr::null_mut());
if size == 0 {
return 0.0;
}
let mut buf = vec![0u64; (size as usize).div_ceil(8)];
let items = buf.as_mut_ptr() as *mut PDH_FMT_COUNTERVALUE_ITEM_W;
if PdhGetFormattedCounterArrayW(self.counter, PDH_FMT_DOUBLE, &mut size, &mut count, items) != 0 {
return 0.0;
}
let prefix = format!("pid_{pid}_");
(0..count as usize).map(|i| &*items.add(i))
.filter(|it| wide_str(it.szName).starts_with(&prefix))
.map(|it| it.FmtValue.Anonymous.doubleValue)
.sum()
}
}
pub fn snapshot(&mut self) -> Snapshot {
// SAFETY: plain query.
let mut state = 0;
let d3d_fullscreen = unsafe { SHQueryUserNotificationState(&mut state) } == 0 && state == QUNS_RUNNING_D3D_FULL_SCREEN;
let foreground = foreground().map(|(pid, path, covers_monitor, no_frame)| {
Foreground { pid, path, covers_monitor, no_frame, gpu_3d: self.gpu_3d(pid) }
});
Snapshot { foreground, d3d_fullscreen }
}
}
impl Drop for Probe {
fn drop(&mut self) {
if !self.query.is_null() {
// SAFETY: the query this probe opened.
unsafe { PdhCloseQuery(self.query) };
}
}
}
unsafe fn wide_str(p: *const u16) -> String {
if p.is_null() {
return String::new();
}
let mut n = 0;
// SAFETY: PDH gives NUL-terminated names that live as long as the buffer.
while unsafe { *p.add(n) } != 0 {
n += 1;
}
String::from_utf16_lossy(unsafe { std::slice::from_raw_parts(p, n) })
}
/// The foreground window's process, program path, whether it covers its monitor and whether
/// it has no title bar or sizing border.
fn foreground() -> Option<(u32, String, bool, bool)> {
// SAFETY: plain window queries on the foreground window; buffers are sized as passed and
// the process handle is closed.
unsafe {
let hwnd: HWND = GetForegroundWindow();
if hwnd.is_null() {
return None;
}
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 mut r: RECT = std::mem::zeroed();
GetWindowRect(hwnd, &mut r);
let mut mi = MONITORINFO { cbSize: std::mem::size_of::<MONITORINFO>() as u32, ..Default::default() };
GetMonitorInfoW(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mut mi);
let m = mi.rcMonitor;
let covers = r.left <= m.left && r.top <= m.top && r.right >= m.right && r.bottom >= m.bottom;
let style = GetWindowLongPtrW(hwnd, GWL_STYLE) as u32;
let no_frame = style & WS_CAPTION != WS_CAPTION && style & WS_THICKFRAME == 0;
Some((pid, path, covers, no_frame))
}
}
/// Whether process `pid` still runs.
pub fn alive(pid: u32) -> bool {
// SAFETY: a handle only to wait on, closed at once.
unsafe {
let h = OpenProcess(PROCESS_SYNCHRONIZE, 0, pid);
if h.is_null() {
return false;
}
let running = WaitForSingleObject(h, 0) == WAIT_TIMEOUT;
CloseHandle(h);
running
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn settings() -> GameSettings {
GameSettings { detect: true, games: vec![], not_games: vec![] }
}
fn fg(path: &str, covers: bool, no_frame: bool, gpu: f64) -> Foreground {
Foreground { pid: 7, path: path.into(), covers_monitor: covers, no_frame, gpu_3d: gpu }
}
#[test]
fn a_borderless_game_is_a_game_and_a_maximised_program_or_video_is_not() {
let s = settings();
// A borderless game outside any launcher folder (the M0 check: covers, no frame, GPU >= 10%).
assert_eq!(classify(&fg(r"D:\Games\Hades\Hades.exe", true, true, 45.0), false, &s), Some(Reason::Borderless));
// Too little 3D work: a borderless tool, not a game.
assert_eq!(classify(&fg(r"D:\Tools\kiosk.exe", true, true, 3.0), false, &s), None);
// A maximised program has a title bar.
assert_eq!(classify(&fg(r"C:\Windows\notepad.exe", true, false, 0.0), false, &s), None);
// A browser's full-screen video, even with the GPU busy.
assert_eq!(classify(&fg(r"C:\Program Files\Google\Chrome\Application\chrome.exe", true, true, 60.0), false, &s), None);
// Exclusive full screen.
assert_eq!(classify(&fg(r"D:\Games\Old\old.exe", true, true, 0.0), true, &s), Some(Reason::Fullscreen));
}
#[test]
fn launcher_folders_make_a_game_whatever_the_window_and_name_it_by_its_folder() {
let s = settings();
let valorant = r"C:\Riot Games\VALORANT\live\ShooterGame\Binaries\Win64\VALORANT-Win64-Shipping.exe";
assert_eq!(classify(&fg(valorant, false, false, 0.0), false, &s), Some(Reason::Launcher("Riot")));
assert_eq!(display_name(valorant), "VALORANT");
let steam = r"D:\SteamLibrary\steamapps\common\Stardew Valley\Stardew Valley.exe";
assert_eq!(classify(&fg(steam, false, false, 0.0), false, &s), Some(Reason::Launcher("Steam")));
assert_eq!(display_name(steam), "Stardew Valley");
// The launchers' own windows are not games.
assert_eq!(classify(&fg(r"C:\Riot Games\Riot Client\RiotClientServices.exe", true, true, 20.0), false, &s), None);
assert_eq!(display_name(r"D:\Games\Hades\Hades.exe"), "Hades");
}
#[test]
fn the_persons_lists_come_first() {
let mut s = settings();
s.games = vec!["MyGame".into()];
s.not_games = vec!["Hades.exe".into()];
assert_eq!(classify(&fg(r"D:\x\mygame.exe", false, false, 0.0), false, &s), Some(Reason::Registered));
assert_eq!(classify(&fg(r"D:\Games\Hades\Hades.exe", true, true, 90.0), true, &s), None, "not a game, whatever it does");
s.games = vec!["chrome".into()];
assert_eq!(classify(&fg(r"C:\c\chrome.exe", false, false, 0.0), false, &s), Some(Reason::Registered));
}
#[test]
fn game_mode_stays_until_the_game_ends_and_detection_off_turns_it_off() {
let mut s = settings();
let mut g = GameState::default();
let game = Snapshot { foreground: Some(fg(r"D:\Games\Hades\Hades.exe", true, true, 50.0)), d3d_fullscreen: false };
let desk = Snapshot { foreground: Some(fg(r"C:\Windows\explorer.exe", true, true, 0.0)), d3d_fullscreen: false };
let on = g.update(&game, &s, |_| true).expect("changed").expect("a game");
assert_eq!((on.name.as_str(), on.reason.clone()), ("Hades", Reason::Borderless));
// Alt+Tab to the desktop: still on while the game runs.
assert_eq!(g.update(&desk, &s, |_| true), None);
assert!(g.current().is_some());
// The game ended: off.
assert_eq!(g.update(&desk, &s, |_| false), Some(None));
assert_eq!(g.update(&desk, &s, |_| false), None, "no change, no event");
// Detection off: nothing, and a game on turns off.
g.update(&game, &s, |_| true);
s.detect = false;
assert_eq!(g.update(&game, &s, |_| true), Some(None));
assert_eq!(g.update(&game, &s, |_| true), None);
}
#[test]
fn the_dashboard_gets_the_name_and_the_reason() {
let d = Detected { pid: 1, name: "VALORANT".into(), reason: Reason::Launcher("Riot") };
assert_eq!(serde_json::to_value(&d).unwrap(),
serde_json::json!({"name": "VALORANT", "reason": {"kind": "launcher", "launcher": "Riot"}}));
assert_eq!(serde_json::to_value(Reason::Borderless).unwrap(), serde_json::json!({"kind": "borderless"}));
}
}