test/testgamecontroller.c
changeset 7056 a5699778d80f
parent 6917 47dc155b774d
child 7191 75360622e65f
     1.1 --- a/test/testgamecontroller.c	Wed Apr 03 16:48:20 2013 -0700
     1.2 +++ b/test/testgamecontroller.c	Wed Apr 03 16:48:23 2013 -0700
     1.3 @@ -21,11 +21,11 @@
     1.4  #ifndef SDL_JOYSTICK_DISABLED
     1.5  
     1.6  #ifdef __IPHONEOS__
     1.7 -#define SCREEN_WIDTH	320
     1.8 -#define SCREEN_HEIGHT	480
     1.9 +#define SCREEN_WIDTH    320
    1.10 +#define SCREEN_HEIGHT    480
    1.11  #else
    1.12 -#define SCREEN_WIDTH	640
    1.13 -#define SCREEN_HEIGHT	480
    1.14 +#define SCREEN_WIDTH    640
    1.15 +#define SCREEN_HEIGHT    480
    1.16  #endif
    1.17  
    1.18  #define MAX_NUM_AXES 6
    1.19 @@ -121,7 +121,7 @@
    1.20      SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.21      SDL_RenderClear(screen);
    1.22      SDL_RenderPresent(screen);
    1.23 -	SDL_RaiseWindow(window);
    1.24 +    SDL_RaiseWindow(window);
    1.25  
    1.26      /* Print info about the controller we are watching */
    1.27      printf("Watching controller %s\n",  name ? name : "Unknown Controller");
    1.28 @@ -199,9 +199,9 @@
    1.29          SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
    1.30  
    1.31          SDL_RenderPresent(screen);
    1.32 -		
    1.33 -		if ( !done )
    1.34 -			done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
    1.35 +        
    1.36 +        if ( !done )
    1.37 +            done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
    1.38      }
    1.39  
    1.40      SDL_DestroyRenderer(screen);
    1.41 @@ -211,54 +211,60 @@
    1.42  int
    1.43  main(int argc, char *argv[])
    1.44  {
    1.45 -    const char *name;
    1.46      int i;
    1.47 -	int nController = 0;
    1.48 +    int nController = 0;
    1.49 +    int retcode = 0;
    1.50 +    char guid[64];
    1.51      SDL_GameController *gamecontroller;
    1.52  
    1.53 -	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" );
    1.54      /* Initialize SDL (Note: video is required to start event loop) */
    1.55      if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
    1.56          fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.57 -        exit(1);
    1.58 +        return 1;
    1.59      }
    1.60  
    1.61      /* Print information about the controller */
    1.62      for (i = 0; i < SDL_NumJoysticks(); ++i) {
    1.63 -		if ( SDL_IsGameController(i) )
    1.64 -		{
    1.65 -			nController++;
    1.66 -			name = SDL_GameControllerNameForIndex(i);
    1.67 -			printf("Game Controller %d: %s\n", i, name ? name : "Unknown Controller");
    1.68 -		}
    1.69 +        const char *name;
    1.70 +        const char *description = "Joystick (not recognized as game controller)";
    1.71 +
    1.72 +        SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
    1.73 +                                  guid, sizeof (guid));
    1.74 +
    1.75 +        if ( SDL_IsGameController(i) )
    1.76 +        {
    1.77 +            nController++;
    1.78 +            name = SDL_GameControllerNameForIndex(i);
    1.79 +        } else {
    1.80 +            name = SDL_JoystickNameForIndex(i);
    1.81 +        }
    1.82 +        printf("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
    1.83      }
    1.84 -	printf("There are %d game controllers attached\n", nController);
    1.85 +    printf("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
    1.86  
    1.87      if (argv[1]) {
    1.88 -		int nreportederror = 0;
    1.89 -		SDL_Event event;
    1.90 -		gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
    1.91 -		while ( s_ForceQuit == SDL_FALSE ) {
    1.92 -			if (gamecontroller == NULL) {
    1.93 -				if ( nreportederror == 0 ) {
    1.94 -					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
    1.95 -					nreportederror = 1;
    1.96 -				}
    1.97 -			} else {
    1.98 -				nreportederror = 0;
    1.99 -				WatchGameController(gamecontroller);
   1.100 -				SDL_GameControllerClose(gamecontroller);
   1.101 -			}
   1.102 -			
   1.103 -			gamecontroller = NULL;
   1.104 -			SDL_WaitEvent( &event );
   1.105 -			if ( event.type == SDL_JOYDEVICEADDED )
   1.106 -				gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   1.107 -		}
   1.108 -	}
   1.109 -    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER );
   1.110 +        int device = atoi(argv[1]);
   1.111 +        if (device >= SDL_NumJoysticks()) {
   1.112 +            printf("%i is an invalid joystick index.\n", device);
   1.113 +            retcode = 1;
   1.114 +        } else {
   1.115 +            SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
   1.116 +                                      guid, sizeof (guid));
   1.117 +            printf("Attempting to open device %i, guid %s\n", device, guid);
   1.118 +            gamecontroller = SDL_GameControllerOpen(device);
   1.119 +            if (gamecontroller == NULL) {
   1.120 +                printf("Couldn't open joystick %d: %s\n", device, SDL_GetError());
   1.121 +                retcode = 1;
   1.122 +            } else {
   1.123 +                WatchGameController(gamecontroller);
   1.124 +                SDL_GameControllerClose(gamecontroller);
   1.125 +            }
   1.126 +        }
   1.127 +    }
   1.128  
   1.129 -    return (0);
   1.130 +    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
   1.131 +
   1.132 +    return retcode;
   1.133  }
   1.134  
   1.135  #else