]> code.delx.au - pulseaudio/blob - src/pulsecore/x11wrap.c
6a6a26924f656b8dc73a096fddd3dcf3c0dbcaa4
[pulseaudio] / src / pulsecore / x11wrap.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #include <assert.h>
25 #include <stdio.h>
26
27 #include <pulse/xmalloc.h>
28
29 #include <pulsecore/llist.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/props.h>
32
33 #include "x11wrap.h"
34
35 typedef struct pa_x11_internal pa_x11_internal;
36
37 struct pa_x11_internal {
38 PA_LLIST_FIELDS(pa_x11_internal);
39 pa_x11_wrapper *wrapper;
40 pa_io_event* io_event;
41 int fd;
42 };
43
44 struct pa_x11_wrapper {
45 pa_core *core;
46 int ref;
47
48 char *property_name;
49 Display *display;
50
51 pa_defer_event* defer_event;
52 pa_io_event* io_event;
53
54 PA_LLIST_HEAD(pa_x11_client, clients);
55 PA_LLIST_HEAD(pa_x11_internal, internals);
56 };
57
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);
62 void *userdata;
63 };
64
65 /* Dispatch all pending X11 events */
66 static void work(pa_x11_wrapper *w) {
67 assert(w && w->ref >= 1);
68
69 while (XPending(w->display)) {
70 pa_x11_client *c;
71 XEvent e;
72 XNextEvent(w->display, &e);
73
74 for (c = w->clients; c; c = c->next) {
75 assert(c->callback);
76 if (c->callback(w, &e, c->userdata) != 0)
77 break;
78 }
79 }
80 }
81
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);
86 work(w);
87 }
88
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);
93
94 m->defer_enable(e, 0);
95
96 work(w);
97 }
98
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);
103
104 XProcessInternalConnection(w->display, fd);
105
106 work(w);
107 }
108
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) {
111 pa_x11_internal *i;
112 assert(fd >= 0);
113
114 i = pa_xmalloc(sizeof(pa_x11_internal));
115 assert(i);
116 i->wrapper = w;
117 i->io_event = w->core->mainloop->io_new(w->core->mainloop, fd, PA_IO_EVENT_INPUT, internal_io_event, w);
118 i->fd = fd;
119
120 PA_LLIST_PREPEND(pa_x11_internal, w->internals, i);
121 return i;
122 }
123
124 /* Remove an IO source for an X11 internal connection */
125 static void x11_internal_remove(pa_x11_wrapper *w, pa_x11_internal *i) {
126 assert(i);
127
128 PA_LLIST_REMOVE(pa_x11_internal, w->internals, i);
129 w->core->mainloop->io_free(i->io_event);
130 pa_xfree(i);
131 }
132
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);
137
138 if (opening)
139 *watch_data = (XPointer) x11_internal_add(w, fd);
140 else
141 x11_internal_remove(w, (pa_x11_internal*) *watch_data);
142 }
143
144 static pa_x11_wrapper* x11_wrapper_new(pa_core *c, const char *name, const char *t) {
145 pa_x11_wrapper*w;
146 Display *d;
147 int r;
148
149 if (!(d = XOpenDisplay(name))) {
150 pa_log("XOpenDisplay() failed");
151 return NULL;
152 }
153
154 w = pa_xmalloc(sizeof(pa_x11_wrapper));
155 w->core = c;
156 w->ref = 1;
157 w->property_name = pa_xstrdup(t);
158 w->display = d;
159
160 PA_LLIST_HEAD_INIT(pa_x11_client, w->clients);
161 PA_LLIST_HEAD_INIT(pa_x11_internal, w->internals);
162
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);
165
166 XAddConnectionWatch(d, x11_watch, (XPointer) w);
167
168 r = pa_property_set(c, w->property_name, w);
169 assert(r >= 0);
170
171 return w;
172 }
173
174 static void x11_wrapper_free(pa_x11_wrapper*w) {
175 int r;
176 assert(w);
177
178 r = pa_property_remove(w->core, w->property_name);
179 assert(r >= 0);
180
181 assert(!w->clients);
182
183 XRemoveConnectionWatch(w->display, x11_watch, (XPointer) w);
184 XCloseDisplay(w->display);
185
186 w->core->mainloop->io_free(w->io_event);
187 w->core->mainloop->defer_free(w->defer_event);
188
189 while (w->internals)
190 x11_internal_remove(w, w->internals);
191
192 pa_xfree(w->property_name);
193 pa_xfree(w);
194 }
195
196 pa_x11_wrapper* pa_x11_wrapper_get(pa_core *c, const char *name) {
197 char t[256];
198 pa_x11_wrapper *w;
199 assert(c);
200
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);
204
205 return x11_wrapper_new(c, name, t);
206 }
207
208 pa_x11_wrapper* pa_x11_wrapper_ref(pa_x11_wrapper *w) {
209 assert(w && w->ref >= 1);
210 w->ref++;
211 return w;
212 }
213
214 void pa_x11_wrapper_unref(pa_x11_wrapper* w) {
215 assert(w && w->ref >= 1);
216
217 if (!(--w->ref))
218 x11_wrapper_free(w);
219 }
220
221 Display *pa_x11_wrapper_get_display(pa_x11_wrapper *w) {
222 assert(w && w->ref >= 1);
223
224 /* Somebody is using us, schedule a output buffer flush */
225 w->core->mainloop->defer_enable(w->defer_event, 1);
226
227 return w->display;
228 }
229
230 pa_x11_client* pa_x11_client_new(pa_x11_wrapper *w, int (*cb)(pa_x11_wrapper *w, XEvent *e, void *userdata), void *userdata) {
231 pa_x11_client *c;
232 assert(w && w->ref >= 1);
233
234 c = pa_xmalloc(sizeof(pa_x11_client));
235 c->wrapper = w;
236 c->callback = cb;
237 c->userdata = userdata;
238
239 PA_LLIST_PREPEND(pa_x11_client, w->clients, c);
240
241 return c;
242 }
243
244 void pa_x11_client_free(pa_x11_client *c) {
245 assert(c && c->wrapper && c->wrapper->ref >= 1);
246
247 PA_LLIST_REMOVE(pa_x11_client, c->wrapper->clients, c);
248 pa_xfree(c);
249 }