game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
steam_monitor.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3
4#include <atomic>
5#include <chrono>
6#include <condition_variable>
7#include <functional>
8#include <mutex>
9#include <thread>
10
11/**
12 * @brief Multi-threaded Steam process monitor
13 *
14 * Monitors Steam process state in a dedicated thread and provides
15 * callbacks for state transitions. Handles Steam start/stop requests
16 * and verifies completion by watching the actual process.
17 */
18class SteamProcessMonitor
19{
20 public:
21 enum class State
22 {
23 NOT_RUNNING, // Steam process not detected
24 STARTING, // Steam start requested, waiting for process
25 RUNNING, // Steam process detected and stable
26 STOPPING, // Steam shutdown requested, waiting for process to exit
27 ERROR // Timeout or error occurred
28 };
29
30 // Callback type for state changes
31 using StateCallback = std::function<void(State oldState, State newState)>;
32
33 // ==========================================================================
34 // Singleton Access
35 // ==========================================================================
36
37 static SteamProcessMonitor& instance();
38
39 // ==========================================================================
40 // Control Methods
41 // ==========================================================================
42
43 // Start the monitoring thread
44 void startMonitoring();
45
46 // Stop the monitoring thread
47 void stopMonitoring();
48
49 // Request Steam to start (transitions to STARTING state)
50 void requestSteamStart();
51
52 // Request Steam to shut down (transitions to STOPPING state)
54
55 // Wait for Steam to reach a specific state (blocking, with timeout)
56 // Returns true if state reached, false if timeout
57 bool waitForState(State targetState, int timeoutMs = 10000);
58
59 // ==========================================================================
60 // State Query
61 // ==========================================================================
62
63 State getCurrentState() const;
64 bool isSteamRunning() const;
65
66 // Check if Steam is fully initialized (not just process running)
67 // This verifies Steam can accept commands by checking for the Steam IPC socket
68 bool isSteamFullyInitialized() const;
69
70 // Wait for Steam to be fully initialized (blocking, with timeout)
71 // Returns true if initialized, false if timeout
72 bool waitForSteamInitialized(int timeoutMs = 10000);
73
74 // ==========================================================================
75 // Callbacks
76 // ==========================================================================
77
78 void setStateCallback(StateCallback callback);
79
80 private:
81 // Singleton - private constructor
82 SteamProcessMonitor();
83 ~SteamProcessMonitor();
84 SteamProcessMonitor(const SteamProcessMonitor&) = delete;
85 SteamProcessMonitor& operator=(const SteamProcessMonitor&) = delete;
86
87 // ==========================================================================
88 // Internal Methods
89 // ==========================================================================
90
91 void monitorThread(); // Main monitoring loop
92 bool checkSteamProcess(); // Check if steam process exists
93 void transitionTo(State newState); // Change state and notify callbacks
94 void handleStartingTimeout(); // Handle STARTING state timeout
95 void handleStoppingTimeout(); // Handle STOPPING state timeout
96
97 // ==========================================================================
98 // Member Variables
99 // ==========================================================================
100
101 std::atomic<State> currentState_;
102 std::atomic<bool> shouldStop_;
103
104 std::thread monitorThread_;
105 std::mutex stateMutex_;
106 std::condition_variable stateCV_;
107
108 StateCallback stateCallback_;
109
110 std::chrono::steady_clock::time_point transitionTime_; // Time when state last changed
111};
void setStateCallback(StateCallback callback)
State getCurrentState() const
bool isSteamRunning() const
static SteamProcessMonitor & instance()
bool waitForSteamInitialized(int timeoutMs=10000)
bool isSteamFullyInitialized() const
bool waitForState(State targetState, int timeoutMs=10000)
std::function< void(State oldState, State newState)> StateCallback