Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Allow testjoystick to quit, instead of looping on hotplug events fore…
Browse files Browse the repository at this point in the history
…ver.
  • Loading branch information
icculus committed Dec 11, 2012
1 parent 1d4d895 commit 07ab7a5
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions test/testjoystick.c
Expand Up @@ -36,13 +36,14 @@ DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
SDL_RenderFillRect(r, &area);
}

void
static SDL_bool
WatchJoystick(SDL_Joystick * joystick)
{
SDL_Window *window = NULL;
SDL_Renderer *screen = NULL;
const char *name = NULL;
int done = 0;
SDL_bool retval = SDL_FALSE;
SDL_bool done = SDL_FALSE;
SDL_Event event;
int i;

Expand All @@ -52,14 +53,14 @@ WatchJoystick(SDL_Joystick * joystick)
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
return;
return SDL_FALSE;
}

screen = SDL_CreateRenderer(window, -1, 0);
if (screen == NULL) {
fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
return;
return SDL_FALSE;
}

SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
Expand Down Expand Up @@ -122,7 +123,7 @@ WatchJoystick(SDL_Joystick * joystick)
}
/* Fall through to signal quit */
case SDL_QUIT:
done = 1;
done = SDL_TRUE;
break;
default:
break;
Expand Down Expand Up @@ -183,12 +184,16 @@ WatchJoystick(SDL_Joystick * joystick)
}

SDL_RenderPresent(screen);

done = SDL_JoystickGetAttached( joystick ) == 0;

if (SDL_JoystickGetAttached( joystick ) == 0) {
done = SDL_TRUE;
retval = SDL_TRUE; /* keep going, wait for reattach. */
}
}

SDL_DestroyRenderer(screen);
SDL_DestroyWindow(window);
return retval;
}

int
Expand Down Expand Up @@ -225,27 +230,30 @@ main(int argc, char *argv[])

if (argv[1]) {
int nreportederror = 0;
SDL_bool keepGoing = SDL_TRUE;
SDL_Event event;
joystick = SDL_JoystickOpen(atoi(argv[1]));
while ( 1 ) {
while ( keepGoing ) {
if (joystick == NULL) {
if ( nreportederror == 0 ) {
printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
nreportederror = 1;
}
} else {
nreportederror = 0;
WatchJoystick(joystick);
keepGoing = WatchJoystick(joystick);
SDL_JoystickClose(joystick);
}

joystick = NULL;
SDL_WaitEvent( &event );
if ( event.type == SDL_JOYDEVICEADDED )
joystick = SDL_JoystickOpen(atoi(argv[1]));
if (keepGoing) {
joystick = NULL;
SDL_WaitEvent( &event );
if ( event.type == SDL_JOYDEVICEADDED )
joystick = SDL_JoystickOpen(atoi(argv[1]));
}
}
}
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);

return (0);
return 0;
}

0 comments on commit 07ab7a5

Please sign in to comment.