1212namespace RGB . NET . Devices . Corsair . Native ;
1313
1414internal delegate void CorsairSessionStateChangedHandler ( nint context , _CorsairSessionStateChanged eventData ) ;
15+ internal delegate void CorsairEventHandler ( nint context , _CorsairEvent corsairEvent ) ;
1516
1617// ReSharper disable once InconsistentNaming
1718internal static unsafe class _CUESDK
@@ -50,14 +51,15 @@ internal static unsafe class _CUESDK
5051 // ReSharper disable once NotAccessedField.Local - This is important, the delegate can be collected if it's not stored!
5152 private static readonly CorsairSessionStateChangedHandler SESSION_STATE_CHANGED_CALLBACK ;
5253
53- internal static bool IsConnected => SesionState == CorsairSessionState . Connected ;
54- internal static CorsairSessionState SesionState { get ; private set ; }
54+ internal static bool IsConnected => SessionState == CorsairSessionState . Connected ;
55+ internal static CorsairSessionState SessionState { get ; private set ; }
5556
5657 #endregion
5758
5859 #region Events
5960
6061 internal static event EventHandler < CorsairSessionState > ? SessionStateChanged ;
62+ internal static event EventHandler < _CorsairDeviceConnectionStatusChangedEvent > ? DeviceConnectionEvent ;
6163
6264 #endregion
6365
@@ -72,10 +74,45 @@ static _CUESDK()
7274
7375 #region Methods
7476
75- private static void CorsairSessionStateChangedCallback ( nint context , _CorsairSessionStateChanged eventdata )
77+ private static void CorsairSessionStateChangedCallback ( nint context , _CorsairSessionStateChanged eventData )
7678 {
77- SesionState = eventdata . state ;
78- SessionStateChanged ? . Invoke ( null , eventdata . state ) ;
79+ SessionState = eventData . state ;
80+ try
81+ {
82+ SessionStateChanged ? . Invoke ( null , eventData . state ) ;
83+ }
84+ catch { /* dont let exception go to sdk */ }
85+
86+ switch ( eventData . state )
87+ {
88+ case CorsairSessionState . Connected :
89+ _corsairSubscribeForEvents ( CorsairEventCallback , 0 ) ;
90+ break ;
91+ case CorsairSessionState . Closed :
92+ _corsairUnsubscribeForEvents ( ) ;
93+ break ;
94+ }
95+ }
96+
97+ private static void CorsairEventCallback ( nint context , _CorsairEvent eventData )
98+ {
99+ if ( eventData . id != CorsairEventId . DeviceConnectionStatusChangedEvent )
100+ {
101+ return ;
102+ }
103+
104+ try
105+ {
106+ if ( eventData . eventPointer == 0 )
107+ {
108+ return ;
109+ }
110+
111+ _CorsairDeviceConnectionStatusChangedEvent connectionStatusChangedEvent =
112+ Marshal . PtrToStructure < _CorsairDeviceConnectionStatusChangedEvent > ( eventData . eventPointer ) ! ;
113+
114+ DeviceConnectionEvent ? . Invoke ( null , connectionStatusChangedEvent ) ;
115+ } catch { /* dont let exception go to sdk */ }
79116 }
80117
81118 #endregion
@@ -109,7 +146,7 @@ private static void LoadCUESDK()
109146 _corsairGetSessionDetails = ( delegate * unmanaged[ Cdecl] < nint , CorsairError > ) LoadFunction ( "CorsairGetSessionDetails" ) ;
110147 _corsairDisconnect = ( delegate * unmanaged[ Cdecl] < CorsairError > ) LoadFunction ( "CorsairDisconnect" ) ;
111148 _corsairGetDevices = ( delegate * unmanaged[ Cdecl] < _CorsairDeviceFilter , int , nint , out int , CorsairError > ) LoadFunction ( "CorsairGetDevices" ) ;
112- _corsairGetDeviceInfo = ( delegate * unmanaged[ Cdecl] < string , _CorsairDeviceInfo , CorsairError > ) LoadFunction ( "CorsairGetDeviceInfo" ) ;
149+ _corsairGetDeviceInfo = ( delegate * unmanaged[ Cdecl] < string , ref _CorsairDeviceInfo , CorsairError > ) LoadFunction ( "CorsairGetDeviceInfo" ) ;
113150 _corsairGetLedPositions = ( delegate * unmanaged[ Cdecl] < string , int , nint , out int , CorsairError > ) LoadFunction ( "CorsairGetLedPositions" ) ;
114151 _corsairSetLedColors = ( delegate * unmanaged[ Cdecl] < string , int , nint , CorsairError > ) LoadFunction ( "CorsairSetLedColors" ) ;
115152 _corsairSetLayerPriority = ( delegate * unmanaged[ Cdecl] < uint , CorsairError > ) LoadFunction ( "CorsairSetLayerPriority" ) ;
@@ -118,6 +155,8 @@ private static void LoadCUESDK()
118155 _corsairReleaseControl = ( delegate * unmanaged[ Cdecl] < string , CorsairError > ) LoadFunction ( "CorsairReleaseControl" ) ;
119156 _getDevicePropertyInfo = ( delegate * unmanaged[ Cdecl] < string , CorsairDevicePropertyId , uint , out CorsairDataType , out CorsairPropertyFlag , CorsairError > ) LoadFunction ( "CorsairGetDevicePropertyInfo" ) ;
120157 _readDeviceProperty = ( delegate * unmanaged[ Cdecl] < string , CorsairDevicePropertyId , uint , nint , CorsairError > ) LoadFunction ( "CorsairReadDeviceProperty" ) ;
158+ _corsairSubscribeForEvents = ( delegate * unmanaged[ Cdecl] < CorsairEventHandler , nint , CorsairError > ) LoadFunction ( "CorsairSubscribeForEvents" ) ;
159+ _corsairUnsubscribeForEvents = ( delegate * unmanaged[ Cdecl] < CorsairError > ) LoadFunction ( "CorsairSubscribeForEvents" ) ;
121160 }
122161
123162 private static nint LoadFunction ( string function )
@@ -170,7 +209,7 @@ internal static void UnloadCUESDK()
170209 private static delegate * unmanaged[ Cdecl] < nint , CorsairError > _corsairGetSessionDetails ;
171210 private static delegate * unmanaged[ Cdecl] < CorsairError > _corsairDisconnect ;
172211 private static delegate * unmanaged[ Cdecl] < _CorsairDeviceFilter , int , nint , out int , CorsairError > _corsairGetDevices ;
173- private static delegate * unmanaged[ Cdecl] < string , _CorsairDeviceInfo , CorsairError > _corsairGetDeviceInfo ;
212+ private static delegate * unmanaged[ Cdecl] < string , ref _CorsairDeviceInfo , CorsairError > _corsairGetDeviceInfo ;
174213 private static delegate * unmanaged[ Cdecl] < string , int , nint , out int , CorsairError > _corsairGetLedPositions ;
175214 private static delegate * unmanaged[ Cdecl] < string , int , nint , CorsairError > _corsairSetLedColors ;
176215 private static delegate * unmanaged[ Cdecl] < uint , CorsairError > _corsairSetLayerPriority ;
@@ -179,12 +218,15 @@ internal static void UnloadCUESDK()
179218 private static delegate * unmanaged[ Cdecl] < string , CorsairError > _corsairReleaseControl ;
180219 private static delegate * unmanaged[ Cdecl] < string , CorsairDevicePropertyId , uint , out CorsairDataType , out CorsairPropertyFlag , CorsairError > _getDevicePropertyInfo ;
181220 private static delegate * unmanaged[ Cdecl] < string , CorsairDevicePropertyId , uint , nint , CorsairError > _readDeviceProperty ;
221+ private static delegate * unmanaged[ Cdecl] < CorsairEventHandler , nint , CorsairError > _corsairSubscribeForEvents ;
222+ private static delegate * unmanaged[ Cdecl] < CorsairError > _corsairUnsubscribeForEvents ;
182223
183224 #endregion
184225
185226 internal static CorsairError CorsairConnect ( )
186227 {
187228 if ( _corsairConnectPtr == null ) throw new RGBDeviceException ( "The Corsair-SDK is not initialized." ) ;
229+ if ( SessionState is CorsairSessionState . Connecting or CorsairSessionState . Timeout ) return CorsairError . Success ;
188230 if ( IsConnected ) throw new RGBDeviceException ( "The Corsair-SDK is already connected." ) ;
189231 return _corsairConnectPtr ( SESSION_STATE_CHANGED_CALLBACK , 0 ) ;
190232 }
@@ -209,7 +251,6 @@ internal static CorsairError CorsairGetSessionDetails(out _CorsairSessionDetails
209251
210252 internal static CorsairError CorsairDisconnect ( )
211253 {
212- if ( ! IsConnected ) throw new RGBDeviceException ( "The Corsair-SDK is not connected." ) ;
213254 return _corsairDisconnect ( ) ;
214255 }
215256
@@ -232,10 +273,12 @@ internal static CorsairError CorsairGetDevices(_CorsairDeviceFilter filter, out
232273 }
233274 }
234275
235- internal static CorsairError CorsairGetDeviceInfo ( string deviceId , _CorsairDeviceInfo deviceInfo )
276+ internal static CorsairError CorsairGetDeviceInfo ( string deviceId , out _CorsairDeviceInfo deviceInfo )
236277 {
237278 if ( ! IsConnected ) throw new RGBDeviceException ( "The Corsair-SDK is not connected." ) ;
238- return _corsairGetDeviceInfo ( deviceId , deviceInfo ) ;
279+
280+ deviceInfo = new _CorsairDeviceInfo ( ) ;
281+ return _corsairGetDeviceInfo ( deviceId , ref deviceInfo ) ;
239282 }
240283
241284 internal static CorsairError CorsairGetLedPositions ( string deviceId , out _CorsairLedPosition [ ] ledPositions )
0 commit comments