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