b7245e58f7399388d25cfc618ec42edecfcb3c53
[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  * vim: ts=4 sw=4 noet ai cindent syntax=c
27  *
28  */
29
30 #include "conky.h"
31 #include "logging.h"
32 #include <sys/ioctl.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <ctype.h>
36
37
38 #ifdef MIXER_IS_ALSA
39 #include <alsa/asoundlib.h>
40 #else
41 #ifdef HAVE_LINUX_SOUNDCARD_H
42 #include <linux/soundcard.h>
43 #else
44 #ifdef __OpenBSD__
45 #include <soundcard.h>
46 #else
47 #include <sys/soundcard.h>
48 #endif /* __OpenBSD__ */
49 #endif /* HAVE_LINUX_SOUNDCARD_H */
50 #endif /* MIXER_IS_ALSA */
51
52 #define MIXER_DEV "/dev/mixer"
53
54 #ifdef MIXER_IS_ALSA
55 #define MAX_MIXERS 8
56 struct mixer_control {
57         char name[64];
58         snd_mixer_t *mixer;
59         snd_mixer_selem_id_t *sid;
60         snd_mixer_elem_t *elem;
61         long vol_min, vol_max;
62 };
63
64 static struct mixer_control mixer_data[MAX_MIXERS];
65 int num_mixers = 0;
66 static char soundcard[64] = "default";
67 #else
68 static int mixer_fd;
69 static const char *devs[] = SOUND_DEVICE_NAMES;
70 #endif
71
72 #ifdef MIXER_IS_ALSA
73 static int parse_simple_id(const char *str, snd_mixer_selem_id_t *sid)
74 {
75         int c, size;
76         char buf[128];
77         char *ptr = buf;
78
79         while (*str == ' ' || *str == '\t')
80                 str++;
81         if (!(*str))
82                 return -EINVAL;
83         size = 1;       /* for '\0' */
84         if (*str != '"' && *str != '\'') {
85                 while (*str && *str != ',') {
86                         if (size < (int)sizeof(buf)) {
87                                 *ptr++ = *str;
88                                 size++;
89                         }
90                         str++;
91                 }
92         } else {
93                 c = *str++;
94                 while (*str && *str != c) {
95                         if (size < (int)sizeof(buf)) {
96                                 *ptr++ = *str;
97                                 size++;
98                         }
99                         str++;
100                 }
101                 if (*str == c)
102                         str++;
103         }
104         if (*str == '\0') {
105                 snd_mixer_selem_id_set_index(sid, 0);
106                 *ptr = 0;
107                 goto _set;
108         }
109         if (*str != ',')
110                 return -EINVAL;
111         *ptr = 0;       /* terminate the string */
112         str++;
113         if (!isdigit(*str))
114                 return -EINVAL;
115         snd_mixer_selem_id_set_index(sid, atoi(str));
116        _set:
117         snd_mixer_selem_id_set_name(sid, buf);
118         return 0;
119 }
120
121 int mixer_init (const char *name)
122 {
123         /* from amixer.c, replaced -EINVAL with -1 */
124         int i, err;
125         if (!name)
126                 name = "Master";
127
128         for (i = 0; i < num_mixers; i++) {
129                 if (!strcasecmp (mixer_data[i].name, name)) {
130                         return i;
131                 }
132         }
133         if (i == MAX_MIXERS) {
134                 fprintf (stderr, "max mixers (%d) reached\n", MAX_MIXERS);
135                 return -1;
136         };
137
138         num_mixers++;
139 #define data mixer_data[i]
140
141         strncpy (mixer_data[i].name, name, 63);
142         mixer_data[i].name[63] = '\0';
143         snd_mixer_selem_id_alloca (&data.sid);
144         data.mixer = NULL;
145         if (parse_simple_id (name, data.sid) < 0) {
146                 fprintf (stderr, "Wrong mixer identifier: %s\n", name);
147                 return -1;
148         }
149         if ((err = snd_mixer_open (&data.mixer, 0)) < 0) {
150                 fprintf (stderr, "snd_mixer_open: %s\n", snd_strerror (err));
151                 return -1;
152         }
153         if ((err = snd_mixer_attach (data.mixer, soundcard)) < 0) {
154                 fprintf (stderr, "snd_mixer_attach: %s\n", snd_strerror (err));
155                 return -1;
156         }
157         if ((err = snd_mixer_selem_register (data.mixer, NULL, NULL)) < 0) {
158                 fprintf (stderr, "snd_mixer_selem_register: %s\n",
159                          snd_strerror (err));
160                 return -1;
161         }
162         if ((err = snd_mixer_load (data.mixer)) < 0) {
163                 fprintf (stderr, "snd_mixer_load: %s\n", snd_strerror (err));
164                 return -1;
165         }
166         if (!(data.elem = snd_mixer_find_selem (data.mixer, data.sid))) {
167                 fprintf (stderr, "snd_mixer_find_selem (\"%s\", %i)\n",
168                          snd_mixer_selem_id_get_name (data.sid),
169                          snd_mixer_selem_id_get_index (data.sid));
170                 return -1;
171         }
172         snd_mixer_selem_get_playback_volume_range(data.elem, &data.vol_min, &data.vol_max);
173         return i;
174 }
175 int mixer_get_avg (int i)
176 {
177   long val;
178
179   snd_mixer_handle_events (data.mixer);
180   snd_mixer_selem_get_playback_volume (data.elem, 0, &val);
181   return (int) val;
182 }
183 int mixer_get_left (int i)
184 {
185   /* stub */
186   return mixer_get_avg (i);
187 }
188 int mixer_get_right (int i)
189 {
190   /* stub */
191   return mixer_get_avg (i);
192 }
193 int mixer_to_255(int i, int x)
194 {
195   return (x-data.vol_min)*255/(data.vol_max-data.vol_min);
196 }
197 int mixer_is_mute(int i)
198 {
199         snd_mixer_handle_events (data.mixer);
200         if (snd_mixer_selem_has_playback_switch (data.elem)) {
201                 int val, err;
202                 if ((err = snd_mixer_selem_get_playback_switch(data.elem, 0, &val)) < 0)
203                         fprintf (stderr, "playback_switch: %s\n", snd_strerror (err));
204                 return !val;
205         } else {
206                 return !mixer_get_avg(i);
207         }
208 }
209 #undef data
210
211 #else
212 int mixer_init(const char *name)
213 {
214         unsigned int i;
215
216         if (name == 0 || name[0] == '\0') {
217                 name = "vol";
218         }
219
220         /* open mixer */
221         if (mixer_fd <= 0) {
222                 mixer_fd = open(MIXER_DEV, O_RDONLY);
223                 if (mixer_fd == -1) {
224                         ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
225                         return -1;
226                 }
227         }
228
229         for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
230                 if (strcasecmp(devs[i], name) == 0) {
231                         return i;
232                 }
233         }
234
235         return -1;
236 }
237
238 static int mixer_get(int i)
239 {
240         static char rep = 0;
241         int val = -1;
242
243         if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
244                 if (!rep) {
245                         ERR("mixer ioctl: %s", strerror(errno));
246                 }
247                 rep = 1;
248                 return 0;
249         }
250         rep = 0;
251
252         return val;
253 }
254
255 int mixer_get_avg(int i)
256 {
257         int v = mixer_get(i);
258
259         return ((v >> 8) + (v & 0xFF)) / 2;
260 }
261
262 int mixer_get_left(int i)
263 {
264         return mixer_get(i) >> 8;
265 }
266
267 int mixer_get_right(int i)
268 {
269         return mixer_get(i) & 0xFF;
270 }
271 int mixer_is_mute(int i)
272 {
273         return !mixer_get(i);
274 }
275 #endif