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