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/>.
30 #include <sys/ioctl.h>
37 #include <alsa/asoundlib.h>
39 #ifdef HAVE_LINUX_SOUNDCARD_H
40 #include <linux/soundcard.h>
43 #include <soundcard.h>
45 #include <sys/soundcard.h>
46 #endif /* __OpenBSD__ */
47 #endif /* HAVE_LINUX_SOUNDCARD_H */
48 #endif /* MIXER_IS_ALSA */
50 #define MIXER_DEV "/dev/mixer"
54 struct mixer_control {
57 snd_mixer_selem_id_t *sid;
58 snd_mixer_elem_t *elem;
59 long vol_min, vol_max;
62 static struct mixer_control mixer_data[MAX_MIXERS];
64 static char soundcard[64] = "default";
67 static const char *devs[] = SOUND_DEVICE_NAMES;
71 static int parse_simple_id(const char *str, snd_mixer_selem_id_t *sid)
77 while (*str == ' ' || *str == '\t')
81 size = 1; /* for '\0' */
82 if (*str != '"' && *str != '\'') {
83 while (*str && *str != ',') {
84 if (size < (int)sizeof(buf)) {
92 while (*str && *str != c) {
93 if (size < (int)sizeof(buf)) {
103 snd_mixer_selem_id_set_index(sid, 0);
109 *ptr = 0; /* terminate the string */
113 snd_mixer_selem_id_set_index(sid, atoi(str));
115 snd_mixer_selem_id_set_name(sid, buf);
119 int mixer_init (const char *name)
121 /* from amixer.c, replaced -EINVAL with -1 */
126 for (i = 0; i < num_mixers; i++) {
127 if (!strcasecmp (mixer_data[i].name, name)) {
131 if (i == MAX_MIXERS) {
132 fprintf (stderr, "max mixers (%d) reached\n", MAX_MIXERS);
137 #define data mixer_data[i]
139 strncpy (mixer_data[i].name, name, 63);
140 mixer_data[i].name[63] = '\0';
141 snd_mixer_selem_id_alloca (&data.sid);
143 if (parse_simple_id (name, data.sid) < 0) {
144 fprintf (stderr, "Wrong mixer identifier: %s\n", name);
147 if ((err = snd_mixer_open (&data.mixer, 0)) < 0) {
148 fprintf (stderr, "snd_mixer_open: %s\n", snd_strerror (err));
151 if ((err = snd_mixer_attach (data.mixer, soundcard)) < 0) {
152 fprintf (stderr, "snd_mixer_attach: %s\n", snd_strerror (err));
155 if ((err = snd_mixer_selem_register (data.mixer, NULL, NULL)) < 0) {
156 fprintf (stderr, "snd_mixer_selem_register: %s\n",
160 if ((err = snd_mixer_load (data.mixer)) < 0) {
161 fprintf (stderr, "snd_mixer_load: %s\n", snd_strerror (err));
164 if (!(data.elem = snd_mixer_find_selem (data.mixer, data.sid))) {
165 fprintf (stderr, "snd_mixer_find_selem (\"%s\", %i)\n",
166 snd_mixer_selem_id_get_name (data.sid),
167 snd_mixer_selem_id_get_index (data.sid));
170 snd_mixer_selem_get_playback_volume_range(data.elem, &data.vol_min, &data.vol_max);
173 int mixer_get_avg (int i)
177 snd_mixer_handle_events (data.mixer);
178 snd_mixer_selem_get_playback_volume (data.elem, 0, &val);
181 int mixer_get_left (int i)
184 return mixer_get_avg (i);
186 int mixer_get_right (int i)
189 return mixer_get_avg (i);
191 int mixer_to_255(int i, int x)
193 return (x-data.vol_min)*255/(data.vol_max-data.vol_min);
195 int mixer_is_mute(int i)
197 snd_mixer_handle_events (data.mixer);
198 if (snd_mixer_selem_has_playback_switch (data.elem)) {
200 if ((err = snd_mixer_selem_get_playback_switch(data.elem, 0, &val)) < 0)
201 fprintf (stderr, "playback_switch: %s\n", snd_strerror (err));
204 return !mixer_get_avg(i);
210 int mixer_init(const char *name)
214 if (name == 0 || name[0] == '\0') {
220 mixer_fd = open(MIXER_DEV, O_RDONLY);
221 if (mixer_fd == -1) {
222 ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
227 for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
228 if (strcasecmp(devs[i], name) == 0) {
236 static int mixer_get(int i)
241 if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
243 ERR("mixer ioctl: %s", strerror(errno));
253 int mixer_get_avg(int i)
255 int v = mixer_get(i);
257 return ((v >> 8) + (v & 0xFF)) / 2;
260 int mixer_get_left(int i)
262 return mixer_get(i) >> 8;
265 int mixer_get_right(int i)
267 return mixer_get(i) & 0xFF;
269 int mixer_is_mute(int i)
271 return !mixer_get(i);