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
32 #include <X11/extensions/XTest.h>
33 #include <X11/XF86keysym.h>
34 #include <X11/keysym.h>
36 #include <pulse/util.h>
37 #include <pulse/xmalloc.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/x11wrap.h>
43 #include <pulsecore/core-util.h>
45 #include "module-x11-cork-request-symdef.h"
47 PA_MODULE_AUTHOR("Lennart Poettering");
48 PA_MODULE_DESCRIPTION("Synthesize X11 media key events when cork/uncork is requested");
49 PA_MODULE_VERSION(PACKAGE_VERSION
);
50 PA_MODULE_LOAD_ONCE(FALSE
);
51 PA_MODULE_USAGE("display=<X11 display>");
53 static const char* const valid_modargs
[] = {
61 pa_x11_wrapper
*x11_wrapper
;
62 pa_x11_client
*x11_client
;
64 pa_hook_slot
*hook_slot
;
67 static void x11_kill_cb(pa_x11_wrapper
*w
, void *userdata
) {
68 struct userdata
*u
= userdata
;
72 pa_assert(u
->x11_wrapper
== w
);
75 pa_x11_client_free(u
->x11_client
);
80 pa_x11_wrapper_unref(u
->x11_wrapper
);
81 u
->x11_wrapper
= NULL
;
84 pa_module_unload_request(u
->module
, TRUE
);
87 static pa_hook_result_t
sink_input_send_event_hook_cb(
89 pa_sink_input_send_event_hook_data
*data
,
100 if (pa_streq(data
->event
, PA_STREAM_EVENT_REQUEST_CORK
))
101 sym
= XF86XK_AudioPause
;
102 else if (pa_streq(data
->event
, PA_STREAM_EVENT_REQUEST_UNCORK
))
103 sym
= XF86XK_AudioPlay
;
107 pa_log_debug("Triggering X11 keysym: %s", XKeysymToString(sym
));
109 display
= pa_x11_wrapper_get_display(u
->x11_wrapper
);
110 code
= XKeysymToKeycode(display
, sym
);
112 XTestFakeKeyEvent(display
, code
, True
, CurrentTime
);
113 XSync(display
, False
);
115 XTestFakeKeyEvent(display
, code
, False
, CurrentTime
);
116 XSync(display
, False
);
121 int pa__init(pa_module
*m
) {
124 int xtest_event_base
, xtest_error_base
;
125 int major_version
, minor_version
;
129 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
130 pa_log("failed to parse module arguments");
134 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
137 if (!(u
->x11_wrapper
= pa_x11_wrapper_get(m
->core
, pa_modargs_get_value(ma
, "display", NULL
))))
140 if (!XTestQueryExtension(
141 pa_x11_wrapper_get_display(u
->x11_wrapper
),
142 &xtest_event_base
, &xtest_error_base
,
143 &major_version
, &minor_version
)) {
145 pa_log("XTest extension not supported.");
149 pa_log_debug("XTest %i.%i supported.", major_version
, minor_version
);
151 u
->x11_client
= pa_x11_client_new(u
->x11_wrapper
, NULL
, x11_kill_cb
, u
);
153 u
->hook_slot
= pa_hook_connect(
154 &m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT
],
156 (pa_hook_cb_t
) sink_input_send_event_hook_cb
, u
);
171 void pa__done(pa_module
*m
) {
176 if (!(u
= m
->userdata
))
180 pa_x11_client_free(u
->x11_client
);
183 pa_x11_wrapper_unref(u
->x11_wrapper
);
186 pa_hook_slot_free(u
->hook_slot
);