xref: /aosp_15_r20/external/wayland/src/wayland-client.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique  * Copyright © 2008-2012 Kristian Høgsberg
3*84e872a0SLloyd Pique  * Copyright © 2010-2012 Intel Corporation
4*84e872a0SLloyd Pique  *
5*84e872a0SLloyd Pique  * Permission is hereby granted, free of charge, to any person obtaining
6*84e872a0SLloyd Pique  * a copy of this software and associated documentation files (the
7*84e872a0SLloyd Pique  * "Software"), to deal in the Software without restriction, including
8*84e872a0SLloyd Pique  * without limitation the rights to use, copy, modify, merge, publish,
9*84e872a0SLloyd Pique  * distribute, sublicense, and/or sell copies of the Software, and to
10*84e872a0SLloyd Pique  * permit persons to whom the Software is furnished to do so, subject to
11*84e872a0SLloyd Pique  * the following conditions:
12*84e872a0SLloyd Pique  *
13*84e872a0SLloyd Pique  * The above copyright notice and this permission notice (including the
14*84e872a0SLloyd Pique  * next paragraph) shall be included in all copies or substantial
15*84e872a0SLloyd Pique  * portions of the Software.
16*84e872a0SLloyd Pique  *
17*84e872a0SLloyd Pique  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18*84e872a0SLloyd Pique  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19*84e872a0SLloyd Pique  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20*84e872a0SLloyd Pique  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21*84e872a0SLloyd Pique  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22*84e872a0SLloyd Pique  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23*84e872a0SLloyd Pique  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24*84e872a0SLloyd Pique  * SOFTWARE.
25*84e872a0SLloyd Pique  */
26*84e872a0SLloyd Pique 
27*84e872a0SLloyd Pique #define _GNU_SOURCE
28*84e872a0SLloyd Pique 
29*84e872a0SLloyd Pique #include <stdlib.h>
30*84e872a0SLloyd Pique #include <stdint.h>
31*84e872a0SLloyd Pique #include <stddef.h>
32*84e872a0SLloyd Pique #include <stdio.h>
33*84e872a0SLloyd Pique #include <stdbool.h>
34*84e872a0SLloyd Pique #include <errno.h>
35*84e872a0SLloyd Pique #include <string.h>
36*84e872a0SLloyd Pique #include <unistd.h>
37*84e872a0SLloyd Pique #include <sys/socket.h>
38*84e872a0SLloyd Pique #include <sys/un.h>
39*84e872a0SLloyd Pique #include <ctype.h>
40*84e872a0SLloyd Pique #include <assert.h>
41*84e872a0SLloyd Pique #include <fcntl.h>
42*84e872a0SLloyd Pique #include <poll.h>
43*84e872a0SLloyd Pique #include <pthread.h>
44*84e872a0SLloyd Pique 
45*84e872a0SLloyd Pique #include "wayland-util.h"
46*84e872a0SLloyd Pique #include "wayland-os.h"
47*84e872a0SLloyd Pique #include "wayland-client.h"
48*84e872a0SLloyd Pique #include "wayland-private.h"
49*84e872a0SLloyd Pique 
50*84e872a0SLloyd Pique /** \cond */
51*84e872a0SLloyd Pique 
52*84e872a0SLloyd Pique enum wl_proxy_flag {
53*84e872a0SLloyd Pique 	WL_PROXY_FLAG_ID_DELETED = (1 << 0),
54*84e872a0SLloyd Pique 	WL_PROXY_FLAG_DESTROYED = (1 << 1),
55*84e872a0SLloyd Pique 	WL_PROXY_FLAG_WRAPPER = (1 << 2),
56*84e872a0SLloyd Pique };
57*84e872a0SLloyd Pique 
58*84e872a0SLloyd Pique struct wl_zombie {
59*84e872a0SLloyd Pique 	int event_count;
60*84e872a0SLloyd Pique 	int *fd_count;
61*84e872a0SLloyd Pique };
62*84e872a0SLloyd Pique 
63*84e872a0SLloyd Pique struct wl_proxy {
64*84e872a0SLloyd Pique 	struct wl_object object;
65*84e872a0SLloyd Pique 	struct wl_display *display;
66*84e872a0SLloyd Pique 	struct wl_event_queue *queue;
67*84e872a0SLloyd Pique 	uint32_t flags;
68*84e872a0SLloyd Pique 	int refcount;
69*84e872a0SLloyd Pique 	void *user_data;
70*84e872a0SLloyd Pique 	wl_dispatcher_func_t dispatcher;
71*84e872a0SLloyd Pique 	uint32_t version;
72*84e872a0SLloyd Pique 	const char * const *tag;
73*84e872a0SLloyd Pique 	struct wl_list queue_link; /**< in struct wl_event_queue::proxy_list */
74*84e872a0SLloyd Pique };
75*84e872a0SLloyd Pique 
76*84e872a0SLloyd Pique struct wl_event_queue {
77*84e872a0SLloyd Pique 	struct wl_list event_list;
78*84e872a0SLloyd Pique 	struct wl_list proxy_list; /**< struct wl_proxy::queue_link */
79*84e872a0SLloyd Pique 	struct wl_display *display;
80*84e872a0SLloyd Pique };
81*84e872a0SLloyd Pique 
82*84e872a0SLloyd Pique struct wl_display {
83*84e872a0SLloyd Pique 	struct wl_proxy proxy;
84*84e872a0SLloyd Pique 	struct wl_connection *connection;
85*84e872a0SLloyd Pique 
86*84e872a0SLloyd Pique 	/* errno of the last wl_display error */
87*84e872a0SLloyd Pique 	int last_error;
88*84e872a0SLloyd Pique 
89*84e872a0SLloyd Pique 	/* When display gets an error event from some object, it stores
90*84e872a0SLloyd Pique 	 * information about it here, so that client can get this
91*84e872a0SLloyd Pique 	 * information afterwards */
92*84e872a0SLloyd Pique 	struct {
93*84e872a0SLloyd Pique 		/* Code of the error. It can be compared to
94*84e872a0SLloyd Pique 		 * the interface's errors enumeration. */
95*84e872a0SLloyd Pique 		uint32_t code;
96*84e872a0SLloyd Pique 		/* interface (protocol) in which the error occurred */
97*84e872a0SLloyd Pique 		const struct wl_interface *interface;
98*84e872a0SLloyd Pique 		/* id of the proxy that caused the error. There's no warranty
99*84e872a0SLloyd Pique 		 * that the proxy is still valid. It's up to client how it will
100*84e872a0SLloyd Pique 		 * use it */
101*84e872a0SLloyd Pique 		uint32_t id;
102*84e872a0SLloyd Pique 	} protocol_error;
103*84e872a0SLloyd Pique 	int fd;
104*84e872a0SLloyd Pique 	struct wl_map objects;
105*84e872a0SLloyd Pique 	struct wl_event_queue display_queue;
106*84e872a0SLloyd Pique 	struct wl_event_queue default_queue;
107*84e872a0SLloyd Pique 	pthread_mutex_t mutex;
108*84e872a0SLloyd Pique 
109*84e872a0SLloyd Pique 	int reader_count;
110*84e872a0SLloyd Pique 	uint32_t read_serial;
111*84e872a0SLloyd Pique 	pthread_cond_t reader_cond;
112*84e872a0SLloyd Pique 
113*84e872a0SLloyd Pique 	struct wl_list observers;
114*84e872a0SLloyd Pique };
115*84e872a0SLloyd Pique 
116*84e872a0SLloyd Pique /** \endcond */
117*84e872a0SLloyd Pique 
118*84e872a0SLloyd Pique struct wl_client_observer {
119*84e872a0SLloyd Pique 	struct wl_list link;
120*84e872a0SLloyd Pique 	struct wl_display *display;
121*84e872a0SLloyd Pique 	wl_client_message_observer_func_t func;
122*84e872a0SLloyd Pique 	void *user_data;
123*84e872a0SLloyd Pique };
124*84e872a0SLloyd Pique 
125*84e872a0SLloyd Pique static int debug_client = 0;
126*84e872a0SLloyd Pique 
127*84e872a0SLloyd Pique /**
128*84e872a0SLloyd Pique  * This helper function adjusts the closure arguments before they are logged.
129*84e872a0SLloyd Pique  * On the client, after the call to create_proxies(), NEW_ID arguments will
130*84e872a0SLloyd Pique  * point to a wl_proxy accessible via arg.o instead of being an int32
131*84e872a0SLloyd Pique  * accessible by arg.n, which is what wl_closure_print() attempts to print.
132*84e872a0SLloyd Pique  * This helper transforms the argument back into an id, so wl_closure_print()
133*84e872a0SLloyd Pique  * doesn't need to handle that as a special case.
134*84e872a0SLloyd Pique  *
135*84e872a0SLloyd Pique  * \param closure  closure to adjust
136*84e872a0SLloyd Pique  * \param send     if this is closure is for a request
137*84e872a0SLloyd Pique  *
138*84e872a0SLloyd Pique  */
139*84e872a0SLloyd Pique static void
adjust_closure_args_for_logging(struct wl_closure * closure,bool send)140*84e872a0SLloyd Pique adjust_closure_args_for_logging(struct wl_closure *closure, bool send)
141*84e872a0SLloyd Pique {
142*84e872a0SLloyd Pique 	int i;
143*84e872a0SLloyd Pique 	struct argument_details arg;
144*84e872a0SLloyd Pique 	const struct wl_proxy *proxy;
145*84e872a0SLloyd Pique 	const char *signature = closure->message->signature;
146*84e872a0SLloyd Pique 
147*84e872a0SLloyd Pique 	// No adjustment needed for a send.
148*84e872a0SLloyd Pique 	if (send)
149*84e872a0SLloyd Pique 		return;
150*84e872a0SLloyd Pique 
151*84e872a0SLloyd Pique 	for (i = 0; i < closure->count; i++) {
152*84e872a0SLloyd Pique 		signature = get_next_argument(signature, &arg);
153*84e872a0SLloyd Pique 
154*84e872a0SLloyd Pique 		switch (arg.type) {
155*84e872a0SLloyd Pique 		case 'n':
156*84e872a0SLloyd Pique 			proxy = (struct wl_proxy *)closure->args[i].o;
157*84e872a0SLloyd Pique 			closure->args[i].n = proxy ? proxy->object.id : 0;
158*84e872a0SLloyd Pique 			break;
159*84e872a0SLloyd Pique 		}
160*84e872a0SLloyd Pique 	}
161*84e872a0SLloyd Pique }
162*84e872a0SLloyd Pique 
163*84e872a0SLloyd Pique /**
164*84e872a0SLloyd Pique  * Maps the \c discard_reason to a string suitable for logging.
165*84e872a0SLloyd Pique  *
166*84e872a0SLloyd Pique  * \param discarded_reason  reason for discard
167*84e872a0SLloyd Pique  * \return A string describing the reason, or NULL.
168*84e872a0SLloyd Pique  *
169*84e872a0SLloyd Pique  */
170*84e872a0SLloyd Pique static const char *
get_discarded_reason_str(enum wl_client_message_discarded_reason discarded_reason)171*84e872a0SLloyd Pique get_discarded_reason_str(
172*84e872a0SLloyd Pique 	enum wl_client_message_discarded_reason discarded_reason)
173*84e872a0SLloyd Pique {
174*84e872a0SLloyd Pique 	switch (discarded_reason) {
175*84e872a0SLloyd Pique 	case WL_CLIENT_MESSAGE_NOT_DISCARDED:
176*84e872a0SLloyd Pique 		return NULL;
177*84e872a0SLloyd Pique 	case WL_CLIENT_MESSAGE_DISCARD_DEAD_PROXY_ON_DISPATCH:
178*84e872a0SLloyd Pique 		return "dead proxy on dispatch";
179*84e872a0SLloyd Pique 	case WL_CLIENT_MESSAGE_DISCARD_NO_LISTENER_ON_DISPATCH:
180*84e872a0SLloyd Pique 		return "no listener on dispatch";
181*84e872a0SLloyd Pique 	case WL_CLIENT_MESSAGE_DISCARD_UNKNOWN_ID_ON_DEMARSHAL:
182*84e872a0SLloyd Pique 		return "unknown id on demarshal";
183*84e872a0SLloyd Pique 	}
184*84e872a0SLloyd Pique 	return NULL;
185*84e872a0SLloyd Pique }
186*84e872a0SLloyd Pique 
187*84e872a0SLloyd Pique /**
188*84e872a0SLloyd Pique  * This function helps log closures from the client, assuming logging is
189*84e872a0SLloyd Pique  * enabled.
190*84e872a0SLloyd Pique  *
191*84e872a0SLloyd Pique  * \param closure    closure for the message
192*84e872a0SLloyd Pique  * \param proxy      proxy for the message
193*84e872a0SLloyd Pique  * \param send       true if this is closure is for a request
194*84e872a0SLloyd Pique  * \param discarded_reason  reason if the message is being discarded, or
195*84e872a0SLloyd Pique  *                          WL_CLIENT_MESSAGE_NOT_DISCARDED
196*84e872a0SLloyd Pique  */
197*84e872a0SLloyd Pique static void
closure_log(struct wl_closure * closure,struct wl_proxy * proxy,bool send,enum wl_client_message_discarded_reason discarded_reason)198*84e872a0SLloyd Pique closure_log(struct wl_closure *closure, struct wl_proxy *proxy, bool send,
199*84e872a0SLloyd Pique 	    enum wl_client_message_discarded_reason discarded_reason)
200*84e872a0SLloyd Pique {
201*84e872a0SLloyd Pique 	struct wl_display *display = proxy->display;
202*84e872a0SLloyd Pique 	const char *discarded_reason_str;
203*84e872a0SLloyd Pique 	struct wl_closure adjusted_closure = { 0 };
204*84e872a0SLloyd Pique 
205*84e872a0SLloyd Pique 	if (!debug_client && wl_list_empty(&display->observers))
206*84e872a0SLloyd Pique 		return;
207*84e872a0SLloyd Pique 
208*84e872a0SLloyd Pique 	// Note: The real closure has extra data (referenced by its args
209*84e872a0SLloyd Pique 	// immediately following the structure in memory, but we don't
210*84e872a0SLloyd Pique 	// need to duplicate that.
211*84e872a0SLloyd Pique 	memcpy(&adjusted_closure, closure, sizeof(struct wl_closure));
212*84e872a0SLloyd Pique 
213*84e872a0SLloyd Pique 	// Adjust the closure arguments.
214*84e872a0SLloyd Pique 	adjust_closure_args_for_logging(&adjusted_closure, send);
215*84e872a0SLloyd Pique 
216*84e872a0SLloyd Pique 	discarded_reason_str = get_discarded_reason_str(discarded_reason);
217*84e872a0SLloyd Pique 
218*84e872a0SLloyd Pique 	if (debug_client)
219*84e872a0SLloyd Pique 		wl_closure_print(&adjusted_closure, &proxy->object, send,
220*84e872a0SLloyd Pique 				 discarded_reason_str);
221*84e872a0SLloyd Pique 
222*84e872a0SLloyd Pique 	if (!wl_list_empty(&display->observers)) {
223*84e872a0SLloyd Pique 		enum wl_client_message_type type =
224*84e872a0SLloyd Pique 			send ? WL_CLIENT_MESSAGE_REQUEST
225*84e872a0SLloyd Pique 			     : WL_CLIENT_MESSAGE_EVENT;
226*84e872a0SLloyd Pique 		struct wl_client_observer *observer;
227*84e872a0SLloyd Pique 		struct wl_client_observed_message message;
228*84e872a0SLloyd Pique 
229*84e872a0SLloyd Pique 		message.proxy = proxy;
230*84e872a0SLloyd Pique 		message.message_opcode = adjusted_closure.opcode;
231*84e872a0SLloyd Pique 		message.message = adjusted_closure.message;
232*84e872a0SLloyd Pique 		message.arguments_count = adjusted_closure.count;
233*84e872a0SLloyd Pique 		message.arguments = adjusted_closure.args;
234*84e872a0SLloyd Pique 		message.discarded_reason = discarded_reason;
235*84e872a0SLloyd Pique 		message.discarded_reason_str = discarded_reason_str;
236*84e872a0SLloyd Pique 		wl_list_for_each(observer, &display->observers, link) {
237*84e872a0SLloyd Pique 			observer->func(observer->user_data, type, &message);
238*84e872a0SLloyd Pique 		}
239*84e872a0SLloyd Pique 	}
240*84e872a0SLloyd Pique }
241*84e872a0SLloyd Pique 
242*84e872a0SLloyd Pique /**
243*84e872a0SLloyd Pique  * This function helps log unknown messages on the client, when logging is
244*84e872a0SLloyd Pique  * enabled.
245*84e872a0SLloyd Pique  *
246*84e872a0SLloyd Pique  * \param display    current display
247*84e872a0SLloyd Pique  * \param zombie     true if there was a zombie for the message target
248*84e872a0SLloyd Pique  * \param id         id of the proxy this message was meant for
249*84e872a0SLloyd Pique  * \param opcode     opcode from the message
250*84e872a0SLloyd Pique  * \param num_fds    number of fd arguments for this message
251*84e872a0SLloyd Pique  * \param num_bytes  byte size of this message
252*84e872a0SLloyd Pique  */
253*84e872a0SLloyd Pique static void
log_unknown_message(struct wl_display * display,bool zombie,uint32_t id,int opcode,int num_fds,int num_bytes)254*84e872a0SLloyd Pique log_unknown_message(struct wl_display *display, bool zombie, uint32_t id,
255*84e872a0SLloyd Pique 		    int opcode, int num_fds, int num_bytes)
256*84e872a0SLloyd Pique {
257*84e872a0SLloyd Pique 	char event_detail[100];
258*84e872a0SLloyd Pique 	struct wl_interface unknown_interface = { 0 };
259*84e872a0SLloyd Pique 	struct wl_proxy unknown_proxy = { 0 };
260*84e872a0SLloyd Pique 	struct wl_message unknown_message = { 0 };
261*84e872a0SLloyd Pique 	struct wl_closure unknown_closure = { 0 };
262*84e872a0SLloyd Pique 
263*84e872a0SLloyd Pique 	if (!debug_client && wl_list_empty(&display->observers))
264*84e872a0SLloyd Pique 		return;
265*84e872a0SLloyd Pique 
266*84e872a0SLloyd Pique 	snprintf(event_detail, sizeof event_detail,
267*84e872a0SLloyd Pique 		 "[event %d, %d fds, %d bytes]", opcode, num_fds, num_bytes);
268*84e872a0SLloyd Pique 
269*84e872a0SLloyd Pique 	unknown_interface.name = zombie ? "[zombie]" : "[unknown]";
270*84e872a0SLloyd Pique 
271*84e872a0SLloyd Pique 	unknown_proxy.object.interface = &unknown_interface;
272*84e872a0SLloyd Pique 	unknown_proxy.object.id = id;
273*84e872a0SLloyd Pique 	unknown_proxy.display = display;
274*84e872a0SLloyd Pique 	unknown_proxy.refcount = -1;
275*84e872a0SLloyd Pique 	unknown_proxy.flags = WL_PROXY_FLAG_WRAPPER;
276*84e872a0SLloyd Pique 
277*84e872a0SLloyd Pique 	unknown_message.name = event_detail;
278*84e872a0SLloyd Pique 	unknown_message.signature = "";
279*84e872a0SLloyd Pique 	unknown_message.types = NULL;
280*84e872a0SLloyd Pique 
281*84e872a0SLloyd Pique 	unknown_closure.message = &unknown_message;
282*84e872a0SLloyd Pique 	unknown_closure.opcode = opcode;
283*84e872a0SLloyd Pique 	unknown_closure.proxy = &unknown_proxy;
284*84e872a0SLloyd Pique 
285*84e872a0SLloyd Pique 	closure_log(&unknown_closure, &unknown_proxy, false,
286*84e872a0SLloyd Pique 		    WL_CLIENT_MESSAGE_DISCARD_UNKNOWN_ID_ON_DEMARSHAL);
287*84e872a0SLloyd Pique }
288*84e872a0SLloyd Pique 
289*84e872a0SLloyd Pique /**
290*84e872a0SLloyd Pique  * This helper function wakes up all threads that are
291*84e872a0SLloyd Pique  * waiting for display->reader_cond (i. e. when reading is done,
292*84e872a0SLloyd Pique  * canceled, or an error occurred)
293*84e872a0SLloyd Pique  *
294*84e872a0SLloyd Pique  * NOTE: must be called with display->mutex locked
295*84e872a0SLloyd Pique  */
296*84e872a0SLloyd Pique static void
display_wakeup_threads(struct wl_display * display)297*84e872a0SLloyd Pique display_wakeup_threads(struct wl_display *display)
298*84e872a0SLloyd Pique {
299*84e872a0SLloyd Pique 	/* Thread can get sleeping only in read_events(). If we're
300*84e872a0SLloyd Pique 	 * waking it up, it means that the read completed or was
301*84e872a0SLloyd Pique 	 * canceled, so we must increase the read_serial.
302*84e872a0SLloyd Pique 	 * This prevents from indefinite sleeping in read_events().
303*84e872a0SLloyd Pique 	 */
304*84e872a0SLloyd Pique 	++display->read_serial;
305*84e872a0SLloyd Pique 
306*84e872a0SLloyd Pique 	pthread_cond_broadcast(&display->reader_cond);
307*84e872a0SLloyd Pique }
308*84e872a0SLloyd Pique 
309*84e872a0SLloyd Pique /**
310*84e872a0SLloyd Pique  * This function is called for local errors (no memory, server hung up)
311*84e872a0SLloyd Pique  *
312*84e872a0SLloyd Pique  * \param display
313*84e872a0SLloyd Pique  * \param error    error value (EINVAL, EFAULT, ...)
314*84e872a0SLloyd Pique  *
315*84e872a0SLloyd Pique  * \note this function is called with display mutex locked
316*84e872a0SLloyd Pique  */
317*84e872a0SLloyd Pique static void
display_fatal_error(struct wl_display * display,int error)318*84e872a0SLloyd Pique display_fatal_error(struct wl_display *display, int error)
319*84e872a0SLloyd Pique {
320*84e872a0SLloyd Pique 	if (display->last_error)
321*84e872a0SLloyd Pique 		return;
322*84e872a0SLloyd Pique 
323*84e872a0SLloyd Pique 	if (!error)
324*84e872a0SLloyd Pique 		error = EFAULT;
325*84e872a0SLloyd Pique 
326*84e872a0SLloyd Pique 	display->last_error = error;
327*84e872a0SLloyd Pique 
328*84e872a0SLloyd Pique 	display_wakeup_threads(display);
329*84e872a0SLloyd Pique }
330*84e872a0SLloyd Pique 
331*84e872a0SLloyd Pique /**
332*84e872a0SLloyd Pique  * This function is called for error events
333*84e872a0SLloyd Pique  * and indicates that in some object an error occurred.
334*84e872a0SLloyd Pique  * The difference between this function and display_fatal_error()
335*84e872a0SLloyd Pique  * is that this one handles errors that will come by wire,
336*84e872a0SLloyd Pique  * whereas display_fatal_error() is called for local errors.
337*84e872a0SLloyd Pique  *
338*84e872a0SLloyd Pique  * \param display
339*84e872a0SLloyd Pique  * \param code    error code
340*84e872a0SLloyd Pique  * \param id      id of the object that generated the error
341*84e872a0SLloyd Pique  * \param intf    protocol interface
342*84e872a0SLloyd Pique  */
343*84e872a0SLloyd Pique static void
display_protocol_error(struct wl_display * display,uint32_t code,uint32_t id,const struct wl_interface * intf)344*84e872a0SLloyd Pique display_protocol_error(struct wl_display *display, uint32_t code,
345*84e872a0SLloyd Pique 		       uint32_t id, const struct wl_interface *intf)
346*84e872a0SLloyd Pique {
347*84e872a0SLloyd Pique 	int err;
348*84e872a0SLloyd Pique 
349*84e872a0SLloyd Pique 	if (display->last_error)
350*84e872a0SLloyd Pique 		return;
351*84e872a0SLloyd Pique 
352*84e872a0SLloyd Pique 	/* set correct errno */
353*84e872a0SLloyd Pique 	if (intf && wl_interface_equal(intf, &wl_display_interface)) {
354*84e872a0SLloyd Pique 		switch (code) {
355*84e872a0SLloyd Pique 		case WL_DISPLAY_ERROR_INVALID_OBJECT:
356*84e872a0SLloyd Pique 		case WL_DISPLAY_ERROR_INVALID_METHOD:
357*84e872a0SLloyd Pique 			err = EINVAL;
358*84e872a0SLloyd Pique 			break;
359*84e872a0SLloyd Pique 		case WL_DISPLAY_ERROR_NO_MEMORY:
360*84e872a0SLloyd Pique 			err = ENOMEM;
361*84e872a0SLloyd Pique 			break;
362*84e872a0SLloyd Pique 		case WL_DISPLAY_ERROR_IMPLEMENTATION:
363*84e872a0SLloyd Pique 			err = EPROTO;
364*84e872a0SLloyd Pique 			break;
365*84e872a0SLloyd Pique 		default:
366*84e872a0SLloyd Pique 			err = EFAULT;
367*84e872a0SLloyd Pique 		}
368*84e872a0SLloyd Pique 	} else {
369*84e872a0SLloyd Pique 		err = EPROTO;
370*84e872a0SLloyd Pique 	}
371*84e872a0SLloyd Pique 
372*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
373*84e872a0SLloyd Pique 
374*84e872a0SLloyd Pique 	display->last_error = err;
375*84e872a0SLloyd Pique 
376*84e872a0SLloyd Pique 	display->protocol_error.code = code;
377*84e872a0SLloyd Pique 	display->protocol_error.id = id;
378*84e872a0SLloyd Pique 	display->protocol_error.interface = intf;
379*84e872a0SLloyd Pique 
380*84e872a0SLloyd Pique 	/*
381*84e872a0SLloyd Pique 	 * here it is not necessary to wake up threads like in
382*84e872a0SLloyd Pique 	 * display_fatal_error, because this function is called from
383*84e872a0SLloyd Pique 	 * an event handler and that means that read_events() is done
384*84e872a0SLloyd Pique 	 * and woke up all threads. Since wl_display_prepare_read()
385*84e872a0SLloyd Pique 	 * fails when there are events in the queue, no threads
386*84e872a0SLloyd Pique 	 * can sleep in read_events() during dispatching
387*84e872a0SLloyd Pique 	 * (and therefore during calling this function), so this is safe.
388*84e872a0SLloyd Pique 	 */
389*84e872a0SLloyd Pique 
390*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
391*84e872a0SLloyd Pique }
392*84e872a0SLloyd Pique 
393*84e872a0SLloyd Pique static void
wl_event_queue_init(struct wl_event_queue * queue,struct wl_display * display)394*84e872a0SLloyd Pique wl_event_queue_init(struct wl_event_queue *queue, struct wl_display *display)
395*84e872a0SLloyd Pique {
396*84e872a0SLloyd Pique 	wl_list_init(&queue->event_list);
397*84e872a0SLloyd Pique 	wl_list_init(&queue->proxy_list);
398*84e872a0SLloyd Pique 	queue->display = display;
399*84e872a0SLloyd Pique }
400*84e872a0SLloyd Pique 
401*84e872a0SLloyd Pique static void
wl_proxy_unref(struct wl_proxy * proxy)402*84e872a0SLloyd Pique wl_proxy_unref(struct wl_proxy *proxy)
403*84e872a0SLloyd Pique {
404*84e872a0SLloyd Pique 	assert(proxy->refcount > 0);
405*84e872a0SLloyd Pique 	if (--proxy->refcount > 0)
406*84e872a0SLloyd Pique 		return;
407*84e872a0SLloyd Pique 
408*84e872a0SLloyd Pique 	/* If we get here, the client must have explicitly requested
409*84e872a0SLloyd Pique 	 * deletion. */
410*84e872a0SLloyd Pique 	assert(proxy->flags & WL_PROXY_FLAG_DESTROYED);
411*84e872a0SLloyd Pique 	free(proxy);
412*84e872a0SLloyd Pique }
413*84e872a0SLloyd Pique 
414*84e872a0SLloyd Pique static void
validate_closure_objects(struct wl_closure * closure)415*84e872a0SLloyd Pique validate_closure_objects(struct wl_closure *closure)
416*84e872a0SLloyd Pique {
417*84e872a0SLloyd Pique 	const char *signature;
418*84e872a0SLloyd Pique 	struct argument_details arg;
419*84e872a0SLloyd Pique 	int i, count;
420*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
421*84e872a0SLloyd Pique 
422*84e872a0SLloyd Pique 	signature = closure->message->signature;
423*84e872a0SLloyd Pique 	count = arg_count_for_signature(signature);
424*84e872a0SLloyd Pique 	for (i = 0; i < count; i++) {
425*84e872a0SLloyd Pique 		signature = get_next_argument(signature, &arg);
426*84e872a0SLloyd Pique 		switch (arg.type) {
427*84e872a0SLloyd Pique 		case 'n':
428*84e872a0SLloyd Pique 		case 'o':
429*84e872a0SLloyd Pique 			proxy = (struct wl_proxy *) closure->args[i].o;
430*84e872a0SLloyd Pique 			if (proxy && proxy->flags & WL_PROXY_FLAG_DESTROYED)
431*84e872a0SLloyd Pique 				closure->args[i].o = NULL;
432*84e872a0SLloyd Pique 			break;
433*84e872a0SLloyd Pique 		default:
434*84e872a0SLloyd Pique 			break;
435*84e872a0SLloyd Pique 		}
436*84e872a0SLloyd Pique 	}
437*84e872a0SLloyd Pique }
438*84e872a0SLloyd Pique 
439*84e872a0SLloyd Pique /* Destroys a closure which was demarshaled for dispatch; unrefs all the
440*84e872a0SLloyd Pique  * proxies in its arguments, as well as its own proxy, and destroys the
441*84e872a0SLloyd Pique  * closure itself. */
442*84e872a0SLloyd Pique static void
destroy_queued_closure(struct wl_closure * closure)443*84e872a0SLloyd Pique destroy_queued_closure(struct wl_closure *closure)
444*84e872a0SLloyd Pique {
445*84e872a0SLloyd Pique 	const char *signature;
446*84e872a0SLloyd Pique 	struct argument_details arg;
447*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
448*84e872a0SLloyd Pique 	int i, count;
449*84e872a0SLloyd Pique 
450*84e872a0SLloyd Pique 	signature = closure->message->signature;
451*84e872a0SLloyd Pique 	count = arg_count_for_signature(signature);
452*84e872a0SLloyd Pique 	for (i = 0; i < count; i++) {
453*84e872a0SLloyd Pique 		signature = get_next_argument(signature, &arg);
454*84e872a0SLloyd Pique 		switch (arg.type) {
455*84e872a0SLloyd Pique 		case 'n':
456*84e872a0SLloyd Pique 		case 'o':
457*84e872a0SLloyd Pique 			proxy = (struct wl_proxy *) closure->args[i].o;
458*84e872a0SLloyd Pique 			if (proxy)
459*84e872a0SLloyd Pique 				wl_proxy_unref(proxy);
460*84e872a0SLloyd Pique 			break;
461*84e872a0SLloyd Pique 		default:
462*84e872a0SLloyd Pique 			break;
463*84e872a0SLloyd Pique 		}
464*84e872a0SLloyd Pique 	}
465*84e872a0SLloyd Pique 
466*84e872a0SLloyd Pique 	wl_proxy_unref(closure->proxy);
467*84e872a0SLloyd Pique 	wl_closure_destroy(closure);
468*84e872a0SLloyd Pique }
469*84e872a0SLloyd Pique 
470*84e872a0SLloyd Pique static void
wl_event_queue_release(struct wl_event_queue * queue)471*84e872a0SLloyd Pique wl_event_queue_release(struct wl_event_queue *queue)
472*84e872a0SLloyd Pique {
473*84e872a0SLloyd Pique 	struct wl_closure *closure;
474*84e872a0SLloyd Pique 
475*84e872a0SLloyd Pique 	if (!wl_list_empty(&queue->proxy_list)) {
476*84e872a0SLloyd Pique 		struct wl_proxy *proxy, *tmp;
477*84e872a0SLloyd Pique 
478*84e872a0SLloyd Pique 		if (queue != &queue->display->default_queue) {
479*84e872a0SLloyd Pique 			wl_log("warning: queue %p destroyed while proxies "
480*84e872a0SLloyd Pique 			       "still attached:\n", queue);
481*84e872a0SLloyd Pique 		}
482*84e872a0SLloyd Pique 
483*84e872a0SLloyd Pique 		wl_list_for_each_safe(proxy, tmp, &queue->proxy_list,
484*84e872a0SLloyd Pique 				      queue_link) {
485*84e872a0SLloyd Pique 			if (queue != &queue->display->default_queue) {
486*84e872a0SLloyd Pique 				wl_log("  %s@%u still attached\n",
487*84e872a0SLloyd Pique 				       proxy->object.interface->name,
488*84e872a0SLloyd Pique 				       proxy->object.id);
489*84e872a0SLloyd Pique 			}
490*84e872a0SLloyd Pique 			proxy->queue = NULL;
491*84e872a0SLloyd Pique 			wl_list_remove(&proxy->queue_link);
492*84e872a0SLloyd Pique 			wl_list_init(&proxy->queue_link);
493*84e872a0SLloyd Pique 		}
494*84e872a0SLloyd Pique 	}
495*84e872a0SLloyd Pique 
496*84e872a0SLloyd Pique 	while (!wl_list_empty(&queue->event_list)) {
497*84e872a0SLloyd Pique 		closure = wl_container_of(queue->event_list.next,
498*84e872a0SLloyd Pique 					  closure, link);
499*84e872a0SLloyd Pique 		wl_list_remove(&closure->link);
500*84e872a0SLloyd Pique 		destroy_queued_closure(closure);
501*84e872a0SLloyd Pique 	}
502*84e872a0SLloyd Pique }
503*84e872a0SLloyd Pique 
504*84e872a0SLloyd Pique /** Destroy an event queue
505*84e872a0SLloyd Pique  *
506*84e872a0SLloyd Pique  * \param queue The event queue to be destroyed
507*84e872a0SLloyd Pique  *
508*84e872a0SLloyd Pique  * Destroy the given event queue. Any pending event on that queue is
509*84e872a0SLloyd Pique  * discarded.
510*84e872a0SLloyd Pique  *
511*84e872a0SLloyd Pique  * The \ref wl_display object used to create the queue should not be
512*84e872a0SLloyd Pique  * destroyed until all event queues created with it are destroyed with
513*84e872a0SLloyd Pique  * this function.
514*84e872a0SLloyd Pique  *
515*84e872a0SLloyd Pique  * \memberof wl_event_queue
516*84e872a0SLloyd Pique  */
517*84e872a0SLloyd Pique WL_EXPORT void
wl_event_queue_destroy(struct wl_event_queue * queue)518*84e872a0SLloyd Pique wl_event_queue_destroy(struct wl_event_queue *queue)
519*84e872a0SLloyd Pique {
520*84e872a0SLloyd Pique 	struct wl_display *display = queue->display;
521*84e872a0SLloyd Pique 
522*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
523*84e872a0SLloyd Pique 	wl_event_queue_release(queue);
524*84e872a0SLloyd Pique 	free(queue);
525*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
526*84e872a0SLloyd Pique }
527*84e872a0SLloyd Pique 
528*84e872a0SLloyd Pique /** Create a new event queue for this display
529*84e872a0SLloyd Pique  *
530*84e872a0SLloyd Pique  * \param display The display context object
531*84e872a0SLloyd Pique  * \return A new event queue associated with this display or NULL on
532*84e872a0SLloyd Pique  * failure.
533*84e872a0SLloyd Pique  *
534*84e872a0SLloyd Pique  * \memberof wl_display
535*84e872a0SLloyd Pique  */
536*84e872a0SLloyd Pique WL_EXPORT struct wl_event_queue *
wl_display_create_queue(struct wl_display * display)537*84e872a0SLloyd Pique wl_display_create_queue(struct wl_display *display)
538*84e872a0SLloyd Pique {
539*84e872a0SLloyd Pique 	struct wl_event_queue *queue;
540*84e872a0SLloyd Pique 
541*84e872a0SLloyd Pique 	queue = zalloc(sizeof *queue);
542*84e872a0SLloyd Pique 	if (queue == NULL)
543*84e872a0SLloyd Pique 		return NULL;
544*84e872a0SLloyd Pique 
545*84e872a0SLloyd Pique 	wl_event_queue_init(queue, display);
546*84e872a0SLloyd Pique 
547*84e872a0SLloyd Pique 	return queue;
548*84e872a0SLloyd Pique }
549*84e872a0SLloyd Pique 
550*84e872a0SLloyd Pique static int
message_count_fds(const char * signature)551*84e872a0SLloyd Pique message_count_fds(const char *signature)
552*84e872a0SLloyd Pique {
553*84e872a0SLloyd Pique 	unsigned int count, i, fds = 0;
554*84e872a0SLloyd Pique 	struct argument_details arg;
555*84e872a0SLloyd Pique 
556*84e872a0SLloyd Pique 	count = arg_count_for_signature(signature);
557*84e872a0SLloyd Pique 	for (i = 0; i < count; i++) {
558*84e872a0SLloyd Pique 		signature = get_next_argument(signature, &arg);
559*84e872a0SLloyd Pique 		if (arg.type == 'h')
560*84e872a0SLloyd Pique 			fds++;
561*84e872a0SLloyd Pique 	}
562*84e872a0SLloyd Pique 
563*84e872a0SLloyd Pique 	return fds;
564*84e872a0SLloyd Pique }
565*84e872a0SLloyd Pique 
566*84e872a0SLloyd Pique static struct wl_zombie *
prepare_zombie(struct wl_proxy * proxy)567*84e872a0SLloyd Pique prepare_zombie(struct wl_proxy *proxy)
568*84e872a0SLloyd Pique {
569*84e872a0SLloyd Pique 	const struct wl_interface *interface = proxy->object.interface;
570*84e872a0SLloyd Pique 	const struct wl_message *message;
571*84e872a0SLloyd Pique 	int i, count;
572*84e872a0SLloyd Pique 	struct wl_zombie *zombie = NULL;
573*84e872a0SLloyd Pique 
574*84e872a0SLloyd Pique 	/* If we hit an event with an FD, ensure we have a zombie object and
575*84e872a0SLloyd Pique 	 * fill the fd_count slot for that event with the number of FDs for
576*84e872a0SLloyd Pique 	 * that event. Interfaces with no events containing FDs will not have
577*84e872a0SLloyd Pique 	 * zombie objects created. */
578*84e872a0SLloyd Pique 	for (i = 0; i < interface->event_count; i++) {
579*84e872a0SLloyd Pique 		message = &interface->events[i];
580*84e872a0SLloyd Pique 		count = message_count_fds(message->signature);
581*84e872a0SLloyd Pique 
582*84e872a0SLloyd Pique 		if (!count)
583*84e872a0SLloyd Pique 			continue;
584*84e872a0SLloyd Pique 
585*84e872a0SLloyd Pique 		if (!zombie) {
586*84e872a0SLloyd Pique 			zombie = zalloc(sizeof(*zombie) +
587*84e872a0SLloyd Pique 				        (interface->event_count * sizeof(int)));
588*84e872a0SLloyd Pique 			if (!zombie)
589*84e872a0SLloyd Pique 				return NULL;
590*84e872a0SLloyd Pique 
591*84e872a0SLloyd Pique 			zombie->event_count = interface->event_count;
592*84e872a0SLloyd Pique 			zombie->fd_count = (int *) &zombie[1];
593*84e872a0SLloyd Pique 		}
594*84e872a0SLloyd Pique 
595*84e872a0SLloyd Pique 		zombie->fd_count[i] = count;
596*84e872a0SLloyd Pique 	}
597*84e872a0SLloyd Pique 
598*84e872a0SLloyd Pique 	return zombie;
599*84e872a0SLloyd Pique }
600*84e872a0SLloyd Pique 
601*84e872a0SLloyd Pique static enum wl_iterator_result
free_zombies(void * element,void * data,uint32_t flags)602*84e872a0SLloyd Pique free_zombies(void *element, void *data, uint32_t flags)
603*84e872a0SLloyd Pique {
604*84e872a0SLloyd Pique 	if (flags & WL_MAP_ENTRY_ZOMBIE)
605*84e872a0SLloyd Pique 		free(element);
606*84e872a0SLloyd Pique 
607*84e872a0SLloyd Pique 	return WL_ITERATOR_CONTINUE;
608*84e872a0SLloyd Pique }
609*84e872a0SLloyd Pique 
610*84e872a0SLloyd Pique static struct wl_proxy *
proxy_create(struct wl_proxy * factory,const struct wl_interface * interface,uint32_t version)611*84e872a0SLloyd Pique proxy_create(struct wl_proxy *factory, const struct wl_interface *interface,
612*84e872a0SLloyd Pique 	     uint32_t version)
613*84e872a0SLloyd Pique {
614*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
615*84e872a0SLloyd Pique 	struct wl_display *display = factory->display;
616*84e872a0SLloyd Pique 
617*84e872a0SLloyd Pique 	proxy = zalloc(sizeof *proxy);
618*84e872a0SLloyd Pique 	if (proxy == NULL)
619*84e872a0SLloyd Pique 		return NULL;
620*84e872a0SLloyd Pique 
621*84e872a0SLloyd Pique 	proxy->object.interface = interface;
622*84e872a0SLloyd Pique 	proxy->display = display;
623*84e872a0SLloyd Pique 	proxy->queue = factory->queue;
624*84e872a0SLloyd Pique 	proxy->refcount = 1;
625*84e872a0SLloyd Pique 	proxy->version = version;
626*84e872a0SLloyd Pique 
627*84e872a0SLloyd Pique 	proxy->object.id = wl_map_insert_new(&display->objects, 0, proxy);
628*84e872a0SLloyd Pique 	if (proxy->object.id == 0) {
629*84e872a0SLloyd Pique 		free(proxy);
630*84e872a0SLloyd Pique 		return NULL;
631*84e872a0SLloyd Pique 	}
632*84e872a0SLloyd Pique 
633*84e872a0SLloyd Pique 	wl_list_insert(&proxy->queue->proxy_list, &proxy->queue_link);
634*84e872a0SLloyd Pique 
635*84e872a0SLloyd Pique 	return proxy;
636*84e872a0SLloyd Pique }
637*84e872a0SLloyd Pique 
638*84e872a0SLloyd Pique /** Create a proxy object with a given interface
639*84e872a0SLloyd Pique  *
640*84e872a0SLloyd Pique  * \param factory Factory proxy object
641*84e872a0SLloyd Pique  * \param interface Interface the proxy object should use
642*84e872a0SLloyd Pique  * \return A newly allocated proxy object or NULL on failure
643*84e872a0SLloyd Pique  *
644*84e872a0SLloyd Pique  * This function creates a new proxy object with the supplied interface. The
645*84e872a0SLloyd Pique  * proxy object will have an id assigned from the client id space. The id
646*84e872a0SLloyd Pique  * should be created on the compositor side by sending an appropriate request
647*84e872a0SLloyd Pique  * with \ref wl_proxy_marshal().
648*84e872a0SLloyd Pique  *
649*84e872a0SLloyd Pique  * The proxy will inherit the display and event queue of the factory object.
650*84e872a0SLloyd Pique  *
651*84e872a0SLloyd Pique  * \note This should not normally be used by non-generated code.
652*84e872a0SLloyd Pique  *
653*84e872a0SLloyd Pique  * \sa wl_display, wl_event_queue, wl_proxy_marshal()
654*84e872a0SLloyd Pique  *
655*84e872a0SLloyd Pique  * \memberof wl_proxy
656*84e872a0SLloyd Pique  */
657*84e872a0SLloyd Pique WL_EXPORT struct wl_proxy *
wl_proxy_create(struct wl_proxy * factory,const struct wl_interface * interface)658*84e872a0SLloyd Pique wl_proxy_create(struct wl_proxy *factory, const struct wl_interface *interface)
659*84e872a0SLloyd Pique {
660*84e872a0SLloyd Pique 	struct wl_display *display = factory->display;
661*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
662*84e872a0SLloyd Pique 
663*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
664*84e872a0SLloyd Pique 	proxy = proxy_create(factory, interface, factory->version);
665*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
666*84e872a0SLloyd Pique 
667*84e872a0SLloyd Pique 	return proxy;
668*84e872a0SLloyd Pique }
669*84e872a0SLloyd Pique 
670*84e872a0SLloyd Pique /* The caller should hold the display lock */
671*84e872a0SLloyd Pique static struct wl_proxy *
wl_proxy_create_for_id(struct wl_proxy * factory,uint32_t id,const struct wl_interface * interface)672*84e872a0SLloyd Pique wl_proxy_create_for_id(struct wl_proxy *factory,
673*84e872a0SLloyd Pique 		       uint32_t id, const struct wl_interface *interface)
674*84e872a0SLloyd Pique {
675*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
676*84e872a0SLloyd Pique 	struct wl_display *display = factory->display;
677*84e872a0SLloyd Pique 
678*84e872a0SLloyd Pique 	proxy = zalloc(sizeof *proxy);
679*84e872a0SLloyd Pique 	if (proxy == NULL)
680*84e872a0SLloyd Pique 		return NULL;
681*84e872a0SLloyd Pique 
682*84e872a0SLloyd Pique 	proxy->object.interface = interface;
683*84e872a0SLloyd Pique 	proxy->object.id = id;
684*84e872a0SLloyd Pique 	proxy->display = display;
685*84e872a0SLloyd Pique 	proxy->queue = factory->queue;
686*84e872a0SLloyd Pique 	proxy->refcount = 1;
687*84e872a0SLloyd Pique 	proxy->version = factory->version;
688*84e872a0SLloyd Pique 
689*84e872a0SLloyd Pique 	if (wl_map_insert_at(&display->objects, 0, id, proxy) == -1) {
690*84e872a0SLloyd Pique 		free(proxy);
691*84e872a0SLloyd Pique 		return NULL;
692*84e872a0SLloyd Pique 	}
693*84e872a0SLloyd Pique 
694*84e872a0SLloyd Pique 	wl_list_insert(&proxy->queue->proxy_list, &proxy->queue_link);
695*84e872a0SLloyd Pique 
696*84e872a0SLloyd Pique 	return proxy;
697*84e872a0SLloyd Pique }
698*84e872a0SLloyd Pique 
699*84e872a0SLloyd Pique static void
proxy_destroy(struct wl_proxy * proxy)700*84e872a0SLloyd Pique proxy_destroy(struct wl_proxy *proxy)
701*84e872a0SLloyd Pique {
702*84e872a0SLloyd Pique 	if (proxy->flags & WL_PROXY_FLAG_ID_DELETED) {
703*84e872a0SLloyd Pique 		wl_map_remove(&proxy->display->objects, proxy->object.id);
704*84e872a0SLloyd Pique 	} else if (proxy->object.id < WL_SERVER_ID_START) {
705*84e872a0SLloyd Pique 		struct wl_zombie *zombie = prepare_zombie(proxy);
706*84e872a0SLloyd Pique 
707*84e872a0SLloyd Pique 		/* The map now contains the zombie entry, until the delete_id
708*84e872a0SLloyd Pique 		 * event arrives. */
709*84e872a0SLloyd Pique 		wl_map_insert_at(&proxy->display->objects,
710*84e872a0SLloyd Pique 				 WL_MAP_ENTRY_ZOMBIE,
711*84e872a0SLloyd Pique 				 proxy->object.id,
712*84e872a0SLloyd Pique 				 zombie);
713*84e872a0SLloyd Pique 	} else {
714*84e872a0SLloyd Pique 		wl_map_insert_at(&proxy->display->objects, 0,
715*84e872a0SLloyd Pique 				 proxy->object.id, NULL);
716*84e872a0SLloyd Pique 	}
717*84e872a0SLloyd Pique 
718*84e872a0SLloyd Pique 	proxy->flags |= WL_PROXY_FLAG_DESTROYED;
719*84e872a0SLloyd Pique 
720*84e872a0SLloyd Pique 	proxy->queue = NULL;
721*84e872a0SLloyd Pique 	wl_list_remove(&proxy->queue_link);
722*84e872a0SLloyd Pique 	wl_list_init(&proxy->queue_link);
723*84e872a0SLloyd Pique 
724*84e872a0SLloyd Pique 	wl_proxy_unref(proxy);
725*84e872a0SLloyd Pique }
726*84e872a0SLloyd Pique 
727*84e872a0SLloyd Pique static void
wl_proxy_destroy_caller_locks(struct wl_proxy * proxy)728*84e872a0SLloyd Pique wl_proxy_destroy_caller_locks(struct wl_proxy *proxy)
729*84e872a0SLloyd Pique {
730*84e872a0SLloyd Pique 	if (proxy->flags & WL_PROXY_FLAG_WRAPPER)
731*84e872a0SLloyd Pique 		wl_abort("Tried to destroy wrapper with wl_proxy_destroy()\n");
732*84e872a0SLloyd Pique 
733*84e872a0SLloyd Pique 	proxy_destroy(proxy);
734*84e872a0SLloyd Pique }
735*84e872a0SLloyd Pique 
736*84e872a0SLloyd Pique /** Destroy a proxy object
737*84e872a0SLloyd Pique  *
738*84e872a0SLloyd Pique  * \param proxy The proxy to be destroyed
739*84e872a0SLloyd Pique  *
740*84e872a0SLloyd Pique  * \c proxy must not be a proxy wrapper.
741*84e872a0SLloyd Pique  *
742*84e872a0SLloyd Pique  * \note This function will abort in response to egregious
743*84e872a0SLloyd Pique  * errors, and will do so with the display lock held. This means
744*84e872a0SLloyd Pique  * SIGABRT handlers must not perform any actions that would
745*84e872a0SLloyd Pique  * attempt to take that lock, or a deadlock would occur.
746*84e872a0SLloyd Pique  *
747*84e872a0SLloyd Pique  * \memberof wl_proxy
748*84e872a0SLloyd Pique  */
749*84e872a0SLloyd Pique WL_EXPORT void
wl_proxy_destroy(struct wl_proxy * proxy)750*84e872a0SLloyd Pique wl_proxy_destroy(struct wl_proxy *proxy)
751*84e872a0SLloyd Pique {
752*84e872a0SLloyd Pique 	struct wl_display *display = proxy->display;
753*84e872a0SLloyd Pique 
754*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
755*84e872a0SLloyd Pique 
756*84e872a0SLloyd Pique 	wl_proxy_destroy_caller_locks(proxy);
757*84e872a0SLloyd Pique 
758*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
759*84e872a0SLloyd Pique }
760*84e872a0SLloyd Pique 
761*84e872a0SLloyd Pique /** Set a proxy's listener
762*84e872a0SLloyd Pique  *
763*84e872a0SLloyd Pique  * \param proxy The proxy object
764*84e872a0SLloyd Pique  * \param implementation The listener to be added to proxy
765*84e872a0SLloyd Pique  * \param data User data to be associated with the proxy
766*84e872a0SLloyd Pique  * \return 0 on success or -1 on failure
767*84e872a0SLloyd Pique  *
768*84e872a0SLloyd Pique  * Set proxy's listener to \c implementation and its user data to
769*84e872a0SLloyd Pique  * \c data. If a listener has already been set, this function
770*84e872a0SLloyd Pique  * fails and nothing is changed.
771*84e872a0SLloyd Pique  *
772*84e872a0SLloyd Pique  * \c implementation is a vector of function pointers. For an opcode
773*84e872a0SLloyd Pique  * \c n, \c implementation[n] should point to the handler of \c n for
774*84e872a0SLloyd Pique  * the given object.
775*84e872a0SLloyd Pique  *
776*84e872a0SLloyd Pique  * \c proxy must not be a proxy wrapper.
777*84e872a0SLloyd Pique  *
778*84e872a0SLloyd Pique  * \memberof wl_proxy
779*84e872a0SLloyd Pique  */
780*84e872a0SLloyd Pique WL_EXPORT int
wl_proxy_add_listener(struct wl_proxy * proxy,void (** implementation)(void),void * data)781*84e872a0SLloyd Pique wl_proxy_add_listener(struct wl_proxy *proxy,
782*84e872a0SLloyd Pique 		      void (**implementation)(void), void *data)
783*84e872a0SLloyd Pique {
784*84e872a0SLloyd Pique 	if (proxy->flags & WL_PROXY_FLAG_WRAPPER)
785*84e872a0SLloyd Pique 		wl_abort("Proxy %p is a wrapper\n", proxy);
786*84e872a0SLloyd Pique 
787*84e872a0SLloyd Pique 	if (proxy->object.implementation || proxy->dispatcher) {
788*84e872a0SLloyd Pique 		wl_log("proxy %p already has listener\n", proxy);
789*84e872a0SLloyd Pique 		return -1;
790*84e872a0SLloyd Pique 	}
791*84e872a0SLloyd Pique 
792*84e872a0SLloyd Pique 	proxy->object.implementation = implementation;
793*84e872a0SLloyd Pique 	proxy->user_data = data;
794*84e872a0SLloyd Pique 
795*84e872a0SLloyd Pique 	return 0;
796*84e872a0SLloyd Pique }
797*84e872a0SLloyd Pique 
798*84e872a0SLloyd Pique /** Get a proxy's listener
799*84e872a0SLloyd Pique  *
800*84e872a0SLloyd Pique  * \param proxy The proxy object
801*84e872a0SLloyd Pique  * \return The address of the proxy's listener or NULL if no listener is set
802*84e872a0SLloyd Pique  *
803*84e872a0SLloyd Pique  * Gets the address to the proxy's listener; which is the listener set with
804*84e872a0SLloyd Pique  * \ref wl_proxy_add_listener.
805*84e872a0SLloyd Pique  *
806*84e872a0SLloyd Pique  * This function is useful in clients with multiple listeners on the same
807*84e872a0SLloyd Pique  * interface to allow the identification of which code to execute.
808*84e872a0SLloyd Pique  *
809*84e872a0SLloyd Pique  * \memberof wl_proxy
810*84e872a0SLloyd Pique  */
811*84e872a0SLloyd Pique WL_EXPORT const void *
wl_proxy_get_listener(struct wl_proxy * proxy)812*84e872a0SLloyd Pique wl_proxy_get_listener(struct wl_proxy *proxy)
813*84e872a0SLloyd Pique {
814*84e872a0SLloyd Pique 	return proxy->object.implementation;
815*84e872a0SLloyd Pique }
816*84e872a0SLloyd Pique 
817*84e872a0SLloyd Pique /** Set a proxy's listener (with dispatcher)
818*84e872a0SLloyd Pique  *
819*84e872a0SLloyd Pique  * \param proxy The proxy object
820*84e872a0SLloyd Pique  * \param dispatcher The dispatcher to be used for this proxy
821*84e872a0SLloyd Pique  * \param implementation The dispatcher-specific listener implementation
822*84e872a0SLloyd Pique  * \param data User data to be associated with the proxy
823*84e872a0SLloyd Pique  * \return 0 on success or -1 on failure
824*84e872a0SLloyd Pique  *
825*84e872a0SLloyd Pique  * Set proxy's listener to use \c dispatcher_func as its dispatcher and \c
826*84e872a0SLloyd Pique  * dispatcher_data as its dispatcher-specific implementation and its user data
827*84e872a0SLloyd Pique  * to \c data. If a listener has already been set, this function
828*84e872a0SLloyd Pique  * fails and nothing is changed.
829*84e872a0SLloyd Pique  *
830*84e872a0SLloyd Pique  * The exact details of dispatcher_data depend on the dispatcher used.  This
831*84e872a0SLloyd Pique  * function is intended to be used by language bindings, not user code.
832*84e872a0SLloyd Pique  *
833*84e872a0SLloyd Pique  * \c proxy must not be a proxy wrapper.
834*84e872a0SLloyd Pique  *
835*84e872a0SLloyd Pique  * \memberof wl_proxy
836*84e872a0SLloyd Pique  */
837*84e872a0SLloyd Pique WL_EXPORT int
wl_proxy_add_dispatcher(struct wl_proxy * proxy,wl_dispatcher_func_t dispatcher,const void * implementation,void * data)838*84e872a0SLloyd Pique wl_proxy_add_dispatcher(struct wl_proxy *proxy,
839*84e872a0SLloyd Pique 			wl_dispatcher_func_t dispatcher,
840*84e872a0SLloyd Pique 			const void *implementation, void *data)
841*84e872a0SLloyd Pique {
842*84e872a0SLloyd Pique 	if (proxy->flags & WL_PROXY_FLAG_WRAPPER)
843*84e872a0SLloyd Pique 		wl_abort("Proxy %p is a wrapper\n", proxy);
844*84e872a0SLloyd Pique 
845*84e872a0SLloyd Pique 	if (proxy->object.implementation || proxy->dispatcher) {
846*84e872a0SLloyd Pique 		wl_log("proxy %p already has listener\n", proxy);
847*84e872a0SLloyd Pique 		return -1;
848*84e872a0SLloyd Pique 	}
849*84e872a0SLloyd Pique 
850*84e872a0SLloyd Pique 	proxy->object.implementation = implementation;
851*84e872a0SLloyd Pique 	proxy->dispatcher = dispatcher;
852*84e872a0SLloyd Pique 	proxy->user_data = data;
853*84e872a0SLloyd Pique 
854*84e872a0SLloyd Pique 	return 0;
855*84e872a0SLloyd Pique }
856*84e872a0SLloyd Pique 
857*84e872a0SLloyd Pique static struct wl_proxy *
create_outgoing_proxy(struct wl_proxy * proxy,const struct wl_message * message,union wl_argument * args,const struct wl_interface * interface,uint32_t version)858*84e872a0SLloyd Pique create_outgoing_proxy(struct wl_proxy *proxy, const struct wl_message *message,
859*84e872a0SLloyd Pique 		      union wl_argument *args,
860*84e872a0SLloyd Pique 		      const struct wl_interface *interface, uint32_t version)
861*84e872a0SLloyd Pique {
862*84e872a0SLloyd Pique 	int i, count;
863*84e872a0SLloyd Pique 	const char *signature;
864*84e872a0SLloyd Pique 	struct argument_details arg;
865*84e872a0SLloyd Pique 	struct wl_proxy *new_proxy = NULL;
866*84e872a0SLloyd Pique 
867*84e872a0SLloyd Pique 	signature = message->signature;
868*84e872a0SLloyd Pique 	count = arg_count_for_signature(signature);
869*84e872a0SLloyd Pique 	for (i = 0; i < count; i++) {
870*84e872a0SLloyd Pique 		signature = get_next_argument(signature, &arg);
871*84e872a0SLloyd Pique 
872*84e872a0SLloyd Pique 		switch (arg.type) {
873*84e872a0SLloyd Pique 		case 'n':
874*84e872a0SLloyd Pique 			new_proxy = proxy_create(proxy, interface, version);
875*84e872a0SLloyd Pique 			if (new_proxy == NULL)
876*84e872a0SLloyd Pique 				return NULL;
877*84e872a0SLloyd Pique 
878*84e872a0SLloyd Pique 			args[i].o = &new_proxy->object;
879*84e872a0SLloyd Pique 			break;
880*84e872a0SLloyd Pique 		}
881*84e872a0SLloyd Pique 	}
882*84e872a0SLloyd Pique 
883*84e872a0SLloyd Pique 	return new_proxy;
884*84e872a0SLloyd Pique }
885*84e872a0SLloyd Pique 
886*84e872a0SLloyd Pique /** Prepare a request to be sent to the compositor
887*84e872a0SLloyd Pique  *
888*84e872a0SLloyd Pique  * \param proxy The proxy object
889*84e872a0SLloyd Pique  * \param opcode Opcode of the request to be sent
890*84e872a0SLloyd Pique  * \param args Extra arguments for the given request
891*84e872a0SLloyd Pique  * \param interface The interface to use for the new proxy
892*84e872a0SLloyd Pique  *
893*84e872a0SLloyd Pique  * This function translates a request given an opcode, an interface and a
894*84e872a0SLloyd Pique  * wl_argument array to the wire format and writes it to the connection
895*84e872a0SLloyd Pique  * buffer.
896*84e872a0SLloyd Pique  *
897*84e872a0SLloyd Pique  * For new-id arguments, this function will allocate a new wl_proxy
898*84e872a0SLloyd Pique  * and send the ID to the server.  The new wl_proxy will be returned
899*84e872a0SLloyd Pique  * on success or NULL on error with errno set accordingly.  The newly
900*84e872a0SLloyd Pique  * created proxy will inherit their version from their parent.
901*84e872a0SLloyd Pique  *
902*84e872a0SLloyd Pique  * \note This is intended to be used by language bindings and not in
903*84e872a0SLloyd Pique  * non-generated code.
904*84e872a0SLloyd Pique  *
905*84e872a0SLloyd Pique  * \sa wl_proxy_marshal()
906*84e872a0SLloyd Pique  *
907*84e872a0SLloyd Pique  * \memberof wl_proxy
908*84e872a0SLloyd Pique  */
909*84e872a0SLloyd Pique WL_EXPORT struct wl_proxy *
wl_proxy_marshal_array_constructor(struct wl_proxy * proxy,uint32_t opcode,union wl_argument * args,const struct wl_interface * interface)910*84e872a0SLloyd Pique wl_proxy_marshal_array_constructor(struct wl_proxy *proxy,
911*84e872a0SLloyd Pique 				   uint32_t opcode, union wl_argument *args,
912*84e872a0SLloyd Pique 				   const struct wl_interface *interface)
913*84e872a0SLloyd Pique {
914*84e872a0SLloyd Pique 	return wl_proxy_marshal_array_constructor_versioned(proxy, opcode,
915*84e872a0SLloyd Pique 							    args, interface,
916*84e872a0SLloyd Pique 							    proxy->version);
917*84e872a0SLloyd Pique }
918*84e872a0SLloyd Pique 
919*84e872a0SLloyd Pique 
920*84e872a0SLloyd Pique /** Prepare a request to be sent to the compositor
921*84e872a0SLloyd Pique  *
922*84e872a0SLloyd Pique  * \param proxy The proxy object
923*84e872a0SLloyd Pique  * \param opcode Opcode of the request to be sent
924*84e872a0SLloyd Pique  * \param args Extra arguments for the given request
925*84e872a0SLloyd Pique  * \param interface The interface to use for the new proxy
926*84e872a0SLloyd Pique  * \param version The protocol object version for the new proxy
927*84e872a0SLloyd Pique  *
928*84e872a0SLloyd Pique  * Translates the request given by opcode and the extra arguments into the
929*84e872a0SLloyd Pique  * wire format and write it to the connection buffer.  This version takes an
930*84e872a0SLloyd Pique  * array of the union type wl_argument.
931*84e872a0SLloyd Pique  *
932*84e872a0SLloyd Pique  * For new-id arguments, this function will allocate a new wl_proxy
933*84e872a0SLloyd Pique  * and send the ID to the server.  The new wl_proxy will be returned
934*84e872a0SLloyd Pique  * on success or NULL on error with errno set accordingly.  The newly
935*84e872a0SLloyd Pique  * created proxy will have the version specified.
936*84e872a0SLloyd Pique  *
937*84e872a0SLloyd Pique  * \note This is intended to be used by language bindings and not in
938*84e872a0SLloyd Pique  * non-generated code.
939*84e872a0SLloyd Pique  *
940*84e872a0SLloyd Pique  * \sa wl_proxy_marshal()
941*84e872a0SLloyd Pique  *
942*84e872a0SLloyd Pique  * \memberof wl_proxy
943*84e872a0SLloyd Pique  */
944*84e872a0SLloyd Pique WL_EXPORT struct wl_proxy *
wl_proxy_marshal_array_constructor_versioned(struct wl_proxy * proxy,uint32_t opcode,union wl_argument * args,const struct wl_interface * interface,uint32_t version)945*84e872a0SLloyd Pique wl_proxy_marshal_array_constructor_versioned(struct wl_proxy *proxy,
946*84e872a0SLloyd Pique 					     uint32_t opcode,
947*84e872a0SLloyd Pique 					     union wl_argument *args,
948*84e872a0SLloyd Pique 					     const struct wl_interface *interface,
949*84e872a0SLloyd Pique 					     uint32_t version)
950*84e872a0SLloyd Pique {
951*84e872a0SLloyd Pique 	return wl_proxy_marshal_array_flags(proxy, opcode, interface, version, 0, args);
952*84e872a0SLloyd Pique }
953*84e872a0SLloyd Pique 
954*84e872a0SLloyd Pique /** Prepare a request to be sent to the compositor
955*84e872a0SLloyd Pique  *
956*84e872a0SLloyd Pique  * \param proxy The proxy object
957*84e872a0SLloyd Pique  * \param opcode Opcode of the request to be sent
958*84e872a0SLloyd Pique  * \param interface The interface to use for the new proxy
959*84e872a0SLloyd Pique  * \param version The protocol object version of the new proxy
960*84e872a0SLloyd Pique  * \param flags Flags that modify marshalling behaviour
961*84e872a0SLloyd Pique  * \param ... Extra arguments for the given request
962*84e872a0SLloyd Pique  * \return A new wl_proxy for the new_id argument or NULL on error
963*84e872a0SLloyd Pique  *
964*84e872a0SLloyd Pique  * Translates the request given by opcode and the extra arguments into the
965*84e872a0SLloyd Pique  * wire format and write it to the connection buffer.
966*84e872a0SLloyd Pique  *
967*84e872a0SLloyd Pique  * For new-id arguments, this function will allocate a new wl_proxy
968*84e872a0SLloyd Pique  * and send the ID to the server.  The new wl_proxy will be returned
969*84e872a0SLloyd Pique  * on success or NULL on error with errno set accordingly.  The newly
970*84e872a0SLloyd Pique  * created proxy will have the version specified.
971*84e872a0SLloyd Pique  *
972*84e872a0SLloyd Pique  * The flag WL_MARSHAL_FLAG_DESTROY may be passed to ensure the proxy
973*84e872a0SLloyd Pique  * is destroyed atomically with the marshalling in order to prevent
974*84e872a0SLloyd Pique  * races that can occur if the display lock is dropped between the
975*84e872a0SLloyd Pique  * marshal and destroy operations.
976*84e872a0SLloyd Pique  *
977*84e872a0SLloyd Pique  * \note This should not normally be used by non-generated code.
978*84e872a0SLloyd Pique  *
979*84e872a0SLloyd Pique  * \memberof wl_proxy
980*84e872a0SLloyd Pique  */
981*84e872a0SLloyd Pique WL_EXPORT struct wl_proxy *
wl_proxy_marshal_flags(struct wl_proxy * proxy,uint32_t opcode,const struct wl_interface * interface,uint32_t version,uint32_t flags,...)982*84e872a0SLloyd Pique wl_proxy_marshal_flags(struct wl_proxy *proxy, uint32_t opcode,
983*84e872a0SLloyd Pique 		       const struct wl_interface *interface, uint32_t version,
984*84e872a0SLloyd Pique 		       uint32_t flags, ...)
985*84e872a0SLloyd Pique {
986*84e872a0SLloyd Pique 	union wl_argument args[WL_CLOSURE_MAX_ARGS];
987*84e872a0SLloyd Pique 	va_list ap;
988*84e872a0SLloyd Pique 
989*84e872a0SLloyd Pique 	va_start(ap, flags);
990*84e872a0SLloyd Pique 	wl_argument_from_va_list(proxy->object.interface->methods[opcode].signature,
991*84e872a0SLloyd Pique 				 args, WL_CLOSURE_MAX_ARGS, ap);
992*84e872a0SLloyd Pique 	va_end(ap);
993*84e872a0SLloyd Pique 
994*84e872a0SLloyd Pique 	return wl_proxy_marshal_array_flags(proxy, opcode, interface, version, flags, args);
995*84e872a0SLloyd Pique }
996*84e872a0SLloyd Pique 
997*84e872a0SLloyd Pique /** Prepare a request to be sent to the compositor
998*84e872a0SLloyd Pique  *
999*84e872a0SLloyd Pique  * \param proxy The proxy object
1000*84e872a0SLloyd Pique  * \param opcode Opcode of the request to be sent
1001*84e872a0SLloyd Pique  * \param interface The interface to use for the new proxy
1002*84e872a0SLloyd Pique  * \param version The protocol object version for the new proxy
1003*84e872a0SLloyd Pique  * \param flags Flags that modify marshalling behaviour
1004*84e872a0SLloyd Pique  * \param args Extra arguments for the given request
1005*84e872a0SLloyd Pique  *
1006*84e872a0SLloyd Pique  * Translates the request given by opcode and the extra arguments into the
1007*84e872a0SLloyd Pique  * wire format and write it to the connection buffer.  This version takes an
1008*84e872a0SLloyd Pique  * array of the union type wl_argument.
1009*84e872a0SLloyd Pique  *
1010*84e872a0SLloyd Pique  * For new-id arguments, this function will allocate a new wl_proxy
1011*84e872a0SLloyd Pique  * and send the ID to the server.  The new wl_proxy will be returned
1012*84e872a0SLloyd Pique  * on success or NULL on error with errno set accordingly.  The newly
1013*84e872a0SLloyd Pique  * created proxy will have the version specified.
1014*84e872a0SLloyd Pique  *
1015*84e872a0SLloyd Pique  * The flag WL_MARSHAL_FLAG_DESTROY may be passed to ensure the proxy
1016*84e872a0SLloyd Pique  * is destroyed atomically with the marshalling in order to prevent
1017*84e872a0SLloyd Pique  * races that can occur if the display lock is dropped between the
1018*84e872a0SLloyd Pique  * marshal and destroy operations.
1019*84e872a0SLloyd Pique  *
1020*84e872a0SLloyd Pique  * \note This is intended to be used by language bindings and not in
1021*84e872a0SLloyd Pique  * non-generated code.
1022*84e872a0SLloyd Pique  *
1023*84e872a0SLloyd Pique  * \sa wl_proxy_marshal_flags()
1024*84e872a0SLloyd Pique  *
1025*84e872a0SLloyd Pique  * \memberof wl_proxy
1026*84e872a0SLloyd Pique  */
1027*84e872a0SLloyd Pique WL_EXPORT struct wl_proxy *
wl_proxy_marshal_array_flags(struct wl_proxy * proxy,uint32_t opcode,const struct wl_interface * interface,uint32_t version,uint32_t flags,union wl_argument * args)1028*84e872a0SLloyd Pique wl_proxy_marshal_array_flags(struct wl_proxy *proxy, uint32_t opcode,
1029*84e872a0SLloyd Pique 			     const struct wl_interface *interface, uint32_t version,
1030*84e872a0SLloyd Pique 			     uint32_t flags, union wl_argument *args)
1031*84e872a0SLloyd Pique {
1032*84e872a0SLloyd Pique 	struct wl_closure *closure;
1033*84e872a0SLloyd Pique 	struct wl_proxy *new_proxy = NULL;
1034*84e872a0SLloyd Pique 	const struct wl_message *message;
1035*84e872a0SLloyd Pique 	struct wl_display *disp = proxy->display;
1036*84e872a0SLloyd Pique 
1037*84e872a0SLloyd Pique 	pthread_mutex_lock(&disp->mutex);
1038*84e872a0SLloyd Pique 
1039*84e872a0SLloyd Pique 	message = &proxy->object.interface->methods[opcode];
1040*84e872a0SLloyd Pique 	if (interface) {
1041*84e872a0SLloyd Pique 		new_proxy = create_outgoing_proxy(proxy, message,
1042*84e872a0SLloyd Pique 						  args, interface,
1043*84e872a0SLloyd Pique 						  version);
1044*84e872a0SLloyd Pique 		if (new_proxy == NULL)
1045*84e872a0SLloyd Pique 			goto err_unlock;
1046*84e872a0SLloyd Pique 	}
1047*84e872a0SLloyd Pique 
1048*84e872a0SLloyd Pique 	if (proxy->display->last_error) {
1049*84e872a0SLloyd Pique 		goto err_unlock;
1050*84e872a0SLloyd Pique 	}
1051*84e872a0SLloyd Pique 
1052*84e872a0SLloyd Pique 	closure = wl_closure_marshal(&proxy->object, opcode, args, message);
1053*84e872a0SLloyd Pique 	if (closure == NULL) {
1054*84e872a0SLloyd Pique 		wl_log("Error marshalling request: %s\n", strerror(errno));
1055*84e872a0SLloyd Pique 		display_fatal_error(proxy->display, errno);
1056*84e872a0SLloyd Pique 		goto err_unlock;
1057*84e872a0SLloyd Pique 	}
1058*84e872a0SLloyd Pique 
1059*84e872a0SLloyd Pique 	closure_log(closure, proxy, true, WL_CLIENT_MESSAGE_NOT_DISCARDED);
1060*84e872a0SLloyd Pique 
1061*84e872a0SLloyd Pique 	if (wl_closure_send(closure, proxy->display->connection)) {
1062*84e872a0SLloyd Pique 		wl_log("Error sending request: %s\n", strerror(errno));
1063*84e872a0SLloyd Pique 		display_fatal_error(proxy->display, errno);
1064*84e872a0SLloyd Pique 	}
1065*84e872a0SLloyd Pique 
1066*84e872a0SLloyd Pique 	wl_closure_destroy(closure);
1067*84e872a0SLloyd Pique 
1068*84e872a0SLloyd Pique  err_unlock:
1069*84e872a0SLloyd Pique 	if (flags & WL_MARSHAL_FLAG_DESTROY)
1070*84e872a0SLloyd Pique 		wl_proxy_destroy_caller_locks(proxy);
1071*84e872a0SLloyd Pique 
1072*84e872a0SLloyd Pique 	pthread_mutex_unlock(&disp->mutex);
1073*84e872a0SLloyd Pique 
1074*84e872a0SLloyd Pique 	return new_proxy;
1075*84e872a0SLloyd Pique }
1076*84e872a0SLloyd Pique 
1077*84e872a0SLloyd Pique 
1078*84e872a0SLloyd Pique /** Prepare a request to be sent to the compositor
1079*84e872a0SLloyd Pique  *
1080*84e872a0SLloyd Pique  * \param proxy The proxy object
1081*84e872a0SLloyd Pique  * \param opcode Opcode of the request to be sent
1082*84e872a0SLloyd Pique  * \param ... Extra arguments for the given request
1083*84e872a0SLloyd Pique  *
1084*84e872a0SLloyd Pique  * This function is similar to wl_proxy_marshal_constructor(), except
1085*84e872a0SLloyd Pique  * it doesn't create proxies for new-id arguments.
1086*84e872a0SLloyd Pique  *
1087*84e872a0SLloyd Pique  * \note This should not normally be used by non-generated code.
1088*84e872a0SLloyd Pique  *
1089*84e872a0SLloyd Pique  * \sa wl_proxy_create()
1090*84e872a0SLloyd Pique  *
1091*84e872a0SLloyd Pique  * \memberof wl_proxy
1092*84e872a0SLloyd Pique  */
1093*84e872a0SLloyd Pique WL_EXPORT void
wl_proxy_marshal(struct wl_proxy * proxy,uint32_t opcode,...)1094*84e872a0SLloyd Pique wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
1095*84e872a0SLloyd Pique {
1096*84e872a0SLloyd Pique 	union wl_argument args[WL_CLOSURE_MAX_ARGS];
1097*84e872a0SLloyd Pique 	va_list ap;
1098*84e872a0SLloyd Pique 
1099*84e872a0SLloyd Pique 	va_start(ap, opcode);
1100*84e872a0SLloyd Pique 	wl_argument_from_va_list(proxy->object.interface->methods[opcode].signature,
1101*84e872a0SLloyd Pique 				 args, WL_CLOSURE_MAX_ARGS, ap);
1102*84e872a0SLloyd Pique 	va_end(ap);
1103*84e872a0SLloyd Pique 
1104*84e872a0SLloyd Pique 	wl_proxy_marshal_array_constructor(proxy, opcode, args, NULL);
1105*84e872a0SLloyd Pique }
1106*84e872a0SLloyd Pique 
1107*84e872a0SLloyd Pique /** Prepare a request to be sent to the compositor
1108*84e872a0SLloyd Pique  *
1109*84e872a0SLloyd Pique  * \param proxy The proxy object
1110*84e872a0SLloyd Pique  * \param opcode Opcode of the request to be sent
1111*84e872a0SLloyd Pique  * \param interface The interface to use for the new proxy
1112*84e872a0SLloyd Pique  * \param ... Extra arguments for the given request
1113*84e872a0SLloyd Pique  * \return A new wl_proxy for the new_id argument or NULL on error
1114*84e872a0SLloyd Pique  *
1115*84e872a0SLloyd Pique  * This function translates a request given an opcode, an interface and extra
1116*84e872a0SLloyd Pique  * arguments to the wire format and writes it to the connection buffer. The
1117*84e872a0SLloyd Pique  * types of the extra arguments must correspond to the argument types of the
1118*84e872a0SLloyd Pique  * method associated with the opcode in the interface.
1119*84e872a0SLloyd Pique  *
1120*84e872a0SLloyd Pique  * For new-id arguments, this function will allocate a new wl_proxy
1121*84e872a0SLloyd Pique  * and send the ID to the server.  The new wl_proxy will be returned
1122*84e872a0SLloyd Pique  * on success or NULL on error with errno set accordingly.  The newly
1123*84e872a0SLloyd Pique  * created proxy will inherit their version from their parent.
1124*84e872a0SLloyd Pique  *
1125*84e872a0SLloyd Pique  * \note This should not normally be used by non-generated code.
1126*84e872a0SLloyd Pique  *
1127*84e872a0SLloyd Pique  * \memberof wl_proxy
1128*84e872a0SLloyd Pique  */
1129*84e872a0SLloyd Pique WL_EXPORT struct wl_proxy *
wl_proxy_marshal_constructor(struct wl_proxy * proxy,uint32_t opcode,const struct wl_interface * interface,...)1130*84e872a0SLloyd Pique wl_proxy_marshal_constructor(struct wl_proxy *proxy, uint32_t opcode,
1131*84e872a0SLloyd Pique 			     const struct wl_interface *interface, ...)
1132*84e872a0SLloyd Pique {
1133*84e872a0SLloyd Pique 	union wl_argument args[WL_CLOSURE_MAX_ARGS];
1134*84e872a0SLloyd Pique 	va_list ap;
1135*84e872a0SLloyd Pique 
1136*84e872a0SLloyd Pique 	va_start(ap, interface);
1137*84e872a0SLloyd Pique 	wl_argument_from_va_list(proxy->object.interface->methods[opcode].signature,
1138*84e872a0SLloyd Pique 				 args, WL_CLOSURE_MAX_ARGS, ap);
1139*84e872a0SLloyd Pique 	va_end(ap);
1140*84e872a0SLloyd Pique 
1141*84e872a0SLloyd Pique 	return wl_proxy_marshal_array_constructor(proxy, opcode,
1142*84e872a0SLloyd Pique 						  args, interface);
1143*84e872a0SLloyd Pique }
1144*84e872a0SLloyd Pique 
1145*84e872a0SLloyd Pique 
1146*84e872a0SLloyd Pique /** Prepare a request to be sent to the compositor
1147*84e872a0SLloyd Pique  *
1148*84e872a0SLloyd Pique  * \param proxy The proxy object
1149*84e872a0SLloyd Pique  * \param opcode Opcode of the request to be sent
1150*84e872a0SLloyd Pique  * \param interface The interface to use for the new proxy
1151*84e872a0SLloyd Pique  * \param version The protocol object version of the new proxy
1152*84e872a0SLloyd Pique  * \param ... Extra arguments for the given request
1153*84e872a0SLloyd Pique  * \return A new wl_proxy for the new_id argument or NULL on error
1154*84e872a0SLloyd Pique  *
1155*84e872a0SLloyd Pique  * Translates the request given by opcode and the extra arguments into the
1156*84e872a0SLloyd Pique  * wire format and write it to the connection buffer.
1157*84e872a0SLloyd Pique  *
1158*84e872a0SLloyd Pique  * For new-id arguments, this function will allocate a new wl_proxy
1159*84e872a0SLloyd Pique  * and send the ID to the server.  The new wl_proxy will be returned
1160*84e872a0SLloyd Pique  * on success or NULL on error with errno set accordingly.  The newly
1161*84e872a0SLloyd Pique  * created proxy will have the version specified.
1162*84e872a0SLloyd Pique  *
1163*84e872a0SLloyd Pique  * \note This should not normally be used by non-generated code.
1164*84e872a0SLloyd Pique  *
1165*84e872a0SLloyd Pique  * \memberof wl_proxy
1166*84e872a0SLloyd Pique  */
1167*84e872a0SLloyd Pique WL_EXPORT struct wl_proxy *
wl_proxy_marshal_constructor_versioned(struct wl_proxy * proxy,uint32_t opcode,const struct wl_interface * interface,uint32_t version,...)1168*84e872a0SLloyd Pique wl_proxy_marshal_constructor_versioned(struct wl_proxy *proxy, uint32_t opcode,
1169*84e872a0SLloyd Pique 				       const struct wl_interface *interface,
1170*84e872a0SLloyd Pique 				       uint32_t version, ...)
1171*84e872a0SLloyd Pique {
1172*84e872a0SLloyd Pique 	union wl_argument args[WL_CLOSURE_MAX_ARGS];
1173*84e872a0SLloyd Pique 	va_list ap;
1174*84e872a0SLloyd Pique 
1175*84e872a0SLloyd Pique 	va_start(ap, version);
1176*84e872a0SLloyd Pique 	wl_argument_from_va_list(proxy->object.interface->methods[opcode].signature,
1177*84e872a0SLloyd Pique 				 args, WL_CLOSURE_MAX_ARGS, ap);
1178*84e872a0SLloyd Pique 	va_end(ap);
1179*84e872a0SLloyd Pique 
1180*84e872a0SLloyd Pique 	return wl_proxy_marshal_array_constructor_versioned(proxy, opcode,
1181*84e872a0SLloyd Pique 							    args, interface,
1182*84e872a0SLloyd Pique 							    version);
1183*84e872a0SLloyd Pique }
1184*84e872a0SLloyd Pique 
1185*84e872a0SLloyd Pique /** Prepare a request to be sent to the compositor
1186*84e872a0SLloyd Pique  *
1187*84e872a0SLloyd Pique  * \param proxy The proxy object
1188*84e872a0SLloyd Pique  * \param opcode Opcode of the request to be sent
1189*84e872a0SLloyd Pique  * \param args Extra arguments for the given request
1190*84e872a0SLloyd Pique  *
1191*84e872a0SLloyd Pique  * This function is similar to wl_proxy_marshal_array_constructor(), except
1192*84e872a0SLloyd Pique  * it doesn't create proxies for new-id arguments.
1193*84e872a0SLloyd Pique  *
1194*84e872a0SLloyd Pique  * \note This is intended to be used by language bindings and not in
1195*84e872a0SLloyd Pique  * non-generated code.
1196*84e872a0SLloyd Pique  *
1197*84e872a0SLloyd Pique  * \sa wl_proxy_marshal()
1198*84e872a0SLloyd Pique  *
1199*84e872a0SLloyd Pique  * \memberof wl_proxy
1200*84e872a0SLloyd Pique  */
1201*84e872a0SLloyd Pique WL_EXPORT void
wl_proxy_marshal_array(struct wl_proxy * proxy,uint32_t opcode,union wl_argument * args)1202*84e872a0SLloyd Pique wl_proxy_marshal_array(struct wl_proxy *proxy, uint32_t opcode,
1203*84e872a0SLloyd Pique 		       union wl_argument *args)
1204*84e872a0SLloyd Pique {
1205*84e872a0SLloyd Pique 	wl_proxy_marshal_array_constructor(proxy, opcode, args, NULL);
1206*84e872a0SLloyd Pique }
1207*84e872a0SLloyd Pique 
1208*84e872a0SLloyd Pique static void
display_handle_error(void * data,struct wl_display * display,void * object,uint32_t code,const char * message)1209*84e872a0SLloyd Pique display_handle_error(void *data,
1210*84e872a0SLloyd Pique 		     struct wl_display *display, void *object,
1211*84e872a0SLloyd Pique 		     uint32_t code, const char *message)
1212*84e872a0SLloyd Pique {
1213*84e872a0SLloyd Pique 	struct wl_proxy *proxy = object;
1214*84e872a0SLloyd Pique 	uint32_t object_id;
1215*84e872a0SLloyd Pique 	const struct wl_interface *interface;
1216*84e872a0SLloyd Pique 
1217*84e872a0SLloyd Pique 	if (proxy) {
1218*84e872a0SLloyd Pique 		wl_log("%s@%u: error %d: %s\n",
1219*84e872a0SLloyd Pique 		       proxy->object.interface->name,
1220*84e872a0SLloyd Pique 		       proxy->object.id,
1221*84e872a0SLloyd Pique 		       code, message);
1222*84e872a0SLloyd Pique 
1223*84e872a0SLloyd Pique 		object_id = proxy->object.id;
1224*84e872a0SLloyd Pique 		interface = proxy->object.interface;
1225*84e872a0SLloyd Pique 	} else {
1226*84e872a0SLloyd Pique 		wl_log("[destroyed object]: error %d: %s\n",
1227*84e872a0SLloyd Pique 		       code, message);
1228*84e872a0SLloyd Pique 
1229*84e872a0SLloyd Pique 		object_id = 0;
1230*84e872a0SLloyd Pique 		interface = NULL;
1231*84e872a0SLloyd Pique 	}
1232*84e872a0SLloyd Pique 
1233*84e872a0SLloyd Pique 	display_protocol_error(display, code, object_id, interface);
1234*84e872a0SLloyd Pique }
1235*84e872a0SLloyd Pique 
1236*84e872a0SLloyd Pique static void
display_handle_delete_id(void * data,struct wl_display * display,uint32_t id)1237*84e872a0SLloyd Pique display_handle_delete_id(void *data, struct wl_display *display, uint32_t id)
1238*84e872a0SLloyd Pique {
1239*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
1240*84e872a0SLloyd Pique 
1241*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
1242*84e872a0SLloyd Pique 
1243*84e872a0SLloyd Pique 	proxy = wl_map_lookup(&display->objects, id);
1244*84e872a0SLloyd Pique 
1245*84e872a0SLloyd Pique 	if (wl_object_is_zombie(&display->objects, id)) {
1246*84e872a0SLloyd Pique 		/* For zombie objects, the 'proxy' is actually the zombie
1247*84e872a0SLloyd Pique 		 * event-information structure, which we can free. */
1248*84e872a0SLloyd Pique 		free(proxy);
1249*84e872a0SLloyd Pique 		wl_map_remove(&display->objects, id);
1250*84e872a0SLloyd Pique 	} else if (proxy) {
1251*84e872a0SLloyd Pique 		proxy->flags |= WL_PROXY_FLAG_ID_DELETED;
1252*84e872a0SLloyd Pique 	} else {
1253*84e872a0SLloyd Pique 		wl_log("error: received delete_id for unknown id (%u)\n", id);
1254*84e872a0SLloyd Pique 	}
1255*84e872a0SLloyd Pique 
1256*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
1257*84e872a0SLloyd Pique }
1258*84e872a0SLloyd Pique 
1259*84e872a0SLloyd Pique static const struct wl_display_listener display_listener = {
1260*84e872a0SLloyd Pique 	display_handle_error,
1261*84e872a0SLloyd Pique 	display_handle_delete_id
1262*84e872a0SLloyd Pique };
1263*84e872a0SLloyd Pique 
1264*84e872a0SLloyd Pique static int
connect_to_socket(const char * name)1265*84e872a0SLloyd Pique connect_to_socket(const char *name)
1266*84e872a0SLloyd Pique {
1267*84e872a0SLloyd Pique 	struct sockaddr_un addr;
1268*84e872a0SLloyd Pique 	socklen_t size;
1269*84e872a0SLloyd Pique 	const char *runtime_dir;
1270*84e872a0SLloyd Pique 	int name_size, fd;
1271*84e872a0SLloyd Pique 	bool path_is_absolute;
1272*84e872a0SLloyd Pique 
1273*84e872a0SLloyd Pique 	if (name == NULL)
1274*84e872a0SLloyd Pique 		name = getenv("WAYLAND_DISPLAY");
1275*84e872a0SLloyd Pique 	if (name == NULL)
1276*84e872a0SLloyd Pique 		name = "wayland-0";
1277*84e872a0SLloyd Pique 
1278*84e872a0SLloyd Pique 	path_is_absolute = name[0] == '/';
1279*84e872a0SLloyd Pique 
1280*84e872a0SLloyd Pique 	runtime_dir = getenv("XDG_RUNTIME_DIR");
1281*84e872a0SLloyd Pique 	if (((!runtime_dir || runtime_dir[0] != '/') && !path_is_absolute)) {
1282*84e872a0SLloyd Pique 		wl_log("error: XDG_RUNTIME_DIR is invalid or not set in the environment.\n");
1283*84e872a0SLloyd Pique 		/* to prevent programs reporting
1284*84e872a0SLloyd Pique 		 * "failed to create display: Success" */
1285*84e872a0SLloyd Pique 		errno = ENOENT;
1286*84e872a0SLloyd Pique 		return -1;
1287*84e872a0SLloyd Pique 	}
1288*84e872a0SLloyd Pique 
1289*84e872a0SLloyd Pique 	fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
1290*84e872a0SLloyd Pique 	if (fd < 0)
1291*84e872a0SLloyd Pique 		return -1;
1292*84e872a0SLloyd Pique 
1293*84e872a0SLloyd Pique 	memset(&addr, 0, sizeof addr);
1294*84e872a0SLloyd Pique 	addr.sun_family = AF_LOCAL;
1295*84e872a0SLloyd Pique 	if (!path_is_absolute) {
1296*84e872a0SLloyd Pique 		name_size =
1297*84e872a0SLloyd Pique 			snprintf(addr.sun_path, sizeof addr.sun_path,
1298*84e872a0SLloyd Pique 			         "%s/%s", runtime_dir, name) + 1;
1299*84e872a0SLloyd Pique 	} else {
1300*84e872a0SLloyd Pique 		/* absolute path */
1301*84e872a0SLloyd Pique 		name_size =
1302*84e872a0SLloyd Pique 			snprintf(addr.sun_path, sizeof addr.sun_path,
1303*84e872a0SLloyd Pique 			         "%s", name) + 1;
1304*84e872a0SLloyd Pique 	}
1305*84e872a0SLloyd Pique 
1306*84e872a0SLloyd Pique 	assert(name_size > 0);
1307*84e872a0SLloyd Pique 	if (name_size > (int)sizeof addr.sun_path) {
1308*84e872a0SLloyd Pique 		if (!path_is_absolute) {
1309*84e872a0SLloyd Pique 			wl_log("error: socket path \"%s/%s\" plus null terminator"
1310*84e872a0SLloyd Pique 			       " exceeds %i bytes\n", runtime_dir, name, (int) sizeof(addr.sun_path));
1311*84e872a0SLloyd Pique 		} else {
1312*84e872a0SLloyd Pique 			wl_log("error: socket path \"%s\" plus null terminator"
1313*84e872a0SLloyd Pique 			       " exceeds %i bytes\n", name, (int) sizeof(addr.sun_path));
1314*84e872a0SLloyd Pique 		}
1315*84e872a0SLloyd Pique 		close(fd);
1316*84e872a0SLloyd Pique 		/* to prevent programs reporting
1317*84e872a0SLloyd Pique 		 * "failed to add socket: Success" */
1318*84e872a0SLloyd Pique 		errno = ENAMETOOLONG;
1319*84e872a0SLloyd Pique 		return -1;
1320*84e872a0SLloyd Pique 	};
1321*84e872a0SLloyd Pique 
1322*84e872a0SLloyd Pique 	size = offsetof (struct sockaddr_un, sun_path) + name_size;
1323*84e872a0SLloyd Pique 
1324*84e872a0SLloyd Pique 	if (connect(fd, (struct sockaddr *) &addr, size) < 0) {
1325*84e872a0SLloyd Pique 		close(fd);
1326*84e872a0SLloyd Pique 		return -1;
1327*84e872a0SLloyd Pique 	}
1328*84e872a0SLloyd Pique 
1329*84e872a0SLloyd Pique 	return fd;
1330*84e872a0SLloyd Pique }
1331*84e872a0SLloyd Pique 
1332*84e872a0SLloyd Pique /** Connect to Wayland display on an already open fd
1333*84e872a0SLloyd Pique  *
1334*84e872a0SLloyd Pique  * \param fd The fd to use for the connection
1335*84e872a0SLloyd Pique  * \return A \ref wl_display object or \c NULL on failure
1336*84e872a0SLloyd Pique  *
1337*84e872a0SLloyd Pique  * The wl_display takes ownership of the fd and will close it when the
1338*84e872a0SLloyd Pique  * display is destroyed.  The fd will also be closed in case of
1339*84e872a0SLloyd Pique  * failure.
1340*84e872a0SLloyd Pique  *
1341*84e872a0SLloyd Pique  * \memberof wl_display
1342*84e872a0SLloyd Pique  */
1343*84e872a0SLloyd Pique WL_EXPORT struct wl_display *
wl_display_connect_to_fd(int fd)1344*84e872a0SLloyd Pique wl_display_connect_to_fd(int fd)
1345*84e872a0SLloyd Pique {
1346*84e872a0SLloyd Pique 	struct wl_display *display;
1347*84e872a0SLloyd Pique 	const char *debug;
1348*84e872a0SLloyd Pique 
1349*84e872a0SLloyd Pique 	debug = getenv("WAYLAND_DEBUG");
1350*84e872a0SLloyd Pique 	if (debug && (strstr(debug, "client") || strstr(debug, "1")))
1351*84e872a0SLloyd Pique 		debug_client = 1;
1352*84e872a0SLloyd Pique 
1353*84e872a0SLloyd Pique 	display = zalloc(sizeof *display);
1354*84e872a0SLloyd Pique 	if (display == NULL) {
1355*84e872a0SLloyd Pique 		close(fd);
1356*84e872a0SLloyd Pique 		return NULL;
1357*84e872a0SLloyd Pique 	}
1358*84e872a0SLloyd Pique 
1359*84e872a0SLloyd Pique 	display->fd = fd;
1360*84e872a0SLloyd Pique 	wl_map_init(&display->objects, WL_MAP_CLIENT_SIDE);
1361*84e872a0SLloyd Pique 	wl_event_queue_init(&display->default_queue, display);
1362*84e872a0SLloyd Pique 	wl_event_queue_init(&display->display_queue, display);
1363*84e872a0SLloyd Pique 	pthread_mutex_init(&display->mutex, NULL);
1364*84e872a0SLloyd Pique 	pthread_cond_init(&display->reader_cond, NULL);
1365*84e872a0SLloyd Pique 	display->reader_count = 0;
1366*84e872a0SLloyd Pique 	wl_list_init(&display->observers);
1367*84e872a0SLloyd Pique 
1368*84e872a0SLloyd Pique 	if (wl_map_insert_at(&display->objects, 0, 0, NULL) == -1)
1369*84e872a0SLloyd Pique 		goto err_connection;
1370*84e872a0SLloyd Pique 
1371*84e872a0SLloyd Pique 	display->proxy.object.id =
1372*84e872a0SLloyd Pique 		wl_map_insert_new(&display->objects, 0, display);
1373*84e872a0SLloyd Pique 
1374*84e872a0SLloyd Pique 	if (display->proxy.object.id == 0)
1375*84e872a0SLloyd Pique 		goto err_connection;
1376*84e872a0SLloyd Pique 
1377*84e872a0SLloyd Pique 	display->proxy.object.interface = &wl_display_interface;
1378*84e872a0SLloyd Pique 	display->proxy.display = display;
1379*84e872a0SLloyd Pique 	display->proxy.object.implementation = (void(**)(void)) &display_listener;
1380*84e872a0SLloyd Pique 	display->proxy.user_data = display;
1381*84e872a0SLloyd Pique 	display->proxy.queue = &display->default_queue;
1382*84e872a0SLloyd Pique 	display->proxy.flags = 0;
1383*84e872a0SLloyd Pique 	display->proxy.refcount = 1;
1384*84e872a0SLloyd Pique 
1385*84e872a0SLloyd Pique 	/* We set this version to 0 for backwards compatibility.
1386*84e872a0SLloyd Pique 	 *
1387*84e872a0SLloyd Pique 	 * If a client is using old versions of protocol headers,
1388*84e872a0SLloyd Pique 	 * it will use unversioned API to create proxies.  Those
1389*84e872a0SLloyd Pique 	 * proxies will inherit this 0.
1390*84e872a0SLloyd Pique 	 *
1391*84e872a0SLloyd Pique 	 * A client could be passing these proxies into library
1392*84e872a0SLloyd Pique 	 * code newer than the headers that checks proxy
1393*84e872a0SLloyd Pique 	 * versions.  When the proxy version is reported as 0
1394*84e872a0SLloyd Pique 	 * the library will know that it can't reliably determine
1395*84e872a0SLloyd Pique 	 * the proxy version, and should do whatever fallback is
1396*84e872a0SLloyd Pique 	 * required.
1397*84e872a0SLloyd Pique 	 *
1398*84e872a0SLloyd Pique 	 * This trick forces wl_display to always report 0, but
1399*84e872a0SLloyd Pique 	 * since it's a special object that we can't bind
1400*84e872a0SLloyd Pique 	 * specific versions of anyway, this should be fine.
1401*84e872a0SLloyd Pique 	 */
1402*84e872a0SLloyd Pique 	display->proxy.version = 0;
1403*84e872a0SLloyd Pique 
1404*84e872a0SLloyd Pique 	display->connection = wl_connection_create(display->fd);
1405*84e872a0SLloyd Pique 	if (display->connection == NULL)
1406*84e872a0SLloyd Pique 		goto err_connection;
1407*84e872a0SLloyd Pique 
1408*84e872a0SLloyd Pique 	return display;
1409*84e872a0SLloyd Pique 
1410*84e872a0SLloyd Pique  err_connection:
1411*84e872a0SLloyd Pique 	pthread_mutex_destroy(&display->mutex);
1412*84e872a0SLloyd Pique 	pthread_cond_destroy(&display->reader_cond);
1413*84e872a0SLloyd Pique 	wl_map_release(&display->objects);
1414*84e872a0SLloyd Pique 	close(display->fd);
1415*84e872a0SLloyd Pique 	free(display);
1416*84e872a0SLloyd Pique 
1417*84e872a0SLloyd Pique 	return NULL;
1418*84e872a0SLloyd Pique }
1419*84e872a0SLloyd Pique 
1420*84e872a0SLloyd Pique /** Connect to a Wayland display
1421*84e872a0SLloyd Pique  *
1422*84e872a0SLloyd Pique  * \param name Name of the Wayland display to connect to
1423*84e872a0SLloyd Pique  * \return A \ref wl_display object or \c NULL on failure
1424*84e872a0SLloyd Pique  *
1425*84e872a0SLloyd Pique  * Connect to the Wayland display named \c name. If \c name is \c NULL,
1426*84e872a0SLloyd Pique  * its value will be replaced with the WAYLAND_DISPLAY environment
1427*84e872a0SLloyd Pique  * variable if it is set, otherwise display "wayland-0" will be used.
1428*84e872a0SLloyd Pique  *
1429*84e872a0SLloyd Pique  * If WAYLAND_SOCKET is set, it's interpreted as a file descriptor number
1430*84e872a0SLloyd Pique  * referring to an already opened socket. In this case, the socket is used
1431*84e872a0SLloyd Pique  * as-is and \c name is ignored.
1432*84e872a0SLloyd Pique  *
1433*84e872a0SLloyd Pique  * If \c name is a relative path, then the socket is opened relative to
1434*84e872a0SLloyd Pique  * the XDG_RUNTIME_DIR directory.
1435*84e872a0SLloyd Pique  *
1436*84e872a0SLloyd Pique  * If \c name is an absolute path, then that path is used as-is for
1437*84e872a0SLloyd Pique  * the location of the socket at which the Wayland server is listening;
1438*84e872a0SLloyd Pique  * no qualification inside XDG_RUNTIME_DIR is attempted.
1439*84e872a0SLloyd Pique  *
1440*84e872a0SLloyd Pique  * If \c name is \c NULL and the WAYLAND_DISPLAY environment variable
1441*84e872a0SLloyd Pique  * is set to an absolute pathname, then that pathname is used as-is
1442*84e872a0SLloyd Pique  * for the socket in the same manner as if \c name held an absolute
1443*84e872a0SLloyd Pique  * path. Support for absolute paths in \c name and WAYLAND_DISPLAY
1444*84e872a0SLloyd Pique  * is present since Wayland version 1.15.
1445*84e872a0SLloyd Pique  *
1446*84e872a0SLloyd Pique  * \memberof wl_display
1447*84e872a0SLloyd Pique  */
1448*84e872a0SLloyd Pique WL_EXPORT struct wl_display *
wl_display_connect(const char * name)1449*84e872a0SLloyd Pique wl_display_connect(const char *name)
1450*84e872a0SLloyd Pique {
1451*84e872a0SLloyd Pique 	char *connection, *end;
1452*84e872a0SLloyd Pique 	int flags, fd;
1453*84e872a0SLloyd Pique 
1454*84e872a0SLloyd Pique 	connection = getenv("WAYLAND_SOCKET");
1455*84e872a0SLloyd Pique 	if (connection) {
1456*84e872a0SLloyd Pique 		int prev_errno = errno;
1457*84e872a0SLloyd Pique 		errno = 0;
1458*84e872a0SLloyd Pique 		fd = strtol(connection, &end, 10);
1459*84e872a0SLloyd Pique 		if (errno != 0 || connection == end || *end != '\0')
1460*84e872a0SLloyd Pique 			return NULL;
1461*84e872a0SLloyd Pique 		errno = prev_errno;
1462*84e872a0SLloyd Pique 
1463*84e872a0SLloyd Pique 		flags = fcntl(fd, F_GETFD);
1464*84e872a0SLloyd Pique 		if (flags == -1 && errno == EBADF)
1465*84e872a0SLloyd Pique 			return NULL;
1466*84e872a0SLloyd Pique 		else if (flags != -1)
1467*84e872a0SLloyd Pique 			fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
1468*84e872a0SLloyd Pique 		unsetenv("WAYLAND_SOCKET");
1469*84e872a0SLloyd Pique 	} else {
1470*84e872a0SLloyd Pique 		fd = connect_to_socket(name);
1471*84e872a0SLloyd Pique 		if (fd < 0)
1472*84e872a0SLloyd Pique 			return NULL;
1473*84e872a0SLloyd Pique 	}
1474*84e872a0SLloyd Pique 
1475*84e872a0SLloyd Pique 	return wl_display_connect_to_fd(fd);
1476*84e872a0SLloyd Pique }
1477*84e872a0SLloyd Pique 
1478*84e872a0SLloyd Pique /** Close a connection to a Wayland display
1479*84e872a0SLloyd Pique  *
1480*84e872a0SLloyd Pique  * \param display The display context object
1481*84e872a0SLloyd Pique  *
1482*84e872a0SLloyd Pique  * Close the connection to \c display. The \ref wl_proxy and
1483*84e872a0SLloyd Pique  * \ref wl_event_queue objects need to be manually destroyed by the caller
1484*84e872a0SLloyd Pique  * before disconnecting.
1485*84e872a0SLloyd Pique  *
1486*84e872a0SLloyd Pique  * \memberof wl_display
1487*84e872a0SLloyd Pique  */
1488*84e872a0SLloyd Pique WL_EXPORT void
wl_display_disconnect(struct wl_display * display)1489*84e872a0SLloyd Pique wl_display_disconnect(struct wl_display *display)
1490*84e872a0SLloyd Pique {
1491*84e872a0SLloyd Pique 	wl_connection_destroy(display->connection);
1492*84e872a0SLloyd Pique 	wl_map_for_each(&display->objects, free_zombies, NULL);
1493*84e872a0SLloyd Pique 	wl_map_release(&display->objects);
1494*84e872a0SLloyd Pique 	wl_event_queue_release(&display->default_queue);
1495*84e872a0SLloyd Pique 	wl_event_queue_release(&display->display_queue);
1496*84e872a0SLloyd Pique 	wl_list_remove(&display->observers);
1497*84e872a0SLloyd Pique 	pthread_mutex_destroy(&display->mutex);
1498*84e872a0SLloyd Pique 	pthread_cond_destroy(&display->reader_cond);
1499*84e872a0SLloyd Pique 	close(display->fd);
1500*84e872a0SLloyd Pique 
1501*84e872a0SLloyd Pique 	free(display);
1502*84e872a0SLloyd Pique }
1503*84e872a0SLloyd Pique 
1504*84e872a0SLloyd Pique /** Get a display context's file descriptor
1505*84e872a0SLloyd Pique  *
1506*84e872a0SLloyd Pique  * \param display The display context object
1507*84e872a0SLloyd Pique  * \return Display object file descriptor
1508*84e872a0SLloyd Pique  *
1509*84e872a0SLloyd Pique  * Return the file descriptor associated with a display so it can be
1510*84e872a0SLloyd Pique  * integrated into the client's main loop.
1511*84e872a0SLloyd Pique  *
1512*84e872a0SLloyd Pique  * \memberof wl_display
1513*84e872a0SLloyd Pique  */
1514*84e872a0SLloyd Pique WL_EXPORT int
wl_display_get_fd(struct wl_display * display)1515*84e872a0SLloyd Pique wl_display_get_fd(struct wl_display *display)
1516*84e872a0SLloyd Pique {
1517*84e872a0SLloyd Pique 	return display->fd;
1518*84e872a0SLloyd Pique }
1519*84e872a0SLloyd Pique 
1520*84e872a0SLloyd Pique static void
sync_callback(void * data,struct wl_callback * callback,uint32_t serial)1521*84e872a0SLloyd Pique sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
1522*84e872a0SLloyd Pique {
1523*84e872a0SLloyd Pique 	int *done = data;
1524*84e872a0SLloyd Pique 
1525*84e872a0SLloyd Pique 	*done = 1;
1526*84e872a0SLloyd Pique 	wl_callback_destroy(callback);
1527*84e872a0SLloyd Pique }
1528*84e872a0SLloyd Pique 
1529*84e872a0SLloyd Pique static const struct wl_callback_listener sync_listener = {
1530*84e872a0SLloyd Pique 	sync_callback
1531*84e872a0SLloyd Pique };
1532*84e872a0SLloyd Pique 
1533*84e872a0SLloyd Pique /** Block until all pending request are processed by the server
1534*84e872a0SLloyd Pique  *
1535*84e872a0SLloyd Pique  * \param display The display context object
1536*84e872a0SLloyd Pique  * \param queue The queue on which to run the roundtrip
1537*84e872a0SLloyd Pique  * \return The number of dispatched events on success or -1 on failure
1538*84e872a0SLloyd Pique  *
1539*84e872a0SLloyd Pique  * This function blocks until the server has processed all currently issued
1540*84e872a0SLloyd Pique  * requests by sending a request to the display server and waiting for a
1541*84e872a0SLloyd Pique  * reply before returning.
1542*84e872a0SLloyd Pique  *
1543*84e872a0SLloyd Pique  * This function uses wl_display_dispatch_queue() internally. It is not allowed
1544*84e872a0SLloyd Pique  * to call this function while the thread is being prepared for reading events,
1545*84e872a0SLloyd Pique  * and doing so will cause a dead lock.
1546*84e872a0SLloyd Pique  *
1547*84e872a0SLloyd Pique  * \note This function may dispatch other events being received on the given
1548*84e872a0SLloyd Pique  * queue.
1549*84e872a0SLloyd Pique  *
1550*84e872a0SLloyd Pique  * \sa wl_display_roundtrip()
1551*84e872a0SLloyd Pique  * \memberof wl_display
1552*84e872a0SLloyd Pique  */
1553*84e872a0SLloyd Pique WL_EXPORT int
wl_display_roundtrip_queue(struct wl_display * display,struct wl_event_queue * queue)1554*84e872a0SLloyd Pique wl_display_roundtrip_queue(struct wl_display *display, struct wl_event_queue *queue)
1555*84e872a0SLloyd Pique {
1556*84e872a0SLloyd Pique 	struct wl_display *display_wrapper;
1557*84e872a0SLloyd Pique 	struct wl_callback *callback;
1558*84e872a0SLloyd Pique 	int done, ret = 0;
1559*84e872a0SLloyd Pique 
1560*84e872a0SLloyd Pique 	done = 0;
1561*84e872a0SLloyd Pique 
1562*84e872a0SLloyd Pique 	display_wrapper = wl_proxy_create_wrapper(display);
1563*84e872a0SLloyd Pique 	if (!display_wrapper)
1564*84e872a0SLloyd Pique 		return -1;
1565*84e872a0SLloyd Pique 
1566*84e872a0SLloyd Pique 	wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
1567*84e872a0SLloyd Pique 	callback = wl_display_sync(display_wrapper);
1568*84e872a0SLloyd Pique 	wl_proxy_wrapper_destroy(display_wrapper);
1569*84e872a0SLloyd Pique 
1570*84e872a0SLloyd Pique 	if (callback == NULL)
1571*84e872a0SLloyd Pique 		return -1;
1572*84e872a0SLloyd Pique 
1573*84e872a0SLloyd Pique 	wl_callback_add_listener(callback, &sync_listener, &done);
1574*84e872a0SLloyd Pique 	while (!done && ret >= 0)
1575*84e872a0SLloyd Pique 		ret = wl_display_dispatch_queue(display, queue);
1576*84e872a0SLloyd Pique 
1577*84e872a0SLloyd Pique 	if (ret == -1 && !done)
1578*84e872a0SLloyd Pique 		wl_callback_destroy(callback);
1579*84e872a0SLloyd Pique 
1580*84e872a0SLloyd Pique 	return ret;
1581*84e872a0SLloyd Pique }
1582*84e872a0SLloyd Pique 
1583*84e872a0SLloyd Pique /** Block until all pending request are processed by the server
1584*84e872a0SLloyd Pique  *
1585*84e872a0SLloyd Pique  * \param display The display context object
1586*84e872a0SLloyd Pique  * \return The number of dispatched events on success or -1 on failure
1587*84e872a0SLloyd Pique  *
1588*84e872a0SLloyd Pique  * This function blocks until the server has processed all currently issued
1589*84e872a0SLloyd Pique  * requests by sending a request to the display server and waiting for a reply
1590*84e872a0SLloyd Pique  * before returning.
1591*84e872a0SLloyd Pique  *
1592*84e872a0SLloyd Pique  * This function uses wl_display_dispatch_queue() internally. It is not allowed
1593*84e872a0SLloyd Pique  * to call this function while the thread is being prepared for reading events,
1594*84e872a0SLloyd Pique  * and doing so will cause a dead lock.
1595*84e872a0SLloyd Pique  *
1596*84e872a0SLloyd Pique  * \note This function may dispatch other events being received on the default
1597*84e872a0SLloyd Pique  * queue.
1598*84e872a0SLloyd Pique  *
1599*84e872a0SLloyd Pique  * \memberof wl_display
1600*84e872a0SLloyd Pique  */
1601*84e872a0SLloyd Pique WL_EXPORT int
wl_display_roundtrip(struct wl_display * display)1602*84e872a0SLloyd Pique wl_display_roundtrip(struct wl_display *display)
1603*84e872a0SLloyd Pique {
1604*84e872a0SLloyd Pique 	return wl_display_roundtrip_queue(display, &display->default_queue);
1605*84e872a0SLloyd Pique }
1606*84e872a0SLloyd Pique 
1607*84e872a0SLloyd Pique static int
create_proxies(struct wl_proxy * sender,struct wl_closure * closure)1608*84e872a0SLloyd Pique create_proxies(struct wl_proxy *sender, struct wl_closure *closure)
1609*84e872a0SLloyd Pique {
1610*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
1611*84e872a0SLloyd Pique 	const char *signature;
1612*84e872a0SLloyd Pique 	struct argument_details arg;
1613*84e872a0SLloyd Pique 	uint32_t id;
1614*84e872a0SLloyd Pique 	int i;
1615*84e872a0SLloyd Pique 	int count;
1616*84e872a0SLloyd Pique 
1617*84e872a0SLloyd Pique 	signature = closure->message->signature;
1618*84e872a0SLloyd Pique 	count = arg_count_for_signature(signature);
1619*84e872a0SLloyd Pique 	for (i = 0; i < count; i++) {
1620*84e872a0SLloyd Pique 		signature = get_next_argument(signature, &arg);
1621*84e872a0SLloyd Pique 		switch (arg.type) {
1622*84e872a0SLloyd Pique 		case 'n':
1623*84e872a0SLloyd Pique 			id = closure->args[i].n;
1624*84e872a0SLloyd Pique 			if (id == 0) {
1625*84e872a0SLloyd Pique 				closure->args[i].o = NULL;
1626*84e872a0SLloyd Pique 				break;
1627*84e872a0SLloyd Pique 			}
1628*84e872a0SLloyd Pique 			proxy = wl_proxy_create_for_id(sender, id,
1629*84e872a0SLloyd Pique 						       closure->message->types[i]);
1630*84e872a0SLloyd Pique 			if (proxy == NULL)
1631*84e872a0SLloyd Pique 				return -1;
1632*84e872a0SLloyd Pique 			closure->args[i].o = (struct wl_object *)proxy;
1633*84e872a0SLloyd Pique 			break;
1634*84e872a0SLloyd Pique 		default:
1635*84e872a0SLloyd Pique 			break;
1636*84e872a0SLloyd Pique 		}
1637*84e872a0SLloyd Pique 	}
1638*84e872a0SLloyd Pique 
1639*84e872a0SLloyd Pique 	return 0;
1640*84e872a0SLloyd Pique }
1641*84e872a0SLloyd Pique 
1642*84e872a0SLloyd Pique static void
increase_closure_args_refcount(struct wl_closure * closure)1643*84e872a0SLloyd Pique increase_closure_args_refcount(struct wl_closure *closure)
1644*84e872a0SLloyd Pique {
1645*84e872a0SLloyd Pique 	const char *signature;
1646*84e872a0SLloyd Pique 	struct argument_details arg;
1647*84e872a0SLloyd Pique 	int i, count;
1648*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
1649*84e872a0SLloyd Pique 
1650*84e872a0SLloyd Pique 	signature = closure->message->signature;
1651*84e872a0SLloyd Pique 	count = arg_count_for_signature(signature);
1652*84e872a0SLloyd Pique 	for (i = 0; i < count; i++) {
1653*84e872a0SLloyd Pique 		signature = get_next_argument(signature, &arg);
1654*84e872a0SLloyd Pique 		switch (arg.type) {
1655*84e872a0SLloyd Pique 		case 'n':
1656*84e872a0SLloyd Pique 		case 'o':
1657*84e872a0SLloyd Pique 			proxy = (struct wl_proxy *) closure->args[i].o;
1658*84e872a0SLloyd Pique 			if (proxy)
1659*84e872a0SLloyd Pique 				proxy->refcount++;
1660*84e872a0SLloyd Pique 			break;
1661*84e872a0SLloyd Pique 		default:
1662*84e872a0SLloyd Pique 			break;
1663*84e872a0SLloyd Pique 		}
1664*84e872a0SLloyd Pique 	}
1665*84e872a0SLloyd Pique 
1666*84e872a0SLloyd Pique 	closure->proxy->refcount++;
1667*84e872a0SLloyd Pique }
1668*84e872a0SLloyd Pique 
1669*84e872a0SLloyd Pique static int
queue_event(struct wl_display * display,int len)1670*84e872a0SLloyd Pique queue_event(struct wl_display *display, int len)
1671*84e872a0SLloyd Pique {
1672*84e872a0SLloyd Pique 	uint32_t p[2], id;
1673*84e872a0SLloyd Pique 	int opcode, size;
1674*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
1675*84e872a0SLloyd Pique 	struct wl_closure *closure;
1676*84e872a0SLloyd Pique 	const struct wl_message *message;
1677*84e872a0SLloyd Pique 	struct wl_event_queue *queue;
1678*84e872a0SLloyd Pique 	int num_zombie_fds;
1679*84e872a0SLloyd Pique 
1680*84e872a0SLloyd Pique 	wl_connection_copy(display->connection, p, sizeof p);
1681*84e872a0SLloyd Pique 	id = p[0];
1682*84e872a0SLloyd Pique 	opcode = p[1] & 0xffff;
1683*84e872a0SLloyd Pique 	size = p[1] >> 16;
1684*84e872a0SLloyd Pique 	if (len < size)
1685*84e872a0SLloyd Pique 		return 0;
1686*84e872a0SLloyd Pique 
1687*84e872a0SLloyd Pique 	/* If our proxy is gone or a zombie, just eat the event (and any FDs,
1688*84e872a0SLloyd Pique 	 * if applicable). */
1689*84e872a0SLloyd Pique 	proxy = wl_map_lookup(&display->objects, id);
1690*84e872a0SLloyd Pique 	if (!proxy || wl_object_is_zombie(&display->objects, id)) {
1691*84e872a0SLloyd Pique 		struct wl_zombie *zombie = wl_map_lookup(&display->objects, id);
1692*84e872a0SLloyd Pique 		num_zombie_fds = (zombie && opcode < zombie->event_count) ?
1693*84e872a0SLloyd Pique 			zombie->fd_count[opcode] : 0;
1694*84e872a0SLloyd Pique 
1695*84e872a0SLloyd Pique 		log_unknown_message(display, !!zombie, id, opcode,
1696*84e872a0SLloyd Pique 				    num_zombie_fds, size);
1697*84e872a0SLloyd Pique 
1698*84e872a0SLloyd Pique 		if (num_zombie_fds > 0)
1699*84e872a0SLloyd Pique 			wl_connection_close_fds_in(display->connection,
1700*84e872a0SLloyd Pique 						   num_zombie_fds);
1701*84e872a0SLloyd Pique 
1702*84e872a0SLloyd Pique 		wl_connection_consume(display->connection, size);
1703*84e872a0SLloyd Pique 		return size;
1704*84e872a0SLloyd Pique 	}
1705*84e872a0SLloyd Pique 
1706*84e872a0SLloyd Pique 	if (opcode >= proxy->object.interface->event_count) {
1707*84e872a0SLloyd Pique 		wl_log("interface '%s' has no event %u\n",
1708*84e872a0SLloyd Pique 		       proxy->object.interface->name, opcode);
1709*84e872a0SLloyd Pique 		return -1;
1710*84e872a0SLloyd Pique 	}
1711*84e872a0SLloyd Pique 
1712*84e872a0SLloyd Pique 	message = &proxy->object.interface->events[opcode];
1713*84e872a0SLloyd Pique 	closure = wl_connection_demarshal(display->connection, size,
1714*84e872a0SLloyd Pique 					  &display->objects, message);
1715*84e872a0SLloyd Pique 	if (!closure)
1716*84e872a0SLloyd Pique 		return -1;
1717*84e872a0SLloyd Pique 
1718*84e872a0SLloyd Pique 	if (create_proxies(proxy, closure) < 0) {
1719*84e872a0SLloyd Pique 		wl_closure_destroy(closure);
1720*84e872a0SLloyd Pique 		return -1;
1721*84e872a0SLloyd Pique 	}
1722*84e872a0SLloyd Pique 
1723*84e872a0SLloyd Pique 	if (wl_closure_lookup_objects(closure, &display->objects) != 0) {
1724*84e872a0SLloyd Pique 		wl_closure_destroy(closure);
1725*84e872a0SLloyd Pique 		return -1;
1726*84e872a0SLloyd Pique 	}
1727*84e872a0SLloyd Pique 
1728*84e872a0SLloyd Pique 	closure->proxy = proxy;
1729*84e872a0SLloyd Pique 	increase_closure_args_refcount(closure);
1730*84e872a0SLloyd Pique 
1731*84e872a0SLloyd Pique 	if (proxy == &display->proxy)
1732*84e872a0SLloyd Pique 		queue = &display->display_queue;
1733*84e872a0SLloyd Pique 	else
1734*84e872a0SLloyd Pique 		queue = proxy->queue;
1735*84e872a0SLloyd Pique 
1736*84e872a0SLloyd Pique 	if (!queue)
1737*84e872a0SLloyd Pique 		wl_abort("Tried to add event to destroyed queue\n");
1738*84e872a0SLloyd Pique 
1739*84e872a0SLloyd Pique 	wl_list_insert(queue->event_list.prev, &closure->link);
1740*84e872a0SLloyd Pique 
1741*84e872a0SLloyd Pique 	return size;
1742*84e872a0SLloyd Pique }
1743*84e872a0SLloyd Pique 
1744*84e872a0SLloyd Pique static void
dispatch_event(struct wl_display * display,struct wl_event_queue * queue)1745*84e872a0SLloyd Pique dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
1746*84e872a0SLloyd Pique {
1747*84e872a0SLloyd Pique 	struct wl_closure *closure;
1748*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
1749*84e872a0SLloyd Pique 	int opcode;
1750*84e872a0SLloyd Pique 	bool proxy_destroyed;
1751*84e872a0SLloyd Pique 
1752*84e872a0SLloyd Pique 	closure = wl_container_of(queue->event_list.next, closure, link);
1753*84e872a0SLloyd Pique 	wl_list_remove(&closure->link);
1754*84e872a0SLloyd Pique 	opcode = closure->opcode;
1755*84e872a0SLloyd Pique 
1756*84e872a0SLloyd Pique 	/* Verify that the receiving object is still valid by checking if has
1757*84e872a0SLloyd Pique 	 * been destroyed by the application. */
1758*84e872a0SLloyd Pique 	validate_closure_objects(closure);
1759*84e872a0SLloyd Pique 	proxy = closure->proxy;
1760*84e872a0SLloyd Pique 	proxy_destroyed = !!(proxy->flags & WL_PROXY_FLAG_DESTROYED);
1761*84e872a0SLloyd Pique 	if (proxy_destroyed) {
1762*84e872a0SLloyd Pique 		closure_log(closure, proxy, false,
1763*84e872a0SLloyd Pique 			    WL_CLIENT_MESSAGE_DISCARD_DEAD_PROXY_ON_DISPATCH);
1764*84e872a0SLloyd Pique 	} else if (proxy->dispatcher) {
1765*84e872a0SLloyd Pique 		closure_log(closure, proxy, false,
1766*84e872a0SLloyd Pique 			    WL_CLIENT_MESSAGE_NOT_DISCARDED);
1767*84e872a0SLloyd Pique 
1768*84e872a0SLloyd Pique 		pthread_mutex_unlock(&display->mutex);
1769*84e872a0SLloyd Pique 		wl_closure_dispatch(closure, proxy->dispatcher,
1770*84e872a0SLloyd Pique 				    &proxy->object, opcode);
1771*84e872a0SLloyd Pique 		pthread_mutex_lock(&display->mutex);
1772*84e872a0SLloyd Pique 	} else if (proxy->object.implementation) {
1773*84e872a0SLloyd Pique 		closure_log(closure, proxy, false,
1774*84e872a0SLloyd Pique 			    WL_CLIENT_MESSAGE_NOT_DISCARDED);
1775*84e872a0SLloyd Pique 
1776*84e872a0SLloyd Pique 		pthread_mutex_unlock(&display->mutex);
1777*84e872a0SLloyd Pique 		wl_closure_invoke(closure, WL_CLOSURE_INVOKE_CLIENT,
1778*84e872a0SLloyd Pique 				  &proxy->object, opcode, proxy->user_data);
1779*84e872a0SLloyd Pique 		pthread_mutex_lock(&display->mutex);
1780*84e872a0SLloyd Pique 	} else {
1781*84e872a0SLloyd Pique 		closure_log(closure, proxy, false,
1782*84e872a0SLloyd Pique 			    WL_CLIENT_MESSAGE_DISCARD_NO_LISTENER_ON_DISPATCH);
1783*84e872a0SLloyd Pique 	}
1784*84e872a0SLloyd Pique 
1785*84e872a0SLloyd Pique 	destroy_queued_closure(closure);
1786*84e872a0SLloyd Pique }
1787*84e872a0SLloyd Pique 
1788*84e872a0SLloyd Pique static int
read_events(struct wl_display * display)1789*84e872a0SLloyd Pique read_events(struct wl_display *display)
1790*84e872a0SLloyd Pique {
1791*84e872a0SLloyd Pique 	int total, rem, size;
1792*84e872a0SLloyd Pique 	uint32_t serial;
1793*84e872a0SLloyd Pique 
1794*84e872a0SLloyd Pique 	display->reader_count--;
1795*84e872a0SLloyd Pique 	if (display->reader_count == 0) {
1796*84e872a0SLloyd Pique 		total = wl_connection_read(display->connection);
1797*84e872a0SLloyd Pique 		if (total == -1) {
1798*84e872a0SLloyd Pique 			if (errno == EAGAIN) {
1799*84e872a0SLloyd Pique 				/* we must wake up threads whenever
1800*84e872a0SLloyd Pique 				 * the reader_count dropped to 0 */
1801*84e872a0SLloyd Pique 				display_wakeup_threads(display);
1802*84e872a0SLloyd Pique 
1803*84e872a0SLloyd Pique 				return 0;
1804*84e872a0SLloyd Pique 			}
1805*84e872a0SLloyd Pique 
1806*84e872a0SLloyd Pique 			display_fatal_error(display, errno);
1807*84e872a0SLloyd Pique 			return -1;
1808*84e872a0SLloyd Pique 		} else if (total == 0) {
1809*84e872a0SLloyd Pique 			/* The compositor has closed the socket. This
1810*84e872a0SLloyd Pique 			 * should be considered an error so we'll fake
1811*84e872a0SLloyd Pique 			 * an errno */
1812*84e872a0SLloyd Pique 			errno = EPIPE;
1813*84e872a0SLloyd Pique 			display_fatal_error(display, errno);
1814*84e872a0SLloyd Pique 			return -1;
1815*84e872a0SLloyd Pique 		}
1816*84e872a0SLloyd Pique 
1817*84e872a0SLloyd Pique 		for (rem = total; rem >= 8; rem -= size) {
1818*84e872a0SLloyd Pique 			size = queue_event(display, rem);
1819*84e872a0SLloyd Pique 			if (size == -1) {
1820*84e872a0SLloyd Pique 				display_fatal_error(display, errno);
1821*84e872a0SLloyd Pique 				return -1;
1822*84e872a0SLloyd Pique 			} else if (size == 0) {
1823*84e872a0SLloyd Pique 				break;
1824*84e872a0SLloyd Pique 			}
1825*84e872a0SLloyd Pique 		}
1826*84e872a0SLloyd Pique 
1827*84e872a0SLloyd Pique 		display_wakeup_threads(display);
1828*84e872a0SLloyd Pique 	} else {
1829*84e872a0SLloyd Pique 		serial = display->read_serial;
1830*84e872a0SLloyd Pique 		while (display->read_serial == serial)
1831*84e872a0SLloyd Pique 			pthread_cond_wait(&display->reader_cond,
1832*84e872a0SLloyd Pique 					  &display->mutex);
1833*84e872a0SLloyd Pique 
1834*84e872a0SLloyd Pique 		if (display->last_error) {
1835*84e872a0SLloyd Pique 			errno = display->last_error;
1836*84e872a0SLloyd Pique 			return -1;
1837*84e872a0SLloyd Pique 		}
1838*84e872a0SLloyd Pique 	}
1839*84e872a0SLloyd Pique 
1840*84e872a0SLloyd Pique 	return 0;
1841*84e872a0SLloyd Pique }
1842*84e872a0SLloyd Pique 
1843*84e872a0SLloyd Pique static void
cancel_read(struct wl_display * display)1844*84e872a0SLloyd Pique cancel_read(struct wl_display *display)
1845*84e872a0SLloyd Pique {
1846*84e872a0SLloyd Pique 	display->reader_count--;
1847*84e872a0SLloyd Pique 	if (display->reader_count == 0)
1848*84e872a0SLloyd Pique 		display_wakeup_threads(display);
1849*84e872a0SLloyd Pique }
1850*84e872a0SLloyd Pique 
1851*84e872a0SLloyd Pique /** Read events from display file descriptor
1852*84e872a0SLloyd Pique  *
1853*84e872a0SLloyd Pique  * \param display The display context object
1854*84e872a0SLloyd Pique  * \return 0 on success or -1 on error.  In case of error errno will
1855*84e872a0SLloyd Pique  * be set accordingly
1856*84e872a0SLloyd Pique  *
1857*84e872a0SLloyd Pique  * Calling this function will result in data available on the display file
1858*84e872a0SLloyd Pique  * descriptor being read and read events will be queued on their corresponding
1859*84e872a0SLloyd Pique  * event queues.
1860*84e872a0SLloyd Pique  *
1861*84e872a0SLloyd Pique  * Before calling this function, depending on what thread it is to be called
1862*84e872a0SLloyd Pique  * from, wl_display_prepare_read_queue() or wl_display_prepare_read() needs to
1863*84e872a0SLloyd Pique  * be called. See wl_display_prepare_read_queue() for more details.
1864*84e872a0SLloyd Pique  *
1865*84e872a0SLloyd Pique  * When being called at a point where other threads have been prepared to read
1866*84e872a0SLloyd Pique  * (using wl_display_prepare_read_queue() or wl_display_prepare_read()) this
1867*84e872a0SLloyd Pique  * function will sleep until all other prepared threads have either been
1868*84e872a0SLloyd Pique  * cancelled (using wl_display_cancel_read()) or them self entered this
1869*84e872a0SLloyd Pique  * function. The last thread that calls this function will then read and queue
1870*84e872a0SLloyd Pique  * events on their corresponding event queues, and finally wake up all other
1871*84e872a0SLloyd Pique  * wl_display_read_events() calls causing them to return.
1872*84e872a0SLloyd Pique  *
1873*84e872a0SLloyd Pique  * If a thread cancels a read preparation when all other threads that have
1874*84e872a0SLloyd Pique  * prepared to read has either called wl_display_cancel_read() or
1875*84e872a0SLloyd Pique  * wl_display_read_events(), all reader threads will return without having read
1876*84e872a0SLloyd Pique  * any data.
1877*84e872a0SLloyd Pique  *
1878*84e872a0SLloyd Pique  * To dispatch events that may have been queued, call
1879*84e872a0SLloyd Pique  * wl_display_dispatch_pending() or wl_display_dispatch_queue_pending().
1880*84e872a0SLloyd Pique  *
1881*84e872a0SLloyd Pique  * \sa wl_display_prepare_read(), wl_display_cancel_read(),
1882*84e872a0SLloyd Pique  * wl_display_dispatch_pending(), wl_display_dispatch()
1883*84e872a0SLloyd Pique  *
1884*84e872a0SLloyd Pique  * \memberof wl_display
1885*84e872a0SLloyd Pique  */
1886*84e872a0SLloyd Pique WL_EXPORT int
wl_display_read_events(struct wl_display * display)1887*84e872a0SLloyd Pique wl_display_read_events(struct wl_display *display)
1888*84e872a0SLloyd Pique {
1889*84e872a0SLloyd Pique 	int ret;
1890*84e872a0SLloyd Pique 
1891*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
1892*84e872a0SLloyd Pique 
1893*84e872a0SLloyd Pique 	if (display->last_error) {
1894*84e872a0SLloyd Pique 		cancel_read(display);
1895*84e872a0SLloyd Pique 		pthread_mutex_unlock(&display->mutex);
1896*84e872a0SLloyd Pique 
1897*84e872a0SLloyd Pique 		errno = display->last_error;
1898*84e872a0SLloyd Pique 		return -1;
1899*84e872a0SLloyd Pique 	}
1900*84e872a0SLloyd Pique 
1901*84e872a0SLloyd Pique 	ret = read_events(display);
1902*84e872a0SLloyd Pique 
1903*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
1904*84e872a0SLloyd Pique 
1905*84e872a0SLloyd Pique 	return ret;
1906*84e872a0SLloyd Pique }
1907*84e872a0SLloyd Pique 
1908*84e872a0SLloyd Pique static int
dispatch_queue(struct wl_display * display,struct wl_event_queue * queue)1909*84e872a0SLloyd Pique dispatch_queue(struct wl_display *display, struct wl_event_queue *queue)
1910*84e872a0SLloyd Pique {
1911*84e872a0SLloyd Pique 	int count;
1912*84e872a0SLloyd Pique 
1913*84e872a0SLloyd Pique 	if (display->last_error)
1914*84e872a0SLloyd Pique 		goto err;
1915*84e872a0SLloyd Pique 
1916*84e872a0SLloyd Pique 	count = 0;
1917*84e872a0SLloyd Pique 	while (!wl_list_empty(&display->display_queue.event_list)) {
1918*84e872a0SLloyd Pique 		dispatch_event(display, &display->display_queue);
1919*84e872a0SLloyd Pique 		if (display->last_error)
1920*84e872a0SLloyd Pique 			goto err;
1921*84e872a0SLloyd Pique 		count++;
1922*84e872a0SLloyd Pique 	}
1923*84e872a0SLloyd Pique 
1924*84e872a0SLloyd Pique 	while (!wl_list_empty(&queue->event_list)) {
1925*84e872a0SLloyd Pique 		dispatch_event(display, queue);
1926*84e872a0SLloyd Pique 		if (display->last_error)
1927*84e872a0SLloyd Pique 			goto err;
1928*84e872a0SLloyd Pique 		count++;
1929*84e872a0SLloyd Pique 	}
1930*84e872a0SLloyd Pique 
1931*84e872a0SLloyd Pique 	return count;
1932*84e872a0SLloyd Pique 
1933*84e872a0SLloyd Pique err:
1934*84e872a0SLloyd Pique 	errno = display->last_error;
1935*84e872a0SLloyd Pique 
1936*84e872a0SLloyd Pique 	return -1;
1937*84e872a0SLloyd Pique }
1938*84e872a0SLloyd Pique 
1939*84e872a0SLloyd Pique /** Prepare to read events from the display's file descriptor to a queue
1940*84e872a0SLloyd Pique  *
1941*84e872a0SLloyd Pique  * \param display The display context object
1942*84e872a0SLloyd Pique  * \param queue The event queue to use
1943*84e872a0SLloyd Pique  * \return 0 on success or -1 if event queue was not empty
1944*84e872a0SLloyd Pique  *
1945*84e872a0SLloyd Pique  * This function (or wl_display_prepare_read()) must be called before reading
1946*84e872a0SLloyd Pique  * from the file descriptor using wl_display_read_events(). Calling
1947*84e872a0SLloyd Pique  * wl_display_prepare_read_queue() announces the calling thread's intention to
1948*84e872a0SLloyd Pique  * read and ensures that until the thread is ready to read and calls
1949*84e872a0SLloyd Pique  * wl_display_read_events(), no other thread will read from the file descriptor.
1950*84e872a0SLloyd Pique  * This only succeeds if the event queue is empty, and if not -1 is returned and
1951*84e872a0SLloyd Pique  * errno set to EAGAIN.
1952*84e872a0SLloyd Pique  *
1953*84e872a0SLloyd Pique  * If a thread successfully calls wl_display_prepare_read_queue(), it must
1954*84e872a0SLloyd Pique  * either call wl_display_read_events() when it's ready or cancel the read
1955*84e872a0SLloyd Pique  * intention by calling wl_display_cancel_read().
1956*84e872a0SLloyd Pique  *
1957*84e872a0SLloyd Pique  * Use this function before polling on the display fd or integrate the fd into a
1958*84e872a0SLloyd Pique  * toolkit event loop in a race-free way. A correct usage would be (with most
1959*84e872a0SLloyd Pique  * error checking left out):
1960*84e872a0SLloyd Pique  *
1961*84e872a0SLloyd Pique  * \code
1962*84e872a0SLloyd Pique  * while (wl_display_prepare_read_queue(display, queue) != 0)
1963*84e872a0SLloyd Pique  *         wl_display_dispatch_queue_pending(display, queue);
1964*84e872a0SLloyd Pique  * wl_display_flush(display);
1965*84e872a0SLloyd Pique  *
1966*84e872a0SLloyd Pique  * ret = poll(fds, nfds, -1);
1967*84e872a0SLloyd Pique  * if (has_error(ret))
1968*84e872a0SLloyd Pique  *         wl_display_cancel_read(display);
1969*84e872a0SLloyd Pique  * else
1970*84e872a0SLloyd Pique  *         wl_display_read_events(display);
1971*84e872a0SLloyd Pique  *
1972*84e872a0SLloyd Pique  * wl_display_dispatch_queue_pending(display, queue);
1973*84e872a0SLloyd Pique  * \endcode
1974*84e872a0SLloyd Pique  *
1975*84e872a0SLloyd Pique  * Here we call wl_display_prepare_read_queue(), which ensures that between
1976*84e872a0SLloyd Pique  * returning from that call and eventually calling wl_display_read_events(), no
1977*84e872a0SLloyd Pique  * other thread will read from the fd and queue events in our queue. If the call
1978*84e872a0SLloyd Pique  * to wl_display_prepare_read_queue() fails, we dispatch the pending events and
1979*84e872a0SLloyd Pique  * try again until we're successful.
1980*84e872a0SLloyd Pique  *
1981*84e872a0SLloyd Pique  * The wl_display_prepare_read_queue() function doesn't acquire exclusive access
1982*84e872a0SLloyd Pique  * to the display's fd. It only registers that the thread calling this function
1983*84e872a0SLloyd Pique  * has intention to read from fd. When all registered readers call
1984*84e872a0SLloyd Pique  * wl_display_read_events(), only one (at random) eventually reads and queues
1985*84e872a0SLloyd Pique  * the events and the others are sleeping meanwhile. This way we avoid races and
1986*84e872a0SLloyd Pique  * still can read from more threads.
1987*84e872a0SLloyd Pique  *
1988*84e872a0SLloyd Pique  * \sa wl_display_cancel_read(), wl_display_read_events(),
1989*84e872a0SLloyd Pique  * wl_display_prepare_read()
1990*84e872a0SLloyd Pique  *
1991*84e872a0SLloyd Pique  * \memberof wl_display
1992*84e872a0SLloyd Pique  */
1993*84e872a0SLloyd Pique WL_EXPORT int
wl_display_prepare_read_queue(struct wl_display * display,struct wl_event_queue * queue)1994*84e872a0SLloyd Pique wl_display_prepare_read_queue(struct wl_display *display,
1995*84e872a0SLloyd Pique 			      struct wl_event_queue *queue)
1996*84e872a0SLloyd Pique {
1997*84e872a0SLloyd Pique 	int ret;
1998*84e872a0SLloyd Pique 
1999*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
2000*84e872a0SLloyd Pique 
2001*84e872a0SLloyd Pique 	if (!wl_list_empty(&queue->event_list)) {
2002*84e872a0SLloyd Pique 		errno = EAGAIN;
2003*84e872a0SLloyd Pique 		ret = -1;
2004*84e872a0SLloyd Pique 	} else {
2005*84e872a0SLloyd Pique 		display->reader_count++;
2006*84e872a0SLloyd Pique 		ret = 0;
2007*84e872a0SLloyd Pique 	}
2008*84e872a0SLloyd Pique 
2009*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
2010*84e872a0SLloyd Pique 
2011*84e872a0SLloyd Pique 	return ret;
2012*84e872a0SLloyd Pique }
2013*84e872a0SLloyd Pique 
2014*84e872a0SLloyd Pique /** Prepare to read events from the display's file descriptor
2015*84e872a0SLloyd Pique  *
2016*84e872a0SLloyd Pique  * \param display The display context object
2017*84e872a0SLloyd Pique  * \return 0 on success or -1 if event queue was not empty
2018*84e872a0SLloyd Pique  *
2019*84e872a0SLloyd Pique  * This function does the same thing as wl_display_prepare_read_queue()
2020*84e872a0SLloyd Pique  * with the default queue passed as the queue.
2021*84e872a0SLloyd Pique  *
2022*84e872a0SLloyd Pique  * \sa wl_display_prepare_read_queue
2023*84e872a0SLloyd Pique  * \memberof wl_display
2024*84e872a0SLloyd Pique  */
2025*84e872a0SLloyd Pique WL_EXPORT int
wl_display_prepare_read(struct wl_display * display)2026*84e872a0SLloyd Pique wl_display_prepare_read(struct wl_display *display)
2027*84e872a0SLloyd Pique {
2028*84e872a0SLloyd Pique 	return wl_display_prepare_read_queue(display, &display->default_queue);
2029*84e872a0SLloyd Pique }
2030*84e872a0SLloyd Pique 
2031*84e872a0SLloyd Pique /** Cancel read intention on display's fd
2032*84e872a0SLloyd Pique  *
2033*84e872a0SLloyd Pique  * \param display The display context object
2034*84e872a0SLloyd Pique  *
2035*84e872a0SLloyd Pique  * After a thread successfully called wl_display_prepare_read() it must
2036*84e872a0SLloyd Pique  * either call wl_display_read_events() or wl_display_cancel_read().
2037*84e872a0SLloyd Pique  * If the threads do not follow this rule it will lead to deadlock.
2038*84e872a0SLloyd Pique  *
2039*84e872a0SLloyd Pique  * \sa wl_display_prepare_read(), wl_display_read_events()
2040*84e872a0SLloyd Pique  *
2041*84e872a0SLloyd Pique  * \memberof wl_display
2042*84e872a0SLloyd Pique  */
2043*84e872a0SLloyd Pique WL_EXPORT void
wl_display_cancel_read(struct wl_display * display)2044*84e872a0SLloyd Pique wl_display_cancel_read(struct wl_display *display)
2045*84e872a0SLloyd Pique {
2046*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
2047*84e872a0SLloyd Pique 
2048*84e872a0SLloyd Pique 	cancel_read(display);
2049*84e872a0SLloyd Pique 
2050*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
2051*84e872a0SLloyd Pique }
2052*84e872a0SLloyd Pique 
2053*84e872a0SLloyd Pique static int
wl_display_poll(struct wl_display * display,short int events)2054*84e872a0SLloyd Pique wl_display_poll(struct wl_display *display, short int events)
2055*84e872a0SLloyd Pique {
2056*84e872a0SLloyd Pique 	int ret;
2057*84e872a0SLloyd Pique 	struct pollfd pfd[1];
2058*84e872a0SLloyd Pique 
2059*84e872a0SLloyd Pique 	pfd[0].fd = display->fd;
2060*84e872a0SLloyd Pique 	pfd[0].events = events;
2061*84e872a0SLloyd Pique 	do {
2062*84e872a0SLloyd Pique 		ret = poll(pfd, 1, -1);
2063*84e872a0SLloyd Pique 	} while (ret == -1 && errno == EINTR);
2064*84e872a0SLloyd Pique 
2065*84e872a0SLloyd Pique 	return ret;
2066*84e872a0SLloyd Pique }
2067*84e872a0SLloyd Pique 
2068*84e872a0SLloyd Pique /** Dispatch events in an event queue
2069*84e872a0SLloyd Pique  *
2070*84e872a0SLloyd Pique  * \param display The display context object
2071*84e872a0SLloyd Pique  * \param queue The event queue to dispatch
2072*84e872a0SLloyd Pique  * \return The number of dispatched events on success or -1 on failure
2073*84e872a0SLloyd Pique  *
2074*84e872a0SLloyd Pique  * Dispatch events on the given event queue.
2075*84e872a0SLloyd Pique  *
2076*84e872a0SLloyd Pique  * If the given event queue is empty, this function blocks until there are
2077*84e872a0SLloyd Pique  * events to be read from the display fd. Events are read and queued on
2078*84e872a0SLloyd Pique  * the appropriate event queues. Finally, events on given event queue are
2079*84e872a0SLloyd Pique  * dispatched. On failure -1 is returned and errno set appropriately.
2080*84e872a0SLloyd Pique  *
2081*84e872a0SLloyd Pique  * In a multi threaded environment, do not manually wait using poll() (or
2082*84e872a0SLloyd Pique  * equivalent) before calling this function, as doing so might cause a dead
2083*84e872a0SLloyd Pique  * lock. If external reliance on poll() (or equivalent) is required, see
2084*84e872a0SLloyd Pique  * wl_display_prepare_read_queue() of how to do so.
2085*84e872a0SLloyd Pique  *
2086*84e872a0SLloyd Pique  * This function is thread safe as long as it dispatches the right queue on the
2087*84e872a0SLloyd Pique  * right thread. It is also compatible with the multi thread event reading
2088*84e872a0SLloyd Pique  * preparation API (see wl_display_prepare_read_queue()), and uses the
2089*84e872a0SLloyd Pique  * equivalent functionality internally. It is not allowed to call this function
2090*84e872a0SLloyd Pique  * while the thread is being prepared for reading events, and doing so will
2091*84e872a0SLloyd Pique  * cause a dead lock.
2092*84e872a0SLloyd Pique  *
2093*84e872a0SLloyd Pique  * It can be used as a helper function to ease the procedure of reading and
2094*84e872a0SLloyd Pique  * dispatching events.
2095*84e872a0SLloyd Pique  *
2096*84e872a0SLloyd Pique  * \note Since Wayland 1.5 the display has an extra queue
2097*84e872a0SLloyd Pique  * for its own events (i. e. delete_id). This queue is dispatched always,
2098*84e872a0SLloyd Pique  * no matter what queue we passed as an argument to this function.
2099*84e872a0SLloyd Pique  * That means that this function can return non-0 value even when it
2100*84e872a0SLloyd Pique  * haven't dispatched any event for the given queue.
2101*84e872a0SLloyd Pique  *
2102*84e872a0SLloyd Pique  * \sa wl_display_dispatch(), wl_display_dispatch_pending(),
2103*84e872a0SLloyd Pique  * wl_display_dispatch_queue_pending(), wl_display_prepare_read_queue()
2104*84e872a0SLloyd Pique  *
2105*84e872a0SLloyd Pique  * \memberof wl_display
2106*84e872a0SLloyd Pique  */
2107*84e872a0SLloyd Pique WL_EXPORT int
wl_display_dispatch_queue(struct wl_display * display,struct wl_event_queue * queue)2108*84e872a0SLloyd Pique wl_display_dispatch_queue(struct wl_display *display,
2109*84e872a0SLloyd Pique 			  struct wl_event_queue *queue)
2110*84e872a0SLloyd Pique {
2111*84e872a0SLloyd Pique 	int ret;
2112*84e872a0SLloyd Pique 
2113*84e872a0SLloyd Pique 	if (wl_display_prepare_read_queue(display, queue) == -1)
2114*84e872a0SLloyd Pique 		return wl_display_dispatch_queue_pending(display, queue);
2115*84e872a0SLloyd Pique 
2116*84e872a0SLloyd Pique 	while (true) {
2117*84e872a0SLloyd Pique 		ret = wl_display_flush(display);
2118*84e872a0SLloyd Pique 
2119*84e872a0SLloyd Pique 		if (ret != -1 || errno != EAGAIN)
2120*84e872a0SLloyd Pique 			break;
2121*84e872a0SLloyd Pique 
2122*84e872a0SLloyd Pique 		if (wl_display_poll(display, POLLOUT) == -1) {
2123*84e872a0SLloyd Pique 			wl_display_cancel_read(display);
2124*84e872a0SLloyd Pique 			return -1;
2125*84e872a0SLloyd Pique 		}
2126*84e872a0SLloyd Pique 	}
2127*84e872a0SLloyd Pique 
2128*84e872a0SLloyd Pique 	/* Don't stop if flushing hits an EPIPE; continue so we can read any
2129*84e872a0SLloyd Pique 	 * protocol error that may have triggered it. */
2130*84e872a0SLloyd Pique 	if (ret < 0 && errno != EPIPE) {
2131*84e872a0SLloyd Pique 		wl_display_cancel_read(display);
2132*84e872a0SLloyd Pique 		return -1;
2133*84e872a0SLloyd Pique 	}
2134*84e872a0SLloyd Pique 
2135*84e872a0SLloyd Pique 	if (wl_display_poll(display, POLLIN) == -1) {
2136*84e872a0SLloyd Pique 		wl_display_cancel_read(display);
2137*84e872a0SLloyd Pique 		return -1;
2138*84e872a0SLloyd Pique 	}
2139*84e872a0SLloyd Pique 
2140*84e872a0SLloyd Pique 	if (wl_display_read_events(display) == -1)
2141*84e872a0SLloyd Pique 		return -1;
2142*84e872a0SLloyd Pique 
2143*84e872a0SLloyd Pique 	return wl_display_dispatch_queue_pending(display, queue);
2144*84e872a0SLloyd Pique }
2145*84e872a0SLloyd Pique 
2146*84e872a0SLloyd Pique /** Dispatch pending events in an event queue
2147*84e872a0SLloyd Pique  *
2148*84e872a0SLloyd Pique  * \param display The display context object
2149*84e872a0SLloyd Pique  * \param queue The event queue to dispatch
2150*84e872a0SLloyd Pique  * \return The number of dispatched events on success or -1 on failure
2151*84e872a0SLloyd Pique  *
2152*84e872a0SLloyd Pique  * Dispatch all incoming events for objects assigned to the given
2153*84e872a0SLloyd Pique  * event queue. On failure -1 is returned and errno set appropriately.
2154*84e872a0SLloyd Pique  * If there are no events queued, this function returns immediately.
2155*84e872a0SLloyd Pique  *
2156*84e872a0SLloyd Pique  * \memberof wl_display
2157*84e872a0SLloyd Pique  * \since 1.0.2
2158*84e872a0SLloyd Pique  */
2159*84e872a0SLloyd Pique WL_EXPORT int
wl_display_dispatch_queue_pending(struct wl_display * display,struct wl_event_queue * queue)2160*84e872a0SLloyd Pique wl_display_dispatch_queue_pending(struct wl_display *display,
2161*84e872a0SLloyd Pique 				  struct wl_event_queue *queue)
2162*84e872a0SLloyd Pique {
2163*84e872a0SLloyd Pique 	int ret;
2164*84e872a0SLloyd Pique 
2165*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
2166*84e872a0SLloyd Pique 
2167*84e872a0SLloyd Pique 	ret = dispatch_queue(display, queue);
2168*84e872a0SLloyd Pique 
2169*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
2170*84e872a0SLloyd Pique 
2171*84e872a0SLloyd Pique 	return ret;
2172*84e872a0SLloyd Pique }
2173*84e872a0SLloyd Pique 
2174*84e872a0SLloyd Pique /** Process incoming events
2175*84e872a0SLloyd Pique  *
2176*84e872a0SLloyd Pique  * \param display The display context object
2177*84e872a0SLloyd Pique  * \return The number of dispatched events on success or -1 on failure
2178*84e872a0SLloyd Pique  *
2179*84e872a0SLloyd Pique  * Dispatch events on the default event queue.
2180*84e872a0SLloyd Pique  *
2181*84e872a0SLloyd Pique  * If the default event queue is empty, this function blocks until there are
2182*84e872a0SLloyd Pique  * events to be read from the display fd. Events are read and queued on
2183*84e872a0SLloyd Pique  * the appropriate event queues. Finally, events on the default event queue
2184*84e872a0SLloyd Pique  * are dispatched. On failure -1 is returned and errno set appropriately.
2185*84e872a0SLloyd Pique  *
2186*84e872a0SLloyd Pique  * In a multi threaded environment, do not manually wait using poll() (or
2187*84e872a0SLloyd Pique  * equivalent) before calling this function, as doing so might cause a dead
2188*84e872a0SLloyd Pique  * lock. If external reliance on poll() (or equivalent) is required, see
2189*84e872a0SLloyd Pique  * wl_display_prepare_read_queue() of how to do so.
2190*84e872a0SLloyd Pique  *
2191*84e872a0SLloyd Pique  * This function is thread safe as long as it dispatches the right queue on the
2192*84e872a0SLloyd Pique  * right thread. It is also compatible with the multi thread event reading
2193*84e872a0SLloyd Pique  * preparation API (see wl_display_prepare_read_queue()), and uses the
2194*84e872a0SLloyd Pique  * equivalent functionality internally. It is not allowed to call this function
2195*84e872a0SLloyd Pique  * while the thread is being prepared for reading events, and doing so will
2196*84e872a0SLloyd Pique  * cause a dead lock.
2197*84e872a0SLloyd Pique  *
2198*84e872a0SLloyd Pique  * \note It is not possible to check if there are events on the queue
2199*84e872a0SLloyd Pique  * or not. For dispatching default queue events without blocking, see \ref
2200*84e872a0SLloyd Pique  * wl_display_dispatch_pending().
2201*84e872a0SLloyd Pique  *
2202*84e872a0SLloyd Pique  * \sa wl_display_dispatch_pending(), wl_display_dispatch_queue(),
2203*84e872a0SLloyd Pique  * wl_display_read_events()
2204*84e872a0SLloyd Pique  *
2205*84e872a0SLloyd Pique  * \memberof wl_display
2206*84e872a0SLloyd Pique  */
2207*84e872a0SLloyd Pique WL_EXPORT int
wl_display_dispatch(struct wl_display * display)2208*84e872a0SLloyd Pique wl_display_dispatch(struct wl_display *display)
2209*84e872a0SLloyd Pique {
2210*84e872a0SLloyd Pique 	return wl_display_dispatch_queue(display, &display->default_queue);
2211*84e872a0SLloyd Pique }
2212*84e872a0SLloyd Pique 
2213*84e872a0SLloyd Pique /** Dispatch default queue events without reading from the display fd
2214*84e872a0SLloyd Pique  *
2215*84e872a0SLloyd Pique  * \param display The display context object
2216*84e872a0SLloyd Pique  * \return The number of dispatched events or -1 on failure
2217*84e872a0SLloyd Pique  *
2218*84e872a0SLloyd Pique  * This function dispatches events on the main event queue. It does not
2219*84e872a0SLloyd Pique  * attempt to read the display fd and simply returns zero if the main
2220*84e872a0SLloyd Pique  * queue is empty, i.e., it doesn't block.
2221*84e872a0SLloyd Pique  *
2222*84e872a0SLloyd Pique  * \sa wl_display_dispatch(), wl_display_dispatch_queue(),
2223*84e872a0SLloyd Pique  * wl_display_flush()
2224*84e872a0SLloyd Pique  *
2225*84e872a0SLloyd Pique  * \memberof wl_display
2226*84e872a0SLloyd Pique  */
2227*84e872a0SLloyd Pique WL_EXPORT int
wl_display_dispatch_pending(struct wl_display * display)2228*84e872a0SLloyd Pique wl_display_dispatch_pending(struct wl_display *display)
2229*84e872a0SLloyd Pique {
2230*84e872a0SLloyd Pique 	return wl_display_dispatch_queue_pending(display,
2231*84e872a0SLloyd Pique 						 &display->default_queue);
2232*84e872a0SLloyd Pique }
2233*84e872a0SLloyd Pique 
2234*84e872a0SLloyd Pique /** Retrieve the last error that occurred on a display
2235*84e872a0SLloyd Pique  *
2236*84e872a0SLloyd Pique  * \param display The display context object
2237*84e872a0SLloyd Pique  * \return The last error that occurred on \c display or 0 if no error occurred
2238*84e872a0SLloyd Pique  *
2239*84e872a0SLloyd Pique  * Return the last error that occurred on the display. This may be an error sent
2240*84e872a0SLloyd Pique  * by the server or caused by the local client.
2241*84e872a0SLloyd Pique  *
2242*84e872a0SLloyd Pique  * \note Errors are \b fatal. If this function returns non-zero the display
2243*84e872a0SLloyd Pique  * can no longer be used.
2244*84e872a0SLloyd Pique  *
2245*84e872a0SLloyd Pique  * \memberof wl_display
2246*84e872a0SLloyd Pique  */
2247*84e872a0SLloyd Pique WL_EXPORT int
wl_display_get_error(struct wl_display * display)2248*84e872a0SLloyd Pique wl_display_get_error(struct wl_display *display)
2249*84e872a0SLloyd Pique {
2250*84e872a0SLloyd Pique 	int ret;
2251*84e872a0SLloyd Pique 
2252*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
2253*84e872a0SLloyd Pique 
2254*84e872a0SLloyd Pique 	ret = display->last_error;
2255*84e872a0SLloyd Pique 
2256*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
2257*84e872a0SLloyd Pique 
2258*84e872a0SLloyd Pique 	return ret;
2259*84e872a0SLloyd Pique }
2260*84e872a0SLloyd Pique 
2261*84e872a0SLloyd Pique /** Retrieves the information about a protocol error:
2262*84e872a0SLloyd Pique  *
2263*84e872a0SLloyd Pique  * \param display    The Wayland display
2264*84e872a0SLloyd Pique  * \param interface  if not NULL, stores the interface where the error occurred,
2265*84e872a0SLloyd Pique  *                   or NULL, if unknown.
2266*84e872a0SLloyd Pique  * \param id         if not NULL, stores the object id that generated
2267*84e872a0SLloyd Pique  *                   the error, or 0, if the object id is unknown. There's no
2268*84e872a0SLloyd Pique  *                   guarantee the object is still valid; the client must know
2269*84e872a0SLloyd Pique  *                   if it deleted the object.
2270*84e872a0SLloyd Pique  * \return           The error code as defined in the interface specification.
2271*84e872a0SLloyd Pique  *
2272*84e872a0SLloyd Pique  * \code
2273*84e872a0SLloyd Pique  * int err = wl_display_get_error(display);
2274*84e872a0SLloyd Pique  *
2275*84e872a0SLloyd Pique  * if (err == EPROTO) {
2276*84e872a0SLloyd Pique  *        code = wl_display_get_protocol_error(display, &interface, &id);
2277*84e872a0SLloyd Pique  *        handle_error(code, interface, id);
2278*84e872a0SLloyd Pique  * }
2279*84e872a0SLloyd Pique  *
2280*84e872a0SLloyd Pique  * ...
2281*84e872a0SLloyd Pique  * \endcode
2282*84e872a0SLloyd Pique  * \memberof wl_display
2283*84e872a0SLloyd Pique  */
2284*84e872a0SLloyd Pique WL_EXPORT uint32_t
wl_display_get_protocol_error(struct wl_display * display,const struct wl_interface ** interface,uint32_t * id)2285*84e872a0SLloyd Pique wl_display_get_protocol_error(struct wl_display *display,
2286*84e872a0SLloyd Pique 			      const struct wl_interface **interface,
2287*84e872a0SLloyd Pique 			      uint32_t *id)
2288*84e872a0SLloyd Pique {
2289*84e872a0SLloyd Pique 	uint32_t ret;
2290*84e872a0SLloyd Pique 
2291*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
2292*84e872a0SLloyd Pique 
2293*84e872a0SLloyd Pique 	ret = display->protocol_error.code;
2294*84e872a0SLloyd Pique 
2295*84e872a0SLloyd Pique 	if (interface)
2296*84e872a0SLloyd Pique 		*interface = display->protocol_error.interface;
2297*84e872a0SLloyd Pique 	if (id)
2298*84e872a0SLloyd Pique 		*id = display->protocol_error.id;
2299*84e872a0SLloyd Pique 
2300*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
2301*84e872a0SLloyd Pique 
2302*84e872a0SLloyd Pique 	return ret;
2303*84e872a0SLloyd Pique }
2304*84e872a0SLloyd Pique 
2305*84e872a0SLloyd Pique 
2306*84e872a0SLloyd Pique /** Send all buffered requests on the display to the server
2307*84e872a0SLloyd Pique  *
2308*84e872a0SLloyd Pique  * \param display The display context object
2309*84e872a0SLloyd Pique  * \return The number of bytes sent on success or -1 on failure
2310*84e872a0SLloyd Pique  *
2311*84e872a0SLloyd Pique  * Send all buffered data on the client side to the server. Clients should
2312*84e872a0SLloyd Pique  * always call this function before blocking on input from the display fd.
2313*84e872a0SLloyd Pique  * On success, the number of bytes sent to the server is returned. On
2314*84e872a0SLloyd Pique  * failure, this function returns -1 and errno is set appropriately.
2315*84e872a0SLloyd Pique  *
2316*84e872a0SLloyd Pique  * wl_display_flush() never blocks.  It will write as much data as
2317*84e872a0SLloyd Pique  * possible, but if all data could not be written, errno will be set
2318*84e872a0SLloyd Pique  * to EAGAIN and -1 returned.  In that case, use poll on the display
2319*84e872a0SLloyd Pique  * file descriptor to wait for it to become writable again.
2320*84e872a0SLloyd Pique  *
2321*84e872a0SLloyd Pique  * \memberof wl_display
2322*84e872a0SLloyd Pique  */
2323*84e872a0SLloyd Pique WL_EXPORT int
wl_display_flush(struct wl_display * display)2324*84e872a0SLloyd Pique wl_display_flush(struct wl_display *display)
2325*84e872a0SLloyd Pique {
2326*84e872a0SLloyd Pique 	int ret;
2327*84e872a0SLloyd Pique 
2328*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
2329*84e872a0SLloyd Pique 
2330*84e872a0SLloyd Pique 	if (display->last_error) {
2331*84e872a0SLloyd Pique 		errno = display->last_error;
2332*84e872a0SLloyd Pique 		ret = -1;
2333*84e872a0SLloyd Pique 	} else {
2334*84e872a0SLloyd Pique 		/* We don't make EPIPE a fatal error here, so that we may try to
2335*84e872a0SLloyd Pique 		 * read events after the failed flush. When the compositor sends
2336*84e872a0SLloyd Pique 		 * an error it will close the socket, and if we make EPIPE fatal
2337*84e872a0SLloyd Pique 		 * here we don't get a chance to process the error. */
2338*84e872a0SLloyd Pique 		ret = wl_connection_flush(display->connection);
2339*84e872a0SLloyd Pique 		if (ret < 0 && errno != EAGAIN && errno != EPIPE)
2340*84e872a0SLloyd Pique 			display_fatal_error(display, errno);
2341*84e872a0SLloyd Pique 	}
2342*84e872a0SLloyd Pique 
2343*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
2344*84e872a0SLloyd Pique 
2345*84e872a0SLloyd Pique 	return ret;
2346*84e872a0SLloyd Pique }
2347*84e872a0SLloyd Pique 
2348*84e872a0SLloyd Pique /** Set the user data associated with a proxy
2349*84e872a0SLloyd Pique  *
2350*84e872a0SLloyd Pique  * \param proxy The proxy object
2351*84e872a0SLloyd Pique  * \param user_data The data to be associated with proxy
2352*84e872a0SLloyd Pique  *
2353*84e872a0SLloyd Pique  * Set the user data associated with \c proxy. When events for this
2354*84e872a0SLloyd Pique  * proxy are received, \c user_data will be supplied to its listener.
2355*84e872a0SLloyd Pique  *
2356*84e872a0SLloyd Pique  * \memberof wl_proxy
2357*84e872a0SLloyd Pique  */
2358*84e872a0SLloyd Pique WL_EXPORT void
wl_proxy_set_user_data(struct wl_proxy * proxy,void * user_data)2359*84e872a0SLloyd Pique wl_proxy_set_user_data(struct wl_proxy *proxy, void *user_data)
2360*84e872a0SLloyd Pique {
2361*84e872a0SLloyd Pique 	proxy->user_data = user_data;
2362*84e872a0SLloyd Pique }
2363*84e872a0SLloyd Pique 
2364*84e872a0SLloyd Pique /** Get the user data associated with a proxy
2365*84e872a0SLloyd Pique  *
2366*84e872a0SLloyd Pique  * \param proxy The proxy object
2367*84e872a0SLloyd Pique  * \return The user data associated with proxy
2368*84e872a0SLloyd Pique  *
2369*84e872a0SLloyd Pique  * \memberof wl_proxy
2370*84e872a0SLloyd Pique  */
2371*84e872a0SLloyd Pique WL_EXPORT void *
wl_proxy_get_user_data(struct wl_proxy * proxy)2372*84e872a0SLloyd Pique wl_proxy_get_user_data(struct wl_proxy *proxy)
2373*84e872a0SLloyd Pique {
2374*84e872a0SLloyd Pique 	return proxy->user_data;
2375*84e872a0SLloyd Pique }
2376*84e872a0SLloyd Pique 
2377*84e872a0SLloyd Pique /** Get the protocol object version of a proxy object
2378*84e872a0SLloyd Pique  *
2379*84e872a0SLloyd Pique  * \param proxy The proxy object
2380*84e872a0SLloyd Pique  * \return The protocol object version of the proxy or 0
2381*84e872a0SLloyd Pique  *
2382*84e872a0SLloyd Pique  * Gets the protocol object version of a proxy object, or 0
2383*84e872a0SLloyd Pique  * if the proxy was created with unversioned API.
2384*84e872a0SLloyd Pique  *
2385*84e872a0SLloyd Pique  * A returned value of 0 means that no version information is
2386*84e872a0SLloyd Pique  * available, so the caller must make safe assumptions about
2387*84e872a0SLloyd Pique  * the object's real version.
2388*84e872a0SLloyd Pique  *
2389*84e872a0SLloyd Pique  * wl_display's version will always return 0.
2390*84e872a0SLloyd Pique  *
2391*84e872a0SLloyd Pique  * \memberof wl_proxy
2392*84e872a0SLloyd Pique  */
2393*84e872a0SLloyd Pique WL_EXPORT uint32_t
wl_proxy_get_version(struct wl_proxy * proxy)2394*84e872a0SLloyd Pique wl_proxy_get_version(struct wl_proxy *proxy)
2395*84e872a0SLloyd Pique {
2396*84e872a0SLloyd Pique 	return proxy->version;
2397*84e872a0SLloyd Pique }
2398*84e872a0SLloyd Pique 
2399*84e872a0SLloyd Pique /** Get the id of a proxy object
2400*84e872a0SLloyd Pique  *
2401*84e872a0SLloyd Pique  * \param proxy The proxy object
2402*84e872a0SLloyd Pique  * \return The id the object associated with the proxy
2403*84e872a0SLloyd Pique  *
2404*84e872a0SLloyd Pique  * \memberof wl_proxy
2405*84e872a0SLloyd Pique  */
2406*84e872a0SLloyd Pique WL_EXPORT uint32_t
wl_proxy_get_id(struct wl_proxy * proxy)2407*84e872a0SLloyd Pique wl_proxy_get_id(struct wl_proxy *proxy)
2408*84e872a0SLloyd Pique {
2409*84e872a0SLloyd Pique 	return proxy->object.id;
2410*84e872a0SLloyd Pique }
2411*84e872a0SLloyd Pique 
2412*84e872a0SLloyd Pique /** Set the tag of a proxy object
2413*84e872a0SLloyd Pique  *
2414*84e872a0SLloyd Pique  * A toolkit or application can set a unique tag on a proxy in order to
2415*84e872a0SLloyd Pique  * identify whether an object is managed by itself or some external part.
2416*84e872a0SLloyd Pique  *
2417*84e872a0SLloyd Pique  * To create a tag, the recommended way is to define a statically allocated
2418*84e872a0SLloyd Pique  * constant char array containing some descriptive string. The tag will be the
2419*84e872a0SLloyd Pique  * pointer to the non-const pointer to the beginning of the array.
2420*84e872a0SLloyd Pique  *
2421*84e872a0SLloyd Pique  * For example, to define and set a tag on a surface managed by a certain
2422*84e872a0SLloyd Pique  * subsystem:
2423*84e872a0SLloyd Pique  *
2424*84e872a0SLloyd Pique  * 	static const char *my_tag = "my tag";
2425*84e872a0SLloyd Pique  *
2426*84e872a0SLloyd Pique  * 	wl_proxy_set_tag((struct wl_proxy *) surface, &my_tag);
2427*84e872a0SLloyd Pique  *
2428*84e872a0SLloyd Pique  * Then, in a callback with wl_surface as an argument, in order to check
2429*84e872a0SLloyd Pique  * whether it's a surface managed by the same subsystem.
2430*84e872a0SLloyd Pique  *
2431*84e872a0SLloyd Pique  * 	const char * const *tag;
2432*84e872a0SLloyd Pique  *
2433*84e872a0SLloyd Pique  * 	tag = wl_proxy_get_tag((struct wl_proxy *) surface);
2434*84e872a0SLloyd Pique  * 	if (tag != &my_tag)
2435*84e872a0SLloyd Pique  *		return;
2436*84e872a0SLloyd Pique  *
2437*84e872a0SLloyd Pique  *	...
2438*84e872a0SLloyd Pique  *
2439*84e872a0SLloyd Pique  * For debugging purposes, a tag should be suitable to be included in a debug
2440*84e872a0SLloyd Pique  * log entry, e.g.
2441*84e872a0SLloyd Pique  *
2442*84e872a0SLloyd Pique  * 	const char * const *tag;
2443*84e872a0SLloyd Pique  *
2444*84e872a0SLloyd Pique  * 	tag = wl_proxy_get_tag((struct wl_proxy *) surface);
2445*84e872a0SLloyd Pique  * 	printf("Got a surface with the tag %p (%s)\n",
2446*84e872a0SLloyd Pique  * 	       tag, (tag && *tag) ? *tag : "");
2447*84e872a0SLloyd Pique  *
2448*84e872a0SLloyd Pique  * \param proxy The proxy object
2449*84e872a0SLloyd Pique  * \param tag The tag
2450*84e872a0SLloyd Pique  *
2451*84e872a0SLloyd Pique  * \memberof wl_proxy
2452*84e872a0SLloyd Pique  * \since 1.17.90
2453*84e872a0SLloyd Pique  */
2454*84e872a0SLloyd Pique WL_EXPORT void
wl_proxy_set_tag(struct wl_proxy * proxy,const char * const * tag)2455*84e872a0SLloyd Pique wl_proxy_set_tag(struct wl_proxy *proxy,
2456*84e872a0SLloyd Pique 		 const char * const *tag)
2457*84e872a0SLloyd Pique {
2458*84e872a0SLloyd Pique 	proxy->tag = tag;
2459*84e872a0SLloyd Pique }
2460*84e872a0SLloyd Pique 
2461*84e872a0SLloyd Pique /** Get the tag of a proxy object
2462*84e872a0SLloyd Pique  *
2463*84e872a0SLloyd Pique  * See wl_proxy_set_tag for details.
2464*84e872a0SLloyd Pique  *
2465*84e872a0SLloyd Pique  * \param proxy The proxy object
2466*84e872a0SLloyd Pique  *
2467*84e872a0SLloyd Pique  * \memberof wl_proxy
2468*84e872a0SLloyd Pique  * \since 1.17.90
2469*84e872a0SLloyd Pique  */
2470*84e872a0SLloyd Pique WL_EXPORT const char * const *
wl_proxy_get_tag(struct wl_proxy * proxy)2471*84e872a0SLloyd Pique wl_proxy_get_tag(struct wl_proxy *proxy)
2472*84e872a0SLloyd Pique {
2473*84e872a0SLloyd Pique 	return proxy->tag;
2474*84e872a0SLloyd Pique }
2475*84e872a0SLloyd Pique 
2476*84e872a0SLloyd Pique /** Get the interface name (class) of a proxy object
2477*84e872a0SLloyd Pique  *
2478*84e872a0SLloyd Pique  * \param proxy The proxy object
2479*84e872a0SLloyd Pique  * \return The interface name of the object associated with the proxy
2480*84e872a0SLloyd Pique  *
2481*84e872a0SLloyd Pique  * \memberof wl_proxy
2482*84e872a0SLloyd Pique  */
2483*84e872a0SLloyd Pique WL_EXPORT const char *
wl_proxy_get_class(struct wl_proxy * proxy)2484*84e872a0SLloyd Pique wl_proxy_get_class(struct wl_proxy *proxy)
2485*84e872a0SLloyd Pique {
2486*84e872a0SLloyd Pique 	return proxy->object.interface->name;
2487*84e872a0SLloyd Pique }
2488*84e872a0SLloyd Pique 
2489*84e872a0SLloyd Pique /** Assign a proxy to an event queue
2490*84e872a0SLloyd Pique  *
2491*84e872a0SLloyd Pique  * \param proxy The proxy object
2492*84e872a0SLloyd Pique  * \param queue The event queue that will handle this proxy or NULL
2493*84e872a0SLloyd Pique  *
2494*84e872a0SLloyd Pique  * Assign proxy to event queue. Events coming from \c proxy will be
2495*84e872a0SLloyd Pique  * queued in \c queue from now. If queue is NULL, then the display's
2496*84e872a0SLloyd Pique  * default queue is set to the proxy.
2497*84e872a0SLloyd Pique  *
2498*84e872a0SLloyd Pique  * In order to guarantee proper handing of all events which were queued
2499*84e872a0SLloyd Pique  * before the queue change takes effect, it is required to dispatch the
2500*84e872a0SLloyd Pique  * proxy's old event queue after setting a new event queue.
2501*84e872a0SLloyd Pique  *
2502*84e872a0SLloyd Pique  * This is particularly important for multi-threaded setups, where it is
2503*84e872a0SLloyd Pique  * possible for events to be queued to the proxy's old queue from a
2504*84e872a0SLloyd Pique  * different thread during the invocation of this function.
2505*84e872a0SLloyd Pique  *
2506*84e872a0SLloyd Pique  * To ensure that all events for a newly created proxy are dispatched
2507*84e872a0SLloyd Pique  * on a particular queue, it is necessary to use a proxy wrapper if
2508*84e872a0SLloyd Pique  * events are read and dispatched on more than one thread. See
2509*84e872a0SLloyd Pique  * wl_proxy_create_wrapper() for more details.
2510*84e872a0SLloyd Pique  *
2511*84e872a0SLloyd Pique  * \note By default, the queue set in proxy is the one inherited from parent.
2512*84e872a0SLloyd Pique  *
2513*84e872a0SLloyd Pique  * \sa wl_display_dispatch_queue()
2514*84e872a0SLloyd Pique  *
2515*84e872a0SLloyd Pique  * \memberof wl_proxy
2516*84e872a0SLloyd Pique  */
2517*84e872a0SLloyd Pique WL_EXPORT void
wl_proxy_set_queue(struct wl_proxy * proxy,struct wl_event_queue * queue)2518*84e872a0SLloyd Pique wl_proxy_set_queue(struct wl_proxy *proxy, struct wl_event_queue *queue)
2519*84e872a0SLloyd Pique {
2520*84e872a0SLloyd Pique 	pthread_mutex_lock(&proxy->display->mutex);
2521*84e872a0SLloyd Pique 
2522*84e872a0SLloyd Pique 	wl_list_remove(&proxy->queue_link);
2523*84e872a0SLloyd Pique 
2524*84e872a0SLloyd Pique 	if (queue) {
2525*84e872a0SLloyd Pique 		assert(proxy->display == queue->display);
2526*84e872a0SLloyd Pique 		proxy->queue = queue;
2527*84e872a0SLloyd Pique 	} else {
2528*84e872a0SLloyd Pique 		proxy->queue = &proxy->display->default_queue;
2529*84e872a0SLloyd Pique 	}
2530*84e872a0SLloyd Pique 
2531*84e872a0SLloyd Pique 	wl_list_insert(&proxy->queue->proxy_list, &proxy->queue_link);
2532*84e872a0SLloyd Pique 
2533*84e872a0SLloyd Pique 	pthread_mutex_unlock(&proxy->display->mutex);
2534*84e872a0SLloyd Pique }
2535*84e872a0SLloyd Pique 
2536*84e872a0SLloyd Pique /** Create a proxy wrapper for making queue assignments thread-safe
2537*84e872a0SLloyd Pique  *
2538*84e872a0SLloyd Pique  * \param proxy The proxy object to be wrapped
2539*84e872a0SLloyd Pique  * \return A proxy wrapper for the given proxy or NULL on failure
2540*84e872a0SLloyd Pique  *
2541*84e872a0SLloyd Pique  * A proxy wrapper is type of 'struct wl_proxy' instance that can be used when
2542*84e872a0SLloyd Pique  * sending requests instead of using the original proxy. A proxy wrapper does
2543*84e872a0SLloyd Pique  * not have an implementation or dispatcher, and events received on the
2544*84e872a0SLloyd Pique  * object is still emitted on the original proxy. Trying to set an
2545*84e872a0SLloyd Pique  * implementation or dispatcher will have no effect but result in a warning
2546*84e872a0SLloyd Pique  * being logged.
2547*84e872a0SLloyd Pique  *
2548*84e872a0SLloyd Pique  * Setting the proxy queue of the proxy wrapper will make new objects created
2549*84e872a0SLloyd Pique  * using the proxy wrapper use the set proxy queue.
2550*84e872a0SLloyd Pique  * Even though there is no implementation nor dispatcher, the proxy queue can
2551*84e872a0SLloyd Pique  * be changed. This will affect the default queue of new objects created by
2552*84e872a0SLloyd Pique  * requests sent via the proxy wrapper.
2553*84e872a0SLloyd Pique  *
2554*84e872a0SLloyd Pique  * A proxy wrapper can only be destroyed using wl_proxy_wrapper_destroy().
2555*84e872a0SLloyd Pique  *
2556*84e872a0SLloyd Pique  * A proxy wrapper must be destroyed before the proxy it was created from.
2557*84e872a0SLloyd Pique  *
2558*84e872a0SLloyd Pique  * If a user reads and dispatches events on more than one thread, it is
2559*84e872a0SLloyd Pique  * necessary to use a proxy wrapper when sending requests on objects when the
2560*84e872a0SLloyd Pique  * intention is that a newly created proxy is to use a proxy queue different
2561*84e872a0SLloyd Pique  * from the proxy the request was sent on, as creating the new proxy and then
2562*84e872a0SLloyd Pique  * setting the queue is not thread safe.
2563*84e872a0SLloyd Pique  *
2564*84e872a0SLloyd Pique  * For example, a module that runs using its own proxy queue that needs to
2565*84e872a0SLloyd Pique  * do display roundtrip must wrap the wl_display proxy object before sending
2566*84e872a0SLloyd Pique  * the wl_display.sync request. For example:
2567*84e872a0SLloyd Pique  *
2568*84e872a0SLloyd Pique  * \code
2569*84e872a0SLloyd Pique  *
2570*84e872a0SLloyd Pique  *   struct wl_event_queue *queue = ...;
2571*84e872a0SLloyd Pique  *   struct wl_display *wrapped_display;
2572*84e872a0SLloyd Pique  *   struct wl_callback *callback;
2573*84e872a0SLloyd Pique  *
2574*84e872a0SLloyd Pique  *   wrapped_display = wl_proxy_create_wrapper(display);
2575*84e872a0SLloyd Pique  *   wl_proxy_set_queue((struct wl_proxy *) wrapped_display, queue);
2576*84e872a0SLloyd Pique  *   callback = wl_display_sync(wrapped_display);
2577*84e872a0SLloyd Pique  *   wl_proxy_wrapper_destroy(wrapped_display);
2578*84e872a0SLloyd Pique  *   wl_callback_add_listener(callback, ...);
2579*84e872a0SLloyd Pique  *
2580*84e872a0SLloyd Pique  * \endcode
2581*84e872a0SLloyd Pique  *
2582*84e872a0SLloyd Pique  * \memberof wl_proxy
2583*84e872a0SLloyd Pique  */
2584*84e872a0SLloyd Pique WL_EXPORT void *
wl_proxy_create_wrapper(void * proxy)2585*84e872a0SLloyd Pique wl_proxy_create_wrapper(void *proxy)
2586*84e872a0SLloyd Pique {
2587*84e872a0SLloyd Pique 	struct wl_proxy *wrapped_proxy = proxy;
2588*84e872a0SLloyd Pique 	struct wl_proxy *wrapper;
2589*84e872a0SLloyd Pique 
2590*84e872a0SLloyd Pique 	wrapper = zalloc(sizeof *wrapper);
2591*84e872a0SLloyd Pique 	if (!wrapper)
2592*84e872a0SLloyd Pique 		return NULL;
2593*84e872a0SLloyd Pique 
2594*84e872a0SLloyd Pique 	pthread_mutex_lock(&wrapped_proxy->display->mutex);
2595*84e872a0SLloyd Pique 
2596*84e872a0SLloyd Pique 	wrapper->object.interface = wrapped_proxy->object.interface;
2597*84e872a0SLloyd Pique 	wrapper->object.id = wrapped_proxy->object.id;
2598*84e872a0SLloyd Pique 	wrapper->version = wrapped_proxy->version;
2599*84e872a0SLloyd Pique 	wrapper->display = wrapped_proxy->display;
2600*84e872a0SLloyd Pique 	wrapper->queue = wrapped_proxy->queue;
2601*84e872a0SLloyd Pique 	wrapper->flags = WL_PROXY_FLAG_WRAPPER;
2602*84e872a0SLloyd Pique 	wrapper->refcount = 1;
2603*84e872a0SLloyd Pique 
2604*84e872a0SLloyd Pique 	wl_list_insert(&wrapper->queue->proxy_list, &wrapper->queue_link);
2605*84e872a0SLloyd Pique 
2606*84e872a0SLloyd Pique 	pthread_mutex_unlock(&wrapped_proxy->display->mutex);
2607*84e872a0SLloyd Pique 
2608*84e872a0SLloyd Pique 	return wrapper;
2609*84e872a0SLloyd Pique }
2610*84e872a0SLloyd Pique 
2611*84e872a0SLloyd Pique /** Destroy a proxy wrapper
2612*84e872a0SLloyd Pique  * \param proxy_wrapper The proxy wrapper to be destroyed
2613*84e872a0SLloyd Pique  *
2614*84e872a0SLloyd Pique  * \memberof wl_proxy
2615*84e872a0SLloyd Pique  */
2616*84e872a0SLloyd Pique WL_EXPORT void
wl_proxy_wrapper_destroy(void * proxy_wrapper)2617*84e872a0SLloyd Pique wl_proxy_wrapper_destroy(void *proxy_wrapper)
2618*84e872a0SLloyd Pique {
2619*84e872a0SLloyd Pique 	struct wl_proxy *wrapper = proxy_wrapper;
2620*84e872a0SLloyd Pique 
2621*84e872a0SLloyd Pique 	if (!(wrapper->flags & WL_PROXY_FLAG_WRAPPER))
2622*84e872a0SLloyd Pique 		wl_abort("Tried to destroy non-wrapper proxy with "
2623*84e872a0SLloyd Pique 			 "wl_proxy_wrapper_destroy\n");
2624*84e872a0SLloyd Pique 
2625*84e872a0SLloyd Pique 	assert(wrapper->refcount == 1);
2626*84e872a0SLloyd Pique 
2627*84e872a0SLloyd Pique 	pthread_mutex_lock(&wrapper->display->mutex);
2628*84e872a0SLloyd Pique 
2629*84e872a0SLloyd Pique 	wl_list_remove(&wrapper->queue_link);
2630*84e872a0SLloyd Pique 
2631*84e872a0SLloyd Pique 	pthread_mutex_unlock(&wrapper->display->mutex);
2632*84e872a0SLloyd Pique 
2633*84e872a0SLloyd Pique 	free(wrapper);
2634*84e872a0SLloyd Pique }
2635*84e872a0SLloyd Pique 
2636*84e872a0SLloyd Pique /** Safely converts an object into its corresponding proxy
2637*84e872a0SLloyd Pique  *
2638*84e872a0SLloyd Pique  * \param object object to get the proxy for
2639*84e872a0SLloyd Pique  * \return A corresponding proxy, or NULL on failure.
2640*84e872a0SLloyd Pique  *
2641*84e872a0SLloyd Pique  * Safely converts an object into its corresponding proxy.
2642*84e872a0SLloyd Pique  *
2643*84e872a0SLloyd Pique  * This is useful for implementing functions that are given a \c wl_argument
2644*84e872a0SLloyd Pique  * array, and that need to do further introspection on the ".o" field, as it
2645*84e872a0SLloyd Pique  * is otherwise an opaque type.
2646*84e872a0SLloyd Pique  *
2647*84e872a0SLloyd Pique  * \memberof wl_proxy
2648*84e872a0SLloyd Pique  */
2649*84e872a0SLloyd Pique WL_EXPORT struct wl_proxy *
wl_proxy_from_object(struct wl_object * object)2650*84e872a0SLloyd Pique wl_proxy_from_object(struct wl_object *object)
2651*84e872a0SLloyd Pique {
2652*84e872a0SLloyd Pique 	struct wl_proxy *proxy;
2653*84e872a0SLloyd Pique 	if (object == NULL)
2654*84e872a0SLloyd Pique 		return NULL;
2655*84e872a0SLloyd Pique 	return wl_container_of(object, proxy, object);
2656*84e872a0SLloyd Pique }
2657*84e872a0SLloyd Pique 
2658*84e872a0SLloyd Pique WL_EXPORT void
wl_log_set_handler_client(wl_log_func_t handler)2659*84e872a0SLloyd Pique wl_log_set_handler_client(wl_log_func_t handler)
2660*84e872a0SLloyd Pique {
2661*84e872a0SLloyd Pique 	wl_log_handler = handler;
2662*84e872a0SLloyd Pique }
2663*84e872a0SLloyd Pique 
2664*84e872a0SLloyd Pique /** Creates an client message observer.
2665*84e872a0SLloyd Pique  *
2666*84e872a0SLloyd Pique  * Note that the observer can potentially start receiving traffic immediately
2667*84e872a0SLloyd Pique  * after being created, and even before this call returns.
2668*84e872a0SLloyd Pique  *
2669*84e872a0SLloyd Pique  * \param display    client display to register with
2670*84e872a0SLloyd Pique  * \param func       function to call when client messages are observed
2671*84e872a0SLloyd Pique  * \param user_data  \c user_data pointer to pass to the observer
2672*84e872a0SLloyd Pique  *
2673*84e872a0SLloyd Pique  * \return The created observer, or NULL.
2674*84e872a0SLloyd Pique  *
2675*84e872a0SLloyd Pique  * \sa wl_client_observer_destroy
2676*84e872a0SLloyd Pique  *
2677*84e872a0SLloyd Pique  * \memberof wl_display
2678*84e872a0SLloyd Pique  */
2679*84e872a0SLloyd Pique 
2680*84e872a0SLloyd Pique WL_EXPORT struct wl_client_observer *
wl_display_create_client_observer(struct wl_display * display,wl_client_message_observer_func_t func,void * user_data)2681*84e872a0SLloyd Pique wl_display_create_client_observer(struct wl_display *display,
2682*84e872a0SLloyd Pique 				  wl_client_message_observer_func_t func,
2683*84e872a0SLloyd Pique 				  void *user_data)
2684*84e872a0SLloyd Pique {
2685*84e872a0SLloyd Pique 	struct wl_client_observer *observer;
2686*84e872a0SLloyd Pique 
2687*84e872a0SLloyd Pique 	observer = malloc(sizeof *observer);
2688*84e872a0SLloyd Pique 	if (!observer)
2689*84e872a0SLloyd Pique 		return NULL;
2690*84e872a0SLloyd Pique 
2691*84e872a0SLloyd Pique 	observer->display = display;
2692*84e872a0SLloyd Pique 	observer->func = func;
2693*84e872a0SLloyd Pique 	observer->user_data = user_data;
2694*84e872a0SLloyd Pique 
2695*84e872a0SLloyd Pique 	pthread_mutex_lock(&display->mutex);
2696*84e872a0SLloyd Pique 
2697*84e872a0SLloyd Pique 	wl_list_insert(&display->observers, &observer->link);
2698*84e872a0SLloyd Pique 
2699*84e872a0SLloyd Pique 	pthread_mutex_unlock(&display->mutex);
2700*84e872a0SLloyd Pique 
2701*84e872a0SLloyd Pique 	return observer;
2702*84e872a0SLloyd Pique }
2703*84e872a0SLloyd Pique 
2704*84e872a0SLloyd Pique /** Destroys a client message obsever.
2705*84e872a0SLloyd Pique  *
2706*84e872a0SLloyd Pique  * This function destroys a client message observer, and removes it from the
2707*84e872a0SLloyd Pique  * display it was added to with \c wl_display_create_client_observer.
2708*84e872a0SLloyd Pique  *
2709*84e872a0SLloyd Pique  * \param observer observer to destroy.
2710*84e872a0SLloyd Pique  *
2711*84e872a0SLloyd Pique  * \memberof wl_client_observer
2712*84e872a0SLloyd Pique  */
2713*84e872a0SLloyd Pique WL_EXPORT void
wl_client_observer_destroy(struct wl_client_observer * observer)2714*84e872a0SLloyd Pique wl_client_observer_destroy(struct wl_client_observer *observer)
2715*84e872a0SLloyd Pique {
2716*84e872a0SLloyd Pique 	pthread_mutex_lock(&observer->display->mutex);
2717*84e872a0SLloyd Pique 
2718*84e872a0SLloyd Pique 	wl_list_remove(&observer->link);
2719*84e872a0SLloyd Pique 
2720*84e872a0SLloyd Pique 	pthread_mutex_unlock(&observer->display->mutex);
2721*84e872a0SLloyd Pique 
2722*84e872a0SLloyd Pique 	free(observer);
2723*84e872a0SLloyd Pique }
2724