Skip to content

Commit

Permalink
ogc: add USB keyboard support (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpapersonic authored Apr 13, 2024
1 parent 4f26b2b commit 8378cba
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/main/wii/SDL_wii_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/video/ogc/SDL_ogcevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -99,6 +100,7 @@ void OGC_PumpEvents(_THIS)

#ifdef __wii__
pump_ir_events(_this);
OGC_PumpKeyboardEvents(_this);
#endif
}

Expand Down
59 changes: 59 additions & 0 deletions src/video/ogc/SDL_ogckeyboard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
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 <gctypes.h>
#include <wiikeyboard/keyboard.h>

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
34 changes: 34 additions & 0 deletions src/video/ogc/SDL_ogckeyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
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: */

0 comments on commit 8378cba

Please sign in to comment.