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
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/llist.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/props.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/macro.h>
40 typedef struct pa_x11_internal pa_x11_internal
;
42 struct pa_x11_internal
{
43 PA_LLIST_FIELDS(pa_x11_internal
);
44 pa_x11_wrapper
*wrapper
;
45 pa_io_event
* io_event
;
49 struct pa_x11_wrapper
{
56 pa_defer_event
* defer_event
;
57 pa_io_event
* io_event
;
59 PA_LLIST_HEAD(pa_x11_client
, clients
);
60 PA_LLIST_HEAD(pa_x11_internal
, internals
);
63 struct pa_x11_client
{
64 PA_LLIST_FIELDS(pa_x11_client
);
65 pa_x11_wrapper
*wrapper
;
66 int (*callback
)(pa_x11_wrapper
*w
, XEvent
*e
, void *userdata
);
70 /* Dispatch all pending X11 events */
71 static void work(pa_x11_wrapper
*w
) {
73 pa_assert(PA_REFCNT_VALUE(w
) >= 1);
75 while (XPending(w
->display
)) {
78 XNextEvent(w
->display
, &e
);
80 for (c
= w
->clients
; c
; c
= c
->next
) {
81 pa_assert(c
->callback
);
82 if (c
->callback(w
, &e
, c
->userdata
) != 0)
88 /* IO notification event for the X11 display connection */
89 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
) {
90 pa_x11_wrapper
*w
= userdata
;
96 pa_assert(PA_REFCNT_VALUE(w
) >= 1);
101 /* Deferred notification event. Called once each main loop iteration */
102 static void defer_event(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
) {
103 pa_x11_wrapper
*w
= userdata
;
108 pa_assert(PA_REFCNT_VALUE(w
) >= 1);
110 m
->defer_enable(e
, 0);
115 /* IO notification event for X11 internal connections */
116 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
) {
117 pa_x11_wrapper
*w
= userdata
;
123 pa_assert(PA_REFCNT_VALUE(w
) >= 1);
125 XProcessInternalConnection(w
->display
, fd
);
130 /* Add a new IO source for the specified X11 internal connection */
131 static pa_x11_internal
* x11_internal_add(pa_x11_wrapper
*w
, int fd
) {
135 i
= pa_xnew(pa_x11_internal
, 1);
137 i
->io_event
= w
->core
->mainloop
->io_new(w
->core
->mainloop
, fd
, PA_IO_EVENT_INPUT
, internal_io_event
, w
);
140 PA_LLIST_PREPEND(pa_x11_internal
, w
->internals
, i
);
144 /* Remove an IO source for an X11 internal connection */
145 static void x11_internal_remove(pa_x11_wrapper
*w
, pa_x11_internal
*i
) {
148 PA_LLIST_REMOVE(pa_x11_internal
, w
->internals
, i
);
149 w
->core
->mainloop
->io_free(i
->io_event
);
153 /* Implementation of XConnectionWatchProc */
154 static void x11_watch(Display
*display
, XPointer userdata
, int fd
, Bool opening
, XPointer
*watch_data
) {
155 pa_x11_wrapper
*w
= (pa_x11_wrapper
*) userdata
;
162 *watch_data
= (XPointer
) x11_internal_add(w
, fd
);
164 x11_internal_remove(w
, (pa_x11_internal
*) *watch_data
);
167 static pa_x11_wrapper
* x11_wrapper_new(pa_core
*c
, const char *name
, const char *t
) {
171 if (!(d
= XOpenDisplay(name
))) {
172 pa_log("XOpenDisplay() failed");
176 w
= pa_xnew(pa_x11_wrapper
, 1);
179 w
->property_name
= pa_xstrdup(t
);
182 PA_LLIST_HEAD_INIT(pa_x11_client
, w
->clients
);
183 PA_LLIST_HEAD_INIT(pa_x11_internal
, w
->internals
);
185 w
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_event
, w
);
186 w
->io_event
= c
->mainloop
->io_new(c
->mainloop
, ConnectionNumber(d
), PA_IO_EVENT_INPUT
, display_io_event
, w
);
188 XAddConnectionWatch(d
, x11_watch
, (XPointer
) w
);
190 pa_assert_se(pa_property_set(c
, w
->property_name
, w
) >= 0);
195 static void x11_wrapper_free(pa_x11_wrapper
*w
) {
198 pa_assert_se(pa_property_remove(w
->core
, w
->property_name
) >= 0);
200 pa_assert(!w
->clients
);
202 XRemoveConnectionWatch(w
->display
, x11_watch
, (XPointer
) w
);
203 XCloseDisplay(w
->display
);
205 w
->core
->mainloop
->io_free(w
->io_event
);
206 w
->core
->mainloop
->defer_free(w
->defer_event
);
209 x11_internal_remove(w
, w
->internals
);
211 pa_xfree(w
->property_name
);
215 pa_x11_wrapper
* pa_x11_wrapper_get(pa_core
*c
, const char *name
) {
219 pa_core_assert_ref(c
);
221 pa_snprintf(t
, sizeof(t
), "x11-wrapper%s%s", name
? "-" : "", name
? name
: "");
222 if ((w
= pa_property_get(c
, t
)))
223 return pa_x11_wrapper_ref(w
);
225 return x11_wrapper_new(c
, name
, t
);
228 pa_x11_wrapper
* pa_x11_wrapper_ref(pa_x11_wrapper
*w
) {
230 pa_assert(PA_REFCNT_VALUE(w
) >= 1);
236 void pa_x11_wrapper_unref(pa_x11_wrapper
* w
) {
238 pa_assert(PA_REFCNT_VALUE(w
) >= 1);
240 if (PA_REFCNT_DEC(w
) <= 0)
244 Display
*pa_x11_wrapper_get_display(pa_x11_wrapper
*w
) {
246 pa_assert(PA_REFCNT_VALUE(w
) >= 1);
248 /* Somebody is using us, schedule a output buffer flush */
249 w
->core
->mainloop
->defer_enable(w
->defer_event
, 1);
254 pa_x11_client
* pa_x11_client_new(pa_x11_wrapper
*w
, int (*cb
)(pa_x11_wrapper
*w
, XEvent
*e
, void *userdata
), void *userdata
) {
258 pa_assert(PA_REFCNT_VALUE(w
) >= 1);
260 c
= pa_xnew(pa_x11_client
, 1);
263 c
->userdata
= userdata
;
265 PA_LLIST_PREPEND(pa_x11_client
, w
->clients
, c
);
270 void pa_x11_client_free(pa_x11_client
*c
) {
272 pa_assert(c
->wrapper
);
273 pa_assert(PA_REFCNT_VALUE(c
->wrapper
) >= 1);
275 PA_LLIST_REMOVE(pa_x11_client
, c
->wrapper
->clients
, c
);