Add a simple example that prints out some OpenAL info
[openal-soft.git] / examples / openal-info.c
blob1693e89960cf4884ef8c9f470b28f9adae195c27
1 /*
2 * openal-info: Display information about ALC and AL.
4 * Idea based on glxinfo for OpenGL.
5 * Initial OpenAL version by Erik Hofman <erik@ehofman.com>.
6 * Further hacked by Sven Panne <sven.panne@aedion.de>.
8 */
10 #if HAVE_CONFIG_H
11 #include "config.h"
12 #endif
14 #include <stdio.h>
15 #include <stdlib.h>
17 #include "AL/alc.h"
18 #include "AL/al.h"
19 #include "AL/alext.h"
22 static const int indentation = 4;
23 static const int maxmimumWidth = 79;
25 static void printChar(int c, int *width)
27 putchar(c);
28 *width = ((c == '\n') ? 0 : ((*width) + 1));
31 static void indent(int *width)
33 int i;
34 for(i = 0; i < indentation; i++)
35 printChar(' ', width);
38 static void printExtensions(const char *header, char separator, const char *extensions)
40 int width = 0, start = 0, end = 0;
42 printf("%s:\n", header);
43 if(extensions == NULL || extensions[0] == '\0')
44 return;
46 indent(&width);
47 while (1)
49 if(extensions[end] == separator || extensions[end] == '\0')
51 if(width + end - start + 2 > maxmimumWidth)
53 printChar('\n', &width);
54 indent(&width);
56 while(start < end)
58 printChar(extensions[start], &width);
59 start++;
61 if(extensions[end] == '\0')
62 break;
63 start++;
64 end++;
65 if(extensions[end] == '\0')
66 break;
67 printChar(',', &width);
68 printChar(' ', &width);
70 end++;
72 printChar('\n', &width);
75 static void die(const char *kind, const char *description)
77 fprintf(stderr, "%s error %s occured\n", kind, description);
78 exit(EXIT_FAILURE);
81 static void checkForErrors(void)
84 ALCdevice *device = alcGetContextsDevice(alcGetCurrentContext());
85 ALCenum error = alcGetError(device);
86 if(error != ALC_NO_ERROR)
87 die("ALC", (const char*)alcGetString(device, error));
90 ALenum error = alGetError();
91 if(error != AL_NO_ERROR)
92 die("AL", (const char*)alGetString(error));
96 static void printDevices(ALCenum which, const char *kind)
98 const char *s = alcGetString(NULL, which);
99 checkForErrors();
101 printf("Available %sdevices:\n", kind);
102 while(*s != '\0')
104 printf(" %s\n", s);
105 while(*s++ != '\0')
110 static void printALCInfo (void)
112 ALCint major, minor;
113 ALCdevice *device;
115 if(alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
117 if(alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATE_ALL_EXT") == AL_TRUE)
118 printDevices(ALC_ALL_DEVICES_SPECIFIER, "playback ");
119 else
120 printDevices(ALC_DEVICE_SPECIFIER, "playback ");
121 printDevices(ALC_CAPTURE_DEVICE_SPECIFIER, "capture ");
123 else
124 printf("No device enumeration available\n");
126 device = alcGetContextsDevice(alcGetCurrentContext());
127 checkForErrors();
129 printf("Default device: %s\n",
130 alcGetString(device, ALC_DEFAULT_DEVICE_SPECIFIER));
132 printf("Default capture device: %s\n",
133 alcGetString(device, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));
135 alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &major);
136 alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &minor);
137 checkForErrors();
138 printf("ALC version: %d.%d\n", (int)major, (int)minor);
140 printExtensions("ALC extensions", ' ',
141 alcGetString(device, ALC_EXTENSIONS));
142 checkForErrors();
145 static void printALInfo(void)
147 printf("OpenAL vendor string: %s\n", alGetString(AL_VENDOR));
148 printf("OpenAL renderer string: %s\n", alGetString(AL_RENDERER));
149 printf("OpenAL version string: %s\n", alGetString(AL_VERSION));
150 printExtensions("OpenAL extensions", ' ', alGetString(AL_EXTENSIONS));
151 checkForErrors();
154 int main()
156 ALCdevice *device = alcOpenDevice(NULL);
157 ALCcontext *context = alcCreateContext(device, NULL);
158 alcMakeContextCurrent(context);
159 checkForErrors();
161 printALCInfo();
162 printALInfo();
163 checkForErrors();
165 alcMakeContextCurrent(NULL);
166 alcDestroyContext(context);
167 alcCloseDevice(device);
169 return EXIT_SUCCESS;