Skip to content

Commit 3ec137a

Browse files
author
Aytackydln
committed
implement array backing dictionary for device key colors
1 parent 5b59da1 commit 3ec137a

6 files changed

Lines changed: 156 additions & 19 deletions

File tree

Project-Aurora/AuroraCommon/Data/MemorySharedArray.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public sealed class MemorySharedArray<T> : SignaledMemoryObject, IEnumerable<T>
1010
private const bool CheckFileSize = false;
1111
public int Count { get; }
1212

13-
private static readonly int ElementSize = Marshal.SizeOf(typeof(T));
13+
private static readonly int ElementSize = Marshal.SizeOf<T>();
1414

1515
private readonly MemoryMappedFile _mmf;
1616
private readonly MemoryMappedViewAccessor _accessor;
@@ -157,6 +157,21 @@ public void WriteCollection(IEnumerable<T> list)
157157

158158
SignalUpdated();
159159
}
160+
161+
public void WriteArray(T[] array)
162+
{
163+
// Write the data at the calculated offset
164+
var offset = HeaderOffset();
165+
// Marshal the struct to a byte array
166+
Marshal.StructureToPtr(array, _writePointer, true);
167+
168+
if (_accessor.CanWrite && !Disposed)
169+
{
170+
_accessor.WriteArray(offset, _writeBuffer, 0, _writeBuffer.Length);
171+
}
172+
173+
SignalUpdated();
174+
}
160175

161176
private static int HeaderOffset()
162177
{

Project-Aurora/AuroraCommon/Devices/DeviceKeys.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -823,12 +823,6 @@ public enum DeviceKeys
823823
[Description("G19")]
824824
G19 = 126,
825825

826-
/// <summary>
827-
/// Macrokey 20 key
828-
/// </summary>
829-
[Description("G20")]
830-
G20 = 127,
831-
832826
/// <summary>
833827
/// Brand Logo
834828
/// </summary>
@@ -1953,5 +1947,5 @@ public enum DeviceKeys
19531947
/// None
19541948
/// </summary>
19551949
[Description("None")]
1956-
NONE = -1,
1950+
NONE = 127, //TODO this is a temporary fix for DeviceKeyStore being an array
19571951
};

