-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicKeyStateProvider.cs
More file actions
203 lines (185 loc) · 6.87 KB
/
DynamicKeyStateProvider.cs
File metadata and controls
203 lines (185 loc) · 6.87 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
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 "spotify_playing":
return IsProcessSessionActive("spotify");
case "discord_mic":
// TODO: no reliable public API for Discord mic mute — MVP returns false.
return false;
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:
return false;
}
}
catch
{
return false;
}
}
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++)
{
var s = sessions[i];
try
{
if (s.State != AudioSessionState.AudioSessionStateActive) continue;
var pid = (int)s.GetProcessID;
if (pid == 0) continue;
var procName = Process.GetProcessById(pid).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"),
("spotify_playing", "Spotify Playing"),
("discord_mic", "Discord Mic Muted (coming soon)"),
("room_lights_on", "Room Lights 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)
{
if (string.IsNullOrWhiteSpace(action)) return "";
switch (action)
{
case "mute_master":
return "mute_master";
case "mute_mic":
return "mute_mic";
case "obs_record":
return "obs_recording";
case "obs_stream":
return "obs_streaming";
case "spotify_play_pause":
case "spotify_next":
case "spotify_prev":
case "spotify_shuffle":
case "spotify_like":
return "spotify_playing";
case "room_toggle":
case "corsair_toggle":
case "govee_toggle":
case "govee_white_toggle":
case "govee_color":
case "govee_scene":
case "room_effect":
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;
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;
}
}