src/audio/SDL_audiodev.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 26 May 2015 21:19:23 -0400
changeset 9649 d7762e30ba24
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Stack hint should look for 0, not -1, and not care about environment variables.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../SDL_internal.h"
    22 
    23 /* Get the name of the audio device we use for output */
    24 
    25 #if SDL_AUDIO_DRIVER_BSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO
    26 
    27 #include <fcntl.h>
    28 #include <sys/types.h>
    29 #include <sys/stat.h>
    30 #include <unistd.h> /* For close() */
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_audiodev_c.h"
    34 
    35 #ifndef _PATH_DEV_DSP
    36 #if defined(__NETBSD__) || defined(__OPENBSD__)
    37 #define _PATH_DEV_DSP  "/dev/audio"
    38 #else
    39 #define _PATH_DEV_DSP  "/dev/dsp"
    40 #endif
    41 #endif
    42 #ifndef _PATH_DEV_DSP24
    43 #define _PATH_DEV_DSP24 "/dev/sound/dsp"
    44 #endif
    45 #ifndef _PATH_DEV_AUDIO
    46 #define _PATH_DEV_AUDIO "/dev/audio"
    47 #endif
    48 
    49 static void
    50 test_device(const int iscapture, const char *fname, int flags, int (*test) (int fd))
    51 {
    52     struct stat sb;
    53     if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
    54         const int audio_fd = open(fname, flags, 0);
    55         if (audio_fd >= 0) {
    56             const int okay = test(audio_fd);
    57             close(audio_fd);
    58             if (okay) {
    59                 static size_t dummyhandle = 0;
    60                 dummyhandle++;
    61                 SDL_assert(dummyhandle != 0);
    62                 SDL_AddAudioDevice(iscapture, fname, (void *) dummyhandle);
    63             }
    64         }
    65     }
    66 }
    67 
    68 static int
    69 test_stub(int fd)
    70 {
    71     return 1;
    72 }
    73 
    74 static void
    75 SDL_EnumUnixAudioDevices_Internal(const int iscapture, const int classic, int (*test)(int))
    76 {
    77     const int flags = iscapture ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT;
    78     const char *audiodev;
    79     char audiopath[1024];
    80 
    81     if (test == NULL)
    82         test = test_stub;
    83 
    84     /* Figure out what our audio device is */
    85     if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
    86         ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
    87         if (classic) {
    88             audiodev = _PATH_DEV_AUDIO;
    89         } else {
    90             struct stat sb;
    91 
    92             /* Added support for /dev/sound/\* in Linux 2.4 */
    93             if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
    94                 && ((stat(_PATH_DEV_DSP24, &sb) == 0)
    95                     && S_ISCHR(sb.st_mode))) {
    96                 audiodev = _PATH_DEV_DSP24;
    97             } else {
    98                 audiodev = _PATH_DEV_DSP;
    99             }
   100         }
   101     }
   102     test_device(iscapture, audiodev, flags, test);
   103 
   104     if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
   105         int instance = 0;
   106         while (instance++ <= 64) {
   107             SDL_snprintf(audiopath, SDL_arraysize(audiopath),
   108                          "%s%d", audiodev, instance);
   109             test_device(iscapture, audiopath, flags, test);
   110         }
   111     }
   112 }
   113 
   114 void
   115 SDL_EnumUnixAudioDevices(const int classic, int (*test)(int))
   116 {
   117     SDL_EnumUnixAudioDevices_Internal(SDL_TRUE, classic, test);
   118     SDL_EnumUnixAudioDevices_Internal(SDL_FALSE, classic, test);
   119 }
   120 
   121 #endif /* Audio driver selection */
   122 
   123 /* vi: set ts=4 sw=4 expandtab: */