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