1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 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 <stdlib.h>
26 #include <unistd.h>
27
28 #include <X11/Xlib.h>
29 #include <X11/extensions/XTest.h>
30 #include <X11/XF86keysym.h>
31 #include <X11/keysym.h>
32
33 #include <pulse/xmalloc.h>
34
35 #include <pulsecore/module.h>
36 #include <pulsecore/modargs.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/x11wrap.h>
39 #include <pulsecore/core-util.h>
40
41 PA_MODULE_AUTHOR("Lennart Poettering");
42 PA_MODULE_DESCRIPTION("Synthesize X11 media key events when cork/uncork is requested");
43 PA_MODULE_VERSION(PACKAGE_VERSION);
44 PA_MODULE_LOAD_ONCE(false);
45 PA_MODULE_USAGE("display=<X11 display>");
46
47 static const char* const valid_modargs[] = {
48 "display",
49 "xauthority",
50 NULL
51 };
52
53 struct userdata {
54 pa_module *module;
55
56 pa_x11_wrapper *x11_wrapper;
57 pa_x11_client *x11_client;
58
59 pa_hook_slot *hook_slot;
60 };
61
x11_kill_cb(pa_x11_wrapper * w,void * userdata)62 static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
63 struct userdata *u = userdata;
64
65 pa_assert(w);
66 pa_assert(u);
67 pa_assert(u->x11_wrapper == w);
68
69 if (u->x11_client) {
70 pa_x11_client_free(u->x11_client);
71 u->x11_client = NULL;
72 }
73
74 if (u->x11_wrapper) {
75 pa_x11_wrapper_unref(u->x11_wrapper);
76 u->x11_wrapper = NULL;
77 }
78
79 pa_module_unload_request(u->module, true);
80 }
81
sink_input_send_event_hook_cb(pa_core * c,pa_sink_input_send_event_hook_data * data,struct userdata * u)82 static pa_hook_result_t sink_input_send_event_hook_cb(
83 pa_core *c,
84 pa_sink_input_send_event_hook_data *data,
85 struct userdata *u) {
86
87 KeySym sym;
88 KeyCode code;
89 Display *display;
90
91 pa_assert(c);
92 pa_assert(data);
93 pa_assert(u);
94
95 if (pa_streq(data->event, PA_STREAM_EVENT_REQUEST_CORK))
96 sym = XF86XK_AudioPause;
97 else if (pa_streq(data->event, PA_STREAM_EVENT_REQUEST_UNCORK))
98 sym = XF86XK_AudioPlay;
99 else
100 return PA_HOOK_OK;
101
102 pa_log_debug("Triggering X11 keysym: %s", XKeysymToString(sym));
103
104 display = pa_x11_wrapper_get_display(u->x11_wrapper);
105 code = XKeysymToKeycode(display, sym);
106
107 XTestFakeKeyEvent(display, code, True, CurrentTime);
108 XSync(display, False);
109
110 XTestFakeKeyEvent(display, code, False, CurrentTime);
111 XSync(display, False);
112
113 return PA_HOOK_OK;
114 }
115
pa__init(pa_module * m)116 int pa__init(pa_module *m) {
117 struct userdata *u;
118 pa_modargs *ma;
119 int xtest_event_base, xtest_error_base;
120 int major_version, minor_version;
121
122 pa_assert(m);
123
124 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
125 pa_log("failed to parse module arguments");
126 goto fail;
127 }
128
129 m->userdata = u = pa_xnew0(struct userdata, 1);
130 u->module = m;
131
132 if (pa_modargs_get_value(ma, "xauthority", NULL)) {
133 if (setenv("XAUTHORITY", pa_modargs_get_value(ma, "xauthority", NULL), 1)) {
134 pa_log("setenv() for $XAUTHORITY failed");
135 goto fail;
136 }
137 }
138
139 if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
140 goto fail;
141
142 if (!XTestQueryExtension(
143 pa_x11_wrapper_get_display(u->x11_wrapper),
144 &xtest_event_base, &xtest_error_base,
145 &major_version, &minor_version)) {
146
147 pa_log("XTest extension not supported.");
148 goto fail;
149 }
150
151 pa_log_debug("XTest %i.%i supported.", major_version, minor_version);
152
153 u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u);
154
155 u->hook_slot = pa_hook_connect(
156 &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT],
157 PA_HOOK_NORMAL,
158 (pa_hook_cb_t) sink_input_send_event_hook_cb, u);
159
160 pa_modargs_free(ma);
161
162 return 0;
163
164 fail:
165 if (ma)
166 pa_modargs_free(ma);
167
168 pa__done(m);
169
170 return -1;
171 }
172
pa__done(pa_module * m)173 void pa__done(pa_module*m) {
174 struct userdata*u;
175
176 pa_assert(m);
177
178 if (!(u = m->userdata))
179 return;
180
181 if (u->x11_client)
182 pa_x11_client_free(u->x11_client);
183
184 if (u->x11_wrapper)
185 pa_x11_wrapper_unref(u->x11_wrapper);
186
187 if (u->hook_slot)
188 pa_hook_slot_free(u->hook_slot);
189
190 pa_xfree(u);
191 }
192