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, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <lirc/lirc_client.h>
30
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/module.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/namereg.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/modargs.h>
38 #include <pulsecore/macro.h>
39
40 PA_MODULE_AUTHOR("Lennart Poettering");
41 PA_MODULE_DESCRIPTION("LIRC volume control");
42 PA_MODULE_VERSION(PACKAGE_VERSION);
43 PA_MODULE_LOAD_ONCE(true);
44 PA_MODULE_USAGE("config=<config file> sink=<sink name> appname=<lirc application name> volume_limit=<volume limit> volume_step=<volume change step>");
45
46 static const char* const valid_modargs[] = {
47 "config",
48 "sink",
49 "appname",
50 "volume_limit",
51 "volume_step",
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 pa_volume_t volume_limit;
63 pa_volume_t volume_step;
64 };
65
io_callback(pa_mainloop_api * io,pa_io_event * e,int fd,pa_io_event_flags_t events,void * userdata)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 pa_cvolume cv = *pa_sink_get_volume(s, false);
123
124 switch (volchange) {
125 case UP:
126 pa_cvolume_inc_clamp(&cv, u->volume_step, u->volume_limit);
127 pa_sink_set_volume(s, &cv, true, true);
128 break;
129
130 case DOWN:
131 pa_cvolume_dec(&cv, u->volume_step);
132 pa_sink_set_volume(s, &cv, true, true);
133 break;
134
135 case MUTE:
136 pa_sink_set_mute(s, true, true);
137 break;
138
139 case RESET:
140 pa_sink_set_mute(s, false, true);
141 break;
142
143 case MUTE_TOGGLE:
144 pa_sink_set_mute(s, !pa_sink_get_mute(s, false), true);
145 break;
146
147 case INVALID:
148 pa_assert_not_reached();
149 }
150 }
151 }
152 }
153 }
154
155 pa_xfree(code);
156
157 return;
158
159 fail:
160 u->module->core->mainloop->io_free(u->io);
161 u->io = NULL;
162
163 pa_module_unload_request(u->module, true);
164
165 pa_xfree(code);
166 }
167
pa__init(pa_module * m)168 int pa__init(pa_module*m) {
169 pa_modargs *ma = NULL;
170 struct userdata *u;
171 pa_volume_t volume_limit = PA_CLAMP_VOLUME(PA_VOLUME_NORM*3/2);
172 pa_volume_t volume_step = PA_VOLUME_NORM/20;
173
174 pa_assert(m);
175
176 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
177 pa_log("Failed to parse module arguments");
178 goto fail;
179 }
180
181 if (pa_modargs_get_value_u32(ma, "volume_limit", &volume_limit) < 0) {
182 pa_log("Failed to parse volume limit");
183 goto fail;
184 }
185
186 if (pa_modargs_get_value_u32(ma, "volume_step", &volume_step) < 0) {
187 pa_log("Failed to parse volume step");
188 goto fail;
189 }
190
191 m->userdata = u = pa_xnew(struct userdata, 1);
192 u->module = m;
193 u->io = NULL;
194 u->config = NULL;
195 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
196 u->lirc_fd = -1;
197 u->mute_toggle_save = 0;
198 u->volume_limit = PA_CLAMP_VOLUME(volume_limit);
199 u->volume_step = PA_CLAMP_VOLUME(volume_step);
200
201 if ((u->lirc_fd = lirc_init((char*) pa_modargs_get_value(ma, "appname", "pulseaudio"), 1)) < 0) {
202 pa_log("lirc_init() failed.");
203 goto fail;
204 }
205
206 if (lirc_readconfig((char*) pa_modargs_get_value(ma, "config", NULL), &u->config, NULL) < 0) {
207 pa_log("lirc_readconfig() failed.");
208 goto fail;
209 }
210
211 u->io = m->core->mainloop->io_new(m->core->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u);
212
213 pa_modargs_free(ma);
214
215 return 0;
216
217 fail:
218
219 if (ma)
220 pa_modargs_free(ma);
221
222 pa__done(m);
223 return -1;
224 }
225
pa__done(pa_module * m)226 void pa__done(pa_module*m) {
227 struct userdata *u;
228 pa_assert(m);
229
230 if (!(u = m->userdata))
231 return;
232
233 if (u->io)
234 m->core->mainloop->io_free(u->io);
235
236 if (u->config)
237 lirc_freeconfig(u->config);
238
239 if (u->lirc_fd >= 0)
240 lirc_deinit();
241
242 pa_xfree(u->sink_name);
243 pa_xfree(u);
244 }
245