Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 144 lines (125 sloc) 3.984 kB
c5c3938 new volume helper
lcapriotti authored
1 /*
2 * Copyright (C) 2005-2010 Team XBMC
3 * http://www.xbmc.org
4 *
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22 /*
11067e0 cosmetics: revised comment
lcapriotti authored
23 * Compile with: gcc -lasound -osetAlsaVolumes setAlsaVolumes.c
24 * (libasound2-dev needed)
c5c3938 new volume helper
lcapriotti authored
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <alsa/asoundlib.h>
31
32 int main(int argc, char *argv[])
33 {
34 long volume = 90;
35 int verbose = 0;
36
37 int err;
38 snd_mixer_t *handle;
39 snd_mixer_elem_t *elem;
40 snd_mixer_selem_id_t *sid;
41 char *card = "default";
42 int aChar;
43 long min=0, max=0;
44
45 opterr = 0;
46
47 while ((aChar = getopt (argc, argv, "v")) != -1) {
48 switch (aChar) {
49 case 'v':
50 verbose = 1;
51 printf(" Verbose mode ON\n");
52 break;
53 default:
54 abort();
55 }
56 }
57
58 if ( optind < argc )
59 volume = atol(argv[optind]);
60
61 printf("Setting volumes at %ld%%\n", volume);
62
63 if ((err = snd_mixer_open(&handle, 0)) < 0) {
8b09b0e Fix build failure of setAlsaVolumes.
ceros7 authored
64 fprintf(stderr, "Mixer %s open error: %s", card, snd_strerror(err));
c5c3938 new volume helper
lcapriotti authored
65 return err;
66 }
67 if ((err = snd_mixer_attach(handle, card)) < 0) {
8b09b0e Fix build failure of setAlsaVolumes.
ceros7 authored
68 fprintf(stderr, "Mixer attach %s error: %s", card, snd_strerror(err));
c5c3938 new volume helper
lcapriotti authored
69 snd_mixer_close(handle);
70 return err;
71 }
72 if( (err=snd_mixer_selem_register(handle,NULL,NULL)) < 0 ){
8b09b0e Fix build failure of setAlsaVolumes.
ceros7 authored
73 fprintf(stderr, "ALSA: snd_mixer_selem_register failed: %d\n",err);
c5c3938 new volume helper
lcapriotti authored
74 snd_mixer_close(handle);
75 return err;
76 }
77 if( (err=snd_mixer_load(handle)) < 0 ){
8b09b0e Fix build failure of setAlsaVolumes.
ceros7 authored
78 fprintf(stderr, "ALSA: snd_mixer_load failed: %d\n",err);
c5c3938 new volume helper
lcapriotti authored
79 snd_mixer_close(handle);
80 return err;
81 }
82
83 snd_mixer_selem_id_alloca(&sid);
84 for (elem = snd_mixer_first_elem(handle); elem; elem = snd_mixer_elem_next(elem)) {
85 snd_mixer_selem_get_id(elem, sid);
86
87 if ( verbose ) {
88 const char *controlName;
89 controlName = snd_mixer_selem_id_get_name(sid);
90 printf("Name = %s\n", controlName);
91 }
92
93 if (snd_mixer_selem_has_playback_volume(elem)) {
94 if ( verbose )
95 printf(" Has playback volume\n");
96 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
97
98 if( (err=snd_mixer_selem_set_playback_volume_all(elem, volume*max/100)) < 0 ) {
99 fprintf(stderr, "ALSA: snd_mixer_selem_set_playback_volume_all failed: %d\n",err);
100 } else {
101 if ( verbose )
102 printf(" ** Volume increased to %ld\n", volume);
103 }
104 }
105 if (snd_mixer_selem_has_playback_switch(elem)) {
106 if ( verbose )
107 printf(" Has playback switch\n");
108
109 if( (err=snd_mixer_selem_set_playback_switch_all(elem, 1)) < 0 ) {
110 fprintf(stderr, "ALSA: snd_mixer_selem_set_playback_switch_all failed: %d\n",err);
111 } else {
112 if ( verbose )
113 printf(" ** Unmuted\n");
114 }
115 }
116 if (snd_mixer_selem_has_capture_volume(elem)) {
117 if ( verbose )
118 printf(" Has capture volume\n");
119
120 if( (err=snd_mixer_selem_set_capture_volume_all(elem, 0)) < 0 ) {
121 fprintf(stderr, "ALSA: snd_mixer_selem_set_capture_volume_all failed: %d\n",err);
122 } else {
123 if ( verbose )
8b09b0e Fix build failure of setAlsaVolumes.
ceros7 authored
124 printf(" ** Volume decreased to 0\n");
c5c3938 new volume helper
lcapriotti authored
125 }
126 }
127 if (snd_mixer_selem_has_capture_switch(elem)) {
128 if ( verbose )
129 printf(" Has capture switch\n");
130
131 if( (err=snd_mixer_selem_set_capture_switch_all(elem, 0)) < 0 ) {
132 fprintf(stderr, "ALSA: snd_mixer_selem_set_capture_switch_all failed: %d\n",err);
133 } else {
134 if ( verbose )
135 printf(" ** Muted\n");
136 }
137 }
138 }
139
140 snd_mixer_close(handle);
141
142 return 0;
143 }
Something went wrong with that request. Please try again.