audioOutput_oss: cleanups (stolen from -ke)

git-svn-id: https://svn.musicpd.org/mpd/trunk@4518 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Eric Wong 2006-08-01 12:39:10 +00:00
parent 83b619e997
commit 682fe8cee6

View File

@ -25,6 +25,7 @@
#ifdef HAVE_OSS #ifdef HAVE_OSS
#include "../utils.h"
#include "../conf.h" #include "../conf.h"
#include "../log.h" #include "../log.h"
#include "../sig_handlers.h" #include "../sig_handlers.h"
@ -54,7 +55,7 @@
typedef struct _OssData { typedef struct _OssData {
int fd; int fd;
char *device; const char *device;
int channels; int channels;
int sampleRate; int sampleRate;
int bitFormat; int bitFormat;
@ -282,9 +283,6 @@ static OssData *newOssData(void)
static void freeOssData(OssData * od) static void freeOssData(OssData * od)
{ {
if (od->device)
free(od->device);
if (od->supported[OSS_RATE]) if (od->supported[OSS_RATE])
free(od->supported[OSS_RATE]); free(od->supported[OSS_RATE]);
if (od->supported[OSS_CHANNELS]) if (od->supported[OSS_CHANNELS])
@ -307,7 +305,7 @@ static void freeOssData(OssData * od)
#define OSS_STAT_DOESN_T_EXIST -3 #define OSS_STAT_DOESN_T_EXIST -3
#define OSS_STAT_OTHER -4 #define OSS_STAT_OTHER -4
static int oss_statDevice(char *device, int *stErrno) static int oss_statDevice(const char *device, int *stErrno)
{ {
struct stat st; struct stat st;
@ -332,89 +330,76 @@ static int oss_statDevice(char *device, int *stErrno)
return 0; return 0;
} }
static const char *default_devices[] = { "/dev/sound/dsp", "/dev/dsp" };
static int oss_testDefault(void) static int oss_testDefault(void)
{ {
int fd; int fd, i;
fd = open("/dev/sound/dsp", O_WRONLY); for (i = ARRAY_SIZE(default_devices); --i >= 0; ) {
if ((fd = open(default_devices[i], O_WRONLY)) >= 0) {
if (fd >= 0) { xclose(fd);
close(fd);
return 0; return 0;
} }
WARNING("Error opening OSS device \"%s\": %s\n",
WARNING("Error opening OSS device \"/dev/sound/dsp\": %s\n", default_devices[i], strerror(errno));
strerror(errno));
fd = open("/dev/dsp", O_WRONLY);
if (fd >= 0) {
close(fd);
return 0;
} }
WARNING("Error opening OSS device \"/dev/dsp\": %s\n", strerror(errno));
return -1; return -1;
} }
static int oss_initDriver(AudioOutput * audioOutput, ConfigParam * param) static int oss_open_default(AudioOutput *ao, ConfigParam *param, OssData *od)
{ {
BlockParam *bp = NULL; int i;
OssData *od; int err[ARRAY_SIZE(default_devices)];
int ret[ARRAY_SIZE(default_devices)];
for (i = ARRAY_SIZE(default_devices); --i >= 0; ) {
ret[i] = oss_statDevice(default_devices[i], &err[i]);
if (ret[i] == 0) {
od->device = default_devices[i];
return 0;
}
}
if (param) if (param)
bp = getBlockParam(param, "device"); ERROR("Error trying to open specified OSS device"
" at line %i\n", param->line);
else
ERROR("error trying to open default OSS device\n");
od = newOssData(); for (i = ARRAY_SIZE(default_devices) - 1; i >= 0; --i) {
audioOutput->data = od; const char *dev = default_devices[i];
switch(ret[i]) {
if (!bp) { case OSS_STAT_DOESN_T_EXIST:
int err[2]; ERROR("%s not found\n", dev);
int ret[2]; break;
case OSS_STAT_NOT_CHAR_DEV:
ret[0] = oss_statDevice("/dev/sound/dsp", err); ERROR("%s is not a character device\n", dev);
ret[1] = oss_statDevice("/dev/dsp", err + 1); break;
case OSS_STAT_NO_PERMS:
if (ret[0] == 0) ERROR("%s: permission denied\n", dev);
od->device = strdup("/dev/sound/dsp"); break;
else if (ret[1] == 0) default:
od->device = strdup("/dev/dsp"); ERROR("Error accessing %s: %s", dev, strerror(err[i]));
else {
if (param) {
ERROR("Error trying to open default OSS device "
"specified at line %i\n", param->line);
} else {
ERROR("Error trying to open default OSS "
"device\n");
} }
if ((ret[0] == OSS_STAT_DOESN_T_EXIST) &&
(ret[1] == OSS_STAT_DOESN_T_EXIST)) {
ERROR("Neither /dev/dsp nor /dev/sound/dsp "
"were found\n");
} else if (ret[0] == OSS_STAT_NOT_CHAR_DEV) {
ERROR("/dev/sound/dsp is not a char device");
} else if (ret[1] == OSS_STAT_NOT_CHAR_DEV) {
ERROR("/dev/dsp is not a char device");
} else if (ret[0] == OSS_STAT_NO_PERMS) {
ERROR("no permission to access /dev/sound/dsp");
} else if (ret[1] == OSS_STAT_NO_PERMS) {
ERROR("no permission to access /dev/dsp");
} else if (ret[0] == OSS_STAT_OTHER) {
ERROR("Error accessing /dev/sound/dsp: %s",
strerror(err[0]));
} else if (ret[1] == OSS_STAT_OTHER) {
ERROR("Error accessing /dev/dsp: %s",
strerror(err[1]));
} }
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} return 0; /* some compilers can be dumb... */
} else }
od->device = strdup(bp->value);
static int oss_initDriver(AudioOutput * audioOutput, ConfigParam * param)
{
OssData *od = newOssData();
audioOutput->data = od;
if (param) {
BlockParam *bp = getBlockParam(param, "device");
if (bp) {
od->device = bp->value;
return 0; return 0;
}
}
return oss_open_default(audioOutput, param, od);
} }
static void oss_finishDriver(AudioOutput * audioOutput) static void oss_finishDriver(AudioOutput * audioOutput)