Skip to content

Commit 8227e46

Browse files
feat: ability to restart OpenRGB
1 parent dfbc9aa commit 8227e46

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

src/ChromaControl.SDK.OpenRGB/IOpenRGBService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public interface IOpenRGBService
3535
/// <param name="config">The <see cref="JsonNode"/> configuration.</param>
3636
void UpdateConfiguration(JsonNode config);
3737

38+
/// <summary>
39+
/// Restarts the service.
40+
/// </summary>
41+
/// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
42+
/// <returns>A <see cref="Task"/>.</returns>
43+
Task Restart(CancellationToken cancellationToken = default);
44+
3845
/// <summary>
3946
/// Updates the device list.
4047
/// </summary>

src/ChromaControl.SDK.OpenRGB/Internal/OpenRGBManager.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ internal sealed partial class OpenRGBManager : IDisposable
1313
{
1414
private static bool Is32Bit => nint.Size == 4;
1515

16-
private readonly Process _process = CreateProcess();
16+
private Process? _process;
1717
private readonly Job _job;
1818
private readonly string _configFilePath = Path.Combine(OpenRGBConstants.ConfigPath, "OpenRGB.json");
1919

2020
public OpenRGBManager()
2121
{
2222
_job = new();
23+
_job.AssignProcess(Process.GetCurrentProcess());
2324
}
2425

2526
public void UpdateConfigFile(JsonNode config)
@@ -34,20 +35,33 @@ public void UpdateConfigFile(JsonNode config)
3435

3536
public void Start()
3637
{
37-
_job.AssignProcess(Process.GetCurrentProcess());
38-
39-
_process.Start();
38+
if (_process == null)
39+
{
40+
_process = CreateProcess();
41+
_process.Start();
42+
}
4043
}
4144

4245
public void Stop()
4346
{
44-
_process.Kill();
47+
if (_process != null)
48+
{
49+
_process.Kill();
50+
_process.Dispose();
51+
_process = null;
52+
}
53+
54+
var processes = Process.GetProcessesByName("ChromaControl.OpenRGB");
55+
56+
foreach (var process in processes)
57+
{
58+
process.Kill();
59+
}
4560
}
4661

4762
public void Dispose()
4863
{
49-
_process.Kill();
50-
_process.Dispose();
64+
Stop();
5165

5266
_job.Dispose();
5367

src/ChromaControl.SDK.OpenRGB/OpenRGBService.cs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace ChromaControl.SDK.OpenRGB;
1414
/// <inheritdoc/>
1515
public class OpenRGBService : IOpenRGBService, IAsyncDisposable
1616
{
17+
private NativeOpenRGBService _service;
18+
private BlockingCollection<OpenRGBDevice> _devices;
1719
private readonly OpenRGBManager _manager;
18-
private readonly NativeOpenRGBService _service;
19-
private readonly BlockingCollection<OpenRGBDevice> _devices;
2020

2121
/// <inheritdoc/>
2222
public bool Started { get; internal set; }
@@ -45,6 +45,26 @@ public void UpdateConfiguration(JsonNode config)
4545
_manager.UpdateConfigFile(config);
4646
}
4747

48+
/// <inheritdoc/>
49+
public async Task Restart(CancellationToken cancellationToken = default)
50+
{
51+
if (Started)
52+
{
53+
Started = false;
54+
55+
_service.DeviceListUpdated -= OnDeviceListUpdated;
56+
await _service.DisposeAsync();
57+
_service = new();
58+
_service.DeviceListUpdated += OnDeviceListUpdated;
59+
60+
_devices = [];
61+
62+
_manager.Stop();
63+
64+
await StartServiceAsync(cancellationToken);
65+
}
66+
}
67+
4868
/// <inheritdoc/>
4969
public async Task UpdateDeviceListAsync(CancellationToken cancellationToken = default)
5070
{
@@ -68,6 +88,11 @@ public async Task UpdateDeviceListAsync(CancellationToken cancellationToken = de
6888
/// <inheritdoc/>
6989
public async Task ResizeZoneAsync(int deviceIndex, int zoneIndex, int size, CancellationToken cancellationToken = default)
7090
{
91+
if (!Started)
92+
{
93+
return;
94+
}
95+
7196
await _service.ResizeZoneAsync((uint)deviceIndex, (uint)zoneIndex, (uint)size, cancellationToken);
7297

7398
_service.OnDeviceListUpdated(_service, new());
@@ -76,60 +101,110 @@ public async Task ResizeZoneAsync(int deviceIndex, int zoneIndex, int size, Canc
76101
/// <inheritdoc/>
77102
public Task ResizeZoneAsync(OpenRGBDevice device, int zoneIndex, int size, CancellationToken cancellationToken = default)
78103
{
104+
if (!Started)
105+
{
106+
return Task.CompletedTask;
107+
}
108+
79109
return ResizeZoneAsync(device.Index, zoneIndex, size, cancellationToken);
80110
}
81111

82112
/// <inheritdoc/>
83113
public Task ResizeZoneAsync(OpenRGBDevice device, OpenRGBZone zone, int size, CancellationToken cancellationToken = default)
84114
{
115+
if (!Started)
116+
{
117+
return Task.CompletedTask;
118+
}
119+
85120
return ResizeZoneAsync(device.Index, zone.Index, size, cancellationToken);
86121
}
87122

88123
/// <inheritdoc/>
89124
public async Task UpdateLedsAsync(int deviceIndex, Color[] colors, CancellationToken cancellationToken = default)
90125
{
126+
if (!Started)
127+
{
128+
return;
129+
}
130+
91131
await _service.UpdateLedsAsync((uint)deviceIndex, colors, cancellationToken);
92132
}
93133

94134
/// <inheritdoc/>
95135
public Task UpdateLedsAsync(OpenRGBDevice device, Color[] colors, CancellationToken cancellationToken = default)
96136
{
137+
if (!Started)
138+
{
139+
return Task.CompletedTask;
140+
}
141+
97142
return UpdateLedsAsync(device.Index, colors, cancellationToken);
98143
}
99144

100145
/// <inheritdoc/>
101146
public async Task UpdateZoneLedsAsync(int deviceIndex, int zoneIndex, Color[] colors, CancellationToken cancellationToken = default)
102147
{
148+
if (!Started)
149+
{
150+
return;
151+
}
152+
103153
await _service.UpdateZoneLedsAsync((uint)deviceIndex, (uint)zoneIndex, colors, cancellationToken);
104154
}
105155

106156
/// <inheritdoc/>
107157
public Task UpdateZoneLedsAsync(OpenRGBDevice device, int zoneIndex, Color[] colors, CancellationToken cancellationToken = default)
108158
{
159+
if (!Started)
160+
{
161+
return Task.CompletedTask;
162+
}
163+
109164
return UpdateZoneLedsAsync(device.Index, zoneIndex, colors, cancellationToken);
110165
}
111166

112167
/// <inheritdoc/>
113168
public Task UpdateZoneLedsAsync(OpenRGBDevice device, OpenRGBZone zone, Color[] colors, CancellationToken cancellationToken = default)
114169
{
170+
if (!Started)
171+
{
172+
return Task.CompletedTask;
173+
}
174+
115175
return UpdateZoneLedsAsync(device.Index, zone.Index, colors, cancellationToken);
116176
}
117177

118178
/// <inheritdoc/>
119179
public async Task UpdateSingleLedAsync(int deviceIndex, int ledIndex, Color color, CancellationToken cancellationToken = default)
120180
{
181+
if (!Started)
182+
{
183+
return;
184+
}
185+
121186
await _service.UpdateSingleLedAsync((uint)deviceIndex, (uint)ledIndex, color, cancellationToken);
122187
}
123188

124189
/// <inheritdoc/>
125190
public Task UpdateSingleLedAsync(OpenRGBDevice device, int ledIndex, Color color, CancellationToken cancellationToken = default)
126191
{
192+
if (!Started)
193+
{
194+
return Task.CompletedTask;
195+
}
196+
127197
return UpdateSingleLedAsync(device.Index, ledIndex, color, cancellationToken);
128198
}
129199

130200
/// <inheritdoc/>
131201
public Task UpdateSingleLedAsync(OpenRGBDevice device, OpenRGBLed led, Color color, CancellationToken cancellationToken = default)
132202
{
203+
if (!Started)
204+
{
205+
return Task.CompletedTask;
206+
}
207+
133208
return UpdateSingleLedAsync(device.Index, led.Index, color, cancellationToken);
134209
}
135210

0 commit comments

Comments
 (0)