blob: 7b8540a92543ad2d56e9849e9dbbfb8f755bab7d [file] [log] [blame]
Simon Wilsonedff7082011-06-06 15:33:34 -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
David Lie95e65a2020-12-09 02:11:18 +000029#include <stdbool.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070030#include <stdio.h>
31#include <stdlib.h>
Ben Zhang6cb14f22016-04-22 17:59:40 -070032#include <stdint.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070033#include <string.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <errno.h>
37#include <ctype.h>
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +053038#include <poll.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070039
Simon Wilson85dc38f2012-05-15 17:37:19 -070040#include <sys/ioctl.h>
41
Simon Wilsonedff7082011-06-06 15:33:34 -070042#include <linux/ioctl.h>
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -070043
David Lie0f41062020-11-05 12:27:29 +000044#define __force
David Lie0f41062020-11-05 12:27:29 +000045#define __bitwise
David Lie0f41062020-11-05 12:27:29 +000046#define __user
David Lie0f41062020-11-05 12:27:29 +000047#include <sound/asound.h>
48
David Lie95e65a2020-12-09 02:11:18 +000049#ifndef SNDRV_CTL_ELEM_ID_NAME_MAXLEN
50#define SNDRV_CTL_ELEM_ID_NAME_MAXLEN 44
51#endif
David Lie0f41062020-11-05 12:27:29 +000052
David Lie95e65a2020-12-09 02:11:18 +000053#include <tinyalsa/asoundlib.h>
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -070054#include "mixer_io.h"
David Lie0f41062020-11-05 12:27:29 +000055
Simon Wilsonedff7082011-06-06 15:33:34 -070056struct mixer_ctl {
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -070057 struct mixer_ctl_group *grp;
David Lie95e65a2020-12-09 02:11:18 +000058 struct snd_ctl_elem_info *info;
Simon Wilsonedff7082011-06-06 15:33:34 -070059 char **ename;
David Lie95e65a2020-12-09 02:11:18 +000060 bool info_retrieved;
Simon Wilsonedff7082011-06-06 15:33:34 -070061};
62
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -070063struct mixer_ctl_group {
David Lie95e65a2020-12-09 02:11:18 +000064 struct snd_ctl_elem_info *elem_info;
65 struct mixer_ctl *ctl;
66 unsigned int count;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -070067 int event_cnt;
68
69 struct mixer_ops *ops;
70 void *data;
David Lie0f41062020-11-05 12:27:29 +000071};
72
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -070073struct mixer {
74 int fd;
75 struct snd_ctl_card_info card_info;
76
77 /* hardware/physical mixer control group */
78 struct mixer_ctl_group *hw_grp;
79
80 /*
81 * Virutal mixer control group.
82 * Currently supports one virtual mixer (.so)
83 * per card. Could be extended to multiple
84 */
85 struct mixer_ctl_group *virt_grp;
86
87 unsigned int total_ctl_count;
88};
89
90static void mixer_grp_close(struct mixer_ctl_group *grp)
91{
92 unsigned int n, m;
93
94 if (!grp)
95 return;
96
97 if (grp->ctl) {
98 for (n = 0; n < grp->count; n++) {
99 if (grp->ctl[n].ename) {
100 unsigned int max = grp->ctl[n].info->value.enumerated.items;
101 for (m = 0; m < max; m++)
102 free(grp->ctl[n].ename[m]);
103 free(grp->ctl[n].ename);
104 }
105 }
106 free(grp->ctl);
107 }
108
109 if (grp->elem_info)
110 free(grp->elem_info);
111
112 free(grp);
113}
114
Simon Wilsonedff7082011-06-06 15:33:34 -0700115void mixer_close(struct mixer *mixer)
116{
Simon Wilsonedff7082011-06-06 15:33:34 -0700117 if (!mixer)
118 return;
119
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700120 if (mixer->fd >= 0 && mixer->hw_grp)
121 mixer->hw_grp->ops->close(mixer->hw_grp->data);
122 mixer_grp_close(mixer->hw_grp);
Simon Wilsonedff7082011-06-06 15:33:34 -0700123
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700124 if (mixer->virt_grp)
125 mixer->virt_grp->ops->close(mixer->virt_grp->data);
126 mixer_grp_close(mixer->virt_grp);
Simon Wilsonedff7082011-06-06 15:33:34 -0700127
128 free(mixer);
129
130 /* TODO: verify frees */
131}
132
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700133static int mixer_grp_open(struct mixer_ctl_group **ctl_grp,
134 struct mixer_ops *ops,
135 void *data, int *num_ctls_in_grp)
Simon Wilsonedff7082011-06-06 15:33:34 -0700136{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700137 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700138 struct snd_ctl_elem_list elist;
Simon Wilsonedff7082011-06-06 15:33:34 -0700139 struct snd_ctl_elem_id *eid = NULL;
John Muir22bca932017-10-23 10:58:12 -0700140 unsigned int n;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700141 int ret;
David Lie95e65a2020-12-09 02:11:18 +0000142
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700143 grp = calloc(1, sizeof(*grp));
144 if (!grp)
145 return -ENOMEM;
Simon Wilsonedff7082011-06-06 15:33:34 -0700146
147 memset(&elist, 0, sizeof(elist));
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700148 ret = ops->ioctl(data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist);
149 if (ret < 0)
150 goto err_get_elem_list;
Simon Wilsonedff7082011-06-06 15:33:34 -0700151
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700152 grp->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
153 grp->elem_info = calloc(elist.count, sizeof(struct snd_ctl_elem_info));
154 if (!grp->ctl || !grp->elem_info) {
155 ret = -ENOMEM;
156 goto err_ctl_alloc;
157 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700158
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700159 eid = calloc(elist.count, sizeof(*eid));
160 if (!eid) {
161 ret = -ENOMEM;
162 goto err_ctl_alloc;
163 }
Simon Wilson7e8a6562013-06-28 16:47:16 -0700164
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700165 grp->count = elist.count;
166 elist.space = grp->count;
David Lie95e65a2020-12-09 02:11:18 +0000167 elist.pids = eid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700168 ret = ops->ioctl(data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist);
169 if (ret < 0)
170 goto err_ctl_alloc;
David Lie95e65a2020-12-09 02:11:18 +0000171
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700172 for (n = 0; n < grp->count; n++) {
173 struct mixer_ctl *ctl = grp->ctl + n;
David Lie95e65a2020-12-09 02:11:18 +0000174
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700175 ctl->grp = grp;
176 ctl->info = grp->elem_info + n;
David Lie95e65a2020-12-09 02:11:18 +0000177 ctl->info->id.numid = eid[n].numid;
178 strncpy((char *)ctl->info->id.name, (char *)eid[n].name,
179 SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
180 ctl->info->id.name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1] = 0;
181 }
182
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700183 grp->data = data;
184 grp->ops = ops;
185 *ctl_grp = grp;
186 *num_ctls_in_grp = grp->count;
187
David Lie95e65a2020-12-09 02:11:18 +0000188 free(eid);
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700189 return 0;
190
191err_ctl_alloc:
192
193 free(eid);
194 free(grp->elem_info);
195 free(grp->ctl);
196
197err_get_elem_list:
198
199 free(grp);
200 return ret;
201
202}
203
204static int mixer_do_hw_open(struct mixer *mixer, unsigned int card)
205{
206 struct mixer_ops *ops;
207 void *data;
208 int fd, ret, num_grp_ctls = 0;
209
210 mixer->fd = -1;
211 fd = mixer_hw_open(card, &data, &ops);
212 if (fd < 0)
213 return fd;
214
215 ret = ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info);
216 if (ret < 0)
217 goto err_card_info;
218
219 ret = mixer_grp_open(&mixer->hw_grp, ops, data, &num_grp_ctls);
220 if (ret < 0)
221 goto err_card_info;
222
223 mixer->total_ctl_count += num_grp_ctls;
224
225 mixer->fd = fd;
226 return 0;
227
228err_card_info:
229 ops->close(data);
230 return ret;
231
232}
233
234static int mixer_do_plugin_open(struct mixer *mixer, unsigned int card,
235 int is_hw_open_failed)
236{
237 struct mixer_ops *ops;
238 void *data;
239 int ret, num_grp_ctls = 0;
240
241 ret = mixer_plugin_open(card, &data, &ops);
242 if (ret < 0)
243 return ret;
244
245 /* Get card_info if hw_open failed */
246 if (is_hw_open_failed) {
247 ret = ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info);
248 if (ret < 0)
249 goto err_card_info;
250 }
251
252 ret = mixer_grp_open(&mixer->virt_grp, ops, data, &num_grp_ctls);
253 if (ret < 0)
254 goto err_card_info;
255
256 mixer->total_ctl_count += num_grp_ctls;
257 return 0;
258
259err_card_info:
260 ops->close(data);
261 return ret;
262
263}
264
265struct mixer *mixer_open(unsigned int card)
266{
267 struct mixer *mixer = NULL;
268 int h_status, v_status;
269
270 mixer = calloc(1, sizeof(*mixer));
271 if (!mixer)
272 goto fail;
273
274 /* open hardware node first */
275 h_status = mixer_do_hw_open(mixer, card);
276
277 /*
278 * open the virtual node even if hw_open fails
279 * since hw_open is expected to fail for virtual cards
280 * for which kernel does not register mixer node
281 */
282 //TODO: open virtual node only if mixer is defined under snd-card-def
283 v_status = mixer_do_plugin_open(mixer, card, h_status);
284
285 /* Fail mixer_open if both hw and plugin nodes cannot be opened */
286 if (h_status < 0 && v_status < 0)
287 goto fail;
288
Simon Wilsonedff7082011-06-06 15:33:34 -0700289 return mixer;
290
291fail:
Simon Wilsonedff7082011-06-06 15:33:34 -0700292 if (mixer)
293 mixer_close(mixer);
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700294
David Lie0f41062020-11-05 12:27:29 +0000295 return 0;
Simon Wilson7e8a6562013-06-28 16:47:16 -0700296}
297
David Lie95e65a2020-12-09 02:11:18 +0000298static bool mixer_ctl_get_elem_info(struct mixer_ctl* ctl)
299{
Akhil Karuturia9724402020-01-17 13:53:14 -0800300 struct mixer_ctl_group *grp;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700301 unsigned int i;
302
Akhil Karuturia9724402020-01-17 13:53:14 -0800303 if (!ctl || !ctl->grp)
304 return false;
305
306 grp = ctl->grp;
David Lie95e65a2020-12-09 02:11:18 +0000307 if (!ctl->info_retrieved) {
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700308 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO,
309 ctl->info) < 0)
David Lie95e65a2020-12-09 02:11:18 +0000310 return false;
311 ctl->info_retrieved = true;
312 }
313
314 if (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED || ctl->ename)
315 return true;
316
317 struct snd_ctl_elem_info tmp;
318 char** enames = calloc(ctl->info->value.enumerated.items, sizeof(char*));
319 if (!enames)
320 return false;
321
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700322 for (i = 0; i < ctl->info->value.enumerated.items; i++) {
David Lie95e65a2020-12-09 02:11:18 +0000323 memset(&tmp, 0, sizeof(tmp));
324 tmp.id.numid = ctl->info->id.numid;
325 tmp.value.enumerated.item = i;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700326 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
David Lie95e65a2020-12-09 02:11:18 +0000327 goto fail;
328 enames[i] = strdup(tmp.value.enumerated.name);
329 if (!enames[i])
330 goto fail;
331 }
332 ctl->ename = enames;
333 return true;
334
335fail:
336 free(enames);
337 return false;
338}
339
340const char *mixer_get_name(struct mixer *mixer)
David Lie0f41062020-11-05 12:27:29 +0000341{
342 return (const char *)mixer->card_info.name;
343}
344
David Lie95e65a2020-12-09 02:11:18 +0000345unsigned int mixer_get_num_ctls(struct mixer *mixer)
David Lie0f41062020-11-05 12:27:29 +0000346{
347 if (!mixer)
348 return 0;
349
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700350 return mixer->total_ctl_count;
351}
352
353static int mixer_grp_get_count(struct mixer_ctl_group *grp)
354{
355 if (!grp)
356 return 0;
357
358 return grp->count;
David Lie0f41062020-11-05 12:27:29 +0000359}
360
David Lie0f41062020-11-05 12:27:29 +0000361struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
362{
David Lie95e65a2020-12-09 02:11:18 +0000363 struct mixer_ctl *ctl;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700364 unsigned int hw_ctl_count, virt_ctl_count;
David Lie0f41062020-11-05 12:27:29 +0000365
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700366 if (!mixer || (id >= mixer->total_ctl_count))
David Lie0f41062020-11-05 12:27:29 +0000367 return NULL;
368
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700369 hw_ctl_count = mixer_grp_get_count(mixer->hw_grp);
370 virt_ctl_count = mixer_grp_get_count(mixer->virt_grp);
371
372 if (id < hw_ctl_count)
373 ctl = mixer->hw_grp->ctl + id;
374 else if ((id - hw_ctl_count) < virt_ctl_count)
375 ctl = mixer->virt_grp->ctl + (id - hw_ctl_count);
376 else
377 return NULL;
378
David Lie95e65a2020-12-09 02:11:18 +0000379 if (!mixer_ctl_get_elem_info(ctl))
380 return NULL;
David Lie0f41062020-11-05 12:27:29 +0000381
David Lie95e65a2020-12-09 02:11:18 +0000382 return ctl;
David Lie0f41062020-11-05 12:27:29 +0000383}
384
David Lie0f41062020-11-05 12:27:29 +0000385struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
386{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700387 struct mixer_ctl_group *grp;
David Lie0f41062020-11-05 12:27:29 +0000388 unsigned int n;
Akhil Karuturia9724402020-01-17 13:53:14 -0800389 int hw_ctl_count;
David Lie0f41062020-11-05 12:27:29 +0000390
391 if (!mixer)
392 return NULL;
393
Akhil Karuturia9724402020-01-17 13:53:14 -0800394 hw_ctl_count = mixer_grp_get_count(mixer->hw_grp);
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700395 if (mixer->hw_grp) {
396 grp = mixer->hw_grp;
397
398 for (n = 0; n < grp->count; n++)
399 if (!strcmp(name, (char*) grp->elem_info[n].id.name))
400 return mixer_get_ctl(mixer, n);
401 }
402
403 if (mixer->virt_grp) {
404 grp = mixer->virt_grp;
405
406 for (n = 0; n < grp->count; n++)
407 if (!strcmp(name, (char*) grp->elem_info[n].id.name))
408 return mixer_get_ctl(mixer, n + hw_ctl_count);
409 }
David Lie0f41062020-11-05 12:27:29 +0000410
David Lie0f41062020-11-05 12:27:29 +0000411 return NULL;
412}
413
David Lie0f41062020-11-05 12:27:29 +0000414void mixer_ctl_update(struct mixer_ctl *ctl)
415{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700416 struct mixer_ctl_group *grp;
417
418 if (!ctl || !ctl->grp)
419 return;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700420
Akhil Karuturia9724402020-01-17 13:53:14 -0800421 grp = ctl->grp;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700422 grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
David Lie0f41062020-11-05 12:27:29 +0000423}
424
David Lie95e65a2020-12-09 02:11:18 +0000425const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700426{
Simon Wilsone44e30a2012-03-08 10:45:31 -0800427 if (!ctl)
428 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700429
David Lie95e65a2020-12-09 02:11:18 +0000430 return (const char *)ctl->info->id.name;
Simon Wilsonedff7082011-06-06 15:33:34 -0700431}
432
David Lie95e65a2020-12-09 02:11:18 +0000433enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700434{
435 if (!ctl)
436 return MIXER_CTL_TYPE_UNKNOWN;
437
David Lie95e65a2020-12-09 02:11:18 +0000438 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700439 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
440 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
441 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
442 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
443 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
444 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
445 default: return MIXER_CTL_TYPE_UNKNOWN;
446 };
447}
448
David Lie95e65a2020-12-09 02:11:18 +0000449const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700450{
451 if (!ctl)
452 return "";
453
David Lie95e65a2020-12-09 02:11:18 +0000454 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700455 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
456 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
457 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
458 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
459 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
460 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
461 default: return "Unknown";
462 };
463}
464
David Lie95e65a2020-12-09 02:11:18 +0000465unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700466{
467 if (!ctl)
468 return 0;
469
David Lie95e65a2020-12-09 02:11:18 +0000470 return ctl->info->count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700471}
472
David Lie95e65a2020-12-09 02:11:18 +0000473static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
Simon Wilsonedff7082011-06-06 15:33:34 -0700474{
David Lie95e65a2020-12-09 02:11:18 +0000475 int range;
Simon Wilsonedff7082011-06-06 15:33:34 -0700476
David Lie95e65a2020-12-09 02:11:18 +0000477 if (percent > 100)
478 percent = 100;
479 else if (percent < 0)
480 percent = 0;
481
482 range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilsonedff7082011-06-06 15:33:34 -0700483
484 return ei->value.integer.min + (range * percent) / 100;
485}
486
David Lie95e65a2020-12-09 02:11:18 +0000487static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
Simon Wilsonedff7082011-06-06 15:33:34 -0700488{
489 int range = (ei->value.integer.max - ei->value.integer.min);
490
491 if (range == 0)
492 return 0;
493
David Lie95e65a2020-12-09 02:11:18 +0000494 return ((value - ei->value.integer.min) / range) * 100;
Simon Wilsonedff7082011-06-06 15:33:34 -0700495}
496
David Lie95e65a2020-12-09 02:11:18 +0000497int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700498{
David Lie95e65a2020-12-09 02:11:18 +0000499 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700500 return -EINVAL;
501
David Lie95e65a2020-12-09 02:11:18 +0000502 return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsonedff7082011-06-06 15:33:34 -0700503}
504
505int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
506{
David Lie95e65a2020-12-09 02:11:18 +0000507 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700508 return -EINVAL;
509
David Lie95e65a2020-12-09 02:11:18 +0000510 return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
Simon Wilsonedff7082011-06-06 15:33:34 -0700511}
512
David Lie95e65a2020-12-09 02:11:18 +0000513int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700514{
Akhil Karuturia9724402020-01-17 13:53:14 -0800515 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700516 struct snd_ctl_elem_value ev;
517 int ret;
518
Akhil Karuturia9724402020-01-17 13:53:14 -0800519 if (!ctl || (id >= ctl->info->count) || !ctl->grp)
Simon Wilsonedff7082011-06-06 15:33:34 -0700520 return -EINVAL;
521
Akhil Karuturia9724402020-01-17 13:53:14 -0800522 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700523 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000524 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700525 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700526 if (ret < 0)
527 return ret;
528
David Lie95e65a2020-12-09 02:11:18 +0000529 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700530 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
531 return !!ev.value.integer.value[id];
532
533 case SNDRV_CTL_ELEM_TYPE_INTEGER:
534 return ev.value.integer.value[id];
535
536 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
537 return ev.value.enumerated.item[id];
538
Simon Wilson5aed71d2011-11-16 14:45:38 -0800539 case SNDRV_CTL_ELEM_TYPE_BYTES:
540 return ev.value.bytes.data[id];
541
Simon Wilsonedff7082011-06-06 15:33:34 -0700542 default:
543 return -EINVAL;
544 }
545
546 return 0;
547}
548
David Lie95e65a2020-12-09 02:11:18 +0000549int mixer_ctl_is_access_tlv_rw(struct mixer_ctl *ctl)
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530550{
David Lie95e65a2020-12-09 02:11:18 +0000551 return (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
552}
553
554int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
555{
Akhil Karuturia9724402020-01-17 13:53:14 -0800556 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800557 struct snd_ctl_elem_value ev;
Mythri P K67b1df42014-08-18 15:39:36 +0530558 int ret = 0;
Simon Wilson8813fe82013-06-24 15:40:34 -0700559 size_t size;
560 void *source;
David Lie95e65a2020-12-09 02:11:18 +0000561 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800562
Akhil Karuturia9724402020-01-17 13:53:14 -0800563 if ((!ctl) || !count || !array || !ctl->grp)
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530564 return -EINVAL;
565
Akhil Karuturia9724402020-01-17 13:53:14 -0800566 grp = ctl->grp;
David Lie95e65a2020-12-09 02:11:18 +0000567 total_count = ctl->info->count;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530568
David Lie95e65a2020-12-09 02:11:18 +0000569 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
570 mixer_ctl_is_access_tlv_rw(ctl)) {
571 /* Additional two words is for the TLV header */
572 total_count += TLV_HEADER_SIZE;
573 }
574
575 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800576 return -EINVAL;
577
578 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000579 ev.id.numid = ctl->info->id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800580
David Lie95e65a2020-12-09 02:11:18 +0000581 switch (ctl->info->type) {
Simon Wilson8813fe82013-06-24 15:40:34 -0700582 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
583 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700584 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530585 if (ret < 0)
586 return ret;
Simon Wilson8813fe82013-06-24 15:40:34 -0700587 size = sizeof(ev.value.integer.value[0]);
588 source = ev.value.integer.value;
589 break;
590
591 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530592 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530593 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530594 struct snd_ctl_tlv *tlv;
595 int ret;
596
Ben Zhang6cb14f22016-04-22 17:59:40 -0700597 if (count > SIZE_MAX - sizeof(*tlv))
598 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530599 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700600 if (!tlv)
601 return -ENOMEM;
David Lie95e65a2020-12-09 02:11:18 +0000602 tlv->numid = ctl->info->id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +0530603 tlv->length = count;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700604 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530605
606 source = tlv->tlv;
607 memcpy(array, source, count);
608
609 free(tlv);
610
611 return ret;
612 } else {
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700613 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530614 if (ret < 0)
615 return ret;
616 size = sizeof(ev.value.bytes.data[0]);
617 source = ev.value.bytes.data;
618 break;
619 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700620
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530621 case SNDRV_CTL_ELEM_TYPE_IEC958:
622 size = sizeof(ev.value.iec958);
623 source = &ev.value.iec958;
624 break;
625
Simon Wilson8813fe82013-06-24 15:40:34 -0700626 default:
627 return -EINVAL;
628 }
629
630 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800631
632 return 0;
633}
634
Simon Wilsonedff7082011-06-06 15:33:34 -0700635int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
636{
Akhil Karuturia9724402020-01-17 13:53:14 -0800637 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700638 struct snd_ctl_elem_value ev;
639 int ret;
640
Akhil Karuturia9724402020-01-17 13:53:14 -0800641 if (!ctl || (id >= ctl->info->count) || !ctl->grp)
Simon Wilsonedff7082011-06-06 15:33:34 -0700642 return -EINVAL;
643
Akhil Karuturia9724402020-01-17 13:53:14 -0800644 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700645 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000646 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700647 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700648 if (ret < 0)
649 return ret;
650
David Lie95e65a2020-12-09 02:11:18 +0000651 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700652 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
653 ev.value.integer.value[id] = !!value;
654 break;
655
656 case SNDRV_CTL_ELEM_TYPE_INTEGER:
657 ev.value.integer.value[id] = value;
658 break;
659
660 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
661 ev.value.enumerated.item[id] = value;
662 break;
663
David.Coutheruta8481aa2013-06-11 16:22:57 +0200664 case SNDRV_CTL_ELEM_TYPE_BYTES:
665 ev.value.bytes.data[id] = value;
666 break;
667
Simon Wilsonedff7082011-06-06 15:33:34 -0700668 default:
669 return -EINVAL;
670 }
671
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700672 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700673}
674
Simon Wilson8813fe82013-06-24 15:40:34 -0700675int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800676{
Akhil Karuturia9724402020-01-17 13:53:14 -0800677 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800678 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -0700679 size_t size;
680 void *dest;
David Lie95e65a2020-12-09 02:11:18 +0000681 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800682
Akhil Karuturia9724402020-01-17 13:53:14 -0800683 if ((!ctl) || !count || !array || !ctl->grp)
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530684 return -EINVAL;
685
Akhil Karuturia9724402020-01-17 13:53:14 -0800686 grp = ctl->grp;
David Lie95e65a2020-12-09 02:11:18 +0000687 total_count = ctl->info->count;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530688
David Lie95e65a2020-12-09 02:11:18 +0000689 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
690 mixer_ctl_is_access_tlv_rw(ctl)) {
691 /* Additional two words is for the TLV header */
692 total_count += TLV_HEADER_SIZE;
693 }
694
695 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800696 return -EINVAL;
697
698 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000699 ev.id.numid = ctl->info->id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800700
David Lie95e65a2020-12-09 02:11:18 +0000701 switch (ctl->info->type) {
Simon Wilson8813fe82013-06-24 15:40:34 -0700702 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
703 case SNDRV_CTL_ELEM_TYPE_INTEGER:
704 size = sizeof(ev.value.integer.value[0]);
705 dest = ev.value.integer.value;
706 break;
707
708 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530709 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530710 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530711 struct snd_ctl_tlv *tlv;
712 int ret = 0;
Ben Zhang6cb14f22016-04-22 17:59:40 -0700713 if (count > SIZE_MAX - sizeof(*tlv))
714 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530715 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700716 if (!tlv)
717 return -ENOMEM;
David Lie95e65a2020-12-09 02:11:18 +0000718 tlv->numid = ctl->info->id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +0530719 tlv->length = count;
720 memcpy(tlv->tlv, array, count);
721
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700722 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530723 free(tlv);
724
725 return ret;
726 } else {
727 size = sizeof(ev.value.bytes.data[0]);
728 dest = ev.value.bytes.data;
729 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700730 break;
731
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530732 case SNDRV_CTL_ELEM_TYPE_IEC958:
733 size = sizeof(ev.value.iec958);
734 dest = &ev.value.iec958;
735 break;
736
Simon Wilson8813fe82013-06-24 15:40:34 -0700737 default:
738 return -EINVAL;
739 }
740
741 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800742
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700743 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800744}
745
David Lie95e65a2020-12-09 02:11:18 +0000746int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700747{
David Lie95e65a2020-12-09 02:11:18 +0000748 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700749 return -EINVAL;
750
David Lie95e65a2020-12-09 02:11:18 +0000751 return ctl->info->value.integer.min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700752}
753
David Lie95e65a2020-12-09 02:11:18 +0000754int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700755{
David Lie95e65a2020-12-09 02:11:18 +0000756 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700757 return -EINVAL;
758
David Lie95e65a2020-12-09 02:11:18 +0000759 return ctl->info->value.integer.max;
Simon Wilsonedff7082011-06-06 15:33:34 -0700760}
761
David Lie95e65a2020-12-09 02:11:18 +0000762unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700763{
764 if (!ctl)
765 return 0;
766
David Lie95e65a2020-12-09 02:11:18 +0000767 return ctl->info->value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -0700768}
769
Simon Wilsone44e30a2012-03-08 10:45:31 -0800770const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
771 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700772{
David Lie95e65a2020-12-09 02:11:18 +0000773 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
774 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsone44e30a2012-03-08 10:45:31 -0800775 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700776
Simon Wilsone44e30a2012-03-08 10:45:31 -0800777 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -0700778}
779
780int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
781{
Akhil Karuturia9724402020-01-17 13:53:14 -0800782 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700783 unsigned int i, num_enums;
784 struct snd_ctl_elem_value ev;
785 int ret;
786
Akhil Karuturia9724402020-01-17 13:53:14 -0800787 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || !ctl->grp)
Simon Wilsonedff7082011-06-06 15:33:34 -0700788 return -EINVAL;
789
Akhil Karuturia9724402020-01-17 13:53:14 -0800790 grp = ctl->grp;
David Lie95e65a2020-12-09 02:11:18 +0000791 num_enums = ctl->info->value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -0700792 for (i = 0; i < num_enums; i++) {
793 if (!strcmp(string, ctl->ename[i])) {
794 memset(&ev, 0, sizeof(ev));
795 ev.value.enumerated.item[0] = i;
David Lie95e65a2020-12-09 02:11:18 +0000796 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700797 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700798 if (ret < 0)
799 return ret;
800 return 0;
801 }
802 }
803
804 return -EINVAL;
805}
David Lie95e65a2020-12-09 02:11:18 +0000806
807/** Subscribes for the mixer events.
808 * @param mixer A mixer handle.
809 * @param subscribe value indicating subscribe or unsubscribe for events
810 * @returns On success, zero.
811 * On failure, non-zero.
812 * @ingroup libtinyalsa-mixer
813 */
814int mixer_subscribe_events(struct mixer *mixer, int subscribe)
815{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700816 struct mixer_ctl_group *grp;
817
818 if (mixer->hw_grp) {
819 grp = mixer->hw_grp;
820 if (!subscribe)
821 grp->event_cnt = 0;
822 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
823 return -1;
824 }
825
826 if (mixer->virt_grp) {
827 grp = mixer->virt_grp;
828 if (!subscribe)
829 grp->event_cnt = 0;
830 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
831 return -1;
David Lie95e65a2020-12-09 02:11:18 +0000832 }
833 return 0;
834}
835
836/** Wait for mixer events.
837 * @param mixer A mixer handle.
838 * @param timeout timout value
839 * @returns On success, 1.
840 * On failure, -errno.
841 * On timeout, 0
842 * @ingroup libtinyalsa-mixer
843 */
844int mixer_wait_event(struct mixer *mixer, int timeout)
845{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700846 struct pollfd pfd[2];
847 struct mixer_ctl_group *grp;
848 int count = 0, num_fds = 0, i;
David Lie95e65a2020-12-09 02:11:18 +0000849
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700850 memset(pfd, 0, sizeof(struct pollfd) * 2);
851
852 if (mixer->fd >= 0)
853 num_fds++;
854
855 if (mixer->virt_grp)
856 num_fds++;
857
858
859 /* TODO wait events for virt fd */
860 if (mixer->fd >= 0) {
861 pfd[count].fd = mixer->fd;
862 pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
863 count++;
864 }
865
866 if (mixer->virt_grp) {
867 grp = mixer->virt_grp;
868 if (!grp->ops->get_poll_fd(grp->data, pfd, count)) {
869 pfd[count].events = POLLIN | POLLERR | POLLNVAL;
870 count++;
871 }
872 }
873
874 if (!count)
875 return 0;
David Lie95e65a2020-12-09 02:11:18 +0000876
877 for (;;) {
878 int err;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700879 err = poll(pfd, count, timeout);
David Lie95e65a2020-12-09 02:11:18 +0000880 if (err < 0)
881 return -errno;
882 if (!err)
883 return 0;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700884 for (i = 0; i < count; i++) {
885 if (pfd[i].revents & (POLLERR | POLLNVAL))
886 return -EIO;
887 if (pfd[i].revents & (POLLIN | POLLOUT)) {
888 if ((i == 0) && mixer->fd >= 0) {
889 grp = mixer->hw_grp;
890 grp->event_cnt++;
891 } else {
892 grp = mixer->virt_grp;
893 grp->event_cnt++;
894 }
895 return 1;
896 }
897 }
David Lie95e65a2020-12-09 02:11:18 +0000898 }
899}
900
901/** Consume a mixer event.
902 * If mixer_subscribe_events has been called,
903 * mixer_wait_event will identify when a control value has changed.
904 * This function will clear a single event from the mixer so that
905 * further events can be alerted.
906 *
907 * @param mixer A mixer handle.
908 * @returns 0 on success. -errno on failure.
909 * @ingroup libtinyalsa-mixer
910 */
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700911int mixer_consume_event(struct mixer *mixer)
912{
David Lie95e65a2020-12-09 02:11:18 +0000913 struct snd_ctl_event ev;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700914 struct mixer_ctl_group *grp;
915 ssize_t count = 0;
916
David Lie95e65a2020-12-09 02:11:18 +0000917 // Exporting the actual event would require exposing snd_ctl_event
918 // via the header file, and all associated structs.
919 // The events generally tell you exactly which value changed,
920 // but reading values you're interested isn't hard and simplifies
921 // the interface greatly.
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700922 if (mixer->hw_grp) {
923 grp = mixer->hw_grp;
924 if (grp->event_cnt) {
925 grp->event_cnt--;
926 count = grp->ops->read_event(grp->data, &ev, sizeof(ev));
927 return (count >= 0) ? 0 : -errno;
928 }
929 }
930
931 if (mixer->virt_grp) {
932 grp = mixer->virt_grp;
933 if (grp->event_cnt) {
934 grp->event_cnt--;
935 count += grp->ops->read_event(grp->data, &ev, sizeof(ev));
936 return (count >= 0) ? 0 : -errno;
937 }
938 }
939 return 0;
940}
941
942/** Read a mixer event.
943 * If mixer_subscribe_events has been called,
944 * mixer_wait_event will identify when a control value has changed.
945 * This function will read and clear a single event from the mixer
946 * so that further events can be alerted.
947 *
948 * @param mixer A mixer handle.
949 * @param ev snd_ctl_event pointer where event needs to be read
950 * @returns 0 on success. -errno on failure.
951 * @ingroup libtinyalsa-mixer
952 */
953int mixer_read_event(struct mixer *mixer, struct ctl_event *ev)
954{
955 struct mixer_ctl_group *grp;
956 ssize_t count = 0;
957
958 if (mixer->hw_grp) {
959 grp = mixer->hw_grp;
960 if (grp->event_cnt) {
961 grp->event_cnt--;
962 count = grp->ops->read_event(grp->data, (struct snd_ctl_event *)ev, sizeof(*ev));
963 return (count >= 0) ? 0 : -errno;
964 }
965 }
966
967 if (mixer->virt_grp) {
968 grp = mixer->virt_grp;
969 if (grp->event_cnt) {
970 grp->event_cnt--;
971 count = grp->ops->read_event(grp->data, (struct snd_ctl_event *)ev, sizeof(*ev));
972 return (count >= 0) ? 0 : -errno;
973 }
974 }
975 return 0;
David Lie95e65a2020-12-09 02:11:18 +0000976}