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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file lib/mixer-oss.c
19 * @brief OSS mixer support
21 * Mono output devices aren't explicitly supported (but may work
27 #if HAVE_SYS_SOUNDCARD_H
33 #include <sys/ioctl.h>
34 #include <sys/soundcard.h>
36 #include "configuration.h"
42 /* documentation does not match implementation! */
43 #ifndef SOUND_MIXER_READ
44 # define SOUND_MIXER_READ(x) MIXER_READ(x)
46 #ifndef SOUND_MIXER_WRITE
47 # define SOUND_MIXER_WRITE(x) MIXER_WRITE(x)
50 /** @brief Channel names */
51 static const char *channels
[] = SOUND_DEVICE_NAMES
;
53 /** @brief Convert channel name to number */
54 static int mixer_channel(const char *c
) {
57 if(!c
[strspn(c
, "0123456789")])
60 for(n
= 0; n
< sizeof channels
/ sizeof *channels
; ++n
)
61 if(!strcmp(channels
[n
], c
))
67 /** @brief Open the OSS mixer device and return its fd */
68 static int oss_do_open(void) {
71 if((fd
= open(config
->mixer
, O_RDWR
, 0)) < 0) {
72 static char *reported
;
74 if(!reported
|| strcmp(reported
, config
->mixer
)) {
77 reported
= xstrdup(config
->mixer
);
78 error(errno
, "error opening %s", config
->mixer
);
84 /** @brief Get the OSS mixer setting */
85 static int oss_do_get(int *left
, int *right
, int fd
, int ch
) {
88 if(ioctl(fd
, SOUND_MIXER_READ(ch
), &r
) == -1) {
89 error(errno
, "error reading %s channel %s",
90 config
->mixer
, config
->channel
);
94 *right
= (r
>> 8) & 0xff;
98 /** @brief Get OSS volume */
99 static int oss_get(int *left
, int *right
) {
104 && (ch
= mixer_channel(config
->channel
)) != -1) {
105 if((fd
= oss_do_open()) < 0)
107 if(oss_do_get(left
, right
, fd
, ch
) < 0) {
117 /** @brief Set OSS volume */
118 static int oss_set(int *left
, int *right
) {
123 && (ch
= mixer_channel(config
->channel
)) != -1) {
124 if((fd
= oss_do_open()) < 0)
126 r
= (*left
& 0xff) + (*right
& 0xff) * 256;
127 if(ioctl(fd
, SOUND_MIXER_WRITE(ch
), &r
) == -1) {
128 error(errno
, "error changing %s channel %s",
129 config
->mixer
, config
->channel
);
133 if(oss_do_get(left
, right
, fd
, ch
) < 0) {
143 /** @brief OSS mixer vtable */
144 const struct mixer mixer_oss
= {