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 */
47 /* Check if we already opened the mixer and get the handle
51 struct mixer_hdl *hdl;
52 TAILQ_FOREACH(hdl, &cached, handles) {
53 if (strcmp(hdl->device, device) != 0 ||
54 strcmp(hdl->mixer, mixer) != 0 ||
55 hdl->mixer_idx != mixer_idx)
62 if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL)
65 if ((hdl->device = strdup(device)) == NULL) {
70 if ((hdl->mixer = strdup(mixer)) == NULL) {
76 hdl->mixer_idx = mixer_idx;
77 snd_mixer_selem_id_malloc(&(hdl->sid));
78 if (hdl->sid == NULL) {
83 if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
84 fprintf(stderr, "ALSA: Cannot open mixer: %s\n", snd_strerror(err));
89 /* Attach this mixer handle to the given device */
90 if ((err = snd_mixer_attach(hdl->m, device)) < 0) {
91 fprintf(stderr, "ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
92 snd_mixer_close(hdl->m);
97 /* Register this mixer */
98 if ((err = snd_mixer_selem_register(hdl->m, NULL, NULL)) < 0) {
99 fprintf(stderr, "ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
100 snd_mixer_close(hdl->m);
105 if ((err = snd_mixer_load(hdl->m)) < 0) {
106 fprintf(stderr, "ALSA: snd_mixer_load: %s\n", snd_strerror(err));
107 snd_mixer_close(hdl->m);
112 /* Find the given mixer */
113 snd_mixer_selem_id_set_index(hdl->sid, mixer_idx);
114 snd_mixer_selem_id_set_name(hdl->sid, mixer);
115 if (!(hdl->elem = snd_mixer_find_selem(hdl->m, hdl->sid))) {
116 fprintf(stderr, "ALSA: Cannot find mixer %s (index %i)\n",
117 snd_mixer_selem_id_get_name(hdl->sid), snd_mixer_selem_id_get_index(hdl->sid));
118 snd_mixer_close(hdl->m);
123 /* Get the volume range to convert the volume later */
124 snd_mixer_selem_get_playback_volume_range(hdl->elem, &(hdl->min), &(hdl->max));
125 TAILQ_INSERT_TAIL(&cached, hdl, handles);
129 snd_mixer_handle_events (hdl->m);
130 snd_mixer_selem_get_playback_volume (hdl->elem, 0, &val);
132 if (hdl->max != 100) {
133 float avgf = ((float)val / hdl->max) * 100;
135 avg = (avgf - avg < 0.5 ? avg : (avg+1));
136 } else avg = (int)val;
139 if (snd_mixer_selem_has_playback_switch(hdl->elem)) {
141 if ((err = snd_mixer_selem_get_playback_switch(hdl->elem, 0, &pbval)) < 0)
142 fprintf (stderr, "ALSA: playback_switch: %s\n", snd_strerror(err));
147 const char *walk = fmt;
148 for (; *walk != '\0'; walk++) {
153 if (BEGINS_WITH(walk+1, "volume")) {
155 walk += strlen("volume");
160 char mixerpath[] = "/dev/mixer";
161 int mixfd, vol, devmask = 0;
163 if ((mixfd = open(mixerpath, O_RDWR)) < 0)
165 if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
167 if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
170 const char *walk = fmt;
171 for (; *walk != '\0'; walk++) {
176 if (BEGINS_WITH(walk+1, "volume")) {
177 printf("%d%%", vol & 0x7f);
178 walk += strlen("volume");