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>
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 */
51 #define MIXER_DEV "/dev/mixer"
54 static const char *devs[] = SOUND_DEVICE_NAMES;
56 int mixer_init(const char *name)
60 if (name == 0 || name[0] == '\0') {
66 mixer_fd = open(MIXER_DEV, O_RDONLY);
68 NORM_ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
73 for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
74 if (strcasecmp(devs[i], name) == 0) {
82 static int mixer_get(int i)
87 if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
89 NORM_ERR("mixer ioctl: %s", strerror(errno));
99 static int mixer_get_avg(int i)
101 int v = mixer_get(i);
103 return ((v >> 8) + (v & 0xFF)) / 2;
106 static int mixer_get_left(int i)
108 return mixer_get(i) >> 8;
111 static int mixer_get_right(int i)
113 return mixer_get(i) & 0xFF;
115 int mixer_is_mute(int i)
117 return !mixer_get(i);
120 #define mixer_to_255(i, x) x * 2.55
122 void parse_mixer_arg(struct text_object *obj, const char *arg)
124 obj->data.l = mixer_init(arg);
127 /* chan specifies the channel to print:
129 * 0 := channel average
132 static void print_mixer_chan(struct text_object *obj, int chan, char *p, int p_max_size)
137 val = mixer_get_left(obj->data.l);
139 val = mixer_get_avg(obj->data.l);
141 val = mixer_get_right(obj->data.l);
143 percent_print(p, p_max_size, val);
146 void print_mixer(struct text_object *obj, char *p, int p_max_size)
148 print_mixer_chan(obj, 0, p, p_max_size);
151 void print_mixerl(struct text_object *obj, char *p, int p_max_size)
153 print_mixer_chan(obj, -1, p, p_max_size);
156 void print_mixerr(struct text_object *obj, char *p, int p_max_size)
158 print_mixer_chan(obj, 1, p, p_max_size);
161 int check_mixer_muted(struct text_object *obj)
163 if (!mixer_is_mute(obj->data.l))
168 void scan_mixer_bar(struct text_object *obj, const char *arg)
173 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
174 obj->data.i = mixer_init(buf1);
175 scan_bar(obj, arg + n);
177 obj->data.i = mixer_init(NULL);
182 /* see print_mixer() above for a description of 'chan' */
183 static void print_mixer_bar_chan(struct text_object *obj, int chan, char *p, int p_max_size)
191 val = mixer_get_left(obj->data.i);
193 val = mixer_get_avg(obj->data.i);
195 val = mixer_get_right(obj->data.i);
197 new_bar(obj, p, p_max_size, mixer_to_255(obj->data.i, val));
200 void print_mixer_bar(struct text_object *obj, char *p, int p_max_size)
202 print_mixer_bar_chan(obj, 0, p, p_max_size);
205 void print_mixerl_bar(struct text_object *obj, char *p, int p_max_size)
207 print_mixer_bar_chan(obj, -1, p, p_max_size);
210 void print_mixerr_bar(struct text_object *obj, char *p, int p_max_size)
212 print_mixer_bar_chan(obj, 1, p, p_max_size);