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