src/joystick/SDL_gamecontroller.c
changeset 10226 cb13d22b7f09
parent 10225 3134026517cb
child 10423 c5b42350ccbb
equal deleted inserted replaced
10225:3134026517cb 10226:cb13d22b7f09
   102 };
   102 };
   103 
   103 
   104 
   104 
   105 int SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
   105 int SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
   106 int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);
   106 int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);
       
   107 
       
   108 /*
       
   109  * If there is an existing add event in the queue, it needs to be modified
       
   110  * to have the right value for which, because the number of controllers in
       
   111  * the system is now one less.
       
   112  */
       
   113 static void UpdateEventsForDeviceRemoval()
       
   114 {
       
   115     int i, num_events;
       
   116     SDL_Event *events;
       
   117 
       
   118     num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
       
   119     if (num_events <= 0) {
       
   120         return;
       
   121     }
       
   122 
       
   123     events = SDL_stack_alloc(SDL_Event, num_events);
       
   124     if (!events) {
       
   125         return;
       
   126     }
       
   127 
       
   128     num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
       
   129     for (i = 0; i < num_events; ++i) {
       
   130         --events[i].cdevice.which;
       
   131     }
       
   132     SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
       
   133 
       
   134     SDL_stack_free(events);
       
   135 }
   107 
   136 
   108 /*
   137 /*
   109  * Event filter to fire controller events from joystick ones
   138  * Event filter to fire controller events from joystick ones
   110  */
   139  */
   111 int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
   140 int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
   220     case SDL_JOYDEVICEREMOVED:
   249     case SDL_JOYDEVICEREMOVED:
   221         {
   250         {
   222             SDL_GameController *controllerlist = SDL_gamecontrollers;
   251             SDL_GameController *controllerlist = SDL_gamecontrollers;
   223             while (controllerlist) {
   252             while (controllerlist) {
   224                 if (controllerlist->joystick->instance_id == event->jdevice.which) {
   253                 if (controllerlist->joystick->instance_id == event->jdevice.which) {
   225 					SDL_Event peeped;
       
   226                     SDL_Event deviceevent;
   254                     SDL_Event deviceevent;
   227 
       
   228 					/* If there is an existing add event in the queue, it
       
   229 					 * needs to be modified to have the right value for which,
       
   230 					 * because the number of controllers in the system is now
       
   231 					 * one less.
       
   232 					 */
       
   233 					if ( SDL_PeepEvents(&peeped, 1, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED) > 0) {
       
   234 						peeped.jdevice.which--;
       
   235 						SDL_PushEvent(&peeped);
       
   236 					}
       
   237 
   255 
   238                     deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
   256                     deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
   239                     deviceevent.cdevice.which = event->jdevice.which;
   257                     deviceevent.cdevice.which = event->jdevice.which;
   240                     SDL_PushEvent(&deviceevent);
   258                     SDL_PushEvent(&deviceevent);
       
   259 
       
   260                     UpdateEventsForDeviceRemoval();
   241                     break;
   261                     break;
   242                 }
   262                 }
   243                 controllerlist = controllerlist->next;
   263                 controllerlist = controllerlist->next;
   244             }
   264             }
   245         }
   265         }