test/testgamecontroller.c
changeset 7056 a5699778d80f
parent 6917 47dc155b774d
child 7191 75360622e65f
equal deleted inserted replaced
7055:76323a708ea5 7056:a5699778d80f
    19 #include "SDL.h"
    19 #include "SDL.h"
    20 
    20 
    21 #ifndef SDL_JOYSTICK_DISABLED
    21 #ifndef SDL_JOYSTICK_DISABLED
    22 
    22 
    23 #ifdef __IPHONEOS__
    23 #ifdef __IPHONEOS__
    24 #define SCREEN_WIDTH	320
    24 #define SCREEN_WIDTH    320
    25 #define SCREEN_HEIGHT	480
    25 #define SCREEN_HEIGHT    480
    26 #else
    26 #else
    27 #define SCREEN_WIDTH	640
    27 #define SCREEN_WIDTH    640
    28 #define SCREEN_HEIGHT	480
    28 #define SCREEN_HEIGHT    480
    29 #endif
    29 #endif
    30 
    30 
    31 #define MAX_NUM_AXES 6
    31 #define MAX_NUM_AXES 6
    32 #define MAX_NUM_HATS 2
    32 #define MAX_NUM_HATS 2
    33 
    33 
   119     }
   119     }
   120 
   120 
   121     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   121     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   122     SDL_RenderClear(screen);
   122     SDL_RenderClear(screen);
   123     SDL_RenderPresent(screen);
   123     SDL_RenderPresent(screen);
   124 	SDL_RaiseWindow(window);
   124     SDL_RaiseWindow(window);
   125 
   125 
   126     /* Print info about the controller we are watching */
   126     /* Print info about the controller we are watching */
   127     printf("Watching controller %s\n",  name ? name : "Unknown Controller");
   127     printf("Watching controller %s\n",  name ? name : "Unknown Controller");
   128     
   128     
   129     /* Loop, getting controller events! */
   129     /* Loop, getting controller events! */
   197         }
   197         }
   198 
   198 
   199         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   199         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   200 
   200 
   201         SDL_RenderPresent(screen);
   201         SDL_RenderPresent(screen);
   202 		
   202         
   203 		if ( !done )
   203         if ( !done )
   204 			done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
   204             done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
   205     }
   205     }
   206 
   206 
   207     SDL_DestroyRenderer(screen);
   207     SDL_DestroyRenderer(screen);
   208     SDL_DestroyWindow(window);
   208     SDL_DestroyWindow(window);
   209 }
   209 }
   210 
   210 
   211 int
   211 int
   212 main(int argc, char *argv[])
   212 main(int argc, char *argv[])
   213 {
   213 {
   214     const char *name;
       
   215     int i;
   214     int i;
   216 	int nController = 0;
   215     int nController = 0;
       
   216     int retcode = 0;
       
   217     char guid[64];
   217     SDL_GameController *gamecontroller;
   218     SDL_GameController *gamecontroller;
   218 
   219 
   219 	SDL_SetHint( SDL_HINT_GAMECONTROLLERCONFIG, "341a3608000000000000504944564944,Aferglow PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7" );
       
   220     /* Initialize SDL (Note: video is required to start event loop) */
   220     /* Initialize SDL (Note: video is required to start event loop) */
   221     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   221     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   222         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   222         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   223         exit(1);
   223         return 1;
   224     }
   224     }
   225 
   225 
   226     /* Print information about the controller */
   226     /* Print information about the controller */
   227     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   227     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   228 		if ( SDL_IsGameController(i) )
   228         const char *name;
   229 		{
   229         const char *description = "Joystick (not recognized as game controller)";
   230 			nController++;
   230 
   231 			name = SDL_GameControllerNameForIndex(i);
   231         SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
   232 			printf("Game Controller %d: %s\n", i, name ? name : "Unknown Controller");
   232                                   guid, sizeof (guid));
   233 		}
   233 
   234     }
   234         if ( SDL_IsGameController(i) )
   235 	printf("There are %d game controllers attached\n", nController);
   235         {
       
   236             nController++;
       
   237             name = SDL_GameControllerNameForIndex(i);
       
   238         } else {
       
   239             name = SDL_JoystickNameForIndex(i);
       
   240         }
       
   241         printf("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
       
   242     }
       
   243     printf("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
   236 
   244 
   237     if (argv[1]) {
   245     if (argv[1]) {
   238 		int nreportederror = 0;
   246         int device = atoi(argv[1]);
   239 		SDL_Event event;
   247         if (device >= SDL_NumJoysticks()) {
   240 		gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   248             printf("%i is an invalid joystick index.\n", device);
   241 		while ( s_ForceQuit == SDL_FALSE ) {
   249             retcode = 1;
   242 			if (gamecontroller == NULL) {
   250         } else {
   243 				if ( nreportederror == 0 ) {
   251             SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
   244 					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   252                                       guid, sizeof (guid));
   245 					nreportederror = 1;
   253             printf("Attempting to open device %i, guid %s\n", device, guid);
   246 				}
   254             gamecontroller = SDL_GameControllerOpen(device);
   247 			} else {
   255             if (gamecontroller == NULL) {
   248 				nreportederror = 0;
   256                 printf("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   249 				WatchGameController(gamecontroller);
   257                 retcode = 1;
   250 				SDL_GameControllerClose(gamecontroller);
   258             } else {
   251 			}
   259                 WatchGameController(gamecontroller);
   252 			
   260                 SDL_GameControllerClose(gamecontroller);
   253 			gamecontroller = NULL;
   261             }
   254 			SDL_WaitEvent( &event );
   262         }
   255 			if ( event.type == SDL_JOYDEVICEADDED )
   263     }
   256 				gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   264 
   257 		}
   265     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
   258 	}
   266 
   259     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER );
   267     return retcode;
   260 
       
   261     return (0);
       
   262 }
   268 }
   263 
   269 
   264 #else
   270 #else
   265 
   271 
   266 int
   272 int