]> code.delx.au - pulseaudio/blob - src/modules/module-lirc.c
alsa-mixer: Add surround 2.1 profile
[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> volume_limit=<volume limit> volume_step=<volume change step>");
49
50 static const char* const valid_modargs[] = {
51 "config",
52 "sink",
53 "appname",
54 "volume_limit",
55 "volume_step",
56 NULL,
57 };
58
59 struct userdata {
60 int lirc_fd;
61 pa_io_event *io;
62 struct lirc_config *config;
63 char *sink_name;
64 pa_module *module;
65 float mute_toggle_save;
66 pa_volume_t volume_limit;
67 pa_volume_t volume_step;
68 };
69
70 static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event_flags_t events, void*userdata) {
71 struct userdata *u = userdata;
72 char *name = NULL, *code = NULL;
73
74 pa_assert(io);
75 pa_assert(u);
76
77 if (events & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) {
78 pa_log("Lost connection to LIRC daemon.");
79 goto fail;
80 }
81
82 if (events & PA_IO_EVENT_INPUT) {
83 char *c;
84
85 if (lirc_nextcode(&code) != 0 || !code) {
86 pa_log("lirc_nextcode() failed.");
87 goto fail;
88 }
89
90 c = pa_xstrdup(code);
91 c[strcspn(c, "\n\r")] = 0;
92 pa_log_debug("Raw IR code '%s'", c);
93 pa_xfree(c);
94
95 while (lirc_code2char(u->config, code, &name) == 0 && name) {
96 enum {
97 INVALID,
98 UP,
99 DOWN,
100 MUTE,
101 RESET,
102 MUTE_TOGGLE
103 } volchange = INVALID;
104
105 pa_log_info("Translated IR code '%s'", name);
106
107 if (strcasecmp(name, "volume-up") == 0)
108 volchange = UP;
109 else if (strcasecmp(name, "volume-down") == 0)
110 volchange = DOWN;
111 else if (strcasecmp(name, "mute") == 0)
112 volchange = MUTE;
113 else if (strcasecmp(name, "mute-toggle") == 0)
114 volchange = MUTE_TOGGLE;
115 else if (strcasecmp(name, "reset") == 0)
116 volchange = RESET;
117
118 if (volchange == INVALID)
119 pa_log_warn("Received unknown IR code '%s'", name);
120 else {
121 pa_sink *s;
122
123 if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK)))
124 pa_log("Failed to get sink '%s'", u->sink_name);
125 else {
126 pa_cvolume cv = *pa_sink_get_volume(s, false);
127
128 switch (volchange) {
129 case UP:
130 pa_cvolume_inc_clamp(&cv, u->volume_step, u->volume_limit);
131 pa_sink_set_volume(s, &cv, true, true);
132 break;
133
134 case DOWN:
135 pa_cvolume_dec(&cv, u->volume_step);
136 pa_sink_set_volume(s, &cv, true, true);
137 break;
138
139 case MUTE:
140 pa_sink_set_mute(s, true, true);
141 break;
142
143 case RESET:
144 pa_sink_set_mute(s, false, true);
145 break;
146
147 case MUTE_TOGGLE:
148 pa_sink_set_mute(s, !pa_sink_get_mute(s, false), true);
149 break;
150
151 case INVALID:
152 pa_assert_not_reached();
153 }
154 }
155 }
156 }
157 }
158
159 pa_xfree(code);
160
161 return;
162
163 fail:
164 u->module->core->mainloop->io_free(u->io);
165 u->io = NULL;
166
167 pa_module_unload_request(u->module, true);
168
169 pa_xfree(code);
170 }
171
172 int pa__init(pa_module*m) {
173 pa_modargs *ma = NULL;
174 struct userdata *u;
175 pa_volume_t volume_limit = PA_CLAMP_VOLUME(PA_VOLUME_NORM*3/2);
176 pa_volume_t volume_step = PA_VOLUME_NORM/20;
177
178 pa_assert(m);
179
180 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
181 pa_log("Failed to parse module arguments");
182 goto fail;
183 }
184
185 if (pa_modargs_get_value_u32(ma, "volume_limit", &volume_limit) < 0) {
186 pa_log("Failed to parse volume limit");
187 goto fail;
188 }
189
190 if (pa_modargs_get_value_u32(ma, "volume_step", &volume_step) < 0) {
191 pa_log("Failed to parse volume step");
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 u->volume_limit = PA_CLAMP_VOLUME(volume_limit);
203 u->volume_step = PA_CLAMP_VOLUME(volume_step);
204
205 if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "pulseaudio"), 1)) < 0) {
206 pa_log("lirc_init() failed.");
207 goto fail;
208 }
209
210 if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
211 pa_log("lirc_readconfig() failed.");
212 goto fail;
213 }
214
215 u->io = m->core->mainloop->io_new(m->core->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
216
217 pa_modargs_free(ma);
218
219 return 0;
220
221 fail:
222
223 if (ma)
224 pa_modargs_free(ma);
225
226 pa__done(m);
227 return -1;
228 }
229
230 void pa__done(pa_module*m) {
231 struct userdata *u;
232 pa_assert(m);
233
234 if (!(u = m->userdata))
235 return;
236
237 if (u->io)
238 m->core->mainloop->io_free(u->io);
239
240 if (u->config)
241 lirc_freeconfig(u->config);
242
243 if (u->lirc_fd >= 0)
244 lirc_deinit();
245
246 pa_xfree(u->sink_name);
247 pa_xfree(u);
248 }