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
31 #include <sys/socket.h>
37 #include "wayland-protocol.h"
39 #include "connection.h"
42 struct wl_connection *connection;
43 struct wl_event_source *source;
44 struct wl_display *display;
45 struct wl_list object_list;
51 struct wl_object base;
52 struct wl_event_loop *loop;
53 struct wl_hash *objects;
55 struct wl_list pending_frame_list;
56 uint32_t client_id_range;
59 struct wl_list global_list;
62 struct wl_object_ref {
63 struct wl_object *object;
68 struct wl_object *object;
69 wl_client_connect_func_t func;
74 wl_client_destroy(struct wl_client *client);
77 wl_client_post_event(struct wl_client *client, struct wl_object *sender,
83 wl_connection_vmarshal(client->connection,
85 &sender->interface->events[opcode]);
90 wl_client_connection_data(int fd, uint32_t mask, void *data)
92 struct wl_client *client = data;
93 struct wl_connection *connection = client->connection;
94 struct wl_object *object;
95 uint32_t p[2], opcode, size;
99 if (mask & WL_EVENT_READABLE)
100 cmask |= WL_CONNECTION_READABLE;
101 if (mask & WL_EVENT_WRITEABLE)
102 cmask |= WL_CONNECTION_WRITABLE;
104 len = wl_connection_data(connection, cmask);
106 wl_client_destroy(client);
110 while (len >= sizeof p) {
111 wl_connection_copy(connection, p, sizeof p);
112 opcode = p[1] & 0xffff;
117 object = wl_hash_lookup(client->display->objects, p[0]);
118 if (object == NULL) {
119 wl_client_post_event(client, &client->display->base,
120 WL_DISPLAY_INVALID_OBJECT, p[0]);
121 wl_connection_consume(connection, size);
126 if (opcode >= object->interface->method_count) {
127 wl_client_post_event(client, &client->display->base,
128 WL_DISPLAY_INVALID_METHOD, p[0], opcode);
129 wl_connection_consume(connection, size);
134 wl_connection_demarshal(client->connection,
136 client->display->objects,
137 object->implementation[opcode],
140 &object->interface->methods[opcode]);
142 wl_connection_consume(connection, size);
148 wl_client_connection_update(struct wl_connection *connection,
149 uint32_t mask, void *data)
151 struct wl_client *client = data;
154 if (mask & WL_CONNECTION_READABLE)
155 emask |= WL_EVENT_READABLE;
156 if (mask & WL_CONNECTION_WRITABLE)
157 emask |= WL_EVENT_WRITEABLE;
159 return wl_event_source_fd_update(client->source, mask);
163 wl_display_post_range(struct wl_display *display, struct wl_client *client)
165 wl_client_post_event(client, &client->display->base,
166 WL_DISPLAY_RANGE, display->client_id_range);
167 display->client_id_range += 256;
168 client->id_count += 256;
171 static struct wl_client *
172 wl_client_create(struct wl_display *display, int fd)
174 struct wl_client *client;
175 struct wl_global *global;
177 client = malloc(sizeof *client);
181 memset(client, 0, sizeof *client);
182 client->display = display;
183 client->source = wl_event_loop_add_fd(display->loop, fd,
185 wl_client_connection_data, client);
187 wl_connection_create(fd, wl_client_connection_update, client);
189 wl_list_init(&client->object_list);
190 wl_list_init(&client->link);
192 wl_display_post_range(display, client);
194 global = container_of(display->global_list.next,
195 struct wl_global, link);
196 while (&global->link != &display->global_list) {
197 wl_client_post_event(client, &client->display->base,
200 global->object->interface->name,
201 global->object->interface->version);
202 global = container_of(global->link.next,
203 struct wl_global, link);
206 global = container_of(display->global_list.next,
207 struct wl_global, link);
208 while (&global->link != &display->global_list) {
210 global->func(client, global->object);
211 global = container_of(global->link.next,
212 struct wl_global, link);
219 wl_object_destroy(struct wl_object *object)
221 const struct wl_surface_interface *interface =
222 (const struct wl_surface_interface *) object->implementation;
224 /* FIXME: Need generic object destructor. */
225 interface->destroy(NULL, (struct wl_surface *) object);
229 wl_client_destroy(struct wl_client *client)
231 struct wl_object_ref *ref;
233 printf("disconnect from client %p\n", client);
235 wl_list_remove(&client->link);
237 while (client->object_list.next != &client->object_list) {
238 ref = container_of(client->object_list.next,
239 struct wl_object_ref, link);
240 wl_list_remove(&ref->link);
241 wl_object_destroy(ref->object);
245 wl_event_source_remove(client->source);
246 wl_connection_destroy(client->connection);
251 wl_client_add_surface(struct wl_client *client,
252 struct wl_surface *surface,
253 const struct wl_surface_interface *implementation,
256 struct wl_display *display = client->display;
257 struct wl_object_ref *ref;
259 if (client->id_count-- < 64)
260 wl_display_post_range(display, client);
262 surface->base.id = id;
263 surface->base.interface = &wl_surface_interface;
264 surface->base.implementation = (void (**)(void)) implementation;
265 surface->client = client;
267 ref = malloc(sizeof *ref);
269 wl_client_post_event(client, &display->base,
270 WL_DISPLAY_NO_MEMORY);
274 ref->object = &surface->base;
275 wl_hash_insert(display->objects, &surface->base);
276 wl_list_insert(client->object_list.prev, &ref->link);
282 wl_client_send_acknowledge(struct wl_client *client,
283 struct wl_compositor *compositor,
284 uint32_t key, uint32_t frame)
286 wl_list_remove(&client->link);
287 wl_list_insert(client->display->pending_frame_list.prev,
289 wl_client_post_event(client, &compositor->base,
290 WL_COMPOSITOR_ACKNOWLEDGE, key, frame);
294 wl_display_set_compositor(struct wl_display *display,
295 struct wl_compositor *compositor,
296 const struct wl_compositor_interface *implementation)
298 compositor->base.interface = &wl_compositor_interface;
299 compositor->base.implementation = (void (**)(void)) implementation;
301 wl_display_add_object(display, &compositor->base);
302 if (wl_display_add_global(display, &compositor->base, NULL))
308 WL_EXPORT struct wl_display *
309 wl_display_create(void)
311 struct wl_display *display;
313 display = malloc(sizeof *display);
317 display->loop = wl_event_loop_create();
318 if (display->loop == NULL) {
323 display->objects = wl_hash_create();
324 if (display->objects == NULL) {
329 wl_list_init(&display->pending_frame_list);
330 wl_list_init(&display->global_list);
332 display->client_id_range = 256; /* Gah, arbitrary... */
335 display->base.interface = &wl_display_interface;
336 display->base.implementation = NULL;
337 wl_display_add_object(display, &display->base);
338 if (wl_display_add_global(display, &display->base, NULL)) {
339 wl_event_loop_destroy(display->loop);
348 wl_display_add_object(struct wl_display *display, struct wl_object *object)
350 object->id = display->id++;
351 wl_hash_insert(display->objects, object);
355 wl_display_add_global(struct wl_display *display,
356 struct wl_object *object, wl_client_connect_func_t func)
358 struct wl_global *global;
360 global = malloc(sizeof *global);
364 global->object = object;
366 wl_list_insert(display->global_list.prev, &global->link);
372 wl_surface_post_event(struct wl_surface *surface,
373 struct wl_object *sender,
379 wl_connection_vmarshal(surface->client->connection,
381 &sender->interface->events[event]);
386 wl_display_post_frame(struct wl_display *display,
387 struct wl_compositor *compositor,
388 uint32_t frame, uint32_t msecs)
390 struct wl_client *client;
392 client = container_of(display->pending_frame_list.next,
393 struct wl_client, link);
395 while (&client->link != &display->pending_frame_list) {
396 wl_client_post_event(client, &compositor->base,
397 WL_COMPOSITOR_FRAME, frame, msecs);
398 client = container_of(client->link.next,
399 struct wl_client, link);
402 wl_list_remove(&display->pending_frame_list);
403 wl_list_init(&display->pending_frame_list);
406 WL_EXPORT struct wl_event_loop *
407 wl_display_get_event_loop(struct wl_display *display)
409 return display->loop;
413 wl_display_run(struct wl_display *display)
416 wl_event_loop_wait(display->loop);
420 socket_data(int fd, uint32_t mask, void *data)
422 struct wl_display *display = data;
423 struct sockaddr_un name;
427 length = sizeof name;
428 client_fd = accept (fd, (struct sockaddr *) &name, &length);
430 fprintf(stderr, "failed to accept\n");
432 wl_client_create(display, client_fd);
436 wl_display_add_socket(struct wl_display *display,
437 const char *name, size_t name_size)
439 struct sockaddr_un addr;
443 sock = socket(PF_LOCAL, SOCK_STREAM, 0);
447 addr.sun_family = AF_LOCAL;
448 memcpy(addr.sun_path, name, name_size);
450 size = offsetof (struct sockaddr_un, sun_path) + name_size;
451 if (bind(sock, (struct sockaddr *) &addr, size) < 0)
454 if (listen(sock, 1) < 0)
457 wl_event_loop_add_fd(display->loop, sock,
459 socket_data, display);