-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerMateVolume.cs
54 lines (46 loc) · 2.03 KB
/
PowerMateVolume.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
using PowerMate;
using PowerMateVolume;
// ReSharper disable AccessToDisposedClosure - disposal happens at program shutdown, so access can't happen after that
if (!float.TryParse(Environment.GetCommandLineArgs().ElementAtOrDefault(1), out float volumeIncrement)) {
volumeIncrement = 0.01f;
}
using IPowerMateClient powerMate = new PowerMateClient();
using IVolumeChanger volumeChanger = new VolumeChanger { VolumeIncrement = volumeIncrement };
powerMate.LightBrightness = 0;
powerMate.InputReceived += (_, powerMateEvent) => {
switch (powerMateEvent) {
case { IsPressed: true, RotationDirection: RotationDirection.None }:
volumeChanger.ToggleMute();
break;
case { IsPressed: false, RotationDirection: RotationDirection.Clockwise }:
volumeChanger.IncreaseVolume((int) powerMateEvent.RotationDistance);
break;
case { IsPressed: false, RotationDirection: RotationDirection.Counterclockwise }:
volumeChanger.IncreaseVolume(-1 * (int) powerMateEvent.RotationDistance);
break;
default:
break;
}
};
using IStandbyListener standbyListener = new EventLogStandbyListener();
standbyListener.FatalError += (_, exception) =>
MessageBox.Show("Event log subscription is broken, continuing without resume detection: " + exception, "PowerMateVolume", MessageBoxButtons.OK, MessageBoxIcon.Error);
standbyListener.Resumed += (_, _) => {
try {
powerMate.SetAllFeaturesIfStale();
} catch (IOException) {
Thread.Sleep(2000);
try {
powerMate.SetAllFeaturesIfStale();
} catch (IOException) {
// device is probably in a bad state, but there's nothing we can do about it
}
}
};
CancellationTokenSource exitTokenSource = new();
Console.CancelKeyPress += (_, eventArgs) => {
eventArgs.Cancel = true;
exitTokenSource.Cancel();
};
Console.WriteLine("Listening for PowerMate events");
exitTokenSource.Token.WaitHandle.WaitOne();