src/audio/SDL_audio.c
changeset 5593 ab22ca13c47f
parent 5592 2e88d0742f4d
child 5596 d4b2f6543074
     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  }