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 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) {
31 char *outwalk = buffer;
34 /* Printing volume only works with ALSA at the moment */
35 if (output_format == O_I3BAR) {
37 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
44 snd_mixer_selem_id_t *sid;
45 snd_mixer_elem_t *elem;
49 if ((err = snd_mixer_open(&m, 0)) < 0) {
50 fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
54 /* Attach this mixer handle to the given device */
55 if ((err = snd_mixer_attach(m, device)) < 0) {
56 fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
61 /* Register this mixer */
62 if ((err = snd_mixer_selem_register(m, NULL, NULL)) < 0) {
63 fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
68 if ((err = snd_mixer_load(m)) < 0) {
69 fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
74 snd_mixer_selem_id_malloc(&sid);
80 /* Find the given mixer */
81 snd_mixer_selem_id_set_index(sid, mixer_idx);
82 snd_mixer_selem_id_set_name(sid, mixer);
83 if (!(elem = snd_mixer_find_selem(m, sid))) {
84 fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
85 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
87 snd_mixer_selem_id_free(sid);
91 /* Get the volume range to convert the volume later */
92 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
94 snd_mixer_handle_events(m);
95 snd_mixer_selem_get_playback_volume(elem, 0, &val);
97 float avgf = ((float)val / max) * 100;
99 avg = (avgf - avg < 0.5 ? avg : (avg + 1));
104 if (snd_mixer_selem_has_playback_switch(elem)) {
105 if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0)
106 fprintf(stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
108 START_COLOR("color_degraded");
114 snd_mixer_selem_id_free(sid);
116 const char *walk = fmt;
117 for (; *walk != '\0'; walk++) {
119 *(outwalk++) = *walk;
122 if (BEGINS_WITH(walk + 1, "%")) {
123 outwalk += sprintf(outwalk, "%%");
126 if (BEGINS_WITH(walk + 1, "volume")) {
127 outwalk += sprintf(outwalk, "%d%%", avg);
128 walk += strlen("volume");
132 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
134 char defaultmixer[] = "/dev/mixer";
135 int mixfd, vol, devmask = 0;
139 asprintf(&mixerpath, "/dev/mixer%d", mixer_idx);
141 mixerpath = defaultmixer;
143 if ((mixfd = open(mixerpath, O_RDWR)) < 0) {
144 warn("OSS: Cannot open mixer");
151 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
152 warn("OSS: Cannot read mixer information");
155 if (ioctl(mixfd, MIXER_READ(0), &vol) == -1) {
156 warn("OSS: Cannot read mixer information");
160 if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) {
161 START_COLOR("color_degraded");
165 const char *walk = fmt;
166 for (; *walk != '\0'; walk++) {
168 *(outwalk++) = *walk;
171 if (BEGINS_WITH(walk + 1, "%")) {
172 outwalk += sprintf(outwalk, "%%");
175 if (BEGINS_WITH(walk + 1, "volume")) {
176 outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
177 walk += strlen("volume");
187 OUTPUT_FULL_TEXT(buffer);