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