1 // vim:ts=4:sw=4:expandtab
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
11 #include <alsa/asoundlib.h>
15 #if defined(__FreeBSD__) || defined(__DragonFly__)
18 #include <sys/soundcard.h>
24 #include <soundcard.h>
30 static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume) {
31 const char *walk = fmt;
33 for (; *walk != '\0'; walk++) {
38 if (BEGINS_WITH(walk + 1, "%")) {
39 outwalk += sprintf(outwalk, "%%");
42 if (BEGINS_WITH(walk + 1, "volume")) {
43 outwalk += sprintf(outwalk, "%d%%", ivolume);
44 walk += strlen("volume");
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;
54 /* Printing volume works with ALSA and PulseAudio at the moment */
55 if (output_format == O_I3BAR) {
57 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
62 /* Try PulseAudio first */
64 /* If the device name has the format "pulse[:N]" where N is the
65 * index of the PulseAudio sink then force PulseAudio, optionally
66 * overriding the default sink */
67 if (!strncasecmp(device, "pulse", strlen("pulse"))) {
68 uint32_t sink_idx = device[5] == ':' ? (uint32_t)atoi(device + 6)
70 int ivolume = pulse_initialize() ? volume_pulseaudio(sink_idx) : 0;
71 /* negative result means error, stick to 0 */
74 outwalk = apply_volume_format(fmt, outwalk, ivolume);
76 } else if (!strcasecmp(device, "default") && pulse_initialize()) {
77 /* no device specified or "default" set */
78 int ivolume = volume_pulseaudio(DEFAULT_SINK_INDEX);
80 outwalk = apply_volume_format(fmt, outwalk, ivolume);
83 /* negative result means error, fail PulseAudio attempt */
85 /* If some other device was specified or PulseAudio is not detected,
86 * proceed to ALSA / OSS */
91 snd_mixer_selem_id_t *sid;
92 snd_mixer_elem_t *elem;
96 if ((err = snd_mixer_open(&m, 0)) < 0) {
97 fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
101 /* Attach this mixer handle to the given device */
102 if ((err = snd_mixer_attach(m, device)) < 0) {
103 fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
108 /* Register this mixer */
109 if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
110 fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
115 if ((err = snd_mixer_load(m)) < 0) {
116 fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
121 snd_mixer_selem_id_malloc(&sid);
127 /* Find the given mixer */
128 snd_mixer_selem_id_set_index(sid, mixer_idx);
129 snd_mixer_selem_id_set_name(sid, mixer);
130 if (!(elem = snd_mixer_find_selem(m, sid))) {
131 fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
132 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
134 snd_mixer_selem_id_free(sid);
138 /* Get the volume range to convert the volume later */
139 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
141 snd_mixer_handle_events(m);
142 snd_mixer_selem_get_playback_volume(elem, 0, &val);
144 float avgf = ((float)val / max) * 100;
146 avg = (avgf - avg < 0.5 ? avg : (avg + 1));
151 if (snd_mixer_selem_has_playback_switch(elem)) {
152 if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0)
153 fprintf(stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
155 START_COLOR("color_degraded");
161 snd_mixer_selem_id_free(sid);
163 outwalk = apply_volume_format(fmt, outwalk, avg);
166 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
168 char defaultmixer[] = "/dev/mixer";
169 int mixfd, vol, devmask = 0;
173 asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
175 mixerpath = defaultmixer;
177 if ((mixfd = open(mixerpath, O_RDWR)) < 0) {
178 warn("OSS: Cannot open mixer");
185 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
186 warn("OSS: Cannot read mixer information");
189 if (ioctl(mixfd, MIXER_READ(0), &vol) == -1) {
190 warn("OSS: Cannot read mixer information");
194 if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
195 START_COLOR("color_degraded");
199 outwalk = apply_volume_format(fmt, outwalk, vol & 0x7f);
207 OUTPUT_FULL_TEXT(buffer);