src/audio/SDL_audiodev.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 17 Oct 2006 09:15:21 +0000
changeset 2049 5f6550e5184f
parent 1895 c121d94672cb
child 2060 866052b01ee5
permissions -rw-r--r--
Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
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
icculus@2049
    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;
icculus@2049
    54
    if ( (stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode)) ) {
icculus@2049
    55
        int audio_fd = open(fname, flags, 0);
icculus@2049
    56
        if ( (audio_fd >= 0) && (test(audio_fd)) ) {
icculus@2049
    57
            void *p = SDL_realloc(*devices, ((*devCount)+1) * sizeof (char *));
icculus@2049
    58
            if (p != NULL) {
icculus@2049
    59
                size_t len = strlen(fname) + 1;
icculus@2049
    60
                char *str = (char *) SDL_malloc(len);
icculus@2049
    61
                *devices = (char **) p;
icculus@2049
    62
                if (str != NULL) {
icculus@2049
    63
                    SDL_strlcpy(str, fname, len);
icculus@2049
    64
                    (*devices)[(*devCount)++] = str;
icculus@2049
    65
                }
icculus@2049
    66
            }
icculus@2049
    67
            close(audio_fd);
icculus@2049
    68
        }
icculus@2049
    69
    }
icculus@2049
    70
}
slouken@0
    71
icculus@2049
    72
void
icculus@2049
    73
SDL_FreeUnixAudioDevices(char ***devices, int *devCount)
icculus@2049
    74
{
icculus@2049
    75
    int i = *devCount;
icculus@2049
    76
    if ((i > 0) && (*devices != NULL)) {
icculus@2049
    77
        while (i--) {
icculus@2049
    78
            SDL_free( (*devices)[*devCount] );
icculus@2049
    79
        }
icculus@2049
    80
    }
icculus@2049
    81
icculus@2049
    82
    if (*devices != NULL) {
icculus@2049
    83
        SDL_free(*devices);
icculus@2049
    84
    }
icculus@2049
    85
icculus@2049
    86
    *devices = NULL;
icculus@2049
    87
    *devCount = 0;
icculus@2049
    88
}
icculus@2049
    89
icculus@2049
    90
static int
icculus@2049
    91
test_stub(int fd)
icculus@2049
    92
{
icculus@2049
    93
    return 1;
icculus@2049
    94
}
icculus@2049
    95
icculus@2049
    96
void
icculus@2049
    97
SDL_EnumUnixAudioDevices(int flags, int classic, int (*test)(int fd),
icculus@2049
    98
                         char ***devices, int *devCount)
slouken@0
    99
{
slouken@1895
   100
    const char *audiodev;
slouken@1895
   101
    char audiopath[1024];
slouken@0
   102
icculus@2049
   103
    if (test == NULL)
icculus@2049
   104
        test = test_stub;
icculus@2049
   105
slouken@1895
   106
    /* Figure out what our audio device is */
slouken@1895
   107
    if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
slouken@1895
   108
        ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
slouken@1895
   109
        if (classic) {
slouken@1895
   110
            audiodev = _PATH_DEV_AUDIO;
slouken@1895
   111
        } else {
slouken@1895
   112
            struct stat sb;
slouken@0
   113
slouken@1895
   114
            /* Added support for /dev/sound/\* in Linux 2.4 */
slouken@1895
   115
            if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
slouken@1895
   116
                && ((stat(_PATH_DEV_DSP24, &sb) == 0)
slouken@1895
   117
                    && S_ISCHR(sb.st_mode))) {
slouken@1895
   118
                audiodev = _PATH_DEV_DSP24;
slouken@1895
   119
            } else {
slouken@1895
   120
                audiodev = _PATH_DEV_DSP;
slouken@1895
   121
            }
slouken@1895
   122
        }
slouken@1895
   123
    }
icculus@2049
   124
    test_device(audiodev, flags, test, devices, devCount);
slouken@0
   125
icculus@2049
   126
    if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
icculus@2049
   127
        int instance = 0;
icculus@2049
   128
        while (instance++ <= 64) {
slouken@1895
   129
            SDL_snprintf(audiopath, SDL_arraysize(audiopath),
icculus@2049
   130
                         "%s%d", audiodev, instance);
icculus@2049
   131
            test_device(audiopath, flags, test, devices, devCount);
slouken@1895
   132
        }
slouken@0
   133
    }
slouken@0
   134
}
slouken@0
   135
slouken@1402
   136
#endif /* Audio driver selection */
slouken@1895
   137
/* vi: set ts=4 sw=4 expandtab: */