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