1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
34 #include <sys/ioctl.h>
41 #include <alsa/asoundlib.h>
43 #ifdef HAVE_LINUX_SOUNDCARD_H
44 #include <linux/soundcard.h>
47 #include <soundcard.h>
49 #include <sys/soundcard.h>
50 #endif /* __OpenBSD__ */
51 #endif /* HAVE_LINUX_SOUNDCARD_H */
52 #endif /* MIXER_IS_ALSA */
54 #define MIXER_DEV "/dev/mixer"
58 struct mixer_control {
61 snd_mixer_selem_id_t *sid;
62 snd_mixer_elem_t *elem;
63 long vol_min, vol_max;
66 static struct mixer_control mixer_data[MAX_MIXERS];
68 static char soundcard[64] = "default";
71 static const char *devs[] = SOUND_DEVICE_NAMES;
75 static int parse_simple_id(const char *str, snd_mixer_selem_id_t *sid)
81 while (*str == ' ' || *str == '\t')
85 size = 1; /* for '\0' */
86 if (*str != '"' && *str != '\'') {
87 while (*str && *str != ',') {
88 if (size < (int)sizeof(buf)) {
96 while (*str && *str != c) {
97 if (size < (int)sizeof(buf)) {
107 snd_mixer_selem_id_set_index(sid, 0);
113 *ptr = 0; /* terminate the string */
117 snd_mixer_selem_id_set_index(sid, atoi(str));
119 snd_mixer_selem_id_set_name(sid, buf);
123 int mixer_init (const char *name)
125 /* from amixer.c, replaced -EINVAL with -1 */
130 for (i = 0; i < num_mixers; i++) {
131 if (!strcasecmp (mixer_data[i].name, name)) {
135 if (i == MAX_MIXERS) {
136 fprintf (stderr, "max mixers (%d) reached\n", MAX_MIXERS);
141 #define data mixer_data[i]
143 strncpy (mixer_data[i].name, name, 63);
144 mixer_data[i].name[63] = '\0';
145 snd_mixer_selem_id_alloca (&data.sid);
147 if (parse_simple_id (name, data.sid) < 0) {
148 fprintf (stderr, "Wrong mixer identifier: %s\n", name);
151 if ((err = snd_mixer_open (&data.mixer, 0)) < 0) {
152 fprintf (stderr, "snd_mixer_open: %s\n", snd_strerror (err));
155 if ((err = snd_mixer_attach (data.mixer, soundcard)) < 0) {
156 fprintf (stderr, "snd_mixer_attach: %s\n", snd_strerror (err));
159 if ((err = snd_mixer_selem_register (data.mixer, NULL, NULL)) < 0) {
160 fprintf (stderr, "snd_mixer_selem_register: %s\n",
164 if ((err = snd_mixer_load (data.mixer)) < 0) {
165 fprintf (stderr, "snd_mixer_load: %s\n", snd_strerror (err));
168 if (!(data.elem = snd_mixer_find_selem (data.mixer, data.sid))) {
169 fprintf (stderr, "snd_mixer_find_selem (\"%s\", %i)\n",
170 snd_mixer_selem_id_get_name (data.sid),
171 snd_mixer_selem_id_get_index (data.sid));
174 snd_mixer_selem_get_playback_volume_range(data.elem, &data.vol_min, &data.vol_max);
177 int mixer_get_avg (int i)
181 snd_mixer_handle_events (data.mixer);
182 snd_mixer_selem_get_playback_volume (data.elem, 0, &val);
185 int mixer_get_left (int i)
188 return mixer_get_avg (i);
190 int mixer_get_right (int i)
193 return mixer_get_avg (i);
195 int mixer_to_255(int i, int x)
197 return (x-data.vol_min)*255/(data.vol_max-data.vol_min);
199 int mixer_is_mute(int i)
201 snd_mixer_handle_events (data.mixer);
202 if (snd_mixer_selem_has_playback_switch (data.elem)) {
204 if ((err = snd_mixer_selem_get_playback_switch(data.elem, 0, &val)) < 0)
205 fprintf (stderr, "playback_switch: %s\n", snd_strerror (err));
208 return !mixer_get_avg(i);
214 int mixer_init(const char *name)
218 if (name == 0 || name[0] == '\0') {
224 mixer_fd = open(MIXER_DEV, O_RDONLY);
225 if (mixer_fd == -1) {
226 NORM_ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
231 for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
232 if (strcasecmp(devs[i], name) == 0) {
240 static int mixer_get(int i)
245 if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
247 NORM_ERR("mixer ioctl: %s", strerror(errno));
257 int mixer_get_avg(int i)
259 int v = mixer_get(i);
261 return ((v >> 8) + (v & 0xFF)) / 2;
264 int mixer_get_left(int i)
266 return mixer_get(i) >> 8;
269 int mixer_get_right(int i)
271 return mixer_get(i) & 0xFF;
273 int mixer_is_mute(int i)
275 return !mixer_get(i);