blob: 5da047e546a80787506411a3b724960e6205cc70 [file] [log] [blame]
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -04001#include <stdlib.h>
2#include <stdint.h>
3#include <stddef.h>
4#include <stdio.h>
5#include <errno.h>
6#include <string.h>
7#include <unistd.h>
8#include <sys/socket.h>
9#include <sys/un.h>
Kristian Høgsberg5503bf82008-11-06 10:38:17 -050010#include <dlfcn.h>
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040011#include <ffi.h>
Kristian Høgsberg680f1c72008-10-08 12:48:46 -040012
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040013#include "wayland.h"
Kristian Høgsberg680f1c72008-10-08 12:48:46 -040014#include "connection.h"
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040015
Kristian Høgsbergf9212892008-10-11 18:40:23 -040016void wl_list_init(struct wl_list *list)
17{
18 list->prev = list;
19 list->next = list;
20}
21
22void
23wl_list_insert(struct wl_list *list, struct wl_list *elm)
24{
25 elm->prev = list;
26 elm->next = list->next;
27 list->next = elm;
28 elm->next->prev = elm;
29}
30
31void
32wl_list_remove(struct wl_list *elm)
33{
34 elm->prev->next = elm->next;
35 elm->next->prev = elm->prev;
36}
37
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040038struct wl_client {
Kristian Høgsberg680f1c72008-10-08 12:48:46 -040039 struct wl_connection *connection;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040040 struct wl_event_source *source;
41 struct wl_display *display;
Kristian Høgsberg94a2e862008-10-11 21:37:55 -040042 struct wl_list object_list;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050043 struct wl_list link;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040044};
45
46struct wl_display {
47 struct wl_object base;
48 struct wl_event_loop *loop;
49 struct wl_hash objects;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040050
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050051 struct wl_object *pointer;
52
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040053 struct wl_compositor *compositor;
54 struct wl_compositor_interface *compositor_interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040055
56 struct wl_list surface_list;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050057 struct wl_list client_list;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040058 uint32_t client_id_range;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050059
Kristian Høgsberg14fcff72008-11-23 19:10:23 -050060 struct wl_list global_list;
61
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050062 int32_t pointer_x;
63 int32_t pointer_y;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040064};
65
66struct wl_surface {
67 struct wl_object base;
68
69 /* provided by client */
70 int width, height;
71 int buffer;
72 int stride;
73
Kristian Høgsberg05eff512008-10-07 10:10:36 -040074 struct wl_map map;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040075 struct wl_list link;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040076
77 /* how to convert buffer contents to pixels in screen format;
78 * yuv->rgb, indexed->rgb, svg->rgb, but mostly just rgb->rgb. */
79
80 /* how to transform/render rectangular contents to polygons. */
Kristian Høgsberg05eff512008-10-07 10:10:36 -040081
82 void *compositor_data;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -040083};
84
Kristian Høgsberg94a2e862008-10-11 21:37:55 -040085struct wl_object_ref {
86 struct wl_object *object;
87 struct wl_list link;
88};
Kristian Høgsberg05eff512008-10-07 10:10:36 -040089
90static void
91wl_surface_destroy(struct wl_client *client,
92 struct wl_surface *surface)
93{
Kristian Høgsberg5503bf82008-11-06 10:38:17 -050094 const struct wl_compositor_interface *interface;
Kristian Høgsberg05eff512008-10-07 10:10:36 -040095
96 interface = client->display->compositor->interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040097 interface->notify_surface_destroy(client->display->compositor,
98 surface);
99 wl_list_remove(&surface->link);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400100}
101
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400102static void
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400103wl_surface_attach(struct wl_client *client,
104 struct wl_surface *surface, uint32_t name,
105 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400106{
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500107 const struct wl_compositor_interface *interface;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400108
109 interface = client->display->compositor->interface;
110 interface->notify_surface_attach(client->display->compositor,
111 surface, name, width, height, stride);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400112}
113
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400114static const struct wl_argument attach_arguments[] = {
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400115 { WL_ARGUMENT_UINT32 },
116 { WL_ARGUMENT_UINT32 },
117 { WL_ARGUMENT_UINT32 },
118 { WL_ARGUMENT_UINT32 },
119};
120
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500121static void
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400122wl_surface_map(struct wl_client *client, struct wl_surface *surface,
123 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400124{
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500125 const struct wl_compositor_interface *interface;
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400126
127 /* FIXME: This needs to take a tri-mesh argument... - count
128 * and a list of tris. 0 tris means unmap. */
129
130 surface->map.x = x;
131 surface->map.y = y;
132 surface->map.width = width;
133 surface->map.height = height;
134
135 interface = client->display->compositor->interface;
136 interface->notify_surface_map(client->display->compositor,
137 surface, &surface->map);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400138}
139
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400140static const struct wl_argument map_arguments[] = {
141 { WL_ARGUMENT_UINT32 },
142 { WL_ARGUMENT_UINT32 },
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400143 { WL_ARGUMENT_UINT32 },
144 { WL_ARGUMENT_UINT32 },
145};
146
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500147static void
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500148wl_surface_copy(struct wl_client *client, struct wl_surface *surface,
149 int32_t dst_x, int32_t dst_y, uint32_t name, uint32_t stride,
150 int32_t x, int32_t y, int32_t width, int32_t height)
151{
152 const struct wl_compositor_interface *interface;
153
154 interface = client->display->compositor->interface;
155 interface->notify_surface_copy(client->display->compositor,
156 surface, dst_x, dst_y,
157 name, stride, x, y, width, height);
158}
159
160static const struct wl_argument copy_arguments[] = {
161 { WL_ARGUMENT_UINT32 },
162 { WL_ARGUMENT_UINT32 },
163 { WL_ARGUMENT_UINT32 },
164 { WL_ARGUMENT_UINT32 },
165 { WL_ARGUMENT_UINT32 },
166 { WL_ARGUMENT_UINT32 },
167 { WL_ARGUMENT_UINT32 },
168 { WL_ARGUMENT_UINT32 },
169 { WL_ARGUMENT_UINT32 },
170 { WL_ARGUMENT_UINT32 },
171};
172
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500173static void
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500174wl_surface_damage(struct wl_client *client, struct wl_surface *surface,
175 int32_t x, int32_t y, int32_t width, int32_t height)
176{
177 const struct wl_compositor_interface *interface;
178
179 interface = client->display->compositor->interface;
180 interface->notify_surface_damage(client->display->compositor,
181 surface, x, y, width, height);
182}
183
184static const struct wl_argument damage_arguments[] = {
185 { WL_ARGUMENT_UINT32 },
186 { WL_ARGUMENT_UINT32 },
187 { WL_ARGUMENT_UINT32 },
188 { WL_ARGUMENT_UINT32 },
189};
190
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400191static const struct wl_method surface_methods[] = {
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400192 { "destroy", wl_surface_destroy,
193 0, NULL },
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400194 { "attach", wl_surface_attach,
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400195 ARRAY_LENGTH(attach_arguments), attach_arguments },
196 { "map", wl_surface_map,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500197 ARRAY_LENGTH(map_arguments), map_arguments },
198 { "copy", wl_surface_copy,
199 ARRAY_LENGTH(copy_arguments), copy_arguments },
200 { "damage", wl_surface_damage,
201 ARRAY_LENGTH(damage_arguments), damage_arguments }
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400202};
203
204static const struct wl_interface surface_interface = {
205 "surface", 1,
206 ARRAY_LENGTH(surface_methods),
207 surface_methods,
208};
209
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500210static struct wl_surface *
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400211wl_surface_create(struct wl_display *display, uint32_t id)
212{
213 struct wl_surface *surface;
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500214 const struct wl_compositor_interface *interface;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400215
216 surface = malloc(sizeof *surface);
217 if (surface == NULL)
218 return NULL;
219
220 surface->base.id = id;
221 surface->base.interface = &surface_interface;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400222
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400223 wl_list_insert(display->surface_list.prev, &surface->link);
224
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400225 interface = display->compositor->interface;
226 interface->notify_surface_create(display->compositor, surface);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400227
228 return surface;
229}
230
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500231WL_EXPORT void
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400232wl_surface_set_data(struct wl_surface *surface, void *data)
233{
234 surface->compositor_data = data;
235}
236
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500237WL_EXPORT void *
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400238wl_surface_get_data(struct wl_surface *surface)
239{
240 return surface->compositor_data;
241}
242
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400243void
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400244wl_client_destroy(struct wl_client *client);
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400245
246static void
247wl_client_demarshal(struct wl_client *client, struct wl_object *target,
248 const struct wl_method *method, size_t size)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400249{
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500250 ffi_type *types[20];
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400251 ffi_cif cif;
252 uint32_t *p, result;
253 int i;
254 union {
255 uint32_t uint32;
256 const char *string;
257 void *object;
258 uint32_t new_id;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500259 } values[20];
260 void *args[20];
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400261 struct wl_object *object;
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400262 uint32_t data[64];
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400263
264 if (method->argument_count > ARRAY_LENGTH(types)) {
265 printf("too many args (%d)\n", method->argument_count);
266 return;
267 }
268
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400269 if (sizeof data < size) {
270 printf("request too big, should malloc tmp buffer here\n");
271 return;
272 }
273
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400274 types[0] = &ffi_type_pointer;
275 values[0].object = client;
276 args[0] = &values[0];
277
278 types[1] = &ffi_type_pointer;
279 values[1].object = target;
280 args[1] = &values[1];
281
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400282 wl_connection_copy(client->connection, data, size);
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400283 p = &data[2];
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400284 for (i = 0; i < method->argument_count; i++) {
285 switch (method->arguments[i].type) {
286 case WL_ARGUMENT_UINT32:
287 types[i + 2] = &ffi_type_uint32;
288 values[i + 2].uint32 = *p;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400289 p++;
290 break;
291 case WL_ARGUMENT_STRING:
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400292 types[i + 2] = &ffi_type_pointer;
293 /* FIXME */
294 values[i + 2].uint32 = *p++;
295 break;
296 case WL_ARGUMENT_OBJECT:
297 types[i + 2] = &ffi_type_pointer;
298 object = wl_hash_lookup(&client->display->objects, *p);
299 if (object == NULL)
300 printf("unknown object (%d)\n", *p);
301 if (object->interface != method->arguments[i].data)
302 printf("wrong object type\n");
303 values[i + 2].object = object;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400304 p++;
305 break;
306 case WL_ARGUMENT_NEW_ID:
307 types[i + 2] = &ffi_type_uint32;
308 values[i + 2].new_id = *p;
309 object = wl_hash_lookup(&client->display->objects, *p);
310 if (object != NULL)
311 printf("object already exists (%d)\n", *p);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400312 p++;
313 break;
314 default:
315 printf("unknown type\n");
316 break;
317 }
318 args[i + 2] = &values[i + 2];
319 }
320
321 ffi_prep_cif(&cif, FFI_DEFAULT_ABI, method->argument_count + 2,
322 &ffi_type_uint32, types);
323 ffi_call(&cif, FFI_FN(method->func), &result, args);
324}
325
326static void
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400327wl_client_event(struct wl_client *client, struct wl_object *object, uint32_t event)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400328{
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400329 uint32_t p[2];
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400330
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400331 p[0] = object->id;
332 p[1] = event | (8 << 16);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400333 wl_connection_write(client->connection, p, sizeof p);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400334}
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400335
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400336#define WL_DISPLAY_INVALID_OBJECT 0
337#define WL_DISPLAY_INVALID_METHOD 1
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400338#define WL_DISPLAY_NO_MEMORY 2
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400339
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400340static void
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400341wl_client_connection_data(int fd, uint32_t mask, void *data)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400342{
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400343 struct wl_client *client = data;
344 struct wl_connection *connection = client->connection;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400345 const struct wl_method *method;
346 struct wl_object *object;
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400347 uint32_t p[2], opcode, size;
348 uint32_t cmask = 0;
349 int len;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400350
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400351 if (mask & WL_EVENT_READABLE)
352 cmask |= WL_CONNECTION_READABLE;
353 if (mask & WL_EVENT_WRITEABLE)
354 cmask |= WL_CONNECTION_WRITABLE;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400355
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400356 len = wl_connection_data(connection, cmask);
357 if (len < 0) {
358 wl_client_destroy(client);
359 return;
360 }
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400361
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400362 while (len > sizeof p) {
363 wl_connection_copy(connection, p, sizeof p);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400364 opcode = p[1] & 0xffff;
365 size = p[1] >> 16;
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400366 if (len < size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400367 break;
368
369 object = wl_hash_lookup(&client->display->objects,
370 p[0]);
371 if (object == NULL) {
372 wl_client_event(client, &client->display->base,
373 WL_DISPLAY_INVALID_OBJECT);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400374 wl_connection_consume(connection, size);
375 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400376 continue;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400377 }
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400378
379 if (opcode >= object->interface->method_count) {
380 wl_client_event(client, &client->display->base,
381 WL_DISPLAY_INVALID_METHOD);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400382 wl_connection_consume(connection, size);
383 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400384 continue;
385 }
386
387 method = &object->interface->methods[opcode];
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400388 wl_client_demarshal(client, object, method, size);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400389 wl_connection_consume(connection, size);
390 len -= size;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400391 }
392}
393
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400394static int
395wl_client_connection_update(struct wl_connection *connection,
396 uint32_t mask, void *data)
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400397{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400398 struct wl_client *client = data;
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400399 uint32_t emask = 0;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400400
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400401 if (mask & WL_CONNECTION_READABLE)
402 emask |= WL_EVENT_READABLE;
403 if (mask & WL_CONNECTION_WRITABLE)
404 emask |= WL_EVENT_WRITEABLE;
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400405
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400406 return wl_event_loop_update_source(client->display->loop,
407 client->source, mask);
408}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400409
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400410static void
411advertise_object(struct wl_client *client, struct wl_object *object)
412{
413 const struct wl_interface *interface;
414 static const char pad[4];
415 uint32_t length, p[2];
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400416
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400417 interface = object->interface;
418 length = strlen(interface->name);
419 p[0] = object->id;
420 p[1] = length;
421 wl_connection_write(client->connection, p, sizeof p);
422 wl_connection_write(client->connection, interface->name, length);
423 wl_connection_write(client->connection, pad, -length & 3);
424}
Kristian Høgsbergc5089872008-10-08 10:47:59 -0400425
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500426static struct wl_client *
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400427wl_client_create(struct wl_display *display, int fd)
428{
429 struct wl_client *client;
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500430 struct wl_object_ref *ref;
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400431
432 client = malloc(sizeof *client);
433 if (client == NULL)
434 return NULL;
435
436 memset(client, 0, sizeof *client);
437 client->display = display;
438 client->source = wl_event_loop_add_fd(display->loop, fd,
439 WL_EVENT_READABLE,
440 wl_client_connection_data, client);
441 client->connection = wl_connection_create(fd,
442 wl_client_connection_update,
443 client);
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400444 wl_list_init(&client->object_list);
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400445
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400446 wl_connection_write(client->connection,
447 &display->client_id_range,
448 sizeof display->client_id_range);
449 display->client_id_range += 256;
450
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500451 ref = container_of(display->global_list.next,
452 struct wl_object_ref, link);
453 while (&ref->link != &display->global_list) {
454 advertise_object(client, ref->object);
455
456 ref = container_of(ref->link.next,
457 struct wl_object_ref, link);
458 }
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400459
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500460 wl_list_insert(display->client_list.prev, &client->link);
461
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400462 return client;
463}
464
465void
466wl_client_destroy(struct wl_client *client)
467{
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400468 struct wl_object_ref *ref;
469
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400470 printf("disconnect from client %p\n", client);
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400471
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500472 wl_list_remove(&client->link);
473
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400474 while (client->object_list.next != &client->object_list) {
475 ref = container_of(client->object_list.next,
476 struct wl_object_ref, link);
477 wl_list_remove(&ref->link);
478 wl_surface_destroy(client, (struct wl_surface *) ref->object);
479 free(ref);
480 }
481
Kristian Høgsberg680f1c72008-10-08 12:48:46 -0400482 wl_event_loop_remove_source(client->display->loop, client->source);
483 wl_connection_destroy(client->connection);
484 free(client);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400485}
486
487static int
488wl_display_create_surface(struct wl_client *client,
489 struct wl_display *display, uint32_t id)
490{
491 struct wl_surface *surface;
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400492 struct wl_object_ref *ref;
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400493
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400494 surface = wl_surface_create(display, id);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400495
Kristian Høgsberg94a2e862008-10-11 21:37:55 -0400496 ref = malloc(sizeof *ref);
497 if (ref == NULL) {
498 wl_client_event(client, &display->base,
499 WL_DISPLAY_NO_MEMORY);
500 return -1;
501 }
502
503 ref->object = &surface->base;
504 wl_hash_insert(&display->objects, &surface->base);
505 wl_list_insert(client->object_list.prev, &ref->link);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400506
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400507 return 0;
508}
509
510static const struct wl_argument create_surface_arguments[] = {
511 { WL_ARGUMENT_NEW_ID }
512};
513
514static const struct wl_method display_methods[] = {
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400515 { "create_surface", wl_display_create_surface,
516 ARRAY_LENGTH(create_surface_arguments), create_surface_arguments },
517};
518
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400519static const struct wl_event display_events[] = {
520 { "invalid_object" },
521 { "invalid_method" },
522};
523
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400524static const struct wl_interface display_interface = {
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400525 "display", 1,
526 ARRAY_LENGTH(display_methods), display_methods,
527 ARRAY_LENGTH(display_events), display_events,
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400528};
529
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500530static const char input_device_file[] =
531 "/dev/input/by-id/usb-Apple__Inc._Apple_Internal_Keyboard_._Trackpad-event-mouse";
532
533static void
534wl_display_create_input_devices(struct wl_display *display)
535{
Kristian Høgsberg33a52bd2008-11-03 15:31:30 -0500536 const char *path;
537
538 path = getenv("WAYLAND_POINTER");
539 if (path == NULL)
540 path = input_device_file;
541
542 display->pointer = wl_input_device_create(display, path, 1);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500543
544 if (display->pointer != NULL)
545 wl_hash_insert(&display->objects, display->pointer);
546
547 display->pointer_x = 100;
548 display->pointer_y = 100;
549}
550
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500551static struct wl_display *
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400552wl_display_create(void)
553{
554 struct wl_display *display;
555
556 display = malloc(sizeof *display);
557 if (display == NULL)
558 return NULL;
559
560 display->loop = wl_event_loop_create();
561 if (display->loop == NULL) {
562 free(display);
563 return NULL;
564 }
565
566 display->base.id = 0;
567 display->base.interface = &display_interface;
568 wl_hash_insert(&display->objects, &display->base);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400569 wl_list_init(&display->surface_list);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500570 wl_list_init(&display->client_list);
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500571 wl_list_init(&display->global_list);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500572
573 wl_display_create_input_devices(display);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400574
575 display->client_id_range = 256; /* Gah, arbitrary... */
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400576
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500577 if (wl_display_add_global(display, &display->base)) {
578 wl_event_loop_destroy(display->loop);
579 free(display);
580 return NULL;
581 }
582
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400583 return display;
584}
585
Kristian Høgsberg14fcff72008-11-23 19:10:23 -0500586WL_EXPORT int
587wl_display_add_global(struct wl_display *display, struct wl_object *object)
588{
589 struct wl_object_ref *ref;
590
591 ref = malloc(sizeof *ref);
592 if (ref == NULL)
593 return -1;
594
595 ref->object = object;
596 wl_list_insert(display->global_list.prev, &ref->link);
597
598 return 0;
599}
600
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500601static void
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500602wl_display_send_event(struct wl_display *display, uint32_t *data, size_t size)
603{
604 struct wl_client *client;
605
606 client = container_of(display->client_list.next,
607 struct wl_client, link);
608 while (&client->link != &display->client_list) {
609 wl_connection_write(client->connection, data, size);
610
611 client = container_of(client->link.next,
612 struct wl_client, link);
613 }
614}
615
616#define WL_POINTER_MOTION 0
617#define WL_POINTER_BUTTON 1
618
619void
620wl_display_post_relative_event(struct wl_display *display,
621 struct wl_object *source, int dx, int dy)
622{
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500623 const struct wl_compositor_interface *interface;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500624 uint32_t p[4];
625
626 display->pointer_x += dx;
627 display->pointer_y += dy;
628
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500629 interface = display->compositor->interface;
630 interface->notify_pointer_motion(display->compositor, source,
631 display->pointer_x, display->pointer_y);
632
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500633 p[0] = source->id;
634 p[1] = (sizeof p << 16) | WL_POINTER_MOTION;
635 p[2] = display->pointer_x;
636 p[3] = display->pointer_y;
637
638 wl_display_send_event(display, p, sizeof p);
639}
640
641void
642wl_display_post_absolute_event(struct wl_display *display,
643 struct wl_object *source, int x, int y)
644{
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500645 const struct wl_compositor_interface *interface;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500646 uint32_t p[4];
647
648 display->pointer_x = x;
649 display->pointer_y = y;
650
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500651 interface = display->compositor->interface;
652 interface->notify_pointer_motion(display->compositor, source,
653 display->pointer_x, display->pointer_y);
654
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500655 p[0] = source->id;
656 p[1] = (sizeof p << 16) | WL_POINTER_MOTION;
657 p[2] = display->pointer_x;
658 p[3] = display->pointer_y;
659
660 wl_display_send_event(display, p, sizeof p);
661}
662
663void
664wl_display_post_button_event(struct wl_display *display,
665 struct wl_object *source, int button, int state)
666{
667 uint32_t p[4];
668
669 p[0] = source->id;
670 p[1] = (sizeof p << 16) | WL_POINTER_BUTTON;
671 p[2] = button;
672 p[3] = state;
673
674 wl_display_send_event(display, p, sizeof p);
675}
676
677void
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400678wl_display_set_compositor(struct wl_display *display,
679 struct wl_compositor *compositor)
680{
681 display->compositor = compositor;
682}
683
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500684WL_EXPORT struct wl_event_loop *
Kristian Høgsberg5ebb3172008-10-11 19:21:35 -0400685wl_display_get_event_loop(struct wl_display *display)
686{
687 return display->loop;
688}
689
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500690static void
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400691wl_display_run(struct wl_display *display)
692{
693 while (1)
694 wl_event_loop_wait(display->loop);
695}
696
697/* The plan here is to generate a random anonymous socket name and
698 * advertise that through a service on the session dbus.
699 */
700static const char socket_name[] = "\0wayland";
701
702static void
703socket_data(int fd, uint32_t mask, void *data)
704{
705 struct wl_display *display = data;
706 struct sockaddr_un name;
707 socklen_t length;
708 int client_fd;
709
710 length = sizeof name;
711 client_fd = accept (fd, (struct sockaddr *) &name, &length);
712 if (client_fd < 0)
713 fprintf(stderr, "failed to accept\n");
714
715 wl_client_create(display, client_fd);
716}
717
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500718static int
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400719wl_display_add_socket(struct wl_display *display)
720{
721 struct sockaddr_un name;
722 int sock;
723 socklen_t size;
724
725 sock = socket(PF_LOCAL, SOCK_STREAM, 0);
726 if (sock < 0)
727 return -1;
728
729 name.sun_family = AF_LOCAL;
730 memcpy(name.sun_path, socket_name, sizeof socket_name);
731
732 size = offsetof (struct sockaddr_un, sun_path) + sizeof socket_name;
733 if (bind(sock, (struct sockaddr *) &name, size) < 0)
734 return -1;
735
736 if (listen(sock, 1) < 0)
737 return -1;
738
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400739 wl_event_loop_add_fd(display->loop, sock,
740 WL_EVENT_READABLE,
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400741 socket_data, display);
742
743 return 0;
744}
745
746
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400747struct wl_surface_iterator {
748 struct wl_list *head;
749 struct wl_surface *surface;
750 uint32_t mask;
751};
752
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500753WL_EXPORT struct wl_surface_iterator *
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400754wl_surface_iterator_create(struct wl_display *display, uint32_t mask)
755{
756 struct wl_surface_iterator *iterator;
757
758 iterator = malloc(sizeof *iterator);
759 if (iterator == NULL)
760 return NULL;
761
762 iterator->head = &display->surface_list;
763 iterator->surface = container_of(display->surface_list.next,
764 struct wl_surface, link);
765 iterator->mask = mask;
766
767 return iterator;
768}
769
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500770WL_EXPORT int
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400771wl_surface_iterator_next(struct wl_surface_iterator *iterator,
772 struct wl_surface **surface)
773{
774 if (&iterator->surface->link == iterator->head)
775 return 0;
776
777 *surface = iterator->surface;
778 iterator->surface = container_of(iterator->surface->link.next,
779 struct wl_surface, link);
780
781 return 1;
782}
783
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500784WL_EXPORT void
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400785wl_surface_iterator_destroy(struct wl_surface_iterator *iterator)
786{
787 free(iterator);
788}
789
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500790static int
791load_compositor(struct wl_display *display, const char *path)
792{
793 struct wl_compositor *(*create)(struct wl_display *display);
794 struct wl_compositor *compositor;
795 void *p;
796
797 p = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
798 if (p == NULL) {
799 fprintf(stderr, "failed to open compositor %s: %s\n",
800 path, dlerror());
801 return -1;
802 }
803
804 create = dlsym(p, "wl_compositor_create");
805 if (create == NULL) {
806 fprintf(stderr, "failed to look up compositor constructor\n");
807 return -1;
808 }
809
810 compositor = create(display);
811 if (compositor == NULL) {
812 fprintf(stderr, "couldn't create compositor\n");
813 return -1;
814 }
815
816 wl_display_set_compositor(display, compositor);
817
818 return 0;
819}
820
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400821int main(int argc, char *argv[])
822{
823 struct wl_display *display;
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500824 const char *compositor = "./egl-compositor.so";
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400825
826 display = wl_display_create();
827
828 if (wl_display_add_socket(display)) {
829 fprintf(stderr, "failed to add socket: %m\n");
830 exit(EXIT_FAILURE);
831 }
832
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500833 if (argc == 2)
834 compositor = argv[1];
835 load_compositor(display, compositor);
Kristian Høgsberg97f1ebe2008-09-30 09:46:10 -0400836
837 wl_display_run(display);
838
839 return 0;
840}