Completed adding new hotplug stubs for the joystick implementations

This commit is contained in:
Sam Lantinga 2012-11-26 22:27:49 -08:00
parent a8966c167e
commit c9f59a287d
9 changed files with 223 additions and 33 deletions

View file

@ -50,6 +50,8 @@ extern "C"
int16 *new_axes; int16 *new_axes;
}; };
static int SDL_SYS_numjoysticks = 0;
/* Function to scan the system for joysticks. /* Function to scan the system for joysticks.
* This function should set SDL_numjoysticks to the number of available * This function should set SDL_numjoysticks to the number of available
* joysticks. Joystick 0 should be the system default joystick. * joysticks. Joystick 0 should be the system default joystick.
@ -58,34 +60,33 @@ extern "C"
int SDL_SYS_JoystickInit(void) int SDL_SYS_JoystickInit(void)
{ {
BJoystick joystick; BJoystick joystick;
int numjoysticks;
int i; int i;
int32 nports; int32 nports;
char name[B_OS_NAME_LENGTH]; char name[B_OS_NAME_LENGTH];
/* Search for attached joysticks */ /* Search for attached joysticks */
nports = joystick.CountDevices(); nports = joystick.CountDevices();
numjoysticks = 0; SDL_SYS_numjoysticks = 0;
SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport)); SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport));
SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname)); 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.GetDeviceName(i, name) == B_OK) {
if (joystick.Open(name) != B_ERROR) { if (joystick.Open(name) != B_ERROR) {
BString stick_name; BString stick_name;
joystick.GetControllerName(&stick_name); joystick.GetControllerName(&stick_name);
SDL_joyport[numjoysticks] = strdup(name); SDL_joyport[SDL_SYS_numjoysticks] = strdup(name);
SDL_joyname[numjoysticks] = strdup(stick_name.String()); SDL_joyname[SDL_SYS_numjoysticks] = strdup(stick_name.String());
numjoysticks++; SDL_SYS_numjoysticks++;
joystick.Close(); joystick.Close();
} }
} }
} }
return (numjoysticks); return (SDL_SYS_numjoysticks);
} }
/* Function to get the device-dependent name of a joystick */ /* 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]; return SDL_joyname[index];
} }
@ -95,11 +96,12 @@ extern "C"
This should fill the nbuttons and naxes fields of the joystick structure. This should fill the nbuttons and naxes fields of the joystick structure.
It returns 0, or -1 if there is an error. 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; BJoystick *stick;
/* Create the joystick data structure */ /* Create the joystick data structure */
joystick->instance_id = device_index;
joystick->hwdata = (struct joystick_hwdata *) joystick->hwdata = (struct joystick_hwdata *)
SDL_malloc(sizeof(*joystick->hwdata)); SDL_malloc(sizeof(*joystick->hwdata));
if (joystick->hwdata == NULL) { if (joystick->hwdata == NULL) {
@ -111,7 +113,7 @@ extern "C"
joystick->hwdata->stick = stick; joystick->hwdata->stick = stick;
/* Open the requested joystick for use */ /* 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_SetError("Unable to open joystick");
SDL_SYS_JoystickClose(joystick); SDL_SYS_JoystickClose(joystick);
return (-1); return (-1);
@ -233,6 +235,53 @@ extern "C"
SDL_joyname[0] = NULL; 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" }; // extern "C"
#endif /* SDL_JOYSTICK_BEOS */ #endif /* SDL_JOYSTICK_BEOS */

View file

@ -278,6 +278,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
return (-1); return (-1);
} }
joy->instance_id = device_index;
hw = (struct joystick_hwdata *) hw = (struct joystick_hwdata *)
SDL_malloc(sizeof(struct joystick_hwdata)); SDL_malloc(sizeof(struct joystick_hwdata));
if (hw == NULL) { if (hw == NULL) {

View file

@ -831,8 +831,8 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
for (index = device_index; index > 0; index--) for (index = device_index; index > 0; index--)
device = device->pNext; device = device->pNext;
joystick->hwdata = device;
joystick->instance_id = device->instance_id; joystick->instance_id = device->instance_id;
joystick->hwdata = device;
joystick->name = device->product; joystick->name = device->product;
joystick->naxes = device->axes; joystick->naxes = device->axes;

View file

@ -42,7 +42,7 @@ SDL_SYS_JoystickInit(void)
/* Function to get the device-dependent name of a joystick */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickName(int index) SDL_SYS_JoystickNameForDevice(int index)
{ {
SDL_SetError("Logic error: No joysticks available"); SDL_SetError("Logic error: No joysticks available");
return (NULL); return (NULL);
@ -54,7 +54,7 @@ SDL_SYS_JoystickName(int index)
It returns 0, or -1 if there is an error. It returns 0, or -1 if there is an error.
*/ */
int int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick) SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{ {
SDL_SetError("Logic error: No joysticks available"); SDL_SetError("Logic error: No joysticks available");
return (-1); return (-1);
@ -85,6 +85,53 @@ SDL_SYS_JoystickQuit(void)
return; 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 */ #endif /* SDL_JOYSTICK_DUMMY || SDL_JOYSTICK_DISABLED */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View file

@ -68,12 +68,10 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
joystick->nbuttons = 0; joystick->nbuttons = 0;
[[SDLUIAccelerationDelegate sharedDelegate] startup]; [[SDLUIAccelerationDelegate sharedDelegate] startup];
return 0; return 0;
} } else {
else {
SDL_SetError("No joystick available with that index"); SDL_SetError("No joystick available with that index");
return (-1); return (-1);
} }
} }
/* Function to update the state of a joystick - called as a device poll. /* Function to update the state of a joystick - called as a device poll.

View file

@ -390,7 +390,7 @@ EV_IsJoystick(int fd)
#endif /* SDL_INPUT_LINUXEV */ #endif /* SDL_INPUT_LINUXEV */
int SDL_SYS_numjoysticks = 0; static int SDL_SYS_numjoysticks = 0;
/* Function to scan the system for joysticks */ /* Function to scan the system for joysticks */
int 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]); SDL_SetError("Unable to open %s\n", SDL_joylist[joystick->instance_id]);
return (-1); return (-1);
} }
joystick->instance_id = device_index;
joystick->hwdata = (struct joystick_hwdata *) joystick->hwdata = (struct joystick_hwdata *)
SDL_malloc(sizeof(*joystick->hwdata)); SDL_malloc(sizeof(*joystick->hwdata));
if (joystick->hwdata == NULL) { 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)); SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
joystick->hwdata->fd = fd; joystick->hwdata->fd = fd;
joystick->hwdata->fname = fname; joystick->hwdata->fname = fname;
joystick->instance_id = device_index;
/* Set the joystick to non-blocking read mode */ /* Set the joystick to non-blocking read mode */
fcntl(fd, F_SETFL, O_NONBLOCK); fcntl(fd, F_SETFL, O_NONBLOCK);

