src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 May 2013 14:17:52 -0700
changeset 7191 75360622e65f
parent 6885 700f1b25f77f
child 7860 2b0bcdea3a79
permissions -rw-r--r--
File style cleanup for the SDL 2.0 release
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@7191
    43
#define _PATH_DEV_DSP24 "/dev/sound/dsp"
slouken@0
    44
#endif
slouken@0
    45
#ifndef _PATH_DEV_AUDIO
slouken@7191
    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@5593
    51
            SDL_AddAudioDevice addfn)
icculus@2049
    52
{
icculus@2049
    53
    struct stat sb;
slouken@2060
    54
    if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
icculus@5593
    55
        const int audio_fd = open(fname, flags, 0);
icculus@5593
    56
        if (audio_fd >= 0) {
icculus@5593
    57
            if (test(audio_fd)) {
icculus@5593
    58
                addfn(fname);
icculus@2049
    59
            }
icculus@2049
    60
            close(audio_fd);
icculus@2049
    61
        }
icculus@2049
    62
    }
icculus@2049
    63
}
slouken@0
    64
icculus@2049
    65
static int
icculus@2049
    66
test_stub(int fd)
icculus@2049
    67
{
icculus@2049
    68
    return 1;
icculus@2049
    69
}
icculus@2049
    70
icculus@2049
    71
void
icculus@5593
    72
SDL_EnumUnixAudioDevices(int iscapture, int classic, int (*test)(int fd),
icculus@5593
    73
                         SDL_AddAudioDevice addfn)
slouken@0
    74
{
icculus@5593
    75
    const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
slouken@1895
    76
    const char *audiodev;
slouken@1895
    77
    char audiopath[1024];
slouken@0
    78
icculus@2049
    79
    if (test == NULL)
icculus@2049
    80
        test = test_stub;
icculus@2049
    81
slouken@1895
    82
    /* Figure out what our audio device is */
slouken@1895
    83
    if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
slouken@1895
    84
        ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
slouken@1895
    85
        if (classic) {
slouken@1895
    86
            audiodev = _PATH_DEV_AUDIO;
slouken@1895
    87
        } else {
slouken@1895
    88
            struct stat sb;
slouken@0
    89
slouken@1895
    90
            /* Added support for /dev/sound/\* in Linux 2.4 */
slouken@1895
    91
            if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
slouken@1895
    92
                && ((stat(_PATH_DEV_DSP24, &sb) == 0)
slouken@1895
    93
                    && S_ISCHR(sb.st_mode))) {
slouken@1895
    94
                audiodev = _PATH_DEV_DSP24;
slouken@1895
    95
            } else {
slouken@1895
    96
                audiodev = _PATH_DEV_DSP;
slouken@1895
    97
            }
slouken@1895
    98
        }
slouken@1895
    99
    }
icculus@5593
   100
    test_device(audiodev, flags, test, addfn);
slouken@0
   101
icculus@2049
   102
    if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
icculus@2049
   103
        int instance = 0;
icculus@2049
   104
        while (instance++ <= 64) {
slouken@1895
   105
            SDL_snprintf(audiopath, SDL_arraysize(audiopath),
icculus@2049
   106
                         "%s%d", audiodev, instance);
icculus@5593
   107
            test_device(audiopath, flags, test, addfn);
slouken@1895
   108
        }
slouken@0
   109
    }
slouken@0
   110
}
slouken@0
   111
slouken@1402
   112
#endif /* Audio driver selection */
slouken@1895
   113
/* vi: set ts=4 sw=4 expandtab: */