]> code.delx.au - pulseaudio/blob - src/modules/x11/module-x11-cork-request.c
c1380c271f67936710368774407e3a5968872d64
[pulseaudio] / src / modules / x11 / module-x11-cork-request.c
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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <X11/Xlib.h>
32 #include <X11/extensions/XTest.h>
33 #include <X11/XF86keysym.h>
34 #include <X11/keysym.h>
35
36 #include <pulse/util.h>
37 #include <pulse/xmalloc.h>
38
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>
44
45 #include "module-x11-cork-request-symdef.h"
46
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>");
52
53 static const char* const valid_modargs[] = {
54 "display",
55 NULL
56 };
57
58 struct userdata {
59 pa_module *module;
60
61 pa_x11_wrapper *x11_wrapper;
62 pa_x11_client *x11_client;
63
64 pa_hook_slot *hook_slot;
65 };
66
67 static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) {
68 struct userdata *u = userdata;
69
70 pa_assert(w);
71 pa_assert(u);
72 pa_assert(u->x11_wrapper == w);
73
74 if (u->x11_client) {
75 pa_x11_client_free(u->x11_client);
76 u->x11_client = NULL;
77 }
78
79 if (u->x11_wrapper) {
80 pa_x11_wrapper_unref(u->x11_wrapper);
81 u->x11_wrapper = NULL;
82 }
83
84 pa_module_unload_request(u->module, TRUE);
85 }
86
87 static pa_hook_result_t sink_input_send_event_hook_cb(
88 pa_core *c,
89 pa_sink_input_send_event_hook_data *data,
90 struct userdata *u) {
91
92 KeySym sym;
93 KeyCode code;
94 Display *display;
95
96 pa_assert(c);
97 pa_assert(data);
98 pa_assert(u);
99
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;
104 else
105 return PA_HOOK_OK;
106
107 pa_log_debug("Triggering X11 keysym: %s", XKeysymToString(sym));
108
109 display = pa_x11_wrapper_get_display(u->x11_wrapper);
110 code = XKeysymToKeycode(display, sym);
111
112 XTestFakeKeyEvent(display, code, True, CurrentTime);
113 XSync(display, False);
114
115 XTestFakeKeyEvent(display, code, False, CurrentTime);
116 XSync(display, False);
117
118 return PA_HOOK_OK;
119 }
120
121 int pa__init(pa_module *m) {
122 struct userdata *u;
123 pa_modargs *ma;
124 int xtest_event_base, xtest_error_base;
125 int major_version, minor_version;
126
127 pa_assert(m);
128
129 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
130 pa_log("failed to parse module arguments");
131 goto fail;
132 }
133
134 m->userdata = u = pa_xnew0(struct userdata, 1);
135 u->module = m;
136
137 if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL))))
138 goto fail;
139
140 if (!XTestQueryExtension(
141 pa_x11_wrapper_get_display(u->x11_wrapper),
142 &xtest_event_base, &xtest_error_base,
143 &major_version, &minor_version)) {
144
145 pa_log("XTest extension not supported.");
146 goto fail;
147 }
148
149 pa_log_debug("XTest %i.%i supported.", major_version, minor_version);
150
151 u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u);
152
153 u->hook_slot = pa_hook_connect(
154 &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT],
155 PA_HOOK_NORMAL,
156 (pa_hook_cb_t) sink_input_send_event_hook_cb, u);
157
158 pa_modargs_free(ma);
159
160 return 0;
161
162 fail:
163 if (ma)
164 pa_modargs_free(ma);
165
166 pa__done(m);
167
168 return -1;
169 }
170
171 void pa__done(pa_module*m) {
172 struct userdata*u;
173
174 pa_assert(m);
175
176 if (!(u = m->userdata))
177 return;
178
179 if (u->x11_client)
180 pa_x11_client_free(u->x11_client);
181
182 if (u->x11_wrapper)
183 pa_x11_wrapper_unref(u->x11_wrapper);
184
185 if (u->hook_slot)
186 pa_hook_slot_free(u->hook_slot);
187
188 pa_xfree(u);
189 }