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

View File

@@ -45,7 +45,7 @@ codegen-units = 4
strip = "symbols"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Globalization", "Win32_Security_Cryptography", "Win32_System_SystemInformation", "Win32_Devices_DeviceAndDriverInstallation", "Win32_Graphics_Gdi", "Win32_UI_WindowsAndMessaging", "Win32_System_Registry", "Win32_UI_Shell", "Win32_System_Threading"] }
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Globalization", "Win32_Security_Cryptography", "Win32_System_SystemInformation", "Win32_Devices_DeviceAndDriverInstallation", "Win32_Graphics_Gdi", "Win32_UI_WindowsAndMessaging", "Win32_System_Registry", "Win32_UI_Shell", "Win32_System_Threading", "Win32_System_Performance"] }
[target.'cfg(unix)'.dependencies]
libc = "0.2"

View File

@@ -191,8 +191,9 @@ reaches the screen.
(`managed`), and while starting Strata's last line and how far that line says it got (its
percentage, or `X of Y GB` for the model download; none on a line without progress), also before
the services are up; a failure's reason in `note`.
- `performance`: the performance stage (normal, gaming, paused, and why) comes with game
detection in M7; until then `null`.
- `game`: the game that game mode is on for, `{name, reason}` (below, "Game detection"), or null.
- `performance`: the performance stage (normal, gaming, paused, and why) comes next in M7; until
then `null`.
The dashboard's state is built from the core's events alone (`Dashboard::on_event` with the time),
so it is tested with made-up events.
@@ -326,6 +327,27 @@ the browser runs headless with a note.
- If the monitor or the window cannot be made, the browser runs headless and the dashboard's
`browser` note says why ("가상 모니터 없이 실행: ...").
## Game detection (`app/src/game.rs`)
`docs/v2/pc-features.md`, "게임 감지 기준". Whether a game runs, for the performance stages (next
in M7). While the services run, the app looks at the foreground window every 2 s (Windows only;
elsewhere it never sees a game). It never looks at the game's screen.
- `game` (new section): `detect` (게임 감지, on by default), `games` (programs that are games, as
file names, ".exe" may be left out) and `notGames` (programs that are never games); each list at
most 100 names, no folders. Read when the services start.
- The foreground program is a game, in this order: in `notGames`: no; in `games`: yes
(`registered`); a browser (also the bot's Chrome), video player, the desktop, Discord, a
launcher's own window or this app: no; under a launcher's game folder (`\steamapps\common\`,
`\epic games\`, `\riot games\`, `\battle.net\`): yes (`launcher`, with the launcher's name);
Windows reports a full-screen Direct3D program (`fullscreen`): yes; its window covers one monitor
completely, has no title bar or sizing border, and its process uses at least 10% of the GPU's 3D
engine (`borderless`, the M0 check's rule): yes.
- Once seen, game mode stays until that game's process ends, also while another window is in
front; with `detect` off it is off. The name is the game's folder under the launcher folder, else
the program's name without ".exe". The `game` log tab gets "game mode on: name (reason)" and
"game mode off".
## LLM (`app/src/strata.rs`)
The brain answers with one model, Qwen3.6-35B-A3B UD-Q4_K_XL through Strata (M0). The services
@@ -428,7 +450,7 @@ Plain HTML, CSS and an ES module, no build step, dark theme after `DESIGN.md`. A
Strata's last line, and stop in the header; the header with start, restart and stop is rebuilt
only when the state changes, so a click is never lost to the refresh. Every screen change starts
a new generation: an answer that arrives after the user moved to another screen is dropped.
- **Settings screens** (봇, 채널·호출, 채팅 채널, 대화 기억, 모델, 기능, 백그라운드 수집): built from `settings_fields`; only
- **Settings screens** (봇, 채널·호출, 채팅 채널, 대화 기억, 모델, 기능, 백그라운드 수집, 게임 모드): built from `settings_fields`; only
changed fields are sent; the server and voice channel are pickers (choosing another server clears
the voice channel and lists only that server's channels); the chat channels are a table
with their switches; the bot screen also changes the token; the 기능 screen has the feature
@@ -518,7 +540,11 @@ settings folder and the next install giving one working device, the monitor off
while the bot browser runs and off after, one Chrome window there filling the work area and holding
the bot's tab, the focus staying in the program the user types into, no taskbar button, no Chrome
left after the stop, and the uninstall leaving no device, driver package, registry key or settings
folder. On every system: a removal forgets the driver only when no device is left and the package
folder. Game detection (`tests/game_watch.rs`): a program started from a Steam game folder turns
game mode on with the folder's name and off once it ends. On every system: game detection's rules
(a borderless game, a maximised program, a browser's full-screen video, exclusive full screen,
launcher folders and their own windows, the person's lists first, staying on until the game ends,
detection off) and its settings. A removal forgets the driver only when no device is left and the package
is gone or was not in the store; a failed install forgets it only under the same rule (and never
when it was ours before), reporting both reasons otherwise; a leftover offers removal; driver tasks refused while the services start, and a start (and a
second task) refused while one runs, also after it failed. On Linux the same window test runs on an X display (one window at the given place and

View File

@@ -274,7 +274,8 @@ impl Controller {
view["error"] = json!(error);
view["system"] = json!(self.system().await);
view["llm"] = self.host.llm();
view["performance"] = Value::Null; // game detection comes in M7
view["game"] = status.get("game").cloned().unwrap_or(Value::Null);
view["performance"] = Value::Null; // the performance stages come next in M7
view
}
@@ -362,6 +363,7 @@ impl Running for crate::services::Services {
.map(|(id, name)| json!({"id": id.to_string(), "name": name})).collect::<Vec<_>>(),
"missed": crate::dashboard::missed_view(&missed, now),
"browser": self.browser.view(),
"game": self.game.view(),
})
}
}

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"}));
}
}

View File

@@ -16,6 +16,7 @@ pub mod discord_setup;
pub mod download;
pub mod earcon;
pub mod features;
pub mod game;
pub mod frames;
pub mod install;
pub mod logs;

View File

@@ -23,6 +23,8 @@ pub struct Services {
pub missed: conversation::MissedLog,
pub llm: Arc<Llm>,
pub browser: BrowserRun,
/// Game detection while the services run (admin.spec.md, "Game detection").
pub game: crate::game::GameWatch,
}
/// The bot browser of a run (admin.spec.md, "Bot browser").
@@ -59,6 +61,7 @@ impl BrowserRun {
impl Services {
/// Stop everything; an error means the LLM's server may still run.
pub async fn stop(&self) -> Result<(), String> {
self.game.stop();
self.core.close().await;
self.worker.close();
self.worker_process.stop().await;
@@ -109,6 +112,7 @@ async fn start_browser(data_dir: &Path, settings: &crate::settings::Settings) ->
/// before the brain can. A failure stops what already started.
pub async fn start_services(data_dir: &Path, llm: Arc<Llm>) -> Result<Services, Box<dyn std::error::Error + Send + Sync>> {
let settings = load_settings(&data_dir.join("config").join("settings.json"))?;
let game_settings = settings.game.clone();
// The stored token (the setup wizard keeps it encrypted, admin.spec.md), else the environment.
let secrets = crate::secrets::Secrets::new(&data_dir.join("config").join("secrets.dat"));
let stored = secrets.get(crate::secrets::DISCORD_TOKEN)?;
@@ -189,7 +193,8 @@ pub async fn start_services(data_dir: &Path, llm: Arc<Llm>) -> Result<Services,
return Err(after_stop(&llm, &browser, e.to_string()).await.into());
}
crate::info_log!("app", "services started");
Ok(Services { core, events, worker, worker_process, missed, llm, browser })
let game = crate::game::GameWatch::start(game_settings);
Ok(Services { core, events, worker, worker_process, missed, llm, browser, game })
}
/// The secret values the services get: the bot token and the MCP servers' environment values.

View File

@@ -100,6 +100,15 @@ pub struct FeatureSettings {
pub browser_share: bool,
}
/// Game detection (admin.spec.md, "Game detection"): on by default; the person's own games and
/// programs that are never games, as program file names (".exe" may be left out).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GameSettings {
pub detect: bool,
pub games: Vec<String>,
pub not_games: Vec<String>,
}
/// Background collection (admin.spec.md, "Background collection"); no targets by default.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CollectSettings {
@@ -146,6 +155,7 @@ pub struct Settings {
pub features: FeatureSettings,
pub mcp_servers: Vec<McpServerSettings>,
pub collect: CollectSettings,
pub game: GameSettings,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -192,6 +202,7 @@ impl Default for Settings {
features: FeatureSettings { weather: true, web_search: true, meals: true, collect: true, browser: true, browser_share: false },
mcp_servers: vec![],
collect: CollectSettings { keep_days: 30, targets: vec![] },
game: GameSettings { detect: true, games: vec![], not_games: vec![] },
}
}
}
@@ -516,6 +527,7 @@ pub fn parse_settings(raw: &Value) -> Result<Settings, SettingsError> {
},
mcp_servers: parse_mcp_servers(raw)?,
collect: parse_collect(raw, &d.collect)?,
game: parse_game(raw, &d.game)?,
})
}
@@ -541,6 +553,20 @@ fn get_uint(m: Option<&Map<String, Value>>, key: &str, path: &str, default: u32,
}
}
fn parse_game(raw: &Map<String, Value>, d: &GameSettings) -> Result<GameSettings, SettingsError> {
let g = section(raw, "game")?;
let list = |key: &str, default: &[String]| -> Result<Vec<String>, SettingsError> {
let v: Vec<String> = get_str_list(g, key, "game", default)?.into_iter().map(|e| e.trim().to_owned())
.filter(|e| !e.is_empty()).collect();
if v.len() > 100 || v.iter().any(|e| e.chars().count() > 100 || e.contains(['\\', '/'])) {
return err(format!("game.{key} must be at most 100 program names (no folders)"));
}
Ok(v)
};
Ok(GameSettings { detect: get_bool(g, "detect", "game", d.detect)?, games: list("games", &d.games)?,
not_games: list("notGames", &d.not_games)? })
}
fn parse_collect(raw: &Map<String, Value>, d: &CollectSettings) -> Result<CollectSettings, SettingsError> {
let c = section(raw, "collect")?;
let keep_days = get_uint(c, "keepDays", "collect", d.keep_days, 1, 365)?;
@@ -808,6 +834,16 @@ mod tests {
}
}
#[test]
fn game_detection_settings_are_read_and_checked() {
assert_eq!(parse_settings(&json!({})).unwrap().game, GameSettings { detect: true, games: vec![], not_games: vec![] });
let s = parse_settings(&json!({"game": {"detect": false, "games": [" MyGame.exe ", ""], "notGames": ["obs64"]}})).unwrap();
assert_eq!(s.game, GameSettings { detect: false, games: vec!["MyGame.exe".into()], not_games: vec!["obs64".into()] });
for bad in [json!({"game": {"games": ["C:\\x\\a.exe"]}}), json!({"game": {"notGames": "a"}}), json!({"game": {"detect": 1}})] {
assert!(parse_settings(&bad).unwrap_err().0.contains("game."), "{bad}");
}
}
#[test]
fn features_and_mcp_servers_are_read_and_checked() {
let s = parse_settings(&json!({}));

View File

@@ -21,6 +21,7 @@ pub enum Screen {
Model,
Features,
Collect,
Game,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
@@ -129,6 +130,10 @@ pub fn fields() -> Vec<Field> {
// Background collection (admin.spec.md, "Background collection"): no targets by default.
f("collect.targets", Collect, CollectTargets, json!([]), "수집 대상"),
f("collect.keepDays", Collect, Int { min: 1, max: 365 }, json!(30), "보관 기간(일)"),
// Game detection (admin.spec.md, "Game detection").
f("game.detect", Game, Bool, json!(true), "게임 감지"),
f("game.games", Game, TextList { max_items: 100, nullable: false }, json!([]), "게임으로 볼 프로그램(파일 이름, 예: MyGame.exe)"),
f("game.notGames", Game, TextList { max_items: 100, nullable: false }, json!([]), "게임이 아닌 프로그램(파일 이름)"),
]
}

42
app/tests/game_watch.rs Normal file
View File

@@ -0,0 +1,42 @@
//! Game detection on Windows (admin.spec.md, "Game detection"): a program started from a launcher's
//! game folder turns game mode on with the folder's name, and off once it ends. Runs only with
//! `JARVIS_TEST_GAME_EXE=<a windowed program under ...\steamapps\common\<Game>\>`; elsewhere it skips.
use std::time::{Duration, Instant};
use jarvis_app::game::GameWatch;
use jarvis_app::settings::GameSettings;
fn wait_for(w: &GameWatch, want: impl Fn(&serde_json::Value) -> bool, secs: u64) -> serde_json::Value {
let until = Instant::now() + Duration::from_secs(secs);
loop {
let v = w.view();
if want(&v) || Instant::now() > until {
return v;
}
std::thread::sleep(Duration::from_millis(200));
}
}
#[test]
fn a_launcher_game_turns_game_mode_on_until_it_ends() {
let Ok(exe) = std::env::var("JARVIS_TEST_GAME_EXE") else {
eprintln!("skipped: set JARVIS_TEST_GAME_EXE");
return;
};
let w = GameWatch::start(GameSettings { detect: true, games: vec![], not_games: vec![] });
assert_eq!(w.view(), serde_json::Value::Null, "no game before it starts");
let mut game = std::process::Command::new(&exe).spawn().expect("the game starts");
let t0 = Instant::now();
let on = wait_for(&w, |v| !v.is_null(), 15);
eprintln!("on after {:?}: {on}", t0.elapsed());
let folder = std::path::Path::new(&exe).parent().unwrap().file_name().unwrap().to_string_lossy().into_owned();
assert_eq!(on, serde_json::json!({"name": folder, "reason": {"kind": "launcher", "launcher": "Steam"}}));
game.kill().unwrap();
let _ = game.wait();
let t1 = Instant::now();
let off = wait_for(&w, |v| v.is_null(), 10);
eprintln!("off after {:?}: {off}", t1.elapsed());
assert_eq!(off, serde_json::Value::Null, "game mode ends with the game");
w.stop();
}

View File

@@ -410,6 +410,9 @@ async function displayDriverCard() {
// ---- dashboard ----------------------------------------------------------------------------------
const GAME_REASON = { fullscreen: () => "전체화면", borderless: () => "테두리 없는 창", registered: () => "직접 등록한 게임",
launcher: (r) => `${r.launcher} 게임 폴더` };
/** The header with the start/stop buttons; rebuilt only when the state changes, so a click is not
* lost to the once-a-second refresh of the cards. */
function dashboardHeader(d) {
@@ -466,6 +469,8 @@ function dashboardBody(d) {
h("span", { class: "k" }, "RAM"), h("span", {}, d.system?.ramTotalMb ? `${(d.system.ramUsedMb / 1024).toFixed(1)} / ${(d.system.ramTotalMb / 1024).toFixed(1)} GB` : "–"),
(d.system?.gpus || []).flatMap((g) => [h("span", { class: "k" }, g.name.replace("NVIDIA GeForce ", "")),
h("span", {}, `${g.usePercent}% · VRAM ${(g.vramUsedMb / 1024).toFixed(1)} / ${(g.vramTotalMb / 1024).toFixed(1)} GB`)]),
h("span", { class: "k" }, "게임 모드"), h("span", { class: d.game ? "" : "muted" },
d.game ? `켜짐 (${d.game.name}, ${GAME_REASON[d.game.reason.kind]?.(d.game.reason) ?? d.game.reason.kind})` : "꺼짐"),
h("span", { class: "k" }, "성능 단계"), h("span", { class: "muted" }, d.performance ?? "평소")))),
h("div", { class: "grid" },
h("div", { class: "card" }, h("h2", {}, "실시간 자막"), h("div", { class: "captions" },
@@ -565,7 +570,7 @@ async function show(name) {
if (name === "dashboard") { screen.replaceChildren(); await refresh(); ticker = setInterval(refresh, 1000); return; }
if (name === "logs") { const l = logsScreen(); screen.replaceChildren(l.view); ticker = setInterval(l.tick, 1000); return; }
const titles = { bot: "봇", channels: "채널·호출", chat: "채팅 채널", memory: "대화 기억", model: "모델", features: "기능",
collect: "백그라운드 수집" };
collect: "백그라운드 수집", game: "게임 모드" };
const extra = name === "bot" ? await tokenCard() : name === "features" ? await displayDriverCard() : null;
const view = await settingsScreen(name, titles[name], extra);
if (name === "collect") view.append(await collectResults());

View File

@@ -19,6 +19,7 @@
<button data-screen="model" class="nav">모델</button>
<button data-screen="features" class="nav">기능</button>
<button data-screen="collect" class="nav">백그라운드 수집</button>
<button data-screen="game" class="nav">게임 모드</button>
<button data-screen="logs" class="nav">로그</button>
<div class="side-foot" id="version"></div>
</nav>

File diff suppressed because one or more lines are too long