4 This file is part of PulseAudio.
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 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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 #include <pulse/xmalloc.h>
27 #include <pulsecore/llist.h>
28 #include <pulsecore/log.h>
29 #include <pulsecore/props.h>
33 typedef struct pa_x11_internal pa_x11_internal;
35 struct pa_x11_internal {
36 PA_LLIST_FIELDS(pa_x11_internal);
37 pa_x11_wrapper *wrapper;
38 pa_io_event* io_event;
42 struct pa_x11_wrapper {
49 pa_defer_event* defer_event;
50 pa_io_event* io_event;
52 PA_LLIST_HEAD(pa_x11_client, clients);
53 PA_LLIST_HEAD(pa_x11_internal, internals);
56 struct pa_x11_client {
57 PA_LLIST_FIELDS(pa_x11_client);
58 pa_x11_wrapper *wrapper;
59 int (*callback)(pa_x11_wrapper *w, XEvent *e, void *userdata);
63 /* Dispatch all pending X11 events */
64 static void work(pa_x11_wrapper *w) {
65 assert(w && w->ref >= 1);
67 while (XPending(w->display)) {
70 XNextEvent(w->display, &e);
72 for (c = w->clients; c; c = c->next) {
74 if (c->callback(w, &e, c->userdata) != 0)
80 /* IO notification event for the X11 display connection */
81 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) {
82 pa_x11_wrapper *w = userdata;
83 assert(m && e && fd >= 0 && w && w->ref >= 1);
87 /* Deferred notification event. Called once each main loop iteration */
88 static void defer_event(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
89 pa_x11_wrapper *w = userdata;
90 assert(m && e && w && w->ref >= 1);
92 m->defer_enable(e, 0);
97 /* IO notification event for X11 internal connections */
98 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) {
99 pa_x11_wrapper *w = userdata;
100 assert(m && e && fd >= 0 && w && w->ref >= 1);
102 XProcessInternalConnection(w->display, fd);
107 /* Add a new IO source for the specified X11 internal connection */
108 static pa_x11_internal* x11_internal_add(pa_x11_wrapper *w, int fd) {
112 i = pa_xmalloc(sizeof(pa_x11_internal));
115 i->io_event = w->core->mainloop->io_new(w->core->mainloop, fd, PA_IO_EVENT_INPUT, internal_io_event, w);
118 PA_LLIST_PREPEND(pa_x11_internal, w->internals, i);
122 /* Remove an IO source for an X11 internal connection */
123 static void x11_internal_remove(pa_x11_wrapper *w, pa_x11_internal *i) {
126 PA_LLIST_REMOVE(pa_x11_internal, w->internals, i);
127 w->core->mainloop->io_free(i->io_event);
131 /* Implementation of XConnectionWatchProc */
132 static void x11_watch(Display *display, XPointer userdata, int fd, Bool opening, XPointer *watch_data) {
133 pa_x11_wrapper *w = (pa_x11_wrapper*) userdata;
134 assert(display && w && fd >= 0);
137 *watch_data = (XPointer) x11_internal_add(w, fd);
139 x11_internal_remove(w, (pa_x11_internal*) *watch_data);
142 static pa_x11_wrapper* x11_wrapper_new(pa_core *c, const char *name, const char *t) {
147 if (!(d = XOpenDisplay(name))) {
148 pa_log(__FILE__": XOpenDisplay() failed");
152 w = pa_xmalloc(sizeof(pa_x11_wrapper));
155 w->property_name = pa_xstrdup(t);
158 PA_LLIST_HEAD_INIT(pa_x11_client, w->clients);
159 PA_LLIST_HEAD_INIT(pa_x11_internal, w->internals);
161 w->defer_event = c->mainloop->defer_new(c->mainloop, defer_event, w);
162 w->io_event = c->mainloop->io_new(c->mainloop, ConnectionNumber(d), PA_IO_EVENT_INPUT, display_io_event, w);
164 XAddConnectionWatch(d, x11_watch, (XPointer) w);
166 r = pa_property_set(c, w->property_name, w);
172 static void x11_wrapper_free(pa_x11_wrapper*w) {
176 r = pa_property_remove(w->core, w->property_name);
181 XRemoveConnectionWatch(w->display, x11_watch, (XPointer) w);
182 XCloseDisplay(w->display);
184 w->core->mainloop->io_free(w->io_event);
185 w->core->mainloop->defer_free(w->defer_event);
188 x11_internal_remove(w, w->internals);
190 pa_xfree(w->property_name);
194 pa_x11_wrapper* pa_x11_wrapper_get(pa_core *c, const char *name) {
199 snprintf(t, sizeof(t), "x11-wrapper%s%s", name ? "-" : "", name ? name : "");
200 if ((w = pa_property_get(c, t)))
201 return pa_x11_wrapper_ref(w);
203 return x11_wrapper_new(c, name, t);
206 pa_x11_wrapper* pa_x11_wrapper_ref(pa_x11_wrapper *w) {
207 assert(w && w->ref >= 1);
212 void pa_x11_wrapper_unref(pa_x11_wrapper* w) {
213 assert(w && w->ref >= 1);
219 Display *pa_x11_wrapper_get_display(pa_x11_wrapper *w) {
220 assert(w && w->ref >= 1);
222 /* Somebody is using us, schedule a output buffer flush */
223 w->core->mainloop->defer_enable(w->defer_event, 1);
228 pa_x11_client* pa_x11_client_new(pa_x11_wrapper *w, int (*cb)(pa_x11_wrapper *w, XEvent *e, void *userdata), void *userdata) {
230 assert(w && w->ref >= 1);
232 c = pa_xmalloc(sizeof(pa_x11_client));
235 c->userdata = userdata;
237 PA_LLIST_PREPEND(pa_x11_client, w->clients, c);
242 void pa_x11_client_free(pa_x11_client *c) {
243 assert(c && c->wrapper && c->wrapper->ref >= 1);
245 PA_LLIST_REMOVE(pa_x11_client, c->wrapper->clients, c);