Removed all code depending on MIXER_IS_ALSA
[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 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
51 #define MIXER_DEV "/dev/mixer"
52
53 static int mixer_fd;
54 static const char *devs[] = SOUND_DEVICE_NAMES;
55
56 int mixer_init(const char *name)
57 {
58         unsigned int i;
59
60         if (name == 0 || name[0] == '\0') {
61                 name = "vol";
62         }
63
64         /* open mixer */
65         if (mixer_fd <= 0) {
66                 mixer_fd = open(MIXER_DEV, O_RDONLY);
67                 if (mixer_fd == -1) {
68                         NORM_ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
69                         return -1;
70                 }
71         }
72
73         for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
74                 if (strcasecmp(devs[i], name) == 0) {
75                         return i;
76                 }
77         }
78
79         return -1;
80 }
81
82 static int mixer_get(int i)
83 {
84         static char rep = 0;
85         int val = -1;
86
87         if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
88                 if (!rep) {
89                         NORM_ERR("mixer ioctl: %s", strerror(errno));
90                 }
91                 rep = 1;
92                 return 0;
93         }
94         rep = 0;
95
96         return val;
97 }
98
99 static int mixer_get_avg(int i)
100 {
101         int v = mixer_get(i);
102
103         return ((v >> 8) + (v & 0xFF)) / 2;
104 }
105
106 static int mixer_get_left(int i)
107 {
108         return mixer_get(i) >> 8;
109 }
110
111 static int mixer_get_right(int i)
112 {
113         return mixer_get(i) & 0xFF;
114 }
115 int mixer_is_mute(int i)
116 {
117         return !mixer_get(i);
118 }
119
120 #define mixer_to_255(i, x) x * 2.55
121
122 void parse_mixer_arg(struct text_object *obj, const char *arg)
123 {
124         obj->data.l = mixer_init(arg);
125 }
126
127 /* chan specifies the channel to print:
128  * -1 := left channel
129  *  0 := channel average
130  *  1 := right channel
131  */
132 static void print_mixer_chan(struct text_object *obj, int chan, char *p, int p_max_size)
133 {
134         int val;
135
136         if (chan < 0)
137                 val = mixer_get_left(obj->data.l);
138         else if (chan == 0)
139                 val = mixer_get_avg(obj->data.l);
140         else
141                 val = mixer_get_right(obj->data.l);
142
143         percent_print(p, p_max_size, val);
144 }
145
146 void print_mixer(struct text_object *obj, char *p, int p_max_size)
147 {
148         print_mixer_chan(obj, 0, p, p_max_size);
149 }
150
151 void print_mixerl(struct text_object *obj, char *p, int p_max_size)
152 {
153         print_mixer_chan(obj, -1, p, p_max_size);
154 }
155
156 void print_mixerr(struct text_object *obj, char *p, int p_max_size)
157 {
158         print_mixer_chan(obj, 1, p, p_max_size);
159 }
160
161 int check_mixer_muted(struct text_object *obj)
162 {
163         if (!mixer_is_mute(obj->data.l))
164                 return 0;
165         return 1;
166 }
167
168 void scan_mixer_bar(struct text_object *obj, const char *arg)
169 {
170         char buf1[64];
171         int n;
172
173         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
174                 obj->data.i = mixer_init(buf1);
175                 scan_bar(obj, arg + n);
176         } else {
177                 obj->data.i = mixer_init(NULL);
178                 scan_bar(obj, arg);
179         }
180 }
181
182 /* see print_mixer() above for a description of 'chan' */
183 static void print_mixer_bar_chan(struct text_object *obj, int chan, char *p, int p_max_size)
184 {
185         int val;
186
187         if (!p_max_size)
188                 return;
189
190         if (chan < 0)
191                 val = mixer_get_left(obj->data.i);
192         else if (chan == 0)
193                 val = mixer_get_avg(obj->data.i);
194         else
195                 val = mixer_get_right(obj->data.i);
196
197         new_bar(obj, p, p_max_size, mixer_to_255(obj->data.i, val));
198 }
199
200 void print_mixer_bar(struct text_object *obj, char *p, int p_max_size)
201 {
202         print_mixer_bar_chan(obj, 0, p, p_max_size);
203 }
204
205 void print_mixerl_bar(struct text_object *obj, char *p, int p_max_size)
206 {
207         print_mixer_bar_chan(obj, -1, p, p_max_size);
208 }
209
210 void print_mixerr_bar(struct text_object *obj, char *p, int p_max_size)
211 {
212         print_mixer_bar_chan(obj, 1, p, p_max_size);
213 }