Allow testjoystick to quit, instead of looping on hotplug events forever.
authorRyan C. Gordon
Tue, 11 Dec 2012 11:26:08 -0500
changeset 673036158e4061f4
parent 6729 fdaeea9e7567
child 6731 572559876818
Allow testjoystick to quit, instead of looping on hotplug events forever.
test/testjoystick.c
     1.1 --- a/test/testjoystick.c	Tue Dec 11 11:07:48 2012 -0500
     1.2 +++ b/test/testjoystick.c	Tue Dec 11 11:26:08 2012 -0500
     1.3 @@ -36,13 +36,14 @@
     1.4      SDL_RenderFillRect(r, &area);
     1.5  }
     1.6  
     1.7 -void
     1.8 +static SDL_bool
     1.9  WatchJoystick(SDL_Joystick * joystick)
    1.10  {
    1.11      SDL_Window *window = NULL;
    1.12      SDL_Renderer *screen = NULL;
    1.13      const char *name = NULL;
    1.14 -    int done = 0;
    1.15 +    SDL_bool retval = SDL_FALSE;
    1.16 +    SDL_bool done = SDL_FALSE;
    1.17      SDL_Event event;
    1.18      int i;
    1.19  
    1.20 @@ -52,14 +53,14 @@
    1.21                                SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    1.22      if (window == NULL) {
    1.23          fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    1.24 -        return;
    1.25 +        return SDL_FALSE;
    1.26      }
    1.27  
    1.28      screen = SDL_CreateRenderer(window, -1, 0);
    1.29      if (screen == NULL) {
    1.30          fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    1.31          SDL_DestroyWindow(window);
    1.32 -        return;
    1.33 +        return SDL_FALSE;
    1.34      }
    1.35  
    1.36      SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.37 @@ -122,7 +123,7 @@
    1.38                  }
    1.39                  /* Fall through to signal quit */
    1.40              case SDL_QUIT:
    1.41 -                done = 1;
    1.42 +                done = SDL_TRUE;
    1.43                  break;
    1.44              default:
    1.45                  break;
    1.46 @@ -183,12 +184,16 @@
    1.47          }
    1.48  
    1.49          SDL_RenderPresent(screen);
    1.50 -		
    1.51 -		done = SDL_JoystickGetAttached( joystick ) == 0;
    1.52 +
    1.53 +        if (SDL_JoystickGetAttached( joystick ) == 0) {
    1.54 +            done = SDL_TRUE;
    1.55 +            retval = SDL_TRUE;  /* keep going, wait for reattach. */
    1.56 +        }
    1.57      }
    1.58  
    1.59      SDL_DestroyRenderer(screen);
    1.60      SDL_DestroyWindow(window);
    1.61 +    return retval;
    1.62  }
    1.63  
    1.64  int
    1.65 @@ -225,9 +230,10 @@
    1.66  
    1.67      if (argv[1]) {
    1.68  		int nreportederror = 0;
    1.69 +        SDL_bool keepGoing = SDL_TRUE;
    1.70  		SDL_Event event;
    1.71  		joystick = SDL_JoystickOpen(atoi(argv[1]));
    1.72 -		while ( 1 ) {
    1.73 +		while ( keepGoing ) {
    1.74  			if (joystick == NULL) {
    1.75  				if ( nreportederror == 0 ) {
    1.76  					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
    1.77 @@ -235,17 +241,19 @@
    1.78  				}
    1.79  			} else {
    1.80  				nreportederror = 0;
    1.81 -				WatchJoystick(joystick);
    1.82 +				keepGoing = WatchJoystick(joystick);
    1.83  				SDL_JoystickClose(joystick);
    1.84  			}
    1.85  			
    1.86 -			joystick = NULL;
    1.87 -			SDL_WaitEvent( &event );
    1.88 -			if ( event.type == SDL_JOYDEVICEADDED )
    1.89 -				joystick = SDL_JoystickOpen(atoi(argv[1]));
    1.90 +            if (keepGoing) {
    1.91 +    			joystick = NULL;
    1.92 +    			SDL_WaitEvent( &event );
    1.93 +    			if ( event.type == SDL_JOYDEVICEADDED )
    1.94 +    				joystick = SDL_JoystickOpen(atoi(argv[1]));
    1.95 +            }
    1.96  		}
    1.97  	}
    1.98      SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    1.99  
   1.100 -    return (0);
   1.101 +    return 0;
   1.102  }