4 This file is part of PulseAudio.
6 Copyright 2005-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <lirc/lirc_client.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/macro.h>
44 #include "module-lirc-symdef.h"
46 PA_MODULE_AUTHOR("Lennart Poettering")
47 PA_MODULE_DESCRIPTION("LIRC volume control")
48 PA_MODULE_VERSION(PACKAGE_VERSION
)
49 PA_MODULE_USAGE("config=<config file> sink=<sink name> appname=<lirc application name>")
51 static const char* const valid_modargs
[] = {
61 struct lirc_config
*config
;
64 float mute_toggle_save
;
67 static int lirc_in_use
= 0;
69 static void io_callback(pa_mainloop_api
*io
, PA_GCC_UNUSED pa_io_event
*e
, PA_GCC_UNUSED
int fd
, pa_io_event_flags_t events
, void*userdata
) {
70 struct userdata
*u
= userdata
;
71 char *name
= NULL
, *code
= NULL
;
76 if (events
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
)) {
77 pa_log("Lost connection to LIRC daemon.");
81 if (events
& PA_IO_EVENT_INPUT
) {
84 if (lirc_nextcode(&code
) != 0 || !code
) {
85 pa_log("lirc_nextcode() failed.");
90 c
[strcspn(c
, "\n\r")] = 0;
91 pa_log_debug("Raw IR code '%s'", c
);
94 while (lirc_code2char(u
->config
, code
, &name
) == 0 && name
) {
102 } volchange
= INVALID
;
104 pa_log_info("Translated IR code '%s'", name
);
106 if (strcasecmp(name
, "volume-up") == 0)
108 else if (strcasecmp(name
, "volume-down") == 0)
110 else if (strcasecmp(name
, "mute") == 0)
112 else if (strcasecmp(name
, "mute-toggle") == 0)
113 volchange
= MUTE_TOGGLE
;
114 else if (strcasecmp(name
, "reset") == 0)
117 if (volchange
== INVALID
)
118 pa_log_warn("Recieved unknown IR code '%s'", name
);
122 if (!(s
= pa_namereg_get(u
->module
->core
, u
->sink_name
, PA_NAMEREG_SINK
, 1)))
123 pa_log("Failed to get sink '%s'", u
->sink_name
);
126 pa_cvolume cv
= *pa_sink_get_volume(s
);
128 #define DELTA (PA_VOLUME_NORM/20)
132 for (i
= 0; i
< cv
.channels
; i
++) {
133 cv
.values
[i
] += DELTA
;
135 if (cv
.values
[i
] > PA_VOLUME_NORM
)
136 cv
.values
[i
] = PA_VOLUME_NORM
;
139 pa_sink_set_volume(s
, &cv
);
143 for (i
= 0; i
< cv
.channels
; i
++) {
144 if (cv
.values
[i
] >= DELTA
)
145 cv
.values
[i
] -= DELTA
;
147 cv
.values
[i
] = PA_VOLUME_MUTED
;
150 pa_sink_set_volume(s
, &cv
);
154 pa_sink_set_mute(s
, 0);
158 pa_sink_set_mute(s
, 1);
163 pa_sink_set_mute(s
, !pa_sink_get_mute(s
));
179 u
->module
->core
->mainloop
->io_free(u
->io
);
182 pa_module_unload_request(u
->module
);
187 int pa__init(pa_module
*m
) {
188 pa_modargs
*ma
= NULL
;
194 pa_log("module-lirc may no be loaded twice.");
198 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
199 pa_log("Failed to parse module arguments");
203 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
207 u
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
209 u
->mute_toggle_save
= 0;
211 if ((u
->lirc_fd
= lirc_init((char*) pa_modargs_get_value(ma
, "appname", "pulseaudio"), 1)) < 0) {
212 pa_log("lirc_init() failed.");
216 if (lirc_readconfig((char*) pa_modargs_get_value(ma
, "config", NULL
), &u
->config
, NULL
) < 0) {
217 pa_log("lirc_readconfig() failed.");
221 u
->io
= m
->core
->mainloop
->io_new(m
->core
->mainloop
, u
->lirc_fd
, PA_IO_EVENT_INPUT
|PA_IO_EVENT_HANGUP
, io_callback
, u
);
238 void pa__done(pa_module
*m
) {
242 if (!(u
= m
->userdata
))
246 m
->core
->mainloop
->io_free(u
->io
);
249 lirc_freeconfig(u
->config
);
254 pa_xfree(u
->sink_name
);