Completed adding new hotplug stubs for the joystick implementations
authorSam Lantinga <slouken@libsdl.org>
Mon, 26 Nov 2012 22:27:49 -0800
changeset 6698 28ab2ef7bfc9
parent 6697 dbda91031456
child 6699 f5c53e855de7
child 6700 c8f3d3f0251e
Completed adding new hotplug stubs for the joystick implementations
src/joystick/beos/SDL_bejoystick.cc
src/joystick/bsd/SDL_sysjoystick.c
src/joystick/darwin/SDL_sysjoystick.c
src/joystick/dummy/SDL_sysjoystick.c
src/joystick/iphoneos/SDL_sysjoystick.m
src/joystick/linux/SDL_sysjoystick.c
src/joystick/nds/SDL_sysjoystick.c
src/joystick/windows/SDL_dxjoystick.c
src/joystick/windows/SDL_mmjoystick.c
--- a/src/joystick/beos/SDL_bejoystick.cc	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/beos/SDL_bejoystick.cc	Mon Nov 26 22:27:49 2012 -0800
@@ -50,6 +50,8 @@
         int16 *new_axes;
     };
 
+    static int SDL_SYS_numjoysticks = 0;
+
 /* Function to scan the system for joysticks.
  * This function should set SDL_numjoysticks to the number of available
  * joysticks.  Joystick 0 should be the system default joystick.
@@ -58,34 +60,33 @@
     int SDL_SYS_JoystickInit(void)
     {
         BJoystick joystick;
-        int numjoysticks;
         int i;
         int32 nports;
         char name[B_OS_NAME_LENGTH];
 
         /* Search for attached joysticks */
           nports = joystick.CountDevices();
-          numjoysticks = 0;
+          SDL_SYS_numjoysticks = 0;
           SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport));
           SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname));
-        for (i = 0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i)
+        for (i = 0; (SDL_SYS_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i)
         {
             if (joystick.GetDeviceName(i, name) == B_OK) {
                 if (joystick.Open(name) != B_ERROR) {
                     BString stick_name;
                       joystick.GetControllerName(&stick_name);
-                      SDL_joyport[numjoysticks] = strdup(name);
-                      SDL_joyname[numjoysticks] = strdup(stick_name.String());
-                      numjoysticks++;
+                      SDL_joyport[SDL_SYS_numjoysticks] = strdup(name);
+                      SDL_joyname[SDL_SYS_numjoysticks] = strdup(stick_name.String());
+                      SDL_SYS_numjoysticks++;
                       joystick.Close();
                 }
             }
         }
-        return (numjoysticks);
+        return (SDL_SYS_numjoysticks);
     }
 
 /* Function to get the device-dependent name of a joystick */
-    const char *SDL_SYS_JoystickName(int index)
+    const char *SDL_SYS_JoystickNameForIndex(int index)
     {
         return SDL_joyname[index];
     }
