upgrade SDL to version 2.0.8
[platform/upstream/SDL.git] / test / loopwave.c
index 4f3e479..dd48af3 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
+  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
 #include <stdio.h>
 #include <stdlib.h>
 
-#if HAVE_SIGNAL_H
-#include <signal.h>
-#endif
-
 #ifdef __EMSCRIPTEN__
 #include <emscripten/emscripten.h>
 #endif
 
 #include "SDL.h"
 
-struct
+static struct
 {
     SDL_AudioSpec spec;
     Uint8 *sound;               /* Pointer to wave data */
@@ -38,6 +34,7 @@ struct
     int soundpos;               /* Current play position */
 } wave;
 
+static SDL_AudioDeviceID device;
 
 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
 static void
@@ -47,6 +44,37 @@ quit(int rc)
     exit(rc);
 }
 
+static void
+close_audio()
+{
+    if (device != 0) {
+        SDL_CloseAudioDevice(device);
+        device = 0;
+    }
+}
+
+static void
+open_audio()
+{
+    /* Initialize fillerup() variables */
+    device = SDL_OpenAudioDevice(NULL, SDL_FALSE, &wave.spec, NULL, 0);
+    if (!device) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
+        SDL_FreeWAV(wave.sound);
+        quit(2);
+    }
+
+
+    /* Let the audio run */
+    SDL_PauseAudioDevice(device, SDL_FALSE);
+}
+
+static void reopen_audio()
+{
+    close_audio();
+    open_audio();
+}
+
 
 void SDLCALL
 fillerup(void *unused, Uint8 * stream, int len)
@@ -72,22 +100,17 @@ fillerup(void *unused, Uint8 * stream, int len)
 }
 
 static int done = 0;
-void
-poked(int sig)
-{
-    done = 1;
-}
 
 #ifdef __EMSCRIPTEN__
 void
 loop()
 {
-    if(done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING))
+    if(done || (SDL_GetAudioDeviceStatus(device) != SDL_AUDIO_PLAYING))
         emscripten_cancel_main_loop();
 }
 #endif
 int
-SDL_main(int argc, char *argv[])
+main(int argc, char *argv[])
 {
     int i;
     char filename[4096];
@@ -96,8 +119,8 @@ SDL_main(int argc, char *argv[])
     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
     /* Load the SDL library */
-    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
-        SDLTest_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
+    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_EVENTS) < 0) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
         return (1);
     }
 
@@ -108,53 +131,47 @@ SDL_main(int argc, char *argv[])
     }
     /* Load the wave file into memory */
     if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
-        SDLTest_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
         quit(1);
     }
 
     wave.spec.callback = fillerup;
-#if HAVE_SIGNAL_H
-    /* Set the signals */
-#ifdef SIGHUP
-    signal(SIGHUP, poked);
-#endif
-    signal(SIGINT, poked);
-#ifdef SIGQUIT
-    signal(SIGQUIT, poked);
-#endif
-    signal(SIGTERM, poked);
-#endif /* HAVE_SIGNAL_H */
 
     /* Show the list of available drivers */
-    SDLTest_Log("Available audio drivers:");
+    SDL_Log("Available audio drivers:");
     for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
-        SDLTest_Log("%i: %s", i, SDL_GetAudioDriver(i));
+        SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
     }
 
-    /* Initialize fillerup() variables */
-    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
-        SDLTest_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
-        SDL_FreeWAV(wave.sound);
-        quit(2);
-    }
 
-    SDLTest_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
 
-    /* Let the audio run */
-    SDL_PauseAudio(0);
+    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
+
+    open_audio();
+
+    SDL_FlushEvents(SDL_AUDIODEVICEADDED, SDL_AUDIODEVICEREMOVED);
 
 #ifdef __EMSCRIPTEN__
     emscripten_set_main_loop(loop, 0, 1);
 #else
-    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
-       {
-        SDL_Delay(10000);
-               done = 1;
-       }
+    while (!done) {
+        SDL_Event event;
+
+        while (SDL_PollEvent(&event) > 0) {
+            if (event.type == SDL_QUIT) {
+                done = 1;
+            }
+            if ((event.type == SDL_AUDIODEVICEADDED && !event.adevice.iscapture) ||
+                (event.type == SDL_AUDIODEVICEREMOVED && !event.adevice.iscapture && event.adevice.which == device)) {
+                reopen_audio();
+            }
+        }
+        SDL_Delay(100);
+    }
 #endif
 
     /* Clean up on signal */
-    SDL_CloseAudio();
+    close_audio();
     SDL_FreeWAV(wave.sound);
     SDL_Quit();
     return (0);