2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/mixer-oss.c
21 * @brief OSS mixer support
23 * Mono output devices aren't explicitly supported (but may work
29 #if HAVE_SYS_SOUNDCARD_H
35 #include <sys/ioctl.h>
36 #include <sys/soundcard.h>
38 #include "configuration.h"
44 /* documentation does not match implementation! */
45 #ifndef SOUND_MIXER_READ
46 # define SOUND_MIXER_READ(x) MIXER_READ(x)
48 #ifndef SOUND_MIXER_WRITE
49 # define SOUND_MIXER_WRITE(x) MIXER_WRITE(x)
52 /** @brief Channel names */
53 static const char *channels
[] = SOUND_DEVICE_NAMES
;
55 /** @brief Convert channel name to number */
56 static int mixer_channel(const char *c
) {
59 if(!c
[strspn(c
, "0123456789")])
62 for(n
= 0; n
< sizeof channels
/ sizeof *channels
; ++n
)
63 if(!strcmp(channels
[n
], c
))
69 /** @brief Open the OSS mixer device and return its fd */
70 static int oss_do_open(void) {
73 if((fd
= open(config
->mixer
, O_RDWR
, 0)) < 0) {
74 static char *reported
;
76 if(!reported
|| strcmp(reported
, config
->mixer
)) {
79 reported
= xstrdup(config
->mixer
);
80 error(errno
, "error opening %s", config
->mixer
);
86 /** @brief Get the OSS mixer setting */
87 static int oss_do_get(int *left
, int *right
, int fd
, int ch
) {
90 if(ioctl(fd
, SOUND_MIXER_READ(ch
), &r
) == -1) {
91 error(errno
, "error reading %s channel %s",
92 config
->mixer
, config
->channel
);
96 *right
= (r
>> 8) & 0xff;
100 /** @brief Get OSS volume */
101 static int oss_get(int *left
, int *right
) {
106 && (ch
= mixer_channel(config
->channel
)) != -1) {
107 if((fd
= oss_do_open()) < 0)
109 if(oss_do_get(left
, right
, fd
, ch
) < 0) {
119 /** @brief Set OSS volume */
120 static int oss_set(int *left
, int *right
) {
125 && (ch
= mixer_channel(config
->channel
)) != -1) {
126 if((fd
= oss_do_open()) < 0)
128 r
= (*left
& 0xff) + (*right
& 0xff) * 256;
129 if(ioctl(fd
, SOUND_MIXER_WRITE(ch
), &r
) == -1) {
130 error(errno
, "error changing %s channel %s",
131 config
->mixer
, config
->channel
);
135 if(oss_do_get(left
, right
, fd
, ch
) < 0) {
145 /** @brief OSS mixer vtable */
146 const struct mixer mixer_oss
= {