test/testjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 26 Nov 2012 16:37:54 -0800
changeset 6690 9548c8a58103
parent 6586 ab57cc69dcc0
child 6730 36158e4061f4
permissions -rw-r--r--
Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
     1 /*
     2   Copyright (C) 1997-2011 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 joystick routines */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <string.h>
    18 
    19 #include "SDL.h"
    20 
    21 #ifdef __IPHONEOS__
    22 #define SCREEN_WIDTH	320
    23 #define SCREEN_HEIGHT	480
    24 #else
    25 #define SCREEN_WIDTH	640
    26 #define SCREEN_HEIGHT	480
    27 #endif
    28 
    29 #define MAX_NUM_AXES 6
    30 #define MAX_NUM_HATS 2
    31 
    32 static void
    33 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    34 {
    35     const SDL_Rect area = { x, y, w, h };
    36     SDL_RenderFillRect(r, &area);
    37 }
    38 
    39 void
    40 WatchJoystick(SDL_Joystick * joystick)
    41 {
    42     SDL_Window *window = NULL;
    43     SDL_Renderer *screen = NULL;
    44     const char *name = NULL;
    45     int done = 0;
    46     SDL_Event event;
    47     int i;
    48 
    49     /* Create a window to display joystick axis position */
    50     window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    51                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    52                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    53     if (window == NULL) {
    54         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    55         return;
    56     }
    57 
    58     screen = SDL_CreateRenderer(window, -1, 0);
    59     if (screen == NULL) {
    60         fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    61         SDL_DestroyWindow(window);
    62         return;
    63     }
    64 
    65     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    66     SDL_RenderClear(screen);
    67     SDL_RenderPresent(screen);
    68 	SDL_RaiseWindow(window);
    69 
    70     /* Print info about the joystick we are watching */
    71     name = SDL_JoystickName(joystick);
    72     printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
    73            name ? name : "Unknown Joystick");
    74     printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    75            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    76            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    77 
    78     /* Loop, getting joystick events! */
    79     while (!done) {
    80         /* blank screen, set up for drawing this frame. */
    81         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    82         SDL_RenderClear(screen);
    83 
    84         while (SDL_PollEvent(&event)) {
    85             switch (event.type) {
    86             case SDL_JOYAXISMOTION:
    87                 printf("Joystick %d axis %d value: %d\n",
    88                        event.jaxis.which,
    89                        event.jaxis.axis, event.jaxis.value);
    90                 break;
    91             case SDL_JOYHATMOTION:
    92                 printf("Joystick %d hat %d value:",
    93                        event.jhat.which, event.jhat.hat);
    94                 if (event.jhat.value == SDL_HAT_CENTERED)
    95                     printf(" centered");
    96                 if (event.jhat.value & SDL_HAT_UP)
    97                     printf(" up");
    98                 if (event.jhat.value & SDL_HAT_RIGHT)
    99                     printf(" right");
   100                 if (event.jhat.value & SDL_HAT_DOWN)
   101                     printf(" down");
   102                 if (event.jhat.value & SDL_HAT_LEFT)
   103                     printf(" left");
   104                 printf("\n");
   105                 break;
   106             case SDL_JOYBALLMOTION:
   107                 printf("Joystick %d ball %d delta: (%d,%d)\n",
   108                        event.jball.which,
   109                        event.jball.ball, event.jball.xrel, event.jball.yrel);
   110                 break;
   111             case SDL_JOYBUTTONDOWN:
   112                 printf("Joystick %d button %d down\n",
   113                        event.jbutton.which, event.jbutton.button);
   114                 break;
   115             case SDL_JOYBUTTONUP:
   116                 printf("Joystick %d button %d up\n",
   117                        event.jbutton.which, event.jbutton.button);
   118                 break;
   119             case SDL_KEYDOWN:
   120                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   121                     break;
   122                 }
   123                 /* Fall through to signal quit */
   124             case SDL_QUIT:
   125                 done = 1;
   126                 break;
   127             default:
   128                 break;
   129             }
   130         }
   131         /* Update visual joystick state */
   132         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   133         for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   134             if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   135                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   136             }
   137         }
   138 
   139         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   140         for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   141             /* Draw the X/Y axis */
   142             int x, y;
   143             x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   144             x *= SCREEN_WIDTH;
   145             x /= 65535;
   146             if (x < 0) {
   147                 x = 0;
   148             } else if (x > (SCREEN_WIDTH - 16)) {
   149                 x = SCREEN_WIDTH - 16;
   150             }
   151             y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   152             y *= SCREEN_HEIGHT;
   153             y /= 65535;
   154             if (y < 0) {
   155                 y = 0;
   156             } else if (y > (SCREEN_HEIGHT - 16)) {
   157                 y = SCREEN_HEIGHT - 16;
   158             }
   159 
   160             DrawRect(screen, x, y, 16, 16);
   161         }
   162 
   163         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   164         for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   165             /* Derive the new position */
   166             int x = SCREEN_WIDTH/2;
   167             int y = SCREEN_HEIGHT/2;
   168             const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   169 
   170             if (hat_pos & SDL_HAT_UP) {
   171                 y = 0;
   172             } else if (hat_pos & SDL_HAT_DOWN) {
   173                 y = SCREEN_HEIGHT-8;
   174             }
   175 
   176             if (hat_pos & SDL_HAT_LEFT) {
   177                 x = 0;
   178             } else if (hat_pos & SDL_HAT_RIGHT) {
   179                 x = SCREEN_WIDTH-8;
   180             }
   181 
   182             DrawRect(screen, x, y, 8, 8);
   183         }
   184 
   185         SDL_RenderPresent(screen);
   186 		
   187 		done = SDL_JoystickGetAttached( joystick ) == 0;
   188     }
   189 
   190     SDL_DestroyRenderer(screen);
   191     SDL_DestroyWindow(window);
   192 }
   193 
   194 int
   195 main(int argc, char *argv[])
   196 {
   197     const char *name;
   198     int i;
   199     SDL_Joystick *joystick;
   200 
   201     /* Initialize SDL (Note: video is required to start event loop) */
   202     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   203         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   204         exit(1);
   205     }
   206 
   207     /* Print information about the joysticks */
   208     printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   209     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   210         name = SDL_JoystickNameForIndex(i);
   211         printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   212         joystick = SDL_JoystickOpen(i);
   213         if (joystick == NULL) {
   214             fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
   215                     SDL_GetError());
   216         } else {
   217             printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   218             printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   219             printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
   220             printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   221             printf("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   222             SDL_JoystickClose(joystick);
   223         }
   224     }
   225 
   226     if (argv[1]) {
   227 		int nreportederror = 0;
   228 		SDL_Event event;
   229 		joystick = SDL_JoystickOpen(atoi(argv[1]));
   230 		while ( 1 ) {
   231 			if (joystick == NULL) {
   232 				if ( nreportederror == 0 ) {
   233 					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   234 					nreportederror = 1;
   235 				}
   236 			} else {
   237 				nreportederror = 0;
   238 				WatchJoystick(joystick);
   239 				SDL_JoystickClose(joystick);
   240 			}
   241 			
   242 			joystick = NULL;
   243 			SDL_WaitEvent( &event );
   244 			if ( event.type == SDL_JOYDEVICEADDED )
   245 				joystick = SDL_JoystickOpen(atoi(argv[1]));
   246 		}
   247 	}
   248     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   249 
   250     return (0);
   251 }