test/testgamecontroller.c
changeset 8068 54fcab720079
parent 8043 74fcbf6987ba
child 8149 681eb46b8ac4
     1.1 --- a/test/testgamecontroller.c	Mon Dec 23 15:10:03 2013 -0800
     1.2 +++ b/test/testgamecontroller.c	Mon Dec 23 15:55:08 2013 -0800
     1.3 @@ -31,8 +31,6 @@
     1.4  #define MAX_NUM_AXES 6
     1.5  #define MAX_NUM_HATS 2
     1.6  
     1.7 -static SDL_bool s_ForceQuit = SDL_FALSE;
     1.8 -
     1.9  static void
    1.10  DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    1.11  {
    1.12 @@ -85,7 +83,7 @@
    1.13      }
    1.14  }
    1.15  
    1.16 -void
    1.17 +SDL_bool
    1.18  WatchGameController(SDL_GameController * gamecontroller)
    1.19  {
    1.20      const char *name = SDL_GameControllerName(gamecontroller);
    1.21 @@ -94,7 +92,8 @@
    1.22      char *title = (char *)SDL_malloc(titlelen);
    1.23      SDL_Window *window = NULL;
    1.24      SDL_Renderer *screen = NULL;
    1.25 -    int done = 0;
    1.26 +    SDL_bool retval = SDL_FALSE;
    1.27 +    SDL_bool done = SDL_FALSE;
    1.28      SDL_Event event;
    1.29      int i;
    1.30  
    1.31 @@ -108,14 +107,14 @@
    1.32                                SCREEN_HEIGHT, 0);
    1.33      if (window == NULL) {
    1.34          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
    1.35 -        return;
    1.36 +        return SDL_FALSE;
    1.37      }
    1.38  
    1.39      screen = SDL_CreateRenderer(window, -1, 0);
    1.40      if (screen == NULL) {
    1.41          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
    1.42          SDL_DestroyWindow(window);
    1.43 -        return;
    1.44 +        return SDL_FALSE;
    1.45      }
    1.46  
    1.47      SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.48 @@ -157,8 +156,7 @@
    1.49                  }
    1.50                  /* Fall through to signal quit */
    1.51              case SDL_QUIT:
    1.52 -                done = 1;
    1.53 -                s_ForceQuit = SDL_TRUE;
    1.54 +                done = SDL_TRUE;
    1.55                  break;
    1.56              default:
    1.57                  break;
    1.58 @@ -200,12 +198,15 @@
    1.59  
    1.60          SDL_RenderPresent(screen);
    1.61  
    1.62 -        if ( !done )
    1.63 -            done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
    1.64 +        if (!SDL_GameControllerGetAttached(gamecontroller)) {
    1.65 +            done = SDL_TRUE;
    1.66 +            retval = SDL_TRUE;  /* keep going, wait for reattach. */
    1.67 +        }
    1.68      }
    1.69  
    1.70      SDL_DestroyRenderer(screen);
    1.71      SDL_DestroyWindow(window);
    1.72 +    return retval;
    1.73  }
    1.74  
    1.75  int
    1.76 @@ -250,6 +251,9 @@
    1.77      SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
    1.78  
    1.79      if (argv[1]) {
    1.80 +        SDL_bool reportederror = SDL_FALSE;
    1.81 +        SDL_bool keepGoing = SDL_TRUE;
    1.82 +        SDL_Event event;
    1.83          int device = atoi(argv[1]);
    1.84          if (device >= SDL_NumJoysticks()) {
    1.85  			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
    1.86 @@ -259,12 +263,36 @@
    1.87                                        guid, sizeof (guid));
    1.88              SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
    1.89              gamecontroller = SDL_GameControllerOpen(device);
    1.90 -            if (gamecontroller == NULL) {
    1.91 -                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open joystick %d: %s\n", device, SDL_GetError());
    1.92 -                retcode = 1;
    1.93 -            } else {
    1.94 -                WatchGameController(gamecontroller);
    1.95 -                SDL_GameControllerClose(gamecontroller);
    1.96 +            while (keepGoing) {
    1.97 +                if (gamecontroller == NULL) {
    1.98 +                    if (!reportederror) {
    1.99 +                        if (gamecontroller == NULL) {
   1.100 +                            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
   1.101 +                            retcode = 1;
   1.102 +                        }
   1.103 +                        keepGoing = SDL_FALSE;
   1.104 +                        reportederror = SDL_TRUE;
   1.105 +                    }
   1.106 +                } else {
   1.107 +                    reportederror = SDL_FALSE;
   1.108 +                    keepGoing = WatchGameController(gamecontroller);
   1.109 +                    SDL_GameControllerClose(gamecontroller);
   1.110 +                }
   1.111 +
   1.112 +                gamecontroller = NULL;
   1.113 +                if (keepGoing) {
   1.114 +                    SDL_Log("Waiting for attach\n");
   1.115 +                }
   1.116 +                while (keepGoing) {
   1.117 +                    SDL_WaitEvent(&event);
   1.118 +                    if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
   1.119 +                        || (event.type == SDL_MOUSEBUTTONDOWN)) {
   1.120 +                        keepGoing = SDL_FALSE;
   1.121 +                    } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
   1.122 +                        gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
   1.123 +                        break;
   1.124 +                    }
   1.125 +                }
   1.126              }
   1.127          }
   1.128      }