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;
96 const struct wl_interface *interface;
99 wl_global_bind_func_t bind;
103 static int wl_debug = 0;
106 destroy_client(void *data)
108 struct wl_client *client = data;
110 wl_client_destroy(client);
114 wl_resource_post_event(struct wl_resource *resource, uint32_t opcode, ...)
116 struct wl_closure *closure;
117 struct wl_object *object = &resource->object;
120 va_start(ap, opcode);
121 closure = wl_closure_vmarshal(object, opcode, ap,
122 &object->interface->events[opcode]);
128 if (wl_closure_send(closure, resource->client->connection))
129 wl_event_loop_add_idle(resource->client->display->loop,
130 destroy_client, resource->client);
133 wl_closure_print(closure, object, true);
135 wl_closure_destroy(closure);
140 wl_resource_queue_event(struct wl_resource *resource, uint32_t opcode, ...)
142 struct wl_closure *closure;
143 struct wl_object *object = &resource->object;
146 va_start(ap, opcode);
147 closure = wl_closure_vmarshal(object, opcode, ap,
148 &object->interface->events[opcode]);
154 if (wl_closure_queue(closure, resource->client->connection))
155 wl_event_loop_add_idle(resource->client->display->loop,
156 destroy_client, resource->client);
159 wl_closure_print(closure, object, true);
161 wl_closure_destroy(closure);
165 wl_resource_post_error(struct wl_resource *resource,
166 uint32_t code, const char *msg, ...)
168 struct wl_client *client = resource->client;
173 vsnprintf(buffer, sizeof buffer, msg, ap);
179 * When a client aborts, its resources are destroyed in id order,
180 * which means the display resource is destroyed first. If destruction
181 * of any later resources results in a protocol error, we end up here
182 * with a NULL display_resource. Do not try to send errors to an
183 * already dead client.
185 if (!client->display_resource)
188 wl_resource_post_event(client->display_resource,
189 WL_DISPLAY_ERROR, resource, code, buffer);
193 deref_new_objects(struct wl_closure *closure)
195 const char *signature;
198 signature = closure->message->signature;
199 for (i = 0; signature[i]; i++) {
200 switch (signature[i]) {
202 closure->args[i + 2] = *(uint32_t **) closure->args[i + 2];
203 closure->types[i] = &ffi_type_uint32;
210 wl_client_connection_data(int fd, uint32_t mask, void *data)
212 struct wl_client *client = data;
213 struct wl_connection *connection = client->connection;
214 struct wl_resource *resource;
215 struct wl_object *object;
216 struct wl_closure *closure;
217 const struct wl_message *message;
222 if (mask & (WL_EVENT_ERROR | WL_EVENT_HANGUP)) {
223 wl_client_destroy(client);
227 if (mask & WL_EVENT_WRITABLE) {
228 len = wl_connection_flush(connection);
229 if (len < 0 && errno != EAGAIN) {
230 wl_client_destroy(client);
232 } else if (len >= 0) {
233 wl_event_source_fd_update(client->source,
239 if (mask & WL_EVENT_READABLE) {
240 len = wl_connection_read(connection);
241 if (len < 0 && errno != EAGAIN) {
242 wl_client_destroy(client);
247 while ((size_t) len >= sizeof p) {
248 wl_connection_copy(connection, p, sizeof p);
249 opcode = p[1] & 0xffff;
254 resource = wl_map_lookup(&client->objects, p[0]);
255 if (resource == NULL) {
256 wl_resource_post_error(client->display_resource,
257 WL_DISPLAY_ERROR_INVALID_OBJECT,
258 "invalid object %u", p[0]);
262 object = &resource->object;
263 if (opcode >= object->interface->method_count) {
264 wl_resource_post_error(client->display_resource,
265 WL_DISPLAY_ERROR_INVALID_METHOD,
266 "invalid method %d, object %s@%u",
268 object->interface->name,
273 message = &object->interface->methods[opcode];
274 closure = wl_connection_demarshal(client->connection, size,
275 &client->objects, message);
278 if ((closure == NULL && errno == EINVAL) ||
279 wl_closure_lookup_objects(closure, &client->objects) < 0) {
280 wl_resource_post_error(client->display_resource,
281 WL_DISPLAY_ERROR_INVALID_METHOD,
282 "invalid arguments for %s@%u.%s",
283 object->interface->name,
287 } else if (closure == NULL && errno == ENOMEM) {
288 wl_resource_post_no_memory(resource);
293 wl_closure_print(closure, object, false);
295 deref_new_objects(closure);
297 wl_closure_invoke(closure, object,
298 object->implementation[opcode], client);
300 wl_closure_destroy(closure);
307 wl_client_destroy(client);
313 wl_client_flush(struct wl_client *client)
315 wl_connection_flush(client->connection);
318 WL_EXPORT struct wl_display *
319 wl_client_get_display(struct wl_client *client)
321 return client->display;
325 bind_display(struct wl_client *client,
326 void *data, uint32_t version, uint32_t id);
328 WL_EXPORT struct wl_client *
329 wl_client_create(struct wl_display *display, int fd)
331 struct wl_client *client;
334 client = malloc(sizeof *client);
338 memset(client, 0, sizeof *client);
339 client->display = display;
340 client->source = wl_event_loop_add_fd(display->loop, fd,
342 wl_client_connection_data, client);
344 len = sizeof client->ucred;
345 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
346 &client->ucred, &len) < 0) {
351 client->connection = wl_connection_create(fd);
352 if (client->connection == NULL) {
357 wl_map_init(&client->objects);
359 if (wl_map_insert_at(&client->objects, 0, NULL) < 0) {
360 wl_map_release(&client->objects);
365 wl_signal_init(&client->destroy_signal);
366 bind_display(client, display, 1, 1);
368 wl_list_insert(display->client_list.prev, &client->link);
374 wl_client_get_credentials(struct wl_client *client,
375 pid_t *pid, uid_t *uid, gid_t *gid)
378 *pid = client->ucred.pid;
380 *uid = client->ucred.uid;
382 *gid = client->ucred.gid;
386 wl_client_add_resource(struct wl_client *client,
387 struct wl_resource *resource)
389 if (resource->object.id == 0) {
390 resource->object.id =
391 wl_map_insert_new(&client->objects,
392 WL_MAP_SERVER_SIDE, resource);
393 } else if (wl_map_insert_at(&client->objects,
394 resource->object.id, resource) < 0) {
395 wl_resource_post_error(client->display_resource,
396 WL_DISPLAY_ERROR_INVALID_OBJECT,
398 resource->object.id);
402 resource->client = client;
403 wl_signal_init(&resource->destroy_signal);
405 return resource->object.id;
408 WL_EXPORT struct wl_resource *
409 wl_client_get_object(struct wl_client *client, uint32_t id)
411 return wl_map_lookup(&client->objects, id);
415 wl_resource_post_no_memory(struct wl_resource *resource)
417 wl_resource_post_error(resource->client->display_resource,
418 WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
422 destroy_resource(void *element, void *data)
424 struct wl_resource *resource = element;
426 wl_signal_emit(&resource->destroy_signal, resource);
428 if (resource->destroy)
429 resource->destroy(resource);
433 wl_resource_destroy(struct wl_resource *resource)
435 struct wl_client *client = resource->client;
438 id = resource->object.id;
439 destroy_resource(resource, NULL);
441 if (id < WL_SERVER_ID_START) {
442 if (client->display_resource) {
443 wl_resource_queue_event(client->display_resource,
444 WL_DISPLAY_DELETE_ID, id);
446 wl_map_insert_at(&client->objects, id, NULL);
448 wl_map_remove(&client->objects, id);
453 wl_client_add_destroy_listener(struct wl_client *client,
454 struct wl_listener *listener)
456 wl_signal_add(&client->destroy_signal, listener);
459 WL_EXPORT struct wl_listener *
460 wl_client_get_destroy_listener(struct wl_client *client,
461 wl_notify_func_t notify)
463 return wl_signal_get(&client->destroy_signal, notify);
467 wl_client_destroy(struct wl_client *client)
471 wl_log("disconnect from client %p\n", client);
473 wl_signal_emit(&client->destroy_signal, client);
475 wl_client_flush(client);
476 wl_map_for_each(&client->objects, destroy_resource, &serial);
477 wl_map_release(&client->objects);
478 wl_event_source_remove(client->source);
479 wl_connection_destroy(client->connection);
480 wl_list_remove(&client->link);
485 lose_pointer_focus(struct wl_listener *listener, void *data)
487 struct wl_pointer *pointer =
488 container_of(listener, struct wl_pointer, focus_listener);
490 pointer->focus_resource = NULL;
494 lose_keyboard_focus(struct wl_listener *listener, void *data)
496 struct wl_keyboard *keyboard =
497 container_of(listener, struct wl_keyboard, focus_listener);
499 keyboard->focus_resource = NULL;
503 lose_touch_focus(struct wl_listener *listener, void *data)
505 struct wl_touch *touch =
506 container_of(listener, struct wl_touch, focus_listener);
508 touch->focus_resource = NULL;
512 default_grab_focus(struct wl_pointer_grab *grab,
513 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
515 struct wl_pointer *pointer = grab->pointer;
517 if (pointer->button_count > 0)
520 wl_pointer_set_focus(pointer, surface, x, y);
524 default_grab_motion(struct wl_pointer_grab *grab,
525 uint32_t time, wl_fixed_t x, wl_fixed_t y)
527 struct wl_resource *resource;
529 resource = grab->pointer->focus_resource;
531 wl_pointer_send_motion(resource, time, x, y);
535 default_grab_button(struct wl_pointer_grab *grab,
536 uint32_t time, uint32_t button, uint32_t state_w)
538 struct wl_pointer *pointer = grab->pointer;
539 struct wl_resource *resource;
541 enum wl_pointer_button_state state = state_w;
543 resource = pointer->focus_resource;
545 serial = wl_display_next_serial(resource->client->display);
546 wl_pointer_send_button(resource, serial, time, button, state_w);
549 if (pointer->button_count == 0 &&
550 state == WL_POINTER_BUTTON_STATE_RELEASED)
551 wl_pointer_set_focus(pointer, pointer->current,
552 pointer->current_x, pointer->current_y);
555 static const struct wl_pointer_grab_interface
556 default_pointer_grab_interface = {
563 default_grab_key(struct wl_keyboard_grab *grab,
564 uint32_t time, uint32_t key, uint32_t state)
566 struct wl_keyboard *keyboard = grab->keyboard;
567 struct wl_resource *resource;
570 resource = keyboard->focus_resource;
572 serial = wl_display_next_serial(resource->client->display);
573 wl_keyboard_send_key(resource, serial, time, key, state);
577 static struct wl_resource *
578 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
580 struct wl_resource *r;
585 wl_list_for_each(r, list, link) {
586 if (r->client == surface->resource.client)
594 default_grab_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
595 uint32_t mods_depressed, uint32_t mods_latched,
596 uint32_t mods_locked, uint32_t group)
598 struct wl_keyboard *keyboard = grab->keyboard;
599 struct wl_pointer *pointer = keyboard->seat->pointer;
600 struct wl_resource *resource, *pr;
602 resource = keyboard->focus_resource;
606 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
607 mods_latched, mods_locked, group);
609 if (pointer && pointer->focus && pointer->focus != keyboard->focus) {
610 pr = find_resource_for_surface(&keyboard->resource_list,
613 wl_keyboard_send_modifiers(pr,
615 keyboard->modifiers.mods_depressed,
616 keyboard->modifiers.mods_latched,
617 keyboard->modifiers.mods_locked,
618 keyboard->modifiers.group);
623 static const struct wl_keyboard_grab_interface
624 default_keyboard_grab_interface = {
626 default_grab_modifiers,
630 wl_pointer_init(struct wl_pointer *pointer)
632 memset(pointer, 0, sizeof *pointer);
633 wl_list_init(&pointer->resource_list);
634 pointer->focus_listener.notify = lose_pointer_focus;
635 pointer->default_grab.interface = &default_pointer_grab_interface;
636 pointer->default_grab.pointer = pointer;
637 pointer->grab = &pointer->default_grab;
638 wl_signal_init(&pointer->focus_signal);
640 /* FIXME: Pick better co-ords. */
641 pointer->x = wl_fixed_from_int(100);
642 pointer->y = wl_fixed_from_int(100);
646 wl_pointer_release(struct wl_pointer *pointer)
648 /* XXX: What about pointer->resource_list? */
649 if (pointer->focus_resource)
650 wl_list_remove(&pointer->focus_listener.link);
654 wl_keyboard_init(struct wl_keyboard *keyboard)
656 memset(keyboard, 0, sizeof *keyboard);
657 wl_list_init(&keyboard->resource_list);
658 wl_array_init(&keyboard->keys);
659 keyboard->focus_listener.notify = lose_keyboard_focus;
660 keyboard->default_grab.interface = &default_keyboard_grab_interface;
661 keyboard->default_grab.keyboard = keyboard;
662 keyboard->grab = &keyboard->default_grab;
663 wl_signal_init(&keyboard->focus_signal);
667 wl_keyboard_release(struct wl_keyboard *keyboard)
669 /* XXX: What about keyboard->resource_list? */
670 if (keyboard->focus_resource)
671 wl_list_remove(&keyboard->focus_listener.link);
672 wl_array_release(&keyboard->keys);
676 wl_touch_init(struct wl_touch *touch)
678 memset(touch, 0, sizeof *touch);
679 wl_list_init(&touch->resource_list);
680 touch->focus_listener.notify = lose_touch_focus;
684 wl_touch_release(struct wl_touch *touch)
686 /* XXX: What about touch->resource_list? */
687 if (touch->focus_resource)
688 wl_list_remove(&touch->focus_listener.link);
692 wl_seat_init(struct wl_seat *seat)
694 memset(seat, 0, sizeof *seat);
696 wl_signal_init(&seat->destroy_signal);
698 seat->selection_data_source = NULL;
699 wl_list_init(&seat->base_resource_list);
700 wl_signal_init(&seat->selection_signal);
701 wl_list_init(&seat->drag_resource_list);
702 wl_signal_init(&seat->drag_icon_signal);
706 wl_seat_release(struct wl_seat *seat)
708 wl_signal_emit(&seat->destroy_signal, seat);
711 wl_pointer_release(seat->pointer);
713 wl_keyboard_release(seat->keyboard);
715 wl_touch_release(seat->touch);
719 seat_send_updated_caps(struct wl_seat *seat)
721 struct wl_resource *r;
722 enum wl_seat_capability caps = 0;
725 caps |= WL_SEAT_CAPABILITY_POINTER;
727 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
729 caps |= WL_SEAT_CAPABILITY_TOUCH;
731 wl_list_for_each(r, &seat->base_resource_list, link)
732 wl_seat_send_capabilities(r, caps);
736 wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer)
738 if (pointer && (seat->pointer || pointer->seat))
739 return; /* XXX: error? */
740 if (!pointer && !seat->pointer)
743 seat->pointer = pointer;
745 pointer->seat = seat;
747 seat_send_updated_caps(seat);
751 wl_seat_set_keyboard(struct wl_seat *seat, struct wl_keyboard *keyboard)
753 if (keyboard && (seat->keyboard || keyboard->seat))
754 return; /* XXX: error? */
755 if (!keyboard && !seat->keyboard)
758 seat->keyboard = keyboard;
760 keyboard->seat = seat;
762 seat_send_updated_caps(seat);
766 wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch)
768 if (touch && (seat->touch || touch->seat))
769 return; /* XXX: error? */
770 if (!touch && !seat->touch)
777 seat_send_updated_caps(seat);
781 wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface,
782 wl_fixed_t sx, wl_fixed_t sy)
784 struct wl_keyboard *kbd = pointer->seat->keyboard;
785 struct wl_resource *resource, *kr;
788 resource = pointer->focus_resource;
789 if (resource && pointer->focus != surface) {
790 serial = wl_display_next_serial(resource->client->display);
791 wl_pointer_send_leave(resource, serial,
792 &pointer->focus->resource);
793 wl_list_remove(&pointer->focus_listener.link);
796 resource = find_resource_for_surface(&pointer->resource_list,
799 (pointer->focus != surface ||
800 pointer->focus_resource != resource)) {
801 serial = wl_display_next_serial(resource->client->display);
803 kr = find_resource_for_surface(&kbd->resource_list,
806 wl_keyboard_send_modifiers(kr,
808 kbd->modifiers.mods_depressed,
809 kbd->modifiers.mods_latched,
810 kbd->modifiers.mods_locked,
811 kbd->modifiers.group);
814 wl_pointer_send_enter(resource, serial, &surface->resource,
816 wl_signal_add(&resource->destroy_signal,
817 &pointer->focus_listener);
818 pointer->focus_serial = serial;
821 pointer->focus_resource = resource;
822 pointer->focus = surface;
823 pointer->default_grab.focus = surface;
824 wl_signal_emit(&pointer->focus_signal, pointer);
828 wl_keyboard_set_focus(struct wl_keyboard *keyboard, struct wl_surface *surface)
830 struct wl_resource *resource;
833 if (keyboard->focus_resource && keyboard->focus != surface) {
834 resource = keyboard->focus_resource;
835 serial = wl_display_next_serial(resource->client->display);
836 wl_keyboard_send_leave(resource, serial,
837 &keyboard->focus->resource);
838 wl_list_remove(&keyboard->focus_listener.link);
841 resource = find_resource_for_surface(&keyboard->resource_list,
844 (keyboard->focus != surface ||
845 keyboard->focus_resource != resource)) {
846 serial = wl_display_next_serial(resource->client->display);
847 wl_keyboard_send_modifiers(resource, serial,
848 keyboard->modifiers.mods_depressed,
849 keyboard->modifiers.mods_latched,
850 keyboard->modifiers.mods_locked,
851 keyboard->modifiers.group);
852 wl_keyboard_send_enter(resource, serial, &surface->resource,
854 wl_signal_add(&resource->destroy_signal,
855 &keyboard->focus_listener);
856 keyboard->focus_serial = serial;
859 keyboard->focus_resource = resource;
860 keyboard->focus = surface;
861 wl_signal_emit(&keyboard->focus_signal, keyboard);
865 wl_keyboard_start_grab(struct wl_keyboard *keyboard,
866 struct wl_keyboard_grab *grab)
868 keyboard->grab = grab;
869 grab->keyboard = keyboard;
875 wl_keyboard_end_grab(struct wl_keyboard *keyboard)
877 keyboard->grab = &keyboard->default_grab;
881 wl_pointer_start_grab(struct wl_pointer *pointer, struct wl_pointer_grab *grab)
883 const struct wl_pointer_grab_interface *interface;
885 pointer->grab = grab;
886 interface = pointer->grab->interface;
887 grab->pointer = pointer;
889 if (pointer->current)
890 interface->focus(pointer->grab, pointer->current,
891 pointer->current_x, pointer->current_y);
895 wl_pointer_end_grab(struct wl_pointer *pointer)
897 const struct wl_pointer_grab_interface *interface;
899 pointer->grab = &pointer->default_grab;
900 interface = pointer->grab->interface;
901 interface->focus(pointer->grab, pointer->current,
902 pointer->current_x, pointer->current_y);
906 registry_bind(struct wl_client *client,
907 struct wl_resource *resource, uint32_t name,
908 const char *interface, uint32_t version, uint32_t id)
910 struct wl_global *global;
911 struct wl_display *display = resource->data;
913 wl_list_for_each(global, &display->global_list, link)
914 if (global->name == name)
917 if (&global->link == &display->global_list)
918 wl_resource_post_error(resource,
919 WL_DISPLAY_ERROR_INVALID_OBJECT,
920 "invalid global %d", name);
922 global->bind(client, global->data, version, id);
925 static const struct wl_registry_interface registry_interface = {
930 display_sync(struct wl_client *client,
931 struct wl_resource *resource, uint32_t id)
933 struct wl_resource *callback;
936 callback = wl_client_add_object(client,
937 &wl_callback_interface, NULL, id, NULL);
938 serial = wl_display_get_serial(client->display);
939 wl_callback_send_done(callback, serial);
940 wl_resource_destroy(callback);
944 unbind_resource(struct wl_resource *resource)
946 wl_list_remove(&resource->link);
951 display_get_registry(struct wl_client *client,
952 struct wl_resource *resource, uint32_t id)
954 struct wl_display *display = resource->data;
955 struct wl_resource *registry_resource;
956 struct wl_global *global;
959 wl_client_add_object(client, &wl_registry_interface,
960 ®istry_interface, id, display);
961 registry_resource->destroy = unbind_resource;
963 wl_list_insert(&display->registry_resource_list,
964 ®istry_resource->link);
966 wl_list_for_each(global, &display->global_list, link)
967 wl_resource_post_event(registry_resource,
970 global->interface->name,
971 global->interface->version);
974 static const struct wl_display_interface display_interface = {
980 destroy_client_display_resource(struct wl_resource *resource)
982 resource->client->display_resource = NULL;
987 bind_display(struct wl_client *client,
988 void *data, uint32_t version, uint32_t id)
990 struct wl_display *display = data;
992 client->display_resource =
993 wl_client_add_object(client, &wl_display_interface,
994 &display_interface, id, display);
995 client->display_resource->destroy = destroy_client_display_resource;
998 WL_EXPORT struct wl_display *
999 wl_display_create(void)
1001 struct wl_display *display;
1004 debug = getenv("WAYLAND_DEBUG");
1008 display = malloc(sizeof *display);
1009 if (display == NULL)
1012 display->loop = wl_event_loop_create();
1013 if (display->loop == NULL) {
1018 wl_list_init(&display->global_list);
1019 wl_list_init(&display->socket_list);
1020 wl_list_init(&display->client_list);
1021 wl_list_init(&display->registry_resource_list);
1024 display->serial = 0;
1026 if (!wl_display_add_global(display, &wl_display_interface,
1027 display, bind_display)) {
1028 wl_event_loop_destroy(display->loop);
1037 wl_display_destroy(struct wl_display *display)
1039 struct wl_socket *s, *next;
1040 struct wl_global *global, *gnext;
1042 wl_list_for_each_safe(s, next, &display->socket_list, link) {
1043 wl_event_source_remove(s->source);
1044 unlink(s->addr.sun_path);
1046 unlink(s->lock_addr);
1050 wl_event_loop_destroy(display->loop);
1052 wl_list_for_each_safe(global, gnext, &display->global_list, link)
1058 WL_EXPORT struct wl_global *
1059 wl_display_add_global(struct wl_display *display,
1060 const struct wl_interface *interface,
1061 void *data, wl_global_bind_func_t bind)
1063 struct wl_global *global;
1064 struct wl_resource *resource;
1066 global = malloc(sizeof *global);
1070 global->name = display->id++;
1071 global->interface = interface;
1072 global->data = data;
1073 global->bind = bind;
1074 wl_list_insert(display->global_list.prev, &global->link);
1076 wl_list_for_each(resource, &display->registry_resource_list, link)
1077 wl_resource_post_event(resource,
1080 global->interface->name,
1081 global->interface->version);
1087 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
1089 struct wl_resource *resource;
1091 wl_list_for_each(resource, &display->registry_resource_list, link)
1092 wl_resource_post_event(resource, WL_REGISTRY_GLOBAL_REMOVE,
1094 wl_list_remove(&global->link);
1099 wl_display_get_serial(struct wl_display *display)
1101 return display->serial;
1105 wl_display_next_serial(struct wl_display *display)
1109 return display->serial;
1112 WL_EXPORT struct wl_event_loop *
1113 wl_display_get_event_loop(struct wl_display *display)
1115 return display->loop;
1119 wl_display_terminate(struct wl_display *display)
1125 wl_display_run(struct wl_display *display)
1129 while (display->run) {
1130 wl_display_flush_clients(display);
1131 wl_event_loop_dispatch(display->loop, -1);
1136 wl_display_flush_clients(struct wl_display *display)
1138 struct wl_client *client, *next;
1141 wl_list_for_each_safe(client, next, &display->client_list, link) {
1142 ret = wl_connection_flush(client->connection);
1143 if (ret < 0 && errno == EAGAIN) {
1144 wl_event_source_fd_update(client->source,
1147 } else if (ret < 0) {
1148 wl_client_destroy(client);
1154 socket_data(int fd, uint32_t mask, void *data)
1156 struct wl_display *display = data;
1157 struct sockaddr_un name;
1161 length = sizeof name;
1162 client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name,
1165 wl_log("failed to accept: %m\n");
1167 wl_client_create(display, client_fd);
1173 get_socket_lock(struct wl_socket *socket)
1175 struct stat socket_stat;
1178 snprintf(socket->lock_addr, sizeof socket->lock_addr,
1179 "%s%s", socket->addr.sun_path, LOCK_SUFFIX);
1181 fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
1182 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
1185 wl_log("unable to open lockfile %s check permissions\n",
1190 if (flock(fd_lock, LOCK_EX | LOCK_NB) < 0) {
1191 wl_log("unable to lock lockfile %s, maybe another compositor is running\n",
1197 if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
1198 if (errno != ENOENT) {
1199 wl_log("did not manage to stat file %s\n",
1200 socket->addr.sun_path);
1204 } else if (socket_stat.st_mode & S_IWUSR ||
1205 socket_stat.st_mode & S_IWGRP) {
1206 unlink(socket->addr.sun_path);
1213 wl_display_add_socket(struct wl_display *display, const char *name)
1215 struct wl_socket *s;
1218 const char *runtime_dir;
1220 runtime_dir = getenv("XDG_RUNTIME_DIR");
1222 wl_log("error: XDG_RUNTIME_DIR not set in the environment\n");
1224 /* to prevent programs reporting
1225 * "failed to add socket: Success" */
1230 s = malloc(sizeof *s);
1234 s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
1241 name = getenv("WAYLAND_DISPLAY");
1245 memset(&s->addr, 0, sizeof s->addr);
1246 s->addr.sun_family = AF_LOCAL;
1247 name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
1248 "%s/%s", runtime_dir, name) + 1;
1250 assert(name_size > 0);
1251 if (name_size > (int)sizeof s->addr.sun_path) {
1252 wl_log("error: socket path \"%s/%s\" plus null terminator"
1253 " exceeds 108 bytes\n", runtime_dir, name);
1256 /* to prevent programs reporting
1257 * "failed to add socket: Success" */
1258 errno = ENAMETOOLONG;
1262 wl_log("using socket %s\n", s->addr.sun_path);
1264 s->fd_lock = get_socket_lock(s);
1265 if (s->fd_lock < 0) {
1271 size = offsetof (struct sockaddr_un, sun_path) + name_size;
1272 if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
1273 wl_log("bind() failed with error: %m\n");
1275 unlink(s->lock_addr);
1281 if (listen(s->fd, 1) < 0) {
1282 wl_log("listen() failed with error: %m\n");
1283 unlink(s->addr.sun_path);
1285 unlink(s->lock_addr);
1291 s->source = wl_event_loop_add_fd(display->loop, s->fd,
1293 socket_data, display);
1294 if (s->source == NULL) {
1295 unlink(s->addr.sun_path);
1297 unlink(s->lock_addr);
1302 wl_list_insert(display->socket_list.prev, &s->link);
1307 WL_EXPORT struct wl_resource *
1308 wl_client_add_object(struct wl_client *client,
1309 const struct wl_interface *interface,
1310 const void *implementation,
1311 uint32_t id, void *data)
1313 struct wl_resource *resource;
1315 resource = malloc(sizeof *resource);
1316 if (resource == NULL) {
1317 wl_resource_post_no_memory(client->display_resource);
1321 resource->object.interface = interface;
1322 resource->object.implementation = implementation;
1323 resource->object.id = id;
1324 resource->client = client;
1325 resource->data = data;
1326 resource->destroy = (void *) free;
1327 wl_signal_init(&resource->destroy_signal);
1329 if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
1330 wl_resource_post_error(client->display_resource,
1331 WL_DISPLAY_ERROR_INVALID_OBJECT,
1332 "invalid new id %d",
1333 resource->object.id);
1341 WL_EXPORT struct wl_resource *
1342 wl_client_new_object(struct wl_client *client,
1343 const struct wl_interface *interface,
1344 const void *implementation, void *data)
1348 id = wl_map_insert_new(&client->objects, WL_MAP_SERVER_SIDE, NULL);
1349 return wl_client_add_object(client,
1350 interface, implementation, id, data);
1355 wl_log_set_handler_server(wl_log_func_t handler)
1357 wl_log_handler = handler;