foxygit / fake-08-xbox360 Log in
commit 10da36b30df0a14e5872df4e506ac2d0c371cd54
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Tue Apr 13 18:27:41 2021 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Tue Apr 13 18:27:41 2021 -0600

    Fix input for switch

    SDL2 input wasn't triggering keyup correctly so swap back to native
    switch input. Also fix p8 cart dir
---
 platform/switch/source/SwitchHost.cpp | 130 ++++++++++++++++------------------
 1 file changed, 61 insertions(+), 69 deletions(-)

diff --git a/platform/switch/source/SwitchHost.cpp b/platform/switch/source/SwitchHost.cpp
index 863bc3f..b133239 100644
--- a/platform/switch/source/SwitchHost.cpp
+++ b/platform/switch/source/SwitchHost.cpp
@@ -22,35 +22,56 @@ using namespace std;
 #define WINDOW_SIZE_X 1280
 #define WINDOW_SIZE_Y 720

-#define JOY_A     0
-#define JOY_B     1
-#define JOY_X     2
-#define JOY_Y     3
-#define LSTICK    4
-#define RSTICK    5
-#define JOY_L     6
-#define JOY_R     7
-#define JOY_ZL    8
-#define JOY_ZR    9
-#define JOY_PLUS  10
-#define JOY_MINUS 11
-#define JOY_LEFT  12
-#define JOY_UP    13
-#define JOY_RIGHT 14
-#define JOY_DOWN  15
-
 #define WINDOW_FLAGS SDL_WINDOW_SHOWN

 #define RENDERER_FLAGS SDL_RENDERER_ACCELERATED
 #define PIXEL_FORMAT SDL_PIXELFORMAT_ARGB8888

-SDL_Event event;
-

 string _desktopSdl2SettingsDir = "switch/fake08";
 string _desktopSdl2SettingsPrefix = "switch/fake08/";
-string _desktopSdl2customBiosLua = "cartpath = \"sdmc:/switch/p8carts/\"\n";
+string _desktopSdl2customBiosLua = "cartpath = \"sdmc:/p8carts/\"\n";
+
+PadState pad;
+u64 currKDown_64;
+u64 currKHeld_64;
+
+uint8_t ConvertInputToP8(u64 input){
+	uint8_t result = 0;
+	if (input & HidNpadButton_Left){
+		result |= P8_KEY_LEFT;
+	}
+
+	if (input & HidNpadButton_Right){
+		result |= P8_KEY_RIGHT;
+	}
+
+	if (input & HidNpadButton_Up){
+		result |= P8_KEY_UP;
+	}
+
+	if (input & HidNpadButton_Down){
+		result |= P8_KEY_DOWN;
+	}
+
+	if (input & HidNpadButton_B){
+		result |= P8_KEY_O;
+	}
+
+	if (input & HidNpadButton_A){
+		result |= P8_KEY_X;
+	}
+
+	if (input & HidNpadButton_Plus){
+		result |= P8_KEY_PAUSE;
+	}

+	if (input & HidNpadButton_Minus){
+		result |= P8_KEY_7;
+	}
+
+	return result;
+}

 Host::Host()
 {
@@ -70,6 +91,13 @@ Host::Host()
         res = mkdir(cartdatadir.c_str(), 0777);
     }

+    // Configure our supported input layout: a single player with standard controller styles
+    padConfigureInput(1, HidNpadStyleSet_NpadStandard);
+
+    // Initialize the default gamepad (which reads handheld mode inputs as well as the first connected controller)
+
+    padInitializeDefault(&pad);
+
     setPlatformParams(
         WINDOW_SIZE_X,
         WINDOW_SIZE_Y,
@@ -83,60 +111,24 @@ Host::Host()


 InputState_t Host::scanInput(){
-    currKDown = 0;
-    uint8_t kUp = 0;
-    stretchKeyPressed = false;
-
-    while (SDL_PollEvent(&event)) {
-        switch (event.type) {
-            case SDL_JOYBUTTONDOWN :
-                switch (event.jbutton.button)
-                {
-                    case JOY_PLUS:  currKDown |= P8_KEY_PAUSE; break;
-                    case JOY_LEFT:  currKDown |= P8_KEY_LEFT; break;
-                    case JOY_RIGHT: currKDown |= P8_KEY_RIGHT; break;
-                    case JOY_UP:    currKDown |= P8_KEY_UP; break;
-                    case JOY_DOWN:  currKDown |= P8_KEY_DOWN; break;
-                    case JOY_A:     currKDown |= P8_KEY_X; break;
-                    case JOY_B:     currKDown |= P8_KEY_O; break;
-
-                    case JOY_L: lDown = true; break;
-                    case JOY_R: rDown = true; stretchKeyPressed = true; break;
-                }
-                break;
-
-            case SDL_JOYBUTTONUP :
-                switch (event.jbutton.button)
-                {
-                    case JOY_PLUS:  kUp |= P8_KEY_PAUSE; break;
-                    case JOY_LEFT:  kUp |= P8_KEY_LEFT; break;
-                    case JOY_RIGHT: kUp |= P8_KEY_RIGHT; break;
-                    case JOY_UP:    kUp |= P8_KEY_UP; break;
-                    case JOY_DOWN:  kUp |= P8_KEY_DOWN; break;
-                    case JOY_A:     kUp |= P8_KEY_X; break;
-                    case JOY_B:     kUp |= P8_KEY_O; break;
-
-                    case JOY_L: lDown = false; break;
-                    case JOY_R: rDown = false; break;
-                }
-               break;
-
-            case SDL_QUIT:
-                quit = 1;
-                break;
-        }
-    }
+    //SDL input doesn't seem to work correctly on the switch, so I've left the switch specific one
+    padUpdate(&pad);
+
+    currKDown_64 = padGetButtonsDown(&pad);
+    currKHeld_64 = padGetButtons(&pad);
+
+    lDown = currKHeld_64 & HidNpadButton_L;
+	rDown = currKDown_64 & HidNpadButton_R;

     if (lDown && rDown){
         quit = 1;
     }

-    currKHeld |= currKDown;
-    currKHeld ^= kUp;
+    stretchKeyPressed = rDown;

     return InputState_t {
-        currKDown,
-        currKHeld
+        ConvertInputToP8(currKDown_64),
+        ConvertInputToP8(currKHeld_64)
     };
 }

@@ -144,9 +136,9 @@ InputState_t Host::scanInput(){
 vector<string> Host::listcarts(){
     vector<string> carts;

-    DIR* dir = opendir("switch/p8carts");
+    DIR* dir = opendir("/p8carts");
     struct dirent *ent;
-    std::string fullCartDir = "switch/p8carts/";
+    std::string fullCartDir = "/p8carts/";

     if (dir) {
         /* print all the files and directories within directory */