From 8378cba5eeb77d6c53fce940192c9c00772cfe80 Mon Sep 17 00:00:00 2001 From: Paper Date: Sat, 13 Apr 2024 12:15:41 -0400 Subject: [PATCH] ogc: add USB keyboard support (#68) --- CMakeLists.txt | 2 +- src/main/wii/SDL_wii_main.c | 2 +- src/video/ogc/SDL_ogcevents.c | 2 ++ src/video/ogc/SDL_ogckeyboard.c | 59 +++++++++++++++++++++++++++++++++ src/video/ogc/SDL_ogckeyboard.h | 34 +++++++++++++++++++ 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/video/ogc/SDL_ogckeyboard.c create mode 100644 src/video/ogc/SDL_ogckeyboard.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7dc928208c77d..56629f588c4e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2944,7 +2944,7 @@ elseif(OGC) endif() list(APPEND EXTRA_LDFLAGS "${OGC_ARCH_SETTINGS} ${OGC_LINKER_FLAGS}") if(NINTENDO_WII) - list(APPEND EXTRA_LIBS "wiiuse;bte") + list(APPEND EXTRA_LIBS "wiiuse;bte;wiikeyboard") endif() list(APPEND EXTRA_LIBS "ogc;m") endif() diff --git a/src/main/wii/SDL_wii_main.c b/src/main/wii/SDL_wii_main.c index 9a42125aae401..47b8b207fe22e 100644 --- a/src/main/wii/SDL_wii_main.c +++ b/src/main/wii/SDL_wii_main.c @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) WPAD_SetVRes(WPAD_CHAN_ALL, 640, 480); MOUSE_Init(); - // TODO KEYBOARD_Init(NULL); + KEYBOARD_Init(NULL); fatInitDefault(); /* Call the user's main function. Make sure that argv contains at least one diff --git a/src/video/ogc/SDL_ogcevents.c b/src/video/ogc/SDL_ogcevents.c index a3ebc0d3d6e1a..6fb8ac4557e34 100644 --- a/src/video/ogc/SDL_ogcevents.c +++ b/src/video/ogc/SDL_ogcevents.c @@ -27,6 +27,7 @@ #include "../../events/SDL_events_c.h" #include "SDL_ogcevents_c.h" +#include "SDL_ogckeyboard.h" #include "SDL_ogcmouse.h" #include "SDL_ogcvideo.h" @@ -99,6 +100,7 @@ void OGC_PumpEvents(_THIS) #ifdef __wii__ pump_ir_events(_this); + OGC_PumpKeyboardEvents(_this); #endif } diff --git a/src/video/ogc/SDL_ogckeyboard.c b/src/video/ogc/SDL_ogckeyboard.c new file mode 100644 index 0000000000000..1ebf0f8fc56d4 --- /dev/null +++ b/src/video/ogc/SDL_ogckeyboard.c @@ -0,0 +1,59 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2023 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_ogckeyboard.h" +#include "../../events/SDL_keyboard_c.h" + +#if defined(SDL_VIDEO_DRIVER_OGC) && defined(__wii__) +#include +#include + +void OGC_PumpKeyboardEvents(_THIS) { + keyboard_event ke; + + s32 res = KEYBOARD_GetEvent(&ke); + if (res && (ke.type == KEYBOARD_RELEASED || ke.type == KEYBOARD_PRESSED)) { + SDL_SendKeyboardKey((ke.type == KEYBOARD_PRESSED) ? SDL_PRESSED : SDL_RELEASED, (SDL_Scancode)ke.keycode); + + if (ke.type == KEYBOARD_PRESSED) { + const Uint16 symbol = ke.symbol; + char utf8[4] = {'\0'}; + + /* ignore private symbols, used by wiikeyboard for special keys */ + if ((symbol >= 0xE000 && symbol <= 0xF8FF) || symbol == 0xFFFF) + return; + + /* convert UCS-2 to UTF-8 */ + if (symbol < 0x80) { + utf8[0] = symbol; + } else if (symbol < 0x800) { + utf8[0] = 0xC0 | (symbol >> 6); + utf8[1] = 0x80 | (symbol & 0x3F); + } else { + utf8[0] = 0xE0 | (symbol >> 12); + utf8[1] = 0x80 | ((symbol >> 6) & 0x3F); + utf8[2] = 0x80 | (symbol & 0x3F); + } + + SDL_SendKeyboardText(utf8); + } + } +} +#endif diff --git a/src/video/ogc/SDL_ogckeyboard.h b/src/video/ogc/SDL_ogckeyboard.h new file mode 100644 index 0000000000000..ca8c0464dfb65 --- /dev/null +++ b/src/video/ogc/SDL_ogckeyboard.h @@ -0,0 +1,34 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2023 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL_OGC_keyboard_h_ +#define SDL_OGC_keyboard_h_ + +#include "../../SDL_internal.h" +#include "SDL_ogcvideo.h" + +#ifdef __wii__ +void OGC_PumpKeyboardEvents(_THIS); +#endif + +#endif /* SDL_OGC_keyboard_h_ */ + +/* vi: set ts=4 sw=4 expandtab: */