Update copyright stuff, fix conky.conf weirdness.
[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
34
35 #ifdef HAVE_LINUX_SOUNDCARD_H
36 #include <linux/soundcard.h>
37 #else
38 #ifdef __OpenBSD__
39 #include <soundcard.h>
40 #else
41 #include <sys/soundcard.h>
42 #endif /* __OpenBSD__ */
43 #endif /* HAVE_LINUX_SOUNDCARD_H */
44
45 #define MIXER_DEV "/dev/mixer"
46
47 static int mixer_fd;
48 static const char *devs[] = SOUND_DEVICE_NAMES;
49
50 int mixer_init(const char *name)
51 {
52         unsigned int i;
53
54         if (name == 0 || name[0] == '\0') {
55                 name = "vol";
56         }
57
58         /* open mixer */
59         if (mixer_fd <= 0) {
60                 mixer_fd = open(MIXER_DEV, O_RDONLY);
61                 if (mixer_fd == -1) {
62                         ERR("can't open %s: %s", MIXER_DEV, strerror(errno));
63                         return -1;
64                 }
65         }
66
67         for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
68                 if (strcasecmp(devs[i], name) == 0) {
69                         return i;
70                 }
71         }
72
73         return -1;
74 }
75
76 static int mixer_get(int i)
77 {
78         static char rep = 0;
79         int val = -1;
80
81         if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
82                 if (!rep) {
83                         ERR("mixer ioctl: %s", strerror(errno));
84                 }
85                 rep = 1;
86                 return 0;
87         }
88         rep = 0;
89
90         return val;
91 }
92
93 int mixer_get_avg(int i)
94 {
95         int v = mixer_get(i);
96
97         return ((v >> 8) + (v & 0xFF)) / 2;
98 }
99
100 int mixer_get_left(int i)
101 {
102         return mixer_get(i) >> 8;
103 }
104
105 int mixer_get_right(int i)
106 {
107         return mixer_get(i) & 0xFF;
108 }