blob: 48e151391ab3d92c8f5e0f5e1ace107836c23269 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040023#include <stdlib.h>
24#include <stdint.h>
25#include <stddef.h>
26#include <stdio.h>
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -050027#include <stdarg.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040028#include <errno.h>
29#include <string.h>
30#include <unistd.h>
31#include <sys/socket.h>
32#include <sys/un.h>
Kristian Høgsberg5503bf82008-11-06 10:38:17 -050033#include <dlfcn.h>
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -050034#include <assert.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040035#include <ffi.h>
Kristian Høgsberg680f1c72008-10-08 12:48:46 -040036
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050037#include "wayland-protocol.h"
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040038#include "wayland.h"
Kristian Høgsberg680f1c72008-10-08 12:48:46 -040039#include "connection.h"
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040040
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040041struct wl_client {
Kristian Høgsberg680f1c72008-10-08 12:48:46 -040042 struct wl_connection *connection;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040043 struct wl_event_source *source;
44 struct wl_display *display;
Kristian Høgsberg94a2e862008-10-11 21:37:55 -040045 struct wl_list object_list;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050046 struct wl_list link;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040047};
48
49struct wl_display {
50 struct wl_object base;
51 struct wl_event_loop *loop;
Kristian Høgsberg864c4682008-12-12 11:05:17 -050052 struct wl_hash *objects;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040053
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050054 struct wl_list pending_frame_list;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040055 uint32_t client_id_range;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050056 uint32_t id;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050057
Kristian Høgsberg14fcff72008-11-23 19:10:23 -050058 struct wl_list global_list;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040059};
60
Kristian Høgsberg94a2e862008-10-11 21:37:55 -040061struct wl_object_ref {
62 struct wl_object *object;
63 struct wl_list link;
64};
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -050065
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040066void
Kristian Høgsberg680f1c72008-10-08 12:48:46 -040067wl_client_destroy(struct wl_client *client);
Kristian Høgsbergc5089872008-10-08 10:47:59 -040068
69static void
Kristian Høgsberg5a75c902008-12-10 13:16:50 -050070wl_client_vmarshal(struct wl_client *client, struct wl_object *sender,
71 uint32_t opcode, va_list ap)
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -050072{
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -050073 const struct wl_message *event;
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -050074 struct wl_object *object;
Kristian Høgsbergbf967b42008-12-21 20:25:16 -050075 uint32_t args[32], length, *p, size;
76 const char *s;
Kristian Høgsberg87e0a382008-12-05 10:38:42 -050077 int i, count;
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -050078
79 event = &sender->interface->events[opcode];
Kristian Høgsbergbf967b42008-12-21 20:25:16 -050080 count = strlen(event->signature);
Kristian Høgsberg87e0a382008-12-05 10:38:42 -050081 assert(count <= ARRAY_LENGTH(args));
82
Kristian Høgsbergbf967b42008-12-21 20:25:16 -050083 p = &args[2];
84 for (i = 0; i < count; i++) {
85 switch (event->signature[i]) {
Kristian Høgsberg87e0a382008-12-05 10:38:42 -050086 case 'u':
87 case 'i':
Kristian Høgsbergbf967b42008-12-21 20:25:16 -050088 *p++ = va_arg(ap, uint32_t);
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -050089 break;
Kristian Høgsberg87e0a382008-12-05 10:38:42 -050090 case 's':
Kristian Høgsbergbf967b42008-12-21 20:25:16 -050091 s = va_arg(ap, const char *);
92 length = strlen(s);
93 *p++ = length;
94 memcpy(p, s, length);
95 p += DIV_ROUNDUP(length, sizeof(*p));
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -050096 break;
Kristian Høgsberg87e0a382008-12-05 10:38:42 -050097 case 'o':
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -050098 object = va_arg(ap, struct wl_object *);
Kristian Høgsbergbf967b42008-12-21 20:25:16 -050099 *p++ = object->id;
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -0500100 break;
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -0500101 default:
102 assert(0);
103 break;
104 }
105 }
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -0500106
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500107 size = (p - args) * sizeof *p;
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -0500108 args[0] = sender->id;
109 args[1] = opcode | (size << 16);
110 wl_connection_write(client->connection, args, size);
111}
112
113static void
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500114wl_client_marshal(struct wl_client *client, struct wl_object *sender,
115 uint32_t opcode, ...)
116{
117 va_list ap;
118
119 va_start(ap, opcode);
120 wl_client_vmarshal(client, sender, opcode, ap);
121 va_end(ap);
122}
123
124static void
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400125wl_client_demarshal(struct wl_client *client, struct wl_object *target,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500126 uint32_t opcode, size_t size)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400127{
Kristian Høgsbergfb6d68d2008-12-21 21:54:51 -0500128 const struct wl_message *method;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500129 ffi_type *types[20];
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400130 ffi_cif cif;
131 uint32_t *p, result;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500132 int i, count;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400133 union {
134 uint32_t uint32;
135 const char *string;
136 void *object;
137 uint32_t new_id;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500138 } values[20];
139 void *args[20];
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400140 struct wl_object *object;
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400141 uint32_t data[64];
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500142 void (*func)(void);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400143
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500144 method = &target->interface->methods[opcode];
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500145 count = strlen(method->signature) + 2;
146 if (count > ARRAY_LENGTH(types)) {
147 printf("too many args (%d)\n", count);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400148 return;
149 }
150
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400151 if (sizeof data < size) {
152 printf("request too big, should malloc tmp buffer here\n");
153 return;
154 }
155
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400156 types[0] = &ffi_type_pointer;
157 values[0].object = client;
158 args[0] = &values[0];
159
160 types[1] = &ffi_type_pointer;
161 values[1].object = target;
162 args[1] = &values[1];
163
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400164 wl_connection_copy(client->connection, data, size);
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400165 p = &data[2];
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500166 for (i = 2; i < count; i++) {
167 switch (method->signature[i - 2]) {
168 case 'u':
169 case 'i':
170 types[i] = &ffi_type_uint32;
171 values[i].uint32 = *p;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400172 p++;
173 break;
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500174 case 's':
175 types[i] = &ffi_type_pointer;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400176 /* FIXME */
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500177 values[i].uint32 = *p++;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400178 break;
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500179 case 'o':
180 types[i] = &ffi_type_pointer;
Kristian Høgsberg864c4682008-12-12 11:05:17 -0500181 object = wl_hash_lookup(client->display->objects, *p);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400182 if (object == NULL)
183 printf("unknown object (%d)\n", *p);
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500184 values[i].object = object;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400185 p++;
186 break;
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500187 case 'n':
188 types[i] = &ffi_type_uint32;
189 values[i].new_id = *p;
Kristian Høgsberg864c4682008-12-12 11:05:17 -0500190 object = wl_hash_lookup(client->display->objects, *p);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400191 if (object != NULL)
192 printf("object already exists (%d)\n", *p);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400193 p++;
194 break;
195 default:
196 printf("unknown type\n");
197 break;
198 }
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500199 args[i] = &values[i];
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400200 }
201
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500202 func = target->implementation[opcode];
Kristian Høgsberg87e0a382008-12-05 10:38:42 -0500203 ffi_prep_cif(&cif, FFI_DEFAULT_ABI, count, &ffi_type_uint32, types);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500204 ffi_call(&cif, func, &result, args);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400205}
206
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400207static void
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400208wl_client_connection_data(int fd, uint32_t mask, void *data)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400209{
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400210 struct wl_client *client = data;
211 struct wl_connection *connection = client->connection;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400212 struct wl_object *object;
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400213 uint32_t p[2], opcode, size;
214 uint32_t cmask = 0;
215 int len;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400216
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400217 if (mask & WL_EVENT_READABLE)
218 cmask |= WL_CONNECTION_READABLE;
219 if (mask & WL_EVENT_WRITEABLE)
220 cmask |= WL_CONNECTION_WRITABLE;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400221
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400222 len = wl_connection_data(connection, cmask);
223 if (len < 0) {
224 wl_client_destroy(client);
225 return;
226 }
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400227
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500228 while (len >= sizeof p) {
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400229 wl_connection_copy(connection, p, sizeof p);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400230 opcode = p[1] & 0xffff;
231 size = p[1] >> 16;
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400232 if (len < size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400233 break;
234
Kristian Høgsberg864c4682008-12-12 11:05:17 -0500235 object = wl_hash_lookup(client->display->objects, p[0]);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400236 if (object == NULL) {
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -0500237 wl_client_marshal(client, &client->display->base,
238 WL_DISPLAY_INVALID_OBJECT, p[0]);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400239 wl_connection_consume(connection, size);
240 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400241 continue;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400242 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400243
244 if (opcode >= object->interface->method_count) {
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -0500245 wl_client_marshal(client, &client->display->base,
246 WL_DISPLAY_INVALID_METHOD, p[0], opcode);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400247 wl_connection_consume(connection, size);
248 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400249 continue;
250 }
251
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500252 wl_client_demarshal(client, object, opcode, size);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400253 wl_connection_consume(connection, size);
254 len -= size;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400255 }
256}
257
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400258static int
259wl_client_connection_update(struct wl_connection *connection,
260 uint32_t mask, void *data)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400261{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400262 struct wl_client *client = data;
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400263 uint32_t emask = 0;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400264
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400265 if (mask & WL_CONNECTION_READABLE)
266 emask |= WL_EVENT_READABLE;
267 if (mask & WL_CONNECTION_WRITABLE)
268 emask |= WL_EVENT_WRITEABLE;
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400269
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500270 return wl_event_source_fd_update(client->source, mask);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400271}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400272
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500273static struct wl_client *
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400274wl_client_create(struct wl_display *display, int fd)
275{
276 struct wl_client *client;
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500277 struct wl_object_ref *ref;
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400278
279 client = malloc(sizeof *client);
280 if (client == NULL)
281 return NULL;
282
283 memset(client, 0, sizeof *client);
284 client->display = display;
285 client->source = wl_event_loop_add_fd(display->loop, fd,
286 WL_EVENT_READABLE,
287 wl_client_connection_data, client);
288 client->connection = wl_connection_create(fd,
289 wl_client_connection_update,
290 client);
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400291 wl_list_init(&client->object_list);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500292 wl_list_init(&client->link);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400293
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400294 wl_connection_write(client->connection,
295 &display->client_id_range,
296 sizeof display->client_id_range);
297 display->client_id_range += 256;
298
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500299 ref = container_of(display->global_list.next,
300 struct wl_object_ref, link);
301 while (&ref->link != &display->global_list) {
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500302 wl_client_marshal(client, &client->display->base,
303 WL_DISPLAY_GLOBAL,
304 ref->object,
305 ref->object->interface->name,
306 ref->object->interface->version);
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500307
308 ref = container_of(ref->link.next,
309 struct wl_object_ref, link);
310 }
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400311
312 return client;
313}
314
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500315static void
316wl_object_destroy(struct wl_object *object)
317{
318 const struct wl_surface_interface *interface =
319 (const struct wl_surface_interface *) object->implementation;
320
321 /* FIXME: Need generic object destructor. */
322 interface->destroy(NULL, (struct wl_surface *) object);
323}
324
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400325void
326wl_client_destroy(struct wl_client *client)
327{
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400328 struct wl_object_ref *ref;
329
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400330 printf("disconnect from client %p\n", client);
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400331
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500332 wl_list_remove(&client->link);
333
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400334 while (client->object_list.next != &client->object_list) {
335 ref = container_of(client->object_list.next,
336 struct wl_object_ref, link);
337 wl_list_remove(&ref->link);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500338 wl_object_destroy(ref->object);
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400339 free(ref);
340 }
341
Kristian Høgsberg4a298902008-11-28 18:35:25 -0500342 wl_event_source_remove(client->source);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400343 wl_connection_destroy(client->connection);
344 free(client);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400345}
346
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500347WL_EXPORT int
348wl_client_add_surface(struct wl_client *client,
349 struct wl_surface *surface,
350 const struct wl_surface_interface *implementation,
351 uint32_t id)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400352{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500353 struct wl_display *display = client->display;
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400354 struct wl_object_ref *ref;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400355
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500356 surface->base.id = id;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500357 surface->base.interface = &wl_surface_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500358 surface->base.implementation = (void (**)(void)) implementation;
359 surface->client = client;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400360
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400361 ref = malloc(sizeof *ref);
362 if (ref == NULL) {
Kristian Høgsbergc2b633e2008-11-28 19:12:45 -0500363 wl_client_marshal(client, &display->base,
364 WL_DISPLAY_NO_MEMORY);
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400365 return -1;
366 }
367
368 ref->object = &surface->base;
Kristian Høgsberg864c4682008-12-12 11:05:17 -0500369 wl_hash_insert(display->objects, &surface->base);
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400370 wl_list_insert(client->object_list.prev, &ref->link);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400371
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400372 return 0;
373}
374
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500375WL_EXPORT void
376wl_client_send_acknowledge(struct wl_client *client,
377 struct wl_compositor *compositor,
378 uint32_t key, uint32_t frame)
Kristian Høgsberg40979232008-11-25 22:40:39 -0500379{
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500380 wl_list_remove(&client->link);
381 wl_list_insert(client->display->pending_frame_list.prev,
382 &client->link);
383 wl_client_marshal(client, &compositor->base,
384 WL_COMPOSITOR_ACKNOWLEDGE, key, frame);
385}
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500386
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500387WL_EXPORT int
388wl_display_set_compositor(struct wl_display *display,
389 struct wl_compositor *compositor,
390 const struct wl_compositor_interface *implementation)
391{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500392 compositor->base.interface = &wl_compositor_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500393 compositor->base.implementation = (void (**)(void)) implementation;
394
395 wl_display_add_object(display, &compositor->base);
396 if (wl_display_add_global(display, &compositor->base))
397 return -1;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500398
399 return 0;
400}
401
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500402WL_EXPORT struct wl_display *
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400403wl_display_create(void)
404{
405 struct wl_display *display;
406
407 display = malloc(sizeof *display);
408 if (display == NULL)
409 return NULL;
410
411 display->loop = wl_event_loop_create();
412 if (display->loop == NULL) {
413 free(display);
414 return NULL;
415 }
416
Kristian Høgsberg864c4682008-12-12 11:05:17 -0500417 display->objects = wl_hash_create();
418 if (display->objects == NULL) {
419 free(display);
420 return NULL;
421 }
422
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500423 wl_list_init(&display->pending_frame_list);
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500424 wl_list_init(&display->global_list);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500425
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400426 display->client_id_range = 256; /* Gah, arbitrary... */
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400427
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500428 display->id = 1;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500429 display->base.interface = &wl_display_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500430 display->base.implementation = NULL;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500431 wl_display_add_object(display, &display->base);
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500432 if (wl_display_add_global(display, &display->base)) {
433 wl_event_loop_destroy(display->loop);
434 free(display);
435 return NULL;
436 }
437
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400438 return display;
439}
440
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500441WL_EXPORT void
442wl_display_add_object(struct wl_display *display, struct wl_object *object)
443{
444 object->id = display->id++;
Kristian Høgsberg864c4682008-12-12 11:05:17 -0500445 wl_hash_insert(display->objects, object);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500446}
447
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500448WL_EXPORT int
449wl_display_add_global(struct wl_display *display, struct wl_object *object)
450{
451 struct wl_object_ref *ref;
452
453 ref = malloc(sizeof *ref);
454 if (ref == NULL)
455 return -1;
456
457 ref->object = object;
458 wl_list_insert(display->global_list.prev, &ref->link);
459
460 return 0;
461}
462
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500463WL_EXPORT void
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500464wl_surface_post_event(struct wl_surface *surface,
465 struct wl_object *sender,
466 uint32_t event, ...)
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500467{
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500468 va_list ap;
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500469
Kristian Høgsberg5a75c902008-12-10 13:16:50 -0500470 va_start(ap, event);
471 wl_client_vmarshal(surface->client, sender, event, ap);
472 va_end(ap);
Kristian Høgsberg715a0812008-12-10 10:42:04 -0500473}
474
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500475WL_EXPORT void
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500476wl_display_post_frame(struct wl_display *display,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500477 struct wl_compositor *compositor,
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500478 uint32_t frame, uint32_t msecs)
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500479{
480 struct wl_client *client;
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500481
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500482 client = container_of(display->pending_frame_list.next,
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500483 struct wl_client, link);
484
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500485 while (&client->link != &display->pending_frame_list) {
486 wl_client_marshal(client, &compositor->base,
487 WL_COMPOSITOR_FRAME, frame, msecs);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500488 client = container_of(client->link.next,
489 struct wl_client, link);
490 }
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500491
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500492 wl_list_remove(&display->pending_frame_list);
493 wl_list_init(&display->pending_frame_list);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400494}
495
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500496WL_EXPORT struct wl_event_loop *
Kristian Høgsberg5ebb3172008-10-11 19:21:35 -0400497wl_display_get_event_loop(struct wl_display *display)
498{
499 return display->loop;
500}
501
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500502WL_EXPORT void
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400503wl_display_run(struct wl_display *display)
504{
505 while (1)
506 wl_event_loop_wait(display->loop);
507}
508
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400509static void
510socket_data(int fd, uint32_t mask, void *data)
511{
512 struct wl_display *display = data;
513 struct sockaddr_un name;
514 socklen_t length;
515 int client_fd;
516
517 length = sizeof name;
518 client_fd = accept (fd, (struct sockaddr *) &name, &length);
519 if (client_fd < 0)
520 fprintf(stderr, "failed to accept\n");
521
522 wl_client_create(display, client_fd);
523}
524
Kristian Høgsberg122912c2008-12-05 11:13:50 -0500525WL_EXPORT int
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500526wl_display_add_socket(struct wl_display *display,
527 const char *name, size_t name_size)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400528{
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500529 struct sockaddr_un addr;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400530 int sock;
531 socklen_t size;
532
533 sock = socket(PF_LOCAL, SOCK_STREAM, 0);
534 if (sock < 0)
535 return -1;
536
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500537 addr.sun_family = AF_LOCAL;
538 memcpy(addr.sun_path, name, name_size);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400539
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500540 size = offsetof (struct sockaddr_un, sun_path) + name_size;
541 if (bind(sock, (struct sockaddr *) &addr, size) < 0)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400542 return -1;
543
544 if (listen(sock, 1) < 0)
545 return -1;
546
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400547 wl_event_loop_add_fd(display->loop, sock,
548 WL_EVENT_READABLE,
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400549 socket_data, display);
550
551 return 0;
552}