Project-Aurora/Project-Aurora/ConfigUi.xaml.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Frozen;
32
using System.Collections.Generic;
43
using System.ComponentModel;
54
using System.Diagnostics;
@@ -21,7 +20,6 @@
2120
using AuroraRgb.Modules;
2221
using AuroraRgb.Modules.GameStateListen;
2322
using AuroraRgb.Modules.Layouts;
24-
using AuroraRgb.Profiles;
2523
using AuroraRgb.Settings;
2624
using AuroraRgb.Settings.Controls;
2725
using AuroraRgb.Settings.Layers;
@@ -226,8 +224,8 @@ private async Task KeyboardTimerCallback()
226224
ConvertToMediaColors(keyLights);
227225
await Dispatcher.InvokeAsync(_updateKeyboardLayouts, dispatcherPriority, _keyboardUpdateCancel.Token);
228226
}
229-
230-
private void ConvertToMediaColors(Dictionary<DeviceKeys, SimpleColor> keyColors)
227+
228+
private void ConvertToMediaColors(IDictionary<DeviceKeys, SimpleColor> keyColors)
231229
{
232230
foreach (var (key, color) in keyColors)
233231
{

Project-Aurora/Project-Aurora/Devices/DeviceManager.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using System.Runtime.CompilerServices;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using AuroraRgb.EffectsEngine;
1011
using AuroraRgb.Modules;
12+
using AuroraRgb.Utils;
1113
using Common;
1214
using Common.Data;
1315
using Common.Devices;
@@ -46,7 +48,7 @@ public sealed class DeviceManager : IDisposable
4648
public DeviceManager(AuroraControlInterface auroraControlInterface)
4749
{
4850
_auroraControlInterface = auroraControlInterface;
49-
_sharedDeviceColor = new MemorySharedArray<SimpleColor>(Constants.DeviceLedMap, Constants.MaxKeyId);
51+
_sharedDeviceColor = new MemorySharedArray<SimpleColor>(Constants.DeviceLedMap, Effects.MaxDeviceId);
5052

5153
_deviceManagerInfo = new MemorySharedStruct<DeviceManagerInfo>(Constants.DeviceInformations);
5254
_deviceManagerInfo.Updated += OnDeviceManagerInfoOnUpdated;
@@ -179,9 +181,9 @@ public void Detach()
179181
_detached = true;
180182
}
181183

182-
public void UpdateDevices(Dictionary<DeviceKeys, SimpleColor> keyColors)
184+
public void UpdateDevices(DeviceKeyStore keyColors)
183185
{
184-
_sharedDeviceColor.WriteDictionary(keyColors);
186+
_sharedDeviceColor.WriteArray(keyColors.ColorArray);
185187
}
186188

187189
public void Dispose()

Project-Aurora/Project-Aurora/EffectsEngine/Effects.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Drawing;
4-
using System.Linq;
54
using System.Threading.Tasks;
65
using AuroraRgb.Bitmaps;
76
using AuroraRgb.Devices;
@@ -36,7 +35,8 @@ public int GetHashCode(Enum obj)
3635
public class Effects(Task<DeviceManager> deviceManager)
3736
{
3837
//Optimization: used to mitigate dictionary resizing
39-
public static readonly int MaxDeviceId = Enum.GetValues(typeof(DeviceKeys)).Cast<int>().Max() + 1;
38+
//TODO https://github.com/skarllot/EnumUtilities/issues/469
39+
public const int MaxDeviceId = 2000;
4040

4141
private static readonly DeviceKeys[] PossiblePeripheralKeys =
4242
[
@@ -152,7 +152,7 @@ public static EffectCanvas Canvas
152152
}
153153
}
154154

155-
private readonly Dictionary<DeviceKeys, SimpleColor> _keyColors = new(MaxDeviceId, EnumHashGetter.Instance as IEqualityComparer<DeviceKeys>);
155+
private readonly DeviceKeyStore _keyColors = new();
156156

157157
private RuntimeChangingLayer Background { get; } = new("Background Layer");
158158
private readonly Color _backgroundColor = Color.Black;
@@ -205,7 +205,7 @@ private void PushFrameLocked(EffectFrame frame)
205205
frame.Dispose();
206206
}
207207

208-
public Dictionary<DeviceKeys, SimpleColor> GetKeyboardLights()
208+
public DeviceKeyStore GetKeyboardLights()
209209
{
210210
return _keyColors;
211211
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using AuroraRgb.EffectsEngine;
5+
using Common;
6+
using Common.Devices;
7+
8+
namespace AuroraRgb.Utils;
9+
10+
public class DeviceKeyStore : IDictionary<DeviceKeys, SimpleColor>
11+
{
12+
public readonly SimpleColor[] ColorArray = new SimpleColor[Effects.MaxDeviceId];
13+
private readonly bool[] _keyExists = new bool[Effects.MaxDeviceId];
14+
15+
public ICollection<DeviceKeys> Keys => Enum.GetValues<DeviceKeys>();
16+
public ICollection<SimpleColor> Values => ColorArray;
17+
18+
public int Count => Effects.MaxDeviceId;
19+
public bool IsReadOnly => false;
20+
21+
public IEnumerator<KeyValuePair<DeviceKeys, SimpleColor>> GetEnumerator()
22+
{
23+
for (var i = 0; i < Effects.MaxDeviceId; i++)
24+
{
25+
if (_keyExists[i])
26+
yield return new KeyValuePair<DeviceKeys, SimpleColor>((DeviceKeys)i, ColorArray[i]);
27+
}
28+
}
29+
30+
IEnumerator IEnumerable.GetEnumerator()
31+
{
32+
return GetEnumerator();
33+
}
34+
35+
public void Add(KeyValuePair<DeviceKeys, SimpleColor> item)
36+
{
37+
var index = GetEnumHash(item.Key);
38+
_keyExists[index] = true;
39+
ColorArray[index] = item.Value;
40+
}
41+
42+
public void Clear()
43+
{
44+
Array.Clear(_keyExists, 0, _keyExists.Length);
45+
Array.Clear(ColorArray, 0, ColorArray.Length);
46+
}
47+
48+
public bool Contains(KeyValuePair<DeviceKeys, SimpleColor> item)
49+
{
50+
var index = GetEnumHash(item.Key);
51+
return _keyExists[index];
52+
}
53+
54+
public void CopyTo(KeyValuePair<DeviceKeys, SimpleColor>[] array, int arrayIndex)
55+
{
56+
foreach (var kvp in this)
57+
{
58+
array[arrayIndex++] = kvp;
59+
}
60+
}
61+
62+
public bool Remove(KeyValuePair<DeviceKeys, SimpleColor> item)
63+
{
64+
var exists = Contains(item);
65+
var index = GetEnumHash(item.Key);
66+
_keyExists[index] = false;
67+
return exists;
68+
}
69+
70+
public void Add(DeviceKeys key, SimpleColor value)
71+
{
72+
var index = GetEnumHash(key);
73+
_keyExists[index] = true;
74+
ColorArray[index] = value;
75+
}
76+
77+
public bool ContainsKey(DeviceKeys key)
78+
{
79+
var index = GetEnumHash(key);
80+
return _keyExists[index];
81+
}
82+
83+
public bool Remove(DeviceKeys key)
84+
{
85+
var exists = ContainsKey(key);
86+
var index = GetEnumHash(key);
87+
_keyExists[index] = false;
88+
return exists;
89+
}
90+
91+
public bool TryGetValue(DeviceKeys key, out SimpleColor value)
92+
{
93+
var index = GetEnumHash(key);
94+
if (index == -1)
95+
{
96+
value = SimpleColor.Black;
97+
return true;
98+
}
99+
100+
if (_keyExists[index])
101+
{
102+
value = ColorArray[index];
103+
return true;
104+
}
105+
value = SimpleColor.Transparent;
106+
return false;
107+
}
108+
109+
public SimpleColor this[DeviceKeys key]
110+
{
111+
get
112+
{
113+
var index = GetEnumHash(key);
114+
return ColorArray[index];
115+
}
116+
set
117+
{
118+
var index = GetEnumHash(key);
119+
_keyExists[index] = true;
120+
ColorArray[index] = value;
121+
}
122+
}
123+
124+
private static int GetEnumHash(Enum obj)
125+
{
126+
return Convert.ToInt32(obj);
127+
}
128+
}

0 commit comments

Comments
 (0)