src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 10 Feb 2006 06:48:43 +0000
changeset 1358 c71e05b4dc2e
parent 1338 604d73db6802
child 1379 c0a74f199ecf
permissions -rw-r--r--
More header massaging... works great on Windows. ;-)
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@0
    22
slouken@0
    23
/* Get the name of the audio device we use for output */
slouken@0
    24
slouken@1035
    25
#if defined(unix) || defined(__unix__) || defined(__riscos__)
slouken@0
    26
slouken@0
    27
#include <fcntl.h>
slouken@0
    28
#include <sys/types.h>
slouken@0
    29
#include <sys/stat.h>
slouken@0
    30
slouken@1358
    31
#include "SDL_stdinc.h"
slouken@0
    32
#include "SDL_audiodev_c.h"
slouken@0
    33
slouken@0
    34
#ifndef _PATH_DEV_DSP
slouken@1026
    35
#if defined(__NetBSD__) || defined(__OpenBSD__)
slouken@94
    36
#define _PATH_DEV_DSP  "/dev/audio"
slouken@94
    37
#else
slouken@94
    38
#define _PATH_DEV_DSP  "/dev/dsp"
slouken@94
    39
#endif
slouken@0
    40
#endif
slouken@0
    41
#ifndef _PATH_DEV_DSP24
slouken@0
    42
#define _PATH_DEV_DSP24	"/dev/sound/dsp"
slouken@0
    43
#endif
slouken@0
    44
#ifndef _PATH_DEV_AUDIO
slouken@0
    45
#define _PATH_DEV_AUDIO	"/dev/audio"
slouken@0
    46
#endif
slouken@0
    47
slouken@0
    48
slouken@0
    49
int SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
slouken@0
    50
{
slouken@0
    51
	const char *audiodev;
slouken@0
    52
	int audio_fd;
slouken@0
    53
	char audiopath[1024];
slouken@0
    54
slouken@0
    55
	/* Figure out what our audio device is */
slouken@1336
    56
	if ( ((audiodev=SDL_getenv("SDL_PATH_DSP")) == NULL) &&
slouken@1336
    57
	     ((audiodev=SDL_getenv("AUDIODEV")) == NULL) ) {
slouken@0
    58
		if ( classic ) {
slouken@0
    59
			audiodev = _PATH_DEV_AUDIO;
slouken@0
    60
		} else {
slouken@0
    61
			struct stat sb;
slouken@0
    62
slouken@0
    63
			/* Added support for /dev/sound/\* in Linux 2.4 */
icculus@1138
    64
			if ( ((stat("/dev/sound", &sb) == 0) && S_ISDIR(sb.st_mode)) &&
icculus@1138
    65
				 ((stat(_PATH_DEV_DSP24, &sb) == 0) && S_ISCHR(sb.st_mode)) ) {
slouken@0
    66
				audiodev = _PATH_DEV_DSP24;
slouken@0
    67
			} else {
slouken@0
    68
				audiodev = _PATH_DEV_DSP;
slouken@0
    69
			}
slouken@0
    70
		}
slouken@0
    71
	}
slouken@0
    72
	audio_fd = open(audiodev, flags, 0);
slouken@0
    73
slouken@0
    74
	/* If the first open fails, look for other devices */
slouken@1336
    75
	if ( (audio_fd < 0) && (SDL_strlen(audiodev) < (sizeof(audiopath)-3)) ) {
slouken@0
    76
		int exists, instance;
slouken@0
    77
		struct stat sb;
slouken@0
    78
slouken@0
    79
		instance = 1;
slouken@0
    80
		do { /* Don't use errno ENOENT - it may not be thread-safe */
slouken@1338
    81
			SDL_snprintf(audiopath, SDL_arraysize(audiopath),
slouken@1338
    82
			             "%s%d", audiodev, instance++);
slouken@0
    83
			exists = 0;
slouken@0
    84
			if ( stat(audiopath, &sb) == 0 ) {
slouken@0
    85
				exists = 1;
slouken@0
    86
				audio_fd = open(audiopath, flags, 0); 
slouken@0
    87
			}
slouken@0
    88
		} while ( exists && (audio_fd < 0) );
slouken@0
    89
		audiodev = audiopath;
slouken@0
    90
	}
slouken@0
    91
	if ( path != NULL ) {
slouken@1336
    92
		SDL_strncpy(path, audiodev, maxlen);
slouken@0
    93
		path[maxlen-1] = '\0';
slouken@0
    94
	}
slouken@0
    95
	return(audio_fd);
slouken@0
    96
}
slouken@0
    97
slouken@0
    98
#elif defined(_AIX)
slouken@0
    99
