blob: 87747c5a87ae98df1809204f79f87f54cb21f8b5 [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{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700300 struct mixer_ctl_group *grp = ctl->grp;
301 unsigned int i;
302
David Lie95e65a2020-12-09 02:11:18 +0000303 if (!ctl->info_retrieved) {
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700304 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO,
305 ctl->info) < 0)
David Lie95e65a2020-12-09 02:11:18 +0000306 return false;
307 ctl->info_retrieved = true;
308 }
309
310 if (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED || ctl->ename)
311 return true;
312
313 struct snd_ctl_elem_info tmp;
314 char** enames = calloc(ctl->info->value.enumerated.items, sizeof(char*));
315 if (!enames)
316 return false;
317
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700318 for (i = 0; i < ctl->info->value.enumerated.items; i++) {
David Lie95e65a2020-12-09 02:11:18 +0000319 memset(&tmp, 0, sizeof(tmp));
320 tmp.id.numid = ctl->info->id.numid;
321 tmp.value.enumerated.item = i;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700322 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
David Lie95e65a2020-12-09 02:11:18 +0000323 goto fail;
324 enames[i] = strdup(tmp.value.enumerated.name);
325 if (!enames[i])
326 goto fail;
327 }
328 ctl->ename = enames;
329 return true;
330
331fail:
332 free(enames);
333 return false;
334}
335
336const char *mixer_get_name(struct mixer *mixer)
David Lie0f41062020-11-05 12:27:29 +0000337{
338 return (const char *)mixer->card_info.name;
339}
340
David Lie95e65a2020-12-09 02:11:18 +0000341unsigned int mixer_get_num_ctls(struct mixer *mixer)
David Lie0f41062020-11-05 12:27:29 +0000342{
343 if (!mixer)
344 return 0;
345
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700346 return mixer->total_ctl_count;
347}
348
349static int mixer_grp_get_count(struct mixer_ctl_group *grp)
350{
351 if (!grp)
352 return 0;
353
354 return grp->count;
David Lie0f41062020-11-05 12:27:29 +0000355}
356
David Lie0f41062020-11-05 12:27:29 +0000357struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
358{
David Lie95e65a2020-12-09 02:11:18 +0000359 struct mixer_ctl *ctl;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700360 unsigned int hw_ctl_count, virt_ctl_count;
David Lie0f41062020-11-05 12:27:29 +0000361
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700362 if (!mixer || (id >= mixer->total_ctl_count))
David Lie0f41062020-11-05 12:27:29 +0000363 return NULL;
364
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700365 hw_ctl_count = mixer_grp_get_count(mixer->hw_grp);
366 virt_ctl_count = mixer_grp_get_count(mixer->virt_grp);
367
368 if (id < hw_ctl_count)
369 ctl = mixer->hw_grp->ctl + id;
370 else if ((id - hw_ctl_count) < virt_ctl_count)
371 ctl = mixer->virt_grp->ctl + (id - hw_ctl_count);
372 else
373 return NULL;
374
David Lie95e65a2020-12-09 02:11:18 +0000375 if (!mixer_ctl_get_elem_info(ctl))
376 return NULL;
David Lie0f41062020-11-05 12:27:29 +0000377
David Lie95e65a2020-12-09 02:11:18 +0000378 return ctl;
David Lie0f41062020-11-05 12:27:29 +0000379}
380
David Lie0f41062020-11-05 12:27:29 +0000381struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
382{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700383 struct mixer_ctl_group *grp;
David Lie0f41062020-11-05 12:27:29 +0000384 unsigned int n;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700385 int hw_ctl_count = mixer_grp_get_count(mixer->hw_grp);
David Lie0f41062020-11-05 12:27:29 +0000386
387 if (!mixer)
388 return NULL;
389
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700390 if (mixer->hw_grp) {
391 grp = mixer->hw_grp;
392
393 for (n = 0; n < grp->count; n++)
394 if (!strcmp(name, (char*) grp->elem_info[n].id.name))
395 return mixer_get_ctl(mixer, n);
396 }
397
398 if (mixer->virt_grp) {
399 grp = mixer->virt_grp;
400
401 for (n = 0; n < grp->count; n++)
402 if (!strcmp(name, (char*) grp->elem_info[n].id.name))
403 return mixer_get_ctl(mixer, n + hw_ctl_count);
404 }
David Lie0f41062020-11-05 12:27:29 +0000405
David Lie0f41062020-11-05 12:27:29 +0000406 return NULL;
407}
408
David Lie0f41062020-11-05 12:27:29 +0000409void mixer_ctl_update(struct mixer_ctl *ctl)
410{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700411 struct mixer_ctl_group *grp;
412
413 if (!ctl || !ctl->grp)
414 return;
415 grp = ctl->grp;
416
417 grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
David Lie0f41062020-11-05 12:27:29 +0000418}
419
David Lie95e65a2020-12-09 02:11:18 +0000420const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700421{
Simon Wilsone44e30a2012-03-08 10:45:31 -0800422 if (!ctl)
423 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700424
David Lie95e65a2020-12-09 02:11:18 +0000425 return (const char *)ctl->info->id.name;
Simon Wilsonedff7082011-06-06 15:33:34 -0700426}
427
David Lie95e65a2020-12-09 02:11:18 +0000428enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700429{
430 if (!ctl)
431 return MIXER_CTL_TYPE_UNKNOWN;
432
David Lie95e65a2020-12-09 02:11:18 +0000433 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700434 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
435 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
436 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
437 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
438 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
439 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
440 default: return MIXER_CTL_TYPE_UNKNOWN;
441 };
442}
443
David Lie95e65a2020-12-09 02:11:18 +0000444const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700445{
446 if (!ctl)
447 return "";
448
David Lie95e65a2020-12-09 02:11:18 +0000449 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700450 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
451 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
452 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
453 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
454 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
455 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
456 default: return "Unknown";
457 };
458}
459
David Lie95e65a2020-12-09 02:11:18 +0000460unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700461{
462 if (!ctl)
463 return 0;
464
David Lie95e65a2020-12-09 02:11:18 +0000465 return ctl->info->count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700466}
467
David Lie95e65a2020-12-09 02:11:18 +0000468static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
Simon Wilsonedff7082011-06-06 15:33:34 -0700469{
David Lie95e65a2020-12-09 02:11:18 +0000470 int range;
Simon Wilsonedff7082011-06-06 15:33:34 -0700471
David Lie95e65a2020-12-09 02:11:18 +0000472 if (percent > 100)
473 percent = 100;
474 else if (percent < 0)
475 percent = 0;
476
477 range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilsonedff7082011-06-06 15:33:34 -0700478
479 return ei->value.integer.min + (range * percent) / 100;
480}
481
David Lie95e65a2020-12-09 02:11:18 +0000482static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
Simon Wilsonedff7082011-06-06 15:33:34 -0700483{
484 int range = (ei->value.integer.max - ei->value.integer.min);
485
486 if (range == 0)
487 return 0;
488
David Lie95e65a2020-12-09 02:11:18 +0000489 return ((value - ei->value.integer.min) / range) * 100;
Simon Wilsonedff7082011-06-06 15:33:34 -0700490}
491
David Lie95e65a2020-12-09 02:11:18 +0000492int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700493{
David Lie95e65a2020-12-09 02:11:18 +0000494 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700495 return -EINVAL;
496
David Lie95e65a2020-12-09 02:11:18 +0000497 return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsonedff7082011-06-06 15:33:34 -0700498}
499
500int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
501{
David Lie95e65a2020-12-09 02:11:18 +0000502 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700503 return -EINVAL;
504
David Lie95e65a2020-12-09 02:11:18 +0000505 return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
Simon Wilsonedff7082011-06-06 15:33:34 -0700506}
507
David Lie95e65a2020-12-09 02:11:18 +0000508int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700509{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700510 struct mixer_ctl_group *grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700511 struct snd_ctl_elem_value ev;
512 int ret;
513
David Lie95e65a2020-12-09 02:11:18 +0000514 if (!ctl || (id >= ctl->info->count))
Simon Wilsonedff7082011-06-06 15:33:34 -0700515 return -EINVAL;
516
517 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000518 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700519 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700520 if (ret < 0)
521 return ret;
522
David Lie95e65a2020-12-09 02:11:18 +0000523 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700524 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
525 return !!ev.value.integer.value[id];
526
527 case SNDRV_CTL_ELEM_TYPE_INTEGER:
528 return ev.value.integer.value[id];
529
530 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
531 return ev.value.enumerated.item[id];
532
Simon Wilson5aed71d2011-11-16 14:45:38 -0800533 case SNDRV_CTL_ELEM_TYPE_BYTES:
534 return ev.value.bytes.data[id];
535
Simon Wilsonedff7082011-06-06 15:33:34 -0700536 default:
537 return -EINVAL;
538 }
539
540 return 0;
541}
542
David Lie95e65a2020-12-09 02:11:18 +0000543int mixer_ctl_is_access_tlv_rw(struct mixer_ctl *ctl)
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530544{
David Lie95e65a2020-12-09 02:11:18 +0000545 return (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
546}
547
548int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
549{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700550 struct mixer_ctl_group *grp = ctl->grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800551 struct snd_ctl_elem_value ev;
Mythri P K67b1df42014-08-18 15:39:36 +0530552 int ret = 0;
Simon Wilson8813fe82013-06-24 15:40:34 -0700553 size_t size;
554 void *source;
David Lie95e65a2020-12-09 02:11:18 +0000555 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800556
David Lie95e65a2020-12-09 02:11:18 +0000557 if ((!ctl) || !count || !array)
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530558 return -EINVAL;
559
David Lie95e65a2020-12-09 02:11:18 +0000560 total_count = ctl->info->count;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530561
David Lie95e65a2020-12-09 02:11:18 +0000562 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
563 mixer_ctl_is_access_tlv_rw(ctl)) {
564 /* Additional two words is for the TLV header */
565 total_count += TLV_HEADER_SIZE;
566 }
567
568 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800569 return -EINVAL;
570
571 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000572 ev.id.numid = ctl->info->id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800573
David Lie95e65a2020-12-09 02:11:18 +0000574 switch (ctl->info->type) {
Simon Wilson8813fe82013-06-24 15:40:34 -0700575 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
576 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700577 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530578 if (ret < 0)
579 return ret;
Simon Wilson8813fe82013-06-24 15:40:34 -0700580 size = sizeof(ev.value.integer.value[0]);
581 source = ev.value.integer.value;
582 break;
583
584 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530585 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530586 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530587 struct snd_ctl_tlv *tlv;
588 int ret;
589
Ben Zhang6cb14f22016-04-22 17:59:40 -0700590 if (count > SIZE_MAX - sizeof(*tlv))
591 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530592 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700593 if (!tlv)
594 return -ENOMEM;
David Lie95e65a2020-12-09 02:11:18 +0000595 tlv->numid = ctl->info->id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +0530596 tlv->length = count;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700597 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530598
599 source = tlv->tlv;
600 memcpy(array, source, count);
601
602 free(tlv);
603
604 return ret;
605 } else {
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700606 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530607 if (ret < 0)
608 return ret;
609 size = sizeof(ev.value.bytes.data[0]);
610 source = ev.value.bytes.data;
611 break;
612 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700613
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530614 case SNDRV_CTL_ELEM_TYPE_IEC958:
615 size = sizeof(ev.value.iec958);
616 source = &ev.value.iec958;
617 break;
618
Simon Wilson8813fe82013-06-24 15:40:34 -0700619 default:
620 return -EINVAL;
621 }
622
623 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800624
625 return 0;
626}
627
Simon Wilsonedff7082011-06-06 15:33:34 -0700628int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
629{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700630 struct mixer_ctl_group *grp = ctl->grp;
631
Simon Wilsonedff7082011-06-06 15:33:34 -0700632 struct snd_ctl_elem_value ev;
633 int ret;
634
David Lie95e65a2020-12-09 02:11:18 +0000635 if (!ctl || (id >= ctl->info->count))
Simon Wilsonedff7082011-06-06 15:33:34 -0700636 return -EINVAL;
637
638 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000639 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700640 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700641 if (ret < 0)
642 return ret;
643
David Lie95e65a2020-12-09 02:11:18 +0000644 switch (ctl->info->type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700645 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
646 ev.value.integer.value[id] = !!value;
647 break;
648
649 case SNDRV_CTL_ELEM_TYPE_INTEGER:
650 ev.value.integer.value[id] = value;
651 break;
652
653 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
654 ev.value.enumerated.item[id] = value;
655 break;
656
David.Coutheruta8481aa2013-06-11 16:22:57 +0200657 case SNDRV_CTL_ELEM_TYPE_BYTES:
658 ev.value.bytes.data[id] = value;
659 break;
660
Simon Wilsonedff7082011-06-06 15:33:34 -0700661 default:
662 return -EINVAL;
663 }
664
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700665 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700666}
667
Simon Wilson8813fe82013-06-24 15:40:34 -0700668int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800669{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700670 struct mixer_ctl_group *grp = ctl->grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800671 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -0700672 size_t size;
673 void *dest;
David Lie95e65a2020-12-09 02:11:18 +0000674 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800675
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530676 if ((!ctl) || !count || !array)
677 return -EINVAL;
678
David Lie95e65a2020-12-09 02:11:18 +0000679 total_count = ctl->info->count;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530680
David Lie95e65a2020-12-09 02:11:18 +0000681 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
682 mixer_ctl_is_access_tlv_rw(ctl)) {
683 /* Additional two words is for the TLV header */
684 total_count += TLV_HEADER_SIZE;
685 }
686
687 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800688 return -EINVAL;
689
690 memset(&ev, 0, sizeof(ev));
David Lie95e65a2020-12-09 02:11:18 +0000691 ev.id.numid = ctl->info->id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800692
David Lie95e65a2020-12-09 02:11:18 +0000693 switch (ctl->info->type) {
Simon Wilson8813fe82013-06-24 15:40:34 -0700694 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
695 case SNDRV_CTL_ELEM_TYPE_INTEGER:
696 size = sizeof(ev.value.integer.value[0]);
697 dest = ev.value.integer.value;
698 break;
699
700 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530701 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530702 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530703 struct snd_ctl_tlv *tlv;
704 int ret = 0;
Ben Zhang6cb14f22016-04-22 17:59:40 -0700705 if (count > SIZE_MAX - sizeof(*tlv))
706 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530707 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700708 if (!tlv)
709 return -ENOMEM;
David Lie95e65a2020-12-09 02:11:18 +0000710 tlv->numid = ctl->info->id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +0530711 tlv->length = count;
712 memcpy(tlv->tlv, array, count);
713
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700714 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530715 free(tlv);
716
717 return ret;
718 } else {
719 size = sizeof(ev.value.bytes.data[0]);
720 dest = ev.value.bytes.data;
721 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700722 break;
723
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530724 case SNDRV_CTL_ELEM_TYPE_IEC958:
725 size = sizeof(ev.value.iec958);
726 dest = &ev.value.iec958;
727 break;
728
Simon Wilson8813fe82013-06-24 15:40:34 -0700729 default:
730 return -EINVAL;
731 }
732
733 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800734
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700735 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800736}
737
David Lie95e65a2020-12-09 02:11:18 +0000738int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700739{
David Lie95e65a2020-12-09 02:11:18 +0000740 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700741 return -EINVAL;
742
David Lie95e65a2020-12-09 02:11:18 +0000743 return ctl->info->value.integer.min;
Simon Wilsonedff7082011-06-06 15:33:34 -0700744}
745
David Lie95e65a2020-12-09 02:11:18 +0000746int mixer_ctl_get_range_max(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.max;
Simon Wilsonedff7082011-06-06 15:33:34 -0700752}
753
David Lie95e65a2020-12-09 02:11:18 +0000754unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700755{
756 if (!ctl)
757 return 0;
758
David Lie95e65a2020-12-09 02:11:18 +0000759 return ctl->info->value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -0700760}
761
Simon Wilsone44e30a2012-03-08 10:45:31 -0800762const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
763 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700764{
David Lie95e65a2020-12-09 02:11:18 +0000765 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
766 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsone44e30a2012-03-08 10:45:31 -0800767 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700768
Simon Wilsone44e30a2012-03-08 10:45:31 -0800769 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -0700770}
771
772int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
773{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700774 struct mixer_ctl_group *grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700775 unsigned int i, num_enums;
776 struct snd_ctl_elem_value ev;
777 int ret;
778
David Lie95e65a2020-12-09 02:11:18 +0000779 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
Simon Wilsonedff7082011-06-06 15:33:34 -0700780 return -EINVAL;
781
David Lie95e65a2020-12-09 02:11:18 +0000782 num_enums = ctl->info->value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -0700783 for (i = 0; i < num_enums; i++) {
784 if (!strcmp(string, ctl->ename[i])) {
785 memset(&ev, 0, sizeof(ev));
786 ev.value.enumerated.item[0] = i;
David Lie95e65a2020-12-09 02:11:18 +0000787 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700788 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700789 if (ret < 0)
790 return ret;
791 return 0;
792 }
793 }
794
795 return -EINVAL;
796}
David Lie95e65a2020-12-09 02:11:18 +0000797
798/** Subscribes for the mixer events.
799 * @param mixer A mixer handle.
800 * @param subscribe value indicating subscribe or unsubscribe for events
801 * @returns On success, zero.
802 * On failure, non-zero.
803 * @ingroup libtinyalsa-mixer
804 */
805int mixer_subscribe_events(struct mixer *mixer, int subscribe)
806{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700807 struct mixer_ctl_group *grp;
808
809 if (mixer->hw_grp) {
810 grp = mixer->hw_grp;
811 if (!subscribe)
812 grp->event_cnt = 0;
813 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
814 return -1;
815 }
816
817 if (mixer->virt_grp) {
818 grp = mixer->virt_grp;
819 if (!subscribe)
820 grp->event_cnt = 0;
821 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
822 return -1;
David Lie95e65a2020-12-09 02:11:18 +0000823 }
824 return 0;
825}
826
827/** Wait for mixer events.
828 * @param mixer A mixer handle.
829 * @param timeout timout value
830 * @returns On success, 1.
831 * On failure, -errno.
832 * On timeout, 0
833 * @ingroup libtinyalsa-mixer
834 */
835int mixer_wait_event(struct mixer *mixer, int timeout)
836{
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700837 struct pollfd pfd[2];
838 struct mixer_ctl_group *grp;
839 int count = 0, num_fds = 0, i;
David Lie95e65a2020-12-09 02:11:18 +0000840
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700841 memset(pfd, 0, sizeof(struct pollfd) * 2);
842
843 if (mixer->fd >= 0)
844 num_fds++;
845
846 if (mixer->virt_grp)
847 num_fds++;
848
849
850 /* TODO wait events for virt fd */
851 if (mixer->fd >= 0) {
852 pfd[count].fd = mixer->fd;
853 pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
854 count++;
855 }
856
857 if (mixer->virt_grp) {
858 grp = mixer->virt_grp;
859 if (!grp->ops->get_poll_fd(grp->data, pfd, count)) {
860 pfd[count].events = POLLIN | POLLERR | POLLNVAL;
861 count++;
862 }
863 }
864
865 if (!count)
866 return 0;
David Lie95e65a2020-12-09 02:11:18 +0000867
868 for (;;) {
869 int err;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700870 err = poll(pfd, count, timeout);
David Lie95e65a2020-12-09 02:11:18 +0000871 if (err < 0)
872 return -errno;
873 if (!err)
874 return 0;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700875 for (i = 0; i < count; i++) {
876 if (pfd[i].revents & (POLLERR | POLLNVAL))
877 return -EIO;
878 if (pfd[i].revents & (POLLIN | POLLOUT)) {
879 if ((i == 0) && mixer->fd >= 0) {
880 grp = mixer->hw_grp;
881 grp->event_cnt++;
882 } else {
883 grp = mixer->virt_grp;
884 grp->event_cnt++;
885 }
886 return 1;
887 }
888 }
David Lie95e65a2020-12-09 02:11:18 +0000889 }
890}
891
892/** Consume a mixer event.
893 * If mixer_subscribe_events has been called,
894 * mixer_wait_event will identify when a control value has changed.
895 * This function will clear a single event from the mixer so that
896 * further events can be alerted.
897 *
898 * @param mixer A mixer handle.
899 * @returns 0 on success. -errno on failure.
900 * @ingroup libtinyalsa-mixer
901 */
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700902int mixer_consume_event(struct mixer *mixer)
903{
David Lie95e65a2020-12-09 02:11:18 +0000904 struct snd_ctl_event ev;
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700905 struct mixer_ctl_group *grp;
906 ssize_t count = 0;
907
David Lie95e65a2020-12-09 02:11:18 +0000908 // Exporting the actual event would require exposing snd_ctl_event
909 // via the header file, and all associated structs.
910 // The events generally tell you exactly which value changed,
911 // but reading values you're interested isn't hard and simplifies
912 // the interface greatly.
Bhalchandra Gajare6b2537f2019-08-21 15:09:43 -0700913 if (mixer->hw_grp) {
914 grp = mixer->hw_grp;
915 if (grp->event_cnt) {
916 grp->event_cnt--;
917 count = grp->ops->read_event(grp->data, &ev, sizeof(ev));
918 return (count >= 0) ? 0 : -errno;
919 }
920 }
921
922 if (mixer->virt_grp) {
923 grp = mixer->virt_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 return 0;
931}
932
933/** Read a mixer event.
934 * If mixer_subscribe_events has been called,
935 * mixer_wait_event will identify when a control value has changed.
936 * This function will read and clear a single event from the mixer
937 * so that further events can be alerted.
938 *
939 * @param mixer A mixer handle.
940 * @param ev snd_ctl_event pointer where event needs to be read
941 * @returns 0 on success. -errno on failure.
942 * @ingroup libtinyalsa-mixer
943 */
944int mixer_read_event(struct mixer *mixer, struct ctl_event *ev)
945{
946 struct mixer_ctl_group *grp;
947 ssize_t count = 0;
948
949 if (mixer->hw_grp) {
950 grp = mixer->hw_grp;
951 if (grp->event_cnt) {
952 grp->event_cnt--;
953 count = grp->ops->read_event(grp->data, (struct snd_ctl_event *)ev, sizeof(*ev));
954 return (count >= 0) ? 0 : -errno;
955 }
956 }
957
958 if (mixer->virt_grp) {
959 grp = mixer->virt_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 return 0;
David Lie95e65a2020-12-09 02:11:18 +0000967}