Changed testgamecontroller to wait for controllers to be reattached, the way testjoystick does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
diff --git a/test/testgamecontroller.c b/test/testgamecontroller.c
index 3d9101a..77d63f2 100644
--- a/test/testgamecontroller.c
+++ b/test/testgamecontroller.c
@@ -31,8 +31,6 @@
#define MAX_NUM_AXES 6
#define MAX_NUM_HATS 2
-static SDL_bool s_ForceQuit = SDL_FALSE;
-
static void
DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
{
@@ -85,7 +83,7 @@ ControllerButtonName(const SDL_GameControllerButton button)
}
}
-void
+SDL_bool
WatchGameController(SDL_GameController * gamecontroller)
{
const char *name = SDL_GameControllerName(gamecontroller);
@@ -94,7 +92,8 @@ WatchGameController(SDL_GameController * gamecontroller)
char *title = (char *)SDL_malloc(titlelen);
SDL_Window *window = NULL;
SDL_Renderer *screen = NULL;
- int done = 0;
+ SDL_bool retval = SDL_FALSE;
+ SDL_bool done = SDL_FALSE;
SDL_Event event;
int i;
@@ -108,14 +107,14 @@ WatchGameController(SDL_GameController * gamecontroller)
SCREEN_HEIGHT, 0);
if (window == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
- return;
+ return SDL_FALSE;
}
screen = SDL_CreateRenderer(window, -1, 0);
if (screen == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
- return;
+ return SDL_FALSE;
}
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
@@ -157,8 +156,7 @@ WatchGameController(SDL_GameController * gamecontroller)
}
/* Fall through to signal quit */
case SDL_QUIT:
- done = 1;
- s_ForceQuit = SDL_TRUE;
+ done = SDL_TRUE;
break;
default:
break;
@@ -200,12 +198,15 @@ WatchGameController(SDL_GameController * gamecontroller)
SDL_RenderPresent(screen);
- if ( !done )
- done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
+ if (!SDL_GameControllerGetAttached(gamecontroller)) {
+ done = SDL_TRUE;
+ retval = SDL_TRUE; /* keep going, wait for reattach. */
+ }
}
SDL_DestroyRenderer(screen);
SDL_DestroyWindow(window);
+ return retval;
}
int
@@ -250,6 +251,9 @@ main(int argc, char *argv[])
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);
@@ -259,12 +263,36 @@ main(int argc, char *argv[])
guid, sizeof (guid));
SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
gamecontroller = SDL_GameControllerOpen(device);
- if (gamecontroller == NULL) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open joystick %d: %s\n", device, SDL_GetError());
- retcode = 1;
- } else {
- WatchGameController(gamecontroller);
- SDL_GameControllerClose(gamecontroller);
+ while (keepGoing) {
+ if (gamecontroller == NULL) {
+ if (!reportederror) {
+ if (gamecontroller == NULL) {
+ 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);
+ break;
+ }
+ }
}
}
}