@@ -95,11 +96,12 @@
    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 SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
     {
         BJoystick *stick;
 
         /* Create the joystick data structure */
+        joystick->instance_id = device_index;
         joystick->hwdata = (struct joystick_hwdata *)
             SDL_malloc(sizeof(*joystick->hwdata));
         if (joystick->hwdata == NULL) {
@@ -111,7 +113,7 @@
         joystick->hwdata->stick = stick;
 
         /* Open the requested joystick for use */
-        if (stick->Open(SDL_joyport[joystick->index]) == B_ERROR) {
+        if (stick->Open(SDL_joyport[device_index]) == B_ERROR) {
             SDL_SetError("Unable to open joystick");
             SDL_SYS_JoystickClose(joystick);
             return (-1);
@@ -233,6 +235,53 @@
         SDL_joyname[0] = NULL;
     }
 
+/* Function to perform the mapping from device index to the instance id for this index */
+    SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
+    {
+        return index;
+    }
+
+/* Function to determine is this joystick is attached to the system right now */
+    int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
+    {
+        return 1;
+    }
+
+    int SDL_SYS_NumJoysticks()
+    {
+        return SDL_SYS_numjoysticks;
+    }
+
+    int SDL_SYS_JoystickNeedsPolling()
+    {
+        return 0;
+    }
+
+    void SDL_SYS_JoystickDetect()
+    {
+    }
+
+    JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
+    {
+        JoystickGUID guid;
+        // the GUID is just the first 16 chars of the name for now
+        const char *name = SDL_SYS_JoystickNameForIndex( device_index );
+        SDL_zero( guid );
+        SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
+        return guid;
+    }
+
+
+    JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
+    {
+        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;
+    }
+
 };                              // extern "C"
 
 #endif /* SDL_JOYSTICK_BEOS */
--- a/src/joystick/bsd/SDL_sysjoystick.c	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/bsd/SDL_sysjoystick.c	Mon Nov 26 22:27:49 2012 -0800
@@ -278,6 +278,7 @@
         return (-1);
     }
 
+    joy->instance_id = device_index;
     hw = (struct joystick_hwdata *)
         SDL_malloc(sizeof(struct joystick_hwdata));
     if (hw == NULL) {
--- a/src/joystick/darwin/SDL_sysjoystick.c	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/darwin/SDL_sysjoystick.c	Mon Nov 26 22:27:49 2012 -0800
@@ -831,8 +831,8 @@
     for (index = device_index; index > 0; index--)
         device = device->pNext;
 
+	joystick->instance_id = device->instance_id;
     joystick->hwdata = device;
-	joystick->instance_id = device->instance_id;
 	joystick->name = device->product;
 
 	joystick->naxes = device->axes;
--- a/src/joystick/dummy/SDL_sysjoystick.c	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/dummy/SDL_sysjoystick.c	Mon Nov 26 22:27:49 2012 -0800
@@ -42,7 +42,7 @@
 
 /* Function to get the device-dependent name of a joystick */
 const char *
-SDL_SYS_JoystickName(int index)
+SDL_SYS_JoystickNameForDevice(int index)
 {
     SDL_SetError("Logic error: No joysticks available");
     return (NULL);
@@ -54,7 +54,7 @@
    It returns 0, or -1 if there is an error.
  */
 int
-SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
+SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
 {
     SDL_SetError("Logic error: No joysticks available");
     return (-1);
@@ -85,6 +85,53 @@
     return;
 }
 
+/* Function to perform the mapping from device index to the instance id for this index */
+SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
+{
+    return index;
+}
+
+/* Function to determine is this joystick is attached to the system right now */
+int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
+{
+    return 1;
+}
+
+int SDL_SYS_NumJoysticks()
+{
+    return 0;
+}
+
+int SDL_SYS_JoystickNeedsPolling()
+{
+    return 0;
+}
+
+void SDL_SYS_JoystickDetect()
+{
+}
+
+JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
+{
+    JoystickGUID guid;
+    // the GUID is just the first 16 chars of the name for now
+    const char *name = SDL_SYS_JoystickNameForIndex( device_index );
+    SDL_zero( guid );
+    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
+    return guid;
+}
+
+
+JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
+{
+    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;
+}
+
 #endif /* SDL_JOYSTICK_DUMMY || SDL_JOYSTICK_DISABLED */
 
 /* vi: set ts=4 sw=4 expandtab: */
--- a/src/joystick/iphoneos/SDL_sysjoystick.m	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Mon Nov 26 22:27:49 2012 -0800
@@ -68,12 +68,10 @@
 		joystick->nbuttons = 0;
 		[[SDLUIAccelerationDelegate sharedDelegate] startup];
 		return 0;
-	}
-	else {
+	} else {
 		SDL_SetError("No joystick available with that index");
 		return (-1);
 	}
-	
 }
 
 /* Function to update the state of a joystick - called as a device poll.
--- a/src/joystick/linux/SDL_sysjoystick.c	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/linux/SDL_sysjoystick.c	Mon Nov 26 22:27:49 2012 -0800
@@ -390,7 +390,7 @@
 
 #endif /* SDL_INPUT_LINUXEV */
 
-int SDL_SYS_numjoysticks = 0;
+static int SDL_SYS_numjoysticks = 0;
 
 /* Function to scan the system for joysticks */
 int
@@ -827,6 +827,7 @@
         SDL_SetError("Unable to open %s\n", SDL_joylist[joystick->instance_id]);
         return (-1);
     }
