Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Updated the removal code to iterate over all joystick add messages instead of just the first one.
  • Loading branch information
slouken committed Aug 26, 2016
1 parent c69bce6 commit ad1bfea
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 217 deletions.
42 changes: 31 additions & 11 deletions src/joystick/SDL_gamecontroller.c
Expand Up @@ -105,6 +105,35 @@ struct _SDL_GameController
int SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);

/*
* If there is an existing add event in the queue, it needs to be modified
* to have the right value for which, because the number of controllers in
* the system is now one less.
*/
static void UpdateEventsForDeviceRemoval()
{
int i, num_events;
SDL_Event *events;

num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
if (num_events <= 0) {
return;
}

events = SDL_stack_alloc(SDL_Event, num_events);
if (!events) {
return;
}

num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
for (i = 0; i < num_events; ++i) {
--events[i].cdevice.which;
}
SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);

SDL_stack_free(events);
}

/*
* Event filter to fire controller events from joystick ones
*/
Expand Down Expand Up @@ -222,22 +251,13 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
SDL_GameController *controllerlist = SDL_gamecontrollers;
while (controllerlist) {
if (controllerlist->joystick->instance_id == event->jdevice.which) {
SDL_Event peeped;
SDL_Event deviceevent;

/* If there is an existing add event in the queue, it
* needs to be modified to have the right value for which,
* because the number of controllers in the system is now
* one less.
*/
if ( SDL_PeepEvents(&peeped, 1, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED) > 0) {
peeped.jdevice.which--;
SDL_PushEvent(&peeped);
}

deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
deviceevent.cdevice.which = event->jdevice.which;
SDL_PushEvent(&deviceevent);

UpdateEventsForDeviceRemoval();
break;
}
controllerlist = controllerlist->next;
Expand Down
65 changes: 65 additions & 0 deletions src/joystick/SDL_joystick.c
Expand Up @@ -497,6 +497,71 @@ SDL_PrivateJoystickShouldIgnoreEvent()

/* These are global for SDL_sysjoystick.c and SDL_events.c */

void SDL_PrivateJoystickAdded(int device_index)
{
#if !SDL_EVENTS_DISABLED
SDL_Event event;

event.type = SDL_JOYDEVICEADDED;

if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
}

/*
* If there is an existing add event in the queue, it needs to be modified
* to have the right value for which, because the number of controllers in
* the system is now one less.
*/
static void UpdateEventsForDeviceRemoval()
{
int i, num_events;
SDL_Event *events;

num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
if (num_events <= 0) {
return;
}

events = SDL_stack_alloc(SDL_Event, num_events);
if (!events) {
return;
}

num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
for (i = 0; i < num_events; ++i) {
--events[i].jdevice.which;
}
SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);

SDL_stack_free(events);
}

void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
{
#if !SDL_EVENTS_DISABLED
SDL_Event event;

event.type = SDL_JOYDEVICEREMOVED;

if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_instance;
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}

UpdateEventsForDeviceRemoval();
#endif /* !SDL_EVENTS_DISABLED */
}

