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