2 This file is part of PulseAudio.
4 Copyright 2004-2006 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, see <http://www.gnu.org/licenses/>.
26 #include <pulse/xmalloc.h>
28 #include <pulsecore/llist.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/shared.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/macro.h>
38 typedef struct pa_x11_internal pa_x11_internal;
40 struct pa_x11_internal {
41 PA_LLIST_FIELDS(pa_x11_internal);
42 pa_x11_wrapper *wrapper;
43 pa_io_event* io_event;
47 struct pa_x11_wrapper {
54 pa_defer_event* defer_event;
55 pa_io_event* io_event;
56 pa_defer_event* cleanup_event;
58 PA_LLIST_HEAD(pa_x11_client, clients);
59 PA_LLIST_HEAD(pa_x11_internal, internals);
62 struct pa_x11_client {
63 PA_LLIST_FIELDS(pa_x11_client);
64 pa_x11_wrapper *wrapper;
65 pa_x11_event_cb_t event_cb;
66 pa_x11_kill_cb_t kill_cb;
70 static void x11_wrapper_kill(pa_x11_wrapper *w);
72 /* Dispatch all pending X11 events */
73 static void work(pa_x11_wrapper *w) {
75 pa_assert(PA_REFCNT_VALUE(w) >= 1);
77 pa_x11_wrapper_ref(w);
79 while (XPending(w->display)) {
82 XNextEvent(w->display, &e);
84 for (c = w->clients; c; c = n) {
88 if (c->event_cb(w, &e, c->userdata) != 0)
93 pa_x11_wrapper_unref(w);
96 /* IO notification event for the X11 display connection */
97 static void display_io_event(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
98 pa_x11_wrapper *w = userdata;
104 pa_assert(PA_REFCNT_VALUE(w) >= 1);
109 /* Deferred notification event. Called once each main loop iteration */
110 static void defer_event(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
111 pa_x11_wrapper *w = userdata;
116 pa_assert(PA_REFCNT_VALUE(w) >= 1);
118 m->defer_enable(e, 0);
123 /* IO notification event for X11 internal connections */
124 static void internal_io_event(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
125 pa_x11_wrapper *w = userdata;
131 pa_assert(PA_REFCNT_VALUE(w) >= 1);
133 XProcessInternalConnection(w->display, fd);
138 /* Add a new IO source for the specified X11 internal connection */
139 static pa_x11_internal* x11_internal_add(pa_x11_wrapper *w, int fd) {
143 i = pa_xnew(pa_x11_internal, 1);
145 i->io_event = w->core->mainloop->io_new(w->core->mainloop, fd, PA_IO_EVENT_INPUT, internal_io_event, w);
148 PA_LLIST_PREPEND(pa_x11_internal, w->internals, i);
152 /* Remove an IO source for an X11 internal connection */
153 static void x11_internal_remove(pa_x11_wrapper *w, pa_x11_internal *i) {
156 PA_LLIST_REMOVE(pa_x11_internal, w->internals, i);
157 w->core->mainloop->io_free(i->io_event);
161 /* Implementation of XConnectionWatchProc */
162 static void x11_watch(Display *display, XPointer userdata, int fd, Bool opening, XPointer *watch_data) {
163 pa_x11_wrapper *w = (pa_x11_wrapper*) userdata;
170 *watch_data = (XPointer) x11_internal_add(w, fd);
172 x11_internal_remove(w, (pa_x11_internal*) *watch_data);
175 static int x11_error_handler(Display* display, XErrorEvent* error_event) {
176 pa_log_warn("X11 error handler called");
180 static int x11_io_error_handler(Display* display) {
181 pa_log_warn("X11 I/O error handler called");
185 static void deferred_x11_teardown(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
186 pa_x11_wrapper *w = userdata;
188 m->defer_enable(e, 0);
190 pa_log_debug("Start tearing down X11 modules after X11 I/O error");
194 pa_log_debug("Done tearing down X11 modules after X11 I/O error");
197 #ifdef HAVE_XSETIOERROREXITHANDLER
198 static void x11_io_error_exit_handler(Display* display, void *userdata) {
199 pa_x11_wrapper *w = userdata;
201 pa_log_warn("X11 I/O error exit handler called, preparing to tear down X11 modules");
203 pa_x11_wrapper_kill_deferred(w);
207 static pa_x11_wrapper* x11_wrapper_new(pa_core *c, const char *name, const char *t) {
211 if (!(d = XOpenDisplay(name))) {
212 pa_log("XOpenDisplay() failed");
216 w = pa_xnew(pa_x11_wrapper, 1);
219 w->property_name = pa_xstrdup(t);
222 PA_LLIST_HEAD_INIT(pa_x11_client, w->clients);
223 PA_LLIST_HEAD_INIT(pa_x11_internal, w->internals);
225 w->defer_event = c->mainloop->defer_new(c->mainloop, defer_event, w);
226 w->io_event = c->mainloop->io_new(c->mainloop, ConnectionNumber(d), PA_IO_EVENT_INPUT, display_io_event, w);
227 w->cleanup_event = c->mainloop->defer_new(c->mainloop, deferred_x11_teardown, w);
228 w->core->mainloop->defer_enable(w->cleanup_event, 0);
230 XSetErrorHandler(x11_error_handler);
231 XSetIOErrorHandler(x11_io_error_handler);
232 #ifdef HAVE_XSETIOERROREXITHANDLER
233 XSetIOErrorExitHandler(d, x11_io_error_exit_handler, w);
235 XAddConnectionWatch(d, x11_watch, (XPointer) w);
237 pa_assert_se(pa_shared_set(c, w->property_name, w) >= 0);
239 pa_log_debug("Created X11 connection wrapper '%s'", w->property_name);
244 static void x11_wrapper_free(pa_x11_wrapper*w) {
247 pa_assert_se(pa_shared_remove(w->core, w->property_name) >= 0);
249 pa_assert(!w->clients);
251 pa_log_debug("Destroying X11 connection wrapper '%s'", w->property_name);
253 XRemoveConnectionWatch(w->display, x11_watch, (XPointer) w);
254 XCloseDisplay(w->display);
256 w->core->mainloop->defer_free(w->cleanup_event);
257 w->core->mainloop->io_free(w->io_event);
258 w->core->mainloop->defer_free(w->defer_event);
261 x11_internal_remove(w, w->internals);
263 pa_xfree(w->property_name);
267 pa_x11_wrapper* pa_x11_wrapper_get(pa_core *c, const char *name) {
271 pa_core_assert_ref(c);
273 pa_snprintf(t, sizeof(t), "x11-wrapper%s%s", name ? "@" : "", name ? name : "");
275 if ((w = pa_shared_get(c, t)))
276 return pa_x11_wrapper_ref(w);
278 return x11_wrapper_new(c, name, t);
281 pa_x11_wrapper* pa_x11_wrapper_ref(pa_x11_wrapper *w) {
283 pa_assert(PA_REFCNT_VALUE(w) >= 1);
289 void pa_x11_wrapper_unref(pa_x11_wrapper* w) {
291 pa_assert(PA_REFCNT_VALUE(w) >= 1);
293 if (PA_REFCNT_DEC(w) > 0)
299 Display *pa_x11_wrapper_get_display(pa_x11_wrapper *w) {
301 pa_assert(PA_REFCNT_VALUE(w) >= 1);
303 /* Somebody is using us, schedule a output buffer flush */
304 w->core->mainloop->defer_enable(w->defer_event, 1);
309 xcb_connection_t *pa_x11_wrapper_get_xcb_connection(pa_x11_wrapper *w) {
310 return XGetXCBConnection(pa_x11_wrapper_get_display(w));
313 void pa_x11_wrapper_kill_deferred(pa_x11_wrapper *w) {
316 /* schedule X11 display teardown */
317 w->core->mainloop->defer_enable(w->cleanup_event, 1);
320 /* Kill the connection to the X11 display */
321 static void x11_wrapper_kill(pa_x11_wrapper *w) {
322 pa_x11_client *c, *n;
326 pa_x11_wrapper_ref(w);
328 for (c = w->clients; c; c = n) {
332 c->kill_cb(w, c->userdata);
335 pa_x11_wrapper_unref(w);
338 pa_x11_client* pa_x11_client_new(pa_x11_wrapper *w, pa_x11_event_cb_t event_cb, pa_x11_kill_cb_t kill_cb, void *userdata) {
342 pa_assert(PA_REFCNT_VALUE(w) >= 1);
344 c = pa_xnew(pa_x11_client, 1);
346 c->event_cb = event_cb;
347 c->kill_cb = kill_cb;
348 c->userdata = userdata;
350 PA_LLIST_PREPEND(pa_x11_client, w->clients, c);
355 void pa_x11_client_free(pa_x11_client *c) {
357 pa_assert(c->wrapper);
358 pa_assert(PA_REFCNT_VALUE(c->wrapper) >= 1);
360 PA_LLIST_REMOVE(pa_x11_client, c->wrapper->clients, c);