void MainLoop()
    {
        while ( true )
        {
            std::this_thread::sleep_for( k_MonitorInterval );

#if defined( WIN32 )
            MSG msg = { 0 };
            while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }

            if ( msg.message == WM_QUIT )
                break;
#endif

            // Display instructions for user if we find any devices that need them
            ShowOverlay( BestOverlayForLeapDevices() );

            vr::VREvent_t Event;
            while ( vr::VRSystem()->PollNextEvent( &Event, sizeof( Event ) ) )
            {
                switch ( Event.eventType )
                {
                case vr::VREvent_Quit:
                    exit( 0 );
                    // NOTREAHED

                case vr::VREvent_TrackedDeviceActivated:
                case vr::VREvent_TrackedDeviceUpdated:
                    UpdateTrackedDevice( Event.trackedDeviceIndex );
                    break;

                case vr::VREvent_VendorSpecific_Reserved_Start + 0:
                    // User has made the "align" gesture.  The driver can't see the HMD
                    // coordinates, so we forward those from our client view.
                    if ( IsLeapDevice( Event.trackedDeviceIndex ) )
                    {
//                        printf("received event vr::VREvent_VendorSpecific_Reserved_Start + 0 for device %d\n", Event.trackedDeviceIndex);
                        TriggerRealignCoordinates( Event );
                    }
                    break;
                }
            }
        }
    }
    void MainLoop()
    {
        while ( true )
        {
            std::this_thread::sleep_for( k_MonitorInterval );

#if defined( WIN32 )
            MSG msg = { 0 };
            while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }

            if ( msg.message == WM_QUIT )
                break;
#endif

            // Display instructions for user if we find any devices that need them
            ShowOverlay( GetCurrentOverlayType() );

            vr::VREvent_t Event;
            while ( vr::VRSystem()->PollNextEvent( &Event, sizeof( Event ) ) )
            {
                switch ( Event.eventType )
                {
                case vr::VREvent_Quit:
                case vr::VREvent_DriverRequestedQuit: // The driver has requested that SteamVR shut down
                    exit( 0 );
                // NOTREAHED

                case vr::VREvent_TrackedDeviceActivated:
                case vr::VREvent_TrackedDeviceUpdated:
                    UpdateTrackedDevice( Event.trackedDeviceIndex );
                    break;

                case vr::VREvent_VendorSpecific_Reserved_Start + 0:
                    // Driver has requested HMD pose.
                    // The driver can't see the HMD coordinates, so we forward those from our client view.
                    if ( IsPSController( Event.trackedDeviceIndex ) )
                    {
                        SendHMDPose( Event );
                    }
                    break;
                }
            }
        }
    }
    bool Init()
    {
        // Start as "background" application.  This prevents vrserver from being started
        // on our behalf, and prevents us from keeping vrserver alive when everything else
        // exits.  This is very important because we're spawning from a driver, and any
        // class besides "background" would keep vrserver running forever
        vr::EVRInitError eVRInitError;
        vr::VR_Init( &eVRInitError, vr::VRApplication_Background );
        if ( !vr::VRSystem() || eVRInitError != vr::VRInitError_None )
            return false;
        
        // Keep track of which devices use driver_leap
        for ( int i = 0; i < vr::k_unMaxTrackedDeviceCount; ++i )
        {
            UpdateTrackedDevice( i );
        }

        return true;
    }