game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
GameProcess Class Reference

Manages a single running game process. More...

#include <game_process.h>

Public Types

enum class  NotificationType { Info , Warning , Error }
 Type of notification to display. More...
enum class  State {
  NotStarted , Starting , Running , Stopping ,
  Stopped , Error
}
 Current state of the game process. More...

Public Member Functions

 GameProcess (const Game &game)
 Constructor - creates a process for the given game.
 ~GameProcess ()
 Destructor - stops the process if still running.
void attachToRunningProcess (int pid, const std::string &processName="")
 Attach to an already-running process.
const Game & getGame () const
 Get the game associated with this process.
const LaunchInfo & getLastLaunchInfo () const
 Get information about what was applied during the last launch.
int getProcessId () const
 Get the process ID.
double getRunningTimeSeconds () const
 Get how long the process has been running.
State getState () const
 Get current process state.
int getTrackedProcessId () const
 Get the tracked game process ID.
const std::string & getTrackedProcessName () const
 Get the name of the tracked game process.
bool isRunning () const
 Check if process is currently running.
bool kill ()
 Force kill the game process.
bool launch ()
 Launch the game process.
void setOnExit (std::function< void(int exitCode)> callback)
 Set callback for process exit.
void setOnNotification (std::function< void(NotificationType, const std::string &)> callback)
 Set callback for notifications.
void setOnStateChanged (std::function< void(State)> callback)
 Set callback for state changes.
bool stop ()
 Stop the game process gracefully.
void update ()
 Check if process is still running and update statistics.

Protected Member Functions

void notify (NotificationType type, const std::string &message) const

Detailed Description

Manages a single running game process.

Creates and monitors a game process, handling platform-specific launching (native, Wine, Proton) and tracking play time statistics.

Usage Example

Game& myGame = ...; // Get game from library
GameProcess process(myGame);
// Set up callbacks
process.setOnStateChanged([](GameProcess::State newState) {
std::cout << "State changed to: " << (int)newState << std::endl;
});
// Launch the game
if (process.launch()) {
// Game started successfully
while (process.isRunning()) {
process.update(); // Call each frame
// ... other work ...
}
}
GameProcess(const Game &game)
Constructor - creates a process for the given game.
State
Current state of the game process.
Complete representation of a game in the library.
Definition game.h:767

Definition at line 79 of file game_process.h.

Member Enumeration Documentation

◆ NotificationType

enum class GameProcess::NotificationType
strong

Type of notification to display.

Enumerator
Info 
Warning 
Error 

Definition at line 279 of file game_process.h.

◆ State

enum class GameProcess::State
strong

Current state of the game process.

Enumerator
NotStarted 

Process has not been started yet.

Starting 

Process is in the process of starting.

Running 

Process is running normally.

Stopping 

Process is being stopped (graceful shutdown).

Stopped 

Process has stopped (either normally or was killed).

Error 

Process encountered an error during launch.

Definition at line 90 of file game_process.h.

Constructor & Destructor Documentation

◆ GameProcess()

GameProcess::GameProcess ( const Game & game)

Constructor - creates a process for the given game.

Parameters
gameGame to launch (will be copied to prevent dangling references)

Definition at line 279 of file game_process.cpp.

◆ ~GameProcess()

GameProcess::~GameProcess ( )

Destructor - stops the process if still running.

Automatically calls stop() if the process is still running.

Definition at line 287 of file game_process.cpp.

References isRunning(), and stop().

Member Function Documentation

◆ attachToRunningProcess()

void GameProcess::attachToRunningProcess ( int pid,
const std::string & processName = "" )

Attach to an already-running process.

Parameters
pidThe process ID of the running game
processNameOptional name of the executable (e.g., "Wow.exe")

Used to track games that were launched externally (e.g., via Lutris) or were already running when the application started. Sets state to Running and begins tracking.

Definition at line 743 of file game_process.cpp.

References Logger::debug(), Logger::error(), Logger::info(), anonymous_namespace{game_process.cpp}::GameModeAPI::instance(), FrameMetricsService::instance(), parseProcessStartTime(), anonymous_namespace{game_process.cpp}::GameModeAPI::requestStartFor(), and Running.

◆ getGame()

const Game & GameProcess::getGame ( ) const
inline

Get the game associated with this process.

Returns
Reference to the game (copy held by this process)

Definition at line 244 of file game_process.h.

◆ getLastLaunchInfo()

const LaunchInfo & GameProcess::getLastLaunchInfo ( ) const
inline

Get information about what was applied during the last launch.

Returns
LaunchInfo structure with applied optimizations

Definition at line 235 of file game_process.h.

Referenced by GameProcessManager::getRunningProcessSnapshot().

◆ getProcessId()

int GameProcess::getProcessId ( ) const
inline

Get the process ID.

Returns
Process ID, or -1 if not running

Definition at line 199 of file game_process.h.

Referenced by GameProcessManager::getRunningProcessSnapshot().

◆ getRunningTimeSeconds()

