-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer_oss.c
252 lines (228 loc) · 5.44 KB
/
mixer_oss.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright 2008-2013 Various Authors
* Copyright 2004-2005 Timo Hirvonen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "mixer.h"
#include "op.h"
#include "utils.h"
#include "xmalloc.h"
#include "debug.h"
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#if defined(__OpenBSD__)
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif
enum {
OSS_MIXER_CHANNEL_VOLUME,
OSS_MIXER_CHANNEL_BASS,
OSS_MIXER_CHANNEL_TREBLE,
OSS_MIXER_CHANNEL_SYNTH,
OSS_MIXER_CHANNEL_PCM,
OSS_MIXER_CHANNEL_SPEAKER,
OSS_MIXER_CHANNEL_LINE,
OSS_MIXER_CHANNEL_MIC,
OSS_MIXER_CHANNEL_CD,
OSS_MIXER_CHANNEL_IMIX,
OSS_MIXER_CHANNEL_ALTPCM,
OSS_MIXER_CHANNEL_RECLEV,
OSS_MIXER_CHANNEL_IGAIN,
OSS_MIXER_CHANNEL_OGAIN,
OSS_MIXER_CHANNEL_LINE1,
OSS_MIXER_CHANNEL_LINE2,
OSS_MIXER_CHANNEL_LINE3,
OSS_MIXER_CHANNEL_DIGITAL1,
OSS_MIXER_CHANNEL_DIGITAL2,
OSS_MIXER_CHANNEL_DIGITAL3,
OSS_MIXER_CHANNEL_PHONEIN,
OSS_MIXER_CHANNEL_PHONEOUT,
OSS_MIXER_CHANNEL_VIDEO,
OSS_MIXER_CHANNEL_RADIO,
OSS_MIXER_CHANNEL_MONITOR,
OSS_MIXER_CHANNEL_MAX
};
static int mixer_fd = -1;
static int mixer_devmask;
/* static int mixer_recmask; */
/* static int mixer_recsrc; */
/* static int mixer_stereodevs; */
static char mixer_channels[OSS_MIXER_CHANNEL_MAX];
/* configuration */
static char *oss_mixer_device = NULL;
static int oss_volume_controls_pcm = 1;
static int mixer_open(const char *device)
{
int i;
mixer_fd = open(device, O_RDWR);
if (mixer_fd == -1)
return -1;
ioctl(mixer_fd, SOUND_MIXER_READ_DEVMASK, &mixer_devmask);
/* ioctl(mixer_fd, SOUND_MIXER_READ_RECMASK, &mixer_recmask); */
/* ioctl(mixer_fd, SOUND_MIXER_READ_RECSRC, &mixer_recsrc); */
/* ioctl(mixer_fd, SOUND_MIXER_READ_STEREODEVS, &mixer_stereodevs); */
i = 0;
while (i < min(SOUND_MIXER_NRDEVICES, OSS_MIXER_CHANNEL_MAX)) {
mixer_channels[i] = (mixer_devmask >> i) & 1;
i++;
}
while (i < OSS_MIXER_CHANNEL_MAX)
mixer_channels[i++] = 0;
return 0;
}
static int mixer_set_level(int channel, int l, int r)
{
int tmp;
tmp = (l & 0x7f) + ((r & 0x7f) << 8);
if (ioctl(mixer_fd, MIXER_WRITE(channel), &tmp) == -1)
return -1;
return 0;
}
static int mixer_get_level(int channel, int *l, int *r)
{
int tmp;
if (ioctl(mixer_fd, MIXER_READ(channel), &tmp) == -1)
return -1;
*l = tmp & 0x7f;
*r = (tmp >> 8) & 0x7f;
return 0;
}
static int oss_device_exists(const char *device)
{
struct stat s;
if (stat(device, &s) == 0) {
d_print("device %s exists\n", device);
return 1;
}
d_print("device %s does not exist\n", device);
return 0;
}
static int oss_mixer_init(void)
{
const char *new_mixer_dev = "/dev/sound/mixer";
const char *mixer_dev = "/dev/mixer";
if (oss_mixer_device) {
if (oss_device_exists(oss_mixer_device))
return 0;
free(oss_mixer_device);
oss_mixer_device = NULL;
return -1;
}
if (oss_device_exists(new_mixer_dev)) {
oss_mixer_device = xstrdup(new_mixer_dev);
return 0;
}
if (oss_device_exists(mixer_dev)) {
oss_mixer_device = xstrdup(mixer_dev);
return 0;
}
return -1;
}
static int oss_mixer_exit(void)
{
if (oss_mixer_device) {
free(oss_mixer_device);
oss_mixer_device = NULL;
}
return 0;
}
static int oss_mixer_open(int *volume_max)
{
*volume_max = 100;
if (mixer_open(oss_mixer_device) == 0)
return 0;
return -1;
}
static int oss_mixer_close(void)
{
close(mixer_fd);
mixer_fd = -1;
return 0;
}
static int oss_mixer_set_volume(int l, int r)
{
if (oss_volume_controls_pcm) {
return mixer_set_level(OSS_MIXER_CHANNEL_PCM, l, r);
} else {
return mixer_set_level(OSS_MIXER_CHANNEL_VOLUME, l, r);
}
}
static int oss_mixer_get_volume(int *l, int *r)
{
if (oss_volume_controls_pcm) {
return mixer_get_level(OSS_MIXER_CHANNEL_PCM, l, r);
} else {
return mixer_get_level(OSS_MIXER_CHANNEL_VOLUME, l, r);
}
}
static int oss_mixer_set_option(int key, const char *val)
{
switch (key) {
case 0:
if (strcasecmp(val, "pcm") == 0) {
oss_volume_controls_pcm = 1;
} else if (strcasecmp(val, "master") == 0) {
oss_volume_controls_pcm = 0;
} else {
errno = EINVAL;
return -OP_ERROR_ERRNO;
}
break;
case 1:
free(oss_mixer_device);
oss_mixer_device = xstrdup(val);
break;
default:
return -OP_ERROR_NOT_OPTION;
}
return 0;
}
static int oss_mixer_get_option(int key, char **val)
{
switch (key) {
case 0:
if (oss_volume_controls_pcm) {
*val = xstrdup("PCM");
} else {
*val = xstrdup("Master");
}
break;
case 1:
if (oss_mixer_device)
*val = xstrdup(oss_mixer_device);
break;
default:
return -OP_ERROR_NOT_OPTION;
}
return 0;
}
const struct mixer_plugin_ops op_mixer_ops = {
.init = oss_mixer_init,
.exit = oss_mixer_exit,
.open = oss_mixer_open,
.close = oss_mixer_close,
.set_volume = oss_mixer_set_volume,
.get_volume = oss_mixer_get_volume,
.set_option = oss_mixer_set_option,
.get_option = oss_mixer_get_option
};
const char * const op_mixer_options[] = {
"channel",
"device",
NULL
};