1 /*
2 * alsa.c
3 *
4 * Copyright (C) 2012 - Dmitry Kosenkov
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <alsa/asoundlib.h>
21
22 #include "alsa.h"
23
24 static char *default_device = "default";
25
26 static snd_mixer_t * mixer_id = NULL;
27 snd_mixer_selem_id_t *sid;
28
vol_backend_init(char * device)29 int vol_backend_init (char *device)
30 {
31 snd_mixer_open(&mixer_id, 0);
32 snd_mixer_attach(mixer_id, device ? device : default_device);
33 snd_mixer_selem_register(mixer_id, NULL, NULL);
34 snd_mixer_load(mixer_id);
35
36 snd_mixer_selem_id_malloc(&sid);
37
38 snd_mixer_selem_id_set_name(sid, "Master");
39 snd_mixer_elem_t* elem = snd_mixer_find_selem(mixer_id, sid);
40 if (!elem) return 0;
41
42 snd_mixer_selem_id_set_name(sid, "PCM");
43 elem = snd_mixer_find_selem(mixer_id, sid);
44 if (!elem) return 0;
45
46 return 1;
47 }
48
vol_backend_get(int mixer)49 int vol_backend_get(int mixer)
50 {
51 snd_mixer_handle_events(mixer_id);
52 snd_mixer_selem_id_set_name(sid, mixer == 0 ? "Master" : "PCM");
53 snd_mixer_elem_t* elem = snd_mixer_find_selem(mixer_id, sid);
54
55 long min, max, vol;
56 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
57 snd_mixer_selem_get_playback_volume(elem, 0, &vol);
58
59 return 100 * vol / max;
60 }
61
vol_backend_set(int mixer,int value)62 void vol_backend_set(int mixer, int value)
63 {
64 snd_mixer_selem_id_set_name(sid, mixer == 0 ? "Master" : "PCM");
65 snd_mixer_elem_t* elem = snd_mixer_find_selem(mixer_id, sid);
66
67 long min, max;
68 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
69 snd_mixer_selem_set_playback_volume_all(elem, max * value / 100);
70 }
71