Пример #1
0
        private static ICorsairRGBDevice GetRGBDevice(CorsairRGBDeviceInfo info, int i, _CorsairDeviceInfo nativeDeviceInfo)
        {
            switch (info.CorsairDeviceType)
            {
            case CorsairDeviceType.Keyboard:
                return(new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo)));

            case CorsairDeviceType.Mouse:
                return(new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo)));

            case CorsairDeviceType.Headset:
                return(new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo)));

            case CorsairDeviceType.Mousepad:
                return(new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo)));

            case CorsairDeviceType.HeadsetStand:
                return(new CorsairHeadsetStandRGBDevice(new CorsairHeadsetStandRGBDeviceInfo(i, nativeDeviceInfo)));

            // ReSharper disable once RedundantCaseLabel
            case CorsairDeviceType.Unknown:
            default:
                throw new RGBDeviceException("Unknown Device-Type");
            }
        }
Пример #2
0
        //TODO DarthAffe 07.07.2018: DAP is a fan right now, that's most likely wrong
        /// <inheritdoc />
        /// <summary>
        /// Internal constructor of managed <see cref="T:RGB.NET.Devices.Corsair.CorsairCustomRGBDeviceInfo" />.
        /// </summary>
        /// <param name="info">The info describing the the <see cref="T:RGB.NET.Devices.Corsair.CorsairCustomRGBDevice" />.</param>
        /// <param name="nativeInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairDeviceInfo" />-struct</param>
        /// <param name="channelDeviceInfo">The native <see cref="T:RGB.NET.Devices.Corsair.Native._CorsairChannelDeviceInfo"/> representing this device.</param>
        /// <param name="referenceCorsairLed">The id of the first led of this device.</param>
        /// <param name="modelCounter">A dictionary containing counters to create unique names for equal devices models.</param>
        internal CorsairCustomRGBDeviceInfo(CorsairRGBDeviceInfo info, _CorsairDeviceInfo nativeInfo,
                                            _CorsairChannelDeviceInfo channelDeviceInfo,
                                            CorsairLedId referenceCorsairLed, Dictionary <string, int> modelCounter)
            : base(info.CorsairDeviceIndex, GetDeviceType(channelDeviceInfo.type), nativeInfo,
                   GetModelName(info, channelDeviceInfo), modelCounter)
        {
            this.ReferenceCorsairLed = referenceCorsairLed;

            LedCount = channelDeviceInfo.deviceLedCount;
        }
