2 * Copyright (c) 2011 Jan Vesely
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup mixerctl
32 /** @file Mixer control for audio devices
38 #include <str_error.h>
40 #include <audio_mixer_iface.h>
43 #define DEFAULT_SERVICE "devices/\\hw\\pci0\\00:01.0\\sb16\\control"
46 * Print volume levels on all channels on all control items.
47 * @param exch IPC exchange
49 static void print_levels(async_exch_t
*exch
)
53 errno_t ret
= audio_mixer_get_info(exch
, &name
, &count
);
55 printf("Failed to get mixer info: %s.\n", str_error(ret
));
58 printf("MIXER %s:\n\n", name
);
60 for (unsigned i
= 0; i
< count
; ++i
) {
62 unsigned levels
= 0, current
= 0;
64 audio_mixer_get_item_info(exch
, i
, &name
, &levels
);
66 printf("Failed to get item %u info: %s.\n",
70 ret
= audio_mixer_get_item_level(exch
, i
, ¤t
);
72 printf("Failed to get item %u info: %s.\n",
77 printf("Control item %u `%s' : %u/%u.\n",
78 i
, name
, current
, levels
- 1);
84 static unsigned get_number(const char *str
)
87 str_uint16_t(str
, NULL
, 10, false, &num
);
91 static void set_level(async_exch_t
*exch
, int argc
, char *argv
[])
94 if (argc
!= 4 && argc
!= 5) {
95 printf("%s [device] setlevel item value\n", argv
[0]);
98 unsigned params
= argc
== 5 ? 3 : 2;
99 const unsigned item
= get_number(argv
[params
++]);
100 const unsigned value
= get_number(argv
[params
]);
101 errno_t ret
= audio_mixer_set_item_level(exch
, item
, value
);
103 printf("Failed to set item level: %s.\n", str_error(ret
));
106 printf("Control item %u new level is %u.\n", item
, value
);
109 static void get_level(async_exch_t
*exch
, int argc
, char *argv
[])
112 if (argc
!= 3 && argc
!= 4) {
113 printf("%s [device] getlevel item \n", argv
[0]);
116 unsigned params
= argc
== 4 ? 3 : 2;
117 const unsigned item
= get_number(argv
[params
++]);
120 errno_t ret
= audio_mixer_get_item_level(exch
, item
, &value
);
122 printf("Failed to get item level: %s.\n", str_error(ret
));
125 printf("Control item %u level: %u.\n", item
, value
);
128 int main(int argc
, char *argv
[])
130 const char *service
= DEFAULT_SERVICE
;
131 void (*command
)(async_exch_t
*, int, char *[]) = NULL
;
133 if (argc
>= 2 && str_cmp(argv
[1], "setlevel") == 0) {
139 if (argc
>= 2 && str_cmp(argv
[1], "getlevel") == 0) {
145 if ((argc
== 2 && command
== NULL
))
148 service_id_t mixer_sid
;
149 errno_t rc
= loc_service_get_id(service
, &mixer_sid
, 0);
151 printf("Failed to resolve service '%s': %s.\n",
152 service
, str_error(rc
));
156 async_sess_t
*session
= loc_service_connect(mixer_sid
, INTERFACE_DDF
, 0);
158 printf("Failed connecting mixer service '%s'.\n", service
);
162 async_exch_t
*exch
= async_exchange_begin(session
);
164 printf("Failed to start session exchange.\n");
165 async_hangup(session
);
170 command(exch
, argc
, argv
);
173 printf("\n%s:\n", argv
[0]);
174 printf("Use '%s getlevel idx' command to read individual "
175 "settings\n", argv
[0]);
176 printf("Use '%s setlevel idx' command to change "
177 "settings\n", argv
[0]);
180 async_exchange_end(exch
);
181 async_hangup(session
);