double GameProcess::getRunningTimeSeconds ( ) const

Get how long the process has been running.

Returns
Running time in seconds, or 0 if not running

Definition at line 961 of file game_process.cpp.

References isRunning().

◆ getState()

State GameProcess::getState ( ) const
inline

Get current process state.

Returns
Current State enum value

Definition at line 181 of file game_process.h.

◆ getTrackedProcessId()

int GameProcess::getTrackedProcessId ( ) const
inline

Get the tracked game process ID.

Returns
Tracked PID, or -1 if not tracking a separate process

For launcher games (Battle.net, EA App, etc.), this returns the actual game process PID which may differ from getProcessId().

Definition at line 211 of file game_process.h.

Referenced by GameProcessManager::getRunningProcessSnapshot().

◆ getTrackedProcessName()

const std::string & GameProcess::getTrackedProcessName ( ) const
inline

Get the name of the tracked game process.

Returns
Process name (e.g., "Wow.exe"), or empty if not tracked

Definition at line 220 of file game_process.h.

Referenced by GameProcessManager::getRunningProcessSnapshot().

◆ isRunning()

bool GameProcess::isRunning ( ) const
inline

Check if process is currently running.

Returns
True if state is Running

Definition at line 190 of file game_process.h.

References Running.

Referenced by ~GameProcess(), getRunningTimeSeconds(), kill(), and stop().

◆ kill()

bool GameProcess::kill ( )

Force kill the game process.

Returns
True if kill was successful

Forcefully terminates the process:

  • Linux: SIGKILL
  • Windows: TerminateProcess with force flag

Use this only if stop() fails or hangs.

Definition at line 675 of file game_process.cpp.

References Logger::info(), isRunning(), kill(), Stopped, and Logger::warn().

Referenced by kill(), and stop().

◆ launch()

bool GameProcess::launch ( )

Launch the game process.

Returns
True if launch was successful, false on error

Launches the game using the appropriate platform runner:

  • Native: Direct execution via fork/exec (Linux) or CreateProcess (Windows)
  • Wine: Executes through Wine with WINEPREFIX and other settings
  • Proton: Executes through Proton with Steam runtime

On success, the process state becomes Running and startTime is recorded. On failure, the process state becomes Error.

Note
This function can only be called when state is NotStarted or Stopped

Definition at line 373 of file game_process.cpp.

References DOSBox, Error, Logger::error(), ProcessExecutor::executeWithOutput(), ProcessResult::exitCode, Logger::info(), anonymous_namespace{game_process.cpp}::GameModeAPI::instance(), FrameMetricsService::instance(), Native, notify(), NotStarted, ProcessResult::output, Proton, anonymous_namespace{game_process.cpp}::GameModeAPI::requestStartFor(), Running, ScummVM, FrameMetricsService::startCollection(), Starting, Stopped, and Wine.

◆ notify()

void GameProcess::notify ( NotificationType type,
const std::string & message ) const
protected

Definition at line 302 of file game_process.cpp.

Referenced by launch().

◆ setOnExit()

void GameProcess::setOnExit ( std::function< void(int exitCode)> callback)
inline

Set callback for process exit.

Parameters
callbackFunction to call when process exits (receives exit code)

The callback is invoked when the process terminates naturally.

Definition at line 270 of file game_process.h.

◆ setOnNotification()

void GameProcess::setOnNotification ( std::function< void(NotificationType, const std::string &)> callback)
inline

Set callback for notifications.

Parameters
callbackFunction to call when a notification should be displayed

This allows the process to communicate status messages to the UI layer without depending on any specific notification implementation.

Definition at line 293 of file game_process.h.

◆ setOnStateChanged()

void GameProcess::setOnStateChanged ( std::function< void(State)> callback)
inline

Set callback for state changes.

Parameters
callbackFunction to call when state changes

The callback is invoked whenever the process state transitions.

Definition at line 259 of file game_process.h.

◆ stop()

bool GameProcess::stop ( )

Stop the game process gracefully.

Returns
True if stop was initiated successfully

Sends a termination signal to the process:

  • Linux: SIGTERM
  • Windows: TerminateProcess

Updates game statistics (play time, last played) before stopping. State becomes Stopped after this call.

Definition at line 590 of file game_process.cpp.

References Logger::info(), FrameMetricsService::instance(), isRunning(), kill(), FrameMetricsService::stopCollection(), Stopped, Stopping, and Logger::warn().

Referenced by ~GameProcess().

◆ update()

void GameProcess::update ( )

Check if process is still running and update statistics.

This should be called every frame (or at least regularly) to:

  • Check if the process has exited
  • Update play time statistics
  • Trigger exit callbacks if process stopped

Automatically transitions state to Stopped if process has exited.

Definition at line 993 of file game_process.cpp.

References Logger::info(), FrameMetricsService::instance(), Running, FrameMetricsService::stopCollection(), Stopped, and Logger::warn().


The documentation for this class was generated from the following files: