sort includes and remove two bogus whitelines.
[monky] / src / mixer.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * This program is licensed under BSD license, read COPYING
5  *
6  *  $Id$
7  */
8
9 #include <sys/ioctl.h>
10
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <string.h>
14 #include <stdlib.h>
15
16 #include "conky.h"
17
18 #ifdef HAVE_LINUX_SOUNDCARD_H
19 #include <linux/soundcard.h>
20 #else
21 #include <sys/soundcard.h>
22 #endif                          /* HAVE_LINUX_SOUNDCARD_H */
23
24 #define MIXER_DEV "/dev/mixer"
25
26 static int mixer_fd;
27 static const char *devs[] = SOUND_DEVICE_NAMES;
28
29 int mixer_init(const char *name)
30 {
31         unsigned int i;
32
33         if (name == 0 || name[0] == '\0')
34                 name = "vol";
35
36         /* open mixer */
37         if (mixer_fd <= 0) {
38                 mixer_fd = open(MIXER_DEV, O_RDONLY);
39                 if (mixer_fd == -1) {
40                         ERR("can't open %s: %s", MIXER_DEV,
41                             strerror(errno));
42                         return -1;
43                 }
44         }
45
46         for (i = 0; i < sizeof(devs) / sizeof(const char *); i++) {
47                 if (strcasecmp(devs[i], name) == 0) {
48                         return i;
49                 }
50         }
51
52         return -1;
53 }
54
55 static int mixer_get(int i)
56 {
57         static char rep = 0;
58         int val = -1;
59
60         if (ioctl(mixer_fd, MIXER_READ(i), &val) == -1) {
61                 if (!rep)
62                         ERR("mixer ioctl: %s", strerror(errno));
63                 rep = 1;
64                 return 0;
65         }
66         rep = 0;
67
68         return val;
69 }
70
71 int mixer_get_avg(int i)
72 {
73         int v = mixer_get(i);
74         return ((v >> 8) + (v & 0xFF)) / 2;
75 }
76
77 int mixer_get_left(int i)
78 {
79         return mixer_get(i) >> 8;
80 }
81
82 int mixer_get_right(int i)
83 {
84         return mixer_get(i) & 0xFF;
85 }