-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathOpenRGBDeviceProvider.cs
More file actions
227 lines (190 loc) · 7.55 KB
/
OpenRGBDeviceProvider.cs
File metadata and controls
227 lines (190 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using RGB.NET.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRGB.NET;
using OpenRgbDevice = OpenRGB.NET.Device;
namespace RGB.NET.Devices.OpenRGB;
/// <inheritdoc />
/// <summary>
/// Represents a device provider responsible for OpenRGB devices.
/// </summary>
public sealed class OpenRGBDeviceProvider : AbstractRGBDeviceProvider
{
#region Properties & Fields
// ReSharper disable once InconsistentNaming
private static readonly object _lock = new();
private readonly List<OpenRGBClientWrapper> _clients = [];
private static OpenRGBDeviceProvider? _instance;
/// <summary>
/// Gets the singleton <see cref="OpenRGBDeviceProvider"/> instance.
/// </summary>
public static OpenRGBDeviceProvider Instance
{
get
{
lock (_lock)
return _instance ?? new OpenRGBDeviceProvider();
}
}
/// <summary>
/// Gets a list of all defined device-definitions.
/// </summary>
public List<OpenRGBServerDefinition> DeviceDefinitions { get; } = [];
/// <summary>
/// Indicates whether all devices will be added, or just the ones with a 'Direct' mode. Defaults to false.
/// </summary>
public bool ForceAddAllDevices { get; set; } = false;
/// <summary>
/// Defines which device types will be separated by zones. Defaults to <see cref="RGBDeviceType.LedStripe" /> | <see cref="RGBDeviceType.Mainboard"/> | <see cref="RGBDeviceType.Speaker" />.
/// </summary>
public RGBDeviceType PerZoneDeviceFlag { get; } = RGBDeviceType.LedStripe | RGBDeviceType.Mainboard | RGBDeviceType.Speaker;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="OpenRGBDeviceProvider"/> class.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public OpenRGBDeviceProvider()
{
lock (_lock)
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instance of type {nameof(OpenRGBDeviceProvider)}");
_instance = this;
}
}
#endregion
#region Methods
/// <summary>
/// Adds the specified <see cref="OpenRGBServerDefinition" /> to this device-provider.
/// </summary>
/// <param name="deviceDefinition">The <see cref="OpenRGBServerDefinition"/> to add.</param>
// ReSharper disable once UnusedMember.Global
public void AddDeviceDefinition(OpenRGBServerDefinition deviceDefinition) => DeviceDefinitions.Add(deviceDefinition);
/// <inheritdoc />
protected override void InitializeSDK()
{
foreach (OpenRGBServerDefinition? deviceDefinition in DeviceDefinitions)
{
try
{
OpenRgbClient openRgb = new(ip: deviceDefinition.Ip, port: deviceDefinition.Port, name: deviceDefinition.ClientName, autoConnect: true);
OpenRGBClientWrapper wrapper = new(openRgb);
openRgb.DeviceListUpdated += (_, _) => RefreshClient(wrapper);
_clients.Add(wrapper);
deviceDefinition.Connected = true;
}
catch (Exception e)
{
deviceDefinition.Connected = false;
deviceDefinition.LastError = e.Message;
Throw(e);
}
}
}
private void RefreshClient(OpenRGBClientWrapper openRgbClient)
{
List<IRGBDevice> currentDevices = new(openRgbClient.Devices);
List<IRGBDevice> newDevices = GetOrgbClientDevices(openRgbClient.OpenRgbClient)
.SelectMany((device, i) => SplitDevice(device, i, openRgbClient.OpenRgbClient)).ToList();
foreach (IRGBDevice rgbDevice in currentDevices.Except(newDevices))
{
RemoveDevice(rgbDevice);
openRgbClient.Devices.Remove(rgbDevice);
}
foreach (IRGBDevice rgbDevice in newDevices)
{
openRgbClient.Devices.Add(rgbDevice);
AddDevice(rgbDevice);
}
}
/// <inheritdoc />
protected override IEnumerable<IRGBDevice> LoadDevices()
{
return _clients.SelectMany(LoadClientDevices);
}
private IEnumerable<IRGBDevice> LoadClientDevices(OpenRGBClientWrapper client)
{
List<IRGBDevice> devices = GetOrgbClientDevices(client.OpenRgbClient)
.SelectMany((device, i) => SplitDevice(device, i, client.OpenRgbClient))
.ToList();
client.Devices.AddRange(devices);
return devices;
}
private IEnumerable<OpenRgbDevice> GetOrgbClientDevices(IOpenRgbClient openRgb)
{
int deviceCount = openRgb.GetControllerCount();
for (int i = 0; i < deviceCount; i++)
{
OpenRgbDevice device = openRgb.GetControllerData(i);
int directModeIndex = Array.FindIndex(device.Modes, d => d.Name == "Direct");
if (directModeIndex != -1)
{
//set the device to direct mode if it has it
openRgb.UpdateMode(i, directModeIndex);
}
else if (!ForceAddAllDevices)
{
//if direct mode does not exist
//and if we're not forcing, continue to the next device.
continue;
}
if (device.Zones.Length == 0)
continue;
if (device.Zones.All(z => z.LedCount == 0))
continue;
yield return device;
}
}
private IEnumerable<IRGBDevice> SplitDevice(OpenRgbDevice device, int i, IOpenRgbClient openRgb)
{
IDeviceUpdateTrigger clientUpdateTrigger = GetUpdateTrigger();
OpenRGBUpdateQueue updateQueue = new(clientUpdateTrigger, i, openRgb, device);
bool anyZoneHasSegments = device.Zones.Any(z => z.Segments.Length > 0);
bool splitDeviceByZones = anyZoneHasSegments || PerZoneDeviceFlag.HasFlag(Helper.GetRgbNetDeviceType(device.Type));
if (!splitDeviceByZones)
{
yield return new OpenRGBGenericDevice(new OpenRGBDeviceInfo(device), updateQueue);
yield break;
}
int totalLedCount = 0;
foreach (Zone zone in device.Zones)
{
if (zone.LedCount <= 0)
continue;
if (zone.Segments.Length <= 0)
{
string zoneId = zone.Name;
yield return new OpenRGBZoneDevice(new OpenRGBDeviceInfo(device, zoneId), totalLedCount, zone, updateQueue);
totalLedCount += (int)zone.LedCount;
}
else
{
foreach (Segment segment in zone.Segments)
{
string zoneId = segment.Name;
yield return new OpenRGBSegmentDevice(new OpenRGBDeviceInfo(device, zoneId), totalLedCount, segment, updateQueue);
totalLedCount += (int)segment.LedCount;
}
}
}
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
lock (_lock)
{
base.Dispose(disposing);
foreach (OpenRGBClientWrapper wrapper in _clients)
{
try { wrapper.Dispose(); }
catch { /* at least we tried */ }
}
_clients.Clear();
DeviceDefinitions.Clear();
IdGenerator.ResetCounter(typeof(OpenRGBDeviceProvider));
_instance = null;
}
}
#endregion
}