SDL: Fix joystick support

From 73b545ed28baf057d15078f7c00a652b83d728a7 Mon Sep 17 00:00:00 2001
From: Ivan Epifanov <[EMAIL REDACTED]>
Date: Mon, 2 Nov 2020 19:38:20 +0300
Subject: [PATCH] Fix joystick support

---
 src/joystick/SDL_joystick.c         |   3 +
 src/joystick/SDL_sysjoystick.h      |   1 +
 src/joystick/vita/SDL_sysjoystick.c | 105 +++++++++++++++-------------
 3 files changed, 60 insertions(+), 49 deletions(-)

diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 5a84b9508..60c9c047a 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -95,6 +95,9 @@ static SDL_JoystickDriver *SDL_joystick_drivers[] = {
 #ifdef SDL_JOYSTICK_VIRTUAL
     &SDL_VIRTUAL_JoystickDriver,
 #endif
+#ifdef SDL_JOYSTICK_VITA
+    &SDL_VITA_JoystickDriver
+#endif
 #if defined(SDL_JOYSTICK_DUMMY) || defined(SDL_JOYSTICK_DISABLED)
     &SDL_DUMMY_JoystickDriver
 #endif
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index 1ba6775f5..b8af118df 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -206,6 +206,7 @@ extern SDL_JoystickDriver SDL_WINDOWS_JoystickDriver;
 extern SDL_JoystickDriver SDL_WINMM_JoystickDriver;
 extern SDL_JoystickDriver SDL_OS2_JoystickDriver;
 extern SDL_JoystickDriver SDL_PSP_JoystickDriver;
+extern SDL_JoystickDriver SDL_VITA_JoystickDriver;
 
 #endif /* SDL_sysjoystick_h_ */
 
diff --git a/src/joystick/vita/SDL_sysjoystick.c b/src/joystick/vita/SDL_sysjoystick.c
index 151e3f86c..2ce10907b 100644
--- a/src/joystick/vita/SDL_sysjoystick.c
+++ b/src/joystick/vita/SDL_sysjoystick.c
@@ -90,7 +90,7 @@ static int calc_bezier_y(float t)
  * Joystick 0 should be the system default joystick.
  * It should return number of joysticks, or -1 on an unrecoverable fatal error.
  */
-int SDL_SYS_JoystickInit(void)
+int VITA_JoystickInit(void)
 {
     int i;
 
@@ -129,61 +129,58 @@ int SDL_SYS_JoystickInit(void)
   return SDL_numjoysticks;
 }
 
-int SDL_SYS_NumJoysticks()
+int VITA_JoystickGetCount()
 {
     return SDL_numjoysticks;
 }
 
-void SDL_SYS_JoystickDetect()
+void VITA_JoystickDetect()
 {
 }
 
-/* Function to get the device-dependent name of a joystick */
-const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
-{
-   if (device_index == 1)
-		return "PSVita Controller";
-
-	if (device_index == 2)
-		return "PSVita Controller";
-
-	if (device_index == 3)
-		return "PSVita Controller";
-
-   return "PSVita Controller";
-}
-
 /* Function to perform the mapping from device index to the instance id for this index */
-SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
+SDL_JoystickID VITA_JoystickGetDeviceInstanceID(int device_index)
 {
     return device_index;
 }
 
 /* Function to get the device-dependent name of a joystick */
-const char *SDL_SYS_JoystickName(int index)
+const char *VITA_JoystickGetDeviceName(int index)
 {
-	if (index == 0)
-   	return "PSVita Controller";
+    if (index == 0)
+        return "PSVita Controller";
 
-	if (index == 1)
-   	return "PSVita Controller";
+    if (index == 1)
+        return "PSVita Controller";
 
-	if (index == 2)
-   	return "PSVita Controller";
+    if (index == 2)
+        return "PSVita Controller";
 
-	if (index == 3)
-   	return "PSVita Controller";
+    if (index == 3)
+        return "PSVita Controller";
 
     SDL_SetError("No joystick available with that index");
     return(NULL);
 }
 
+static int
+VITA_JoystickGetDevicePlayerIndex(int device_index)
+{
+    return -1;
+}
+
+static void
+VITA_JoystickSetDevicePlayerIndex(int device_index, int player_index)
+{
+}
+
+
 /* Function to open a joystick for use.
    The joystick to open is specified by the device index.
    This should fill the nbuttons and naxes fields of the joystick structure.
    It returns 0, or -1 if there is an error.
  */
-int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
+int VITA_JoystickOpen(SDL_Joystick *joystick, int device_index)
 {
     joystick->nbuttons = sizeof(button_map)/sizeof(*button_map);
     joystick->naxes = 4;
@@ -193,18 +190,12 @@ int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
     return 0;
 }
 
-/* Function to determine if this joystick is attached to the system right now */
-SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
-{
-    return SDL_TRUE;
-}
-
 /* Function to update the state of a joystick - called as a device poll.
  * This function shouldn't update the joystick structure directly,
  * but instead should call SDL_PrivateJoystick*() to deliver events
  * and update joystick device state.
  */
-void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
+void VITA_JoystickUpdate(SDL_Joystick *joystick)
 {
     int i;
     unsigned int buttons;
@@ -217,11 +208,11 @@ void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
     static unsigned char old_ry[] = { 0, 0, 0, 0 };
     SceCtrlData *pad = NULL;
 
-	  int index = (int) SDL_JoystickInstanceID(joystick);
+    int index = (int) SDL_JoystickInstanceID(joystick);
 
     if (index == 0) pad = &pad0;
     else if (index == 1) pad = &pad1;
-	  else if (index == 2) pad = &pad2;
+    else if (index == 2) pad = &pad2;
     else if (index == 3) pad = &pad3;
     else return;
 
@@ -275,35 +266,51 @@ void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
 }
 
 /* Function to close a joystick after use */
-void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
+void VITA_JoystickClose(SDL_Joystick *joystick)
 {
 }
 
 /* Function to perform any system-specific joystick related cleanup */
-void SDL_SYS_JoystickQuit(void)
+void VITA_JoystickQuit(void)
 {
 }
 
-SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
+SDL_JoystickGUID VITA_JoystickGetDeviceGUID( int device_index )
 {
     SDL_JoystickGUID guid;
     /* the GUID is just the first 16 chars of the name for now */
-    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
+    const char *name = VITA_JoystickGetDeviceName( device_index );
     SDL_zero( guid );
     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
     return guid;
 }
 
-SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
+static int
+VITA_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
 {
-    SDL_JoystickGUID guid;
-    /* the GUID is just the first 16 chars of the name for now */
-    const char *name = joystick->name;
-    SDL_zero( guid );
-    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
-    return guid;
+    return SDL_Unsupported();
 }
 
+
+SDL_JoystickDriver SDL_VITA_JoystickDriver =
+{
+    VITA_JoystickInit,
+    VITA_JoystickGetCount,
+    VITA_JoystickDetect,
+    VITA_JoystickGetDeviceName,
+    VITA_JoystickGetDevicePlayerIndex,
+    VITA_JoystickSetDevicePlayerIndex,
+    VITA_JoystickGetDeviceGUID,
+    VITA_JoystickGetDeviceInstanceID,
+
+    VITA_JoystickOpen,
+    VITA_JoystickRumble,
+    VITA_JoystickUpdate,
+    VITA_JoystickClose,
+    VITA_JoystickQuit,
+};
+
+
 #endif /* SDL_JOYSTICK_VITA */
 
 /* vim: ts=4 sw=4