Skip to content

Commit

Permalink
Make foregrounding a separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
oneup03 committed Sep 11, 2024
1 parent d5bc02a commit 76e1be5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
59 changes: 36 additions & 23 deletions vrto3d/src/hmd_device_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ vr::EVRInitError MockControllerDeviceDriver::Activate( uint32_t unObjectId )
{
device_index_ = unObjectId;
is_active_ = true;
is_on_top_ = false;

// A list of properties available is contained in vr::ETrackedDeviceProperty.
auto* vrp = vr::VRProperties();
Expand Down Expand Up @@ -287,6 +288,7 @@ vr::EVRInitError MockControllerDeviceDriver::Activate( uint32_t unObjectId )
vrs->SetBool(vr::k_pch_SteamVR_Section, vr::k_pch_SteamVR_ForceFadeOnBadTracking_Bool, false);

pose_update_thread_ = std::thread( &MockControllerDeviceDriver::PoseUpdateThread, this );
focus_update_thread_ = std::thread(&MockControllerDeviceDriver::FocusUpdateThread, this);

return vr::VRInitError_None;
}
Expand Down Expand Up @@ -427,31 +429,9 @@ void MockControllerDeviceDriver::PoseUpdateThread()
static int sleep_time = (int)(floor(1000.0 / stereo_display_component_->GetConfig().display_frequency));
static int height_sleep = 0;
static int top_sleep = 0;
static uint32_t window_count = 0;
static bool always_on_top = false;
static HWND vr_window = NULL;
static HWND top_window = NULL;

while ( is_active_ )
{
// Keep VR display always on top for 3D rendering
if (always_on_top) {
if (window_count == 0) {
top_window = GetTopWindow(GetDesktopWindow());
}
if (vr_window != NULL && vr_window != top_window) {
SetWindowPos(vr_window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
else if (vr_window == NULL) {
vr_window = FindWindow(NULL, L"Headset Window");
}
window_count = (window_count + 1) % 100;
}
else if (vr_window != NULL) {
SetWindowPos(vr_window, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
vr_window = NULL;
}

// Inform the vrserver that our tracked device's pose has updated, giving it the pose returned by our GetPose().
vr::VRServerDriverHost()->TrackedDevicePoseUpdated( device_index_, GetPose(), sizeof( vr::DriverPose_t ) );

Expand All @@ -478,7 +458,7 @@ void MockControllerDeviceDriver::PoseUpdateThread()
// Ctrl+F8 Toggle Always On Top
if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) && (GetAsyncKeyState(VK_F8) & 0x8000) && top_sleep == 0) {
top_sleep = stereo_display_component_->GetConfig().sleep_count_max;
always_on_top = !always_on_top;
is_on_top_ = !is_on_top_;
}
else if (top_sleep > 0) {
top_sleep--;
Expand All @@ -500,6 +480,39 @@ void MockControllerDeviceDriver::PoseUpdateThread()
}
}


//-----------------------------------------------------------------------------
// Purpose: Keep Headset Window on top if set
//-----------------------------------------------------------------------------
void MockControllerDeviceDriver::FocusUpdateThread()
{
static int sleep_time = 1000;
static HWND vr_window = NULL;
static HWND top_window = NULL;

while (is_active_)
{
// Keep VR display always on top for 3D rendering
if (is_on_top_) {
top_window = GetTopWindow(GetDesktopWindow());
if (vr_window != NULL && vr_window != top_window) {
SetWindowPos(vr_window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
else if (vr_window == NULL) {
vr_window = FindWindow(NULL, L"Headset Window");
}
}
else if (vr_window != NULL) {
SetWindowPos(vr_window, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
vr_window = NULL;
}

// Sleep for 1s
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
}
}


//-----------------------------------------------------------------------------
// Purpose: Save Depth and Convergence to Steam\config\steamvr.vrsettings
//-----------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions vrto3d/src/hmd_device_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class MockControllerDeviceDriver : public vr::ITrackedDeviceServerDriver
void Deactivate() override;

void PoseUpdateThread();
void FocusUpdateThread();
void SaveDepthConv();

private:
Expand All @@ -133,6 +134,8 @@ class MockControllerDeviceDriver : public vr::ITrackedDeviceServerDriver

std::atomic< bool > is_active_;
std::atomic< uint32_t > device_index_;
std::atomic< bool > is_on_top_;

std::thread pose_update_thread_;
std::thread focus_update_thread_;
};

0 comments on commit 76e1be5

Please sign in to comment.