audio: All device names reported by SDL must be unique. This means that if you have two devices named "Soundblaster Pro" in your machine, one will be reported as "Soundblaster Pro" and the other as "Soundblaster Pro (2)". This makes it so you can't into a position where one of your devices can't be opened because another is sitting on the same name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 69781ee..f4999f1 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -378,21 +378,57 @@ static int
add_audio_device(const char *name, void *handle, SDL_AudioDeviceItem **devices, int *devCount)
{
int retval = -1;
- const size_t size = sizeof (SDL_AudioDeviceItem) + SDL_strlen(name) + 1;
- SDL_AudioDeviceItem *item = (SDL_AudioDeviceItem *) SDL_malloc(size);
- if (item == NULL) {
- return -1;
- }
+ SDL_AudioDeviceItem *item;
+ const SDL_AudioDeviceItem *i;
+ int dupenum = 0;
SDL_assert(handle != NULL); /* we reserve NULL, audio backends can't use it. */
+ SDL_assert(name != NULL);
+ item = (SDL_AudioDeviceItem *) SDL_malloc(sizeof (SDL_AudioDeviceItem));
+ if (!item) {
+ return SDL_OutOfMemory();
+ }
+
+ item->original_name = SDL_strdup(name);
+ if (!item->original_name) {
+ SDL_free(item);
+ return SDL_OutOfMemory();
+ }
+
+ item->dupenum = 0;
+ item->name = item->original_name;
item->handle = handle;
- SDL_strlcpy(item->name, name, size - sizeof (SDL_AudioDeviceItem));
SDL_LockMutex(current_audio.detectionLock);
+
+ for (i = *devices; i != NULL; i = i->next) {
+ if (SDL_strcmp(name, i->original_name) == 0) {
+ dupenum = i->dupenum + 1;
+ break; /* stop at the highest-numbered dupe. */
+ }
+ }
+
+ if (dupenum) {
+ const size_t len = SDL_strlen(name) + 16;
+ char *replacement = (char *) SDL_malloc(len);
+ if (!replacement) {
+ SDL_UnlockMutex(current_audio.detectionLock);
+ SDL_free(item->original_name);
+ SDL_free(item);
+ SDL_OutOfMemory();
+ return -1;
+ }
+
+ SDL_snprintf(replacement, len, "%s (%d)", name, dupenum + 1);
+ item->dupenum = dupenum;
+ item->name = replacement;
+ }
+
item->next = *devices;
*devices = item;
- retval = (*devCount)++;
+ retval = (*devCount)++; /* !!! FIXME: this should be an atomic increment */
+
SDL_UnlockMutex(current_audio.detectionLock);
return retval;
@@ -420,6 +456,11 @@ free_device_list(SDL_AudioDeviceItem **devices, int *devCount)
if (item->handle != NULL) {
current_audio.impl.FreeDeviceHandle(item->handle);
}
+ /* these two pointers are the same if not a duplicate devname */
+ if (item->name != item->original_name) {
+ SDL_free(item->name);
+ }
+ SDL_free(item->original_name);
SDL_free(item);
}
*devices = NULL;
@@ -977,6 +1018,11 @@ clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *re
} else {
*devices = next;
}
+ /* these two pointers are the same if not a duplicate devname */
+ if (item->name != item->original_name) {
+ SDL_free(item->name);
+ }
+ SDL_free(item->original_name);
SDL_free(item);
}
item = next;
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index f0e1f3d..579dea5 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -98,8 +98,10 @@ typedef struct SDL_AudioDriverImpl
typedef struct SDL_AudioDeviceItem
{
void *handle;
+ char *name;
+ char *original_name;
+ int dupenum;
struct SDL_AudioDeviceItem *next;
- char name[SDL_VARIABLE_LENGTH_ARRAY];
} SDL_AudioDeviceItem;