2 This file is part of PulseAudio.
4 Copyright 2005-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <lirc/lirc_client.h>
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/namereg.h>
38 #include <pulsecore/sink.h>
39 #include <pulsecore/modargs.h>
40 #include <pulsecore/macro.h>
42 #include "module-lirc-symdef.h"
44 PA_MODULE_AUTHOR("Lennart Poettering");
45 PA_MODULE_DESCRIPTION("LIRC volume control");
46 PA_MODULE_VERSION(PACKAGE_VERSION
);
47 PA_MODULE_LOAD_ONCE(TRUE
);
48 PA_MODULE_USAGE("config=<config file> sink=<sink name> appname=<lirc application name> volume_limit=<volume limit> volume_step=<volume change step>");
50 static const char* const valid_modargs
[] = {
62 struct lirc_config
*config
;
65 float mute_toggle_save
;
66 pa_volume_t volume_limit
;
67 pa_volume_t volume_step
;
70 static void io_callback(pa_mainloop_api
*io
, pa_io_event
*e
, int fd
, pa_io_event_flags_t events
, void*userdata
) {
71 struct userdata
*u
= userdata
;
72 char *name
= NULL
, *code
= NULL
;
77 if (events
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
)) {
78 pa_log("Lost connection to LIRC daemon.");
82 if (events
& PA_IO_EVENT_INPUT
) {
85 if (lirc_nextcode(&code
) != 0 || !code
) {
86 pa_log("lirc_nextcode() failed.");
91 c
[strcspn(c
, "\n\r")] = 0;
92 pa_log_debug("Raw IR code '%s'", c
);
95 while (lirc_code2char(u
->config
, code
, &name
) == 0 && name
) {
103 } volchange
= INVALID
;
105 pa_log_info("Translated IR code '%s'", name
);
107 if (strcasecmp(name
, "volume-up") == 0)
109 else if (strcasecmp(name
, "volume-down") == 0)
111 else if (strcasecmp(name
, "mute") == 0)
113 else if (strcasecmp(name
, "mute-toggle") == 0)
114 volchange
= MUTE_TOGGLE
;
115 else if (strcasecmp(name
, "reset") == 0)
118 if (volchange
== INVALID
)
119 pa_log_warn("Received unknown IR code '%s'", name
);
123 if (!(s
= pa_namereg_get(u
->module
->core
, u
->sink_name
, PA_NAMEREG_SINK
)))
124 pa_log("Failed to get sink '%s'", u
->sink_name
);
126 pa_cvolume cv
= *pa_sink_get_volume(s
, FALSE
);
130 pa_cvolume_inc_clamp(&cv
, u
->volume_step
, u
->volume_limit
);
131 pa_sink_set_volume(s
, &cv
, TRUE
, TRUE
);
135 pa_cvolume_dec(&cv
, u
->volume_step
);
136 pa_sink_set_volume(s
, &cv
, TRUE
, TRUE
);
140 pa_sink_set_mute(s
, TRUE
, TRUE
);
144 pa_sink_set_mute(s
, FALSE
, TRUE
);
148 pa_sink_set_mute(s
, !pa_sink_get_mute(s
, FALSE
), TRUE
);
152 pa_assert_not_reached();
164 u
->module
->core
->mainloop
->io_free(u
->io
);
167 pa_module_unload_request(u
->module
, TRUE
);
172 int pa__init(pa_module
*m
) {
173 pa_modargs
*ma
= NULL
;
175 pa_volume_t volume_limit
= PA_VOLUME_NORM
*3/2;
176 pa_volume_t volume_step
= PA_VOLUME_NORM
/20;
180 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
181 pa_log("Failed to parse module arguments");
185 if (pa_modargs_get_value_u32(ma
, "volume_limit", &volume_limit
) < 0) {
186 pa_log("Failed to parse volume limit");
190 if (pa_modargs_get_value_u32(ma
, "volume_step", &volume_step
) < 0) {
191 pa_log("Failed to parse volume step");
195 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
199 u
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
201 u
->mute_toggle_save
= 0;
202 u
->volume_limit
= volume_limit
;
203 u
->volume_step
= volume_step
;
205 if ((u
->lirc_fd
= lirc_init((char*) pa_modargs_get_value(ma
, "appname", "pulseaudio"), 1)) < 0) {
206 pa_log("lirc_init() failed.");
210 if (lirc_readconfig((char*) pa_modargs_get_value(ma
, "config", NULL
), &u
->config
, NULL
) < 0) {
211 pa_log("lirc_readconfig() failed.");
215 u
->io
= m
->core
->mainloop
->io_new(m
->core
->mainloop
, u
->lirc_fd
, PA_IO_EVENT_INPUT
|PA_IO_EVENT_HANGUP
, io_callback
, u
);
230 void pa__done(pa_module
*m
) {
234 if (!(u
= m
->userdata
))
238 m
->core
->mainloop
->io_free(u
->io
);
241 lirc_freeconfig(u
->config
);
246 pa_xfree(u
->sink_name
);