Repository navigation
Expand file tree
/
Copy pathSteelSeriesGameSense.cpp
More file actions
306 lines (249 loc) · 9 KB
/
Copy pathSteelSeriesGameSense.cpp
File metadata and controls
306 lines (249 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*---------------------------------------------------------*\
| SteelSeriesGameSense.cpp |
| |
| Sends 8-zone keyboard lighting through SteelSeries |
| Engine's local GameSense API. |
| |
| xmo4 12 Nov 2026 |
| |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <cstdlib>
#include <fstream>
#include <nlohmann/json.hpp>
#include "httplib.h"
#include "LogManager.h"
#include "SteelSeriesGameSense.h"
using json = nlohmann::json;
using namespace std::chrono_literals;
#define GAMESENSE_GAME "OPENRGB"
#define GAMESENSE_EVENT "LIGHTING"
#define GAMESENSE_HOST "127.0.0.1"
#define GAMESENSE_DEINIT_MS 60000
#define GAMESENSE_RETRY_PERIOD 5s
#define GAMESENSE_KEEPALIVE_PERIOD 2s
#define GAMESENSE_MAX_PROPS_SIZE 4096
static const char* zone_names[GAMESENSE_ZONE_COUNT] =
{
"one", "two", "three", "four", "five", "six", "seven", "eight"
};
std::string SteelSeriesGameSense::ReadCoreProps()
{
#ifdef _WIN32
const char* program_data = std::getenv("PROGRAMDATA");
if(program_data == NULL)
{
return("");
}
std::ifstream file(std::string(program_data) + "\\SteelSeries\\SteelSeries Engine 3\\coreProps.json", std::ios::binary);
std::string contents(GAMESENSE_MAX_PROPS_SIZE, '\0');
file.read(&contents[0], GAMESENSE_MAX_PROPS_SIZE);
contents.resize((size_t)file.gcount());
return(contents);
#else
/*-----------------------------------------------------*\
| SteelSeries GG only exists on Windows |
\*-----------------------------------------------------*/
return("");
#endif
}
bool SteelSeriesGameSense::ParseLoopbackAddress(const std::string& core_props, int& port)
{
json props = json::parse(core_props, nullptr, false);
if(!props.is_object() || !props.contains("address") || !props["address"].is_string())
{
return(false);
}
const std::string address = props["address"].get<std::string>();
const std::string prefix = GAMESENSE_HOST ":";
if(address.compare(0, prefix.size(), prefix) != 0)
{
return(false);
}
const std::string port_str = address.substr(prefix.size());
if(port_str.empty() || port_str.size() > 5 || port_str.find_first_not_of("0123456789") != std::string::npos)
{
return(false);
}
int value = std::stoi(port_str);
if(value < 1 || value > 65535)
{
return(false);
}
port = value;
return(true);
}
SteelSeriesGameSense::SteelSeriesGameSense(OpenRGBPluginAPIInterface* api_ptr)
{
api = api_ptr;
port = 0;
event_value = 0;
registered = false;
active = false;
keepalive_thread_run = true;
keepalive_thread = std::thread(&SteelSeriesGameSense::KeepaliveThreadFunction, this);
}
SteelSeriesGameSense::~SteelSeriesGameSense()
{
keepalive_thread_run = false;
keepalive_thread.join();
Stop();
}
bool SteelSeriesGameSense::Post(const char* uri, const std::string& body)
{
if(port == 0)
{
return(false);
}
/*-----------------------------------------------------*\
| Short timeouts, Engine is local and this is called |
| from the device update path |
\*-----------------------------------------------------*/
httplib::Client client(GAMESENSE_HOST, port);
client.set_connection_timeout(0, 300000);
client.set_read_timeout(1, 0);
client.set_write_timeout(1, 0);
httplib::Result result = client.Post(uri, body, "application/json");
return(result && result->status == 200);
}
bool SteelSeriesGameSense::Register()
{
registered = false;
if(std::chrono::steady_clock::now() < retry_after)
{
return(false);
}
/*-----------------------------------------------------*\
| Re-read every attempt: Engine picks a new port each |
| time it starts. |
\*-----------------------------------------------------*/
if(!ParseLoopbackAddress(ReadCoreProps(), port))
{
port = 0;
retry_after = std::chrono::steady_clock::now() + GAMESENSE_RETRY_PERIOD;
return(false);
}
json metadata =
{
{ "game", GAMESENSE_GAME },
{ "game_display_name", "OpenRGB" },
{ "developer", "OpenRGB" },
{ "deinitialize_timer_length_ms", GAMESENSE_DEINIT_MS }
};
json handlers = json::array();
for(unsigned int zone_idx = 0; zone_idx < GAMESENSE_ZONE_COUNT; zone_idx++)
{
handlers.push_back(
{
{ "device-type", "rgb-8-zone" },
{ "zone", zone_names[zone_idx] },
{ "mode", "context-color" },
{ "context-frame-key", "z" + std::to_string(zone_idx) }
});
}
json binding =
{
{ "game", GAMESENSE_GAME },
{ "event", GAMESENSE_EVENT },
{ "value_optional", true },
{ "handlers", handlers }
};
if(!Post("/game_metadata", metadata.dump()) || !Post("/bind_game_event", binding.dump()))
{
retry_after = std::chrono::steady_clock::now() + GAMESENSE_RETRY_PERIOD;
return(false);
}
api->LogEntry(__FILE__, __LINE__, LL_INFO, "[GameSense Plugin] Registered with Engine at %s:%d", GAMESENSE_HOST, port);
registered = true;
return(true);
}
bool SteelSeriesGameSense::SendLastColors()
{
json frame = json::object();
for(unsigned int zone_idx = 0; zone_idx < GAMESENSE_ZONE_COUNT && zone_idx < last_colors.size(); zone_idx++)
{
frame["z" + std::to_string(zone_idx)] =
{
{ "red", RGBGetRValue(last_colors[zone_idx]) },
{ "green", RGBGetGValue(last_colors[zone_idx]) },
{ "blue", RGBGetBValue(last_colors[zone_idx]) }
};
}
/*-----------------------------------------------------*\
| Value changes every event so Engine re-renders it. |
\*-----------------------------------------------------*/
event_value = (event_value % 100000) + 1;
json event =
{
{ "game", GAMESENSE_GAME },
{ "event", GAMESENSE_EVENT },
{ "data", { { "value", event_value }, { "frame", frame } } }
};
return(Post("/game_event", event.dump()));
}
bool SteelSeriesGameSense::SendColors(const std::vector<RGBColor>& colors)
{
std::lock_guard<std::mutex> lock(mutex);
/*-----------------------------------------------------*\
| Mode/brightness changes re-signal unchanged colors; |
| the keepalive already re-sends them. |
\*-----------------------------------------------------*/
if(active && registered && colors == last_colors)
{
return(true);
}
last_colors = colors;
/*-----------------------------------------------------*\
| One re-register attempt covers an Engine restart |
| (new port, registration lost). |
\*-----------------------------------------------------*/
bool sent = (registered || Register()) && SendLastColors();
if(!sent && registered)
{
sent = Register() && SendLastColors();
}
if(!sent)
{
registered = false;
}
/*-----------------------------------------------------*\
| Stay active even if Engine isn't up yet: the |
| keepalive thread applies these colors once it is. |
\*-----------------------------------------------------*/
active = true;
return(sent);
}
void SteelSeriesGameSense::Stop()
{
std::lock_guard<std::mutex> lock(mutex);
if(active && registered)
{
Post("/stop_game", json({ { "game", GAMESENSE_GAME } }).dump());
}
active = false;
}
void SteelSeriesGameSense::KeepaliveThreadFunction()
{
while(keepalive_thread_run.load())
{
std::this_thread::sleep_for(GAMESENSE_KEEPALIVE_PERIOD);
std::lock_guard<std::mutex> lock(mutex);
if(!keepalive_thread_run.load() || !active)
{
continue;
}
/*-------------------------------------------------*\
| Re-send colors rather than a bare heartbeat: it |
| keeps the event alive and undoes Prism loading |
| its own profile over ours. If Engine restarted, |
| re-register first. |
\*-------------------------------------------------*/
if(!(registered && SendLastColors()))
{
if(!(Register() && SendLastColors()))
{
registered = false;
}
}
}
}