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