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