int
SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
{
Expand Down
6 changes: 4 additions & 2 deletions src/joystick/SDL_joystick_c.h
Expand Up @@ -33,6 +33,8 @@ extern void SDL_GameControllerQuit(void);


/* Internal event queueing functions */
extern void SDL_PrivateJoystickAdded(int device_index);
extern void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance);
extern int SDL_PrivateJoystickAxis(SDL_Joystick * joystick,
Uint8 axis, Sint16 value);
extern int SDL_PrivateJoystickBall(SDL_Joystick * joystick,
Expand All @@ -41,8 +43,8 @@ extern int SDL_PrivateJoystickHat(SDL_Joystick * joystick,
Uint8 hat, Uint8 value);
extern int SDL_PrivateJoystickButton(SDL_Joystick * joystick,
Uint8 button, Uint8 state);
extern void SDL_PrivateJoystickBatteryLevel( SDL_Joystick * joystick,
SDL_JoystickPowerLevel ePowerLevel );
extern void SDL_PrivateJoystickBatteryLevel(SDL_Joystick * joystick,
SDL_JoystickPowerLevel ePowerLevel);

/* Internal sanity checking functions */
extern int SDL_PrivateJoystickValid(SDL_Joystick * joystick);
Expand Down
34 changes: 2 additions & 32 deletions src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -27,10 +27,6 @@
#include "SDL_error.h"
#include "SDL_events.h"

#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif

#include "SDL_joystick.h"
#include "SDL_hints.h"
#include "SDL_assert.h"
Expand Down Expand Up @@ -252,9 +248,6 @@ Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer,
{
SDL_JoystickGUID guid;
SDL_joylist_item *item;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif

if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
return -1;
Expand Down Expand Up @@ -299,17 +292,7 @@ Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer,
/* Need to increment the joystick count before we post the event */
++numjoysticks;

#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEADDED;

if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = (numjoysticks - 1);
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_PrivateJoystickAdded(numjoysticks - 1);

#ifdef DEBUG_JOYSTICK
SDL_Log("Added joystick %s with device_id %d", name, device_id);
Expand All @@ -323,9 +306,6 @@ Android_RemoveJoystick(int device_id)
{
SDL_joylist_item *item = SDL_joylist;
SDL_joylist_item *prev = NULL;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif

/* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
while (item != NULL) {
Expand Down Expand Up @@ -357,17 +337,7 @@ Android_RemoveJoystick(int device_id)
/* Need to decrement the joystick count before we post the event */
--numjoysticks;

#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEREMOVED;

if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = item->device_instance;
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_PrivateJoystickRemoved(item->device_instance);

#ifdef DEBUG_JOYSTICK
SDL_Log("Removed joystick with device_id %d", device_id);
Expand Down
35 changes: 2 additions & 33 deletions src/joystick/darwin/SDL_sysjoystick.c
Expand Up @@ -34,9 +34,6 @@
#include "SDL_sysjoystick_c.h"
#include "SDL_events.h"
#include "../../haptic/darwin/SDL_syshaptic_c.h" /* For haptic hot plugging */
#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif

#define SDL_JOYSTICK_RUNLOOP_MODE CFSTR("SDLJoystick")

Expand Down Expand Up @@ -154,21 +151,7 @@ JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
MacHaptic_MaybeRemoveDevice(device->ffservice);
#endif

/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
#if !SDL_EVENTS_DISABLED
{
SDL_Event event;
event.type = SDL_JOYDEVICEREMOVED;

if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device->instance_id;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_PrivateJoystickRemoved(device->instance_id);
}


Expand Down Expand Up @@ -476,21 +459,7 @@ JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDevic
++device_index; /* bump by one since we counted by pNext. */
}

/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
#if !SDL_EVENTS_DISABLED
{
SDL_Event event;
event.type = SDL_JOYDEVICEADDED;

if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_PrivateJoystickAdded(device_index);
}

static SDL_bool
Expand Down
37 changes: 4 additions & 33 deletions src/joystick/emscripten/SDL_sysjoystick.c
Expand Up @@ -27,10 +27,6 @@
#include "SDL_error.h"
#include "SDL_events.h"

#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif

#include "SDL_joystick.h"
#include "SDL_hints.h"
#include "SDL_assert.h"
Expand All @@ -57,10 +53,6 @@ Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepa
return 1;
}

#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif

item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
if (item == NULL) {
return 1;
Expand Down Expand Up @@ -105,20 +97,12 @@ Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepa
}

++numjoysticks;

SDL_PrivateJoystickAdded(numjoysticks - 1);

#ifdef DEBUG_JOYSTICK
SDL_Log("Number of joysticks is %d", numjoysticks);
#endif
#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEADDED;

if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = numjoysticks - 1;
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */

#ifdef DEBUG_JOYSTICK
SDL_Log("Added joystick with index %d", item->index);
Expand All @@ -132,9 +116,6 @@ Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGamepadEvent *gam
{
SDL_joylist_item *item = SDL_joylist;
SDL_joylist_item *prev = NULL;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif

while (item != NULL) {
if (item->index == gamepadEvent->index) {
Expand Down Expand Up @@ -165,17 +146,7 @@ Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGamepadEvent *gam
/* Need to decrement the joystick count before we post the event */
--numjoysticks;

#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEREMOVED;

if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = item->device_instance;
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_PrivateJoystickRemoved(item->device_instance);

#ifdef DEBUG_JOYSTICK
SDL_Log("Removed joystick with id %d", item->device_instance);
Expand Down

0 comments on commit ad1bfea

Please sign in to comment.