mirror of https://github.com/encounter/SDL.git
switch: add touchscreen support
This commit is contained in:
parent
97afa95d57
commit
bf248dd581
|
@ -0,0 +1,115 @@
|
||||||
|
/*
|
||||||
|
Simple DirectMedia Layer
|
||||||
|
Copyright (C) 1997-2017 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_switchtouch.h"
|
||||||
|
#include "../../events/SDL_touch_c.h"
|
||||||
|
|
||||||
|
#define MAX_TOUCH 16
|
||||||
|
|
||||||
|
typedef struct SwitchTouch
|
||||||
|
{
|
||||||
|
SDL_FingerID id;
|
||||||
|
touchPosition position;
|
||||||
|
} SwitchTouch;
|
||||||
|
|
||||||
|
typedef struct TouchState
|
||||||
|
{
|
||||||
|
SwitchTouch touch[MAX_TOUCH];
|
||||||
|
u32 count;
|
||||||
|
} TouchState;
|
||||||
|
|
||||||
|
TouchState touchState;
|
||||||
|
|
||||||
|
TouchState touchState_old;
|
||||||
|
|
||||||
|
void
|
||||||
|
SWITCH_InitTouch(void)
|
||||||
|
{
|
||||||
|
SDL_AddTouch((SDL_TouchID) 0, "Switch");
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
SWITCH_QuitTouch(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
SWITCH_PollTouch(void)
|
||||||
|
{
|
||||||
|
memcpy(&touchState_old, &touchState, sizeof(touchState));
|
||||||
|
|
||||||
|
touchState.count = hidTouchCount();
|
||||||
|
if (touchState.count >= MAX_TOUCH) {
|
||||||
|
touchState.count = MAX_TOUCH - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (touchState.count > 0) {
|
||||||
|
for (u32 i = 0; i < touchState.count; i++) {
|
||||||
|
|
||||||
|
touchState.touch[i].id = i;
|
||||||
|
hidTouchRead(&touchState.touch[i].position, i);
|
||||||
|
|
||||||
|
// Send an initial touch
|
||||||
|
SDL_SendTouch(0, (SDL_FingerID) i, SDL_TRUE,
|
||||||
|
(float) touchState.touch[i].position.px / 1280.0f,
|
||||||
|
(float) touchState.touch[i].position.py / 720.0f, 1);
|
||||||
|
|
||||||
|
// Always send the motion
|
||||||
|
SDL_SendTouchMotion(0, (SDL_FingerID) i,
|
||||||
|
(float) touchState.touch[i].position.px / 1280.0f,
|
||||||
|
(float) touchState.touch[i].position.py / 720.0f, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// some fingers might have been let go
|
||||||
|
if (touchState_old.count > 0) {
|
||||||
|
|
||||||
|
for (int i = 0; i < touchState_old.count; i++) {
|
||||||
|
|
||||||
|
int finger_up = 1;
|
||||||
|
if (touchState.count > 0) {
|
||||||
|
for (int j = 0; j < touchState.count; j++) {
|
||||||
|
if (touchState.touch[j].id == touchState_old.touch[i].id) {
|
||||||
|
finger_up = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (finger_up == 1) {
|
||||||
|
// Finger released from screen
|
||||||
|
SDL_SendTouch((SDL_TouchID) 0, (SDL_FingerID) touchState_old.touch[i].id, SDL_FALSE,
|
||||||
|
(float) touchState_old.touch[i].position.px / 1280.0f,
|
||||||
|
(float) touchState_old.touch[i].position.py / 720.0f, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* SDL_VIDEO_DRIVER_SWITCH */
|
||||||
|
|
||||||
|
/* vi: set ts=4 sw=4 expandtab: */
|
|
@ -0,0 +1,34 @@
|
||||||
|
/*
|
||||||
|
Simple DirectMedia Layer
|
||||||
|
Copyright (C) 1997-2017 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_switchtouch_h
|
||||||
|
#define _SDL_switchtouch_h
|
||||||
|
|
||||||
|
#include "../../SDL_internal.h"
|
||||||
|
|
||||||
|
/* Touch functions */
|
||||||
|
extern void SWITCH_InitTouch(void);
|
||||||
|
extern void SWITCH_QuitTouch(void);
|
||||||
|
extern void SWITCH_PollTouch(void);
|
||||||
|
|
||||||
|
#endif /* _SDL_switchtouch_h */
|
||||||
|
|
||||||
|
/* vi: set ts=4 sw=4 expandtab: */
|
|
@ -27,6 +27,7 @@
|
||||||
#include "../SDL_sysvideo.h"
|
#include "../SDL_sysvideo.h"
|
||||||
#include "../../events/SDL_keyboard_c.h"
|
#include "../../events/SDL_keyboard_c.h"
|
||||||
#include "../../events/SDL_windowevents_c.h"
|
#include "../../events/SDL_windowevents_c.h"
|
||||||
|
#include "SDL_switchtouch.h"
|
||||||
|
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
|
|
||||||
|
@ -117,11 +118,15 @@ static int SWITCH_VideoInit(_THIS)
|
||||||
mode.h = 0;
|
mode.h = 0;
|
||||||
SDL_AddDisplayMode(&_this->displays[0], &mode);
|
SDL_AddDisplayMode(&_this->displays[0], &mode);
|
||||||
|
|
||||||
|
// init touch
|
||||||
|
SWITCH_InitTouch();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SWITCH_VideoQuit(_THIS)
|
static void SWITCH_VideoQuit(_THIS)
|
||||||
{
|
{
|
||||||
|
SWITCH_QuitTouch();
|
||||||
gfxExit();
|
gfxExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,6 +148,7 @@ static void SWITCH_PumpEvents(_THIS)
|
||||||
}
|
}
|
||||||
|
|
||||||
hidScanInput();
|
hidScanInput();
|
||||||
|
SWITCH_PollTouch();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SWITCH_SetResolution(u32 width, u32 height)
|
static void SWITCH_SetResolution(u32 width, u32 height)
|
||||||
|
|
Loading…
Reference in New Issue