src/audio/SDL_audiodev.c
author Sam Lantinga <slouken@lokigames.com>
Sun, 08 Jul 2001 09:00:06 +0000
changeset 94 ae6e6b73333f
parent 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Cleaned up the OpenBSD port, thanks to Peter Valchev
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Get the name of the audio device we use for output */
slouken@0
    29
slouken@0
    30
#if defined(unix) || defined(__unix__)
slouken@0
    31
slouken@0
    32
#include <stdlib.h>
slouken@0
    33
#include <stdio.h>
slouken@0
    34
#include <fcntl.h>
slouken@0
    35
#include <sys/types.h>
slouken@0
    36
#include <sys/stat.h>
slouken@0
    37
#include <string.h>
slouken@0
    38
slouken@0
    39
#include "SDL_audiodev_c.h"
slouken@0
    40
slouken@0
    41
#ifndef _PATH_DEV_DSP
slouken@94
    42
#ifdef __OpenBSD__
slouken@94
    43
#define _PATH_DEV_DSP  "/dev/audio"
slouken@94
    44
#else
slouken@94
    45
#define _PATH_DEV_DSP  "/dev/dsp"
slouken@94
    46
#endif
slouken@0
    47
#endif
slouken@0
    48
#ifndef _PATH_DEV_DSP24
slouken@0
    49
#define _PATH_DEV_DSP24	"/dev/sound/dsp"
slouken@0
    50
#endif
slouken@0
    51
#ifndef _PATH_DEV_AUDIO
slouken@0
    52
#define _PATH_DEV_AUDIO	"/dev/audio"
slouken@0
    53
#endif
slouken@0
    54
slouken@0
    55
slouken@0
    56
int SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
slouken@0
    57
{
slouken@0
    58
	const char *audiodev;
slouken@0
    59
	int audio_fd;
slouken@0
    60
	char audiopath[1024];
slouken@0
    61
slouken@0
    62
	/* Figure out what our audio device is */
slouken@0
    63
	if ( ((audiodev=getenv("SDL_PATH_DSP")) == NULL) &&
slouken@0
    64
	     ((audiodev=getenv("AUDIODEV")) == NULL) ) {
slouken@0
    65
		if ( classic ) {
slouken@0
    66
			audiodev = _PATH_DEV_AUDIO;
slouken@0
    67
		} else {
slouken@0
    68
			struct stat sb;
slouken@0
    69
slouken@0
    70
			/* Added support for /dev/sound/\* in Linux 2.4 */
slouken@0
    71
			if ( (stat("/dev/sound", &sb) == 0) &&
slouken@0
    72
			     S_ISDIR(sb.st_mode) ) {
slouken@0
    73
				audiodev = _PATH_DEV_DSP24;
slouken@0
    74
			} else {
slouken@0
    75
				audiodev = _PATH_DEV_DSP;
slouken@0
    76
			}
slouken@0
    77
		}
slouken@0
    78
	}
slouken@0
    79
	audio_fd = open(audiodev, flags, 0);
slouken@0
    80
slouken@0
    81
	/* If the first open fails, look for other devices */
slouken@0
    82
	if ( (audio_fd < 0) && (strlen(audiodev) < (sizeof(audiopath)-3)) ) {
slouken@0
    83
		int exists, instance;
slouken@0
    84
		struct stat sb;
slouken@0
    85
slouken@0
    86
		instance = 1;
slouken@0
    87
		do { /* Don't use errno ENOENT - it may not be thread-safe */
slouken@0
    88
			sprintf(audiopath, "%s%d", audiodev, instance++);
slouken@0
    89
			exists = 0;
slouken@0
    90
			if ( stat(audiopath, &sb) == 0 ) {
slouken@0
    91
				exists = 1;
slouken@0
    92
				audio_fd = open(audiopath, flags, 0); 
slouken@0
    93
			}
slouken@0
    94
		} while ( exists && (audio_fd < 0) );
slouken@0
    95
		audiodev = audiopath;
slouken@0
    96
	}
slouken@0
    97
	if ( path != NULL ) {
slouken@0
    98
		strncpy(path, audiodev, maxlen);
slouken@0
    99
		path[maxlen-1] = '\0';
slouken@0
   100
	}
slouken@0
   101
	return(audio_fd);
slouken@0
   102
}
slouken@0
   103
slouken@0
   104
#elif defined(_AIX)
slouken@0
   105
slouken@0
   106
/* Get the name of the audio device we use for output */
slouken@0
   107
slouken@0
   108
#include <stdlib.h>
slouken@0
   109
#include <sys/types.h>
slouken@0
   110
#include <sys/stat.h>
slouken@0
   111
