]> git.sur5r.net Git - i3/i3status/blob - src/print_volume.c
DragonFlyBSD support added
[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 #if defined(__FreeBSD__) || defined(__DragonFly__)
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         int pbval = 1;
55
56         /* Printing volume only works with ALSA at the moment */
57         if (output_format == O_I3BAR) {
58                 char *instance;
59                 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
60                 INSTANCE(instance);
61                 free(instance);
62         }
63 #ifdef LINUX
64         /* Check if we already opened the mixer and get the handle
65          * from cache if so */
66         bool found = false;
67         int err;
68         struct mixer_hdl *hdl;
69         TAILQ_FOREACH(hdl, &cached, handles) {
70                 if (strcmp(hdl->device, device) != 0 ||
71                     strcmp(hdl->mixer, mixer) != 0 ||
72                     hdl->mixer_idx != mixer_idx)
73                         continue;
74                 found = true;
75                 break;
76         }
77
78         if (!found) {
79                 if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL)
80                         return;
81
82                 if ((hdl->device = strdup(device)) == NULL) {
83                         free(hdl);
84                         return;
85                 }
86
87                 if ((hdl->mixer = strdup(mixer)) == NULL) {
88                         free(hdl->device);
89                         free(hdl);
90                         return;
91                 }
92
93                 hdl->mixer_idx = mixer_idx;
94                 snd_mixer_selem_id_malloc(&(hdl->sid));
95                 if (hdl->sid == NULL) {
96                         free_hdl(hdl);
97                         return;
98                 }
99
100                 if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
101                         fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
102                         free_hdl(hdl);
103                         return;
104                 }
105
106                 /* Attach this mixer handle to the given device */
107                 if ((err = snd_mixer_attach(hdl->m, device)) < 0) {
108                         fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
109                         snd_mixer_close(hdl->m);
110                         free_hdl(hdl);
111                         return;
112                 }
113
114                 /* Register this mixer */
115                 if ((err = snd_mixer_selem_register(hdl->m, NULL, NULL)) < 0) {
116                         fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
117                         snd_mixer_close(hdl->m);
118                         free_hdl(hdl);
119                         return;
120                 }
121
122                 if ((err = snd_mixer_load(hdl->m)) < 0) {
123                         fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
124                         snd_mixer_close(hdl->m);
125                         free_hdl(hdl);
126                         return;
127                 }
128
129                 /* Find the given mixer */
130                 snd_mixer_selem_id_set_index(hdl->sid, mixer_idx);
131                 snd_mixer_selem_id_set_name(hdl->sid, mixer);
132                 if (!(hdl->elem = snd_mixer_find_selem(hdl->m, hdl->sid))) {
133                         fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
134                                 snd_mixer_selem_id_get_name(hdl->sid), snd_mixer_selem_id_get_index(hdl->sid));
135                         snd_mixer_close(hdl->m);
136                         free_hdl(hdl);
137                         return;
138                 }
139
140                 /* Get the volume range to convert the volume later */
141                 snd_mixer_selem_get_playback_volume_range(hdl->elem, &(hdl->min), &(hdl->max));
142                 TAILQ_INSERT_TAIL(&cached, hdl, handles);
143         }
144
145         long val;
146         snd_mixer_handle_events (hdl->m);
147         snd_mixer_selem_get_playback_volume (hdl->elem, 0, &val);
148         int avg;
149         if (hdl->max != 100) {
150                 float avgf = ((float)val / hdl->max) * 100;
151                 avg = (int)avgf;
152                 avg = (avgf - avg < 0.5 ? avg : (avg+1));
153         } else avg = (int)val;
154
155         /* Check for mute */
156         if (snd_mixer_selem_has_playback_switch(hdl->elem)) {
157                 if ((err = snd_mixer_selem_get_playback_switch(hdl->elem, 0, &pbval)) < 0)
158                         fprintf (stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
159                 if (!pbval)  {
160                         START_COLOR("color_bad");
161                         avg = 0;
162                 }
163         }
164
165         const char *walk = fmt;
166         for (; *walk != '\0'; walk++) {
167                 if (*walk != '%') {
168                         *(outwalk++) = *walk;
169                         continue;
170                 }
171                 if (BEGINS_WITH(walk+1, "volume")) {
172                         outwalk += sprintf(outwalk, "%d%%", avg);
173                         walk += strlen("volume");
174                 }
175         }
176 #endif
177 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
178         char mixerpath[] = "/dev/mixer";
179         int mixfd, vol, devmask = 0;
180
181         if ((mixfd = open(mixerpath, O_RDWR)) < 0)
182                 return;
183         if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
184                 return;
185         if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
186                 return;
187
188         const char *walk = fmt;
189         for (; *walk != '\0'; walk++) {
190                 if (*walk != '%') {
191                         *(outwalk++) = *walk;
192                         continue;
193                 }
194                 if (BEGINS_WITH(walk+1, "volume")) {
195                         outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
196                         walk += strlen("volume");
197                 }
198         }
199         close(mixfd);
200 #endif
201
202         *outwalk = '\0';
203         OUTPUT_FULL_TEXT(buffer);
204
205         if (!pbval)
206                 END_COLOR;
207 }