8 #include <alsa/asoundlib.h>
15 #include <sys/soundcard.h>
26 snd_mixer_selem_id_t *sid;
28 snd_mixer_elem_t *elem;
32 TAILQ_ENTRY(mixer_hdl) handles;
35 TAILQ_HEAD(handles_head, mixer_hdl) cached = TAILQ_HEAD_INITIALIZER(cached);
37 static void free_hdl(struct mixer_hdl *hdl) {
44 void print_volume(const char *fmt, const char *device, const char *mixer, int mixer_idx) {
45 /* Printing volume only works with ALSA at the moment */
46 if (output_format == O_I3BAR)
47 printf("{\"name\":\"volume\", \"instance\": \"%s.%s.%d\", \"full_text\":\"", device, mixer, mixer_idx);
49 /* Check if we already opened the mixer and get the handle
53 struct mixer_hdl *hdl;
54 TAILQ_FOREACH(hdl, &cached, handles) {
55 if (strcmp(hdl->device, device) != 0 ||
56 strcmp(hdl->mixer, mixer) != 0 ||
57 hdl->mixer_idx != mixer_idx)
64 if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL)
67 if ((hdl->device = strdup(device)) == NULL) {
72 if ((hdl->mixer = strdup(mixer)) == NULL) {
78 hdl->mixer_idx = mixer_idx;
79 snd_mixer_selem_id_malloc(&(hdl->sid));
80 if (hdl->sid == NULL) {
85 if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
86 fprintf(stderr, "ALSA: Cannot open mixer: %s\n", snd_strerror(err));
91 /* Attach this mixer handle to the given device */
92 if ((err = snd_mixer_attach(hdl->m, device)) < 0) {
93 fprintf(stderr, "ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
94 snd_mixer_close(hdl->m);
99 /* Register this mixer */
100 if ((err = snd_mixer_selem_register(hdl->m, NULL, NULL)) < 0) {
101 fprintf(stderr, "ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
102 snd_mixer_close(hdl->m);
107 if ((err = snd_mixer_load(hdl->m)) < 0) {
108 fprintf(stderr, "ALSA: snd_mixer_load: %s\n", snd_strerror(err));
109 snd_mixer_close(hdl->m);
114 /* Find the given mixer */
115 snd_mixer_selem_id_set_index(hdl->sid, mixer_idx);
116 snd_mixer_selem_id_set_name(hdl->sid, mixer);
117 if (!(hdl->elem = snd_mixer_find_selem(hdl->m, hdl->sid))) {
118 fprintf(stderr, "ALSA: Cannot find mixer %s (index %i)\n",
119 snd_mixer_selem_id_get_name(hdl->sid), snd_mixer_selem_id_get_index(hdl->sid));
120 snd_mixer_close(hdl->m);
125 /* Get the volume range to convert the volume later */
126 snd_mixer_selem_get_playback_volume_range(hdl->elem, &(hdl->min), &(hdl->max));
127 TAILQ_INSERT_TAIL(&cached, hdl, handles);
131 snd_mixer_handle_events (hdl->m);
132 snd_mixer_selem_get_playback_volume (hdl->elem, 0, &val);
134 if (hdl->max != 100) {
135 float avgf = ((float)val / hdl->max) * 100;
137 avg = (avgf - avg < 0.5 ? avg : (avg+1));
138 } else avg = (int)val;
141 if (snd_mixer_selem_has_playback_switch(hdl->elem)) {
143 if ((err = snd_mixer_selem_get_playback_switch(hdl->elem, 0, &pbval)) < 0)
144 fprintf (stderr, "ALSA: playback_switch: %s\n", snd_strerror(err));
149 const char *walk = fmt;
150 for (; *walk != '\0'; walk++) {
155 if (BEGINS_WITH(walk+1, "volume")) {
157 walk += strlen("volume");
162 char mixerpath[] = "/dev/mixer";
163 int mixfd, vol, devmask = 0;
165 if ((mixfd = open(mixerpath, O_RDWR)) < 0)
167 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
169 if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
172 const char *walk = fmt;
173 for (; *walk != '\0'; walk++) {
178 if (BEGINS_WITH(walk+1, "volume")) {
179 printf("%d%%", vol & 0x7f);
180 walk += strlen("volume");
185 if (output_format == O_I3BAR)