1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "text_object.h"
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 static 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 static int mixer_get_left (int i)
189 return mixer_get_avg (i);
191 static 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 static int mixer_get_avg(int i)
260 int v = mixer_get(i);
262 return ((v >> 8) + (v & 0xFF)) / 2;
265 static int mixer_get_left(int i)
267 return mixer_get(i) >> 8;
270 static 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 #define mixer_to_255(i, x) x
280 #endif /* MIXER_IS_ALSA */
282 void parse_mixer_arg(struct text_object *obj, const char *arg)
284 obj->data.l = mixer_init(arg);
287 /* chan specifies the channel to print:
289 * 0 := channel average
292 static void print_mixer_chan(struct text_object *obj, int chan, char *p, int p_max_size)
297 val = mixer_get_left(obj->data.l);
299 val = mixer_get_avg(obj->data.l);
301 val = mixer_get_right(obj->data.l);
303 percent_print(p, p_max_size, val);
306 void print_mixer(struct text_object *obj, char *p, int p_max_size)
308 print_mixer_chan(obj, 0, p, p_max_size);
311 void print_mixerl(struct text_object *obj, char *p, int p_max_size)
313 print_mixer_chan(obj, -1, p, p_max_size);
316 void print_mixerr(struct text_object *obj, char *p, int p_max_size)
318 print_mixer_chan(obj, 1, p, p_max_size);
321 int check_mixer_muted(struct text_object *obj)
323 if (!mixer_is_mute(obj->data.l))
328 void scan_mixer_bar(struct text_object *obj, const char *arg)
333 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
334 obj->data.i = mixer_init(buf1);
335 scan_bar(obj, arg + n);
337 obj->data.i = mixer_init(NULL);
342 /* see print_mixer() above for a description of 'chan' */
343 static void print_mixer_bar_chan(struct text_object *obj, int chan, char *p, int p_max_size)
351 val = mixer_get_left(obj->data.i);
353 val = mixer_get_avg(obj->data.i);
355 val = mixer_get_right(obj->data.i);
357 new_bar(obj, p, p_max_size, mixer_to_255(obj->data.i, val));
360 void print_mixer_bar(struct text_object *obj, char *p, int p_max_size)
362 print_mixer_bar_chan(obj, 0, p, p_max_size);
365 void print_mixerl_bar(struct text_object *obj, char *p, int p_max_size)
367 print_mixer_bar_chan(obj, -1, p, p_max_size);
370 void print_mixerr_bar(struct text_object *obj, char *p, int p_max_size)
372 print_mixer_bar_chan(obj, 1, p, p_max_size);