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_event(client, &client->display->object,
250 global->object->interface->name,
251 global->object->interface->version);
257 wl_client_add_resource(struct wl_client *client,
258 struct wl_resource *resource)
260 struct wl_display *display = client->display;
262 if (client->id_count-- < 64)
263 wl_display_post_range(display, client);
265 wl_hash_table_insert(client->display->objects,
266 resource->object.id, resource);
267 wl_list_insert(client->resource_list.prev, &resource->link);
271 wl_client_post_no_memory(struct wl_client *client)
273 wl_client_post_event(client,
274 &client->display->object,
275 WL_DISPLAY_NO_MEMORY);
279 wl_client_post_global(struct wl_client *client, struct wl_object *object)
281 wl_client_post_event(client,
282 &client->display->object,
285 object->interface->name,
286 object->interface->version);
290 wl_resource_destroy(struct wl_resource *resource, struct wl_client *client)
292 struct wl_display *display = client->display;
294 wl_list_remove(&resource->link);
295 if (resource->object.id > 0)
296 wl_hash_table_remove(display->objects, resource->object.id);
297 resource->destroy(resource, client);
301 wl_client_destroy(struct wl_client *client)
303 struct wl_resource *resource, *tmp;
305 printf("disconnect from client %p\n", client);
307 wl_list_for_each_safe(resource, tmp, &client->resource_list, link)
308 wl_resource_destroy(resource, client);
310 wl_event_source_remove(client->source);
311 wl_connection_destroy(client->connection);
316 lose_pointer_focus(struct wl_listener *listener,
317 struct wl_surface *surface, uint32_t time)
319 struct wl_input_device *device =
320 container_of(listener, struct wl_input_device,
321 pointer_focus_listener);
323 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
327 lose_keyboard_focus(struct wl_listener *listener,
328 struct wl_surface *surface, uint32_t time)
330 struct wl_input_device *device =
331 container_of(listener, struct wl_input_device,
332 keyboard_focus_listener);
334 wl_input_device_set_keyboard_focus(device, NULL, time);
338 wl_input_device_init(struct wl_input_device *device,
339 struct wl_compositor *compositor)
341 wl_list_init(&device->pointer_focus_listener.link);
342 device->pointer_focus_listener.func = lose_pointer_focus;
343 wl_list_init(&device->keyboard_focus_listener.link);
344 device->keyboard_focus_listener.func = lose_keyboard_focus;
348 device->compositor = compositor;
352 wl_input_device_set_pointer_focus(struct wl_input_device *device,
353 struct wl_surface *surface,
355 int32_t x, int32_t y,
356 int32_t sx, int32_t sy)
358 if (device->pointer_focus == surface)
361 if (device->pointer_focus &&
362 (!surface || device->pointer_focus->client != surface->client))
363 wl_client_post_event(device->pointer_focus->client,
365 WL_INPUT_DEVICE_POINTER_FOCUS,
366 time, NULL, 0, 0, 0, 0);
367 if (device->pointer_focus)
368 wl_list_remove(&device->pointer_focus_listener.link);
371 wl_client_post_event(surface->client,
373 WL_INPUT_DEVICE_POINTER_FOCUS,
374 time, surface, x, y, sx, sy);
375 wl_list_insert(surface->destroy_listener_list.prev,
376 &device->pointer_focus_listener.link);
379 device->pointer_focus = surface;
380 device->pointer_focus_time = time;
385 wl_input_device_set_keyboard_focus(struct wl_input_device *device,
386 struct wl_surface *surface,
389 if (device->keyboard_focus == surface)
392 if (device->keyboard_focus &&
393 (!surface || device->keyboard_focus->client != surface->client))
394 wl_client_post_event(device->keyboard_focus->client,
396 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
397 time, NULL, &device->keys);
398 if (device->keyboard_focus)
399 wl_list_remove(&device->keyboard_focus_listener.link);
402 wl_client_post_event(surface->client,
404 WL_INPUT_DEVICE_KEYBOARD_FOCUS,
405 time, surface, &device->keys);
406 wl_list_insert(surface->destroy_listener_list.prev,
407 &device->keyboard_focus_listener.link);
410 device->keyboard_focus = surface;
411 device->keyboard_focus_time = time;
415 wl_input_device_end_grab(struct wl_input_device *device, uint32_t time)
417 const struct wl_grab_interface *interface;
419 interface = device->grab->interface;
420 interface->end(device->grab, time);
421 device->grab->input_device = NULL;
424 wl_list_remove(&device->grab_listener.link);
428 lose_grab_surface(struct wl_listener *listener,
429 struct wl_surface *surface, uint32_t time)
431 struct wl_input_device *device =
432 container_of(listener,
433 struct wl_input_device, grab_listener);
435 wl_input_device_end_grab(device, time);
439 wl_input_device_start_grab(struct wl_input_device *device,
440 struct wl_grab *grab,
441 uint32_t button, uint32_t time)
443 struct wl_surface *focus = device->pointer_focus;
446 device->grab_button = button;
447 device->grab_time = time;
448 device->grab_x = device->x;
449 device->grab_y = device->y;
451 device->grab_listener.func = lose_grab_surface;
452 wl_list_insert(focus->destroy_listener_list.prev,
453 &device->grab_listener.link);
455 grab->input_device = device;
459 wl_input_device_update_grab(struct wl_input_device *device,
460 struct wl_grab *grab,
461 struct wl_surface *surface, uint32_t time)
463 if (device->grab != &device->motion_grab ||
464 device->grab_time != time ||
465 device->pointer_focus != surface)
469 grab->input_device = device;
475 display_bind(struct wl_client *client,
476 struct wl_display *display, uint32_t id,
477 const char *interface, uint32_t version)
479 struct wl_global *global;
481 wl_list_for_each(global, &display->global_list, link)
482 if (global->object->id == id && global->func)
483 global->func(client, global->object);
487 display_sync(struct wl_client *client,
488 struct wl_display *display, uint32_t key)
490 wl_client_post_event(client, &display->object, WL_DISPLAY_KEY, key, 0);
494 destroy_frame_listener(struct wl_resource *resource, struct wl_client *client)
496 struct wl_frame_listener *listener =
497 container_of(resource, struct wl_frame_listener, resource);
499 wl_list_remove(&listener->link);
504 display_frame(struct wl_client *client,
505 struct wl_display *display,
506 struct wl_surface *surface,
509 struct wl_frame_listener *listener;
511 listener = malloc(sizeof *listener);
512 if (listener == NULL) {
513 wl_client_post_no_memory(client);
517 /* The listener is a resource so we destroy it when the client
519 listener->resource.destroy = destroy_frame_listener;
520 listener->resource.object.id = 0;
521 listener->client = client;
523 listener->surface = surface;
524 wl_list_insert(client->resource_list.prev, &listener->resource.link);
525 wl_list_insert(display->frame_list.prev, &listener->link);
528 struct wl_display_interface display_interface = {
535 WL_EXPORT struct wl_display *
536 wl_display_create(void)
538 struct wl_display *display;
541 debug = getenv("WAYLAND_DEBUG");
545 display = malloc(sizeof *display);
549 display->loop = wl_event_loop_create();
550 if (display->loop == NULL) {
555 display->objects = wl_hash_table_create();
556 if (display->objects == NULL) {
557 wl_event_loop_destroy(display->loop);
562 wl_list_init(&display->frame_list);
563 wl_list_init(&display->global_list);
564 wl_list_init(&display->socket_list);
566 display->client_id_range = 256; /* Gah, arbitrary... */
569 display->object.interface = &wl_display_interface;
570 display->object.implementation = (void (**)(void)) &display_interface;
571 wl_display_add_object(display, &display->object);
572 if (wl_display_add_global(display, &display->object, NULL)) {
573 wl_hash_table_destroy(display->objects);
574 wl_event_loop_destroy(display->loop);
583 wl_display_destroy(struct wl_display *display)
585 struct wl_socket *s, *next;
587 wl_event_loop_destroy(display->loop);
588 wl_hash_table_destroy(display->objects);
590 wl_list_for_each_safe(s, next, &display->socket_list, link) {
592 unlink(s->addr.sun_path);
594 unlink(s->lock_addr);
602 wl_display_add_object(struct wl_display *display, struct wl_object *object)
604 object->id = display->id++;
605 wl_hash_table_insert(display->objects, object->id, object);
609 wl_display_add_global(struct wl_display *display,
610 struct wl_object *object, wl_client_connect_func_t func)
612 struct wl_global *global;
614 global = malloc(sizeof *global);
618 global->object = object;
620 wl_list_insert(display->global_list.prev, &global->link);
626 wl_display_post_frame(struct wl_display *display, struct wl_surface *surface,
629 struct wl_frame_listener *listener, *next;
631 wl_list_for_each_safe(listener, next, &display->frame_list, link) {
632 if (listener->surface != surface)
634 wl_client_post_event(listener->client, &display->object,
635 WL_DISPLAY_KEY, listener->key, time);
636 wl_resource_destroy(&listener->resource, listener->client);
640 WL_EXPORT struct wl_event_loop *
641 wl_display_get_event_loop(struct wl_display *display)
643 return display->loop;
647 wl_display_terminate(struct wl_display *display)
653 wl_display_run(struct wl_display *display)
658 wl_event_loop_dispatch(display->loop, -1);
662 socket_data(int fd, uint32_t mask, void *data)
664 struct wl_display *display = data;
665 struct sockaddr_un name;
669 length = sizeof name;
671 accept4(fd, (struct sockaddr *) &name, &length, SOCK_CLOEXEC);
673 fprintf(stderr, "failed to accept\n");
675 wl_client_create(display, client_fd);
679 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
681 struct stat socket_stat;
682 int lock_size = name_size + 5;
684 snprintf(socket->lock_addr, lock_size,
685 "%s.lock", socket->addr.sun_path);
687 socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
688 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
690 if (socket->fd_lock < 0) {
692 "unable to open lockfile %s check permissions\n",
697 if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
699 "unable to lock lockfile %s, maybe another compositor is running\n",
701 close(socket->fd_lock);
705 if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
706 if (errno != ENOENT) {
707 fprintf(stderr, "did not manage to stat file %s\n",
708 socket->addr.sun_path);
709 close(socket->fd_lock);
712 } else if (socket_stat.st_mode & S_IWUSR ||
713 socket_stat.st_mode & S_IWGRP) {
714 unlink(socket->addr.sun_path);
721 wl_display_add_socket(struct wl_display *display, const char *name)
724 socklen_t size, name_size;
725 const char *runtime_dir;
727 s = malloc(sizeof *s);
731 s->fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
737 runtime_dir = getenv("XDG_RUNTIME_DIR");
738 if (runtime_dir == NULL) {
741 "XDG_RUNTIME_DIR not set, falling back to %s\n",
746 name = getenv("WAYLAND_DISPLAY");
750 memset(&s->addr, 0, sizeof s->addr);
751 s->addr.sun_family = AF_LOCAL;
752 name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
753 "%s/%s", runtime_dir, name) + 1;
754 fprintf(stderr, "using socket %s\n", s->addr.sun_path);
756 if (get_socket_lock(s,name_size) < 0) {
762 size = offsetof (struct sockaddr_un, sun_path) + name_size;
763 if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
769 if (listen(s->fd, 1) < 0) {
771 unlink(s->addr.sun_path);
776 if (wl_event_loop_add_fd(display->loop, s->fd,
778 socket_data, display) == NULL) {
780 unlink(s->addr.sun_path);
784 wl_list_insert(display->socket_list.prev, &s->link);
790 wl_compositor_init(struct wl_compositor *compositor,
791 const struct wl_compositor_interface *interface,
792 struct wl_display *display)
794 compositor->object.interface = &wl_compositor_interface;
795 compositor->object.implementation = (void (**)(void)) interface;
796 wl_display_add_object(display, &compositor->object);
797 if (wl_display_add_global(display, &compositor->object, NULL))
800 compositor->argb_visual.object.interface = &wl_visual_interface;
801 compositor->argb_visual.object.implementation = NULL;
802 wl_display_add_object(display, &compositor->argb_visual.object);
803 if (wl_display_add_global(display, &compositor->argb_visual.object, NULL))
806 compositor->premultiplied_argb_visual.object.interface =
807 &wl_visual_interface;
808 compositor->premultiplied_argb_visual.object.implementation = NULL;
809 wl_display_add_object(display,
810 &compositor->premultiplied_argb_visual.object);
811 if (wl_display_add_global(display,
812 &compositor->premultiplied_argb_visual.object,
816 compositor->rgb_visual.object.interface = &wl_visual_interface;
817 compositor->rgb_visual.object.implementation = NULL;
818 wl_display_add_object(display,
819 &compositor->rgb_visual.object);
820 if (wl_display_add_global(display,
821 &compositor->rgb_visual.object, NULL))