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> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 39 | |
Simon Wilson | 6a52f2c | 2012-05-04 16:32:10 -0700 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
| 41 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 42 | #include <linux/ioctl.h> |
| 43 | #define __force |
| 44 | #define __bitwise |
| 45 | #define __user |
| 46 | #include <sound/asound.h> |
| 47 | |
Ricardo Biehl Pasquali | 04952ee | 2016-10-05 20:32:09 -0300 | [diff] [blame] | 48 | #include <tinyalsa/mixer.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 49 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 50 | /** A mixer control. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 51 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 52 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 53 | struct mixer_ctl { |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 54 | /** The mixer that the mixer control belongs to */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 55 | struct mixer *mixer; |
Taylor Holberton | ad4d7d7 | 2016-11-23 13:34:18 -0800 | [diff] [blame] | 56 | /** Information on the control's value (i.e. type, number of values) */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 57 | struct snd_ctl_elem_info info; |
Taylor Holberton | ad4d7d7 | 2016-11-23 13:34:18 -0800 | [diff] [blame] | 58 | /** 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] | 59 | char **ename; |
| 60 | }; |
| 61 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 62 | /** A mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 63 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 64 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 65 | struct mixer { |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 66 | /** File descriptor for the card */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 67 | int fd; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 68 | /** Card information */ |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 69 | struct snd_ctl_card_info card_info; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 70 | /** A continuous array of mixer controls */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 71 | struct mixer_ctl *ctl; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 72 | /** The number of mixer controls */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 73 | unsigned int count; |
| 74 | }; |
| 75 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 76 | /** Closes a mixer returned by @ref mixer_open. |
| 77 | * @param mixer A mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 78 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 79 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 80 | void mixer_close(struct mixer *mixer) |
| 81 | { |
| 82 | unsigned int n,m; |
| 83 | |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 84 | if (!mixer) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 85 | return; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 86 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 87 | if (mixer->fd >= 0) |
| 88 | close(mixer->fd); |
| 89 | |
| 90 | if (mixer->ctl) { |
| 91 | for (n = 0; n < mixer->count; n++) { |
| 92 | if (mixer->ctl[n].ename) { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 93 | unsigned int max = mixer->ctl[n].info.value.enumerated.items; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 94 | for (m = 0; m < max; m++) |
| 95 | free(mixer->ctl[n].ename[m]); |
| 96 | free(mixer->ctl[n].ename); |
| 97 | } |
| 98 | } |
| 99 | free(mixer->ctl); |
| 100 | } |
| 101 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 102 | free(mixer); |
| 103 | |
| 104 | /* TODO: verify frees */ |
| 105 | } |
| 106 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 107 | /** Opens a mixer for a given card. |
| 108 | * @param card The card to open the mixer for. |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 109 | * @returns An initialized mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 110 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 111 | */ |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 112 | struct mixer *mixer_open(unsigned int card) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 113 | { |
| 114 | struct snd_ctl_elem_list elist; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 115 | struct snd_ctl_elem_id *eid = NULL; |
| 116 | struct mixer *mixer = NULL; |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 117 | unsigned int n; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 118 | int fd; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 119 | char fn[256]; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 120 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 121 | snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card); |
| 122 | fd = open(fn, O_RDWR); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 123 | if (fd < 0) |
| 124 | return 0; |
| 125 | |
| 126 | memset(&elist, 0, sizeof(elist)); |
| 127 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 128 | goto fail; |
| 129 | |
| 130 | mixer = calloc(1, sizeof(*mixer)); |
| 131 | if (!mixer) |
| 132 | goto fail; |
| 133 | |
| 134 | mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 135 | if (!mixer->ctl) |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 136 | goto fail; |
| 137 | |
| 138 | if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 139 | goto fail; |
| 140 | |
| 141 | eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id)); |
| 142 | if (!eid) |
| 143 | goto fail; |
| 144 | |
| 145 | mixer->count = elist.count; |
| 146 | mixer->fd = fd; |
| 147 | elist.space = mixer->count; |
| 148 | elist.pids = eid; |
| 149 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 150 | goto fail; |
| 151 | |
| 152 | for (n = 0; n < mixer->count; n++) { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 153 | struct snd_ctl_elem_info *ei = &mixer->ctl[n].info; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 154 | ei->id.numid = eid[n].numid; |
| 155 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0) |
| 156 | goto fail; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 157 | mixer->ctl[n].mixer = mixer; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | free(eid); |
| 161 | return mixer; |
| 162 | |
| 163 | fail: |
| 164 | /* TODO: verify frees in failure case */ |
| 165 | if (eid) |
| 166 | free(eid); |
| 167 | if (mixer) |
| 168 | mixer_close(mixer); |
| 169 | else if (fd >= 0) |
| 170 | close(fd); |
| 171 | return 0; |
| 172 | } |
| 173 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 174 | /** Gets the name of the mixer's card. |
| 175 | * @param mixer An initialized mixer handle. |
| 176 | * @returns The name of the mixer's card. |
| 177 | * @ingroup libtinyalsa-mixer |
| 178 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 179 | const char *mixer_get_name(const struct mixer *mixer) |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 180 | { |
| 181 | return (const char *)mixer->card_info.name; |
| 182 | } |
| 183 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 184 | /** Gets the number of mixer controls for a given mixer. |
| 185 | * @param mixer An initialized mixer handle. |
| 186 | * @returns The number of mixer controls for the given mixer. |
| 187 | * @ingroup libtinyalsa-mixer |
| 188 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 189 | unsigned int mixer_get_num_ctls(const struct mixer *mixer) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 190 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 191 | if (!mixer) |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 192 | return 0; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 193 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 194 | return mixer->count; |
| 195 | } |
| 196 | |
Taylor Holberton | 94c7c83 | 2016-12-01 18:35:24 -0800 | [diff] [blame] | 197 | /** Gets the number of mixer controls, that go by a specified name, for a given mixer. |
| 198 | * @param mixer An initialized mixer handle. |
| 199 | * @param name The name of the mixer control |
| 200 | * @returns The number of mixer controls, specified by @p name, for the given mixer. |
| 201 | * @ingroup libtinyalsa-mixer |
| 202 | */ |
| 203 | unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name) |
| 204 | { |
| 205 | unsigned int n; |
| 206 | unsigned int count = 0; |
| 207 | struct mixer_ctl *ctl; |
| 208 | |
| 209 | if (!mixer) |
| 210 | return 0; |
| 211 | |
| 212 | ctl = mixer->ctl; |
| 213 | |
| 214 | for (n = 0; n < mixer->count; n++) |
| 215 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
| 216 | count++; |
| 217 | |
| 218 | return count; |
| 219 | } |
| 220 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 221 | /** Gets a mixer control handle, by the mixer control's id. |
Taylor Holberton | a94295b | 2016-12-01 18:23:16 -0800 | [diff] [blame] | 222 | * For non-const access, see @ref mixer_get_ctl |
| 223 | * @param mixer An initialized mixer handle. |
| 224 | * @param id The control's id in the given mixer. |
| 225 | * @returns A handle to the mixer control. |
| 226 | * @ingroup libtinyalsa-mixer |
| 227 | */ |
| 228 | const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id) |
| 229 | { |
| 230 | if (mixer && (id < mixer->count)) |
| 231 | return mixer->ctl + id; |
| 232 | |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | /** Gets a mixer control handle, by the mixer control's id. |
| 237 | * For const access, see @ref mixer_get_ctl_const |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 238 | * @param mixer An initialized mixer handle. |
| 239 | * @param id The control's id in the given mixer. |
| 240 | * @returns A handle to the mixer control. |
| 241 | * @ingroup libtinyalsa-mixer |
| 242 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 243 | struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id) |
| 244 | { |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 245 | if (mixer && (id < mixer->count)) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 246 | return mixer->ctl + id; |
| 247 | |
| 248 | return NULL; |
| 249 | } |
| 250 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 251 | /** Gets the first instance of mixer control handle, by the mixer control's name. |
| 252 | * @param mixer An initialized mixer handle. |
| 253 | * @param name The control's name in the given mixer. |
| 254 | * @returns A handle to the mixer control. |
| 255 | * @ingroup libtinyalsa-mixer |
| 256 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 257 | struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name) |
| 258 | { |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 259 | return mixer_get_ctl_by_name_and_index(mixer, name, 0); |
| 260 | } |
| 261 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 262 | /** Gets an instance of mixer control handle, by the mixer control's name and index. |
| 263 | * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control. |
| 264 | * @param mixer An initialized mixer handle. |
| 265 | * @param name The control's name in the given mixer. |
| 266 | * @param index The control's index. |
| 267 | * @returns A handle to the mixer control. |
| 268 | * @ingroup libtinyalsa-mixer |
| 269 | */ |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 270 | struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer, |
| 271 | const char *name, |
| 272 | unsigned int index) |
| 273 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 274 | unsigned int n; |
Svyatoslav Mishyn | c732836 | 2016-01-24 19:43:38 +0200 | [diff] [blame] | 275 | struct mixer_ctl *ctl; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 276 | |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 277 | if (!mixer) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 278 | return NULL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 279 | |
Svyatoslav Mishyn | c732836 | 2016-01-24 19:43:38 +0200 | [diff] [blame] | 280 | ctl = mixer->ctl; |
| 281 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 282 | for (n = 0; n < mixer->count; n++) |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 283 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 284 | if (index-- == 0) |
| 285 | return mixer->ctl + n; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 286 | |
| 287 | return NULL; |
| 288 | } |
| 289 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 290 | /** Updates the control's info. |
| 291 | * This is useful for a program that may be idle for a period of time. |
| 292 | * @param ctl An initialized control handle. |
| 293 | * @ingroup libtinyalsa-mixer |
| 294 | */ |
Simon Wilson | 710df88 | 2013-06-28 16:17:50 -0700 | [diff] [blame] | 295 | void mixer_ctl_update(struct mixer_ctl *ctl) |
| 296 | { |
| 297 | ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info); |
| 298 | } |
| 299 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 300 | /** Gets the control's ID. |
| 301 | * @param ctl An initialized control handle. |
| 302 | * @returns On success, the control's ID is returned. |
| 303 | * On error, UINT_MAX is returned instead. |
| 304 | * @ingroup libtinyalsa-mixer |
| 305 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 306 | unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl) |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 307 | { |
| 308 | if (!ctl) |
| 309 | return UINT_MAX; |
| 310 | |
| 311 | /* numid values start at 1, return a 0-base value that |
| 312 | * can be passed to mixer_get_ctl() |
| 313 | */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 314 | return ctl->info.id.numid - 1; |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 317 | /** Gets the name of the control. |
| 318 | * @param ctl An initialized control handle. |
| 319 | * @returns On success, the name of the control. |
| 320 | * On error, NULL. |
| 321 | * @ingroup libtinyalsa-mixer |
| 322 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 323 | const char *mixer_ctl_get_name(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 324 | { |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 325 | if (!ctl) |
| 326 | return NULL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 327 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 328 | return (const char *)ctl->info.id.name; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 331 | /** Gets the value type of the control. |
| 332 | * @param ctl An initialized control handle |
| 333 | * @returns On success, the type of mixer control. |
| 334 | * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN |
| 335 | * @ingroup libtinyalsa-mixer |
| 336 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 337 | 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] | 338 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 339 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 340 | return MIXER_CTL_TYPE_UNKNOWN; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 341 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 342 | switch (ctl->info.type) { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 343 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL; |
| 344 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT; |
| 345 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM; |
| 346 | case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE; |
| 347 | case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958; |
| 348 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64; |
| 349 | default: return MIXER_CTL_TYPE_UNKNOWN; |
| 350 | }; |
| 351 | } |
| 352 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 353 | /** Gets the string that describes the value type of the control. |
| 354 | * @param ctl An initialized control handle |
| 355 | * @returns On success, a string describing type of mixer control. |
| 356 | * @ingroup libtinyalsa-mixer |
| 357 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 358 | const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 359 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 360 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 361 | return ""; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 362 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 363 | switch (ctl->info.type) { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 364 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL"; |
| 365 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT"; |
| 366 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM"; |
| 367 | case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE"; |
| 368 | case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958"; |
| 369 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64"; |
| 370 | default: return "Unknown"; |
| 371 | }; |
| 372 | } |
| 373 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 374 | /** Gets the number of values in the control. |
| 375 | * @param ctl An initialized control handle |
| 376 | * @returns The number of values in the mixer control |
| 377 | * @ingroup libtinyalsa-mixer |
| 378 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 379 | unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 380 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 381 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 382 | return 0; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 383 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 384 | return ctl->info.count; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 387 | 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] | 388 | { |
Baptiste Robert | 4a484e1 | 2013-09-12 15:37:53 +0200 | [diff] [blame] | 389 | if ((percent > 100) || (percent < 0)) { |
| 390 | return -EINVAL; |
| 391 | } |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 392 | |
Baptiste Robert | 4a484e1 | 2013-09-12 15:37:53 +0200 | [diff] [blame] | 393 | int range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 394 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 395 | return ei->value.integer.min + (range * percent) / 100; |
| 396 | } |
| 397 | |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 398 | 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] | 399 | { |
| 400 | int range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 401 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 402 | if (range == 0) |
| 403 | return 0; |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 404 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 405 | return ((value - ei->value.integer.min) / range) * 100; |
| 406 | } |
| 407 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 408 | /** Gets a percentage representation of a specified control value. |
| 409 | * @param ctl An initialized control handle. |
| 410 | * @param id The index of the value within the control. |
| 411 | * @returns On success, the percentage representation of the control value. |
| 412 | * On failure, -EINVAL is returned. |
| 413 | * @ingroup libtinyalsa-mixer |
| 414 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 415 | 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] | 416 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 417 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 418 | return -EINVAL; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 419 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 420 | return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id)); |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 423 | /** Sets the value of a control by percent, specified by the value index. |
| 424 | * @param ctl An initialized control handle. |
| 425 | * @param id The index of the value to set |
| 426 | * @param percent A percentage value between 0 and 100. |
| 427 | * @returns On success, zero is returned. |
| 428 | * On failure, non-zero is returned. |
| 429 | * @ingroup libtinyalsa-mixer |
| 430 | */ |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 431 | 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] | 432 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 433 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 434 | return -EINVAL; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 435 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 436 | 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] | 437 | } |
| 438 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 439 | /** Gets the value of a control. |
| 440 | * @param ctl An initialized control handle. |
| 441 | * @param id The index of the control value. |
| 442 | * @returns On success, the specified value is returned. |
| 443 | * On failure, -EINVAL is returned. |
| 444 | * @ingroup libtinyalsa-mixer |
| 445 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 446 | 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] | 447 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 448 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 449 | int ret; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 450 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 451 | if (!ctl || (id >= ctl->info.count)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 452 | return -EINVAL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 453 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 454 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 455 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 456 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 457 | if (ret < 0) |
| 458 | return ret; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 459 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 460 | switch (ctl->info.type) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 461 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 462 | return !!ev.value.integer.value[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 463 | |
| 464 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 465 | return ev.value.integer.value[id]; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 466 | |
| 467 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 468 | return ev.value.enumerated.item[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 469 | |
Pierre-Louis Bossart | 5251016 | 2011-10-24 15:00:17 -0500 | [diff] [blame] | 470 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 471 | return ev.value.bytes.data[id]; |
| 472 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 473 | default: |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 474 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 480 | /** Gets the contents of a control's value array. |
| 481 | * @param ctl An initialized control handle. |
| 482 | * @param array A pointer to write the array data to. |
| 483 | * The size of this array must be equal to the number of items in the array |
| 484 | * multiplied by the size of each item. |
| 485 | * @param count The number of items in the array. |
| 486 | * This parameter must match the number of items in the control. |
| 487 | * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values |
| 488 | * @returns On success, zero. |
| 489 | * On failure, non-zero. |
| 490 | * @ingroup libtinyalsa-mixer |
| 491 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 492 | 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] | 493 | { |
| 494 | struct snd_ctl_elem_value ev; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 495 | int ret = 0; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 496 | size_t size; |
| 497 | void *source; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 498 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 499 | if (!ctl || (count > ctl->info.count) || !count || !array) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 500 | return -EINVAL; |
| 501 | |
| 502 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 503 | ev.id.numid = ctl->info.id.numid; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 504 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 505 | switch (ctl->info.type) { |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 506 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 507 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 508 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 509 | if (ret < 0) |
| 510 | return ret; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 511 | size = sizeof(ev.value.integer.value[0]); |
| 512 | source = ev.value.integer.value; |
| 513 | break; |
| 514 | |
| 515 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 516 | /* check if this is new bytes TLV */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 517 | if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) { |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 518 | struct snd_ctl_tlv *tlv; |
| 519 | int ret; |
| 520 | |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 521 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 522 | return -EINVAL; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 523 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 524 | if (!tlv) |
| 525 | return -ENOMEM; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 526 | tlv->numid = ctl->info.id.numid; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 527 | tlv->length = count; |
| 528 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv); |
| 529 | |
| 530 | source = tlv->tlv; |
| 531 | memcpy(array, source, count); |
| 532 | |
| 533 | free(tlv); |
| 534 | |
| 535 | return ret; |
| 536 | } else { |
| 537 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 538 | if (ret < 0) |
| 539 | return ret; |
| 540 | size = sizeof(ev.value.bytes.data[0]); |
| 541 | source = ev.value.bytes.data; |
| 542 | break; |
| 543 | } |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 544 | |
| 545 | default: |
| 546 | return -EINVAL; |
| 547 | } |
| 548 | |
| 549 | memcpy(array, source, size * count); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 554 | /** Sets the value of a control, specified by the value index. |
| 555 | * @param ctl An initialized control handle. |
| 556 | * @param id The index of the value within the control. |
| 557 | * @param value The value to set. |
| 558 | * This must be in a range specified by @ref mixer_ctl_get_range_min |
| 559 | * and @ref mixer_ctl_get_range_max. |
| 560 | * @returns On success, zero is returned. |
| 561 | * On failure, non-zero is returned. |
| 562 | * @ingroup libtinyalsa-mixer |
| 563 | */ |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 564 | 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] | 565 | { |
| 566 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 567 | int ret; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 568 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 569 | if (!ctl || (id >= ctl->info.count)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 570 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 571 | |
| 572 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 573 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 574 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 575 | if (ret < 0) |
| 576 | return ret; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 577 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 578 | switch (ctl->info.type) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 579 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 580 | ev.value.integer.value[id] = !!value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 581 | break; |
| 582 | |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 583 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Baptiste Robert | 6cd7d5f | 2013-08-07 11:32:45 +0200 | [diff] [blame] | 584 | if ((value < mixer_ctl_get_range_min(ctl)) || |
| 585 | (value > mixer_ctl_get_range_max(ctl))) { |
| 586 | return -EINVAL; |
| 587 | } |
| 588 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 589 | ev.value.integer.value[id] = value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 590 | break; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 591 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 592 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 593 | ev.value.enumerated.item[id] = value; |
| 594 | break; |
| 595 | |
David.Coutherut | ce2b634 | 2013-06-11 16:22:57 +0200 | [diff] [blame] | 596 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 597 | ev.value.bytes.data[id] = value; |
| 598 | break; |
| 599 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 600 | default: |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 601 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 602 | } |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 603 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 604 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 605 | } |
| 606 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 607 | /** Sets the contents of a control's value array. |
| 608 | * @param ctl An initialized control handle. |
| 609 | * @param array The array containing control values. |
| 610 | * @param count The number of values in the array. |
| 611 | * This must match the number of values in the control. |
| 612 | * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values |
| 613 | * @returns On success, zero. |
| 614 | * On failure, non-zero. |
| 615 | * @ingroup libtinyalsa-mixer |
| 616 | */ |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 617 | 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] | 618 | { |
| 619 | struct snd_ctl_elem_value ev; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 620 | size_t size; |
| 621 | void *dest; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 622 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 623 | if (!ctl || (count > ctl->info.count) || !count || !array) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 624 | return -EINVAL; |
| 625 | |
| 626 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 627 | ev.id.numid = ctl->info.id.numid; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 628 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 629 | switch (ctl->info.type) { |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 630 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 631 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 632 | size = sizeof(ev.value.integer.value[0]); |
| 633 | dest = ev.value.integer.value; |
| 634 | break; |
| 635 | |
| 636 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 637 | /* check if this is new bytes TLV */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 638 | if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) { |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 639 | struct snd_ctl_tlv *tlv; |
| 640 | int ret = 0; |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 641 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 642 | return -EINVAL; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 643 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 644 | if (!tlv) |
| 645 | return -ENOMEM; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 646 | tlv->numid = ctl->info.id.numid; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 647 | tlv->length = count; |
| 648 | memcpy(tlv->tlv, array, count); |
| 649 | |
| 650 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv); |
| 651 | free(tlv); |
| 652 | |
| 653 | return ret; |
| 654 | } else { |
| 655 | size = sizeof(ev.value.bytes.data[0]); |
| 656 | dest = ev.value.bytes.data; |
| 657 | } |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 658 | break; |
| 659 | |
| 660 | default: |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | |
| 664 | memcpy(dest, array, size * count); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 665 | |
| 666 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 667 | } |
| 668 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 669 | /** Gets the minimum value of an control. |
| 670 | * The control must have an integer type. |
| 671 | * The type of the control can be checked with @ref mixer_ctl_get_type. |
| 672 | * @param ctl An initialized control handle. |
| 673 | * @returns On success, the minimum value of the control. |
| 674 | * On failure, -EINVAL. |
| 675 | * @ingroup libtinyalsa-mixer |
| 676 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 677 | int mixer_ctl_get_range_min(const struct mixer_ctl *ctl) |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 678 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 679 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 680 | return -EINVAL; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 681 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 682 | return ctl->info.value.integer.min; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 685 | /** Gets the maximum value of an control. |
| 686 | * The control must have an integer type. |
| 687 | * The type of the control can be checked with @ref mixer_ctl_get_type. |
| 688 | * @param ctl An initialized control handle. |
| 689 | * @returns On success, the maximum value of the control. |
| 690 | * On failure, -EINVAL. |
| 691 | * @ingroup libtinyalsa-mixer |
| 692 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 693 | int mixer_ctl_get_range_max(const struct mixer_ctl *ctl) |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 694 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 695 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 696 | return -EINVAL; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 697 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 698 | return ctl->info.value.integer.max; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 701 | /** Get the number of enumerated items in the control. |
| 702 | * @param ctl An initialized control handle. |
| 703 | * @returns The number of enumerated items in the control. |
| 704 | * @ingroup libtinyalsa-mixer |
| 705 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 706 | unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl) |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 707 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 708 | if (!ctl) |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 709 | return 0; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 710 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 711 | return ctl->info.value.enumerated.items; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 712 | } |
| 713 | |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 714 | int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl) |
| 715 | { |
| 716 | struct snd_ctl_elem_info tmp; |
| 717 | unsigned int m; |
| 718 | char **enames; |
| 719 | |
| 720 | if (ctl->ename) { |
| 721 | return 0; |
| 722 | } |
| 723 | |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 724 | enames = calloc(ctl->info.value.enumerated.items, sizeof(char*)); |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 725 | if (!enames) |
| 726 | goto fail; |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 727 | for (m = 0; m < ctl->info.value.enumerated.items; m++) { |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 728 | memset(&tmp, 0, sizeof(tmp)); |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 729 | tmp.id.numid = ctl->info.id.numid; |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 730 | tmp.value.enumerated.item = m; |
| 731 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0) |
| 732 | goto fail; |
| 733 | enames[m] = strdup(tmp.value.enumerated.name); |
| 734 | if (!enames[m]) |
| 735 | goto fail; |
| 736 | } |
| 737 | ctl->ename = enames; |
| 738 | return 0; |
| 739 | |
| 740 | fail: |
| 741 | if (enames) { |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 742 | for (m = 0; m < ctl->info.value.enumerated.items; m++) { |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 743 | if (enames[m]) { |
| 744 | free(enames[m]); |
| 745 | } |
| 746 | } |
| 747 | free(enames); |
| 748 | } |
| 749 | return -1; |
| 750 | } |
| 751 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 752 | /** Gets the string representation of an enumerated item. |
| 753 | * @param ctl An initialized control handle. |
| 754 | * @param enum_id The index of the enumerated value. |
| 755 | * @returns A string representation of the enumerated item. |
| 756 | * @ingroup libtinyalsa-mixer |
| 757 | */ |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 758 | const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, |
| 759 | unsigned int enum_id) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 760 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 761 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 762 | (enum_id >= ctl->info.value.enumerated.items) || |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 763 | mixer_ctl_fill_enum_string(ctl) != 0) |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 764 | return NULL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 765 | |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 766 | return (const char *)ctl->ename[enum_id]; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 769 | /** Set an enumeration value by string value. |
| 770 | * @param ctl An enumerated mixer control. |
| 771 | * @param string The string representation of an enumeration. |
| 772 | * @returns On success, zero. |
| 773 | * On failure, zero. |
| 774 | * @ingroup libtinyalsa-mixer |
| 775 | */ |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 776 | int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string) |
| 777 | { |
| 778 | unsigned int i, num_enums; |
| 779 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 780 | int ret; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 781 | |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 782 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 783 | mixer_ctl_fill_enum_string(ctl) != 0) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 784 | return -EINVAL; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 785 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 786 | num_enums = ctl->info.value.enumerated.items; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 787 | for (i = 0; i < num_enums; i++) { |
| 788 | if (!strcmp(string, ctl->ename[i])) { |
| 789 | memset(&ev, 0, sizeof(ev)); |
| 790 | ev.value.enumerated.item[0] = i; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 791 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 792 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 793 | if (ret < 0) |
| 794 | return ret; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 795 | return 0; |
| 796 | } |
| 797 | } |
| 798 | |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 799 | return -EINVAL; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 800 | } |
| 801 | |