Repository navigation
Expand file tree
/
Copy pathDynamicKeyStateProvider.cs
More file actions
315 lines (283 loc) · 12.4 KB
/
Copy pathDynamicKeyStateProvider.cs
File metadata and controls
315 lines (283 loc) · 12.4 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System.Diagnostics;
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
using AmpUp.Core.Services;
namespace AmpUp;
/// <summary>
/// Resolves "is this dynamic state active right now?" for Stream Controller
/// LCD keys configured with <c>DisplayKeyType.DynamicState</c>.
///
/// Supported sources:
/// mute_master — default render endpoint is muted
/// mute_mic — default communications capture endpoint is muted
/// obs_recording — OBS is recording
/// obs_streaming — OBS is streaming
/// spotify_playing — Spotify has an active (non-paused) audio session on the default output
/// discord_mic — Discord mic muted (NOT implemented — always false for MVP)
/// </summary>
internal static class DynamicKeyStateProvider
{
private static readonly MMDeviceEnumerator _enumerator = new();
private static readonly object _enumLock = new();
public static bool IsActive(string source, ObsIntegration? obs, AudioMixer? mixer)
{
if (string.IsNullOrWhiteSpace(source)) return false;
try
{
switch (source)
{
case "mute_master":
return GetDefaultEndpointMute(DataFlow.Render, Role.Multimedia);
case "mute_mic":
return GetDefaultEndpointMute(DataFlow.Capture, Role.Communications);
case "obs_recording":
return obs?.IsRecording == true;
case "obs_streaming":
return obs?.IsStreaming == true;
case "obs_virtualcam":
return obs?.IsVirtualCamActive == true;
case "obs_replay_buffer":
return obs?.IsReplayBufferActive == true;
case "obs_record_paused":
return obs?.IsRecordingPaused == true;
case "spotify_playing":
return IsProcessSessionActive("spotify");
case "discord_mic":
// TODO: no reliable public API for Discord mic mute — MVP returns false.
return App.DiscordRpc?.LastKnownMute == true;
case "discord_deafen":
return App.DiscordRpc?.LastKnownDeafen == true;
case "room_screen_sync":
return App.Config?.Ambience.ScreenSync.Enabled == true;
case "room_vu_fill":
return (System.Windows.Application.Current as App)?.MainWindowRoomView?.IsVuFillOn == true;
case "room_music_reactive":
return (System.Windows.Application.Current as App)?.MainWindowRoomView?.IsMusicReactiveOn == true;
case "room_game_mode":
return App.Config?.Ambience.GameModeEnabled == true;
case "corsair_on":
return App.Config?.Corsair.Enabled == true;
case "room_lights_on":
// Any Govee device currently powered on, or Corsair
// active in a non-off mode, counts as "lights on".
return AreRoomLightsOn();
default:
if (source.StartsWith(RoomEffectPrefix, StringComparison.Ordinal))
return IsRoomEffectPlaying(source.Substring(RoomEffectPrefix.Length));
if (source.StartsWith(GoveeOnPrefix, StringComparison.Ordinal))
return IsGoveeDeviceOn(source.Substring(GoveeOnPrefix.Length));
return false;
}
}
catch
{
return false;
}
}
// Sources tied to what the key itself controls: "room_effect:<effect>" and
// "govee_on:<ip or cloud:deviceId>". They're filled from the bound button at render
// time (SyncBoundSource), so changing a key's effect or light keeps its glow right.
public const string RoomEffectPrefix = "room_effect:";
public const string GoveeOnPrefix = "govee_on:";
private static bool IsBoundSource(string source) =>
source.StartsWith(RoomEffectPrefix, StringComparison.Ordinal)
|| source.StartsWith(GoveeOnPrefix, StringComparison.Ordinal);
/// <summary>
/// For a DynamicState key, fill in (or refresh) a source that follows the bound button:
/// empty sources are derived from the action, and per-effect / per-light sources are
/// re-derived so they always name the effect or light the key currently controls.
/// Sources the user picked by hand are left alone.
/// </summary>
public static void SyncBoundSource(AmpUp.Core.Models.StreamControllerDisplayKeyConfig key, AmpUp.Core.Models.ButtonConfig? button)
{
if (key.DisplayType != AmpUp.Core.Models.DisplayKeyType.DynamicState) return;
string current = key.DynamicStateSource ?? "";
if (current.Length > 0 && !IsBoundSource(current)) return;
string derived = DeriveSourceFromAction(button?.Action, button?.Path);
if (!string.IsNullOrWhiteSpace(derived) && derived != current)
key.DynamicStateSource = derived;
}
/// <summary>The effect is the room's current effect and nothing else has the lights
/// (Screen Sync, the Amp Up knob mirror) and they're on.</summary>
private static bool IsRoomEffectPlaying(string effect)
{
var amb = App.Config?.Ambience;
if (amb == null || string.IsNullOrWhiteSpace(effect)) return false;
if (amb.ScreenSync.Enabled || amb.LinkToLights) return false;
return string.Equals(amb.RoomEffect, effect, StringComparison.OrdinalIgnoreCase) && AreRoomLightsOn();
}
private static bool IsGoveeDeviceOn(string target)
{
var devices = App.Config?.Ambience.GoveeDevices;
if (devices == null || string.IsNullOrWhiteSpace(target)) return false;
var dev = target.StartsWith("cloud:", StringComparison.OrdinalIgnoreCase)
? devices.FirstOrDefault(d => string.Equals(d.DeviceId, target.Substring(6), StringComparison.OrdinalIgnoreCase))
: devices.FirstOrDefault(d => string.Equals(d.Ip, target, StringComparison.OrdinalIgnoreCase));
return dev?.PoweredOn == true;
}
private static bool GetDefaultEndpointMute(DataFlow flow, Role role)
{
try
{
MMDevice? dev;
lock (_enumLock)
dev = _enumerator.GetDefaultAudioEndpoint(flow, role);
if (dev == null) return false;
bool muted = dev.AudioEndpointVolume.Mute;
dev.Dispose();
return muted;
}
catch
{
return false;
}
}
/// <summary>
/// True when any WASAPI session on the default render endpoint whose process name
/// contains <paramref name="processNameFragment"/> reports <c>AudioSessionStateActive</c>
/// (i.e. producing audio right now — not paused or inactive).
/// </summary>
private static bool IsProcessSessionActive(string processNameFragment)
{
try
{
MMDevice? device;
lock (_enumLock)
device = _enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
if (device == null) return false;
using (device)
{
var sessions = device.AudioSessionManager.Sessions;
for (int i = 0; i < sessions.Count; i++)
{
using var s = sessions[i];
try
{
if (s.State != AudioSessionState.AudioSessionStateActive) continue;
var pid = (int)s.GetProcessID;
if (pid == 0) continue;
using var process = Process.GetProcessById(pid);
var procName = process.ProcessName;
if (procName.Contains(processNameFragment, StringComparison.OrdinalIgnoreCase))
return true;
}
catch { }
}
}
}
catch { }
return false;
}
/// <summary>Labels for the editor UI. Keep order stable.</summary>
public static readonly (string Source, string Label)[] Sources =
{
("mute_master", "Master Muted"),
("mute_mic", "Mic Muted"),
("obs_recording", "OBS Recording"),
("obs_streaming", "OBS Streaming"),
("obs_virtualcam", "OBS Virtual Camera On"),
("obs_replay_buffer", "OBS Replay Buffer On"),
("obs_record_paused", "OBS Recording Paused"),
("spotify_playing", "Spotify Playing"),
("discord_mic", "Discord Mic Muted"),
("discord_deafen", "Discord Deafened"),
("room_lights_on", "Room Lights On"),
("room_screen_sync", "Screen Sync On"),
("room_vu_fill", "VU Fill On"),
("room_music_reactive", "Music Reactive On"),
("room_game_mode", "Game Mode On"),
("corsair_on", "Corsair Lighting On"),
};
/// <summary>
/// Auto-derive the dynamic state source that matches a bound button action.
/// Returns empty string if the action has no meaningful on/off state to track.
/// </summary>
public static string DeriveSourceFromAction(string? action, string? path = null)
{
if (string.IsNullOrWhiteSpace(action)) return "";
switch (action)
{
case "room_effect":
return RoomEffectPrefix + (path ?? "").Trim();
case "govee_toggle":
return GoveeOnPrefix + (path ?? "").Trim();
case "corsair_toggle":
return "corsair_on";
case "mute_master":
return "mute_master";
case "mute_mic":
return "mute_mic";
case "discord_toggle_mute":
case "discord_mute_on":
case "discord_mute_off":
return "discord_mic";
case "discord_toggle_deafen":
case "discord_deafen_on":
case "discord_deafen_off":
return "discord_deafen";
case "obs_record":
return "obs_recording";
case "obs_stream":
return "obs_streaming";
case "obs_virtualcam":
return "obs_virtualcam";
case "obs_replay_buffer":
case "obs_replay_save":
return "obs_replay_buffer";
case "obs_record_pause":
return "obs_record_paused";
case "spotify_play_pause":
case "spotify_next":
case "spotify_prev":
case "spotify_shuffle":
case "spotify_like":
return "spotify_playing";
case "room_screen_sync":
case "room_vu_fill":
case "room_music_reactive":
case "room_game_mode":
return action;
case "room_toggle":
case "govee_white_toggle":
case "govee_color":
case "govee_scene":
case "group_toggle":
return "room_lights_on";
default:
return "";
}
}
/// <summary>Human label for a source key, or empty string if unknown.</summary>
public static string GetSourceLabel(string source)
{
foreach (var (s, l) in Sources)
if (s == source) return l;
if (source != null && source.StartsWith(RoomEffectPrefix, StringComparison.Ordinal)) return "This Effect Playing";
if (source != null && source.StartsWith(GoveeOnPrefix, StringComparison.Ordinal)) return "This Light On";
return "";
}
/// <summary>True when any room light (Govee LAN/Cloud or Corsair)
/// is currently in a powered-on state.</summary>
private static bool AreRoomLightsOn()
{
var app = System.Windows.Application.Current as App;
if (app == null) return false;
try
{
var cfg = App.Config;
if (cfg != null)
{
// Any Govee device reporting PoweredOn?
foreach (var dev in cfg.Ambience.GoveeDevices)
if (dev.PoweredOn) return true;
// Enabled Corsair lighting counts as "on" even in static mode
// (e.g. forced white). Only explicit "off" should read false.
if (cfg.Corsair.Enabled
&& !string.Equals(cfg.Corsair.LightSyncMode, "off", StringComparison.OrdinalIgnoreCase))
return true;
}
}
catch { }
return false;
}
}