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> |
| 31 | #include <string.h> |
| 32 | #include <unistd.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <errno.h> |
| 35 | #include <ctype.h> |
| 36 | |
| 37 | #include <linux/ioctl.h> |
| 38 | #define __force |
| 39 | #define __bitwise |
| 40 | #define __user |
| 41 | #include <sound/asound.h> |
| 42 | |
| 43 | #include <tinyalsa/asoundlib.h> |
| 44 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 45 | struct mixer_ctl { |
| 46 | struct mixer *mixer; |
| 47 | struct snd_ctl_elem_info *info; |
| 48 | char **ename; |
| 49 | }; |
| 50 | |
| 51 | struct mixer { |
| 52 | int fd; |
| 53 | struct snd_ctl_elem_info *info; |
| 54 | struct mixer_ctl *ctl; |
| 55 | unsigned int count; |
| 56 | }; |
| 57 | |
| 58 | void mixer_close(struct mixer *mixer) |
| 59 | { |
| 60 | unsigned int n,m; |
| 61 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 62 | if (!mixer) { |
| 63 | errno = EINVAL; |
| 64 | return; |
| 65 | } |
| 66 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 67 | if (mixer->fd >= 0) |
| 68 | close(mixer->fd); |
| 69 | |
| 70 | if (mixer->ctl) { |
| 71 | for (n = 0; n < mixer->count; n++) { |
| 72 | if (mixer->ctl[n].ename) { |
| 73 | unsigned int max = mixer->ctl[n].info->value.enumerated.items; |
| 74 | for (m = 0; m < max; m++) |
| 75 | free(mixer->ctl[n].ename[m]); |
| 76 | free(mixer->ctl[n].ename); |
| 77 | } |
| 78 | } |
| 79 | free(mixer->ctl); |
| 80 | } |
| 81 | |
| 82 | if (mixer->info) |
| 83 | free(mixer->info); |
| 84 | |
| 85 | free(mixer); |
| 86 | |
| 87 | /* TODO: verify frees */ |
| 88 | } |
| 89 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 90 | struct mixer *mixer_open(unsigned int card) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 91 | { |
| 92 | struct snd_ctl_elem_list elist; |
| 93 | struct snd_ctl_elem_info tmp; |
| 94 | struct snd_ctl_elem_id *eid = NULL; |
| 95 | struct mixer *mixer = NULL; |
| 96 | unsigned int n, m; |
| 97 | int fd; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 98 | char fn[256]; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 99 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 100 | snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card); |
| 101 | fd = open(fn, O_RDWR); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 102 | if (fd < 0) |
| 103 | return 0; |
| 104 | |
| 105 | memset(&elist, 0, sizeof(elist)); |
| 106 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 107 | goto fail; |
| 108 | |
| 109 | mixer = calloc(1, sizeof(*mixer)); |
| 110 | if (!mixer) |
| 111 | goto fail; |
| 112 | |
| 113 | mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl)); |
| 114 | mixer->info = calloc(elist.count, sizeof(struct snd_ctl_elem_info)); |
| 115 | if (!mixer->ctl || !mixer->info) |
| 116 | goto fail; |
| 117 | |
| 118 | eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id)); |
| 119 | if (!eid) |
| 120 | goto fail; |
| 121 | |
| 122 | mixer->count = elist.count; |
| 123 | mixer->fd = fd; |
| 124 | elist.space = mixer->count; |
| 125 | elist.pids = eid; |
| 126 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 127 | goto fail; |
| 128 | |
| 129 | for (n = 0; n < mixer->count; n++) { |
| 130 | struct snd_ctl_elem_info *ei = mixer->info + n; |
| 131 | ei->id.numid = eid[n].numid; |
| 132 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0) |
| 133 | goto fail; |
| 134 | mixer->ctl[n].info = ei; |
| 135 | mixer->ctl[n].mixer = mixer; |
| 136 | if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) { |
| 137 | char **enames = calloc(ei->value.enumerated.items, sizeof(char*)); |
| 138 | if (!enames) |
| 139 | goto fail; |
| 140 | mixer->ctl[n].ename = enames; |
| 141 | for (m = 0; m < ei->value.enumerated.items; m++) { |
| 142 | memset(&tmp, 0, sizeof(tmp)); |
| 143 | tmp.id.numid = ei->id.numid; |
| 144 | tmp.value.enumerated.item = m; |
| 145 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0) |
| 146 | goto fail; |
| 147 | enames[m] = strdup(tmp.value.enumerated.name); |
| 148 | if (!enames[m]) |
| 149 | goto fail; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | free(eid); |
| 155 | return mixer; |
| 156 | |
| 157 | fail: |
| 158 | /* TODO: verify frees in failure case */ |
| 159 | if (eid) |
| 160 | free(eid); |
| 161 | if (mixer) |
| 162 | mixer_close(mixer); |
| 163 | else if (fd >= 0) |
| 164 | close(fd); |
| 165 | return 0; |
| 166 | } |
| 167 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 168 | unsigned int mixer_get_num_ctls(struct mixer *mixer) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 169 | { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 170 | if (!mixer) { |
| 171 | errno = EINVAL; |
| 172 | return -1; |
| 173 | } |
| 174 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 175 | return mixer->count; |
| 176 | } |
| 177 | |
| 178 | struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id) |
| 179 | { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 180 | if (!mixer) { |
| 181 | errno = EINVAL; |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 185 | if (id < mixer->count) |
| 186 | return mixer->ctl + id; |
| 187 | |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name) |
| 192 | { |
| 193 | unsigned int n; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 194 | |
| 195 | if (!mixer) { |
| 196 | errno = EINVAL; |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 200 | for (n = 0; n < mixer->count; n++) |
| 201 | if (!strcmp(name, (char*) mixer->info[n].id.name)) |
| 202 | return mixer->ctl + n; |
| 203 | |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 207 | int mixer_ctl_get_name(struct mixer_ctl *ctl, char *name, unsigned int size) |
| 208 | { |
| 209 | if (!ctl || !name || (size == 0)) { |
| 210 | errno = EINVAL; |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | strncpy(name, (char *)ctl->info->id.name, size); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl) |
| 219 | { |
| 220 | if (!ctl) { |
| 221 | errno = EINVAL; |
| 222 | return MIXER_CTL_TYPE_UNKNOWN; |
| 223 | } |
| 224 | |
| 225 | switch (ctl->info->type) { |
| 226 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL; |
| 227 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT; |
| 228 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM; |
| 229 | case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE; |
| 230 | case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958; |
| 231 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64; |
| 232 | default: return MIXER_CTL_TYPE_UNKNOWN; |
| 233 | }; |
| 234 | } |
| 235 | |
| 236 | const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl) |
| 237 | { |
| 238 | if (!ctl) { |
| 239 | errno = EINVAL; |
| 240 | return ""; |
| 241 | } |
| 242 | |
| 243 | switch (ctl->info->type) { |
| 244 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL"; |
| 245 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT"; |
| 246 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM"; |
| 247 | case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE"; |
| 248 | case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958"; |
| 249 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64"; |
| 250 | default: return "Unknown"; |
| 251 | }; |
| 252 | } |
| 253 | |
| 254 | unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl) |
| 255 | { |
| 256 | if (!ctl) { |
| 257 | errno = EINVAL; |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | return ctl->info->count; |
| 262 | } |
| 263 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 264 | static int percent_to_int(struct snd_ctl_elem_info *ei, int percent) |
| 265 | { |
| 266 | int range; |
| 267 | |
| 268 | if (percent > 100) |
| 269 | percent = 100; |
| 270 | else if (percent < 0) |
| 271 | percent = 0; |
| 272 | |
| 273 | range = (ei->value.integer.max - ei->value.integer.min); |
| 274 | |
| 275 | return ei->value.integer.min + (range * percent) / 100; |
| 276 | } |
| 277 | |
| 278 | static int int_to_percent(struct snd_ctl_elem_info *ei, int value) |
| 279 | { |
| 280 | int range = (ei->value.integer.max - ei->value.integer.min); |
| 281 | |
| 282 | if (range == 0) |
| 283 | return 0; |
| 284 | |
| 285 | return ((value - ei->value.integer.min) / range) * 100; |
| 286 | } |
| 287 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 288 | int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 289 | { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 290 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) { |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 291 | errno = EINVAL; |
| 292 | return -1; |
| 293 | } |
| 294 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 295 | return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id)); |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 298 | 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] | 299 | { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 300 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) { |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 301 | errno = EINVAL; |
| 302 | return -1; |
| 303 | } |
| 304 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 305 | 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] | 306 | } |
| 307 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 308 | int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 309 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 310 | struct snd_ctl_elem_value ev; |
| 311 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 312 | if (!ctl || (id >= ctl->info->count)) { |
| 313 | errno = EINVAL; |
| 314 | return -1; |
| 315 | } |
| 316 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 317 | memset(&ev, 0, sizeof(ev)); |
| 318 | ev.id.numid = ctl->info->id.numid; |
| 319 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev)) |
| 320 | return -1; |
| 321 | |
| 322 | switch (ctl->info->type) { |
| 323 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 324 | return !!ev.value.integer.value[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 325 | |
| 326 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 327 | return ev.value.integer.value[id]; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 328 | |
| 329 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 330 | return ev.value.enumerated.item[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 331 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 332 | default: |
| 333 | errno = EINVAL; |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 340 | 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] | 341 | { |
| 342 | struct snd_ctl_elem_value ev; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 343 | |
| 344 | if (!ctl || (id >= ctl->info->count)) { |
| 345 | errno = EINVAL; |
| 346 | return -1; |
| 347 | } |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 348 | |
| 349 | memset(&ev, 0, sizeof(ev)); |
| 350 | ev.id.numid = ctl->info->id.numid; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 351 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev)) |
| 352 | return -1; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 353 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 354 | switch (ctl->info->type) { |
| 355 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 356 | ev.value.integer.value[id] = !!value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 357 | break; |
| 358 | |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 359 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 360 | ev.value.integer.value[id] = value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 361 | break; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 362 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 363 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 364 | ev.value.enumerated.item[id] = value; |
| 365 | break; |
| 366 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 367 | default: |
| 368 | errno = EINVAL; |
| 369 | return -1; |
| 370 | } |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 371 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 372 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 373 | } |
| 374 | |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame^] | 375 | int mixer_ctl_get_range_min(struct mixer_ctl *ctl) |
| 376 | { |
| 377 | struct snd_ctl_elem_value ev; |
| 378 | |
| 379 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) { |
| 380 | errno = EINVAL; |
| 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | memset(&ev, 0, sizeof(ev)); |
| 385 | ev.id.numid = ctl->info->id.numid; |
| 386 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev)) |
| 387 | return -1; |
| 388 | |
| 389 | return ctl->info->value.integer.min; |
| 390 | } |
| 391 | |
| 392 | int mixer_ctl_get_range_max(struct mixer_ctl *ctl) |
| 393 | { |
| 394 | struct snd_ctl_elem_value ev; |
| 395 | |
| 396 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) { |
| 397 | errno = EINVAL; |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | memset(&ev, 0, sizeof(ev)); |
| 402 | ev.id.numid = ctl->info->id.numid; |
| 403 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev)) |
| 404 | return -1; |
| 405 | |
| 406 | return ctl->info->value.integer.max; |
| 407 | } |
| 408 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 409 | unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl) |
| 410 | { |
| 411 | if (!ctl) { |
| 412 | errno = EINVAL; |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | return ctl->info->value.enumerated.items; |
| 417 | } |
| 418 | |
| 419 | int mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id, |
| 420 | char *string, unsigned int size) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 421 | { |
| 422 | struct snd_ctl_elem_value ev; |
| 423 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 424 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 425 | (enum_id >= ctl->info->value.enumerated.items)) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 426 | errno = EINVAL; |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | memset(&ev, 0, sizeof(ev)); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 431 | ev.id.numid = ctl->info->id.numid; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 432 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev)) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 433 | return -1; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 434 | strncpy(string, (char *)ctl->ename[enum_id], size); |
| 435 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 439 | int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string) |
| 440 | { |
| 441 | unsigned int i, num_enums; |
| 442 | struct snd_ctl_elem_value ev; |
| 443 | |
| 444 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED)) { |
| 445 | errno = EINVAL; |
| 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | num_enums = ctl->info->value.enumerated.items; |
| 450 | for (i = 0; i < num_enums; i++) { |
| 451 | if (!strcmp(string, ctl->ename[i])) { |
| 452 | memset(&ev, 0, sizeof(ev)); |
| 453 | ev.value.enumerated.item[0] = i; |
| 454 | ev.id.numid = ctl->info->id.numid; |
| 455 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev) < 0) |
| 456 | return -1; |
| 457 | return 0; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | errno = EINVAL; |
| 462 | return -1; |
| 463 | } |
| 464 | |