/
Program.cs
323 lines (274 loc) · 10.9 KB
/
Program.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
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
using System;
using System.IO;
using System.Media;
using System.Runtime.InteropServices;
using System.Threading;
using Valve.VR;
namespace OVRBrightnessPanic
{
public class Program
{
public const float ACTIVATE_FACTOR = 0.5f;
public const float DT = 0.05f;
public const float RESET_SPEED = 0.25f;
private CVRInput vrInput;
private CVRSettings vrSettings;
private CVRSystem vrSystem;
private ulong inputSet;
private ulong inputActivate, inputResetAuto, inputResetHold;
private SoundPlayer activateSound, resetSound;
private float? initialBrightness = null;
private bool resetting = false;
private bool running = true;
public static void Main(string[] args)
{
var program = new Program();
try
{
program.Init();
program.Run();
}
catch(Exception e)
{
Console.Error.WriteLine($"An error has occurred: {e.Message}");
}
finally
{
program.Deinit();
}
}
public void Init()
{
Console.CancelKeyPress += CancelKeyPressed;
var initError = EVRInitError.None;
vrSystem = OpenVR.Init(ref initError, EVRApplicationType.VRApplication_Background);
if(initError != EVRInitError.None)
{
var message = OpenVR.GetStringForHmdError(initError);
throw new Exception($"Failed to initialize OpenVR: {message}");
}
vrInput = OpenVR.Input;
vrSettings = OpenVR.Settings;
var appDir = AppDomain.CurrentDomain.BaseDirectory;
var manifestPath = Path.Combine(appDir, "action_manifest.json");
var inputError = vrInput.SetActionManifestPath(manifestPath);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to set action manifest path: {message}");
}
inputError = vrInput.GetActionSetHandle("/actions/main", ref inputSet);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to action set handle: {message}");
}
inputError = vrInput.GetActionHandle("/actions/main/in/activate", ref inputActivate);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to get action handle for Activate: {message}");
}
inputError = vrInput.GetActionHandle("/actions/main/in/reset-auto", ref inputResetAuto);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to get action handle for Reset (Auto): {message}");
}
inputError = vrInput.GetActionHandle("/actions/main/in/reset", ref inputResetHold);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to get action handle for Reset (Hold): {message}");
}
activateSound = new SoundPlayer(Path.Combine(appDir, "activate.wav"));
resetSound = new SoundPlayer(Path.Combine(appDir, "reset.wav"));
}
public void Run()
{
var ev = new VREvent_t();
var evSize = (uint)Marshal.SizeOf(typeof(VREvent_t));
var actionSets = new VRActiveActionSet_t[]
{
new VRActiveActionSet_t
{
ulActionSet = inputSet,
},
};
var actionSetSize = (uint)Marshal.SizeOf(typeof(VRActiveActionSet_t));
var actionData = new InputDigitalActionData_t();
var actionDataSize = (uint)Marshal.SizeOf(typeof(InputDigitalActionData_t));
Console.WriteLine("Brightness panic button is running. Input bindings may be changed through SteamVR's input bindings.");
var sleepTime = (int)Math.Round(1000 * DT);
while(running)
{
while(vrSystem.PollNextEvent(ref ev, evSize))
{
switch((EVREventType)ev.eventType)
{
case EVREventType.VREvent_DriverRequestedQuit:
case EVREventType.VREvent_Quit:
vrSystem.AcknowledgeQuit_Exiting();
running = false;
break;
}
}
var inputError = vrInput.UpdateActionState(actionSets, actionSetSize);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to update action state: {message}");
}
inputError = vrInput.GetDigitalActionData(inputActivate, ref actionData, actionDataSize, 0);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to get Activate action state: {message}");
}
var activatePressed = actionData.bChanged && actionData.bState;
inputError = vrInput.GetDigitalActionData(inputResetAuto, ref actionData, actionDataSize, 0);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to get Reset (Auto) action state: {message}");
}
var resetAutoPressed = actionData.bChanged && actionData.bState;
inputError = vrInput.GetDigitalActionData(inputResetHold, ref actionData, actionDataSize, 0);
if(inputError != EVRInputError.None)
{
var message = inputError.ToString();
throw new Exception($"Failed to get Reset (Hold) action state: {message}");
}
var resetHoldChanged = actionData.bChanged;
var resetHoldHeld = actionData.bState;
if(activatePressed)
{
TriggerActivate();
}
if(initialBrightness.HasValue && resetAutoPressed)
{
resetting = !resetting;
if(resetting)
{
Console.WriteLine("Starting automatic reset.");
}
else
{
Console.WriteLine("Cancelling automatic reset.");
}
}
if(initialBrightness.HasValue && resetHoldChanged)
{
resetting = resetHoldHeld;
if(resetting)
{
Console.WriteLine("Starting held reset.");
}
else
{
Console.WriteLine("Cancelling held reset.");
}
}
if(resetting)
{
resetting = TriggerReset(DT * RESET_SPEED);
if(resetting && (resetAutoPressed || resetHoldChanged))
{
try
{
resetSound.Play();
}
catch(FileNotFoundException) {}
}
}
Thread.Sleep(sleepTime);
}
}
public void Deinit()
{
if(vrSettings != null)
{
TriggerReset();
}
if(vrSystem != null)
{
OpenVR.Shutdown();
vrInput = null;
vrSettings = null;
vrSystem = null;
inputActivate = 0;
inputResetAuto = 0;
inputResetHold = 0;
inputSet = 0;
}
}
public void CancelKeyPressed(object sender, ConsoleCancelEventArgs args)
{
if(running)
{
args.Cancel = true;
running = false;
}
}
public void TriggerActivate()
{
var brightness = GetBrightness();
if(initialBrightness == null || initialBrightness < brightness)
{
initialBrightness = brightness;
}
brightness = Math.Max(0.2f, Math.Min(ACTIVATE_FACTOR * brightness, 1.6f));
SetBrightness(brightness);
Console.WriteLine($"Panic button activated! Brightness decreased to {100 * brightness:f0}%.");
try
{
activateSound.Play();
}
catch(FileNotFoundException) {}
}
public bool TriggerReset(float maxAmount = float.PositiveInfinity)
{
var result = false;
if(initialBrightness.HasValue)
{
var brightness = GetBrightness();
var targetBrightness = initialBrightness.Value;
var delta = targetBrightness - brightness;
if(delta > 0)
{
brightness = brightness + Math.Min(delta, maxAmount);
SetBrightness(brightness);
result = true;
}
if(brightness >= targetBrightness)
{
Console.WriteLine("Reset completed.");
initialBrightness = null;
}
}
return result;
}
public float GetBrightness()
{
var error = EVRSettingsError.None;
var gain = vrSettings.GetFloat("steamvr", "analogGain", ref error);
if(error != EVRSettingsError.None)
{
var message = vrSettings.GetSettingsErrorNameFromEnum(error);
throw new Exception($"Failed to get display brightness: {message}");
}
return (float)Math.Pow(gain, 1/2.2);
}
public void SetBrightness(float brightness)
{
var gain = (float)Math.Pow(brightness, 2.2);
var error = EVRSettingsError.None;
vrSettings.SetFloat("steamvr", "analogGain", gain, ref error);
if(error != EVRSettingsError.None)
{
var message = vrSettings.GetSettingsErrorNameFromEnum(error);
throw new Exception($"Failed to set display brightness: {message}");
}
}
}
}