[client] mouse: correct issues with cursor alignment on enter/exit/focus

This commit is contained in:
Geoffrey McRae 2021-01-17 12:53:55 +11:00
parent 76182bbeb8
commit e23d536af5
5 changed files with 107 additions and 64 deletions

View File

@ -146,6 +146,12 @@ static void sdlWarpPointer(int x, int y, bool exiting)
SDL_WarpMouseInWindow(app_getWindow(), x, y);
}
static void sdlRealignPointer(void)
{
// no need to care about grab, realign only happens in normal mode
app_handleMouseNormal(0, 0);
}
struct LG_DisplayServerOps LGDS_SDL =
{
.subsystem = SDL_SYSWM_UNKNOWN,
@ -161,6 +167,7 @@ struct LG_DisplayServerOps LGDS_SDL =
.grabKeyboard = sdlGrabKeyboard,
.ungrabKeyboard = sdlUngrabKeyboard,
.warpPointer = sdlWarpPointer,
.realignPointer = sdlRealignPointer,
/* SDL does not have clipboard support */
.cbInit = NULL,

View File

@ -391,6 +391,11 @@ static void waylandWarpPointer(int x, int y, bool exiting)
// This is an unsupported operation on Wayland.
}
static void waylandRealignPointer(void)
{
app_handleMouseBasic();
}
static void waylandFree(void)
{
waylandUngrabPointer();
@ -774,6 +779,7 @@ struct LG_DisplayServerOps LGDS_Wayland =
.grabKeyboard = waylandGrabKeyboard,
.ungrabKeyboard = waylandUngrabKeyboard,
.warpPointer = waylandWarpPointer,
.realignPointer = waylandRealignPointer,
.cbInit = waylandCBInit,
.cbNotice = waylandCBNotice,

View File

@ -92,9 +92,14 @@ struct LG_DisplayServerOps
void (*grabKeyboard)();
void (*ungrabKeyboard)();
//exiting = true if the warp is to leave the window
/* exiting = true if the warp is to leave the window */
void (*warpPointer)(int x, int y, bool exiting);
/* called when the client needs to realign the pointer. This should simply
* call the appropriate app_handleMouse* method for the platform with zero
* deltas */
void (*realignPointer)();
/* clipboard support */
bool (* cbInit)(void);
void (* cbNotice)(LG_ClipboardData type);

View File

@ -133,6 +133,24 @@ void app_updateCursorPos(double x, double y)
g_cursor.pos.y = y;
}
void app_handleFocusEvent(bool focused)
{
if (!app_inputEnabled())
return;
if (params.grabKeyboardOnFocus)
{
if (focused)
g_state.ds->grabKeyboard();
else
g_state.ds->ungrabKeyboard();
}
g_state.focused = focused;
g_cursor.realign = true;
g_state.ds->realignPointer();
}
static void alignToGuest(void)
{
if (SDL_HasEvent(e_SDLEvent))
@ -921,19 +939,22 @@ static void cursorToInt(double ex, double ey, int *x, int *y)
static void setCursorInView(bool enable)
{
// if we don't have focus don't do anything
// if the state has not changed, don't do anything else
if (g_cursor.inView == enable)
return;
if (enable && !g_state.focused)
return;
g_cursor.inView = enable;
g_cursor.draw = params.alwaysShowCursor ? true : enable;
g_cursor.redraw = true;
/* if the display server does not support warp, then we can not operate in
* always relative mode and we should not grab the pointer */
bool warpSupport = true;
app_getProp(LG_DS_WARP_SUPPORT, &warpSupport);
g_cursor.inView = enable;
g_cursor.draw = params.alwaysShowCursor ? true : enable;
g_cursor.redraw = true;
g_cursor.warpState = enable ? WARP_STATE_ON : WARP_STATE_OFF;
if (enable)
@ -951,7 +972,12 @@ static void setCursorInView(bool enable)
if (warpSupport)
g_state.ds->ungrabPointer();
if (!g_cursor.grab)
g_cursor.realign = true;
}
g_cursor.warpState = WARP_STATE_ON;
}
void app_handleMouseGrabbed(double ex, double ey)
@ -985,16 +1011,14 @@ void app_handleMouseGrabbed(double ex, double ey)
static void guestCurToLocal(struct DoublePoint *local)
{
local->x = (g_cursor.guest.x + g_cursor.guest.hx) / g_cursor.scale.x;
local->y = (g_cursor.guest.y + g_cursor.guest.hy) / g_cursor.scale.y;
local->x = g_state.dstRect.x +
(g_cursor.guest.x + g_cursor.guest.hx) / g_cursor.scale.x;
local->y = g_state.dstRect.y +
(g_cursor.guest.y + g_cursor.guest.hy) / g_cursor.scale.y;
}
void app_handleMouseNormal(double ex, double ey)
{
/* do not pass mouse events to the guest if we do not have focus */
if (!g_state.focused)
return;
// prevent cursor handling outside of capture if the position is not known
if (!g_cursor.guest.valid)
return;
@ -1007,8 +1031,6 @@ void app_handleMouseNormal(double ex, double ey)
}
bool testExit = true;
/* if the cursor was outside the viewport, check if it moved in */
if (!g_cursor.inView)
{
const bool inView =
@ -1017,9 +1039,25 @@ void app_handleMouseNormal(double ex, double ey)
g_cursor.pos.y >= g_state.dstRect.y &&
g_cursor.pos.y < g_state.dstRect.y + g_state.dstRect.h;
if (inView)
setCursorInView(inView);
}
/* nothing to do if we are outside the viewport */
if (!g_cursor.inView)
return;
/*
* do not pass mouse events to the guest if we do not have focus, this must be
* done after the inView test has been performed so that when focus is gained
* we know if we should be drawing the cursor.
*/
if (!g_state.focused)
return;
/* if we have been instructed to realign */
if (g_cursor.realign)
{
setCursorInView(true);
g_cursor.realign = false;
struct DoublePoint guest =
{
@ -1035,12 +1073,6 @@ void app_handleMouseNormal(double ex, double ey)
* loop otherwise */
testExit = false;
}
else
{
/* nothing to do if the cursor is not in the guest window */
return;
}
}
/* if we are in "autoCapture" and the delta was large don't test for exit */
if (params.autoCapture &&
@ -1053,17 +1085,15 @@ void app_handleMouseNormal(double ex, double ey)
/* check if the move would push the cursor outside the guest's viewport */
if (testExit && (
local.x + ex < 0.0 ||
local.y + ey < 0.0 ||
local.x + ex >= g_state.dstRect.w ||
local.y + ey >= g_state.dstRect.h))
local.x + ex < g_state.dstRect.x ||
local.y + ey < g_state.dstRect.y ||
local.x + ex >= g_state.dstRect.x + g_state.dstRect.w ||
local.y + ey >= g_state.dstRect.y + g_state.dstRect.h))
{
local.x += ex;
local.y += ey;
const int tx = ((local.x <= 0.0) ? floor(local.x) : ceil(local.x)) +
g_state.dstRect.x;
const int ty = ((local.y <= 0.0) ? floor(local.y) : ceil(local.y)) +
g_state.dstRect.y;
const int tx = (local.x <= 0.0) ? floor(local.x) : ceil(local.x);
const int ty = (local.y <= 0.0) ? floor(local.y) : ceil(local.y);
if (isValidCursorLocation(
g_state.windowPos.x + g_state.border.x + tx,
@ -1077,6 +1107,7 @@ void app_handleMouseNormal(double ex, double ey)
/* ungrab the pointer and move the local cursor to the exit point */
g_state.ds->ungrabPointer();
warpPointer(tx, ty, true);
return;
}
@ -1133,7 +1164,6 @@ void app_handleMouseBasic()
g_cursor.pos.y >= g_state.dstRect.y &&
g_cursor.pos.y < g_state.dstRect.y + g_state.dstRect.h;
if (inView != g_cursor.inView)
setCursorInView(inView);
if (g_cursor.guest.dpiScale == 0)
@ -1194,7 +1224,7 @@ void app_handleResizeEvent(int w, int h)
void app_handleWindowLeave(void)
{
g_cursor.inWindow = false;
g_cursor.inView = false;
setCursorInView(false);
if (!app_inputEnabled())
return;
@ -1209,6 +1239,8 @@ void app_handleWindowEnter(void)
g_cursor.inWindow = true;
if (!app_inputEnabled())
return;
g_cursor.realign = true;
}
static void setGrab(bool enable)
@ -1241,7 +1273,6 @@ static void setGrabQuiet(bool enable)
if (enable)
{
if (!g_cursor.inView)
setCursorInView(true);
if (params.grabKeyboard)
@ -1268,7 +1299,7 @@ static void setGrabQuiet(bool enable)
alignToGuest();
if (g_cursor.grab)
g_cursor.inView = true;
setCursorInView(true);
}
int eventFilter(void * userdata, SDL_Event * event)
@ -1319,21 +1350,11 @@ int eventFilter(void * userdata, SDL_Event * event)
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
g_state.focused = true;
if (!app_inputEnabled())
break;
if (params.grabKeyboardOnFocus)
g_state.ds->grabKeyboard();
app_handleFocusEvent(true);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
g_state.focused = false;
if (!app_inputEnabled())
break;
if (params.grabKeyboardOnFocus)
g_state.ds->ungrabKeyboard();
app_handleFocusEvent(false);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
@ -1715,6 +1736,7 @@ static int lg_run(void)
SET_FALLBACK(grabPointer);
SET_FALLBACK(ungrabKeyboard);
SET_FALLBACK(warpPointer);
SET_FALLBACK(realignPointer);
SET_FALLBACK(cbInit);
SET_FALLBACK(cbNotice);
SET_FALLBACK(cbRelease);

View File

@ -205,6 +205,9 @@ struct CursorState
/* true if the cursor is currently in the guest view area */
bool inView;
/* true if the guest should be realigned to the host when next drawn */
bool realign;
/* true if the cursor needs re-drawing/updating */
bool redraw;