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;
55 /* Printing volume only works with ALSA at the moment */
56 if (output_format == O_I3BAR) {
58 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
63 /* Check if we already opened the mixer and get the handle
67 struct mixer_hdl *hdl;
68 TAILQ_FOREACH(hdl, &cached, handles) {
69 if (strcmp(hdl->device, device) != 0 ||
70 strcmp(hdl->mixer, mixer) != 0 ||
71 hdl->mixer_idx != mixer_idx)
78 if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL)
81 if ((hdl->device = strdup(device)) == NULL) {
86 if ((hdl->mixer = strdup(mixer)) == NULL) {
92 hdl->mixer_idx = mixer_idx;
93 snd_mixer_selem_id_malloc(&(hdl->sid));
94 if (hdl->sid == NULL) {
99 if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
100 fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
105 /* Attach this mixer handle to the given device */
106 if ((err = snd_mixer_attach(hdl->m, device)) < 0) {
107 fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
108 snd_mixer_close(hdl->m);
113 /* Register this mixer */
114 if ((err = snd_mixer_selem_register(hdl->m, NULL, NULL)) < 0) {
115 fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
116 snd_mixer_close(hdl->m);
121 if ((err = snd_mixer_load(hdl->m)) < 0) {
122 fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
123 snd_mixer_close(hdl->m);
128 /* Find the given mixer */
129 snd_mixer_selem_id_set_index(hdl->sid, mixer_idx);
130 snd_mixer_selem_id_set_name(hdl->sid, mixer);
131 if (!(hdl->elem = snd_mixer_find_selem(hdl->m, hdl->sid))) {
132 fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
133 snd_mixer_selem_id_get_name(hdl->sid), snd_mixer_selem_id_get_index(hdl->sid));
134 snd_mixer_close(hdl->m);
139 /* Get the volume range to convert the volume later */
140 snd_mixer_selem_get_playback_volume_range(hdl->elem, &(hdl->min), &(hdl->max));
141 TAILQ_INSERT_TAIL(&cached, hdl, handles);
145 snd_mixer_handle_events (hdl->m);
146 snd_mixer_selem_get_playback_volume (hdl->elem, 0, &val);
148 if (hdl->max != 100) {
149 float avgf = ((float)val / hdl->max) * 100;
151 avg = (avgf - avg < 0.5 ? avg : (avg+1));
152 } else avg = (int)val;
155 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));
163 const char *walk = fmt;
164 for (; *walk != '\0'; walk++) {
166 *(outwalk++) = *walk;
169 if (BEGINS_WITH(walk+1, "volume")) {
170 outwalk += sprintf(outwalk, "%d%%", avg);
171 walk += strlen("volume");
175 #if defined(__FreeBSD__) || defined(__OpenBSD__)
176 char mixerpath[] = "/dev/mixer";
177 int mixfd, vol, devmask = 0;
179 if ((mixfd = open(mixerpath, O_RDWR)) < 0)
181 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
183 if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
186 const char *walk = fmt;
187 for (; *walk != '\0'; walk++) {
189 *(outwalk++) = *walk;
192 if (BEGINS_WITH(walk+1, "volume")) {
193 outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
194 walk += strlen("volume");
201 OUTPUT_FULL_TEXT(buffer);