test/controllermap.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 09 Feb 2014 15:20:41 -0500
changeset 8198 51935d107921
parent 8149 681eb46b8ac4
child 8200 17b801b31cd5
permissions -rw-r--r--
No need to use a render target here.
     1 /*
     2   Copyright (C) 1997-2014 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 /* Game controller mapping generator */
    14 /* Gabriel Jacobo <gabomdq@gmail.com> */
    15 
    16 #include <stdio.h>
    17 #include <stdlib.h>
    18 #include <string.h>
    19 
    20 #include "SDL.h"
    21 
    22 #ifndef SDL_JOYSTICK_DISABLED
    23 
    24 #ifdef __IPHONEOS__
    25 #define SCREEN_WIDTH    320
    26 #define SCREEN_HEIGHT   480
    27 #else
    28 #define SCREEN_WIDTH    512
    29 #define SCREEN_HEIGHT   317
    30 #endif
    31 
    32 #define MAP_WIDTH 512
    33 #define MAP_HEIGHT 317
    34 
    35 #define MARKER_BUTTON 1
    36 #define MARKER_AXIS 2
    37 
    38 typedef struct MappingStep
    39 {
    40     int x, y;
    41     double angle;
    42     int marker;
    43     char *field;
    44     int axis, button, hat, hat_value;
    45     char mapping[4096];
    46 }MappingStep;
    47 
    48 
    49 SDL_Texture *
    50 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    51 {
    52     SDL_Surface *temp;
    53     SDL_Texture *texture;
    54 
    55     /* Load the sprite image */
    56     temp = SDL_LoadBMP(file);
    57     if (temp == NULL) {
    58         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
    59         return NULL;
    60     }
    61 
    62     /* Set transparent pixel as the pixel at (0,0) */
    63     if (transparent) {
    64         if (temp->format->palette) {
    65             SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    66         } else {
    67             switch (temp->format->BitsPerPixel) {
    68             case 15:
    69                 SDL_SetColorKey(temp, SDL_TRUE,
    70                                 (*(Uint16 *) temp->pixels) & 0x00007FFF);
    71                 break;
    72             case 16:
    73                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    74                 break;
    75             case 24:
    76                 SDL_SetColorKey(temp, SDL_TRUE,
    77                                 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    78                 break;
    79             case 32:
    80                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    81                 break;
    82             }
    83         }
    84     }
    85 
    86     /* Create textures from the image */
    87     texture = SDL_CreateTextureFromSurface(renderer, temp);
    88     if (!texture) {
    89         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
    90         SDL_FreeSurface(temp);
    91         return NULL;
    92     }
    93     SDL_FreeSurface(temp);
    94 
    95     /* We're ready to roll. :) */
    96     return texture;
    97 }
    98 
    99 static void
   100 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
   101 {
   102     const SDL_Rect area = { x, y, w, h };
   103     SDL_RenderFillRect(r, &area);
   104 }
   105 
   106 static SDL_bool
   107 WatchJoystick(SDL_Joystick * joystick)
   108 {
   109     SDL_Window *window = NULL;
   110     SDL_Renderer *screen = NULL;
   111     SDL_Texture *background, *button, *axis, *marker;
   112     const char *name = NULL;
   113     SDL_bool retval = SDL_FALSE;
   114     SDL_bool done = SDL_FALSE, next=SDL_FALSE;
   115     SDL_Event event;
   116     SDL_Rect dst;
   117     int s, _s;
   118     Uint8 alpha=200, alpha_step = -1;
   119     Uint32 alpha_ticks;
   120     char mapping[4096], temp[4096];
   121     MappingStep *step;
   122     MappingStep steps[] = {
   123         {342, 132,  0.0,  MARKER_BUTTON, "x", -1, -1, -1, -1, ""},
   124         {387, 167,  0.0,  MARKER_BUTTON, "a", -1, -1, -1, -1, ""},
   125         {431, 132,  0.0,  MARKER_BUTTON, "b", -1, -1, -1, -1, ""},
   126         {389, 101,  0.0,  MARKER_BUTTON, "y", -1, -1, -1, -1, ""},
   127         {174, 132,  0.0,  MARKER_BUTTON, "back", -1, -1, -1, -1, ""},
   128         {233, 132,  0.0,  MARKER_BUTTON, "guide", -1, -1, -1, -1, ""},
   129         {289, 132,  0.0,  MARKER_BUTTON, "start", -1, -1, -1, -1, ""},        
   130         {116, 217,  0.0,  MARKER_BUTTON, "dpleft", -1, -1, -1, -1, ""},
   131         {154, 249,  0.0,  MARKER_BUTTON, "dpdown", -1, -1, -1, -1, ""},
   132         {186, 217,  0.0,  MARKER_BUTTON, "dpright", -1, -1, -1, -1, ""},
   133         {154, 188,  0.0,  MARKER_BUTTON, "dpup", -1, -1, -1, -1, ""},
   134         {77,  40,   0.0,  MARKER_BUTTON, "leftshoulder", -1, -1, -1, -1, ""},
   135         {91, 0,    0.0,  MARKER_BUTTON, "lefttrigger", -1, -1, -1, -1, ""},
   136         {396, 36,   0.0,  MARKER_BUTTON, "rightshoulder", -1, -1, -1, -1, ""},
   137         {375, 0,    0.0,  MARKER_BUTTON, "righttrigger", -1, -1, -1, -1, ""},
   138         {75,  154,  0.0,  MARKER_BUTTON, "leftstick", -1, -1, -1, -1, ""},
   139         {305, 230,  0.0,  MARKER_BUTTON, "rightstick", -1, -1, -1, -1, ""},
   140         {75,  154,  0.0,  MARKER_AXIS,   "leftx", -1, -1, -1, -1, ""},
   141         {75,  154,  90.0, MARKER_AXIS,   "lefty", -1, -1, -1, -1, ""},        
   142         {305, 230,  0.0,  MARKER_AXIS,   "rightx", -1, -1, -1, -1, ""},
   143         {305, 230,  90.0, MARKER_AXIS,   "righty", -1, -1, -1, -1, ""},
   144     };
   145 
   146     /* Create a window to display joystick axis position */
   147     window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
   148                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
   149                               SCREEN_HEIGHT, 0);
   150     if (window == NULL) {
   151         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
   152         return SDL_FALSE;
   153     }
   154 
   155     screen = SDL_CreateRenderer(window, -1, 0);
   156     if (screen == NULL) {
   157         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
   158         SDL_DestroyWindow(window);
   159         return SDL_FALSE;
   160     }
   161     
   162     background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
   163     button = LoadTexture(screen, "button.bmp", SDL_TRUE);
   164     axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
   165     SDL_RaiseWindow(window);
   166 
   167     /* Print info about the joystick we are watching */
   168     name = SDL_JoystickName(joystick);
   169     SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
   170            name ? name : "Unknown Joystick");
   171     SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
   172            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
   173            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
   174     
   175     SDL_Log("\n\n\
   176     ====================================================================================\n\
   177     Press the buttons on your controller when indicated\n\
   178     (Your controller may look different than the picture)\n\
   179     If you want to correct a mistake, press backspace or the back button on your device\n\
   180     To skip a button, press SPACE or click/touch the screen\n\
   181     To exit, press ESC\n\
   182     ====================================================================================\n");
   183     
   184     /* Initialize mapping with GUID and name */
   185     SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, SDL_arraysize(temp));
   186     SDL_snprintf(mapping, SDL_arraysize(mapping), "%s,%s,platform:%s,",
   187         temp, name ? name : "Unknown Joystick", SDL_GetPlatform());
   188 
   189     /* Loop, getting joystick events! */
   190     for(s=0; s<SDL_arraysize(steps) && !done;) {
   191         /* blank screen, set up for drawing this frame. */
   192         step = &steps[s];
   193         SDL_strlcpy(step->mapping, mapping, SDL_arraysize(step->mapping));
   194         step->axis = -1;
   195         step->button = -1;
   196         step->hat = -1;
   197         step->hat_value = -1;
   198         SDL_SetClipboardText("TESTING TESTING 123");
   199         
   200         switch(step->marker) {
   201             case MARKER_AXIS:
   202                 marker = axis;
   203                 break;
   204             case MARKER_BUTTON:
   205                 marker = button;
   206                 break;
   207             default:
   208                 break;
   209         }
   210         
   211         dst.x = step->x;
   212         dst.y = step->y;
   213         SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
   214         next=SDL_FALSE;
   215         
   216         while (!done && !next) {
   217             if (SDL_GetTicks() - alpha_ticks > 5) {
   218                 alpha_ticks = SDL_GetTicks();
   219                 alpha += alpha_step;
   220                 if (alpha == 255) {
   221                     alpha_step = -1;
   222                 }
   223                 if (alpha < 128) {
   224                     alpha_step = 1;
   225                 }
   226             }
   227             
   228             SDL_RenderCopy(screen, background, NULL, NULL);
   229             SDL_SetTextureAlphaMod(marker, alpha);
   230             SDL_SetTextureColorMod(marker, 10, 255, 21);
   231             SDL_RenderCopyEx(screen, marker, NULL, &dst, step->angle, NULL, 0);
   232             SDL_RenderPresent(screen);
   233             
   234             if (SDL_PollEvent(&event)) {
   235                 switch (event.type) {
   236                 case SDL_JOYAXISMOTION:
   237                     if (event.jaxis.value > 20000 || event.jaxis.value < -20000) {
   238                         for (_s = 0; _s < s; _s++) {
   239                             if (steps[_s].axis == event.jaxis.axis) {
   240                                 break;
   241                             }
   242                         }
   243                         if (_s == s) {
   244                             step->axis = event.jaxis.axis;
   245                             SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   246                             SDL_snprintf(temp, SDL_arraysize(temp), ":a%u,", event.jaxis.axis);
   247                             SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   248                             s++;
   249                             next=SDL_TRUE;
   250                         }
   251                     }
   252                     
   253                     break;
   254                 case SDL_JOYHATMOTION:
   255                         for (_s = 0; _s < s; _s++) {
   256                             if (steps[_s].hat == event.jhat.hat && steps[_s].hat_value == event.jhat.value) {
   257                                 break;
   258                             }
   259                         }
   260                         if (_s == s) {
   261                             step->hat = event.jhat.hat;
   262                             step->hat_value = event.jhat.value;
   263                             SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   264                             SDL_snprintf(temp, SDL_arraysize(temp), ":h%u.%u,", event.jhat.hat, event.jhat.value );
   265                             SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   266                             s++;
   267                             next=SDL_TRUE;
   268                         }
   269                     break;
   270                 case SDL_JOYBALLMOTION:
   271                     break;
   272                 case SDL_JOYBUTTONUP:
   273                     for (_s = 0; _s < s; _s++) {
   274                         if (steps[_s].button == event.jbutton.button) {
   275                             break;
   276                         }
   277                     }
   278                     if (_s == s) {
   279                         step->button = event.jbutton.button;
   280                         SDL_strlcat(mapping, step->field, SDL_arraysize(mapping));
   281                         SDL_snprintf(temp, SDL_arraysize(temp), ":b%u,", event.jbutton.button);
   282                         SDL_strlcat(mapping, temp, SDL_arraysize(mapping));
   283                         s++;
   284                         next=SDL_TRUE;
   285                     }
   286                     break;
   287                 case SDL_FINGERDOWN:
   288                 case SDL_MOUSEBUTTONDOWN:
   289                     /* Skip this step */
   290                     s++;
   291                     next=SDL_TRUE;
   292                     break;
   293                 case SDL_KEYDOWN:
   294                     if (event.key.keysym.sym == SDLK_BACKSPACE || event.key.keysym.sym == SDLK_AC_BACK) {
   295                         /* Undo! */
   296                         if (s > 0) {
   297                             SDL_strlcpy(mapping, step->mapping, SDL_arraysize(step->mapping));
   298                             s--;
   299                             next = SDL_TRUE;
   300                         }
   301                         break;
   302                     }
   303                     if (event.key.keysym.sym == SDLK_SPACE) {
   304                         /* Skip this step */
   305                         s++;
   306                         next=SDL_TRUE;
   307                         break;
   308                     }
   309                     
   310                     if ((event.key.keysym.sym != SDLK_ESCAPE)) {
   311                         break;
   312                     }
   313                     /* Fall through to signal quit */
   314                 case SDL_QUIT:
   315                     done = SDL_TRUE;
   316                     break;
   317                 default:
   318                     break;
   319                 }
   320             }
   321         }
   322 
   323     }
   324 
   325     if (s == SDL_arraysize(steps) ) {
   326         SDL_Log("Mapping:\n\n%s\n\n", mapping);
   327         /* Print to stdout as well so the user can cat the output somewhere */
   328         printf("%s\n", mapping);
   329     }
   330     
   331     while(SDL_PollEvent(&event)) {};
   332     
   333     SDL_DestroyRenderer(screen);
   334     SDL_DestroyWindow(window);
   335     return retval;
   336 }
   337 
   338 int
   339 main(int argc, char *argv[])
   340 {
   341     const char *name;
   342     int i;
   343     SDL_Joystick *joystick;
   344 
   345     /* Enable standard application logging */
   346     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);	
   347 
   348     /* Initialize SDL (Note: video is required to start event loop) */
   349     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   350         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   351         exit(1);
   352     }
   353 
   354     /* Print information about the joysticks */
   355     SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
   356     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   357         name = SDL_JoystickNameForIndex(i);
   358         SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   359         joystick = SDL_JoystickOpen(i);
   360         if (joystick == NULL) {
   361             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
   362                     SDL_GetError());
   363         } else {
   364             char guid[64];
   365             SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
   366                                       guid, sizeof (guid));
   367             SDL_Log("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   368             SDL_Log("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   369             SDL_Log("       hats: %d\n", SDL_JoystickNumHats(joystick));
   370             SDL_Log("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   371             SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   372             SDL_Log("       guid: %s\n", guid);
   373             SDL_JoystickClose(joystick);
   374         }
   375     }
   376 
   377 #ifdef ANDROID
   378     if (SDL_NumJoysticks() > 0) {
   379 #else
   380     if (argv[1]) {
   381 #endif
   382         SDL_bool reportederror = SDL_FALSE;
   383         SDL_bool keepGoing = SDL_TRUE;
   384         SDL_Event event;
   385         int device;
   386 #ifdef ANDROID
   387         device = 0;
   388 #else
   389         device = atoi(argv[1]);
   390 #endif
   391         joystick = SDL_JoystickOpen(device);
   392 
   393         while ( keepGoing ) {
   394             if (joystick == NULL) {
   395                 if ( !reportederror ) {
   396                     SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   397                     keepGoing = SDL_FALSE;
   398                     reportederror = SDL_TRUE;
   399                 }
   400             } else {
   401                 reportederror = SDL_FALSE;
   402                 keepGoing = WatchJoystick(joystick);
   403                 SDL_JoystickClose(joystick);
   404             }
   405 
   406             joystick = NULL;
   407             if (keepGoing) {
   408                 SDL_Log("Waiting for attach\n");
   409             }
   410             while (keepGoing) {
   411                 SDL_WaitEvent(&event);
   412                 if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
   413                     || (event.type == SDL_MOUSEBUTTONDOWN)) {
   414                     keepGoing = SDL_FALSE;
   415                 } else if (event.type == SDL_JOYDEVICEADDED) {
   416                     joystick = SDL_JoystickOpen(device);
   417                     break;
   418                 }
   419             }
   420         }
   421     }
   422     else {
   423         SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
   424     }
   425     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   426 
   427     return 0;
   428 }
   429 
   430 #else
   431 
   432 int
   433 main(int argc, char *argv[])
   434 {
   435     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
   436     exit(1);
   437 }
   438 
   439 #endif