src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 28 Oct 2006 16:48:03 +0000
changeset 2060 866052b01ee5
parent 2049 5f6550e5184f
child 2258 771bd3389e3a
permissions -rw-r--r--
indent is evil
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Get the name of the audio device we use for output */
slouken@0
    25
slouken@1567
    26
#if SDL_AUDIO_DRIVER_BSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO
slouken@0
    27
slouken@0
    28
#include <fcntl.h>
slouken@0
    29
#include <sys/types.h>
slouken@0
    30
#include <sys/stat.h>
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@2060
    79
            SDL_free((*devices)[*devCount]);
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: */