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