Skip to content

Commit

Permalink
Switch: Add support for physical keyboard and mouse (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsn8887 authored and WinterMute committed Jun 18, 2021
1 parent e2adbf5 commit c30ec89
Show file tree
Hide file tree
Showing 5 changed files with 455 additions and 0 deletions.
270 changes: 270 additions & 0 deletions src/video/switch/SDL_switchkeyboard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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_internal.h"

#if SDL_VIDEO_DRIVER_SWITCH

#include <switch.h>

#include "SDL_events.h"
#include "SDL_log.h"
#include "SDL_switchvideo.h"
#include "SDL_switchkeyboard.h"
#include "../../events/SDL_keyboard_c.h"

#define NUM_SCANCODES_SWITCH 160

static uint8_t locks = 0;
static bool keystate[NUM_SCANCODES_SWITCH] = { 0 };
static const uint8_t switch_scancodes[NUM_SCANCODES_SWITCH] = {
KBD_A,
KBD_B,
KBD_C,
KBD_D,
KBD_E,
KBD_F,
KBD_G,
KBD_H,
KBD_I,
KBD_J,
KBD_K,
KBD_L,
KBD_M,
KBD_N,
KBD_O,
KBD_P,
KBD_Q,
KBD_R,
KBD_S,
KBD_T,
KBD_U,
KBD_V,
KBD_W,
KBD_X,
KBD_Y,
KBD_Z,
KBD_1,
KBD_2,
KBD_3,
KBD_4,
KBD_5,
KBD_6,
KBD_7,
KBD_8,
KBD_9,
KBD_0,
KBD_ENTER,
KBD_ESC,
KBD_BACKSPACE,
KBD_TAB,
KBD_S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IGHTCTRL,
KBD_RIGHTSHIFT,
KBD_RIGHTALT,
KBD_RIGHTMETA,
KBD_MEDIA_PLAYPAUSE,
KBD_MEDIA_STOPCD,
KBD_MEDIA_PREVIOUSSONG,
KBD_MEDIA_NEXTSONG,
KBD_MEDIA_EJECTCD,
KBD_MEDIA_VOLUMEUP,
KBD_MEDIA_VOLUMEDOWN,
KBD_MEDIA_MUTE,
KBD_MEDIA_WWW,
KBD_MEDIA_BACK,
KBD_MEDIA_FORWARD,
KBD_MEDIA_STOP,
KBD_MEDIA_FIND,
KBD_MEDIA_SCROLLUP,
KBD_MEDIA_SCROLLDOWN,
KBD_MEDIA_EDIT,
KBD_MEDIA_SLEEP,
KBD_MEDIA_COFFEE,
KBD_MEDIA_REFRESH,
KBD_MEDIA_CALC
};

void
SWITCH_InitKeyboard(void)
{
}

void
SWITCH_PollKeyboard(void)
{
// We skip polling keyboard if no window is created
if (SDL_GetFocusWindow() == NULL)
return;

for (int i = 0; i < NUM_SCANCODES_SWITCH; i++) {

int keyCode = switch_scancodes[i];

if (hidKeyboardHeld(keyCode) && !keystate[i]) {
switch (keyCode) {
case SDL_SCANCODE_NUMLOCKCLEAR:
if (!(locks & 0x1)) {
SDL_SendKeyboardKey(SDL_PRESSED, keyCode);
locks |= 0x1;
} else {
SDL_SendKeyboardKey(SDL_RELEASED, keyCode);
locks &= ~0x1;
}
break;
case SDL_SCANCODE_CAPSLOCK:
if (!(locks & 0x2)) {
SDL_SendKeyboardKey(SDL_PRESSED, keyCode);
locks |= 0x2;
} else {
SDL_SendKeyboardKey(SDL_RELEASED, keyCode);
locks &= ~0x2;
}
break;
case SDL_SCANCODE_SCROLLLOCK:
if (!(locks & 0x4)) {
SDL_SendKeyboardKey(SDL_PRESSED, keyCode);
locks |= 0x4;
} else {
SDL_SendKeyboardKey(SDL_RELEASED, keyCode);
locks &= ~0x4;
}
break;
default:
SDL_SendKeyboardKey(SDL_PRESSED, keyCode);
}
keystate[i] = true;
} else if (!hidKeyboardHeld(keyCode) && keystate[i]) {
switch (keyCode) {
case SDL_SCANCODE_CAPSLOCK:
case SDL_SCANCODE_NUMLOCKCLEAR:
case SDL_SCANCODE_SCROLLLOCK:
break;
default:
SDL_SendKeyboardKey(SDL_RELEASED, keyCode);
}
keystate[i] = false;
}
}
}

void
SWITCH_QuitKeyboard(void)
{
}

#endif /* SDL_VIDEO_DRIVER_SWITCH */

/* vi: set ts=4 sw=4 expandtab: */
34 changes: 34 additions & 0 deletions src/video/switch/SDL_switchkeyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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_switchkeyboard_h
#define _SDL_switchkeyboard_h

#include "../../SDL_internal.h"

/* Keyboard functions */
extern void SWITCH_InitKeyboard(void);
extern void SWITCH_PollKeyboard(void);
extern void SWITCH_QuitKeyboard(void);

#endif /* _SDL_switchkeyboard_h */

/* vi: set ts=4 sw=4 expandtab: */
Loading

0 comments on commit c30ec89

Please sign in to comment.