9cdf2b843e765e9672cd59723199b2940911dfdc
[monky] / src / mixer.c
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
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
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.
21  *
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/>.
28  *
29  */
30
31 #include "conky.h"
32 #include "logging.h"
33 #include "specials.h"
34 #include "text_object.h"
35 #include <sys/ioctl.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <ctype.h>
39
40
41 #ifdef MIXER_IS_ALSA
42 #include <alsa/asoundlib.h>
43 #else
44 #ifdef HAVE_LINUX_SOUNDCARD_H
45 #include <linux/soundcard.h>
46 #else
47 #ifdef __OpenBSD__
48 #include <soundcard.h>
49 #else
50 #include <sys/soundcard.h>
51 #endif /* __OpenBSD__ */
52 #endif /* HAVE_LINUX_SOUNDCARD_H */
53 #endif /* MIXER_IS_ALSA */
54
55 #define MIXER_DEV "/dev/mixer"
56
57 #ifdef MIXER_IS_ALSA
58 #define MAX_MIXERS 8
59 struct mixer_control {
60         char name[64];
61         snd_mixer_t *mixer;
62         snd_mixer_selem_id_t *sid;
63         snd_mixer_elem_t *elem;
64         long vol_min, vol_max;
65 };
66
67 static struct mixer_control mixer_data[MAX_MIXERS];
68 int num_mixers = 0;
69 static char soundcard[64] = "default";
70 #else
71 static int mixer_fd;
72 static const char *devs[] = SOUND_DEVICE_NAMES;
73 #endif
74
75 #ifdef MIXER_IS_ALSA
76 static int parse_simple_id(const char *str, snd_mixer_selem_id_t *sid)
77 {
78         int c, size;
79         char buf[128];
80         char *ptr = buf;
81
82         while (*str == ' ' || *str == '\t')
83                 str++;
84         if (!(*str))
85                 return -EINVAL;
86         size = 1;       /* for '\0' */
87         if (*str != '"' && *str != '\'') {
88                 while (*str && *str != ',') {
89                         if (size < (int)sizeof(buf)) {
90                                 *ptr++ = *str;
91                                 size++;
92                         }
93                         str++;
94                 }
95         } else {
96                 c = *str++;
97                 while (*str && *str != c) {
98                         if (size < (int)sizeof(buf)) {
99                                 *ptr++ = *str;
100                                 size++;
101                         }
102                         str++;
103                 }
104                 if (*str == c)
105                         str++;
106         }
107         if (*str == '\0') {
108                 snd_mixer_selem_id_set_index(sid, 0);
109                 *ptr = 0;
110                 goto _set;
111         }
112         if (*str != ',')
113                 return -EINVAL;
114         *ptr = 0;       /* terminate the string */
115         str++;
116         if (!isdigit(*str))
117                 return -EINVAL;
118         snd_mixer_selem_id_set_index(sid, atoi(str));
119        _set:
120         snd_mixer_selem_id_set_name(sid, buf);
121         return 0;
122 }
123
124 int mixer_init (const char *name)
125 {
126         /* from amixer.c, replaced -EINVAL with -1 */
127         int i, err;
128         if (!name)
129                 name = "Master";
130
131         for (i = 0; i < num_mixers; i++) {
132                 if (!strcasecmp (mixer_data[i].name, name)) {
133                         return i;
134                 }
135         }
136         if (i == MAX_MIXERS) {
137                 fprintf (stderr, "max mixers (%d) reached\n", MAX_MIXERS);
138                 return -1;
139         };
140
141         num_mixers++;
142 #define data mixer_data[i]
143
144         strncpy (mixer_data[i].name, name, 63);
145         mixer_data[i].name[63] = '\0';
146         snd_mixer_selem_id_alloca (&data.sid);
147         data.mixer = NULL;
148         if (parse_simple_id (name, data.sid) < 0) {
149                 fprintf (stderr, "Wrong mixer identifier: %s\n", name);
150                 return -1;
151         }
152         if ((err = snd_mixer_open (&data.mixer, 0)) < 0) {
153                 fprintf (stderr, "snd_mixer_open: %s\n", snd_strerror (err));
154                 return -1;
155         }
156         if ((err = snd_mixer_attach (data.mixer, soundcard)) < 0) {
157                 fprintf (stderr, "snd_mixer_attach: %s\n", snd_strerror (err));
158                 return -1;
159         }
160         if ((err = snd_mixer_selem_register (data.mixer, NULL, NULL)) < 0) {
161                 fprintf (stderr, "snd_mixer_selem_register: %s\n",
162                          snd_strerror (err));
163                 return -1;
164         }
165         if ((err = snd_mixer_load (data.mixer)) < 0) {
166                 fprintf (stderr, "snd_mixer_load: %s\n", snd_strerror (err));
167                 return -1;
168         }
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));
173                 return -1;
174         }
175         snd_mixer_selem_get_playback_volume_range(data.elem, &data.vol_min, &data.vol_max);
176         return i;
177 }
178 static int mixer_get_avg (int i)
179 {
180         long val;
181
182         snd_mixer_handle_events (data.mixer);
183         snd_mixer_selem_get_playback_volume (data.elem, 0, &val);
184         if(data.vol_max != 100) {
185                 float avgf = ((float)val / data.vol_max) * 100;
186                 int avg = (int)avgf;
187                 return (avgf - avg < 0.5) ? avg : avg + 1;
188         }
189         return (int) val;
190 }
191 static int mixer_get_left (int i)
192 {
193   /* stub */
194   return mixer_get_avg (i);
195 }
196 static int mixer_get_right (int i)
197 {
198   /* stub */
199   return mixer_get_avg (i);
200 }
201 int mixer_to_255(int i, int x)
202 {
203   return (x-data.vol_min)*255/(data.vol_max-data.vol_min);
204 }
205 int mixer_is_mute(int i)
206 {
207         snd_mixer_handle_events (data.mixer);
208         if (snd_mixer_selem_has_playback_switch (data.elem)) {
209                 int val, err;
210                 if ((err = snd_mixer_selem_get_playback_switch(data.elem, 0, &val)) < 0)
211                         fprintf (stderr, "playback_switch: %s\n", snd_strerror (err));
212                 return !val;
213         } else {
214                 return !mixer_get_avg(i);
215         }
216 }
217 #undef data
218
219 #else /* MIXER_IS_ALSA */
220 int mixer_init(const char *name)
221 {
222         unsigned int i;
223
224         if (name == 0 || name[0] == '\0') {
225                 name = "vol";
226         }
227
228         /* open mixer */
229         if (mixer_fd <= 0) {
230                 mixer_fd = open(MIXER_DEV, O_RDONLY);
231                 if (mixer_fd == -1) {
232                         NORM_ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
233                         return -1;
234                 }
235         }
236
237         for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
238                 if (strcasecmp(devs[i], name) == 0) {
239                         return i;
240                 }
241         }
242
243         return -1;
244 }
245
246 static int mixer_get(int i)
247 {
248         static char rep = 0;
249         int val = -1;
250
251         if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
252                 if (!rep) {
253                         NORM_ERR("mixer ioctl: %s", strerror(errno));
254                 }
255                 rep = 1;
256                 return 0;
257         }
258         rep = 0;
259
260         return val;
261 }
262
263 static int mixer_get_avg(int i)
264 {
265         int v = mixer_get(i);
266
267         return ((v >> 8) + (v & 0xFF)) / 2;
268 }
269
270 static int mixer_get_left(int i)
271 {
272         return mixer_get(i) >> 8;
273 }
274
275 static int mixer_get_right(int i)
276 {
277         return mixer_get(i) & 0xFF;
278 }
279 int mixer_is_mute(int i)
280 {
281         return !mixer_get(i);
282 }
283
284 #define mixer_to_255(i, x) x * 2.55
285 #endif /* MIXER_IS_ALSA */
286
287 void parse_mixer_arg(struct text_object *obj, const char *arg)
288 {
289         obj->data.l = mixer_init(arg);
290 }
291
292 /* chan specifies the channel to print:
293  * -1 := left channel
294  *  0 := channel average
295  *  1 := right channel
296  */
297 static void print_mixer_chan(struct text_object *obj, int chan, char *p, int p_max_size)
298 {
299         int val;
300
301         if (chan < 0)
302                 val = mixer_get_left(obj->data.l);
303         else if (chan == 0)
304                 val = mixer_get_avg(obj->data.l);
305         else
306                 val = mixer_get_right(obj->data.l);
307
308         percent_print(p, p_max_size, val);
309 }
310
311 void print_mixer(struct text_object *obj, char *p, int p_max_size)
312 {
313         print_mixer_chan(obj, 0, p, p_max_size);
314 }
315
316 void print_mixerl(struct text_object *obj, char *p, int p_max_size)
317 {
318         print_mixer_chan(obj, -1, p, p_max_size);
319 }
320
321 void print_mixerr(struct text_object *obj, char *p, int p_max_size)
322 {
323         print_mixer_chan(obj, 1, p, p_max_size);
324 }
325
326 int check_mixer_muted(struct text_object *obj)
327 {
328         if (!mixer_is_mute(obj->data.l))
329                 return 0;
330         return 1;
331 }
332
333 void scan_mixer_bar(struct text_object *obj, const char *arg)
334 {
335         char buf1[64];
336         int n;
337
338         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
339                 obj->data.i = mixer_init(buf1);
340                 scan_bar(obj, arg + n);
341         } else {
342                 obj->data.i = mixer_init(NULL);
343                 scan_bar(obj, arg);
344         }
345 }
346
347 /* see print_mixer() above for a description of 'chan' */
348 static void print_mixer_bar_chan(struct text_object *obj, int chan, char *p, int p_max_size)
349 {
350         int val;
351
352         if (!p_max_size)
353                 return;
354
355         if (chan < 0)
356                 val = mixer_get_left(obj->data.i);
357         else if (chan == 0)
358                 val = mixer_get_avg(obj->data.i);
359         else
360                 val = mixer_get_right(obj->data.i);
361
362         new_bar(obj, p, p_max_size, mixer_to_255(obj->data.i, val));
363 }
364
365 void print_mixer_bar(struct text_object *obj, char *p, int p_max_size)
366 {
367         print_mixer_bar_chan(obj, 0, p, p_max_size);
368 }
369
370 void print_mixerl_bar(struct text_object *obj, char *p, int p_max_size)
371 {
372         print_mixer_bar_chan(obj, -1, p, p_max_size);
373 }
374
375 void print_mixerr_bar(struct text_object *obj, char *p, int p_max_size)
376 {
377         print_mixer_bar_chan(obj, 1, p, p_max_size);
378 }