Skip to content

Commit

Permalink
OpenVRBackend: Consider visible when an appid's scene app is visible
Browse files Browse the repository at this point in the history
  • Loading branch information
misyltoad committed Aug 23, 2024
1 parent a4a4948 commit f03168c
Showing 1 changed file with 107 additions and 9 deletions.
116 changes: 107 additions & 9 deletions src/Backends/OpenVRBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace vr
const EVRButtonId k_EButton_QAM = (EVRButtonId)(51);
}

uint32_t get_appid_from_pid( pid_t pid );

///////////////////////////////////////////////
// Josh:
// GetVulkanInstanceExtensionsRequired and GetVulkanDeviceExtensionsRequired return *space separated* exts :(
Expand Down Expand Up @@ -307,6 +309,28 @@ namespace gamescope
bool ConsumeNudgeToVisible() { return std::exchange( m_bNudgeToVisible, false ); }
bool IsRelativeMouse() const { return m_bRelativeMouse; }

// Thread safe.
bool IsVisible() const
{
return m_bOverlayShown || m_bSceneAppVisible;
}

// Only called from event thread
void MarkOverlayShown( bool bShown )
{
m_bOverlayShown = bShown;
UpdateVisibility( "Overlay Visibility" );
}

// Only called from event thread
void MarkSceneAppShown( bool bShown )
{
m_bSceneAppVisible = bShown;
UpdateVisibility( "Scene App Visibility" );
}

void UpdateVisibility( const char *pszReason );

private:
COpenVRBackend *m_pBackend = nullptr;
COpenVRPlane m_Planes[8];
Expand All @@ -316,6 +340,10 @@ namespace gamescope

bool m_bNudgeToVisible = false;
std::atomic<bool> m_bRelativeMouse = false;

bool m_bWasVisible = false; // Event thread only
std::atomic<bool> m_bOverlayShown = { false };
std::atomic<bool> m_bSceneAppVisible = { false };
};

class COpenVRBackend final : public CBaseBackend
Expand Down Expand Up @@ -843,6 +871,46 @@ namespace gamescope
break;
}

case vr::VREvent_SceneApplicationChanged:
{
if ( m_uCurrentScenePid != vrEvent.data.process.pid )
{
m_uCurrentScenePid = vrEvent.data.process.pid;
m_uCurrentSceneAppId = get_appid_from_pid( m_uCurrentScenePid );

openvr_log.debugf( "SceneApplicationChanged -> pid: %u appid: %u", m_uCurrentScenePid, m_uCurrentSceneAppId );

std::optional<VirtualConnectorKey_t> oulNewSceneAppVirtualConnectorKey;
if ( cv_backend_virtual_connector_strategy == VirtualConnectorStrategies::PerAppId )
{
oulNewSceneAppVirtualConnectorKey = m_uCurrentSceneAppId;
}

if ( ( oulNewSceneAppVirtualConnectorKey || m_oulCurrentSceneVirtualConnectorKey ) &&
( oulNewSceneAppVirtualConnectorKey != m_oulCurrentSceneVirtualConnectorKey ) )
{
for ( COpenVRConnector *pOtherConnector : m_pActiveConnectors )
{
if ( oulNewSceneAppVirtualConnectorKey )
{
if ( pOtherConnector->GetVirtualConnectorKey() == *oulNewSceneAppVirtualConnectorKey )
pOtherConnector->MarkSceneAppShown( true );
}

if ( m_oulCurrentSceneVirtualConnectorKey )
{
if ( pOtherConnector->GetVirtualConnectorKey() == *m_oulCurrentSceneVirtualConnectorKey )
pOtherConnector->MarkSceneAppShown( false );
}
}
}

m_oulCurrentSceneVirtualConnectorKey = oulNewSceneAppVirtualConnectorKey;
}

break;
}

case vr::VREvent_KeyboardCharInput:
{
if (m_pIME)
Expand Down Expand Up @@ -992,15 +1060,7 @@ namespace gamescope
// or for other reasons.
if ( !plane.IsSubview() )
{
int nNewOverlayVisibleCount;
if ( vrEvent.eventType == vr::VREvent_OverlayShown )
nNewOverlayVisibleCount = ++m_nOverlaysVisible;
else
nNewOverlayVisibleCount = --m_nOverlaysVisible;

openvr_log.debugf( "nNewOverlayVisibleCount: %d", nNewOverlayVisibleCount );
m_nOverlaysVisible.notify_all();
assert( m_nOverlaysVisible >= 0 );
pConnector->MarkOverlayShown( vrEvent.eventType == vr::VREvent_OverlayShown );
}
break;
}
Expand Down Expand Up @@ -1049,6 +1109,10 @@ namespace gamescope
glm::vec2 m_vScreenTrackpadPos{};
glm::vec2 m_vScreenStartTrackpadPos{};

uint32_t m_uCurrentScenePid = -1;
uint32_t m_uCurrentSceneAppId = 0;
std::optional<uint64_t> m_oulCurrentSceneVirtualConnectorKey;

friend COpenVRConnector;
std::vector<COpenVRConnector*> m_pActiveConnectors;
std::mutex m_mutActiveConnectors;
Expand All @@ -1071,6 +1135,9 @@ namespace gamescope
{
std::scoped_lock lock{ m_pBackend->m_mutActiveConnectors };

MarkSceneAppShown( false );
MarkOverlayShown( false );

auto iter = m_pBackend->m_pActiveConnectors.begin();
for ( ; iter != m_pBackend->m_pActiveConnectors.end(); iter++ )
{
Expand Down Expand Up @@ -1330,6 +1397,8 @@ namespace gamescope

bool COpenVRConnector::Init()
{
openvr_log.debugf( "New connector! -> ulKey: %lu", GetVirtualConnectorKey() );

m_bNudgeToVisible = m_pBackend->ShouldNudgeToVisible();

for ( uint32_t i = 0; i < 8; i++ )
Expand All @@ -1344,10 +1413,39 @@ namespace gamescope

if ( g_bForceRelativeMouse )
this->SetRelativeMouseMode( true );

if ( m_pBackend->m_oulCurrentSceneVirtualConnectorKey &&
GetVirtualConnectorKey() == *m_pBackend->m_oulCurrentSceneVirtualConnectorKey )
{
MarkSceneAppShown( true );
}

return true;
}

void COpenVRConnector::UpdateVisibility( const char *pszReason )
{
bool bVisible = IsVisible();
if ( m_bWasVisible != bVisible )
{
int nNewOverlayVisibleCount;
if ( bVisible )
nNewOverlayVisibleCount = ++m_pBackend->m_nOverlaysVisible;
else
nNewOverlayVisibleCount = --m_pBackend->m_nOverlaysVisible;

m_pBackend->m_nOverlaysVisible.notify_all();

m_bWasVisible = bVisible;
openvr_log.debugf( "[%s] ulKey: %lu nNewOverlayVisibleCount: %d -> m_bOverlayShown: %s m_bSceneAppVisible: %s",
pszReason,
GetVirtualConnectorKey(),
nNewOverlayVisibleCount,
m_bOverlayShown ? "true" : "false",
m_bSceneAppVisible ? "true" : "false" );
}
}

/////////////////////////
// COpenVRFb
/////////////////////////
Expand Down

0 comments on commit f03168c

Please sign in to comment.