doc: Document change of return value of dispatch functions
[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  * \return The number of dispatched events on success or -1 on failure
620  *
621  * Blocks until the server process all currently issued requests and
622  * sends out pending events on all event queues.
623  *
624  * \memberof wl_display
625  */
626 WL_EXPORT int
627 wl_display_roundtrip(struct wl_display *display)
628 {
629         struct wl_callback *callback;
630         int done, ret = 0;
631
632         done = 0;
633         callback = wl_display_sync(display);
634         wl_callback_add_listener(callback, &sync_listener, &done);
635         while (!done && !ret)
636                 ret = wl_display_dispatch(display);
637
638         return ret;
639 }
640
641 static int
642 create_proxies(struct wl_proxy *sender, struct wl_closure *closure)
643 {
644         struct wl_proxy *proxy;
645         const char *signature;
646         struct argument_details arg;
647         uint32_t id;
648         int i;
649         int count;
650
651         signature = closure->message->signature;
652         count = arg_count_for_signature(signature) + 2;
653         for (i = 2; i < count; i++) {
654                 signature = get_next_argument(signature, &arg);
655                 switch (arg.type) {
656                 case 'n':
657                         id = **(uint32_t **) closure->args[i];
658                         if (id == 0) {
659                                 *(void **) closure->args[i] = NULL;
660                                 break;
661                         }
662                         proxy = wl_proxy_create_for_id(sender, id,
663                                                        closure->message->types[i - 2]);
664                         if (proxy == NULL)
665                                 return -1;
666                         *(void **) closure->args[i] = proxy;
667                         break;
668                 default:
669                         break;
670                 }
671         }
672
673         return 0;
674 }
675
676 static int
677 queue_event(struct wl_display *display, int len)
678 {
679         uint32_t p[2], id;
680         int opcode, size;
681         struct wl_proxy *proxy;
682         struct wl_closure *closure;
683         const struct wl_message *message;
684
685         wl_connection_copy(display->connection, p, sizeof p);
686         id = p[0];
687         opcode = p[1] & 0xffff;
688         size = p[1] >> 16;
689         if (len < size)
690                 return 0;
691
692         proxy = wl_map_lookup(&display->objects, id);
693         if (proxy == WL_ZOMBIE_OBJECT) {
694                 wl_connection_consume(display->connection, size);
695                 return size;
696         } else if (proxy == NULL) {
697                 wl_connection_consume(display->connection, size);
698                 return size;
699         }
700
701         message = &proxy->object.interface->events[opcode];
702         closure = wl_connection_demarshal(display->connection, size,
703                                           &display->objects, message);
704         if (!closure)
705                 return -1;
706
707         if (create_proxies(proxy, closure) < 0) {
708                 wl_closure_destroy(closure);
709                 return -1;
710         }
711
712         if (wl_list_empty(&proxy->queue->event_list))
713                 pthread_cond_signal(&proxy->queue->cond);
714         wl_list_insert(proxy->queue->event_list.prev, &closure->link);
715
716         return size;
717 }
718
719 static void
720 dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
721 {
722         struct wl_closure *closure;
723         struct wl_proxy *proxy;
724         uint32_t id;
725         int opcode, ret;
726
727         closure = container_of(queue->event_list.next,
728                                struct wl_closure, link);
729         wl_list_remove(&closure->link);
730         id = closure->buffer[0];
731         opcode = closure->buffer[1] & 0xffff;
732
733         /* Verify that the receiving object is still valid and look up
734          * proxies for any arguments.  We have to do this just before
735          * calling the handler, since preceeding events may have
736          * destroyed either the proxy or the proxy args since the
737          * event was queued. */
738         proxy = wl_map_lookup(&display->objects, id);
739         ret = wl_closure_lookup_objects(closure, &display->objects);
740
741         pthread_mutex_unlock(&display->mutex);
742
743         if (proxy != WL_ZOMBIE_OBJECT &&
744             proxy->object.implementation && ret == 0) {
745                 if (wl_debug)
746                         wl_closure_print(closure, &proxy->object, false);
747
748                 wl_closure_invoke(closure, &proxy->object,
749                                   proxy->object.implementation[opcode],
750                                   proxy->user_data);
751         }
752
753         wl_closure_destroy(closure);
754
755         pthread_mutex_lock(&display->mutex);
756 }
757
758 static int
759 dispatch_queue(struct wl_display *display,
760                struct wl_event_queue *queue, int block)
761 {
762         int len, size, count, ret;
763
764         pthread_mutex_lock(&display->mutex);
765
766         if (display->last_error)
767                 goto err_unlock;
768
769         ret = wl_connection_flush(display->connection);
770         if (ret < 0 && errno != EAGAIN) {
771                 display_fatal_error(display, errno);
772                 goto err_unlock;
773         }
774
775         if (block && wl_list_empty(&queue->event_list) &&
776             pthread_equal(display->display_thread, pthread_self())) {
777                 len = wl_connection_read(display->connection);
778                 if (len == -1) {
779                         display_fatal_error(display, errno);
780                         goto err_unlock;
781                 } else if (len == 0) {
782                         display_fatal_error(display, EPIPE);
783                         goto err_unlock;
784                 }
785                 while (len >= 8) {
786                         size = queue_event(display, len);
787                         if (size == -1) {
788                                 display_fatal_error(display, errno);
789                                 goto err_unlock;
790                         } else if (size == 0) {
791                                 break;
792                         }
793                         len -= size;
794                 }
795         } else if (block && wl_list_empty(&queue->event_list)) {
796                 pthread_cond_wait(&queue->cond, &display->mutex);
797                 if (display->last_error)
798                         goto err_unlock;
799         }
800
801         for (count = 0; !wl_list_empty(&queue->event_list); count++) {
802                 dispatch_event(display, queue);
803                 if (display->last_error)
804                         goto err_unlock;
805         }
806
807         pthread_mutex_unlock(&display->mutex);
808
809         return count;
810
811 err_unlock:
812         errno = display->last_error;
813         pthread_mutex_unlock(&display->mutex);
814
815         return -1;
816 }
817
818 /** Dispatch events in an event queue
819  *
820  * \param display The display context object
821  * \param queue The event queue to dispatch
822  * \return The number of dispatched events on success or -1 on failure
823  *
824  * Dispatch all incoming events for objects assigned to the given
825  * event queue. On failure -1 is returned and errno set appropriately.
826  *
827  * \memberof wl_display
828  */
829 WL_EXPORT int
830 wl_display_dispatch_queue(struct wl_display *display,
831                           struct wl_event_queue *queue)
832 {
833         return dispatch_queue(display, queue, 1);
834 }
835
836 /** Dispatch a display's main event queue
837  *
838  * \param display The display context object
839  * \return The number of dispatched events on success or -1 on failure
840  *
841  * Dispatch the display's main event queue.
842  *
843  * \sa wl_display_dispatch_queue()
844  *
845  * \memberof wl_display
846  */
847 WL_EXPORT int
848 wl_display_dispatch(struct wl_display *display)
849 {
850         display->display_thread = pthread_self();
851
852         return dispatch_queue(display, &display->queue, 1);
853 }
854
855 WL_EXPORT int
856 wl_display_dispatch_pending(struct wl_display *display)
857 {
858         display->display_thread = pthread_self();
859
860         return dispatch_queue(display, &display->queue, 0);
861 }
862
863 WL_EXPORT int
864 wl_display_get_error(struct wl_display *display)
865 {
866         int ret;
867
868         pthread_mutex_lock(&display->mutex);
869
870         ret = display->last_error;
871
872         pthread_mutex_unlock(&display->mutex);
873
874         return ret;
875 }
876
877 /** Send all buffered request on the display to the server
878  *
879  * \param display The display context object
880  * \return The number of bytes send on success or -1 on failure
881  *
882  * Send all buffered data on the client side to the server. Clients
883  * should call this function before blocking. On success, the number
884  * of bytes sent to the server is returned. On failure, this
885  * function returns -1 and errno is set appropriately.
886  *
887  * \memberof wl_display
888  */
889 WL_EXPORT int
890 wl_display_flush(struct wl_display *display)
891 {
892         int ret;
893
894         pthread_mutex_lock(&display->mutex);
895
896         if (display->last_error) {
897                 errno = display->last_error;
898                 ret = -1;
899         } else {
900                 ret = wl_connection_flush(display->connection);
901                 if (ret < 0 && errno != EAGAIN)
902                         display_fatal_error(display, errno);
903         }
904
905         pthread_mutex_unlock(&display->mutex);
906
907         return ret;
908 }
909
910 /** Set the user data associated with a proxy
911  *
912  * \param proxy The proxy object
913  * \param user_data The data to be associated with proxy
914  *
915  * Set the user data associated with \c proxy. When events for this
916  * proxy are received, \c user_data will be supplied to its listener.
917  *
918  * \memberof wl_proxy
919  */
920 WL_EXPORT void
921 wl_proxy_set_user_data(struct wl_proxy *proxy, void *user_data)
922 {
923         proxy->user_data = user_data;
924 }
925
926 /** Get the user data associated with a proxy
927  *
928  * \param proxy The proxy object
929  * \return The user data associated with proxy
930  *
931  * \memberof wl_proxy
932  */
933 WL_EXPORT void *
934 wl_proxy_get_user_data(struct wl_proxy *proxy)
935 {
936         return proxy->user_data;
937 }
938
939 /** Get the id of a proxy object
940  *
941  * \param proxy The proxy object
942  * \return The id the object associated with the proxy
943  *
944  * \memberof wl_proxy
945  */
946 WL_EXPORT uint32_t
947 wl_proxy_get_id(struct wl_proxy *proxy)
948 {
949         return proxy->object.id;
950 }
951
952
953 /** Assign a proxy to an event queue
954  *
955  * \param proxy The proxy object
956  * \param queue The event queue that will handle this proxy
957  *
958  * Assign proxy to event queue. Events coming from \c proxy will be
959  * queued in \c queue instead of the display's main queue.
960  *
961  * \sa wl_display_dispatch_queue()
962  *
963  * \memberof wl_proxy
964  */
965 WL_EXPORT void
966 wl_proxy_set_queue(struct wl_proxy *proxy, struct wl_event_queue *queue)
967 {
968         proxy->queue = queue;
969 }
970
971 WL_EXPORT void
972 wl_log_set_handler_client(wl_log_func_t handler)
973 {
974         wl_log_handler = handler;
975 }