2 * Copyright © 2008-2012 Kristian Høgsberg
3 * Copyright © 2010-2012 Intel Corporation
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.
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
32 #include <sys/socket.h>
40 #include "wayland-util.h"
41 #include "wayland-os.h"
42 #include "wayland-client.h"
43 #include "wayland-private.h"
49 struct wl_object object;
50 struct wl_display *display;
51 struct wl_event_queue *queue;
63 struct wl_event_queue {
65 struct wl_list event_list;
66 struct wl_display *display;
71 struct wl_proxy proxy;
72 struct wl_connection *connection;
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;
84 static int wl_debug = 0;
87 display_fatal_error(struct wl_display *display, int error)
89 struct wl_event_queue *iter;
91 if (display->last_error)
97 display->last_error = error;
101 wl_list_for_each(iter, &display->event_queue_list, link)
102 pthread_cond_broadcast(&iter->cond);
106 wl_display_fatal_error(struct wl_display *display, int error)
108 pthread_mutex_lock(&display->mutex);
109 display_fatal_error(display, error);
110 pthread_mutex_unlock(&display->mutex);
114 wl_event_queue_init(struct wl_event_queue *queue, struct wl_display *display)
116 wl_list_init(&queue->event_list);
117 pthread_cond_init(&queue->cond, NULL);
118 queue->display = display;
122 wl_event_queue_release(struct wl_event_queue *queue)
124 struct wl_closure *closure;
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);
132 pthread_cond_destroy(&queue->cond);
135 /** Destroy an event queue
137 * \param queue The event queue to be destroyed
139 * Destroy the given event queue. Any pending event on that queue is
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
146 * \memberof wl_event_queue
149 wl_event_queue_destroy(struct wl_event_queue *queue)
151 struct wl_display *display = queue->display;
153 pthread_mutex_lock(&display->mutex);
154 wl_list_remove(&queue->link);
155 wl_event_queue_release(queue);
157 pthread_mutex_unlock(&display->mutex);
160 /** Create a new event queue for this display
162 * \param display The display context object
163 * \return A new event queue associated with this display or NULL on
166 * \memberof wl_display
168 WL_EXPORT struct wl_event_queue *
169 wl_display_create_queue(struct wl_display *display)
171 struct wl_event_queue *queue;
173 queue = malloc(sizeof *queue);
177 wl_event_queue_init(queue, display);
179 pthread_mutex_lock(&display->mutex);
180 wl_list_insert(&display->event_queue_list, &queue->link);
181 pthread_mutex_unlock(&display->mutex);
186 /** Create a proxy object with a given interface
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
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().
197 * The proxy will inherit the display and event queue of the factory object.
199 * \note This should not normally be used by non-generated code.
201 * \sa wl_display, wl_event_queue, wl_proxy_marshal()
205 WL_EXPORT struct wl_proxy *
206 wl_proxy_create(struct wl_proxy *factory, const struct wl_interface *interface)
208 struct wl_proxy *proxy;
209 struct wl_display *display = factory->display;
211 proxy = malloc(sizeof *proxy);
215 proxy->object.interface = interface;
216 proxy->object.implementation = NULL;
217 proxy->display = display;
218 proxy->queue = factory->queue;
219 proxy->id_deleted = 0;
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);
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)
234 struct wl_proxy *proxy;
235 struct wl_display *display = factory->display;
237 proxy = malloc(sizeof *proxy);
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;
248 wl_map_insert_at(&display->objects, id, proxy);
253 /** Destroy a proxy object
255 * \param proxy The proxy to be destroyed
260 wl_proxy_destroy(struct wl_proxy *proxy)
262 pthread_mutex_lock(&proxy->display->mutex);
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);
270 wl_map_insert_at(&proxy->display->objects,
271 proxy->object.id, NULL);
273 pthread_mutex_unlock(&proxy->display->mutex);
278 /** Set a proxy's listener
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
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.
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
296 wl_proxy_add_listener(struct wl_proxy *proxy,
297 void (**implementation)(void), void *data)
299 if (proxy->object.implementation) {
300 fprintf(stderr, "proxy already has listener\n");
304 proxy->object.implementation = implementation;
305 proxy->user_data = data;
310 /** Prepare a request to be sent to the compositor
312 * \param proxy The proxy object
313 * \param opcode Opcode of the request to be sent
314 * \param ... Extra arguments for the given request
316 * Translates the request given by opcode and the extra arguments into the
317 * wire format and write it to the connection buffer.
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
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);
332 * \note This should not normally be used by non-generated code.
334 * \sa wl_proxy_create()
339 wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
341 struct wl_closure *closure;
344 pthread_mutex_lock(&proxy->display->mutex);
346 va_start(ap, opcode);
347 closure = wl_closure_vmarshal(&proxy->object, opcode, ap,
348 &proxy->object.interface->methods[opcode]);
351 if (closure == NULL) {
352 fprintf(stderr, "Error marshalling request\n");
357 wl_closure_print(closure, &proxy->object, true);
359 if (wl_closure_send(closure, proxy->display->connection)) {
360 fprintf(stderr, "Error sending request: %m\n");
364 wl_closure_destroy(closure);
366 pthread_mutex_unlock(&proxy->display->mutex);
370 display_handle_error(void *data,
371 struct wl_display *display, struct wl_object *object,
372 uint32_t code, const char *message)
376 wl_log("%s@%u: error %d: %s\n",
377 object->interface->name, object->id, code, message);
380 case WL_DISPLAY_ERROR_INVALID_OBJECT:
381 case WL_DISPLAY_ERROR_INVALID_METHOD:
384 case WL_DISPLAY_ERROR_NO_MEMORY:
392 wl_display_fatal_error(display, err);
396 display_handle_delete_id(void *data, struct wl_display *display, uint32_t id)
398 struct wl_proxy *proxy;
400 pthread_mutex_lock(&display->mutex);
402 proxy = wl_map_lookup(&display->objects, id);
403 if (proxy != WL_ZOMBIE_OBJECT)
404 proxy->id_deleted = 1;
406 wl_map_remove(&display->objects, id);
408 pthread_mutex_unlock(&display->mutex);
411 static const struct wl_display_listener display_listener = {
412 display_handle_error,
413 display_handle_delete_id
417 connect_to_socket(const char *name)
419 struct sockaddr_un addr;
421 const char *runtime_dir;
424 runtime_dir = getenv("XDG_RUNTIME_DIR");
427 "error: XDG_RUNTIME_DIR not set in the environment.\n");
429 /* to prevent programs reporting
430 * "failed to create display: Success" */
436 name = getenv("WAYLAND_DISPLAY");
440 fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
444 memset(&addr, 0, sizeof addr);
445 addr.sun_family = AF_LOCAL;
447 snprintf(addr.sun_path, sizeof addr.sun_path,
448 "%s/%s", runtime_dir, name) + 1;
450 assert(name_size > 0);
451 if (name_size > (int)sizeof addr.sun_path) {
453 "error: socket path \"%s/%s\" plus null terminator"
454 " exceeds 108 bytes\n", runtime_dir, name);
456 /* to prevent programs reporting
457 * "failed to add socket: Success" */
458 errno = ENAMETOOLONG;
462 size = offsetof (struct sockaddr_un, sun_path) + name_size;
464 if (connect(fd, (struct sockaddr *) &addr, size) < 0) {
472 /** Connect to Wayland display on an already open fd
474 * \param fd The fd to use for the connection
475 * \return A \ref wl_display object or \c NULL on failure
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
481 * \memberof wl_display
483 WL_EXPORT struct wl_display *
484 wl_display_connect_to_fd(int fd)
486 struct wl_display *display;
489 debug = getenv("WAYLAND_DEBUG");
493 display = malloc(sizeof *display);
494 if (display == NULL) {
499 memset(display, 0, sizeof *display);
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);
507 wl_map_insert_new(&display->objects, WL_MAP_CLIENT_SIDE, NULL);
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;
518 display->connection = wl_connection_create(display->fd);
519 if (display->connection == NULL) {
520 wl_map_release(&display->objects);
529 /** Connect to a Wayland display
531 * \param name Name of the Wayland display to connect to
532 * \return A \ref wl_display object or \c NULL on failure
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.
538 * \memberof wl_display
540 WL_EXPORT struct wl_display *
541 wl_display_connect(const char *name)
543 char *connection, *end;
546 connection = getenv("WAYLAND_SOCKET");
548 fd = strtol(connection, &end, 0);
552 flags = fcntl(fd, F_GETFD);
554 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
555 unsetenv("WAYLAND_SOCKET");
557 fd = connect_to_socket(name);
562 return wl_display_connect_to_fd(fd);
565 /** Close a connection to a Wayland display
567 * \param display The display context object
569 * Close the connection to \c display and free all resources associated
572 * \memberof wl_display
575 wl_display_disconnect(struct wl_display *display)
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);
587 /** Get a display context's file descriptor
589 * \param display The display context object
590 * \return Display object file descriptor
592 * Return the file descriptor associated with a display so it can be
593 * integrated into the client's main loop.
595 * \memberof wl_display
598 wl_display_get_fd(struct wl_display *display)
604 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
609 wl_callback_destroy(callback);
612 static const struct wl_callback_listener sync_listener = {
616 /** Block until all pending request are processed by the server
618 * \param display The display context object
619 * \return The number of dispatched events on success or -1 on failure
621 * Blocks until the server process all currently issued requests and
622 * sends out pending events on all event queues.
624 * \memberof wl_display
627 wl_display_roundtrip(struct wl_display *display)
629 struct wl_callback *callback;
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);
642 create_proxies(struct wl_proxy *sender, struct wl_closure *closure)
644 struct wl_proxy *proxy;
645 const char *signature;
646 struct argument_details arg;
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);
657 id = **(uint32_t **) closure->args[i];
659 *(void **) closure->args[i] = NULL;
662 proxy = wl_proxy_create_for_id(sender, id,
663 closure->message->types[i - 2]);
666 *(void **) closure->args[i] = proxy;
677 queue_event(struct wl_display *display, int len)
681 struct wl_proxy *proxy;
682 struct wl_closure *closure;
683 const struct wl_message *message;
685 wl_connection_copy(display->connection, p, sizeof p);
687 opcode = p[1] & 0xffff;
692 proxy = wl_map_lookup(&display->objects, id);
693 if (proxy == WL_ZOMBIE_OBJECT) {
694 wl_connection_consume(display->connection, size);
696 } else if (proxy == NULL) {
697 wl_connection_consume(display->connection, size);
701 message = &proxy->object.interface->events[opcode];
702 closure = wl_connection_demarshal(display->connection, size,
703 &display->objects, message);
707 if (create_proxies(proxy, closure) < 0) {
708 wl_closure_destroy(closure);
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);
720 dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
722 struct wl_closure *closure;
723 struct wl_proxy *proxy;
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;
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);
741 pthread_mutex_unlock(&display->mutex);
743 if (proxy != WL_ZOMBIE_OBJECT &&
744 proxy->object.implementation && ret == 0) {
746 wl_closure_print(closure, &proxy->object, false);
748 wl_closure_invoke(closure, &proxy->object,
749 proxy->object.implementation[opcode],
753 wl_closure_destroy(closure);
755 pthread_mutex_lock(&display->mutex);
759 dispatch_queue(struct wl_display *display,
760 struct wl_event_queue *queue, int block)
762 int len, size, count, ret;
764 pthread_mutex_lock(&display->mutex);
766 if (display->last_error)
769 ret = wl_connection_flush(display->connection);
770 if (ret < 0 && errno != EAGAIN) {
771 display_fatal_error(display, errno);
775 if (block && wl_list_empty(&queue->event_list) &&
776 pthread_equal(display->display_thread, pthread_self())) {
777 len = wl_connection_read(display->connection);
779 display_fatal_error(display, errno);
781 } else if (len == 0) {
782 display_fatal_error(display, EPIPE);
786 size = queue_event(display, len);
788 display_fatal_error(display, errno);
790 } else if (size == 0) {
795 } else if (block && wl_list_empty(&queue->event_list)) {
796 pthread_cond_wait(&queue->cond, &display->mutex);
797 if (display->last_error)
801 for (count = 0; !wl_list_empty(&queue->event_list); count++) {
802 dispatch_event(display, queue);
803 if (display->last_error)
807 pthread_mutex_unlock(&display->mutex);
812 errno = display->last_error;
813 pthread_mutex_unlock(&display->mutex);
818 /** Dispatch events in an event queue
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
824 * Dispatch all incoming events for objects assigned to the given
825 * event queue. On failure -1 is returned and errno set appropriately.
827 * \memberof wl_display
830 wl_display_dispatch_queue(struct wl_display *display,
831 struct wl_event_queue *queue)
833 return dispatch_queue(display, queue, 1);
836 /** Dispatch a display's main event queue
838 * \param display The display context object
839 * \return The number of dispatched events on success or -1 on failure
841 * Dispatch the display's main event queue.
843 * \sa wl_display_dispatch_queue()
845 * \memberof wl_display
848 wl_display_dispatch(struct wl_display *display)
850 display->display_thread = pthread_self();
852 return dispatch_queue(display, &display->queue, 1);
856 wl_display_dispatch_pending(struct wl_display *display)
858 display->display_thread = pthread_self();
860 return dispatch_queue(display, &display->queue, 0);
864 wl_display_get_error(struct wl_display *display)
868 pthread_mutex_lock(&display->mutex);
870 ret = display->last_error;
872 pthread_mutex_unlock(&display->mutex);
877 /** Send all buffered request on the display to the server
879 * \param display The display context object
880 * \return The number of bytes send on success or -1 on failure
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.
887 * \memberof wl_display
890 wl_display_flush(struct wl_display *display)
894 pthread_mutex_lock(&display->mutex);
896 if (display->last_error) {
897 errno = display->last_error;
900 ret = wl_connection_flush(display->connection);
901 if (ret < 0 && errno != EAGAIN)
902 display_fatal_error(display, errno);
905 pthread_mutex_unlock(&display->mutex);
910 /** Set the user data associated with a proxy
912 * \param proxy The proxy object
913 * \param user_data The data to be associated with proxy
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.
921 wl_proxy_set_user_data(struct wl_proxy *proxy, void *user_data)
923 proxy->user_data = user_data;
926 /** Get the user data associated with a proxy
928 * \param proxy The proxy object
929 * \return The user data associated with proxy
934 wl_proxy_get_user_data(struct wl_proxy *proxy)
936 return proxy->user_data;
939 /** Get the id of a proxy object
941 * \param proxy The proxy object
942 * \return The id the object associated with the proxy
947 wl_proxy_get_id(struct wl_proxy *proxy)
949 return proxy->object.id;
953 /** Assign a proxy to an event queue
955 * \param proxy The proxy object
956 * \param queue The event queue that will handle this proxy
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.
961 * \sa wl_display_dispatch_queue()
966 wl_proxy_set_queue(struct wl_proxy *proxy, struct wl_event_queue *queue)
968 proxy->queue = queue;
972 wl_log_set_handler_client(wl_log_func_t handler)
974 wl_log_handler = handler;