blob: 742c2315bee5d2e0fd9ea1e90a6609b1f1c1f3bd [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>
Pankaj Bharadiya010121a2017-01-11 08:57:06 +053039#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070040
Simon Wilson6a52f2c2012-05-04 16:32:10 -070041#include <sys/ioctl.h>
42
Simon Wilson79d39652011-05-25 13:44:23 -070043#include <linux/ioctl.h>
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050044
45#ifndef __force
Simon Wilson79d39652011-05-25 13:44:23 -070046#define __force
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050047#endif
48
49#ifndef __bitwise
Simon Wilson79d39652011-05-25 13:44:23 -070050#define __bitwise
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050051#endif
52
53#ifndef __user
Simon Wilson79d39652011-05-25 13:44:23 -070054#define __user
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050055#endif
56
Simon Wilson79d39652011-05-25 13:44:23 -070057#include <sound/asound.h>
58
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030059#include <tinyalsa/mixer.h>
Simon Wilson79d39652011-05-25 13:44:23 -070060
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040061/** A mixer control.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080062 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040063 */
Simon Wilson79d39652011-05-25 13:44:23 -070064struct mixer_ctl {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040065 /** The mixer that the mixer control belongs to */
Simon Wilson79d39652011-05-25 13:44:23 -070066 struct mixer *mixer;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080067 /** Information on the control's value (i.e. type, number of values) */
Richard Fitzgerald899cece2014-09-09 17:03:21 +010068 struct snd_ctl_elem_info info;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080069 /** A list of string representations of enumerated values (only valid for enumerated controls) */
Simon Wilson79d39652011-05-25 13:44:23 -070070 char **ename;
71};
72
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040073/** A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080074 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040075 */
Simon Wilson79d39652011-05-25 13:44:23 -070076struct mixer {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040077 /** File descriptor for the card */
Simon Wilson79d39652011-05-25 13:44:23 -070078 int fd;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040079 /** Card information */
Simon Wilsonec281392013-06-28 16:21:31 -070080 struct snd_ctl_card_info card_info;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040081 /** A continuous array of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070082 struct mixer_ctl *ctl;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040083 /** The number of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070084 unsigned int count;
85};
86
Richard Fitzgeraldfd329032014-09-09 17:14:09 +010087static void mixer_cleanup_control(struct mixer_ctl *ctl)
88{
89 unsigned int m;
90
91 if (ctl->ename) {
92 unsigned int max = ctl->info.value.enumerated.items;
93 for (m = 0; m < max; m++)
94 free(ctl->ename[m]);
95 free(ctl->ename);
96 }
97}
98
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040099/** Closes a mixer returned by @ref mixer_open.
100 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800101 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400102 */
Simon Wilson79d39652011-05-25 13:44:23 -0700103void mixer_close(struct mixer *mixer)
104{
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100105 unsigned int n;
Simon Wilson79d39652011-05-25 13:44:23 -0700106
Simon Wilson193b1c32011-06-07 23:42:38 -0700107 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700108 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700109
Simon Wilson79d39652011-05-25 13:44:23 -0700110 if (mixer->fd >= 0)
111 close(mixer->fd);
112
113 if (mixer->ctl) {
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100114 for (n = 0; n < mixer->count; n++)
115 mixer_cleanup_control(&mixer->ctl[n]);
Simon Wilson79d39652011-05-25 13:44:23 -0700116 free(mixer->ctl);
117 }
118
Simon Wilson79d39652011-05-25 13:44:23 -0700119 free(mixer);
120
121 /* TODO: verify frees */
122}
123
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100124static void *mixer_realloc_z(void *ptr, size_t curnum, size_t newnum, size_t size)
125{
126 int8_t *newp;
127
128 newp = realloc(ptr, size * newnum);
129 if (!newp)
130 return NULL;
131
132 memset(newp + (curnum * size), 0, (newnum - curnum) * size);
133 return newp;
134}
135
136static int add_controls(struct mixer *mixer)
137{
138 struct snd_ctl_elem_list elist;
139 struct snd_ctl_elem_id *eid = NULL;
140 struct mixer_ctl *ctl;
141 int fd = mixer->fd;
142 const unsigned int old_count = mixer->count;
143 unsigned int new_count;
144 unsigned int n;
145
146 memset(&elist, 0, sizeof(elist));
147 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
148 goto fail;
149
150 if (old_count == elist.count)
151 return 0; /* no new controls return unchanged */
152
153 if (old_count > elist.count)
154 return -1; /* driver has removed controls - this is bad */
155
156 ctl = mixer_realloc_z(mixer->ctl, old_count, elist.count,
157 sizeof(struct mixer_ctl));
158 if (!ctl)
159 goto fail;
160
161 mixer->ctl = ctl;
162
163 /* ALSA drivers are not supposed to remove or re-order controls that
164 * have already been created so we know that any new controls must
165 * be after the ones we have already collected
166 */
167 new_count = elist.count;
168 elist.space = new_count - old_count; /* controls we haven't seen before */
169 elist.offset = old_count; /* first control we haven't seen */
170
171 eid = calloc(elist.space, sizeof(struct snd_ctl_elem_id));
172 if (!eid)
173 goto fail;
174
175 elist.pids = eid;
176
177 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
178 goto fail;
179
180 for (n = old_count; n < new_count; n++) {
181 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
182 ei->id.numid = eid[n - old_count].numid;
183 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
184 goto fail_extend;
185 ctl[n].mixer = mixer;
186 }
187
188 mixer->count = new_count;
189 free(eid);
190 return 0;
191
192fail_extend:
193 /* cleanup the control we failed on but leave the ones that were already
194 * added. Also no advantage to shrinking the resized memory block, we
195 * might want to extend the controls again later
196 */
197 mixer_cleanup_control(&ctl[n]);
198
199 mixer->count = n; /* keep controls we successfully added */
200 /* fall through... */
201fail:
202 free(eid);
203 return -1;
204}
205
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400206/** Opens a mixer for a given card.
207 * @param card The card to open the mixer for.
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500208 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800209 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400210 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700211struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700212{
Simon Wilson79d39652011-05-25 13:44:23 -0700213 struct mixer *mixer = NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700214 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700215 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -0700216
Simon Wilson1bd580f2011-06-02 15:58:41 -0700217 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
218 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700219 if (fd < 0)
220 return 0;
221
Simon Wilson79d39652011-05-25 13:44:23 -0700222 mixer = calloc(1, sizeof(*mixer));
223 if (!mixer)
224 goto fail;
225
Simon Wilsonec281392013-06-28 16:21:31 -0700226 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700227 goto fail;
228
Simon Wilson79d39652011-05-25 13:44:23 -0700229 mixer->fd = fd;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100230
231 if (add_controls(mixer) != 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700232 goto fail;
233
Simon Wilson79d39652011-05-25 13:44:23 -0700234 return mixer;
235
236fail:
Simon Wilson79d39652011-05-25 13:44:23 -0700237 if (mixer)
238 mixer_close(mixer);
239 else if (fd >= 0)
240 close(fd);
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100241 return NULL;
242}
243
244/** Some controls may not be present at boot time, e.g. controls from runtime
245 * loadable DSP firmware. This function adds any new controls that have appeared
246 * since mixer_open() or the last call to this function. This assumes a well-
247 * behaved codec driver that does not delete controls that already exists, so
248 * any added controls must be after the last one we already saw. Scanning only
249 * the new controls is much faster than calling mixer_close() then mixer_open()
250 * to re-scan all controls.
251 *
252 * NOTE: this invalidates any struct mixer_ctl pointers previously obtained
253 * from mixer_get_ctl() and mixer_get_ctl_by_name(). Either refresh all your
254 * stored pointers after calling mixer_update_ctls(), or (better) do not
255 * store struct mixer_ctl pointers, instead lookup the control by name or
256 * id only when you are about to use it. The overhead of lookup by id
257 * using mixer_get_ctl() is negligible.
258 * @param mixer An initialized mixer handle.
259 * @returns 0 on success, -1 on failure
260 */
261int mixer_add_new_ctls(struct mixer *mixer)
262{
263 if (!mixer)
264 return 0;
265
266 return add_controls(mixer);
Simon Wilson79d39652011-05-25 13:44:23 -0700267}
268
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500269/** Gets the name of the mixer's card.
270 * @param mixer An initialized mixer handle.
271 * @returns The name of the mixer's card.
272 * @ingroup libtinyalsa-mixer
273 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800274const char *mixer_get_name(const struct mixer *mixer)
Simon Wilsonec281392013-06-28 16:21:31 -0700275{
276 return (const char *)mixer->card_info.name;
277}
278
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500279/** Gets the number of mixer controls for a given mixer.
280 * @param mixer An initialized mixer handle.
281 * @returns The number of mixer controls for the given mixer.
282 * @ingroup libtinyalsa-mixer
283 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800284unsigned int mixer_get_num_ctls(const struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700285{
Simon Wilson193b1c32011-06-07 23:42:38 -0700286 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700287 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700288
Simon Wilson79d39652011-05-25 13:44:23 -0700289 return mixer->count;
290}
291
Taylor Holberton94c7c832016-12-01 18:35:24 -0800292/** Gets the number of mixer controls, that go by a specified name, for a given mixer.
293 * @param mixer An initialized mixer handle.
294 * @param name The name of the mixer control
295 * @returns The number of mixer controls, specified by @p name, for the given mixer.
296 * @ingroup libtinyalsa-mixer
297 */
298unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name)
299{
300 unsigned int n;
301 unsigned int count = 0;
302 struct mixer_ctl *ctl;
303
304 if (!mixer)
305 return 0;
306
307 ctl = mixer->ctl;
308
309 for (n = 0; n < mixer->count; n++)
310 if (!strcmp(name, (char*) ctl[n].info.id.name))
311 count++;
312
313 return count;
314}
315
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530316/** Subscribes for the mixer events.
317 * @param mixer A mixer handle.
318 * @param subscribe value indicating subscribe or unsubscribe for events
319 * @returns On success, zero.
320 * On failure, non-zero.
321 * @ingroup libtinyalsa-mixer
322 */
323int mixer_subscribe_events(struct mixer *mixer, int subscribe)
324{
325 if (ioctl(mixer->fd, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) {
326 return -1;
327 }
328 return 0;
329}
330
331/** Wait for mixer events.
332 * @param mixer A mixer handle.
333 * @param timeout timout value
Pankaj Bharadiya95c79d82017-01-11 11:28:02 +0530334 * @returns On success, 1.
335 * On failure, -errno.
336 * On timeout, 0
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530337 * @ingroup libtinyalsa-mixer
338 */
339int mixer_wait_event(struct mixer *mixer, int timeout)
340{
341 struct pollfd pfd;
342
343 pfd.fd = mixer->fd;
344 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
345
346 for (;;) {
347 int err;
348 err = poll(&pfd, 1, timeout);
349 if (err < 0)
350 return -errno;
351 if (!err)
352 return 0;
353 if (pfd.revents & (POLLERR | POLLNVAL))
354 return -EIO;
355 if (pfd.revents & (POLLIN | POLLOUT))
356 return 1;
357 }
358}
359
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500360/** Gets a mixer control handle, by the mixer control's id.
Taylor Holbertona94295b2016-12-01 18:23:16 -0800361 * For non-const access, see @ref mixer_get_ctl
362 * @param mixer An initialized mixer handle.
363 * @param id The control's id in the given mixer.
364 * @returns A handle to the mixer control.
365 * @ingroup libtinyalsa-mixer
366 */
367const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
368{
369 if (mixer && (id < mixer->count))
370 return mixer->ctl + id;
371
372 return NULL;
373}
374
375/** Gets a mixer control handle, by the mixer control's id.
376 * For const access, see @ref mixer_get_ctl_const
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500377 * @param mixer An initialized mixer handle.
378 * @param id The control's id in the given mixer.
379 * @returns A handle to the mixer control.
380 * @ingroup libtinyalsa-mixer
381 */
Simon Wilson79d39652011-05-25 13:44:23 -0700382struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
383{
Simon Wilson98c1f162011-06-07 16:12:32 -0700384 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700385 return mixer->ctl + id;
386
387 return NULL;
388}
389
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500390/** Gets the first instance of mixer control handle, by the mixer control's name.
391 * @param mixer An initialized mixer handle.
392 * @param name The control's name in the given mixer.
393 * @returns A handle to the mixer control.
394 * @ingroup libtinyalsa-mixer
395 */
Simon Wilson79d39652011-05-25 13:44:23 -0700396struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
397{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200398 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
399}
400
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500401/** Gets an instance of mixer control handle, by the mixer control's name and index.
402 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
403 * @param mixer An initialized mixer handle.
404 * @param name The control's name in the given mixer.
405 * @param index The control's index.
406 * @returns A handle to the mixer control.
407 * @ingroup libtinyalsa-mixer
408 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200409struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
410 const char *name,
411 unsigned int index)
412{
Simon Wilson79d39652011-05-25 13:44:23 -0700413 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200414 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700415
Simon Wilson98c1f162011-06-07 16:12:32 -0700416 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700417 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700418
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200419 ctl = mixer->ctl;
420
Simon Wilson79d39652011-05-25 13:44:23 -0700421 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100422 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200423 if (index-- == 0)
424 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700425
426 return NULL;
427}
428
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500429/** Updates the control's info.
430 * This is useful for a program that may be idle for a period of time.
431 * @param ctl An initialized control handle.
432 * @ingroup libtinyalsa-mixer
433 */
Simon Wilson710df882013-06-28 16:17:50 -0700434void mixer_ctl_update(struct mixer_ctl *ctl)
435{
436 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
437}
438
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530439/** Checks the control for TLV Read/Write access.
440 * @param ctl An initialized control handle.
441 * @returns On success, non-zero.
442 * On failure, zero.
443 * @ingroup libtinyalsa-mixer
444 */
445int mixer_ctl_is_access_tlv_rw(const struct mixer_ctl *ctl)
446{
447 return (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
448}
449
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500450/** Gets the control's ID.
451 * @param ctl An initialized control handle.
452 * @returns On success, the control's ID is returned.
453 * On error, UINT_MAX is returned instead.
454 * @ingroup libtinyalsa-mixer
455 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800456unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100457{
458 if (!ctl)
459 return UINT_MAX;
460
461 /* numid values start at 1, return a 0-base value that
462 * can be passed to mixer_get_ctl()
463 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100464 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100465}
466
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500467/** Gets the name of the control.
468 * @param ctl An initialized control handle.
469 * @returns On success, the name of the control.
470 * On error, NULL.
471 * @ingroup libtinyalsa-mixer
472 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800473const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700474{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800475 if (!ctl)
476 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700477
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100478 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700479}
480
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500481/** Gets the value type of the control.
482 * @param ctl An initialized control handle
483 * @returns On success, the type of mixer control.
484 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
485 * @ingroup libtinyalsa-mixer
486 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800487enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700488{
Simon Wilson193b1c32011-06-07 23:42:38 -0700489 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700490 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700491
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100492 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700493 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
494 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
495 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
496 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
497 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
498 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
499 default: return MIXER_CTL_TYPE_UNKNOWN;
500 };
501}
502
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500503/** Gets the string that describes the value type of the control.
504 * @param ctl An initialized control handle
505 * @returns On success, a string describing type of mixer control.
506 * @ingroup libtinyalsa-mixer
507 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800508const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700509{
Simon Wilson193b1c32011-06-07 23:42:38 -0700510 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700511 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700512
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100513 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700514 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
515 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
516 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
517 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
518 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
519 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
520 default: return "Unknown";
521 };
522}
523
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500524/** Gets the number of values in the control.
525 * @param ctl An initialized control handle
526 * @returns The number of values in the mixer control
527 * @ingroup libtinyalsa-mixer
528 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800529unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700530{
Simon Wilson193b1c32011-06-07 23:42:38 -0700531 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700532 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700533
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100534 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700535}
536
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800537static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilson79d39652011-05-25 13:44:23 -0700538{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200539 if ((percent > 100) || (percent < 0)) {
540 return -EINVAL;
541 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700542
Baptiste Robert4a484e12013-09-12 15:37:53 +0200543 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700544
Simon Wilson79d39652011-05-25 13:44:23 -0700545 return ei->value.integer.min + (range * percent) / 100;
546}
547
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800548static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700549{
550 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700551
Simon Wilson79d39652011-05-25 13:44:23 -0700552 if (range == 0)
553 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700554
Simon Wilson79d39652011-05-25 13:44:23 -0700555 return ((value - ei->value.integer.min) / range) * 100;
556}
557
Taylor Holberton4e557192016-11-23 11:48:06 -0800558/** Gets a percentage representation of a specified control value.
559 * @param ctl An initialized control handle.
560 * @param id The index of the value within the control.
561 * @returns On success, the percentage representation of the control value.
562 * On failure, -EINVAL is returned.
563 * @ingroup libtinyalsa-mixer
564 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800565int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700566{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100567 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700568 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700569
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100570 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700571}
572
Taylor Holberton4e557192016-11-23 11:48:06 -0800573/** Sets the value of a control by percent, specified by the value index.
574 * @param ctl An initialized control handle.
575 * @param id The index of the value to set
576 * @param percent A percentage value between 0 and 100.
577 * @returns On success, zero is returned.
578 * On failure, non-zero is returned.
579 * @ingroup libtinyalsa-mixer
580 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700581int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700582{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100583 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700584 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700585
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100586 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700587}
588
Taylor Holberton4e557192016-11-23 11:48:06 -0800589/** Gets the value of a control.
590 * @param ctl An initialized control handle.
591 * @param id The index of the control value.
592 * @returns On success, the specified value is returned.
593 * On failure, -EINVAL is returned.
594 * @ingroup libtinyalsa-mixer
595 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800596int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700597{
Simon Wilson79d39652011-05-25 13:44:23 -0700598 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700599 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700600
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100601 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700602 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700603
Simon Wilson79d39652011-05-25 13:44:23 -0700604 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100605 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700606 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
607 if (ret < 0)
608 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700609
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100610 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700611 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700612 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700613
614 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700615 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700616
617 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
618 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700619
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500620 case SNDRV_CTL_ELEM_TYPE_BYTES:
621 return ev.value.bytes.data[id];
622
Simon Wilson79d39652011-05-25 13:44:23 -0700623 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700624 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700625 }
626
627 return 0;
628}
629
Taylor Holberton4e557192016-11-23 11:48:06 -0800630/** Gets the contents of a control's value array.
631 * @param ctl An initialized control handle.
632 * @param array A pointer to write the array data to.
633 * The size of this array must be equal to the number of items in the array
634 * multiplied by the size of each item.
635 * @param count The number of items in the array.
636 * This parameter must match the number of items in the control.
637 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
638 * @returns On success, zero.
639 * On failure, non-zero.
640 * @ingroup libtinyalsa-mixer
641 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800642int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100643{
644 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530645 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700646 size_t size;
647 void *source;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530648 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100649
Charles Keepaxe40758b2017-09-07 16:38:52 +0100650 if (!ctl || !count || !array)
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530651 return -EINVAL;
652
653 total_count = ctl->info.count;
654
655 if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
656 (mixer_ctl_is_access_tlv_rw(ctl))) {
657 /* Additional two words is for the TLV header */
658 total_count += TLV_HEADER_SIZE;
659 }
660
661 if (count > total_count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100662 return -EINVAL;
663
664 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100665 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100666
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100667 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700668 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
669 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530670 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
671 if (ret < 0)
672 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700673 size = sizeof(ev.value.integer.value[0]);
674 source = ev.value.integer.value;
675 break;
676
677 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530678 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530679 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +0530680 struct snd_ctl_tlv *tlv;
681 int ret;
682
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700683 if (count > SIZE_MAX - sizeof(*tlv))
684 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530685 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700686 if (!tlv)
687 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100688 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530689 tlv->length = count;
690 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
691
692 source = tlv->tlv;
693 memcpy(array, source, count);
694
695 free(tlv);
696
697 return ret;
698 } else {
699 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
700 if (ret < 0)
701 return ret;
702 size = sizeof(ev.value.bytes.data[0]);
703 source = ev.value.bytes.data;
704 break;
705 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700706
707 default:
708 return -EINVAL;
709 }
710
711 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100712
713 return 0;
714}
715
Taylor Holberton4e557192016-11-23 11:48:06 -0800716/** Sets the value of a control, specified by the value index.
717 * @param ctl An initialized control handle.
718 * @param id The index of the value within the control.
719 * @param value The value to set.
720 * This must be in a range specified by @ref mixer_ctl_get_range_min
721 * and @ref mixer_ctl_get_range_max.
722 * @returns On success, zero is returned.
723 * On failure, non-zero is returned.
724 * @ingroup libtinyalsa-mixer
725 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700726int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700727{
728 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700729 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700730
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100731 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700732 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700733
734 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100735 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700736 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
737 if (ret < 0)
738 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700739
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100740 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700741 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700742 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700743 break;
744
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700745 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200746 if ((value < mixer_ctl_get_range_min(ctl)) ||
747 (value > mixer_ctl_get_range_max(ctl))) {
748 return -EINVAL;
749 }
750
Simon Wilsond2cb5032011-06-04 00:57:17 -0700751 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700752 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700753
Simon Wilson066c9f62011-06-05 18:23:05 -0700754 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
755 ev.value.enumerated.item[id] = value;
756 break;
757
David.Coutherutce2b6342013-06-11 16:22:57 +0200758 case SNDRV_CTL_ELEM_TYPE_BYTES:
759 ev.value.bytes.data[id] = value;
760 break;
761
Simon Wilson79d39652011-05-25 13:44:23 -0700762 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700763 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700764 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700765
Simon Wilson79d39652011-05-25 13:44:23 -0700766 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
767}
768
Taylor Holberton4e557192016-11-23 11:48:06 -0800769/** Sets the contents of a control's value array.
770 * @param ctl An initialized control handle.
771 * @param array The array containing control values.
772 * @param count The number of values in the array.
773 * This must match the number of values in the control.
774 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
775 * @returns On success, zero.
776 * On failure, non-zero.
777 * @ingroup libtinyalsa-mixer
778 */
Simon Wilson38f87f32012-10-23 15:05:23 -0700779int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100780{
781 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700782 size_t size;
783 void *dest;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530784 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100785
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530786 if ((!ctl) || !count || !array)
787 return -EINVAL;
788
789 total_count = ctl->info.count;
790
791 if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
792 (mixer_ctl_is_access_tlv_rw(ctl))) {
793 /* Additional TLV header */
794 total_count += TLV_HEADER_SIZE;
795 }
796
797 if (count > total_count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100798 return -EINVAL;
799
800 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100801 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100802
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100803 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700804 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
805 case SNDRV_CTL_ELEM_TYPE_INTEGER:
806 size = sizeof(ev.value.integer.value[0]);
807 dest = ev.value.integer.value;
808 break;
809
810 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530811 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530812 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +0530813 struct snd_ctl_tlv *tlv;
814 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700815 if (count > SIZE_MAX - sizeof(*tlv))
816 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530817 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700818 if (!tlv)
819 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100820 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530821 tlv->length = count;
822 memcpy(tlv->tlv, array, count);
823
824 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
825 free(tlv);
826
827 return ret;
828 } else {
829 size = sizeof(ev.value.bytes.data[0]);
830 dest = ev.value.bytes.data;
831 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700832 break;
833
834 default:
835 return -EINVAL;
836 }
837
838 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100839
840 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
841}
842
Taylor Holberton4e557192016-11-23 11:48:06 -0800843/** Gets the minimum value of an control.
844 * The control must have an integer type.
845 * The type of the control can be checked with @ref mixer_ctl_get_type.
846 * @param ctl An initialized control handle.
847 * @returns On success, the minimum value of the control.
848 * On failure, -EINVAL.
849 * @ingroup libtinyalsa-mixer
850 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800851int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700852{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100853 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700854 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700855
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100856 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700857}
858
Taylor Holberton4e557192016-11-23 11:48:06 -0800859/** Gets the maximum value of an control.
860 * The control must have an integer type.
861 * The type of the control can be checked with @ref mixer_ctl_get_type.
862 * @param ctl An initialized control handle.
863 * @returns On success, the maximum value of the control.
864 * On failure, -EINVAL.
865 * @ingroup libtinyalsa-mixer
866 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800867int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700868{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100869 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700870 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700871
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100872 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700873}
874
Taylor Holberton4e557192016-11-23 11:48:06 -0800875/** Get the number of enumerated items in the control.
876 * @param ctl An initialized control handle.
877 * @returns The number of enumerated items in the control.
878 * @ingroup libtinyalsa-mixer
879 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800880unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700881{
Simon Wilson193b1c32011-06-07 23:42:38 -0700882 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700883 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700884
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100885 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700886}
887
David.Coutherut9b423962013-06-03 13:45:51 +0200888int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
889{
890 struct snd_ctl_elem_info tmp;
891 unsigned int m;
892 char **enames;
893
894 if (ctl->ename) {
895 return 0;
896 }
897
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400898 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +0200899 if (!enames)
900 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400901 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200902 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400903 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +0200904 tmp.value.enumerated.item = m;
905 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
906 goto fail;
907 enames[m] = strdup(tmp.value.enumerated.name);
908 if (!enames[m])
909 goto fail;
910 }
911 ctl->ename = enames;
912 return 0;
913
914fail:
915 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400916 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200917 if (enames[m]) {
918 free(enames[m]);
919 }
920 }
921 free(enames);
922 }
923 return -1;
924}
925
Taylor Holberton4e557192016-11-23 11:48:06 -0800926/** Gets the string representation of an enumerated item.
927 * @param ctl An initialized control handle.
928 * @param enum_id The index of the enumerated value.
929 * @returns A string representation of the enumerated item.
930 * @ingroup libtinyalsa-mixer
931 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800932const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
933 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700934{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100935 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400936 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200937 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800938 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700939
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800940 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700941}
942
Taylor Holberton4e557192016-11-23 11:48:06 -0800943/** Set an enumeration value by string value.
944 * @param ctl An enumerated mixer control.
945 * @param string The string representation of an enumeration.
946 * @returns On success, zero.
947 * On failure, zero.
948 * @ingroup libtinyalsa-mixer
949 */
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700950int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
951{
952 unsigned int i, num_enums;
953 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700954 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700955
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400956 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200957 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -0700958 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700959
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100960 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700961 for (i = 0; i < num_enums; i++) {
962 if (!strcmp(string, ctl->ename[i])) {
963 memset(&ev, 0, sizeof(ev));
964 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100965 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700966 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
967 if (ret < 0)
968 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700969 return 0;
970 }
971 }
972
Simon Wilson193b1c32011-06-07 23:42:38 -0700973 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700974}
975