]> code.delx.au - pulseaudio/blob - src/modules/oss-util.c
move modules to ${libdir}/polypaudio-${PA_MAJORMINOR}/modules/
[pulseaudio] / src / modules / oss-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <sys/soundcard.h>
28 #include <sys/ioctl.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #include <polyp/error.h>
38
39 #include <polypcore/core-util.h>
40 #include <polypcore/log.h>
41
42 #include "oss-util.h"
43
44 int pa_oss_open(const char *device, int *mode, int* pcaps) {
45 int fd = -1;
46 assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
47
48 if (*mode == O_RDWR) {
49 if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
50 int dcaps, *tcaps;
51 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
52
53 tcaps = pcaps ? pcaps : &dcaps;
54
55 if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
56 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
57 goto fail;
58 }
59
60 if (*tcaps & DSP_CAP_DUPLEX)
61 goto success;
62
63 pa_log_warn(__FILE__": '%s' doesn't support full duplex", device);
64
65 close(fd);
66 }
67
68 if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
69 if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 0) {
70 pa_log(__FILE__": open('%s'): %s", device, pa_cstrerror(errno));
71 goto fail;
72 }
73 }
74 } else {
75 if ((fd = open(device, *mode|O_NDELAY)) < 0) {
76 pa_log(__FILE__": open('%s'): %s", device, pa_cstrerror(errno));
77 goto fail;
78 }
79 }
80
81 success:
82
83 if (pcaps) {
84 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
85 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno));
86 goto fail;
87 }
88 }
89
90 pa_fd_set_cloexec(fd, 1);
91
92 return fd;
93
94 fail:
95 if (fd >= 0)
96 close(fd);
97 return -1;
98 }
99
100 int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
101 int format, channels, speed, reqformat;
102 static const int format_trans[PA_SAMPLE_MAX] = {
103 [PA_SAMPLE_U8] = AFMT_U8,
104 [PA_SAMPLE_ALAW] = AFMT_A_LAW,
105 [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
106 [PA_SAMPLE_S16LE] = AFMT_S16_LE,
107 [PA_SAMPLE_S16BE] = AFMT_S16_BE,
108 [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
109 [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
110 };
111
112 assert(fd >= 0 && ss);
113
114 reqformat = format = format_trans[ss->format];
115 if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
116 format = AFMT_S16_NE;
117 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
118 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
119 format = f;
120 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
121 format = AFMT_U8;
122 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
123 pa_log(__FILE__": SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : pa_cstrerror(errno));
124 return -1;
125 } else
126 ss->format = PA_SAMPLE_U8;
127 } else
128 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
129 } else
130 ss->format = PA_SAMPLE_S16NE;
131 }
132
133 channels = ss->channels;
134 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
135 pa_log(__FILE__": SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno));
136 return -1;
137 }
138 assert(channels);
139 ss->channels = channels;
140
141 speed = ss->rate;
142 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
143 pa_log(__FILE__": SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno));
144 return -1;
145 }
146 assert(speed);
147 ss->rate = speed;
148
149 return 0;
150 }
151
152 static int simple_log2(int v) {
153 int k = 0;
154
155 for (;;) {
156 v >>= 1;
157 if (!v) break;
158 k++;
159 }
160
161 return k;
162 }
163
164 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
165 int arg;
166 arg = ((int) nfrags << 16) | simple_log2(frag_size);
167
168 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
169 pa_log(__FILE__": SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno));
170 return -1;
171 }
172
173 return 0;
174 }
175
176 static int pa_oss_get_volume(int fd, int mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
177 char cv[PA_CVOLUME_SNPRINT_MAX];
178 unsigned vol;
179
180 assert(fd >= 0);
181 assert(ss);
182 assert(volume);
183
184 if (ioctl(fd, mixer, &vol) < 0)
185 return -1;
186
187 volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
188
189 if ((volume->channels = ss->channels) >= 2)
190 volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
191
192 pa_log_debug(__FILE__": Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
193 return 0;
194 }
195
196 static int pa_oss_set_volume(int fd, int mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
197 char cv[PA_CVOLUME_SNPRINT_MAX];
198 unsigned vol;
199 pa_volume_t l, r;
200
201 l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
202
203 vol = (l*100)/PA_VOLUME_NORM;
204
205 if (ss->channels >= 2) {
206 r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
207 vol |= ((r*100)/PA_VOLUME_NORM) << 8;
208 }
209
210 if (ioctl(fd, mixer, &vol) < 0)
211 return -1;
212
213 pa_log_debug(__FILE__": Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
214 return 0;
215 }
216
217 int pa_oss_get_pcm_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
218 return pa_oss_get_volume(fd, SOUND_MIXER_READ_PCM, ss, volume);
219 }
220
221 int pa_oss_set_pcm_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
222 return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_PCM, ss, volume);
223 }
224
225 int pa_oss_get_input_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
226 return pa_oss_get_volume(fd, SOUND_MIXER_READ_IGAIN, ss, volume);
227 }
228
229 int pa_oss_set_input_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
230 return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_IGAIN, ss, volume);
231 }
232
233 int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
234 FILE *f;
235 const char *e = NULL;
236 int n, r = -1;
237 int b = 0;
238
239 if (strncmp(dev, "/dev/dsp", 8) == 0)
240 e = dev+8;
241 else if (strncmp(dev, "/dev/adsp", 9) == 0)
242 e = dev+9;
243 else
244 return -1;
245
246 if (*e == 0)
247 n = 0;
248 else if (*e >= '0' && *e <= '9' && *(e+1) == 0)
249 n = *e - '0';
250 else
251 return -1;
252
253 if (!(f = fopen("/dev/sndstat", "r")) &&
254 !(f = fopen("/proc/sndstat", "r")) &&
255 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
256
257 if (errno != ENOENT)
258 pa_log_warn(__FILE__": failed to open OSS sndstat device: %s", pa_cstrerror(errno));
259
260 return -1;
261 }
262
263 while (!feof(f)) {
264 char line[64];
265 int device;
266
267 if (!fgets(line, sizeof(line), f))
268 break;
269
270 line[strcspn(line, "\r\n")] = 0;
271
272 if (!b) {
273 b = strcmp(line, "Audio devices:") == 0;
274 continue;
275 }
276
277 if (line[0] == 0)
278 break;
279
280 if (sscanf(line, "%i: ", &device) != 1)
281 continue;
282
283 if (device == n) {
284 char *k = strchr(line, ':');
285 assert(k);
286 k++;
287 k += strspn(k, " ");
288
289 if (pa_endswith(k, " (DUPLEX)"))
290 k[strlen(k)-9] = 0;
291
292 pa_strlcpy(name, k, l);
293 r = 0;
294 break;
295 }
296 }
297
298 fclose(f);
299 return r;
300 }