6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
10 #include <alsa/asoundlib.h>
14 #if defined(__FreeBSD__) || defined(__DragonFly__)
17 #include <sys/soundcard.h>
23 #include <soundcard.h>
29 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) {
30 char *outwalk = buffer;
33 /* Printing volume only works with ALSA at the moment */
34 if (output_format == O_I3BAR) {
36 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
43 snd_mixer_selem_id_t *sid;
44 snd_mixer_elem_t *elem;
48 if ((err = snd_mixer_open(&m, 0)) < 0) {
49 fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
53 /* Attach this mixer handle to the given device */
54 if ((err = snd_mixer_attach(m, device)) < 0) {
55 fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
60 /* Register this mixer */
61 if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
62 fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
67 if ((err = snd_mixer_load(m)) < 0) {
68 fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
73 snd_mixer_selem_id_malloc(&sid);
79 /* Find the given mixer */
80 snd_mixer_selem_id_set_index(sid, mixer_idx);
81 snd_mixer_selem_id_set_name(sid, mixer);
82 if (!(elem = snd_mixer_find_selem(m, sid))) {
83 fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
84 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
86 snd_mixer_selem_id_free(sid);
90 /* Get the volume range to convert the volume later */
91 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
93 snd_mixer_handle_events (m);
94 snd_mixer_selem_get_playback_volume (elem, 0, &val);
96 float avgf = ((float)val / max) * 100;
98 avg = (avgf - avg < 0.5 ? avg : (avg+1));
99 } else avg = (int)val;
102 if (snd_mixer_selem_has_playback_switch(elem)) {
103 if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0)
104 fprintf (stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
106 START_COLOR("color_degraded");
112 snd_mixer_selem_id_free(sid);
114 const char *walk = fmt;
115 for (; *walk != '\0'; walk++) {
117 *(outwalk++) = *walk;
120 if (BEGINS_WITH(walk+1, "%")) {
121 outwalk += sprintf(outwalk, "%%");
124 if (BEGINS_WITH(walk+1, "volume")) {
125 outwalk += sprintf(outwalk, "%d%%", avg);
126 walk += strlen("volume");
130 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
132 char defaultmixer[] = "/dev/mixer";
133 int mixfd, vol, devmask = 0;
137 asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
139 mixerpath = defaultmixer;
141 if ((mixfd = open(mixerpath, O_RDWR)) < 0)
147 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
149 if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
152 if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
153 START_COLOR("color_degraded");
157 const char *walk = fmt;
158 for (; *walk != '\0'; walk++) {
160 *(outwalk++) = *walk;
163 if (BEGINS_WITH(walk+1, "%")) {
164 outwalk += sprintf(outwalk, "%%");
167 if (BEGINS_WITH(walk+1, "volume")) {
168 outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
169 walk += strlen("volume");
177 OUTPUT_FULL_TEXT(buffer);