chiark / gitweb /
fatal() testing for dateparse()
[disorder] / lib / mixer.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
bd8895a8 3 * Copyright (C) 2007 Richard Kettlewell
460b9539 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 2 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, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
b25aac59 20/** @file lib/mixer.c
21 * @brief Mixer support
22 */
460b9539 23
05b75f8d 24#include "common.h"
460b9539 25
460b9539 26#include "configuration.h"
27#include "mixer.h"
28#include "log.h"
b25aac59 29#include "mem.h"
460b9539 30
b25aac59 31/** @brief Whether lack of volume support has been reported yet */
32static int none_reported;
33
34/** @brief Get/set volume stub if volume control is not supported */
35static int none_get_set(int attribute((unused)) *left,
36 int attribute((unused)) *right) {
37 if(!none_reported) {
38 error(0, "don't know how to get/set volume with this api");
39 none_reported = 1;
40 }
41 return -1;
42}
43
44/** @brief Stub mixer control */
45static const struct mixer mixer_none = {
46 -1,
47 none_get_set,
48 none_get_set,
49 "",
50 ""
51};
52
53/** @brief Table of mixer definitions */
bd8895a8 54static const struct mixer *mixers[] = {
460b9539 55#if HAVE_SYS_SOUNDCARD_H
b25aac59 56 &mixer_oss,
57#endif
58#if HAVE_ALSA_ASOUNDLIB_H
59 &mixer_alsa,
460b9539 60#endif
b25aac59 61 &mixer_none /* make sure array is never empty */
bd8895a8 62};
460b9539 63
b25aac59 64/** @brief Number of mixer definitions */
bd8895a8 65#define NMIXERS (sizeof mixers / sizeof *mixers)
66
b25aac59 67/** @brief Find the mixer definition */
bd8895a8 68static const struct mixer *find_mixer(int api) {
69 size_t n;
70
3c499fe7
RK
71 if(api == -1)
72 api = config->api;
bd8895a8 73 for(n = 0; n < NMIXERS; ++n)
74 if(mixers[n]->api == api)
75 return mixers[n];
b25aac59 76 return &mixer_none;
460b9539 77}
78
3c499fe7
RK
79/** @brief Return true if we know how to drive the mixer
80 * @param api Sound api or -1 for default
81 * @return true if suppored, false otherwise
82 */
83int mixer_supported(int api) {
84 const struct mixer *const m = find_mixer(api);
85 return m != &mixer_none;
86}
87
b25aac59 88/** @brief Get/set volume
3c499fe7 89 * @param api Sound api or -1 for default
b25aac59 90 * @param left Left channel level, 0-100
91 * @param right Right channel level, 0-100
92 * @param set Set volume if non-0
93 * @return 0 on success, non-0 on error
94 *
95 * If getting the volume then @p left and @p right are filled in.
96 *
97 * If setting the volume then the target levels are read from @p left and
98 * @p right, and the actual level set is stored in them.
99 */
3c499fe7
RK
100int mixer_control(int api, int *left, int *right, int set) {
101 const struct mixer *const m = find_mixer(api);
bd8895a8 102
b25aac59 103 /* We impose defaults bizarrely late, but this has the advantage of
104 * not making everything depend on sound libraries */
105 if(!config->mixer)
106 config->mixer = xstrdup(m->device);
107 if(!config->channel)
108 config->channel = xstrdup(m->channel);
109 if(set)
110 return m->set(left, right);
111 else
112 return m->get(left, right);
460b9539 113}
460b9539 114
115/*
116Local Variables:
117c-basic-offset:2
118comment-column:40
119End:
120*/