| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2007 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file lib/mixer.c |
| 19 | * @brief Mixer support |
| 20 | */ |
| 21 | |
| 22 | #include "common.h" |
| 23 | |
| 24 | #include "configuration.h" |
| 25 | #include "mixer.h" |
| 26 | #include "log.h" |
| 27 | #include "mem.h" |
| 28 | |
| 29 | /** @brief Whether lack of volume support has been reported yet */ |
| 30 | static int none_reported; |
| 31 | |
| 32 | /** @brief Get/set volume stub if volume control is not supported */ |
| 33 | static int none_get_set(int attribute((unused)) *left, |
| 34 | int attribute((unused)) *right) { |
| 35 | if(!none_reported) { |
| 36 | error(0, "don't know how to get/set volume with this api"); |
| 37 | none_reported = 1; |
| 38 | } |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | /** @brief Stub mixer control */ |
| 43 | static const struct mixer mixer_none = { |
| 44 | -1, |
| 45 | none_get_set, |
| 46 | none_get_set, |
| 47 | "", |
| 48 | "" |
| 49 | }; |
| 50 | |
| 51 | /** @brief Table of mixer definitions */ |
| 52 | static const struct mixer *mixers[] = { |
| 53 | #if HAVE_SYS_SOUNDCARD_H |
| 54 | &mixer_oss, |
| 55 | #endif |
| 56 | #if HAVE_ALSA_ASOUNDLIB_H |
| 57 | &mixer_alsa, |
| 58 | #endif |
| 59 | &mixer_none /* make sure array is never empty */ |
| 60 | }; |
| 61 | |
| 62 | /** @brief Number of mixer definitions */ |
| 63 | #define NMIXERS (sizeof mixers / sizeof *mixers) |
| 64 | |
| 65 | /** @brief Find the mixer definition */ |
| 66 | static const struct mixer *find_mixer(int api) { |
| 67 | size_t n; |
| 68 | |
| 69 | if(api == -1) |
| 70 | api = config->api; |
| 71 | for(n = 0; n < NMIXERS; ++n) |
| 72 | if(mixers[n]->api == api) |
| 73 | return mixers[n]; |
| 74 | return &mixer_none; |
| 75 | } |
| 76 | |
| 77 | /** @brief Return true if we know how to drive the mixer |
| 78 | * @param api Sound api or -1 for default |
| 79 | * @return true if suppored, false otherwise |
| 80 | */ |
| 81 | int mixer_supported(int api) { |
| 82 | const struct mixer *const m = find_mixer(api); |
| 83 | return m != &mixer_none; |
| 84 | } |
| 85 | |
| 86 | /** @brief Get/set volume |
| 87 | * @param api Sound api or -1 for default |
| 88 | * @param left Left channel level, 0-100 |
| 89 | * @param right Right channel level, 0-100 |
| 90 | * @param set Set volume if non-0 |
| 91 | * @return 0 on success, non-0 on error |
| 92 | * |
| 93 | * If getting the volume then @p left and @p right are filled in. |
| 94 | * |
| 95 | * If setting the volume then the target levels are read from @p left and |
| 96 | * @p right, and the actual level set is stored in them. |
| 97 | */ |
| 98 | int mixer_control(int api, int *left, int *right, int set) { |
| 99 | const struct mixer *const m = find_mixer(api); |
| 100 | |
| 101 | /* We impose defaults bizarrely late, but this has the advantage of |
| 102 | * not making everything depend on sound libraries */ |
| 103 | if(!config->mixer) |
| 104 | config->mixer = xstrdup(m->device); |
| 105 | if(!config->channel) |
| 106 | config->channel = xstrdup(m->channel); |
| 107 | if(set) |
| 108 | return m->set(left, right); |
| 109 | else |
| 110 | return m->get(left, right); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | Local Variables: |
| 115 | c-basic-offset:2 |
| 116 | comment-column:40 |
| 117 | End: |
| 118 | */ |