From 9e9b99b5f655af3025815f395f57ae4ff8f17959 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 26 Nov 2012 22:27:49 -0800 Subject: [PATCH] Completed adding new hotplug stubs for the joystick implementations --- src/joystick/beos/SDL_bejoystick.cc | 69 ++++++++++++++++++++---- src/joystick/bsd/SDL_sysjoystick.c | 1 + src/joystick/darwin/SDL_sysjoystick.c | 2 +- src/joystick/dummy/SDL_sysjoystick.c | 51 +++++++++++++++++- src/joystick/iphoneos/SDL_sysjoystick.m | 4 +- src/joystick/linux/SDL_sysjoystick.c | 4 +- src/joystick/nds/SDL_sysjoystick.c | 52 ++++++++++++++++-- src/joystick/windows/SDL_dxjoystick.c | 2 +- src/joystick/windows/SDL_mmjoystick.c | 71 +++++++++++++++++++++---- 9 files changed, 223 insertions(+), 33 deletions(-) diff --git a/src/joystick/beos/SDL_bejoystick.cc b/src/joystick/beos/SDL_bejoystick.cc index cae0ae188..289c3fd7e 100755 --- a/src/joystick/beos/SDL_bejoystick.cc +++ b/src/joystick/beos/SDL_bejoystick.cc @@ -50,6 +50,8 @@ extern "C" 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 @@ extern "C" 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 @@ extern "C" 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 @@ extern "C" 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 @@ extern "C" 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 */ diff --git a/src/joystick/bsd/SDL_sysjoystick.c b/src/joystick/bsd/SDL_sysjoystick.c index 5f3bbb4a8..17dcc9686 100644 --- a/src/joystick/bsd/SDL_sysjoystick.c +++ b/src/joystick/bsd/SDL_sysjoystick.c @@ -278,6 +278,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index) return (-1); } + joy->instance_id = device_index; hw = (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata)); if (hw == NULL) { diff --git a/src/joystick/darwin/SDL_sysjoystick.c b/src/joystick/darwin/SDL_sysjoystick.c index d46712f0c..c80c499e2 100644 --- a/src/joystick/darwin/SDL_sysjoystick.c +++ b/src/joystick/darwin/SDL_sysjoystick.c @@ -831,8 +831,8 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) for (index = device_index; index > 0; index--) device = device->pNext; - joystick->hwdata = device; joystick->instance_id = device->instance_id; + joystick->hwdata = device; joystick->name = device->product; joystick->naxes = device->axes; diff --git a/src/joystick/dummy/SDL_sysjoystick.c b/src/joystick/dummy/SDL_sysjoystick.c index 02bd5d751..7e81e9695 100644 --- a/src/joystick/dummy/SDL_sysjoystick.c +++ b/src/joystick/dummy/SDL_sysjoystick.c @@ -42,7 +42,7 @@ SDL_SYS_JoystickInit(void) /* 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 @@ SDL_SYS_JoystickName(int index) 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 @@ SDL_SYS_JoystickQuit(void) 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: */ diff --git a/src/joystick/iphoneos/SDL_sysjoystick.m b/src/joystick/iphoneos/SDL_sysjoystick.m index 6a6b8037c..5c5d66618 100755 --- a/src/joystick/iphoneos/SDL_sysjoystick.m +++ b/src/joystick/iphoneos/SDL_sysjoystick.m @@ -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. diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c index 1485dc80d..8c6c0a3e4 100644 --- a/src/joystick/linux/SDL_sysjoystick.c +++ b/src/joystick/linux/SDL_sysjoystick.c @@ -390,7 +390,7 @@ EV_IsJoystick(int fd) #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_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) 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_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) 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); diff --git a/src/joystick/nds/SDL_sysjoystick.c b/src/joystick/nds/SDL_sysjoystick.c index 703734f57..e7fa3bb49 100644 --- a/src/joystick/nds/SDL_sysjoystick.c +++ b/src/joystick/nds/SDL_sysjoystick.c @@ -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 @@ SDL_SYS_JoystickName(int index) 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 @@ SDL_SYS_JoystickQuit(void) { } +/* 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 */ diff --git a/src/joystick/windows/SDL_dxjoystick.c b/src/joystick/windows/SDL_dxjoystick.c index bcd66417f..ba3d52c38 100644 --- a/src/joystick/windows/SDL_dxjoystick.c +++ b/src/joystick/windows/SDL_dxjoystick.c @@ -707,6 +707,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) 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_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index) 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; diff --git a/src/joystick/windows/SDL_mmjoystick.c b/src/joystick/windows/SDL_mmjoystick.c index 5298c537b..e2c2531c7 100644 --- a/src/joystick/windows/SDL_mmjoystick.c +++ b/src/joystick/windows/SDL_mmjoystick.c @@ -135,6 +135,8 @@ GetJoystickName(int index, const char *szRegKey) 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 @@ SDL_SYS_JoystickInit(void) { int i; int maxdevs; - int numdevs; JOYINFOEX joyinfo; JOYCAPS joycaps; MMRESULT result; @@ -157,9 +158,9 @@ SDL_SYS_JoystickInit(void) } /* 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 @@ SDL_SYS_JoystickInit(void) 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 @@ SDL_SYS_JoystickName(int index) 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 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick) /* 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 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick) 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 @@ SDL_SYS_JoystickQuit(void) } } +/* 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