]> git.sur5r.net Git - i3/i3status/blob - src/print_volume.c
refactoring to avoid code duplication
[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 static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume) {
31     const char *walk = fmt;
32
33     for (; *walk != '\0'; walk++) {
34         if (*walk != '%') {
35             *(outwalk++) = *walk;
36             continue;
37         }
38         if (BEGINS_WITH(walk + 1, "%")) {
39             outwalk += sprintf(outwalk, "%%");
40             walk += strlen("%");
41         }
42         if (BEGINS_WITH(walk + 1, "volume")) {
43             outwalk += sprintf(outwalk, "%d%%", ivolume);
44             walk += strlen("volume");
45         }
46     }
47     return outwalk;
48 }
49
50 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) {
51     char *outwalk = buffer;
52     int pbval = 1;
53
54     /* Printing volume only works with ALSA at the moment */
55     if (output_format == O_I3BAR) {
56         char *instance;
57         asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
58         INSTANCE(instance);
59         free(instance);
60     }
61 #ifdef LINUX
62     int err;
63     snd_mixer_t *m;
64     snd_mixer_selem_id_t *sid;
65     snd_mixer_elem_t *elem;
66     long min, max, val;
67     int avg;
68
69     if ((err = snd_mixer_open(&m, 0)) < 0) {
70         fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
71         goto out;
72     }
73
74     /* Attach this mixer handle to the given device */
75     if ((err = snd_mixer_attach(m, device)) < 0) {
76         fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
77         snd_mixer_close(m);
78         goto out;
79     }
80
81     /* Register this mixer */
82     if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
83         fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
84         snd_mixer_close(m);
85         goto out;
86     }
87
88     if ((err = snd_mixer_load(m)) < 0) {
89         fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
90         snd_mixer_close(m);
91         goto out;
92     }
93
94     snd_mixer_selem_id_malloc(&sid);
95     if (sid == NULL) {
96         snd_mixer_close(m);
97         goto out;
98     }
99
100     /* Find the given mixer */
101     snd_mixer_selem_id_set_index(sid, mixer_idx);
102     snd_mixer_selem_id_set_name(sid, mixer);
103     if (!(elem = snd_mixer_find_selem(m, sid))) {
104         fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
105                 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
106         snd_mixer_close(m);
107         snd_mixer_selem_id_free(sid);
108         goto out;
109     }
110
111     /* Get the volume range to convert the volume later */
112     snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
113
114     snd_mixer_handle_events(m);
115     snd_mixer_selem_get_playback_volume(elem, 0, &val);
116     if (max != 100) {
117         float avgf = ((float)val / max) * 100;
118         avg = (int)avgf;
119         avg = (avgf - avg < 0.5 ? avg : (avg + 1));
120     } else
121         avg = (int)val;
122
123     /* Check for mute */
124     if (snd_mixer_selem_has_playback_switch(elem)) {
125         if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0)
126             fprintf(stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
127         if (!pbval) {
128             START_COLOR("color_degraded");
129             fmt = fmt_muted;
130         }
131     }
132
133     snd_mixer_close(m);
134     snd_mixer_selem_id_free(sid);
135
136     outwalk = apply_volume_format(fmt, outwalk, avg);
137
138 #endif
139 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
140     char *mixerpath;
141     char defaultmixer[] = "/dev/mixer";
142     int mixfd, vol, devmask = 0;
143     pbval = 1;
144
145     if (mixer_idx > 0)
146         asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
147     else
148         mixerpath = defaultmixer;
149
150     if ((mixfd = open(mixerpath, O_RDWR)) < 0) {
151         warn("OSS: Cannot open mixer");
152         goto out;
153     }
154
155     if (mixer_idx > 0)
156         free(mixerpath);
157
158     if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
159         warn("OSS: Cannot read mixer information");
160         goto out;
161     }
162     if (ioctl(mixfd, MIXER_READ(0), &vol) == -1) {
163         warn("OSS: Cannot read mixer information");
164         goto out;
165     }
166
167     if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
168         START_COLOR("color_degraded");
169         pbval = 0;
170     }
171
172     outwalk = apply_volume_format(fmt, outwalk, vol & 0x7f);
173     close(mixfd);
174 #endif
175
176 out:
177     *outwalk = '\0';
178     if (!pbval)
179         END_COLOR;
180     OUTPUT_FULL_TEXT(buffer);
181 }