test/testgamecontroller.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 14 Nov 2015 12:35:45 -0500
changeset 9916 9429bc102632
parent 9876 1496e502e51d
child 9922 b066d5fd5fa7
permissions -rw-r--r--
Added SDL_JoystickFromInstanceID() and SDL_GameControllerFromInstanceID().
     1 /*
     2   Copyright (C) 1997-2015 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 #ifdef __EMSCRIPTEN__
    22 #include <emscripten/emscripten.h>
    23 #endif
    24 
    25 #ifndef SDL_JOYSTICK_DISABLED
    26 
    27 #ifdef __IPHONEOS__
    28 #define SCREEN_WIDTH    480
    29 #define SCREEN_HEIGHT    320
    30 #else
    31 #define SCREEN_WIDTH    512
    32 #define SCREEN_HEIGHT   317
    33 #endif
    34 
    35 /* This is indexed by SDL_GameControllerButton. */
    36 static const struct { int x; int y; } button_positions[] = {
    37     {387, 167},  /* A */
    38     {431, 132},  /* B */
    39     {342, 132},  /* X */
    40     {389, 101},  /* Y */
    41     {174, 132},  /* BACK */
    42     {233, 132},  /* GUIDE */
    43     {289, 132},  /* START */
    44     {75,  154},  /* LEFTSTICK */
    45     {305, 230},  /* RIGHTSTICK */
    46     {77,  40},   /* LEFTSHOULDER */
    47     {396, 36},   /* RIGHTSHOULDER */
    48     {154, 188},  /* DPAD_UP */
    49     {154, 249},  /* DPAD_DOWN */
    50     {116, 217},  /* DPAD_LEFT */
    51     {186, 217},  /* DPAD_RIGHT */
    52 };
    53 
    54 /* This is indexed by SDL_GameControllerAxis. */
    55 static const struct { int x; int y; double angle; } axis_positions[] = {
    56     {75,  154, 0.0},  /* LEFTX */
    57     {75,  154, 90.0},  /* LEFTY */
    58     {305, 230, 0.0},  /* RIGHTX */
    59     {305, 230, 90.0},  /* RIGHTY */
    60     {91, 0, 90.0},     /* TRIGGERLEFT */
    61     {375, 0, 90.0},    /* TRIGGERRIGHT */
    62 };
    63 
    64 SDL_Renderer *screen = NULL;
    65 SDL_bool retval = SDL_FALSE;
    66 SDL_bool done = SDL_FALSE;
    67 SDL_Texture *background, *button, *axis;
    68 
    69 static SDL_Texture *
    70 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    71 {
    72     SDL_Surface *temp = NULL;
    73     SDL_Texture *texture = NULL;
    74 
    75     temp = SDL_LoadBMP(file);
    76     if (temp == NULL) {
    77         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
    78     } else {
    79         /* Set transparent pixel as the pixel at (0,0) */
    80         if (transparent) {
    81             if (temp->format->BytesPerPixel == 1) {
    82                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
    83             } else {
    84                 SDL_assert(!temp->format->palette);
    85                 SDL_assert(temp->format->BitsPerPixel == 24);
    86                 SDL_SetColorKey(temp, SDL_TRUE, (*(Uint32 *)temp->pixels) & 0x00FFFFFF);
    87             }
    88         }
    89 
    90         texture = SDL_CreateTextureFromSurface(renderer, temp);
    91         if (!texture) {
    92             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
    93         }
    94     }
    95     if (temp) {
    96         SDL_FreeSurface(temp);
    97     }
    98     return texture;
    99 }
   100 
   101 void
   102 loop(void *arg)
   103 {
   104     SDL_Event event;
   105     int i;
   106     SDL_GameController *gamecontroller = (SDL_GameController *)arg;
   107 
   108     /* blank screen, set up for drawing this frame. */
   109     SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   110     SDL_RenderClear(screen);
   111     SDL_RenderCopy(screen, background, NULL, NULL);
   112 
   113     while (SDL_PollEvent(&event)) {
   114         switch (event.type) {
   115         case SDL_KEYDOWN:
   116             if (event.key.keysym.sym != SDLK_ESCAPE) {
   117                 break;
   118             }
   119             /* Fall through to signal quit */
   120         case SDL_QUIT:
   121             done = SDL_TRUE;
   122             break;
   123         default:
   124             break;
   125         }
   126     }
   127 
   128     /* Update visual controller state */
   129     for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
   130         if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
   131             const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
   132             SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, 0);
   133         }
   134     }
   135 
   136     for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
   137         const Sint16 deadzone = 8000;  /* !!! FIXME: real deadzone */
   138         const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
   139         if (value < -deadzone) {
   140             const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   141             const double angle = axis_positions[i].angle;
   142             SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   143         } else if (value > deadzone) {
   144             const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
   145             const double angle = axis_positions[i].angle + 180.0;
   146             SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, 0);
   147         }
   148     }
   149 
   150     SDL_RenderPresent(screen);
   151 
   152     if (!SDL_GameControllerGetAttached(gamecontroller)) {
   153         done = SDL_TRUE;
   154         retval = SDL_TRUE;  /* keep going, wait for reattach. */
   155     }
   156 
   157 #ifdef __EMSCRIPTEN__
   158     if (done) {
   159         emscripten_cancel_main_loop();
   160     }
   161 #endif
   162 }
   163 
   164 SDL_bool
   165 WatchGameController(SDL_GameController * gamecontroller)
   166 {
   167     const char *name = SDL_GameControllerName(gamecontroller);
   168     const char *basetitle = "Game Controller Test: ";
   169     const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
   170     char *title = (char *)SDL_malloc(titlelen);
   171     SDL_Window *window = NULL;
   172 
   173     retval = SDL_FALSE;
   174     done = SDL_FALSE;
   175 
   176     if (title) {
   177         SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
   178     }
   179 
   180     /* Create a window to display controller state */
   181     window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
   182                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   183                               SCREEN_HEIGHT, 0);
   184     if (window == NULL) {
   185         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   186         return SDL_FALSE;
   187     }
   188 
   189     screen = SDL_CreateRenderer(window, -1, 0);
   190     if (screen == NULL) {
   191         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   192         SDL_DestroyWindow(window);
   193         return SDL_FALSE;
   194     }
   195 
   196     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   197     SDL_RenderClear(screen);
   198     SDL_RenderPresent(screen);
   199     SDL_RaiseWindow(window);
   200 
   201     /* scale for platforms that don't give you the window size you asked for. */
   202     SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
   203 
   204     background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
   205     button = LoadTexture(screen, "button.bmp", SDL_TRUE);
   206     axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
   207 
   208     if (!background || !button || !axis) {
   209         SDL_DestroyRenderer(screen);
   210         SDL_DestroyWindow(window);
   211         return SDL_FALSE;
   212     }
   213     SDL_SetTextureColorMod(button, 10, 255, 21);
   214     SDL_SetTextureColorMod(axis, 10, 255, 21);
   215 
   216     /* !!! FIXME: */
   217     /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
   218 
   219     /* Print info about the controller we are watching */
   220     SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
   221 
   222     /* Loop, getting controller events! */
   223 #ifdef __EMSCRIPTEN__
   224     emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
   225 #else
   226     while (!done) {
   227         loop(gamecontroller);
   228     }
   229 #endif
   230 
   231     SDL_DestroyRenderer(screen);
   232     screen = NULL;
   233     background = NULL;
   234     button = NULL;
   235     axis = NULL;
   236     SDL_DestroyWindow(window);
   237     return retval;
   238 }
   239 
   240 int
   241 main(int argc, char *argv[])
   242 {
   243     int i;
   244     int nController = 0;
   245     int retcode = 0;
   246     char guid[64];
   247     SDL_GameController *gamecontroller;
   248 
   249     /* Enable standard application logging */
   250 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   251 
   252     /* Initialize SDL (Note: video is required to start event loop) */
   253     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   254         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   255         return 1;
   256     }
   257     
   258     SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
   259 
   260     /* Print information about the controller */
   261     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   262         const char *name;
   263         const char *description;
   264 
   265         SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
   266                                   guid, sizeof (guid));
   267 
   268         if ( SDL_IsGameController(i) )
   269         {
   270             nController++;
   271             name = SDL_GameControllerNameForIndex(i);
   272             description = "Controller";
   273         } else {
   274             name = SDL_JoystickNameForIndex(i);
   275             description = "Joystick";
   276         }
   277         SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
   278     }
   279     SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
   280 
   281     if (argv[1]) {
   282         SDL_bool reportederror = SDL_FALSE;
   283         SDL_bool keepGoing = SDL_TRUE;
   284         SDL_Event event;
   285         int device = atoi(argv[1]);
   286         if (device >= SDL_NumJoysticks()) {
   287 			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
   288             retcode = 1;
   289         } else {
   290             SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
   291                                       guid, sizeof (guid));
   292             SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
   293             gamecontroller = SDL_GameControllerOpen(device);
   294 
   295             if (gamecontroller != NULL) {
   296                 SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
   297             }
   298 
   299             while (keepGoing) {
   300                 if (gamecontroller == NULL) {
   301                     if (!reportederror) {
   302                         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
   303                         retcode = 1;
   304                         keepGoing = SDL_FALSE;
   305                         reportederror = SDL_TRUE;
   306                     }
   307                 } else {
   308                     reportederror = SDL_FALSE;
   309                     keepGoing = WatchGameController(gamecontroller);
   310                     SDL_GameControllerClose(gamecontroller);
   311                 }
   312 
   313                 gamecontroller = NULL;
   314                 if (keepGoing) {
   315                     SDL_Log("Waiting for attach\n");
   316                 }
   317                 while (keepGoing) {
   318                     SDL_WaitEvent(&event);
   319                     if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
   320                         || (event.type == SDL_MOUSEBUTTONDOWN)) {
   321                         keepGoing = SDL_FALSE;
   322                     } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
   323                         gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
   324                         if (gamecontroller != NULL) {
   325                             SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
   326                         }
   327                         break;
   328                     }
   329                 }
   330             }
   331         }
   332     }
   333 
   334     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
   335 
   336     return retcode;
   337 }
   338 
   339 #else
   340 
   341 int
   342 main(int argc, char *argv[])
   343 {
   344     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   345     exit(1);
   346 }
   347 
   348 #endif