test/testgamecontroller.c
changeset 8068 54fcab720079
parent 8043 74fcbf6987ba
child 8149 681eb46b8ac4
equal deleted inserted replaced
8067:8314a9008bf1 8068:54fcab720079
    28 #define SCREEN_HEIGHT    480
    28 #define SCREEN_HEIGHT    480
    29 #endif
    29 #endif
    30 
    30 
    31 #define MAX_NUM_AXES 6
    31 #define MAX_NUM_AXES 6
    32 #define MAX_NUM_HATS 2
    32 #define MAX_NUM_HATS 2
    33 
       
    34 static SDL_bool s_ForceQuit = SDL_FALSE;
       
    35 
    33 
    36 static void
    34 static void
    37 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    35 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    38 {
    36 {
    39     const SDL_Rect area = { x, y, w, h };
    37     const SDL_Rect area = { x, y, w, h };
    83         #undef BUTTON_CASE
    81         #undef BUTTON_CASE
    84         default: return "???";
    82         default: return "???";
    85     }
    83     }
    86 }
    84 }
    87 
    85 
    88 void
    86 SDL_bool
    89 WatchGameController(SDL_GameController * gamecontroller)
    87 WatchGameController(SDL_GameController * gamecontroller)
    90 {
    88 {
    91     const char *name = SDL_GameControllerName(gamecontroller);
    89     const char *name = SDL_GameControllerName(gamecontroller);
    92     const char *basetitle = "Game Controller Test: ";
    90     const char *basetitle = "Game Controller Test: ";
    93     const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
    91     const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
    94     char *title = (char *)SDL_malloc(titlelen);
    92     char *title = (char *)SDL_malloc(titlelen);
    95     SDL_Window *window = NULL;
    93     SDL_Window *window = NULL;
    96     SDL_Renderer *screen = NULL;
    94     SDL_Renderer *screen = NULL;
    97     int done = 0;
    95     SDL_bool retval = SDL_FALSE;
       
    96     SDL_bool done = SDL_FALSE;
    98     SDL_Event event;
    97     SDL_Event event;
    99     int i;
    98     int i;
   100 
    99 
   101     if (title) {
   100     if (title) {
   102         SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   101         SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   106     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
   105     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
   107                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   106                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   108                               SCREEN_HEIGHT, 0);
   107                               SCREEN_HEIGHT, 0);
   109     if (window == NULL) {
   108     if (window == NULL) {
   110         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   109         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   111         return;
   110         return SDL_FALSE;
   112     }
   111     }
   113 
   112 
   114     screen = SDL_CreateRenderer(window, -1, 0);
   113     screen = SDL_CreateRenderer(window, -1, 0);
   115     if (screen == NULL) {
   114     if (screen == NULL) {
   116         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   115         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   117         SDL_DestroyWindow(window);
   116         SDL_DestroyWindow(window);
   118         return;
   117         return SDL_FALSE;
   119     }
   118     }
   120 
   119 
   121     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   120     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   122     SDL_RenderClear(screen);
   121     SDL_RenderClear(screen);
   123     SDL_RenderPresent(screen);
   122     SDL_RenderPresent(screen);
   155                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   154                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   156                     break;
   155                     break;
   157                 }
   156                 }
   158                 /* Fall through to signal quit */
   157                 /* Fall through to signal quit */
   159             case SDL_QUIT:
   158             case SDL_QUIT:
   160                 done = 1;
   159                 done = SDL_TRUE;
   161                 s_ForceQuit = SDL_TRUE;
       
   162                 break;
   160                 break;
   163             default:
   161             default:
   164                 break;
   162                 break;
   165             }
   163             }
   166         }
   164         }
   198 
   196 
   199         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   197         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   200 
   198 
   201         SDL_RenderPresent(screen);
   199         SDL_RenderPresent(screen);
   202 
   200 
   203         if ( !done )
   201         if (!SDL_GameControllerGetAttached(gamecontroller)) {
   204             done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
   202             done = SDL_TRUE;
       
   203             retval = SDL_TRUE;  /* keep going, wait for reattach. */
       
   204         }
   205     }
   205     }
   206 
   206 
   207     SDL_DestroyRenderer(screen);
   207     SDL_DestroyRenderer(screen);
   208     SDL_DestroyWindow(window);
   208     SDL_DestroyWindow(window);
       
   209     return retval;
   209 }
   210 }
   210 
   211 
   211 int
   212 int
   212 main(int argc, char *argv[])
   213 main(int argc, char *argv[])
   213 {
   214 {
   248         SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
   249         SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
   249     }
   250     }
   250     SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
   251     SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
   251 
   252 
   252     if (argv[1]) {
   253     if (argv[1]) {
       
   254         SDL_bool reportederror = SDL_FALSE;
       
   255         SDL_bool keepGoing = SDL_TRUE;
       
   256         SDL_Event event;
   253         int device = atoi(argv[1]);
   257         int device = atoi(argv[1]);
   254         if (device >= SDL_NumJoysticks()) {
   258         if (device >= SDL_NumJoysticks()) {
   255 			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
   259 			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
   256             retcode = 1;
   260             retcode = 1;
   257         } else {
   261         } else {
   258             SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
   262             SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
   259                                       guid, sizeof (guid));
   263                                       guid, sizeof (guid));
   260             SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
   264             SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
   261             gamecontroller = SDL_GameControllerOpen(device);
   265             gamecontroller = SDL_GameControllerOpen(device);
   262             if (gamecontroller == NULL) {
   266             while (keepGoing) {
   263                 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open joystick %d: %s\n", device, SDL_GetError());
   267                 if (gamecontroller == NULL) {
   264                 retcode = 1;
   268                     if (!reportederror) {
   265             } else {
   269                         if (gamecontroller == NULL) {
   266                 WatchGameController(gamecontroller);
   270                             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
   267                 SDL_GameControllerClose(gamecontroller);
   271                             retcode = 1;
       
   272                         }
       
   273                         keepGoing = SDL_FALSE;
       
   274                         reportederror = SDL_TRUE;
       
   275                     }
       
   276                 } else {
       
   277                     reportederror = SDL_FALSE;
       
   278                     keepGoing = WatchGameController(gamecontroller);
       
   279                     SDL_GameControllerClose(gamecontroller);
       
   280                 }
       
   281 
       
   282                 gamecontroller = NULL;
       
   283                 if (keepGoing) {
       
   284                     SDL_Log("Waiting for attach\n");
       
   285                 }
       
   286                 while (keepGoing) {
       
   287                     SDL_WaitEvent(&event);
       
   288                     if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
       
   289                         || (event.type == SDL_MOUSEBUTTONDOWN)) {
       
   290                         keepGoing = SDL_FALSE;
       
   291                     } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
       
   292                         gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
       
   293                         break;
       
   294                     }
       
   295                 }
   268             }
   296             }
   269         }
   297         }
   270     }
   298     }
   271 
   299 
   272     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
   300     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);