Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / 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 <assert.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <lirc/lirc_client.h>
33 #include <stdlib.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
43 #include "module-lirc-symdef.h"
44
45 PA_MODULE_AUTHOR("Lennart Poettering")
46 PA_MODULE_DESCRIPTION("LIRC volume control")
47 PA_MODULE_VERSION(PACKAGE_VERSION)
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 int lirc_in_use = 0;
67
68 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) {
69     struct userdata *u = userdata;
70     char *name = NULL, *code = NULL;
71     assert(io);
72     assert(u);
73
74     if (events & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) {
75         pa_log("lost connection to LIRC daemon.");
76         goto fail;
77     }
78
79     if (events & PA_IO_EVENT_INPUT) {
80         char *c;
81
82         if (lirc_nextcode(&code) != 0 || !code) {
83             pa_log("lirc_nextcode() failed.");
84             goto fail;
85         }
86
87         c = pa_xstrdup(code);
88         c[strcspn(c, "\n\r")] = 0;
89         pa_log_debug("raw IR code '%s'", c);
90         pa_xfree(c);
91
92         while (lirc_code2char(u->config, code, &name) == 0 && name) {
93             enum {
94                 INVALID,
95                 UP,
96                 DOWN,
97                 MUTE,
98                 RESET,
99                 MUTE_TOGGLE
100             } volchange = INVALID;
101
102             pa_log_info("translated IR code '%s'", name);
103
104             if (strcasecmp(name, "volume-up") == 0)
105                 volchange = UP;
106             else if (strcasecmp(name, "volume-down") == 0)
107                 volchange = DOWN;
108             else if (strcasecmp(name, "mute") == 0)
109                 volchange = MUTE;
110             else if (strcasecmp(name, "mute-toggle") == 0)
111                 volchange = MUTE_TOGGLE;
112             else if (strcasecmp(name, "reset") == 0)
113                 volchange = RESET;
114
115             if (volchange == INVALID)
116                 pa_log_warn("recieved unknown IR code '%s'", name);
117             else {
118                 pa_sink *s;
119
120                 if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK, 1)))
121                     pa_log("failed to get sink '%s'", u->sink_name);
122                 else {
123                     int i;
124                     pa_cvolume cv = *pa_sink_get_volume(s, PA_MIXER_HARDWARE);
125
126 #define DELTA (PA_VOLUME_NORM/20)
127
128                     switch (volchange) {
129                         case UP:
130                             for (i = 0; i < cv.channels; i++) {
131                                 cv.values[i] += DELTA;
132
133                                 if (cv.values[i] > PA_VOLUME_NORM)
134                                     cv.values[i] = PA_VOLUME_NORM;
135                             }
136
137                             pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
138                             break;
139
140                         case DOWN:
141                             for (i = 0; i < cv.channels; i++) {
142                                 if (cv.values[i] >= DELTA)
143                                     cv.values[i] -= DELTA;
144                                 else
145                                     cv.values[i] = PA_VOLUME_MUTED;
146                             }
147
148                             pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
149                             break;
150
151                         case MUTE:
152                             pa_sink_set_mute(s, PA_MIXER_HARDWARE, 0);
153                             break;
154
155                         case RESET:
156                             pa_sink_set_mute(s, PA_MIXER_HARDWARE, 1);
157                             break;
158
159                         case MUTE_TOGGLE:
160
161                             pa_sink_set_mute(s, PA_MIXER_HARDWARE, !pa_sink_get_mute(s, PA_MIXER_HARDWARE));
162                             break;
163
164                         case INVALID:
165                             ;
166                     }
167                 }
168             }
169         }
170     }
171
172     pa_xfree(code);
173
174     return;
175
176 fail:
177     u->module->core->mainloop->io_free(u->io);
178     u->io = NULL;
179
180     pa_module_unload_request(u->module);
181
182     free(code);
183 }
184
185 int pa__init(pa_core *c, pa_module*m) {
186     pa_modargs *ma = NULL;
187     struct userdata *u;
188     assert(c && m);
189
190     if (lirc_in_use) {
191         pa_log("module-lirc may no be loaded twice.");
192         return -1;
193     }
194
195     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
196         pa_log("Failed to parse module arguments");
197         goto fail;
198     }
199
200     m->userdata = u = pa_xmalloc(sizeof(struct userdata));
201     u->module = m;
202     u->io = NULL;
203     u->config = NULL;
204     u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
205     u->lirc_fd = -1;
206     u->mute_toggle_save = 0;
207
208     if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "pulseaudio"), 1)) < 0) {
209         pa_log("lirc_init() failed.");
210         goto fail;
211     }
212
213     if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
214         pa_log("lirc_readconfig() failed.");
215         goto fail;
216     }
217
218     u->io = c->mainloop->io_new(c->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
219
220     lirc_in_use = 1;
221
222     pa_modargs_free(ma);
223
224     return 0;
225
226 fail:
227
228     if (ma)
229         pa_modargs_free(ma);
230
231     pa__done(c, m);
232     return -1;
233 }
234
235 void pa__done(pa_core *c, pa_module*m) {
236     struct userdata *u;
237     assert(c);
238     assert(m);
239
240     if (!(u = m->userdata))
241         return;
242
243     if (u->io)
244         m->core->mainloop->io_free(u->io);
245
246     if (u->config)
247         lirc_freeconfig(u->config);
248
249     if (u->lirc_fd >= 0)
250         lirc_deinit();
251
252     pa_xfree(u->sink_name);
253     pa_xfree(u);
254
255     lirc_in_use = 0;
256 }