test/testgamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 23 Dec 2013 15:55:08 -0800
changeset 8068 54fcab720079
parent 8043 74fcbf6987ba
child 8149 681eb46b8ac4
permissions -rw-r--r--
Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
     1 /*
     2   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple program to test the SDL game controller routines */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <string.h>
    18 
    19 #include "SDL.h"
    20 
    21 #ifndef SDL_JOYSTICK_DISABLED
    22 
    23 #ifdef __IPHONEOS__
    24 #define SCREEN_WIDTH    320
    25 #define SCREEN_HEIGHT    480
    26 #else
    27 #define SCREEN_WIDTH    640
    28 #define SCREEN_HEIGHT    480
    29 #endif
    30 
    31 #define MAX_NUM_AXES 6
    32 #define MAX_NUM_HATS 2
    33 
    34 static void
    35 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    36 {
    37     const SDL_Rect area = { x, y, w, h };
    38     SDL_RenderFillRect(r, &area);
    39 }
    40 
    41 static const char *
    42 ControllerAxisName(const SDL_GameControllerAxis axis)
    43 {
    44     switch (axis)
    45     {
    46         #define AXIS_CASE(ax) case SDL_CONTROLLER_AXIS_##ax: return #ax
    47         AXIS_CASE(INVALID);
    48         AXIS_CASE(LEFTX);
    49         AXIS_CASE(LEFTY);
    50         AXIS_CASE(RIGHTX);
    51         AXIS_CASE(RIGHTY);
    52         AXIS_CASE(TRIGGERLEFT);
    53         AXIS_CASE(TRIGGERRIGHT);
    54         #undef AXIS_CASE
    55         default: return "???";
    56     }
    57 }
    58 
    59 static const char *
    60 ControllerButtonName(const SDL_GameControllerButton button)
    61 {
    62     switch (button)
    63     {
    64         #define BUTTON_CASE(btn) case SDL_CONTROLLER_BUTTON_##btn: return #btn
    65         BUTTON_CASE(INVALID);
    66         BUTTON_CASE(A);
    67         BUTTON_CASE(B);
    68         BUTTON_CASE(X);
    69         BUTTON_CASE(Y);
    70         BUTTON_CASE(BACK);
    71         BUTTON_CASE(GUIDE);
    72         BUTTON_CASE(START);
    73         BUTTON_CASE(LEFTSTICK);
    74         BUTTON_CASE(RIGHTSTICK);
    75         BUTTON_CASE(LEFTSHOULDER);
    76         BUTTON_CASE(RIGHTSHOULDER);
    77         BUTTON_CASE(DPAD_UP);
    78         BUTTON_CASE(DPAD_DOWN);
    79         BUTTON_CASE(DPAD_LEFT);
    80         BUTTON_CASE(DPAD_RIGHT);
    81         #undef BUTTON_CASE
    82         default: return "???";
    83     }
    84 }
    85 
    86 SDL_bool
    87 WatchGameController(SDL_GameController * gamecontroller)
    88 {
    89     const char *name = SDL_GameControllerName(gamecontroller);
    90     const char *basetitle = "Game Controller Test: ";
    91     const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
    92     char *title = (char *)SDL_malloc(titlelen);
    93     SDL_Window *window = NULL;
    94     SDL_Renderer *screen = NULL;
    95     SDL_bool retval = SDL_FALSE;
    96     SDL_bool done = SDL_FALSE;
    97     SDL_Event event;
    98     int i;
    99 
   100     if (title) {
   101         SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   102     }
   103 
   104     /* Create a window to display controller axis position */
   105     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
   106                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   107                               SCREEN_HEIGHT, 0);
   108     if (window == NULL) {
   109         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   110         return SDL_FALSE;
   111     }
   112 
   113     screen = SDL_CreateRenderer(window, -1, 0);
   114     if (screen == NULL) {
   115         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   116         SDL_DestroyWindow(window);
   117         return SDL_FALSE;
   118     }
   119 
   120     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   121     SDL_RenderClear(screen);
   122     SDL_RenderPresent(screen);
   123     SDL_RaiseWindow(window);
   124 
   125     /* Print info about the controller we are watching */
   126     SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
   127 
   128     /* Loop, getting controller events! */
   129     while (!done) {
   130         /* blank screen, set up for drawing this frame. */
   131         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   132         SDL_RenderClear(screen);
   133 
   134         while (SDL_PollEvent(&event)) {
   135             switch (event.type) {
   136             case SDL_CONTROLLERAXISMOTION:
   137                 SDL_Log("Controller %d axis %d ('%s') value: %d\n",
   138                        event.caxis.which,
   139                        event.caxis.axis,
   140                        ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
   141                        event.caxis.value);
   142                 break;
   143             case SDL_CONTROLLERBUTTONDOWN:
   144                 SDL_Log("Controller %d button %d ('%s') down\n",
   145                        event.cbutton.which, event.cbutton.button,
   146                        ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
   147                 break;
   148             case SDL_CONTROLLERBUTTONUP:
   149                 SDL_Log("Controller %d button %d ('%s') up\n",
   150                        event.cbutton.which, event.cbutton.button,
   151                        ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
   152                 break;
   153             case SDL_KEYDOWN:
   154                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   155                     break;
   156                 }
   157                 /* Fall through to signal quit */
   158             case SDL_QUIT:
   159                 done = SDL_TRUE;
   160                 break;
   161             default:
   162                 break;
   163             }
   164         }
   165         /* Update visual controller state */
   166         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   167         for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
   168             if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
   169                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   170             }
   171         }
   172 
   173         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   174         for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
   175             /* Draw the X/Y axis */
   176             int x, y;
   177             x = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 0))) + 32768);
   178             x *= SCREEN_WIDTH;
   179             x /= 65535;
   180             if (x < 0) {
   181                 x = 0;
   182             } else if (x > (SCREEN_WIDTH - 16)) {
   183                 x = SCREEN_WIDTH - 16;
   184             }
   185             y = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 1))) + 32768);
   186             y *= SCREEN_HEIGHT;
   187             y /= 65535;
   188             if (y < 0) {
   189                 y = 0;
   190             } else if (y > (SCREEN_HEIGHT - 16)) {
   191                 y = SCREEN_HEIGHT - 16;
   192             }
   193 
   194             DrawRect(screen, x, y, 16, 16);
   195         }
   196 
   197         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   198 
   199         SDL_RenderPresent(screen);
   200 
   201         if (!SDL_GameControllerGetAttached(gamecontroller)) {
   202             done = SDL_TRUE;
   203             retval = SDL_TRUE;  /* keep going, wait for reattach. */
   204         }
   205     }
   206 
   207     SDL_DestroyRenderer(screen);
   208     SDL_DestroyWindow(window);
   209     return retval;
   210 }
   211 
   212 int
   213 main(int argc, char *argv[])
   214 {
   215     int i;
   216     int nController = 0;
   217     int retcode = 0;
   218     char guid[64];
   219     SDL_GameController *gamecontroller;
   220 
   221     /* Enable standard application logging */
   222 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   223 
   224     /* Initialize SDL (Note: video is required to start event loop) */
   225     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   226         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   227         return 1;
   228     }
   229     
   230     SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
   231 
   232     /* Print information about the controller */
   233     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   234         const char *name;
   235         const char *description;
   236 
   237         SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
   238                                   guid, sizeof (guid));
   239 
   240         if ( SDL_IsGameController(i) )
   241         {
   242             nController++;
   243             name = SDL_GameControllerNameForIndex(i);
   244             description = "Controller";
   245         } else {
   246             name = SDL_JoystickNameForIndex(i);
   247             description = "Joystick";
   248         }
   249         SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
   250     }
   251     SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
   252 
   253     if (argv[1]) {
   254         SDL_bool reportederror = SDL_FALSE;
   255         SDL_bool keepGoing = SDL_TRUE;
   256         SDL_Event event;
   257         int device = atoi(argv[1]);
   258         if (device >= SDL_NumJoysticks()) {
   259 			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
   260             retcode = 1;
   261         } else {
   262             SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
   263                                       guid, sizeof (guid));
   264             SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
   265             gamecontroller = SDL_GameControllerOpen(device);
   266             while (keepGoing) {
   267                 if (gamecontroller == NULL) {
   268                     if (!reportederror) {
   269                         if (gamecontroller == NULL) {
   270                             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
   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                 }
   296             }
   297         }
   298     }
   299 
   300     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
   301 
   302     return retcode;
   303 }
   304 
   305 #else
   306 
   307 int
   308 main(int argc, char *argv[])
   309 {
   310     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   311     exit(1);
   312 }
   313 
   314 #endif