1
/*
2
 * Copyright 2008-2011 Various Authors
3
 * Copyright 2004-2005 Timo Hirvonen
4
 *
5
 * mixer_sun.c by alex <pukpuk@gmx.de>
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License as
9
 * published by the Free Software Foundation; either version 2 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include <sys/types.h>
22
#include <sys/ioctl.h>
23
#include <sys/audioio.h>
24
#include <sys/stat.h>
25
#include <fcntl.h>
26
#include <string.h>
27
#include <unistd.h>
28
29
#include "debug.h"
30
#include "mixer.h"
31
#include "op.h"
32
#include "sf.h"
33
#include "xmalloc.h"
34
35
static int sun_mixer_device_id = -1;
36
static int sun_mixer_channels = -1;
37
static int sun_mixer_volume_delta = -1;
38
static int mixer_fd = -1;
39
40
static char *sun_mixer_device = NULL;
41
static char *sun_mixer_channel = NULL;
42
43
static int mixer_open(const char *);
44
static int min_delta(int, int, int);
45
static int sun_device_exists(const char *);
46
static int sun_mixer_init(void);
47
static int sun_mixer_exit(void);
48
static int sun_mixer_open(int *);
49
static int sun_mixer_close(void);
50
static int sun_mixer_set_volume(int, int);
51
static int sun_mixer_get_volume(int *, int *);
52
static int sun_mixer_set_option(int, const char *);
53
static int sun_mixer_get_option(int, char **);
54
55
static int mixer_open(const char *dev)
56
{
57
	struct mixer_devinfo minf;
58
	int output_class;
59
60
	mixer_fd = open(dev, O_RDWR);
61
	if (mixer_fd == -1)
62
		return -1;
63
64
	output_class = -1;
65
	sun_mixer_device_id = -1;
66
	/* determine output class */
67
	minf.index = 0;
68
	while (ioctl(mixer_fd, AUDIO_MIXER_DEVINFO, &minf) != -1) {
69
		if (minf.type == AUDIO_MIXER_CLASS) {
70
			if (strcmp(minf.label.name, AudioCoutputs) == 0)
71
				output_class = minf.index;
72
		}
73
		++minf.index;
74
	}
75
	/* no output class found?? something must be wrong */
76
	if (output_class == -1)
77
		return -1;
78
79
	minf.index = 0;
80
	/* query all mixer devices and try choose the correct one */
81
	while (ioctl(mixer_fd, AUDIO_MIXER_DEVINFO, &minf) != -1) {
82
		/* only scan output channels */
83
		if (minf.type == AUDIO_MIXER_VALUE && minf.prev == AUDIO_MIXER_LAST &&
84
		    minf.mixer_class == output_class) {
85
			if (strcasecmp(minf.label.name, sun_mixer_channel) == 0) {
86
				sun_mixer_volume_delta = minf.un.v.delta;
87
				sun_mixer_device_id = minf.index;
88
				sun_mixer_channels = minf.un.v.num_channels;
89
			}
90
		}
91
		++minf.index;
92
	}
93
94
	if (sun_mixer_device_id == -1)
95
		return -1;
96
97
	d_print("sun: found mixer-channel: %s, devid: %d, delta: %d, channels: %d\n", sun_mixer_channel,
98
	    sun_mixer_device_id, sun_mixer_volume_delta, sun_mixer_channels);
99
100
	if (sun_mixer_volume_delta == 0)
101
		sun_mixer_volume_delta = 1;
102
103
	return 0;
