-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonHandler.cs
More file actions
1398 lines (1256 loc) · 53.5 KB
/
ButtonHandler.cs
File metadata and controls
1398 lines (1256 loc) · 53.5 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using NAudio.CoreAudioApi;
using AmpUp.Core.Engine;
using AmpUp.Core.Models;
using AmpUp.Core.Services;
using AmpUp.Services;
namespace AmpUp;
public class ButtonHandler : IDisposable
{
// P/Invoke declarations consolidated in NativeMethods.cs
// ── Virtual key constants ─────────────────────────────────────────
private const byte VK_MEDIA_PLAY_PAUSE = 0xB3;
private const byte VK_MEDIA_NEXT_TRACK = 0xB0;
private const byte VK_MEDIA_PREV_TRACK = 0xB1;
private const byte VK_VOLUME_MUTE = 0xAD;
private const byte VK_CONTROL = 0xA2;
private const byte VK_SHIFT = 0xA0;
private const byte VK_MENU = 0xA4; // Alt
private const byte VK_LWIN = 0x5B;
private const byte VK_RETURN = 0x0D;
private const byte VK_TAB = 0x09;
private const byte VK_ESCAPE = 0x1B;
private const byte VK_SPACE = 0x20;
private const byte VK_UP = 0x26;
private const byte VK_DOWN = 0x28;
private const byte VK_LEFT = 0x25;
private const byte VK_RIGHT = 0x27;
private const byte VK_DELETE = 0x2E;
private const byte VK_BACK = 0x08;
private const byte VK_INSERT = 0x2D;
private const byte VK_HOME = 0x24;
private const byte VK_END = 0x23;
private const byte VK_PRIOR = 0x21; // Page Up
private const byte VK_NEXT = 0x22; // Page Down
private const byte VK_SNAPSHOT = 0x2C; // Print Screen
private const uint KEYEVENTF_KEYUP = 0x0002;
// ── Fields ────────────────────────────────────────────────────────
private HAIntegration? _ha;
private ObsIntegration? _obs;
private VoiceMeeterIntegration? _vm;
private readonly MMDeviceEnumerator _enumerator = new();
private readonly ButtonGestureEngine _gestureEngine = new();
// Stash config ref for action execution (needed by MuteAppGroup etc.)
private volatile AppConfig? _lastConfig;
// Cycle state: tracks current index per button for cycle_output / cycle_input
private readonly Dictionary<int, int> _cycleIndex = new();
private readonly Dictionary<string, int> _signalRgbCycleIndex = new(StringComparer.OrdinalIgnoreCase);
// ── Events (forwarded from gesture engine) ──────────────────────
public void SetHAIntegration(HAIntegration? ha) => _ha = ha;
public void SetObsIntegration(ObsIntegration? obs) => _obs = obs;
public void SetVoiceMeeterIntegration(VoiceMeeterIntegration? vm) => _vm = vm;
/// <summary>Fires when room_toggle action is triggered — toggle all room lights.</summary>
public event Action? OnRoomToggle;
/// <summary>Fires when corsair_toggle action is triggered — toggle Corsair iCUE lights on/off.</summary>
public event Action? OnCorsairToggle;
/// <summary>Fires when govee_white_toggle action is triggered — flips the
/// room between "forced white at 100%" and "all off". Any other room-
/// control path (room_toggle / group_toggle) clears the forced state.</summary>
public event Action? OnRoomWhiteToggle;
/// <summary>Spotify control events — the handler in App forwards to the
/// SpotifyIntegration service.</summary>
public event Action? OnSpotifyPlayPause;
public event Action? OnSpotifyNext;
public event Action? OnSpotifyPrev;
public event Action? OnSpotifyShuffleToggle;
public event Action? OnSpotifyLikeToggle;
/// <summary>Fires when room_effect action is triggered — set the active room effect by name (LightEffect enum string).</summary>
public event Action<string>? OnRoomEffectSet;
/// <summary>Fires with group name when group_toggle action is triggered — toggle a device group.</summary>
public event Action<string>? OnGroupToggle;
/// <summary>Fires with button index when quick_wheel is triggered.</summary>
public event Action<int>? OnQuickWheelOpen;
/// <summary>Fires with button index when the quick_wheel button is released.</summary>
public event Action<int>? OnQuickWheelClose;
/// <summary>Fires with page delta (-1, +1) or absolute page (0-based) for SC page nav.</summary>
public event Action<int, bool>? OnScPageChange;
/// <summary>Fires after a button action mutates an app group.</summary>
public event Action? OnAppGroupChanged;
// Track which button opened the wheel (for release detection)
private int _quickWheelActiveButton = -1;
public event Action<string>? OnProfileSwitch
{
add => _gestureEngine.OnProfileSwitch += value;
remove => _gestureEngine.OnProfileSwitch -= value;
}
/// <summary>Fires with (deviceName, isOutput) when the default audio device changes.</summary>
public event Action<string, bool>? OnDeviceSwitched
{
add => _gestureEngine.OnDeviceSwitched += value;
remove => _gestureEngine.OnDeviceSwitched -= value;
}
/// <summary>Fires with new brightness percentage when cycle_brightness is triggered.</summary>
public event Action<int>? OnBrightnessCycle
{
add => _gestureEngine.OnBrightnessCycle += value;
remove => _gestureEngine.OnBrightnessCycle -= value;
}
// Brightness cycle presets (matches Turn Up behavior)
private static readonly int[] BrightnessPresets = { 100, 75, 50, 25, 0 };
private int _brightnessPresetIndex = 0;
// ── IPolicyConfig COM interface for changing default audio device ──
[ComImport]
[Guid("f8679f50-850a-41cf-9c72-430f290290c8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IPolicyConfig
{
// We only need SetDefaultEndpoint; pad the vtable with placeholder slots.
// IPolicyConfig has many methods before SetDefaultEndpoint.
// The exact vtable layout varies by Windows version. The commonly used
// approach puts SetDefaultEndpoint at slot index 0 in a minimal interface.
// We use the "IPolicyConfigVista" compatible layout:
int GetMixFormat(string pszDeviceName, IntPtr ppFormat);
int GetDeviceFormat(string pszDeviceName, int bDefault, IntPtr ppFormat);
int ResetDeviceFormat(string pszDeviceName);
int SetDeviceFormat(string pszDeviceName, IntPtr pEndpointFormat, IntPtr mixFormat);
int GetProcessingPeriod(string pszDeviceName, int bDefault, IntPtr pmftDefaultPeriod, IntPtr pmftMinimumPeriod);
int SetProcessingPeriod(string pszDeviceName, IntPtr pmftPeriod);
int GetShareMode(string pszDeviceName, IntPtr pMode);
int SetShareMode(string pszDeviceName, IntPtr mode);
int GetPropertyValue(string pszDeviceName, int bFx, IntPtr pKey, IntPtr pValue);
int SetPropertyValue(string pszDeviceName, int bFx, IntPtr pKey, IntPtr pValue);
int SetDefaultEndpoint(string pszDeviceName, int role);
int SetEndpointVisibility(string pszDeviceName, int bVisible);
}
private static readonly Guid CLSID_PolicyConfigClient = new("870af99c-171d-4f9e-af0d-e63df40c2bc9");
// ── Constructor ─────────────────────────────────────────────────
public ButtonHandler()
{
_gestureEngine.OnGestureAction += HandleGestureAction;
}
// ── Gesture forwarding (delegates to core engine) ───────────────
public void HandleDown(int idx, AppConfig config)
{
_lastConfig = config;
_gestureEngine.HandleDown(idx, config);
}
public void HandleUp(int idx, AppConfig config)
{
_lastConfig = config;
_gestureEngine.HandleUp(idx, config);
// If this button was holding the wheel open, fire close event
if (_quickWheelActiveButton == idx)
{
_quickWheelActiveButton = -1;
OnQuickWheelClose?.Invoke(idx);
}
}
private void HandleGestureAction(int idx, string gesture, string action, ButtonConfig btn)
{
// Auto-trigger Quick Wheel on hold if this is a configured trigger button
if (gesture == "hold" && _lastConfig?.Osd?.QuickWheels != null)
{
foreach (var qw in _lastConfig.Osd.QuickWheels)
{
if (qw.Enabled && idx == qw.GetVirtualButtonIdx())
{
_quickWheelActiveButton = idx;
OnQuickWheelOpen?.Invoke(idx);
return; // override the button's normal hold action
}
}
}
ExecuteAction(action, btn.Path ?? "", btn);
}
/// <summary>Execute an action by name string, without needing a full ButtonConfig.</summary>
public void ExecuteActionByName(string action, string path = "")
{
ExecuteAction(action, path, null);
}
// ── Action dispatcher ─────────────────────────────────────────────
public void ExecuteAction(string action, string path, ButtonConfig? btn = null)
{
try
{
switch (action.ToLowerInvariant())
{
case "media_play_pause":
PressKey(VK_MEDIA_PLAY_PAUSE); break;
case "media_next":
PressKey(VK_MEDIA_NEXT_TRACK); break;
case "media_prev":
PressKey(VK_MEDIA_PREV_TRACK); break;
case "mute_master":
PressKey(VK_VOLUME_MUTE); break;
case "mute_mic":
ToggleMicMute(); break;
case "launch_exe":
LaunchExe(path); break;
case "mute_program":
MuteProgram(path); break;
case "mute_active_window":
MuteActiveWindow(); break;
case "mute_app_group":
MuteAppGroup(btn); break;
case "add_active_app_to_group":
AddForegroundAppToGroup(btn); break;
case "mute_device":
MuteDevice(btn?.DeviceId ?? ""); break;
case "cycle_output":
CycleOutputDevice(btn); break;
case "cycle_input":
CycleInputDevice(btn); break;
case "select_output":
SelectDevice(btn?.DeviceId ?? "", DataFlow.Render); break;
case "select_input":
SelectDevice(btn?.DeviceId ?? "", DataFlow.Capture); break;
case "close_program":
CloseProgram(path); break;
case "macro":
ExecuteMacro(btn?.MacroKeys ?? ""); break;
case "switch_profile":
SwitchProfile(btn?.ProfileName ?? ""); break;
case "cycle_profile":
CycleProfile(btn); break;
case "system_power":
ExecuteSystemPower(btn?.PowerAction ?? ""); break;
// Individual power actions (no sub-picker needed)
case "power_sleep":
ExecuteSystemPower("sleep"); break;
case "power_lock":
ExecuteSystemPower("lock"); break;
case "power_off":
ExecuteSystemPower("shutdown"); break;
case "power_restart":
ExecuteSystemPower("restart"); break;
case "power_logoff":
ExecuteSystemPower("logoff"); break;
case "power_hibernate":
ExecuteSystemPower("hibernate"); break;
case "cycle_brightness":
CycleBrightness(); break;
case "ha_toggle":
if (_ha != null && !string.IsNullOrEmpty(path))
_ = _ha.ToggleEntityAsync(path);
break;
case "ha_scene":
if (_ha != null && !string.IsNullOrEmpty(path))
_ = _ha.ActivateSceneAsync(path);
break;
case "ha_color":
if (_ha != null && !string.IsNullOrEmpty(path))
_ = SetHomeAssistantLightColorAsync(path);
break;
case "ha_color_temp":
if (_ha != null && !string.IsNullOrEmpty(path))
_ = SetHomeAssistantLightTemperatureAsync(path);
break;
case "ha_service":
if (_ha != null && !string.IsNullOrEmpty(path))
{
// path format: "domain.service:entity_id" or just "entity_id" for toggle
var parts = path.Split(':', 2);
if (parts.Length == 2)
{
var domSvc = parts[0].Split('.', 2);
if (domSvc.Length == 2)
_ = _ha.CallServiceAsync(domSvc[0], domSvc[1], parts[1]);
}
else
{
_ = _ha.ToggleEntityAsync(path);
}
}
break;
case "room_effect":
// path = LightEffect name (e.g. "Fire", "Ocean", "Aurora")
if (!string.IsNullOrEmpty(path))
OnRoomEffectSet?.Invoke(path);
break;
case "room_toggle":
// Toggle ALL room lights (Govee + Corsair) on/off
OnRoomToggle?.Invoke();
break;
case "corsair_toggle":
// Toggle Corsair iCUE lights on/off (flips config.Corsair.Enabled)
OnCorsairToggle?.Invoke();
break;
case "group_toggle":
// path = group name
if (!string.IsNullOrEmpty(path))
OnGroupToggle?.Invoke(path);
break;
case "govee_toggle":
// path = device IP (LAN) OR "cloud:<deviceId>" for cloud-only
// devices that Govee doesn't expose on LAN (e.g. H604C G1S Pro).
if (!string.IsNullOrEmpty(path))
_ = GoveeToggleAsync(path);
break;
case "quick_wheel":
// Fired by gesture engine (hold gesture) — open the radial wheel
if (btn != null)
{
_quickWheelActiveButton = btn.Idx;
OnQuickWheelOpen?.Invoke(btn.Idx);
}
break;
case "obs_record":
if (_obs != null && _obs.IsAvailable)
_ = _obs.ToggleRecordingAsync();
break;
case "obs_stream":
if (_obs != null && _obs.IsAvailable)
_ = _obs.ToggleStreamingAsync();
break;
case "obs_scene":
if (_obs != null && _obs.IsAvailable && !string.IsNullOrEmpty(path))
_ = _obs.SetSceneAsync(path);
break;
case "obs_mute":
if (_obs != null && _obs.IsAvailable && !string.IsNullOrEmpty(path))
_ = _obs.ToggleMuteAsync(path);
break;
case "vm_mute_strip":
if (_vm != null && _vm.IsAvailable && int.TryParse(path, out int vmStripIdx))
_vm.ToggleStripMute(vmStripIdx);
break;
case "vm_mute_bus":
if (_vm != null && _vm.IsAvailable && int.TryParse(path, out int vmBusIdx))
_vm.ToggleBusMute(vmBusIdx);
break;
case "govee_color":
// path = "ip|hexcolor" e.g. "192.168.1.50|FF0080"
if (!string.IsNullOrEmpty(path))
{
var govParts = path.Split('|', 2);
if (govParts.Length == 2 && !string.IsNullOrEmpty(govParts[0]) && !string.IsNullOrEmpty(govParts[1]))
{
var ip = govParts[0];
var hex = govParts[1].TrimStart('#');
if (hex.Length == 6 && int.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out int rgb))
{
byte r = (byte)((rgb >> 16) & 0xFF);
byte g = (byte)((rgb >> 8) & 0xFF);
byte b = (byte)(rgb & 0xFF);
_ = AmbienceSync.SendColorAsync(ip, r, g, b);
}
else
{
}
}
else if (govParts.Length == 1 && !string.IsNullOrEmpty(govParts[0]))
{
}
}
break;
case "govee_white_toggle":
// Now a room-wide toggle: press 1 = all room lights on at
// 100% white, press 2 = all room lights off. Any other
// room-control path (room_toggle / group_toggle) clears
// the forced-white state so the room effect resumes.
OnRoomWhiteToggle?.Invoke();
break;
case "spotify_play_pause": OnSpotifyPlayPause?.Invoke(); break;
case "spotify_next": OnSpotifyNext?.Invoke(); break;
case "spotify_prev": OnSpotifyPrev?.Invoke(); break;
case "spotify_shuffle": OnSpotifyShuffleToggle?.Invoke(); break;
case "spotify_like": OnSpotifyLikeToggle?.Invoke(); break;
case "signalrgb_effect":
SignalRgbEffectCatalog.ApplyEffect(path);
break;
case "signalrgb_effect_cycle":
CycleSignalRgbEffect(btn, path);
break;
case "signalrgb_blackout":
SignalRgbEffectCatalog.ApplyBlackout();
break;
case "signalrgb_restore":
SignalRgbEffectCatalog.RestoreLastEffect(GetSignalRgbRestoreFallback(btn, path));
break;
case "sc_page_next":
OnScPageChange?.Invoke(1, false);
break;
case "sc_page_prev":
OnScPageChange?.Invoke(-1, false);
break;
case "sc_page_home":
OnScPageChange?.Invoke(0, true);
break;
case "sc_go_to_page":
if (int.TryParse(path, out int targetPage))
OnScPageChange?.Invoke(targetPage - 1, true); // path is 1-based
break;
case "multi_action":
if (btn?.ActionSequence != null && btn.ActionSequence.Count > 0)
_ = RunMultiActionAsync(btn.ActionSequence);
break;
case "toggle_action":
if (btn != null)
RunToggleAction(btn);
break;
case "open_folder":
// Empty FolderName is a valid target — means "go Home".
// The user can explicitly bind a key to return to Home
// after removing the auto Back key via the Spaces list.
if (btn != null)
OnOpenFolder?.Invoke(btn.FolderName ?? "");
break;
case "open_url":
OpenUrl(path);
break;
case "type_text":
TypeTextSnippet(btn?.TextSnippet ?? path);
break;
case "screenshot":
CaptureScreenshot();
break;
}
}
catch (Exception ex)
{
Logger.Log($"ExecuteAction error ({action}): {ex.Message}");
}
}
// ── Stream Controller extended actions (stubs; filled in by feature implementations) ─
/// <summary>Fires with folder name when `open_folder` fires — UI handler navigates to that folder.</summary>
public event Action<string>? OnOpenFolder;
private async Task RunMultiActionAsync(List<MultiActionStep> steps)
{
foreach (var step in steps)
{
if (step.DelayMs > 0)
await Task.Delay(step.DelayMs);
var stepBtn = new ButtonConfig
{
Action = step.Action,
Path = step.Path ?? "",
MacroKeys = step.MacroKeys ?? "",
DeviceId = step.DeviceId ?? "",
ProfileName = step.ProfileName ?? "",
PowerAction = step.PowerAction ?? "",
};
ExecuteAction(step.Action, step.Path ?? "", stepBtn);
}
}
private void RunToggleAction(ButtonConfig btn)
{
bool runB = btn.ToggleStateIsB;
var action = runB ? btn.ToggleActionB : btn.ToggleActionA;
var path = runB ? btn.TogglePathB : btn.TogglePathA;
btn.ToggleStateIsB = !runB;
ExecuteAction(action, path ?? "", btn);
}
private void CycleSignalRgbEffect(ButtonConfig? btn, string path)
{
var effects = GetSignalRgbEffectNames(btn, path);
if (effects.Count == 0) return;
string key = $"{btn?.Idx ?? -1}:{string.Join("|", effects)}";
int next = _signalRgbCycleIndex.GetValueOrDefault(key);
if (next < 0 || next >= effects.Count) next = 0;
SignalRgbEffectCatalog.ApplyEffect(effects[next]);
_signalRgbCycleIndex[key] = (next + 1) % effects.Count;
}
private static string GetSignalRgbRestoreFallback(ButtonConfig? btn, string path)
{
var effects = GetSignalRgbEffectNames(btn, path);
return effects.Count > 0 ? effects[0] : "";
}
private static List<string> GetSignalRgbEffectNames(ButtonConfig? btn, string path)
{
var names = btn?.SignalRgbEffectNames?
.Where(n => !string.IsNullOrWhiteSpace(n))
.Select(n => n.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList() ?? new List<string>();
if (names.Count > 0)
return names;
return SplitSignalRgbEffectPath(path);
}
private static List<string> SplitSignalRgbEffectPath(string path)
{
if (string.IsNullOrWhiteSpace(path)) return new();
return path
.Split(new[] { '|', ';', '\n', '\r', '\t' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(p => !string.IsNullOrWhiteSpace(p))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
}
private static void OpenUrl(string url)
{
if (string.IsNullOrWhiteSpace(url)) return;
try
{
var trimmed = url.Trim();
if (!trimmed.Contains("://"))
trimmed = "https://" + trimmed;
Process.Start(new ProcessStartInfo { FileName = trimmed, UseShellExecute = true });
}
catch (Exception ex) { Logger.Log($"open_url error: {ex.Message}"); }
}
private static void TypeTextSnippet(string text)
{
if (string.IsNullOrEmpty(text)) return;
try
{
// SendKeys.SendWait escapes by default via System.Windows.Forms.
// We use low-level keybd_event via ExecuteMacro's chain is keystroke-combo oriented,
// so for plain text we use SendKeys.
System.Windows.Forms.SendKeys.SendWait(EscapeSendKeys(text));
}
catch (Exception ex) { Logger.Log($"type_text error: {ex.Message}"); }
}
private static string EscapeSendKeys(string text)
{
// SendKeys treats +^%~(){}[] as special — escape with {}
var sb = new System.Text.StringBuilder(text.Length + 8);
foreach (var ch in text)
{
if ("+^%~(){}[]".IndexOf(ch) >= 0)
sb.Append('{').Append(ch).Append('}');
else if (ch == '\n')
sb.Append('~'); // Enter
else
sb.Append(ch);
}
return sb.ToString();
}
private static void CaptureScreenshot()
{
try
{
var bounds = System.Windows.Forms.Screen.PrimaryScreen?.Bounds ?? new System.Drawing.Rectangle(0, 0, 1920, 1080);
using var bmp = new System.Drawing.Bitmap(bounds.Width, bounds.Height);
using (var g = System.Drawing.Graphics.FromImage(bmp))
g.CopyFromScreen(bounds.Left, bounds.Top, 0, 0, bounds.Size);
System.Windows.Forms.Clipboard.SetImage(bmp);
}
catch (Exception ex) { Logger.Log($"screenshot error: {ex.Message}"); }
}
// ── Govee on/off toggle (LAN or Cloud) ─────────────────────────────
/// <summary>
/// Toggle a Govee device on/off. Path is either a raw IP (LAN sync) or
/// "cloud:<deviceId>" for devices Govee blocks from LAN control (e.g.
/// DreamView G1S Pro / H604C — confirmed by Govee support). Cloud path
/// looks up the matching config entry for SKU + current PoweredOn
/// state, flips it via the REST API, and persists the new state.
/// </summary>
private async Task GoveeToggleAsync(string path)
{
try
{
if (path.StartsWith("cloud:", StringComparison.OrdinalIgnoreCase))
{
if (_lastConfig == null) return;
var apiKey = _lastConfig.Ambience.GoveeApiKey;
if (string.IsNullOrWhiteSpace(apiKey)) return;
var deviceId = path.Substring("cloud:".Length);
var dev = _lastConfig.Ambience.GoveeDevices
.FirstOrDefault(d => d.DeviceId == deviceId);
if (dev == null || string.IsNullOrEmpty(dev.Sku)) return;
bool newState = !dev.PoweredOn;
using var api = new GoveeCloudApi(apiKey);
await api.ControlDeviceAsync(deviceId, dev.Sku,
GoveeCloudApi.TurnOnOff(newState));
dev.PoweredOn = newState;
return;
}
await AmbienceSync.SendToggleAsync(path);
}
catch (Exception ex)
{
Logger.Log($"govee_toggle error: {ex.Message}");
}
}
// ── Simple key press ──────────────────────────────────────────────
private static void PressKey(byte vk)
{
NativeMethods.keybd_event(vk, 0, 0, UIntPtr.Zero);
NativeMethods.keybd_event(vk, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
}
// ── Launch executable ─────────────────────────────────────────────
private static void LaunchExe(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return;
}
try
{
// Support "path args" format (e.g. "C:\Update.exe --processStart Discord.exe")
string fileName = path;
string arguments = "";
if (!File.Exists(path) && path.Contains(' '))
{
// Try splitting at first space to separate exe from args
var parts = path.Split(' ', 2);
if (File.Exists(parts[0]))
{
fileName = parts[0];
arguments = parts[1];
}
// Also try quoted path: "path with spaces" args
else if (path.StartsWith('"'))
{
int closeQuote = path.IndexOf('"', 1);
if (closeQuote > 0)
{
fileName = path.Substring(1, closeQuote - 1);
arguments = path.Length > closeQuote + 1 ? path.Substring(closeQuote + 2) : "";
}
}
}
Process.Start(new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = true
});
}
catch (Exception ex)
{
Logger.Log($"launch_exe error: {ex.Message}");
}
}
// ── Mic mute toggle ───────────────────────────────────────────────
private void ToggleMicMute()
{
try
{
using var mic = _enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Communications);
mic.AudioEndpointVolume.Mute = !mic.AudioEndpointVolume.Mute;
}
catch (Exception ex)
{
Logger.Log($"ToggleMicMute error: {ex.Message}");
}
}
// ── Mute program by process name ──────────────────────────────────
private void MuteProgram(string processName)
{
if (string.IsNullOrWhiteSpace(processName))
{
return;
}
try
{
using var device = _enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
var sessions = device.AudioSessionManager.Sessions;
for (int i = 0; i < sessions.Count; i++)
{
var session = sessions[i];
try
{
var pid = (int)session.GetProcessID;
if (pid == 0) continue;
var proc = Process.GetProcessById(pid);
if (proc.ProcessName.Contains(processName, StringComparison.OrdinalIgnoreCase)
|| proc.ProcessName.Replace(" ", "").Contains(processName.Replace(" ", ""), StringComparison.OrdinalIgnoreCase))
{
session.SimpleAudioVolume.Mute = !session.SimpleAudioVolume.Mute;
return;
}
}
catch
{
// Process may have exited
}
}
}
catch (Exception ex)
{
Logger.Log($"mute_program error: {ex.Message}");
}
}
// ── Mute active (foreground) window ───────────────────────────────
private void MuteActiveWindow()
{
try
{
var hwnd = NativeMethods.GetForegroundWindow();
if (hwnd == IntPtr.Zero)
{
return;
}
NativeMethods.GetWindowThreadProcessId(hwnd, out uint pid);
if (pid == 0)
{
return;
}
using var device = _enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
var sessions = device.AudioSessionManager.Sessions;
for (int i = 0; i < sessions.Count; i++)
{
var session = sessions[i];
if (session.GetProcessID == pid)
{
session.SimpleAudioVolume.Mute = !session.SimpleAudioVolume.Mute;
return;
}
}
// Some apps spawn child processes for audio — try matching by process name
try
{
var fgProc = Process.GetProcessById((int)pid);
var fgName = fgProc.ProcessName;
for (int i = 0; i < sessions.Count; i++)
{
var session = sessions[i];
try
{
var sPid = (int)session.GetProcessID;
if (sPid == 0) continue;
var sProc = Process.GetProcessById(sPid);
if (sProc.ProcessName.Contains(fgName, StringComparison.OrdinalIgnoreCase))
{
session.SimpleAudioVolume.Mute = !session.SimpleAudioVolume.Mute;
return;
}
}
catch { }
}
}
catch { }
}
catch (Exception ex)
{
Logger.Log($"mute_active_window error: {ex.Message}");
}
}
// ── Mute all apps in a knob's app group ───────────────────────────
private void MuteAppGroup(ButtonConfig? btn)
{
if (btn == null || _lastConfig == null)
{
return;
}
int knobIdx = btn.LinkedKnobIdx;
if (knobIdx < 0 || knobIdx > 4)
{
return;
}
var knob = _lastConfig.Knobs.FirstOrDefault(k => k.Idx == knobIdx);
if (knob == null || knob.Apps == null || knob.Apps.Count == 0)
{
return;
}
try
{
using var device = _enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
var sessions = device.AudioSessionManager.Sessions;
var matchingSessions = new List<NAudio.CoreAudioApi.AudioSessionControl>();
for (int i = 0; i < sessions.Count; i++)
{
var session = sessions[i];
try
{
var pid = (int)session.GetProcessID;
if (pid == 0) continue;
var proc = Process.GetProcessById(pid);
var procName = proc.ProcessName.ToLowerInvariant();
foreach (var appName in knob.Apps)
{
var appLower = appName.ToLowerInvariant();
if (procName.Contains(appLower)
|| procName.Replace(" ", "").Contains(appLower.Replace(" ", "")))
{
matchingSessions.Add(session);
break;
}
}
}
catch { }
}
if (matchingSessions.Count == 0)
{
return;
}
// Toggle: if ANY session is unmuted → mute all. If ALL muted → unmute all.
bool anyUnmuted = matchingSessions.Any(s => !s.SimpleAudioVolume.Mute);
bool newMuteState = anyUnmuted;
foreach (var session in matchingSessions)
{
try { session.SimpleAudioVolume.Mute = newMuteState; } catch { }
}
}
catch (Exception ex)
{
Logger.Log($"mute_app_group error: {ex.Message}");
}
}
// ── Add foreground app to a knob app group ───────────────────────
private void AddForegroundAppToGroup(ButtonConfig? btn)
{
if (btn == null || _lastConfig == null)
{
return;
}
int knobIdx = btn.LinkedKnobIdx;
if (knobIdx < 0 || knobIdx > 4)
{
return;
}
try
{
var hwnd = NativeMethods.GetForegroundWindow();
if (hwnd == IntPtr.Zero)
{
return;
}
NativeMethods.GetWindowThreadProcessId(hwnd, out uint pid);
if (pid == 0 || pid == Environment.ProcessId)
{
return;
}
using var proc = Process.GetProcessById((int)pid);
var processName = proc.ProcessName.Trim();
if (string.IsNullOrWhiteSpace(processName)
|| processName.Equals("AmpUp", StringComparison.OrdinalIgnoreCase))
{
return;
}
var knob = _lastConfig.Knobs.FirstOrDefault(k => k.Idx == knobIdx);
if (knob == null)
{
return;
}
knob.Target = "apps";
knob.Apps ??= new List<string>();
if (knob.Apps.Any(a => a.Equals(processName, StringComparison.OrdinalIgnoreCase)))
{
return;
}
knob.Apps.Add(processName.ToLowerInvariant());
Logger.Log($"Added focused app '{processName}' to knob {knobIdx + 1} app group");
OnAppGroupChanged?.Invoke();
}
catch (Exception ex)
{
Logger.Log($"add_active_app_to_group error: {ex.Message}");
}
}
private async Task SetHomeAssistantLightColorAsync(string path)
{
try
{
var parts = path.Split('|', 2);
if (parts.Length != 2) return;
var entityId = parts[0].Trim();
var hex = parts[1].Trim().TrimStart('#');
if (string.IsNullOrWhiteSpace(entityId) || hex.Length != 6)
return;
if (!int.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out int rgb))
return;
byte r = (byte)((rgb >> 16) & 0xFF);
byte g = (byte)((rgb >> 8) & 0xFF);
byte b = (byte)(rgb & 0xFF);
await _ha!.SetLightColorAsync(entityId, r, g, b);
}
catch (Exception ex)
{
Logger.Log($"ha_color error: {ex.Message}");
}
}
// ── Mute specific audio device by DeviceId ───────────────────────
private async Task SetHomeAssistantLightTemperatureAsync(string path)
{
try
{
var parts = path.Split('|', 2);
if (parts.Length != 2) return;
var entityId = parts[0].Trim();
var rawKelvin = parts[1].Trim();
if (rawKelvin.StartsWith("temp:", StringComparison.OrdinalIgnoreCase))
rawKelvin = rawKelvin.Substring(5);
if (string.IsNullOrWhiteSpace(entityId) || !int.TryParse(rawKelvin, out int kelvin))
return;
await _ha!.SetLightColorTemperatureAsync(entityId, kelvin);
}
catch (Exception ex)
{
Logger.Log($"ha_color_temp error: {ex.Message}");
}
}
private void MuteDevice(string deviceId)
{
if (string.IsNullOrWhiteSpace(deviceId))
{
return;
}
try
{
// Try Render (output) devices first, then Capture (input)
foreach (var flow in new[] { DataFlow.Render, DataFlow.Capture })
{
try
{
var devices = _enumerator.EnumerateAudioEndPoints(flow, DeviceState.Active);
for (int i = 0; i < devices.Count; i++)
{
var device = devices[i];
if (device.ID == deviceId)
{
bool newMute = !device.AudioEndpointVolume.Mute;
device.AudioEndpointVolume.Mute = newMute;
return;
}
}
}
catch { }
}
}
catch (Exception ex)
{
Logger.Log($"mute_device error: {ex.Message}");