]> git.sur5r.net Git - i3/i3status/blob - src/print_volume.c
85b6176de03267b2f0e66df64aa76bf8ab7768ad
[i3/i3status] / src / print_volume.c
1 // vim:ts=8:expandtab
2 #include <time.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #ifdef LINUX
8 #include <alsa/asoundlib.h>
9 #include <alloca.h>
10 #endif
11
12 #ifdef __FreeBSD__
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <sys/soundcard.h>
16 #endif
17
18 #include "i3status.h"
19 #include "queue.h"
20
21 #ifdef LINUX
22 struct mixer_hdl {
23         char *device;
24         char *mixer;
25         int mixer_idx;
26         snd_mixer_selem_id_t *sid;
27         snd_mixer_t *m;
28         snd_mixer_elem_t *elem;
29         long min;
30         long max;
31
32         TAILQ_ENTRY(mixer_hdl) handles;
33 };
34
35 TAILQ_HEAD(handles_head, mixer_hdl) cached = TAILQ_HEAD_INITIALIZER(cached);
36
37 static void free_hdl(struct mixer_hdl *hdl) {
38         free(hdl->device);
39         free(hdl->mixer);
40         free(hdl);
41 }
42 #endif
43
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);
48 #ifdef LINUX
49         /* Check if we already opened the mixer and get the handle
50          * from cache if so */
51         bool found = false;
52         int err;
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)
58                         continue;
59                 found = true;
60                 break;
61         }
62
63         if (!found) {
64                 if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL)
65                         return;
66
67                 if ((hdl->device = strdup(device)) == NULL) {
68                         free(hdl);
69                         return;
70                 }
71
72                 if ((hdl->mixer = strdup(mixer)) == NULL) {
73                         free(hdl->device);
74                         free(hdl);
75                         return;
76                 }
77
78                 hdl->mixer_idx = mixer_idx;
79                 snd_mixer_selem_id_malloc(&(hdl->sid));
80                 if (hdl->sid == NULL) {
81                         free_hdl(hdl);
82                         return;
83                 }
84
85                 if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
86                         fprintf(stderr, "ALSA: Cannot open mixer: %s\n", snd_strerror(err));
87                         free_hdl(hdl);
88                         return;
89                 }
90
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);
95                         free_hdl(hdl);
96                         return;
97                 }
98
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);
103                         free_hdl(hdl);
104                         return;
105                 }
106
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);
110                         free_hdl(hdl);
111                         return;
112                 }
113
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);
121                         free_hdl(hdl);
122                         return;
123                 }
124
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);
128         }
129
130         long val;
131         snd_mixer_handle_events (hdl->m);
132         snd_mixer_selem_get_playback_volume (hdl->elem, 0, &val);
133         int avg;
134         if (hdl->max != 100) {
135                 float avgf = ((float)val / hdl->max) * 100;
136                 avg = (int)avgf;
137                 avg = (avgf - avg < 0.5 ? avg : (avg+1));
138         } else avg = (int)val;
139
140         /* Check for mute */
141         if (snd_mixer_selem_has_playback_switch(hdl->elem)) {
142                 int pbval;
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));
145                 if (!pbval)
146                         avg = 0;
147         }
148
149         const char *walk = fmt;
150         for (; *walk != '\0'; walk++) {
151                 if (*walk != '%') {
152                         putchar(*walk);
153                         continue;
154                 }
155                 if (BEGINS_WITH(walk+1, "volume")) {
156                         printf("%d%%", avg);
157                         walk += strlen("volume");
158                 }
159         }
160 #endif
161 #ifdef __FreeBSD__
162         char mixerpath[] = "/dev/mixer";
163         int mixfd, vol, devmask = 0;
164
165         if ((mixfd = open(mixerpath, O_RDWR)) < 0)
166                 return;
167         if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
168                 return;
169         if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
170                 return;
171
172         const char *walk = fmt;
173         for (; *walk != '\0'; walk++) {
174                 if (*walk != '%') {
175                         putchar(*walk);
176                         continue;
177                 }
178                 if (BEGINS_WITH(walk+1, "volume")) {
179                         printf("%d%%", vol & 0x7f);
180                         walk += strlen("volume");
181                 }
182         }
183         close(mixfd);
184 #endif
185         if (output_format == O_I3BAR)
186                 printf("\"}");
187 }