1c660ef809e9bf548e95f53b17b2ea500c98600b
[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 struct wl_proxy {
49         struct wl_object object;
50         struct wl_display *display;
51         struct wl_event_queue *queue;
52         int id_deleted;
53         void *user_data;
54 };
55
56 struct wl_global {
57         uint32_t id;
58         char *interface;
59         uint32_t version;
60         struct wl_list link;
61 };
62
63 struct wl_event_queue {
64         struct wl_list link;
65         struct wl_list event_list;
66         struct wl_display *display;
67         pthread_cond_t cond;
68 };
69
70 struct wl_display {
71         struct wl_proxy proxy;
72         struct wl_connection *connection;
73         int last_error;
74         int fd;
75         pthread_t display_thread;
76         struct wl_map objects;
77         struct wl_event_queue queue;
78         struct wl_list event_queue_list;
79         pthread_mutex_t mutex;
80 };
81
82 /** \endcond */
83
84 static int wl_debug = 0;
85
86 static void
87 display_fatal_error(struct wl_display *display, int error)
88 {
89         struct wl_event_queue *iter;
90
91         if (display->last_error)
92                 return;
93
94         if (!error)
95                 error = 1;
96
97         display->last_error = error;
98         close(display->fd);
99         display->fd = -1;
100
101         wl_list_for_each(iter, &display->event_queue_list, link)
102                 pthread_cond_broadcast(&iter->cond);
103 }
104
105 static void
106 wl_display_fatal_error(struct wl_display *display, int error)
107 {
108         pthread_mutex_lock(&display->mutex);
109         display_fatal_error(display, error);
110         pthread_mutex_unlock(&display->mutex);
111 }
112
113 static void
114 wl_event_queue_init(struct wl_event_queue *queue, struct wl_display *display)
115 {
116         wl_list_init(&queue->event_list);
117         pthread_cond_init(&queue->cond, NULL);
118         queue->display = display;
119 }
120
121 static void
122 wl_event_queue_release(struct wl_event_queue *queue)
123 {
124         struct wl_closure *closure;
125
126         while (!wl_list_empty(&queue->event_list)) {
127                 closure = container_of(queue->event_list.next,
128                                        struct wl_closure, link);
129                 wl_list_remove(&closure->link);
130                 wl_closure_destroy(closure);
131         }
132         pthread_cond_destroy(&queue->cond);
133 }
134
135 /** Destroy an event queue
136  *
137  * \param queue The event queue to be destroyed
138  *
139  * Destroy the given event queue. Any pending event on that queue is
140  * discarded.
141  *
142  * The \ref wl_display object used to create the queue should not be
143  * destroyed until all event queues created with it are destroyed with
144  * this function.
145  *
146  * \memberof wl_event_queue
147  */
148 WL_EXPORT void
149 wl_event_queue_destroy(struct wl_event_queue *queue)
150 {
151         struct wl_display *display = queue->display;
152
153         pthread_mutex_lock(&display->mutex);
154         wl_list_remove(&queue->link);
155         wl_event_queue_release(queue);
156         free(queue);
157         pthread_mutex_unlock(&display->mutex);
158 }
159
160 /** Create a new event queue for this display
161  *
162  * \param display The display context object
163  * \return A new event queue associated with this display or NULL on
164  * failure.
165  *
166  * \memberof wl_display
167  */
168 WL_EXPORT struct wl_event_queue *
169 wl_display_create_queue(struct wl_display *display)
170 {
171         struct wl_event_queue *queue;
172
173         queue = malloc(sizeof *queue);
174         if (queue == NULL)
175                 return NULL;
176
177         wl_event_queue_init(queue, display);
178
179         pthread_mutex_lock(&display->mutex);
180         wl_list_insert(&display->event_queue_list, &queue->link);
181         pthread_mutex_unlock(&display->mutex);
182
183         return queue;
184 }
185
186 /** Create a proxy object with a given interface
187  *
188  * \param factory Factory proxy object
189  * \param interface Interface the proxy object should use
190  * \return A newly allocated proxy object or NULL on failure
191  *
192  * This function creates a new proxy object with the supplied interface. The
193  * proxy object will have an id assigned from the client id space. The id
194  * should be created on the compositor side by sending an appropriate request
195  * with \ref wl_proxy_marshal().
196  *
197  * The proxy will inherit the display and event queue of the factory object.
198  *
199  * \note This should not normally be used by non-generated code.
200  *
201  * \sa wl_display, wl_event_queue, wl_proxy_marshal()
202  *
203  * \memberof wl_proxy
204  */
205 WL_EXPORT struct wl_proxy *
206 wl_proxy_create(struct wl_proxy *factory, const struct wl_interface *interface)
207 {
208         struct wl_proxy *proxy;
209         struct wl_display *display = factory->display;
210
211         proxy = malloc(sizeof *proxy);
212         if (proxy == NULL)
213                 return NULL;
214
215         proxy->object.interface = interface;
216         proxy->object.implementation = NULL;
217         proxy->display = display;
218         proxy->queue = factory->queue;
219         proxy->id_deleted = 0;
220
221         pthread_mutex_lock(&display->mutex);
222         proxy->object.id = wl_map_insert_new(&display->objects,
223                                              WL_MAP_CLIENT_SIDE, proxy);
224         pthread_mutex_unlock(&display->mutex);
225
226         return proxy;
227 }
228
229 /* The caller should hold the display lock */
230 static struct wl_proxy *
231 wl_proxy_create_for_id(struct wl_proxy *factory,
232                        uint32_t id, const struct wl_interface *interface)
233 {
234         struct wl_proxy *proxy;
235         struct wl_display *display = factory->display;
236
237         proxy = malloc(sizeof *proxy);
238         if (proxy == NULL)
239                 return NULL;
240
241         proxy->object.interface = interface;
242         proxy->object.implementation = NULL;
243         proxy->object.id = id;
244         proxy->display = display;
245         proxy->queue = factory->queue;
246         proxy->id_deleted = 0;
247
248         wl_map_insert_at(&display->objects, id, proxy);
249
250         return proxy;
251 }
252
253 /** Destroy a proxy object
254  *
255  * \param proxy The proxy to be destroyed
256  *
257  * \memberof wl_proxy
258  */
259 WL_EXPORT void
260 wl_proxy_destroy(struct wl_proxy *proxy)
261 {
262         pthread_mutex_lock(&proxy->display->mutex);
263
264         if (proxy->id_deleted)
265                 wl_map_remove(&proxy->display->objects, proxy->object.id);
266         else if (proxy->object.id < WL_SERVER_ID_START)
267                 wl_map_insert_at(&proxy->display->objects,
268                                  proxy->object.id, WL_ZOMBIE_OBJECT);
269         else
270                 wl_map_insert_at(&proxy->display->objects,
271                                  proxy->object.id, NULL);
272
273         pthread_mutex_unlock(&proxy->display->mutex);
274
275         free(proxy);
276 }
277
278 /** Set a proxy's listener
279  *
280  * \param proxy The proxy object
281  * \param implementation The listener to be added to proxy
282  * \param data User data to be associated with the proxy
283  * \return 0 on success or -1 on failure
284  *
285  * Set proxy's listener to \c implementation and its user data to
286  * \c data. Ifa listener has already been set, this functions
287  * fails and nothing is changed.
288  *
289  * \c implementation is a vector of function pointers. For an opcode
290  * \c n, \c implemention[n] should point to the handler of \c n for
291  * the given object.
292  *
293  * \memberof wl_proxy
294  */
295 WL_EXPORT int
296 wl_proxy_add_listener(struct wl_proxy *proxy,
297                       void (**implementation)(void), void *data)
298 {
299         if (proxy->object.implementation) {
300                 fprintf(stderr, "proxy already has listener\n");
301                 return -1;
302         }
303
304         proxy->object.implementation = implementation;
305         proxy->user_data = data;
306
307         return 0;
308 }
309
310 /** Prepare a request to be sent to the compositor
311  *
312  * \param proxy The proxy object
313  * \param opcode Opcode of the request to be sent
314  * \param ... Extra arguments for the given request
315  *
316  * Translates the request given by opcode and the extra arguments into the
317  * wire format and write it to the connection buffer.
318  *
319  * The example below creates a proxy object with the wl_surface_interface
320  * using a wl_compositor factory interface and sends the
321  * \c compositor.create_surface request using \ref wl_proxy_marshal(). Note
322  * the \c id is the extra argument to the request as specified by the
323  * protocol.
324  *
325  * \code
326  * id = wl_proxy_create((struct wl_proxy *) wl_compositor,
327  *                      &wl_surface_interface);
328  * wl_proxy_marshal((struct wl_proxy *) wl_compositor,
329  *                  WL_COMPOSITOR_CREATE_SURFACE, id);
330  * \endcode
331  *
332  * \note This should not normally be used by non-generated code.
333  *
334  * \sa wl_proxy_create()
335  *
336  * \memberof wl_proxy
337  */
338 WL_EXPORT void
339 wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
340 {
341         struct wl_closure *closure;
342         va_list ap;
343
344         pthread_mutex_lock(&proxy->display->mutex);
345
346         va_start(ap, opcode);
347         closure = wl_closure_vmarshal(&proxy->object, opcode, ap,
348                                       &proxy->object.interface->methods[opcode]);
349         va_end(ap);
350
351         if (closure == NULL) {
352                 fprintf(stderr, "Error marshalling request\n");
353                 abort();
354         }
355
356         if (wl_debug)
357                 wl_closure_print(closure, &proxy->object, true);
358
359         if (wl_closure_send(closure, proxy->display->connection)) {
360                 fprintf(stderr, "Error sending request: %m\n");
361                 abort();
362         }
363
364         wl_closure_destroy(closure);
365
366         pthread_mutex_unlock(&proxy->display->mutex);
367 }
368
369 static void
370 display_handle_error(void *data,
371                      struct wl_display *display, struct wl_object *object,
372                      uint32_t code, const char *message)
373 {
374         int err;
375
376         wl_log("%s@%u: error %d: %s\n",
377                object->interface->name, object->id, code, message);
378
379         switch (code) {
380         case WL_DISPLAY_ERROR_INVALID_OBJECT:
381         case WL_DISPLAY_ERROR_INVALID_METHOD:
382                 err = -EINVAL;
383                 break;
384         case WL_DISPLAY_ERROR_NO_MEMORY:
385                 err = -ENOMEM;
386                 break;
387         default:
388                 err = -EFAULT;
389                 break;
390         }
391
392         wl_display_fatal_error(display, err);
393 }
394
395 static void
396 display_handle_delete_id(void *data, struct wl_display *display, uint32_t id)
397 {
398         struct wl_proxy *proxy;
399
400         pthread_mutex_lock(&display->mutex);
401
402         proxy = wl_map_lookup(&display->objects, id);
403         if (proxy != WL_ZOMBIE_OBJECT)
404                 proxy->id_deleted = 1;
405         else
406                 wl_map_remove(&display->objects, id);
407
408         pthread_mutex_unlock(&display->mutex);
409 }
410
411 static const struct wl_display_listener display_listener = {
412         display_handle_error,
413         display_handle_delete_id
414 };
415
416 static int
417 connect_to_socket(const char *name)
418 {
419         struct sockaddr_un addr;
420         socklen_t size;
421         const char *runtime_dir;
422         int name_size, fd;
423
424         runtime_dir = getenv("XDG_RUNTIME_DIR");
425         if (!runtime_dir) {
426                 fprintf(stderr,
427                         "error: XDG_RUNTIME_DIR not set in the environment.\n");
428
429                 /* to prevent programs reporting
430                  * "failed to create display: Success" */
431                 errno = ENOENT;
432                 return -1;
433         }
434
435         if (name == NULL)
436                 name = getenv("WAYLAND_DISPLAY");
437         if (name == NULL)
438                 name = "wayland-0";
439
440         fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
441         if (fd < 0)
442                 return -1;
443
444         memset(&addr, 0, sizeof addr);
445         addr.sun_family = AF_LOCAL;
446         name_size =
447                 snprintf(addr.sun_path, sizeof addr.sun_path,
448                          "%s/%s", runtime_dir, name) + 1;
449
450         assert(name_size > 0);
451         if (name_size > (int)sizeof addr.sun_path) {
452                 fprintf(stderr,
453                        "error: socket path \"%s/%s\" plus null terminator"
454                        " exceeds 108 bytes\n", runtime_dir, name);
455                 close(fd);
456                 /* to prevent programs reporting
457                  * "failed to add socket: Success" */
458                 errno = ENAMETOOLONG;
459                 return -1;
460         };
461
462         size = offsetof (struct sockaddr_un, sun_path) + name_size;
463
464         if (connect(fd, (struct sockaddr *) &addr, size) < 0) {
465                 close(fd);
466                 return -1;
467         }
468
469         return fd;
470 }
471
472 /** Connect to Wayland display on an already open fd
473  *
474  * \param fd The fd to use for the connection
475  * \return A \ref wl_display object or \c NULL on failure
476  *
477  * The wl_display takes ownership of the fd and will close it when the
478  * display is destroyed.  The fd will also be closed in case of
479  * failure.
480  *
481  * \memberof wl_display
482  */
483 WL_EXPORT struct wl_display *
484 wl_display_connect_to_fd(int fd)
485 {
486         struct wl_display *display;
487         const char *debug;
488
489         debug = getenv("WAYLAND_DEBUG");
490         if (debug)
491                 wl_debug = 1;
492
493         display = malloc(sizeof *display);
494         if (display == NULL) {
495                 close(fd);
496                 return NULL;
497         }
498
499         memset(display, 0, sizeof *display);
500
501         display->fd = fd;
502         wl_map_init(&display->objects);
503         wl_event_queue_init(&display->queue, display);
504         wl_list_init(&display->event_queue_list);
505         pthread_mutex_init(&display->mutex, NULL);
506
507         wl_map_insert_new(&display->objects, WL_MAP_CLIENT_SIDE, NULL);
508
509         display->proxy.object.interface = &wl_display_interface;
510         display->proxy.object.id =
511                 wl_map_insert_new(&display->objects,
512                                   WL_MAP_CLIENT_SIDE, display);
513         display->proxy.display = display;
514         display->proxy.object.implementation = (void(**)(void)) &display_listener;
515         display->proxy.user_data = display;
516         display->proxy.queue = &display->queue;
517
518         display->connection = wl_connection_create(display->fd);
519         if (display->connection == NULL) {
520                 wl_map_release(&display->objects);
521                 close(display->fd);
522                 free(display);
523                 return NULL;
524         }
525
526         return display;
527 }
528
529 /** Connect to a Wayland display
530  *
531  * \param name Name of the Wayland display to connect to
532  * \return A \ref wl_display object or \c NULL on failure
533  *
534  * Connect to the Wayland display named \c name. If \c name is \c NULL,
535  * its value will bee replaced with the WAYLAND_DISPLAY environment
536  * variable if it is set, otherwise display "wayland-0" will be used.
537  *
538  * \memberof wl_display
539  */
540 WL_EXPORT struct wl_display *
541 wl_display_connect(const char *name)
542 {
543         char *connection, *end;
544         int flags, fd;
545
546         connection = getenv("WAYLAND_SOCKET");
547         if (connection) {
548                 fd = strtol(connection, &end, 0);
549                 if (*end != '\0')
550                         return NULL;
551
552                 flags = fcntl(fd, F_GETFD);
553                 if (flags != -1)
554                         fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
555                 unsetenv("WAYLAND_SOCKET");
556         } else {
557                 fd = connect_to_socket(name);
558                 if (fd < 0)
559                         return NULL;
560         }
561
562         return wl_display_connect_to_fd(fd);
563 }
564
565 /** Close a connection to a Wayland display
566  *
567  * \param display The display context object
568  *
569  * Close the connection to \c display and free all resources associated
570  * with it.
571  *
572  * \memberof wl_display
573  */
574 WL_EXPORT void
575 wl_display_disconnect(struct wl_display *display)
576 {
577         wl_connection_destroy(display->connection);
578         wl_map_release(&display->objects);
579         wl_event_queue_release(&display->queue);
580         pthread_mutex_destroy(&display->mutex);
581         if (display->fd > 0)
582                 close(display->fd);
583
584         free(display);
585 }
586
587 /** Get a display context's file descriptor
588  *
589  * \param display The display context object
590  * \return Display object file descriptor
591  *
592  * Return the file descriptor associated with a display so it can be
593  * integrated into the client's main loop.
594  *
595  * \memberof wl_display
596  */
597 WL_EXPORT int
598 wl_display_get_fd(struct wl_display *display)
599 {
600         return display->fd;
601 }
602
603 static void
604 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
605 {
606    int *done = data;
607
608    *done = 1;
609    wl_callback_destroy(callback);
610 }
611
612 static const struct wl_callback_listener sync_listener = {
613         sync_callback
614 };
615
616 /** Block until all pending request are processed by the server
617  *
618  * \param display The display context object
619  *
620  * Blocks until the server process all currently issued requests and
621  * sends out pending events on all event queues.
622  *
623  * \memberof wl_display
624  */
625 WL_EXPORT int
626 wl_display_roundtrip(struct wl_display *display)
627 {
628         struct wl_callback *callback;
629         int done, ret = 0;
630
631         done = 0;
632         callback = wl_display_sync(display);
633         wl_callback_add_listener(callback, &sync_listener, &done);
634         while (!done && !ret)
635                 ret = wl_display_dispatch(display);
636
637         return ret;
638 }
639
640 static int
641 create_proxies(struct wl_proxy *sender, struct wl_closure *closure)
642 {
643         struct wl_proxy *proxy;
644         const char *signature;
645         struct argument_details arg;
646         uint32_t id;
647         int i;
648         int count;
649
650         signature = closure->message->signature;
651         count = arg_count_for_signature(signature) + 2;
652         for (i = 2; i < count; i++) {
653                 signature = get_next_argument(signature, &arg);
654                 switch (arg.type) {
655                 case 'n':
656                         id = **(uint32_t **) closure->args[i];
657                         if (id == 0) {
658                                 *(void **) closure->args[i] = NULL;
659                                 break;
660                         }
661                         proxy = wl_proxy_create_for_id(sender, id,
662                                                        closure->message->types[i - 2]);
663                         if (proxy == NULL)
664                                 return -1;
665                         *(void **) closure->args[i] = proxy;
666                         break;
667                 default:
668                         break;
669                 }
670         }
671
672         return 0;
673 }
674
675 static int
676 queue_event(struct wl_display *display, int len)
677 {
678         uint32_t p[2], id;
679         int opcode, size;
680         struct wl_proxy *proxy;
681         struct wl_closure *closure;
682         const struct wl_message *message;
683
684         wl_connection_copy(display->connection, p, sizeof p);
685         id = p[0];
686         opcode = p[1] & 0xffff;
687         size = p[1] >> 16;
688         if (len < size)
689                 return 0;
690
691         proxy = wl_map_lookup(&display->objects, id);
692         if (proxy == WL_ZOMBIE_OBJECT) {
693                 wl_connection_consume(display->connection, size);
694                 return size;
695         } else if (proxy == NULL) {
696                 wl_connection_consume(display->connection, size);
697                 return size;
698         }
699
700         message = &proxy->object.interface->events[opcode];
701         closure = wl_connection_demarshal(display->connection, size,
702                                           &display->objects, message);
703         if (!closure)
704                 return -1;
705
706         if (create_proxies(proxy, closure) < 0) {
707                 wl_closure_destroy(closure);
708                 return -1;
709         }
710
711         if (wl_list_empty(&proxy->queue->event_list))
712                 pthread_cond_signal(&proxy->queue->cond);
713         wl_list_insert(proxy->queue->event_list.prev, &closure->link);
714
715         return size;
716 }
717
718 static void
719 dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
720 {
721         struct wl_closure *closure;
722         struct wl_proxy *proxy;
723         uint32_t id;
724         int opcode, ret;
725
726         closure = container_of(queue->event_list.next,
727                                struct wl_closure, link);
728         wl_list_remove(&closure->link);
729         id = closure->buffer[0];
730         opcode = closure->buffer[1] & 0xffff;
731
732         /* Verify that the receiving object is still valid and look up
733          * proxies for any arguments.  We have to do this just before
734          * calling the handler, since preceeding events may have
735          * destroyed either the proxy or the proxy args since the
736          * event was queued. */
737         proxy = wl_map_lookup(&display->objects, id);
738         ret = wl_closure_lookup_objects(closure, &display->objects);
739
740         pthread_mutex_unlock(&display->mutex);
741
742         if (proxy != WL_ZOMBIE_OBJECT &&
743             proxy->object.implementation && ret == 0) {
744                 if (wl_debug)
745                         wl_closure_print(closure, &proxy->object, false);
746
747                 wl_closure_invoke(closure, &proxy->object,
748                                   proxy->object.implementation[opcode],
749                                   proxy->user_data);
750         }
751
752         wl_closure_destroy(closure);
753
754         pthread_mutex_lock(&display->mutex);
755 }
756
757 static int
758 dispatch_queue(struct wl_display *display,
759                struct wl_event_queue *queue, int block)
760 {
761         int len, size, count, ret;
762
763         pthread_mutex_lock(&display->mutex);
764
765         if (display->last_error)
766                 goto err_unlock;
767
768         ret = wl_connection_flush(display->connection);
769         if (ret < 0 && errno != EAGAIN) {
770                 display_fatal_error(display, errno);
771                 goto err_unlock;
772         }
773
774         if (block && wl_list_empty(&queue->event_list) &&
775             pthread_equal(display->display_thread, pthread_self())) {
776                 len = wl_connection_read(display->connection);
777                 if (len == -1) {
778                         display_fatal_error(display, errno);
779                         goto err_unlock;
780                 } else if (len == 0) {
781                         display_fatal_error(display, EPIPE);
782                         goto err_unlock;
783                 }
784                 while (len >= 8) {
785                         size = queue_event(display, len);
786                         if (size == -1) {
787                                 display_fatal_error(display, errno);
788                                 goto err_unlock;
789                         } else if (size == 0) {
790                                 break;
791                         }
792                         len -= size;
793                 }
794         } else if (block && wl_list_empty(&queue->event_list)) {
795                 pthread_cond_wait(&queue->cond, &display->mutex);
796                 if (display->last_error)
797                         goto err_unlock;
798         }
799
800         for (count = 0; !wl_list_empty(&queue->event_list); count++) {
801                 dispatch_event(display, queue);
802                 if (display->last_error)
803                         goto err_unlock;
804         }
805
806         pthread_mutex_unlock(&display->mutex);
807
808         return count;
809
810 err_unlock:
811         errno = display->last_error;
812         pthread_mutex_unlock(&display->mutex);
813
814         return -1;
815 }
816
817 /** Dispatch events in an event queue
818  *
819  * \param display The display context object
820  * \param queue The event queue to dispatch
821  * \return 0 on success; -1 on failure
822  *
823  * Dispatch all incoming events for objects assigned to the given
824  * event queue. On failure -1 is returned and errno set appropriately.
825  *
826  * \memberof wl_display
827  */
828 WL_EXPORT int
829 wl_display_dispatch_queue(struct wl_display *display,
830                           struct wl_event_queue *queue)
831 {
832         return dispatch_queue(display, queue, 1);
833 }
834
835 /** Dispatch a display's main event queue
836  *
837  * \param display The display context object
838  * \return 0 on success or -1 on failure
839  *
840  * Dispatch the display's main event queue.
841  *
842  * \sa wl_display_dispatch_queue()
843  *
844  * \memberof wl_display
845  */
846 WL_EXPORT int
847 wl_display_dispatch(struct wl_display *display)
848 {
849         display->display_thread = pthread_self();
850
851         return dispatch_queue(display, &display->queue, 1);
852 }
853
854 WL_EXPORT int
855 wl_display_dispatch_pending(struct wl_display *display)
856 {
857         display->display_thread = pthread_self();
858
859         return dispatch_queue(display, &display->queue, 0);
860 }
861
862 WL_EXPORT int
863 wl_display_get_error(struct wl_display *display)
864 {
865         int ret;
866
867         pthread_mutex_lock(&display->mutex);
868
869         ret = display->last_error;
870
871         pthread_mutex_unlock(&display->mutex);
872
873         return ret;
874 }
875
876 /** Send all buffered request on the display to the server
877  *
878  * \param display The display context object
879  * \return The number of bytes send on success or -1 on failure
880  *
881  * Send all buffered data on the client side to the server. Clients
882  * should call this function before blocking. On success, the number
883  * of bytes sent to the server is returned. On failure, this
884  * function returns -1 and errno is set appropriately.
885  *
886  * \memberof wl_display
887  */
888 WL_EXPORT int
889 wl_display_flush(struct wl_display *display)
890 {
891         int ret;
892
893         pthread_mutex_lock(&display->mutex);
894
895         if (display->last_error) {
896                 errno = display->last_error;
897                 ret = -1;
898         } else {
899                 ret = wl_connection_flush(display->connection);
900                 if (ret < 0 && errno != EAGAIN)
901                         display_fatal_error(display, errno);
902         }
903
904         pthread_mutex_unlock(&display->mutex);
905
906         return ret;
907 }
908
909 /** Set the user data associated with a proxy
910  *
911  * \param proxy The proxy object
912  * \param user_data The data to be associated with proxy
913  *
914  * Set the user data associated with \c proxy. When events for this
915  * proxy are received, \c user_data will be supplied to its listener.
916  *
917  * \memberof wl_proxy
918  */
919 WL_EXPORT void
920 wl_proxy_set_user_data(struct wl_proxy *proxy, void *user_data)
921 {
922         proxy->user_data = user_data;
923 }
924
925 /** Get the user data associated with a proxy
926  *
927  * \param proxy The proxy object
928  * \return The user data associated with proxy
929  *
930  * \memberof wl_proxy
931  */
932 WL_EXPORT void *
933 wl_proxy_get_user_data(struct wl_proxy *proxy)
934 {
935         return proxy->user_data;
936 }
937
938 /** Get the id of a proxy object
939  *
940  * \param proxy The proxy object
941  * \return The id the object associated with the proxy
942  *
943  * \memberof wl_proxy
944  */
945 WL_EXPORT uint32_t
946 wl_proxy_get_id(struct wl_proxy *proxy)
947 {
948         return proxy->object.id;
949 }
950
951
952 /** Assign a proxy to an event queue
953  *
954  * \param proxy The proxy object
955  * \param queue The event queue that will handle this proxy
956  *
957  * Assign proxy to event queue. Events coming from \c proxy will be
958  * queued in \c queue instead of the display's main queue.
959  *
960  * \sa wl_display_dispatch_queue()
961  *
962  * \memberof wl_proxy
963  */
964 WL_EXPORT void
965 wl_proxy_set_queue(struct wl_proxy *proxy, struct wl_event_queue *queue)
966 {
967         proxy->queue = queue;
968 }
969
970 WL_EXPORT void
971 wl_log_set_handler_client(wl_log_func_t handler)
972 {
973         wl_log_handler = handler;
974 }