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