]> code.delx.au - pulseaudio/blob - src/modules/module-lirc.c
918177a307ea3c15436d055013c688e5b4a7ae6d
[pulseaudio] / src / modules / module-lirc.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 polypaudio 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 polypaudio; 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 <polyp/xmalloc.h>
34
35 #include <polypcore/module.h>
36 #include <polypcore/log.h>
37 #include <polypcore/namereg.h>
38 #include <polypcore/sink.h>
39 #include <polypcore/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 { INVALID, UP, DOWN, MUTE, RESET, MUTE_TOGGLE } volchange = INVALID;
92
93 pa_log_info(__FILE__": translated IR code '%s'", name);
94
95 if (strcasecmp(name, "volume-up") == 0)
96 volchange = UP;
97 else if (strcasecmp(name, "volume-down") == 0)
98 volchange = DOWN;
99 else if (strcasecmp(name, "mute") == 0)
100 volchange = MUTE;
101 else if (strcasecmp(name, "mute-toggle") == 0)
102 volchange = MUTE_TOGGLE;
103 else if (strcasecmp(name, "reset") == 0)
104 volchange = RESET;
105
106 if (volchange == INVALID)
107 pa_log_warn(__FILE__": recieved unknown IR code '%s'", name);
108 else {
109 pa_sink *s;
110
111 if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK, 1)))
112 pa_log(__FILE__": failed to get sink '%s'", u->sink_name);
113 else {
114 pa_volume_t v = pa_cvolume_avg(pa_sink_get_volume(s, PA_MIXER_HARDWARE));
115 pa_cvolume cv;
116 #define DELTA (PA_VOLUME_NORM/20)
117
118 switch (volchange) {
119 case UP:
120 v += PA_VOLUME_NORM/20;
121 break;
122
123 case DOWN:
124 if (v > DELTA)
125 v -= DELTA;
126 else
127 v = PA_VOLUME_MUTED;
128
129 break;
130
131 case MUTE:
132 v = PA_VOLUME_MUTED;
133 break;
134
135 case RESET:
136 v = PA_VOLUME_NORM;
137 break;
138
139 case MUTE_TOGGLE: {
140
141 if (v > 0) {
142 u->mute_toggle_save = v;
143 v = PA_VOLUME_MUTED;
144 } else
145 v = u->mute_toggle_save;
146 }
147 default:
148 ;
149 }
150
151 pa_cvolume_set(&cv, PA_CHANNELS_MAX, v);
152 pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
153 }
154 }
155 }
156 }
157
158 free(code);
159
160 return;
161
162 fail:
163 u->module->core->mainloop->io_free(u->io);
164 u->io = NULL;
165
166 pa_module_unload_request(u->module);
167
168 free(code);
169 }
170
171 int pa__init(pa_core *c, pa_module*m) {
172 pa_modargs *ma = NULL;
173 struct userdata *u;
174 assert(c && m);
175
176 if (lirc_in_use) {
177 pa_log(__FILE__": module-lirc may no be loaded twice.");
178 return -1;
179 }
180
181 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
182 pa_log(__FILE__": Failed to parse module arguments");
183 goto fail;
184 }
185
186 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
187 u->module = m;
188 u->io = NULL;
189 u->config = NULL;
190 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
191 u->lirc_fd = -1;
192 u->mute_toggle_save = 0;
193
194 if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "polypaudio"), 1)) < 0) {
195 pa_log(__FILE__": lirc_init() failed.");
196 goto fail;
197 }
198
199 if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
200 pa_log(__FILE__": lirc_readconfig() failed.");
201 goto fail;
202 }
203
204 u->io = c->mainloop->io_new(c->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
205
206 lirc_in_use = 1;
207
208 pa_modargs_free(ma);
209
210 return 0;
211
212 fail:
213
214 if (ma)
215 pa_modargs_free(ma);
216
217 pa__done(c, m);
218 return -1;
219 }
220
221 void pa__done(pa_core *c, pa_module*m) {
222 struct userdata *u;
223 assert(c);
224 assert(m);
225
226 if (!(u = m->userdata))
227 return;
228
229 if (u->io)
230 m->core->mainloop->io_free(u->io);
231
232 if (u->config)
233 lirc_freeconfig(u->config);
234
235 if (u->lirc_fd >= 0)
236 lirc_deinit();
237
238 pa_xfree(u->sink_name);
239 pa_xfree(u);
240
241 lirc_in_use = 0;
242 }