2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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.
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.
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
31 #include <X11/extensions/XTest.h>
32 #include <X11/XF86keysym.h>
33 #include <X11/keysym.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/modargs.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/x11wrap.h>
41 #include <pulsecore/core-util.h>
43 #include "module-x11-cork-request-symdef.h"
45 PA_MODULE_AUTHOR("Lennart Poettering");
46 PA_MODULE_DESCRIPTION("Synthesize X11 media key events when cork/uncork is requested");
47 PA_MODULE_VERSION(PACKAGE_VERSION
);
48 PA_MODULE_LOAD_ONCE(false);
49 PA_MODULE_USAGE("display=<X11 display>");
51 static const char* const valid_modargs
[] = {
59 pa_x11_wrapper
*x11_wrapper
;
60 pa_x11_client
*x11_client
;
62 pa_hook_slot
*hook_slot
;
65 static void x11_kill_cb(pa_x11_wrapper
*w
, void *userdata
) {
66 struct userdata
*u
= userdata
;
70 pa_assert(u
->x11_wrapper
== w
);
73 pa_x11_client_free(u
->x11_client
);
78 pa_x11_wrapper_unref(u
->x11_wrapper
);
79 u
->x11_wrapper
= NULL
;
82 pa_module_unload_request(u
->module
, true);
85 static pa_hook_result_t
sink_input_send_event_hook_cb(
87 pa_sink_input_send_event_hook_data
*data
,
98 if (pa_streq(data
->event
, PA_STREAM_EVENT_REQUEST_CORK
))
99 sym
= XF86XK_AudioPause
;
100 else if (pa_streq(data
->event
, PA_STREAM_EVENT_REQUEST_UNCORK
))
101 sym
= XF86XK_AudioPlay
;
105 pa_log_debug("Triggering X11 keysym: %s", XKeysymToString(sym
));
107 display
= pa_x11_wrapper_get_display(u
->x11_wrapper
);
108 code
= XKeysymToKeycode(display
, sym
);
110 XTestFakeKeyEvent(display
, code
, True
, CurrentTime
);
111 XSync(display
, False
);
113 XTestFakeKeyEvent(display
, code
, False
, CurrentTime
);
114 XSync(display
, False
);
119 int pa__init(pa_module
*m
) {
122 int xtest_event_base
, xtest_error_base
;
123 int major_version
, minor_version
;
127 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
128 pa_log("failed to parse module arguments");
132 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
135 if (!(u
->x11_wrapper
= pa_x11_wrapper_get(m
->core
, pa_modargs_get_value(ma
, "display", NULL
))))
138 if (!XTestQueryExtension(
139 pa_x11_wrapper_get_display(u
->x11_wrapper
),
140 &xtest_event_base
, &xtest_error_base
,
141 &major_version
, &minor_version
)) {
143 pa_log("XTest extension not supported.");
147 pa_log_debug("XTest %i.%i supported.", major_version
, minor_version
);
149 u
->x11_client
= pa_x11_client_new(u
->x11_wrapper
, NULL
, x11_kill_cb
, u
);
151 u
->hook_slot
= pa_hook_connect(
152 &m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT
],
154 (pa_hook_cb_t
) sink_input_send_event_hook_cb
, u
);
169 void pa__done(pa_module
*m
) {
174 if (!(u
= m
->userdata
))
178 pa_x11_client_free(u
->x11_client
);
181 pa_x11_wrapper_unref(u
->x11_wrapper
);
184 pa_hook_slot_free(u
->hook_slot
);