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