From 0f24f49a44a12d139692d0846b2722de2213f1a8 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Thu, 8 Sep 2016 12:05:08 -0700
Subject: Allow specifying the device to open for the examples

---
 examples/common/alhelpers.c | 30 ++++++++++++++++++++++++------
 examples/common/alhelpers.h |  2 +-
 2 files changed, 25 insertions(+), 7 deletions(-)

(limited to 'examples/common')

diff --git a/examples/common/alhelpers.c b/examples/common/alhelpers.c
index 4582321c..43548b5c 100644
--- a/examples/common/alhelpers.c
+++ b/examples/common/alhelpers.c
@@ -29,6 +29,7 @@
  * channel configs and sample types. */
 
 #include <stdio.h>
+#include <string.h>
 
 #include "AL/al.h"
 #include "AL/alc.h"
@@ -37,15 +38,26 @@
 #include "alhelpers.h"
 
 
-/* InitAL opens the default device and sets up a context using default
- * attributes, making the program ready to call OpenAL functions. */
-int InitAL(void)
+/* InitAL opens a device and sets up a context using default attributes, making
+ * the program ready to call OpenAL functions. */
+int InitAL(char ***argv, int *argc)
 {
+    const ALCchar *name;
     ALCdevice *device;
     ALCcontext *ctx;
 
-    /* Open and initialize a device with default settings */
-    device = alcOpenDevice(NULL);
+    /* Open and initialize a device */
+    device = NULL;
+    if(argc && argv && *argc > 1 && strcmp((*argv)[0], "-device") == 0)
+    {
+        device = alcOpenDevice((*argv)[1]);
+        if(!device)
+            fprintf(stderr, "Failed to open \"%s\", trying default\n", (*argv)[1]);
+        (*argv) += 2;
+        (*argc) -= 2;
+    }
+    if(!device)
+        device = alcOpenDevice(NULL);
     if(!device)
     {
         fprintf(stderr, "Could not open a device!\n");
@@ -62,7 +74,13 @@ int InitAL(void)
         return 1;
     }
 
-    printf("Opened \"%s\"\n", alcGetString(device, ALC_DEVICE_SPECIFIER));
+    name = NULL;
+    if(alcIsExtensionPresent(device, "ALC_ENUMERATE_ALL_EXT"))
+        name = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
+    if(!name || alcGetError(device) != AL_NO_ERROR)
+        name = alcGetString(device, ALC_DEVICE_SPECIFIER);
+    printf("Opened \"%s\"\n", name);
+
     return 0;
 }
 
diff --git a/examples/common/alhelpers.h b/examples/common/alhelpers.h
index 1b4d2fbf..9f60df2a 100644
--- a/examples/common/alhelpers.h
+++ b/examples/common/alhelpers.h
@@ -35,7 +35,7 @@ void AL_APIENTRY wrap_BufferSamples(ALuint buffer, ALuint samplerate,
                                     const ALvoid *data);
 
 /* Easy device init/deinit functions. InitAL returns 0 on success. */
-int InitAL(void);
+int InitAL(char ***argv, int *argc);
 void CloseAL(void);
 
 #ifdef __cplusplus
-- 
cgit v1.2.3