-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMixerManager.cs
171 lines (146 loc) · 5.87 KB
/
MixerManager.cs
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
using KeyboardMixer.config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace KeyboardMixer
{
public class MixerManager
{
//Reference to the main form
private MainForm mainForm;
//Application settings
public SerializableSettings settings;
//Keyboard hook
private GlobalKeyboardHook globalKeyboardHook;
//Keep cache of sessions and their volumes, as calling for them repeatedly creates a lot of overhead
private List<AppVolume> appVolumeCache;
private int ticksRenewedCacheAt;
private bool listeningForKeybind = false;
public Keys[] detectedNewKeybind;
public MixerManager(MainForm mainForm)
{
this.mainForm = mainForm;
//Load settings
settings = ConfigManager.ReadConfig();
//Init volume app cache
RenewCachedValues();
globalKeyboardHook = new GlobalKeyboardHook();
globalKeyboardHook.KeyDown += new KeyEventHandler(OnKeyDown);
globalKeyboardHook.KeyUp += new KeyEventHandler(OnKeyUp);
}
//Renews the application and volume cache
public void RenewCachedValues()
{
appVolumeCache = VolumeHook.getVolumes();
ticksRenewedCacheAt = Environment.TickCount;
}
//Called every time a key is pressed down
void OnKeyDown(object sender, KeyEventArgs e)
{
if (listeningForKeybind)
{
//Set key entry as handled
e.Handled = true;
//suppress listening for volume keys
if (e.KeyCode == Keys.VolumeDown || e.KeyCode == Keys.VolumeUp) return;
//Update currently held keys
DetectNewKeybind(e);
return;
}
//Check for volume keys
if (e.KeyCode == Keys.VolumeUp || e.KeyCode == Keys.VolumeDown)
{
//Process keybinds
OnVolumePressed(e);
//If none found, try the current application (Ctrl + Vol)
if (!e.Handled)
{
if (CtrlDown()) //Ctrl Vol
{
TryStepApplicationVolume(ProcessHook.GetActiveProcessFileName(), e.KeyCode == Keys.VolumeUp ? settings.volumeStep : -settings.volumeStep);
e.Handled = true;
}
}
}
}
void OnKeyUp(object sender, KeyEventArgs e)
{
}
//Called when the VolUp or VolDown key is detected
private void OnVolumePressed(KeyEventArgs e)
{
//Order keybinds by most complex (more keys) to least complex. This prevents issues with keybinds conflicting when they share some of the same keys.
List<MixerKeybindEntry> sortedKeybinds = settings.keybinds.OrderBy(keybind => -keybind.KeyList.Length).ToList();
foreach (MixerKeybindEntry keybind in sortedKeybinds)
{
bool allMatch = true;
foreach (Keys requiredKey in keybind.KeyList)
{
if (!globalKeyboardHook.IsKeyPressed(requiredKey))
{
allMatch = false;
break;
}
}
if (allMatch)//Keybind is pressed
{
TryStepApplicationVolume(keybind.ProcessName, e.KeyCode == Keys.VolumeUp ? settings.volumeStep : -settings.volumeStep);
e.Handled = true;
break;
}
}
}
public void StartListeningKeybind()
{
if (!listeningForKeybind)
{
listeningForKeybind = true;
mainForm.resetNewKeybindField();
}
}
public void StopListeningKeybind()
{
listeningForKeybind = false;
}
//Detects currently held keys and passes back to the main form
private void DetectNewKeybind(KeyEventArgs e)
{
detectedNewKeybind = globalKeyboardHook.KeyTable
.Where(pair => pair.Value == true)
.Select(pair => pair.Key)
.ToArray();
mainForm.updateNewKeybindField(detectedNewKeybind);
}
bool CtrlDown()
{
return globalKeyboardHook.IsKeyPressed(Keys.LControlKey) || globalKeyboardHook.IsKeyPressed(Keys.RControlKey);
}
private void TryStepApplicationVolume(String configuredIdentifier, float stepAmount)
{
if (configuredIdentifier == "") return;
//Renew volume cache if it is at least 4 seconds old
if (Environment.TickCount - ticksRenewedCacheAt > 4000)
{
RenewCachedValues();
}
//get sessions whose identifier match the given one
var matchingApps = appVolumeCache.Where(app => app.Identifier.IndexOf(configuredIdentifier, System.StringComparison.CurrentCultureIgnoreCase) >= 0).ToList();
if (matchingApps.Any())
{
float newVolume = matchingApps.First().Volume + stepAmount;
newVolume = Math.Min(100, newVolume);
newVolume = Math.Max(0, newVolume);
//adjust cached volumes to the new volume
matchingApps.ForEach(app => app.Volume = newVolume);
//set volume for all matching groups
matchingApps.Select(app => app.Guid).Distinct().ToList().ForEach(guid =>
VolumeHook.SetApplicationVolumeByGroup(guid, newVolume)
);
mainForm.popupForm.ShowSlider((int)newVolume, matchingApps.First().Pid);
}
}
}
}