]> git.sur5r.net Git - i3/i3status/blob - src/print_volume.c
print_volume: don’t return, complete the buffer first
[i3/i3status] / src / print_volume.c
1 // vim:ts=4:sw=4:expandtab
2 #include <time.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <err.h>
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
9
10 #ifdef LINUX
11 #include <alsa/asoundlib.h>
12 #include <alloca.h>
13 #endif
14
15 #if defined(__FreeBSD__) || defined(__DragonFly__)
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/soundcard.h>
19 #endif
20
21 #ifdef __OpenBSD__
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <soundcard.h>
25 #endif
26
27 #include "i3status.h"
28 #include "queue.h"
29
30 void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx) {
31     char *outwalk = buffer;
32     int pbval = 1;
33
34     /* Printing volume only works with ALSA at the moment */
35     if (output_format == O_I3BAR) {
36         char *instance;
37         asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
38         INSTANCE(instance);
39         free(instance);
40     }
41 #ifdef LINUX
42     int err;
43     snd_mixer_t *m;
44     snd_mixer_selem_id_t *sid;
45     snd_mixer_elem_t *elem;
46     long min, max, val;
47     int avg;
48
49     if ((err = snd_mixer_open(&m, 0)) < 0) {
50         fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
51         goto out;
52     }
53
54     /* Attach this mixer handle to the given device */
55     if ((err = snd_mixer_attach(m, device)) < 0) {
56         fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
57         snd_mixer_close(m);
58         goto out;
59     }
60
61     /* Register this mixer */
62     if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
63         fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
64         snd_mixer_close(m);
65         goto out;
66     }
67
68     if ((err = snd_mixer_load(m)) < 0) {
69         fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
70         snd_mixer_close(m);
71         goto out;
72     }
73
74     snd_mixer_selem_id_malloc(&sid);
75     if (sid == NULL) {
76         snd_mixer_close(m);
77         goto out;
78     }
79
80     /* Find the given mixer */
81     snd_mixer_selem_id_set_index(sid, mixer_idx);
82     snd_mixer_selem_id_set_name(sid, mixer);
83     if (!(elem = snd_mixer_find_selem(m, sid))) {
84         fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
85                 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
86         snd_mixer_close(m);
87         snd_mixer_selem_id_free(sid);
88         goto out;
89     }
90
91     /* Get the volume range to convert the volume later */
92     snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
93
94     snd_mixer_handle_events(m);
95     snd_mixer_selem_get_playback_volume(elem, 0, &val);
96     if (max != 100) {
97         float avgf = ((float)val / max) * 100;
98         avg = (int)avgf;
99         avg = (avgf - avg < 0.5 ? avg : (avg + 1));
100     } else
101         avg = (int)val;
102
103     /* Check for mute */
104     if (snd_mixer_selem_has_playback_switch(elem)) {
105         if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0)
106             fprintf(stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
107         if (!pbval) {
108             START_COLOR("color_degraded");
109             fmt = fmt_muted;
110         }
111     }
112
113     snd_mixer_close(m);
114     snd_mixer_selem_id_free(sid);
115
116     const char *walk = fmt;
117     for (; *walk != '\0'; walk++) {
118         if (*walk != '%') {
119             *(outwalk++) = *walk;
120             continue;
121         }
122         if (BEGINS_WITH(walk + 1, "%")) {
123             outwalk += sprintf(outwalk, "%%");
124             walk += strlen("%");
125         }
126         if (BEGINS_WITH(walk + 1, "volume")) {
127             outwalk += sprintf(outwalk, "%d%%", avg);
128             walk += strlen("volume");
129         }
130     }
131 #endif
132 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
133     char *mixerpath;
134     char defaultmixer[] = "/dev/mixer";
135     int mixfd, vol, devmask = 0;
136     pbval = 1;
137
138     if (mixer_idx > 0)
139         asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
140     else
141         mixerpath = defaultmixer;
142
143     if ((mixfd = open(mixerpath, O_RDWR)) < 0) {
144         warn("OSS: Cannot open mixer");
145         goto out;
146     }
147
148     if (mixer_idx > 0)
149         free(mixerpath);
150
151     if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
152         warn("OSS: Cannot read mixer information");
153         goto out;
154     }
155     if (ioctl(mixfd, MIXER_READ(0), &vol) == -1) {
156         warn("OSS: Cannot read mixer information");
157         goto out;
158     }
159
160     if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
161         START_COLOR("color_degraded");
162         pbval = 0;
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, "%")) {
172             outwalk += sprintf(outwalk, "%%");
173             walk += strlen("%");
174         }
175         if (BEGINS_WITH(walk + 1, "volume")) {
176             outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
177             walk += strlen("volume");
178         }
179     }
180     close(mixfd);
181 #endif
182
183 out:
184     *outwalk = '\0';
185     if (!pbval)
186         END_COLOR;
187     OUTPUT_FULL_TEXT(buffer);
188 }