Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
authorSam Lantinga <slouken@libsdl.org>
Mon, 23 Dec 2013 15:10:03 -0800
changeset 8067 8314a9008bf1
parent 8066 658b461d81be
child 8068 54fcab720079
Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
src/joystick/SDL_gamecontroller.c
--- a/src/joystick/SDL_gamecontroller.c	Mon Dec 23 12:17:52 2013 -0800
+++ b/src/joystick/SDL_gamecontroller.c	Mon Dec 23 15:10:03 2013 -0800
@@ -866,8 +866,7 @@
     const char *pMappingString = NULL;
     s_pSupportedControllers = NULL;
     pMappingString = s_ControllerMappings[i];
-    while ( pMappingString )
-    {
+    while ( pMappingString ) {
         SDL_GameControllerAddMapping( pMappingString );
 
         i++;
@@ -880,6 +879,16 @@
     /* watch for joy events and fire controller ones if needed */
     SDL_AddEventWatch( SDL_GameControllerEventWatcher, NULL );
 
+    /* Send added events for controllers currently attached */
+    for (i = 0; i < SDL_NumJoysticks(); ++i) {
+        if (SDL_IsGameController(i)) {
+            SDL_Event deviceevent;
+            deviceevent.type = SDL_CONTROLLERDEVICEADDED;
+            deviceevent.cdevice.which = i;
+            SDL_PushEvent(&deviceevent);
+        }
+    }
+
     return (0);
 }