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 static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume) {
34 const char *walk = fmt;
36 for (; *walk != '\0'; walk++) {
40 } else if (BEGINS_WITH(walk + 1, "%")) {
41 outwalk += sprintf(outwalk, "%s", pct_mark);
44 } else if (BEGINS_WITH(walk + 1, "volume")) {
45 outwalk += sprintf(outwalk, "%d%s", ivolume, pct_mark);
46 walk += strlen("volume");
55 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) {
56 char *outwalk = buffer;
59 /* Printing volume works with ALSA and PulseAudio at the moment */
60 if (output_format == O_I3BAR) {
62 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
67 #if !defined(__DragonFly__) && !defined(__OpenBSD__)
68 /* Try PulseAudio first */
70 /* If the device name has the format "pulse[:N]" where N is the
71 * index of the PulseAudio sink then force PulseAudio, optionally
72 * overriding the default sink */
73 if (!strncasecmp(device, "pulse", strlen("pulse"))) {
74 uint32_t sink_idx = device[strlen("pulse")] == ':' ? (uint32_t)atoi(device + strlen("pulse:")) : DEFAULT_SINK_INDEX;
75 const char *sink_name = device[strlen("pulse")] == ':' &&
76 !isdigit(device[strlen("pulse:")])
77 ? device + strlen("pulse:")
79 int cvolume = pulse_initialize() ? volume_pulseaudio(sink_idx, sink_name) : 0;
80 int ivolume = DECOMPOSE_VOLUME(cvolume);
81 bool muted = DECOMPOSE_MUTED(cvolume);
83 START_COLOR("color_degraded");
86 /* negative result means error, stick to 0 */
89 outwalk = apply_volume_format(muted ? fmt_muted : fmt,
93 } else if (!strcasecmp(device, "default") && pulse_initialize()) {
94 /* no device specified or "default" set */
95 int cvolume = volume_pulseaudio(DEFAULT_SINK_INDEX, NULL);
96 int ivolume = DECOMPOSE_VOLUME(cvolume);
97 bool muted = DECOMPOSE_MUTED(cvolume);
100 START_COLOR("color_degraded");
103 outwalk = apply_volume_format(muted ? fmt_muted : fmt,
108 /* negative result means error, fail PulseAudio attempt */
110 /* If some other device was specified or PulseAudio is not detected,
111 * proceed to ALSA / OSS */
115 const long MAX_LINEAR_DB_SCALE = 24;
118 snd_mixer_selem_id_t *sid;
119 snd_mixer_elem_t *elem;
121 bool force_linear = false;
124 if ((err = snd_mixer_open(&m, 0)) < 0) {
125 fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
129 /* Attach this mixer handle to the given device */
130 if ((err = snd_mixer_attach(m, device)) < 0) {
131 fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
136 /* Register this mixer */
137 if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
138 fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
143 if ((err = snd_mixer_load(m)) < 0) {
144 fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
149 snd_mixer_selem_id_malloc(&sid);
155 /* Find the given mixer */
156 snd_mixer_selem_id_set_index(sid, mixer_idx);
157 snd_mixer_selem_id_set_name(sid, mixer);
158 if (!(elem = snd_mixer_find_selem(m, sid))) {
159 fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %u)\n",
160 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
162 snd_mixer_selem_id_free(sid);
166 /* Get the volume range to convert the volume later */
167 snd_mixer_handle_events(m);
168 err = snd_mixer_selem_get_playback_dB_range(elem, &min, &max) ||
169 snd_mixer_selem_get_playback_dB(elem, 0, &val);
170 if (err != 0 || min >= max) {
171 err = snd_mixer_selem_get_playback_volume_range(elem, &min, &max) ||
172 snd_mixer_selem_get_playback_volume(elem, 0, &val);
177 fprintf(stderr, "i3status: ALSA: Cannot get playback volume.\n");
181 /* Use linear mapping for raw register values or small ranges of 24 dB */
182 if (force_linear || max - min <= MAX_LINEAR_DB_SCALE * 100) {
183 float avgf = ((float)(val - min) / (max - min)) * 100;
185 avg = (avgf - avg < 0.5 ? avg : (avg + 1));
187 /* mapped volume to be more natural for the human ear */
188 double normalized = exp10((val - max) / 6000.0);
189 if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
190 double min_norm = exp10((min - max) / 6000.0);
191 normalized = (normalized - min_norm) / (1 - min_norm);
193 avg = lround(normalized * 100);
197 if (snd_mixer_selem_has_playback_switch(elem)) {
198 if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0)
199 fprintf(stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
201 START_COLOR("color_degraded");
207 snd_mixer_selem_id_free(sid);
209 outwalk = apply_volume_format(fmt, outwalk, avg);
212 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
214 char defaultmixer[] = "/dev/mixer";
215 int mixfd, vol, devmask = 0;
219 asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
221 mixerpath = defaultmixer;
223 if ((mixfd = open(mixerpath, O_RDWR)) < 0) {
224 #if defined(__OpenBSD__)
225 warn("audioio: Cannot open mixer");
227 warn("OSS: Cannot open mixer");
235 #if defined(__OpenBSD__)
236 int oclass_idx = -1, master_idx = -1, master_mute_idx = -1;
237 int master_next = AUDIO_MIXER_LAST;
238 mixer_devinfo_t devinfo, devinfo2;
242 while (ioctl(mixfd, AUDIO_MIXER_DEVINFO, &devinfo) >= 0) {
243 if (devinfo.type != AUDIO_MIXER_CLASS) {
247 if (strncmp(devinfo.label.name, AudioCoutputs, MAX_AUDIO_DEV_LEN) == 0)
248 oclass_idx = devinfo.index;
254 while (ioctl(mixfd, AUDIO_MIXER_DEVINFO, &devinfo2) >= 0) {
255 if ((devinfo2.type == AUDIO_MIXER_VALUE) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmaster, MAX_AUDIO_DEV_LEN) == 0)) {
256 master_idx = devinfo2.index;
257 master_next = devinfo2.next;
260 if ((devinfo2.type == AUDIO_MIXER_ENUM) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmute, MAX_AUDIO_DEV_LEN) == 0))
261 if (master_next == devinfo2.index)
262 master_mute_idx = devinfo2.index;
264 if (master_next != AUDIO_MIXER_LAST)
265 master_next = devinfo2.next;
269 if (master_idx == -1)
272 devinfo.index = master_idx;
273 if (ioctl(mixfd, AUDIO_MIXER_DEVINFO, &devinfo) == -1)
276 vinfo.dev = master_idx;
277 vinfo.type = AUDIO_MIXER_VALUE;
278 vinfo.un.value.num_channels = devinfo.un.v.num_channels;
279 if (ioctl(mixfd, AUDIO_MIXER_READ, &vinfo) == -1)
282 if (AUDIO_MAX_GAIN != 100) {
283 float avgf = ((float)vinfo.un.value.level[AUDIO_MIXER_LEVEL_MONO] / AUDIO_MAX_GAIN) * 100;
285 vol = (avgf - vol < 0.5 ? vol : (vol + 1));
287 vol = (int)vinfo.un.value.level[AUDIO_MIXER_LEVEL_MONO];
290 vinfo.dev = master_mute_idx;
291 vinfo.type = AUDIO_MIXER_ENUM;
292 if (ioctl(mixfd, AUDIO_MIXER_READ, &vinfo) == -1)
295 if (master_mute_idx != -1 && vinfo.un.ord) {
296 START_COLOR("color_degraded");
302 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
303 warn("OSS: Cannot read mixer information");
306 if (ioctl(mixfd, MIXER_READ(0), &vol) == -1) {
307 warn("OSS: Cannot read mixer information");
311 if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
312 START_COLOR("color_degraded");
317 outwalk = apply_volume_format(fmt, outwalk, vol & 0x7f);
325 OUTPUT_FULL_TEXT(buffer);