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"
49 /* This is the size of the char array in struct sock_addr_un.
50 No Wayland socket can be created with a path longer than this,
51 including the null terminator. */
53 #define UNIX_PATH_MAX 108
56 #define LOCK_SUFFIX ".lock"
57 #define LOCK_SUFFIXLEN 5
62 struct sockaddr_un addr;
63 char lock_addr[UNIX_PATH_MAX + LOCK_SUFFIXLEN];
65 struct wl_event_source *source;
69 struct wl_connection *connection;
70 struct wl_event_source *source;
71 struct wl_display *display;
72 struct wl_resource *display_resource;
76 struct wl_map objects;
77 struct wl_signal destroy_signal;
83 struct wl_event_loop *loop;
89 struct wl_list registry_resource_list;
90 struct wl_list global_list;
91 struct wl_list socket_list;
92 struct wl_list client_list;
94 struct wl_signal destroy_signal;
98 const struct wl_interface *interface;
101 wl_global_bind_func_t bind;
105 static int wl_debug = 0;
108 destroy_client(void *data)
110 struct wl_client *client = data;
112 wl_client_destroy(client);
116 wl_resource_post_event(struct wl_resource *resource, uint32_t opcode, ...)
118 struct wl_closure *closure;
119 struct wl_object *object = &resource->object;
122 va_start(ap, opcode);
123 closure = wl_closure_vmarshal(object, opcode, ap,
124 &object->interface->events[opcode]);
130 if (wl_closure_send(closure, resource->client->connection))
131 wl_event_loop_add_idle(resource->client->display->loop,
132 destroy_client, resource->client);
135 wl_closure_print(closure, object, true);
137 wl_closure_destroy(closure);
142 wl_resource_queue_event(struct wl_resource *resource, uint32_t opcode, ...)
144 struct wl_closure *closure;
145 struct wl_object *object = &resource->object;
148 va_start(ap, opcode);
149 closure = wl_closure_vmarshal(object, opcode, ap,
150 &object->interface->events[opcode]);
156 if (wl_closure_queue(closure, resource->client->connection))
157 wl_event_loop_add_idle(resource->client->display->loop,
158 destroy_client, resource->client);
161 wl_closure_print(closure, object, true);
163 wl_closure_destroy(closure);
167 wl_resource_post_error(struct wl_resource *resource,
168 uint32_t code, const char *msg, ...)
170 struct wl_client *client = resource->client;
175 vsnprintf(buffer, sizeof buffer, msg, ap);
181 * When a client aborts, its resources are destroyed in id order,
182 * which means the display resource is destroyed first. If destruction
183 * of any later resources results in a protocol error, we end up here
184 * with a NULL display_resource. Do not try to send errors to an
185 * already dead client.
187 if (!client->display_resource)
190 wl_resource_post_event(client->display_resource,
191 WL_DISPLAY_ERROR, resource, code, buffer);
195 deref_new_objects(struct wl_closure *closure)
197 const char *signature;
200 signature = closure->message->signature;
201 for (i = 0; signature[i]; i++) {
202 switch (signature[i]) {
204 closure->args[i + 2] = *(uint32_t **) closure->args[i + 2];
205 closure->types[i] = &ffi_type_uint32;
212 wl_client_connection_data(int fd, uint32_t mask, void *data)
214 struct wl_client *client = data;
215 struct wl_connection *connection = client->connection;
216 struct wl_resource *resource;
217 struct wl_object *object;
218 struct wl_closure *closure;
219 const struct wl_message *message;
224 if (mask & (WL_EVENT_ERROR | WL_EVENT_HANGUP)) {
225 wl_client_destroy(client);
229 if (mask & WL_EVENT_WRITABLE) {
230 len = wl_connection_flush(connection);
231 if (len < 0 && errno != EAGAIN) {
232 wl_client_destroy(client);
234 } else if (len >= 0) {
235 wl_event_source_fd_update(client->source,
241 if (mask & WL_EVENT_READABLE) {
242 len = wl_connection_read(connection);
243 if (len < 0 && errno != EAGAIN) {
244 wl_client_destroy(client);
249 while ((size_t) len >= sizeof p) {
250 wl_connection_copy(connection, p, sizeof p);
251 opcode = p[1] & 0xffff;
256 resource = wl_map_lookup(&client->objects, p[0]);
257 if (resource == NULL) {
258 wl_resource_post_error(client->display_resource,
259 WL_DISPLAY_ERROR_INVALID_OBJECT,
260 "invalid object %u", p[0]);
264 object = &resource->object;
265 if (opcode >= object->interface->method_count) {
266 wl_resource_post_error(client->display_resource,
267 WL_DISPLAY_ERROR_INVALID_METHOD,
268 "invalid method %d, object %s@%u",
270 object->interface->name,
275 message = &object->interface->methods[opcode];
276 closure = wl_connection_demarshal(client->connection, size,
277 &client->objects, message);
280 if ((closure == NULL && errno == EINVAL) ||
281 wl_closure_lookup_objects(closure, &client->objects) < 0) {
282 wl_resource_post_error(client->display_resource,
283 WL_DISPLAY_ERROR_INVALID_METHOD,
284 "invalid arguments for %s@%u.%s",
285 object->interface->name,
289 } else if (closure == NULL && errno == ENOMEM) {
290 wl_resource_post_no_memory(resource);
295 wl_closure_print(closure, object, false);
297 deref_new_objects(closure);
299 wl_closure_invoke(closure, object,
300 object->implementation[opcode], client);
302 wl_closure_destroy(closure);
309 wl_client_destroy(client);
315 wl_client_flush(struct wl_client *client)
317 wl_connection_flush(client->connection);
320 WL_EXPORT struct wl_display *
321 wl_client_get_display(struct wl_client *client)
323 return client->display;
327 bind_display(struct wl_client *client,
328 void *data, uint32_t version, uint32_t id);
330 WL_EXPORT struct wl_client *
331 wl_client_create(struct wl_display *display, int fd)
333 struct wl_client *client;
336 client = malloc(sizeof *client);
340 memset(client, 0, sizeof *client);
341 client->display = display;
342 client->source = wl_event_loop_add_fd(display->loop, fd,
344 wl_client_connection_data, client);
349 len = sizeof client->ucred;
350 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
351 &client->ucred, &len) < 0)
354 client->connection = wl_connection_create(fd);
355 if (client->connection == NULL)
358 wl_map_init(&client->objects);
360 if (wl_map_insert_at(&client->objects, 0, NULL) < 0)
363 wl_signal_init(&client->destroy_signal);
364 bind_display(client, display, 1, 1);
366 if (!client->display_resource)
369 wl_list_insert(display->client_list.prev, &client->link);
374 wl_map_release(&client->objects);
375 wl_connection_destroy(client->connection);
377 wl_event_source_remove(client->source);
384 wl_client_get_credentials(struct wl_client *client,
385 pid_t *pid, uid_t *uid, gid_t *gid)
388 *pid = client->ucred.pid;
390 *uid = client->ucred.uid;
392 *gid = client->ucred.gid;
396 wl_client_add_resource(struct wl_client *client,
397 struct wl_resource *resource)
399 if (resource->object.id == 0) {
400 resource->object.id =
401 wl_map_insert_new(&client->objects,
402 WL_MAP_SERVER_SIDE, resource);
403 } else if (wl_map_insert_at(&client->objects,
404 resource->object.id, resource) < 0) {
405 wl_resource_post_error(client->display_resource,
406 WL_DISPLAY_ERROR_INVALID_OBJECT,
408 resource->object.id);
412 resource->client = client;
413 wl_signal_init(&resource->destroy_signal);
415 return resource->object.id;
418 WL_EXPORT struct wl_resource *
419 wl_client_get_object(struct wl_client *client, uint32_t id)
421 return wl_map_lookup(&client->objects, id);
425 wl_resource_post_no_memory(struct wl_resource *resource)
427 wl_resource_post_error(resource->client->display_resource,
428 WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
432 destroy_resource(void *element, void *data)
434 struct wl_resource *resource = element;
436 wl_signal_emit(&resource->destroy_signal, resource);
438 if (resource->destroy)
439 resource->destroy(resource);
443 wl_resource_destroy(struct wl_resource *resource)
445 struct wl_client *client = resource->client;
448 id = resource->object.id;
449 destroy_resource(resource, NULL);
451 if (id < WL_SERVER_ID_START) {
452 if (client->display_resource) {
453 wl_resource_queue_event(client->display_resource,
454 WL_DISPLAY_DELETE_ID, id);
456 wl_map_insert_at(&client->objects, id, NULL);
458 wl_map_remove(&client->objects, id);
463 wl_client_add_destroy_listener(struct wl_client *client,
464 struct wl_listener *listener)
466 wl_signal_add(&client->destroy_signal, listener);
469 WL_EXPORT struct wl_listener *
470 wl_client_get_destroy_listener(struct wl_client *client,
471 wl_notify_func_t notify)
473 return wl_signal_get(&client->destroy_signal, notify);
477 wl_client_destroy(struct wl_client *client)
481 wl_log("disconnect from client %p\n", client);
483 wl_signal_emit(&client->destroy_signal, client);
485 wl_client_flush(client);
486 wl_map_for_each(&client->objects, destroy_resource, &serial);
487 wl_map_release(&client->objects);
488 wl_event_source_remove(client->source);
489 wl_connection_destroy(client->connection);
490 wl_list_remove(&client->link);
495 lose_pointer_focus(struct wl_listener *listener, void *data)
497 struct wl_pointer *pointer =
498 container_of(listener, struct wl_pointer, focus_listener);
500 pointer->focus_resource = NULL;
504 lose_keyboard_focus(struct wl_listener *listener, void *data)
506 struct wl_keyboard *keyboard =
507 container_of(listener, struct wl_keyboard, focus_listener);
509 keyboard->focus_resource = NULL;
513 lose_touch_focus(struct wl_listener *listener, void *data)
515 struct wl_touch *touch =
516 container_of(listener, struct wl_touch, focus_listener);
518 touch->focus_resource = NULL;
522 default_grab_focus(struct wl_pointer_grab *grab,
523 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
525 struct wl_pointer *pointer = grab->pointer;
527 if (pointer->button_count > 0)
530 wl_pointer_set_focus(pointer, surface, x, y);
534 default_grab_motion(struct wl_pointer_grab *grab,
535 uint32_t time, wl_fixed_t x, wl_fixed_t y)
537 struct wl_resource *resource;
539 resource = grab->pointer->focus_resource;
541 wl_pointer_send_motion(resource, time, x, y);
545 default_grab_button(struct wl_pointer_grab *grab,
546 uint32_t time, uint32_t button, uint32_t state_w)
548 struct wl_pointer *pointer = grab->pointer;
549 struct wl_resource *resource;
551 enum wl_pointer_button_state state = state_w;
553 resource = pointer->focus_resource;
555 serial = wl_display_next_serial(resource->client->display);
556 wl_pointer_send_button(resource, serial, time, button, state_w);
559 if (pointer->button_count == 0 &&
560 state == WL_POINTER_BUTTON_STATE_RELEASED)
561 wl_pointer_set_focus(pointer, pointer->current,
562 pointer->current_x, pointer->current_y);
565 static const struct wl_pointer_grab_interface
566 default_pointer_grab_interface = {
572 static void default_grab_touch_down(struct wl_touch_grab *grab,
578 struct wl_touch *touch = grab->touch;
581 if (touch->focus_resource && touch->focus) {
582 serial = wl_display_next_serial(touch->focus_resource->client->display);
583 wl_touch_send_down(touch->focus_resource, serial, time,
584 &touch->focus->resource, touch_id, sx, sy);
588 static void default_grab_touch_up(struct wl_touch_grab *grab,
592 struct wl_touch *touch = grab->touch;
595 if (touch->focus_resource) {
596 serial = wl_display_next_serial(touch->focus_resource->client->display);
597 wl_touch_send_up(touch->focus_resource, serial, time, touch_id);
601 static void default_grab_touch_motion(struct wl_touch_grab *grab,
607 struct wl_touch *touch = grab->touch;
609 if (touch->focus_resource) {
610 wl_touch_send_motion(touch->focus_resource, time,
615 static const struct wl_touch_grab_interface default_touch_grab_interface = {
616 default_grab_touch_down,
617 default_grab_touch_up,
618 default_grab_touch_motion
622 default_grab_key(struct wl_keyboard_grab *grab,
623 uint32_t time, uint32_t key, uint32_t state)
625 struct wl_keyboard *keyboard = grab->keyboard;
626 struct wl_resource *resource;
629 resource = keyboard->focus_resource;
631 serial = wl_display_next_serial(resource->client->display);
632 wl_keyboard_send_key(resource, serial, time, key, state);
636 static struct wl_resource *
637 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
639 struct wl_resource *r;
644 wl_list_for_each(r, list, link) {
645 if (r->client == surface->resource.client)
653 default_grab_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
654 uint32_t mods_depressed, uint32_t mods_latched,
655 uint32_t mods_locked, uint32_t group)
657 struct wl_keyboard *keyboard = grab->keyboard;
658 struct wl_pointer *pointer = keyboard->seat->pointer;
659 struct wl_resource *resource, *pr;
661 resource = keyboard->focus_resource;
665 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
666 mods_latched, mods_locked, group);
668 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
669 pr = find_resource_for_surface(&keyboard->resource_list,
672 wl_keyboard_send_modifiers(pr,
674 keyboard->modifiers.mods_depressed,
675 keyboard->modifiers.mods_latched,
676 keyboard->modifiers.mods_locked,
677 keyboard->modifiers.group);
682 static const struct wl_keyboard_grab_interface
683 default_keyboard_grab_interface = {
685 default_grab_modifiers,
689 wl_pointer_init(struct wl_pointer *pointer)
691 memset(pointer, 0, sizeof *pointer);
692 wl_list_init(&pointer->resource_list);
693 pointer->focus_listener.notify = lose_pointer_focus;
694 pointer->default_grab.interface = &default_pointer_grab_interface;
695 pointer->default_grab.pointer = pointer;
696 pointer->grab = &pointer->default_grab;
697 wl_signal_init(&pointer->focus_signal);
699 /* FIXME: Pick better co-ords. */
700 pointer->x = wl_fixed_from_int(100);
701 pointer->y = wl_fixed_from_int(100);
705 wl_pointer_release(struct wl_pointer *pointer)
707 /* XXX: What about pointer->resource_list? */
708 if (pointer->focus_resource)
709 wl_list_remove(&pointer->focus_listener.link);
713 wl_keyboard_init(struct wl_keyboard *keyboard)
715 memset(keyboard, 0, sizeof *keyboard);
716 wl_list_init(&keyboard->resource_list);
717 wl_array_init(&keyboard->keys);
718 keyboard->focus_listener.notify = lose_keyboard_focus;
719 keyboard->default_grab.interface = &default_keyboard_grab_interface;
720 keyboard->default_grab.keyboard = keyboard;
721 keyboard->grab = &keyboard->default_grab;
722 wl_signal_init(&keyboard->focus_signal);
726 wl_keyboard_release(struct wl_keyboard *keyboard)
728 /* XXX: What about keyboard->resource_list? */
729 if (keyboard->focus_resource)
730 wl_list_remove(&keyboard->focus_listener.link);
731 wl_array_release(&keyboard->keys);
735 wl_touch_init(struct wl_touch *touch)
737 memset(touch, 0, sizeof *touch);
738 wl_list_init(&touch->resource_list);
739 touch->focus_listener.notify = lose_touch_focus;
740 touch->default_grab.interface = &default_touch_grab_interface;
741 touch->default_grab.touch = touch;
742 touch->grab = &touch->default_grab;
743 wl_signal_init(&touch->focus_signal);
747 wl_touch_release(struct wl_touch *touch)
749 /* XXX: What about touch->resource_list? */
750 if (touch->focus_resource)
751 wl_list_remove(&touch->focus_listener.link);
755 wl_seat_init(struct wl_seat *seat)
757 memset(seat, 0, sizeof *seat);
759 wl_signal_init(&seat->destroy_signal);
761 seat->selection_data_source = NULL;
762 wl_list_init(&seat->base_resource_list);
763 wl_signal_init(&seat->selection_signal);
764 wl_list_init(&seat->drag_resource_list);
765 wl_signal_init(&seat->drag_icon_signal);
769 wl_seat_release(struct wl_seat *seat)
771 wl_signal_emit(&seat->destroy_signal, seat);
774 wl_pointer_release(seat->pointer);
776 wl_keyboard_release(seat->keyboard);
778 wl_touch_release(seat->touch);
782 seat_send_updated_caps(struct wl_seat *seat)
784 struct wl_resource *r;
785 enum wl_seat_capability caps = 0;
788 caps |= WL_SEAT_CAPABILITY_POINTER;
790 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
792 caps |= WL_SEAT_CAPABILITY_TOUCH;
794 wl_list_for_each(r, &seat->base_resource_list, link)
795 wl_seat_send_capabilities(r, caps);
799 wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer)
801 if (pointer && (seat->pointer || pointer->seat))
802 return; /* XXX: error? */
803 if (!pointer && !seat->pointer)
806 seat->pointer = pointer;
808 pointer->seat = seat;
810 seat_send_updated_caps(seat);
814 wl_seat_set_keyboard(struct wl_seat *seat, struct wl_keyboard *keyboard)
816 if (keyboard && (seat->keyboard || keyboard->seat))
817 return; /* XXX: error? */
818 if (!keyboard && !seat->keyboard)
821 seat->keyboard = keyboard;
823 keyboard->seat = seat;
825 seat_send_updated_caps(seat);
829 wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch)
831 if (touch && (seat->touch || touch->seat))
832 return; /* XXX: error? */
833 if (!touch && !seat->touch)
840 seat_send_updated_caps(seat);
844 wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface,
845 wl_fixed_t sx, wl_fixed_t sy)
847 struct wl_keyboard *kbd = pointer->seat->keyboard;
848 struct wl_resource *resource, *kr;
851 resource = pointer->focus_resource;
852 if (resource && pointer->focus != surface) {
853 serial = wl_display_next_serial(resource->client->display);
854 wl_pointer_send_leave(resource, serial,
855 &pointer->focus->resource);
856 wl_list_remove(&pointer->focus_listener.link);
859 resource = find_resource_for_surface(&pointer->resource_list,
862 (pointer->focus != surface ||
863 pointer->focus_resource != resource)) {
864 serial = wl_display_next_serial(resource->client->display);
866 kr = find_resource_for_surface(&kbd->resource_list,
869 wl_keyboard_send_modifiers(kr,
871 kbd->modifiers.mods_depressed,
872 kbd->modifiers.mods_latched,
873 kbd->modifiers.mods_locked,
874 kbd->modifiers.group);
877 wl_pointer_send_enter(resource, serial, &surface->resource,
879 wl_signal_add(&resource->destroy_signal,
880 &pointer->focus_listener);
881 pointer->focus_serial = serial;
884 pointer->focus_resource = resource;
885 pointer->focus = surface;
886 pointer->default_grab.focus = surface;
887 wl_signal_emit(&pointer->focus_signal, pointer);
891 wl_keyboard_set_focus(struct wl_keyboard *keyboard, struct wl_surface *surface)
893 struct wl_resource *resource;
896 if (keyboard->focus_resource && keyboard->focus != surface) {
897 resource = keyboard->focus_resource;
898 serial = wl_display_next_serial(resource->client->display);
899 wl_keyboard_send_leave(resource, serial,
900 &keyboard->focus->resource);
901 wl_list_remove(&keyboard->focus_listener.link);
904 resource = find_resource_for_surface(&keyboard->resource_list,
907 (keyboard->focus != surface ||
908 keyboard->focus_resource != resource)) {
909 serial = wl_display_next_serial(resource->client->display);
910 wl_keyboard_send_modifiers(resource, serial,
911 keyboard->modifiers.mods_depressed,
912 keyboard->modifiers.mods_latched,
913 keyboard->modifiers.mods_locked,
914 keyboard->modifiers.group);
915 wl_keyboard_send_enter(resource, serial, &surface->resource,
917 wl_signal_add(&resource->destroy_signal,
918 &keyboard->focus_listener);
919 keyboard->focus_serial = serial;
922 keyboard->focus_resource = resource;
923 keyboard->focus = surface;
924 wl_signal_emit(&keyboard->focus_signal, keyboard);
928 wl_keyboard_start_grab(struct wl_keyboard *keyboard,
929 struct wl_keyboard_grab *grab)
931 keyboard->grab = grab;
932 grab->keyboard = keyboard;
938 wl_keyboard_end_grab(struct wl_keyboard *keyboard)
940 keyboard->grab = &keyboard->default_grab;
944 wl_pointer_start_grab(struct wl_pointer *pointer, struct wl_pointer_grab *grab)
946 const struct wl_pointer_grab_interface *interface;
948 pointer->grab = grab;
949 interface = pointer->grab->interface;
950 grab->pointer = pointer;
952 if (pointer->current)
953 interface->focus(pointer->grab, pointer->current,
954 pointer->current_x, pointer->current_y);
958 wl_pointer_end_grab(struct wl_pointer *pointer)
960 const struct wl_pointer_grab_interface *interface;
962 pointer->grab = &pointer->default_grab;
963 interface = pointer->grab->interface;
964 interface->focus(pointer->grab, pointer->current,
965 pointer->current_x, pointer->current_y);
969 wl_touch_start_grab(struct wl_touch *touch, struct wl_touch_grab *grab)
976 wl_touch_end_grab(struct wl_touch *touch)
978 touch->grab = &touch->default_grab;
982 registry_bind(struct wl_client *client,
983 struct wl_resource *resource, uint32_t name,
984 const char *interface, uint32_t version, uint32_t id)
986 struct wl_global *global;
987 struct wl_display *display = resource->data;
989 wl_list_for_each(global, &display->global_list, link)
990 if (global->name == name)
993 if (&global->link == &display->global_list)
994 wl_resource_post_error(resource,
995 WL_DISPLAY_ERROR_INVALID_OBJECT,
996 "invalid global %d", name);
998 global->bind(client, global->data, version, id);
1001 static const struct wl_registry_interface registry_interface = {
1006 display_sync(struct wl_client *client,
1007 struct wl_resource *resource, uint32_t id)
1009 struct wl_resource *callback;
1012 callback = wl_client_add_object(client,
1013 &wl_callback_interface, NULL, id, NULL);
1014 serial = wl_display_get_serial(client->display);
1015 wl_callback_send_done(callback, serial);
1016 wl_resource_destroy(callback);
1020 unbind_resource(struct wl_resource *resource)
1022 wl_list_remove(&resource->link);
1027 display_get_registry(struct wl_client *client,
1028 struct wl_resource *resource, uint32_t id)
1030 struct wl_display *display = resource->data;
1031 struct wl_resource *registry_resource;
1032 struct wl_global *global;
1035 wl_client_add_object(client, &wl_registry_interface,
1036 ®istry_interface, id, display);
1037 registry_resource->destroy = unbind_resource;
1039 wl_list_insert(&display->registry_resource_list,
1040 ®istry_resource->link);
1042 wl_list_for_each(global, &display->global_list, link)
1043 wl_resource_post_event(registry_resource,
1046 global->interface->name,
1047 global->interface->version);
1050 static const struct wl_display_interface display_interface = {
1052 display_get_registry
1056 destroy_client_display_resource(struct wl_resource *resource)
1058 resource->client->display_resource = NULL;
1063 bind_display(struct wl_client *client,
1064 void *data, uint32_t version, uint32_t id)
1066 struct wl_display *display = data;
1068 client->display_resource =
1069 wl_client_add_object(client, &wl_display_interface,
1070 &display_interface, id, display);
1072 if(client->display_resource)
1073 client->display_resource->destroy = destroy_client_display_resource;
1076 WL_EXPORT struct wl_display *
1077 wl_display_create(void)
1079 struct wl_display *display;
1082 debug = getenv("WAYLAND_DEBUG");
1083 if (debug && (strstr(debug, "server") || strstr(debug, "1")))
1086 display = malloc(sizeof *display);
1087 if (display == NULL)
1090 display->loop = wl_event_loop_create();
1091 if (display->loop == NULL) {
1096 wl_list_init(&display->global_list);
1097 wl_list_init(&display->socket_list);
1098 wl_list_init(&display->client_list);
1099 wl_list_init(&display->registry_resource_list);
1101 wl_signal_init(&display->destroy_signal);
1104 display->serial = 0;
1106 if (!wl_display_add_global(display, &wl_display_interface,
1107 display, bind_display)) {
1108 wl_event_loop_destroy(display->loop);
1117 wl_display_destroy(struct wl_display *display)
1119 struct wl_socket *s, *next;
1120 struct wl_global *global, *gnext;
1122 wl_signal_emit(&display->destroy_signal, display);
1124 wl_list_for_each_safe(s, next, &display->socket_list, link) {
1125 wl_event_source_remove(s->source);
1126 unlink(s->addr.sun_path);
1128 unlink(s->lock_addr);
1132 wl_event_loop_destroy(display->loop);
1134 wl_list_for_each_safe(global, gnext, &display->global_list, link)
1140 WL_EXPORT struct wl_global *
1141 wl_display_add_global(struct wl_display *display,
1142 const struct wl_interface *interface,
1143 void *data, wl_global_bind_func_t bind)
1145 struct wl_global *global;
1146 struct wl_resource *resource;
1148 global = malloc(sizeof *global);
1152 global->name = display->id++;
1153 global->interface = interface;
1154 global->data = data;
1155 global->bind = bind;
1156 wl_list_insert(display->global_list.prev, &global->link);
1158 wl_list_for_each(resource, &display->registry_resource_list, link)
1159 wl_resource_post_event(resource,
1162 global->interface->name,
1163 global->interface->version);
1169 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
1171 struct wl_resource *resource;
1173 wl_list_for_each(resource, &display->registry_resource_list, link)
1174 wl_resource_post_event(resource, WL_REGISTRY_GLOBAL_REMOVE,
1176 wl_list_remove(&global->link);
1181 wl_display_get_serial(struct wl_display *display)
1183 return display->serial;
1187 wl_display_next_serial(struct wl_display *display)
1191 return display->serial;
1194 WL_EXPORT struct wl_event_loop *
1195 wl_display_get_event_loop(struct wl_display *display)
1197 return display->loop;
1201 wl_display_terminate(struct wl_display *display)
1207 wl_display_run(struct wl_display *display)
1211 while (display->run) {
1212 wl_display_flush_clients(display);
1213 wl_event_loop_dispatch(display->loop, -1);
1218 wl_display_flush_clients(struct wl_display *display)
1220 struct wl_client *client, *next;
1223 wl_list_for_each_safe(client, next, &display->client_list, link) {
1224 ret = wl_connection_flush(client->connection);
1225 if (ret < 0 && errno == EAGAIN) {
1226 wl_event_source_fd_update(client->source,
1229 } else if (ret < 0) {
1230 wl_client_destroy(client);
1236 socket_data(int fd, uint32_t mask, void *data)
1238 struct wl_display *display = data;
1239 struct sockaddr_un name;
1243 length = sizeof name;
1244 client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name,
1247 wl_log("failed to accept: %m\n");
1249 if (!wl_client_create(display, client_fd))
1256 get_socket_lock(struct wl_socket *socket)
1258 struct stat socket_stat;
1261 snprintf(socket->lock_addr, sizeof socket->lock_addr,
1262 "%s%s", socket->addr.sun_path, LOCK_SUFFIX);
1264 fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
1265 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
1268 wl_log("unable to open lockfile %s check permissions\n",
1273 if (flock(fd_lock, LOCK_EX | LOCK_NB) < 0) {
1274 wl_log("unable to lock lockfile %s, maybe another compositor is running\n",
1280 if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
1281 if (errno != ENOENT) {
1282 wl_log("did not manage to stat file %s\n",
1283 socket->addr.sun_path);
1287 } else if (socket_stat.st_mode & S_IWUSR ||
1288 socket_stat.st_mode & S_IWGRP) {
1289 unlink(socket->addr.sun_path);
1296 wl_display_add_socket(struct wl_display *display, const char *name)
1298 struct wl_socket *s;
1301 const char *runtime_dir;
1303 runtime_dir = getenv("XDG_RUNTIME_DIR");
1305 wl_log("error: XDG_RUNTIME_DIR not set in the environment\n");
1307 /* to prevent programs reporting
1308 * "failed to add socket: Success" */
1313 s = malloc(sizeof *s);
1317 s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
1324 name = getenv("WAYLAND_DISPLAY");
1328 memset(&s->addr, 0, sizeof s->addr);
1329 s->addr.sun_family = AF_LOCAL;
1330 name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
1331 "%s/%s", runtime_dir, name) + 1;
1333 assert(name_size > 0);
1334 if (name_size > (int)sizeof s->addr.sun_path) {
1335 wl_log("error: socket path \"%s/%s\" plus null terminator"
1336 " exceeds 108 bytes\n", runtime_dir, name);
1339 /* to prevent programs reporting
1340 * "failed to add socket: Success" */
1341 errno = ENAMETOOLONG;
1345 wl_log("using socket %s\n", s->addr.sun_path);
1347 s->fd_lock = get_socket_lock(s);
1348 if (s->fd_lock < 0) {
1354 size = offsetof (struct sockaddr_un, sun_path) + name_size;
1355 if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
1356 wl_log("bind() failed with error: %m\n");
1358 unlink(s->lock_addr);
1364 if (listen(s->fd, 1) < 0) {
1365 wl_log("listen() failed with error: %m\n");
1366 unlink(s->addr.sun_path);
1368 unlink(s->lock_addr);
1374 s->source = wl_event_loop_add_fd(display->loop, s->fd,
1376 socket_data, display);
1377 if (s->source == NULL) {
1378 unlink(s->addr.sun_path);
1380 unlink(s->lock_addr);
1385 wl_list_insert(display->socket_list.prev, &s->link);
1391 wl_display_add_destroy_listener(struct wl_display *display,
1392 struct wl_listener *listener)
1394 wl_signal_add(&display->destroy_signal, listener);
1397 WL_EXPORT struct wl_listener *
1398 wl_display_get_destroy_listener(struct wl_display *display,
1399 wl_notify_func_t notify)
1401 return wl_signal_get(&display->destroy_signal, notify);
1404 WL_EXPORT struct wl_resource *
1405 wl_client_add_object(struct wl_client *client,
1406 const struct wl_interface *interface,
1407 const void *implementation,
1408 uint32_t id, void *data)
1410 struct wl_resource *resource;
1412 resource = malloc(sizeof *resource);
1413 if (resource == NULL) {
1414 wl_resource_post_no_memory(client->display_resource);
1418 resource->object.interface = interface;
1419 resource->object.implementation = implementation;
1420 resource->object.id = id;
1421 resource->client = client;
1422 resource->data = data;
1423 resource->destroy = (void *) free;
1424 wl_signal_init(&resource->destroy_signal);
1426 if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
1427 wl_resource_post_error(client->display_resource,
1428 WL_DISPLAY_ERROR_INVALID_OBJECT,
1429 "invalid new id %d",
1430 resource->object.id);
1438 WL_EXPORT struct wl_resource *
1439 wl_client_new_object(struct wl_client *client,
1440 const struct wl_interface *interface,
1441 const void *implementation, void *data)
1445 id = wl_map_insert_new(&client->objects, WL_MAP_SERVER_SIDE, NULL);
1446 return wl_client_add_object(client,
1447 interface, implementation, id, data);
1452 wl_log_set_handler_server(wl_log_func_t handler)
1454 wl_log_handler = handler;