[client] spice: just minor refactoring and commenting of code

This commit is contained in:
Geoffrey McRae 2021-01-04 15:40:02 +11:00
parent 3016f0c53e
commit 5d5b7b3d3c

View File

@ -87,8 +87,8 @@ static void lgInit()
g_cursor.scale = false; g_cursor.scale = false;
g_cursor.scaleX = 1.0f; g_cursor.scaleX = 1.0f;
g_cursor.scaleY = 1.0f; g_cursor.scaleY = 1.0f;
g_cursor.draw = true; g_cursor.draw = true;
g_cursor.inView = true; g_cursor.inView = false;
g_cursor.guest.valid = false; g_cursor.guest.valid = false;
} }
@ -810,6 +810,23 @@ static bool isValidCursorLocation(int x, int y)
static void handleMouseMoveEvent(int ex, int ey) static void handleMouseMoveEvent(int ex, int ey)
{ {
if (!params.useSpiceInput)
return;
/* check if there is a warp in progress, and if it was completed */
if (g_cursor.warpState == WARP_STATE_ACTIVE &&
ex == g_cursor.warpTo.x && ey == g_cursor.warpTo.y)
{
g_cursor.last.x = ex;
g_cursor.last.y = ey;
g_cursor.warpState = WARP_STATE_ON;
return;
}
if (!g_cursor.inWindow || g_state.ignoreInput)
return;
/* calculate the relative movement */
SDL_Point delta = { SDL_Point delta = {
.x = ex - g_cursor.last.x, .x = ex - g_cursor.last.x,
.y = ey - g_cursor.last.y .y = ey - g_cursor.last.y
@ -821,16 +838,6 @@ static void handleMouseMoveEvent(int ex, int ey)
g_cursor.last.x = ex; g_cursor.last.x = ex;
g_cursor.last.y = ey; g_cursor.last.y = ey;
if (g_cursor.warpState == WARP_STATE_ACTIVE &&
ex == g_cursor.warpTo.x && ey == g_cursor.warpTo.y)
{
g_cursor.warpState = WARP_STATE_ON;
return;
}
if (!g_cursor.inWindow || g_state.ignoreInput || !params.useSpiceInput)
return;
/* if we don't have the current cursor pos just send cursor movements */ /* if we don't have the current cursor pos just send cursor movements */
if (!g_cursor.guest.valid) if (!g_cursor.guest.valid)
{ {
@ -846,13 +853,14 @@ static void handleMouseMoveEvent(int ex, int ey)
return; return;
} }
const bool inView = !( /* check if the cursor is in the guests viewport */
ex < g_state.dstRect.x || const bool inView =
ex >= g_state.dstRect.x + g_state.dstRect.w || ex >= g_state.dstRect.x &&
ey < g_state.dstRect.y || ex < g_state.dstRect.x + g_state.dstRect.w &&
ey >= g_state.dstRect.y + g_state.dstRect.h); ey >= g_state.dstRect.y &&
ey < g_state.dstRect.y + g_state.dstRect.h;
/* if the cursor is to move in/outside the display area */ /* if the cursor has moved in/outside the display area */
if (g_cursor.inView != inView) if (g_cursor.inView != inView)
{ {
g_cursor.inView = inView; g_cursor.inView = inView;
@ -869,8 +877,6 @@ static void handleMouseMoveEvent(int ex, int ey)
if (g_cursor.warpState == WARP_STATE_OFF) if (g_cursor.warpState == WARP_STATE_OFF)
g_cursor.warpState = WARP_STATE_ON; g_cursor.warpState = WARP_STATE_ON;
warpMouse(g_state.windowW / 2, g_state.windowH / 2);
/* convert guest to local and calculate the delta */ /* convert guest to local and calculate the delta */
const int lx = ((g_cursor.guest.x + g_cursor.guest.hx) / g_cursor.scaleX) + g_state.dstRect.x; const int lx = ((g_cursor.guest.x + g_cursor.guest.hx) / g_cursor.scaleX) + g_state.dstRect.x;
const int ly = ((g_cursor.guest.y + g_cursor.guest.hy) / g_cursor.scaleY) + g_state.dstRect.y; const int ly = ((g_cursor.guest.y + g_cursor.guest.hy) / g_cursor.scaleY) + g_state.dstRect.y;
@ -886,14 +892,15 @@ static void handleMouseMoveEvent(int ex, int ey)
g_cursor.draw = false; g_cursor.draw = false;
} }
} }
else if (inView)
if (inView)
{ {
/* stop the mouse from runing into the edges of the window */
if (ex < g_state.windowCX - 25 || ex > g_state.windowCX + 25 || if (ex < g_state.windowCX - 25 || ex > g_state.windowCX + 25 ||
ey < g_state.windowCY - 25 || ey > g_state.windowCY + 25) ey < g_state.windowCY - 25 || ey > g_state.windowCY + 25)
warpMouse(g_state.windowCX, g_state.windowCY); warpMouse(g_state.windowCX, g_state.windowCY);
} }
else
if (!inView)
{ {
/* cursor outside of the bounds, don't do anything */ /* cursor outside of the bounds, don't do anything */
return; return;
@ -919,6 +926,9 @@ static void handleMouseMoveEvent(int ex, int ey)
g_cursor.sensY -= delta.y; g_cursor.sensY -= delta.y;
} }
/* if the cursor is not grabbed and warp is possible, check if the translated
* guest cursor movement would live the window, and if so move the host cursor
* to the exit location and enable it, but only if the target is valid */
if (!g_cursor.grab && g_cursor.warpState == WARP_STATE_ON) if (!g_cursor.grab && g_cursor.warpState == WARP_STATE_ON)
{ {
const float fx = (float)(g_cursor.guest.x + g_cursor.guest.hx + delta.x) / const float fx = (float)(g_cursor.guest.x + g_cursor.guest.hx + delta.x) /