#include <string.h>
slouken@0
   112
slouken@0
   113
#include "SDL_audiodev_c.h"
slouken@0
   114
slouken@0
   115
#ifndef _PATH_DEV_DSP
slouken@0
   116
#define _PATH_DEV_DSP	"/dev/%caud%c/%c"
slouken@0
   117
#endif
slouken@0
   118
slouken@0
   119
char devsettings[][3] =
slouken@0
   120
{
slouken@0
   121
    { 'p', '0', '1' }, { 'p', '0', '2' }, { 'p', '0', '3' }, { 'p', '0', '4' },
slouken@0
   122
    { 'p', '1', '1' }, { 'p', '1', '2' }, { 'p', '1', '3' }, { 'p', '1', '4' },
slouken@0
   123
    { 'p', '2', '1' }, { 'p', '2', '2' }, { 'p', '2', '3' }, { 'p', '2', '4' },
slouken@0
   124
    { 'p', '3', '1' }, { 'p', '3', '2' }, { 'p', '3', '3' }, { 'p', '3', '4' },
slouken@0
   125
    { 'b', '0', '1' }, { 'b', '0', '2' }, { 'b', '0', '3' }, { 'b', '0', '4' },
slouken@0
   126
    { 'b', '1', '1' }, { 'b', '1', '2' }, { 'b', '1', '3' }, { 'b', '1', '4' },
slouken@0
   127
    { 'b', '2', '1' }, { 'b', '2', '2' }, { 'b', '2', '3' }, { 'b', '2', '4' },
slouken@0
   128
    { 'b', '3', '1' }, { 'b', '3', '2' }, { 'b', '3', '3' }, { 'b', '3', '4' },
slouken@0
   129
    { '\0', '\0', '\0' }
slouken@0
   130
};
slouken@0
   131
slouken@0
   132
static int OpenUserDefinedDevice(char *path, int maxlen, int flags)
slouken@0
   133
{
slouken@0
   134
	const char *audiodev;
slouken@0
   135
	int  audio_fd;
slouken@0
   136
slouken@0
   137
	/* Figure out what our audio device is */
slouken@0
   138
	if ((audiodev=getenv("SDL_PATH_DSP")) == NULL) {
slouken@0
   139
	    audiodev=getenv("AUDIODEV");
slouken@0
   140
	}
slouken@0
   141
	if ( audiodev == NULL ) {
slouken@0
   142
	    return -1;
slouken@0
   143
	}
slouken@0
   144
	audio_fd = open(audiodev, flags, 0);
slouken@0
   145
	if ( path != NULL ) {
slouken@0
   146
		strncpy(path, audiodev, maxlen);
slouken@0
   147
		path[maxlen-1] = '\0';
slouken@0
   148
	}
slouken@0
   149
	return audio_fd;
slouken@0
   150
}
slouken@0
   151
slouken@0
   152
int SDL_OpenAudioPath(char *path, int maxlen, int flags, int classic)
slouken@0
   153
{
slouken@0
   154
    struct stat sb;
slouken@0
   155
    int         audio_fd;
slouken@0
   156
    char        audiopath[1024];
slouken@0
   157
    int         cycle;
slouken@0
   158
slouken@0
   159
    audio_fd = OpenUserDefinedDevice(path,maxlen,flags);
slouken@0
   160
    if ( audio_fd != -1 ) {
slouken@0
   161
        return audio_fd;
slouken@0
   162
    }
slouken@0
   163
slouken@0
   164
    cycle    = 0;
slouken@0
   165
    while( devsettings[cycle][0] != '\0' ) {
slouken@0
   166
        sprintf( audiopath,
slouken@0
   167
                 _PATH_DEV_DSP,
slouken@0
   168
                 devsettings[cycle][0],
slouken@0
   169
                 devsettings[cycle][1],
slouken@0
   170
                 devsettings[cycle][2]);
slouken@0
   171
slouken@0
   172
	if ( stat(audiopath, &sb) == 0 ) {
slouken@0
   173
	    audio_fd = open(audiopath, flags, 0);
slouken@0
   174
	    if ( audio_fd > 0 ) {
slouken@0
   175
		if ( path != NULL ) {
slouken@0
   176
		    strncpy( path, audiopath, maxlen );
slouken@0
   177
		    path[maxlen-1] = '\0';
slouken@0
   178
		}
slouken@0
   179
	        return audio_fd;
slouken@0
   180
	    }
slouken@0
   181
	}
slouken@0
   182
    }
slouken@0
   183
    return -1;
slouken@0
   184
}
slouken@0
   185
slouken@0
   186
#endif /* UNIX system */