Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 1 | /* mixer.c |
| 2 | ** |
| 3 | ** Copyright 2011, The Android Open Source Project |
| 4 | ** |
| 5 | ** Redistribution and use in source and binary forms, with or without |
| 6 | ** modification, are permitted provided that the following conditions are met: |
| 7 | ** * Redistributions of source code must retain the above copyright |
| 8 | ** notice, this list of conditions and the following disclaimer. |
| 9 | ** * Redistributions in binary form must reproduce the above copyright |
| 10 | ** notice, this list of conditions and the following disclaimer in the |
| 11 | ** documentation and/or other materials provided with the distribution. |
| 12 | ** * Neither the name of The Android Open Source Project nor the names of |
| 13 | ** its contributors may be used to endorse or promote products derived |
| 14 | ** from this software without specific prior written permission. |
| 15 | ** |
| 16 | ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND |
| 17 | ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE |
| 20 | ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 | ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 | ** DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 31 | #include <stdint.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <errno.h> |
| 36 | #include <ctype.h> |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 37 | #include <limits.h> |
Dima Krasner | 696c448 | 2016-03-05 19:50:02 +0200 | [diff] [blame] | 38 | #include <time.h> |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 39 | #include <poll.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 40 | |
Simon Wilson | 6a52f2c | 2012-05-04 16:32:10 -0700 | [diff] [blame] | 41 | #include <sys/ioctl.h> |
| 42 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 43 | #include <linux/ioctl.h> |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame^] | 44 | |
| 45 | #ifndef __force |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 46 | #define __force |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame^] | 47 | #endif |
| 48 | |
| 49 | #ifndef __bitwise |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 50 | #define __bitwise |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame^] | 51 | #endif |
| 52 | |
| 53 | #ifndef __user |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 54 | #define __user |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame^] | 55 | #endif |
| 56 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 57 | #include <sound/asound.h> |
| 58 | |
Ricardo Biehl Pasquali | 04952ee | 2016-10-05 20:32:09 -0300 | [diff] [blame] | 59 | #include <tinyalsa/mixer.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 60 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 61 | /** A mixer control. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 62 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 63 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 64 | struct mixer_ctl { |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 65 | /** The mixer that the mixer control belongs to */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 66 | struct mixer *mixer; |
Taylor Holberton | ad4d7d7 | 2016-11-23 13:34:18 -0800 | [diff] [blame] | 67 | /** Information on the control's value (i.e. type, number of values) */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 68 | struct snd_ctl_elem_info info; |
Taylor Holberton | ad4d7d7 | 2016-11-23 13:34:18 -0800 | [diff] [blame] | 69 | /** A list of string representations of enumerated values (only valid for enumerated controls) */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 70 | char **ename; |
| 71 | }; |
| 72 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 73 | /** A mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 74 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 75 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 76 | struct mixer { |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 77 | /** File descriptor for the card */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 78 | int fd; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 79 | /** Card information */ |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 80 | struct snd_ctl_card_info card_info; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 81 | /** A continuous array of mixer controls */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 82 | struct mixer_ctl *ctl; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 83 | /** The number of mixer controls */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 84 | unsigned int count; |
| 85 | }; |
| 86 | |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 87 | static void mixer_cleanup_control(struct mixer_ctl *ctl) |
| 88 | { |
| 89 | unsigned int m; |
| 90 | |
| 91 | if (ctl->ename) { |
| 92 | unsigned int max = ctl->info.value.enumerated.items; |
| 93 | for (m = 0; m < max; m++) |
| 94 | free(ctl->ename[m]); |
| 95 | free(ctl->ename); |
| 96 | } |
| 97 | } |
| 98 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 99 | /** Closes a mixer returned by @ref mixer_open. |
| 100 | * @param mixer A mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 101 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 102 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 103 | void mixer_close(struct mixer *mixer) |
| 104 | { |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 105 | unsigned int n; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 106 | |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 107 | if (!mixer) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 108 | return; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 109 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 110 | if (mixer->fd >= 0) |
| 111 | close(mixer->fd); |
| 112 | |
| 113 | if (mixer->ctl) { |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 114 | for (n = 0; n < mixer->count; n++) |
| 115 | mixer_cleanup_control(&mixer->ctl[n]); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 116 | free(mixer->ctl); |
| 117 | } |
| 118 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 119 | free(mixer); |
| 120 | |
| 121 | /* TODO: verify frees */ |
| 122 | } |
| 123 | |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 124 | static void *mixer_realloc_z(void *ptr, size_t curnum, size_t newnum, size_t size) |
| 125 | { |
| 126 | int8_t *newp; |
| 127 | |
| 128 | newp = realloc(ptr, size * newnum); |
| 129 | if (!newp) |
| 130 | return NULL; |
| 131 | |
| 132 | memset(newp + (curnum * size), 0, (newnum - curnum) * size); |
| 133 | return newp; |
| 134 | } |
| 135 | |
| 136 | static int add_controls(struct mixer *mixer) |
| 137 | { |
| 138 | struct snd_ctl_elem_list elist; |
| 139 | struct snd_ctl_elem_id *eid = NULL; |
| 140 | struct mixer_ctl *ctl; |
| 141 | int fd = mixer->fd; |
| 142 | const unsigned int old_count = mixer->count; |
| 143 | unsigned int new_count; |
| 144 | unsigned int n; |
| 145 | |
| 146 | memset(&elist, 0, sizeof(elist)); |
| 147 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 148 | goto fail; |
| 149 | |
| 150 | if (old_count == elist.count) |
| 151 | return 0; /* no new controls return unchanged */ |
| 152 | |
| 153 | if (old_count > elist.count) |
| 154 | return -1; /* driver has removed controls - this is bad */ |
| 155 | |
| 156 | ctl = mixer_realloc_z(mixer->ctl, old_count, elist.count, |
| 157 | sizeof(struct mixer_ctl)); |
| 158 | if (!ctl) |
| 159 | goto fail; |
| 160 | |
| 161 | mixer->ctl = ctl; |
| 162 | |
| 163 | /* ALSA drivers are not supposed to remove or re-order controls that |
| 164 | * have already been created so we know that any new controls must |
| 165 | * be after the ones we have already collected |
| 166 | */ |
| 167 | new_count = elist.count; |
| 168 | elist.space = new_count - old_count; /* controls we haven't seen before */ |
| 169 | elist.offset = old_count; /* first control we haven't seen */ |
| 170 | |
| 171 | eid = calloc(elist.space, sizeof(struct snd_ctl_elem_id)); |
| 172 | if (!eid) |
| 173 | goto fail; |
| 174 | |
| 175 | elist.pids = eid; |
| 176 | |
| 177 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 178 | goto fail; |
| 179 | |
| 180 | for (n = old_count; n < new_count; n++) { |
| 181 | struct snd_ctl_elem_info *ei = &mixer->ctl[n].info; |
| 182 | ei->id.numid = eid[n - old_count].numid; |
| 183 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0) |
| 184 | goto fail_extend; |
| 185 | ctl[n].mixer = mixer; |
| 186 | } |
| 187 | |
| 188 | mixer->count = new_count; |
| 189 | free(eid); |
| 190 | return 0; |
| 191 | |
| 192 | fail_extend: |
| 193 | /* cleanup the control we failed on but leave the ones that were already |
| 194 | * added. Also no advantage to shrinking the resized memory block, we |
| 195 | * might want to extend the controls again later |
| 196 | */ |
| 197 | mixer_cleanup_control(&ctl[n]); |
| 198 | |
| 199 | mixer->count = n; /* keep controls we successfully added */ |
| 200 | /* fall through... */ |
| 201 | fail: |
| 202 | free(eid); |
| 203 | return -1; |
| 204 | } |
| 205 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 206 | /** Opens a mixer for a given card. |
| 207 | * @param card The card to open the mixer for. |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 208 | * @returns An initialized mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 209 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 210 | */ |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 211 | struct mixer *mixer_open(unsigned int card) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 212 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 213 | struct mixer *mixer = NULL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 214 | int fd; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 215 | char fn[256]; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 216 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 217 | snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card); |
| 218 | fd = open(fn, O_RDWR); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 219 | if (fd < 0) |
| 220 | return 0; |
| 221 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 222 | mixer = calloc(1, sizeof(*mixer)); |
| 223 | if (!mixer) |
| 224 | goto fail; |
| 225 | |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 226 | if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 227 | goto fail; |
| 228 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 229 | mixer->fd = fd; |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 230 | |
| 231 | if (add_controls(mixer) != 0) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 232 | goto fail; |
| 233 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 234 | return mixer; |
| 235 | |
| 236 | fail: |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 237 | if (mixer) |
| 238 | mixer_close(mixer); |
| 239 | else if (fd >= 0) |
| 240 | close(fd); |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | /** Some controls may not be present at boot time, e.g. controls from runtime |
| 245 | * loadable DSP firmware. This function adds any new controls that have appeared |
| 246 | * since mixer_open() or the last call to this function. This assumes a well- |
| 247 | * behaved codec driver that does not delete controls that already exists, so |
| 248 | * any added controls must be after the last one we already saw. Scanning only |
| 249 | * the new controls is much faster than calling mixer_close() then mixer_open() |
| 250 | * to re-scan all controls. |
| 251 | * |
| 252 | * NOTE: this invalidates any struct mixer_ctl pointers previously obtained |
| 253 | * from mixer_get_ctl() and mixer_get_ctl_by_name(). Either refresh all your |
| 254 | * stored pointers after calling mixer_update_ctls(), or (better) do not |
| 255 | * store struct mixer_ctl pointers, instead lookup the control by name or |
| 256 | * id only when you are about to use it. The overhead of lookup by id |
| 257 | * using mixer_get_ctl() is negligible. |
| 258 | * @param mixer An initialized mixer handle. |
| 259 | * @returns 0 on success, -1 on failure |
| 260 | */ |
| 261 | int mixer_add_new_ctls(struct mixer *mixer) |
| 262 | { |
| 263 | if (!mixer) |
| 264 | return 0; |
| 265 | |
| 266 | return add_controls(mixer); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 269 | /** Gets the name of the mixer's card. |
| 270 | * @param mixer An initialized mixer handle. |
| 271 | * @returns The name of the mixer's card. |
| 272 | * @ingroup libtinyalsa-mixer |
| 273 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 274 | const char *mixer_get_name(const struct mixer *mixer) |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 275 | { |
| 276 | return (const char *)mixer->card_info.name; |
| 277 | } |
| 278 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 279 | /** Gets the number of mixer controls for a given mixer. |
| 280 | * @param mixer An initialized mixer handle. |
| 281 | * @returns The number of mixer controls for the given mixer. |
| 282 | * @ingroup libtinyalsa-mixer |
| 283 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 284 | unsigned int mixer_get_num_ctls(const struct mixer *mixer) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 285 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 286 | if (!mixer) |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 287 | return 0; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 288 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 289 | return mixer->count; |
| 290 | } |
| 291 | |
Taylor Holberton | 94c7c83 | 2016-12-01 18:35:24 -0800 | [diff] [blame] | 292 | /** Gets the number of mixer controls, that go by a specified name, for a given mixer. |
| 293 | * @param mixer An initialized mixer handle. |
| 294 | * @param name The name of the mixer control |
| 295 | * @returns The number of mixer controls, specified by @p name, for the given mixer. |
| 296 | * @ingroup libtinyalsa-mixer |
| 297 | */ |
| 298 | unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name) |
| 299 | { |
| 300 | unsigned int n; |
| 301 | unsigned int count = 0; |
| 302 | struct mixer_ctl *ctl; |
| 303 | |
| 304 | if (!mixer) |
| 305 | return 0; |
| 306 | |
| 307 | ctl = mixer->ctl; |
| 308 | |
| 309 | for (n = 0; n < mixer->count; n++) |
| 310 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
| 311 | count++; |
| 312 | |
| 313 | return count; |
| 314 | } |
| 315 | |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 316 | /** Subscribes for the mixer events. |
| 317 | * @param mixer A mixer handle. |
| 318 | * @param subscribe value indicating subscribe or unsubscribe for events |
| 319 | * @returns On success, zero. |
| 320 | * On failure, non-zero. |
| 321 | * @ingroup libtinyalsa-mixer |
| 322 | */ |
| 323 | int mixer_subscribe_events(struct mixer *mixer, int subscribe) |
| 324 | { |
| 325 | if (ioctl(mixer->fd, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) { |
| 326 | return -1; |
| 327 | } |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /** Wait for mixer events. |
| 332 | * @param mixer A mixer handle. |
| 333 | * @param timeout timout value |
Pankaj Bharadiya | 95c79d8 | 2017-01-11 11:28:02 +0530 | [diff] [blame] | 334 | * @returns On success, 1. |
| 335 | * On failure, -errno. |
| 336 | * On timeout, 0 |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 337 | * @ingroup libtinyalsa-mixer |
| 338 | */ |
| 339 | int mixer_wait_event(struct mixer *mixer, int timeout) |
| 340 | { |
| 341 | struct pollfd pfd; |
| 342 | |
| 343 | pfd.fd = mixer->fd; |
| 344 | pfd.events = POLLIN | POLLOUT | POLLERR | POLLNVAL; |
| 345 | |
| 346 | for (;;) { |
| 347 | int err; |
| 348 | err = poll(&pfd, 1, timeout); |
| 349 | if (err < 0) |
| 350 | return -errno; |
| 351 | if (!err) |
| 352 | return 0; |
| 353 | if (pfd.revents & (POLLERR | POLLNVAL)) |
| 354 | return -EIO; |
| 355 | if (pfd.revents & (POLLIN | POLLOUT)) |
| 356 | return 1; |
| 357 | } |
| 358 | } |
| 359 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 360 | /** Gets a mixer control handle, by the mixer control's id. |
Taylor Holberton | a94295b | 2016-12-01 18:23:16 -0800 | [diff] [blame] | 361 | * For non-const access, see @ref mixer_get_ctl |
| 362 | * @param mixer An initialized mixer handle. |
| 363 | * @param id The control's id in the given mixer. |
| 364 | * @returns A handle to the mixer control. |
| 365 | * @ingroup libtinyalsa-mixer |
| 366 | */ |
| 367 | const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id) |
| 368 | { |
| 369 | if (mixer && (id < mixer->count)) |
| 370 | return mixer->ctl + id; |
| 371 | |
| 372 | return NULL; |
| 373 | } |
| 374 | |
| 375 | /** Gets a mixer control handle, by the mixer control's id. |
| 376 | * For const access, see @ref mixer_get_ctl_const |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 377 | * @param mixer An initialized mixer handle. |
| 378 | * @param id The control's id in the given mixer. |
| 379 | * @returns A handle to the mixer control. |
| 380 | * @ingroup libtinyalsa-mixer |
| 381 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 382 | struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id) |
| 383 | { |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 384 | if (mixer && (id < mixer->count)) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 385 | return mixer->ctl + id; |
| 386 | |
| 387 | return NULL; |
| 388 | } |
| 389 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 390 | /** Gets the first instance of mixer control handle, by the mixer control's name. |
| 391 | * @param mixer An initialized mixer handle. |
| 392 | * @param name The control's name in the given mixer. |
| 393 | * @returns A handle to the mixer control. |
| 394 | * @ingroup libtinyalsa-mixer |
| 395 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 396 | struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name) |
| 397 | { |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 398 | return mixer_get_ctl_by_name_and_index(mixer, name, 0); |
| 399 | } |
| 400 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 401 | /** Gets an instance of mixer control handle, by the mixer control's name and index. |
| 402 | * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control. |
| 403 | * @param mixer An initialized mixer handle. |
| 404 | * @param name The control's name in the given mixer. |
| 405 | * @param index The control's index. |
| 406 | * @returns A handle to the mixer control. |
| 407 | * @ingroup libtinyalsa-mixer |
| 408 | */ |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 409 | struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer, |
| 410 | const char *name, |
| 411 | unsigned int index) |
| 412 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 413 | unsigned int n; |
Svyatoslav Mishyn | c732836 | 2016-01-24 19:43:38 +0200 | [diff] [blame] | 414 | struct mixer_ctl *ctl; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 415 | |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 416 | if (!mixer) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 417 | return NULL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 418 | |
Svyatoslav Mishyn | c732836 | 2016-01-24 19:43:38 +0200 | [diff] [blame] | 419 | ctl = mixer->ctl; |
| 420 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 421 | for (n = 0; n < mixer->count; n++) |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 422 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 423 | if (index-- == 0) |
| 424 | return mixer->ctl + n; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 425 | |
| 426 | return NULL; |
| 427 | } |
| 428 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 429 | /** Updates the control's info. |
| 430 | * This is useful for a program that may be idle for a period of time. |
| 431 | * @param ctl An initialized control handle. |
| 432 | * @ingroup libtinyalsa-mixer |
| 433 | */ |
Simon Wilson | 710df88 | 2013-06-28 16:17:50 -0700 | [diff] [blame] | 434 | void mixer_ctl_update(struct mixer_ctl *ctl) |
| 435 | { |
| 436 | ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info); |
| 437 | } |
| 438 | |
Pankaj Bharadiya | 9698d03 | 2017-01-09 12:23:14 +0530 | [diff] [blame] | 439 | /** Checks the control for TLV Read/Write access. |
| 440 | * @param ctl An initialized control handle. |
| 441 | * @returns On success, non-zero. |
| 442 | * On failure, zero. |
| 443 | * @ingroup libtinyalsa-mixer |
| 444 | */ |
| 445 | int mixer_ctl_is_access_tlv_rw(const struct mixer_ctl *ctl) |
| 446 | { |
| 447 | return (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE); |
| 448 | } |
| 449 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 450 | /** Gets the control's ID. |
| 451 | * @param ctl An initialized control handle. |
| 452 | * @returns On success, the control's ID is returned. |
| 453 | * On error, UINT_MAX is returned instead. |
| 454 | * @ingroup libtinyalsa-mixer |
| 455 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 456 | unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl) |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 457 | { |
| 458 | if (!ctl) |
| 459 | return UINT_MAX; |
| 460 | |
| 461 | /* numid values start at 1, return a 0-base value that |
| 462 | * can be passed to mixer_get_ctl() |
| 463 | */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 464 | return ctl->info.id.numid - 1; |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 467 | /** Gets the name of the control. |
| 468 | * @param ctl An initialized control handle. |
| 469 | * @returns On success, the name of the control. |
| 470 | * On error, NULL. |
| 471 | * @ingroup libtinyalsa-mixer |
| 472 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 473 | const char *mixer_ctl_get_name(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 474 | { |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 475 | if (!ctl) |
| 476 | return NULL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 477 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 478 | return (const char *)ctl->info.id.name; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 481 | /** Gets the value type of the control. |
| 482 | * @param ctl An initialized control handle |
| 483 | * @returns On success, the type of mixer control. |
| 484 | * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN |
| 485 | * @ingroup libtinyalsa-mixer |
| 486 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 487 | enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 488 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 489 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 490 | return MIXER_CTL_TYPE_UNKNOWN; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 491 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 492 | switch (ctl->info.type) { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 493 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL; |
| 494 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT; |
| 495 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM; |
| 496 | case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE; |
| 497 | case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958; |
| 498 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64; |
| 499 | default: return MIXER_CTL_TYPE_UNKNOWN; |
| 500 | }; |
| 501 | } |
| 502 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 503 | /** Gets the string that describes the value type of the control. |
| 504 | * @param ctl An initialized control handle |
| 505 | * @returns On success, a string describing type of mixer control. |
| 506 | * @ingroup libtinyalsa-mixer |
| 507 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 508 | const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 509 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 510 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 511 | return ""; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 512 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 513 | switch (ctl->info.type) { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 514 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL"; |
| 515 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT"; |
| 516 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM"; |
| 517 | case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE"; |
| 518 | case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958"; |
| 519 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64"; |
| 520 | default: return "Unknown"; |
| 521 | }; |
| 522 | } |
| 523 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 524 | /** Gets the number of values in the control. |
| 525 | * @param ctl An initialized control handle |
| 526 | * @returns The number of values in the mixer control |
| 527 | * @ingroup libtinyalsa-mixer |
| 528 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 529 | unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 530 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 531 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 532 | return 0; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 533 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 534 | return ctl->info.count; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 537 | static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 538 | { |
Baptiste Robert | 4a484e1 | 2013-09-12 15:37:53 +0200 | [diff] [blame] | 539 | if ((percent > 100) || (percent < 0)) { |
| 540 | return -EINVAL; |
| 541 | } |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 542 | |
Baptiste Robert | 4a484e1 | 2013-09-12 15:37:53 +0200 | [diff] [blame] | 543 | int range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 544 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 545 | return ei->value.integer.min + (range * percent) / 100; |
| 546 | } |
| 547 | |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 548 | static int int_to_percent(const struct snd_ctl_elem_info *ei, int value) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 549 | { |
| 550 | int range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 551 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 552 | if (range == 0) |
| 553 | return 0; |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 554 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 555 | return ((value - ei->value.integer.min) / range) * 100; |
| 556 | } |
| 557 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 558 | /** Gets a percentage representation of a specified control value. |
| 559 | * @param ctl An initialized control handle. |
| 560 | * @param id The index of the value within the control. |
| 561 | * @returns On success, the percentage representation of the control value. |
| 562 | * On failure, -EINVAL is returned. |
| 563 | * @ingroup libtinyalsa-mixer |
| 564 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 565 | int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 566 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 567 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 568 | return -EINVAL; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 569 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 570 | return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id)); |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 573 | /** Sets the value of a control by percent, specified by the value index. |
| 574 | * @param ctl An initialized control handle. |
| 575 | * @param id The index of the value to set |
| 576 | * @param percent A percentage value between 0 and 100. |
| 577 | * @returns On success, zero is returned. |
| 578 | * On failure, non-zero is returned. |
| 579 | * @ingroup libtinyalsa-mixer |
| 580 | */ |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 581 | int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent) |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 582 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 583 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 584 | return -EINVAL; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 585 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 586 | return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent)); |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 589 | /** Gets the value of a control. |
| 590 | * @param ctl An initialized control handle. |
| 591 | * @param id The index of the control value. |
| 592 | * @returns On success, the specified value is returned. |
| 593 | * On failure, -EINVAL is returned. |
| 594 | * @ingroup libtinyalsa-mixer |
| 595 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 596 | int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 597 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 598 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 599 | int ret; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 600 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 601 | if (!ctl || (id >= ctl->info.count)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 602 | return -EINVAL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 603 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 604 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 605 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 606 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 607 | if (ret < 0) |
| 608 | return ret; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 609 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 610 | switch (ctl->info.type) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 611 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 612 | return !!ev.value.integer.value[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 613 | |
| 614 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 615 | return ev.value.integer.value[id]; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 616 | |
| 617 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 618 | return ev.value.enumerated.item[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 619 | |
Pierre-Louis Bossart | 5251016 | 2011-10-24 15:00:17 -0500 | [diff] [blame] | 620 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 621 | return ev.value.bytes.data[id]; |
| 622 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 623 | default: |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 624 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 630 | /** Gets the contents of a control's value array. |
| 631 | * @param ctl An initialized control handle. |
| 632 | * @param array A pointer to write the array data to. |
| 633 | * The size of this array must be equal to the number of items in the array |
| 634 | * multiplied by the size of each item. |
| 635 | * @param count The number of items in the array. |
| 636 | * This parameter must match the number of items in the control. |
| 637 | * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values |
| 638 | * @returns On success, zero. |
| 639 | * On failure, non-zero. |
| 640 | * @ingroup libtinyalsa-mixer |
| 641 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 642 | int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 643 | { |
| 644 | struct snd_ctl_elem_value ev; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 645 | int ret = 0; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 646 | size_t size; |
| 647 | void *source; |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 648 | size_t total_count; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 649 | |
Charles Keepax | e40758b | 2017-09-07 16:38:52 +0100 | [diff] [blame] | 650 | if (!ctl || !count || !array) |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 651 | return -EINVAL; |
| 652 | |
| 653 | total_count = ctl->info.count; |
| 654 | |
| 655 | if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) && |
| 656 | (mixer_ctl_is_access_tlv_rw(ctl))) { |
| 657 | /* Additional two words is for the TLV header */ |
| 658 | total_count += TLV_HEADER_SIZE; |
| 659 | } |
| 660 | |
| 661 | if (count > total_count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 662 | return -EINVAL; |
| 663 | |
| 664 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 665 | ev.id.numid = ctl->info.id.numid; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 666 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 667 | switch (ctl->info.type) { |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 668 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 669 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 670 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 671 | if (ret < 0) |
| 672 | return ret; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 673 | size = sizeof(ev.value.integer.value[0]); |
| 674 | source = ev.value.integer.value; |
| 675 | break; |
| 676 | |
| 677 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 678 | /* check if this is new bytes TLV */ |
Pankaj Bharadiya | 9698d03 | 2017-01-09 12:23:14 +0530 | [diff] [blame] | 679 | if (mixer_ctl_is_access_tlv_rw(ctl)) { |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 680 | struct snd_ctl_tlv *tlv; |
| 681 | int ret; |
| 682 | |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 683 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 684 | return -EINVAL; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 685 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 686 | if (!tlv) |
| 687 | return -ENOMEM; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 688 | tlv->numid = ctl->info.id.numid; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 689 | tlv->length = count; |
| 690 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv); |
| 691 | |
| 692 | source = tlv->tlv; |
| 693 | memcpy(array, source, count); |
| 694 | |
| 695 | free(tlv); |
| 696 | |
| 697 | return ret; |
| 698 | } else { |
| 699 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 700 | if (ret < 0) |
| 701 | return ret; |
| 702 | size = sizeof(ev.value.bytes.data[0]); |
| 703 | source = ev.value.bytes.data; |
| 704 | break; |
| 705 | } |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 706 | |
| 707 | default: |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
| 711 | memcpy(array, source, size * count); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 716 | /** Sets the value of a control, specified by the value index. |
| 717 | * @param ctl An initialized control handle. |
| 718 | * @param id The index of the value within the control. |
| 719 | * @param value The value to set. |
| 720 | * This must be in a range specified by @ref mixer_ctl_get_range_min |
| 721 | * and @ref mixer_ctl_get_range_max. |
| 722 | * @returns On success, zero is returned. |
| 723 | * On failure, non-zero is returned. |
| 724 | * @ingroup libtinyalsa-mixer |
| 725 | */ |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 726 | int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 727 | { |
| 728 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 729 | int ret; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 730 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 731 | if (!ctl || (id >= ctl->info.count)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 732 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 733 | |
| 734 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 735 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 736 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 737 | if (ret < 0) |
| 738 | return ret; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 739 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 740 | switch (ctl->info.type) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 741 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 742 | ev.value.integer.value[id] = !!value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 743 | break; |
| 744 | |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 745 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Baptiste Robert | 6cd7d5f | 2013-08-07 11:32:45 +0200 | [diff] [blame] | 746 | if ((value < mixer_ctl_get_range_min(ctl)) || |
| 747 | (value > mixer_ctl_get_range_max(ctl))) { |
| 748 | return -EINVAL; |
| 749 | } |
| 750 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 751 | ev.value.integer.value[id] = value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 752 | break; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 753 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 754 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 755 | ev.value.enumerated.item[id] = value; |
| 756 | break; |
| 757 | |
David.Coutherut | ce2b634 | 2013-06-11 16:22:57 +0200 | [diff] [blame] | 758 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 759 | ev.value.bytes.data[id] = value; |
| 760 | break; |
| 761 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 762 | default: |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 763 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 764 | } |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 765 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 766 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 767 | } |
| 768 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 769 | /** Sets the contents of a control's value array. |
| 770 | * @param ctl An initialized control handle. |
| 771 | * @param array The array containing control values. |
| 772 | * @param count The number of values in the array. |
| 773 | * This must match the number of values in the control. |
| 774 | * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values |
| 775 | * @returns On success, zero. |
| 776 | * On failure, non-zero. |
| 777 | * @ingroup libtinyalsa-mixer |
| 778 | */ |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 779 | int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 780 | { |
| 781 | struct snd_ctl_elem_value ev; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 782 | size_t size; |
| 783 | void *dest; |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 784 | size_t total_count; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 785 | |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 786 | if ((!ctl) || !count || !array) |
| 787 | return -EINVAL; |
| 788 | |
| 789 | total_count = ctl->info.count; |
| 790 | |
| 791 | if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) && |
| 792 | (mixer_ctl_is_access_tlv_rw(ctl))) { |
| 793 | /* Additional TLV header */ |
| 794 | total_count += TLV_HEADER_SIZE; |
| 795 | } |
| 796 | |
| 797 | if (count > total_count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | |
| 800 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 801 | ev.id.numid = ctl->info.id.numid; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 802 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 803 | switch (ctl->info.type) { |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 804 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 805 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 806 | size = sizeof(ev.value.integer.value[0]); |
| 807 | dest = ev.value.integer.value; |
| 808 | break; |
| 809 | |
| 810 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 811 | /* check if this is new bytes TLV */ |
Pankaj Bharadiya | 9698d03 | 2017-01-09 12:23:14 +0530 | [diff] [blame] | 812 | if (mixer_ctl_is_access_tlv_rw(ctl)) { |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 813 | struct snd_ctl_tlv *tlv; |
| 814 | int ret = 0; |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 815 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 816 | return -EINVAL; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 817 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 818 | if (!tlv) |
| 819 | return -ENOMEM; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 820 | tlv->numid = ctl->info.id.numid; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 821 | tlv->length = count; |
| 822 | memcpy(tlv->tlv, array, count); |
| 823 | |
| 824 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv); |
| 825 | free(tlv); |
| 826 | |
| 827 | return ret; |
| 828 | } else { |
| 829 | size = sizeof(ev.value.bytes.data[0]); |
| 830 | dest = ev.value.bytes.data; |
| 831 | } |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 832 | break; |
| 833 | |
| 834 | default: |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | |
| 838 | memcpy(dest, array, size * count); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 839 | |
| 840 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 841 | } |
| 842 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 843 | /** Gets the minimum value of an control. |
| 844 | * The control must have an integer type. |
| 845 | * The type of the control can be checked with @ref mixer_ctl_get_type. |
| 846 | * @param ctl An initialized control handle. |
| 847 | * @returns On success, the minimum value of the control. |
| 848 | * On failure, -EINVAL. |
| 849 | * @ingroup libtinyalsa-mixer |
| 850 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 851 | int mixer_ctl_get_range_min(const struct mixer_ctl *ctl) |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 852 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 853 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 854 | return -EINVAL; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 855 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 856 | return ctl->info.value.integer.min; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 859 | /** Gets the maximum value of an control. |
| 860 | * The control must have an integer type. |
| 861 | * The type of the control can be checked with @ref mixer_ctl_get_type. |
| 862 | * @param ctl An initialized control handle. |
| 863 | * @returns On success, the maximum value of the control. |
| 864 | * On failure, -EINVAL. |
| 865 | * @ingroup libtinyalsa-mixer |
| 866 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 867 | int mixer_ctl_get_range_max(const struct mixer_ctl *ctl) |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 868 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 869 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 870 | return -EINVAL; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 871 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 872 | return ctl->info.value.integer.max; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 875 | /** Get the number of enumerated items in the control. |
| 876 | * @param ctl An initialized control handle. |
| 877 | * @returns The number of enumerated items in the control. |
| 878 | * @ingroup libtinyalsa-mixer |
| 879 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 880 | unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl) |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 881 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 882 | if (!ctl) |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 883 | return 0; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 884 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 885 | return ctl->info.value.enumerated.items; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 886 | } |
| 887 | |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 888 | int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl) |
| 889 | { |
| 890 | struct snd_ctl_elem_info tmp; |
| 891 | unsigned int m; |
| 892 | char **enames; |
| 893 | |
| 894 | if (ctl->ename) { |
| 895 | return 0; |
| 896 | } |
| 897 | |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 898 | enames = calloc(ctl->info.value.enumerated.items, sizeof(char*)); |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 899 | if (!enames) |
| 900 | goto fail; |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 901 | for (m = 0; m < ctl->info.value.enumerated.items; m++) { |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 902 | memset(&tmp, 0, sizeof(tmp)); |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 903 | tmp.id.numid = ctl->info.id.numid; |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 904 | tmp.value.enumerated.item = m; |
| 905 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0) |
| 906 | goto fail; |
| 907 | enames[m] = strdup(tmp.value.enumerated.name); |
| 908 | if (!enames[m]) |
| 909 | goto fail; |
| 910 | } |
| 911 | ctl->ename = enames; |
| 912 | return 0; |
| 913 | |
| 914 | fail: |
| 915 | if (enames) { |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 916 | for (m = 0; m < ctl->info.value.enumerated.items; m++) { |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 917 | if (enames[m]) { |
| 918 | free(enames[m]); |
| 919 | } |
| 920 | } |
| 921 | free(enames); |
| 922 | } |
| 923 | return -1; |
| 924 | } |
| 925 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 926 | /** Gets the string representation of an enumerated item. |
| 927 | * @param ctl An initialized control handle. |
| 928 | * @param enum_id The index of the enumerated value. |
| 929 | * @returns A string representation of the enumerated item. |
| 930 | * @ingroup libtinyalsa-mixer |
| 931 | */ |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 932 | const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, |
| 933 | unsigned int enum_id) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 934 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 935 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 936 | (enum_id >= ctl->info.value.enumerated.items) || |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 937 | mixer_ctl_fill_enum_string(ctl) != 0) |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 938 | return NULL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 939 | |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 940 | return (const char *)ctl->ename[enum_id]; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 943 | /** Set an enumeration value by string value. |
| 944 | * @param ctl An enumerated mixer control. |
| 945 | * @param string The string representation of an enumeration. |
| 946 | * @returns On success, zero. |
| 947 | * On failure, zero. |
| 948 | * @ingroup libtinyalsa-mixer |
| 949 | */ |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 950 | int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string) |
| 951 | { |
| 952 | unsigned int i, num_enums; |
| 953 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 954 | int ret; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 955 | |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 956 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 957 | mixer_ctl_fill_enum_string(ctl) != 0) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 958 | return -EINVAL; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 959 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 960 | num_enums = ctl->info.value.enumerated.items; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 961 | for (i = 0; i < num_enums; i++) { |
| 962 | if (!strcmp(string, ctl->ename[i])) { |
| 963 | memset(&ev, 0, sizeof(ev)); |
| 964 | ev.value.enumerated.item[0] = i; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 965 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 966 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 967 | if (ret < 0) |
| 968 | return ret; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 969 | return 0; |
| 970 | } |
| 971 | } |
| 972 | |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 973 | return -EINVAL; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 974 | } |
| 975 | |