Better file structure and build system
[sysstatus.git] / src / status / volume_alsa.c
1 #include <alsa/asoundlib.h>
2
3 #include "status/volume_alsa.h"
4
5
6 int status_volume_alsa(char *cardname, char *mixername, snd_mixer_selem_channel_id_t channel)
7 {
8   snd_mixer_t *handle = NULL;
9   snd_mixer_elem_t *elem;
10   snd_mixer_selem_id_t *sid;
11
12   long min = 0, max = 0;
13   long volume;
14   int on_off;
15
16
17   snd_mixer_selem_id_alloca(&sid);
18
19   if (snd_mixer_open(&handle, 0) < 0) {
20     return -1;
21   }
22
23   if (snd_mixer_attach(handle, cardname) < 0) {
24     goto ERROR;
25   }
26
27   snd_mixer_selem_id_set_name(sid, mixername);
28
29   if (snd_mixer_selem_register(handle, NULL, NULL) < 0) {
30     goto ERROR;
31   }
32
33   if (snd_mixer_load(handle) < 0) {
34     goto ERROR;
35   }
36
37   elem = snd_mixer_find_selem(handle, sid);
38   if (!elem) {
39     goto ERROR;
40   }
41
42   if (snd_mixer_selem_has_playback_volume(elem)
43       && snd_mixer_selem_has_playback_channel(elem, channel)) {
44     snd_mixer_selem_get_playback_switch(elem, channel, &on_off);
45     if (on_off) {
46       fputs("^fg(#22FF22)", stdout);
47     } else {
48       fputs("^fg(red)", stdout);
49     }
50
51     snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
52
53     snd_mixer_selem_get_playback_volume(elem, channel, &volume);
54     fputs("^ca(1, amixer sset Master toggle)", stdout);
55     fputs("^ca(4, amixer sset Master 2+ unmute)", stdout);
56     fputs("^ca(5, amixer sset Master 2- unmute)", stdout);
57     printf(" Vol: %d ", (int)volume);
58     fputs("^ca()", stdout);
59     fputs("^ca()", stdout);
60     fputs("^ca()", stdout);
61   }
62
63   snd_mixer_close(handle);
64
65   return 0;
66
67   ERROR:
68
69   snd_mixer_close(handle);
70   return -1;
71 }