예제 #1
0
        /// <summary>
        /// Initializes the device.
        /// </summary>
        public void Initialize(CorsairDeviceUpdateQueue deviceUpdateQueue)
        {
            DeviceUpdateQueue = deviceUpdateQueue;

            InitializeLayout();

            foreach (Led led in LedMapping.Values)
            {
                CorsairLedId ledId = (CorsairLedId)led.CustomData;
                if (ledId != CorsairLedId.Invalid)
                {
                    InternalLedMapping.Add(ledId, led);
                }
            }

            if (Size == Size.Invalid)
            {
                Rectangle ledRectangle = new Rectangle(this.Select(x => x.LedRectangle));
                Size = ledRectangle.Size + new Size(ledRectangle.Location.X, ledRectangle.Location.Y);
            }
        }
예제 #2
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);
        }