summaryrefslogtreecommitdiff
path: root/cras/src/alsa_plugin/ctl_cras.c
blob: 822b63ab206b0ff480fd86042e86a591bfae8900 (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <alsa/asoundlib.h>
#include <alsa/control_external.h>
#include <cras_client.h>

static const size_t MAX_IODEVS = 10; /* Max devices to print out. */
static const size_t MAX_IONODES = 20; /* Max ionodes to print out. */

/* Support basic input/output volume/mute only. */
enum CTL_CRAS_MIXER_CONTROLS {
	CTL_CRAS_MIXER_PLAYBACK_SWITCH,
	CTL_CRAS_MIXER_PLAYBACK_VOLUME,
	CTL_CRAS_MIXER_CAPTURE_SWITCH,
	CTL_CRAS_MIXER_CAPTURE_VOLUME,
	NUM_CTL_CRAS_MIXER_ELEMS
};

/* Hold info specific to each control. */
struct cras_mixer_control {
	const char *name;
	int type;
	unsigned int access;
	unsigned int count;
};

/* CRAS mixer elements. */
static const struct cras_mixer_control cras_elems[NUM_CTL_CRAS_MIXER_ELEMS] = {
	{ "Master Playback Switch", SND_CTL_ELEM_TYPE_BOOLEAN,
	  SND_CTL_EXT_ACCESS_READWRITE, 1 },
	{ "Master Playback Volume", SND_CTL_ELEM_TYPE_INTEGER,
	  SND_CTL_EXT_ACCESS_READWRITE, 1 },
	{ "Capture Switch", SND_CTL_ELEM_TYPE_BOOLEAN,
	  SND_CTL_EXT_ACCESS_READWRITE, 1 },
	{ "Capture Volume", SND_CTL_ELEM_TYPE_INTEGER,
	  SND_CTL_EXT_ACCESS_READWRITE, 1 },
};

/* Holds the client and ctl plugin pointers. */
struct ctl_cras {
	snd_ctl_ext_t ext_ctl;
	struct cras_client *client;
};

/* Frees resources when the plugin is closed. */
static void ctl_cras_close(snd_ctl_ext_t *ext_ctl)
{
	struct ctl_cras *cras = (struct ctl_cras *)ext_ctl->private_data;

	if (cras) {
		cras_client_stop(cras->client);
		cras_client_destroy(cras->client);
	}
	free(cras);
}

/* Lists available controls. */
static int ctl_cras_elem_list(snd_ctl_ext_t *ext_ctl, unsigned int offset,
			      snd_ctl_elem_id_t *id)
{
	snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
	if (offset >= NUM_CTL_CRAS_MIXER_ELEMS)
		return -EINVAL;
	snd_ctl_elem_id_set_name(id, cras_elems[offset].name);
	return 0;
}

/* Returns the number of available controls. */
static int ctl_cras_elem_count(snd_ctl_ext_t *ext_ctl)
{
	return NUM_CTL_CRAS_MIXER_ELEMS;
}

/* Gets a control key from a search id. */
static snd_ctl_ext_key_t ctl_cras_find_elem(snd_ctl_ext_t *ext_ctl,
					    const snd_ctl_elem_id_t *id)
{
	const char *name;
	unsigned int numid;

	numid = snd_ctl_elem_id_get_numid(id);
	if (numid - 1 < NUM_CTL_CRAS_MIXER_ELEMS)
		return numid - 1;

	name = snd_ctl_elem_id_get_name(id);

	for (numid = 0; numid < NUM_CTL_CRAS_MIXER_ELEMS; numid++)
		if (strcmp(cras_elems[numid].name, name) == 0)
			return numid;

	return SND_CTL_EXT_KEY_NOT_FOUND;
}

/* Fills accessibility, type and count based on the specified control. */
static int ctl_cras_get_attribute(snd_ctl_ext_t *ext_ctl, snd_ctl_ext_key_t key,
				  int *type, unsigned int *acc,
				  unsigned int *count)
{
	if (key >= NUM_CTL_CRAS_MIXER_ELEMS)
		return -EINVAL;
	*type = cras_elems[key].type;
	*acc = cras_elems[key].access;
	*count = cras_elems[key].count;
	return 0;
}

/* Returns the range of the specified control.  The volume sliders always run
 * from 0 to 100 for CRAS. */
static int ctl_cras_get_integer_info(snd_ctl_ext_t *ext_ctl,
				     snd_ctl_ext_key_t key, long *imin,
				     long *imax, long *istep)
{
	*istep = 0;
	*imin = 0;
	*imax = 100;
	return 0;
}

static long capture_index_to_gain(struct cras_client *client, long index)
{
	long min;
	long max;
	long dB_step;

	min = cras_client_get_system_min_capture_gain(client);
	max = cras_client_get_system_max_capture_gain(client);
	if (min >= max)
		return min;

	dB_step = (max - min) / 100;

	if (index <= 0)
		return min;
	if (index >= 100)
		return max;
	return index * dB_step + min;
}

static long capture_gain_to_index(struct cras_client *client, long gain)
{
	long min;
	long max;
	long dB_step;

	min = cras_client_get_system_min_capture_gain(client);
	max = cras_client_get_system_max_capture_gain(client);
	if (min >= max)
		return 0;

	dB_step = (max - min) / 100;

	if (gain <= min)
		return 0;
	if (gain >= max)
		return 100;
	return (gain - min) / dB_step;
}

static int get_nodes(struct cras_client *client, enum CRAS_STREAM_DIRECTION dir,
		     struct cras_ionode_info *nodes, size_t num_nodes)
{
	struct cras_iodev_info devs[MAX_IODEVS];
	size_t num_devs;
	int rc;

	if (dir == CRAS_STREAM_OUTPUT)
		rc = cras_client_get_output_devices(client, devs, nodes,
						    &num_devs, &num_nodes);
	else
		rc = cras_client_get_input_devices(client, devs, nodes,
						   &num_devs, &num_nodes);
	if (rc < 0)
		return 0;
	return num_nodes;
}

/* Gets the value of the given control from CRAS and puts it in value. */
static int ctl_cras_read_integer(snd_ctl_ext_t *ext_ctl, snd_ctl_ext_key_t key,
				 long *value)
{
	struct ctl_cras *cras = (struct ctl_cras *)ext_ctl->private_data;
	struct cras_ionode_info nodes[MAX_IONODES];
	int num_nodes, i;

	switch (key) {
	case CTL_CRAS_MIXER_PLAYBACK_SWITCH:
		*value = !cras_client_get_user_muted(cras->client);
		break;
	case CTL_CRAS_MIXER_PLAYBACK_VOLUME:
		num_nodes = get_nodes(cras->client, CRAS_STREAM_OUTPUT, nodes,
				      MAX_IONODES);
		for (i = 0; i < num_nodes; i++) {
			if (!nodes[i].active)
				continue;
			*value = nodes[i].volume;
			break;
		}
		break;
	case CTL_CRAS_MIXER_CAPTURE_SWITCH:
		*value = !cras_client_get_system_capture_muted(cras->client);
		break;
	case CTL_CRAS_MIXER_CAPTURE_VOLUME:
		num_nodes = get_nodes(cras->client, CRAS_STREAM_INPUT, nodes,
				      MAX_IONODES);
		for (i = 0; i < num_nodes; i++) {
			if (!nodes[i].active)
				continue;
			*value = capture_gain_to_index(cras->client,
						       nodes[i].capture_gain);
			break;
		}
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

/* Writes the given values to CRAS. */
static int ctl_cras_write_integer(snd_ctl_ext_t *ext_ctl, snd_ctl_ext_key_t key,
				  long *value)
{
	struct ctl_cras *cras = (struct ctl_cras *)ext_ctl->private_data;
	struct cras_ionode_info nodes[MAX_IONODES];
	int num_nodes, i;
	long gain;

	switch (key) {
	case CTL_CRAS_MIXER_PLAYBACK_SWITCH:
		cras_client_set_user_mute(cras->client, !(*value));
		break;
	case CTL_CRAS_MIXER_PLAYBACK_VOLUME:
		num_nodes = get_nodes(cras->client, CRAS_STREAM_OUTPUT, nodes,
				      MAX_IONODES);
		for (i = 0; i < num_nodes; i++) {
			if (!nodes[i].active)
				continue;
			cras_client_set_node_volume(
				cras->client,
				cras_make_node_id(nodes[i].iodev_idx,
						  nodes[i].ionode_idx),
				*value);
		}
		break;
	case CTL_CRAS_MIXER_CAPTURE_SWITCH:
		cras_client_set_system_capture_mute(cras->client, !(*value));
		break;
	case CTL_CRAS_MIXER_CAPTURE_VOLUME:
		gain = capture_index_to_gain(cras->client, *value);
		num_nodes = get_nodes(cras->client, CRAS_STREAM_INPUT, nodes,
				      MAX_IONODES);
		for (i = 0; i < num_nodes; i++) {
			if (!nodes[i].active)
				continue;
			cras_client_set_node_capture_gain(
				cras->client,
				cras_make_node_id(nodes[i].iodev_idx,
						  nodes[i].ionode_idx),
				gain);
		}
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static const snd_ctl_ext_callback_t ctl_cras_ext_callback = {
	.close = ctl_cras_close,
	.elem_count = ctl_cras_elem_count,
	.elem_list = ctl_cras_elem_list,
	.find_elem = ctl_cras_find_elem,
	.get_attribute = ctl_cras_get_attribute,
	.get_integer_info = ctl_cras_get_integer_info,
	.read_integer = ctl_cras_read_integer,
	.write_integer = ctl_cras_write_integer,
};

SND_CTL_PLUGIN_DEFINE_FUNC(cras)
{
	struct ctl_cras *cras;
	int rc;

	cras = malloc(sizeof(*cras));
	if (cras == NULL)
		return -ENOMEM;

	rc = cras_client_create(&cras->client);
	if (rc != 0 || cras->client == NULL) {
		fprintf(stderr, "Couldn't create CRAS client\n");
		free(cras);
		return rc;
	}

	rc = cras_client_connect(cras->client);
	if (rc < 0) {
		fprintf(stderr, "Couldn't connect to cras.\n");
		cras_client_destroy(cras->client);
		free(cras);
		return rc;
	}

	rc = cras_client_run_thread(cras->client);
	if (rc < 0) {
		fprintf(stderr, "Couldn't start client thread.\n");
		cras_client_stop(cras->client);
		cras_client_destroy(cras->client);
		free(cras);
		return rc;
	}

	rc = cras_client_connected_wait(cras->client);
	if (rc < 0) {
		fprintf(stderr, "CRAS client wouldn't connect.\n");
		cras_client_stop(cras->client);
		cras_client_destroy(cras->client);
		free(cras);
		return rc;
	}

	cras->ext_ctl.version = SND_CTL_EXT_VERSION;
	cras->ext_ctl.card_idx = 0;
	strncpy(cras->ext_ctl.id, "cras", sizeof(cras->ext_ctl.id) - 1);
	cras->ext_ctl.id[sizeof(cras->ext_ctl.id) - 1] = '\0';
	strncpy(cras->ext_ctl.driver, "CRAS plugin",
		sizeof(cras->ext_ctl.driver) - 1);
	cras->ext_ctl.driver[sizeof(cras->ext_ctl.driver) - 1] = '\0';
	strncpy(cras->ext_ctl.name, "CRAS", sizeof(cras->ext_ctl.name) - 1);
	cras->ext_ctl.name[sizeof(cras->ext_ctl.name) - 1] = '\0';
	strncpy(cras->ext_ctl.longname, "CRAS",
		sizeof(cras->ext_ctl.longname) - 1);
	cras->ext_ctl.longname[sizeof(cras->ext_ctl.longname) - 1] = '\0';
	strncpy(cras->ext_ctl.mixername, "CRAS",
		sizeof(cras->ext_ctl.mixername) - 1);
	cras->ext_ctl.mixername[sizeof(cras->ext_ctl.mixername) - 1] = '\0';
	cras->ext_ctl.poll_fd = -1;

	cras->ext_ctl.callback = &ctl_cras_ext_callback;
	cras->ext_ctl.private_data = cras;

	rc = snd_ctl_ext_create(&cras->ext_ctl, name, mode);
	if (rc < 0) {
		cras_client_stop(cras->client);
		cras_client_destroy(cras->client);
		free(cras);
		return rc;
	}

	*handlep = cras->ext_ctl.handle;
	return 0;
}

SND_CTL_PLUGIN_SYMBOL(cras);