Пример #3
0
        private static IEnumerable <ICorsairRGBDevice> GetRGBDevice(CorsairRGBDeviceInfo info, int i, _CorsairDeviceInfo nativeDeviceInfo, Dictionary <string, int> modelCounter)
        {
            switch (info.CorsairDeviceType)
            {
            case CorsairDeviceType.Keyboard:
                yield return(new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo, modelCounter)));

                break;

            case CorsairDeviceType.Mouse:
                yield return(new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo, modelCounter)));

                break;

            case CorsairDeviceType.Headset:
                yield return(new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo, modelCounter)));

                break;

            case CorsairDeviceType.Mousepad:
                yield return(new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo, modelCounter)));

                break;

            case CorsairDeviceType.HeadsetStand:
                yield return(new CorsairHeadsetStandRGBDevice(new CorsairHeadsetStandRGBDeviceInfo(i, nativeDeviceInfo, modelCounter)));

                break;

            case CorsairDeviceType.MemoryModule:
                yield return(new CorsairMemoryRGBDevice(new CorsairMemoryRGBDeviceInfo(i, nativeDeviceInfo, modelCounter)));

                break;

            case CorsairDeviceType.Cooler:
            case CorsairDeviceType.CommanderPro:
            case CorsairDeviceType.LightningNodePro:
                _CorsairChannelsInfo channelsInfo = nativeDeviceInfo.channels;
                if (channelsInfo != null)
                {
                    IntPtr channelInfoPtr = channelsInfo.channels;

                    for (int channel = 0; channel < channelsInfo.channelsCount; channel++)
                    {
                        CorsairLedId referenceLed = channel == 0 ? CorsairLedId.CustomDeviceChannel1Led1 : CorsairLedId.CustomDeviceChannel2Led1;

                        _CorsairChannelInfo channelInfo = (_CorsairChannelInfo)Marshal.PtrToStructure(channelInfoPtr, typeof(_CorsairChannelInfo));

                        int    channelDeviceInfoStructSize = Marshal.SizeOf(typeof(_CorsairChannelDeviceInfo));
                        IntPtr channelDeviceInfoPtr        = channelInfo.devices;

                        for (int device = 0; device < channelInfo.devicesCount; device++)
                        {
                            _CorsairChannelDeviceInfo channelDeviceInfo = (_CorsairChannelDeviceInfo)Marshal.PtrToStructure(channelDeviceInfoPtr, typeof(_CorsairChannelDeviceInfo));

                            yield return(new CorsairCustomRGBDevice(new CorsairCustomRGBDeviceInfo(info.CorsairDeviceIndex, nativeDeviceInfo, channelDeviceInfo, referenceLed, modelCounter)));

                            referenceLed += channelDeviceInfo.deviceLedCount;

                            channelDeviceInfoPtr = new IntPtr(channelDeviceInfoPtr.ToInt64() + channelDeviceInfoStructSize);
                        }

                        int channelInfoStructSize = Marshal.SizeOf(typeof(_CorsairChannelInfo));
                        channelInfoPtr = new IntPtr(channelInfoPtr.ToInt64() + channelInfoStructSize);
                    }
                }

                break;


            // ReSharper disable once RedundantCaseLabel
            case CorsairDeviceType.Unknown:
            default:
                throw new RGBDeviceException("Unknown Device-Type");
            }
        }
Пример #4
0
        /// <inheritdoc />
        /// <exception cref="RGBDeviceException">Thrown if the SDK is already initialized or if the SDK is not compatible to CUE.</exception>
        /// <exception cref="CUEException">Thrown if the CUE-SDK provides an error.</exception>
        public bool Initialize(RGBDeviceType loadFilter = RGBDeviceType.All, bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
        {
            IsInitialized = false;

            try
            {
                UpdateTrigger?.Stop();

                _CUESDK.Reload();

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

                CorsairError error = LastError;
                if (error != CorsairError.Success)
                {
                    throw new CUEException(error);
                }

                if (ProtocolDetails.BreakingChanges)
                {
                    throw new RGBDeviceException("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 (exclusiveAccessIfPossible)
                {
                    if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl))
                    {
                        throw new CUEException(LastError);
                    }

                    HasExclusiveAccess = true;
                }
                else
                {
                    HasExclusiveAccess = false;
                }

                // DarthAffe 07.07.2018: 127 is CUE, we want to directly compete with it as in older versions.
                if (!_CUESDK.CorsairSetLayerPriority(127))
                {
                    throw new CUEException(LastError);
                }

                Dictionary <string, int> modelCounter = new Dictionary <string, int>();
                IList <IRGBDevice>       devices      = new List <IRGBDevice>();
                int deviceCount = _CUESDK.CorsairGetDeviceCount();
                for (int i = 0; i < deviceCount; i++)
                {
                    try
                    {
                        _CorsairDeviceInfo   nativeDeviceInfo = (_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo));
                        CorsairRGBDeviceInfo info             = new CorsairRGBDeviceInfo(i, RGBDeviceType.Unknown, nativeDeviceInfo, modelCounter);
                        if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting))
                        {
                            continue; // Everything that doesn't support lighting control is useless
                        }
                        CorsairDeviceUpdateQueue deviceUpdateQueue = null;
                        foreach (ICorsairRGBDevice device in GetRGBDevice(info, i, nativeDeviceInfo, modelCounter))
                        {
                            if ((device == null) || !loadFilter.HasFlag(device.DeviceInfo.DeviceType))
                            {
                                continue;
                            }

                            if (deviceUpdateQueue == null)
                            {
                                deviceUpdateQueue = new CorsairDeviceUpdateQueue(UpdateTrigger, info.CorsairDeviceIndex);
                            }

                            device.Initialize(deviceUpdateQueue);
                            AddSpecialParts(device);

                            error = LastError;
                            if (error != CorsairError.Success)
                            {
                                throw new CUEException(error);
                            }

                            devices.Add(device);
                        }
                    }
                    catch { if (throwExceptions)
                            {
                                throw;
                            }
                    }
                }

                UpdateTrigger?.Start();

                Devices       = new ReadOnlyCollection <IRGBDevice>(devices);
                IsInitialized = true;
            }
            catch
            {
                Reset();
                if (throwExceptions)
                {
                    throw;
                }
                return(false);
            }

            return(true);
        }
