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
35 #include <sys/ioctl.h>
42 #include <alsa/asoundlib.h>
44 #ifdef HAVE_LINUX_SOUNDCARD_H
45 #include <linux/soundcard.h>
48 #include <soundcard.h>
50 #include <sys/soundcard.h>
51 #endif /* __OpenBSD__ */
52 #endif /* HAVE_LINUX_SOUNDCARD_H */
53 #endif /* MIXER_IS_ALSA */
55 #define MIXER_DEV "/dev/mixer"
59 struct mixer_control {
62 snd_mixer_selem_id_t *sid;
63 snd_mixer_elem_t *elem;
64 long vol_min, vol_max;
67 static struct mixer_control mixer_data[MAX_MIXERS];
69 static char soundcard[64] = "default";
72 static const char *devs[] = SOUND_DEVICE_NAMES;
76 static int parse_simple_id(const char *str, snd_mixer_selem_id_t *sid)
82 while (*str == ' ' || *str == '\t')
86 size = 1; /* for '\0' */
87 if (*str != '"' && *str != '\'') {
88 while (*str && *str != ',') {
89 if (size < (int)sizeof(buf)) {
97 while (*str && *str != c) {
98 if (size < (int)sizeof(buf)) {
108 snd_mixer_selem_id_set_index(sid, 0);
114 *ptr = 0; /* terminate the string */
118 snd_mixer_selem_id_set_index(sid, atoi(str));
120 snd_mixer_selem_id_set_name(sid, buf);
124 int mixer_init (const char *name)
126 /* from amixer.c, replaced -EINVAL with -1 */
131 for (i = 0; i < num_mixers; i++) {
132 if (!strcasecmp (mixer_data[i].name, name)) {
136 if (i == MAX_MIXERS) {
137 fprintf (stderr, "max mixers (%d) reached\n", MAX_MIXERS);
142 #define data mixer_data[i]
144 strncpy (mixer_data[i].name, name, 63);
145 mixer_data[i].name[63] = '\0';
146 snd_mixer_selem_id_alloca (&data.sid);
148 if (parse_simple_id (name, data.sid) < 0) {
149 fprintf (stderr, "Wrong mixer identifier: %s\n", name);
152 if ((err = snd_mixer_open (&data.mixer, 0)) < 0) {
153 fprintf (stderr, "snd_mixer_open: %s\n", snd_strerror (err));
156 if ((err = snd_mixer_attach (data.mixer, soundcard)) < 0) {
157 fprintf (stderr, "snd_mixer_attach: %s\n", snd_strerror (err));
160 if ((err = snd_mixer_selem_register (data.mixer, NULL, NULL)) < 0) {
161 fprintf (stderr, "snd_mixer_selem_register: %s\n",
165 if ((err = snd_mixer_load (data.mixer)) < 0) {
166 fprintf (stderr, "snd_mixer_load: %s\n", snd_strerror (err));
169 if (!(data.elem = snd_mixer_find_selem (data.mixer, data.sid))) {
170 fprintf (stderr, "snd_mixer_find_selem (\"%s\", %i)\n",
171 snd_mixer_selem_id_get_name (data.sid),
172 snd_mixer_selem_id_get_index (data.sid));
175 snd_mixer_selem_get_playback_volume_range(data.elem, &data.vol_min, &data.vol_max);
178 int mixer_get_avg (int i)
182 snd_mixer_handle_events (data.mixer);
183 snd_mixer_selem_get_playback_volume (data.elem, 0, &val);
186 int mixer_get_left (int i)
189 return mixer_get_avg (i);
191 int mixer_get_right (int i)
194 return mixer_get_avg (i);
196 int mixer_to_255(int i, int x)
198 return (x-data.vol_min)*255/(data.vol_max-data.vol_min);
200 int mixer_is_mute(int i)
202 snd_mixer_handle_events (data.mixer);
203 if (snd_mixer_selem_has_playback_switch (data.elem)) {
205 if ((err = snd_mixer_selem_get_playback_switch(data.elem, 0, &val)) < 0)
206 fprintf (stderr, "playback_switch: %s\n", snd_strerror (err));
209 return !mixer_get_avg(i);
214 #else /* MIXER_IS_ALSA */
215 int mixer_init(const char *name)
219 if (name == 0 || name[0] == '\0') {
225 mixer_fd = open(MIXER_DEV, O_RDONLY);
226 if (mixer_fd == -1) {
227 NORM_ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
232 for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
233 if (strcasecmp(devs[i], name) == 0) {
241 static int mixer_get(int i)
246 if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
248 NORM_ERR("mixer ioctl: %s", strerror(errno));
258 int mixer_get_avg(int i)
260 int v = mixer_get(i);
262 return ((v >> 8) + (v & 0xFF)) / 2;
265 int mixer_get_left(int i)
267 return mixer_get(i) >> 8;
270 int mixer_get_right(int i)
272 return mixer_get(i) & 0xFF;
274 int mixer_is_mute(int i)
276 return !mixer_get(i);
279 #endif /* MIXER_IS_ALSA */
282 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
287 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
288 *a = mixer_init(buf1);
289 scan_bar(arg + n, w, h);
291 *a = mixer_init(NULL);