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_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
475 default_grab_button(struct wl_pointer_grab *grab,
476 uint32_t time, int32_t button, int32_t state)
478 struct wl_input_device *device = grab->input_device;
479 struct wl_resource *resource;
481 resource = device->pointer_focus_resource;
483 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
484 time, button, state);
486 if (device->button_count == 0 && state == 0)
487 wl_input_device_set_pointer_focus(device,
488 device->current, time,
493 static const struct wl_pointer_grab_interface
494 default_pointer_grab_interface = {
501 default_grab_key(struct wl_keyboard_grab *grab,
502 uint32_t time, uint32_t key, int32_t state)
504 struct wl_input_device *device = grab->input_device;
505 struct wl_resource *resource;
507 resource = device->keyboard_focus_resource;
509 wl_resource_post_event(resource, WL_INPUT_DEVICE_KEY,
513 static const struct wl_keyboard_grab_interface
514 default_keyboard_grab_interface = {
519 wl_input_device_init(struct wl_input_device *device)
521 memset(device, 0, sizeof *device);
522 wl_list_init(&device->resource_list);
523 wl_array_init(&device->keys);
524 device->pointer_focus_listener.func = lose_pointer_focus;
525 device->keyboard_focus_listener.func = lose_keyboard_focus;
527 device->default_pointer_grab.interface = &default_pointer_grab_interface;
528 device->default_pointer_grab.input_device = device;
529 device->pointer_grab = &device->default_pointer_grab;
531 device->default_keyboard_grab.interface = &default_keyboard_grab_interface;
532 device->default_keyboard_grab.input_device = device;
533 device->keyboard_grab = &device->default_keyboard_grab;
535 wl_list_init(&device->drag_resource_list);
536 device->selection_data_source = NULL;
537 wl_list_init(&device->selection_listener_list);
544 wl_input_device_release(struct wl_input_device *device)
546 if (device->keyboard_focus_resource)
547 wl_list_remove(&device->keyboard_focus_listener.link);
549 if (device->pointer_focus_resource)
550 wl_list_remove(&device->pointer_focus_listener.link);
552 /* XXX: What about device->resource_list? */
554 wl_array_release(&device->keys);
557 static struct wl_resource *
558 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
560 struct wl_resource *r;
565 wl_list_for_each(r, list, link) {
566 if (r->client == surface->resource.client)
574 wl_input_device_set_pointer_focus(struct wl_input_device *device,
575 struct wl_surface *surface,
577 int32_t sx, int32_t sy)
579 struct wl_resource *resource;
581 if (device->pointer_focus == surface)
584 if (device->pointer_focus_resource) {
585 wl_resource_post_event(device->pointer_focus_resource,
586 WL_INPUT_DEVICE_POINTER_LEAVE,
587 time, device->pointer_focus);
588 wl_list_remove(&device->pointer_focus_listener.link);
591 resource = find_resource_for_surface(&device->resource_list, surface);
593 wl_resource_post_event(resource,
594 WL_INPUT_DEVICE_POINTER_ENTER,
595 time, surface, sx, sy);
596 wl_list_insert(resource->destroy_listener_list.prev,
597 &device->pointer_focus_listener.link);
600 device->pointer_focus_resource = resource;
601 device->pointer_focus = surface;
602 device->pointer_focus_time = time;
603 device->default_pointer_grab.focus = surface;
607 wl_input_device_set_keyboard_focus(struct wl_input_device *device,
608 struct wl_surface *surface,
611 struct wl_resource *resource;
613 if (device->keyboard_focus == surface)
616 if (device->keyboard_focus_resource) {
617 wl_resource_post_event(device->keyboard_focus_resource,
618 WL_INPUT_DEVICE_KEYBOARD_LEAVE,
619 time, device->keyboard_focus);
620 wl_list_remove(&device->keyboard_focus_listener.link);
623 resource = find_resource_for_surface(&device->resource_list, surface);
625 wl_resource_post_event(resource,
626 WL_INPUT_DEVICE_KEYBOARD_ENTER,
627 time, surface, &device->keys);
628 wl_list_insert(resource->destroy_listener_list.prev,
629 &device->keyboard_focus_listener.link);
632 device->keyboard_focus_resource = resource;
633 device->keyboard_focus = surface;
634 device->keyboard_focus_time = time;
638 wl_input_device_start_keyboard_grab(struct wl_input_device *device,
639 struct wl_keyboard_grab *grab, uint32_t time)
641 device->keyboard_grab = grab;
642 grab->input_device = device;
647 wl_input_device_end_keyboard_grab(struct wl_input_device *device, uint32_t time)
649 device->keyboard_grab = &device->default_keyboard_grab;
653 wl_input_device_start_pointer_grab(struct wl_input_device *device,
654 struct wl_pointer_grab *grab, uint32_t time)
656 const struct wl_pointer_grab_interface *interface;
658 device->pointer_grab = grab;
659 interface = device->pointer_grab->interface;
660 grab->input_device = device;
663 interface->focus(device->pointer_grab, time, device->current,
664 device->current_x, device->current_y);
668 wl_input_device_end_pointer_grab(struct wl_input_device *device, uint32_t time)
670 const struct wl_pointer_grab_interface *interface;
672 device->pointer_grab = &device->default_pointer_grab;
673 interface = device->pointer_grab->interface;
674 interface->focus(device->pointer_grab, time, device->current,
675 device->current_x, device->current_y);
679 display_bind(struct wl_client *client,
680 struct wl_resource *resource, uint32_t name,
681 const char *interface, uint32_t version, uint32_t id)
683 struct wl_global *global;
684 struct wl_display *display = resource->data;
686 wl_list_for_each(global, &display->global_list, link)
687 if (global->name == name)
690 if (&global->link == &display->global_list)
691 wl_resource_post_error(resource,
692 WL_DISPLAY_ERROR_INVALID_OBJECT,
693 "invalid global %d", name);
695 global->bind(client, global->data, version, id);
699 display_sync(struct wl_client *client,
700 struct wl_resource *resource, uint32_t id)
702 struct wl_resource *callback;
704 callback = wl_client_add_object(client,
705 &wl_callback_interface, NULL, id, NULL);
706 wl_resource_post_event(callback, WL_CALLBACK_DONE, 0);
707 wl_resource_destroy(callback, 0);
710 struct wl_display_interface display_interface = {
716 destroy_client_display_resource(struct wl_resource *resource)
718 resource->client->display_resource = NULL;
723 bind_display(struct wl_client *client,
724 void *data, uint32_t version, uint32_t id)
726 struct wl_display *display = data;
727 struct wl_global *global;
729 client->display_resource =
730 wl_client_add_object(client, &wl_display_interface,
731 &display_interface, id, display);
732 client->display_resource->destroy = destroy_client_display_resource;
734 wl_list_for_each(global, &display->global_list, link)
735 wl_resource_post_event(client->display_resource,
738 global->interface->name,
739 global->interface->version);
742 WL_EXPORT struct wl_display *
743 wl_display_create(void)
745 struct wl_display *display;
748 debug = getenv("WAYLAND_DEBUG");
752 display = malloc(sizeof *display);
756 display->loop = wl_event_loop_create();
757 if (display->loop == NULL) {
762 wl_list_init(&display->global_list);
763 wl_list_init(&display->socket_list);
764 wl_list_init(&display->client_list);
768 if (!wl_display_add_global(display, &wl_display_interface,
769 display, bind_display)) {
770 wl_event_loop_destroy(display->loop);
779 wl_display_destroy(struct wl_display *display)
781 struct wl_socket *s, *next;
782 struct wl_global *global, *gnext;
784 wl_list_for_each_safe(s, next, &display->socket_list, link) {
785 wl_event_source_remove(s->source);
787 unlink(s->addr.sun_path);
789 unlink(s->lock_addr);
792 wl_event_loop_destroy(display->loop);
794 wl_list_for_each_safe(global, gnext, &display->global_list, link)
800 WL_EXPORT struct wl_global *
801 wl_display_add_global(struct wl_display *display,
802 const struct wl_interface *interface,
803 void *data, wl_global_bind_func_t bind)
805 struct wl_global *global;
807 global = malloc(sizeof *global);
811 global->name = display->id++;
812 global->interface = interface;
815 wl_list_insert(display->global_list.prev, &global->link);
821 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
823 struct wl_client *client;
825 wl_list_for_each(client, &display->client_list, link)
826 wl_resource_post_event(client->display_resource,
827 WL_DISPLAY_GLOBAL_REMOVE, global->name);
828 wl_list_remove(&global->link);
832 WL_EXPORT struct wl_event_loop *
833 wl_display_get_event_loop(struct wl_display *display)
835 return display->loop;
839 wl_display_terminate(struct wl_display *display)
845 wl_display_run(struct wl_display *display)
850 wl_event_loop_dispatch(display->loop, -1);
854 socket_data(int fd, uint32_t mask, void *data)
856 struct wl_display *display = data;
857 struct sockaddr_un name;
861 length = sizeof name;
863 accept4(fd, (struct sockaddr *) &name, &length, SOCK_CLOEXEC);
864 if (client_fd < 0 && errno == ENOSYS) {
865 client_fd = accept(fd, (struct sockaddr *) &name, &length);
866 if (client_fd >= 0 && fcntl(client_fd, F_SETFD, FD_CLOEXEC) == -1)
867 fprintf(stderr, "failed to set FD_CLOEXEC flag on client fd, errno: %d\n", errno);
871 fprintf(stderr, "failed to accept, errno: %d\n", errno);
873 wl_client_create(display, client_fd);
879 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
881 struct stat socket_stat;
882 int lock_size = name_size + 5;
884 snprintf(socket->lock_addr, lock_size,
885 "%s.lock", socket->addr.sun_path);
887 socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
888 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
890 if (socket->fd_lock < 0) {
892 "unable to open lockfile %s check permissions\n",
897 if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
899 "unable to lock lockfile %s, maybe another compositor is running\n",
901 close(socket->fd_lock);
905 if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
906 if (errno != ENOENT) {
907 fprintf(stderr, "did not manage to stat file %s\n",
908 socket->addr.sun_path);
909 close(socket->fd_lock);
912 } else if (socket_stat.st_mode & S_IWUSR ||
913 socket_stat.st_mode & S_IWGRP) {
914 unlink(socket->addr.sun_path);
921 wl_display_add_socket(struct wl_display *display, const char *name)
924 socklen_t size, name_size;
925 const char *runtime_dir;
927 s = malloc(sizeof *s);
931 s->fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
937 runtime_dir = getenv("XDG_RUNTIME_DIR");
938 if (runtime_dir == NULL) {
941 "XDG_RUNTIME_DIR not set, falling back to %s\n",
946 name = getenv("WAYLAND_DISPLAY");
950 memset(&s->addr, 0, sizeof s->addr);
951 s->addr.sun_family = AF_LOCAL;
952 name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
953 "%s/%s", runtime_dir, name) + 1;
954 fprintf(stderr, "using socket %s\n", s->addr.sun_path);
956 if (get_socket_lock(s,name_size) < 0) {
962 size = offsetof (struct sockaddr_un, sun_path) + name_size;
963 if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
969 if (listen(s->fd, 1) < 0) {
971 unlink(s->addr.sun_path);
976 s->source = wl_event_loop_add_fd(display->loop, s->fd,
978 socket_data, display);
979 if (s->source == NULL) {
981 unlink(s->addr.sun_path);
985 wl_list_insert(display->socket_list.prev, &s->link);
990 WL_EXPORT struct wl_resource *
991 wl_client_add_object(struct wl_client *client,
992 const struct wl_interface *interface,
993 const void *implementation,
994 uint32_t id, void *data)
996 struct wl_resource *resource;
998 resource = malloc(sizeof *resource);
999 if (resource == NULL) {
1000 wl_resource_post_no_memory(client->display_resource);
1004 resource->object.interface = interface;
1005 resource->object.implementation = implementation;
1006 resource->object.id = id;
1007 resource->client = client;
1008 resource->data = data;
1009 resource->destroy = (void *) free;
1010 wl_list_init(&resource->destroy_listener_list);
1012 if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
1013 wl_resource_post_no_memory(client->display_resource);
1021 WL_EXPORT struct wl_resource *
1022 wl_client_new_object(struct wl_client *client,
1023 const struct wl_interface *interface,
1024 const void *implementation, void *data)
1028 id = wl_map_insert_new(&client->objects, WL_MAP_SERVER_SIDE, NULL);
1029 return wl_client_add_object(client,
1030 interface, implementation, id, data);