blob: 66881e97d03a925167bb01315d77295b0e069f07 [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* mixer.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7** * Redistributions of source code must retain the above copyright
8** notice, this list of conditions and the following disclaimer.
9** * Redistributions in binary form must reproduce the above copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of The Android Open Source Project nor the names of
13** its contributors may be used to endorse or promote products derived
14** from this software without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26** DAMAGE.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
Ben Zhang7ed2ffb2016-04-22 17:59:40 -070031#include <stdint.h>
Simon Wilson79d39652011-05-25 13:44:23 -070032#include <string.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <ctype.h>
Richard Fitzgerald57a87742014-09-09 16:54:12 +010037#include <limits.h>
Dima Krasner696c4482016-03-05 19:50:02 +020038#include <time.h>
Simon Wilson79d39652011-05-25 13:44:23 -070039
Simon Wilson6a52f2c2012-05-04 16:32:10 -070040#include <sys/ioctl.h>
41
Simon Wilson79d39652011-05-25 13:44:23 -070042#include <linux/ioctl.h>
43#define __force
44#define __bitwise
45#define __user
46#include <sound/asound.h>
47
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030048#include <tinyalsa/mixer.h>
Simon Wilson79d39652011-05-25 13:44:23 -070049
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040050/** A mixer control.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080051 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040052 */
Simon Wilson79d39652011-05-25 13:44:23 -070053struct mixer_ctl {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040054 /** The mixer that the mixer control belongs to */
Simon Wilson79d39652011-05-25 13:44:23 -070055 struct mixer *mixer;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080056 /** Information on the control's value (i.e. type, number of values) */
Richard Fitzgerald899cece2014-09-09 17:03:21 +010057 struct snd_ctl_elem_info info;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080058 /** A list of string representations of enumerated values (only valid for enumerated controls) */
Simon Wilson79d39652011-05-25 13:44:23 -070059 char **ename;
60};
61
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040062/** A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080063 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040064 */
Simon Wilson79d39652011-05-25 13:44:23 -070065struct mixer {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040066 /** File descriptor for the card */
Simon Wilson79d39652011-05-25 13:44:23 -070067 int fd;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040068 /** Card information */
Simon Wilsonec281392013-06-28 16:21:31 -070069 struct snd_ctl_card_info card_info;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040070 /** A continuous array of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070071 struct mixer_ctl *ctl;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040072 /** The number of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070073 unsigned int count;
74};
75
Richard Fitzgeraldfd329032014-09-09 17:14:09 +010076static void mixer_cleanup_control(struct mixer_ctl *ctl)
77{
78 unsigned int m;
79
80 if (ctl->ename) {
81 unsigned int max = ctl->info.value.enumerated.items;
82 for (m = 0; m < max; m++)
83 free(ctl->ename[m]);
84 free(ctl->ename);
85 }
86}
87
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040088/** Closes a mixer returned by @ref mixer_open.
89 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080090 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040091 */
Simon Wilson79d39652011-05-25 13:44:23 -070092void mixer_close(struct mixer *mixer)
93{
Richard Fitzgeraldfd329032014-09-09 17:14:09 +010094 unsigned int n;
Simon Wilson79d39652011-05-25 13:44:23 -070095
Simon Wilson193b1c32011-06-07 23:42:38 -070096 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -070097 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -070098
Simon Wilson79d39652011-05-25 13:44:23 -070099 if (mixer->fd >= 0)
100 close(mixer->fd);
101
102 if (mixer->ctl) {
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100103 for (n = 0; n < mixer->count; n++)
104 mixer_cleanup_control(&mixer->ctl[n]);
Simon Wilson79d39652011-05-25 13:44:23 -0700105 free(mixer->ctl);
106 }
107
Simon Wilson79d39652011-05-25 13:44:23 -0700108 free(mixer);
109
110 /* TODO: verify frees */
111}
112
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100113static void *mixer_realloc_z(void *ptr, size_t curnum, size_t newnum, size_t size)
114{
115 int8_t *newp;
116
117 newp = realloc(ptr, size * newnum);
118 if (!newp)
119 return NULL;
120
121 memset(newp + (curnum * size), 0, (newnum - curnum) * size);
122 return newp;
123}
124
125static int add_controls(struct mixer *mixer)
126{
127 struct snd_ctl_elem_list elist;
128 struct snd_ctl_elem_id *eid = NULL;
129 struct mixer_ctl *ctl;
130 int fd = mixer->fd;
131 const unsigned int old_count = mixer->count;
132 unsigned int new_count;
133 unsigned int n;
134
135 memset(&elist, 0, sizeof(elist));
136 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
137 goto fail;
138
139 if (old_count == elist.count)
140 return 0; /* no new controls return unchanged */
141
142 if (old_count > elist.count)
143 return -1; /* driver has removed controls - this is bad */
144
145 ctl = mixer_realloc_z(mixer->ctl, old_count, elist.count,
146 sizeof(struct mixer_ctl));
147 if (!ctl)
148 goto fail;
149
150 mixer->ctl = ctl;
151
152 /* ALSA drivers are not supposed to remove or re-order controls that
153 * have already been created so we know that any new controls must
154 * be after the ones we have already collected
155 */
156 new_count = elist.count;
157 elist.space = new_count - old_count; /* controls we haven't seen before */
158 elist.offset = old_count; /* first control we haven't seen */
159
160 eid = calloc(elist.space, sizeof(struct snd_ctl_elem_id));
161 if (!eid)
162 goto fail;
163
164 elist.pids = eid;
165
166 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
167 goto fail;
168
169 for (n = old_count; n < new_count; n++) {
170 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
171 ei->id.numid = eid[n - old_count].numid;
172 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
173 goto fail_extend;
174 ctl[n].mixer = mixer;
175 }
176
177 mixer->count = new_count;
178 free(eid);
179 return 0;
180
181fail_extend:
182 /* cleanup the control we failed on but leave the ones that were already
183 * added. Also no advantage to shrinking the resized memory block, we
184 * might want to extend the controls again later
185 */
186 mixer_cleanup_control(&ctl[n]);
187
188 mixer->count = n; /* keep controls we successfully added */
189 /* fall through... */
190fail:
191 free(eid);
192 return -1;
193}
194
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400195/** Opens a mixer for a given card.
196 * @param card The card to open the mixer for.
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500197 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800198 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400199 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700200struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700201{
Simon Wilson79d39652011-05-25 13:44:23 -0700202 struct mixer *mixer = NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700203 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700204 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -0700205
Simon Wilson1bd580f2011-06-02 15:58:41 -0700206 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
207 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700208 if (fd < 0)
209 return 0;
210
Simon Wilson79d39652011-05-25 13:44:23 -0700211 mixer = calloc(1, sizeof(*mixer));
212 if (!mixer)
213 goto fail;
214
Simon Wilsonec281392013-06-28 16:21:31 -0700215 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700216 goto fail;
217
Simon Wilson79d39652011-05-25 13:44:23 -0700218 mixer->fd = fd;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100219
220 if (add_controls(mixer) != 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700221 goto fail;
222
Simon Wilson79d39652011-05-25 13:44:23 -0700223 return mixer;
224
225fail:
Simon Wilson79d39652011-05-25 13:44:23 -0700226 if (mixer)
227 mixer_close(mixer);
228 else if (fd >= 0)
229 close(fd);
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100230 return NULL;
231}
232
233/** Some controls may not be present at boot time, e.g. controls from runtime
234 * loadable DSP firmware. This function adds any new controls that have appeared
235 * since mixer_open() or the last call to this function. This assumes a well-
236 * behaved codec driver that does not delete controls that already exists, so
237 * any added controls must be after the last one we already saw. Scanning only
238 * the new controls is much faster than calling mixer_close() then mixer_open()
239 * to re-scan all controls.
240 *
241 * NOTE: this invalidates any struct mixer_ctl pointers previously obtained
242 * from mixer_get_ctl() and mixer_get_ctl_by_name(). Either refresh all your
243 * stored pointers after calling mixer_update_ctls(), or (better) do not
244 * store struct mixer_ctl pointers, instead lookup the control by name or
245 * id only when you are about to use it. The overhead of lookup by id
246 * using mixer_get_ctl() is negligible.
247 * @param mixer An initialized mixer handle.
248 * @returns 0 on success, -1 on failure
249 */
250int mixer_add_new_ctls(struct mixer *mixer)
251{
252 if (!mixer)
253 return 0;
254
255 return add_controls(mixer);
Simon Wilson79d39652011-05-25 13:44:23 -0700256}
257
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500258/** Gets the name of the mixer's card.
259 * @param mixer An initialized mixer handle.
260 * @returns The name of the mixer's card.
261 * @ingroup libtinyalsa-mixer
262 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800263const char *mixer_get_name(const struct mixer *mixer)
Simon Wilsonec281392013-06-28 16:21:31 -0700264{
265 return (const char *)mixer->card_info.name;
266}
267
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500268/** Gets the number of mixer controls for a given mixer.
269 * @param mixer An initialized mixer handle.
270 * @returns The number of mixer controls for the given mixer.
271 * @ingroup libtinyalsa-mixer
272 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800273unsigned int mixer_get_num_ctls(const struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700274{
Simon Wilson193b1c32011-06-07 23:42:38 -0700275 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700276 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700277
Simon Wilson79d39652011-05-25 13:44:23 -0700278 return mixer->count;
279}
280
Taylor Holberton94c7c832016-12-01 18:35:24 -0800281/** Gets the number of mixer controls, that go by a specified name, for a given mixer.
282 * @param mixer An initialized mixer handle.
283 * @param name The name of the mixer control
284 * @returns The number of mixer controls, specified by @p name, for the given mixer.
285 * @ingroup libtinyalsa-mixer
286 */
287unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name)
288{
289 unsigned int n;
290 unsigned int count = 0;
291 struct mixer_ctl *ctl;
292
293 if (!mixer)
294 return 0;
295
296 ctl = mixer->ctl;
297
298 for (n = 0; n < mixer->count; n++)
299 if (!strcmp(name, (char*) ctl[n].info.id.name))
300 count++;
301
302 return count;
303}
304
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500305/** Gets a mixer control handle, by the mixer control's id.
Taylor Holbertona94295b2016-12-01 18:23:16 -0800306 * For non-const access, see @ref mixer_get_ctl
307 * @param mixer An initialized mixer handle.
308 * @param id The control's id in the given mixer.
309 * @returns A handle to the mixer control.
310 * @ingroup libtinyalsa-mixer
311 */
312const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
313{
314 if (mixer && (id < mixer->count))
315 return mixer->ctl + id;
316
317 return NULL;
318}
319
320/** Gets a mixer control handle, by the mixer control's id.
321 * For const access, see @ref mixer_get_ctl_const
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500322 * @param mixer An initialized mixer handle.
323 * @param id The control's id in the given mixer.
324 * @returns A handle to the mixer control.
325 * @ingroup libtinyalsa-mixer
326 */
Simon Wilson79d39652011-05-25 13:44:23 -0700327struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
328{
Simon Wilson98c1f162011-06-07 16:12:32 -0700329 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700330 return mixer->ctl + id;
331
332 return NULL;
333}
334
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500335/** Gets the first instance of mixer control handle, by the mixer control's name.
336 * @param mixer An initialized mixer handle.
337 * @param name The control's name in the given mixer.
338 * @returns A handle to the mixer control.
339 * @ingroup libtinyalsa-mixer
340 */
Simon Wilson79d39652011-05-25 13:44:23 -0700341struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
342{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200343 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
344}
345
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500346/** Gets an instance of mixer control handle, by the mixer control's name and index.
347 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
348 * @param mixer An initialized mixer handle.
349 * @param name The control's name in the given mixer.
350 * @param index The control's index.
351 * @returns A handle to the mixer control.
352 * @ingroup libtinyalsa-mixer
353 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200354struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
355 const char *name,
356 unsigned int index)
357{
Simon Wilson79d39652011-05-25 13:44:23 -0700358 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200359 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700360
Simon Wilson98c1f162011-06-07 16:12:32 -0700361 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700362 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700363
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200364 ctl = mixer->ctl;
365
Simon Wilson79d39652011-05-25 13:44:23 -0700366 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100367 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200368 if (index-- == 0)
369 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700370
371 return NULL;
372}
373
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500374/** Updates the control's info.
375 * This is useful for a program that may be idle for a period of time.
376 * @param ctl An initialized control handle.
377 * @ingroup libtinyalsa-mixer
378 */
Simon Wilson710df882013-06-28 16:17:50 -0700379void mixer_ctl_update(struct mixer_ctl *ctl)
380{
381 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
382}
383
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500384/** Gets the control's ID.
385 * @param ctl An initialized control handle.
386 * @returns On success, the control's ID is returned.
387 * On error, UINT_MAX is returned instead.
388 * @ingroup libtinyalsa-mixer
389 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800390unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100391{
392 if (!ctl)
393 return UINT_MAX;
394
395 /* numid values start at 1, return a 0-base value that
396 * can be passed to mixer_get_ctl()
397 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100398 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100399}
400
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500401/** Gets the name of the control.
402 * @param ctl An initialized control handle.
403 * @returns On success, the name of the control.
404 * On error, NULL.
405 * @ingroup libtinyalsa-mixer
406 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800407const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700408{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800409 if (!ctl)
410 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700411
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100412 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700413}
414
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500415/** Gets the value type of the control.
416 * @param ctl An initialized control handle
417 * @returns On success, the type of mixer control.
418 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
419 * @ingroup libtinyalsa-mixer
420 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800421enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700422{
Simon Wilson193b1c32011-06-07 23:42:38 -0700423 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700424 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700425
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100426 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700427 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
428 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
429 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
430 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
431 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
432 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
433 default: return MIXER_CTL_TYPE_UNKNOWN;
434 };
435}
436
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500437/** Gets the string that describes the value type of the control.
438 * @param ctl An initialized control handle
439 * @returns On success, a string describing type of mixer control.
440 * @ingroup libtinyalsa-mixer
441 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800442const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700443{
Simon Wilson193b1c32011-06-07 23:42:38 -0700444 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700445 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700446
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100447 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700448 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
449 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
450 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
451 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
452 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
453 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
454 default: return "Unknown";
455 };
456}
457
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500458/** Gets the number of values in the control.
459 * @param ctl An initialized control handle
460 * @returns The number of values in the mixer control
461 * @ingroup libtinyalsa-mixer
462 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800463unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700464{
Simon Wilson193b1c32011-06-07 23:42:38 -0700465 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700466 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700467
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100468 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700469}
470
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800471static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilson79d39652011-05-25 13:44:23 -0700472{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200473 if ((percent > 100) || (percent < 0)) {
474 return -EINVAL;
475 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700476
Baptiste Robert4a484e12013-09-12 15:37:53 +0200477 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700478
Simon Wilson79d39652011-05-25 13:44:23 -0700479 return ei->value.integer.min + (range * percent) / 100;
480}
481
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800482static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700483{
484 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700485
Simon Wilson79d39652011-05-25 13:44:23 -0700486 if (range == 0)
487 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700488
Simon Wilson79d39652011-05-25 13:44:23 -0700489 return ((value - ei->value.integer.min) / range) * 100;
490}
491
Taylor Holberton4e557192016-11-23 11:48:06 -0800492/** Gets a percentage representation of a specified control value.
493 * @param ctl An initialized control handle.
494 * @param id The index of the value within the control.
495 * @returns On success, the percentage representation of the control value.
496 * On failure, -EINVAL is returned.
497 * @ingroup libtinyalsa-mixer
498 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800499int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700500{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100501 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700502 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700503
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100504 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700505}
506
Taylor Holberton4e557192016-11-23 11:48:06 -0800507/** Sets the value of a control by percent, specified by the value index.
508 * @param ctl An initialized control handle.
509 * @param id The index of the value to set
510 * @param percent A percentage value between 0 and 100.
511 * @returns On success, zero is returned.
512 * On failure, non-zero is returned.
513 * @ingroup libtinyalsa-mixer
514 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700515int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700516{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100517 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700518 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700519
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100520 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700521}
522
Taylor Holberton4e557192016-11-23 11:48:06 -0800523/** Gets the value of a control.
524 * @param ctl An initialized control handle.
525 * @param id The index of the control value.
526 * @returns On success, the specified value is returned.
527 * On failure, -EINVAL is returned.
528 * @ingroup libtinyalsa-mixer
529 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800530int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700531{
Simon Wilson79d39652011-05-25 13:44:23 -0700532 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700533 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700534
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100535 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700536 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700537
Simon Wilson79d39652011-05-25 13:44:23 -0700538 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100539 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700540 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
541 if (ret < 0)
542 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700543
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100544 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700545 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700546 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700547
548 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700549 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700550
551 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
552 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700553
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500554 case SNDRV_CTL_ELEM_TYPE_BYTES:
555 return ev.value.bytes.data[id];
556
Simon Wilson79d39652011-05-25 13:44:23 -0700557 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700558 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700559 }
560
561 return 0;
562}
563
Taylor Holberton4e557192016-11-23 11:48:06 -0800564/** Gets the contents of a control's value array.
565 * @param ctl An initialized control handle.
566 * @param array A pointer to write the array data to.
567 * The size of this array must be equal to the number of items in the array
568 * multiplied by the size of each item.
569 * @param count The number of items in the array.
570 * This parameter must match the number of items in the control.
571 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
572 * @returns On success, zero.
573 * On failure, non-zero.
574 * @ingroup libtinyalsa-mixer
575 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800576int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100577{
578 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530579 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700580 size_t size;
581 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100582
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100583 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100584 return -EINVAL;
585
586 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100587 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100588
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100589 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700590 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
591 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530592 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
593 if (ret < 0)
594 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700595 size = sizeof(ev.value.integer.value[0]);
596 source = ev.value.integer.value;
597 break;
598
599 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530600 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100601 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530602 struct snd_ctl_tlv *tlv;
603 int ret;
604
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700605 if (count > SIZE_MAX - sizeof(*tlv))
606 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530607 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700608 if (!tlv)
609 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100610 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530611 tlv->length = count;
612 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
613
614 source = tlv->tlv;
615 memcpy(array, source, count);
616
617 free(tlv);
618
619 return ret;
620 } else {
621 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
622 if (ret < 0)
623 return ret;
624 size = sizeof(ev.value.bytes.data[0]);
625 source = ev.value.bytes.data;
626 break;
627 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700628
629 default:
630 return -EINVAL;
631 }
632
633 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100634
635 return 0;
636}
637
Taylor Holberton4e557192016-11-23 11:48:06 -0800638/** Sets the value of a control, specified by the value index.
639 * @param ctl An initialized control handle.
640 * @param id The index of the value within the control.
641 * @param value The value to set.
642 * This must be in a range specified by @ref mixer_ctl_get_range_min
643 * and @ref mixer_ctl_get_range_max.
644 * @returns On success, zero is returned.
645 * On failure, non-zero is returned.
646 * @ingroup libtinyalsa-mixer
647 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700648int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700649{
650 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700651 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700652
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100653 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700654 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700655
656 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100657 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700658 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
659 if (ret < 0)
660 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700661
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100662 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700663 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700664 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700665 break;
666
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700667 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200668 if ((value < mixer_ctl_get_range_min(ctl)) ||
669 (value > mixer_ctl_get_range_max(ctl))) {
670 return -EINVAL;
671 }
672
Simon Wilsond2cb5032011-06-04 00:57:17 -0700673 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700674 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700675
Simon Wilson066c9f62011-06-05 18:23:05 -0700676 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
677 ev.value.enumerated.item[id] = value;
678 break;
679
David.Coutherutce2b6342013-06-11 16:22:57 +0200680 case SNDRV_CTL_ELEM_TYPE_BYTES:
681 ev.value.bytes.data[id] = value;
682 break;
683
Simon Wilson79d39652011-05-25 13:44:23 -0700684 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700685 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700686 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700687
Simon Wilson79d39652011-05-25 13:44:23 -0700688 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
689}
690
Taylor Holberton4e557192016-11-23 11:48:06 -0800691/** Sets the contents of a control's value array.
692 * @param ctl An initialized control handle.
693 * @param array The array containing control values.
694 * @param count The number of values in the array.
695 * This must match the number of values in the control.
696 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
697 * @returns On success, zero.
698 * On failure, non-zero.
699 * @ingroup libtinyalsa-mixer
700 */
Simon Wilson38f87f32012-10-23 15:05:23 -0700701int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100702{
703 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700704 size_t size;
705 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100706
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100707 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100708 return -EINVAL;
709
710 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100711 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100712
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100713 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700714 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
715 case SNDRV_CTL_ELEM_TYPE_INTEGER:
716 size = sizeof(ev.value.integer.value[0]);
717 dest = ev.value.integer.value;
718 break;
719
720 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530721 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100722 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530723 struct snd_ctl_tlv *tlv;
724 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700725 if (count > SIZE_MAX - sizeof(*tlv))
726 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530727 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700728 if (!tlv)
729 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100730 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530731 tlv->length = count;
732 memcpy(tlv->tlv, array, count);
733
734 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
735 free(tlv);
736
737 return ret;
738 } else {
739 size = sizeof(ev.value.bytes.data[0]);
740 dest = ev.value.bytes.data;
741 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700742 break;
743
744 default:
745 return -EINVAL;
746 }
747
748 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100749
750 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
751}
752
Taylor Holberton4e557192016-11-23 11:48:06 -0800753/** Gets the minimum value of an control.
754 * The control must have an integer type.
755 * The type of the control can be checked with @ref mixer_ctl_get_type.
756 * @param ctl An initialized control handle.
757 * @returns On success, the minimum value of the control.
758 * On failure, -EINVAL.
759 * @ingroup libtinyalsa-mixer
760 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800761int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700762{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100763 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700764 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700765
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100766 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700767}
768
Taylor Holberton4e557192016-11-23 11:48:06 -0800769/** Gets the maximum value of an control.
770 * The control must have an integer type.
771 * The type of the control can be checked with @ref mixer_ctl_get_type.
772 * @param ctl An initialized control handle.
773 * @returns On success, the maximum value of the control.
774 * On failure, -EINVAL.
775 * @ingroup libtinyalsa-mixer
776 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800777int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700778{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100779 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700780 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700781
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100782 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700783}
784
Taylor Holberton4e557192016-11-23 11:48:06 -0800785/** Get the number of enumerated items in the control.
786 * @param ctl An initialized control handle.
787 * @returns The number of enumerated items in the control.
788 * @ingroup libtinyalsa-mixer
789 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800790unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700791{
Simon Wilson193b1c32011-06-07 23:42:38 -0700792 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700793 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700794
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100795 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700796}
797
David.Coutherut9b423962013-06-03 13:45:51 +0200798int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
799{
800 struct snd_ctl_elem_info tmp;
801 unsigned int m;
802 char **enames;
803
804 if (ctl->ename) {
805 return 0;
806 }
807
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400808 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +0200809 if (!enames)
810 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400811 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200812 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400813 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +0200814 tmp.value.enumerated.item = m;
815 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
816 goto fail;
817 enames[m] = strdup(tmp.value.enumerated.name);
818 if (!enames[m])
819 goto fail;
820 }
821 ctl->ename = enames;
822 return 0;
823
824fail:
825 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400826 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200827 if (enames[m]) {
828 free(enames[m]);
829 }
830 }
831 free(enames);
832 }
833 return -1;
834}
835
Taylor Holberton4e557192016-11-23 11:48:06 -0800836/** Gets the string representation of an enumerated item.
837 * @param ctl An initialized control handle.
838 * @param enum_id The index of the enumerated value.
839 * @returns A string representation of the enumerated item.
840 * @ingroup libtinyalsa-mixer
841 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800842const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
843 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700844{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100845 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400846 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200847 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800848 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700849
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800850 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700851}
852
Taylor Holberton4e557192016-11-23 11:48:06 -0800853/** Set an enumeration value by string value.
854 * @param ctl An enumerated mixer control.
855 * @param string The string representation of an enumeration.
856 * @returns On success, zero.
857 * On failure, zero.
858 * @ingroup libtinyalsa-mixer
859 */
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700860int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
861{
862 unsigned int i, num_enums;
863 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700864 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700865
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400866 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200867 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -0700868 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700869
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100870 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700871 for (i = 0; i < num_enums; i++) {
872 if (!strcmp(string, ctl->ename[i])) {
873 memset(&ev, 0, sizeof(ev));
874 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100875 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700876 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
877 if (ret < 0)
878 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700879 return 0;
880 }
881 }
882
Simon Wilson193b1c32011-06-07 23:42:38 -0700883 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700884}
885