]> git.sur5r.net Git - i3/i3status/blob - src/print_volume.c
c13cb1761f4a206e9d2c999cef6c888c9563e153
[i3/i3status] / src / print_volume.c
1 // vim:ts=8:expandtab
2 #include <time.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #ifdef LINUX
8 #include <alsa/asoundlib.h>
9 #include <alloca.h>
10 #endif
11
12 #ifdef __FreeBSD__
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <sys/soundcard.h>
16 #endif
17
18 #include "i3status.h"
19 #include "queue.h"
20
21 #ifdef LINUX
22 struct mixer_hdl {
23         char *device;
24         char *mixer;
25         int mixer_idx;
26         snd_mixer_selem_id_t *sid;
27         snd_mixer_t *m;
28         snd_mixer_elem_t *elem;
29         long min;
30         long max;
31
32         TAILQ_ENTRY(mixer_hdl) handles;
33 };
34
35 TAILQ_HEAD(handles_head, mixer_hdl) cached = TAILQ_HEAD_INITIALIZER(cached);
36
37 static void free_hdl(struct mixer_hdl *hdl) {
38         free(hdl->device);
39         free(hdl->mixer);
40         free(hdl);
41 }
42 #endif
43
44 void print_volume(const char *fmt, const char *device, const char *mixer, int mixer_idx) {
45 /* Printing volume only works with ALSA at the moment */
46 #ifdef LINUX
47         /* Check if we already opened the mixer and get the handle
48          * from cache if so */
49         bool found = false;
50         int err;
51         struct mixer_hdl *hdl;
52         TAILQ_FOREACH(hdl, &cached, handles) {
53                 if (strcmp(hdl->device, device) != 0 ||
54                     strcmp(hdl->mixer, mixer) != 0 ||
55                     hdl->mixer_idx != mixer_idx)
56                         continue;
57                 found = true;
58                 break;
59         }
60
61         if (!found) {
62                 if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL)
63                         return;
64
65                 if ((hdl->device = strdup(device)) == NULL) {
66                         free(hdl);
67                         return;
68                 }
69
70                 if ((hdl->mixer = strdup(mixer)) == NULL) {
71                         free(hdl->device);
72                         free(hdl);
73                         return;
74                 }
75
76                 hdl->mixer_idx = mixer_idx;
77                 snd_mixer_selem_id_malloc(&(hdl->sid));
78                 if (hdl->sid == NULL) {
79                         free_hdl(hdl);
80                         return;
81                 }
82
83                 if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
84                         fprintf(stderr, "ALSA: Cannot open mixer: %s\n", snd_strerror(err));
85                         free_hdl(hdl);
86                         return;
87                 }
88
89                 /* Attach this mixer handle to the given device */
90                 if ((err = snd_mixer_attach(hdl->m, device)) < 0) {
91                         fprintf(stderr, "ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
92                         snd_mixer_close(hdl->m);
93                         free_hdl(hdl);
94                         return;
95                 }
96
97                 /* Register this mixer */
98                 if ((err = snd_mixer_selem_register(hdl->m, NULL, NULL)) < 0) {
99                         fprintf(stderr, "ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
100                         snd_mixer_close(hdl->m);
101                         free_hdl(hdl);
102                         return;
103                 }
104
105                 if ((err = snd_mixer_load(hdl->m)) < 0) {
106                         fprintf(stderr, "ALSA: snd_mixer_load: %s\n", snd_strerror(err));
107                         snd_mixer_close(hdl->m);
108                         free_hdl(hdl);
109                         return;
110                 }
111
112                 /* Find the given mixer */
113                 snd_mixer_selem_id_set_index(hdl->sid, mixer_idx);
114                 snd_mixer_selem_id_set_name(hdl->sid, mixer);
115                 if (!(hdl->elem = snd_mixer_find_selem(hdl->m, hdl->sid))) {
116                         fprintf(stderr, "ALSA: Cannot find mixer %s (index %i)\n",
117                                 snd_mixer_selem_id_get_name(hdl->sid), snd_mixer_selem_id_get_index(hdl->sid));
118                         snd_mixer_close(hdl->m);
119                         free_hdl(hdl);
120                         return;
121                 }
122
123                 /* Get the volume range to convert the volume later */
124                 snd_mixer_selem_get_playback_volume_range(hdl->elem, &(hdl->min), &(hdl->max));
125                 TAILQ_INSERT_TAIL(&cached, hdl, handles);
126         }
127
128         long val;
129         snd_mixer_handle_events (hdl->m);
130         snd_mixer_selem_get_playback_volume (hdl->elem, 0, &val);
131         int avg;
132         if (hdl->max != 100) {
133                 float avgf = ((float)val / hdl->max) * 100;
134                 avg = (int)avgf;
135                 avg = (avgf - avg < 0.5 ? avg : (avg+1));
136         } else avg = (int)val;
137
138         /* Check for mute */
139         if (snd_mixer_selem_has_playback_switch(hdl->elem)) {
140                 int pbval;
141                 if ((err = snd_mixer_selem_get_playback_switch(hdl->elem, 0, &pbval)) < 0)
142                         fprintf (stderr, "ALSA: playback_switch: %s\n", snd_strerror(err));
143                 if (!pbval)
144                         avg = 0;
145         }
146
147         const char *walk = fmt;
148         for (; *walk != '\0'; walk++) {
149                 if (*walk != '%') {
150                         putchar(*walk);
151                         continue;
152                 }
153                 if (BEGINS_WITH(walk+1, "volume")) {
154                         printf("%d%%", avg);
155                         walk += strlen("volume");
156                 }
157         }
158 #endif
159 #ifdef __FreeBSD__
160         char mixerpath[] = "/dev/mixer";
161         int mixfd, vol, devmask = 0;
162
163         if ((mixfd = open(mixerpath, O_RDWR)) < 0)
164                 return;
165         if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
166                 return;
167         if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
168                 return;
169
170         const char *walk = fmt;
171         for (; *walk != '\0'; walk++) {
172                 if (*walk != '%') {
173                         putchar(*walk);
174                         continue;
175                 }
176                 if (BEGINS_WITH(walk+1, "volume")) {
177                         printf("%d%%", vol & 0x7f);
178                         walk += strlen("volume");
179                 }
180         }
181         close(mixfd);
182 #endif
183 }