clarifying licensing
[monky] / src / mixer.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * Any original torsmo code is licensed under the BSD license
5  *
6  * All code written since the fork of torsmo is licensed under the GPL
7  *
8  * Please see COPYING for details
9  *
10  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
11  * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (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  *  $Id$
27  */
28
29 #include <sys/ioctl.h>
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #include "conky.h"
37
38 #ifdef HAVE_LINUX_SOUNDCARD_H
39 #include <linux/soundcard.h>
40 #else
41 #ifdef __OpenBSD__
42 #include <soundcard.h>
43 #else
44 #include <sys/soundcard.h>
45 #endif                          /* __OpenBSD__ */
46 #endif                          /* HAVE_LINUX_SOUNDCARD_H */
47
48 #define MIXER_DEV "/dev/mixer"
49
50 static int mixer_fd;
51 static const char *devs[] = SOUND_DEVICE_NAMES;
52
53 int mixer_init(const char *name)
54 {
55         unsigned int i;
56
57         if (name == 0 || name[0] == '\0')
58                 name = "vol";
59
60         /* open mixer */
61         if (mixer_fd <= 0) {
62                 mixer_fd = open(MIXER_DEV, O_RDONLY);
63                 if (mixer_fd == -1) {
64                         ERR("can't open %s: %s", MIXER_DEV,
65                             strerror(errno));
66                         return -1;
67                 }
68         }
69
70         for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
71                 if (strcasecmp(devs[i], name) == 0) {
72                         return i;
73                 }
74         }
75
76         return -1;
77 }
78
79 static int mixer_get(int i)
80 {
81         static char rep = 0;
82         int val = -1;
83
84         if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
85                 if (!rep)
86                         ERR("mixer ioctl: %s", strerror(errno));
87                 rep = 1;
88                 return 0;
89         }
90         rep = 0;
91
92         return val;
93 }
94
95 int mixer_get_avg(int i)
96 {
97         int v = mixer_get(i);
98         return ((v >> 8) + (v & 0xFF)) / 2;
99 }
100
101 int mixer_get_left(int i)
102 {
103         return mixer_get(i) >> 8;
104 }
105
106 int mixer_get_right(int i)
107 {
108         return mixer_get(i) & 0xFF;
109 }