src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 5593 ab22ca13c47f
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* Get the name of the audio device we use for output */
slouken@0
    24
slouken@1567
    25
#if SDL_AUDIO_DRIVER_BSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO
slouken@0
    26
slouken@0
    27
#include <fcntl.h>
slouken@0
    28
#include <sys/types.h>
slouken@0
    29
#include <sys/stat.h>
slouken@4512
    30
#include <unistd.h> /* For close() */
slouken@0
    31
slouken@1358
    32
#include "SDL_stdinc.h"
slouken@0
    33
#include "SDL_audiodev_c.h"
slouken@0
    34
slouken@0
    35
#ifndef _PATH_DEV_DSP
slouken@1402
    36
#if defined(__NETBSD__) || defined(__OPENBSD__)
slouken@94
    37
#define _PATH_DEV_DSP  "/dev/audio"
slouken@94
    38
#else
slouken@94
    39
#define _PATH_DEV_DSP  "/dev/dsp"
slouken@94
    40
#endif
slouken@0
    41
#endif
slouken@0
    42
#ifndef _PATH_DEV_DSP24
slouken@0
    43
#define _PATH_DEV_DSP24	"/dev/sound/dsp"
slouken@0
    44
#endif
slouken@0
    45
#ifndef _PATH_DEV_AUDIO
slouken@0
    46
#define _PATH_DEV_AUDIO	"/dev/audio"
slouken@0
    47
#endif
slouken@0
    48
icculus@2049
    49
static inline void
slouken@2060
    50
test_device(const char *fname, int flags, int (*test) (int fd),
icculus@2049
    51
            char ***devices, int *devCount)
icculus@2049
    52
{
icculus@2049
    53
    struct stat sb;
slouken@2060
    54
    if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
icculus@2049
    55
        int audio_fd = open(fname, flags, 0);
slouken@2060
    56
        if ((audio_fd >= 0) && (test(audio_fd))) {
slouken@2060
    57
            void *p =
slouken@2060
    58
                SDL_realloc(*devices, ((*devCount) + 1) * sizeof(char *));
icculus@2049
    59
            if (p != NULL) {
icculus@2049
    60
                size_t len = strlen(fname) + 1;
icculus@2049
    61
                char *str = (char *) SDL_malloc(len);
icculus@2049
    62
                *devices = (char **) p;
icculus@2049
    63
                if (str != NULL) {
icculus@2049
    64
                    SDL_strlcpy(str, fname, len);
icculus@2049
    65
                    (*devices)[(*devCount)++] = str;
icculus@2049
    66
                }
icculus@2049
    67
            }
icculus@2049
    68
            close(audio_fd);
icculus@2049
    69
        }
icculus@2049
    70
    }
icculus@2049
    71
}
slouken@0
    72
icculus@2049
    73
void
icculus@2049
    74
SDL_FreeUnixAudioDevices(char ***devices, int *devCount)
icculus@2049
    75
{
icculus@2049
    76
    int i = *devCount;
icculus@2049
    77
    if ((i > 0) && (*devices != NULL)) {
icculus@2049
    78
        while (i--) {
slouken@2258
    79
            SDL_free((*devices)[i]);
icculus@2049
    80
        }
icculus@2049
    81
    }
icculus@2049
    82
icculus@2049
    83
    if (*devices != NULL) {
icculus@2049
    84
        SDL_free(*devices);
icculus@2049
    85
    }
icculus@2049
    86
icculus@2049
    87
    *devices = NULL;
icculus@2049
    88
    *devCount = 0;
icculus@2049
    89
}
icculus@2049
    90
icculus@2049
    91
static int
icculus@2049
    92
test_stub(int fd)
icculus@2049
    93
{
icculus@2049
    94
    return 1;
icculus@2049
    95
}
icculus@2049
    96
icculus@2049
    97
void
slouken@2060
    98
SDL_EnumUnixAudioDevices(int flags, int classic, int (*test) (int fd),
icculus@2049
    99
                         char ***devices, int *devCount)
slouken@0
   100
{
slouken@1895
   101
    const char *audiodev;
slouken@1895
   102
    char audiopath[1024];
slouken@0
   103
icculus@2049
   104
    if (test == NULL)
icculus@2049
   105
        test = test_stub;
icculus@2049
   106
slouken@1895
   107
    /* Figure out what our audio device is */
slouken@1895
   108
    if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
slouken@1895
   109
        ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
slouken@1895
   110
        if (classic) {
slouken@1895
   111
            audiodev = _PATH_DEV_AUDIO;
slouken@1895
   112
        } else {
slouken@1895
   113
            struct stat sb;
slouken@0
   114
slouken@1895
   115
            /* Added support for /dev/sound/\* in Linux 2.4 */
slouken@1895
   116
            if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
slouken@1895
   117
                && ((stat(_PATH_DEV_DSP24, &sb) == 0)
slouken@1895
   118
                    && S_ISCHR(sb.st_mode))) {
slouken@1895
   119
                audiodev = _PATH_DEV_DSP24;
slouken@1895
   120
            } else {
slouken@1895
   121
                audiodev = _PATH_DEV_DSP;
slouken@1895
   122
            }
slouken@1895
   123
        }
slouken@1895
   124
    }
icculus@2049
   125
    test_device(audiodev, flags, test, devices, devCount);
slouken@0
   126
icculus@2049
   127
    if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
icculus@2049
   128
        int instance = 0;
icculus@2049
   129
        while (instance++ <= 64) {
slouken@1895
   130
            SDL_snprintf(audiopath, SDL_arraysize(audiopath),
icculus@2049
   131
                         "%s%d", audiodev, instance);
icculus@2049
   132
            test_device(audiopath, flags, test, devices, devCount);
slouken@1895
   133
        }
slouken@0
   134
    }
slouken@0
   135
}
slouken@0
   136
slouken@1402
   137
#endif /* Audio driver selection */
slouken@1895
   138
/* vi: set ts=4 sw=4 expandtab: */