src/audio/SDL_audiodev.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 18 Mar 2015 02:01:17 -0400
changeset 9394 bb28e5281770
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Bunch of reworking to how we manage audio devices.

Device enumeration now happens at startup and then is managed exclusively
through hotplugging instead of full redetection. The device name list now has
a unique "handle" associated with each item and SDL will pass this to the
backend so they don't have to figure out how a human readable name maps to
real hardware for a second time.

Other cleanups, fixes, improvements, plus all the audio backends updated to
the new interface...largely untested at this point, though.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../SDL_internal.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@9394
    49
static void
icculus@9394
    50
test_device(const int iscapture, const char *fname, int flags, int (*test) (int fd))
icculus@2049
    51
{
icculus@2049
    52
    struct stat sb;
slouken@2060
    53
    if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) {
icculus@5593
    54
        const int audio_fd = open(fname, flags, 0);
icculus@5593
    55
        if (audio_fd >= 0) {
icculus@9394
    56
            const int okay = test(audio_fd);
icculus@9394
    57
            close(audio_fd);
icculus@9394
    58
            if (okay) {
icculus@9394
    59
                static size_t dummyhandle = 0;
icculus@9394
    60
                dummyhandle++;
icculus@9394
    61
                SDL_assert(dummyhandle != 0);
icculus@9394
    62
                SDL_AddAudioDevice(iscapture, fname, (void *) dummyhandle);
icculus@2049
    63
            }
icculus@2049
    64
        }
icculus@2049
    65
    }
icculus@2049
    66
}
slouken@0
    67
icculus@2049
    68
static int
icculus@2049
    69
test_stub(int fd)
icculus@2049
    70
{
icculus@2049
    71
    return 1;
icculus@2049
    72
}
icculus@2049
    73
icculus@9394
    74
static void
icculus@9394
    75
SDL_EnumUnixAudioDevices_Internal(const int iscapture, const int classic, int (*test)(int))
slouken@0
    76
{
icculus@9394
    77
    const int flags = iscapture ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT;
slouken@1895
    78
    const char *audiodev;
slouken@1895
    79
    char audiopath[1024];
slouken@0
    80
icculus@2049
    81
    if (test == NULL)
icculus@2049
    82
        test = test_stub;
icculus@2049
    83
slouken@1895
    84
    /* Figure out what our audio device is */
slouken@1895
    85
    if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
slouken@1895
    86
        ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
slouken@1895
    87
        if (classic) {
slouken@1895
    88
            audiodev = _PATH_DEV_AUDIO;
slouken@1895
    89
        } else {
slouken@1895
    90
            struct stat sb;
slouken@0
    91
slouken@1895
    92
            /* Added support for /dev/sound/\* in Linux 2.4 */
slouken@1895
    93
            if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
slouken@1895
    94
                && ((stat(_PATH_DEV_DSP24, &sb) == 0)
slouken@1895
    95
                    && S_ISCHR(sb.st_mode))) {
slouken@1895
    96
                audiodev = _PATH_DEV_DSP24;
slouken@1895
    97
            } else {
slouken@1895
    98
                audiodev = _PATH_DEV_DSP;
slouken@1895
    99
            }
slouken@1895
   100
        }
slouken@1895
   101
    }
icculus@9394
   102
    test_device(iscapture, audiodev, flags, test);
slouken@0
   103
icculus@2049
   104
    if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) {
icculus@2049
   105
        int instance = 0;
icculus@2049
   106
        while (instance++ <= 64) {
slouken@1895
   107
            SDL_snprintf(audiopath, SDL_arraysize(audiopath),
icculus@2049
   108
                         "%s%d", audiodev, instance);
icculus@9394
   109
            test_device(iscapture, audiopath, flags, test);
slouken@1895
   110
        }
slouken@0
   111
    }
slouken@0
   112
}
slouken@0
   113
icculus@9394
   114
void
icculus@9394
   115
SDL_EnumUnixAudioDevices(const int classic, int (*test)(int))
icculus@9394
   116
{
icculus@9394
   117
    SDL_EnumUnixAudioDevices_Internal(SDL_TRUE, classic, test);
icculus@9394
   118
    SDL_EnumUnixAudioDevices_Internal(SDL_FALSE, classic, test);
icculus@9394
   119
}
icculus@9394
   120
slouken@1402
   121
#endif /* Audio driver selection */
icculus@9394
   122
slouken@1895
   123
/* vi: set ts=4 sw=4 expandtab: */