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