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
33 #include <sys/socket.h>
43 #include "wayland-server.h"
44 #include "wayland-server-protocol.h"
45 #include "connection.h"
50 struct sockaddr_un addr;
56 struct wl_connection *connection;
57 struct wl_event_source *source;
58 struct wl_display *display;
59 struct wl_list resource_list;
64 struct wl_object object;
65 struct wl_event_loop *loop;
66 struct wl_hash_table *objects;
69 struct wl_list frame_list;
70 uint32_t client_id_range;
73 struct wl_list global_list;
74 struct wl_list socket_list;
77 struct wl_frame_listener {
78 struct wl_resource resource;
79 struct wl_client *client;
81 struct wl_surface *surface;
86 struct wl_object *object;
87 wl_client_connect_func_t func;
91 static int wl_debug = 0;
94 wl_client_post_event(struct wl_client *client, struct wl_object *sender,
97 struct wl_closure *closure;
100 va_start(ap, opcode);
101 closure = wl_connection_vmarshal(client->connection,
103 &sender->interface->events[opcode]);
106 wl_closure_send(closure, client->connection);
109 fprintf(stderr, " -> ");
110 wl_closure_print(closure, sender);
113 wl_closure_destroy(closure);
117 wl_client_connection_data(int fd, uint32_t mask, void *data)
119 struct wl_client *client = data;
120 struct wl_connection *connection = client->connection;
121 struct wl_object *object;
122 struct wl_closure *closure;
123 const struct wl_message *message;
124 uint32_t p[2], opcode, size;
128 if (mask & WL_EVENT_READABLE)
129 cmask |= WL_CONNECTION_READABLE;
130 if (mask & WL_EVENT_WRITEABLE)
131 cmask |= WL_CONNECTION_WRITABLE;
133 len = wl_connection_data(connection, cmask);
135 wl_client_destroy(client);
139 while (len >= sizeof p) {
140 wl_connection_copy(connection, p, sizeof p);
141 opcode = p[1] & 0xffff;
146 object = wl_hash_table_lookup(client->display->objects, p[0]);
147 if (object == NULL) {
148 wl_client_post_event(client, &client->display->object,
149 WL_DISPLAY_INVALID_OBJECT, p[0]);
150 wl_connection_consume(connection, size);
155 if (opcode >= object->interface->method_count) {
156 wl_client_post_event(client, &client->display->object,
157 WL_DISPLAY_INVALID_METHOD, p[0], opcode);
158 wl_connection_consume(connection, size);
163 message = &object->interface->methods[opcode];
164 closure = wl_connection_demarshal(client->connection, size,
165 client->display->objects,
169 if (closure == NULL && errno == EINVAL) {
170 wl_client_post_event(client, &client->display->object,
171 WL_DISPLAY_INVALID_METHOD,
174 } else if (closure == NULL && errno == ENOMEM) {
175 wl_client_post_no_memory(client);
181 wl_closure_print(closure, object);
183 wl_closure_invoke(closure, object,
184 object->implementation[opcode], client);
186 wl_closure_destroy(closure);
191 wl_client_connection_update(struct wl_connection *connection,
192 uint32_t mask, void *data)
194 struct wl_client *client = data;
197 if (mask & WL_CONNECTION_READABLE)
198 emask |= WL_EVENT_READABLE;
199 if (mask & WL_CONNECTION_WRITABLE)
200 emask |= WL_EVENT_WRITEABLE;
202 return wl_event_source_fd_update(client->source, emask);
205 WL_EXPORT struct wl_display *
206 wl_client_get_display(struct wl_client *client)
208 return client->display;
212 wl_display_post_range(struct wl_display *display, struct wl_client *client)
214 wl_client_post_event(client, &client->display->object,
215 WL_DISPLAY_RANGE, display->client_id_range);
216 display->client_id_range += 256;
217 client->id_count += 256;
220 WL_EXPORT struct wl_client *
221 wl_client_create(struct wl_display *display, int fd)
223 struct wl_client *client;
224 struct wl_global *global;
226 client = malloc(sizeof *client);
230 memset(client, 0, sizeof *client);
231 client->display = display;
232 client->source = wl_event_loop_add_fd(display->loop, fd,
234 wl_client_connection_data, client);
236 wl_connection_create(fd, wl_client_connection_update, client);
237 if (client->connection == NULL) {
242 wl_list_init(&client->resource_list);
244 wl_display_post_range(display, client);
246 wl_list_for_each(global, &display->global_list, link)
247 wl_client_post_global(client, global->object);
253 wl_client_add_resource(struct wl_client *client,
254 struct wl_resource *resource)
256 struct wl_display *display = client->display;
258 if (client->id_count-- < 64)
259 wl_display_post_range(display, client);
261 wl_hash_table_insert(client->display->objects,
262 resource->object.id, resource);
263 wl_list_insert(client->resource_list.prev, &resource->link);
267 wl_client_post_no_memory(struct wl_client *client)
269 wl_client_post_event(client,
270 &client->display->object,
271 WL_DISPLAY_NO_MEMORY);
275 wl_client_post_global(struct wl_client *client, struct wl_object *object)
277 wl_client_post_event(client,
278 &client->display->object,
281 object->interface->name,
282 object->interface->version);
286 wl_resource_destroy(struct wl_resource *resource, struct wl_client *client)
288 struct wl_display *display = client->display;
290 wl_list_remove(&resource->link);
291 if (resource->object.id > 0)
292 wl_hash_table_remove(display->objects, resource->object.id);
293 resource->destroy(resource, client);
297 wl_client_destroy(struct wl_client *client)
299 struct wl_resource *resource, *tmp;
301 printf("disconnect from client %p\n", client);
303 wl_list_for_each_safe(resource, tmp, &client->resource_list, link)
304 wl_resource_destroy(resource, client);
306 wl_event_source_remove(client->source);
307 wl_connection_destroy(client->connection);
312 lose_pointer_focus(struct wl_listener *listener,
313 struct wl_surface *surface, uint32_t time)
315 struct wl_input_device *device =
316 container_of(listener, struct wl_input_device,
317 pointer_focus_listener);
319 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
323 lose_keyboard_focus(struct wl_listener *listener,
324 struct wl_surface *surface, uint32_t time)
326 struct wl_input_device *device =
327 container_of(listener, struct wl_input_device,
328 keyboard_focus_listener);
330 wl_input_device_set_keyboard_focus(device, NULL, time);
334 wl_input_device_init(struct wl_input_device *device,
335 struct wl_compositor *compositor)
337 wl_list_init(&device->pointer_focus_listener.link);
338 device->pointer_focus_listener.func = lose_pointer_focus;
339 wl_list_init(&device->keyboard_focus_listener.link);
340 device->keyboard_focus_listener.func = lose_keyboard_focus;
344 device->compositor = compositor;
348 wl_input_device_set_pointer_focus(struct wl_input_device *device,
349 struct wl_surface *surface,
351 int32_t x, int32_t y,
352 int32_t sx, int32_t sy)
354 if (device->pointer_focus == surface)
357 if (device->pointer_focus &&
358 (!surface || device->pointer_focus->client != surface->client))
359 wl_client_post_event(device->pointer_focus->client,
361 WL_INPUT_DEVICE_POINTER_FOCUS,
362 time, NULL, 0, 0, 0, 0);
363 if (device->pointer_focus)
364 wl_list_remove(&device->pointer_focus_listener.link);
367 wl_client_post_event(surface->client,
369 WL_INPUT_DEVICE_POINTER_FOCUS,
370 time, surface, x, y, sx, sy);
371 wl_list_insert(surface->destroy_listener_list.prev,
372 &device->pointer_focus_listener.link);
375 device->pointer_focus = surface;
376 device->pointer_focus_time = time;
381 wl_input_device_set_keyboard_focus(struct wl_input_device *device,
382 struct wl_surface *surface,
385 if (device->keyboard_focus == surface)
388 if (device->keyboard_focus &&
389 (!surface || device->keyboard_focus->client != surface->client))
390 wl_client_post_event(device->keyboard_focus->client,
392 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
393 time, NULL, &device->keys);
394 if (device->keyboard_focus)
395 wl_list_remove(&device->keyboard_focus_listener.link);
398 wl_client_post_event(surface->client,
400 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
401 time, surface, &device->keys);
402 wl_list_insert(surface->destroy_listener_list.prev,
403 &device->keyboard_focus_listener.link);
406 device->keyboard_focus = surface;
407 device->keyboard_focus_time = time;
411 wl_input_device_end_grab(struct wl_input_device *device, uint32_t time)
413 const struct wl_grab_interface *interface;
415 interface = device->grab->interface;
416 interface->end(device->grab, time);
417 device->grab->input_device = NULL;
420 wl_list_remove(&device->grab_listener.link);
424 lose_grab_surface(struct wl_listener *listener,
425 struct wl_surface *surface, uint32_t time)
427 struct wl_input_device *device =
428 container_of(listener,
429 struct wl_input_device, grab_listener);
431 wl_input_device_end_grab(device, time);
435 wl_input_device_start_grab(struct wl_input_device *device,
436 struct wl_grab *grab,
437 uint32_t button, uint32_t time)
439 struct wl_surface *focus = device->pointer_focus;
442 device->grab_button = button;
443 device->grab_time = time;
444 device->grab_x = device->x;
445 device->grab_y = device->y;
447 device->grab_listener.func = lose_grab_surface;
448 wl_list_insert(focus->destroy_listener_list.prev,
449 &device->grab_listener.link);
451 grab->input_device = device;
455 wl_input_device_update_grab(struct wl_input_device *device,
456 struct wl_grab *grab,
457 struct wl_surface *surface, uint32_t time)
459 if (device->grab != &device->motion_grab ||
460 device->grab_time != time ||
461 device->pointer_focus != surface)
465 grab->input_device = device;
471 display_bind(struct wl_client *client,
472 struct wl_display *display, uint32_t id,
473 const char *interface, uint32_t version)
475 struct wl_global *global;
477 wl_list_for_each(global, &display->global_list, link)
478 if (global->object->id == id && global->func)
479 global->func(client, global->object);
483 display_sync(struct wl_client *client,
484 struct wl_display *display, uint32_t key)
486 wl_client_post_event(client, &display->object, WL_DISPLAY_KEY, key, 0);
490 destroy_frame_listener(struct wl_resource *resource, struct wl_client *client)
492 struct wl_frame_listener *listener =
493 container_of(resource, struct wl_frame_listener, resource);
495 wl_list_remove(&listener->link);
500 display_frame(struct wl_client *client,
501 struct wl_display *display,
502 struct wl_surface *surface,
505 struct wl_frame_listener *listener;
507 listener = malloc(sizeof *listener);
508 if (listener == NULL) {
509 wl_client_post_no_memory(client);
513 /* The listener is a resource so we destroy it when the client
515 listener->resource.destroy = destroy_frame_listener;
516 listener->resource.object.id = 0;
517 listener->client = client;
519 listener->surface = surface;
520 wl_list_insert(client->resource_list.prev, &listener->resource.link);
521 wl_list_insert(display->frame_list.prev, &listener->link);
524 struct wl_display_interface display_interface = {
531 WL_EXPORT struct wl_display *
532 wl_display_create(void)
534 struct wl_display *display;
537 debug = getenv("WAYLAND_DEBUG");
541 display = malloc(sizeof *display);
545 display->loop = wl_event_loop_create();
546 if (display->loop == NULL) {
551 display->objects = wl_hash_table_create();
552 if (display->objects == NULL) {
553 wl_event_loop_destroy(display->loop);
558 wl_list_init(&display->frame_list);
559 wl_list_init(&display->global_list);
560 wl_list_init(&display->socket_list);
562 display->client_id_range = 256; /* Gah, arbitrary... */
565 display->object.interface = &wl_display_interface;
566 display->object.implementation = (void (**)(void)) &display_interface;
567 wl_display_add_object(display, &display->object);
568 if (wl_display_add_global(display, &display->object, NULL)) {
569 wl_hash_table_destroy(display->objects);
570 wl_event_loop_destroy(display->loop);
579 wl_display_destroy(struct wl_display *display)
581 struct wl_socket *s, *next;
583 wl_event_loop_destroy(display->loop);
584 wl_hash_table_destroy(display->objects);
586 wl_list_for_each_safe(s, next, &display->socket_list, link) {
588 unlink(s->addr.sun_path);
590 unlink(s->lock_addr);
598 wl_display_add_object(struct wl_display *display, struct wl_object *object)
600 object->id = display->id++;
601 wl_hash_table_insert(display->objects, object->id, object);
605 wl_display_add_global(struct wl_display *display,
606 struct wl_object *object, wl_client_connect_func_t func)
608 struct wl_global *global;
610 global = malloc(sizeof *global);
614 global->object = object;
616 wl_list_insert(display->global_list.prev, &global->link);
622 wl_display_post_frame(struct wl_display *display, struct wl_surface *surface,
625 struct wl_frame_listener *listener, *next;
627 wl_list_for_each_safe(listener, next, &display->frame_list, link) {
628 if (listener->surface != surface)
630 wl_client_post_event(listener->client, &display->object,
631 WL_DISPLAY_KEY, listener->key, time);
632 wl_resource_destroy(&listener->resource, listener->client);
636 WL_EXPORT struct wl_event_loop *
637 wl_display_get_event_loop(struct wl_display *display)
639 return display->loop;
643 wl_display_terminate(struct wl_display *display)
649 wl_display_run(struct wl_display *display)
654 wl_event_loop_dispatch(display->loop, -1);
658 socket_data(int fd, uint32_t mask, void *data)
660 struct wl_display *display = data;
661 struct sockaddr_un name;
665 length = sizeof name;
667 accept4(fd, (struct sockaddr *) &name, &length, SOCK_CLOEXEC);
669 fprintf(stderr, "failed to accept\n");
671 wl_client_create(display, client_fd);
675 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
677 struct stat socket_stat;
678 int lock_size = name_size + 5;
680 snprintf(socket->lock_addr, lock_size,
681 "%s.lock", socket->addr.sun_path);
683 socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
684 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
686 if (socket->fd_lock < 0) {
688 "unable to open lockfile %s check permissions\n",
693 if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
695 "unable to lock lockfile %s, maybe another compositor is running\n",
697 close(socket->fd_lock);
701 if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
702 if (errno != ENOENT) {
703 fprintf(stderr, "did not manage to stat file %s\n",
704 socket->addr.sun_path);
705 close(socket->fd_lock);
708 } else if (socket_stat.st_mode & S_IWUSR ||
709 socket_stat.st_mode & S_IWGRP) {
710 unlink(socket->addr.sun_path);
717 wl_display_add_socket(struct wl_display *display, const char *name)
720 socklen_t size, name_size;
721 const char *runtime_dir;
723 s = malloc(sizeof *s);
727 s->fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
733 runtime_dir = getenv("XDG_RUNTIME_DIR");
734 if (runtime_dir == NULL) {
737 "XDG_RUNTIME_DIR not set, falling back to %s\n",
742 name = getenv("WAYLAND_DISPLAY");
746 memset(&s->addr, 0, sizeof s->addr);
747 s->addr.sun_family = AF_LOCAL;
748 name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
749 "%s/%s", runtime_dir, name) + 1;
750 fprintf(stderr, "using socket %s\n", s->addr.sun_path);
752 if (get_socket_lock(s,name_size) < 0) {
758 size = offsetof (struct sockaddr_un, sun_path) + name_size;
759 if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
765 if (listen(s->fd, 1) < 0) {
767 unlink(s->addr.sun_path);
772 if (wl_event_loop_add_fd(display->loop, s->fd,
774 socket_data, display) == NULL) {
776 unlink(s->addr.sun_path);
780 wl_list_insert(display->socket_list.prev, &s->link);
786 wl_compositor_init(struct wl_compositor *compositor,
787 const struct wl_compositor_interface *interface,
788 struct wl_display *display)
790 compositor->object.interface = &wl_compositor_interface;
791 compositor->object.implementation = (void (**)(void)) interface;
792 wl_display_add_object(display, &compositor->object);
793 if (wl_display_add_global(display, &compositor->object, NULL))
796 compositor->argb_visual.object.interface = &wl_visual_interface;
797 compositor->argb_visual.object.implementation = NULL;
798 wl_display_add_object(display, &compositor->argb_visual.object);
799 if (wl_display_add_global(display, &compositor->argb_visual.object, NULL))
802 compositor->premultiplied_argb_visual.object.interface =
803 &wl_visual_interface;
804 compositor->premultiplied_argb_visual.object.implementation = NULL;
805 wl_display_add_object(display,
806 &compositor->premultiplied_argb_visual.object);
807 if (wl_display_add_global(display,
808 &compositor->premultiplied_argb_visual.object,
812 compositor->rgb_visual.object.interface = &wl_visual_interface;
813 compositor->rgb_visual.object.implementation = NULL;
814 wl_display_add_object(display,
815 &compositor->rgb_visual.object);
816 if (wl_display_add_global(display,
817 &compositor->rgb_visual.object, NULL))