src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1567 12b6d331d82a
child 2049 5f6550e5184f
child 3809 7852b5b78af5
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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
slouken@0
    49
slouken@1895
    50
int
slouken@1895
    51
SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
slouken@0
    52
{
slouken@1895
    53
    const char *audiodev;
slouken@1895
    54
    int audio_fd;
slouken@1895
    55
    char audiopath[1024];
slouken@0
    56
slouken@1895
    57
    /* Figure out what our audio device is */
slouken@1895
    58
    if (((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) &&
slouken@1895
    59
        ((audiodev = SDL_getenv("AUDIODEV")) == NULL)) {
slouken@1895
    60
        if (classic) {
slouken@1895
    61
            audiodev = _PATH_DEV_AUDIO;
slouken@1895
    62
        } else {
slouken@1895
    63
            struct stat sb;
slouken@0
    64
slouken@1895
    65
            /* Added support for /dev/sound/\* in Linux 2.4 */
slouken@1895
    66
            if (((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode))
slouken@1895
    67
                && ((stat(_PATH_DEV_DSP24, &sb) == 0)
slouken@1895
    68
                    && S_ISCHR(sb.st_mode))) {
slouken@1895
    69
                audiodev = _PATH_DEV_DSP24;
slouken@1895
    70
            } else {
slouken@1895
    71
                audiodev = _PATH_DEV_DSP;
slouken@1895
    72
            }
slouken@1895
    73
        }
slouken@1895
    74
    }
slouken@1895
    75
    audio_fd = open(audiodev, flags, 0);
slouken@0
    76
slouken@1895
    77
    /* If the first open fails, look for other devices */
slouken@1895
    78
    if ((audio_fd < 0) && (SDL_strlen(audiodev) < (sizeof(audiopath) - 3))) {
slouken@1895
    79
        int exists, instance;
slouken@1895
    80
        struct stat sb;
slouken@0
    81
slouken@1895
    82
        instance = 1;
slouken@1895
    83
        do {                    /* Don't use errno ENOENT - it may not be thread-safe */
slouken@1895
    84
            SDL_snprintf(audiopath, SDL_arraysize(audiopath),
slouken@1895
    85
                         "%s%d", audiodev, instance++);
slouken@1895
    86
            exists = 0;
slouken@1895
    87
            if (stat(audiopath, &sb) == 0) {
slouken@1895
    88
                exists = 1;
slouken@1895
    89
                audio_fd = open(audiopath, flags, 0);
slouken@1895
    90
            }
slouken@1895
    91
        }
slouken@1895
    92
        while (exists && (audio_fd < 0));
slouken@1895
    93
        audiodev = audiopath;
slouken@1895
    94
    }
slouken@1895
    95
    if (path != NULL) {
slouken@1895
    96
        SDL_strlcpy(path, audiodev, maxlen);
slouken@1895
    97
        path[maxlen - 1] = '\0';
slouken@1895
    98
    }
slouken@1895
    99
    return (audio_fd);
slouken@0
   100
}
slouken@0
   101
slouken@1402
   102
#elif SDL_AUDIO_DRIVER_PAUD
slouken@0
   103
slouken@0
   104
/* Get the name of the audio device we use for output */
slouken@0
   105
slouken@0
   106
#include <sys/types.h>
slouken@0
   107
#include <sys/stat.h>
slouken@0
   108
slouken@1358
   109
#include "SDL_stdinc.h"
slouken@0
   110
#include "SDL_audiodev_c.h"
slouken@0
   111
slouken@0
   112
#ifndef _PATH_DEV_DSP
slouken@0
   113
#define _PATH_DEV_DSP	"/dev/%caud%c/%c"
slouken@0
   114
#endif
slouken@0
   115
slouken@1895
   116
char devsettings[][3] = {
slouken@1895
   117
    {'p', '0', '1'}, {'p', '0', '2'}, {'p', '0', '3'}, {'p', '0', '4'},
slouken@1895
   118
    {'p', '1', '1'}, {'p', '1', '2'}, {'p', '1', '3'}, {'p', '1', '4'},
slouken@1895
   119
    {'p', '2', '1'}, {'p', '2', '2'}, {'p', '2', '3'}, {'p', '2', '4'},
slouken@1895
   120
    {'p', '3', '1'}, {'p', '3', '2'}, {'p', '3', '3'}, {'p', '3', '4'},
slouken@1895
   121
    {'b', '0', '1'}, {'b', '0', '2'}, {'b', '0', '3'}, {'b', '0', '4'},
slouken@1895
   122
    {'b', '1', '1'}, {'b', '1', '2'}, {'b', '1', '3'}, {'b', '1', '4'},
slouken@1895
   123
    {'b', '2', '1'}, {'b', '2', '2'}, {'b', '2', '3'}, {'b', '2', '4'},
slouken@1895
   124
    {'b', '3', '1'}, {'b', '3', '2'}, {'b', '3', '3'}, {'b', '3', '4'},
slouken@1895
   125
    {'\0', '\0', '\0'}
slouken@0
   126
};
slouken@0
   127
slouken@1895
   128
static int
slouken@1895
   129
OpenUserDefinedDevice(char *path, int maxlen, int flags)
slouken@0
   130
{
slouken@1895
   131
    const char *audiodev;
slouken@1895
   132
    int audio_fd;
slouken@0
   133
slouken@1895
   134
    /* Figure out what our audio device is */
slouken@1895
   135
    if ((audiodev = SDL_getenv("SDL_PATH_DSP")) == NULL) {
slouken@1895
   136
        audiodev = SDL_getenv("AUDIODEV");
slouken@1895
   137
    }
slouken@1895
   138
    if (audiodev == NULL) {
slouken@1895
   139
        return -1;
slouken@1895
   140
    }
slouken@1895
   141
    audio_fd = open(audiodev, flags, 0);
slouken@1895
   142
    if (path != NULL) {
slouken@1895
   143
        SDL_strlcpy(path, audiodev, maxlen);
slouken@1895
   144
        path[maxlen - 1] = '\0';
slouken@1895
   145
    }
slouken@1895
   146
    return audio_fd;
slouken@0
   147
}
slouken@0
   148
slouken@1895
   149
int
slouken@1895
   150
SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
slouken@0
   151
{
slouken@0
   152
    struct stat sb;
slouken@1895
   153
    int audio_fd;
slouken@1895
   154
    char audiopath[1024];
slouken@1895
   155
    int cycle;
slouken@0
   156
slouken@1895
   157
    audio_fd = OpenUserDefinedDevice(path, maxlen, flags);
slouken@1895
   158
    if (audio_fd != -1) {
slouken@0
   159
        return audio_fd;
slouken@0
   160
    }
slouken@0
   161
slouken@1895
   162
    cycle = 0;
slouken@1895
   163
    while (devsettings[cycle][0] != '\0') {
slouken@1895
   164
        SDL_snprintf(audiopath, SDL_arraysize(audiopath),
slouken@1895
   165
                     _PATH_DEV_DSP,
slouken@1895
   166
                     devsettings[cycle][0],
slouken@1895
   167
                     devsettings[cycle][1], devsettings[cycle][2]);
slouken@0
   168
slouken@1895
   169
        if (stat(audiopath, &sb) == 0) {
slouken@1895
   170
            audio_fd = open(audiopath, flags, 0);
slouken@1895
   171
            if (audio_fd > 0) {
slouken@1895
   172
                if (path != NULL) {
slouken@1895
   173
                    SDL_strlcpy(path, audiopath, maxlen);
slouken@1895
   174
                }
slouken@1895
   175
                return audio_fd;
slouken@1895
   176
            }
slouken@1895
   177
        }
slouken@0
   178
    }
slouken@0
   179
    return -1;
slouken@0
   180
}
slouken@0
   181
slouken@1402
   182
#endif /* Audio driver selection */
slouken@1895
   183
/* vi: set ts=4 sw=4 expandtab: */