6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
10 #include <alsa/asoundlib.h>
17 #include <sys/soundcard.h>
23 #include <soundcard.h>
34 snd_mixer_selem_id_t *sid;
36 snd_mixer_elem_t *elem;
40 TAILQ_ENTRY(mixer_hdl) handles;
43 TAILQ_HEAD(handles_head, mixer_hdl) cached = TAILQ_HEAD_INITIALIZER(cached);
45 static void free_hdl(struct mixer_hdl *hdl) {
52 void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx) {
53 char *outwalk = buffer;
56 /* Printing volume only works with ALSA at the moment */
57 if (output_format == O_I3BAR) {
59 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
64 /* Check if we already opened the mixer and get the handle
68 struct mixer_hdl *hdl;
69 TAILQ_FOREACH(hdl, &cached, handles) {
70 if (strcmp(hdl->device, device) != 0 ||
71 strcmp(hdl->mixer, mixer) != 0 ||
72 hdl->mixer_idx != mixer_idx)
79 if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL)
82 if ((hdl->device = strdup(device)) == NULL) {
87 if ((hdl->mixer = strdup(mixer)) == NULL) {
93 hdl->mixer_idx = mixer_idx;
94 snd_mixer_selem_id_malloc(&(hdl->sid));
95 if (hdl->sid == NULL) {
100 if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
101 fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
106 /* Attach this mixer handle to the given device */
107 if ((err = snd_mixer_attach(hdl->m, device)) < 0) {
108 fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
109 snd_mixer_close(hdl->m);
114 /* Register this mixer */
115 if ((err = snd_mixer_selem_register(hdl->m, NULL, NULL)) < 0) {
116 fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
117 snd_mixer_close(hdl->m);
122 if ((err = snd_mixer_load(hdl->m)) < 0) {
123 fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
124 snd_mixer_close(hdl->m);
129 /* Find the given mixer */
130 snd_mixer_selem_id_set_index(hdl->sid, mixer_idx);
131 snd_mixer_selem_id_set_name(hdl->sid, mixer);
132 if (!(hdl->elem = snd_mixer_find_selem(hdl->m, hdl->sid))) {
133 fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
134 snd_mixer_selem_id_get_name(hdl->sid), snd_mixer_selem_id_get_index(hdl->sid));
135 snd_mixer_close(hdl->m);
140 /* Get the volume range to convert the volume later */
141 snd_mixer_selem_get_playback_volume_range(hdl->elem, &(hdl->min), &(hdl->max));
142 TAILQ_INSERT_TAIL(&cached, hdl, handles);
146 snd_mixer_handle_events (hdl->m);
147 snd_mixer_selem_get_playback_volume (hdl->elem, 0, &val);
149 if (hdl->max != 100) {
150 float avgf = ((float)val / hdl->max) * 100;
152 avg = (avgf - avg < 0.5 ? avg : (avg+1));
153 } else avg = (int)val;
156 if (snd_mixer_selem_has_playback_switch(hdl->elem)) {
157 if ((err = snd_mixer_selem_get_playback_switch(hdl->elem, 0, &pbval)) < 0)
158 fprintf (stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
160 START_COLOR("color_bad");
165 const char *walk = fmt;
166 for (; *walk != '\0'; walk++) {
168 *(outwalk++) = *walk;
171 if (BEGINS_WITH(walk+1, "volume")) {
172 outwalk += sprintf(outwalk, "%d%%", avg);
173 walk += strlen("volume");
177 #if defined(__FreeBSD__) || defined(__OpenBSD__)
178 char mixerpath[] = "/dev/mixer";
179 int mixfd, vol, devmask = 0;
181 if ((mixfd = open(mixerpath, O_RDWR)) < 0)
183 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
185 if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
188 const char *walk = fmt;
189 for (; *walk != '\0'; walk++) {
191 *(outwalk++) = *walk;
194 if (BEGINS_WITH(walk+1, "volume")) {
195 outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
196 walk += strlen("volume");
203 OUTPUT_FULL_TEXT(buffer);