game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
game_session_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// game_session_service.h
4// ==============================================================================
5// Game session management - screensaver inhibition and power profile switching
6// ==============================================================================
7// When a game is running, this service:
8// - Inhibits the screensaver/idle timeout to prevent interruptions
9// - Switches to the performance power profile for optimal FPS
10//
11// Both are restored when the game exits.
12//
13// ## Implementation
14// - Screensaver inhibition: Uses systemd-inhibit to hold an idle inhibit lock
15// - Power profile: Uses powerprofilesctl to switch to performance mode
16//
17// ## Thread Safety
18// All methods should be called from the same thread (main thread).
19// ==============================================================================
20
21#pragma once
22
23#include <atomic>
24#include <mutex>
25#include <string>
26
27// ==============================================================================
28// GameSessionService Class
29// ==============================================================================
30
31/**
32 * @class GameSessionService
33 * @brief Manages system-level game session state (screensaver, power profile)
34 *
35 * Singleton service that inhibits screensaver and switches power profile
36 * when games are running. Uses reference counting to handle multiple
37 * simultaneous game sessions.
38 */
40{
41 public:
42 /**
43 * @brief Get singleton instance
44 */
46
47 // Non-copyable
50
51 /**
52 * @brief Called when a game starts running
53 *
54 * On first game start:
55 * - Inhibits screensaver via systemd-inhibit
56 * - Switches to performance power profile (if powerprofilesctl available)
57 *
58 * Uses reference counting for multiple simultaneous games.
59 */
60 void onGameStarted();
61
62 /**
63 * @brief Called when a game stops running
64 *
65 * When last game stops:
66 * - Releases screensaver inhibition
67 * - Restores previous power profile
68 */
69 void onGameStopped();
70
71 /**
72 * @brief Check if any inhibitions are currently active
73 */
74 bool isActive() const;
75
76 /**
77 * @brief Get current number of running games being tracked
78 */
79 int activeGameCount() const;
80
81 private:
84
85 // Screensaver inhibition
86 void inhibitScreensaver();
87 void uninhibitScreensaver();
88
89 // Power profile management
90 void switchToPerformanceProfile();
91 void restorePowerProfile();
92
93 // Helper to check if a command is available
94 static bool isCommandAvailable(const std::string& cmd);
95
96 mutable std::mutex mutex_;
97 int activeGames_ = 0; // Reference count of running games
98 // inhibitPid_ is mutated by the screensaver-inhibit helpers, which run
99 // after mutex_ is released (they popen). Making it atomic removes the
100 // torn-read / double-kill race if a start (0->1) and stop (1->0) ever
101 // overlap on different threads. previousPowerProfile_ (a std::string) stays
102 // protected by the service's documented single-thread contract.
103 std::atomic<int> inhibitPid_{-1}; // PID of systemd-inhibit process
104 std::string previousPowerProfile_; // Profile to restore on game exit
105 bool powerProfilesAvailable_ = false; // Whether powerprofilesctl exists
106 bool systemdInhibitAvailable_ = false; // Whether systemd-inhibit exists
107};
static GameSessionService & instance()
Get singleton instance.
GameSessionService(const GameSessionService &)=delete
GameSessionService & operator=(const GameSessionService &)=delete
int activeGameCount() const
Get current number of running games being tracked.
void onGameStopped()
Called when a game stops running.
bool isActive() const
Check if any inhibitions are currently active.
void onGameStarted()
Called when a game starts running.