View file

@ -43,13 +43,12 @@
int int
SDL_SYS_JoystickInit(void) SDL_SYS_JoystickInit(void)
{ {
SDL_numjoysticks = 1;
return (1); return (1);
} }
/* Function to get the device-dependent name of a joystick */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickName(int index) SDL_SYS_JoystickNameForIndex(int index)
{ {
if (!index) if (!index)
return "NDS builtin joypad"; return "NDS builtin joypad";
@ -63,7 +62,7 @@ SDL_SYS_JoystickName(int index)
It returns 0, or -1 if there is an error. It returns 0, or -1 if there is an error.
*/ */
int int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick) SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{ {
joystick->nbuttons = 8; joystick->nbuttons = 8;
joystick->nhats = 0; 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 */ #endif /* SDL_JOYSTICK_NDS */

View file

@ -707,6 +707,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
/* allocate memory for system specific hardware data */ /* allocate memory for system specific hardware data */
joystick->instance_id = joystickdevice->nInstanceID;
joystick->hwdata = joystick->hwdata =
(struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata)); (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
if (joystick->hwdata == NULL) { 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)); SDL_memset(joystick->hwdata, 0, sizeof(struct joystick_hwdata));
joystick->hwdata->buffered = 1; joystick->hwdata->buffered = 1;
joystick->hwdata->removed = 0; joystick->hwdata->removed = 0;
joystick->instance_id = joystickdevice->nInstanceID;
joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS); joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
joystick->hwdata->guid = joystickdevice->guid; joystick->hwdata->guid = joystickdevice->guid;

View file

@ -135,6 +135,8 @@ GetJoystickName(int index, const char *szRegKey)
return (name); return (name);
} }
static int SDL_SYS_numjoysticks = 0;
/* Function to scan the system for joysticks. /* Function to scan the system for joysticks.
* This function should set SDL_numjoysticks to the number of available * This function should set SDL_numjoysticks to the number of available
* joysticks. Joystick 0 should be the system default joystick. * joysticks. Joystick 0 should be the system default joystick.
@ -145,7 +147,6 @@ SDL_SYS_JoystickInit(void)
{ {
int i; int i;
int maxdevs; int maxdevs;
int numdevs;
JOYINFOEX joyinfo; JOYINFOEX joyinfo;
JOYCAPS joycaps; JOYCAPS joycaps;
MMRESULT result; MMRESULT result;
@ -157,9 +158,9 @@ SDL_SYS_JoystickInit(void)
} }
/* Loop over all potential joystick devices */ /* Loop over all potential joystick devices */
numdevs = 0; SDL_SYS_numjoysticks = 0;
maxdevs = joyGetNumDevs(); 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.dwSize = sizeof(joyinfo);
joyinfo.dwFlags = JOY_RETURNALL; joyinfo.dwFlags = JOY_RETURNALL;
@ -167,20 +168,20 @@ SDL_SYS_JoystickInit(void)
if (result == JOYERR_NOERROR) { if (result == JOYERR_NOERROR) {
result = joyGetDevCaps(i, &joycaps, sizeof(joycaps)); result = joyGetDevCaps(i, &joycaps, sizeof(joycaps));
if (result == JOYERR_NOERROR) { if (result == JOYERR_NOERROR) {
SYS_JoystickID[numdevs] = i; SYS_JoystickID[SDL_SYS_numjoysticks] = i;
SYS_Joystick[numdevs] = joycaps; SYS_Joystick[SDL_SYS_numjoysticks] = joycaps;
SYS_JoystickName[numdevs] = SYS_JoystickName[SDL_SYS_numjoysticks] =
GetJoystickName(i, joycaps.szRegKey); GetJoystickName(i, joycaps.szRegKey);
numdevs++; SDL_SYS_numjoysticks++;
} }
} }
} }
return (numdevs); return (SDL_SYS_numjoysticks);
} }
/* Function to get the device-dependent name of a joystick */ /* Function to get the device-dependent name of a joystick */
const char * const char *
SDL_SYS_JoystickName(int index) SDL_SYS_JoystickNameForIndex(int index)
{ {
if (SYS_JoystickName[index] != NULL) { if (SYS_JoystickName[index] != NULL) {
return (SYS_JoystickName[index]); return (SYS_JoystickName[index]);
@ -195,7 +196,7 @@ SDL_SYS_JoystickName(int index)
It returns 0, or -1 if there is an error. It returns 0, or -1 if there is an error.
*/ */
int int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick) SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{ {
int index, i; int index, i;
int caps_flags[MAX_AXES - 2] = int caps_flags[MAX_AXES - 2] =
@ -204,7 +205,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
/* shortcut */ /* shortcut */
index = joystick->index; index = device_index;
axis_min[0] = SYS_Joystick[index].wXmin; axis_min[0] = SYS_Joystick[index].wXmin;
axis_max[0] = SYS_Joystick[index].wXmax; axis_max[0] = SYS_Joystick[index].wXmax;
axis_min[1] = SYS_Joystick[index].wYmin; axis_min[1] = SYS_Joystick[index].wYmin;
@ -219,6 +220,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
axis_max[5] = SYS_Joystick[index].wVmax; axis_max[5] = SYS_Joystick[index].wVmax;
/* allocate memory for system specific hardware data */ /* allocate memory for system specific hardware data */
joystick->instance_id = device_index;
joystick->hwdata = joystick->hwdata =
(struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata)); (struct joystick_hwdata *) SDL_malloc(sizeof(*joystick->hwdata));
if (joystick->hwdata == NULL) { 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 */ /* implementation functions */
void void