src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 13 Jul 2010 22:22:43 -0700
changeset 4512 ba9ac6bb0c75
parent 3697 f7b03b6838cb
child 5262 b530ef003506
permissions -rw-r--r--
Fixed compile warning.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@4512
    31
#include <unistd.h> /* For close() */
slouken@0
    32
slouken@1358
    33
#include "SDL_stdinc.h"
slouken@0
    34
#include "SDL_audiodev_c.h"
slouken@0
    35
slouken@0
    36
#ifndef _PATH_DEV_DSP
slouken@1402
    37
#if defined(__NETBSD__) || defined(__OPENBSD__)
slouken@94
    38
#define _PATH_DEV_DSP  "/dev/audio"
slouken@94
    39
#else
slouken@94
    40
#define _PATH_DEV_DSP  "/dev/dsp"
slouken@94
    41
#endif
slouken@0
    42
#endif
slouken@0
    43
#ifndef _PATH_DEV_DSP24
slouken@0
    44
#define _PATH_DEV_DSP24	"/dev/sound/dsp"
slouken@0
    45
#endif
slouken@0
    46
#ifndef _PATH_DEV_AUDIO
slouken@0
    47
#define _PATH_DEV_AUDIO	"/dev/audio"
slouken@0
    48
#endif
slouken@0
    49
icculus@2049
    50
static inline void
slouken@2060
    51
test_device(const char *fname, int flags, int (*test) (int fd),
icculus@2049
    52
            char ***devices, int *devCount)
icculus@2049
    53
{
icculus@2049
    54
    struct stat sb;
slouken@2060
    55
    if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
icculus@2049
    56
        int audio_fd = open(fname, flags, 0);
slouken@2060
    57
        if ((audio_fd >= 0) && (test(audio_fd))) {
slouken@2060
    58
            void *p =
slouken@2060
    59
                SDL_realloc(*devices, ((*devCount) + 1) * sizeof(char *));
icculus@2049
    60
            if (p != NULL) {
icculus@2049
    61
                size_t len = strlen(fname) + 1;
icculus@2049
    62
                char *str = (char *) SDL_malloc(len);
icculus@2049
    63
                *devices = (char **) p;
icculus@2049
    64
                if (str != NULL) {
icculus@2049
    65
                    SDL_strlcpy(str, fname, len);
icculus@2049
    66
                    (*devices)[(*devCount)++] = str;
icculus@2049
    67
                }
icculus@2049
    68
            }
icculus@2049
    69
            close(audio_fd);
icculus@2049
    70
        }
icculus@2049
    71
    }
icculus@2049
    72
}
slouken@0
    73
icculus@2049
    74
void
icculus@2049
    75
SDL_FreeUnixAudioDevices(char ***devices, int *devCount)
icculus@2049
    76
{
icculus@2049
    77
    int i = *devCount;
icculus@2049
    78
    if ((i > 0) && (*devices != NULL)) {
icculus@2049
    79
        while (i--) {
slouken@2258
    80
            SDL_free((*devices)[i]);
icculus@2049
    81
        }
icculus@2049
    82
    }
icculus@2049
    83
icculus@2049
    84
    if (*devices != NULL) {
icculus@2049
    85
        SDL_free(*devices);
icculus@2049
    86
    }
icculus@2049
    87
icculus@2049
    88
    *devices = NULL;
icculus@2049
    89
    *devCount = 0;
icculus@2049
    90
}
icculus@2049
    91
icculus@2049
    92
static int
icculus@2049
    93
test_stub(int fd)
icculus@2049
    94
{
icculus@2049
    95
    return 1;
icculus@2049
    96
}
icculus@2049
    97
icculus@2049
    98
void
slouken@2060
    99
SDL_EnumUnixAudioDevices(int flags, int classic, int (*test) (int fd),
icculus@2049
   100
                         char ***devices, int *devCount)
slouken@0
   101
{
slouken@1895
   102
    const char *audiodev;
slouken@1895
   103
    char audiopath[1024];
slouken@0
   104
icculus@2049
   105
    if (test == NULL)
icculus@2049
   106
        test = test_stub;
icculus@2049
   107
slouken@1895
   108
    /* Figure out what our audio device is */
slouken@1895
   109
    if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
slouken@1895
   110
        ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
slouken@1895
   111
        if (classic) {
slouken@1895
   112
            audiodev = _PATH_DEV_AUDIO;
slouken@1895
   113
        } else {
slouken@1895
   114
            struct stat sb;
slouken@0
   115
slouken@1895
   116
            /* Added support for /dev/sound/\* in Linux 2.4 */
slouken@1895
   117
            if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
slouken@1895
   118
                && ((stat(_PATH_DEV_DSP24, &sb) == 0)
slouken@1895
   119
                    && S_ISCHR(sb.st_mode))) {
slouken@1895
   120
                audiodev = _PATH_DEV_DSP24;
slouken@1895
   121
            } else {
slouken@1895
   122
                audiodev = _PATH_DEV_DSP;
slouken@1895
   123
            }
slouken@1895
   124
        }
slouken@1895
   125
    }
icculus@2049
   126
    test_device(audiodev, flags, test, devices, devCount);
slouken@0
   127
icculus@2049
   128
    if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
icculus@2049
   129
        int instance = 0;
icculus@2049
   130
        while (instance++ <= 64) {
slouken@1895
   131
            SDL_snprintf(audiopath, SDL_arraysize(audiopath),
icculus@2049
   132
                         "%s%d", audiodev, instance);
icculus@2049
   133
            test_device(audiopath, flags, test, devices, devCount);
slouken@1895
   134
        }
slouken@0
   135
    }
slouken@0
   136
}
slouken@0
   137
slouken@1402
   138
#endif /* Audio driver selection */
slouken@1895
   139
/* vi: set ts=4 sw=4 expandtab: */