1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * vim: ts=4 sw=4 noet ai cindent syntax=c
32 #include <sys/ioctl.h>
39 #include <alsa/asoundlib.h>
41 #ifdef HAVE_LINUX_SOUNDCARD_H
42 #include <linux/soundcard.h>
45 #include <soundcard.h>
47 #include <sys/soundcard.h>
48 #endif /* __OpenBSD__ */
49 #endif /* HAVE_LINUX_SOUNDCARD_H */
50 #endif /* MIXER_IS_ALSA */
52 #define MIXER_DEV "/dev/mixer"
56 struct mixer_control {
59 snd_mixer_selem_id_t *sid;
60 snd_mixer_elem_t *elem;
61 long vol_min, vol_max;
64 static struct mixer_control mixer_data[MAX_MIXERS];
66 static char soundcard[64] = "default";
69 static const char *devs[] = SOUND_DEVICE_NAMES;
73 static int parse_simple_id(const char *str, snd_mixer_selem_id_t *sid)
79 while (*str == ' ' || *str == '\t')
83 size = 1; /* for '\0' */
84 if (*str != '"' && *str != '\'') {
85 while (*str && *str != ',') {
86 if (size < (int)sizeof(buf)) {
94 while (*str && *str != c) {
95 if (size < (int)sizeof(buf)) {
105 snd_mixer_selem_id_set_index(sid, 0);
111 *ptr = 0; /* terminate the string */
115 snd_mixer_selem_id_set_index(sid, atoi(str));
117 snd_mixer_selem_id_set_name(sid, buf);
121 int mixer_init (const char *name)
123 /* from amixer.c, replaced -EINVAL with -1 */
128 for (i = 0; i < num_mixers; i++) {
129 if (!strcasecmp (mixer_data[i].name, name)) {
133 if (i == MAX_MIXERS) {
134 fprintf (stderr, "max mixers (%d) reached\n", MAX_MIXERS);
139 #define data mixer_data[i]
141 strncpy (mixer_data[i].name, name, 63);
142 mixer_data[i].name[63] = '\0';
143 snd_mixer_selem_id_alloca (&data.sid);
145 if (parse_simple_id (name, data.sid) < 0) {
146 fprintf (stderr, "Wrong mixer identifier: %s\n", name);
149 if ((err = snd_mixer_open (&data.mixer, 0)) < 0) {
150 fprintf (stderr, "snd_mixer_open: %s\n", snd_strerror (err));
153 if ((err = snd_mixer_attach (data.mixer, soundcard)) < 0) {
154 fprintf (stderr, "snd_mixer_attach: %s\n", snd_strerror (err));
157 if ((err = snd_mixer_selem_register (data.mixer, NULL, NULL)) < 0) {
158 fprintf (stderr, "snd_mixer_selem_register: %s\n",
162 if ((err = snd_mixer_load (data.mixer)) < 0) {
163 fprintf (stderr, "snd_mixer_load: %s\n", snd_strerror (err));
166 if (!(data.elem = snd_mixer_find_selem (data.mixer, data.sid))) {
167 fprintf (stderr, "snd_mixer_find_selem (\"%s\", %i)\n",
168 snd_mixer_selem_id_get_name (data.sid),
169 snd_mixer_selem_id_get_index (data.sid));
172 snd_mixer_selem_get_playback_volume_range(data.elem, &data.vol_min, &data.vol_max);
175 int mixer_get_avg (int i)
179 snd_mixer_handle_events (data.mixer);
180 snd_mixer_selem_get_playback_volume (data.elem, 0, &val);
183 int mixer_get_left (int i)
186 return mixer_get_avg (i);
188 int mixer_get_right (int i)
191 return mixer_get_avg (i);
193 int mixer_to_255(int i, int x)
195 return (x-data.vol_min)*255/(data.vol_max-data.vol_min);
197 int mixer_is_mute(int i)
199 snd_mixer_handle_events (data.mixer);
200 if (snd_mixer_selem_has_playback_switch (data.elem)) {
202 if ((err = snd_mixer_selem_get_playback_switch(data.elem, 0, &val)) < 0)
203 fprintf (stderr, "playback_switch: %s\n", snd_strerror (err));
206 return !mixer_get_avg(i);
212 int mixer_init(const char *name)
216 if (name == 0 || name[0] == '\0') {
222 mixer_fd = open(MIXER_DEV, O_RDONLY);
223 if (mixer_fd == -1) {
224 ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
229 for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
230 if (strcasecmp(devs[i], name) == 0) {
238 static int mixer_get(int i)
243 if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
245 ERR("mixer ioctl: %s", strerror(errno));
255 int mixer_get_avg(int i)
257 int v = mixer_get(i);
259 return ((v >> 8) + (v & 0xFF)) / 2;
262 int mixer_get_left(int i)
264 return mixer_get(i) >> 8;
267 int mixer_get_right(int i)
269 return mixer_get(i) & 0xFF;
271 int mixer_is_mute(int i)
273 return !mixer_get(i);