Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 04 Aug 2011 00:31:11 -0400
changeset 5593ab22ca13c47f
parent 5592 2e88d0742f4d
child 5594 e741303e08a8
Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
src/audio/SDL_audio.c
src/audio/SDL_audiodev.c
src/audio/SDL_audiodev_c.h
src/audio/SDL_sysaudio.h
src/audio/bsd/SDL_bsdaudio.c
src/audio/directsound/SDL_directsound.c
src/audio/dsp/SDL_dspaudio.c
src/audio/macosx/SDL_coreaudio.c
src/audio/qsa/SDL_qsa_audio.c
src/audio/winmm/SDL_winmm.c
src/audio/xaudio2/SDL_xaudio2.c
     1.1 --- a/src/audio/SDL_audio.c	Thu Aug 04 01:07:09 2011 -0400
     1.2 +++ b/src/audio/SDL_audio.c	Thu Aug 04 00:31:11 2011 -0400
     1.3 @@ -151,10 +151,9 @@
     1.4  
     1.5  
     1.6  /* stubs for audio drivers that don't need a specific entry point... */
     1.7 -static int
     1.8 -SDL_AudioDetectDevices_Default(int iscapture)
     1.9 -{
    1.10 -    return -1;
    1.11 +static void
    1.12 +SDL_AudioDetectDevices_Default(int iscapture, SDL_AddAudioDevice addfn)
    1.13 +{                               /* no-op. */
    1.14  }
    1.15  
    1.16  static void
    1.17 @@ -199,13 +198,6 @@
    1.18      return 0;
    1.19  }
    1.20  
    1.21 -static const char *
    1.22 -SDL_AudioGetDeviceName_Default(int index, int iscapture)
    1.23 -{
    1.24 -    SDL_SetError("No such device");
    1.25 -    return NULL;
    1.26 -}
    1.27 -
    1.28  static void
    1.29  SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
    1.30  {
    1.31 @@ -238,7 +230,6 @@
    1.32              current_audio.impl.x = SDL_Audio##x##_Default; \
    1.33          }
    1.34      FILL_STUB(DetectDevices);
    1.35 -    FILL_STUB(GetDeviceName);
    1.36      FILL_STUB(OpenDevice);
    1.37      FILL_STUB(ThreadInit);
    1.38      FILL_STUB(WaitDevice);
    1.39 @@ -641,13 +632,64 @@
    1.40      return current_audio.name;
    1.41  }
    1.42  
    1.43 +static void
    1.44 +free_device_list(char ***devices, int *devCount)
    1.45 +{
    1.46 +    int i = *devCount;
    1.47 +    if ((i > 0) && (*devices != NULL)) {
    1.48 +        while (i--) {
    1.49 +            SDL_free((*devices)[i]);
    1.50 +        }
    1.51 +    }
    1.52 +
    1.53 +    if (*devices != NULL) {
    1.54 +        SDL_free(*devices);
    1.55 +    }
    1.56 +
    1.57 +    *devices = NULL;
    1.58 +    *devCount = 0;
    1.59 +}
    1.60 +
    1.61 +static
    1.62 +void SDL_AddCaptureAudioDevice(const char *_name)
    1.63 +{
    1.64 +    char *name = NULL;
    1.65 +    void *ptr = SDL_realloc(current_audio.inputDevices,
    1.66 +                          (current_audio.inputDeviceCount+1) * sizeof(char*));
    1.67 +    if (ptr == NULL) {
    1.68 +        return;  /* oh well. */
    1.69 +    }
    1.70 +
    1.71 +    current_audio.inputDevices = (char **) ptr;
    1.72 +    name = SDL_strdup(_name);  /* if this returns NULL, that's okay. */
    1.73 +    current_audio.inputDevices[current_audio.inputDeviceCount++] = name;
    1.74 +}
    1.75 +
    1.76 +static
    1.77 +void SDL_AddOutputAudioDevice(const char *_name)
    1.78 +{
    1.79 +    char *name = NULL;
    1.80 +    void *ptr = SDL_realloc(current_audio.outputDevices,
    1.81 +                          (current_audio.outputDeviceCount+1) * sizeof(char*));
    1.82 +    if (ptr == NULL) {
    1.83 +        return;  /* oh well. */
    1.84 +    }
    1.85 +
    1.86 +    current_audio.outputDevices = (char **) ptr;
    1.87 +    name = SDL_strdup(_name);  /* if this returns NULL, that's okay. */
    1.88 +    current_audio.outputDevices[current_audio.outputDeviceCount++] = name;
    1.89 +}
    1.90 +
    1.91  
    1.92  int
    1.93  SDL_GetNumAudioDevices(int iscapture)
    1.94  {
    1.95 +    int retval = 0;
    1.96 +
    1.97      if (!SDL_WasInit(SDL_INIT_AUDIO)) {
    1.98          return -1;
    1.99      }
   1.100 +
   1.101      if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
   1.102          return 0;
   1.103      }
   1.104 @@ -660,7 +702,19 @@
   1.105          return 1;
   1.106      }
   1.107  
   1.108 -    return current_audio.impl.DetectDevices(iscapture);
   1.109 +    if (iscapture) {
   1.110 +        free_device_list(&current_audio.inputDevices,
   1.111 +                         &current_audio.inputDeviceCount);
   1.112 +        current_audio.impl.DetectDevices(iscapture, SDL_AddCaptureAudioDevice);
   1.113 +        retval = current_audio.inputDeviceCount;
   1.114 +    } else {
   1.115 +        free_device_list(&current_audio.outputDevices,
   1.116 +                         &current_audio.outputDeviceCount);
   1.117 +        current_audio.impl.DetectDevices(iscapture, SDL_AddOutputAudioDevice);
   1.118 +        retval = current_audio.outputDeviceCount;
   1.119 +    }
   1.120 +
   1.121 +    return retval;
   1.122  }
   1.123  
   1.124  
   1.125 @@ -678,8 +732,7 @@
   1.126      }
   1.127  
   1.128      if (index < 0) {
   1.129 -        SDL_SetError("No such device");
   1.130 -        return NULL;
   1.131 +        goto no_such_device;
   1.132      }
   1.133  
   1.134      if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
   1.135 @@ -690,7 +743,21 @@
   1.136          return DEFAULT_OUTPUT_DEVNAME;
   1.137      }
   1.138  
   1.139 -    return current_audio.impl.GetDeviceName(index, iscapture);
   1.140 +    if (iscapture) {
   1.141 +        if (index >= current_audio.inputDeviceCount) {
   1.142 +            goto no_such_device;
   1.143 +        }
   1.144 +        return current_audio.inputDevices[index];
   1.145 +    } else {
   1.146 +        if (index >= current_audio.outputDeviceCount) {
   1.147 +            goto no_such_device;
   1.148 +        }
   1.149 +        return current_audio.outputDevices[index];
   1.150 +    }
   1.151 +
   1.152 +no_such_device:
   1.153 +    SDL_SetError("No such device");
   1.154 +    return NULL;
   1.155  }
   1.156  
   1.157  
   1.158 @@ -880,6 +947,11 @@
   1.159          }
   1.160      }
   1.161  
   1.162 +    /* force a device detection if we haven't done one yet. */
   1.163 +    if ( ((iscapture) && (current_audio.inputDevices == NULL)) ||
   1.164 +         ((!iscapture) && (current_audio.outputDevices == NULL)) )
   1.165 +        SDL_GetNumAudioDevices(iscapture);
   1.166 +
   1.167      if (!current_audio.impl.OpenDevice(device, devname, iscapture)) {
   1.168          close_audio_device(device);
   1.169          return 0;
   1.170 @@ -1121,6 +1193,10 @@
   1.171  
   1.172      /* Free the driver data */
   1.173      current_audio.impl.Deinitialize();
   1.174 +    free_device_list(&current_audio.outputDevices,
   1.175 +                     &current_audio.outputDeviceCount);
   1.176 +    free_device_list(&current_audio.inputDevices,
   1.177 +                     &current_audio.inputDeviceCount);
   1.178      SDL_memset(&current_audio, '\0', sizeof(current_audio));
   1.179      SDL_memset(open_devices, '\0', sizeof(open_devices));
   1.180  }
     2.1 --- a/src/audio/SDL_audiodev.c	Thu Aug 04 01:07:09 2011 -0400
     2.2 +++ b/src/audio/SDL_audiodev.c	Thu Aug 04 00:31:11 2011 -0400
     2.3 @@ -48,46 +48,20 @@
     2.4  
     2.5  static inline void
     2.6  test_device(const char *fname, int flags, int (*test) (int fd),
     2.7 -            char ***devices, int *devCount)
     2.8 +            SDL_AddAudioDevice addfn)
     2.9  {
    2.10      struct stat sb;
    2.11      if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
    2.12 -        int audio_fd = open(fname, flags, 0);
    2.13 -        if ((audio_fd >= 0) && (test(audio_fd))) {
    2.14 -            void *p =
    2.15 -                SDL_realloc(*devices, ((*devCount) + 1) * sizeof(char *));
    2.16 -            if (p != NULL) {
    2.17 -                size_t len = strlen(fname) + 1;
    2.18 -                char *str = (char *) SDL_malloc(len);
    2.19 -                *devices = (char **) p;
    2.20 -                if (str != NULL) {
    2.21 -                    SDL_strlcpy(str, fname, len);
    2.22 -                    (*devices)[(*devCount)++] = str;
    2.23 -                }
    2.24 +        const int audio_fd = open(fname, flags, 0);
    2.25 +        if (audio_fd >= 0) {
    2.26 +            if (test(audio_fd)) {
    2.27 +                addfn(fname);
    2.28              }
    2.29              close(audio_fd);
    2.30          }
    2.31      }
    2.32  }
    2.33  
    2.34 -void
    2.35 -SDL_FreeUnixAudioDevices(char ***devices, int *devCount)
    2.36 -{
    2.37 -    int i = *devCount;
    2.38 -    if ((i > 0) && (*devices != NULL)) {
    2.39 -        while (i--) {
    2.40 -            SDL_free((*devices)[i]);
    2.41 -        }
    2.42 -    }
    2.43 -
    2.44 -    if (*devices != NULL) {
    2.45 -        SDL_free(*devices);
    2.46 -    }
    2.47 -
    2.48 -    *devices = NULL;
    2.49 -    *devCount = 0;
    2.50 -}
    2.51 -
    2.52  static int
    2.53  test_stub(int fd)
    2.54  {
    2.55 @@ -95,9 +69,10 @@
    2.56  }
    2.57  
    2.58  void
    2.59 -SDL_EnumUnixAudioDevices(int flags, int classic, int (*test) (int fd),
    2.60 -                         char ***devices, int *devCount)
    2.61 +SDL_EnumUnixAudioDevices(int iscapture, int classic, int (*test)(int fd),
    2.62 +                         SDL_AddAudioDevice addfn)
    2.63  {
    2.64 +    const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
    2.65      const char *audiodev;
    2.66      char audiopath[1024];
    2.67  
    2.68 @@ -122,14 +97,14 @@
    2.69              }
    2.70          }
    2.71      }
    2.72 -    test_device(audiodev, flags, test, devices, devCount);
    2.73 +    test_device(audiodev, flags, test, addfn);
    2.74  
    2.75      if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
    2.76          int instance = 0;
    2.77          while (instance++ <= 64) {
    2.78              SDL_snprintf(audiopath, SDL_arraysize(audiopath),
    2.79                           "%s%d", audiodev, instance);
    2.80 -            test_device(audiopath, flags, test, devices, devCount);
    2.81 +            test_device(audiopath, flags, test, addfn);
    2.82          }
    2.83      }
    2.84  }
     3.1 --- a/src/audio/SDL_audiodev_c.h	Thu Aug 04 01:07:09 2011 -0400
     3.2 +++ b/src/audio/SDL_audiodev_c.h	Thu Aug 04 00:31:11 2011 -0400
     3.3 @@ -18,10 +18,22 @@
     3.4       misrepresented as being the original software.
     3.5    3. This notice may not be removed or altered from any source distribution.
     3.6  */
     3.7 +#include "SDL.h"
     3.8  #include "SDL_config.h"
     3.9 +#include "SDL_sysaudio.h"
    3.10  
    3.11 -void SDL_EnumUnixAudioDevices(int flags, int classic, int (*test) (int fd),
    3.12 -                              char ***devs, int *count);
    3.13 -void SDL_FreeUnixAudioDevices(char ***devices, int *devCount);
    3.14 +/* Open the audio device for playback, and don't block if busy */
    3.15 +/* #define USE_BLOCKING_WRITES */
    3.16 +
    3.17 +#ifdef USE_BLOCKING_WRITES
    3.18 +#define OPEN_FLAGS_OUTPUT O_WRONLY
    3.19 +#define OPEN_FLAGS_INPUT O_RDONLY
    3.20 +#else
    3.21 +#define OPEN_FLAGS_OUTPUT (O_WRONLY|O_NONBLOCK)
    3.22 +#define OPEN_FLAGS_INPUT (O_RDONLY|O_NONBLOCK)
    3.23 +#endif
    3.24 +
    3.25 +void SDL_EnumUnixAudioDevices(int iscapture, int classic,
    3.26 +                              int (*test) (int fd), SDL_AddAudioDevice addfn);
    3.27  
    3.28  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/audio/SDL_sysaudio.h	Thu Aug 04 01:07:09 2011 -0400
     4.2 +++ b/src/audio/SDL_sysaudio.h	Thu Aug 04 00:31:11 2011 -0400
     4.3 @@ -30,10 +30,12 @@
     4.4  typedef struct SDL_AudioDevice SDL_AudioDevice;
     4.5  #define _THIS	SDL_AudioDevice *_this
     4.6  
     4.7 +/* Used by audio targets during DetectDevices() */
     4.8 +typedef void (*SDL_AddAudioDevice)(const char *name);
     4.9 +
    4.10  typedef struct SDL_AudioDriverImpl
    4.11  {
    4.12 -    int (*DetectDevices) (int iscapture);
    4.13 -    const char *(*GetDeviceName) (int index, int iscapture);
    4.14 +    void (*DetectDevices) (int iscapture, SDL_AddAudioDevice addfn);
    4.15      int (*OpenDevice) (_THIS, const char *devname, int iscapture);
    4.16      void (*ThreadInit) (_THIS); /* Called by audio thread at start */
    4.17      void (*WaitDevice) (_THIS);
    4.18 @@ -67,6 +69,12 @@
    4.19      const char *desc;
    4.20  
    4.21      SDL_AudioDriverImpl impl;
    4.22 +
    4.23 +    char **outputDevices;
    4.24 +    int outputDeviceCount;
    4.25 +
    4.26 +    char **inputDevices;
    4.27 +    int inputDeviceCount;
    4.28  } SDL_AudioDriver;
    4.29  
    4.30  
     5.1 --- a/src/audio/bsd/SDL_bsdaudio.c	Thu Aug 04 01:07:09 2011 -0400
     5.2 +++ b/src/audio/bsd/SDL_bsdaudio.c	Thu Aug 04 00:31:11 2011 -0400
     5.3 @@ -50,91 +50,23 @@
     5.4  #define BSD_AUDIO_DRIVER_DESC         "Native OpenBSD audio"
     5.5  #endif
     5.6  
     5.7 -/* Open the audio device for playback, and don't block if busy */
     5.8 -/* #define USE_BLOCKING_WRITES */
     5.9 -
    5.10  /* Use timer for synchronization */
    5.11  /* #define USE_TIMER_SYNC */
    5.12  
    5.13  /* #define DEBUG_AUDIO */
    5.14  /* #define DEBUG_AUDIO_STREAM */
    5.15  
    5.16 -#ifdef USE_BLOCKING_WRITES
    5.17 -#define OPEN_FLAGS_OUTPUT O_WRONLY
    5.18 -#define OPEN_FLAGS_INPUT O_RDONLY
    5.19 -#else
    5.20 -#define OPEN_FLAGS_OUTPUT (O_WRONLY|O_NONBLOCK)
    5.21 -#define OPEN_FLAGS_INPUT (O_RDONLY|O_NONBLOCK)
    5.22 -#endif
    5.23 -
    5.24 -/* !!! FIXME: so much cut and paste with dsp target... */
    5.25 -static char **outputDevices = NULL;
    5.26 -static int outputDeviceCount = 0;
    5.27 -static char **inputDevices = NULL;
    5.28 -static int inputDeviceCount = 0;
    5.29 -
    5.30 -static inline void
    5.31 -free_device_list(char ***devs, int *count)
    5.32 -{
    5.33 -    SDL_FreeUnixAudioDevices(devs, count);
    5.34 -}
    5.35 -
    5.36 -static inline void
    5.37 -build_device_list(int iscapture, char ***devs, int *count)
    5.38 -{
    5.39 -    const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
    5.40 -    free_device_list(devs, count);
    5.41 -    SDL_EnumUnixAudioDevices(flags, 0, NULL, devs, count);
    5.42 -}
    5.43 -
    5.44 -static inline void
    5.45 -build_device_lists(void)
    5.46 -{
    5.47 -    build_device_list(0, &outputDevices, &outputDeviceCount);
    5.48 -    build_device_list(1, &inputDevices, &inputDeviceCount);
    5.49 -}
    5.50 -
    5.51 -
    5.52 -static inline void
    5.53 -free_device_lists(void)
    5.54 -{
    5.55 -    free_device_list(&outputDevices, &outputDeviceCount);
    5.56 -    free_device_list(&inputDevices, &inputDeviceCount);
    5.57 -}
    5.58 -
    5.59  
    5.60  static void
    5.61  BSDAUDIO_Deinitialize(void)
    5.62  {
    5.63 -    free_device_lists();
    5.64  }
    5.65  
    5.66  
    5.67 -static int
    5.68 -BSDAUDIO_DetectDevices(int iscapture)
    5.69 +static void
    5.70 +BSDAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
    5.71  {
    5.72 -    if (iscapture) {
    5.73 -        build_device_list(1, &inputDevices, &inputDeviceCount);
    5.74 -        return inputDeviceCount;
    5.75 -    } else {
    5.76 -        build_device_list(0, &outputDevices, &outputDeviceCount);
    5.77 -        return outputDeviceCount;
    5.78 -    }
    5.79 -
    5.80 -    return 0;                   /* shouldn't ever hit this. */
    5.81 -}
    5.82 -
    5.83 -static const char *
    5.84 -BSDAUDIO_GetDeviceName(int index, int iscapture)
    5.85 -{
    5.86 -    if ((iscapture) && (index < inputDeviceCount)) {
    5.87 -        return inputDevices[index];
    5.88 -    } else if ((!iscapture) && (index < outputDeviceCount)) {
    5.89 -        return outputDevices[index];
    5.90 -    }
    5.91 -
    5.92 -    SDL_SetError("No such device");
    5.93 -    return NULL;
    5.94 +    SDL_EnumUnixAudioDevices(iscapture, 0, NULL, addfn);
    5.95  }
    5.96  
    5.97  
    5.98 @@ -318,12 +250,11 @@
    5.99      /* We don't care what the devname is...we'll try to open anything. */
   5.100      /*  ...but default to first name in the list... */
   5.101      if (devname == NULL) {
   5.102 -        if (((iscapture) && (inputDeviceCount == 0)) ||
   5.103 -            ((!iscapture) && (outputDeviceCount == 0))) {
   5.104 +        devname = SDL_GetAudioDeviceName(0, iscapture);
   5.105 +        if (devname == NULL) {
   5.106              SDL_SetError("No such audio device");
   5.107              return 0;
   5.108          }
   5.109 -        devname = ((iscapture) ? inputDevices[0] : outputDevices[0]);
   5.110      }
   5.111  
   5.112      /* Initialize all variables that we clean on shutdown */
   5.113 @@ -434,7 +365,6 @@
   5.114  {
   5.115      /* Set the function pointers */
   5.116      impl->DetectDevices = BSDAUDIO_DetectDevices;
   5.117 -    impl->GetDeviceName = BSDAUDIO_GetDeviceName;
   5.118      impl->OpenDevice = BSDAUDIO_OpenDevice;
   5.119      impl->PlayDevice = BSDAUDIO_PlayDevice;
   5.120      impl->WaitDevice = BSDAUDIO_WaitDevice;
   5.121 @@ -442,8 +372,6 @@
   5.122      impl->CloseDevice = BSDAUDIO_CloseDevice;
   5.123      impl->Deinitialize = BSDAUDIO_Deinitialize;
   5.124  
   5.125 -    build_device_lists();
   5.126 -
   5.127      return 1;   /* this audio target is available. */
   5.128  }
   5.129  
     6.1 --- a/src/audio/directsound/SDL_directsound.c	Thu Aug 04 01:07:09 2011 -0400
     6.2 +++ b/src/audio/directsound/SDL_directsound.c	Thu Aug 04 00:31:11 2011 -0400
     6.3 @@ -148,96 +148,28 @@
     6.4  }
     6.5  
     6.6  
     6.7 -
     6.8 -/* !!! FIXME: this is a cut and paste of SDL_FreeUnixAudioDevices(),
     6.9 - * !!! FIXME:  which is more proof this needs to be managed in SDL_audio.c
    6.10 - * !!! FIXME:  and not in drivers.
    6.11 - */
    6.12 -static void
    6.13 -FreeDSoundAudioDevices(char ***devices, int *devCount)
    6.14 +static BOOL CALLBACK
    6.15 +FindAllDevs(LPGUID guid, LPCWSTR desc, LPCWSTR module, LPVOID data)
    6.16  {
    6.17 -    int i = *devCount;
    6.18 -    if ((i > 0) && (*devices != NULL)) {
    6.19 -        while (i--) {
    6.20 -            SDL_free((*devices)[i]);
    6.21 +    SDL_AddAudioDevice addfn = (SDL_AddAudioDevice) data;
    6.22 +    if (guid != NULL) {  /* skip default device */
    6.23 +        char *str = utf16_to_utf8(desc);
    6.24 +        if (str != NULL) {
    6.25 +            addfn(str);
    6.26 +            SDL_free(str);  /* addfn() makes a copy of this string. */
    6.27          }
    6.28      }
    6.29 -
    6.30 -    if (*devices != NULL) {
    6.31 -        SDL_free(*devices);
    6.32 -    }
    6.33 -
    6.34 -    *devices = NULL;
    6.35 -    *devCount = 0;
    6.36 -}
    6.37 -
    6.38 -
    6.39 -typedef struct FindAllDevsData
    6.40 -{
    6.41 -    const char **devs;
    6.42 -    unsigned int devcount;
    6.43 -} FindAllDevsData;
    6.44 -
    6.45 -static BOOL CALLBACK
    6.46 -FindAllDevs(LPGUID guid, LPCWSTR desc, LPCWSTR module, LPVOID _data)
    6.47 -{
    6.48 -    FindAllDevsData *data = (FindAllDevsData *) _data;
    6.49 -    void *ptr;
    6.50 -    char *name;
    6.51 -
    6.52 -    if (guid == NULL)
    6.53 -        return TRUE;  /* skip default device, go to the next one. */
    6.54 -
    6.55 -    ptr = SDL_realloc(data->devs, ((data->devcount) + 1) * sizeof(char *));
    6.56 -    if (ptr == NULL)
    6.57 -        return TRUE;  /* oh well. */
    6.58 -
    6.59 -    data->devs = (const char **) ptr;
    6.60 -    name = utf16_to_utf8(desc);
    6.61 -    if (name != NULL)
    6.62 -        data->devs[data->devcount++] = name;
    6.63 -
    6.64      return TRUE;  /* keep enumerating. */
    6.65  }
    6.66  
    6.67 -static char **outputDevices = NULL;
    6.68 -static int outputDeviceCount = 0;
    6.69 -static char **inputDevices = NULL;
    6.70 -static int inputDeviceCount = 0;
    6.71 -
    6.72 -static int
    6.73 -DSOUND_DetectDevices(int iscapture)
    6.74 +static void
    6.75 +DSOUND_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
    6.76  {
    6.77 -    FindAllDevsData data;
    6.78 -    data.devs = NULL;
    6.79 -    data.devcount = 0;
    6.80 -
    6.81      if (iscapture) {
    6.82 -        FreeDSoundAudioDevices(&inputDevices, &inputDeviceCount);
    6.83 -        pDirectSoundCaptureEnumerateW(FindAllDevs, &devs);
    6.84 -        inputDevices = data.devs;
    6.85 -        inputDeviceCount = data.devcount;
    6.86 +        pDirectSoundCaptureEnumerateW(FindAllDevs, addfn);
    6.87      } else {
    6.88 -        FreeDSoundAudioDevices(&outputDevices, &outputDeviceCount);
    6.89 -        pDirectSoundEnumerateW(FindAllDevs, &devs);
    6.90 -        outputDevices = data.devs;
    6.91 -        outputDeviceCount = data.devcount;
    6.92 +        pDirectSoundEnumerateW(FindAllDevs, addfn);
    6.93      }
    6.94 -
    6.95 -    return data.devcount;
    6.96 -}
    6.97 -
    6.98 -static const char *
    6.99 -DSOUND_GetDeviceName(int index, int iscapture)
   6.100 -{
   6.101 -    if ((iscapture) && (index < inputDeviceCount)) {
   6.102 -        return inputDevices[index];
   6.103 -    } else if ((!iscapture) && (index < outputDeviceCount)) {
   6.104 -        return outputDevices[index];
   6.105 -    }
   6.106 -
   6.107 -    SDL_SetError("No such device");
   6.108 -    return NULL;
   6.109  }
   6.110  
   6.111  
   6.112 @@ -589,8 +521,6 @@
   6.113  static void
   6.114  DSOUND_Deinitialize(void)
   6.115  {
   6.116 -    FreeDSoundAudioDevices(&inputDevices, &inputDeviceCount);
   6.117 -    FreeDSoundAudioDevices(&outputDevices, &outputDeviceCount);
   6.118      DSOUND_Unload();
   6.119  }
   6.120  
   6.121 @@ -604,7 +534,6 @@
   6.122  
   6.123      /* Set the function pointers */
   6.124      impl->DetectDevices = DSOUND_DetectDevices;
   6.125 -    impl->GetDeviceName = DSOUND_GetDeviceName;
   6.126      impl->OpenDevice = DSOUND_OpenDevice;
   6.127      impl->PlayDevice = DSOUND_PlayDevice;
   6.128      impl->WaitDevice = DSOUND_WaitDevice;
     7.1 --- a/src/audio/dsp/SDL_dspaudio.c	Thu Aug 04 01:07:09 2011 -0400
     7.2 +++ b/src/audio/dsp/SDL_dspaudio.c	Thu Aug 04 00:31:11 2011 -0400
     7.3 @@ -50,77 +50,16 @@
     7.4  /* The tag name used by DSP audio */
     7.5  #define DSP_DRIVER_NAME         "dsp"
     7.6  
     7.7 -/* Open the audio device for playback, and don't block if busy */
     7.8 -#define OPEN_FLAGS_OUTPUT    (O_WRONLY|O_NONBLOCK)
     7.9 -#define OPEN_FLAGS_INPUT    (O_RDONLY|O_NONBLOCK)
    7.10 -
    7.11 -static char **outputDevices = NULL;
    7.12 -static int outputDeviceCount = 0;
    7.13 -static char **inputDevices = NULL;
    7.14 -static int inputDeviceCount = 0;
    7.15 -
    7.16 -static inline void
    7.17 -free_device_list(char ***devs, int *count)
    7.18 -{
    7.19 -    SDL_FreeUnixAudioDevices(devs, count);
    7.20 -}
    7.21 -
    7.22 -static inline void
    7.23 -build_device_list(int iscapture, char ***devs, int *count)
    7.24 -{
    7.25 -    const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
    7.26 -    free_device_list(devs, count);
    7.27 -    SDL_EnumUnixAudioDevices(flags, 0, NULL, devs, count);
    7.28 -}
    7.29 -
    7.30 -static inline void
    7.31 -build_device_lists(void)
    7.32 -{
    7.33 -    build_device_list(0, &outputDevices, &outputDeviceCount);
    7.34 -    build_device_list(1, &inputDevices, &inputDeviceCount);
    7.35 -}
    7.36 -
    7.37 -
    7.38 -static inline void
    7.39 -free_device_lists(void)
    7.40 -{
    7.41 -    free_device_list(&outputDevices, &outputDeviceCount);
    7.42 -    free_device_list(&inputDevices, &inputDeviceCount);
    7.43 -}
    7.44 -
    7.45 -
    7.46  static void
    7.47  DSP_Deinitialize(void)
    7.48  {
    7.49 -    free_device_lists();
    7.50  }
    7.51  
    7.52  
    7.53 -static int
    7.54 +static void
    7.55  DSP_DetectDevices(int iscapture)
    7.56  {
    7.57 -    if (iscapture) {
    7.58 -        build_device_list(1, &inputDevices, &inputDeviceCount);
    7.59 -        return inputDeviceCount;
    7.60 -    } else {
    7.61 -        build_device_list(0, &outputDevices, &outputDeviceCount);
    7.62 -        return outputDeviceCount;
    7.63 -    }
    7.64 -
    7.65 -    return 0;                   /* shouldn't ever hit this. */
    7.66 -}
    7.67 -
    7.68 -static const char *
    7.69 -DSP_GetDeviceName(int index, int iscapture)
    7.70 -{
    7.71 -    if ((iscapture) && (index < inputDeviceCount)) {
    7.72 -        return inputDevices[index];
    7.73 -    } else if ((!iscapture) && (index < outputDeviceCount)) {
    7.74 -        return outputDevices[index];
    7.75 -    }
    7.76 -
    7.77 -    SDL_SetError("No such device");
    7.78 -    return NULL;
    7.79 +    SDL_EnumUnixAudioDevices(iscapture, 0, NULL);
    7.80  }
    7.81  
    7.82  
    7.83 @@ -154,12 +93,11 @@
    7.84      /* We don't care what the devname is...we'll try to open anything. */
    7.85      /*  ...but default to first name in the list... */
    7.86      if (devname == NULL) {
    7.87 -        if (((iscapture) && (inputDeviceCount == 0)) ||
    7.88 -            ((!iscapture) && (outputDeviceCount == 0))) {
    7.89 +        devname = SDL_GetAudioDeviceName(0, iscapture);
    7.90 +        if (devname == NULL) {
    7.91              SDL_SetError("No such audio device");
    7.92              return 0;
    7.93          }
    7.94 -        devname = ((iscapture) ? inputDevices[0] : outputDevices[0]);
    7.95      }
    7.96  
    7.97      /* Make sure fragment size stays a power of 2, or OSS fails. */
    7.98 @@ -369,15 +307,12 @@
    7.99  {
   7.100      /* Set the function pointers */
   7.101      impl->DetectDevices = DSP_DetectDevices;
   7.102 -    impl->GetDeviceName = DSP_GetDeviceName;
   7.103      impl->OpenDevice = DSP_OpenDevice;
   7.104      impl->PlayDevice = DSP_PlayDevice;
   7.105      impl->GetDeviceBuf = DSP_GetDeviceBuf;
   7.106      impl->CloseDevice = DSP_CloseDevice;
   7.107      impl->Deinitialize = DSP_Deinitialize;
   7.108  
   7.109 -    build_device_lists();
   7.110 -
   7.111      return 1;   /* this audio target is available. */
   7.112  }
   7.113  
     8.1 --- a/src/audio/macosx/SDL_coreaudio.c	Thu Aug 04 01:07:09 2011 -0400
     8.2 +++ b/src/audio/macosx/SDL_coreaudio.c	Thu Aug 04 00:31:11 2011 -0400
     8.3 @@ -34,34 +34,36 @@
     8.4  
     8.5  #define DEBUG_COREAUDIO 0
     8.6  
     8.7 -typedef struct COREAUDIO_DeviceList
     8.8 -{
     8.9 -    AudioDeviceID id;
    8.10 -    const char *name;
    8.11 -} COREAUDIO_DeviceList;
    8.12 -
    8.13 -static COREAUDIO_DeviceList *inputDevices = NULL;
    8.14 -static int inputDeviceCount = 0;
    8.15 -static COREAUDIO_DeviceList *outputDevices = NULL;
    8.16 -static int outputDeviceCount = 0;
    8.17 +typedef void (*addDevFn)(const char *name, AudioDeviceID devId, void *data);
    8.18  
    8.19  static void
    8.20 -free_device_list(COREAUDIO_DeviceList ** devices, int *devCount)
    8.21 +addToDevList(const char *name, AudioDeviceID devId, void *data)
    8.22  {
    8.23 -    if (*devices) {
    8.24 -        int i = *devCount;
    8.25 -        while (i--)
    8.26 -            SDL_free((void *) (*devices)[i].name);
    8.27 -        SDL_free(*devices);
    8.28 -        *devices = NULL;
    8.29 -    }
    8.30 -    *devCount = 0;
    8.31 +    SDL_AddAudioDevice addfn = (SDL_AddAudioDevice) data;
    8.32 +    addfn(name);
    8.33  }
    8.34  
    8.35 +typedef struct
    8.36 +{
    8.37 +    const char *findname;
    8.38 +    AudioDeviceID devId;
    8.39 +    int found;
    8.40 +} FindDevIdData;
    8.41  
    8.42  static void
    8.43 -build_device_list(int iscapture, COREAUDIO_DeviceList ** devices,
    8.44 -                  int *devCount)
    8.45 +findDevId(const char *name, AudioDeviceID devId, void *_data)
    8.46 +{
    8.47 +    FindDevIdData *data = (FindDevIdData *) _data;
    8.48 +    if (!data->found) {
    8.49 +        if (SDL_strcmp(name, data->findname) == 0) {
    8.50 +            data->found = 1;
    8.51 +            data->devId = devId;
    8.52 +        }
    8.53 +    }
    8.54 +}
    8.55 +
    8.56 +static void
    8.57 +build_device_list(int iscapture, addDevFn addfn, void *addfndata)
    8.58  {
    8.59      Boolean outWritable = 0;
    8.60      OSStatus result = noErr;
    8.61 @@ -70,8 +72,6 @@
    8.62      UInt32 i = 0;
    8.63      UInt32 max = 0;
    8.64  
    8.65 -    free_device_list(devices, devCount);
    8.66 -
    8.67      result = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,
    8.68                                            &size, &outWritable);
    8.69  
    8.70 @@ -82,16 +82,12 @@
    8.71      if (devs == NULL)
    8.72          return;
    8.73  
    8.74 -    max = size / sizeof(AudioDeviceID);
    8.75 -    *devices = (COREAUDIO_DeviceList *) SDL_malloc(max * sizeof(**devices));
    8.76 -    if (*devices == NULL)
    8.77 -        return;
    8.78 -
    8.79      result = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
    8.80                                        &size, devs);
    8.81      if (result != kAudioHardwareNoError)
    8.82          return;
    8.83  
    8.84 +    max = size / sizeof (AudioDeviceID);
    8.85      for (i = 0; i < max; i++) {
    8.86          CFStringRef cfstr = NULL;
    8.87          char *ptr = NULL;
    8.88 @@ -156,9 +152,7 @@
    8.89              usable = (len > 0);
    8.90          }
    8.91  
    8.92 -        if (!usable) {
    8.93 -            SDL_free(ptr);
    8.94 -        } else {
    8.95 +        if (usable) {
    8.96              ptr[len] = '\0';
    8.97  
    8.98  #if DEBUG_COREAUDIO
    8.99 @@ -166,80 +160,22 @@
   8.100                     ((iscapture) ? "capture" : "output"),
   8.101                     (int) *devCount, ptr, (int) dev);
   8.102  #endif
   8.103 -
   8.104 -            (*devices)[*devCount].id = dev;
   8.105 -            (*devices)[*devCount].name = ptr;
   8.106 -            (*devCount)++;
   8.107 +            addfn(ptr, dev, addfndata);
   8.108          }
   8.109 +        SDL_free(ptr);  /* addfn() would have copied the string. */
   8.110      }
   8.111  }
   8.112  
   8.113 -static inline void
   8.114 -build_device_lists(void)
   8.115 +static void
   8.116 +COREAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
   8.117  {
   8.118 -    build_device_list(0, &outputDevices, &outputDeviceCount);
   8.119 -    build_device_list(1, &inputDevices, &inputDeviceCount);
   8.120 -}
   8.121 -
   8.122 -
   8.123 -static inline void
   8.124 -free_device_lists(void)
   8.125 -{
   8.126 -    free_device_list(&outputDevices, &outputDeviceCount);
   8.127 -    free_device_list(&inputDevices, &inputDeviceCount);
   8.128 -}
   8.129 -
   8.130 -
   8.131 -static int
   8.132 -find_device_id(const char *devname, int iscapture, AudioDeviceID * id)
   8.133 -{
   8.134 -    int i = ((iscapture) ? inputDeviceCount : outputDeviceCount);
   8.135 -    COREAUDIO_DeviceList *devs = ((iscapture) ? inputDevices : outputDevices);
   8.136 -    while (i--) {
   8.137 -        if (SDL_strcmp(devname, devs->name) == 0) {
   8.138 -            *id = devs->id;
   8.139 -            return 1;
   8.140 -        }
   8.141 -        devs++;
   8.142 -    }
   8.143 -
   8.144 -    return 0;
   8.145 -}
   8.146 -
   8.147 -
   8.148 -static int
   8.149 -COREAUDIO_DetectDevices(int iscapture)
   8.150 -{
   8.151 -    if (iscapture) {
   8.152 -        build_device_list(1, &inputDevices, &inputDeviceCount);
   8.153 -        return inputDeviceCount;
   8.154 -    } else {
   8.155 -        build_device_list(0, &outputDevices, &outputDeviceCount);
   8.156 -        return outputDeviceCount;
   8.157 -    }
   8.158 -
   8.159 -    return 0;                   /* shouldn't ever hit this. */
   8.160 -}
   8.161 -
   8.162 -
   8.163 -static const char *
   8.164 -COREAUDIO_GetDeviceName(int index, int iscapture)
   8.165 -{
   8.166 -    if ((iscapture) && (index < inputDeviceCount)) {
   8.167 -        return inputDevices[index].name;
   8.168 -    } else if ((!iscapture) && (index < outputDeviceCount)) {
   8.169 -        return outputDevices[index].name;
   8.170 -    }
   8.171 -
   8.172 -    SDL_SetError("No such device");
   8.173 -    return NULL;
   8.174 +    build_device_list(iscapture, addToDevList, addfn);
   8.175  }
   8.176  
   8.177  
   8.178  static void
   8.179  COREAUDIO_Deinitialize(void)
   8.180  {
   8.181 -    free_device_lists();
   8.182  }
   8.183  
   8.184  
   8.185 @@ -378,10 +314,15 @@
   8.186          result = AudioHardwareGetProperty(propid, &size, &devid);
   8.187          CHECK_RESULT("AudioHardwareGetProperty (default device)");
   8.188      } else {
   8.189 -        if (!find_device_id(devname, iscapture, &devid)) {
   8.190 +        FindDevIdData data;
   8.191 +        SDL_zero(data);
   8.192 +        data.findname = devname;
   8.193 +        build_device_list(iscapture, findDevId, &data);
   8.194 +        if (!data.found) {
   8.195              SDL_SetError("CoreAudio: No such audio device.");
   8.196              return 0;
   8.197          }
   8.198 +        devid = data.devId;
   8.199      }
   8.200  
   8.201      size = sizeof(alive);
   8.202 @@ -565,14 +506,11 @@
   8.203  {
   8.204      /* Set the function pointers */
   8.205      impl->DetectDevices = COREAUDIO_DetectDevices;
   8.206 -    impl->GetDeviceName = COREAUDIO_GetDeviceName;
   8.207      impl->OpenDevice = COREAUDIO_OpenDevice;
   8.208      impl->CloseDevice = COREAUDIO_CloseDevice;
   8.209      impl->Deinitialize = COREAUDIO_Deinitialize;
   8.210      impl->ProvidesOwnCallbackThread = 1;
   8.211  
   8.212 -    build_device_lists();       /* do an initial check for devices... */
   8.213 -
   8.214      return 1;   /* this audio target is available. */
   8.215  }
   8.216  
     9.1 --- a/src/audio/qsa/SDL_qsa_audio.c	Thu Aug 04 01:07:09 2011 -0400
     9.2 +++ b/src/audio/qsa/SDL_qsa_audio.c	Thu Aug 04 00:31:11 2011 -0400
     9.3 @@ -651,8 +651,8 @@
     9.4      return 1;
     9.5  }
     9.6  
     9.7 -static int
     9.8 -QSA_DetectDevices(int iscapture)
     9.9 +static void
    9.10 +QSA_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
    9.11  {
    9.12      uint32_t it;
    9.13      uint32_t cards;
    9.14 @@ -667,7 +667,7 @@
    9.15      /* of available audio devices                                 */
    9.16      if (cards == 0) {
    9.17          /* We have no any available audio devices */
    9.18 -        return 0;
    9.19 +        return;
    9.20      }
    9.21  
    9.22      /* Find requested devices by type */
    9.23 @@ -702,6 +702,7 @@
    9.24                              devices;
    9.25                          status = snd_pcm_close(handle);
    9.26                          if (status == EOK) {
    9.27 +                            addfn(qsa_playback_device[qsa_playback_devices].name);
    9.28                              qsa_playback_devices++;
    9.29                          }
    9.30                      } else {
    9.31 @@ -757,6 +758,7 @@
    9.32                              devices;
    9.33                          status = snd_pcm_close(handle);
    9.34                          if (status == EOK) {
    9.35 +                            addfn(qsa_capture_device[qsa_capture_devices].name);
    9.36                              qsa_capture_devices++;
    9.37                          }
    9.38                      } else {
    9.39 @@ -782,31 +784,6 @@
    9.40              }
    9.41          }
    9.42      }
    9.43 -
    9.44 -    /* Return amount of available playback or capture devices */
    9.45 -    if (!iscapture) {
    9.46 -        return qsa_playback_devices;
    9.47 -    } else {
    9.48 -        return qsa_capture_devices;
    9.49 -    }
    9.50 -}
    9.51 -
    9.52 -static const char *
    9.53 -QSA_GetDeviceName(int index, int iscapture)
    9.54 -{
    9.55 -    if (!iscapture) {
    9.56 -        if (index >= qsa_playback_devices) {
    9.57 -            return "No such playback device";
    9.58 -        }
    9.59 -
    9.60 -        return qsa_playback_device[index].name;
    9.61 -    } else {
    9.62 -        if (index >= qsa_capture_devices) {
    9.63 -            return "No such capture device";
    9.64 -        }
    9.65 -
    9.66 -        return qsa_capture_device[index].name;
    9.67 -    }
    9.68  }
    9.69  
    9.70  static void
    9.71 @@ -857,7 +834,6 @@
    9.72      /* DeviceLock and DeviceUnlock functions are used default,   */
    9.73      /* provided by SDL, which uses pthread_mutex for lock/unlock */
    9.74      impl->DetectDevices = QSA_DetectDevices;
    9.75 -    impl->GetDeviceName = QSA_GetDeviceName;
    9.76      impl->OpenDevice = QSA_OpenDevice;
    9.77      impl->ThreadInit = QSA_ThreadInit;
    9.78      impl->WaitDevice = QSA_WaitDevice;
    10.1 --- a/src/audio/winmm/SDL_winmm.c	Thu Aug 04 01:07:09 2011 -0400
    10.2 +++ b/src/audio/winmm/SDL_winmm.c	Thu Aug 04 00:31:11 2011 -0400
    10.3 @@ -33,89 +33,33 @@
    10.4  #include "win_ce_semaphore.h"
    10.5  #endif
    10.6  
    10.7 -
    10.8 -/* !!! FIXME: this is a cut and paste of SDL_FreeUnixAudioDevices(),
    10.9 - * !!! FIXME:  which is more proof this needs to be managed in SDL_audio.c
   10.10 - * !!! FIXME:  and not in drivers.
   10.11 - */
   10.12 -static void
   10.13 -FreeWaveOutAudioDevices(char ***devices, int *devCount)
   10.14 -{
   10.15 -    int i = *devCount;
   10.16 -    if ((i > 0) && (*devices != NULL)) {
   10.17 -        while (i--) {
   10.18 -            SDL_free((*devices)[i]);
   10.19 -        }
   10.20 -    }
   10.21 -
   10.22 -    if (*devices != NULL) {
   10.23 -        SDL_free(*devices);
   10.24 -    }
   10.25 -
   10.26 -    *devices = NULL;
   10.27 -    *devCount = 0;
   10.28 +#define DETECT_DEV_IMPL(typ, capstyp) \
   10.29 +static void DetectWave##typ##Devs(SDL_AddAudioDevice addfn) { \
   10.30 +    const UINT devcount = wave##typ##GetNumDevs(); \
   10.31 +    capstyp caps; \
   10.32 +    UINT i; \
   10.33 +    for (i = 0; i < devcount; i++) { \
   10.34 +        if (wave##typ##GetDevCaps(i,&caps,sizeof(caps))==MMSYSERR_NOERROR) { \
   10.35 +            char *name = WIN_StringToUTF8(caps.szPname); \
   10.36 +            if (name != NULL) { \
   10.37 +                addfn(name); \
   10.38 +                SDL_free(name); \
   10.39 +            } \
   10.40 +        } \
   10.41 +    } \
   10.42  }
   10.43  
   10.44 -static char **outputDevices = NULL;
   10.45 -static int outputDeviceCount = 0;
   10.46 -static char **inputDevices = NULL;
   10.47 -static int inputDeviceCount = 0;
   10.48 +DETECT_DEV_IMPL(Out, WAVEOUTCAPS)
   10.49 +DETECT_DEV_IMPL(In, WAVEINCAPS)
   10.50  
   10.51 -static int
   10.52 -DetectWaveOutDevices(void)
   10.53 +static void
   10.54 +WINMM_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
   10.55  {
   10.56 -    UINT i;
   10.57 -    const UINT devcount = waveOutGetNumDevs();
   10.58 -    WAVEOUTCAPS caps;
   10.59 -    FreeWaveOutAudioDevices(&outputDevices, &outputDeviceCount);
   10.60 -    outputDevices = (const char **) SDL_malloc(sizeof (char *) * devcount);
   10.61 -    for (i = 0; i < devcount; i++) {
   10.62 -        if (waveOutGetDevCaps(i, &caps, sizeof (caps)) == MMSYSERR_NOERROR) {
   10.63 -            outputDevices[outputDeviceCount] = WIN_StringToUTF8(caps.szPname);
   10.64 -            if (outputDevices[outputDeviceCount] != NULL) {
   10.65 -                outputDeviceCount++;
   10.66 -            }
   10.67 -        }
   10.68 +    if (iscapture) {
   10.69 +        DetectWaveInDevs(addfn);
   10.70 +    } else {
   10.71 +        DetectWaveOutDevs(addfn);
   10.72      }
   10.73 -    return outputDeviceCount;
   10.74 -}
   10.75 -
   10.76 -static int
   10.77 -DetectWaveInDevices(void)
   10.78 -{
   10.79 -    UINT i;
   10.80 -    const UINT devcount = waveInGetNumDevs();
   10.81 -    WAVEINCAPS caps;
   10.82 -    FreeWaveInAudioDevices(&inputDevices, &inputDeviceCount);
   10.83 -    inputDevices = (const char **) SDL_malloc(sizeof (char *) * devcount);
   10.84 -    for (i = 0; i < devcount; i++) {
   10.85 -        if (waveInGetDevCaps(i, &caps, sizeof (caps)) == MMSYSERR_NOERROR) {
   10.86 -            inputDevices[inputDeviceCount] = WIN_StringToUTF8(caps.szPname);
   10.87 -            if (inputDevices[inputDeviceCount] != NULL) {
   10.88 -                inputDeviceCount++;
   10.89 -            }
   10.90 -        }
   10.91 -    }
   10.92 -    return inputDeviceCount;
   10.93 -}
   10.94 -
   10.95 -static int
   10.96 -WINMM_DetectDevices(int iscapture)
   10.97 -{
   10.98 -    return (iscapture) ? DetectWaveInDevices() : DetectWaveOutDevices();
   10.99 -}
  10.100 -
  10.101 -static const char *
  10.102 -WINMM_GetDeviceName(int index, int iscapture)
  10.103 -{
  10.104 -    if ((iscapture) && (index < inputDeviceCount)) {
  10.105 -        return inputDevices[index];
  10.106 -    } else if ((!iscapture) && (index < outputDeviceCount)) {
  10.107 -        return outputDevices[index];
  10.108 -    }
  10.109 -
  10.110 -    SDL_SetError("No such device");
  10.111 -    return NULL;
  10.112  }
  10.113  
  10.114  static void CALLBACK
  10.115 @@ -448,7 +392,6 @@
  10.116  {
  10.117      /* Set the function pointers */
  10.118      impl->DetectDevices = WINMM_DetectDevices;
  10.119 -    impl->GetDeviceName = WINMM_GetDeviceName;
  10.120      impl->OpenDevice = WINMM_OpenDevice;
  10.121      impl->PlayDevice = WINMM_PlayDevice;
  10.122      impl->WaitDevice = WINMM_WaitDevice;
    11.1 --- a/src/audio/xaudio2/SDL_xaudio2.c	Thu Aug 04 01:07:09 2011 -0400
    11.2 +++ b/src/audio/xaudio2/SDL_xaudio2.c	Thu Aug 04 00:31:11 2011 -0400
    11.3 @@ -24,35 +24,9 @@
    11.4  #include "../SDL_audio_c.h"
    11.5  #include "SDL_assert.h"
    11.6  
    11.7 -#define INITGUID 1
    11.8 +#define INITGUID 1
    11.9  #include "SDL_xaudio2.h"
   11.10  
   11.11 -/* !!! FIXME: this is a cut and paste of SDL_FreeUnixAudioDevices(),
   11.12 - * !!! FIXME:  which is more proof this needs to be managed in SDL_audio.c
   11.13 - * !!! FIXME:  and not in drivers.
   11.14 - */
   11.15 -static void
   11.16 -FreeXAudio2AudioDevices(char ***devices, int *devCount)
   11.17 -{
   11.18 -    int i = *devCount;
   11.19 -    if ((i > 0) && (*devices != NULL)) {
   11.20 -        while (i--) {
   11.21 -            SDL_free((*devices)[i]);
   11.22 -        }
   11.23 -    }
   11.24 -
   11.25 -    if (*devices != NULL) {
   11.26 -        SDL_free(*devices);
   11.27 -    }
   11.28 -
   11.29 -    *devices = NULL;
   11.30 -    *devCount = 0;
   11.31 -}
   11.32 -
   11.33 -
   11.34 -static char **outputDevices = NULL;
   11.35 -static int outputDeviceCount = 0;
   11.36 -
   11.37  static __inline__ char *
   11.38  utf16_to_utf8(const WCHAR *S)
   11.39  {
   11.40 @@ -61,59 +35,38 @@
   11.41                              (SDL_wcslen(S)+1)*sizeof(WCHAR));
   11.42  }
   11.43  
   11.44 -static int
   11.45 -XAUDIO2_DetectDevices(int iscapture)
   11.46 +static void
   11.47 +XAUDIO2_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
   11.48  {
   11.49      IXAudio2 *ixa2 = NULL;
   11.50      UINT32 devcount = 0;
   11.51      UINT32 i = 0;
   11.52      void *ptr = NULL;
   11.53  
   11.54 -    if (!iscapture) {
   11.55 -        FreeXAudio2AudioDevices(&outputDevices, &outputDeviceCount);
   11.56 -    }
   11.57 -
   11.58      if (iscapture) {
   11.59          SDL_SetError("XAudio2: capture devices unsupported.");
   11.60 -        return 0;
   11.61 +        return;
   11.62      } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
   11.63          SDL_SetError("XAudio2: XAudio2Create() failed.");
   11.64 -        return 0;
   11.65 +        return;
   11.66      } else if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
   11.67          SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
   11.68          IXAudio2_Release(ixa2);
   11.69 -        return 0;
   11.70 -    } else if ((ptr = SDL_malloc(sizeof (char *) * devcount)) == NULL) {
   11.71 -        SDL_OutOfMemory();
   11.72 -        IXAudio2_Release(ixa2);
   11.73 -        return 0;
   11.74 +        return;
   11.75      }
   11.76  
   11.77 -    outputDevices = (char **) ptr;
   11.78      for (i = 0; i < devcount; i++) {
   11.79          XAUDIO2_DEVICE_DETAILS details;
   11.80          if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
   11.81              char *str = utf16_to_utf8(details.DisplayName);
   11.82              if (str != NULL) {
   11.83 -                outputDevices[outputDeviceCount++] = str;
   11.84 +                addfn(str);
   11.85 +                SDL_free(str);  /* addfn() made a copy of the string. */
   11.86              }
   11.87          }
   11.88      }
   11.89  
   11.90      IXAudio2_Release(ixa2);
   11.91 -
   11.92 -    return outputDeviceCount;
   11.93 -}
   11.94 -
   11.95 -static const char *
   11.96 -XAUDIO2_GetDeviceName(int index, int iscapture)
   11.97 -{
   11.98 -    if ((!iscapture) && (index < outputDeviceCount)) {
   11.99 -        return outputDevices[index];
  11.100 -    }
  11.101 -
  11.102 -    SDL_SetError("XAudio2: No such device");
  11.103 -    return NULL;
  11.104  }
  11.105  
  11.106  static void STDMETHODCALLTYPE
  11.107 @@ -436,7 +389,6 @@
  11.108  
  11.109      /* Set the function pointers */
  11.110      impl->DetectDevices = XAUDIO2_DetectDevices;
  11.111 -    impl->GetDeviceName = XAUDIO2_GetDeviceName;
  11.112      impl->OpenDevice = XAUDIO2_OpenDevice;
  11.113      impl->PlayDevice = XAUDIO2_PlayDevice;
  11.114      impl->WaitDevice = XAUDIO2_WaitDevice;