56 std::map<std::string, std::chrono::steady_clock::time_point>
startTimes_;
80 bool alreadyRunning =
false;
87 std::lock_guard<std::mutex> lock(impl_->mutex_);
90 onLaunchErrorCopy = impl_->onLaunchError_;
91 onStateChangedCopy = impl_->onStateChanged_;
96 alreadyRunning =
true;
97 errorEvent.
gameId = gameId;
105 impl_->startTimes_[gameId] = std::chrono::steady_clock::now();
107 stateEvent.
gameId = gameId;
110 stateEvent.
message =
"Preparing to launch " + game.name;
117 if (onLaunchErrorCopy)
119 onLaunchErrorCopy(errorEvent);
124 if (onStateChangedCopy)
126 onStateChangedCopy(stateEvent);
132 [
this, gameId](
bool success,
const std::string& message)
141 std::lock_guard<std::mutex> lock(impl_->mutex_);
143 impl_->gameStates_.erase(gameId);
144 impl_->startTimes_.erase(gameId);
145 errCb = impl_->onLaunchError_;
152 Logger::error(
"[LaunchController] Launch failed for {}: {}", gameId, message);
177 std::lock_guard<std::mutex> lock(impl_->mutex_);
179 onStateChangedCopy = impl_->onStateChanged_;
181 stateEvent.
gameId = gameId;
184 stateEvent.
message =
"Stopping game...";
187 if (onStateChangedCopy)
189 onStateChangedCopy(stateEvent);
209 std::lock_guard<std::mutex> lock(impl_->mutex_);
211 onStateChangedCopy = impl_->onStateChanged_;
213 stateEvent.
gameId = gameId;
216 stateEvent.
message =
"Force killing game...";
219 if (onStateChangedCopy)
221 onStateChangedCopy(stateEvent);
233 for (
const auto& gameId : runningIds)
255 std::lock_guard<std::mutex> lock(impl_->mutex_);
257 auto it = impl_->gameStates_.find(gameId);
258 if (it != impl_->gameStates_.end())
284 info.
gameName = snap.trackedProcessName.empty() ? gameId : snap.trackedProcessName;
294 std::lock_guard<std::mutex> lock(impl_->mutex_);
295 auto timeIt = impl_->startTimes_.find(gameId);
296 if (timeIt != impl_->startTimes_.end())
302 info.
startTime = std::chrono::steady_clock::now();
305 auto stateIt = impl_->gameStates_.find(gameId);
313 std::vector<RunningGameInfo> result;
316 for (
const auto& gameId : runningIds)
320 result.push_back(*info);
351 return "Not running";
353 return "Starting...";
357 return "Stopping...";
371 if (duration.count() > 0)
377 std::lock_guard<std::mutex> lock(impl_->mutex_);
379 auto it = impl_->startTimes_.find(gameId);
380 if (it == impl_->startTimes_.end())
382 return std::chrono::seconds(0);
385 auto now = std::chrono::steady_clock::now();
386 return std::chrono::duration_cast<std::chrono::seconds>(now - it->second);
391 std::lock_guard<std::mutex> lock(impl_->mutex_);
393 std::chrono::seconds total(0);
394 auto now = std::chrono::steady_clock::now();
396 for (
const auto& [gameId, startTime] : impl_->startTimes_)
400 total += std::chrono::duration_cast<std::chrono::seconds>(now - startTime);
413 std::lock_guard<std::mutex> lock(impl_->mutex_);
414 impl_->onStateChanged_ = std::move(callback);
419 std::lock_guard<std::mutex> lock(impl_->mutex_);
420 impl_->onProcessStarted_ = std::move(callback);
425 std::lock_guard<std::mutex> lock(impl_->mutex_);
426 impl_->onProcessExited_ = std::move(callback);
431 std::lock_guard<std::mutex> lock(impl_->mutex_);
432 impl_->onLaunchError_ = std::move(callback);
445 std::vector<LaunchStateChangedEvent> stateChangedEvents;
446 std::vector<ProcessStartedEvent> processStartedEvents;
447 std::vector<ProcessExitedEvent> processExitedEvents;
453 std::lock_guard<std::mutex> lock(impl_->mutex_);
456 onStateChangedCopy = impl_->onStateChanged_;
457 onProcessStartedCopy = impl_->onProcessStarted_;
458 onProcessExitedCopy = impl_->onProcessExited_;
461 std::vector<std::string> toRemove;
463 for (
auto& [gameId, state] : impl_->gameStates_)
477 stateEvent.
gameId = gameId;
480 stateEvent.
message =
"Game is running";
481 stateChangedEvents.push_back(stateEvent);
487 startEvent.
gameId = gameId;
490 processStartedEvents.push_back(startEvent);
506 auto startIt = impl_->startTimes_.find(gameId);
507 std::chrono::seconds duration(0);
508 if (startIt != impl_->startTimes_.end())
510 auto now = std::chrono::steady_clock::now();
512 std::chrono::duration_cast<std::chrono::seconds>(now - startIt->second);
516 stateEvent.
gameId = gameId;
519 stateEvent.
message =
"Game exited";
520 stateChangedEvents.push_back(stateEvent);
523 exitEvent.
gameId = gameId;
526 processExitedEvents.push_back(exitEvent);
531 toRemove.push_back(gameId);
537 for (
const auto& gameId : toRemove)
539 impl_->gameStates_.erase(gameId);
540 impl_->startTimes_.erase(gameId);
545 if (onStateChangedCopy)
547 for (
const auto& event : stateChangedEvents)
549 onStateChangedCopy(event);
553 if (onProcessStartedCopy)
555 for (
const auto& event : processStartedEvents)
557 onProcessStartedCopy(event);
561 if (onProcessExitedCopy)
563 for (
const auto& event : processExitedEvents)
565 onProcessExitedCopy(event);
void requestLaunch(const Game &game)
static GameLaunchManager & instance()
void setCompletionCallback(CompletionCallback callback)
int getLastExitCode(const std::string &gameId, int defaultCode=0) const
bool stopGame(const std::string &gameId, Game *game=nullptr, bool force=false)
bool isGameRunning(const std::string &gameId) const
std::vector< std::string > getRunningGameIds() const
ProcessSnapshot getRunningProcessSnapshot(const std::string &gameId) const
std::chrono::seconds getGameSessionDuration(const std::string &gameId) const
static GameProcessManager & instance()
static GameSessionService & instance()
Get singleton instance.
void onGameStopped()
Called when a game stops running.
void onGameStarted()
Called when a game starts running.
Complete representation of a game in the library.
static void error(const std::string &message, Args &&... args)
Log error message (recoverable failure).
State getCurrentState() const
static SteamProcessMonitor & instance()
std::map< std::string, std::chrono::steady_clock::time_point > startTimes_
OnProcessExited onProcessExited_
void setupProcessManagerCallbacks()
OnStateChanged onStateChanged_
OnProcessStarted onProcessStarted_
OnLaunchError onLaunchError_
std::map< std::string, LaunchState > gameStates_
std::chrono::seconds getSessionDuration(const std::string &gameId) const
Get current session duration for a running game.
bool stopGame(const std::string &gameId)
Request graceful stop of a running game.
void setOnProcessStarted(OnProcessStarted callback)
void stopAllGames()
Stop all running games.
std::vector< RunningGameInfo > getAllRunningGames() const
Get all currently running games.
void setOnProcessExited(OnProcessExited callback)
bool killGame(const std::string &gameId)
Force kill a running game.
std::function< void(const LaunchStateChangedEvent &)> OnStateChanged
void setOnLaunchError(OnLaunchError callback)
void setOnStateChanged(OnStateChanged callback)
bool launchGame(const std::string &gameId, const Game &game)
Launch a game Returns true if launch was initiated (doesn't mean success yet).
size_t getRunningGameCount() const
Get the number of running games.
std::optional< RunningGameInfo > getRunningGameInfo(const std::string &gameId) const
Get info about a running game (returns nullopt if not running).
std::chrono::seconds getTotalSessionPlayTime() const
Get total play time recorded for this session.
std::string getSteamStateDescription() const
Get Steam state description.
bool hasRunningGames() const
Check if any game is currently running.
std::function< void(const LaunchErrorEvent &)> OnLaunchError
bool isSteamRunning() const
Check if Steam is running.
void requestSteamStart()
Request Steam to start (async).
void update()
Called periodically to update internal state (process monitoring, etc.) UI layer should call this fro...
LaunchState getGameState(const std::string &gameId) const
Get current launch state for a game.
std::function< void(const ProcessExitedEvent &)> OnProcessExited
bool isGameRunning(const std::string &gameId) const
Check if a specific game is running.
std::function< void(const ProcessStartedEvent &)> OnProcessStarted
std::chrono::seconds sessionDuration
std::chrono::steady_clock::time_point startTime
std::string protonVersion
std::string trackedProcessName