Skip to content

Commit

Permalink
bsd: Update joystick code for new interfaces.
Browse files Browse the repository at this point in the history
(this is an untested push to see if buildbot likes it.)
  • Loading branch information
icculus committed Aug 10, 2018
1 parent b692c35 commit 6776407
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
3 changes: 3 additions & 0 deletions src/joystick/SDL_joystick.c
Expand Up @@ -64,6 +64,9 @@ static SDL_JoystickDriver *SDL_joystick_drivers[] = {
#ifdef SDL_JOYSTICK_EMSCRIPTEN
&SDL_EMSCRIPTEN_JoystickDriver,
#endif
#ifdef SDL_JOYSTICK_USBHID /* !!! FIXME: "USBHID" is a generic name, and doubly-confusing with HIDAPI next to it. This is the *BSD interface, rename this. */
&SDL_BSD_JoystickDriver,
#endif
#ifdef SDL_JOYSTICK_HIDAPI
&SDL_HIDAPI_JoystickDriver,
#endif
Expand Down
1 change: 1 addition & 0 deletions src/joystick/SDL_sysjoystick.h
Expand Up @@ -138,6 +138,7 @@ typedef struct _SDL_JoystickDriver

/* The available joystick drivers */
extern SDL_JoystickDriver SDL_ANDROID_JoystickDriver;
extern SDL_JoystickDriver SDL_BSD_JoystickDriver;
extern SDL_JoystickDriver SDL_DARWIN_JoystickDriver;
extern SDL_JoystickDriver SDL_DUMMY_JoystickDriver;
extern SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver;
Expand Down
97 changes: 55 additions & 42 deletions src/joystick/bsd/SDL_sysjoystick.c
Expand Up @@ -161,15 +161,15 @@ static void report_free(struct report *);
#define REP_BUF_DATA(rep) ((rep)->buf->data)
#endif

static int SDL_SYS_numjoysticks = 0;
static int numjoysticks = 0;

int
SDL_SYS_JoystickInit(void)
static int
BSD_JoystickInit(void)
{
char s[16];
int i, fd;

SDL_SYS_numjoysticks = 0;
numjoysticks = 0;

SDL_memset(joynames, 0, sizeof(joynames));
SDL_memset(joydevnames, 0, sizeof(joydevnames));
Expand All @@ -179,44 +179,44 @@ SDL_SYS_JoystickInit(void)

SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);

joynames[SDL_SYS_numjoysticks] = SDL_strdup(s);
joynames[numjoysticks] = SDL_strdup(s);

if (SDL_SYS_JoystickOpen(&nj, SDL_SYS_numjoysticks) == 0) {
SDL_SYS_JoystickClose(&nj);
SDL_SYS_numjoysticks++;
if (BSD_JoystickOpen(&nj, numjoysticks) == 0) {
BSD_JoystickClose(&nj);
numjoysticks++;
} else {
SDL_free(joynames[SDL_SYS_numjoysticks]);
joynames[SDL_SYS_numjoysticks] = NULL;
SDL_free(joynames[numjoysticks]);
joynames[numjoysticks] = NULL;
}
}
for (i = 0; i < MAX_JOY_JOYS; i++) {
SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
fd = open(s, O_RDONLY);
if (fd != -1) {
joynames[SDL_SYS_numjoysticks++] = SDL_strdup(s);
joynames[numjoysticks++] = SDL_strdup(s);
close(fd);
}
}

/* Read the default USB HID usage table. */
hid_init(NULL);

return (SDL_SYS_numjoysticks);
return (numjoysticks);
}

int
SDL_SYS_NumJoysticks(void)
static int
BSD_JoystickGetCount(void)
{
return SDL_SYS_numjoysticks;
return numjoysticks;
}

void
SDL_SYS_JoystickDetect(void)
static void
BSD_JoystickDetect(void)
{
}

const char *
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
static const char *
BSD_JoystickGetDeviceName(int device_index)
{
if (joydevnames[device_index] != NULL) {
return (joydevnames[device_index]);
Expand All @@ -225,7 +225,8 @@ SDL_SYS_JoystickNameForDeviceIndex(int device_index)
}

/* Function to perform the mapping from device index to the instance id for this index */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
static SDL_JoystickID
BSD_JoystickGetDeviceInstanceID(int device_index)
{
return device_index;
}
Expand Down Expand Up @@ -281,8 +282,8 @@ hatval_to_sdl(Sint32 hatval)
}


int
SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
static int
BSD_JoystickOpen(SDL_Joystick * joy, int device_index)
{
char *path = joynames[device_index];
struct joystick_hwdata *hw;
Expand Down Expand Up @@ -365,8 +366,8 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
str[i] = '\0';
asprintf(&new_name, "%s @ %s", str, path);
if (new_name != NULL) {
SDL_free(joydevnames[SDL_SYS_numjoysticks]);
joydevnames[SDL_SYS_numjoysticks] = new_name;
SDL_free(joydevnames[numjoysticks]);
joydevnames[numjoysticks] = new_name;
}
}
desc_failed:
Expand Down Expand Up @@ -467,8 +468,8 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joy, int device_index)
return (-1);
}

void
SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
static void
BSD_JoystickUpdate(SDL_Joystick * joy)
{
struct hid_item hitem;
struct hid_data *hdata;
Expand Down Expand Up @@ -580,8 +581,8 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joy)
}

/* Function to close a joystick after use */
void
SDL_SYS_JoystickClose(SDL_Joystick * joy)
static void
BSD_JoystickClose(SDL_Joystick * joy)
{
if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
report_free(&joy->hwdata->inreport);
Expand All @@ -592,8 +593,8 @@ SDL_SYS_JoystickClose(SDL_Joystick * joy)
SDL_free(joy->hwdata);
}

void
SDL_SYS_JoystickQuit(void)
static void
BSD_JoystickQuit(void)
{
int i;

Expand All @@ -605,21 +606,12 @@ SDL_SYS_JoystickQuit(void)
return;
}

SDL_JoystickGUID SDL_SYS_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 );
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 SDL_JoystickGUID
BSD_JoystickGetDeviceGUID( int device_index )
{
SDL_JoystickGUID guid;
/* the GUID is just the first 16 chars of the name for now */
const char *name = joystick->name;
const char *name = BSD_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid;
Expand Down Expand Up @@ -680,6 +672,27 @@ report_free(struct report *r)
r->status = SREPORT_UNINIT;
}

static int
BSD_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
{
return SDL_Unsupported();
}

SDL_JoystickDriver SDL_BSD_JoystickDriver =
{
BSD_JoystickInit,
BSD_JoystickGetCount,
BSD_JoystickDetect,
BSD_JoystickGetDeviceName,
BSD_JoystickGetDeviceGUID,
BSD_JoystickGetDeviceInstanceID,
BSD_JoystickOpen,
BSD_JoystickRumble,
BSD_JoystickUpdate,
BSD_JoystickClose,
BSD_JoystickQuit,
};

#endif /* SDL_JOYSTICK_USBHID */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 6776407

Please sign in to comment.