]> code.delx.au - pulseaudio/blob - src/modules/module-lirc.c
IGAIN is a better choice than IMIX for source volume.
[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 <polypcore/module.h>
34 #include <polypcore/log.h>
35 #include <polypcore/namereg.h>
36 #include <polypcore/sink.h>
37 #include <polypcore/xmalloc.h>
38 #include <polypcore/modargs.h>
39
40 #include "module-lirc-symdef.h"
41
42 PA_MODULE_AUTHOR("Lennart Poettering")
43 PA_MODULE_DESCRIPTION("LIRC volume control")
44 PA_MODULE_VERSION(PACKAGE_VERSION)
45 PA_MODULE_USAGE("config=<config file> sink=<sink name> appname=<lirc application name>")
46
47 static const char* const valid_modargs[] = {
48 "config",
49 "sink",
50 "appname",
51 NULL,
52 };
53
54 struct userdata {
55 int lirc_fd;
56 pa_io_event *io;
57 struct lirc_config *config;
58 char *sink_name;
59 pa_module *module;
60 float mute_toggle_save;
61 };
62
63 static int lirc_in_use = 0;
64
65 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) {
66 struct userdata *u = userdata;
67 char *name = NULL, *code = NULL;
68 assert(io);
69 assert(u);
70
71 if (events & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) {
72 pa_log(__FILE__": lost connection to LIRC daemon.");
73 goto fail;
74 }
75
76 if (events & PA_IO_EVENT_INPUT) {
77 char *c;
78
79 if (lirc_nextcode(&code) != 0 || !code) {
80 pa_log(__FILE__": lirc_nextcode() failed.");
81 goto fail;
82 }
83
84 c = pa_xstrdup(code);
85 c[strcspn(c, "\n\r")] = 0;
86 pa_log_debug(__FILE__": raw IR code '%s'", c);
87 pa_xfree(c);
88
89 while (lirc_code2char(u->config, code, &name) == 0 && name) {
90 enum { INVALID, UP, DOWN, MUTE, RESET, MUTE_TOGGLE } volchange = INVALID;
91
92 pa_log_info(__FILE__": translated IR code '%s'", name);
93
94 if (strcasecmp(name, "volume-up") == 0)
95 volchange = UP;
96 else if (strcasecmp(name, "volume-down") == 0)
97 volchange = DOWN;
98 else if (strcasecmp(name, "mute") == 0)
99 volchange = MUTE;
100 else if (strcasecmp(name, "mute-toggle") == 0)
101 volchange = MUTE_TOGGLE;
102 else if (strcasecmp(name, "reset") == 0)
103 volchange = RESET;
104
105 if (volchange == INVALID)
106 pa_log_warn(__FILE__": recieved unknown IR code '%s'", name);
107 else {
108 pa_sink *s;
109
110 if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK, 1)))
111 pa_log(__FILE__": failed to get sink '%s'", u->sink_name);
112 else {
113 pa_volume_t v = pa_cvolume_avg(pa_sink_get_volume(s, PA_MIXER_HARDWARE));
114 pa_cvolume cv;
115 #define DELTA (PA_VOLUME_NORM/20)
116
117 switch (volchange) {
118 case UP:
119 v += PA_VOLUME_NORM/20;
120 break;
121
122 case DOWN:
123 if (v > DELTA)
124 v -= DELTA;
125 else
126 v = PA_VOLUME_MUTED;
127
128 break;
129
130 case MUTE:
131 v = PA_VOLUME_MUTED;
132 break;
133
134 case RESET:
135 v = PA_VOLUME_NORM;
136 break;
137
138 case MUTE_TOGGLE: {
139
140 if (v > 0) {
141 u->mute_toggle_save = v;
142 v = PA_VOLUME_MUTED;
143 } else
144 v = u->mute_toggle_save;
145 }
146 default:
147 ;
148 }
149
150 pa_cvolume_set(&cv, PA_CHANNELS_MAX, v);
151 pa_sink_set_volume(s, PA_MIXER_HARDWARE, &cv);
152 }
153 }
154 }
155 }
156
157 free(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);
166
167 free(code);
168 }
169
170 int pa__init(pa_core *c, pa_module*m) {
171 pa_modargs *ma = NULL;
172 struct userdata *u;
173 assert(c && m);
174
175 if (lirc_in_use) {
176 pa_log(__FILE__": module-lirc may no be loaded twice.");
177 return -1;
178 }
179
180 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
181 pa_log(__FILE__": Failed to parse module arguments");
182 goto fail;
183 }
184
185 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
186 u->module = m;
187 u->io = NULL;
188 u->config = NULL;
189 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
190 u->lirc_fd = -1;
191 u->mute_toggle_save = 0;
192
193 if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "polypaudio"), 1)) < 0) {
194 pa_log(__FILE__": lirc_init() failed.");
195 goto fail;
196 }
197
198 if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
199 pa_log(__FILE__": lirc_readconfig() failed.");
200 goto fail;
201 }
202
203 u->io = c->mainloop->io_new(c->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
204
205 lirc_in_use = 1;
206
207 pa_modargs_free(ma);
208
209 return 0;
210
211 fail:
212
213 if (ma)
214 pa_modargs_free(ma);
215
216 pa__done(c, m);
217 return -1;
218 }
219
220 void pa__done(pa_core *c, pa_module*m) {
221 struct userdata *u;
222 assert(c);
223 assert(m);
224
225 if (!(u = m->userdata))
226 return;
227
228 if (u->io)
229 m->core->mainloop->io_free(u->io);
230
231 if (u->config)
232 lirc_freeconfig(u->config);
233
234 if (u->lirc_fd >= 0)
235 lirc_deinit();
236
237 pa_xfree(u->sink_name);
238 pa_xfree(u);
239
240 lirc_in_use = 0;
241 }