From 80f3c7934a8d404e9b819593940f6847cfa09e41 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sat, 11 Jan 2020 05:22:12 +1100 Subject: [PATCH] [client] more cursor tweaks and some cleanup --- VERSION | 2 +- client/src/main.c | 71 ++++++++++++++++++++++++++++++----------------- client/src/main.h | 13 ++++++--- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/VERSION b/VERSION index 2e4d05b0..e0bdedc3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -B1-75-g5b163063c3+1 \ No newline at end of file +B1-76-g1341bf8fbd+1 \ No newline at end of file diff --git a/client/src/main.c b/client/src/main.c index 047eb2e6..678a272c 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -67,17 +67,9 @@ struct AppState state; // this structure is initialized in config.c struct AppParams params = { 0 }; -static int lastX = 0; -static int lastY = 0; -static void alignMouseWithGuest() -{ - if (state.ignoreInput || !params.useSpiceInput) - return; - - lastX = (int)round((float)state.cursor.x / state.scaleX) + state.dstRect.x; - lastY = (int)round((float)state.cursor.y / state.scaleY) + state.dstRect.y; - SDL_WarpMouseInWindow(state.window, lastX, lastY); -} +static void handleMouseMoveEvent(int ex, int ey); +static void alignMouseWithGuest(); +static void alignMouseWithHost(); static void updatePositionInfo() { @@ -292,7 +284,12 @@ static int cursorThread(void * unused) state.cursor.x = cursor->x; state.cursor.y = cursor->y; state.cursorVisible = cursor->visible; - state.haveCursorPos = true; + + if (!state.haveCursorPos) + { + state.haveCursorPos = true; + alignMouseWithHost(); + } if (msg.udata == 1) { @@ -331,14 +328,6 @@ static int cursorThread(void * unused) state.cursor.x, state.cursor.y ); - - const uint64_t now = microtime(); - static uint64_t nextAlign = 0; - if (!state.serverMode && now > nextAlign) - { - nextAlign = now + 100000; - alignMouseWithGuest(); - } } lgmpClientUnsubscribe(&queue); @@ -650,14 +639,17 @@ static void handleMouseMoveEvent(int ex, int ey) if (state.ignoreInput || !params.useSpiceInput) return; + state.curlocalX = ex; + state.curlocalY = ey; + if (state.serverMode) { if (wrapping) { if (ex == state.windowW / 2 && ey == state.windowH / 2) { - lastX += (state.windowW / 2) - wrapX; - lastY += (state.windowH / 2) - wrapY; + state.curLastX += (state.windowW / 2) - wrapX; + state.curLastY += (state.windowH / 2) - wrapY; wrapping = false; } } @@ -683,10 +675,10 @@ static void handleMouseMoveEvent(int ex, int ey) return; } - int rx = ex - lastX; - int ry = ey - lastY; - lastX = ex; - lastY = ey; + int rx = ex - state.curLastX; + int ry = ey - state.curLastY; + state.curLastX = ex; + state.curLastY = ey; if (rx == 0 && ry == 0) return; @@ -715,6 +707,29 @@ static void handleMouseMoveEvent(int ex, int ey) DEBUG_ERROR("failed to send mouse motion message"); } +static void alignMouseWithGuest() +{ + if (state.ignoreInput || !params.useSpiceInput) + return; + + state.curLastX = (int)round((float)state.cursor.x / state.scaleX) + state.dstRect.x; + state.curLastY = (int)round((float)state.cursor.y / state.scaleY) + state.dstRect.y; + SDL_WarpMouseInWindow(state.window, state.curLastX, state.curLastY); +} + +static void alignMouseWithHost() +{ + if (state.ignoreInput || !params.useSpiceInput) + return; + + if (!state.haveCursorPos || state.serverMode) + return; + + state.curLastX = (int)round((float)state.cursor.x / state.scaleX) + state.dstRect.x; + state.curLastY = (int)round((float)state.cursor.y / state.scaleY) + state.dstRect.y; + handleMouseMoveEvent(state.curlocalX, state.curlocalY); +} + static void handleResizeEvent(unsigned int w, unsigned int h) { if (state.windowW == w || state.windowH == w) @@ -743,6 +758,10 @@ int eventFilter(void * userdata, SDL_Event * event) { switch(event->window.event) { + case SDL_WINDOWEVENT_ENTER: + alignMouseWithHost(); + break; + case SDL_WINDOWEVENT_SIZE_CHANGED: case SDL_WINDOWEVENT_RESIZED: if (state.wminfo.subsystem != SDL_SYSWM_X11) diff --git a/client/src/main.h b/client/src/main.h index 5e2892ec..98b09f36 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -43,10 +43,15 @@ struct AppState LG_RendererRect dstRect; SDL_Point cursor; bool cursorVisible; - bool haveCursorPos; - float scaleX, scaleY; - float accX, accY; - bool serverMode; + + bool serverMode; + bool haveCursorPos; + float scaleX, scaleY; + float accX, accY; + int curLastX; + int curLastY; + int curlocalX; + int curlocalY; const LG_Renderer * lgr; void * lgrData;