Switch: Add support for physical keyboard and mouse (#33)

This commit is contained in:
rsn8887 2019-02-10 15:45:23 -06:00 committed by Dave Murphy
parent e2adbf54e6
commit c30ec89b7a
5 changed files with 455 additions and 0 deletions

View File

@ -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_SPACE,
KBD_MINUS,
KBD_EQUAL,
KBD_LEFTBRACE,
KBD_RIGHTBRACE,
KBD_BACKSLASH,
KBD_HASHTILDE,
KBD_SEMICO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
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: */

View File

@ -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: */

View File

@ -0,0 +1,105 @@
/*
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_timer.h"
#include "SDL_events.h"
#include "SDL_log.h"
#include "SDL_mouse.h"
#include "SDL_switchvideo.h"
#include "SDL_switchmouse_c.h"
#include "../../events/SDL_mouse_c.h"
static uint64_t prev_buttons = 0;
static uint64_t last_timestamp = 0;
const uint64_t mouse_read_interval = 15; // in ms
void
SWITCH_InitMouse(void)
{
}
void
SWITCH_PollMouse(void)
{
SDL_Window *window = SDL_GetFocusWindow();
uint64_t buttons;
uint64_t changed_buttons;
MousePosition mouse_pos;
uint64_t timestamp;
int dx, dy;
// We skip polling mouse if no window is created
if (window == NULL)
return;
buttons = hidMouseButtonsHeld();
changed_buttons = buttons ^ prev_buttons;
if (changed_buttons & MOUSE_LEFT) {
if (prev_buttons & MOUSE_LEFT)
SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_LEFT);
else
SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_LEFT);
}
if (changed_buttons & MOUSE_RIGHT) {
if (prev_buttons & MOUSE_RIGHT)
SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_RIGHT);
else
SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_RIGHT);
}
if (changed_buttons & MOUSE_MIDDLE) {
if (prev_buttons & MOUSE_MIDDLE)
SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_MIDDLE);
else
SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_MIDDLE);
}
prev_buttons = buttons;
timestamp = SDL_GetTicks();
if (SDL_TICKS_PASSED(timestamp, last_timestamp + mouse_read_interval)) {
hidMouseRead(&mouse_pos);
// if hidMouseRead is called once per frame, a factor two on the velocities
// results in approximately the same mouse motion as reported by mouse_pos.x and mouse_pos.y
// but without the clamping to 1280 x 720
dx = mouse_pos.velocityX * 2;
dy = mouse_pos.velocityY * 2;
if (dx || dy) {
SDL_SendMouseMotion(window, 0, 1, dx, dy);
}
last_timestamp = timestamp;
}
}
void
SWITCH_QuitMouse(void)
{
}
#endif /* SDL_VIDEO_DRIVER_SWITCH */
/* vi: set ts=4 sw=4 expandtab: */

View File

@ -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_switchmouse_h
#define _SDL_switchmouse_h
#include "../../SDL_internal.h"
/* mouse functions */
extern void SWITCH_InitMouse(void);
extern void SWITCH_PollMouse(void);
extern void SWITCH_QuitMouse(void);
#endif /* _SDL_switchmouse_h */
/* vi: set ts=4 sw=4 expandtab: */

View File

@ -32,6 +32,8 @@
#include "SDL_switchvideo.h" #include "SDL_switchvideo.h"
#include "SDL_switchopengles.h" #include "SDL_switchopengles.h"
#include "SDL_switchtouch.h" #include "SDL_switchtouch.h"
#include "SDL_switchkeyboard.h"
#include "SDL_switchmouse_c.h"
static int static int
SWITCH_Available(void) SWITCH_Available(void)
@ -157,6 +159,10 @@ SWITCH_VideoInit(_THIS)
// init touch // init touch
SWITCH_InitTouch(); SWITCH_InitTouch();
//init keyboard
SWITCH_InitKeyboard();
//init mouse
SWITCH_InitMouse();
return 0; return 0;
} }
@ -172,6 +178,10 @@ SWITCH_VideoQuit(_THIS)
// exit touch // exit touch
SWITCH_QuitTouch(); SWITCH_QuitTouch();
//exit keyboard
SWITCH_QuitKeyboard();
//exit mouse
SWITCH_QuitMouse();
} }
void void
@ -361,6 +371,8 @@ SWITCH_PumpEvents(_THIS)
hidScanInput(); hidScanInput();
SWITCH_PollTouch(); SWITCH_PollTouch();
SWITCH_PollKeyboard();
SWITCH_PollMouse();
} }
#endif /* SDL_VIDEO_DRIVER_SWITCH */ #endif /* SDL_VIDEO_DRIVER_SWITCH */