Change named colors to hex values
[sysstatus.git] / src / status / volume_alsa.c
1 #include <alsa/asoundlib.h>
2
3 #include "common.h"
4
5
6 int status_volume_alsa(GlobalData *g,
7                         char *cardname,
8                         char *mixername,
9                         snd_mixer_selem_channel_id_t channel)
10 {
11   StatusItem s;
12   char text[16] = { 0 };
13
14   snd_mixer_t *handle = NULL;
15   snd_mixer_elem_t *elem;
16   snd_mixer_selem_id_t *sid;
17
18   long min = 0, max = 0;
19   long volume;
20   int on_off;
21
22
23   statusitem_init(&s);
24   s.text = text;
25
26   snd_mixer_selem_id_alloca(&sid);
27
28   if (snd_mixer_open(&handle, 0) < 0) {
29     return -1;
30   }
31
32   if (snd_mixer_attach(handle, cardname) < 0) {
33     goto ERROR;
34   }
35
36   snd_mixer_selem_id_set_name(sid, mixername);
37
38   if (snd_mixer_selem_register(handle, NULL, NULL) < 0) {
39     goto ERROR;
40   }
41
42   if (snd_mixer_load(handle) < 0) {
43     goto ERROR;
44   }
45
46   elem = snd_mixer_find_selem(handle, sid);
47   if (!elem) {
48     goto ERROR;
49   }
50
51   if (snd_mixer_selem_has_playback_volume(elem)
52       && snd_mixer_selem_has_playback_channel(elem, channel)) {
53     snd_mixer_selem_get_playback_switch(elem, channel, &on_off);
54     snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
55     snd_mixer_selem_get_playback_volume(elem, channel, &volume);
56
57     s.color = on_off ? "#22FF22" : "#FF0000";  // green/red
58     snprintf(text, sizeof(text), "Vol: %d", (int)volume);
59   }
60
61   snd_mixer_close(handle);
62
63   line_append_item(g, &s);
64
65   return 0;
66
67   ERROR:
68
69   snd_mixer_close(handle);
70   return -1;
71 }