blob: 4bb17565b6be27cd29e7694d8df2a2352e67f6d4 [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.
Taylor Holbertona94295b2016-12-01 18:23:16 -0800198 * For non-const access, see @ref mixer_get_ctl
199 * @param mixer An initialized mixer handle.
200 * @param id The control's id in the given mixer.
201 * @returns A handle to the mixer control.
202 * @ingroup libtinyalsa-mixer
203 */
204const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
205{
206 if (mixer && (id < mixer->count))
207 return mixer->ctl + id;
208
209 return NULL;
210}
211
212/** Gets a mixer control handle, by the mixer control's id.
213 * For const access, see @ref mixer_get_ctl_const
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500214 * @param mixer An initialized mixer handle.
215 * @param id The control's id in the given mixer.
216 * @returns A handle to the mixer control.
217 * @ingroup libtinyalsa-mixer
218 */
Simon Wilson79d39652011-05-25 13:44:23 -0700219struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
220{
Simon Wilson98c1f162011-06-07 16:12:32 -0700221 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700222 return mixer->ctl + id;
223
224 return NULL;
225}
226
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500227/** Gets the first instance of mixer control handle, by the mixer control's name.
228 * @param mixer An initialized mixer handle.
229 * @param name The control's name in the given mixer.
230 * @returns A handle to the mixer control.
231 * @ingroup libtinyalsa-mixer
232 */
Simon Wilson79d39652011-05-25 13:44:23 -0700233struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
234{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200235 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
236}
237
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500238/** Gets an instance of mixer control handle, by the mixer control's name and index.
239 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
240 * @param mixer An initialized mixer handle.
241 * @param name The control's name in the given mixer.
242 * @param index The control's index.
243 * @returns A handle to the mixer control.
244 * @ingroup libtinyalsa-mixer
245 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200246struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
247 const char *name,
248 unsigned int index)
249{
Simon Wilson79d39652011-05-25 13:44:23 -0700250 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200251 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700252
Simon Wilson98c1f162011-06-07 16:12:32 -0700253 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700254 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700255
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200256 ctl = mixer->ctl;
257
Simon Wilson79d39652011-05-25 13:44:23 -0700258 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100259 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200260 if (index-- == 0)
261 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700262
263 return NULL;
264}
265
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500266/** Updates the control's info.
267 * This is useful for a program that may be idle for a period of time.
268 * @param ctl An initialized control handle.
269 * @ingroup libtinyalsa-mixer
270 */
Simon Wilson710df882013-06-28 16:17:50 -0700271void mixer_ctl_update(struct mixer_ctl *ctl)
272{
273 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
274}
275
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500276/** Gets the control's ID.
277 * @param ctl An initialized control handle.
278 * @returns On success, the control's ID is returned.
279 * On error, UINT_MAX is returned instead.
280 * @ingroup libtinyalsa-mixer
281 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800282unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100283{
284 if (!ctl)
285 return UINT_MAX;
286
287 /* numid values start at 1, return a 0-base value that
288 * can be passed to mixer_get_ctl()
289 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100290 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100291}
292
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500293/** Gets the name of the control.
294 * @param ctl An initialized control handle.
295 * @returns On success, the name of the control.
296 * On error, NULL.
297 * @ingroup libtinyalsa-mixer
298 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800299const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700300{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800301 if (!ctl)
302 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700303
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100304 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700305}
306
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500307/** Gets the value type of the control.
308 * @param ctl An initialized control handle
309 * @returns On success, the type of mixer control.
310 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
311 * @ingroup libtinyalsa-mixer
312 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800313enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700314{
Simon Wilson193b1c32011-06-07 23:42:38 -0700315 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700316 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700317
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100318 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700319 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
320 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
321 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
322 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
323 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
324 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
325 default: return MIXER_CTL_TYPE_UNKNOWN;
326 };
327}
328
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500329/** Gets the string that describes the value type of the control.
330 * @param ctl An initialized control handle
331 * @returns On success, a string describing type of mixer control.
332 * @ingroup libtinyalsa-mixer
333 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800334const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700335{
Simon Wilson193b1c32011-06-07 23:42:38 -0700336 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700337 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700338
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100339 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700340 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
341 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
342 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
343 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
344 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
345 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
346 default: return "Unknown";
347 };
348}
349
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500350/** Gets the number of values in the control.
351 * @param ctl An initialized control handle
352 * @returns The number of values in the mixer control
353 * @ingroup libtinyalsa-mixer
354 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800355unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700356{
Simon Wilson193b1c32011-06-07 23:42:38 -0700357 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700358 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700359
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100360 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700361}
362
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800363static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilson79d39652011-05-25 13:44:23 -0700364{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200365 if ((percent > 100) || (percent < 0)) {
366 return -EINVAL;
367 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700368
Baptiste Robert4a484e12013-09-12 15:37:53 +0200369 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700370
Simon Wilson79d39652011-05-25 13:44:23 -0700371 return ei->value.integer.min + (range * percent) / 100;
372}
373
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800374static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700375{
376 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700377
Simon Wilson79d39652011-05-25 13:44:23 -0700378 if (range == 0)
379 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700380
Simon Wilson79d39652011-05-25 13:44:23 -0700381 return ((value - ei->value.integer.min) / range) * 100;
382}
383
Taylor Holberton4e557192016-11-23 11:48:06 -0800384/** Gets a percentage representation of a specified control value.
385 * @param ctl An initialized control handle.
386 * @param id The index of the value within the control.
387 * @returns On success, the percentage representation of the control value.
388 * On failure, -EINVAL is returned.
389 * @ingroup libtinyalsa-mixer
390 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800391int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -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 int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700397}
398
Taylor Holberton4e557192016-11-23 11:48:06 -0800399/** Sets the value of a control by percent, specified by the value index.
400 * @param ctl An initialized control handle.
401 * @param id The index of the value to set
402 * @param percent A percentage value between 0 and 100.
403 * @returns On success, zero is returned.
404 * On failure, non-zero is returned.
405 * @ingroup libtinyalsa-mixer
406 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700407int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700408{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100409 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700410 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700411
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100412 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700413}
414
Taylor Holberton4e557192016-11-23 11:48:06 -0800415/** Gets the value of a control.
416 * @param ctl An initialized control handle.
417 * @param id The index of the control value.
418 * @returns On success, the specified value is returned.
419 * On failure, -EINVAL is returned.
420 * @ingroup libtinyalsa-mixer
421 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800422int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700423{
Simon Wilson79d39652011-05-25 13:44:23 -0700424 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700425 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700426
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100427 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700428 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700429
Simon Wilson79d39652011-05-25 13:44:23 -0700430 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100431 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700432 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
433 if (ret < 0)
434 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700435
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100436 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700437 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700438 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700439
440 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700441 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700442
443 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
444 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700445
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500446 case SNDRV_CTL_ELEM_TYPE_BYTES:
447 return ev.value.bytes.data[id];
448
Simon Wilson79d39652011-05-25 13:44:23 -0700449 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700450 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700451 }
452
453 return 0;
454}
455
Taylor Holberton4e557192016-11-23 11:48:06 -0800456/** Gets the contents of a control's value array.
457 * @param ctl An initialized control handle.
458 * @param array A pointer to write the array data to.
459 * The size of this array must be equal to the number of items in the array
460 * multiplied by the size of each item.
461 * @param count The number of items in the array.
462 * This parameter must match the number of items in the control.
463 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
464 * @returns On success, zero.
465 * On failure, non-zero.
466 * @ingroup libtinyalsa-mixer
467 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800468int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100469{
470 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530471 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700472 size_t size;
473 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100474
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100475 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100476 return -EINVAL;
477
478 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100479 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100480
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100481 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700482 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
483 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530484 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
485 if (ret < 0)
486 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700487 size = sizeof(ev.value.integer.value[0]);
488 source = ev.value.integer.value;
489 break;
490
491 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530492 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100493 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530494 struct snd_ctl_tlv *tlv;
495 int ret;
496
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700497 if (count > SIZE_MAX - sizeof(*tlv))
498 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530499 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700500 if (!tlv)
501 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100502 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530503 tlv->length = count;
504 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
505
506 source = tlv->tlv;
507 memcpy(array, source, count);
508
509 free(tlv);
510
511 return ret;
512 } else {
513 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
514 if (ret < 0)
515 return ret;
516 size = sizeof(ev.value.bytes.data[0]);
517 source = ev.value.bytes.data;
518 break;
519 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700520
521 default:
522 return -EINVAL;
523 }
524
525 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100526
527 return 0;
528}
529
Taylor Holberton4e557192016-11-23 11:48:06 -0800530/** Sets the value of a control, specified by the value index.
531 * @param ctl An initialized control handle.
532 * @param id The index of the value within the control.
533 * @param value The value to set.
534 * This must be in a range specified by @ref mixer_ctl_get_range_min
535 * and @ref mixer_ctl_get_range_max.
536 * @returns On success, zero is returned.
537 * On failure, non-zero is returned.
538 * @ingroup libtinyalsa-mixer
539 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700540int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700541{
542 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700543 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700544
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100545 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700546 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700547
548 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100549 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700550 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
551 if (ret < 0)
552 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700553
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100554 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700555 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700556 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700557 break;
558
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700559 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200560 if ((value < mixer_ctl_get_range_min(ctl)) ||
561 (value > mixer_ctl_get_range_max(ctl))) {
562 return -EINVAL;
563 }
564
Simon Wilsond2cb5032011-06-04 00:57:17 -0700565 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700566 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700567
Simon Wilson066c9f62011-06-05 18:23:05 -0700568 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
569 ev.value.enumerated.item[id] = value;
570 break;
571
David.Coutherutce2b6342013-06-11 16:22:57 +0200572 case SNDRV_CTL_ELEM_TYPE_BYTES:
573 ev.value.bytes.data[id] = value;
574 break;
575
Simon Wilson79d39652011-05-25 13:44:23 -0700576 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700577 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700578 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700579
Simon Wilson79d39652011-05-25 13:44:23 -0700580 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
581}
582
Taylor Holberton4e557192016-11-23 11:48:06 -0800583/** Sets the contents of a control's value array.
584 * @param ctl An initialized control handle.
585 * @param array The array containing control values.
586 * @param count The number of values in the array.
587 * This must match the number of values in the control.
588 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
589 * @returns On success, zero.
590 * On failure, non-zero.
591 * @ingroup libtinyalsa-mixer
592 */
Simon Wilson38f87f32012-10-23 15:05:23 -0700593int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100594{
595 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700596 size_t size;
597 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100598
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100599 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100600 return -EINVAL;
601
602 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100603 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100604
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100605 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700606 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
607 case SNDRV_CTL_ELEM_TYPE_INTEGER:
608 size = sizeof(ev.value.integer.value[0]);
609 dest = ev.value.integer.value;
610 break;
611
612 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530613 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100614 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530615 struct snd_ctl_tlv *tlv;
616 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700617 if (count > SIZE_MAX - sizeof(*tlv))
618 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530619 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700620 if (!tlv)
621 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100622 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530623 tlv->length = count;
624 memcpy(tlv->tlv, array, count);
625
626 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
627 free(tlv);
628
629 return ret;
630 } else {
631 size = sizeof(ev.value.bytes.data[0]);
632 dest = ev.value.bytes.data;
633 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700634 break;
635
636 default:
637 return -EINVAL;
638 }
639
640 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100641
642 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
643}
644
Taylor Holberton4e557192016-11-23 11:48:06 -0800645/** Gets the minimum 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 minimum 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_min(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.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700659}
660
Taylor Holberton4e557192016-11-23 11:48:06 -0800661/** Gets the maximum value of an control.
662 * The control must have an integer type.
663 * The type of the control can be checked with @ref mixer_ctl_get_type.
664 * @param ctl An initialized control handle.
665 * @returns On success, the maximum value of the control.
666 * On failure, -EINVAL.
667 * @ingroup libtinyalsa-mixer
668 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800669int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700670{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100671 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700672 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700673
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100674 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700675}
676
Taylor Holberton4e557192016-11-23 11:48:06 -0800677/** Get the number of enumerated items in the control.
678 * @param ctl An initialized control handle.
679 * @returns The number of enumerated items in the control.
680 * @ingroup libtinyalsa-mixer
681 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800682unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700683{
Simon Wilson193b1c32011-06-07 23:42:38 -0700684 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700685 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700686
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100687 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700688}
689
David.Coutherut9b423962013-06-03 13:45:51 +0200690int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
691{
692 struct snd_ctl_elem_info tmp;
693 unsigned int m;
694 char **enames;
695
696 if (ctl->ename) {
697 return 0;
698 }
699
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400700 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +0200701 if (!enames)
702 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400703 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200704 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400705 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +0200706 tmp.value.enumerated.item = m;
707 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
708 goto fail;
709 enames[m] = strdup(tmp.value.enumerated.name);
710 if (!enames[m])
711 goto fail;
712 }
713 ctl->ename = enames;
714 return 0;
715
716fail:
717 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400718 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200719 if (enames[m]) {
720 free(enames[m]);
721 }
722 }
723 free(enames);
724 }
725 return -1;
726}
727
Taylor Holberton4e557192016-11-23 11:48:06 -0800728/** Gets the string representation of an enumerated item.
729 * @param ctl An initialized control handle.
730 * @param enum_id The index of the enumerated value.
731 * @returns A string representation of the enumerated item.
732 * @ingroup libtinyalsa-mixer
733 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800734const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
735 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700736{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100737 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400738 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200739 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800740 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700741
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800742 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700743}
744
Taylor Holberton4e557192016-11-23 11:48:06 -0800745/** Set an enumeration value by string value.
746 * @param ctl An enumerated mixer control.
747 * @param string The string representation of an enumeration.
748 * @returns On success, zero.
749 * On failure, zero.
750 * @ingroup libtinyalsa-mixer
751 */
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700752int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
753{
754 unsigned int i, num_enums;
755 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700756 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700757
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400758 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200759 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -0700760 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700761
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100762 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700763 for (i = 0; i < num_enums; i++) {
764 if (!strcmp(string, ctl->ename[i])) {
765 memset(&ev, 0, sizeof(ev));
766 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100767 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700768 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
769 if (ret < 0)
770 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700771 return 0;
772 }
773 }
774
Simon Wilson193b1c32011-06-07 23:42:38 -0700775 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700776}
777