6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
10 #include <alsa/asoundlib.h>
17 #include <sys/soundcard.h>
28 snd_mixer_selem_id_t *sid;
30 snd_mixer_elem_t *elem;
34 TAILQ_ENTRY(mixer_hdl) handles;
37 TAILQ_HEAD(handles_head, mixer_hdl) cached = TAILQ_HEAD_INITIALIZER(cached);
39 static void free_hdl(struct mixer_hdl *hdl) {
46 void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx) {
47 char *outwalk = buffer;
49 /* Printing volume only works with ALSA at the moment */
50 if (output_format == O_I3BAR) {
52 asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
57 /* Check if we already opened the mixer and get the handle
61 struct mixer_hdl *hdl;
62 TAILQ_FOREACH(hdl, &cached, handles) {
63 if (strcmp(hdl->device, device) != 0 ||
64 strcmp(hdl->mixer, mixer) != 0 ||
65 hdl->mixer_idx != mixer_idx)
72 if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL)
75 if ((hdl->device = strdup(device)) == NULL) {
80 if ((hdl->mixer = strdup(mixer)) == NULL) {
86 hdl->mixer_idx = mixer_idx;
87 snd_mixer_selem_id_malloc(&(hdl->sid));
88 if (hdl->sid == NULL) {
93 if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
94 fprintf(stderr, "ALSA: Cannot open mixer: %s\n", snd_strerror(err));
99 /* Attach this mixer handle to the given device */
100 if ((err = snd_mixer_attach(hdl->m, device)) < 0) {
101 fprintf(stderr, "ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
102 snd_mixer_close(hdl->m);
107 /* Register this mixer */
108 if ((err = snd_mixer_selem_register(hdl->m, NULL, NULL)) < 0) {
109 fprintf(stderr, "ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
110 snd_mixer_close(hdl->m);
115 if ((err = snd_mixer_load(hdl->m)) < 0) {
116 fprintf(stderr, "ALSA: snd_mixer_load: %s\n", snd_strerror(err));
117 snd_mixer_close(hdl->m);
122 /* Find the given mixer */
123 snd_mixer_selem_id_set_index(hdl->sid, mixer_idx);
124 snd_mixer_selem_id_set_name(hdl->sid, mixer);
125 if (!(hdl->elem = snd_mixer_find_selem(hdl->m, hdl->sid))) {
126 fprintf(stderr, "ALSA: Cannot find mixer %s (index %i)\n",
127 snd_mixer_selem_id_get_name(hdl->sid), snd_mixer_selem_id_get_index(hdl->sid));
128 snd_mixer_close(hdl->m);
133 /* Get the volume range to convert the volume later */
134 snd_mixer_selem_get_playback_volume_range(hdl->elem, &(hdl->min), &(hdl->max));
135 TAILQ_INSERT_TAIL(&cached, hdl, handles);
139 snd_mixer_handle_events (hdl->m);
140 snd_mixer_selem_get_playback_volume (hdl->elem, 0, &val);
142 if (hdl->max != 100) {
143 float avgf = ((float)val / hdl->max) * 100;
145 avg = (avgf - avg < 0.5 ? avg : (avg+1));
146 } else avg = (int)val;
149 if (snd_mixer_selem_has_playback_switch(hdl->elem)) {
151 if ((err = snd_mixer_selem_get_playback_switch(hdl->elem, 0, &pbval)) < 0)
152 fprintf (stderr, "ALSA: playback_switch: %s\n", snd_strerror(err));
157 const char *walk = fmt;
158 for (; *walk != '\0'; walk++) {
160 *(outwalk++) = *walk;
163 if (BEGINS_WITH(walk+1, "volume")) {
164 outwalk += sprintf(outwalk, "%d%%", avg);
165 walk += strlen("volume");
170 char mixerpath[] = "/dev/mixer";
171 int mixfd, vol, devmask = 0;
173 if ((mixfd = open(mixerpath, O_RDWR)) < 0)
175 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
177 if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
180 const char *walk = fmt;
181 for (; *walk != '\0'; walk++) {
183 *(outwalk++) = *walk;
186 if (BEGINS_WITH(walk+1, "volume")) {
187 outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
188 walk += strlen("volume");
195 OUTPUT_FULL_TEXT(buffer);