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