Modify last commit to keep compiler happy since it claims devbuf can never be null...
[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   return (int) val;
185 }
186 static int mixer_get_left (int i)
187 {
188   /* stub */
189   return mixer_get_avg (i);
190 }
191 static int mixer_get_right (int i)
192 {
193   /* stub */
194   return mixer_get_avg (i);
195 }
196 int mixer_to_255(int i, int x)
197 {
198   return (x-data.vol_min)*255/(data.vol_max-data.vol_min);
199 }
200 int mixer_is_mute(int i)
201 {
202         snd_mixer_handle_events (data.mixer);
203         if (snd_mixer_selem_has_playback_switch (data.elem)) {
204                 int val, err;
205                 if ((err = snd_mixer_selem_get_playback_switch(data.elem, 0, &val)) < 0)
206                         fprintf (stderr, "playback_switch: %s\n", snd_strerror (err));
207                 return !val;
208         } else {
209                 return !mixer_get_avg(i);
210         }
211 }
212 #undef data
213
214 #else /* MIXER_IS_ALSA */
215 int mixer_init(const char *name)
216 {
217         unsigned int i;
218
219         if (name == 0 || name[0] == '\0') {
220                 name = "vol";
221         }
222
223         /* open mixer */
224         if (mixer_fd <= 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));
228                         return -1;
229                 }
230         }
231
232         for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
233                 if (strcasecmp(devs[i], name) == 0) {
234                         return i;
235                 }
236         }
237
238         return -1;
239 }
240
241 static int mixer_get(int i)
242 {
243         static char rep = 0;
244         int val = -1;
245
246         if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
247                 if (!rep) {
248                         NORM_ERR("mixer ioctl: %s", strerror(errno));
249                 }
250                 rep = 1;
251                 return 0;
252         }
253         rep = 0;
254
255         return val;
256 }
257
258 static int mixer_get_avg(int i)
259 {
260         int v = mixer_get(i);
261
262         return ((v >> 8) + (v & 0xFF)) / 2;
263 }
264
265 static int mixer_get_left(int i)
266 {
267         return mixer_get(i) >> 8;
268 }
269
270 static int mixer_get_right(int i)
271 {
272         return mixer_get(i) & 0xFF;
273 }
274 int mixer_is_mute(int i)
275 {
276         return !mixer_get(i);
277 }
278
279 #define mixer_to_255(i, x) x
280 #endif /* MIXER_IS_ALSA */
281
282 void parse_mixer_arg(struct text_object *obj, const char *arg)
283 {
284         obj->data.l = mixer_init(arg);
285 }
286
287 /* chan specifies the channel to print:
288  * -1 := left channel
289  *  0 := channel average
290  *  1 := right channel
291  */
292 static void print_mixer_chan(struct text_object *obj, int chan, char *p, int p_max_size)
293 {
294         int val;
295
296         if (chan < 0)
297                 val = mixer_get_left(obj->data.l);
298         else if (chan == 0)
299                 val = mixer_get_avg(obj->data.l);
300         else
301                 val = mixer_get_right(obj->data.l);
302
303         percent_print(p, p_max_size, val);
304 }
305
306 void print_mixer(struct text_object *obj, char *p, int p_max_size)
307 {
308         print_mixer_chan(obj, 0, p, p_max_size);
309 }
310
311 void print_mixerl(struct text_object *obj, char *p, int p_max_size)
312 {
313         print_mixer_chan(obj, -1, p, p_max_size);
314 }
315
316 void print_mixerr(struct text_object *obj, char *p, int p_max_size)
317 {
318         print_mixer_chan(obj, 1, p, p_max_size);
319 }
320
321 int check_mixer_muted(struct text_object *obj)
322 {
323         if (!mixer_is_mute(obj->data.l))
324                 return 0;
325         return 1;
326 }
327
328 void scan_mixer_bar(struct text_object *obj, const char *arg)
329 {
330         char buf1[64];
331         int n;
332
333         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
334                 obj->data.i = mixer_init(buf1);
335                 scan_bar(obj, arg + n);
336         } else {
337                 obj->data.i = mixer_init(NULL);
338                 scan_bar(obj, arg);
339         }
340 }
341
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)
344 {
345         int val;
346
347         if (!p_max_size)
348                 return;
349
350         if (chan < 0)
351                 val = mixer_get_left(obj->data.i);
352         else if (chan == 0)
353                 val = mixer_get_avg(obj->data.i);
354         else
355                 val = mixer_get_right(obj->data.i);
356
357         new_bar(obj, p, p_max_size, mixer_to_255(obj->data.i, val));
358 }
359
360 void print_mixer_bar(struct text_object *obj, char *p, int p_max_size)
361 {
362         print_mixer_bar_chan(obj, 0, p, p_max_size);
363 }
364
365 void print_mixerl_bar(struct text_object *obj, char *p, int p_max_size)
366 {
367         print_mixer_bar_chan(obj, -1, p, p_max_size);
368 }
369
370 void print_mixerr_bar(struct text_object *obj, char *p, int p_max_size)
371 {
372         print_mixer_bar_chan(obj, 1, p, p_max_size);
373 }