Skip to content

Commit 4cc922f

Browse files
committed
iCUESDK Demo: Avoid calling iCUESDK APIs when the session is not fully connected.
1 parent 16b596b commit 4cc922f

File tree

3 files changed

+77
-53
lines changed

3 files changed

+77
-53
lines changed

demo/source/demos/iCUESDKDemo.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ class CueSDKDemo final : public DemoBase,
5858

5959
void timerCallback() override
6060
{
61+
if (! corsair::globalSession->isConnected())
62+
{
63+
devices.clearQuick (true);
64+
listbox.updateContent();
65+
repaint(); // For the LEDs.
66+
return;
67+
}
68+
6169
const auto newDevs = corsair::getAllAvailableDevices();
6270

6371
bool changed = false;
@@ -205,8 +213,6 @@ class CueSDKDemo final : public DemoBase,
205213
TextButton redButton, greenButton, blueButton;
206214
ListBox listbox;
207215

208-
Grid pixelGrid;
209-
210216
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CueSDKDemo)
211217
};
212218

demo/source/main/Main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class MainWindow final : public DocumentWindow
100100
centreWithSize (getWidth(), getHeight());
101101
#endif
102102

103-
corsair::logInfo();
103+
corsair::globalSession->logInfo();
104104

105105
setVisible (true);
106106
}

modules/squarepine_graphics/linkers/iCUESDKIncluder.h

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -92,55 +92,6 @@
9292
return s;
9393
}
9494

95-
//==============================================================================
96-
/** */
97-
struct iCUESDKConnector final
98-
{
99-
/** */
100-
iCUESDKConnector()
101-
{
102-
isValid (CorsairConnect (sessionStateChangeHandler, this));
103-
}
104-
105-
/** */
106-
~iCUESDKConnector()
107-
{
108-
isValid (CorsairDisconnect());
109-
}
110-
111-
static void sessionStateChangeHandler (void*, const CorsairSessionStateChanged*)
112-
{
113-
}
114-
};
115-
116-
static juce::SharedResourcePointer<iCUESDKConnector> connector;
117-
118-
//==============================================================================
119-
/** */
120-
inline void logInfo()
121-
{
122-
CorsairSessionDetails details;
123-
juce::zerostruct (details);
124-
if (const auto result = toResult (CorsairGetSessionDetails (&details)); result.failed())
125-
{
126-
juce::Logger::writeToLog ("Could not log iCUE SDK information: " + result.getErrorMessage());
127-
return;
128-
}
129-
130-
juce::String info;
131-
info
132-
<< juce::newLine
133-
<< "--------------------------------------------------" << juce::newLine << juce::newLine
134-
<< "=== Corsair iCUE SDK Information ===" << juce::newLine << juce::newLine
135-
<< "clientVersion: " << toString (details.clientVersion) << juce::newLine
136-
<< "serverVersion: " << toString (details.serverVersion) << juce::newLine
137-
<< "serverHostVersion: " << toString (details.serverHostVersion) << juce::newLine
138-
<< juce::newLine
139-
<< "--------------------------------------------------" << juce::newLine;
140-
141-
juce::Logger::writeToLog (info);
142-
}
143-
14495
//==============================================================================
14596
/** */
14697
inline bool operator== (const CorsairDeviceInfo& lhs, const CorsairDeviceInfo& rhs)
@@ -159,6 +110,7 @@
159110
return ! operator== (lhs, rhs);
160111
}
161112

113+
//==============================================================================
162114
/** */
163115
inline juce::Colour toColour (const CorsairLedColor& clc)
164116
{
@@ -177,6 +129,72 @@
177129
return c;
178130
}
179131

132+
//==============================================================================
133+
/** */
134+
class iCUESDKSession final
135+
{
136+
public:
137+
/** */
138+
iCUESDKSession()
139+
{
140+
juce::zerostruct (details);
141+
isValid (CorsairConnect (sessionStateChangeHandler, this));
142+
}
143+
144+
/** */
145+
~iCUESDKSession()
146+
{
147+
isValid (CorsairDisconnect());
148+
}
149+
150+
/** */
151+
bool isConnected() const { return connected.load (std::memory_order_relaxed); }
152+
153+
/** */
154+
const CorsairSessionDetails& getDetails() const { return details; }
155+
156+
//==============================================================================
157+
/** */
158+
void logInfo()
159+
{
160+
juce::String info;
161+
info
162+
<< juce::newLine
163+
<< "--------------------------------------------------" << juce::newLine << juce::newLine
164+
<< "=== Corsair iCUE SDK Information ===" << juce::newLine << juce::newLine
165+
<< "clientVersion: " << toString (details.clientVersion) << juce::newLine
166+
<< "serverVersion: " << toString (details.serverVersion) << juce::newLine
167+
<< "serverHostVersion: " << toString (details.serverHostVersion) << juce::newLine
168+
<< juce::newLine
169+
<< "--------------------------------------------------" << juce::newLine;
170+
171+
juce::Logger::writeToLog (info);
172+
}
173+
174+
//==============================================================================
175+
/** */
176+
static void sessionStateChangeHandler (void* sourcePtr, const CorsairSessionStateChanged* newSession)
177+
{
178+
if (auto* session = (iCUESDKSession*) sourcePtr)
179+
{
180+
session->connected = newSession != nullptr
181+
? newSession->state == CSS_Connected
182+
: false;
183+
184+
session->details = session->details;
185+
}
186+
}
187+
188+
private:
189+
std::atomic<bool> connected { false };
190+
CorsairSessionDetails details;
191+
192+
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (iCUESDKSession)
193+
};
194+
195+
/** */
196+
static juce::SharedResourcePointer<iCUESDKSession> globalSession;
197+
180198
//==============================================================================
181199
/**
182200
*/
@@ -211,7 +229,7 @@
211229
return {};
212230
}
213231

214-
int numLEDs = device.ledCount;
232+
auto numLEDs = device.ledCount;
215233
std::vector<CorsairLedPosition> ledIDs ((size_t) numLEDs);
216234
if (isValid (CorsairGetLedPositions (device.id, numLEDs, ledIDs.data(), &numLEDs)))
217235
return { ledIDs.data(), numLEDs };

0 commit comments

Comments
 (0)