switch: update touch interface to new hid api

This commit is contained in:
cpasjuste 2021-06-24 13:31:47 +02:00
parent 75258715b8
commit 177b651fe6
1 changed files with 28 additions and 58 deletions

View File

@ -29,89 +29,59 @@
#include "../../events/SDL_touch_c.h" #include "../../events/SDL_touch_c.h"
#include "../../video/SDL_sysvideo.h" #include "../../video/SDL_sysvideo.h"
#define MAX_TOUCH 16 HidTouchScreenState touchState;
HidTouchScreenState touchStateOld;
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 void
SWITCH_InitTouch(void) SWITCH_InitTouch(void) {
{ hidInitializeTouchScreen();
SDL_AddTouch((SDL_TouchID) 0, SDL_TOUCH_DEVICE_DIRECT, "Switch"); SDL_AddTouch((SDL_TouchID) 0, SDL_TOUCH_DEVICE_DIRECT, "Switch");
SDL_SetHintWithPriority(SDL_HINT_TOUCH_MOUSE_EVENTS, "0", SDL_HINT_DEFAULT); SDL_SetHintWithPriority(SDL_HINT_TOUCH_MOUSE_EVENTS, "0", SDL_HINT_DEFAULT);
} }
void void
SWITCH_QuitTouch(void) SWITCH_QuitTouch(void) {
{
} }
void void
SWITCH_PollTouch(void) SWITCH_PollTouch(void) {
{
SDL_Window *window = SDL_GetFocusWindow(); SDL_Window *window = SDL_GetFocusWindow();
if (window == NULL) { if (window == NULL) {
return; return;
} }
memcpy(&touchState_old, &touchState, sizeof(touchState)); memcpy(&touchStateOld, &touchState, sizeof(touchState));
touchState.count = hidTouchCount(); if (hidGetTouchScreenStates(&touchState, 1)) {
if (touchState.count >= MAX_TOUCH) {
touchState.count = MAX_TOUCH - 1;
}
if (touchState.count > 0) {
for (u32 i = 0; i < touchState.count; i++) { for (u32 i = 0; i < touchState.count; i++) {
touchState.touch[i].id = i;
hidTouchRead(&touchState.touch[i].position, i);
// Send an initial touch // Send an initial touch
SDL_SendTouch(0, (SDL_FingerID) i, window, SDL_TRUE, SDL_SendTouch(0, touchState.touches[i].finger_id, window, SDL_TRUE,
(float) touchState.touch[i].position.px / 1280.0f, (float) touchState.touches[i].x / 1280.0f,
(float) touchState.touch[i].position.py / 720.0f, 1); (float) touchState.touches[i].y / 720.0f, 1);
// Always send the motion // Always send the motion
SDL_SendTouchMotion(0, (SDL_FingerID) i, window, SDL_SendTouchMotion(0, (SDL_FingerID) i, window,
(float) touchState.touch[i].position.px / 1280.0f, (float) touchState.touches[i].x / 1280.0f,
(float) touchState.touch[i].position.py / 720.0f, 1); (float) touchState.touches[i].y / 720.0f, 1);
}
} }
// some fingers might have been let go // some fingers might have been let go
if (touchState_old.count > 0) { if (touchStateOld.count > 0) {
for (int i = 0; i < touchStateOld.count; i++) {
for (int i = 0; i < touchState_old.count; i++) {
int finger_up = 1; int finger_up = 1;
if (touchState.count > 0) { if (touchState.count > 0) {
for (int j = 0; j < touchState.count; j++) { for (int j = 0; j < touchState.count; j++) {
if (touchState.touch[j].id == touchState_old.touch[i].id) { if (touchState.touches[j].finger_id == touchStateOld.touches[i].finger_id) {
finger_up = 0; finger_up = 0;
} }
} }
} }
if (finger_up == 1) { if (finger_up == 1) {
// Finger released from screen // Finger released from screen
SDL_SendTouch((SDL_TouchID) 0, (SDL_FingerID) touchState_old.touch[i].id, window, SDL_FALSE, SDL_SendTouch((SDL_TouchID) 0, (SDL_FingerID) touchStateOld.touches[i].finger_id, window, SDL_FALSE,
(float) touchState_old.touch[i].position.px / 1280.0f, (float) touchStateOld.touches[i].x / 1280.0f,
(float) touchState_old.touch[i].position.py / 720.0f, 1); (float) touchStateOld.touches[i].y / 720.0f, 1);
}
} }
} }
} }