chiark / gitweb /
Merge Core Audio fixes
[disorder] / lib / mixer-oss.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
4  *
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.
9  * 
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.
14  * 
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/>.
17  */
18 /** @file lib/mixer-oss.c
19  * @brief OSS mixer support
20  *
21  * Mono output devices aren't explicitly supported (but may work
22  * nonetheless).
23  */
24
25 #include "common.h"
26
27 #if HAVE_SYS_SOUNDCARD_H
28
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <stddef.h>
33 #include <sys/ioctl.h>
34 #include <sys/soundcard.h>
35
36 #include "configuration.h"
37 #include "mixer.h"
38 #include "log.h"
39 #include "syscalls.h"
40 #include "mem.h"
41
42 /* documentation does not match implementation! */
43 #ifndef SOUND_MIXER_READ
44 # define SOUND_MIXER_READ(x) MIXER_READ(x)
45 #endif
46 #ifndef SOUND_MIXER_WRITE
47 # define SOUND_MIXER_WRITE(x) MIXER_WRITE(x)
48 #endif
49
50 /** @brief Channel names */
51 static const char *channels[] = SOUND_DEVICE_NAMES;
52
53 /** @brief Convert channel name to number */
54 static int mixer_channel(const char *c) {
55   unsigned n;
56   
57   if(!c[strspn(c, "0123456789")])
58     return atoi(c);
59   else {
60     for(n = 0; n < sizeof channels / sizeof *channels; ++n)
61       if(!strcmp(channels[n], c))
62         return n;
63     return -1;
64   }
65 }
66
67 /** @brief Open the OSS mixer device and return its fd */
68 static int oss_do_open(void) {
69   int fd;
70   
71   if((fd = open(config->mixer, O_RDWR, 0)) < 0) {
72     static char *reported;
73
74     if(!reported || strcmp(reported, config->mixer)) {
75       if(reported)
76         xfree(reported);
77       reported = xstrdup(config->mixer);
78       error(errno, "error opening %s", config->mixer);
79     }
80   }
81   return fd;
82 }
83
84 /** @brief Get the OSS mixer setting */
85 static int oss_do_get(int *left, int *right, int fd, int ch) {
86   int r;
87   
88   if(ioctl(fd, SOUND_MIXER_READ(ch), &r) == -1) {
89     error(errno, "error reading %s channel %s",
90           config->mixer, config->channel);
91     return -1;
92   }
93   *left = r & 0xff;
94   *right = (r >> 8) & 0xff;
95   return 0;
96 }
97
98 /** @brief Get OSS volume */
99 static int oss_get(int *left, int *right) {
100   int ch, fd;
101
102   if(config->mixer
103      && config->channel
104      && (ch = mixer_channel(config->channel)) != -1) {
105     if((fd = oss_do_open()) < 0)
106       return -1;
107     if(oss_do_get(left, right, fd, ch) < 0) {
108       xclose(fd);
109       return -1;
110     }
111     xclose(fd);
112     return 0;
113   } else
114     return -1;
115 }
116
117 /** @brief Set OSS volume */
118 static int oss_set(int *left, int *right) {
119   int ch, fd, r;
120
121   if(config->mixer
122      && config->channel
123      && (ch = mixer_channel(config->channel)) != -1) {
124     if((fd = oss_do_open()) < 0)
125       return -1;
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);
130       xclose(fd);
131       return -1;
132     }
133     if(oss_do_get(left, right, fd, ch) < 0) {
134       xclose(fd);
135       return -1;
136     }
137     xclose(fd);
138     return 0;
139   } else
140     return -1;
141 }
142
143 /** @brief OSS mixer vtable */
144 const struct mixer mixer_oss = {
145   BACKEND_OSS,
146   oss_get,
147   oss_set,
148   "/dev/mixer",
149   "pcm"
150 };
151 #endif
152
153 /*
154 Local Variables:
155 c-basic-offset:2
156 comment-column:40
157 End:
158 */