blob: fd0ad8d673c6cb24729fddd85f965a9b7ef9b677 [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:
David Li10c9ed02022-01-19 02:37:27 +0000622 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
623 if (ret < 0)
624 return ret;
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530625 size = sizeof(ev.value.iec958);
626 source = &ev.value.iec958;
627 break;
628
Simon Wilson8813fe82013-06-24 15:40:34 -0700629 default:
630 return -EINVAL;
631 }
632
633 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800634
635 return 0;
636}
637
Simon Wilsonedff7082011-06-06 15:33:34 -0700638int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
639{
Akhil Karuturia9724402020-01-17 13:53:14 -0800640 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700641 struct snd_ctl_elem_value ev;
642 int ret;
643
Akhil Karuturia9724402020-01-17 13:53:14 -0800644 if (!ctl || (id >= ctl->info->count) || !ctl->grp)
Simon Wilsonedff7082011-06-06 15:33:34 -0700645 return -EINVAL;
646
Akhil Karuturia9724402020-01-17 13:53:14 -0800647 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700648 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000649 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700650 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700651 if (ret < 0)
652 return ret;
653
David Lie95e65a2020-12-09 02:11:18 +0000654 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700655 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
656 ev.value.integer.value[id] = !!value;
657 break;
658
659 case SNDRV_CTL_ELEM_TYPE_INTEGER:
660 ev.value.integer.value[id] = value;
661 break;
662
663 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
664 ev.value.enumerated.item[id] = value;
665 break;
666
David.Coutheruta8481aa2013-06-11 16:22:57 +0200667 case SNDRV_CTL_ELEM_TYPE_BYTES:
668 ev.value.bytes.data[id] = value;
669 break;
670
Simon Wilsonedff7082011-06-06 15:33:34 -0700671 default:
672 return -EINVAL;
673 }
674
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700675 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700676}
677
Simon Wilson8813fe82013-06-24 15:40:34 -0700678int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800679{
Akhil Karuturia9724402020-01-17 13:53:14 -0800680 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800681 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -0700682 size_t size;
683 void *dest;
David Lie95e65a2020-12-09 02:11:18 +0000684 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800685
Akhil Karuturia9724402020-01-17 13:53:14 -0800686 if ((!ctl) || !count || !array || !ctl->grp)
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530687 return -EINVAL;
688
Akhil Karuturia9724402020-01-17 13:53:14 -0800689 grp = ctl->grp;
David Lie95e65a2020-12-09 02:11:18 +0000690 total_count = ctl->info->count;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530691
David Lie95e65a2020-12-09 02:11:18 +0000692 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
693 mixer_ctl_is_access_tlv_rw(ctl)) {
694 /* Additional two words is for the TLV header */
695 total_count += TLV_HEADER_SIZE;
696 }
697
698 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800699 return -EINVAL;
700
701 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000702 ev.id.numid = ctl->info->id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800703
David Lie95e65a2020-12-09 02:11:18 +0000704 switch (ctl->info->type) {
Simon Wilson8813fe82013-06-24 15:40:34 -0700705 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
706 case SNDRV_CTL_ELEM_TYPE_INTEGER:
707 size = sizeof(ev.value.integer.value[0]);
708 dest = ev.value.integer.value;
709 break;
710
711 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530712 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530713 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530714 struct snd_ctl_tlv *tlv;
715 int ret = 0;
Ben Zhang6cb14f22016-04-22 17:59:40 -0700716 if (count > SIZE_MAX - sizeof(*tlv))
717 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530718 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700719 if (!tlv)
720 return -ENOMEM;
David Lie95e65a2020-12-09 02:11:18 +0000721 tlv->numid = ctl->info->id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +0530722 tlv->length = count;
723 memcpy(tlv->tlv, array, count);
724
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700725 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530726 free(tlv);
727
728 return ret;
729 } else {
730 size = sizeof(ev.value.bytes.data[0]);
731 dest = ev.value.bytes.data;
732 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700733 break;
734
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530735 case SNDRV_CTL_ELEM_TYPE_IEC958:
736 size = sizeof(ev.value.iec958);
737 dest = &ev.value.iec958;
738 break;
739
Simon Wilson8813fe82013-06-24 15:40:34 -0700740 default:
741 return -EINVAL;
742 }
743
744 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800745
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700746 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800747}
748
David Lie95e65a2020-12-09 02:11:18 +0000749int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700750{
David Lie95e65a2020-12-09 02:11:18 +0000751 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700752 return -EINVAL;
753
David Lie95e65a2020-12-09 02:11:18 +0000754 return ctl->info->value.integer.min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700755}
756
David Lie95e65a2020-12-09 02:11:18 +0000757int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700758{
David Lie95e65a2020-12-09 02:11:18 +0000759 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700760 return -EINVAL;
761
David Lie95e65a2020-12-09 02:11:18 +0000762 return ctl->info->value.integer.max;
Simon Wilsonedff7082011-06-06 15:33:34 -0700763}
764
David Lie95e65a2020-12-09 02:11:18 +0000765unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700766{
767 if (!ctl)
768 return 0;
769
David Lie95e65a2020-12-09 02:11:18 +0000770 return ctl->info->value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -0700771}
772
Simon Wilsone44e30a2012-03-08 10:45:31 -0800773const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
774 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700775{
David Lie95e65a2020-12-09 02:11:18 +0000776 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
777 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsone44e30a2012-03-08 10:45:31 -0800778 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700779
Simon Wilsone44e30a2012-03-08 10:45:31 -0800780 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -0700781}
782
783int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
784{
Akhil Karuturia9724402020-01-17 13:53:14 -0800785 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700786 unsigned int i, num_enums;
787 struct snd_ctl_elem_value ev;
788 int ret;
789
Akhil Karuturia9724402020-01-17 13:53:14 -0800790 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || !ctl->grp)
Simon Wilsonedff7082011-06-06 15:33:34 -0700791 return -EINVAL;
792
Akhil Karuturia9724402020-01-17 13:53:14 -0800793 grp = ctl->grp;
David Lie95e65a2020-12-09 02:11:18 +0000794 num_enums = ctl->info->value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -0700795 for (i = 0; i < num_enums; i++) {
796 if (!strcmp(string, ctl->ename[i])) {
797 memset(&ev, 0, sizeof(ev));
798 ev.value.enumerated.item[0] = i;
David Lie95e65a2020-12-09 02:11:18 +0000799 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700800 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700801 if (ret < 0)
802 return ret;
803 return 0;
804 }
805 }
806
807 return -EINVAL;
808}
David Lie95e65a2020-12-09 02:11:18 +0000809
810/** Subscribes for the mixer events.
811 * @param mixer A mixer handle.
812 * @param subscribe value indicating subscribe or unsubscribe for events
813 * @returns On success, zero.
814 * On failure, non-zero.
815 * @ingroup libtinyalsa-mixer
816 */
817int mixer_subscribe_events(struct mixer *mixer, int subscribe)
818{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700819 struct mixer_ctl_group *grp;
820
821 if (mixer->hw_grp) {
822 grp = mixer->hw_grp;
823 if (!subscribe)
824 grp->event_cnt = 0;
825 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
826 return -1;
827 }
828
829 if (mixer->virt_grp) {
830 grp = mixer->virt_grp;
831 if (!subscribe)
832 grp->event_cnt = 0;
833 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
834 return -1;
David Lie95e65a2020-12-09 02:11:18 +0000835 }
836 return 0;
837}
838
839/** Wait for mixer events.
840 * @param mixer A mixer handle.
841 * @param timeout timout value
842 * @returns On success, 1.
843 * On failure, -errno.
844 * On timeout, 0
845 * @ingroup libtinyalsa-mixer
846 */
847int mixer_wait_event(struct mixer *mixer, int timeout)
848{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700849 struct pollfd pfd[2];
850 struct mixer_ctl_group *grp;
851 int count = 0, num_fds = 0, i;
David Lie95e65a2020-12-09 02:11:18 +0000852
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700853 memset(pfd, 0, sizeof(struct pollfd) * 2);
854
855 if (mixer->fd >= 0)
856 num_fds++;
857
858 if (mixer->virt_grp)
859 num_fds++;
860
861
862 /* TODO wait events for virt fd */
863 if (mixer->fd >= 0) {
864 pfd[count].fd = mixer->fd;
865 pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
866 count++;
867 }
868
869 if (mixer->virt_grp) {
870 grp = mixer->virt_grp;
871 if (!grp->ops->get_poll_fd(grp->data, pfd, count)) {
872 pfd[count].events = POLLIN | POLLERR | POLLNVAL;
873 count++;
874 }
875 }
876
877 if (!count)
878 return 0;
David Lie95e65a2020-12-09 02:11:18 +0000879
880 for (;;) {
881 int err;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700882 err = poll(pfd, count, timeout);
David Lie95e65a2020-12-09 02:11:18 +0000883 if (err < 0)
884 return -errno;
885 if (!err)
886 return 0;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700887 for (i = 0; i < count; i++) {
888 if (pfd[i].revents & (POLLERR | POLLNVAL))
889 return -EIO;
890 if (pfd[i].revents & (POLLIN | POLLOUT)) {
891 if ((i == 0) && mixer->fd >= 0) {
892 grp = mixer->hw_grp;
893 grp->event_cnt++;
894 } else {
895 grp = mixer->virt_grp;
896 grp->event_cnt++;
897 }
898 return 1;
899 }
900 }
David Lie95e65a2020-12-09 02:11:18 +0000901 }
902}
903
904/** Consume a mixer event.
905 * If mixer_subscribe_events has been called,
906 * mixer_wait_event will identify when a control value has changed.
907 * This function will clear a single event from the mixer so that
908 * further events can be alerted.
909 *
910 * @param mixer A mixer handle.
911 * @returns 0 on success. -errno on failure.
912 * @ingroup libtinyalsa-mixer
913 */
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700914int mixer_consume_event(struct mixer *mixer)
915{
David Lie95e65a2020-12-09 02:11:18 +0000916 struct snd_ctl_event ev;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700917 struct mixer_ctl_group *grp;
918 ssize_t count = 0;
919
David Lie95e65a2020-12-09 02:11:18 +0000920 // Exporting the actual event would require exposing snd_ctl_event
921 // via the header file, and all associated structs.
922 // The events generally tell you exactly which value changed,
923 // but reading values you're interested isn't hard and simplifies
924 // the interface greatly.
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700925 if (mixer->hw_grp) {
926 grp = mixer->hw_grp;
927 if (grp->event_cnt) {
928 grp->event_cnt--;
929 count = grp->ops->read_event(grp->data, &ev, sizeof(ev));
930 return (count >= 0) ? 0 : -errno;
931 }
932 }
933
934 if (mixer->virt_grp) {
935 grp = mixer->virt_grp;
936 if (grp->event_cnt) {
937 grp->event_cnt--;
938 count += grp->ops->read_event(grp->data, &ev, sizeof(ev));
939 return (count >= 0) ? 0 : -errno;
940 }
941 }
942 return 0;
943}
944
945/** Read a mixer event.
946 * If mixer_subscribe_events has been called,
947 * mixer_wait_event will identify when a control value has changed.
948 * This function will read and clear a single event from the mixer
949 * so that further events can be alerted.
950 *
951 * @param mixer A mixer handle.
952 * @param ev snd_ctl_event pointer where event needs to be read
953 * @returns 0 on success. -errno on failure.
954 * @ingroup libtinyalsa-mixer
955 */
956int mixer_read_event(struct mixer *mixer, struct ctl_event *ev)
957{
958 struct mixer_ctl_group *grp;
959 ssize_t count = 0;
960
961 if (mixer->hw_grp) {
962 grp = mixer->hw_grp;
963 if (grp->event_cnt) {
964 grp->event_cnt--;
965 count = grp->ops->read_event(grp->data, (struct snd_ctl_event *)ev, sizeof(*ev));
966 return (count >= 0) ? 0 : -errno;
967 }
968 }
969
970 if (mixer->virt_grp) {
971 grp = mixer->virt_grp;
972 if (grp->event_cnt) {
973 grp->event_cnt--;
974 count = grp->ops->read_event(grp->data, (struct snd_ctl_event *)ev, sizeof(*ev));
975 return (count >= 0) ? 0 : -errno;
976 }
977 }
978 return 0;
David Lie95e65a2020-12-09 02:11:18 +0000979}