974081afefbdb29bfb1e14c84f564b2c2766ca1b
[monky] / src / mixer.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27
28 #include "conky.h"
29 #include "logging.h"
30 #include <sys/ioctl.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34
35
36 #ifdef MIXER_IS_ALSA
37 #include <alsa/asoundlib.h>
38 #else
39 #ifdef HAVE_LINUX_SOUNDCARD_H
40 #include <linux/soundcard.h>
41 #else
42 #ifdef __OpenBSD__
43 #include <soundcard.h>
44 #else
45 #include <sys/soundcard.h>
46 #endif /* __OpenBSD__ */
47 #endif /* HAVE_LINUX_SOUNDCARD_H */
48 #endif /* MIXER_IS_ALSA */
49
50 #define MIXER_DEV "/dev/mixer"
51
52 #ifdef MIXER_IS_ALSA
53 #define MAX_MIXERS 8
54 struct mixer_control {
55         char name[64];
56         snd_mixer_t *mixer;
57         snd_mixer_selem_id_t *sid;
58         snd_mixer_elem_t *elem;
59         long vol_min, vol_max;
60 };
61
62 static struct mixer_control mixer_data[MAX_MIXERS];
63 int num_mixers = 0;
64 static char soundcard[64] = "default";
65 #else
66 static int mixer_fd;
67 static const char *devs[] = SOUND_DEVICE_NAMES;
68 #endif
69
70 #ifdef MIXER_IS_ALSA
71 static int parse_simple_id(const char *str, snd_mixer_selem_id_t *sid)
72 {
73         int c, size;
74         char buf[128];
75         char *ptr = buf;
76
77         while (*str == ' ' || *str == '\t')
78                 str++;
79         if (!(*str))
80                 return -EINVAL;
81         size = 1;       /* for '\0' */
82         if (*str != '"' && *str != '\'') {
83                 while (*str && *str != ',') {
84                         if (size < (int)sizeof(buf)) {
85                                 *ptr++ = *str;
86                                 size++;
87                         }
88                         str++;
89                 }
90         } else {
91                 c = *str++;
92                 while (*str && *str != c) {
93                         if (size < (int)sizeof(buf)) {
94                                 *ptr++ = *str;
95                                 size++;
96                         }
97                         str++;
98                 }
99                 if (*str == c)
100                         str++;
101         }
102         if (*str == '\0') {
103                 snd_mixer_selem_id_set_index(sid, 0);
104                 *ptr = 0;
105                 goto _set;
106         }
107         if (*str != ',')
108                 return -EINVAL;
109         *ptr = 0;       /* terminate the string */
110         str++;
111         if (!isdigit(*str))
112                 return -EINVAL;
113         snd_mixer_selem_id_set_index(sid, atoi(str));
114        _set:
115         snd_mixer_selem_id_set_name(sid, buf);
116         return 0;
117 }
118
119 int mixer_init (const char *name)
120 {
121         /* from amixer.c, replaced -EINVAL with -1 */
122         int i, err;
123         if (!name)
124                 name = "Master";
125
126         for (i = 0; i < num_mixers; i++) {
127                 if (!strcasecmp (mixer_data[i].name, name)) {
128                         return i;
129                 }
130         }
131         if (i == MAX_MIXERS) {
132                 fprintf (stderr, "max mixers (%d) reached\n", MAX_MIXERS);
133                 return -1;
134         };
135
136         num_mixers++;
137 #define data mixer_data[i]
138
139         strncpy (mixer_data[i].name, name, 63);
140         mixer_data[i].name[63] = '\0';
141         snd_mixer_selem_id_alloca (&data.sid);
142         data.mixer = NULL;
143         if (parse_simple_id (name, data.sid) < 0) {
144                 fprintf (stderr, "Wrong mixer identifier: %s\n", name);
145                 return -1;
146         }
147         if ((err = snd_mixer_open (&data.mixer, 0)) < 0) {
148                 fprintf (stderr, "snd_mixer_open: %s\n", snd_strerror (err));
149                 return -1;
150         }
151         if ((err = snd_mixer_attach (data.mixer, soundcard)) < 0) {
152                 fprintf (stderr, "snd_mixer_attach: %s\n", snd_strerror (err));
153                 return -1;
154         }
155         if ((err = snd_mixer_selem_register (data.mixer, NULL, NULL)) < 0) {
156                 fprintf (stderr, "snd_mixer_selem_register: %s\n",
157                          snd_strerror (err));
158                 return -1;
159         }
160         if ((err = snd_mixer_load (data.mixer)) < 0) {
161                 fprintf (stderr, "snd_mixer_load: %s\n", snd_strerror (err));
162                 return -1;
163         }
164         if (!(data.elem = snd_mixer_find_selem (data.mixer, data.sid))) {
165                 fprintf (stderr, "snd_mixer_find_selem (\"%s\", %i)\n",
166                          snd_mixer_selem_id_get_name (data.sid),
167                          snd_mixer_selem_id_get_index (data.sid));
168                 return -1;
169         }
170         snd_mixer_selem_get_playback_volume_range(data.elem, &data.vol_min, &data.vol_max);
171         return i;
172 }
173 int mixer_get_avg (int i)
174 {
175   long val;
176
177   snd_mixer_handle_events (data.mixer);
178   snd_mixer_selem_get_playback_volume (data.elem, 0, &val);
179   return (int) val;
180 }
181 int mixer_get_left (int i)
182 {
183   /* stub */
184   return mixer_get_avg (i);
185 }
186 int mixer_get_right (int i)
187 {
188   /* stub */
189   return mixer_get_avg (i);
190 }
191 int mixer_to_255(int i, int x)
192 {
193   return (x-data.vol_min)*255/(data.vol_max-data.vol_min);
194 }
195 int mixer_is_mute(int i)
196 {
197         snd_mixer_handle_events (data.mixer);
198         if (snd_mixer_selem_has_playback_switch (data.elem)) {
199                 int val, err;
200                 if ((err = snd_mixer_selem_get_playback_switch(data.elem, 0, &val)) < 0)
201                         fprintf (stderr, "playback_switch: %s\n", snd_strerror (err));
202                 return !val;
203         } else {
204                 return !mixer_get_avg(i);
205         }
206 }
207 #undef data
208
209 #else
210 int mixer_init(const char *name)
211 {
212         unsigned int i;
213
214         if (name == 0 || name[0] == '\0') {
215                 name = "vol";
216         }
217
218         /* open mixer */
219         if (mixer_fd <= 0) {
220                 mixer_fd = open(MIXER_DEV, O_RDONLY);
221                 if (mixer_fd == -1) {
222                         ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
223                         return -1;
224                 }
225         }
226
227         for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
228                 if (strcasecmp(devs[i], name) == 0) {
229                         return i;
230                 }
231         }
232
233         return -1;
234 }
235
236 static int mixer_get(int i)
237 {
238         static char rep = 0;
239         int val = -1;
240
241         if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
242                 if (!rep) {
243                         ERR("mixer ioctl: %s", strerror(errno));
244                 }
245                 rep = 1;
246                 return 0;
247         }
248         rep = 0;
249
250         return val;
251 }
252
253 int mixer_get_avg(int i)
254 {
255         int v = mixer_get(i);
256
257         return ((v >> 8) + (v & 0xFF)) / 2;
258 }
259
260 int mixer_get_left(int i)
261 {
262         return mixer_get(i) >> 8;
263 }
264
265 int mixer_get_right(int i)
266 {
267         return mixer_get(i) & 0xFF;
268 }
269 int mixer_is_mute(int i)
270 {
271         return !mixer_get(i);
272 }
273 #endif