<commit_before>﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections.Generic;
using CUE.NET;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Headset;
using CUE.NET.Devices.Keyboard;
using CUE.NET.Devices.Mouse;
using CUE.NET.Devices.Mousemat;
using CUE.NET.Exceptions;
using IdleRGB.Properties;
using System.Timers;

namespace IdleRGB
{
    internal class LedChanger
    {
        // Color settings.
        private readonly Color muteColor;
        private readonly Color nextColor;
        private readonly Color playPauseColor;
        private readonly Color prevColor;
        private readonly Color stopColor;

        private CorsairHeadset corsairHeadset;
        private CorsairKeyboard corsairKeyboard;
        private CorsairMouse corsairMouse;
        private CorsairMousemat corsairMousemat;

        Timer initializationTimer;

        /// <summary>
        ///     Initializes a new instance of the <see cref="LedChanger" /> class.
        /// </summary>
        public LedChanger()
        {
            stopColor = Settings.Default.stopColor;
            prevColor = Settings.Default.nextColor;
            playPauseColor = Settings.Default.playPauseColor;
            nextColor = Settings.Default.nextColor;
            muteColor = Settings.Default.muteColor;

            InitializeSDK();

            // Continues to look for CUE and adds devices.
            initializationTimer = new Timer(3000);
            initializationTimer.Elapsed += Timer1_Tick;
            initializationTimer.Enabled = true;
            GC.KeepAlive(initializationTimer);
        }


        /// <summary>
        ///     Attempts to initialize CUE SDK or looks for new devices.
        /// </summary>
        private void InitializeSDK()
        {
            try
            {
                if (CueSDK.IsInitialized)
                {
                    if (CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard))
                        corsairKeyboard = CueSDK.KeyboardSDK;

                    if (CueSDK.IsSDKAvailable(CorsairDeviceType.Mouse))
                        corsairMouse = CueSDK.MouseSDK;

                    if (CueSDK.IsSDKAvailable(CorsairDeviceType.Headset))
                        corsairHeadset = CueSDK.HeadsetSDK;

                    if (CueSDK.IsSDKAvailable(CorsairDeviceType.Mousemat))
                        corsairMousemat = CueSDK.MousematSDK;

                    initializationTimer.Stop();
                    initializationTimer.Dispose();
                }

                else
                    CueSDK.Initialize();
            }

            catch (WrapperException e)
            {
                Debug.WriteLine("Wrapper Exception! Message:" + e.Message);
            }

            catch (CUEException e)
            {
                Debug.WriteLine("CUE Exception! ErrorCode: " + Enum.GetName(typeof(CorsairError), e.Error));
            }
        }

        /// <summary>
        ///     Changes all LED colors with the exception of possible Media, Brightness, WinLock LEDs.
        /// </summary>
        /// <param name="newColor">The new <see cref="System.Drawing.Color" />.</param>
        public void ChangeLeds(Color newColor)
        {
            if (CueSDK.IsInitialized)
            {
                // No changes are done to these LEDs.
                List<CorsairLedId> skipLeds = new List<CorsairLedId>();
                skipLeds.Add(CorsairLedId.Stop);
                skipLeds.Add(CorsairLedId.ScanPreviousTrack);
                skipLeds.Add(CorsairLedId.PlayPause);
                skipLeds.Add(CorsairLedId.ScanNextTrack);
                skipLeds.Add(CorsairLedId.Mute);
                skipLeds.Add(CorsairLedId.Brightness);
                skipLeds.Add(CorsairLedId.WinLock);

                var initializedDevices = CueSDK.InitializedDevices.GetEnumerator();

                while (initializedDevices.MoveNext())
                {
                    try
                    {
                        var leds = initializedDevices.Current.GetEnumerator();

                        while (leds.MoveNext())
                        {<commit_msg>Bug fix.<commit_after>﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections.Generic;
using CUE.NET;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Headset;
using CUE.NET.Devices.Keyboard;
using CUE.NET.Devices.Mouse;
using CUE.NET.Devices.Mousemat;
using CUE.NET.Exceptions;
using IdleRGB.Properties;
using System.Timers;

namespace IdleRGB
{
    internal class LedChanger
    {
        // Color settings.
        private readonly Color muteColor;
        private readonly Color nextColor;
        private readonly Color playPauseColor;
        private readonly Color prevColor;
        private readonly Color stopColor;

        Timer initializationTimer;
        bool isCueReady = false;

        /// <summary>
        ///     Initializes a new instance of the <see cref="LedChanger" /> class.
        /// </summary>
        public LedChanger()
        {
            stopColor = Settings.Default.stopColor;
            prevColor = Settings.Default.nextColor;
            playPauseColor = Settings.Default.playPauseColor;
            nextColor = Settings.Default.nextColor;
            muteColor = Settings.Default.muteColor;

            InitializeSDK();

            // Continues to look for CUE and adds devices.
            initializationTimer = new Timer(10000);
            initializationTimer.Elapsed += Timer1_Tick;
            initializationTimer.Enabled = true;
            GC.KeepAlive(initializationTimer);
        }


        /// <summary>
        ///     Attempts to initialize CUE SDK or looks for new devices.
        /// </summary>
        private void InitializeSDK()
        {
            if (CueSDK.IsSDKAvailable() && !isCueReady)
            {
                isCueReady = true;
            }

            else if (CueSDK.IsSDKAvailable() && isCueReady)
            {
                try
                {
                    CueSDK.Initialize();
                    initializationTimer.Stop();
                    initializationTimer.Dispose();
                }

                catch (WrapperException e)
                {
                    Debug.WriteLine("Wrapper Exception! Message:" + e.Message);
                }

                catch (CUEException e)
                {
                    Debug.WriteLine("CUE Exception! ErrorCode: " + Enum.GetName(typeof(CorsairError), e.Error));

                }
            }
        }

        /// <summary>
        ///     Changes all LED colors with the exception of possible Media, Brightness, WinLock LEDs.
        /// </summary>
        /// <param name="newColor">The new <see cref="System.Drawing.Color" />.</param>
        public void ChangeLeds(Color newColor)
        {
            if (CueSDK.IsInitialized)
            {
                // No changes are done to these LEDs.
                List<CorsairLedId> skipLeds = new List<CorsairLedId>();
                skipLeds.Add(CorsairLedId.Stop);
                skipLeds.Add(CorsairLedId.ScanPreviousTrack);
                skipLeds.Add(CorsairLedId.PlayPause);
                skipLeds.Add(CorsairLedId.ScanNextTrack);
                skipLeds.Add(CorsairLedId.Mute);
                skipLeds.Add(CorsairLedId.Brightness);
                skipLeds.Add(CorsairLedId.WinLock);

                var initializedDevices = CueSDK.InitializedDevices.GetEnumerator();

                while (initializedDevices.MoveNext())
                {
                    try
                    {
                        var leds = initializedDevices.Current.GetEnumerator();

                        while (leds.MoveNext())
                        {
{'fraction_non_alphanumeric': 0.08628005657708628, 'fraction_numerical': 0.0014144271570014145, 'mean_word_length': 1.614453781512605, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5681463', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2014, 'n_words': 414}