2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
34 #include <sys/socket.h>
44 #include "wayland-private.h"
45 #include "wayland-server.h"
46 #include "wayland-server-protocol.h"
47 #include "wayland-os.h"
52 struct sockaddr_un addr;
55 struct wl_event_source *source;
59 struct wl_connection *connection;
60 struct wl_event_source *source;
61 struct wl_display *display;
62 struct wl_resource *display_resource;
66 struct wl_map objects;
67 struct wl_signal destroy_signal;
73 struct wl_event_loop *loop;
79 struct wl_list global_list;
80 struct wl_list socket_list;
81 struct wl_list client_list;
85 const struct wl_interface *interface;
88 wl_global_bind_func_t bind;
92 static int wl_debug = 0;
95 destroy_client(void *data)
97 struct wl_client *client = data;
99 wl_client_destroy(client);
103 wl_resource_post_event(struct wl_resource *resource, uint32_t opcode, ...)
105 struct wl_closure closure;
106 struct wl_object *object = &resource->object;
110 va_start(ap, opcode);
111 ret = wl_closure_vmarshal(&closure, object, opcode, ap,
112 &object->interface->events[opcode]);
118 if (wl_closure_send(&closure, resource->client->connection))
119 wl_event_loop_add_idle(resource->client->display->loop,
120 destroy_client, resource->client);
123 wl_closure_print(&closure, object, true);
125 wl_closure_destroy(&closure);
130 wl_resource_queue_event(struct wl_resource *resource, uint32_t opcode, ...)
132 struct wl_closure closure;
133 struct wl_object *object = &resource->object;
137 va_start(ap, opcode);
138 ret = wl_closure_vmarshal(&closure, object, opcode, ap,
139 &object->interface->events[opcode]);
145 if (wl_closure_queue(&closure, resource->client->connection))
146 wl_event_loop_add_idle(resource->client->display->loop,
147 destroy_client, resource->client);
150 wl_closure_print(&closure, object, true);
152 wl_closure_destroy(&closure);
156 wl_resource_post_error(struct wl_resource *resource,
157 uint32_t code, const char *msg, ...)
159 struct wl_client *client = resource->client;
164 vsnprintf(buffer, sizeof buffer, msg, ap);
170 * When a client aborts, its resources are destroyed in id order,
171 * which means the display resource is destroyed first. If destruction
172 * of any later resources results in a protocol error, we end up here
173 * with a NULL display_resource. Do not try to send errors to an
174 * already dead client.
176 if (!client->display_resource)
179 wl_resource_post_event(client->display_resource,
180 WL_DISPLAY_ERROR, resource, code, buffer);
184 wl_client_connection_data(int fd, uint32_t mask, void *data)
186 struct wl_client *client = data;
187 struct wl_connection *connection = client->connection;
188 struct wl_resource *resource;
189 struct wl_object *object;
190 struct wl_closure closure;
191 const struct wl_message *message;
197 if (mask & WL_EVENT_READABLE)
198 cmask |= WL_CONNECTION_READABLE;
199 if (mask & WL_EVENT_WRITABLE)
200 cmask |= WL_CONNECTION_WRITABLE;
202 len = wl_connection_data(connection, cmask);
204 wl_client_destroy(client);
208 while ((size_t) len >= sizeof p) {
209 wl_connection_copy(connection, p, sizeof p);
210 opcode = p[1] & 0xffff;
215 resource = wl_map_lookup(&client->objects, p[0]);
216 if (resource == NULL) {
217 wl_resource_post_error(client->display_resource,
218 WL_DISPLAY_ERROR_INVALID_OBJECT,
219 "invalid object %d", p[0]);
223 object = &resource->object;
224 if (opcode >= object->interface->method_count) {
225 wl_resource_post_error(client->display_resource,
226 WL_DISPLAY_ERROR_INVALID_METHOD,
227 "invalid method %d, object %s@%d",
229 object->interface->name,
234 message = &object->interface->methods[opcode];
235 ret = wl_connection_demarshal(client->connection, &closure,
236 size, &client->objects, message);
239 if (ret && errno == EINVAL) {
240 wl_resource_post_error(client->display_resource,
241 WL_DISPLAY_ERROR_INVALID_METHOD,
242 "invalid arguments for %s@%d.%s",
243 object->interface->name,
247 } else if (ret && errno == ENOMEM) {
248 wl_resource_post_no_memory(resource);
253 wl_closure_print(&closure, object, false);
255 wl_closure_invoke(&closure, object,
256 object->implementation[opcode], client);
258 wl_closure_destroy(&closure);
265 wl_client_destroy(client);
271 wl_client_connection_update(struct wl_connection *connection,
272 uint32_t mask, void *data)
274 struct wl_client *client = data;
278 if (mask & WL_CONNECTION_READABLE)
279 emask |= WL_EVENT_READABLE;
280 if (mask & WL_CONNECTION_WRITABLE)
281 emask |= WL_EVENT_WRITABLE;
283 return wl_event_source_fd_update(client->source, emask);
287 wl_client_flush(struct wl_client *client)
289 if (client->mask & WL_CONNECTION_WRITABLE)
290 wl_connection_data(client->connection, WL_CONNECTION_WRITABLE);
293 WL_EXPORT struct wl_display *
294 wl_client_get_display(struct wl_client *client)
296 return client->display;
300 bind_display(struct wl_client *client,
301 void *data, uint32_t version, uint32_t id);
303 WL_EXPORT struct wl_client *
304 wl_client_create(struct wl_display *display, int fd)
306 struct wl_client *client;
309 client = malloc(sizeof *client);
313 memset(client, 0, sizeof *client);
314 client->display = display;
315 client->source = wl_event_loop_add_fd(display->loop, fd,
317 wl_client_connection_data, client);
319 len = sizeof client->ucred;
320 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
321 &client->ucred, &len) < 0) {
327 wl_connection_create(fd, wl_client_connection_update, client);
328 if (client->connection == NULL) {
333 wl_map_init(&client->objects);
335 if (wl_map_insert_at(&client->objects, 0, NULL) < 0) {
336 wl_map_release(&client->objects);
341 wl_signal_init(&client->destroy_signal);
342 bind_display(client, display, 1, 1);
344 wl_list_insert(display->client_list.prev, &client->link);
350 wl_client_get_credentials(struct wl_client *client,
351 pid_t *pid, uid_t *uid, gid_t *gid)
354 *pid = client->ucred.pid;
356 *uid = client->ucred.uid;
358 *gid = client->ucred.gid;
362 wl_client_add_resource(struct wl_client *client,
363 struct wl_resource *resource)
365 if (resource->object.id == 0)
366 resource->object.id =
367 wl_map_insert_new(&client->objects,
368 WL_MAP_SERVER_SIDE, resource);
370 wl_map_insert_at(&client->objects,
371 resource->object.id, resource);
373 resource->client = client;
374 wl_signal_init(&resource->destroy_signal);
377 WL_EXPORT struct wl_resource *
378 wl_client_get_object(struct wl_client *client, uint32_t id)
380 return wl_map_lookup(&client->objects, id);
384 wl_resource_post_no_memory(struct wl_resource *resource)
386 wl_resource_post_error(resource->client->display_resource,
387 WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
391 destroy_resource(void *element, void *data)
393 struct wl_resource *resource = element;
395 wl_signal_emit(&resource->destroy_signal, resource);
397 if (resource->destroy)
398 resource->destroy(resource);
402 wl_resource_destroy(struct wl_resource *resource)
404 struct wl_client *client = resource->client;
407 id = resource->object.id;
408 destroy_resource(resource, NULL);
410 if (id < WL_SERVER_ID_START) {
411 if (client->display_resource) {
412 wl_resource_queue_event(client->display_resource,
413 WL_DISPLAY_DELETE_ID, id);
415 wl_map_insert_at(&client->objects, id, NULL);
417 wl_map_remove(&client->objects, id);
422 wl_client_add_destroy_listener(struct wl_client *client,
423 struct wl_listener *listener)
425 wl_signal_add(&client->destroy_signal, listener);
428 WL_EXPORT struct wl_listener *
429 wl_client_get_destroy_listener(struct wl_client *client,
430 wl_notify_func_t notify)
432 return wl_signal_get(&client->destroy_signal, notify);
436 wl_client_destroy(struct wl_client *client)
440 wl_log("disconnect from client %p\n", client);
442 wl_signal_emit(&client->destroy_signal, client);
444 wl_client_flush(client);
445 wl_map_for_each(&client->objects, destroy_resource, &serial);
446 wl_map_release(&client->objects);
447 wl_event_source_remove(client->source);
448 wl_connection_destroy(client->connection);
449 wl_list_remove(&client->link);
454 lose_pointer_focus(struct wl_listener *listener, void *data)
456 struct wl_pointer *pointer =
457 container_of(listener, struct wl_pointer, focus_listener);
459 pointer->focus_resource = NULL;
463 lose_keyboard_focus(struct wl_listener *listener, void *data)
465 struct wl_keyboard *keyboard =
466 container_of(listener, struct wl_keyboard, focus_listener);
468 keyboard->focus_resource = NULL;
472 default_grab_focus(struct wl_pointer_grab *grab,
473 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
475 struct wl_pointer *pointer = grab->pointer;
477 if (pointer->button_count > 0)
480 wl_pointer_set_focus(pointer, surface, x, y);
484 default_grab_motion(struct wl_pointer_grab *grab,
485 uint32_t time, wl_fixed_t x, wl_fixed_t y)
487 struct wl_resource *resource;
489 resource = grab->pointer->focus_resource;
491 wl_pointer_send_motion(resource, time, x, y);
495 default_grab_button(struct wl_pointer_grab *grab,
496 uint32_t time, uint32_t button, uint32_t state)
498 struct wl_pointer *pointer = grab->pointer;
499 struct wl_resource *resource;
502 resource = pointer->focus_resource;
504 serial = wl_display_next_serial(resource->client->display);
505 wl_pointer_send_button(resource, serial, time, button, state);
508 if (pointer->button_count == 0 && state == 0)
509 wl_pointer_set_focus(pointer, pointer->current,
510 pointer->current_x, pointer->current_y);
513 static const struct wl_pointer_grab_interface
514 default_pointer_grab_interface = {
521 default_grab_key(struct wl_keyboard_grab *grab,
522 uint32_t time, uint32_t key, uint32_t state)
524 struct wl_keyboard *keyboard = grab->keyboard;
525 struct wl_resource *resource;
528 resource = keyboard->focus_resource;
530 serial = wl_display_next_serial(resource->client->display);
531 wl_keyboard_send_key(resource, serial, time, key, state);
535 static const struct wl_keyboard_grab_interface
536 default_keyboard_grab_interface = {
541 wl_pointer_init(struct wl_pointer *pointer)
543 memset(pointer, 0, sizeof *pointer);
544 wl_list_init(&pointer->resource_list);
545 pointer->focus_listener.notify = lose_pointer_focus;
546 pointer->default_grab.interface = &default_pointer_grab_interface;
547 pointer->default_grab.pointer = pointer;
548 pointer->grab = &pointer->default_grab;
550 /* FIXME: Pick better co-ords. */
551 pointer->x = wl_fixed_from_int(100);
552 pointer->y = wl_fixed_from_int(100);
556 wl_pointer_release(struct wl_pointer *pointer)
558 /* XXX: What about pointer->resource_list? */
559 if (pointer->focus_resource)
560 wl_list_remove(&pointer->focus_listener.link);
564 wl_keyboard_init(struct wl_keyboard *keyboard)
566 memset(keyboard, 0, sizeof *keyboard);
567 wl_list_init(&keyboard->resource_list);
568 wl_array_init(&keyboard->keys);
569 keyboard->focus_listener.notify = lose_keyboard_focus;
570 keyboard->default_grab.interface = &default_keyboard_grab_interface;
571 keyboard->default_grab.keyboard = keyboard;
572 keyboard->grab = &keyboard->default_grab;
576 wl_keyboard_release(struct wl_keyboard *keyboard)
578 /* XXX: What about keyboard->resource_list? */
579 if (keyboard->focus_resource)
580 wl_list_remove(&keyboard->focus_listener.link);
581 wl_array_release(&keyboard->keys);
585 wl_touch_init(struct wl_touch *touch)
587 memset(touch, 0, sizeof *touch);
588 wl_list_init(&touch->resource_list);
592 wl_touch_release(struct wl_touch *touch)
594 /* XXX: What about touch->resource_list? */
595 if (touch->focus_resource)
596 wl_list_remove(&touch->focus_listener.link);
600 wl_seat_init(struct wl_seat *seat)
602 memset(seat, 0, sizeof *seat);
604 seat->selection_data_source = NULL;
605 wl_list_init(&seat->base_resource_list);
606 wl_signal_init(&seat->selection_signal);
607 wl_list_init(&seat->drag_resource_list);
608 wl_signal_init(&seat->drag_icon_signal);
612 wl_seat_release(struct wl_seat *seat)
615 wl_pointer_release(seat->pointer);
617 wl_keyboard_release(seat->keyboard);
619 wl_touch_release(seat->touch);
623 seat_send_updated_caps(struct wl_seat *seat)
625 struct wl_resource *r;
626 enum wl_seat_capability caps = 0;
629 caps |= WL_SEAT_CAPABILITY_POINTER;
631 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
633 caps |= WL_SEAT_CAPABILITY_TOUCH;
635 wl_list_for_each(r, &seat->base_resource_list, link)
636 wl_seat_send_capabilities(r, caps);
640 wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer)
642 if (pointer && (seat->pointer || pointer->seat))
643 return; /* XXX: error? */
644 if (!pointer && !seat->pointer)
647 seat->pointer = pointer;
649 pointer->seat = seat;
651 seat_send_updated_caps(seat);
655 wl_seat_set_keyboard(struct wl_seat *seat, struct wl_keyboard *keyboard)
657 if (keyboard && (seat->keyboard || keyboard->seat))
658 return; /* XXX: error? */
659 if (!keyboard && seat->keyboard)
662 seat->keyboard = keyboard;
664 keyboard->seat = seat;
666 seat_send_updated_caps(seat);
670 wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch)
672 if (touch && (seat->touch || touch->seat))
673 return; /* XXX: error? */
674 if (!touch && !seat->touch)
681 seat_send_updated_caps(seat);
684 static struct wl_resource *
685 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
687 struct wl_resource *r;
692 wl_list_for_each(r, list, link) {
693 if (r->client == surface->resource.client)
701 wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface,
702 wl_fixed_t sx, wl_fixed_t sy)
704 struct wl_resource *resource;
707 if (pointer->focus == surface)
710 resource = pointer->focus_resource;
712 serial = wl_display_next_serial(resource->client->display);
713 wl_pointer_send_leave(resource, serial,
714 &pointer->focus->resource);
715 wl_list_remove(&pointer->focus_listener.link);
718 resource = find_resource_for_surface(&pointer->resource_list,
721 serial = wl_display_next_serial(resource->client->display);
722 wl_pointer_send_enter(resource, serial, &surface->resource,
724 wl_signal_add(&resource->destroy_signal,
725 &pointer->focus_listener);
726 pointer->focus_serial = serial;
729 pointer->focus_resource = resource;
730 pointer->focus = surface;
731 pointer->default_grab.focus = surface;
735 wl_keyboard_set_focus(struct wl_keyboard *keyboard, struct wl_surface *surface)
737 struct wl_resource *resource;
740 if (keyboard->focus == surface)
743 if (keyboard->focus_resource) {
744 resource = keyboard->focus_resource;
745 serial = wl_display_next_serial(resource->client->display);
746 wl_keyboard_send_leave(resource, serial,
747 &keyboard->focus->resource);
748 wl_list_remove(&keyboard->focus_listener.link);
751 resource = find_resource_for_surface(&keyboard->resource_list,
754 serial = wl_display_next_serial(resource->client->display);
755 wl_keyboard_send_enter(resource, serial, &surface->resource,
757 wl_signal_add(&resource->destroy_signal,
758 &keyboard->focus_listener);
759 keyboard->focus_serial = serial;
762 keyboard->focus_resource = resource;
763 keyboard->focus = surface;
767 wl_keyboard_start_grab(struct wl_keyboard *keyboard,
768 struct wl_keyboard_grab *grab)
770 keyboard->grab = grab;
771 grab->keyboard = keyboard;
777 wl_keyboard_end_grab(struct wl_keyboard *keyboard)
779 keyboard->grab = &keyboard->default_grab;
783 wl_pointer_start_grab(struct wl_pointer *pointer, struct wl_pointer_grab *grab)
785 const struct wl_pointer_grab_interface *interface;
787 pointer->grab = grab;
788 interface = pointer->grab->interface;
789 grab->pointer = pointer;
791 if (pointer->current)
792 interface->focus(pointer->grab, pointer->current,
793 pointer->current_x, pointer->current_y);
797 wl_pointer_end_grab(struct wl_pointer *pointer)
799 const struct wl_pointer_grab_interface *interface;
801 pointer->grab = &pointer->default_grab;
802 interface = pointer->grab->interface;
803 interface->focus(pointer->grab, pointer->current,
804 pointer->current_x, pointer->current_y);
808 display_bind(struct wl_client *client,
809 struct wl_resource *resource, uint32_t name,
810 const char *interface, uint32_t version, uint32_t id)
812 struct wl_global *global;
813 struct wl_display *display = resource->data;
815 wl_list_for_each(global, &display->global_list, link)
816 if (global->name == name)
819 if (&global->link == &display->global_list)
820 wl_resource_post_error(resource,
821 WL_DISPLAY_ERROR_INVALID_OBJECT,
822 "invalid global %d", name);
824 global->bind(client, global->data, version, id);
828 display_sync(struct wl_client *client,
829 struct wl_resource *resource, uint32_t id)
831 struct wl_resource *callback;
834 callback = wl_client_add_object(client,
835 &wl_callback_interface, NULL, id, NULL);
836 serial = wl_display_get_serial(client->display);
837 wl_callback_send_done(callback, serial);
838 wl_resource_destroy(callback);
841 struct wl_display_interface display_interface = {
847 destroy_client_display_resource(struct wl_resource *resource)
849 resource->client->display_resource = NULL;
854 bind_display(struct wl_client *client,
855 void *data, uint32_t version, uint32_t id)
857 struct wl_display *display = data;
858 struct wl_global *global;
860 client->display_resource =
861 wl_client_add_object(client, &wl_display_interface,
862 &display_interface, id, display);
863 client->display_resource->destroy = destroy_client_display_resource;
865 wl_list_for_each(global, &display->global_list, link)
866 wl_resource_post_event(client->display_resource,
869 global->interface->name,
870 global->interface->version);
873 WL_EXPORT struct wl_display *
874 wl_display_create(void)
876 struct wl_display *display;
879 debug = getenv("WAYLAND_DEBUG");
883 display = malloc(sizeof *display);
887 display->loop = wl_event_loop_create();
888 if (display->loop == NULL) {
893 wl_list_init(&display->global_list);
894 wl_list_init(&display->socket_list);
895 wl_list_init(&display->client_list);
900 if (!wl_display_add_global(display, &wl_display_interface,
901 display, bind_display)) {
902 wl_event_loop_destroy(display->loop);
911 wl_display_destroy(struct wl_display *display)
913 struct wl_socket *s, *next;
914 struct wl_global *global, *gnext;
916 wl_list_for_each_safe(s, next, &display->socket_list, link) {
917 wl_event_source_remove(s->source);
919 unlink(s->addr.sun_path);
921 unlink(s->lock_addr);
924 wl_event_loop_destroy(display->loop);
926 wl_list_for_each_safe(global, gnext, &display->global_list, link)
932 WL_EXPORT struct wl_global *
933 wl_display_add_global(struct wl_display *display,
934 const struct wl_interface *interface,
935 void *data, wl_global_bind_func_t bind)
937 struct wl_global *global;
938 struct wl_client *client;
940 global = malloc(sizeof *global);
944 global->name = display->id++;
945 global->interface = interface;
948 wl_list_insert(display->global_list.prev, &global->link);
950 wl_list_for_each(client, &display->client_list, link)
951 wl_resource_post_event(client->display_resource,
954 global->interface->name,
955 global->interface->version);
961 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
963 struct wl_client *client;
965 wl_list_for_each(client, &display->client_list, link)
966 wl_resource_post_event(client->display_resource,
967 WL_DISPLAY_GLOBAL_REMOVE, global->name);
968 wl_list_remove(&global->link);
973 wl_display_get_serial(struct wl_display *display)
975 return display->serial;
979 wl_display_next_serial(struct wl_display *display)
983 return display->serial;
986 WL_EXPORT struct wl_event_loop *
987 wl_display_get_event_loop(struct wl_display *display)
989 return display->loop;
993 wl_display_terminate(struct wl_display *display)
999 wl_display_run(struct wl_display *display)
1003 while (display->run)
1004 wl_event_loop_dispatch(display->loop, -1);
1008 socket_data(int fd, uint32_t mask, void *data)
1010 struct wl_display *display = data;
1011 struct sockaddr_un name;
1015 length = sizeof name;
1016 client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name,
1019 wl_log("failed to accept, errno: %d\n", errno);
1021 wl_client_create(display, client_fd);
1027 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
1029 struct stat socket_stat;
1030 int lock_size = name_size + 5;
1032 snprintf(socket->lock_addr, lock_size,
1033 "%s.lock", socket->addr.sun_path);
1035 socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
1036 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
1038 if (socket->fd_lock < 0) {
1039 wl_log("unable to open lockfile %s check permissions\n",
1044 if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
1045 wl_log("unable to lock lockfile %s, maybe another compositor is running\n",
1047 close(socket->fd_lock);
1051 if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
1052 if (errno != ENOENT) {
1053 wl_log("did not manage to stat file %s\n",
1054 socket->addr.sun_path);
1055 close(socket->fd_lock);
1058 } else if (socket_stat.st_mode & S_IWUSR ||
1059 socket_stat.st_mode & S_IWGRP) {
1060 unlink(socket->addr.sun_path);
1067 wl_display_add_socket(struct wl_display *display, const char *name)
1069 struct wl_socket *s;
1070 socklen_t size, name_size;
1071 const char *runtime_dir;
1073 s = malloc(sizeof *s);
1077 s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
1083 runtime_dir = getenv("XDG_RUNTIME_DIR");
1084 if (runtime_dir == NULL) {
1086 wl_log("XDG_RUNTIME_DIR not set, falling back to %s\n",
1091 name = getenv("WAYLAND_DISPLAY");
1095 memset(&s->addr, 0, sizeof s->addr);
1096 s->addr.sun_family = AF_LOCAL;
1097 name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
1098 "%s/%s", runtime_dir, name) + 1;
1099 wl_log("using socket %s\n", s->addr.sun_path);
1101 if (get_socket_lock(s,name_size) < 0) {
1107 size = offsetof (struct sockaddr_un, sun_path) + name_size;
1108 if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
1114 if (listen(s->fd, 1) < 0) {
1116 unlink(s->addr.sun_path);
1121 s->source = wl_event_loop_add_fd(display->loop, s->fd,
1123 socket_data, display);
1124 if (s->source == NULL) {
1126 unlink(s->addr.sun_path);
1130 wl_list_insert(display->socket_list.prev, &s->link);
1135 WL_EXPORT struct wl_resource *
1136 wl_client_add_object(struct wl_client *client,
1137 const struct wl_interface *interface,
1138 const void *implementation,
1139 uint32_t id, void *data)
1141 struct wl_resource *resource;
1143 resource = malloc(sizeof *resource);
1144 if (resource == NULL) {
1145 wl_resource_post_no_memory(client->display_resource);
1149 resource->object.interface = interface;
1150 resource->object.implementation = implementation;
1151 resource->object.id = id;
1152 resource->client = client;
1153 resource->data = data;
1154 resource->destroy = (void *) free;
1155 wl_signal_init(&resource->destroy_signal);
1157 if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
1158 wl_resource_post_no_memory(client->display_resource);
1166 WL_EXPORT struct wl_resource *
1167 wl_client_new_object(struct wl_client *client,
1168 const struct wl_interface *interface,
1169 const void *implementation, void *data)
1173 id = wl_map_insert_new(&client->objects, WL_MAP_SERVER_SIDE, NULL);
1174 return wl_client_add_object(client,
1175 interface, implementation, id, data);
1180 wl_log_set_handler_server(wl_log_func_t handler)
1182 wl_log_handler = handler;