test/testjoystick.c
changeset 6690 9548c8a58103
parent 6586 ab57cc69dcc0
child 6730 36158e4061f4
equal deleted inserted replaced
6689:e2fc3ae07582 6690:9548c8a58103
    15 #include <stdio.h>
    15 #include <stdio.h>
    16 #include <stdlib.h>
    16 #include <stdlib.h>
    17 #include <string.h>
    17 #include <string.h>
    18 
    18 
    19 #include "SDL.h"
    19 #include "SDL.h"
    20 #include "common.h"
    20 
    21 
    21 #ifdef __IPHONEOS__
    22 static CommonState *state;
    22 #define SCREEN_WIDTH	320
    23 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    23 #define SCREEN_HEIGHT	480
       
    24 #else
       
    25 #define SCREEN_WIDTH	640
       
    26 #define SCREEN_HEIGHT	480
       
    27 #endif
    24 
    28 
    25 #define MAX_NUM_AXES 6
    29 #define MAX_NUM_AXES 6
    26 #define MAX_NUM_HATS 2
    30 #define MAX_NUM_HATS 2
    27 
    31 
    28 static void
    32 static void
    35 void
    39 void
    36 WatchJoystick(SDL_Joystick * joystick)
    40 WatchJoystick(SDL_Joystick * joystick)
    37 {
    41 {
    38     SDL_Window *window = NULL;
    42     SDL_Window *window = NULL;
    39     SDL_Renderer *screen = NULL;
    43     SDL_Renderer *screen = NULL;
    40     SDL_Rect viewport;
       
    41     SDL_Event event;
       
    42 
       
    43     const char *name = NULL;
    44     const char *name = NULL;
    44     int done = 0;
    45     int done = 0;
       
    46     SDL_Event event;
    45     int i;
    47     int i;
    46 
    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 
    47     /* Print info about the joystick we are watching */
    70     /* Print info about the joystick we are watching */
    48     name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    71     name = SDL_JoystickName(joystick);
    49     printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    72     printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
    50            name ? name : "Unknown Joystick");
    73            name ? name : "Unknown Joystick");
    51     printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    74     printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    52            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    75            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    53            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    76            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    54 
    77 
    55     /* Loop, getting joystick events! */
    78     /* Loop, getting joystick events! */
    56     while (!done) {
    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 
    57         while (SDL_PollEvent(&event)) {
    84         while (SDL_PollEvent(&event)) {
    58             switch (event.type) {
    85             switch (event.type) {
    59             case SDL_JOYAXISMOTION:
    86             case SDL_JOYAXISMOTION:
    60                 printf("Joystick %d axis %d value: %d\n",
    87                 printf("Joystick %d axis %d value: %d\n",
    61                        event.jaxis.which,
    88                        event.jaxis.which,
    99                 break;
   126                 break;
   100             default:
   127             default:
   101                 break;
   128                 break;
   102             }
   129             }
   103         }
   130         }
   104 
       
   105         /* Update visual joystick state */
   131         /* Update visual joystick state */
   106         for (i = 0; i < state->num_windows; ++i) {
   132         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   107             screen = state->renderers[i];
   133         for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   108 
   134             if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   109             /* Erase previous axes */
   135                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   110             SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   136             }
   111             SDL_RenderClear(screen);
   137         }
   112 
   138 
   113             /* Query the sizes */
   139         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   114             SDL_RenderGetViewport(screen, &viewport);
   140         for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   115 
   141             /* Draw the X/Y axis */
   116             SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   142             int x, y;
   117             for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   143             x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   118                 if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   144             x *= SCREEN_WIDTH;
   119                     DrawRect(screen, i * 34, viewport.h - 34, 32, 32);
   145             x /= 65535;
   120                 }
   146             if (x < 0) {
   121             }
   147                 x = 0;
   122 
   148             } else if (x > (SCREEN_WIDTH - 16)) {
   123             SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   149                 x = SCREEN_WIDTH - 16;
   124             for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   150             }
   125                 /* Draw the X/Y axis */
   151             y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   126                 int x, y;
   152             y *= SCREEN_HEIGHT;
   127                 x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   153             y /= 65535;
   128                 x *= viewport.w ;
   154             if (y < 0) {
   129                 x /= 65535;
   155                 y = 0;
   130                 if (x < 0) {
   156             } else if (y > (SCREEN_HEIGHT - 16)) {
   131                     x = 0;
   157                 y = SCREEN_HEIGHT - 16;
   132                 } else if (x > (viewport.w - 16)) {
   158             }
   133                     x = viewport.w - 16;
   159 
   134                 }
   160             DrawRect(screen, x, y, 16, 16);
   135                 y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   161         }
   136                 y *= viewport.h;
   162 
   137                 y /= 65535;
   163         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   138                 if (y < 0) {
   164         for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   139                     y = 0;
   165             /* Derive the new position */
   140                 } else if (y > (viewport.h - 16)) {
   166             int x = SCREEN_WIDTH/2;
   141                     y = viewport.h - 16;
   167             int y = SCREEN_HEIGHT/2;
   142                 }
   168             const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   143 
   169 
   144                 DrawRect(screen, x, y, 16, 16);
   170             if (hat_pos & SDL_HAT_UP) {
   145             }
   171                 y = 0;
   146 
   172             } else if (hat_pos & SDL_HAT_DOWN) {
   147             SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   173                 y = SCREEN_HEIGHT-8;
   148             for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   174             }
   149                 /* Derive the new position */
   175 
   150                 int x = viewport.w/2;
   176             if (hat_pos & SDL_HAT_LEFT) {
   151                 int y = viewport.h/2;
   177                 x = 0;
   152                 const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   178             } else if (hat_pos & SDL_HAT_RIGHT) {
   153 
   179                 x = SCREEN_WIDTH-8;
   154                 if (hat_pos & SDL_HAT_UP) {
   180             }
   155                     y = 0;
   181 
   156                 } else if (hat_pos & SDL_HAT_DOWN) {
   182             DrawRect(screen, x, y, 8, 8);
   157                     y = viewport.h-8;
   183         }
   158                 }
   184 
   159 
   185         SDL_RenderPresent(screen);
   160                 if (hat_pos & SDL_HAT_LEFT) {
   186 		
   161                     x = 0;
   187 		done = SDL_JoystickGetAttached( joystick ) == 0;
   162                 } else if (hat_pos & SDL_HAT_RIGHT) {
   188     }
   163                     x = viewport.w-8;
   189 
   164                 }
   190     SDL_DestroyRenderer(screen);
   165 
   191     SDL_DestroyWindow(window);
   166                 DrawRect(screen, x, y, 8, 8);
       
   167             }
       
   168 
       
   169             SDL_RenderPresent(screen);
       
   170         }
       
   171     }
       
   172 }
   192 }
   173 
   193 
   174 int
   194 int
   175 main(int argc, char *argv[])
   195 main(int argc, char *argv[])
   176 {
   196 {
   177     const char *name;
   197     const char *name;
   178     int i, joy=-1;
   198     int i;
   179     SDL_Joystick *joystick;
   199     SDL_Joystick *joystick;
   180 
   200 
   181     /* Initialize SDL (Note: video is required to start event loop) */
   201     /* Initialize SDL (Note: video is required to start event loop) */
   182     if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
   202     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   183         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   203         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   184         exit(1);
   204         exit(1);
   185     }
       
   186 
       
   187     /* Initialize test framework */
       
   188     state = CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
       
   189     if (!state) {
       
   190         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
       
   191         return 1;
       
   192     }
   205     }
   193 
   206 
   194     /* Print information about the joysticks */
   207     /* Print information about the joysticks */
   195     printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   208     printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   196     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   209     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   197         name = SDL_JoystickName(i);
   210         name = SDL_JoystickNameForIndex(i);
   198         printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   211         printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   199         joystick = SDL_JoystickOpen(i);
   212         joystick = SDL_JoystickOpen(i);
   200         if (joystick == NULL) {
   213         if (joystick == NULL) {
   201             fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
   214             fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
   202                     SDL_GetError());
   215                     SDL_GetError());
   203         } else {
   216         } else {
   204             printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   217             printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   205             printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   218             printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   206             printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
   219             printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
   207             printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   220             printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
       
   221             printf("instance id: %d\n", SDL_JoystickInstanceID(joystick));
   208             SDL_JoystickClose(joystick);
   222             SDL_JoystickClose(joystick);
   209         }
   223         }
   210     }
   224     }
   211 
   225 
   212     for (i = 1; i < argc;) {
   226     if (argv[1]) {
   213         int consumed;
   227 		int nreportederror = 0;
   214 
   228 		SDL_Event event;
   215         consumed = CommonArg(state, i);
   229 		joystick = SDL_JoystickOpen(atoi(argv[1]));
   216         if (consumed == 0) {
   230 		while ( 1 ) {
   217             consumed = -1;
   231 			if (joystick == NULL) {
   218             if (SDL_isdigit(*argv[i])) {
   232 				if ( nreportederror == 0 ) {
   219                 joy = SDL_atoi(argv[i]);
   233 					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   220                 consumed = 1;
   234 					nreportederror = 1;
   221             }
   235 				}
   222         }
   236 			} else {
   223         if (consumed < 0) {
   237 				nreportederror = 0;
   224             return 1;
   238 				WatchJoystick(joystick);
   225         }
   239 				SDL_JoystickClose(joystick);
   226         i += consumed;
   240 			}
   227     }
   241 			
   228     if (!CommonInit(state)) {
   242 			joystick = NULL;
   229         return 2;
   243 			SDL_WaitEvent( &event );
   230     }
   244 			if ( event.type == SDL_JOYDEVICEADDED )
   231 
   245 				joystick = SDL_JoystickOpen(atoi(argv[1]));
   232     if (joy > -1) {
   246 		}
   233         joystick = SDL_JoystickOpen(joy);
   247 	}
   234         if (joystick == NULL) {
   248     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   235             printf("Couldn't open joystick %d: %s\n", joy,
       
   236                    SDL_GetError());
       
   237         } else {
       
   238             WatchJoystick(joystick);
       
   239             SDL_JoystickClose(joystick);
       
   240         }
       
   241     }
       
   242     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
       
   243     CommonQuit(state);
       
   244 
   249 
   245     return (0);
   250     return (0);
   246 }
   251 }