From 3a9451bbeb71eff57a201f3042ca7ef3ee487846 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Fri, 19 Jan 2018 13:58:32 -0500 Subject: [PATCH] Added CONTROLLER_P1_AUTO. Added bounds checking for id in hid{Set|Get}ControllerLayout. --- nx/include/switch/services/hid.h | 1 + nx/source/services/hid.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/nx/include/switch/services/hid.h b/nx/include/switch/services/hid.h index 69478171..e2e9660e 100644 --- a/nx/include/switch/services/hid.h +++ b/nx/include/switch/services/hid.h @@ -293,6 +293,7 @@ typedef enum CONTROLLER_PLAYER_8 = 7, CONTROLLER_HANDHELD = 8, CONTROLLER_UNKNOWN = 9, + CONTROLLER_P1_AUTO = 10, /// Not an actual HID-sysmodule ID. Only for hidKeys*(). Automatically uses CONTROLLER_PLAYER_1 when connected, otherwise uses CONTROLLER_HANDHELD. } HidControllerID; typedef struct touchPosition diff --git a/nx/source/services/hid.c b/nx/source/services/hid.c index 05678a51..1e4d80e0 100644 --- a/nx/source/services/hid.c +++ b/nx/source/services/hid.c @@ -18,6 +18,8 @@ static u64 g_controllerOld[10], g_controllerHeld[10], g_controllerDown[10], g_co static HidControllerLayoutType g_controllerLayout[10]; static u64 g_touchTimestamp, g_mouseTimestamp, g_keyboardTimestamp, g_controllerTimestamps[10]; +static HidControllerID g_controllerP1AutoID; + static RwLock g_hidLock; static Result _hidCreateAppletResource(Service* srv, Service* srv_out, u64 AppletResourceUserId); @@ -90,6 +92,8 @@ void hidReset(void) for (int i = 0; i < 10; i++) g_controllerTimestamps[i] = 0; + g_controllerP1AutoID = CONTROLLER_HANDHELD; + rwlockWriteUnlock(&g_hidLock); } @@ -102,12 +106,16 @@ void* hidGetSharedmemAddr(void) { } void hidSetControllerLayout(HidControllerID id, HidControllerLayoutType layoutType) { + if (id < 0 || id > 9) return; + rwlockWriteLock(&g_hidLock); g_controllerLayout[id] = layoutType; rwlockWriteUnlock(&g_hidLock); } HidControllerLayoutType hidGetControllerLayout(HidControllerID id) { + if (id < 0 || id > 9) return LAYOUT_DEFAULT; + rwlockReadLock(&g_hidLock); HidControllerLayoutType tmp = g_controllerLayout[id]; rwlockReadUnlock(&g_hidLock); @@ -188,10 +196,15 @@ void hidScanInput(void) { g_controllerUp[i] = g_controllerOld[i] & (~g_controllerHeld[i]); } + g_controllerP1AutoID = CONTROLLER_HANDHELD; + if (g_controllerEntries[CONTROLLER_PLAYER_1].connectionState & CONTROLLER_STATE_CONNECTED) + g_controllerP1AutoID = CONTROLLER_PLAYER_1; + rwlockWriteUnlock(&g_hidLock); } u64 hidKeysHeld(HidControllerID id) { + if (id==CONTROLLER_P1_AUTO) return hidKeysHeld(g_controllerP1AutoID); if (id < 0 || id > 9) return 0; rwlockReadLock(&g_hidLock); @@ -202,6 +215,7 @@ u64 hidKeysHeld(HidControllerID id) { } u64 hidKeysDown(HidControllerID id) { + if (id==CONTROLLER_P1_AUTO) return hidKeysDown(g_controllerP1AutoID); if (id < 0 || id > 9) return 0; rwlockReadLock(&g_hidLock); @@ -212,6 +226,7 @@ u64 hidKeysDown(HidControllerID id) { } u64 hidKeysUp(HidControllerID id) { + if (id==CONTROLLER_P1_AUTO) return hidKeysUp(g_controllerP1AutoID); if (id < 0 || id > 9) return 0; rwlockReadLock(&g_hidLock);