cleanup SDL_GetAudioDeviceName

- drop unnecessary hascapture check
- call SDL_InvalidParamError in case the index is out of range
This commit is contained in:
pionere 2022-01-19 17:18:47 +01:00 committed by Ryan C. Gordon
parent f91211eb17
commit 113109f839
1 changed files with 14 additions and 23 deletions

View File

@ -1081,38 +1081,29 @@ SDL_GetNumAudioDevices(int iscapture)
const char * const char *
SDL_GetAudioDeviceName(int index, int iscapture) SDL_GetAudioDeviceName(int index, int iscapture)
{ {
const char *retval = NULL; SDL_AudioDeviceItem *item;
int i;
const char *retval;
if (!SDL_GetCurrentAudioDriver()) { if (!SDL_GetCurrentAudioDriver()) {
SDL_SetError("Audio subsystem is not initialized"); SDL_SetError("Audio subsystem is not initialized");
return NULL; return NULL;
} }
if (iscapture && !current_audio.impl.HasCaptureSupport) {
SDL_SetError("No capture support");
return NULL;
}
if (index >= 0) {
SDL_AudioDeviceItem *item;
int i;
SDL_LockMutex(current_audio.detectionLock); SDL_LockMutex(current_audio.detectionLock);
item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount; i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
if (index < i) { if (index >= 0 && index < i) {
for (i--; i > index; i--, item = item->next) { for (i--; i > index; i--, item = item->next) {
SDL_assert(item != NULL); SDL_assert(item != NULL);
} }
SDL_assert(item != NULL); SDL_assert(item != NULL);
retval = item->name; retval = item->name;
} else {
SDL_InvalidParamError("index");
retval = NULL;
} }
SDL_UnlockMutex(current_audio.detectionLock); SDL_UnlockMutex(current_audio.detectionLock);
}
if (retval == NULL) {
SDL_SetError("No such device");
}
return retval; return retval;
} }