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