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