diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp index 17bae3a..91ec0be 100644 --- a/src/steamcompmgr.cpp +++ b/src/steamcompmgr.cpp @@ -32,6 +32,7 @@ #include "backend.h" #include "gamescope_shared.h" #include "xwayland_ctx.hpp" +#include #include #include #include @@ -2088,6 +2089,7 @@ void MouseCursor::QueryRootPosition( int *pX, int *pY ) XQueryPointer( m_ctx->dpy, DefaultRootWindow( m_ctx->dpy ), &root_ret, &child_ret, pX, pY, &win_x, &win_y, &mask ); } +static bool s_bFollowClientPointer = false; static bool relative_mouse_forced(); void MouseCursor::checkSuspension() @@ -3083,12 +3085,101 @@ static gamescope::ConVar cv_relative_mouse( "relative_mouse", 0, "0 = Leave it to the game and the nested hints. 1 = Send host pointer motion as relative movement, for games that confine the cursor and warp it back to read movement. 2 = Never go relative." ); static bool relative_mouse_forced() { - return cv_relative_mouse.Get() == 1; + return cv_relative_mouse.Get() == 1 || s_bFollowClientPointer; +} + +static void update_client_pointer_follow( global_focus_t *pFocus, bool bAllowed ) +{ + static int s_nAwayFrames = 0; + static int s_nQuietFrames = 0; + static uint64_t s_ulLastAway = 0; + static double s_flLastX = 0.0; + static double s_flLastY = 0.0; + + int nClientX = 0; + int nClientY = 0; + if ( bAllowed && pFocus->cursor ) + pFocus->cursor->QueryRootPosition( &nClientX, &nClientY ); + else + bAllowed = false; + + steamcompmgr_win_t *pInputWindow = pFocus->inputFocusWindow; + if ( pInputWindow && pInputWindow->type == steamcompmgr_win_type_t::XWAYLAND ) + { + nClientX -= pInputWindow->xwayland().a.x; + nClientY -= pInputWindow->xwayland().a.y; + } + + wlserver_lock(); + const double flX = wlserver.mouse_surface_cursorx; + const double flY = wlserver.mouse_surface_cursory; + if ( s_bFollowClientPointer && !wlserver.bFollowClientPointer ) + { + s_nAwayFrames = 0; + s_bFollowClientPointer = false; + } + if ( !bAllowed ) + { + s_nAwayFrames = 0; + s_bFollowClientPointer = false; + wlserver.bFollowClientPointer = false; + } + else if ( !s_bFollowClientPointer ) + { + const bool bMoved = flX != s_flLastX || flY != s_flLastY; + if ( bMoved && ( std::abs( nClientX - flX ) > 48.0 || std::abs( nClientY - flY ) > 48.0 ) ) + { + const uint64_t ulNow = get_time_in_nanos(); + if ( ulNow - s_ulLastAway > 500'000'000ul ) + s_nAwayFrames = 0; + s_ulLastAway = ulNow; + s_nAwayFrames++; + } + + if ( s_nAwayFrames >= 3 ) + { + s_nAwayFrames = 0; + s_nQuietFrames = 0; + s_bFollowClientPointer = true; + wlserver.bFollowClientPointer = true; + wlserver.flFollowDX = 0.0; + wlserver.flFollowDY = 0.0; + wlserver_mousewarp( nClientX, nClientY, 0, true ); + } + } + else + { + const bool bWarped = std::abs( nClientX - flX ) > 2.0 || std::abs( nClientY - flY ) > 2.0; + const double flDX = std::trunc( wlserver.flFollowDX ); + const double flDY = std::trunc( wlserver.flFollowDY ); + if ( bWarped ) + s_nQuietFrames = 0; + else if ( flDX != 0.0 || flDY != 0.0 ) + s_nQuietFrames++; + + if ( flDX != 0.0 || flDY != 0.0 || bWarped ) + { + wlserver.flFollowDX -= flDX; + wlserver.flFollowDY -= flDY; + wlserver_mousewarp( nClientX, nClientY, 0, true ); + if ( flDX != 0.0 || flDY != 0.0 ) + wlserver_mousewarp( nClientX + flDX, nClientY + flDY, 0, true ); + } + + if ( s_nQuietFrames >= 60 ) + { + s_bFollowClientPointer = false; + wlserver.bFollowClientPointer = false; + } + } + s_flLastX = wlserver.mouse_surface_cursorx; + s_flLastY = wlserver.mouse_surface_cursory; + wlserver_unlock(); } bool ShouldDrawCursor() { - if ( cv_relative_mouse.Get() == 1 ) + if ( relative_mouse_forced() ) return true; global_focus_t *pFocus = GetCurrentFocus(); @@ -10892,9 +10983,15 @@ steamcompmgr_main(int argc, char **argv) const int nRelativeMouse = cv_relative_mouse.Get(); const bool bForcedRelativeMouse = nRelativeMouse == 1 && !window_is_steam( pPaintFocus->inputFocusWindow ); + if ( vblank ) + { + update_client_pointer_follow( pPaintFocus, + nRelativeMouse != 2 && !bExcludedAppId && !bHasPointerConstraint && !window_is_steam( pPaintFocus->inputFocusWindow ) ); + } + const bool bRelativeMouseMode = nRelativeMouse == 2 ? false - : ( bForcedRelativeMouse || ( bImageEmpty && bHasPointerConstraint && !bExcludedAppId ) ); + : ( bForcedRelativeMouse || s_bFollowClientPointer || ( bImageEmpty && bHasPointerConstraint && !bExcludedAppId ) ); static bool s_bRelativeMouseMode = false; if ( s_bRelativeMouseMode && !bRelativeMouseMode && pPaintFocus->cursor && diff --git a/src/wlserver.cpp b/src/wlserver.cpp index 5733871..df23f8a 100644 --- a/src/wlserver.cpp +++ b/src/wlserver.cpp @@ -1,3 +1,4 @@ +#include #include #define _GNU_SOURCE 1 @@ -3086,6 +3087,16 @@ void wlserver_mousemotion( double dx, double dy, uint32_t time ) wlserver_perform_rel_pointer_motion( dx, dy ); + if ( wlserver.bFollowClientPointer ) + { + wlserver.flFollowDX += dx; + wlserver.flFollowDY += dy; + wlserver.ulLastMovedCursorTime = get_time_in_nanos(); + wlserver.bCursorHidden = !wlserver.bCursorHasImage; + wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); + return; + } + if ( !wlserver_apply_constraint( &dx, &dy ) ) { wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); @@ -3110,10 +3121,22 @@ void wlserver_mousemotion( double dx, double dy, uint32_t time ) wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); } -void wlserver_mousewarp( double x, double y, uint32_t time, bool bSynthetic ) +void wlserver_mousewarp( double x, double y, uint32_t time, bool bSynthetic, bool bFromTouch ) { assert( wlserver_is_lock_held() ); + if ( !bSynthetic && wlserver.bFollowClientPointer ) + { + if ( !bFromTouch ) + return; + wlserver.bFollowClientPointer = false; + } + + const double flStepX = x - wlserver.mouse_surface_cursorx; + const double flStepY = y - wlserver.mouse_surface_cursory; + if ( !bSynthetic && !bFromTouch && ( flStepX != 0.0 || flStepY != 0.0 ) && std::abs( flStepX ) < 256.0 && std::abs( flStepY ) < 256.0 ) + wlserver_perform_rel_pointer_motion( flStepX, flStepY ); + wlserver.mouse_surface_cursorx = x; wlserver.mouse_surface_cursory = y; @@ -3304,7 +3327,7 @@ void wlserver_touchmotion( double x, double y, int touch_id, uint32_t time, bool { g_bPendingTouchMovement = true; - wlserver_mousewarp( tx, ty, time, false ); + wlserver_mousewarp( tx, ty, time, false, !bAlwaysWarpCursor ); } } @@ -3352,7 +3375,7 @@ void wlserver_touchdown( double x, double y, int touch_id, uint32_t time, gamesc if ( eMode != gamescope::TouchClickModes::Trackpad ) { - wlserver_mousewarp( tx, ty, time, false ); + wlserver_mousewarp( tx, ty, time, false, true ); } uint32_t button = TouchClickModeToLinuxButton( eMode ); diff --git a/src/wlserver.hpp b/src/wlserver.hpp index 79a0723..ca8d015 100644 --- a/src/wlserver.hpp +++ b/src/wlserver.hpp @@ -168,6 +168,9 @@ struct wlserver_t { uint64_t ulLastMovedCursorTime = 0; bool bCursorHidden = true; bool bCursorHasImage = true; + bool bFollowClientPointer = false; + double flFollowDX = 0.0; + double flFollowDY = 0.0; bool button_held[ WLSERVER_BUTTON_COUNT ]; std::set touch_down_ids; @@ -271,7 +274,7 @@ void wlserver_clear_dropdowns(); void wlserver_notify_dropdown( struct wlr_surface *wlrsurface, int nX, int nY ); void wlserver_mousemotion( double x, double y, uint32_t time ); void wlserver_mousehide(); -void wlserver_mousewarp( double x, double y, uint32_t time, bool bSynthetic ); +void wlserver_mousewarp( double x, double y, uint32_t time, bool bSynthetic, bool bFromTouch = false ); void wlserver_mousebutton( int button, bool press, uint32_t time ); void wlserver_mousewheel( double x, double y, uint32_t time ); bool wlserver_input_held();