1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/*
* Copyright 2004-2005 Timo Hirvonen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include "mixer.h"
#include "op.h"
#include "xmalloc.h"
#include "debug.h"
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API
#include <alsa/asoundlib.h>
static snd_mixer_t *alsa_mixer_handle;
static snd_mixer_elem_t *mixer_elem = NULL;
static long mixer_vol_min, mixer_vol_max;
/* configuration */
static char *alsa_mixer_device = NULL;
static char *alsa_mixer_element = NULL;
static int alsa_mixer_init(void)
{
if (alsa_mixer_device == NULL)
alsa_mixer_device = xstrdup("default");
if (alsa_mixer_element == NULL)
alsa_mixer_element = xstrdup("PCM");
/* FIXME: check device */
return 0;
}
static int alsa_mixer_exit(void)
{
free(alsa_mixer_device);
alsa_mixer_device = NULL;
free(alsa_mixer_element);
alsa_mixer_element = NULL;
return 0;
}
static int alsa_mixer_open(int *volume_max)
{
snd_mixer_selem_id_t *sid;
snd_mixer_elem_t *elem;
int count;
int rc;
snd_mixer_selem_id_alloca(&sid);
rc = snd_mixer_open(&alsa_mixer_handle, 0);
if (rc < 0)
goto error;
rc = snd_mixer_attach(alsa_mixer_handle, alsa_mixer_device);
if (rc < 0)
goto error;
rc = snd_mixer_selem_register(alsa_mixer_handle, NULL, NULL);
if (rc < 0)
goto error;
rc = snd_mixer_load(alsa_mixer_handle);
if (rc < 0)
goto error;
count = snd_mixer_get_count(alsa_mixer_handle);
if (count == 0) {
d_print("error: mixer does not have elements\n");
return -2;
}
elem = snd_mixer_first_elem(alsa_mixer_handle);
while (elem) {
const char *name;
int has_vol, has_switch;
snd_mixer_selem_get_id(elem, sid);
name = snd_mixer_selem_id_get_name(sid);
d_print("name = %s\n", name);
d_print("has playback volume = %d\n", snd_mixer_selem_has_playback_volume(elem));
d_print("has playback switch = %d\n", snd_mixer_selem_has_playback_switch(elem));
if (strcasecmp(name, alsa_mixer_element)) {
elem = snd_mixer_elem_next(elem);
continue;
}
has_vol = snd_mixer_selem_has_playback_volume(elem);
if (!has_vol) {
d_print("mixer element `%s' does not have playback volume\n", name);
return -2;
}
snd_mixer_selem_get_playback_volume_range(elem,
&mixer_vol_min, &mixer_vol_max);
has_switch = snd_mixer_selem_has_playback_switch(elem);
/* FIXME: get number of channels */
mixer_elem = elem;
*volume_max = mixer_vol_max - mixer_vol_min;
return 0;
}
d_print("error: mixer element `%s' not found\n", alsa_mixer_element);
return -2;
error:
d_print("error: %s\n", snd_strerror(rc));
return -1;
}
static int alsa_mixer_close(void)
{
snd_mixer_close(alsa_mixer_handle);
return 0;
}
static int alsa_mixer_set_volume(int l, int r)
{
if (mixer_elem == NULL) {
return -1;
}
l += mixer_vol_min;
r += mixer_vol_min;
if (l > mixer_vol_max)
d_print("error: left volume too high (%d > %ld)\n",
l, mixer_vol_max);
if (r > mixer_vol_max)
d_print("error: right volume too high (%d > %ld)\n",
r, mixer_vol_max);
snd_mixer_selem_set_playback_volume(mixer_elem, SND_MIXER_SCHN_FRONT_LEFT, l);
snd_mixer_selem_set_playback_volume(mixer_elem, SND_MIXER_SCHN_FRONT_RIGHT, r);
return 0;
}
static int alsa_mixer_get_volume(int *l, int *r)
{
long lv, rv;
if (mixer_elem == NULL)
return -1;
snd_mixer_handle_events(alsa_mixer_handle);
snd_mixer_selem_get_playback_volume(mixer_elem, SND_MIXER_SCHN_FRONT_LEFT, &lv);
snd_mixer_selem_get_playback_volume(mixer_elem, SND_MIXER_SCHN_FRONT_RIGHT, &rv);
*l = lv - mixer_vol_min;
*r = rv - mixer_vol_min;
return 0;
}
static int alsa_mixer_set_option(int key, const char *val)
{
switch (key) {
case 0:
free(alsa_mixer_element);
alsa_mixer_element = xstrdup(val);
break;
case 1:
free(alsa_mixer_device);
alsa_mixer_device = xstrdup(val);
break;
default:
return -OP_ERROR_NOT_OPTION;
}
return 0;
}
static int alsa_mixer_get_option(int key, char **val)
{
switch (key) {
case 0:
if (alsa_mixer_element)
*val = xstrdup(alsa_mixer_element);
break;
case 1:
if (alsa_mixer_device)
*val = xstrdup(alsa_mixer_device);
break;
default:
return -OP_ERROR_NOT_OPTION;
}
return 0;
}
const struct mixer_plugin_ops op_mixer_ops = {
.init = alsa_mixer_init,
.exit = alsa_mixer_exit,
.open = alsa_mixer_open,
.close = alsa_mixer_close,
.set_volume = alsa_mixer_set_volume,
.get_volume = alsa_mixer_get_volume,
.set_option = alsa_mixer_set_option,
.get_option = alsa_mixer_get_option
};
const char * const op_mixer_options[] = {
"channel",
"device",
NULL
};
|