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