client: Keep track of proxy validity and number of reference holders
[profile/ivi/wayland.git] / src / wayland-client.c
1 /*
2  * Copyright © 2008-2012 Kristian Høgsberg
3  * Copyright © 2010-2012 Intel Corporation
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting documentation, and
9  * that the name of the copyright holders not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  The copyright holders make no representations
12  * about the suitability of this software for any purpose.  It is provided "as
13  * is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21  * OF THIS SOFTWARE.
22  */
23
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <ctype.h>
35 #include <assert.h>
36 #include <fcntl.h>
37 #include <sys/poll.h>
38 #include <pthread.h>
39
40 #include "wayland-util.h"
41 #include "wayland-os.h"
42 #include "wayland-client.h"
43 #include "wayland-private.h"
44
45
46 /** \cond */
47
48 enum wl_proxy_flag {
49         WL_PROXY_FLAG_ID_DELETED = (1 << 0),
50         WL_PROXY_FLAG_DESTROYED = (1 << 1)
51 };
52
53 struct wl_proxy {
54         struct wl_object object;
55         struct wl_display *display;
56         struct wl_event_queue *queue;
57         uint32_t flags;
58         int refcount;
59         void *user_data;
60 };
61
62 struct wl_global {
63         uint32_t id;
64         char *interface;
65         uint32_t version;
66         struct wl_list link;
67 };
68
69 struct wl_event_queue {
70         struct wl_list link;
71         struct wl_list event_list;
72         struct wl_display *display;
73         pthread_cond_t cond;
74 };
75
76 struct wl_display {
77         struct wl_proxy proxy;
78         struct wl_connection *connection;
79         int last_error;
80         int fd;
81         pthread_t display_thread;
82         struct wl_map objects;
83         struct wl_event_queue queue;
84         struct wl_list event_queue_list;
85         pthread_mutex_t mutex;
86 };
87
88 /** \endcond */
89
90 static int wl_debug = 0;
91
92 static void
93 display_fatal_error(struct wl_display *display, int error)
94 {
95         struct wl_event_queue *iter;
96
97         if (display->last_error)
98                 return;
99
100         if (!error)
101                 error = 1;
102
103         display->last_error = error;
104         close(display->fd);
105         display->fd = -1;
106
107         wl_list_for_each(iter, &display->event_queue_list, link)
108                 pthread_cond_broadcast(&iter->cond);
109 }
110
111 static void
112 wl_display_fatal_error(struct wl_display *display, int error)
113 {
114         pthread_mutex_lock(&display->mutex);
115         display_fatal_error(display, error);
116         pthread_mutex_unlock(&display->mutex);
117 }
118
119 static void
120 wl_event_queue_init(struct wl_event_queue *queue, struct wl_display *display)
121 {
122         wl_list_init(&queue->event_list);
123         pthread_cond_init(&queue->cond, NULL);
124         queue->display = display;
125 }
126
127 static void
128 wl_event_queue_release(struct wl_event_queue *queue)
129 {
130         struct wl_closure *closure;
131
132         while (!wl_list_empty(&queue->event_list)) {
133                 closure = container_of(queue->event_list.next,
134                                        struct wl_closure, link);
135                 wl_list_remove(&closure->link);
136                 wl_closure_destroy(closure);
137         }
138         pthread_cond_destroy(&queue->cond);
139 }
140
141 /** Destroy an event queue
142  *
143  * \param queue The event queue to be destroyed
144  *
145  * Destroy the given event queue. Any pending event on that queue is
146  * discarded.
147  *
148  * The \ref wl_display object used to create the queue should not be
149  * destroyed until all event queues created with it are destroyed with
150  * this function.
151  *
152  * \memberof wl_event_queue
153  */
154 WL_EXPORT void
155 wl_event_queue_destroy(struct wl_event_queue *queue)
156 {
157         struct wl_display *display = queue->display;
158
159         pthread_mutex_lock(&display->mutex);
160         wl_list_remove(&queue->link);
161         wl_event_queue_release(queue);
162         free(queue);
163         pthread_mutex_unlock(&display->mutex);
164 }
165
166 /** Create a new event queue for this display
167  *
168  * \param display The display context object
169  * \return A new event queue associated with this display or NULL on
170  * failure.
171  *
172  * \memberof wl_display
173  */
174 WL_EXPORT struct wl_event_queue *
175 wl_display_create_queue(struct wl_display *display)
176 {
177         struct wl_event_queue *queue;
178
179         queue = malloc(sizeof *queue);
180         if (queue == NULL)
181                 return NULL;
182
183         wl_event_queue_init(queue, display);
184
185         pthread_mutex_lock(&display->mutex);
186         wl_list_insert(&display->event_queue_list, &queue->link);
187         pthread_mutex_unlock(&display->mutex);
188
189         return queue;
190 }
191
192 /** Create a proxy object with a given interface
193  *
194  * \param factory Factory proxy object
195  * \param interface Interface the proxy object should use
196  * \return A newly allocated proxy object or NULL on failure
197  *
198  * This function creates a new proxy object with the supplied interface. The
199  * proxy object will have an id assigned from the client id space. The id
200  * should be created on the compositor side by sending an appropriate request
201  * with \ref wl_proxy_marshal().
202  *
203  * The proxy will inherit the display and event queue of the factory object.
204  *
205  * \note This should not normally be used by non-generated code.
206  *
207  * \sa wl_display, wl_event_queue, wl_proxy_marshal()
208  *
209  * \memberof wl_proxy
210  */
211 WL_EXPORT struct wl_proxy *
212 wl_proxy_create(struct wl_proxy *factory, const struct wl_interface *interface)
213 {
214         struct wl_proxy *proxy;
215         struct wl_display *display = factory->display;
216
217         proxy = malloc(sizeof *proxy);
218         if (proxy == NULL)
219                 return NULL;
220
221         proxy->object.interface = interface;
222         proxy->object.implementation = NULL;
223         proxy->display = display;
224         proxy->queue = factory->queue;
225         proxy->flags = 0;
226         proxy->refcount = 1;
227
228         pthread_mutex_lock(&display->mutex);
229         proxy->object.id = wl_map_insert_new(&display->objects,
230                                              WL_MAP_CLIENT_SIDE, proxy);
231         pthread_mutex_unlock(&display->mutex);
232
233         return proxy;
234 }
235
236 /* The caller should hold the display lock */
237 static struct wl_proxy *
238 wl_proxy_create_for_id(struct wl_proxy *factory,
239                        uint32_t id, const struct wl_interface *interface)
240 {
241         struct wl_proxy *proxy;
242         struct wl_display *display = factory->display;
243
244         proxy = malloc(sizeof *proxy);
245         if (proxy == NULL)
246                 return NULL;
247
248         proxy->object.interface = interface;
249         proxy->object.implementation = NULL;
250         proxy->object.id = id;
251         proxy->display = display;
252         proxy->queue = factory->queue;
253         proxy->flags = 0;
254         proxy->refcount = 1;
255
256         wl_map_insert_at(&display->objects, id, proxy);
257
258         return proxy;
259 }
260
261 /** Destroy a proxy object
262  *
263  * \param proxy The proxy to be destroyed
264  *
265  * \memberof wl_proxy
266  */
267 WL_EXPORT void
268 wl_proxy_destroy(struct wl_proxy *proxy)
269 {
270         struct wl_display *display = proxy->display;
271
272         pthread_mutex_lock(&display->mutex);
273
274         if (proxy->flags & WL_PROXY_FLAG_ID_DELETED)
275                 wl_map_remove(&proxy->display->objects, proxy->object.id);
276         else if (proxy->object.id < WL_SERVER_ID_START)
277                 wl_map_insert_at(&proxy->display->objects,
278                                  proxy->object.id, WL_ZOMBIE_OBJECT);
279         else
280                 wl_map_insert_at(&proxy->display->objects,
281                                  proxy->object.id, NULL);
282
283
284         proxy->flags |= WL_PROXY_FLAG_DESTROYED;
285
286         proxy->refcount--;
287         if (!proxy->refcount)
288                 free(proxy);
289
290         pthread_mutex_unlock(&display->mutex);
291 }
292
293 /** Set a proxy's listener
294  *
295  * \param proxy The proxy object
296  * \param implementation The listener to be added to proxy
297  * \param data User data to be associated with the proxy
298  * \return 0 on success or -1 on failure
299  *
300  * Set proxy's listener to \c implementation and its user data to
301  * \c data. Ifa listener has already been set, this functions
302  * fails and nothing is changed.
303  *
304  * \c implementation is a vector of function pointers. For an opcode
305  * \c n, \c implemention[n] should point to the handler of \c n for
306  * the given object.
307  *
308  * \memberof wl_proxy
309  */
310 WL_EXPORT int
311 wl_proxy_add_listener(struct wl_proxy *proxy,
312                       void (**implementation)(void), void *data)
313 {
314         if (proxy->object.implementation) {
315                 fprintf(stderr, "proxy already has listener\n");
316                 return -1;
317         }
318
319         proxy->object.implementation = implementation;
320         proxy->user_data = data;
321
322         return 0;
323 }
324
325 /** Prepare a request to be sent to the compositor
326  *
327  * \param proxy The proxy object
328  * \param opcode Opcode of the request to be sent
329  * \param ... Extra arguments for the given request
330  *
331  * Translates the request given by opcode and the extra arguments into the
332  * wire format and write it to the connection buffer.
333  *
334  * The example below creates a proxy object with the wl_surface_interface
335  * using a wl_compositor factory interface and sends the
336  * \c compositor.create_surface request using \ref wl_proxy_marshal(). Note
337  * the \c id is the extra argument to the request as specified by the
338  * protocol.
339  *
340  * \code
341  * id = wl_proxy_create((struct wl_proxy *) wl_compositor,
342  *                      &wl_surface_interface);
343  * wl_proxy_marshal((struct wl_proxy *) wl_compositor,
344  *                  WL_COMPOSITOR_CREATE_SURFACE, id);
345  * \endcode
346  *
347  * \note This should not normally be used by non-generated code.
348  *
349  * \sa wl_proxy_create()
350  *
351  * \memberof wl_proxy
352  */
353 WL_EXPORT void
354 wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
355 {
356         struct wl_closure *closure;
357         va_list ap;
358
359         pthread_mutex_lock(&proxy->display->mutex);
360
361         va_start(ap, opcode);
362         closure = wl_closure_vmarshal(&proxy->object, opcode, ap,
363                                       &proxy->object.interface->methods[opcode]);
364         va_end(ap);
365
366         if (closure == NULL) {
367                 fprintf(stderr, "Error marshalling request\n");
368                 abort();
369         }
370
371         if (wl_debug)
372                 wl_closure_print(closure, &proxy->object, true);
373
374         if (wl_closure_send(closure, proxy->display->connection)) {
375                 fprintf(stderr, "Error sending request: %m\n");
376                 abort();
377         }
378
379         wl_closure_destroy(closure);
380
381         pthread_mutex_unlock(&proxy->display->mutex);
382 }
383
384 static void
385 display_handle_error(void *data,
386                      struct wl_display *display, struct wl_object *object,
387                      uint32_t code, const char *message)
388 {
389         int err;
390
391         wl_log("%s@%u: error %d: %s\n",
392                object->interface->name, object->id, code, message);
393
394         switch (code) {
395         case WL_DISPLAY_ERROR_INVALID_OBJECT:
396         case WL_DISPLAY_ERROR_INVALID_METHOD:
397                 err = -EINVAL;
398                 break;
399         case WL_DISPLAY_ERROR_NO_MEMORY:
400                 err = -ENOMEM;
401                 break;
402         default:
403                 err = -EFAULT;
404                 break;
405         }
406
407         wl_display_fatal_error(display, err);
408 }
409
410 static void
411 display_handle_delete_id(void *data, struct wl_display *display, uint32_t id)
412 {
413         struct wl_proxy *proxy;
414
415         pthread_mutex_lock(&display->mutex);
416
417         proxy = wl_map_lookup(&display->objects, id);
418         if (proxy != WL_ZOMBIE_OBJECT)
419                 proxy->flags |= WL_PROXY_FLAG_ID_DELETED;
420         else
421                 wl_map_remove(&display->objects, id);
422
423         pthread_mutex_unlock(&display->mutex);
424 }
425
426 static const struct wl_display_listener display_listener = {
427         display_handle_error,
428         display_handle_delete_id
429 };
430
431 static int
432 connect_to_socket(const char *name)
433 {
434         struct sockaddr_un addr;
435         socklen_t size;
436         const char *runtime_dir;
437         int name_size, fd;
438
439         runtime_dir = getenv("XDG_RUNTIME_DIR");
440         if (!runtime_dir) {
441                 fprintf(stderr,
442                         "error: XDG_RUNTIME_DIR not set in the environment.\n");
443
444                 /* to prevent programs reporting
445                  * "failed to create display: Success" */
446                 errno = ENOENT;
447                 return -1;
448         }
449
450         if (name == NULL)
451                 name = getenv("WAYLAND_DISPLAY");
452         if (name == NULL)
453                 name = "wayland-0";
454
455         fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
456         if (fd < 0)
457                 return -1;
458
459         memset(&addr, 0, sizeof addr);
460         addr.sun_family = AF_LOCAL;
461         name_size =
462                 snprintf(addr.sun_path, sizeof addr.sun_path,
463                          "%s/%s", runtime_dir, name) + 1;
464
465         assert(name_size > 0);
466         if (name_size > (int)sizeof addr.sun_path) {
467                 fprintf(stderr,
468                        "error: socket path \"%s/%s\" plus null terminator"
469                        " exceeds 108 bytes\n", runtime_dir, name);
470                 close(fd);
471                 /* to prevent programs reporting
472                  * "failed to add socket: Success" */
473                 errno = ENAMETOOLONG;
474                 return -1;
475         };
476
477         size = offsetof (struct sockaddr_un, sun_path) + name_size;
478
479         if (connect(fd, (struct sockaddr *) &addr, size) < 0) {
480                 close(fd);
481                 return -1;
482         }
483
484         return fd;
485 }
486
487 /** Connect to Wayland display on an already open fd
488  *
489  * \param fd The fd to use for the connection
490  * \return A \ref wl_display object or \c NULL on failure
491  *
492  * The wl_display takes ownership of the fd and will close it when the
493  * display is destroyed.  The fd will also be closed in case of
494  * failure.
495  *
496  * \memberof wl_display
497  */
498 WL_EXPORT struct wl_display *
499 wl_display_connect_to_fd(int fd)
500 {
501         struct wl_display *display;
502         const char *debug;
503
504         debug = getenv("WAYLAND_DEBUG");
505         if (debug)
506                 wl_debug = 1;
507
508         display = malloc(sizeof *display);
509         if (display == NULL) {
510                 close(fd);
511                 return NULL;
512         }
513
514         memset(display, 0, sizeof *display);
515
516         display->fd = fd;
517         wl_map_init(&display->objects);
518         wl_event_queue_init(&display->queue, display);
519         wl_list_init(&display->event_queue_list);
520         pthread_mutex_init(&display->mutex, NULL);
521
522         wl_map_insert_new(&display->objects, WL_MAP_CLIENT_SIDE, NULL);
523
524         display->proxy.object.interface = &wl_display_interface;
525         display->proxy.object.id =
526                 wl_map_insert_new(&display->objects,
527                                   WL_MAP_CLIENT_SIDE, display);
528         display->proxy.display = display;
529         display->proxy.object.implementation = (void(**)(void)) &display_listener;
530         display->proxy.user_data = display;
531         display->proxy.queue = &display->queue;
532         display->proxy.flags = 0;
533         display->proxy.refcount = 1;
534
535         display->connection = wl_connection_create(display->fd);
536         if (display->connection == NULL) {
537                 wl_map_release(&display->objects);
538                 close(display->fd);
539                 free(display);
540                 return NULL;
541         }
542
543         return display;
544 }
545
546 /** Connect to a Wayland display
547  *
548  * \param name Name of the Wayland display to connect to
549  * \return A \ref wl_display object or \c NULL on failure
550  *
551  * Connect to the Wayland display named \c name. If \c name is \c NULL,
552  * its value will bee replaced with the WAYLAND_DISPLAY environment
553  * variable if it is set, otherwise display "wayland-0" will be used.
554  *
555  * \memberof wl_display
556  */
557 WL_EXPORT struct wl_display *
558 wl_display_connect(const char *name)
559 {
560         char *connection, *end;
561         int flags, fd;
562
563         connection = getenv("WAYLAND_SOCKET");
564         if (connection) {
565                 fd = strtol(connection, &end, 0);
566                 if (*end != '\0')
567                         return NULL;
568
569                 flags = fcntl(fd, F_GETFD);
570                 if (flags != -1)
571                         fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
572                 unsetenv("WAYLAND_SOCKET");
573         } else {
574                 fd = connect_to_socket(name);
575                 if (fd < 0)
576                         return NULL;
577         }
578
579         return wl_display_connect_to_fd(fd);
580 }
581
582 /** Close a connection to a Wayland display
583  *
584  * \param display The display context object
585  *
586  * Close the connection to \c display and free all resources associated
587  * with it.
588  *
589  * \memberof wl_display
590  */
591 WL_EXPORT void
592 wl_display_disconnect(struct wl_display *display)
593 {
594         wl_connection_destroy(display->connection);
595         wl_map_release(&display->objects);
596         wl_event_queue_release(&display->queue);
597         pthread_mutex_destroy(&display->mutex);
598         if (display->fd > 0)
599                 close(display->fd);
600
601         free(display);
602 }
603
604 /** Get a display context's file descriptor
605  *
606  * \param display The display context object
607  * \return Display object file descriptor
608  *
609  * Return the file descriptor associated with a display so it can be
610  * integrated into the client's main loop.
611  *
612  * \memberof wl_display
613  */
614 WL_EXPORT int
615 wl_display_get_fd(struct wl_display *display)
616 {
617         return display->fd;
618 }
619
620 static void
621 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
622 {
623    int *done = data;
624
625    *done = 1;
626    wl_callback_destroy(callback);
627 }
628
629 static const struct wl_callback_listener sync_listener = {
630         sync_callback
631 };
632
633 /** Block until all pending request are processed by the server
634  *
635  * \param display The display context object
636  * \return The number of dispatched events on success or -1 on failure
637  *
638  * Blocks until the server process all currently issued requests and
639  * sends out pending events on all event queues.
640  *
641  * \memberof wl_display
642  */
643 WL_EXPORT int
644 wl_display_roundtrip(struct wl_display *display)
645 {
646         struct wl_callback *callback;
647         int done, ret = 0;
648
649         done = 0;
650         callback = wl_display_sync(display);
651         wl_callback_add_listener(callback, &sync_listener, &done);
652         while (!done && !ret)
653                 ret = wl_display_dispatch(display);
654
655         return ret;
656 }
657
658 static int
659 create_proxies(struct wl_proxy *sender, struct wl_closure *closure)
660 {
661         struct wl_proxy *proxy;
662         const char *signature;
663         struct argument_details arg;
664         uint32_t id;
665         int i;
666         int count;
667
668         signature = closure->message->signature;
669         count = arg_count_for_signature(signature) + 2;
670         for (i = 2; i < count; i++) {
671                 signature = get_next_argument(signature, &arg);
672                 switch (arg.type) {
673                 case 'n':
674                         id = **(uint32_t **) closure->args[i];
675                         if (id == 0) {
676                                 *(void **) closure->args[i] = NULL;
677                                 break;
678                         }
679                         proxy = wl_proxy_create_for_id(sender, id,
680                                                        closure->message->types[i - 2]);
681                         if (proxy == NULL)
682                                 return -1;
683                         *(void **) closure->args[i] = proxy;
684                         break;
685                 default:
686                         break;
687                 }
688         }
689
690         return 0;
691 }
692
693 static void
694 increase_closure_args_refcount(struct wl_closure *closure)
695 {
696         const char *signature;
697         struct argument_details arg;
698         int i, count;
699         struct wl_proxy *proxy;
700
701         signature = closure->message->signature;
702         count = arg_count_for_signature(signature) + 2;
703         for (i = 2; i < count; i++) {
704                 signature = get_next_argument(signature, &arg);
705                 switch (arg.type) {
706                 case 'n':
707                 case 'o':
708                         proxy = *(struct wl_proxy **) closure->args[i];
709                         if (proxy)
710                                 proxy->refcount++;
711                         break;
712                 default:
713                         break;
714                 }
715         }
716 }
717
718 static int
719 queue_event(struct wl_display *display, int len)
720 {
721         uint32_t p[2], id;
722         int opcode, size;
723         struct wl_proxy *proxy;
724         struct wl_closure *closure;
725         const struct wl_message *message;
726
727         wl_connection_copy(display->connection, p, sizeof p);
728         id = p[0];
729         opcode = p[1] & 0xffff;
730         size = p[1] >> 16;
731         if (len < size)
732                 return 0;
733
734         proxy = wl_map_lookup(&display->objects, id);
735         if (proxy == WL_ZOMBIE_OBJECT) {
736                 wl_connection_consume(display->connection, size);
737                 return size;
738         } else if (proxy == NULL) {
739                 wl_connection_consume(display->connection, size);
740                 return size;
741         }
742
743         message = &proxy->object.interface->events[opcode];
744         closure = wl_connection_demarshal(display->connection, size,
745                                           &display->objects, message);
746         if (!closure)
747                 return -1;
748
749         if (create_proxies(proxy, closure) < 0) {
750                 wl_closure_destroy(closure);
751                 return -1;
752         }
753
754         if (wl_closure_lookup_objects(closure, &display->objects) != 0) {
755                 wl_closure_destroy(closure);
756                 return -1;
757         }
758
759         increase_closure_args_refcount(closure);
760         proxy->refcount++;
761         closure->proxy = proxy;
762
763         if (wl_list_empty(&proxy->queue->event_list))
764                 pthread_cond_signal(&proxy->queue->cond);
765         wl_list_insert(proxy->queue->event_list.prev, &closure->link);
766
767         return size;
768 }
769
770 static void
771 decrease_closure_args_refcount(struct wl_closure *closure)
772 {
773         const char *signature;
774         struct argument_details arg;
775         int i, count;
776         struct wl_proxy *proxy;
777
778         signature = closure->message->signature;
779         count = arg_count_for_signature(signature) + 2;
780         for (i = 2; i < count; i++) {
781                 signature = get_next_argument(signature, &arg);
782                 switch (arg.type) {
783                 case 'n':
784                 case 'o':
785                         proxy = *(struct wl_proxy **) closure->args[i];
786                         if (proxy) {
787                                 if (proxy->flags & WL_PROXY_FLAG_DESTROYED)
788                                         *(void **) closure->args[i] = NULL;
789
790                                 proxy->refcount--;
791                                 if (!proxy->refcount)
792                                         free(proxy);
793                         }
794                         break;
795                 default:
796                         break;
797                 }
798         }
799 }
800
801 static void
802 dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
803 {
804         struct wl_closure *closure;
805         struct wl_proxy *proxy;
806         int opcode;
807         bool proxy_destroyed;
808
809         closure = container_of(queue->event_list.next,
810                                struct wl_closure, link);
811         wl_list_remove(&closure->link);
812         opcode = closure->buffer[1] & 0xffff;
813
814         /* Verify that the receiving object is still valid by checking if has
815          * been destroyed by the application. */
816
817         decrease_closure_args_refcount(closure);
818         proxy = closure->proxy;
819         proxy_destroyed = !!(proxy->flags & WL_PROXY_FLAG_DESTROYED);
820
821         proxy->refcount--;
822         if (!proxy->refcount)
823                 free(proxy);
824
825         if (proxy_destroyed) {
826                 wl_closure_destroy(closure);
827                 return;
828         }
829
830         pthread_mutex_unlock(&display->mutex);
831
832         if (proxy->object.implementation) {
833                 if (wl_debug)
834                         wl_closure_print(closure, &proxy->object, false);
835
836                 wl_closure_invoke(closure, &proxy->object,
837                                   proxy->object.implementation[opcode],
838                                   proxy->user_data);
839         }
840
841         wl_closure_destroy(closure);
842
843         pthread_mutex_lock(&display->mutex);
844 }
845
846 static int
847 dispatch_queue(struct wl_display *display,
848                struct wl_event_queue *queue, int block)
849 {
850         int len, size, count, ret;
851
852         pthread_mutex_lock(&display->mutex);
853
854         if (display->last_error)
855                 goto err_unlock;
856
857         ret = wl_connection_flush(display->connection);
858         if (ret < 0 && errno != EAGAIN) {
859                 display_fatal_error(display, errno);
860                 goto err_unlock;
861         }
862
863         if (block && wl_list_empty(&queue->event_list) &&
864             pthread_equal(display->display_thread, pthread_self())) {
865                 len = wl_connection_read(display->connection);
866                 if (len == -1) {
867                         display_fatal_error(display, errno);
868                         goto err_unlock;
869                 } else if (len == 0) {
870                         display_fatal_error(display, EPIPE);
871                         goto err_unlock;
872                 }
873                 while (len >= 8) {
874                         size = queue_event(display, len);
875                         if (size == -1) {
876                                 display_fatal_error(display, errno);
877                                 goto err_unlock;
878                         } else if (size == 0) {
879                                 break;
880                         }
881                         len -= size;
882                 }
883         } else if (block && wl_list_empty(&queue->event_list)) {
884                 pthread_cond_wait(&queue->cond, &display->mutex);
885                 if (display->last_error)
886                         goto err_unlock;
887         }
888
889         for (count = 0; !wl_list_empty(&queue->event_list); count++) {
890                 dispatch_event(display, queue);
891                 if (display->last_error)
892                         goto err_unlock;
893         }
894
895         pthread_mutex_unlock(&display->mutex);
896
897         return count;
898
899 err_unlock:
900         errno = display->last_error;
901         pthread_mutex_unlock(&display->mutex);
902
903         return -1;
904 }
905
906 /** Dispatch events in an event queue
907  *
908  * \param display The display context object
909  * \param queue The event queue to dispatch
910  * \return The number of dispatched events on success or -1 on failure
911  *
912  * Dispatch all incoming events for objects assigned to the given
913  * event queue. On failure -1 is returned and errno set appropriately.
914  *
915  * This function blocks if there are no events to dispatch. If calling from
916  * the main thread, it will block reading data from the display fd. For other
917  * threads this will block until the main thread queues events on the queue
918  * passed as argument.
919  *
920  * \memberof wl_display
921  */
922 WL_EXPORT int
923 wl_display_dispatch_queue(struct wl_display *display,
924                           struct wl_event_queue *queue)
925 {
926         return dispatch_queue(display, queue, 1);
927 }
928
929 /** Process incoming events
930  *
931  * \param display The display context object
932  * \return The number of dispatched events on success or -1 on failure
933  *
934  * Dispatch the display's main event queue.
935  *
936  * If the main event queue is empty, this function blocks until there are
937  * events to be read from the display fd. Events are read and queued on
938  * the appropriate event queues. Finally, events on the main event queue
939  * are dispatched.
940  *
941  * \note It is not possible to check if there are events on the main queue
942  * or not. For dispatching main queue events without blocking, see \ref
943  * wl_display_dispatch_pending().
944  *
945  * \note Calling this makes the current thread the main one.
946  *
947  * \sa wl_display_dispatch_pending(), wl_display_dispatch_queue()
948  *
949  * \memberof wl_display
950  */
951 WL_EXPORT int
952 wl_display_dispatch(struct wl_display *display)
953 {
954         display->display_thread = pthread_self();
955
956         return dispatch_queue(display, &display->queue, 1);
957 }
958
959 /** Dispatch main queue events without reading from the display fd
960  *
961  * \param display The display context object
962  * \return The number of dispatched events or -1 on failure
963  *
964  * This function dispatches events on the main event queue. It does not
965  * attempt to read the display fd and simply returns zero if the main
966  * queue is empty, i.e., it doesn't block.
967  *
968  * This is necessary when a client's main loop wakes up on some fd other
969  * than the display fd (network socket, timer fd, etc) and calls \ref
970  * wl_display_dispatch_queue() from that callback. This may queue up
971  * events in the main queue while reading all data from the display fd.
972  * When the main thread returns to the main loop to block, the display fd
973  * no longer has data, causing a call to \em poll(2) (or similar
974  * functions) to block indefinitely, even though there are events ready
975  * to dispatch.
976  *
977  * To proper integrate the wayland display fd into a main loop, the
978  * client should always call \ref wl_display_dispatch_pending() and then
979  * \ref wl_display_flush() prior to going back to sleep. At that point,
980  * the fd typically doesn't have data so attempting I/O could block, but
981  * events queued up on the main queue should be dispatched.
982  *
983  * A real-world example is a main loop that wakes up on a timerfd (or a
984  * sound card fd becoming writable, for example in a video player), which
985  * then triggers GL rendering and eventually eglSwapBuffers().
986  * eglSwapBuffers() may call wl_display_dispatch_queue() if it didn't
987  * receive the frame event for the previous frame, and as such queue
988  * events in the main queue.
989  *
990  * \note Calling this makes the current thread the main one.
991  *
992  * \sa wl_display_dispatch(), wl_display_dispatch_queue(),
993  * wl_display_flush()
994  *
995  * \memberof wl_display
996  */
997 WL_EXPORT int
998 wl_display_dispatch_pending(struct wl_display *display)
999 {
1000         display->display_thread = pthread_self();
1001
1002         return dispatch_queue(display, &display->queue, 0);
1003 }
1004
1005 /** Retrieve the last error occured on a display
1006  *
1007  * \param display The display context object
1008  * \return The last error occured on \c display or 0 if no error occured
1009  *
1010  * Return the last error occured on the display. This may be an error sent
1011  * by the server or caused by the local client.
1012  *
1013  * \note Errors are \b fatal. If this function returns non-zero the display
1014  * can no longer be used.
1015  *
1016  * \memberof wl_display
1017  */
1018 WL_EXPORT int
1019 wl_display_get_error(struct wl_display *display)
1020 {
1021         int ret;
1022
1023         pthread_mutex_lock(&display->mutex);
1024
1025         ret = display->last_error;
1026
1027         pthread_mutex_unlock(&display->mutex);
1028
1029         return ret;
1030 }
1031
1032 /** Send all buffered request on the display to the server
1033  *
1034  * \param display The display context object
1035  * \return The number of bytes send on success or -1 on failure
1036  *
1037  * Send all buffered data on the client side to the server. Clients
1038  * should call this function before blocking. On success, the number
1039  * of bytes sent to the server is returned. On failure, this
1040  * function returns -1 and errno is set appropriately.
1041  *
1042  * \memberof wl_display
1043  */
1044 WL_EXPORT int
1045 wl_display_flush(struct wl_display *display)
1046 {
1047         int ret;
1048
1049         pthread_mutex_lock(&display->mutex);
1050
1051         if (display->last_error) {
1052                 errno = display->last_error;
1053                 ret = -1;
1054         } else {
1055                 ret = wl_connection_flush(display->connection);
1056                 if (ret < 0 && errno != EAGAIN)
1057                         display_fatal_error(display, errno);
1058         }
1059
1060         pthread_mutex_unlock(&display->mutex);
1061
1062         return ret;
1063 }
1064
1065 /** Set the user data associated with a proxy
1066  *
1067  * \param proxy The proxy object
1068  * \param user_data The data to be associated with proxy
1069  *
1070  * Set the user data associated with \c proxy. When events for this
1071  * proxy are received, \c user_data will be supplied to its listener.
1072  *
1073  * \memberof wl_proxy
1074  */
1075 WL_EXPORT void
1076 wl_proxy_set_user_data(struct wl_proxy *proxy, void *user_data)
1077 {
1078         proxy->user_data = user_data;
1079 }
1080
1081 /** Get the user data associated with a proxy
1082  *
1083  * \param proxy The proxy object
1084  * \return The user data associated with proxy
1085  *
1086  * \memberof wl_proxy
1087  */
1088 WL_EXPORT void *
1089 wl_proxy_get_user_data(struct wl_proxy *proxy)
1090 {
1091         return proxy->user_data;
1092 }
1093
1094 /** Get the id of a proxy object
1095  *
1096  * \param proxy The proxy object
1097  * \return The id the object associated with the proxy
1098  *
1099  * \memberof wl_proxy
1100  */
1101 WL_EXPORT uint32_t
1102 wl_proxy_get_id(struct wl_proxy *proxy)
1103 {
1104         return proxy->object.id;
1105 }
1106
1107
1108 /** Assign a proxy to an event queue
1109  *
1110  * \param proxy The proxy object
1111  * \param queue The event queue that will handle this proxy
1112  *
1113  * Assign proxy to event queue. Events coming from \c proxy will be
1114  * queued in \c queue instead of the display's main queue.
1115  *
1116  * \sa wl_display_dispatch_queue()
1117  *
1118  * \memberof wl_proxy
1119  */
1120 WL_EXPORT void
1121 wl_proxy_set_queue(struct wl_proxy *proxy, struct wl_event_queue *queue)
1122 {
1123         proxy->queue = queue;
1124 }
1125
1126 WL_EXPORT void
1127 wl_log_set_handler_client(wl_log_func_t handler)
1128 {
1129         wl_log_handler = handler;
1130 }