diff --git a/src/Ryujinx.Common/Utilities/Rainbow.cs b/src/Ryujinx.Common/Utilities/Rainbow.cs index 3b49872c2..a3db94925 100644 --- a/src/Ryujinx.Common/Utilities/Rainbow.cs +++ b/src/Ryujinx.Common/Utilities/Rainbow.cs @@ -1,6 +1,7 @@ using Gommon; using System; using System.Drawing; +using System.Threading; using System.Threading.Tasks; namespace Ryujinx.Common.Utilities @@ -18,7 +19,7 @@ namespace Ryujinx.Common.Utilities { while (CyclingEnabled) { - await Task.Delay(15); + await Task.Delay(20); Tick(); } }); @@ -32,29 +33,47 @@ namespace Ryujinx.Common.Utilities public static float Speed { get; set; } = 1; + + private static readonly Lock _lock = new(); - public static Color Color { get; private set; } = Color.Blue; + private static Color _color = Color.Blue; + + public static ref Color Color + { + get + { + lock (_lock) + { + return ref _color; + } + } + } public static void Tick() { - Color = HsbToRgb((Color.GetHue() + Speed) / 360); + lock (_lock) + { + _color = HsbToRgb((_color.GetHue() + Speed) / 360); - UpdatedHandler.Call(Color.ToArgb()); + _updatedHandler.Call(_color.ToArgb()); + } } public static void Reset() { - Color = Color.Blue; - UpdatedHandler.Clear(); + _updatedHandler.Clear(); + + lock (_lock) + _color = Color.Blue; } public static event Action Updated { - add => UpdatedHandler.Add(value); - remove => UpdatedHandler.Remove(value); + add => _updatedHandler.Add(value); + remove => _updatedHandler.Remove(value); } - internal static Event UpdatedHandler = new(); + private static readonly Event _updatedHandler = new(); private static Color HsbToRgb(float hue, float saturation = 1, float brightness = 1) { diff --git a/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs b/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs index 8809c83f0..7d40bac66 100644 --- a/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs +++ b/src/Ryujinx.Input.SDL2/SDL2Gamepad.cs @@ -148,8 +148,6 @@ namespace Ryujinx.Input.SDL2 { if (disposing && _gamepadHandle != nint.Zero) { - Rainbow.Updated -= RainbowColorChanged; - SDL_GameControllerClose(_gamepadHandle); _gamepadHandle = nint.Zero; @@ -227,15 +225,6 @@ namespace Ryujinx.Input.SDL2 private static Vector3 RadToDegree(Vector3 rad) => rad * (180 / MathF.PI); private static Vector3 GsToMs2(Vector3 gs) => gs / SDL_STANDARD_GRAVITY; - - private void RainbowColorChanged(int packedRgb) - { - if (!_configuration.Led.UseRainbow) return; - - SetLed((uint)packedRgb); - } - - private bool _rainbowColorEnabled; public void SetConfiguration(InputConfig configuration) { @@ -247,19 +236,10 @@ namespace Ryujinx.Input.SDL2 { if (_configuration.Led.TurnOffLed) (this as IGamepad).ClearLed(); - else switch (_configuration.Led.UseRainbow) - { - case true when !_rainbowColorEnabled: - Rainbow.Updated += RainbowColorChanged; - _rainbowColorEnabled = true; - break; - case false when _rainbowColorEnabled: - Rainbow.Updated -= RainbowColorChanged; - _rainbowColorEnabled = false; - break; - } + else if (_configuration.Led.UseRainbow) + SetLed((uint)Rainbow.Color.ToArgb()); - if (!_configuration.Led.TurnOffLed && !_rainbowColorEnabled) + if (!_configuration.Led.TurnOffLed && !_configuration.Led.UseRainbow) SetLed(_configuration.Led.LedColor); }