blob: 1d2cd74d6de701448d964dc118ba6d7024b18330 [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;
Richard Fitzgerald899cece2014-09-09 17:03:21 +010056 struct snd_ctl_elem_info info;
Simon Wilson79d39652011-05-25 13:44:23 -070057 char **ename;
58};
59
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040060/** A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080061 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040062 */
Simon Wilson79d39652011-05-25 13:44:23 -070063struct mixer {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040064 /** File descriptor for the card */
Simon Wilson79d39652011-05-25 13:44:23 -070065 int fd;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040066 /** Card information */
Simon Wilsonec281392013-06-28 16:21:31 -070067 struct snd_ctl_card_info card_info;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040068 /** A continuous array of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070069 struct mixer_ctl *ctl;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040070 /** The number of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070071 unsigned int count;
72};
73
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040074/** Closes a mixer returned by @ref mixer_open.
75 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080076 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040077 */
Simon Wilson79d39652011-05-25 13:44:23 -070078void mixer_close(struct mixer *mixer)
79{
80 unsigned int n,m;
81
Simon Wilson193b1c32011-06-07 23:42:38 -070082 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -070083 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -070084
Simon Wilson79d39652011-05-25 13:44:23 -070085 if (mixer->fd >= 0)
86 close(mixer->fd);
87
88 if (mixer->ctl) {
89 for (n = 0; n < mixer->count; n++) {
90 if (mixer->ctl[n].ename) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +010091 unsigned int max = mixer->ctl[n].info.value.enumerated.items;
Simon Wilson79d39652011-05-25 13:44:23 -070092 for (m = 0; m < max; m++)
93 free(mixer->ctl[n].ename[m]);
94 free(mixer->ctl[n].ename);
95 }
96 }
97 free(mixer->ctl);
98 }
99
Simon Wilson79d39652011-05-25 13:44:23 -0700100 free(mixer);
101
102 /* TODO: verify frees */
103}
104
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400105/** Opens a mixer for a given card.
106 * @param card The card to open the mixer for.
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500107 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800108 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400109 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700110struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700111{
112 struct snd_ctl_elem_list elist;
Simon Wilson79d39652011-05-25 13:44:23 -0700113 struct snd_ctl_elem_id *eid = NULL;
114 struct mixer *mixer = NULL;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400115 unsigned int n;
Simon Wilson79d39652011-05-25 13:44:23 -0700116 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700117 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -0700118
Simon Wilson1bd580f2011-06-02 15:58:41 -0700119 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
120 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700121 if (fd < 0)
122 return 0;
123
124 memset(&elist, 0, sizeof(elist));
125 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
126 goto fail;
127
128 mixer = calloc(1, sizeof(*mixer));
129 if (!mixer)
130 goto fail;
131
132 mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100133 if (!mixer->ctl)
Simon Wilsonec281392013-06-28 16:21:31 -0700134 goto fail;
135
136 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700137 goto fail;
138
139 eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
140 if (!eid)
141 goto fail;
142
143 mixer->count = elist.count;
144 mixer->fd = fd;
145 elist.space = mixer->count;
146 elist.pids = eid;
147 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
148 goto fail;
149
150 for (n = 0; n < mixer->count; n++) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100151 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
Simon Wilson79d39652011-05-25 13:44:23 -0700152 ei->id.numid = eid[n].numid;
153 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
154 goto fail;
Simon Wilson79d39652011-05-25 13:44:23 -0700155 mixer->ctl[n].mixer = mixer;
Simon Wilson79d39652011-05-25 13:44:23 -0700156 }
157
158 free(eid);
159 return mixer;
160
161fail:
162 /* TODO: verify frees in failure case */
163 if (eid)
164 free(eid);
165 if (mixer)
166 mixer_close(mixer);
167 else if (fd >= 0)
168 close(fd);
169 return 0;
170}
171
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500172/** Gets the name of the mixer's card.
173 * @param mixer An initialized mixer handle.
174 * @returns The name of the mixer's card.
175 * @ingroup libtinyalsa-mixer
176 */
Simon Wilsonec281392013-06-28 16:21:31 -0700177const char *mixer_get_name(struct mixer *mixer)
178{
179 return (const char *)mixer->card_info.name;
180}
181
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500182/** Gets the number of mixer controls for a given mixer.
183 * @param mixer An initialized mixer handle.
184 * @returns The number of mixer controls for the given mixer.
185 * @ingroup libtinyalsa-mixer
186 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700187unsigned int mixer_get_num_ctls(struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700188{
Simon Wilson193b1c32011-06-07 23:42:38 -0700189 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700190 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700191
Simon Wilson79d39652011-05-25 13:44:23 -0700192 return mixer->count;
193}
194
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500195/** Gets a mixer control handle, by the mixer control's id.
196 * @param mixer An initialized mixer handle.
197 * @param id The control's id in the given mixer.
198 * @returns A handle to the mixer control.
199 * @ingroup libtinyalsa-mixer
200 */
Simon Wilson79d39652011-05-25 13:44:23 -0700201struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
202{
Simon Wilson98c1f162011-06-07 16:12:32 -0700203 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700204 return mixer->ctl + id;
205
206 return NULL;
207}
208
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500209/** Gets the first instance of mixer control handle, by the mixer control's name.
210 * @param mixer An initialized mixer handle.
211 * @param name The control's name in the given mixer.
212 * @returns A handle to the mixer control.
213 * @ingroup libtinyalsa-mixer
214 */
Simon Wilson79d39652011-05-25 13:44:23 -0700215struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
216{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200217 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
218}
219
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500220/** Gets an instance of mixer control handle, by the mixer control's name and index.
221 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
222 * @param mixer An initialized mixer handle.
223 * @param name The control's name in the given mixer.
224 * @param index The control's index.
225 * @returns A handle to the mixer control.
226 * @ingroup libtinyalsa-mixer
227 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200228struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
229 const char *name,
230 unsigned int index)
231{
Simon Wilson79d39652011-05-25 13:44:23 -0700232 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200233 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700234
Simon Wilson98c1f162011-06-07 16:12:32 -0700235 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700236 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700237
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200238 ctl = mixer->ctl;
239
Simon Wilson79d39652011-05-25 13:44:23 -0700240 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100241 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200242 if (index-- == 0)
243 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700244
245 return NULL;
246}
247
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500248/** Updates the control's info.
249 * This is useful for a program that may be idle for a period of time.
250 * @param ctl An initialized control handle.
251 * @ingroup libtinyalsa-mixer
252 */
Simon Wilson710df882013-06-28 16:17:50 -0700253void mixer_ctl_update(struct mixer_ctl *ctl)
254{
255 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
256}
257
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500258/** Gets the control's ID.
259 * @param ctl An initialized control handle.
260 * @returns On success, the control's ID is returned.
261 * On error, UINT_MAX is returned instead.
262 * @ingroup libtinyalsa-mixer
263 */
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100264unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl)
265{
266 if (!ctl)
267 return UINT_MAX;
268
269 /* numid values start at 1, return a 0-base value that
270 * can be passed to mixer_get_ctl()
271 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100272 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100273}
274
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500275/** Gets the name of the control.
276 * @param ctl An initialized control handle.
277 * @returns On success, the name of the control.
278 * On error, NULL.
279 * @ingroup libtinyalsa-mixer
280 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800281const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700282{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800283 if (!ctl)
284 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700285
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100286 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700287}
288
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500289/** Gets the value type of the control.
290 * @param ctl An initialized control handle
291 * @returns On success, the type of mixer control.
292 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
293 * @ingroup libtinyalsa-mixer
294 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700295enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
296{
Simon Wilson193b1c32011-06-07 23:42:38 -0700297 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700298 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700299
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100300 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700301 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
302 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
303 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
304 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
305 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
306 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
307 default: return MIXER_CTL_TYPE_UNKNOWN;
308 };
309}
310
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500311/** Gets the string that describes the value type of the control.
312 * @param ctl An initialized control handle
313 * @returns On success, a string describing type of mixer control.
314 * @ingroup libtinyalsa-mixer
315 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700316const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
317{
Simon Wilson193b1c32011-06-07 23:42:38 -0700318 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700319 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700320
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100321 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700322 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
323 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
324 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
325 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
326 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
327 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
328 default: return "Unknown";
329 };
330}
331
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500332/** Gets the number of values in the control.
333 * @param ctl An initialized control handle
334 * @returns The number of values in the mixer control
335 * @ingroup libtinyalsa-mixer
336 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700337unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
338{
Simon Wilson193b1c32011-06-07 23:42:38 -0700339 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700340 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700341
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100342 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700343}
344
Simon Wilson79d39652011-05-25 13:44:23 -0700345static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
346{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200347 if ((percent > 100) || (percent < 0)) {
348 return -EINVAL;
349 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700350
Baptiste Robert4a484e12013-09-12 15:37:53 +0200351 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700352
Simon Wilson79d39652011-05-25 13:44:23 -0700353 return ei->value.integer.min + (range * percent) / 100;
354}
355
356static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
357{
358 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700359
Simon Wilson79d39652011-05-25 13:44:23 -0700360 if (range == 0)
361 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700362
Simon Wilson79d39652011-05-25 13:44:23 -0700363 return ((value - ei->value.integer.min) / range) * 100;
364}
365
Taylor Holberton4e557192016-11-23 11:48:06 -0800366/** Gets a percentage representation of a specified control value.
367 * @param ctl An initialized control handle.
368 * @param id The index of the value within the control.
369 * @returns On success, the percentage representation of the control value.
370 * On failure, -EINVAL is returned.
371 * @ingroup libtinyalsa-mixer
372 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700373int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700374{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100375 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700376 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700377
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100378 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700379}
380
Taylor Holberton4e557192016-11-23 11:48:06 -0800381/** Sets the value of a control by percent, specified by the value index.
382 * @param ctl An initialized control handle.
383 * @param id The index of the value to set
384 * @param percent A percentage value between 0 and 100.
385 * @returns On success, zero is returned.
386 * On failure, non-zero is returned.
387 * @ingroup libtinyalsa-mixer
388 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700389int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700390{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100391 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700392 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700393
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100394 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700395}
396
Taylor Holberton4e557192016-11-23 11:48:06 -0800397/** Gets the value of a control.
398 * @param ctl An initialized control handle.
399 * @param id The index of the control value.
400 * @returns On success, the specified value is returned.
401 * On failure, -EINVAL is returned.
402 * @ingroup libtinyalsa-mixer
403 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700404int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700405{
Simon Wilson79d39652011-05-25 13:44:23 -0700406 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700407 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700408
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100409 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700410 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700411
Simon Wilson79d39652011-05-25 13:44:23 -0700412 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100413 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700414 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
415 if (ret < 0)
416 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700417
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100418 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700419 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700420 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700421
422 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700423 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700424
425 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
426 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700427
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500428 case SNDRV_CTL_ELEM_TYPE_BYTES:
429 return ev.value.bytes.data[id];
430
Simon Wilson79d39652011-05-25 13:44:23 -0700431 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700432 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700433 }
434
435 return 0;
436}
437
Taylor Holberton4e557192016-11-23 11:48:06 -0800438/** Gets the contents of a control's value array.
439 * @param ctl An initialized control handle.
440 * @param array A pointer to write the array data to.
441 * The size of this array must be equal to the number of items in the array
442 * multiplied by the size of each item.
443 * @param count The number of items in the array.
444 * This parameter must match the number of items in the control.
445 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
446 * @returns On success, zero.
447 * On failure, non-zero.
448 * @ingroup libtinyalsa-mixer
449 */
Simon Wilson38f87f32012-10-23 15:05:23 -0700450int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100451{
452 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530453 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700454 size_t size;
455 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100456
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100457 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100458 return -EINVAL;
459
460 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100461 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100462
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100463 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700464 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
465 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530466 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
467 if (ret < 0)
468 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700469 size = sizeof(ev.value.integer.value[0]);
470 source = ev.value.integer.value;
471 break;
472
473 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530474 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100475 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530476 struct snd_ctl_tlv *tlv;
477 int ret;
478
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700479 if (count > SIZE_MAX - sizeof(*tlv))
480 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530481 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700482 if (!tlv)
483 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100484 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530485 tlv->length = count;
486 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
487
488 source = tlv->tlv;
489 memcpy(array, source, count);
490
491 free(tlv);
492
493 return ret;
494 } else {
495 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
496 if (ret < 0)
497 return ret;
498 size = sizeof(ev.value.bytes.data[0]);
499 source = ev.value.bytes.data;
500 break;
501 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700502
503 default:
504 return -EINVAL;
505 }
506
507 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100508
509 return 0;
510}
511
Taylor Holberton4e557192016-11-23 11:48:06 -0800512/** Sets the value of a control, specified by the value index.
513 * @param ctl An initialized control handle.
514 * @param id The index of the value within the control.
515 * @param value The value to set.
516 * This must be in a range specified by @ref mixer_ctl_get_range_min
517 * and @ref mixer_ctl_get_range_max.
518 * @returns On success, zero is returned.
519 * On failure, non-zero is returned.
520 * @ingroup libtinyalsa-mixer
521 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700522int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700523{
524 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700525 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700526
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100527 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700528 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700529
530 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100531 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700532 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
533 if (ret < 0)
534 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700535
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100536 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700537 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700538 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700539 break;
540
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700541 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200542 if ((value < mixer_ctl_get_range_min(ctl)) ||
543 (value > mixer_ctl_get_range_max(ctl))) {
544 return -EINVAL;
545 }
546
Simon Wilsond2cb5032011-06-04 00:57:17 -0700547 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700548 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700549
Simon Wilson066c9f62011-06-05 18:23:05 -0700550 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
551 ev.value.enumerated.item[id] = value;
552 break;
553
David.Coutherutce2b6342013-06-11 16:22:57 +0200554 case SNDRV_CTL_ELEM_TYPE_BYTES:
555 ev.value.bytes.data[id] = value;
556 break;
557
Simon Wilson79d39652011-05-25 13:44:23 -0700558 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700559 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700560 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700561
Simon Wilson79d39652011-05-25 13:44:23 -0700562 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
563}
564
Taylor Holberton4e557192016-11-23 11:48:06 -0800565/** Sets the contents of a control's value array.
566 * @param ctl An initialized control handle.
567 * @param array The array containing control values.
568 * @param count The number of values in the array.
569 * This must match the number of values in the control.
570 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
571 * @returns On success, zero.
572 * On failure, non-zero.
573 * @ingroup libtinyalsa-mixer
574 */
Simon Wilson38f87f32012-10-23 15:05:23 -0700575int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100576{
577 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700578 size_t size;
579 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100580
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100581 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100582 return -EINVAL;
583
584 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100585 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100586
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100587 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700588 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
589 case SNDRV_CTL_ELEM_TYPE_INTEGER:
590 size = sizeof(ev.value.integer.value[0]);
591 dest = ev.value.integer.value;
592 break;
593
594 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530595 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100596 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530597 struct snd_ctl_tlv *tlv;
598 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700599 if (count > SIZE_MAX - sizeof(*tlv))
600 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530601 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700602 if (!tlv)
603 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100604 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530605 tlv->length = count;
606 memcpy(tlv->tlv, array, count);
607
608 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
609 free(tlv);
610
611 return ret;
612 } else {
613 size = sizeof(ev.value.bytes.data[0]);
614 dest = ev.value.bytes.data;
615 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700616 break;
617
618 default:
619 return -EINVAL;
620 }
621
622 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100623
624 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
625}
626
Taylor Holberton4e557192016-11-23 11:48:06 -0800627/** Gets the minimum value of an control.
628 * The control must have an integer type.
629 * The type of the control can be checked with @ref mixer_ctl_get_type.
630 * @param ctl An initialized control handle.
631 * @returns On success, the minimum value of the control.
632 * On failure, -EINVAL.
633 * @ingroup libtinyalsa-mixer
634 */
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700635int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
636{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100637 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700638 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700639
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100640 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700641}
642
Taylor Holberton4e557192016-11-23 11:48:06 -0800643/** Gets the maximum value of an control.
644 * The control must have an integer type.
645 * The type of the control can be checked with @ref mixer_ctl_get_type.
646 * @param ctl An initialized control handle.
647 * @returns On success, the maximum value of the control.
648 * On failure, -EINVAL.
649 * @ingroup libtinyalsa-mixer
650 */
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700651int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
652{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100653 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700654 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700655
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100656 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700657}
658
Taylor Holberton4e557192016-11-23 11:48:06 -0800659/** Get the number of enumerated items in the control.
660 * @param ctl An initialized control handle.
661 * @returns The number of enumerated items in the control.
662 * @ingroup libtinyalsa-mixer
663 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700664unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
665{
Simon Wilson193b1c32011-06-07 23:42:38 -0700666 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700667 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700668
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100669 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700670}
671
David.Coutherut9b423962013-06-03 13:45:51 +0200672int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
673{
674 struct snd_ctl_elem_info tmp;
675 unsigned int m;
676 char **enames;
677
678 if (ctl->ename) {
679 return 0;
680 }
681
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400682 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +0200683 if (!enames)
684 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400685 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200686 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400687 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +0200688 tmp.value.enumerated.item = m;
689 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
690 goto fail;
691 enames[m] = strdup(tmp.value.enumerated.name);
692 if (!enames[m])
693 goto fail;
694 }
695 ctl->ename = enames;
696 return 0;
697
698fail:
699 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400700 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200701 if (enames[m]) {
702 free(enames[m]);
703 }
704 }
705 free(enames);
706 }
707 return -1;
708}
709
Taylor Holberton4e557192016-11-23 11:48:06 -0800710/** Gets the string representation of an enumerated item.
711 * @param ctl An initialized control handle.
712 * @param enum_id The index of the enumerated value.
713 * @returns A string representation of the enumerated item.
714 * @ingroup libtinyalsa-mixer
715 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800716const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
717 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700718{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100719 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400720 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200721 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800722 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700723
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800724 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700725}
726
Taylor Holberton4e557192016-11-23 11:48:06 -0800727/** Set an enumeration value by string value.
728 * @param ctl An enumerated mixer control.
729 * @param string The string representation of an enumeration.
730 * @returns On success, zero.
731 * On failure, zero.
732 * @ingroup libtinyalsa-mixer
733 */
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700734int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
735{
736 unsigned int i, num_enums;
737 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700738 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700739
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400740 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200741 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -0700742 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700743
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100744 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700745 for (i = 0; i < num_enums; i++) {
746 if (!strcmp(string, ctl->ename[i])) {
747 memset(&ev, 0, sizeof(ev));
748 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100749 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700750 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
751 if (ret < 0)
752 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700753 return 0;
754 }
755 }
756
Simon Wilson193b1c32011-06-07 23:42:38 -0700757 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700758}
759