33using System . Diagnostics ;
44using System . IO ;
55using System . Text . RegularExpressions ;
6+ using System . Threading ;
67using System . Threading . Tasks ;
78using System . Windows . Forms ;
89using Windows . ApplicationModel . Email . DataProvider ;
910using Windows . Devices . Bluetooth ;
1011using Windows . Devices . Bluetooth . GenericAttributeProfile ;
1112using Windows . Devices . Enumeration ;
13+ using Windows . Media . Capture ;
1214using Windows . Storage . Streams ;
15+ using Valve . VR ;
1316
1417namespace LighthouseV2PowerControl
1518{
@@ -26,18 +29,39 @@ static class Program
2629 public delegate void LogHandler ( object msg , LogType type ) ;
2730 public static event LogHandler OnLog ;
2831 private static Form1 app = null ;
32+ private static CVRSystem OVRSystem ;
2933
30- [ STAThread ]
3134 static void Main ( string [ ] args )
3235 {
3336 Application . SetHighDpiMode ( HighDpiMode . SystemAware ) ;
3437 Application . EnableVisualStyles ( ) ;
3538 Application . SetCompatibleTextRenderingDefault ( false ) ;
39+
3640 app = new Form1 ( ) ;
3741 OnLog += ( msg , type ) => app . Log ( msg , type ) ;
38- Stack < EventHandler > eventHandlers = new Stack < EventHandler > ( ) ;
42+ try
43+ {
44+ EVRInitError error = EVRInitError . None ;
45+ OVRSystem = OpenVR . Init ( ref error , EVRApplicationType . VRApplication_Background ) ; //çàïóñêàåòñÿ è âûðóáàåòñÿ ñ SteamVR
46+ if ( error == EVRInitError . Init_NoServerForBackgroundApp )
47+ {
48+ Log ( "Init without SteamVR;" ) ;
49+ }
50+ else if ( error != EVRInitError . None )
51+ {
52+ LogError ( error ) ;
53+ }
54+ }
55+ catch ( Exception e )
56+ {
57+ LogError ( e . Message ) ;
58+ }
59+
60+ Stack < EventHandler > eventHandlers = new Stack < EventHandler > ( ) ; //Ïðñîòî äëÿ óäîáíîãî íàçíà÷åíèÿ êíîïîê.
3961 eventHandlers . Push ( new EventHandler ( ( obj , args ) => SendActiveStatus ( true ) ) ) ;
4062 eventHandlers . Push ( new EventHandler ( ( obj , args ) => SendActiveStatus ( false ) ) ) ;
63+ eventHandlers . Push ( new EventHandler ( ( obj , args ) => SetAppManifest ( ) ) ) ;
64+ eventHandlers . Push ( new EventHandler ( ( obj , args ) => RmAppManifest ( ) ) ) ;
4165 foreach ( Button button in app . GetButtons ( ) )
4266 {
4367 button . Click += eventHandlers . Pop ( ) ;
@@ -53,7 +77,6 @@ static void Main(string[] args)
5377 {
5478 GetGattCharacteristicsAsync ( ) ;
5579 }
56-
5780 Application . Run ( app ) ;
5881 }
5982
@@ -141,7 +164,15 @@ private static async void UseArgumentsAsync(string[] args)
141164 {
142165 await SendOnLighthouseAsync ( deactivateByte ) ;
143166 }
144- Application . Exit ( ) ;
167+ else if ( args [ 0 ] == "--reg" )
168+ {
169+ SetAppManifest ( ) ;
170+ }
171+ else if ( args [ 0 ] == "--rm" )
172+ {
173+ RmAppManifest ( ) ;
174+ }
175+ Exit ( ) ;
145176 }
146177
147178 private static void Log ( object msg )
@@ -156,10 +187,48 @@ private static void LogError(object msg)
156187 OnLog . Invoke ( msg , LogType . error ) ;
157188 }
158189
190+ private static void SetAppManifest ( )
191+ {
192+ EVRInitError evrInitError = EVRInitError . None ;
193+ OpenVR . Init ( ref evrInitError , EVRApplicationType . VRApplication_Utility ) ;
194+ if ( evrInitError != EVRInitError . None )
195+ {
196+ LogError ( evrInitError ) ;
197+ }
198+
199+ EVRApplicationError applicationError = OpenVR . Applications . AddApplicationManifest ( Directory . GetCurrentDirectory ( ) + @"\manifest.vrmanifest" , false ) ;
200+ if ( applicationError != EVRApplicationError . None ) {
201+ LogError ( applicationError ) ;
202+ }
203+ Log ( "Application manifest registered;" ) ;
204+ }
205+
206+ private static void RmAppManifest ( )
207+ {
208+ EVRInitError evrInitError = EVRInitError . None ;
209+ OpenVR . Init ( ref evrInitError , EVRApplicationType . VRApplication_Utility ) ;
210+ if ( evrInitError != EVRInitError . None )
211+ {
212+ LogError ( evrInitError ) ;
213+ }
214+
215+ EVRApplicationError applicationError = OpenVR . Applications . RemoveApplicationManifest ( Directory . GetCurrentDirectory ( ) + @"\manifest.vrmanifest" ) ;
216+ if ( applicationError != EVRApplicationError . None ) {
217+ LogError ( applicationError ) ;
218+ }
219+ Log ( "Application manifest removed;" ) ;
220+ }
221+
159222 public static void SendActiveStatus ( bool status )
160223 {
161224 SendOnLighthouseAsync ( ( status ) ? activateByte : deactivateByte ) ;
162225 }
226+
227+ private static void Exit ( )
228+ {
229+ OpenVR . Shutdown ( ) ;
230+ Application . Exit ( ) ;
231+ }
163232 }
164233
165234 public enum LogType
0 commit comments