From 69747d1378d40e00d98439d2700fc67f7373bc82 Mon Sep 17 00:00:00 2001 From: Cpasjuste Date: Sun, 3 May 2020 17:07:59 +0200 Subject: [PATCH] switch: add software keyboard support (#49) --- src/joystick/switch/SDL_sysjoystick.c | 2 +- src/video/switch/SDL_switchswkb.c | 111 ++++++++++++++++++++++++++ src/video/switch/SDL_switchswkb.h | 20 +++++ src/video/switch/SDL_switchvideo.c | 28 +++++-- 4 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 src/video/switch/SDL_switchswkb.c create mode 100644 src/video/switch/SDL_switchswkb.h diff --git a/src/joystick/switch/SDL_sysjoystick.c b/src/joystick/switch/SDL_sysjoystick.c index c1a1199f1..446039bda 100644 --- a/src/joystick/switch/SDL_sysjoystick.c +++ b/src/joystick/switch/SDL_sysjoystick.c @@ -156,7 +156,7 @@ SWITCH_JoystickUpdate(SDL_Joystick *joystick) static JoystickState pad_old[JOYSTICK_COUNT]; int index = (int) SDL_JoystickInstanceID(joystick); - if (index > JOYSTICK_COUNT) { + if (index > JOYSTICK_COUNT || SDL_IsTextInputActive()) { return; } diff --git a/src/video/switch/SDL_switchswkb.c b/src/video/switch/SDL_switchswkb.c new file mode 100644 index 000000000..1a7a4b546 --- /dev/null +++ b/src/video/switch/SDL_switchswkb.c @@ -0,0 +1,111 @@ +// +// Created by cpasjuste on 22/04/2020. +// + +#include "../../SDL_internal.h" + +#if SDL_VIDEO_DRIVER_SWITCH + +#include +#include "SDL_switchswkb.h" + +static SwkbdInline kbd; +static SwkbdAppearArg kbdAppearArg; +static bool kbdInited = SDL_FALSE; +static bool kbdShown = SDL_FALSE; + +void +SWITCH_InitSwkb() +{ +} + +void +SWITCH_PollSwkb(void) +{ + if(kbdInited) { + if(kbdShown) { + swkbdInlineUpdate(&kbd, NULL); + } else if(SDL_IsTextInputActive()) { + SDL_StopTextInput(); + } + } +} + +void +SWITCH_QuitSwkb() +{ + if(kbdInited) { + swkbdInlineClose(&kbd); + kbdInited = false; + } +} + +SDL_bool +SWITCH_HasScreenKeyboardSupport(_THIS) +{ + return SDL_TRUE; +} + +SDL_bool +SWITCH_IsScreenKeyboardShown(_THIS, SDL_Window *window) +{ + return kbdShown; +} + +static void +SWITCH_EnterCb(const char *str, SwkbdDecidedEnterArg* arg) +{ + if(arg->stringLen > 0) { + SDL_SendKeyboardText(str); + } + + kbdShown = false; +} + +static void +SWITCH_CancelCb(void) +{ + SDL_StopTextInput(); +} + +void +SWITCH_StartTextInput(_THIS) +{ + Result rc; + + if(!kbdInited) { + rc = swkbdInlineCreate(&kbd); + if (R_SUCCEEDED(rc)) { + rc = swkbdInlineLaunchForLibraryApplet(&kbd, SwkbdInlineMode_AppletDisplay, 0); + if(R_SUCCEEDED(rc)) { + swkbdInlineSetDecidedEnterCallback(&kbd, SWITCH_EnterCb); + swkbdInlineSetDecidedCancelCallback(&kbd, SWITCH_CancelCb); + swkbdInlineMakeAppearArg(&kbdAppearArg, SwkbdType_Normal); + swkbdInlineAppearArgSetOkButtonText(&kbdAppearArg, "Submit"); + kbdAppearArg.dicFlag = 1; + kbdAppearArg.returnButtonFlag = 1; + kbdInited = true; + } + } + } + + if(kbdInited) { + swkbdInlineSetInputText(&kbd, ""); + swkbdInlineSetCursorPos(&kbd, 0); + swkbdInlineUpdate(&kbd, NULL); + swkbdInlineAppear(&kbd, &kbdAppearArg); + kbdShown = true; + } +} + +void +SWITCH_StopTextInput(_THIS) +{ + if(kbdInited) { + swkbdInlineDisappear(&kbd); + } + + kbdShown = false; +} + +#endif diff --git a/src/video/switch/SDL_switchswkb.h b/src/video/switch/SDL_switchswkb.h new file mode 100644 index 000000000..988728c08 --- /dev/null +++ b/src/video/switch/SDL_switchswkb.h @@ -0,0 +1,20 @@ +// +// Created by cpasjuste on 22/04/2020. +// + +#ifndef SDL2_SDL_SWITCHSWKB_H +#define SDL2_SDL_SWITCHSWKB_H + +#include "../../events/SDL_events_c.h" + +extern void SWITCH_InitSwkb(); +extern void SWITCH_PollSwkb(); +extern void SWITCH_QuitSwkb(); + +extern SDL_bool SWITCH_HasScreenKeyboardSupport(_THIS); +extern SDL_bool SWITCH_IsScreenKeyboardShown(_THIS, SDL_Window * window); + +extern void SWITCH_StartTextInput(_THIS); +extern void SWITCH_StopTextInput(_THIS); + +#endif //SDL2_SDL_SWITCHSWKB_H diff --git a/src/video/switch/SDL_switchvideo.c b/src/video/switch/SDL_switchvideo.c index a39bf1835..9590de207 100644 --- a/src/video/switch/SDL_switchvideo.c +++ b/src/video/switch/SDL_switchvideo.c @@ -34,6 +34,7 @@ #include "SDL_switchtouch.h" #include "SDL_switchkeyboard.h" #include "SDL_switchmouse_c.h" +#include "SDL_switchswkb.h" /* Currently only one window */ static SDL_Window *switch_window = NULL; @@ -105,6 +106,11 @@ SWITCH_CreateDevice(int devindex) device->GL_DeleteContext = SWITCH_GLES_DeleteContext; device->GL_DefaultProfileConfig = SWITCH_GLES_DefaultProfileConfig; + device->StartTextInput = SWITCH_StartTextInput; + device->StopTextInput = SWITCH_StopTextInput; + device->HasScreenKeyboardSupport = SWITCH_HasScreenKeyboardSupport; + device->IsScreenKeyboardShown = SWITCH_IsScreenKeyboardShown; + device->PumpEvents = SWITCH_PumpEvents; return device; @@ -141,10 +147,12 @@ SWITCH_VideoInit(_THIS) // init touch SWITCH_InitTouch(); - //init keyboard + // init keyboard SWITCH_InitKeyboard(); - //init mouse + // init mouse SWITCH_InitMouse(); + // init software keyboard + SWITCH_InitSwkb(); return 0; } @@ -158,10 +166,12 @@ SWITCH_VideoQuit(_THIS) // exit touch SWITCH_QuitTouch(); - //exit keyboard + // exit keyboard SWITCH_QuitKeyboard(); - //exit mouse + // exit mouse SWITCH_QuitMouse(); + // exit software keyboard + SWITCH_QuitSwkb(); } void @@ -343,9 +353,13 @@ SWITCH_PumpEvents(_THIS) } hidScanInput(); - SWITCH_PollTouch(); - SWITCH_PollKeyboard(); - SWITCH_PollMouse(); + // we don't want other inputs overlapping with software keyboard + if(!SDL_IsTextInputActive()) { + SWITCH_PollTouch(); + SWITCH_PollKeyboard(); + SWITCH_PollMouse(); + } + SWITCH_PollSwkb(); // handle docked / un-docked modes // note that SDL_WINDOW_RESIZABLE is only possible in windowed mode,