Allow testjoystick to quit, instead of looping on hotplug events forever.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 11 Dec 2012 11:26:08 -0500
changeset 6730 36158e4061f4
parent 6729 fdaeea9e7567
child 6731 572559876818
Allow testjoystick to quit, instead of looping on hotplug events forever.
test/testjoystick.c
--- a/test/testjoystick.c	Tue Dec 11 11:07:48 2012 -0500
+++ b/test/testjoystick.c	Tue Dec 11 11:26:08 2012 -0500
@@ -36,13 +36,14 @@
     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;
 
@@ -52,14 +53,14 @@
                               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);
@@ -122,7 +123,7 @@
                 }
                 /* Fall through to signal quit */
             case SDL_QUIT:
-                done = 1;
+                done = SDL_TRUE;
                 break;
             default:
                 break;
@@ -183,12 +184,16 @@
         }
 
         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
@@ -225,9 +230,10 @@
 
     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());
@@ -235,17 +241,19 @@
 				}
 			} 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;
 }