Пример #5
0
        /// <inheritdoc />
        /// <exception cref="RGBDeviceException">Thrown if the SDK is already initialized or if the SDK is not compatible to CUE.</exception>
        /// <exception cref="CUEException">Thrown if the CUE-SDK provides an error.</exception>
        public bool Initialize(bool exclusiveAccessIfPossible = false, bool throwExceptions = false)
        {
            IsInitialized = false;

            try
            {
                _CUESDK.Reload();

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

                CorsairError error = LastError;
                if (error != CorsairError.Success)
                {
                    throw new CUEException(error);
                }

                if (ProtocolDetails.BreakingChanges)
                {
                    throw new RGBDeviceException("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 (exclusiveAccessIfPossible)
                {
                    if (!_CUESDK.CorsairRequestControl(CorsairAccessMode.ExclusiveLightingControl))
                    {
                        throw new CUEException(LastError);
                    }

                    HasExclusiveAccess = true;
                }
                else
                {
                    HasExclusiveAccess = false;
                }

                IList <IRGBDevice> devices = new List <IRGBDevice>();
                int deviceCount            = _CUESDK.CorsairGetDeviceCount();
                for (int i = 0; i < deviceCount; i++)
                {
                    try
                    {
                        _CorsairDeviceInfo   nativeDeviceInfo = (_CorsairDeviceInfo)Marshal.PtrToStructure(_CUESDK.CorsairGetDeviceInfo(i), typeof(_CorsairDeviceInfo));
                        CorsairRGBDeviceInfo info             = new CorsairRGBDeviceInfo(i, RGBDeviceType.Unknown, nativeDeviceInfo);
                        if (!info.CapsMask.HasFlag(CorsairDeviceCaps.Lighting))
                        {
                            continue; // Everything that doesn't support lighting control is useless
                        }
                        ICorsairRGBDevice device;
                        switch (info.CorsairDeviceType)
                        {
                        case CorsairDeviceType.Keyboard:
                            device = new CorsairKeyboardRGBDevice(new CorsairKeyboardRGBDeviceInfo(i, nativeDeviceInfo));
                            break;

                        case CorsairDeviceType.Mouse:
                            device = new CorsairMouseRGBDevice(new CorsairMouseRGBDeviceInfo(i, nativeDeviceInfo));
                            break;

                        case CorsairDeviceType.Headset:
                            device = new CorsairHeadsetRGBDevice(new CorsairHeadsetRGBDeviceInfo(i, nativeDeviceInfo));
                            break;

                        case CorsairDeviceType.Mousepad:
                            device = new CorsairMousepadRGBDevice(new CorsairMousepadRGBDeviceInfo(i, nativeDeviceInfo));
                            break;

                        case CorsairDeviceType.HeadsetStand:
                            device = new CorsairHeadsetStandRGBDevice(new CorsairHeadsetStandRGBDeviceInfo(i, nativeDeviceInfo));
                            break;

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

                        error = LastError;
                        if (error != CorsairError.Success)
                        {
                            throw new CUEException(error);
                        }

                        devices.Add(device);
                    }
                    catch { if (throwExceptions)
                            {
                                throw;
                            }
                    }
                }

                Devices       = new ReadOnlyCollection <IRGBDevice>(devices);
                IsInitialized = true;
            }
            catch
            {
                Reset();
                if (throwExceptions)
                {
                    throw;
                }
                return(false);
            }

            return(true);
        }