30 systemdInhibitAvailable_ = isCommandAvailable(
"systemd-inhibit");
31 powerProfilesAvailable_ = isCommandAvailable(
"powerprofilesctl");
33 Logger::info(
"GameSessionService: systemd-inhibit={}, powerprofilesctl={}",
34 systemdInhibitAvailable_ ?
"available" :
"not found",
35 powerProfilesAvailable_ ?
"available" :
"not found");
38GameSessionService::~GameSessionService()
43 uninhibitScreensaver();
45 if (!previousPowerProfile_.empty())
47 restorePowerProfile();
59 std::lock_guard<std::mutex> lock(mutex_);
61 Logger::info(
"GameSessionService: game started (active count: {})", activeGames_);
62 firstGame = (activeGames_ == 1);
69 switchToPerformanceProfile();
77 std::lock_guard<std::mutex> lock(mutex_);
79 if (activeGames_ <= 0)
81 Logger::warn(
"GameSessionService: onGameStopped called but no games are active");
86 Logger::info(
"GameSessionService: game stopped (active count: {})", activeGames_);
87 lastGame = (activeGames_ == 0);
91 uninhibitScreensaver();
92 restorePowerProfile();
98 std::lock_guard<std::mutex> lock(mutex_);
99 return activeGames_ > 0;
104 std::lock_guard<std::mutex> lock(mutex_);
112void GameSessionService::inhibitScreensaver()
114 if (!systemdInhibitAvailable_)
117 "GameSessionService: systemd-inhibit not available, skipping screensaver inhibition");
123 Logger::debug(
"GameSessionService: screensaver already inhibited (pid: {})", inhibitPid_);
130 std::string cmd =
"systemd-inhibit --what=idle --who='Game Launcher' "
131 "--why='Game is running' --mode=block sleep infinity &";
138 "pgrep -n -f 'systemd-inhibit.*Game Launcher.*sleep infinity' 2>/dev/null");
143 inhibitPid_ = std::stoi(pidResult.
output);
144 Logger::info(
"GameSessionService: screensaver inhibited (pid: {})", inhibitPid_);
148 Logger::warn(
"GameSessionService: failed to parse inhibit PID from: {}",
154 Logger::warn(
"GameSessionService: inhibit command succeeded but could not find PID");
159 Logger::warn(
"GameSessionService: failed to inhibit screensaver");
163void GameSessionService::uninhibitScreensaver()
165 if (inhibitPid_ <= 0)
170 Logger::info(
"GameSessionService: releasing screensaver inhibition (pid: {})", inhibitPid_);
173 if (kill(inhibitPid_, SIGTERM) != 0)
176 kill(inhibitPid_, SIGKILL);
186void GameSessionService::switchToPerformanceProfile()
188 if (!powerProfilesAvailable_)
190 Logger::info(
"GameSessionService: powerprofilesctl not available, skipping profile switch");
199 previousPowerProfile_ = current.
output;
200 while (!previousPowerProfile_.empty() &&
201 (previousPowerProfile_.back() ==
'\n' || previousPowerProfile_.back() ==
'\r' ||
202 previousPowerProfile_.back() ==
' '))
204 previousPowerProfile_.pop_back();
209 previousPowerProfile_ =
"balanced";
213 if (previousPowerProfile_ ==
"performance")
215 Logger::info(
"GameSessionService: already on performance profile, no switch needed");
216 previousPowerProfile_.clear();
224 Logger::info(
"GameSessionService: switched power profile to performance (was: {})",
225 previousPowerProfile_);
229 Logger::warn(
"GameSessionService: failed to switch power profile to performance");
230 previousPowerProfile_.clear();
234void GameSessionService::restorePowerProfile()
236 if (!powerProfilesAvailable_ || previousPowerProfile_.empty())
241 Logger::info(
"GameSessionService: restoring power profile to '{}'", previousPowerProfile_);
244 static const std::vector<std::string> validProfiles = {
"performance",
"balanced",
246 if (std::find(validProfiles.begin(), validProfiles.end(), previousPowerProfile_) ==
249 Logger::warn(
"GameSessionService: unknown power profile '{}', skipping restore",
250 previousPowerProfile_);
251 previousPowerProfile_.clear();
255 ProcessResult result =
259 Logger::info(
"GameSessionService: power profile restored to {}", previousPowerProfile_);
263 Logger::warn(
"GameSessionService: failed to restore power profile to {}",
264 previousPowerProfile_);
267 previousPowerProfile_.clear();
274bool GameSessionService::isCommandAvailable(
const std::string& cmd)
static GameSessionService & instance()
Get singleton instance.
GameSessionService(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.
static void info(const std::string &message, Args &&... args)
Log info message (general information).
static void debug(const std::string &message, Args &&... args)
Log debug message (verbose development info).
static void warn(const std::string &message, Args &&... args)
Log warning message (non-critical issue).
static ProcessResult executeWithOutput(const std::string &command, bool silent=false)
Execute command and capture stdout.
static ProcessResult executeBackground(const std::string &command)
Execute command in background (non-blocking).
static ProcessResult execute(const std::string &command, bool silent=false)
Execute command and check exit code.
static std::string escapeArg(const std::string &arg)
Escape a string for safe use as a shell argument.
bool success
true if command executed successfully (exit code 0)
std::string output
Captured stdout (if requested).