Repository navigation
Expand file tree
/
Copy pathGameSensePlugin.cpp
More file actions
200 lines (167 loc) · 7.9 KB
/
Copy pathGameSensePlugin.cpp
File metadata and controls
200 lines (167 loc) · 7.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
/*---------------------------------------------------------*\
| GameSensePlugin.cpp |
| |
| OpenRGB plugin that mirrors OpenRGB's own SteelSeries |
| 8-zone keyboard device to SteelSeries GG's GameSense |
| API, so its colors stick while GG is running. |
| |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <algorithm>
#include "GameSensePlugin.h"
#include "LogManager.h"
#include "ResourceManagerCallback.h"
/*---------------------------------------------------------*\
| Description OpenRGB's Apex 3 driver gives its devices; |
| the 8-zone variants are the ones with 8 LEDs. |
\*---------------------------------------------------------*/
#define APEX3_DESCRIPTION "SteelSeries Apex 3 Device"
static bool IsEightZoneApex(RGBControllerInterface* controller)
{
return(controller->GetDescription() == APEX3_DESCRIPTION && controller->GetLEDCount() == GAMESENSE_ZONE_COUNT);
}
OpenRGBPluginInfo GameSensePlugin::GetPluginInfo()
{
OpenRGBPluginInfo info;
info.Name = "SteelSeries GameSense";
info.Description = "Keeps SteelSeries 8-zone keyboard colors while SteelSeries GG is running";
info.Version = "1.0";
info.Commit = "";
info.URL = "https://gitlab.com/xmo4/OpenRGB-GameSensePlugin";
/*-----------------------------------------------------*\
| No UI: an out-of-range location tells OpenRGB not to |
| create a tab for it. |
\*-----------------------------------------------------*/
info.Location = OPENRGB_PLUGIN_LOCATION_SETTINGS + 1;
info.Label = "SteelSeries GameSense";
info.ProtocolVersion = 0;
return(info);
}
unsigned int GameSensePlugin::GetPluginAPIVersion()
{
return(OPENRGB_PLUGIN_API_VERSION);
}
void GameSensePlugin::Load(OpenRGBPluginAPIInterface* plugin_api_ptr)
{
api = plugin_api_ptr;
/*-----------------------------------------------------*\
| coreProps.json only exists once GG has been installed |
| and run; without it there is nothing to drive. |
\*-----------------------------------------------------*/
if(SteelSeriesGameSense::ReadCoreProps().empty())
{
api->LogEntry(__FILE__, __LINE__, LL_INFO, "[GameSense Plugin] SteelSeries GG not found, staying idle");
return;
}
gamesense = new SteelSeriesGameSense(api);
AttachControllers();
}
void GameSensePlugin::Unload()
{
{
std::lock_guard<std::mutex> lock(attached_mutex);
/*-------------------------------------------------*\
| Only touch controllers that still exist |
\*-------------------------------------------------*/
std::vector<RGBControllerInterface*> current = api->GetRGBControllers();
for(RGBControllerInterface* controller : attached)
{
if(std::find(current.begin(), current.end(), controller) != current.end())
{
controller->UnregisterUpdateCallback(this);
}
}
attached.clear();
}
/*-----------------------------------------------------*\
| Deleting the client stops our GameSense game, so GG |
| goes back to its own lighting. |
| Safe: UnregisterUpdateCallback above takes the same |
| mutex SignalUpdate holds while calling callbacks, so |
| no callback is still running here. |
\*-----------------------------------------------------*/
delete gamesense;
gamesense = nullptr;
}
void GameSensePlugin::AttachControllers()
{
std::lock_guard<std::mutex> lock(attached_mutex);
std::vector<RGBControllerInterface*> current = api->GetRGBControllers();
/*-----------------------------------------------------*\
| Forget controllers that were removed (rescan), without|
| calling into them as they may already be freed. |
\*-----------------------------------------------------*/
attached.erase(std::remove_if(attached.begin(), attached.end(), [&](RGBControllerInterface* controller)
{
return(std::find(current.begin(), current.end(), controller) == current.end());
}), attached.end());
for(RGBControllerInterface* controller : current)
{
if(IsEightZoneApex(controller) && std::find(attached.begin(), attached.end(), controller) == attached.end())
{
controller->RegisterUpdateCallback(ControllerUpdated, this);
attached.push_back(controller);
api->LogEntry(__FILE__, __LINE__, LL_INFO, "[GameSense Plugin] Mirroring %s to GameSense", controller->GetName().c_str());
/*---------------------------------------------*\
| A profile may have been applied before we |
| attached; push the current state now. All |
| black means OpenRGB has no state yet, so |
| leave GG's lighting alone in that case. |
\*---------------------------------------------*/
bool has_color = false;
for(unsigned int led_idx = 0; led_idx < GAMESENSE_ZONE_COUNT; led_idx++)
{
has_color = has_color || (controller->GetColor(led_idx) != 0);
}
if(has_color)
{
ControllerUpdated(this, RGBCONTROLLER_UPDATE_REASON_UPDATELEDS, controller);
}
}
}
}
void GameSensePlugin::ControllerUpdated(void* this_ptr, unsigned int update_reason, void* controller_ptr)
{
if(update_reason != RGBCONTROLLER_UPDATE_REASON_UPDATELEDS && update_reason != RGBCONTROLLER_UPDATE_REASON_UPDATEMODE)
{
return;
}
GameSensePlugin* plugin = (GameSensePlugin*)this_ptr;
RGBControllerInterface* controller = (RGBControllerInterface*)controller_ptr;
int active_mode = controller->GetActiveMode();
/*-----------------------------------------------------*\
| Only Direct is mirrored; hardware effects can't be |
| expressed through GameSense, so hand control back. |
| Brightness needs nothing here: the keyboard's own |
| brightness command still applies under GG. |
\*-----------------------------------------------------*/
if(active_mode < 0 || controller->GetModeName((unsigned int)active_mode) != "Direct")
{
plugin->gamesense->Stop();
return;
}
std::vector<RGBColor> colors(GAMESENSE_ZONE_COUNT);
for(unsigned int led_idx = 0; led_idx < GAMESENSE_ZONE_COUNT; led_idx++)
{
colors[led_idx] = controller->GetColor(led_idx);
}
plugin->gamesense->SendColors(colors);
}
void GameSensePlugin::ResourceManagerUpdated(unsigned int update_reason)
{
if(gamesense != nullptr && update_reason == RESOURCEMANAGER_UPDATE_REASON_DEVICE_LIST_UPDATED)
{
AttachControllers();
}
}
/*---------------------------------------------------------*\
| Unused plugin hooks |
\*---------------------------------------------------------*/
QWidget* GameSensePlugin::GetWidget() { return(nullptr); }
QMenu* GameSensePlugin::GetTrayMenu() { return(nullptr); }
void GameSensePlugin::OnProfileAboutToLoad() {}
void GameSensePlugin::OnProfileLoad(nlohmann::json) {}
nlohmann::json GameSensePlugin::OnProfileSave() { return(nlohmann::json()); }
unsigned char* GameSensePlugin::OnSDKCommand(unsigned int, unsigned char*, unsigned int*) { return(nullptr); }
void GameSensePlugin::ProfileManagerUpdated(unsigned int) {}
void GameSensePlugin::SettingsManagerUpdated(unsigned int) {}