4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <pulse/xmalloc.h>
29 #include <pulsecore/llist.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/props.h>
35 typedef struct pa_x11_internal pa_x11_internal;
37 struct pa_x11_internal {
38 PA_LLIST_FIELDS(pa_x11_internal);
39 pa_x11_wrapper *wrapper;
40 pa_io_event* io_event;
44 struct pa_x11_wrapper {
51 pa_defer_event* defer_event;
52 pa_io_event* io_event;
54 PA_LLIST_HEAD(pa_x11_client, clients);
55 PA_LLIST_HEAD(pa_x11_internal, internals);
58 struct pa_x11_client {
59 PA_LLIST_FIELDS(pa_x11_client);
60 pa_x11_wrapper *wrapper;
61 int (*callback)(pa_x11_wrapper *w, XEvent *e, void *userdata);
65 /* Dispatch all pending X11 events */
66 static void work(pa_x11_wrapper *w) {
67 assert(w && w->ref >= 1);
69 while (XPending(w->display)) {
72 XNextEvent(w->display, &e);
74 for (c = w->clients; c; c = c->next) {
76 if (c->callback(w, &e, c->userdata) != 0)
82 /* IO notification event for the X11 display connection */
83 static void display_io_event(pa_mainloop_api *m, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
84 pa_x11_wrapper *w = userdata;
85 assert(m && e && fd >= 0 && w && w->ref >= 1);
89 /* Deferred notification event. Called once each main loop iteration */
90 static void defer_event(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
91 pa_x11_wrapper *w = userdata;
92 assert(m && e && w && w->ref >= 1);
94 m->defer_enable(e, 0);
99 /* IO notification event for X11 internal connections */
100 static void internal_io_event(pa_mainloop_api *m, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
101 pa_x11_wrapper *w = userdata;
102 assert(m && e && fd >= 0 && w && w->ref >= 1);
104 XProcessInternalConnection(w->display, fd);
109 /* Add a new IO source for the specified X11 internal connection */
110 static pa_x11_internal* x11_internal_add(pa_x11_wrapper *w, int fd) {
114 i = pa_xmalloc(sizeof(pa_x11_internal));
117 i->io_event = w->core->mainloop->io_new(w->core->mainloop, fd, PA_IO_EVENT_INPUT, internal_io_event, w);
120 PA_LLIST_PREPEND(pa_x11_internal, w->internals, i);
124 /* Remove an IO source for an X11 internal connection */
125 static void x11_internal_remove(pa_x11_wrapper *w, pa_x11_internal *i) {
128 PA_LLIST_REMOVE(pa_x11_internal, w->internals, i);
129 w->core->mainloop->io_free(i->io_event);
133 /* Implementation of XConnectionWatchProc */
134 static void x11_watch(Display *display, XPointer userdata, int fd, Bool opening, XPointer *watch_data) {
135 pa_x11_wrapper *w = (pa_x11_wrapper*) userdata;
136 assert(display && w && fd >= 0);
139 *watch_data = (XPointer) x11_internal_add(w, fd);
141 x11_internal_remove(w, (pa_x11_internal*) *watch_data);
144 static pa_x11_wrapper* x11_wrapper_new(pa_core *c, const char *name, const char *t) {
149 if (!(d = XOpenDisplay(name))) {
150 pa_log("XOpenDisplay() failed");
154 w = pa_xmalloc(sizeof(pa_x11_wrapper));
157 w->property_name = pa_xstrdup(t);
160 PA_LLIST_HEAD_INIT(pa_x11_client, w->clients);
161 PA_LLIST_HEAD_INIT(pa_x11_internal, w->internals);
163 w->defer_event = c->mainloop->defer_new(c->mainloop, defer_event, w);
164 w->io_event = c->mainloop->io_new(c->mainloop, ConnectionNumber(d), PA_IO_EVENT_INPUT, display_io_event, w);
166 XAddConnectionWatch(d, x11_watch, (XPointer) w);
168 r = pa_property_set(c, w->property_name, w);
174 static void x11_wrapper_free(pa_x11_wrapper*w) {
178 r = pa_property_remove(w->core, w->property_name);
183 XRemoveConnectionWatch(w->display, x11_watch, (XPointer) w);
184 XCloseDisplay(w->display);
186 w->core->mainloop->io_free(w->io_event);
187 w->core->mainloop->defer_free(w->defer_event);
190 x11_internal_remove(w, w->internals);
192 pa_xfree(w->property_name);
196 pa_x11_wrapper* pa_x11_wrapper_get(pa_core *c, const char *name) {
201 snprintf(t, sizeof(t), "x11-wrapper%s%s", name ? "-" : "", name ? name : "");
202 if ((w = pa_property_get(c, t)))
203 return pa_x11_wrapper_ref(w);
205 return x11_wrapper_new(c, name, t);
208 pa_x11_wrapper* pa_x11_wrapper_ref(pa_x11_wrapper *w) {
209 assert(w && w->ref >= 1);
214 void pa_x11_wrapper_unref(pa_x11_wrapper* w) {
215 assert(w && w->ref >= 1);
221 Display *pa_x11_wrapper_get_display(pa_x11_wrapper *w) {
222 assert(w && w->ref >= 1);
224 /* Somebody is using us, schedule a output buffer flush */
225 w->core->mainloop->defer_enable(w->defer_event, 1);
230 pa_x11_client* pa_x11_client_new(pa_x11_wrapper *w, int (*cb)(pa_x11_wrapper *w, XEvent *e, void *userdata), void *userdata) {
232 assert(w && w->ref >= 1);
234 c = pa_xmalloc(sizeof(pa_x11_client));
237 c->userdata = userdata;
239 PA_LLIST_PREPEND(pa_x11_client, w->clients, c);
244 void pa_x11_client_free(pa_x11_client *c) {
245 assert(c && c->wrapper && c->wrapper->ref >= 1);
247 PA_LLIST_REMOVE(pa_x11_client, c->wrapper->clients, c);