1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 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, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25
26 #include <pulse/xmalloc.h>
27
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>
33
34 #include "x11wrap.h"
35
36 typedef struct pa_x11_internal pa_x11_internal;
37
38 struct pa_x11_internal {
39 PA_LLIST_FIELDS(pa_x11_internal);
40 pa_x11_wrapper *wrapper;
41 pa_io_event* io_event;
42 int fd;
43 };
44
45 struct pa_x11_wrapper {
46 PA_REFCNT_DECLARE;
47 pa_core *core;
48
49 char *property_name;
50 Display *display;
51
52 pa_defer_event* defer_event;
53 pa_io_event* io_event;
54
55 PA_LLIST_HEAD(pa_x11_client, clients);
56 PA_LLIST_HEAD(pa_x11_internal, internals);
57 };
58
59 struct pa_x11_client {
60 PA_LLIST_FIELDS(pa_x11_client);
61 pa_x11_wrapper *wrapper;
62 pa_x11_event_cb_t event_cb;
63 pa_x11_kill_cb_t kill_cb;
64 void *userdata;
65 };
66
67 /* Dispatch all pending X11 events */
work(pa_x11_wrapper * w)68 static void work(pa_x11_wrapper *w) {
69 pa_assert(w);
70 pa_assert(PA_REFCNT_VALUE(w) >= 1);
71
72 pa_x11_wrapper_ref(w);
73
74 while (XPending(w->display)) {
75 pa_x11_client *c, *n;
76 XEvent e;
77 XNextEvent(w->display, &e);
78
79 for (c = w->clients; c; c = n) {
80 n = c->next;
81
82 if (c->event_cb)
83 if (c->event_cb(w, &e, c->userdata) != 0)
84 break;
85 }
86 }
87
88 pa_x11_wrapper_unref(w);
89 }
90
91 /* IO notification event for the X11 display connection */
display_io_event(pa_mainloop_api * m,pa_io_event * e,int fd,pa_io_event_flags_t f,void * userdata)92 static void display_io_event(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
93 pa_x11_wrapper *w = userdata;
94
95 pa_assert(m);
96 pa_assert(e);
97 pa_assert(fd >= 0);
98 pa_assert(w);
99 pa_assert(PA_REFCNT_VALUE(w) >= 1);
100
101 work(w);
102 }
103
104 /* Deferred notification event. Called once each main loop iteration */
defer_event(pa_mainloop_api * m,pa_defer_event * e,void * userdata)105 static void defer_event(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
106 pa_x11_wrapper *w = userdata;
107
108 pa_assert(m);
109 pa_assert(e);
110 pa_assert(w);
111 pa_assert(PA_REFCNT_VALUE(w) >= 1);
112
113 m->defer_enable(e, 0);
114
115 work(w);
116 }
117
118 /* IO notification event for X11 internal connections */
internal_io_event(pa_mainloop_api * m,pa_io_event * e,int fd,pa_io_event_flags_t f,void * userdata)119 static void internal_io_event(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
120 pa_x11_wrapper *w = userdata;
121
122 pa_assert(m);
123 pa_assert(e);
124 pa_assert(fd >= 0);
125 pa_assert(w);
126 pa_assert(PA_REFCNT_VALUE(w) >= 1);
127
128 XProcessInternalConnection(w->display, fd);
129
130 work(w);
131 }
132
133 /* Add a new IO source for the specified X11 internal connection */
x11_internal_add(pa_x11_wrapper * w,int fd)134 static pa_x11_internal* x11_internal_add(pa_x11_wrapper *w, int fd) {
135 pa_x11_internal *i;
136 pa_assert(fd >= 0);
137
138 i = pa_xnew(pa_x11_internal, 1);
139 i->wrapper = w;
140 i->io_event = w->core->mainloop->io_new(w->core->mainloop, fd, PA_IO_EVENT_INPUT, internal_io_event, w);
141 i->fd = fd;
142
143 PA_LLIST_PREPEND(pa_x11_internal, w->internals, i);
144 return i;
145 }
146
147 /* Remove an IO source for an X11 internal connection */
x11_internal_remove(pa_x11_wrapper * w,pa_x11_internal * i)148 static void x11_internal_remove(pa_x11_wrapper *w, pa_x11_internal *i) {
149 pa_assert(i);
150
151 PA_LLIST_REMOVE(pa_x11_internal, w->internals, i);
152 w->core->mainloop->io_free(i->io_event);
153 pa_xfree(i);
154 }
155
156 /* Implementation of XConnectionWatchProc */
x11_watch(Display * display,XPointer userdata,int fd,Bool opening,XPointer * watch_data)157 static void x11_watch(Display *display, XPointer userdata, int fd, Bool opening, XPointer *watch_data) {
158 pa_x11_wrapper *w = (pa_x11_wrapper*) userdata;
159
160 pa_assert(display);
161 pa_assert(w);
162 pa_assert(fd >= 0);
163
164 if (opening)
165 *watch_data = (XPointer) x11_internal_add(w, fd);
166 else
167 x11_internal_remove(w, (pa_x11_internal*) *watch_data);
168 }
169
x11_wrapper_new(pa_core * c,const char * name,const char * t)170 static pa_x11_wrapper* x11_wrapper_new(pa_core *c, const char *name, const char *t) {
171 pa_x11_wrapper*w;
172 Display *d;
173
174 if (!(d = XOpenDisplay(name))) {
175 pa_log("XOpenDisplay() failed");
176 return NULL;
177 }
178
179 w = pa_xnew(pa_x11_wrapper, 1);
180 PA_REFCNT_INIT(w);
181 w->core = c;
182 w->property_name = pa_xstrdup(t);
183 w->display = d;
184
185 PA_LLIST_HEAD_INIT(pa_x11_client, w->clients);
186 PA_LLIST_HEAD_INIT(pa_x11_internal, w->internals);
187
188 w->defer_event = c->mainloop->defer_new(c->mainloop, defer_event, w);
189 w->io_event = c->mainloop->io_new(c->mainloop, ConnectionNumber(d), PA_IO_EVENT_INPUT, display_io_event, w);
190
191 XAddConnectionWatch(d, x11_watch, (XPointer) w);
192
193 pa_assert_se(pa_shared_set(c, w->property_name, w) >= 0);
194
195 return w;
196 }
197
x11_wrapper_free(pa_x11_wrapper * w)198 static void x11_wrapper_free(pa_x11_wrapper*w) {
199 pa_assert(w);
200
201 pa_assert_se(pa_shared_remove(w->core, w->property_name) >= 0);
202
203 pa_assert(!w->clients);
204
205 XRemoveConnectionWatch(w->display, x11_watch, (XPointer) w);
206 XCloseDisplay(w->display);
207
208 w->core->mainloop->io_free(w->io_event);
209 w->core->mainloop->defer_free(w->defer_event);
210
211 while (w->internals)
212 x11_internal_remove(w, w->internals);
213
214 pa_xfree(w->property_name);
215 pa_xfree(w);
216 }
217
pa_x11_wrapper_get(pa_core * c,const char * name)218 pa_x11_wrapper* pa_x11_wrapper_get(pa_core *c, const char *name) {
219 char t[256];
220 pa_x11_wrapper *w;
221
222 pa_core_assert_ref(c);
223
224 pa_snprintf(t, sizeof(t), "x11-wrapper%s%s", name ? "@" : "", name ? name : "");
225
226 if ((w = pa_shared_get(c, t)))
227 return pa_x11_wrapper_ref(w);
228
229 return x11_wrapper_new(c, name, t);
230 }
231
pa_x11_wrapper_ref(pa_x11_wrapper * w)232 pa_x11_wrapper* pa_x11_wrapper_ref(pa_x11_wrapper *w) {
233 pa_assert(w);
234 pa_assert(PA_REFCNT_VALUE(w) >= 1);
235
236 PA_REFCNT_INC(w);
237 return w;
238 }
239
pa_x11_wrapper_unref(pa_x11_wrapper * w)240 void pa_x11_wrapper_unref(pa_x11_wrapper* w) {
241 pa_assert(w);
242 pa_assert(PA_REFCNT_VALUE(w) >= 1);
243
244 if (PA_REFCNT_DEC(w) > 0)
245 return;
246
247 x11_wrapper_free(w);
248 }
249
pa_x11_wrapper_get_display(pa_x11_wrapper * w)250 Display *pa_x11_wrapper_get_display(pa_x11_wrapper *w) {
251 pa_assert(w);
252 pa_assert(PA_REFCNT_VALUE(w) >= 1);
253
254 /* Somebody is using us, schedule a output buffer flush */
255 w->core->mainloop->defer_enable(w->defer_event, 1);
256
257 return w->display;
258 }
259
pa_x11_wrapper_get_xcb_connection(pa_x11_wrapper * w)260 xcb_connection_t *pa_x11_wrapper_get_xcb_connection(pa_x11_wrapper *w) {
261 return XGetXCBConnection(pa_x11_wrapper_get_display(w));
262 }
263
pa_x11_wrapper_kill(pa_x11_wrapper * w)264 void pa_x11_wrapper_kill(pa_x11_wrapper *w) {
265 pa_x11_client *c, *n;
266
267 pa_assert(w);
268
269 pa_x11_wrapper_ref(w);
270
271 for (c = w->clients; c; c = n) {
272 n = c->next;
273
274 if (c->kill_cb)
275 c->kill_cb(w, c->userdata);
276 }
277
278 pa_x11_wrapper_unref(w);
279 }
280
pa_x11_client_new(pa_x11_wrapper * w,pa_x11_event_cb_t event_cb,pa_x11_kill_cb_t kill_cb,void * userdata)281 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) {
282 pa_x11_client *c;
283
284 pa_assert(w);
285 pa_assert(PA_REFCNT_VALUE(w) >= 1);
286
287 c = pa_xnew(pa_x11_client, 1);
288 c->wrapper = w;
289 c->event_cb = event_cb;
290 c->kill_cb = kill_cb;
291 c->userdata = userdata;
292
293 PA_LLIST_PREPEND(pa_x11_client, w->clients, c);
294
295 return c;
296 }
297
pa_x11_client_free(pa_x11_client * c)298 void pa_x11_client_free(pa_x11_client *c) {
299 pa_assert(c);
300 pa_assert(c->wrapper);
301 pa_assert(PA_REFCNT_VALUE(c->wrapper) >= 1);
302
303 PA_LLIST_REMOVE(pa_x11_client, c->wrapper->clients, c);
304 pa_xfree(c);
305 }
306