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"
51 struct sockaddr_un addr;
54 struct wl_event_source *source;
58 struct wl_connection *connection;
59 struct wl_event_source *source;
60 struct wl_display *display;
61 struct wl_resource *display_resource;
65 struct wl_map objects;
71 struct wl_event_loop *loop;
76 struct wl_list global_list;
77 struct wl_list socket_list;
78 struct wl_list client_list;
82 const struct wl_interface *interface;
85 wl_global_bind_func_t bind;
89 static int wl_debug = 0;
92 destroy_client(void *data)
94 struct wl_client *client = data;
96 wl_client_destroy(client);
100 wl_resource_post_event(struct wl_resource *resource, uint32_t opcode, ...)
102 struct wl_closure *closure;
103 struct wl_object *object = &resource->object;
106 va_start(ap, opcode);
107 closure = wl_connection_vmarshal(resource->client->connection,
109 &object->interface->events[opcode]);
115 if (wl_closure_send(closure, resource->client->connection))
116 wl_event_loop_add_idle(resource->client->display->loop,
117 destroy_client, resource->client);
120 wl_closure_print(closure, object, true);
122 wl_closure_destroy(closure);
127 wl_resource_queue_event(struct wl_resource *resource, uint32_t opcode, ...)
129 struct wl_closure *closure;
130 struct wl_object *object = &resource->object;
133 va_start(ap, opcode);
134 closure = wl_connection_vmarshal(resource->client->connection,
136 &object->interface->events[opcode]);
142 if (wl_closure_queue(closure, resource->client->connection))
143 wl_event_loop_add_idle(resource->client->display->loop,
144 destroy_client, resource->client);
147 wl_closure_print(closure, object, true);
149 wl_closure_destroy(closure);
153 wl_resource_post_error(struct wl_resource *resource,
154 uint32_t code, const char *msg, ...)
156 struct wl_client *client = resource->client;
161 vsnprintf(buffer, sizeof buffer, msg, ap);
167 * When a client aborts, its resources are destroyed in id order,
168 * which means the display resource is destroyed first. If destruction
169 * of any later resources results in a protocol error, we end up here
170 * with a NULL display_resource. Do not try to send errors to an
171 * already dead client.
173 if (!client->display_resource)
176 wl_resource_post_event(client->display_resource,
177 WL_DISPLAY_ERROR, resource, code, buffer);
181 wl_client_connection_data(int fd, uint32_t mask, void *data)
183 struct wl_client *client = data;
184 struct wl_connection *connection = client->connection;
185 struct wl_resource *resource;
186 struct wl_object *object;
187 struct wl_closure *closure;
188 const struct wl_message *message;
189 uint32_t p[2], opcode, size;
193 if (mask & WL_EVENT_READABLE)
194 cmask |= WL_CONNECTION_READABLE;
195 if (mask & WL_EVENT_WRITABLE)
196 cmask |= WL_CONNECTION_WRITABLE;
198 len = wl_connection_data(connection, cmask);
200 wl_client_destroy(client);
204 while (len >= sizeof p) {
205 wl_connection_copy(connection, p, sizeof p);
206 opcode = p[1] & 0xffff;
211 resource = wl_map_lookup(&client->objects, p[0]);
212 if (resource == NULL) {
213 wl_resource_post_error(client->display_resource,
214 WL_DISPLAY_ERROR_INVALID_OBJECT,
215 "invalid object %d", p[0]);
219 object = &resource->object;
220 if (opcode >= object->interface->method_count) {
221 wl_resource_post_error(client->display_resource,
222 WL_DISPLAY_ERROR_INVALID_METHOD,
223 "invalid method %d, object %s@%d",
224 object->interface->name,
229 message = &object->interface->methods[opcode];
230 closure = wl_connection_demarshal(client->connection, size,
231 &client->objects, message);
234 if (closure == NULL && errno == EINVAL) {
235 wl_resource_post_error(client->display_resource,
236 WL_DISPLAY_ERROR_INVALID_METHOD,
237 "invalid arguments for %s@%d.%s",
238 object->interface->name,
242 } else if (closure == NULL && errno == ENOMEM) {
243 wl_resource_post_no_memory(resource);
248 wl_closure_print(closure, object, false);
250 wl_closure_invoke(closure, object,
251 object->implementation[opcode], client);
253 wl_closure_destroy(closure);
260 wl_client_destroy(client);
266 wl_client_connection_update(struct wl_connection *connection,
267 uint32_t mask, void *data)
269 struct wl_client *client = data;
273 if (mask & WL_CONNECTION_READABLE)
274 emask |= WL_EVENT_READABLE;
275 if (mask & WL_CONNECTION_WRITABLE)
276 emask |= WL_EVENT_WRITABLE;
278 return wl_event_source_fd_update(client->source, emask);
282 wl_client_flush(struct wl_client *client)
284 if (client->mask & WL_CONNECTION_WRITABLE)
285 wl_connection_data(client->connection, WL_CONNECTION_WRITABLE);
288 WL_EXPORT struct wl_display *
289 wl_client_get_display(struct wl_client *client)
291 return client->display;
295 bind_display(struct wl_client *client,
296 void *data, uint32_t version, uint32_t id);
298 WL_EXPORT struct wl_client *
299 wl_client_create(struct wl_display *display, int fd)
301 struct wl_client *client;
304 client = malloc(sizeof *client);
308 memset(client, 0, sizeof *client);
309 client->display = display;
310 client->source = wl_event_loop_add_fd(display->loop, fd,
312 wl_client_connection_data, client);
314 len = sizeof client->ucred;
315 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
316 &client->ucred, &len) < 0) {
322 wl_connection_create(fd, wl_client_connection_update, client);
323 if (client->connection == NULL) {
328 wl_map_init(&client->objects);
330 if (wl_map_insert_at(&client->objects, 0, NULL) < 0) {
331 wl_map_release(&client->objects);
336 bind_display(client, display, 1, 1);
338 wl_list_insert(display->client_list.prev, &client->link);
344 wl_client_get_credentials(struct wl_client *client,
345 pid_t *pid, uid_t *uid, gid_t *gid)
348 *pid = client->ucred.pid;
350 *uid = client->ucred.uid;
352 *gid = client->ucred.gid;
356 wl_client_add_resource(struct wl_client *client,
357 struct wl_resource *resource)
359 if (resource->object.id == 0)
360 resource->object.id =
361 wl_map_insert_new(&client->objects,
362 WL_MAP_SERVER_SIDE, resource);
364 wl_map_insert_at(&client->objects,
365 resource->object.id, resource);
367 resource->client = client;
368 wl_list_init(&resource->destroy_listener_list);
372 wl_resource_post_no_memory(struct wl_resource *resource)
374 wl_resource_post_error(resource->client->display_resource,
375 WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
379 destroy_resource(void *element, void *data)
381 struct wl_resource *resource = element;
382 struct wl_listener *l, *next;
383 uint32_t *time = data;
385 wl_list_for_each_safe(l, next, &resource->destroy_listener_list, link)
386 l->func(l, resource, *time);
388 if (resource->destroy)
389 resource->destroy(resource);
393 wl_resource_destroy(struct wl_resource *resource, uint32_t time)
395 struct wl_client *client = resource->client;
398 id = resource->object.id;
399 destroy_resource(resource, &time);
401 if (id < WL_SERVER_ID_START) {
402 if (client->display_resource) {
403 wl_resource_queue_event(client->display_resource,
404 WL_DISPLAY_DELETE_ID, id);
406 wl_map_insert_at(&client->objects, id, NULL);
408 wl_map_remove(&client->objects, id);
413 wl_client_destroy(struct wl_client *client)
417 printf("disconnect from client %p\n", client);
419 wl_client_flush(client);
420 wl_map_for_each(&client->objects, destroy_resource, &time);
421 wl_map_release(&client->objects);
422 wl_event_source_remove(client->source);
423 wl_connection_destroy(client->connection);
424 wl_list_remove(&client->link);
429 lose_pointer_focus(struct wl_listener *listener,
430 struct wl_resource *resource, uint32_t time)
432 struct wl_input_device *device =
433 container_of(listener, struct wl_input_device,
434 pointer_focus_listener);
436 device->pointer_focus_resource = NULL;
440 lose_keyboard_focus(struct wl_listener *listener,
441 struct wl_resource *resource, uint32_t time)
443 struct wl_input_device *device =
444 container_of(listener, struct wl_input_device,
445 keyboard_focus_listener);
447 device->keyboard_focus_resource = NULL;
451 default_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
452 struct wl_surface *surface, int32_t x, int32_t y)
454 struct wl_input_device *device = grab->input_device;
456 if (device->button_count > 0)
459 wl_input_device_set_pointer_focus(device, surface, time, x, y);
463 default_grab_motion(struct wl_pointer_grab *grab,
464 uint32_t time, int32_t x, int32_t y)
466 struct wl_resource *resource;
468 resource = grab->input_device->pointer_focus_resource;
470 wl_input_device_send_motion(resource, time, x, y);
474 default_grab_button(struct wl_pointer_grab *grab,
475 uint32_t time, int32_t button, int32_t state)
477 struct wl_input_device *device = grab->input_device;
478 struct wl_resource *resource;
480 resource = device->pointer_focus_resource;
482 wl_input_device_send_button(resource, time, button, state);
484 if (device->button_count == 0 && state == 0)
485 wl_input_device_set_pointer_focus(device,
486 device->current, time,
491 static const struct wl_pointer_grab_interface
492 default_pointer_grab_interface = {
499 default_grab_key(struct wl_keyboard_grab *grab,
500 uint32_t time, uint32_t key, int32_t state)
502 struct wl_input_device *device = grab->input_device;
503 struct wl_resource *resource;
505 resource = device->keyboard_focus_resource;
507 wl_input_device_send_key(resource, time, key, state);
510 static const struct wl_keyboard_grab_interface
511 default_keyboard_grab_interface = {
516 wl_input_device_init(struct wl_input_device *device)
518 memset(device, 0, sizeof *device);
519 wl_list_init(&device->resource_list);
520 wl_array_init(&device->keys);
521 device->pointer_focus_listener.func = lose_pointer_focus;
522 device->keyboard_focus_listener.func = lose_keyboard_focus;
524 device->default_pointer_grab.interface = &default_pointer_grab_interface;
525 device->default_pointer_grab.input_device = device;
526 device->pointer_grab = &device->default_pointer_grab;
528 device->default_keyboard_grab.interface = &default_keyboard_grab_interface;
529 device->default_keyboard_grab.input_device = device;
530 device->keyboard_grab = &device->default_keyboard_grab;
532 wl_list_init(&device->drag_resource_list);
533 device->selection_data_source = NULL;
534 wl_list_init(&device->selection_listener_list);
541 wl_input_device_release(struct wl_input_device *device)
543 if (device->keyboard_focus_resource)
544 wl_list_remove(&device->keyboard_focus_listener.link);
546 if (device->pointer_focus_resource)
547 wl_list_remove(&device->pointer_focus_listener.link);
549 /* XXX: What about device->resource_list? */
551 wl_array_release(&device->keys);
554 static struct wl_resource *
555 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
557 struct wl_resource *r;
562 wl_list_for_each(r, list, link) {
563 if (r->client == surface->resource.client)
571 wl_input_device_set_pointer_focus(struct wl_input_device *device,
572 struct wl_surface *surface,
574 int32_t sx, int32_t sy)
576 struct wl_resource *resource;
578 if (device->pointer_focus == surface)
581 if (device->pointer_focus_resource) {
582 wl_input_device_send_pointer_leave(
583 device->pointer_focus_resource,
584 time, device->pointer_focus);
585 wl_list_remove(&device->pointer_focus_listener.link);
588 resource = find_resource_for_surface(&device->resource_list, surface);
590 wl_input_device_send_pointer_enter(resource, time,
592 wl_list_insert(resource->destroy_listener_list.prev,
593 &device->pointer_focus_listener.link);
596 device->pointer_focus_resource = resource;
597 device->pointer_focus = surface;
598 device->pointer_focus_time = time;
599 device->default_pointer_grab.focus = surface;
603 wl_input_device_set_keyboard_focus(struct wl_input_device *device,
604 struct wl_surface *surface,
607 struct wl_resource *resource;
609 if (device->keyboard_focus == surface)
612 if (device->keyboard_focus_resource) {
613 wl_input_device_send_keyboard_leave(
614 device->keyboard_focus_resource,
615 time, device->keyboard_focus);
616 wl_list_remove(&device->keyboard_focus_listener.link);
619 resource = find_resource_for_surface(&device->resource_list, surface);
621 wl_input_device_send_keyboard_enter(resource, time,
622 surface, &device->keys);
623 wl_list_insert(resource->destroy_listener_list.prev,
624 &device->keyboard_focus_listener.link);
627 device->keyboard_focus_resource = resource;
628 device->keyboard_focus = surface;
629 device->keyboard_focus_time = time;
633 wl_input_device_start_keyboard_grab(struct wl_input_device *device,
634 struct wl_keyboard_grab *grab, uint32_t time)
636 device->keyboard_grab = grab;
637 grab->input_device = device;
642 wl_input_device_end_keyboard_grab(struct wl_input_device *device, uint32_t time)
644 device->keyboard_grab = &device->default_keyboard_grab;
648 wl_input_device_start_pointer_grab(struct wl_input_device *device,
649 struct wl_pointer_grab *grab, uint32_t time)
651 const struct wl_pointer_grab_interface *interface;
653 device->pointer_grab = grab;
654 interface = device->pointer_grab->interface;
655 grab->input_device = device;
658 interface->focus(device->pointer_grab, time, device->current,
659 device->current_x, device->current_y);
663 wl_input_device_end_pointer_grab(struct wl_input_device *device, uint32_t time)
665 const struct wl_pointer_grab_interface *interface;
667 device->pointer_grab = &device->default_pointer_grab;
668 interface = device->pointer_grab->interface;
669 interface->focus(device->pointer_grab, time, device->current,
670 device->current_x, device->current_y);
674 display_bind(struct wl_client *client,
675 struct wl_resource *resource, uint32_t name,
676 const char *interface, uint32_t version, uint32_t id)
678 struct wl_global *global;
679 struct wl_display *display = resource->data;
681 wl_list_for_each(global, &display->global_list, link)
682 if (global->name == name)
685 if (&global->link == &display->global_list)
686 wl_resource_post_error(resource,
687 WL_DISPLAY_ERROR_INVALID_OBJECT,
688 "invalid global %d", name);
690 global->bind(client, global->data, version, id);
694 display_sync(struct wl_client *client,
695 struct wl_resource *resource, uint32_t id)
697 struct wl_resource *callback;
699 callback = wl_client_add_object(client,
700 &wl_callback_interface, NULL, id, NULL);
701 wl_callback_send_done(callback, 0);
702 wl_resource_destroy(callback, 0);
705 struct wl_display_interface display_interface = {
711 destroy_client_display_resource(struct wl_resource *resource)
713 resource->client->display_resource = NULL;
718 bind_display(struct wl_client *client,
719 void *data, uint32_t version, uint32_t id)
721 struct wl_display *display = data;
722 struct wl_global *global;
724 client->display_resource =
725 wl_client_add_object(client, &wl_display_interface,
726 &display_interface, id, display);
727 client->display_resource->destroy = destroy_client_display_resource;
729 wl_list_for_each(global, &display->global_list, link)
730 wl_resource_post_event(client->display_resource,
733 global->interface->name,
734 global->interface->version);
737 WL_EXPORT struct wl_display *
738 wl_display_create(void)
740 struct wl_display *display;
743 debug = getenv("WAYLAND_DEBUG");
747 display = malloc(sizeof *display);
751 display->loop = wl_event_loop_create();
752 if (display->loop == NULL) {
757 wl_list_init(&display->global_list);
758 wl_list_init(&display->socket_list);
759 wl_list_init(&display->client_list);
763 if (!wl_display_add_global(display, &wl_display_interface,
764 display, bind_display)) {
765 wl_event_loop_destroy(display->loop);
774 wl_display_destroy(struct wl_display *display)
776 struct wl_socket *s, *next;
777 struct wl_global *global, *gnext;
779 wl_list_for_each_safe(s, next, &display->socket_list, link) {
780 wl_event_source_remove(s->source);
782 unlink(s->addr.sun_path);
784 unlink(s->lock_addr);
787 wl_event_loop_destroy(display->loop);
789 wl_list_for_each_safe(global, gnext, &display->global_list, link)
795 WL_EXPORT struct wl_global *
796 wl_display_add_global(struct wl_display *display,
797 const struct wl_interface *interface,
798 void *data, wl_global_bind_func_t bind)
800 struct wl_global *global;
802 global = malloc(sizeof *global);
806 global->name = display->id++;
807 global->interface = interface;
810 wl_list_insert(display->global_list.prev, &global->link);
816 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
818 struct wl_client *client;
820 wl_list_for_each(client, &display->client_list, link)
821 wl_resource_post_event(client->display_resource,
822 WL_DISPLAY_GLOBAL_REMOVE, global->name);
823 wl_list_remove(&global->link);
827 WL_EXPORT struct wl_event_loop *
828 wl_display_get_event_loop(struct wl_display *display)
830 return display->loop;
834 wl_display_terminate(struct wl_display *display)
840 wl_display_run(struct wl_display *display)
845 wl_event_loop_dispatch(display->loop, -1);
849 socket_data(int fd, uint32_t mask, void *data)
851 struct wl_display *display = data;
852 struct sockaddr_un name;
856 length = sizeof name;
858 accept4(fd, (struct sockaddr *) &name, &length, SOCK_CLOEXEC);
859 if (client_fd < 0 && errno == ENOSYS) {
860 client_fd = accept(fd, (struct sockaddr *) &name, &length);
861 if (client_fd >= 0 && fcntl(client_fd, F_SETFD, FD_CLOEXEC) == -1)
862 fprintf(stderr, "failed to set FD_CLOEXEC flag on client fd, errno: %d\n", errno);
866 fprintf(stderr, "failed to accept, errno: %d\n", errno);
868 wl_client_create(display, client_fd);
874 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
876 struct stat socket_stat;
877 int lock_size = name_size + 5;
879 snprintf(socket->lock_addr, lock_size,
880 "%s.lock", socket->addr.sun_path);
882 socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
883 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
885 if (socket->fd_lock < 0) {
887 "unable to open lockfile %s check permissions\n",
892 if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
894 "unable to lock lockfile %s, maybe another compositor is running\n",
896 close(socket->fd_lock);
900 if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
901 if (errno != ENOENT) {
902 fprintf(stderr, "did not manage to stat file %s\n",
903 socket->addr.sun_path);
904 close(socket->fd_lock);
907 } else if (socket_stat.st_mode & S_IWUSR ||
908 socket_stat.st_mode & S_IWGRP) {
909 unlink(socket->addr.sun_path);
916 wl_display_add_socket(struct wl_display *display, const char *name)
919 socklen_t size, name_size;
920 const char *runtime_dir;
922 s = malloc(sizeof *s);
926 s->fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
932 runtime_dir = getenv("XDG_RUNTIME_DIR");
933 if (runtime_dir == NULL) {
936 "XDG_RUNTIME_DIR not set, falling back to %s\n",
941 name = getenv("WAYLAND_DISPLAY");
945 memset(&s->addr, 0, sizeof s->addr);
946 s->addr.sun_family = AF_LOCAL;
947 name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
948 "%s/%s", runtime_dir, name) + 1;
949 fprintf(stderr, "using socket %s\n", s->addr.sun_path);
951 if (get_socket_lock(s,name_size) < 0) {
957 size = offsetof (struct sockaddr_un, sun_path) + name_size;
958 if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
964 if (listen(s->fd, 1) < 0) {
966 unlink(s->addr.sun_path);
971 s->source = wl_event_loop_add_fd(display->loop, s->fd,
973 socket_data, display);
974 if (s->source == NULL) {
976 unlink(s->addr.sun_path);
980 wl_list_insert(display->socket_list.prev, &s->link);
985 WL_EXPORT struct wl_resource *
986 wl_client_add_object(struct wl_client *client,
987 const struct wl_interface *interface,
988 const void *implementation,
989 uint32_t id, void *data)
991 struct wl_resource *resource;
993 resource = malloc(sizeof *resource);
994 if (resource == NULL) {
995 wl_resource_post_no_memory(client->display_resource);
999 resource->object.interface = interface;
1000 resource->object.implementation = implementation;
1001 resource->object.id = id;
1002 resource->client = client;
1003 resource->data = data;
1004 resource->destroy = (void *) free;
1005 wl_list_init(&resource->destroy_listener_list);
1007 if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
1008 wl_resource_post_no_memory(client->display_resource);
1016 WL_EXPORT struct wl_resource *
1017 wl_client_new_object(struct wl_client *client,
1018 const struct wl_interface *interface,
1019 const void *implementation, void *data)
1023 id = wl_map_insert_new(&client->objects, WL_MAP_SERVER_SIDE, NULL);
1024 return wl_client_add_object(client,
1025 interface, implementation, id, data);