// ReSharper restore UnusedAutoPropertyAccessor.Global #endregion #region Methods public static void Initialize(bool exclusiveAccess = false) { if (ProtocolDetails != null) throw new WrapperException("CueSDK is already initialized."); ProtocolDetails = new CorsairProtocolDetails(_CUESDK.CorsairPerformProtocolHandshake()); CorsairError error = LastError; if (error != CorsairError.Success) Throw(error); if (ProtocolDetails.BreakingChanges) throw new WrapperException("The SDK currently used isn't compatible with the installed version of CUE.\r\n" + $"CUE-Version: {ProtocolDetails.ServerVersion} (Protocol {ProtocolDetails.ServerProtocolVersion})\r\n" + $"SDK-Version: {ProtocolDetails.SdkVersion} (Protocol {ProtocolDetails.SdkProtocolVersion})"); if (exclusiveAccess) { if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl)) Throw(error); HasExclusiveAccess = true; } int deviceCount = _CUESDK.CorsairGetDeviceCount(); for (int i = 0; i < deviceCount; i++) { _CorsairDeviceInfo nativeDeviceInfo = (_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo)); GenericDeviceInfo info = new GenericDeviceInfo(nativeDeviceInfo); if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting)) continue; // Everything that doesn't support lighting control is useless switch (info.Type) { case CorsairDeviceType.Keyboard: KeyboardSDK = new CorsairKeyboard(new CorsairKeyboardDeviceInfo(nativeDeviceInfo)); break; case CorsairDeviceType.Mouse: MouseSDK = new CorsairMouse(new CorsairMouseDeviceInfo(nativeDeviceInfo)); break; case CorsairDeviceType.Headset: HeadsetSDK = new CorsairHeadset(new CorsairHeadsetDeviceInfo(nativeDeviceInfo)); break; // ReSharper disable once RedundantCaseLabel case CorsairDeviceType.Unknown: default: throw new WrapperException("Unknown Device-Type"); } error = LastError; if (error != CorsairError.Success) Throw(error); } }
/// <summary> /// Initializes the mousemat. /// </summary> public override void Initialize() { int deviceCount = _CUESDK.CorsairGetDeviceCount(); // Get mousemat device index int mousematIndex = -1; for (int i = 0; i < deviceCount; i++) { _CorsairDeviceInfo nativeDeviceInfo = (_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo)); GenericDeviceInfo info = new GenericDeviceInfo(nativeDeviceInfo); if (info.Type != CorsairDeviceType.Mousemat) continue; mousematIndex = i; break; } if (mousematIndex < 0) throw new WrapperException("Can't determine mousemat device index"); _CorsairLedPositions nativeLedPositions = (_CorsairLedPositions)Marshal.PtrToStructure(_CUESDK.CorsairGetLedPositionsByDeviceIndex(mousematIndex), typeof(_CorsairLedPositions)); int structSize = Marshal.SizeOf(typeof(_CorsairLedPosition)); IntPtr ptr = nativeLedPositions.pLedPosition; // Put the positions in an array for sorting later on List<_CorsairLedPosition> positions = new List<_CorsairLedPosition>(); for (int i = 0; i < nativeLedPositions.numberOfLed; i++) { _CorsairLedPosition ledPosition = (_CorsairLedPosition)Marshal.PtrToStructure(ptr, typeof(_CorsairLedPosition)); ptr = new IntPtr(ptr.ToInt64() + structSize); positions.Add(ledPosition); } // Sort for easy iteration by clients foreach (_CorsairLedPosition ledPosition in positions.OrderBy(p => p.ledId)) InitializeLed(ledPosition.ledId, new RectangleF((float)ledPosition.left, (float)ledPosition.top, (float)ledPosition.width, (float)ledPosition.height)); base.Initialize(); }
// ReSharper restore UnusedAutoPropertyAccessor.Global #endregion #region Methods /// <summary> /// Checks if the SDK for the provided <see cref="CorsairDeviceType"/> is available or checks if CUE is installed and SDK supported enabled if null is provided. /// </summary> /// <param name="sdkType">The <see cref="CorsairDeviceType"/> to check or null to check for generall SDK availability.</param> /// <returns>The availability of the provided <see cref="CorsairDeviceType"/>.</returns> public static bool IsSDKAvailable(CorsairDeviceType? sdkType = null) { try { if (IsInitialized) { // ReSharper disable once SwitchStatementMissingSomeCases - everything else is true switch (sdkType) { case CorsairDeviceType.Keyboard: return KeyboardSDK != null; case CorsairDeviceType.Mouse: return MouseSDK != null; case CorsairDeviceType.Headset: return HeadsetSDK != null; case CorsairDeviceType.Mousemat: return MousematSDK != null; default: return true; } } else { _CUESDK.CorsairPerformProtocolHandshake(); if (sdkType == null || sdkType == CorsairDeviceType.Unknown) return LastError == CorsairError.Success; int deviceCount = _CUESDK.CorsairGetDeviceCount(); for (int i = 0; i < deviceCount; i++) { GenericDeviceInfo info = new GenericDeviceInfo((_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo))); if (info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting) && info.Type == sdkType.Value) return true; } } } catch { return false; } return false; }
/// <summary> /// Reinitialize the CUE-SDK and temporarily hand back full control to CUE. /// </summary> /// <param name="exclusiveAccess">Specifies whether the application should request exclusive access or not.</param> public static void Reinitialize(bool exclusiveAccess) { if (!IsInitialized) throw new WrapperException("CueSDK isn't initialized."); KeyboardSDK?.ResetLeds(); MouseSDK?.ResetLeds(); HeadsetSDK?.ResetLeds(); MousematSDK?.ResetLeds(); _CUESDK.Reload(); _CUESDK.CorsairPerformProtocolHandshake(); CorsairError error = LastError; if (error != CorsairError.Success) Throw(error); if (ProtocolDetails.BreakingChanges) throw new WrapperException("The SDK currently used isn't compatible with the installed version of CUE.\r\n" + $"CUE-Version: {ProtocolDetails.ServerVersion} (Protocol {ProtocolDetails.ServerProtocolVersion})\r\n" + $"SDK-Version: {ProtocolDetails.SdkVersion} (Protocol {ProtocolDetails.SdkProtocolVersion})"); if (exclusiveAccess) if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl)) Throw(LastError); HasExclusiveAccess = exclusiveAccess; int deviceCount = _CUESDK.CorsairGetDeviceCount(); Dictionary<CorsairDeviceType, GenericDeviceInfo> reloadedDevices = new Dictionary<CorsairDeviceType, GenericDeviceInfo>(); for (int i = 0; i < deviceCount; i++) { GenericDeviceInfo info = new GenericDeviceInfo((_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo))); if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting)) continue; // Everything that doesn't support lighting control is useless reloadedDevices.Add(info.Type, info); error = LastError; if (error != CorsairError.Success) Throw(error); } if (KeyboardSDK != null) if (!reloadedDevices.ContainsKey(CorsairDeviceType.Keyboard) || KeyboardSDK.KeyboardDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Keyboard].Model) throw new WrapperException("The previously loaded Keyboard got disconnected."); if (MouseSDK != null) if (!reloadedDevices.ContainsKey(CorsairDeviceType.Mouse) || MouseSDK.MouseDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Mouse].Model) throw new WrapperException("The previously loaded Mouse got disconnected."); if (HeadsetSDK != null) if (!reloadedDevices.ContainsKey(CorsairDeviceType.Headset) || HeadsetSDK.HeadsetDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Headset].Model) throw new WrapperException("The previously loaded Headset got disconnected."); if (MousematSDK != null) if (!reloadedDevices.ContainsKey(CorsairDeviceType.Mousemat) || MousematSDK.MousematDeviceInfo.Model != reloadedDevices[CorsairDeviceType.Mousemat].Model) throw new WrapperException("The previously loaded Mousemat got disconnected."); IsInitialized = true; }