Example #1
0
        /// <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 general SDK availability.</param>
        /// <returns>The availability of the provided <see cref="CorsairDeviceType"/>.</returns>
        public static bool IsSDKAvailable(CorsairDeviceType?sdkType = null)
        {
            try
            {
                // ReSharper disable once RedundantIfElseBlock
                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);

                    case CorsairDeviceType.HeadsetStand:
                        return(HeadsetStandSDK != null);

                    default:
                        return(true);
                    }
                }
                else
                {
                    _CUESDK.Reload();
                    _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);
        }
Example #2
0
        /// <summary>
        /// Initializes the headset stand.
        /// </summary>
        public override void Initialize()
        {
            int deviceCount = _CUESDK.CorsairGetDeviceCount();

            // Get headset stand device index
            int MemoryModuleIndex = -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.MemoryModule)
                {
                    continue;
                }

                MemoryModuleIndex = i;
                break;
            }
            if (MemoryModuleIndex < 0)
            {
                throw new WrapperException("Can't determine headset stand device index");
            }

            _CorsairLedPositions nativeLedPositions = (_CorsairLedPositions)Marshal.PtrToStructure(_CUESDK.CorsairGetLedPositionsByDeviceIndex(MemoryModuleIndex), 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();
        }
Example #3
0
        /// <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.");
            }

            if (_onKeyPressedDelegate != null)
            {
                throw new WrapperException("Keypress-Callback is enabled.");
            }

            KeyboardSDK?.ResetLeds();
            MouseSDK?.ResetLeds();
            HeadsetSDK?.ResetLeds();
            MousematSDK?.ResetLeds();
            HeadsetStandSDK?.ResetLeds();

            _CUESDK.Reload();

            _CUESDK.CorsairPerformProtocolHandshake();

            CorsairError error = LastError;

            if (error != CorsairError.Success)
            {
                Throw(error, false);
            }

            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, false);
                }
            }
            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, false);
                }
            }

            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.");
                }
            }
            if (HeadsetStandSDK != null)
            {
                if (!reloadedDevices.ContainsKey(CorsairDeviceType.HeadsetStand) ||
                    HeadsetStandSDK.HeadsetStandDeviceInfo.Model != reloadedDevices[CorsairDeviceType.HeadsetStand].Model)
                {
                    throw new WrapperException("The previously loaded Headset Stand got disconnected.");
                }
            }

            error = LastError;
            if (error != CorsairError.Success)
            {
                Throw(error, false);
            }

            IsInitialized = true;
        }
Example #4
0
        // ReSharper disable once ExceptionNotThrown
        /// <summary>
        /// Initializes the CUE-SDK. This method should be called exactly ONE time, before anything else is done.
        /// </summary>
        /// <param name="exclusiveAccess">Specifies whether the application should request exclusive access or not.</param>
        /// <exception cref="WrapperException">Thrown if the SDK is already initialized, the SDK is not compatible to CUE or if CUE returns unknown devices.</exception>
        /// <exception cref="CUEException">Thrown if the CUE-SDK provides an error.</exception>
        public static void Initialize(bool exclusiveAccess = false)
        {
            if (IsInitialized)
            {
                throw new WrapperException("CueSDK is already initialized.");
            }

            _CUESDK.Reload();

            ProtocolDetails = new CorsairProtocolDetails(_CUESDK.CorsairPerformProtocolHandshake());

            CorsairError error = LastError;

            if (error != CorsairError.Success)
            {
                Throw(error, true);
            }

            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, true);
                }

                HasExclusiveAccess = true;
            }

            IList <ICueDevice> devices = new List <ICueDevice>();
            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
                }
                ICueDevice device;
                switch (info.Type)
                {
                case CorsairDeviceType.Keyboard:
                    device = KeyboardSDK = new CorsairKeyboard(new CorsairKeyboardDeviceInfo(nativeDeviceInfo));
                    break;

                case CorsairDeviceType.Mouse:
                    device = MouseSDK = new CorsairMouse(new CorsairMouseDeviceInfo(nativeDeviceInfo));
                    break;

                case CorsairDeviceType.Headset:
                    device = HeadsetSDK = new CorsairHeadset(new CorsairHeadsetDeviceInfo(nativeDeviceInfo));
                    break;

                case CorsairDeviceType.Mousemat:
                    device = MousematSDK = new CorsairMousemat(new CorsairMousematDeviceInfo(nativeDeviceInfo));
                    break;

                case CorsairDeviceType.HeadsetStand:
                    device = HeadsetStandSDK = new CorsairHeadsetStand(new CorsairHeadsetStandDeviceInfo(nativeDeviceInfo));
                    break;

                // ReSharper disable once RedundantCaseLabel
                case CorsairDeviceType.Unknown:
                default:
                    throw new WrapperException("Unknown Device-Type");
                }

                device.Initialize();
                devices.Add(device);

                error = LastError;
                if (error != CorsairError.Success)
                {
                    Throw(error, true);
                }
            }

            error = LastError;
            if (error != CorsairError.Success)
            {
                Throw(error, false);
            }

            InitializedDevices = new ReadOnlyCollection <ICueDevice>(devices);

            IsInitialized = true;
        }