Ejemplo n.º 1
0
int
main(int argc, char *argv[])
{
    const char *name;
    int i;
	int nController = 0;
    SDL_GameController *gamecontroller;

	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" );
    /* Initialize SDL (Note: video is required to start event loop) */
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }

    /* Print information about the controller */
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
		if ( SDL_IsGameController(i) )
		{
			nController++;
			name = SDL_GameControllerNameForIndex(i);
			printf("Game Controller %d: %s\n", i, name ? name : "Unknown Controller");
		}
    }
	printf("There are %d game controllers attached\n", nController);

    if (argv[1]) {
		int nreportederror = 0;
		SDL_Event event;
		gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
		while ( s_ForceQuit == SDL_FALSE ) {
			if (gamecontroller == NULL) {
				if ( nreportederror == 0 ) {
					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
					nreportederror = 1;
				}
			} else {
				nreportederror = 0;
				WatchGameController(gamecontroller);
				SDL_GameControllerClose(gamecontroller);
			}
			
			gamecontroller = NULL;
			SDL_WaitEvent( &event );
			if ( event.type == SDL_JOYDEVICEADDED )
				gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
		}
	}
    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER );

    return (0);
}
int main(int argc, char *argv[]) {
  int i;
  int nController = 0;
  int retcode = 0;
  char guid[64];
  SDL_GameController *gamecontroller;

  /* Enable standard application logging */
  SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

  /* Initialize SDL (Note: video is required to start event loop) */
  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) <
      0) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n",
                 SDL_GetError());
    return 1;
  }

  SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");

  /* Print information about the controller */
  for (i = 0; i < SDL_NumJoysticks(); ++i) {
    const char *name;
    const char *description;

    SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i), guid, sizeof(guid));

    if (SDL_IsGameController(i)) {
      nController++;
      name = SDL_GameControllerNameForIndex(i);
      description = "Controller";
    } else {
      name = SDL_JoystickNameForIndex(i);
      description = "Joystick";
    }
    SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown",
            guid);
  }
  SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n",
          nController, SDL_NumJoysticks());

  if (argv[1]) {
    SDL_bool reportederror = SDL_FALSE;
    SDL_bool keepGoing = SDL_TRUE;
    SDL_Event event;
    int device = atoi(argv[1]);
    if (device >= SDL_NumJoysticks()) {
      SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                   "%i is an invalid joystick index.\n", device);
      retcode = 1;
    } else {
      SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device), guid,
                                sizeof(guid));
      SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
      gamecontroller = SDL_GameControllerOpen(device);

      /* - this requires SDL 2.0.4
if (gamecontroller != NULL) {
SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller)))
== gamecontroller);
}
*/

      while (keepGoing) {
        if (gamecontroller == NULL) {
          if (!reportederror) {
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
                         "Couldn't open gamecontroller %d: %s\n", device,
                         SDL_GetError());
            retcode = 1;
            keepGoing = SDL_FALSE;
            reportederror = SDL_TRUE;
          }
        } else {
          reportederror = SDL_FALSE;
          keepGoing = WatchGameController(gamecontroller);
          SDL_GameControllerClose(gamecontroller);
        }

        gamecontroller = NULL;
        if (keepGoing) {
          SDL_Log("Waiting for attach\n");
        }
        while (keepGoing) {
          SDL_WaitEvent(&event);
          if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN) ||
              (event.type == SDL_MOUSEBUTTONDOWN)) {
            keepGoing = SDL_FALSE;
          } else if (event.type == SDL_CONTROLLERDEVICEADDED) {
            gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
            /* - this requires SDL 2.0.4
            if (gamecontroller != NULL) {
                SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller)))
            == gamecontroller);
            }
            */
            break;
          }
        }
      }
    }
  }

  SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK |
                    SDL_INIT_GAMECONTROLLER);

  return retcode;
}