2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
34 #include <sys/socket.h>
44 #include "wayland-private.h"
45 #include "wayland-server.h"
46 #include "wayland-server-protocol.h"
47 #include "wayland-os.h"
52 struct sockaddr_un addr;
55 struct wl_event_source *source;
59 struct wl_connection *connection;
60 struct wl_event_source *source;
61 struct wl_display *display;
62 struct wl_resource *display_resource;
66 struct wl_map objects;
67 struct wl_signal destroy_signal;
73 struct wl_event_loop *loop;
79 struct wl_list global_list;
80 struct wl_list socket_list;
81 struct wl_list client_list;
85 const struct wl_interface *interface;
88 wl_global_bind_func_t bind;
92 static int wl_debug = 0;
95 destroy_client(void *data)
97 struct wl_client *client = data;
99 wl_client_destroy(client);
103 wl_resource_post_event(struct wl_resource *resource, uint32_t opcode, ...)
105 struct wl_closure closure;
106 struct wl_object *object = &resource->object;
110 va_start(ap, opcode);
111 ret = wl_closure_vmarshal(&closure, object, opcode, ap,
112 &object->interface->events[opcode]);
118 if (wl_closure_send(&closure, resource->client->connection))
119 wl_event_loop_add_idle(resource->client->display->loop,
120 destroy_client, resource->client);
123 wl_closure_print(&closure, object, true);
125 wl_closure_destroy(&closure);
130 wl_resource_queue_event(struct wl_resource *resource, uint32_t opcode, ...)
132 struct wl_closure closure;
133 struct wl_object *object = &resource->object;
137 va_start(ap, opcode);
138 ret = wl_closure_vmarshal(&closure, object, opcode, ap,
139 &object->interface->events[opcode]);
145 if (wl_closure_queue(&closure, resource->client->connection))
146 wl_event_loop_add_idle(resource->client->display->loop,
147 destroy_client, resource->client);
150 wl_closure_print(&closure, object, true);
152 wl_closure_destroy(&closure);
156 wl_resource_post_error(struct wl_resource *resource,
157 uint32_t code, const char *msg, ...)
159 struct wl_client *client = resource->client;
164 vsnprintf(buffer, sizeof buffer, msg, ap);
170 * When a client aborts, its resources are destroyed in id order,
171 * which means the display resource is destroyed first. If destruction
172 * of any later resources results in a protocol error, we end up here
173 * with a NULL display_resource. Do not try to send errors to an
174 * already dead client.
176 if (!client->display_resource)
179 wl_resource_post_event(client->display_resource,
180 WL_DISPLAY_ERROR, resource, code, buffer);
184 wl_client_connection_data(int fd, uint32_t mask, void *data)
186 struct wl_client *client = data;
187 struct wl_connection *connection = client->connection;
188 struct wl_resource *resource;
189 struct wl_object *object;
190 struct wl_closure closure;
191 const struct wl_message *message;
197 if (mask & WL_EVENT_READABLE)
198 cmask |= WL_CONNECTION_READABLE;
199 if (mask & WL_EVENT_WRITABLE)
200 cmask |= WL_CONNECTION_WRITABLE;
202 len = wl_connection_data(connection, cmask);
204 wl_client_destroy(client);
208 while ((size_t) len >= sizeof p) {
209 wl_connection_copy(connection, p, sizeof p);
210 opcode = p[1] & 0xffff;
215 resource = wl_map_lookup(&client->objects, p[0]);
216 if (resource == NULL) {
217 wl_resource_post_error(client->display_resource,
218 WL_DISPLAY_ERROR_INVALID_OBJECT,
219 "invalid object %d", p[0]);
223 object = &resource->object;
224 if (opcode >= object->interface->method_count) {
225 wl_resource_post_error(client->display_resource,
226 WL_DISPLAY_ERROR_INVALID_METHOD,
227 "invalid method %d, object %s@%d",
229 object->interface->name,
234 message = &object->interface->methods[opcode];
235 ret = wl_connection_demarshal(client->connection, &closure,
236 size, &client->objects, message);
239 if (ret && errno == EINVAL) {
240 wl_resource_post_error(client->display_resource,
241 WL_DISPLAY_ERROR_INVALID_METHOD,
242 "invalid arguments for %s@%d.%s",
243 object->interface->name,
247 } else if (ret && errno == ENOMEM) {
248 wl_resource_post_no_memory(resource);
253 wl_closure_print(&closure, object, false);
255 wl_closure_invoke(&closure, object,
256 object->implementation[opcode], client);
258 wl_closure_destroy(&closure);
265 wl_client_destroy(client);
271 wl_client_connection_update(struct wl_connection *connection,
272 uint32_t mask, void *data)
274 struct wl_client *client = data;
278 if (mask & WL_CONNECTION_READABLE)
279 emask |= WL_EVENT_READABLE;
280 if (mask & WL_CONNECTION_WRITABLE)
281 emask |= WL_EVENT_WRITABLE;
283 return wl_event_source_fd_update(client->source, emask);
287 wl_client_flush(struct wl_client *client)
289 if (client->mask & WL_CONNECTION_WRITABLE)
290 wl_connection_data(client->connection, WL_CONNECTION_WRITABLE);
293 WL_EXPORT struct wl_display *
294 wl_client_get_display(struct wl_client *client)
296 return client->display;
300 bind_display(struct wl_client *client,
301 void *data, uint32_t version, uint32_t id);
303 WL_EXPORT struct wl_client *
304 wl_client_create(struct wl_display *display, int fd)
306 struct wl_client *client;
309 client = malloc(sizeof *client);
313 memset(client, 0, sizeof *client);
314 client->display = display;
315 client->source = wl_event_loop_add_fd(display->loop, fd,
317 wl_client_connection_data, client);
319 len = sizeof client->ucred;
320 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
321 &client->ucred, &len) < 0) {
327 wl_connection_create(fd, wl_client_connection_update, client);
328 if (client->connection == NULL) {
333 wl_map_init(&client->objects);
335 if (wl_map_insert_at(&client->objects, 0, NULL) < 0) {
336 wl_map_release(&client->objects);
341 wl_signal_init(&client->destroy_signal);
342 bind_display(client, display, 1, 1);
344 wl_list_insert(display->client_list.prev, &client->link);
350 wl_client_get_credentials(struct wl_client *client,
351 pid_t *pid, uid_t *uid, gid_t *gid)
354 *pid = client->ucred.pid;
356 *uid = client->ucred.uid;
358 *gid = client->ucred.gid;
362 wl_client_add_resource(struct wl_client *client,
363 struct wl_resource *resource)
365 if (resource->object.id == 0)
366 resource->object.id =
367 wl_map_insert_new(&client->objects,
368 WL_MAP_SERVER_SIDE, resource);
370 wl_map_insert_at(&client->objects,
371 resource->object.id, resource);
373 resource->client = client;
374 wl_signal_init(&resource->destroy_signal);
377 WL_EXPORT struct wl_resource *
378 wl_client_get_object(struct wl_client *client, uint32_t id)
380 return wl_map_lookup(&client->objects, id);
384 wl_resource_post_no_memory(struct wl_resource *resource)
386 wl_resource_post_error(resource->client->display_resource,
387 WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
391 destroy_resource(void *element, void *data)
393 struct wl_resource *resource = element;
395 wl_signal_emit(&resource->destroy_signal, resource);
397 if (resource->destroy)
398 resource->destroy(resource);
402 wl_resource_destroy(struct wl_resource *resource)
404 struct wl_client *client = resource->client;
407 id = resource->object.id;
408 destroy_resource(resource, NULL);
410 if (id < WL_SERVER_ID_START) {
411 if (client->display_resource) {
412 wl_resource_queue_event(client->display_resource,
413 WL_DISPLAY_DELETE_ID, id);
415 wl_map_insert_at(&client->objects, id, NULL);
417 wl_map_remove(&client->objects, id);
422 wl_client_add_destroy_listener(struct wl_client *client,
423 struct wl_listener *listener)
425 wl_signal_add(&client->destroy_signal, listener);
428 WL_EXPORT struct wl_listener *
429 wl_client_get_destroy_listener(struct wl_client *client,
430 wl_notify_func_t notify)
432 return wl_signal_get(&client->destroy_signal, notify);
436 wl_client_destroy(struct wl_client *client)
440 printf("disconnect from client %p\n", client);
442 wl_signal_emit(&client->destroy_signal, client);
444 wl_client_flush(client);
445 wl_map_for_each(&client->objects, destroy_resource, &serial);
446 wl_map_release(&client->objects);
447 wl_event_source_remove(client->source);
448 wl_connection_destroy(client->connection);
449 wl_list_remove(&client->link);
454 lose_pointer_focus(struct wl_listener *listener, void *data)
456 struct wl_input_device *device =
457 container_of(listener, struct wl_input_device,
458 pointer_focus_listener);
460 device->pointer_focus_resource = NULL;
464 lose_keyboard_focus(struct wl_listener *listener, void *data)
466 struct wl_input_device *device =
467 container_of(listener, struct wl_input_device,
468 keyboard_focus_listener);
470 device->keyboard_focus_resource = NULL;
474 default_grab_focus(struct wl_pointer_grab *grab,
475 struct wl_surface *surface, int32_t x, int32_t y)
477 struct wl_input_device *device = grab->input_device;
479 if (device->button_count > 0)
482 wl_input_device_set_pointer_focus(device, surface, x, y);
486 default_grab_motion(struct wl_pointer_grab *grab,
487 uint32_t time, int32_t x, int32_t y)
489 struct wl_resource *resource;
491 resource = grab->input_device->pointer_focus_resource;
493 wl_input_device_send_motion(resource, time, x, y);
497 default_grab_button(struct wl_pointer_grab *grab,
498 uint32_t time, uint32_t button, int32_t state)
500 struct wl_input_device *device = grab->input_device;
501 struct wl_resource *resource;
504 resource = device->pointer_focus_resource;
506 serial = wl_display_next_serial(resource->client->display);
507 wl_input_device_send_button(resource, serial, time,
511 if (device->button_count == 0 && state == 0)
512 wl_input_device_set_pointer_focus(device,
518 static const struct wl_pointer_grab_interface
519 default_pointer_grab_interface = {
526 default_grab_key(struct wl_keyboard_grab *grab,
527 uint32_t time, uint32_t key, int32_t state)
529 struct wl_input_device *device = grab->input_device;
530 struct wl_resource *resource;
533 resource = device->keyboard_focus_resource;
535 serial = wl_display_next_serial(resource->client->display);
536 wl_input_device_send_key(resource, serial, time, key, state);
540 static const struct wl_keyboard_grab_interface
541 default_keyboard_grab_interface = {
546 wl_input_device_init(struct wl_input_device *device)
548 memset(device, 0, sizeof *device);
549 wl_list_init(&device->resource_list);
550 wl_array_init(&device->keys);
551 device->pointer_focus_listener.notify = lose_pointer_focus;
552 device->keyboard_focus_listener.notify = lose_keyboard_focus;
554 device->default_pointer_grab.interface = &default_pointer_grab_interface;
555 device->default_pointer_grab.input_device = device;
556 device->pointer_grab = &device->default_pointer_grab;
558 device->default_keyboard_grab.interface = &default_keyboard_grab_interface;
559 device->default_keyboard_grab.input_device = device;
560 device->keyboard_grab = &device->default_keyboard_grab;
562 wl_list_init(&device->drag_resource_list);
563 device->selection_data_source = NULL;
564 wl_signal_init(&device->selection_signal);
565 wl_signal_init(&device->drag_icon_signal);
572 wl_input_device_release(struct wl_input_device *device)
574 if (device->keyboard_focus_resource)
575 wl_list_remove(&device->keyboard_focus_listener.link);
577 if (device->pointer_focus_resource)
578 wl_list_remove(&device->pointer_focus_listener.link);
580 /* XXX: What about device->resource_list? */
582 wl_array_release(&device->keys);
585 static struct wl_resource *
586 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
588 struct wl_resource *r;
593 wl_list_for_each(r, list, link) {
594 if (r->client == surface->resource.client)
602 wl_input_device_set_pointer_focus(struct wl_input_device *device,
603 struct wl_surface *surface,
604 int32_t sx, int32_t sy)
606 struct wl_resource *resource;
609 if (device->pointer_focus == surface)
612 resource = device->pointer_focus_resource;
614 serial = wl_display_next_serial(resource->client->display);
615 wl_input_device_send_pointer_leave(resource, serial,
616 &device->pointer_focus->resource);
617 wl_list_remove(&device->pointer_focus_listener.link);
621 resource = find_resource_for_surface(&device->resource_list, surface);
623 serial = wl_display_next_serial(resource->client->display);
624 wl_input_device_send_pointer_enter(resource, serial,
627 wl_signal_add(&resource->destroy_signal,
628 &device->pointer_focus_listener);
629 device->pointer_focus_serial = serial;
632 device->pointer_focus_resource = resource;
633 device->pointer_focus = surface;
634 device->default_pointer_grab.focus = surface;
638 wl_input_device_set_keyboard_focus(struct wl_input_device *device,
639 struct wl_surface *surface)
641 struct wl_resource *resource;
644 if (device->keyboard_focus == surface)
647 if (device->keyboard_focus_resource) {
648 resource = device->keyboard_focus_resource;
649 serial = wl_display_next_serial(resource->client->display);
650 wl_input_device_send_keyboard_leave(resource, serial,
651 &device->keyboard_focus->resource);
652 wl_list_remove(&device->keyboard_focus_listener.link);
655 resource = find_resource_for_surface(&device->resource_list, surface);
657 serial = wl_display_next_serial(resource->client->display);
658 wl_input_device_send_keyboard_enter(resource, serial,
661 wl_signal_add(&resource->destroy_signal,
662 &device->keyboard_focus_listener);
663 device->keyboard_focus_serial = serial;
666 device->keyboard_focus_resource = resource;
667 device->keyboard_focus = surface;
671 wl_input_device_start_keyboard_grab(struct wl_input_device *device,
672 struct wl_keyboard_grab *grab)
674 device->keyboard_grab = grab;
675 grab->input_device = device;
680 wl_input_device_end_keyboard_grab(struct wl_input_device *device)
682 device->keyboard_grab = &device->default_keyboard_grab;
686 wl_input_device_start_pointer_grab(struct wl_input_device *device,
687 struct wl_pointer_grab *grab)
689 const struct wl_pointer_grab_interface *interface;
691 device->pointer_grab = grab;
692 interface = device->pointer_grab->interface;
693 grab->input_device = device;
696 interface->focus(device->pointer_grab, device->current,
697 device->current_x, device->current_y);
701 wl_input_device_end_pointer_grab(struct wl_input_device *device)
703 const struct wl_pointer_grab_interface *interface;
705 device->pointer_grab = &device->default_pointer_grab;
706 interface = device->pointer_grab->interface;
707 interface->focus(device->pointer_grab, device->current,
708 device->current_x, device->current_y);
712 display_bind(struct wl_client *client,
713 struct wl_resource *resource, uint32_t name,
714 const char *interface, uint32_t version, uint32_t id)
716 struct wl_global *global;
717 struct wl_display *display = resource->data;
719 wl_list_for_each(global, &display->global_list, link)
720 if (global->name == name)
723 if (&global->link == &display->global_list)
724 wl_resource_post_error(resource,
725 WL_DISPLAY_ERROR_INVALID_OBJECT,
726 "invalid global %d", name);
728 global->bind(client, global->data, version, id);
732 display_sync(struct wl_client *client,
733 struct wl_resource *resource, uint32_t id)
735 struct wl_resource *callback;
738 callback = wl_client_add_object(client,
739 &wl_callback_interface, NULL, id, NULL);
740 serial = wl_display_get_serial(client->display);
741 wl_callback_send_done(callback, serial);
742 wl_resource_destroy(callback);
745 struct wl_display_interface display_interface = {
751 destroy_client_display_resource(struct wl_resource *resource)
753 resource->client->display_resource = NULL;
758 bind_display(struct wl_client *client,
759 void *data, uint32_t version, uint32_t id)
761 struct wl_display *display = data;
762 struct wl_global *global;
764 client->display_resource =
765 wl_client_add_object(client, &wl_display_interface,
766 &display_interface, id, display);
767 client->display_resource->destroy = destroy_client_display_resource;
769 wl_list_for_each(global, &display->global_list, link)
770 wl_resource_post_event(client->display_resource,
773 global->interface->name,
774 global->interface->version);
777 WL_EXPORT struct wl_display *
778 wl_display_create(void)
780 struct wl_display *display;
783 debug = getenv("WAYLAND_DEBUG");
787 display = malloc(sizeof *display);
791 display->loop = wl_event_loop_create();
792 if (display->loop == NULL) {
797 wl_list_init(&display->global_list);
798 wl_list_init(&display->socket_list);
799 wl_list_init(&display->client_list);
803 if (!wl_display_add_global(display, &wl_display_interface,
804 display, bind_display)) {
805 wl_event_loop_destroy(display->loop);
814 wl_display_destroy(struct wl_display *display)
816 struct wl_socket *s, *next;
817 struct wl_global *global, *gnext;
819 wl_list_for_each_safe(s, next, &display->socket_list, link) {
820 wl_event_source_remove(s->source);
822 unlink(s->addr.sun_path);
824 unlink(s->lock_addr);
827 wl_event_loop_destroy(display->loop);
829 wl_list_for_each_safe(global, gnext, &display->global_list, link)
835 WL_EXPORT struct wl_global *
836 wl_display_add_global(struct wl_display *display,
837 const struct wl_interface *interface,
838 void *data, wl_global_bind_func_t bind)
840 struct wl_global *global;
841 struct wl_client *client;
843 global = malloc(sizeof *global);
847 global->name = display->id++;
848 global->interface = interface;
851 wl_list_insert(display->global_list.prev, &global->link);
853 wl_list_for_each(client, &display->client_list, link)
854 wl_resource_post_event(client->display_resource,
857 global->interface->name,
858 global->interface->version);
864 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
866 struct wl_client *client;
868 wl_list_for_each(client, &display->client_list, link)
869 wl_resource_post_event(client->display_resource,
870 WL_DISPLAY_GLOBAL_REMOVE, global->name);
871 wl_list_remove(&global->link);
876 wl_display_get_serial(struct wl_display *display)
878 return display->serial;
882 wl_display_next_serial(struct wl_display *display)
886 return display->serial;
889 WL_EXPORT struct wl_event_loop *
890 wl_display_get_event_loop(struct wl_display *display)
892 return display->loop;
896 wl_display_terminate(struct wl_display *display)
902 wl_display_run(struct wl_display *display)
907 wl_event_loop_dispatch(display->loop, -1);
911 socket_data(int fd, uint32_t mask, void *data)
913 struct wl_display *display = data;
914 struct sockaddr_un name;
918 length = sizeof name;
919 client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name,
922 fprintf(stderr, "failed to accept, errno: %d\n", errno);
924 wl_client_create(display, client_fd);
930 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
932 struct stat socket_stat;
933 int lock_size = name_size + 5;
935 snprintf(socket->lock_addr, lock_size,
936 "%s.lock", socket->addr.sun_path);
938 socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
939 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
941 if (socket->fd_lock < 0) {
943 "unable to open lockfile %s check permissions\n",
948 if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
950 "unable to lock lockfile %s, maybe another compositor is running\n",
952 close(socket->fd_lock);
956 if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
957 if (errno != ENOENT) {
958 fprintf(stderr, "did not manage to stat file %s\n",
959 socket->addr.sun_path);
960 close(socket->fd_lock);
963 } else if (socket_stat.st_mode & S_IWUSR ||
964 socket_stat.st_mode & S_IWGRP) {
965 unlink(socket->addr.sun_path);
972 wl_display_add_socket(struct wl_display *display, const char *name)
975 socklen_t size, name_size;
976 const char *runtime_dir;
978 s = malloc(sizeof *s);
982 s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
988 runtime_dir = getenv("XDG_RUNTIME_DIR");
989 if (runtime_dir == NULL) {
992 "XDG_RUNTIME_DIR not set, falling back to %s\n",
997 name = getenv("WAYLAND_DISPLAY");
1001 memset(&s->addr, 0, sizeof s->addr);
1002 s->addr.sun_family = AF_LOCAL;
1003 name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
1004 "%s/%s", runtime_dir, name) + 1;
1005 fprintf(stderr, "using socket %s\n", s->addr.sun_path);
1007 if (get_socket_lock(s,name_size) < 0) {
1013 size = offsetof (struct sockaddr_un, sun_path) + name_size;
1014 if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
1020 if (listen(s->fd, 1) < 0) {
1022 unlink(s->addr.sun_path);
1027 s->source = wl_event_loop_add_fd(display->loop, s->fd,
1029 socket_data, display);
1030 if (s->source == NULL) {
1032 unlink(s->addr.sun_path);
1036 wl_list_insert(display->socket_list.prev, &s->link);
1041 WL_EXPORT struct wl_resource *
1042 wl_client_add_object(struct wl_client *client,
1043 const struct wl_interface *interface,
1044 const void *implementation,
1045 uint32_t id, void *data)
1047 struct wl_resource *resource;
1049 resource = malloc(sizeof *resource);
1050 if (resource == NULL) {
1051 wl_resource_post_no_memory(client->display_resource);
1055 resource->object.interface = interface;
1056 resource->object.implementation = implementation;
1057 resource->object.id = id;
1058 resource->client = client;
1059 resource->data = data;
1060 resource->destroy = (void *) free;
1061 wl_signal_init(&resource->destroy_signal);
1063 if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
1064 wl_resource_post_no_memory(client->display_resource);
1072 WL_EXPORT struct wl_resource *
1073 wl_client_new_object(struct wl_client *client,
1074 const struct wl_interface *interface,
1075 const void *implementation, void *data)
1079 id = wl_map_insert_new(&client->objects, WL_MAP_SERVER_SIDE, NULL);
1080 return wl_client_add_object(client,
1081 interface, implementation, id, data);