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