1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/* PipeWire
*
* Copyright © 2022 Wim Taymans <wim.taymans@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "config.h"
#include <spa/utils/string.h>
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#include <X11/XKBlib.h>
#ifdef HAVE_XFIXES_6
#include <X11/extensions/Xfixes.h>
#endif
#include <canberra.h>
#include <pipewire/pipewire.h>
#include <pipewire/impl.h>
/** \page page_module_x11_bell PipeWire Module: X11 Bell
*
* The `x11-bell` module intercept the X11 bell events and uses libcanberra to
* play a sound.
*
* ## Module Options
*
* - `sink.name = <str>`: node.name of the sink to connect to
* - `sample.name = <str>`: the name of the sample to play, default 'bell-window-system'
* - `x11.display = <str>`: the X11 display to use
* - `x11.xauthority = <str>`: the X11 XAuthority string placed in XAUTHORITY env
*
* ## General options
*
* There are no general options for this module.
*
* ## Example configuration
*\code{.unparsed}
* context.modules = [
* { name = libpipewire-x11-bell }
* args = {
* #sink.name = @DEFAULT_SINK@
* sample.name = "bell-window-system"
* #x11.display = ":1"
* #x11.xauthority = "test"
* ]
*\endcode
*
*/
#define NAME "x11-bell"
PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
#define PW_LOG_TOPIC_DEFAULT mod_topic
struct impl {
struct pw_context *context;
struct pw_thread_loop *thread_loop;
struct pw_loop *thread_loop_loop;
struct pw_loop *loop;
struct spa_source *source;
struct pw_properties *properties;
struct pw_impl_module *module;
struct spa_hook module_listener;
Display *display;
};
static int play_sample(struct impl *impl)
{
const char *sample = NULL;
ca_context *ca;
int res;
if (impl->properties)
sample = pw_properties_get(impl->properties, "sample.name");
if (sample == NULL)
sample = "bell-window-system";
pw_log_info("play sample %s", sample);
if ((res = ca_context_create(&ca)) < 0) {
pw_log_error("canberra context create error: %s", ca_strerror(res));
res = -EIO;
goto exit;
}
if ((res = ca_context_open(ca)) < 0) {
pw_log_error("canberra context open error: %s", ca_strerror(res));
res = -EIO;
goto exit_destroy;
}
if ((res = ca_context_play(ca, 0,
CA_PROP_EVENT_ID, sample,
CA_PROP_MEDIA_NAME, "X11 bell event",
CA_PROP_CANBERRA_CACHE_CONTROL, "permanent",
NULL)) < 0) {
pw_log_warn("can't play sample (%s): %s", sample, ca_strerror(res));
res = -EIO;
goto exit_destroy;
}
exit_destroy:
ca_context_destroy(ca);
exit:
return res;
}
static int do_play_sample(struct spa_loop *loop, bool async, uint32_t seq, const void *data, size_t size, void *user_data)
{
play_sample(user_data);
return 0;
}
static void display_io(void *data, int fd, uint32_t mask)
{
struct impl *impl = data;
XEvent e;
while (XPending(impl->display)) {
XNextEvent(impl->display, &e);
if (((XkbEvent*) &e)->any.xkb_type != XkbBellNotify)
continue;
pw_loop_invoke(impl->thread_loop_loop, do_play_sample, 0, NULL, 0, false, impl);
}
}
#ifdef HAVE_XSETIOERROREXITHANDLER
static void x11_io_error_exit_handler(Display *display, void *data)
{
struct impl *impl = data;
spa_assert(display == impl->display);
pw_log_warn("X11 display (%s) has encountered a fatal I/O error", DisplayString(display));
pw_loop_destroy_source(impl->loop, impl->source);
impl->source = NULL;
pw_impl_module_schedule_destroy(impl->module);
}
#endif
static int x11_connect(struct impl *impl, const char *name)
{
int major, minor;
unsigned int auto_ctrls, auto_values;
if (!(impl->display = XOpenDisplay(name))) {
pw_log_error("XOpenDisplay() failed");
return -EIO;
}
impl->source = pw_loop_add_io(impl->loop,
ConnectionNumber(impl->display),
SPA_IO_IN, false, display_io, impl);
if (!impl->source)
return -errno;
#ifdef HAVE_XSETIOERROREXITHANDLER
XSetIOErrorExitHandler(impl->display, x11_io_error_exit_handler, impl);
#endif
#ifdef HAVE_XFIXES_6
XFixesSetClientDisconnectMode(impl->display, XFixesClientDisconnectFlagTerminate);
#endif
major = XkbMajorVersion;
minor = XkbMinorVersion;
if (!XkbLibraryVersion(&major, &minor)) {
pw_log_error("XkbLibraryVersion() failed");
return -EIO;
}
major = XkbMajorVersion;
minor = XkbMinorVersion;
if (!XkbQueryExtension(impl->display, NULL, NULL, NULL, &major, &minor)) {
pw_log_error("XkbQueryExtension() failed");
return -EIO;
}
XkbSelectEvents(impl->display, XkbUseCoreKbd, XkbBellNotifyMask, XkbBellNotifyMask);
auto_ctrls = auto_values = XkbAudibleBellMask;
XkbSetAutoResetControls(impl->display, XkbAudibleBellMask, &auto_ctrls, &auto_values);
XkbChangeEnabledControls(impl->display, XkbUseCoreKbd, XkbAudibleBellMask, 0);
return 0;
}
static void module_destroy(void *data)
{
struct impl *impl = data;
if (impl->module)
spa_hook_remove(&impl->module_listener);
if (impl->source)
pw_loop_destroy_source(impl->loop, impl->source);
if (impl->display)
XCloseDisplay(impl->display);
if (impl->thread_loop)
pw_thread_loop_destroy(impl->thread_loop);
pw_properties_free(impl->properties);
free(impl);
}
static const struct pw_impl_module_events module_events = {
PW_VERSION_IMPL_MODULE_EVENTS,
.destroy = module_destroy,
};
static const struct spa_dict_item module_x11_bell_info[] = {
{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
{ PW_KEY_MODULE_DESCRIPTION, "X11 Bell interceptor" },
{ PW_KEY_MODULE_USAGE, "sink.name=<name for the sink> "
"sample.name=<the sample name> "
"x11.display=<the X11 display> "
"x11.xauthority=<the X11 XAuthority> " },
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
};
SPA_EXPORT
int pipewire__module_init(struct pw_impl_module *module, const char *args)
{
struct pw_context *context = pw_impl_module_get_context(module);
struct impl *impl;
const char *name = NULL, *str;
int res;
PW_LOG_TOPIC_INIT(mod_topic);
impl = calloc(1, sizeof(struct impl));
if (impl == NULL)
return -ENOMEM;
pw_log_debug("module %p: new", impl);
impl->context = context;
impl->loop = pw_context_get_main_loop(context);
impl->thread_loop = pw_thread_loop_new("X11 Bell", NULL);
if (impl->thread_loop == NULL) {
res = -errno;
pw_log_error("can't create thread loop: %m");
goto error;
}
impl->thread_loop_loop = pw_thread_loop_get_loop(impl->thread_loop);
impl->properties = args ? pw_properties_new_string(args) : NULL;
impl->module = module;
pw_impl_module_add_listener(module, &impl->module_listener, &module_events, impl);
pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_x11_bell_info));
if (impl->properties) {
if ((str = pw_properties_get(impl->properties, "x11.xauthority")) != NULL) {
if (setenv("XAUTHORITY", str, 1)) {
res = -errno;
pw_log_error("XAUTHORITY setenv failed: %m");
goto error;
}
}
name = pw_properties_get(impl->properties, "x11.display");
}
/* we need to use a thread loop because this module will connect
* to pipewire eventually and will then block the mainloop. */
pw_thread_loop_start(impl->thread_loop);
res = x11_connect(impl, name);
if (res < 0)
goto error;
return 0;
error:
module_destroy(impl);
return res;
}
static int x11_error_handler(Display *display, XErrorEvent *error)
{
pw_log_warn("X11 error handler called on display %s with error %d",
DisplayString(display), error->error_code);
return 0;
}
static int x11_io_error_handler(Display *display)
{
pw_log_warn("X11 I/O error handler called on display %s", DisplayString(display));
return 0;
}
__attribute__((constructor))
static void set_x11_handlers(void)
{
{
XErrorHandler prev = XSetErrorHandler(NULL);
XErrorHandler def = XSetErrorHandler(x11_error_handler);
if (prev != def)
XSetErrorHandler(prev);
}
{
XIOErrorHandler prev = XSetIOErrorHandler(NULL);
XIOErrorHandler def = XSetIOErrorHandler(x11_io_error_handler);
if (prev != def)
XSetIOErrorHandler(prev);
}
}
__attribute__((destructor))
static void restore_x11_handlers(void)
{
{
XErrorHandler prev = XSetErrorHandler(NULL);
if (prev != x11_error_handler)
XSetErrorHandler(prev);
}
{
XIOErrorHandler prev = XSetIOErrorHandler(NULL);
if (prev != x11_io_error_handler)
XSetIOErrorHandler(prev);
}
}
|