src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Aug 2009 18:39:57 +0000
changeset 3227 458e53d8662c
parent 2859 99210400e8b9
child 3697 f7b03b6838cb
permissions -rw-r--r--
Clarified API documentation
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Get the name of the audio device we use for output */
    25 
    26 #if SDL_AUDIO_DRIVER_BSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO
    27 
    28 #include <fcntl.h>
    29 #include <sys/types.h>
    30 #include <sys/stat.h>
    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 inline void
    50 test_device(const char *fname, int flags, int (*test) (int fd),
    51             char ***devices, int *devCount)
    52 {
    53     struct stat sb;
    54     if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
    55         int audio_fd = open(fname, flags, 0);
    56         if ((audio_fd >= 0) && (test(audio_fd))) {
    57             void *p =
    58                 SDL_realloc(*devices, ((*devCount) + 1) * sizeof(char *));
    59             if (p != NULL) {
    60                 size_t len = strlen(fname) + 1;
    61                 char *str = (char *) SDL_malloc(len);
    62                 *devices = (char **) p;
    63                 if (str != NULL) {
    64                     SDL_strlcpy(str, fname, len);
    65                     (*devices)[(*devCount)++] = str;
    66                 }
    67             }
    68             close(audio_fd);
    69         }
    70     }
    71 }
    72 
    73 void
    74 SDL_FreeUnixAudioDevices(char ***devices, int *devCount)
    75 {
    76     int i = *devCount;
    77     if ((i > 0) && (*devices != NULL)) {
    78         while (i--) {
    79             SDL_free((*devices)[i]);
    80         }
    81     }
    82 
    83     if (*devices != NULL) {
    84         SDL_free(*devices);
    85     }
    86 
    87     *devices = NULL;
    88     *devCount = 0;
    89 }
    90 
    91 static int
    92 test_stub(int fd)
    93 {
    94     return 1;
    95 }
    96 
    97 void
    98 SDL_EnumUnixAudioDevices(int flags, int classic, int (*test) (int fd),
    99                          char ***devices, int *devCount)
   100 {
   101     const char *audiodev;
   102     char audiopath[1024];
   103 
   104     if (test == NULL)
   105         test = test_stub;
   106 
   107     /* Figure out what our audio device is */
   108     if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
   109         ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
   110         if (classic) {
   111             audiodev = _PATH_DEV_AUDIO;
   112         } else {
   113             struct stat sb;
   114 
   115             /* Added support for /dev/sound/\* in Linux 2.4 */
   116             if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
   117                 && ((stat(_PATH_DEV_DSP24, &sb) == 0)
   118                     && S_ISCHR(sb.st_mode))) {
   119                 audiodev = _PATH_DEV_DSP24;
   120             } else {
   121                 audiodev = _PATH_DEV_DSP;
   122             }
   123         }
   124     }
   125     test_device(audiodev, flags, test, devices, devCount);
   126 
   127     if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
   128         int instance = 0;
   129         while (instance++ <= 64) {
   130             SDL_snprintf(audiopath, SDL_arraysize(audiopath),
   131                          "%s%d", audiodev, instance);
   132             test_device(audiopath, flags, test, devices, devCount);
   133         }
   134     }
   135 }
   136 
   137 #endif /* Audio driver selection */
   138 /* vi: set ts=4 sw=4 expandtab: */