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