]> code.delx.au - pulseaudio/blob - src/modules/module-lirc.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-lirc.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2005-2006 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include <lirc/lirc_client.h>
34
35 #include <pulse/xmalloc.h>
36
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>
43
44 #include "module-lirc-symdef.h"
45
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>")
50
51 static const char* const valid_modargs[] = {
52 "config",
53 "sink",
54 "appname",
55 NULL,
56 };
57
58 struct userdata {
59 int lirc_fd;
60 pa_io_event *io;
61 struct lirc_config *config;
62 char *sink_name;
63 pa_module *module;
64 float mute_toggle_save;
65 };
66
67 static int lirc_in_use = 0;
68
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;
72
73 pa_assert(io);
74 pa_assert(u);
75
76 if (events & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) {
77 pa_log("Lost connection to LIRC daemon.");
78 goto fail;
79 }
80
81 if (events & PA_IO_EVENT_INPUT) {
82 char *c;
83
84 if (lirc_nextcode(&code) != 0 || !code) {
85 pa_log("lirc_nextcode() failed.");
86 goto fail;
87 }
88
89 c = pa_xstrdup(code);
90 c[strcspn(c, "\n\r")] = 0;
91 pa_log_debug("Raw IR code '%s'", c);
92 pa_xfree(c);
93
94 while (lirc_code2char(u->config, code, &name) == 0 && name) {
95 enum {
96 INVALID,
97 UP,
98 DOWN,
99 MUTE,
100 RESET,
101 MUTE_TOGGLE
102 } volchange = INVALID;
103
104 pa_log_info("Translated IR code '%s'", name);
105
106 if (strcasecmp(name, "volume-up") == 0)
107 volchange = UP;
108 else if (strcasecmp(name, "volume-down") == 0)
109 volchange = DOWN;
110 else if (strcasecmp(name, "mute") == 0)
111 volchange = MUTE;
112 else if (strcasecmp(name, "mute-toggle") == 0)
113 volchange = MUTE_TOGGLE;
114 else if (strcasecmp(name, "reset") == 0)
115 volchange = RESET;
116
117 if (volchange == INVALID)
118 pa_log_warn("Recieved unknown IR code '%s'", name);
119 else {
120 pa_sink *s;
121
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);
124 else {
125 int i;
126 pa_cvolume cv = *pa_sink_get_volume(s);
127
128 #define DELTA (PA_VOLUME_NORM/20)
129
130 switch (volchange) {
131 case UP:
132 for (i = 0; i < cv.channels; i++) {
133 cv.values[i] += DELTA;
134
135 if (cv.values[i] > PA_VOLUME_NORM)
136 cv.values[i] = PA_VOLUME_NORM;
137 }
138
139 pa_sink_set_volume(s, &cv);
140 break;
141
142 case DOWN:
143 for (i = 0; i < cv.channels; i++) {
144 if (cv.values[i] >= DELTA)
145 cv.values[i] -= DELTA;
146 else
147 cv.values[i] = PA_VOLUME_MUTED;
148 }
149
150 pa_sink_set_volume(s, &cv);
151 break;
152
153 case MUTE:
154 pa_sink_set_mute(s, 0);
155 break;
156
157 case RESET:
158 pa_sink_set_mute(s, 1);
159 break;
160
161 case MUTE_TOGGLE:
162
163 pa_sink_set_mute(s, !pa_sink_get_mute(s));
164 break;
165
166 case INVALID:
167 ;
168 }
169 }
170 }
171 }
172 }
173
174 pa_xfree(code);
175
176 return;
177
178 fail:
179 u->module->core->mainloop->io_free(u->io);
180 u->io = NULL;
181
182 pa_module_unload_request(u->module);
183
184 pa_xfree(code);
185 }
186
187 int pa__init(pa_module*m) {
188 pa_modargs *ma = NULL;
189 struct userdata *u;
190
191 pa_assert(m);
192
193 if (lirc_in_use) {
194 pa_log("module-lirc may no be loaded twice.");
195 return -1;
196 }
197
198 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
199 pa_log("Failed to parse module arguments");
200 goto fail;
201 }
202
203 m->userdata = u = pa_xnew(struct userdata, 1);
204 u->module = m;
205 u->io = NULL;
206 u->config = NULL;
207 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
208 u->lirc_fd = -1;
209 u->mute_toggle_save = 0;
210
211 if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "pulseaudio"), 1)) < 0) {
212 pa_log("lirc_init() failed.");
213 goto fail;
214 }
215
216 if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
217 pa_log("lirc_readconfig() failed.");
218 goto fail;
219 }
220
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);
222
223 lirc_in_use = 1;
224
225 pa_modargs_free(ma);
226
227 return 0;
228
229 fail:
230
231 if (ma)
232 pa_modargs_free(ma);
233
234 pa__done(m);
235 return -1;
236 }
237
238 void pa__done(pa_module*m) {
239 struct userdata *u;
240 pa_assert(m);
241
242 if (!(u = m->userdata))
243 return;
244
245 if (u->io)
246 m->core->mainloop->io_free(u->io);
247
248 if (u->config)
249 lirc_freeconfig(u->config);
250
251 if (u->lirc_fd >= 0)
252 lirc_deinit();
253
254 pa_xfree(u->sink_name);
255 pa_xfree(u);
256
257 lirc_in_use = 0;
258 }