104
105
}
106
107
static int min_delta(int oval, int nval, int delta)
108
{
109
	if (oval > nval && oval - nval < delta)
110
		nval -= delta;
111
	else if (oval < nval && nval - oval < delta)
112
		nval += delta;
113
114
	nval = (nval < 0) ? 0 : nval;
115
	nval = (nval > AUDIO_MAX_GAIN) ? AUDIO_MAX_GAIN : nval;
116
117
	return nval;
118
}
119
120
static int sun_device_exists(const char *dev)
121
{
122
	struct stat s;
123
124
	if (stat(dev, &s) == 0) {
125
		d_print("device %s exists\n", dev);
126
		return 1;
127
	}
128
	d_print("device %s does not exist\n", dev);
129
130
	return 0;
131
}
132
133
static int sun_mixer_init(void)
134
{
135
	const char *mixer_dev = "/dev/mixer";
136
137
	if (sun_mixer_device != NULL) {
138
		if (sun_device_exists(sun_mixer_device))
139
			return 0;
140
		free(sun_mixer_device);
141
		sun_mixer_device = NULL;
142
		return -1;
143
	}
144
	if (sun_device_exists(mixer_dev)) {
145
		sun_mixer_device = xstrdup(mixer_dev);
146
		return 0;
147
	}
148
149
	return -1;
150
}
151
152
static int sun_mixer_exit(void)
153
{
154
	if (sun_mixer_device != NULL) {
155
		free(sun_mixer_device);
156
		sun_mixer_device = NULL;
157
	}
158
	if (sun_mixer_channel != NULL) {
159
		free(sun_mixer_channel);
160
		sun_mixer_channel = NULL;
161
	}
162
163
	return 0;
164
}
165
166
static int sun_mixer_open(int *vol_max)
167
{
168
	const char *mixer_channel = "master";
169
170
	/* set default mixer channel */
171
	if (sun_mixer_channel == NULL)
172
		sun_mixer_channel = xstrdup(mixer_channel);
173
174
	if (mixer_open(sun_mixer_device) == 0) {
175
		*vol_max = AUDIO_MAX_GAIN;
176
		return 0;
177
	}
178
179
	return -1;
180
}
181
182
static int sun_mixer_close(void)
183
{
184
	if (mixer_fd != -1) {
185
		close(mixer_fd);
186
		mixer_fd = -1;
187
	}
188
189
	return 0;
190
}
191
192
static int sun_mixer_set_volume(int l, int r)
193
{
194
	struct mixer_ctrl minf;
195
	int ovall, ovalr;
196
197
	if (sun_mixer_get_volume(&ovall, &ovalr) == -1)
198
		return -1;
199
200
	/* OpenBSD mixer values are `discrete' */
201
	l = min_delta(ovall, l, sun_mixer_volume_delta);
202
	r = min_delta(ovalr, r, sun_mixer_volume_delta);
203
204
	minf.type = AUDIO_MIXER_VALUE;
205
	minf.dev = sun_mixer_device_id;
206
207
	if (sun_mixer_channels == 1)
208
		minf.un.value.level[AUDIO_MIXER_LEVEL_MONO] = l;
209
	else {
210
		minf.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
211
		minf.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
212
	}
213
	minf.un.value.num_channels = sun_mixer_channels;
214
215
	if (ioctl(mixer_fd, AUDIO_MIXER_WRITE, &minf) == -1)
216
		return -1;
217
218
	return 0;
219
}
220
221
static int sun_mixer_get_volume(int *l, int *r)
222
{
223
	struct mixer_ctrl minf;
224
225
	minf.dev = sun_mixer_device_id;
226
	minf.type = AUDIO_MIXER_VALUE;
227
	minf.un.value.num_channels = sun_mixer_channels;
228
229
	if (ioctl(mixer_fd, AUDIO_MIXER_READ, &minf) == -1)
230
		return -1;
231
232
	if (sun_mixer_channels == 1) {
233
		*l = minf.un.value.level[AUDIO_MIXER_LEVEL_MONO];
234
		*r = *l;
235
	} else {
236
		*l = minf.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
237
		*r = minf.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
238
	}
239
240
	return 0;
241
}
242
243
static int sun_mixer_set_option(int key, const char *val)
244
{
245
	switch (key) {
246
	case 0:
247
		if (sun_mixer_channel != NULL)
248
			free(sun_mixer_channel);
249
		sun_mixer_channel = xstrdup(val);
250
		break;
251
	case 1:
252
		free(sun_mixer_device);
253
		sun_mixer_device = xstrdup(val);
254
		break;
255
	default:
256
		return -OP_ERROR_NOT_OPTION;
257
	}
258
259
	return 0;
260
}
261
262
static int sun_mixer_get_option(int key, char **val)
263
{
264
	switch (key) {
265
	case 0:
266
		if (sun_mixer_channel)
267
			*val = xstrdup(sun_mixer_channel);
268
		break;
269
	case 1:
270
		if (sun_mixer_device)
271
			*val = xstrdup(sun_mixer_device);
272
		break;
273
	default:
274
		return -OP_ERROR_NOT_OPTION;
275
	}
276
277
	return 0;
278
}
279
280
const struct mixer_plugin_ops op_mixer_ops = {
281
	.init = sun_mixer_init,
282
	.exit = sun_mixer_exit,
283
	.open = sun_mixer_open,
284
	.close = sun_mixer_close,
285
	.set_volume = sun_mixer_set_volume,
286
	.get_volume = sun_mixer_get_volume,
287
	.set_option = sun_mixer_set_option,
288
	.get_option = sun_mixer_get_option
289
};
290
291
const char * const op_mixer_options[] = {
292
	"channel",
293
	"device",
294
	NULL
295
};