+    joystick->instance_id = device_index;
     joystick->hwdata = (struct joystick_hwdata *)
         SDL_malloc(sizeof(*joystick->hwdata));
     if (joystick->hwdata == NULL) {
@@ -837,7 +838,6 @@
     SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
     joystick->hwdata->fd = fd;
     joystick->hwdata->fname = fname;
-    joystick->instance_id = device_index;
 
     /* Set the joystick to non-blocking read mode */
     fcntl(fd, F_SETFL, O_NONBLOCK);
--- a/src/joystick/nds/SDL_sysjoystick.c	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/nds/SDL_sysjoystick.c	Mon Nov 26 22:27:49 2012 -0800
@@ -43,13 +43,12 @@
 int
 SDL_SYS_JoystickInit(void)
 {
-    SDL_numjoysticks = 1;
     return (1);
 }
 
 /* Function to get the device-dependent name of a joystick */
 const char *
-SDL_SYS_JoystickName(int index)
+SDL_SYS_JoystickNameForIndex(int index)
 {
     if (!index)
         return "NDS builtin joypad";
@@ -63,7 +62,7 @@
    It returns 0, or -1 if there is an error.
  */
 int
-SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
+SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
 {
     joystick->nbuttons = 8;
     joystick->nhats = 0;
@@ -168,4 +167,51 @@
 {
 }
 
+/* Function to perform the mapping from device index to the instance id for this index */
+SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
+{
+    return index;
+}
+
+/* Function to determine is this joystick is attached to the system right now */
+int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
+{
+    return 1;
+}
+
+int SDL_SYS_NumJoysticks()
+{
+    return 1;
+}
+
+int SDL_SYS_JoystickNeedsPolling()
+{
+    return 0;
+}
+
+void SDL_SYS_JoystickDetect()
+{
+}
+
+JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
+{
+    JoystickGUID guid;
+    // the GUID is just the first 16 chars of the name for now
+    const char *name = SDL_SYS_JoystickNameForIndex( device_index );
+    SDL_zero( guid );
+    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
+    return guid;
+}
+
+
+JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
+{
+    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;
+}
+
 #endif /* SDL_JOYSTICK_NDS */
--- a/src/joystick/windows/SDL_dxjoystick.c	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/windows/SDL_dxjoystick.c	Mon Nov 26 22:27:49 2012 -0800
@@ -707,6 +707,7 @@
     dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
 
     /* allocate memory for system specific hardware data */
+	joystick->instance_id = joystickdevice->nInstanceID;
     joystick->hwdata =
         (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
     if (joystick->hwdata == NULL) {
@@ -716,7 +717,6 @@
     SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
     joystick->hwdata->buffered = 1;
 	joystick->hwdata->removed = 0;
-	joystick->instance_id = joystickdevice->nInstanceID;
     joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
 	joystick->hwdata->guid = joystickdevice->guid;
 
--- a/src/joystick/windows/SDL_mmjoystick.c	Mon Nov 26 22:09:58 2012 -0800
+++ b/src/joystick/windows/SDL_mmjoystick.c	Mon Nov 26 22:27:49 2012 -0800
@@ -135,6 +135,8 @@
     return (name);
 }
 
+static int SDL_SYS_numjoysticks = 0;
+
 /* Function to scan the system for joysticks.
  * This function should set SDL_numjoysticks to the number of available
  * joysticks.  Joystick 0 should be the system default joystick.
@@ -145,7 +147,6 @@
 {
     int i;
     int maxdevs;
-    int numdevs;
     JOYINFOEX joyinfo;
     JOYCAPS joycaps;
     MMRESULT result;
@@ -157,9 +158,9 @@
     }
 
     /* Loop over all potential joystick devices */
-    numdevs = 0;
+    SDL_SYS_numjoysticks = 0;
     maxdevs = joyGetNumDevs();
-    for (i = JOYSTICKID1; i < maxdevs && numdevs < MAX_JOYSTICKS; ++i) {
+    for (i = JOYSTICKID1; i < maxdevs && SDL_SYS_numjoysticks < MAX_JOYSTICKS; ++i) {
 
         joyinfo.dwSize = sizeof(joyinfo);
         joyinfo.dwFlags = JOY_RETURNALL;
@@ -167,20 +168,20 @@
         if (result == JOYERR_NOERROR) {
             result = joyGetDevCaps(i, &joycaps, sizeof(joycaps));
             if (result == JOYERR_NOERROR) {
-                SYS_JoystickID[numdevs] = i;
-                SYS_Joystick[numdevs] = joycaps;
-                SYS_JoystickName[numdevs] =
+                SYS_JoystickID[SDL_SYS_numjoysticks] = i;
+                SYS_Joystick[SDL_SYS_numjoysticks] = joycaps;
+                SYS_JoystickName[SDL_SYS_numjoysticks] =
                     GetJoystickName(i, joycaps.szRegKey);
-                numdevs++;
+                SDL_SYS_numjoysticks++;
             }
         }
     }
-    return (numdevs);
+    return (SDL_SYS_numjoysticks);
 }
 
 /* Function to get the device-dependent name of a joystick */
 const char *
-SDL_SYS_JoystickName(int index)
+SDL_SYS_JoystickNameForIndex(int index)
 {
     if (SYS_JoystickName[index] != NULL) {
         return (SYS_JoystickName[index]);
@@ -195,7 +196,7 @@
    It returns 0, or -1 if there is an error.
  */
 int
-SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
+SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
 {
     int index, i;
     int caps_flags[MAX_AXES - 2] =
@@ -204,7 +205,7 @@
 
 
     /* shortcut */
-    index = joystick->index;
+    index = device_index;
     axis_min[0] = SYS_Joystick[index].wXmin;
     axis_max[0] = SYS_Joystick[index].wXmax;
     axis_min[1] = SYS_Joystick[index].wYmin;
@@ -219,6 +220,7 @@
     axis_max[5] = SYS_Joystick[index].wVmax;
 
     /* allocate memory for system specific hardware data */
+    joystick->instance_id = device_index;
     joystick->hwdata =
         (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
     if (joystick->hwdata == NULL) {
@@ -377,6 +379,53 @@
     }
 }
 
+/* Function to perform the mapping from device index to the instance id for this index */
+SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
+{
+    return index;
+}
+
+/* Function to determine is this joystick is attached to the system right now */
+int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
+{
+    return 1;
+}
+
+int SDL_SYS_NumJoysticks()
+{
+    return SDL_SYS_numjoysticks;
+}
+
+int SDL_SYS_JoystickNeedsPolling()
+{
+    return 0;
+}
+
+void SDL_SYS_JoystickDetect()
+{
+}
+
+JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
+{
+    JoystickGUID guid;
+    // the GUID is just the first 16 chars of the name for now
+    const char *name = SDL_SYS_JoystickNameForIndex( device_index );
+    SDL_zero( guid );
+    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
+    return guid;
+}
+
+
+JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
+{
+    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;
+}
+
 
 /* implementation functions */
 void