[client] app: add and expose app_handleKeyPress and app_handleKeyRelease

This commit is contained in:
Tudor Brindus 2021-01-17 21:20:42 -05:00 committed by Geoffrey McRae
parent cac454d9cf
commit 7c9b273f70
2 changed files with 90 additions and 78 deletions

View File

@ -37,6 +37,8 @@ void app_handleMouseNormal(double ex, double ey);
void app_handleMouseBasic(void);
void app_handleButtonPress(int button);
void app_handleButtonRelease(int button);
void app_handleKeyPress(int scancode);
void app_handleKeyRelease(int scancode);
void app_handleWindowEnter(void);
void app_handleWindowLeave(void);
void app_handleFocusEvent(bool focused);

View File

@ -1031,6 +1031,92 @@ void app_handleButtonRelease(int button)
DEBUG_ERROR("SDL_MOUSEBUTTONUP: failed to send message");
}
void app_handleKeyPress(int sc)
{
if (sc == params.escapeKey && !g_state.escapeActive)
{
g_state.escapeActive = true;
g_state.escapeAction = -1;
return;
}
if (g_state.escapeActive)
{
g_state.escapeAction = sc;
return;
}
if (!app_inputEnabled())
return;
if (params.ignoreWindowsKeys &&
(sc == SDL_SCANCODE_LGUI || sc == SDL_SCANCODE_RGUI))
return;
uint32_t scancode = mapScancode(sc);
if (scancode == 0)
return;
if (!g_state.keyDown[sc])
{
if (spice_key_down(scancode))
g_state.keyDown[sc] = true;
else
{
DEBUG_ERROR("SDL_KEYDOWN: failed to send message");
return;
}
}
}
void app_handleKeyRelease(int sc)
{
if (g_state.escapeActive)
{
if (g_state.escapeAction == -1)
{
if (params.useSpiceInput)
setGrab(!g_cursor.grab);
}
else
{
KeybindHandle handle = g_state.bindings[sc];
if (handle)
{
handle->callback(sc, handle->opaque);
return;
}
}
if (sc == params.escapeKey)
g_state.escapeActive = false;
}
if (!app_inputEnabled())
return;
// avoid sending key up events when we didn't send a down
if (!g_state.keyDown[sc])
return;
if (params.ignoreWindowsKeys &&
(sc == SDL_SCANCODE_LGUI || sc == SDL_SCANCODE_RGUI))
return;
uint32_t scancode = mapScancode(sc);
if (scancode == 0)
return;
if (spice_key_up(scancode))
g_state.keyDown[sc] = false;
else
{
DEBUG_ERROR("SDL_KEYUP: failed to send message");
return;
}
}
static void guestCurToLocal(struct DoublePoint *local)
{
local->x = g_state.dstRect.x +
@ -1374,90 +1460,14 @@ int eventFilter(void * userdata, SDL_Event * event)
case SDL_KEYDOWN:
{
SDL_Scancode sc = event->key.keysym.scancode;
if (sc == params.escapeKey && !g_state.escapeActive)
{
g_state.escapeActive = true;
g_state.escapeAction = -1;
break;
}
if (g_state.escapeActive)
{
g_state.escapeAction = sc;
break;
}
if (!app_inputEnabled())
break;
if (params.ignoreWindowsKeys &&
(sc == SDL_SCANCODE_LGUI || sc == SDL_SCANCODE_RGUI))
break;
uint32_t scancode = mapScancode(sc);
if (scancode == 0)
break;
if (!g_state.keyDown[sc])
{
if (spice_key_down(scancode))
g_state.keyDown[sc] = true;
else
{
DEBUG_ERROR("SDL_KEYDOWN: failed to send message");
break;
}
}
app_handleKeyPress(sc);
break;
}
case SDL_KEYUP:
{
SDL_Scancode sc = event->key.keysym.scancode;
if (g_state.escapeActive)
{
if (g_state.escapeAction == -1)
{
if (params.useSpiceInput)
setGrab(!g_cursor.grab);
}
else
{
KeybindHandle handle = g_state.bindings[sc];
if (handle)
{
handle->callback(sc, handle->opaque);
break;
}
}
if (sc == params.escapeKey)
g_state.escapeActive = false;
}
if (!app_inputEnabled())
break;
// avoid sending key up events when we didn't send a down
if (!g_state.keyDown[sc])
break;
if (params.ignoreWindowsKeys &&
(sc == SDL_SCANCODE_LGUI || sc == SDL_SCANCODE_RGUI))
break;
uint32_t scancode = mapScancode(sc);
if (scancode == 0)
break;
if (spice_key_up(scancode))
g_state.keyDown[sc] = false;
else
{
DEBUG_ERROR("SDL_KEYUP: failed to send message");
break;
}
app_handleKeyRelease(sc);
break;
}
}