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