blob: a9cc483c2c766d223e852b064bc37bf6ffb1671a [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>
44#define __force
45#define __bitwise
46#define __user
47#include <sound/asound.h>
48
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030049#include <tinyalsa/mixer.h>
Simon Wilson79d39652011-05-25 13:44:23 -070050
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040051/** A mixer control.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080052 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040053 */
Simon Wilson79d39652011-05-25 13:44:23 -070054struct mixer_ctl {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040055 /** The mixer that the mixer control belongs to */
Simon Wilson79d39652011-05-25 13:44:23 -070056 struct mixer *mixer;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080057 /** Information on the control's value (i.e. type, number of values) */
Richard Fitzgerald899cece2014-09-09 17:03:21 +010058 struct snd_ctl_elem_info info;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080059 /** A list of string representations of enumerated values (only valid for enumerated controls) */
Simon Wilson79d39652011-05-25 13:44:23 -070060 char **ename;
61};
62
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040063/** A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080064 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040065 */
Simon Wilson79d39652011-05-25 13:44:23 -070066struct mixer {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040067 /** File descriptor for the card */
Simon Wilson79d39652011-05-25 13:44:23 -070068 int fd;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040069 /** Card information */
Simon Wilsonec281392013-06-28 16:21:31 -070070 struct snd_ctl_card_info card_info;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040071 /** A continuous array of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070072 struct mixer_ctl *ctl;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040073 /** The number of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070074 unsigned int count;
75};
76
Richard Fitzgeraldfd329032014-09-09 17:14:09 +010077static void mixer_cleanup_control(struct mixer_ctl *ctl)
78{
79 unsigned int m;
80
81 if (ctl->ename) {
82 unsigned int max = ctl->info.value.enumerated.items;
83 for (m = 0; m < max; m++)
84 free(ctl->ename[m]);
85 free(ctl->ename);
86 }
87}
88
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040089/** Closes a mixer returned by @ref mixer_open.
90 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080091 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040092 */
Simon Wilson79d39652011-05-25 13:44:23 -070093void mixer_close(struct mixer *mixer)
94{
Richard Fitzgeraldfd329032014-09-09 17:14:09 +010095 unsigned int n;
Simon Wilson79d39652011-05-25 13:44:23 -070096
Simon Wilson193b1c32011-06-07 23:42:38 -070097 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -070098 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -070099
Simon Wilson79d39652011-05-25 13:44:23 -0700100 if (mixer->fd >= 0)
101 close(mixer->fd);
102
103 if (mixer->ctl) {
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100104 for (n = 0; n < mixer->count; n++)
105 mixer_cleanup_control(&mixer->ctl[n]);
Simon Wilson79d39652011-05-25 13:44:23 -0700106 free(mixer->ctl);
107 }
108
Simon Wilson79d39652011-05-25 13:44:23 -0700109 free(mixer);
110
111 /* TODO: verify frees */
112}
113
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100114static void *mixer_realloc_z(void *ptr, size_t curnum, size_t newnum, size_t size)
115{
116 int8_t *newp;
117
118 newp = realloc(ptr, size * newnum);
119 if (!newp)
120 return NULL;
121
122 memset(newp + (curnum * size), 0, (newnum - curnum) * size);
123 return newp;
124}
125
126static int add_controls(struct mixer *mixer)
127{
128 struct snd_ctl_elem_list elist;
129 struct snd_ctl_elem_id *eid = NULL;
130 struct mixer_ctl *ctl;
131 int fd = mixer->fd;
132 const unsigned int old_count = mixer->count;
133 unsigned int new_count;
134 unsigned int n;
135
136 memset(&elist, 0, sizeof(elist));
137 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
138 goto fail;
139
140 if (old_count == elist.count)
141 return 0; /* no new controls return unchanged */
142
143 if (old_count > elist.count)
144 return -1; /* driver has removed controls - this is bad */
145
146 ctl = mixer_realloc_z(mixer->ctl, old_count, elist.count,
147 sizeof(struct mixer_ctl));
148 if (!ctl)
149 goto fail;
150
151 mixer->ctl = ctl;
152
153 /* ALSA drivers are not supposed to remove or re-order controls that
154 * have already been created so we know that any new controls must
155 * be after the ones we have already collected
156 */
157 new_count = elist.count;
158 elist.space = new_count - old_count; /* controls we haven't seen before */
159 elist.offset = old_count; /* first control we haven't seen */
160
161 eid = calloc(elist.space, sizeof(struct snd_ctl_elem_id));
162 if (!eid)
163 goto fail;
164
165 elist.pids = eid;
166
167 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
168 goto fail;
169
170 for (n = old_count; n < new_count; n++) {
171 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
172 ei->id.numid = eid[n - old_count].numid;
173 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
174 goto fail_extend;
175 ctl[n].mixer = mixer;
176 }
177
178 mixer->count = new_count;
179 free(eid);
180 return 0;
181
182fail_extend:
183 /* cleanup the control we failed on but leave the ones that were already
184 * added. Also no advantage to shrinking the resized memory block, we
185 * might want to extend the controls again later
186 */
187 mixer_cleanup_control(&ctl[n]);
188
189 mixer->count = n; /* keep controls we successfully added */
190 /* fall through... */
191fail:
192 free(eid);
193 return -1;
194}
195
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400196/** Opens a mixer for a given card.
197 * @param card The card to open the mixer for.
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500198 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800199 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400200 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700201struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700202{
Simon Wilson79d39652011-05-25 13:44:23 -0700203 struct mixer *mixer = NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700204 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700205 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -0700206
Simon Wilson1bd580f2011-06-02 15:58:41 -0700207 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
208 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700209 if (fd < 0)
210 return 0;
211
Simon Wilson79d39652011-05-25 13:44:23 -0700212 mixer = calloc(1, sizeof(*mixer));
213 if (!mixer)
214 goto fail;
215
Simon Wilsonec281392013-06-28 16:21:31 -0700216 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700217 goto fail;
218
Simon Wilson79d39652011-05-25 13:44:23 -0700219 mixer->fd = fd;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100220
221 if (add_controls(mixer) != 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700222 goto fail;
223
Simon Wilson79d39652011-05-25 13:44:23 -0700224 return mixer;
225
226fail:
Simon Wilson79d39652011-05-25 13:44:23 -0700227 if (mixer)
228 mixer_close(mixer);
229 else if (fd >= 0)
230 close(fd);
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100231 return NULL;
232}
233
234/** Some controls may not be present at boot time, e.g. controls from runtime
235 * loadable DSP firmware. This function adds any new controls that have appeared
236 * since mixer_open() or the last call to this function. This assumes a well-
237 * behaved codec driver that does not delete controls that already exists, so
238 * any added controls must be after the last one we already saw. Scanning only
239 * the new controls is much faster than calling mixer_close() then mixer_open()
240 * to re-scan all controls.
241 *
242 * NOTE: this invalidates any struct mixer_ctl pointers previously obtained
243 * from mixer_get_ctl() and mixer_get_ctl_by_name(). Either refresh all your
244 * stored pointers after calling mixer_update_ctls(), or (better) do not
245 * store struct mixer_ctl pointers, instead lookup the control by name or
246 * id only when you are about to use it. The overhead of lookup by id
247 * using mixer_get_ctl() is negligible.
248 * @param mixer An initialized mixer handle.
249 * @returns 0 on success, -1 on failure
250 */
251int mixer_add_new_ctls(struct mixer *mixer)
252{
253 if (!mixer)
254 return 0;
255
256 return add_controls(mixer);
Simon Wilson79d39652011-05-25 13:44:23 -0700257}
258
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500259/** Gets the name of the mixer's card.
260 * @param mixer An initialized mixer handle.
261 * @returns The name of the mixer's card.
262 * @ingroup libtinyalsa-mixer
263 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800264const char *mixer_get_name(const struct mixer *mixer)
Simon Wilsonec281392013-06-28 16:21:31 -0700265{
266 return (const char *)mixer->card_info.name;
267}
268
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500269/** Gets the number of mixer controls for a given mixer.
270 * @param mixer An initialized mixer handle.
271 * @returns The number of mixer controls for the given mixer.
272 * @ingroup libtinyalsa-mixer
273 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800274unsigned int mixer_get_num_ctls(const struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700275{
Simon Wilson193b1c32011-06-07 23:42:38 -0700276 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700277 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700278
Simon Wilson79d39652011-05-25 13:44:23 -0700279 return mixer->count;
280}
281
Taylor Holberton94c7c832016-12-01 18:35:24 -0800282/** Gets the number of mixer controls, that go by a specified name, for a given mixer.
283 * @param mixer An initialized mixer handle.
284 * @param name The name of the mixer control
285 * @returns The number of mixer controls, specified by @p name, for the given mixer.
286 * @ingroup libtinyalsa-mixer
287 */
288unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name)
289{
290 unsigned int n;
291 unsigned int count = 0;
292 struct mixer_ctl *ctl;
293
294 if (!mixer)
295 return 0;
296
297 ctl = mixer->ctl;
298
299 for (n = 0; n < mixer->count; n++)
300 if (!strcmp(name, (char*) ctl[n].info.id.name))
301 count++;
302
303 return count;
304}
305
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530306/** Subscribes for the mixer events.
307 * @param mixer A mixer handle.
308 * @param subscribe value indicating subscribe or unsubscribe for events
309 * @returns On success, zero.
310 * On failure, non-zero.
311 * @ingroup libtinyalsa-mixer
312 */
313int mixer_subscribe_events(struct mixer *mixer, int subscribe)
314{
315 if (ioctl(mixer->fd, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) {
316 return -1;
317 }
318 return 0;
319}
320
321/** Wait for mixer events.
322 * @param mixer A mixer handle.
323 * @param timeout timout value
Pankaj Bharadiya95c79d82017-01-11 11:28:02 +0530324 * @returns On success, 1.
325 * On failure, -errno.
326 * On timeout, 0
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530327 * @ingroup libtinyalsa-mixer
328 */
329int mixer_wait_event(struct mixer *mixer, int timeout)
330{
331 struct pollfd pfd;
332
333 pfd.fd = mixer->fd;
334 pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
335
336 for (;;) {
337 int err;
338 err = poll(&pfd, 1, timeout);
339 if (err < 0)
340 return -errno;
341 if (!err)
342 return 0;
343 if (pfd.revents & (POLLERR | POLLNVAL))
344 return -EIO;
345 if (pfd.revents & (POLLIN | POLLOUT))
346 return 1;
347 }
348}
349
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500350/** Gets a mixer control handle, by the mixer control's id.
Taylor Holbertona94295b2016-12-01 18:23:16 -0800351 * For non-const access, see @ref mixer_get_ctl
352 * @param mixer An initialized mixer handle.
353 * @param id The control's id in the given mixer.
354 * @returns A handle to the mixer control.
355 * @ingroup libtinyalsa-mixer
356 */
357const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
358{
359 if (mixer && (id < mixer->count))
360 return mixer->ctl + id;
361
362 return NULL;
363}
364
365/** Gets a mixer control handle, by the mixer control's id.
366 * For const access, see @ref mixer_get_ctl_const
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500367 * @param mixer An initialized mixer handle.
368 * @param id The control's id in the given mixer.
369 * @returns A handle to the mixer control.
370 * @ingroup libtinyalsa-mixer
371 */
Simon Wilson79d39652011-05-25 13:44:23 -0700372struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
373{
Simon Wilson98c1f162011-06-07 16:12:32 -0700374 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700375 return mixer->ctl + id;
376
377 return NULL;
378}
379
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500380/** Gets the first instance of mixer control handle, by the mixer control's name.
381 * @param mixer An initialized mixer handle.
382 * @param name The control's name in the given mixer.
383 * @returns A handle to the mixer control.
384 * @ingroup libtinyalsa-mixer
385 */
Simon Wilson79d39652011-05-25 13:44:23 -0700386struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
387{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200388 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
389}
390
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500391/** Gets an instance of mixer control handle, by the mixer control's name and index.
392 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
393 * @param mixer An initialized mixer handle.
394 * @param name The control's name in the given mixer.
395 * @param index The control's index.
396 * @returns A handle to the mixer control.
397 * @ingroup libtinyalsa-mixer
398 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200399struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
400 const char *name,
401 unsigned int index)
402{
Simon Wilson79d39652011-05-25 13:44:23 -0700403 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200404 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700405
Simon Wilson98c1f162011-06-07 16:12:32 -0700406 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700407 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700408
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200409 ctl = mixer->ctl;
410
Simon Wilson79d39652011-05-25 13:44:23 -0700411 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100412 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200413 if (index-- == 0)
414 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700415
416 return NULL;
417}
418
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500419/** Updates the control's info.
420 * This is useful for a program that may be idle for a period of time.
421 * @param ctl An initialized control handle.
422 * @ingroup libtinyalsa-mixer
423 */
Simon Wilson710df882013-06-28 16:17:50 -0700424void mixer_ctl_update(struct mixer_ctl *ctl)
425{
426 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
427}
428
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530429/** Checks the control for TLV Read/Write access.
430 * @param ctl An initialized control handle.
431 * @returns On success, non-zero.
432 * On failure, zero.
433 * @ingroup libtinyalsa-mixer
434 */
435int mixer_ctl_is_access_tlv_rw(const struct mixer_ctl *ctl)
436{
437 return (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
438}
439
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500440/** Gets the control's ID.
441 * @param ctl An initialized control handle.
442 * @returns On success, the control's ID is returned.
443 * On error, UINT_MAX is returned instead.
444 * @ingroup libtinyalsa-mixer
445 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800446unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100447{
448 if (!ctl)
449 return UINT_MAX;
450
451 /* numid values start at 1, return a 0-base value that
452 * can be passed to mixer_get_ctl()
453 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100454 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100455}
456
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500457/** Gets the name of the control.
458 * @param ctl An initialized control handle.
459 * @returns On success, the name of the control.
460 * On error, NULL.
461 * @ingroup libtinyalsa-mixer
462 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800463const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700464{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800465 if (!ctl)
466 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700467
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100468 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700469}
470
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500471/** Gets the value type of the control.
472 * @param ctl An initialized control handle
473 * @returns On success, the type of mixer control.
474 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
475 * @ingroup libtinyalsa-mixer
476 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800477enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700478{
Simon Wilson193b1c32011-06-07 23:42:38 -0700479 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700480 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700481
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100482 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700483 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
484 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
485 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
486 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
487 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
488 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
489 default: return MIXER_CTL_TYPE_UNKNOWN;
490 };
491}
492
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500493/** Gets the string that describes the value type of the control.
494 * @param ctl An initialized control handle
495 * @returns On success, a string describing type of mixer control.
496 * @ingroup libtinyalsa-mixer
497 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800498const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700499{
Simon Wilson193b1c32011-06-07 23:42:38 -0700500 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700501 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700502
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100503 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700504 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
505 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
506 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
507 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
508 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
509 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
510 default: return "Unknown";
511 };
512}
513
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500514/** Gets the number of values in the control.
515 * @param ctl An initialized control handle
516 * @returns The number of values in the mixer control
517 * @ingroup libtinyalsa-mixer
518 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800519unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700520{
Simon Wilson193b1c32011-06-07 23:42:38 -0700521 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700522 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700523
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100524 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700525}
526
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800527static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilson79d39652011-05-25 13:44:23 -0700528{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200529 if ((percent > 100) || (percent < 0)) {
530 return -EINVAL;
531 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700532
Baptiste Robert4a484e12013-09-12 15:37:53 +0200533 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700534
Simon Wilson79d39652011-05-25 13:44:23 -0700535 return ei->value.integer.min + (range * percent) / 100;
536}
537
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800538static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700539{
540 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700541
Simon Wilson79d39652011-05-25 13:44:23 -0700542 if (range == 0)
543 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700544
Simon Wilson79d39652011-05-25 13:44:23 -0700545 return ((value - ei->value.integer.min) / range) * 100;
546}
547
Taylor Holberton4e557192016-11-23 11:48:06 -0800548/** Gets a percentage representation of a specified control value.
549 * @param ctl An initialized control handle.
550 * @param id The index of the value within the control.
551 * @returns On success, the percentage representation of the control value.
552 * On failure, -EINVAL is returned.
553 * @ingroup libtinyalsa-mixer
554 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800555int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700556{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100557 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700558 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700559
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100560 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700561}
562
Taylor Holberton4e557192016-11-23 11:48:06 -0800563/** Sets the value of a control by percent, specified by the value index.
564 * @param ctl An initialized control handle.
565 * @param id The index of the value to set
566 * @param percent A percentage value between 0 and 100.
567 * @returns On success, zero is returned.
568 * On failure, non-zero is returned.
569 * @ingroup libtinyalsa-mixer
570 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700571int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700572{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100573 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700574 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700575
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100576 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700577}
578
Taylor Holberton4e557192016-11-23 11:48:06 -0800579/** Gets the value of a control.
580 * @param ctl An initialized control handle.
581 * @param id The index of the control value.
582 * @returns On success, the specified value is returned.
583 * On failure, -EINVAL is returned.
584 * @ingroup libtinyalsa-mixer
585 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800586int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700587{
Simon Wilson79d39652011-05-25 13:44:23 -0700588 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700589 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700590
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100591 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700592 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700593
Simon Wilson79d39652011-05-25 13:44:23 -0700594 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100595 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700596 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
597 if (ret < 0)
598 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700599
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100600 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700601 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700602 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700603
604 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700605 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700606
607 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
608 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700609
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500610 case SNDRV_CTL_ELEM_TYPE_BYTES:
611 return ev.value.bytes.data[id];
612
Simon Wilson79d39652011-05-25 13:44:23 -0700613 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700614 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700615 }
616
617 return 0;
618}
619
Taylor Holberton4e557192016-11-23 11:48:06 -0800620/** Gets the contents of a control's value array.
621 * @param ctl An initialized control handle.
622 * @param array A pointer to write the array data to.
623 * The size of this array must be equal to the number of items in the array
624 * multiplied by the size of each item.
625 * @param count The number of items in the array.
626 * This parameter must match the number of items in the control.
627 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
628 * @returns On success, zero.
629 * On failure, non-zero.
630 * @ingroup libtinyalsa-mixer
631 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800632int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100633{
634 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530635 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700636 size_t size;
637 void *source;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530638 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100639
Charles Keepaxe40758b2017-09-07 16:38:52 +0100640 if (!ctl || !count || !array)
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530641 return -EINVAL;
642
643 total_count = ctl->info.count;
644
645 if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
646 (mixer_ctl_is_access_tlv_rw(ctl))) {
647 /* Additional two words is for the TLV header */
648 total_count += TLV_HEADER_SIZE;
649 }
650
651 if (count > total_count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100652 return -EINVAL;
653
654 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100655 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100656
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100657 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700658 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
659 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530660 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
661 if (ret < 0)
662 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700663 size = sizeof(ev.value.integer.value[0]);
664 source = ev.value.integer.value;
665 break;
666
667 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530668 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530669 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +0530670 struct snd_ctl_tlv *tlv;
671 int ret;
672
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700673 if (count > SIZE_MAX - sizeof(*tlv))
674 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530675 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700676 if (!tlv)
677 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100678 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530679 tlv->length = count;
680 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
681
682 source = tlv->tlv;
683 memcpy(array, source, count);
684
685 free(tlv);
686
687 return ret;
688 } else {
689 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
690 if (ret < 0)
691 return ret;
692 size = sizeof(ev.value.bytes.data[0]);
693 source = ev.value.bytes.data;
694 break;
695 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700696
697 default:
698 return -EINVAL;
699 }
700
701 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100702
703 return 0;
704}
705
Taylor Holberton4e557192016-11-23 11:48:06 -0800706/** Sets the value of a control, specified by the value index.
707 * @param ctl An initialized control handle.
708 * @param id The index of the value within the control.
709 * @param value The value to set.
710 * This must be in a range specified by @ref mixer_ctl_get_range_min
711 * and @ref mixer_ctl_get_range_max.
712 * @returns On success, zero is returned.
713 * On failure, non-zero is returned.
714 * @ingroup libtinyalsa-mixer
715 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700716int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700717{
718 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700719 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700720
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100721 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700722 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700723
724 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100725 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700726 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
727 if (ret < 0)
728 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700729
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100730 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700731 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700732 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700733 break;
734
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700735 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200736 if ((value < mixer_ctl_get_range_min(ctl)) ||
737 (value > mixer_ctl_get_range_max(ctl))) {
738 return -EINVAL;
739 }
740
Simon Wilsond2cb5032011-06-04 00:57:17 -0700741 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700742 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700743
Simon Wilson066c9f62011-06-05 18:23:05 -0700744 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
745 ev.value.enumerated.item[id] = value;
746 break;
747
David.Coutherutce2b6342013-06-11 16:22:57 +0200748 case SNDRV_CTL_ELEM_TYPE_BYTES:
749 ev.value.bytes.data[id] = value;
750 break;
751
Simon Wilson79d39652011-05-25 13:44:23 -0700752 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700753 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700754 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700755
Simon Wilson79d39652011-05-25 13:44:23 -0700756 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
757}
758
Taylor Holberton4e557192016-11-23 11:48:06 -0800759/** Sets the contents of a control's value array.
760 * @param ctl An initialized control handle.
761 * @param array The array containing control values.
762 * @param count The number of values in the array.
763 * This must match the number of values in the control.
764 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
765 * @returns On success, zero.
766 * On failure, non-zero.
767 * @ingroup libtinyalsa-mixer
768 */
Simon Wilson38f87f32012-10-23 15:05:23 -0700769int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100770{
771 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700772 size_t size;
773 void *dest;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530774 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100775
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530776 if ((!ctl) || !count || !array)
777 return -EINVAL;
778
779 total_count = ctl->info.count;
780
781 if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
782 (mixer_ctl_is_access_tlv_rw(ctl))) {
783 /* Additional TLV header */
784 total_count += TLV_HEADER_SIZE;
785 }
786
787 if (count > total_count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100788 return -EINVAL;
789
790 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100791 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100792
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100793 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700794 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
795 case SNDRV_CTL_ELEM_TYPE_INTEGER:
796 size = sizeof(ev.value.integer.value[0]);
797 dest = ev.value.integer.value;
798 break;
799
800 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530801 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530802 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +0530803 struct snd_ctl_tlv *tlv;
804 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700805 if (count > SIZE_MAX - sizeof(*tlv))
806 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530807 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700808 if (!tlv)
809 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100810 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530811 tlv->length = count;
812 memcpy(tlv->tlv, array, count);
813
814 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
815 free(tlv);
816
817 return ret;
818 } else {
819 size = sizeof(ev.value.bytes.data[0]);
820 dest = ev.value.bytes.data;
821 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700822 break;
823
824 default:
825 return -EINVAL;
826 }
827
828 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100829
830 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
831}
832
Taylor Holberton4e557192016-11-23 11:48:06 -0800833/** Gets the minimum value of an control.
834 * The control must have an integer type.
835 * The type of the control can be checked with @ref mixer_ctl_get_type.
836 * @param ctl An initialized control handle.
837 * @returns On success, the minimum value of the control.
838 * On failure, -EINVAL.
839 * @ingroup libtinyalsa-mixer
840 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800841int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700842{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100843 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700844 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700845
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100846 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700847}
848
Taylor Holberton4e557192016-11-23 11:48:06 -0800849/** Gets the maximum value of an control.
850 * The control must have an integer type.
851 * The type of the control can be checked with @ref mixer_ctl_get_type.
852 * @param ctl An initialized control handle.
853 * @returns On success, the maximum value of the control.
854 * On failure, -EINVAL.
855 * @ingroup libtinyalsa-mixer
856 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800857int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700858{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100859 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700860 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700861
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100862 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700863}
864
Taylor Holberton4e557192016-11-23 11:48:06 -0800865/** Get the number of enumerated items in the control.
866 * @param ctl An initialized control handle.
867 * @returns The number of enumerated items in the control.
868 * @ingroup libtinyalsa-mixer
869 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800870unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700871{
Simon Wilson193b1c32011-06-07 23:42:38 -0700872 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700873 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700874
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100875 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700876}
877
David.Coutherut9b423962013-06-03 13:45:51 +0200878int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
879{
880 struct snd_ctl_elem_info tmp;
881 unsigned int m;
882 char **enames;
883
884 if (ctl->ename) {
885 return 0;
886 }
887
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400888 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +0200889 if (!enames)
890 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400891 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200892 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400893 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +0200894 tmp.value.enumerated.item = m;
895 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
896 goto fail;
897 enames[m] = strdup(tmp.value.enumerated.name);
898 if (!enames[m])
899 goto fail;
900 }
901 ctl->ename = enames;
902 return 0;
903
904fail:
905 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400906 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200907 if (enames[m]) {
908 free(enames[m]);
909 }
910 }
911 free(enames);
912 }
913 return -1;
914}
915
Taylor Holberton4e557192016-11-23 11:48:06 -0800916/** Gets the string representation of an enumerated item.
917 * @param ctl An initialized control handle.
918 * @param enum_id The index of the enumerated value.
919 * @returns A string representation of the enumerated item.
920 * @ingroup libtinyalsa-mixer
921 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800922const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
923 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700924{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100925 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400926 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200927 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800928 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700929
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800930 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700931}
932
Taylor Holberton4e557192016-11-23 11:48:06 -0800933/** Set an enumeration value by string value.
934 * @param ctl An enumerated mixer control.
935 * @param string The string representation of an enumeration.
936 * @returns On success, zero.
937 * On failure, zero.
938 * @ingroup libtinyalsa-mixer
939 */
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700940int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
941{
942 unsigned int i, num_enums;
943 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700944 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700945
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400946 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200947 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -0700948 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700949
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100950 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700951 for (i = 0; i < num_enums; i++) {
952 if (!strcmp(string, ctl->ename[i])) {
953 memset(&ev, 0, sizeof(ev));
954 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100955 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700956 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
957 if (ret < 0)
958 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700959 return 0;
960 }
961 }
962
Simon Wilson193b1c32011-06-07 23:42:38 -0700963 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700964}
965