1 // vim:ts=4:sw=4:expandtab
8 #include <yajl/yajl_gen.h>
9 #include <yajl/yajl_version.h>
12 #include <alsa/asoundlib.h>
17 #if defined(__FreeBSD__) || defined(__DragonFly__)
20 #include <sys/soundcard.h>
26 #include <sys/audioio.h>
27 #include <sys/ioctl.h>
33 #define ALSA_VOLUME(channel) \
34 err = snd_mixer_selem_get_##channel##_dB_range(elem, &min, &max) || \
35 snd_mixer_selem_get_##channel##_dB(elem, 0, &val); \
36 if (err != 0 || min >= max) { \
37 err = snd_mixer_selem_get_##channel##_volume_range(elem, &min, &max) || \
38 snd_mixer_selem_get_##channel##_volume(elem, 0, &val); \
39 force_linear = true; \
42 #define ALSA_MUTE_SWITCH(channel) \
43 if ((err = snd_mixer_selem_get_##channel##_switch(elem, 0, &pbval)) < 0) \
44 fprintf(stderr, "i3status: ALSA: " #channel "_switch: %s\n", snd_strerror(err)); \
46 START_COLOR("color_degraded"); \
50 static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume) {
51 const char *walk = fmt;
53 for (; *walk != '\0'; walk++) {
57 } else if (BEGINS_WITH(walk + 1, "%")) {
58 outwalk += sprintf(outwalk, "%s", pct_mark);
61 } else if (BEGINS_WITH(walk + 1, "volume")) {
62 outwalk += sprintf(outwalk, "%d%s", ivolume, pct_mark);
63 walk += strlen("volume");
72 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) {
73 char *outwalk = buffer;
76 /* Printing volume works with ALSA and PulseAudio at the moment */
77 if (output_format == O_I3BAR) {
79 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
84 #if !defined(__DragonFly__) && !defined(__OpenBSD__)
85 /* Try PulseAudio first */
87 /* If the device name has the format "pulse[:N]" where N is the
88 * index of the PulseAudio sink then force PulseAudio, optionally
89 * overriding the default sink */
90 if (!strncasecmp(device, "pulse", strlen("pulse"))) {
91 uint32_t sink_idx = device[strlen("pulse")] == ':' ? (uint32_t)atoi(device + strlen("pulse:")) : DEFAULT_SINK_INDEX;
92 const char *sink_name = device[strlen("pulse")] == ':' &&
93 !isdigit(device[strlen("pulse:")])
94 ? device + strlen("pulse:")
96 int cvolume = pulse_initialize() ? volume_pulseaudio(sink_idx, sink_name) : 0;
97 int ivolume = DECOMPOSE_VOLUME(cvolume);
98 bool muted = DECOMPOSE_MUTED(cvolume);
100 START_COLOR("color_degraded");
103 /* negative result means error, stick to 0 */
106 outwalk = apply_volume_format(muted ? fmt_muted : fmt,
110 } else if (!strcasecmp(device, "default") && pulse_initialize()) {
111 /* no device specified or "default" set */
112 int cvolume = volume_pulseaudio(DEFAULT_SINK_INDEX, NULL);
113 int ivolume = DECOMPOSE_VOLUME(cvolume);
114 bool muted = DECOMPOSE_MUTED(cvolume);
117 START_COLOR("color_degraded");
120 outwalk = apply_volume_format(muted ? fmt_muted : fmt,
125 /* negative result means error, fail PulseAudio attempt */
127 /* If some other device was specified or PulseAudio is not detected,
128 * proceed to ALSA / OSS */
132 const long MAX_LINEAR_DB_SCALE = 24;
135 snd_mixer_selem_id_t *sid;
136 snd_mixer_elem_t *elem;
138 bool force_linear = false;
141 if ((err = snd_mixer_open(&m, 0)) < 0) {
142 fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
146 /* Attach this mixer handle to the given device */
147 if ((err = snd_mixer_attach(m, device)) < 0) {
148 fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
153 /* Register this mixer */
154 if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
155 fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
160 if ((err = snd_mixer_load(m)) < 0) {
161 fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
166 snd_mixer_selem_id_malloc(&sid);
172 /* Find the given mixer */
173 snd_mixer_selem_id_set_index(sid, mixer_idx);
174 snd_mixer_selem_id_set_name(sid, mixer);
175 if (!(elem = snd_mixer_find_selem(m, sid))) {
176 fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %u)\n",
177 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
179 snd_mixer_selem_id_free(sid);
183 /* Get the volume range to convert the volume later */
184 snd_mixer_handle_events(m);
185 if (!strncasecmp(mixer, "capture", strlen("capture"))) {
188 ALSA_VOLUME(playback)
192 fprintf(stderr, "i3status: ALSA: Cannot get playback volume.\n");
196 /* Use linear mapping for raw register values or small ranges of 24 dB */
197 if (force_linear || max - min <= MAX_LINEAR_DB_SCALE * 100) {
198 float avgf = ((float)(val - min) / (max - min)) * 100;
200 avg = (avgf - avg < 0.5 ? avg : (avg + 1));
202 /* mapped volume to be more natural for the human ear */
203 double normalized = exp10((val - max) / 6000.0);
204 if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
205 double min_norm = exp10((min - max) / 6000.0);
206 normalized = (normalized - min_norm) / (1 - min_norm);
208 avg = lround(normalized * 100);
212 if (snd_mixer_selem_has_playback_switch(elem)) {
213 ALSA_MUTE_SWITCH(playback)
214 } else if (snd_mixer_selem_has_capture_switch(elem)) {
215 ALSA_MUTE_SWITCH(capture)
219 snd_mixer_selem_id_free(sid);
221 outwalk = apply_volume_format(fmt, outwalk, avg);
224 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
226 char defaultmixer[] = "/dev/mixer";
227 int mixfd, vol, devmask = 0;
231 asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
233 mixerpath = defaultmixer;
235 if ((mixfd = open(mixerpath, O_RDWR)) < 0) {
236 #if defined(__OpenBSD__)
237 warn("audioio: Cannot open mixer");
239 warn("OSS: Cannot open mixer");
247 #if defined(__OpenBSD__)
248 int oclass_idx = -1, master_idx = -1, master_mute_idx = -1;
249 int master_next = AUDIO_MIXER_LAST;
250 mixer_devinfo_t devinfo, devinfo2;
254 while (ioctl(mixfd, AUDIO_MIXER_DEVINFO, &devinfo) >= 0) {
255 if (devinfo.type != AUDIO_MIXER_CLASS) {
259 if (strncmp(devinfo.label.name, AudioCoutputs, MAX_AUDIO_DEV_LEN) == 0)
260 oclass_idx = devinfo.index;
266 while (ioctl(mixfd, AUDIO_MIXER_DEVINFO, &devinfo2) >= 0) {
267 if ((devinfo2.type == AUDIO_MIXER_VALUE) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmaster, MAX_AUDIO_DEV_LEN) == 0)) {
268 master_idx = devinfo2.index;
269 master_next = devinfo2.next;
272 if ((devinfo2.type == AUDIO_MIXER_ENUM) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmute, MAX_AUDIO_DEV_LEN) == 0))
273 if (master_next == devinfo2.index)
274 master_mute_idx = devinfo2.index;
276 if (master_next != AUDIO_MIXER_LAST)
277 master_next = devinfo2.next;
281 if (master_idx == -1)
284 devinfo.index = master_idx;
285 if (ioctl(mixfd, AUDIO_MIXER_DEVINFO, &devinfo) == -1)
288 vinfo.dev = master_idx;
289 vinfo.type = AUDIO_MIXER_VALUE;
290 vinfo.un.value.num_channels = devinfo.un.v.num_channels;
291 if (ioctl(mixfd, AUDIO_MIXER_READ, &vinfo) == -1)
294 if (AUDIO_MAX_GAIN != 100) {
295 float avgf = ((float)vinfo.un.value.level[AUDIO_MIXER_LEVEL_MONO] / AUDIO_MAX_GAIN) * 100;
297 vol = (avgf - vol < 0.5 ? vol : (vol + 1));
299 vol = (int)vinfo.un.value.level[AUDIO_MIXER_LEVEL_MONO];
302 vinfo.dev = master_mute_idx;
303 vinfo.type = AUDIO_MIXER_ENUM;
304 if (ioctl(mixfd, AUDIO_MIXER_READ, &vinfo) == -1)
307 if (master_mute_idx != -1 && vinfo.un.ord) {
308 START_COLOR("color_degraded");
314 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
315 warn("OSS: Cannot read mixer information");
318 if (ioctl(mixfd, MIXER_READ(0), &vol) == -1) {
319 warn("OSS: Cannot read mixer information");
323 if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
324 START_COLOR("color_degraded");
329 outwalk = apply_volume_format(fmt, outwalk, vol & 0x7f);
337 OUTPUT_FULL_TEXT(buffer);