slouken@0
   100
/* Get the name of the audio device we use for output */
slouken@0
   101
slouken@0
   102
#include <sys/types.h>
slouken@0
   103
#include <sys/stat.h>
slouken@0
   104
slouken@1358
   105
#include "SDL_stdinc.h"
slouken@0
   106
#include "SDL_audiodev_c.h"
slouken@0
   107
slouken@0
   108
#ifndef _PATH_DEV_DSP
slouken@0
   109
#define _PATH_DEV_DSP	"/dev/%caud%c/%c"
slouken@0
   110
#endif
slouken@0
   111
slouken@0
   112
char devsettings[][3] =
slouken@0
   113
{
slouken@0
   114
    { 'p', '0', '1' }, { 'p', '0', '2' }, { 'p', '0', '3' }, { 'p', '0', '4' },
slouken@0
   115
    { 'p', '1', '1' }, { 'p', '1', '2' }, { 'p', '1', '3' }, { 'p', '1', '4' },
slouken@0
   116
    { 'p', '2', '1' }, { 'p', '2', '2' }, { 'p', '2', '3' }, { 'p', '2', '4' },
slouken@0
   117
    { 'p', '3', '1' }, { 'p', '3', '2' }, { 'p', '3', '3' }, { 'p', '3', '4' },
slouken@0
   118
    { 'b', '0', '1' }, { 'b', '0', '2' }, { 'b', '0', '3' }, { 'b', '0', '4' },
slouken@0
   119
    { 'b', '1', '1' }, { 'b', '1', '2' }, { 'b', '1', '3' }, { 'b', '1', '4' },
slouken@0
   120
    { 'b', '2', '1' }, { 'b', '2', '2' }, { 'b', '2', '3' }, { 'b', '2', '4' },
slouken@0
   121
    { 'b', '3', '1' }, { 'b', '3', '2' }, { 'b', '3', '3' }, { 'b', '3', '4' },
slouken@0
   122
    { '\0', '\0', '\0' }
slouken@0
   123
};
slouken@0
   124
slouken@0
   125
static int OpenUserDefinedDevice(char *path, int maxlen, int flags)
slouken@0
   126
{
slouken@0
   127
	const char *audiodev;
slouken@0
   128
	int  audio_fd;
slouken@0
   129
slouken@0
   130
	/* Figure out what our audio device is */
slouken@1336
   131
	if ((audiodev=SDL_getenv("SDL_PATH_DSP")) == NULL) {
slouken@1336
   132
	    audiodev=SDL_getenv("AUDIODEV");
slouken@0
   133
	}
slouken@0
   134
	if ( audiodev == NULL ) {
slouken@0
   135
	    return -1;
slouken@0
   136
	}
slouken@0
   137
	audio_fd = open(audiodev, flags, 0);
slouken@0
   138
	if ( path != NULL ) {
slouken@1336
   139
		SDL_strncpy(path, audiodev, maxlen);
slouken@0
   140
		path[maxlen-1] = '\0';
slouken@0
   141
	}
slouken@0
   142
	return audio_fd;
slouken@0
   143
}
slouken@0
   144
slouken@0
   145
int SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
slouken@0
   146
{
slouken@0
   147
    struct stat sb;
slouken@0
   148
    int         audio_fd;
slouken@0
   149
    char        audiopath[1024];
slouken@0
   150
    int         cycle;
slouken@0
   151
slouken@0
   152
    audio_fd = OpenUserDefinedDevice(path,maxlen,flags);
slouken@0
   153
    if ( audio_fd != -1 ) {
slouken@0
   154
        return audio_fd;
slouken@0
   155
    }
slouken@0
   156
slouken@0
   157
    cycle    = 0;
slouken@0
   158
    while( devsettings[cycle][0] != '\0' ) {
slouken@1338
   159
        SDL_snprintf( audiopath, SDL_arraysize(audiopath),
slouken@0
   160
                 _PATH_DEV_DSP,
slouken@0
   161
                 devsettings[cycle][0],
slouken@0
   162
                 devsettings[cycle][1],
slouken@0
   163
                 devsettings[cycle][2]);
slouken@0
   164
slouken@0
   165
	if ( stat(audiopath, &sb) == 0 ) {
slouken@0
   166
	    audio_fd = open(audiopath, flags, 0);
slouken@0
   167
	    if ( audio_fd > 0 ) {
slouken@0
   168
		if ( path != NULL ) {
slouken@1336
   169
		    SDL_strncpy( path, audiopath, maxlen );
slouken@0
   170
		    path[maxlen-1] = '\0';
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@0
   179
#endif /* UNIX system */