summaryrefslogtreecommitdiff
path: root/src/status/volume_alsa.c
blob: 54fe44ef8c07c20627ea173f92c0ac5df559efb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <alsa/asoundlib.h>

#include "common.h"


int status_volume_alsa(GlobalData *g,
                        char *cardname,
                        char *mixername,
                        snd_mixer_selem_channel_id_t channel)
{
  StatusItem s;
  char text[16] = { 0 };

  snd_mixer_t *handle = NULL;
  snd_mixer_elem_t *elem;
  snd_mixer_selem_id_t *sid;

  long min = 0, max = 0;
  long volume;
  int on_off;


  statusitem_init(&s);
  s.text = text;

  snd_mixer_selem_id_alloca(&sid);

  if (snd_mixer_open(&handle, 0) < 0) {
    return -1;
  }

  if (snd_mixer_attach(handle, cardname) < 0) {
    goto ERROR;
  }

  snd_mixer_selem_id_set_name(sid, mixername);

  if (snd_mixer_selem_register(handle, NULL, NULL) < 0) {
    goto ERROR;
  }

  if (snd_mixer_load(handle) < 0) {
    goto ERROR;
  }

  elem = snd_mixer_find_selem(handle, sid);
  if (!elem) {
    goto ERROR;
  }

  if (snd_mixer_selem_has_playback_volume(elem)
      && snd_mixer_selem_has_playback_channel(elem, channel)) {
    snd_mixer_selem_get_playback_switch(elem, channel, &on_off);
    snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
    snd_mixer_selem_get_playback_volume(elem, channel, &volume);

    s.color = on_off ? "#22FF22" : "red";
    snprintf(text, sizeof(text), "Vol: %d", (int)volume);
  }

  snd_mixer_close(handle);

  line_append_item(g, &s);

  return 0;

  ERROR:

  snd_mixer_close(handle);
  return -1;
}