blob: 5fed4a8f8abf42db71f4f12114fc6663c3da4e07 [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
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040076/** Closes a mixer returned by @ref mixer_open.
77 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080078 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040079 */
Simon Wilson79d39652011-05-25 13:44:23 -070080void mixer_close(struct mixer *mixer)
81{
82 unsigned int n,m;
83
Simon Wilson193b1c32011-06-07 23:42:38 -070084 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -070085 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -070086
Simon Wilson79d39652011-05-25 13:44:23 -070087 if (mixer->fd >= 0)
88 close(mixer->fd);
89
90 if (mixer->ctl) {
91 for (n = 0; n < mixer->count; n++) {
92 if (mixer->ctl[n].ename) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +010093 unsigned int max = mixer->ctl[n].info.value.enumerated.items;
Simon Wilson79d39652011-05-25 13:44:23 -070094 for (m = 0; m < max; m++)
95 free(mixer->ctl[n].ename[m]);
96 free(mixer->ctl[n].ename);
97 }
98 }
99 free(mixer->ctl);
100 }
101
Simon Wilson79d39652011-05-25 13:44:23 -0700102 free(mixer);
103
104 /* TODO: verify frees */
105}
106
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400107/** Opens a mixer for a given card.
108 * @param card The card to open the mixer for.
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500109 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800110 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400111 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700112struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700113{
114 struct snd_ctl_elem_list elist;
Simon Wilson79d39652011-05-25 13:44:23 -0700115 struct snd_ctl_elem_id *eid = NULL;
116 struct mixer *mixer = NULL;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400117 unsigned int n;
Simon Wilson79d39652011-05-25 13:44:23 -0700118 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700119 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -0700120
Simon Wilson1bd580f2011-06-02 15:58:41 -0700121 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
122 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700123 if (fd < 0)
124 return 0;
125
126 memset(&elist, 0, sizeof(elist));
127 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
128 goto fail;
129
130 mixer = calloc(1, sizeof(*mixer));
131 if (!mixer)
132 goto fail;
133
134 mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100135 if (!mixer->ctl)
Simon Wilsonec281392013-06-28 16:21:31 -0700136 goto fail;
137
138 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700139 goto fail;
140
141 eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
142 if (!eid)
143 goto fail;
144
145 mixer->count = elist.count;
146 mixer->fd = fd;
147 elist.space = mixer->count;
148 elist.pids = eid;
149 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
150 goto fail;
151
152 for (n = 0; n < mixer->count; n++) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100153 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
Simon Wilson79d39652011-05-25 13:44:23 -0700154 ei->id.numid = eid[n].numid;
155 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
156 goto fail;
Simon Wilson79d39652011-05-25 13:44:23 -0700157 mixer->ctl[n].mixer = mixer;
Simon Wilson79d39652011-05-25 13:44:23 -0700158 }
159
160 free(eid);
161 return mixer;
162
163fail:
164 /* TODO: verify frees in failure case */
165 if (eid)
166 free(eid);
167 if (mixer)
168 mixer_close(mixer);
169 else if (fd >= 0)
170 close(fd);
171 return 0;
172}
173
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500174/** Gets the name of the mixer's card.
175 * @param mixer An initialized mixer handle.
176 * @returns The name of the mixer's card.
177 * @ingroup libtinyalsa-mixer
178 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800179const char *mixer_get_name(const struct mixer *mixer)
Simon Wilsonec281392013-06-28 16:21:31 -0700180{
181 return (const char *)mixer->card_info.name;
182}
183
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500184/** Gets the number of mixer controls for a given mixer.
185 * @param mixer An initialized mixer handle.
186 * @returns The number of mixer controls for the given mixer.
187 * @ingroup libtinyalsa-mixer
188 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800189unsigned int mixer_get_num_ctls(const struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700190{
Simon Wilson193b1c32011-06-07 23:42:38 -0700191 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700192 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700193
Simon Wilson79d39652011-05-25 13:44:23 -0700194 return mixer->count;
195}
196
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500197/** Gets a mixer control handle, by the mixer control's id.
198 * @param mixer An initialized mixer handle.
199 * @param id The control's id in the given mixer.
200 * @returns A handle to the mixer control.
201 * @ingroup libtinyalsa-mixer
202 */
Simon Wilson79d39652011-05-25 13:44:23 -0700203struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
204{
Simon Wilson98c1f162011-06-07 16:12:32 -0700205 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700206 return mixer->ctl + id;
207
208 return NULL;
209}
210
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500211/** Gets the first instance of mixer control handle, by the mixer control's name.
212 * @param mixer An initialized mixer handle.
213 * @param name The control's name in the given mixer.
214 * @returns A handle to the mixer control.
215 * @ingroup libtinyalsa-mixer
216 */
Simon Wilson79d39652011-05-25 13:44:23 -0700217struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
218{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200219 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
220}
221
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500222/** Gets an instance of mixer control handle, by the mixer control's name and index.
223 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
224 * @param mixer An initialized mixer handle.
225 * @param name The control's name in the given mixer.
226 * @param index The control's index.
227 * @returns A handle to the mixer control.
228 * @ingroup libtinyalsa-mixer
229 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200230struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
231 const char *name,
232 unsigned int index)
233{
Simon Wilson79d39652011-05-25 13:44:23 -0700234 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200235 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700236
Simon Wilson98c1f162011-06-07 16:12:32 -0700237 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700238 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700239
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200240 ctl = mixer->ctl;
241
Simon Wilson79d39652011-05-25 13:44:23 -0700242 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100243 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200244 if (index-- == 0)
245 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700246
247 return NULL;
248}
249
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500250/** Updates the control's info.
251 * This is useful for a program that may be idle for a period of time.
252 * @param ctl An initialized control handle.
253 * @ingroup libtinyalsa-mixer
254 */
Simon Wilson710df882013-06-28 16:17:50 -0700255void mixer_ctl_update(struct mixer_ctl *ctl)
256{
257 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
258}
259
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500260/** Gets the control's ID.
261 * @param ctl An initialized control handle.
262 * @returns On success, the control's ID is returned.
263 * On error, UINT_MAX is returned instead.
264 * @ingroup libtinyalsa-mixer
265 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800266unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100267{
268 if (!ctl)
269 return UINT_MAX;
270
271 /* numid values start at 1, return a 0-base value that
272 * can be passed to mixer_get_ctl()
273 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100274 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100275}
276
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500277/** Gets the name of the control.
278 * @param ctl An initialized control handle.
279 * @returns On success, the name of the control.
280 * On error, NULL.
281 * @ingroup libtinyalsa-mixer
282 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800283const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700284{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800285 if (!ctl)
286 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700287
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100288 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700289}
290
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500291/** Gets the value type of the control.
292 * @param ctl An initialized control handle
293 * @returns On success, the type of mixer control.
294 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
295 * @ingroup libtinyalsa-mixer
296 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800297enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700298{
Simon Wilson193b1c32011-06-07 23:42:38 -0700299 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700300 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700301
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100302 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700303 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
304 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
305 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
306 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
307 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
308 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
309 default: return MIXER_CTL_TYPE_UNKNOWN;
310 };
311}
312
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500313/** Gets the string that describes the value type of the control.
314 * @param ctl An initialized control handle
315 * @returns On success, a string describing type of mixer control.
316 * @ingroup libtinyalsa-mixer
317 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800318const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700319{
Simon Wilson193b1c32011-06-07 23:42:38 -0700320 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700321 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700322
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100323 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700324 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
325 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
326 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
327 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
328 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
329 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
330 default: return "Unknown";
331 };
332}
333
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500334/** Gets the number of values in the control.
335 * @param ctl An initialized control handle
336 * @returns The number of values in the mixer control
337 * @ingroup libtinyalsa-mixer
338 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800339unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700340{
Simon Wilson193b1c32011-06-07 23:42:38 -0700341 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700342 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700343
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100344 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700345}
346
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800347static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilson79d39652011-05-25 13:44:23 -0700348{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200349 if ((percent > 100) || (percent < 0)) {
350 return -EINVAL;
351 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700352
Baptiste Robert4a484e12013-09-12 15:37:53 +0200353 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700354
Simon Wilson79d39652011-05-25 13:44:23 -0700355 return ei->value.integer.min + (range * percent) / 100;
356}
357
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800358static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700359{
360 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700361
Simon Wilson79d39652011-05-25 13:44:23 -0700362 if (range == 0)
363 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700364
Simon Wilson79d39652011-05-25 13:44:23 -0700365 return ((value - ei->value.integer.min) / range) * 100;
366}
367
Taylor Holberton4e557192016-11-23 11:48:06 -0800368/** Gets a percentage representation of a specified control value.
369 * @param ctl An initialized control handle.
370 * @param id The index of the value within the control.
371 * @returns On success, the percentage representation of the control value.
372 * On failure, -EINVAL is returned.
373 * @ingroup libtinyalsa-mixer
374 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800375int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700376{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100377 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700378 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700379
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100380 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700381}
382
Taylor Holberton4e557192016-11-23 11:48:06 -0800383/** Sets the value of a control by percent, specified by the value index.
384 * @param ctl An initialized control handle.
385 * @param id The index of the value to set
386 * @param percent A percentage value between 0 and 100.
387 * @returns On success, zero is returned.
388 * On failure, non-zero is returned.
389 * @ingroup libtinyalsa-mixer
390 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700391int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700392{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100393 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700394 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700395
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100396 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700397}
398
Taylor Holberton4e557192016-11-23 11:48:06 -0800399/** Gets the value of a control.
400 * @param ctl An initialized control handle.
401 * @param id The index of the control value.
402 * @returns On success, the specified value is returned.
403 * On failure, -EINVAL is returned.
404 * @ingroup libtinyalsa-mixer
405 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800406int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700407{
Simon Wilson79d39652011-05-25 13:44:23 -0700408 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700409 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700410
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100411 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700412 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700413
Simon Wilson79d39652011-05-25 13:44:23 -0700414 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100415 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700416 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
417 if (ret < 0)
418 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700419
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100420 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700421 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700422 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700423
424 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700425 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700426
427 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
428 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700429
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500430 case SNDRV_CTL_ELEM_TYPE_BYTES:
431 return ev.value.bytes.data[id];
432
Simon Wilson79d39652011-05-25 13:44:23 -0700433 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700434 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700435 }
436
437 return 0;
438}
439
Taylor Holberton4e557192016-11-23 11:48:06 -0800440/** Gets the contents of a control's value array.
441 * @param ctl An initialized control handle.
442 * @param array A pointer to write the array data to.
443 * The size of this array must be equal to the number of items in the array
444 * multiplied by the size of each item.
445 * @param count The number of items in the array.
446 * This parameter must match the number of items in the control.
447 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
448 * @returns On success, zero.
449 * On failure, non-zero.
450 * @ingroup libtinyalsa-mixer
451 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800452int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100453{
454 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530455 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700456 size_t size;
457 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100458
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100459 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100460 return -EINVAL;
461
462 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100463 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100464
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100465 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700466 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
467 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530468 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
469 if (ret < 0)
470 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700471 size = sizeof(ev.value.integer.value[0]);
472 source = ev.value.integer.value;
473 break;
474
475 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530476 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100477 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530478 struct snd_ctl_tlv *tlv;
479 int ret;
480
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700481 if (count > SIZE_MAX - sizeof(*tlv))
482 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530483 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700484 if (!tlv)
485 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100486 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530487 tlv->length = count;
488 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
489
490 source = tlv->tlv;
491 memcpy(array, source, count);
492
493 free(tlv);
494
495 return ret;
496 } else {
497 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
498 if (ret < 0)
499 return ret;
500 size = sizeof(ev.value.bytes.data[0]);
501 source = ev.value.bytes.data;
502 break;
503 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700504
505 default:
506 return -EINVAL;
507 }
508
509 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100510
511 return 0;
512}
513
Taylor Holberton4e557192016-11-23 11:48:06 -0800514/** Sets the value of a control, specified by the value index.
515 * @param ctl An initialized control handle.
516 * @param id The index of the value within the control.
517 * @param value The value to set.
518 * This must be in a range specified by @ref mixer_ctl_get_range_min
519 * and @ref mixer_ctl_get_range_max.
520 * @returns On success, zero is returned.
521 * On failure, non-zero is returned.
522 * @ingroup libtinyalsa-mixer
523 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700524int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700525{
526 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700527 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700528
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100529 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700530 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700531
532 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100533 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700534 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
535 if (ret < 0)
536 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700537
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100538 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700539 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700540 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700541 break;
542
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700543 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200544 if ((value < mixer_ctl_get_range_min(ctl)) ||
545 (value > mixer_ctl_get_range_max(ctl))) {
546 return -EINVAL;
547 }
548
Simon Wilsond2cb5032011-06-04 00:57:17 -0700549 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700550 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700551
Simon Wilson066c9f62011-06-05 18:23:05 -0700552 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
553 ev.value.enumerated.item[id] = value;
554 break;
555
David.Coutherutce2b6342013-06-11 16:22:57 +0200556 case SNDRV_CTL_ELEM_TYPE_BYTES:
557 ev.value.bytes.data[id] = value;
558 break;
559
Simon Wilson79d39652011-05-25 13:44:23 -0700560 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700561 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700562 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700563
Simon Wilson79d39652011-05-25 13:44:23 -0700564 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
565}
566
Taylor Holberton4e557192016-11-23 11:48:06 -0800567/** Sets the contents of a control's value array.
568 * @param ctl An initialized control handle.
569 * @param array The array containing control values.
570 * @param count The number of values in the array.
571 * This must match the number of values in the control.
572 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
573 * @returns On success, zero.
574 * On failure, non-zero.
575 * @ingroup libtinyalsa-mixer
576 */
Simon Wilson38f87f32012-10-23 15:05:23 -0700577int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100578{
579 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700580 size_t size;
581 void *dest;
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:
592 size = sizeof(ev.value.integer.value[0]);
593 dest = ev.value.integer.value;
594 break;
595
596 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530597 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100598 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530599 struct snd_ctl_tlv *tlv;
600 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700601 if (count > SIZE_MAX - sizeof(*tlv))
602 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530603 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700604 if (!tlv)
605 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100606 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530607 tlv->length = count;
608 memcpy(tlv->tlv, array, count);
609
610 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
611 free(tlv);
612
613 return ret;
614 } else {
615 size = sizeof(ev.value.bytes.data[0]);
616 dest = ev.value.bytes.data;
617 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700618 break;
619
620 default:
621 return -EINVAL;
622 }
623
624 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100625
626 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
627}
628
Taylor Holberton4e557192016-11-23 11:48:06 -0800629/** Gets the minimum value of an control.
630 * The control must have an integer type.
631 * The type of the control can be checked with @ref mixer_ctl_get_type.
632 * @param ctl An initialized control handle.
633 * @returns On success, the minimum value of the control.
634 * On failure, -EINVAL.
635 * @ingroup libtinyalsa-mixer
636 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800637int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700638{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100639 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700640 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700641
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100642 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700643}
644
Taylor Holberton4e557192016-11-23 11:48:06 -0800645/** Gets the maximum value of an control.
646 * The control must have an integer type.
647 * The type of the control can be checked with @ref mixer_ctl_get_type.
648 * @param ctl An initialized control handle.
649 * @returns On success, the maximum value of the control.
650 * On failure, -EINVAL.
651 * @ingroup libtinyalsa-mixer
652 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800653int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700654{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100655 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700656 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700657
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100658 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700659}
660
Taylor Holberton4e557192016-11-23 11:48:06 -0800661/** Get the number of enumerated items in the control.
662 * @param ctl An initialized control handle.
663 * @returns The number of enumerated items in the control.
664 * @ingroup libtinyalsa-mixer
665 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800666unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700667{
Simon Wilson193b1c32011-06-07 23:42:38 -0700668 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700669 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700670
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100671 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700672}
673
David.Coutherut9b423962013-06-03 13:45:51 +0200674int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
675{
676 struct snd_ctl_elem_info tmp;
677 unsigned int m;
678 char **enames;
679
680 if (ctl->ename) {
681 return 0;
682 }
683
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400684 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +0200685 if (!enames)
686 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400687 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200688 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400689 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +0200690 tmp.value.enumerated.item = m;
691 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
692 goto fail;
693 enames[m] = strdup(tmp.value.enumerated.name);
694 if (!enames[m])
695 goto fail;
696 }
697 ctl->ename = enames;
698 return 0;
699
700fail:
701 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400702 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200703 if (enames[m]) {
704 free(enames[m]);
705 }
706 }
707 free(enames);
708 }
709 return -1;
710}
711
Taylor Holberton4e557192016-11-23 11:48:06 -0800712/** Gets the string representation of an enumerated item.
713 * @param ctl An initialized control handle.
714 * @param enum_id The index of the enumerated value.
715 * @returns A string representation of the enumerated item.
716 * @ingroup libtinyalsa-mixer
717 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800718const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
719 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700720{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100721 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400722 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200723 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800724 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700725
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800726 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700727}
728
Taylor Holberton4e557192016-11-23 11:48:06 -0800729/** Set an enumeration value by string value.
730 * @param ctl An enumerated mixer control.
731 * @param string The string representation of an enumeration.
732 * @returns On success, zero.
733 * On failure, zero.
734 * @ingroup libtinyalsa-mixer
735 */
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700736int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
737{
738 unsigned int i, num_enums;
739 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700740 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700741
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400742 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200743 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -0700744 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700745
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100746 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700747 for (i = 0; i < num_enums; i++) {
748 if (!strcmp(string, ctl->ename[i])) {
749 memset(&ev, 0, sizeof(ev));
750 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100751 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700752 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
753 if (ret < 0)
754 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700755 return 0;
756 }
757 }
758
Simon Wilson193b1c32011-06-07 23:42:38 -0700759 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700760}
761