]> code.delx.au - pulseaudio/blob - src/modules/module-lirc.c
d0e902f69b3b080e34b9005d68d30af49aa9fbc5
[pulseaudio] / src / modules / module-lirc.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2005-2006 Lennart Poettering
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.1 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 <unistd.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include <lirc/lirc_client.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 #include <pulsecore/macro.h>
41
42 #include "module-lirc-symdef.h"
43
44 PA_MODULE_AUTHOR("Lennart Poettering");
45 PA_MODULE_DESCRIPTION("LIRC volume control");
46 PA_MODULE_VERSION(PACKAGE_VERSION);
47 PA_MODULE_LOAD_ONCE(TRUE);
48 PA_MODULE_USAGE("config=<config file> sink=<sink name> appname=<lirc application name>");
49
50 static const char* const valid_modargs[] = {
51 "config",
52 "sink",
53 "appname",
54 NULL,
55 };
56
57 struct userdata {
58 int lirc_fd;
59 pa_io_event *io;
60 struct lirc_config *config;
61 char *sink_name;
62 pa_module *module;
63 float mute_toggle_save;
64 };
65
66 #define DELTA (PA_VOLUME_NORM/20)
67
68 static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event_flags_t events, void*userdata) {
69 struct userdata *u = userdata;
70 char *name = NULL, *code = NULL;
71
72 pa_assert(io);
73 pa_assert(u);
74
75 if (events & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) {
76 pa_log("Lost connection to LIRC daemon.");
77 goto fail;
78 }
79
80 if (events & PA_IO_EVENT_INPUT) {
81 char *c;
82
83 if (lirc_nextcode(&code) != 0 || !code) {
84 pa_log("lirc_nextcode() failed.");
85 goto fail;
86 }
87
88 c = pa_xstrdup(code);
89 c[strcspn(c, "\n\r")] = 0;
90 pa_log_debug("Raw IR code '%s'", c);
91 pa_xfree(c);
92
93 while (lirc_code2char(u->config, code, &name) == 0 && name) {
94 enum {
95 INVALID,
96 UP,
97 DOWN,
98 MUTE,
99 RESET,
100 MUTE_TOGGLE
101 } volchange = INVALID;
102
103 pa_log_info("Translated IR code '%s'", name);
104
105 if (strcasecmp(name, "volume-up") == 0)
106 volchange = UP;
107 else if (strcasecmp(name, "volume-down") == 0)
108 volchange = DOWN;
109 else if (strcasecmp(name, "mute") == 0)
110 volchange = MUTE;
111 else if (strcasecmp(name, "mute-toggle") == 0)
112 volchange = MUTE_TOGGLE;
113 else if (strcasecmp(name, "reset") == 0)
114 volchange = RESET;
115
116 if (volchange == INVALID)
117 pa_log_warn("Received unknown IR code '%s'", name);
118 else {
119 pa_sink *s;
120
121 if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK)))
122 pa_log("Failed to get sink '%s'", u->sink_name);
123 else {
124 pa_cvolume cv = *pa_sink_get_volume(s, FALSE);
125
126 switch (volchange) {
127 case UP:
128 pa_cvolume_inc(&cv, DELTA);
129 pa_sink_set_volume(s, &cv, TRUE, TRUE);
130 break;
131
132 case DOWN:
133 pa_cvolume_dec(&cv, DELTA);
134 pa_sink_set_volume(s, &cv, TRUE, TRUE);
135 break;
136
137 case MUTE:
138 pa_sink_set_mute(s, TRUE, TRUE);
139 break;
140
141 case RESET:
142 pa_sink_set_mute(s, FALSE, TRUE);
143 break;
144
145 case MUTE_TOGGLE:
146 pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE), TRUE);
147 break;
148
149 case INVALID:
150 pa_assert_not_reached();
151 }
152 }
153 }
154 }
155 }
156
157 pa_xfree(code);
158
159 return;
160
161 fail:
162 u->module->core->mainloop->io_free(u->io);
163 u->io = NULL;
164
165 pa_module_unload_request(u->module, TRUE);
166
167 pa_xfree(code);
168 }
169
170 int pa__init(pa_module*m) {
171 pa_modargs *ma = NULL;
172 struct userdata *u;
173
174 pa_assert(m);
175
176 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
177 pa_log("Failed to parse module arguments");
178 goto fail;
179 }
180
181 m->userdata = u = pa_xnew(struct userdata, 1);
182 u->module = m;
183 u->io = NULL;
184 u->config = NULL;
185 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
186 u->lirc_fd = -1;
187 u->mute_toggle_save = 0;
188
189 if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "pulseaudio"), 1)) < 0) {
190 pa_log("lirc_init() failed.");
191 goto fail;
192 }
193
194 if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
195 pa_log("lirc_readconfig() failed.");
196 goto fail;
197 }
198
199 u->io = m->core->mainloop->io_new(m->core->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
200
201 pa_modargs_free(ma);
202
203 return 0;
204
205 fail:
206
207 if (ma)
208 pa_modargs_free(ma);
209
210 pa__done(m);
211 return -1;
212 }
213
214 void pa__done(pa_module*m) {
215 struct userdata *u;
216 pa_assert(m);
217
218 if (!(u = m->userdata))
219 return;
220
221 if (u->io)
222 m->core->mainloop->io_free(u->io);
223
224 if (u->config)
225 lirc_freeconfig(u->config);
226
227 if (u->lirc_fd >= 0)
228 lirc_deinit();
229
230 pa_xfree(u->sink_name);
231 pa_xfree(u);
232 }