Fixed bug 1759 - Updates to testjoystick.c
authorSam Lantinga <slouken@libsdl.org>
Tue, 19 Mar 2013 22:25:02 -0700
changeset 70177c2eb015a6d7
parent 7016 24edf0189bc5
child 7018 9cef1005df5f
Fixed bug 1759 - Updates to testjoystick.c

ny00

A minor patch is attached, with the following few changes to testjoystick.c:
- Unused constant definitions have been removed.
- Output for all analog axes is drawn, even when there is an odd number of axes. (I have a controller with 5 analog axes.)
- Buttons are now drawn on two rows, so there's room for more. In fact, it has been used for testing a proposed joystick patch for Android, where large button ID numbers have been involved (20 and up). For more details see http://bugzilla.libsdl.org/show_bug.cgi?id=1700.
- A few adaptations have been done for the Android platform, assuming joystick support is ever applied to it. One of them is that the very first joystick (in the enumeration of all joysticks) is opened for testing, if there is any.
- It is now possible to quit from the calibration by pressing on a mouse button, tapping on a touchscreen or pressing/tapping on the "Back" button of an Android device. Technically, a press on a key identified by key code SDLK_AC_BACK results in that.
test/testjoystick.c
     1.1 --- a/test/testjoystick.c	Tue Mar 19 22:15:07 2013 -0700
     1.2 +++ b/test/testjoystick.c	Tue Mar 19 22:25:02 2013 -0700
     1.3 @@ -28,9 +28,6 @@
     1.4  #define SCREEN_HEIGHT	480
     1.5  #endif
     1.6  
     1.7 -#define MAX_NUM_AXES 6
     1.8 -#define MAX_NUM_HATS 2
     1.9 -
    1.10  
    1.11  static void
    1.12  DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    1.13 @@ -121,10 +118,13 @@
    1.14                         event.jbutton.which, event.jbutton.button);
    1.15                  break;
    1.16              case SDL_KEYDOWN:
    1.17 -                if (event.key.keysym.sym != SDLK_ESCAPE) {
    1.18 +                if ((event.key.keysym.sym != SDLK_ESCAPE) &&
    1.19 +                    (event.key.keysym.sym != SDLK_AC_BACK)) {
    1.20                      break;
    1.21                  }
    1.22                  /* Fall through to signal quit */
    1.23 +            case SDL_FINGERDOWN:
    1.24 +            case SDL_MOUSEBUTTONDOWN:
    1.25              case SDL_QUIT:
    1.26                  done = SDL_TRUE;
    1.27                  break;
    1.28 @@ -136,15 +136,15 @@
    1.29          SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
    1.30          for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
    1.31              if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
    1.32 -                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
    1.33 +                DrawRect(screen, (i%20) * 34, SCREEN_HEIGHT - 68 + (i/20) * 34, 32, 32);
    1.34              }
    1.35          }
    1.36  
    1.37          SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.38 -        for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
    1.39 +        for (i = 0; i < SDL_JoystickNumAxes(joystick); ++i) {
    1.40              /* Draw the X/Y axis */
    1.41              int x, y;
    1.42 -            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
    1.43 +            x = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
    1.44              x *= SCREEN_WIDTH;
    1.45              x /= 65535;
    1.46              if (x < 0) {
    1.47 @@ -152,7 +152,12 @@
    1.48              } else if (x > (SCREEN_WIDTH - 16)) {
    1.49                  x = SCREEN_WIDTH - 16;
    1.50              }
    1.51 -            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
    1.52 +            ++i;
    1.53 +            if (i < SDL_JoystickNumAxes(joystick)) {
    1.54 +                y = (((int) SDL_JoystickGetAxis(joystick, i)) + 32768);
    1.55 +            } else {
    1.56 +                y = 32768;
    1.57 +            }
    1.58              y *= SCREEN_HEIGHT;
    1.59              y /= 65535;
    1.60              if (y < 0) {
    1.61 @@ -235,11 +240,19 @@
    1.62          }
    1.63      }
    1.64  
    1.65 +#ifdef ANDROID
    1.66 +    if (SDL_NumJoysticks() > 0) {
    1.67 +#else
    1.68      if (argv[1]) {
    1.69 +#endif
    1.70          SDL_bool reportederror = SDL_FALSE;
    1.71          SDL_bool keepGoing = SDL_TRUE;
    1.72          SDL_Event event;
    1.73 +#ifdef ANDROID
    1.74 +        joystick = SDL_JoystickOpen(0);
    1.75 +#else
    1.76          joystick = SDL_JoystickOpen(atoi(argv[1]));
    1.77 +#endif
    1.78          while ( keepGoing ) {
    1.79              if (joystick == NULL) {
    1.80                  if ( !reportederror ) {
    1.81 @@ -259,7 +272,8 @@
    1.82              }
    1.83              while (keepGoing) {
    1.84                  SDL_WaitEvent(&event);
    1.85 -                if (event.type == SDL_QUIT) {
    1.86 +                if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
    1.87 +                    || (event.type == SDL_MOUSEBUTTONDOWN)) {
    1.88                      keepGoing = SDL_FALSE;
    1.89                  } else if (event.type == SDL_JOYDEVICEADDED) {
    1.90                      joystick = SDL_JoystickOpen(atoi(argv[1]));
    1.91 @@ -270,7 +284,11 @@
    1.92      }
    1.93      SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    1.94  
    1.95 +#ifdef ANDROID
    1.96 +    exit(0);
    1.97 +#else
    1.98      return 0;
    1.99 +#endif
   1.100  }
   1.101  
   1.102  #else