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
32 #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>
43 #include "module-lirc-symdef.h"
45 PA_MODULE_AUTHOR("Lennart Poettering")
46 PA_MODULE_DESCRIPTION("LIRC volume control")
47 PA_MODULE_VERSION(PACKAGE_VERSION)
48 PA_MODULE_USAGE("config=<config file> sink=<sink name> appname=<lirc application name>")
50 static const char* const valid_modargs[] = {
60 struct lirc_config *config;
63 float mute_toggle_save;
66 static int lirc_in_use = 0;
68 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) {
69 struct userdata *u = userdata;
70 char *name = NULL, *code = NULL;
74 if (events & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) {
75 pa_log("lost connection to LIRC daemon.");
79 if (events & PA_IO_EVENT_INPUT) {
82 if (lirc_nextcode(&code) != 0 || !code) {
83 pa_log("lirc_nextcode() failed.");
88 c[strcspn(c, "\n\r")] = 0;
89 pa_log_debug("raw IR code '%s'", c);
92 while (lirc_code2char(u->config, code, &name) == 0 && name) {
100 } volchange = INVALID;
102 pa_log_info("translated IR code '%s'", name);
104 if (strcasecmp(name, "volume-up") == 0)
106 else if (strcasecmp(name, "volume-down") == 0)
108 else if (strcasecmp(name, "mute") == 0)
110 else if (strcasecmp(name, "mute-toggle") == 0)
111 volchange = MUTE_TOGGLE;
112 else if (strcasecmp(name, "reset") == 0)
115 if (volchange == INVALID)
116 pa_log_warn("recieved unknown IR code '%s'", name);
120 if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK, 1)))
121 pa_log("failed to get sink '%s'", u->sink_name);
124 pa_cvolume cv = *pa_sink_get_volume(s, PA_MIXER_HARDWARE);
126 #define DELTA (PA_VOLUME_NORM/20)
130 for (i = 0; i < cv.channels; i++) {
131 cv.values[i] += DELTA;
133 if (cv.values[i] > PA_VOLUME_NORM)
134 cv.values[i] = PA_VOLUME_NORM;
137 pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
141 for (i = 0; i < cv.channels; i++) {
142 if (cv.values[i] >= DELTA)
143 cv.values[i] -= DELTA;
145 cv.values[i] = PA_VOLUME_MUTED;
148 pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
152 pa_sink_set_mute(s, PA_MIXER_HARDWARE, 0);
156 pa_sink_set_mute(s, PA_MIXER_HARDWARE, 1);
161 pa_sink_set_mute(s, PA_MIXER_HARDWARE, !pa_sink_get_mute(s, PA_MIXER_HARDWARE));
177 u->module->core->mainloop->io_free(u->io);
180 pa_module_unload_request(u->module);
185 int pa__init(pa_core *c, pa_module*m) {
186 pa_modargs *ma = NULL;
191 pa_log("module-lirc may no be loaded twice.");
195 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
196 pa_log("Failed to parse module arguments");
200 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
204 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
206 u->mute_toggle_save = 0;
208 if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "pulseaudio"), 1)) < 0) {
209 pa_log("lirc_init() failed.");
213 if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
214 pa_log("lirc_readconfig() failed.");
218 u->io = c->mainloop->io_new(c->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
235 void pa__done(pa_core *c, pa_module*m) {
240 if (!(u = m->userdata))
244 m->core->mainloop->io_free(u->io);
247 lirc_freeconfig(u->config);
252 pa_xfree(u->sink_name);