blob: 8cabbb27e2fea972d62b3650e1ab8804d7dc79dc [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øgsberga67a71a2008-10-07 10:10:36 -040023#include <stdlib.h>
24#include <stdint.h>
25#include <stddef.h>
26#include <stdio.h>
27#include <errno.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/socket.h>
31#include <sys/un.h>
32#include <ctype.h>
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050033#include <assert.h>
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040034#include <sys/poll.h>
35
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050036#include "wayland-protocol.h"
Kristian Høgsberg427524a2008-10-08 13:32:07 -040037#include "connection.h"
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050038#include "wayland-util.h"
Kristian Høgsberg427524a2008-10-08 13:32:07 -040039#include "wayland-client.h"
40
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040041static const char socket_name[] = "\0wayland";
42
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050043struct wl_global {
44 uint32_t id;
45 char *interface;
Kristian Høgsbergbf967b42008-12-21 20:25:16 -050046 uint32_t version;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050047 struct wl_proxy *proxy;
48 struct wl_list link;
49};
50
51struct wl_global_listener {
52 wl_display_global_func_t handler;
53 void *data;
54 struct wl_list link;
55};
56
57struct wl_listener {
58 void (**implementation)(void);
59 void *data;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050060 struct wl_list link;
61};
62
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040063struct wl_proxy {
Kristian Høgsbergfabd4392008-12-22 18:06:49 -050064 struct wl_object base;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050065 struct wl_display *display;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050066 struct wl_list listener_list;
Kristian Høgsbergecf65fe2009-09-18 09:49:21 -040067 void *user_data;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040068};
69
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050070struct wl_compositor {
71 struct wl_proxy proxy;
72};
73
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040074struct wl_surface {
75 struct wl_proxy proxy;
76};
77
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050078struct wl_visual {
79 struct wl_proxy proxy;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050080};
81
Kristian Høgsberg12ea62e2008-12-22 16:37:07 -050082struct wl_output {
83 struct wl_proxy proxy;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050084 struct wl_listener listener;
Kristian Høgsberg12ea62e2008-12-22 16:37:07 -050085 int32_t width, height;
86};
87
Kristian Høgsberg94448c02008-12-30 11:03:33 -050088struct wl_input_device {
89 struct wl_proxy proxy;
90};
91
92struct wl_display {
93 struct wl_proxy proxy;
94 struct wl_connection *connection;
95 int fd;
96 uint32_t id, id_count, next_range;
97 uint32_t mask;
Kristian Høgsbergf52e03f2010-02-26 11:42:59 -050098 struct wl_hash_table *objects;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050099 struct wl_list global_list;
100 struct wl_listener listener;
101 struct wl_list global_listener_list;
102
103 struct wl_visual *argb_visual;
104 struct wl_visual *premultiplied_argb_visual;
105 struct wl_visual *rgb_visual;
106
107 wl_display_update_func_t update;
108 void *update_data;
109
110 wl_display_global_func_t global_handler;
111 void *global_handler_data;
112
113 struct wl_compositor *compositor;
114};
115
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500116static int
117connection_update(struct wl_connection *connection,
118 uint32_t mask, void *data)
119{
120 struct wl_display *display = data;
121
122 display->mask = mask;
123 if (display->update)
124 return display->update(display->mask,
125 display->update_data);
126
127 return 0;
128}
129
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500130WL_EXPORT int
131wl_object_implements(struct wl_object *object,
132 const char *interface, int version)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500133{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500134 return strcmp(object->interface->name, interface) == 0 &&
135 object->interface->version >= version;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500136}
137
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500138WL_EXPORT struct wl_global_listener *
139wl_display_add_global_listener(struct wl_display *display,
140 wl_display_global_func_t handler, void *data)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500141{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500142 struct wl_global_listener *listener;
143 struct wl_global *global;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500144
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500145 listener = malloc(sizeof *listener);
146 if (listener == NULL)
147 return NULL;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500148
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500149 listener->handler = handler;
150 listener->data = data;
151 wl_list_insert(display->global_listener_list.prev, &listener->link);
152
153 /* FIXME: Need a destructor for void *data? */
154
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500155 wl_list_for_each(global, &display->global_list, link)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500156 if (global->proxy != NULL)
157 (*handler)(display, &global->proxy->base, data);
158
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500159 return listener;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500160}
161
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500162WL_EXPORT void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500163wl_display_remove_global_listener(struct wl_display *display,
164 struct wl_global_listener *listener)
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500165{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500166 wl_list_remove(&listener->link);
167 free(listener);
168}
169
170static struct wl_proxy *
171wl_proxy_create_for_global(struct wl_display *display,
172 struct wl_global *global,
173 const struct wl_interface *interface)
174{
175 struct wl_proxy *proxy;
176 struct wl_global_listener *listener;
177
178 proxy = malloc(sizeof *proxy);
179 if (proxy == NULL)
180 return NULL;
181
182 proxy->base.interface = interface;
183 proxy->base.id = global->id;
184 proxy->display = display;
185 global->proxy = proxy;
186 wl_list_init(&proxy->listener_list);
Kristian Høgsbergf52e03f2010-02-26 11:42:59 -0500187 wl_hash_table_insert(display->objects, proxy->base.id, proxy);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500188
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500189 wl_list_for_each(listener, &display->global_listener_list, link)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500190 (*listener->handler)(display, &proxy->base, listener->data);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500191
192 return proxy;
193}
194
195static int
196wl_proxy_add_listener(struct wl_proxy *proxy, void (**implementation)(void), void *data)
197{
198 struct wl_listener *listener;
199
200 listener = malloc(sizeof *listener);
201 if (listener == NULL)
202 return -1;
203
204 listener->implementation = (void (**)(void)) implementation;
205 listener->data = data;
206 wl_list_insert(proxy->listener_list.prev, &listener->link);
207
208 return 0;
209}
210
211static void
212wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
213{
214 va_list ap;
215
216 va_start(ap, opcode);
217 wl_connection_vmarshal(proxy->display->connection,
218 &proxy->base, opcode, ap,
219 &proxy->base.interface->methods[opcode]);
220 va_end(ap);
221}
222
223WL_EXPORT int
224wl_output_add_listener(struct wl_output *output,
225 const struct wl_output_listener *listener,
226 void *data)
227{
228 return wl_proxy_add_listener(&output->proxy,
229 (void (**)(void)) listener, data);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500230}
231
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500232static void
233add_visual(struct wl_display *display, struct wl_global *global)
234{
235 struct wl_visual *visual;
236
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500237 visual = (struct wl_visual *)
238 wl_proxy_create_for_global(display, global,
239 &wl_visual_interface);
240 if (display->argb_visual == NULL)
241 display->argb_visual = visual;
242 else if (display->premultiplied_argb_visual == NULL)
243 display->premultiplied_argb_visual = visual;
244 else
245 display->rgb_visual = visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500246}
247
248WL_EXPORT struct wl_visual *
249wl_display_get_argb_visual(struct wl_display *display)
250{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500251 return display->argb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500252}
253
254WL_EXPORT struct wl_visual *
255wl_display_get_premultiplied_argb_visual(struct wl_display *display)
256{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500257 return display->premultiplied_argb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500258}
259
260WL_EXPORT struct wl_visual *
261wl_display_get_rgb_visual(struct wl_display *display)
262{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500263 return display->rgb_visual;
264}
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500265
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500266WL_EXPORT int
267wl_input_device_add_listener(struct wl_input_device *input_device,
268 const struct wl_input_device_listener *listener,
269 void *data)
270{
271 return wl_proxy_add_listener(&input_device->proxy,
272 (void (**)(void)) listener, data);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500273}
274
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500275static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500276display_handle_invalid_object(void *data,
277 struct wl_display *display, uint32_t id)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500278{
279 fprintf(stderr, "sent request to invalid object\n");
280}
281
282static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500283display_handle_invalid_method(void *data,
284 struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500285 uint32_t id, uint32_t opcode)
286{
287 fprintf(stderr, "sent invalid request opcode\n");
288}
289
290static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500291display_handle_no_memory(void *data,
292 struct wl_display *display)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500293{
294 fprintf(stderr, "server out of memory\n");
295}
296
297static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500298display_handle_global(void *data,
299 struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500300 uint32_t id, const char *interface, uint32_t version)
301{
302 struct wl_global *global;
303
304 global = malloc(sizeof *global);
305 if (global == NULL)
306 return;
307
308 global->id = id;
309 global->interface = strdup(interface);
310 global->version = version;
311 wl_list_insert(display->global_list.prev, &global->link);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500312
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500313 if (strcmp(global->interface, "display") == 0)
Kristian Høgsbergf52e03f2010-02-26 11:42:59 -0500314 wl_hash_table_insert(display->objects,
315 id, &display->proxy.base);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500316 else if (strcmp(global->interface, "compositor") == 0)
317 display->compositor = (struct wl_compositor *)
318 wl_proxy_create_for_global(display, global,
319 &wl_compositor_interface);
320 else if (strcmp(global->interface, "visual") == 0)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500321 add_visual(display, global);
322 else if (strcmp(global->interface, "output") == 0)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500323 wl_proxy_create_for_global(display, global,
324 &wl_output_interface);
325 else if (strcmp(global->interface, "input_device") == 0)
326 wl_proxy_create_for_global(display, global,
327 &wl_input_device_interface);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500328}
329
330static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500331display_handle_range(void *data,
332 struct wl_display *display, uint32_t range)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500333{
334 display->next_range = range;
335}
336
337struct wl_display_listener {
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500338 void (*invalid_object)(void *data,
339 struct wl_display *display, uint32_t id);
340 void (*invalid_method)(void *data, struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500341 uint32_t id, uint32_t opcode);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500342 void (*no_memory)(void *data,
343 struct wl_display *display);
344 void (*global)(void *data, struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500345 uint32_t id, const char *interface, uint32_t version);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500346 void (*range)(void *data,
347 struct wl_display *display, uint32_t range);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500348};
349
350static const struct wl_display_listener display_listener = {
351 display_handle_invalid_object,
352 display_handle_invalid_method,
353 display_handle_no_memory,
354 display_handle_global,
355 display_handle_range
356};
357
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500358WL_EXPORT struct wl_display *
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500359wl_display_create(const char *name, size_t name_size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400360{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400361 struct wl_display *display;
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500362 struct sockaddr_un addr;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400363 socklen_t size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400364
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400365 display = malloc(sizeof *display);
366 if (display == NULL)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400367 return NULL;
368
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400369 memset(display, 0, sizeof *display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400370 display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
371 if (display->fd < 0) {
372 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400373 return NULL;
374 }
375
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500376 addr.sun_family = AF_LOCAL;
377 memcpy(addr.sun_path, name, name_size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400378
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500379 size = offsetof (struct sockaddr_un, sun_path) + name_size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400380
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500381 if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400382 close(display->fd);
383 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400384 return NULL;
385 }
386
Kristian Høgsbergf52e03f2010-02-26 11:42:59 -0500387 display->objects = wl_hash_table_create();
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500388 wl_list_init(&display->global_list);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500389 wl_list_init(&display->global_listener_list);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400390
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500391 display->proxy.base.interface = &wl_display_interface;
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500392 display->proxy.base.id = 1;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500393 display->proxy.display = display;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500394 wl_list_init(&display->proxy.listener_list);
395
396 display->listener.implementation = (void(**)(void)) &display_listener;
397 wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400398
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400399 display->connection = wl_connection_create(display->fd,
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500400 connection_update,
401 display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400402
403 return display;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400404}
405
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500406WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400407wl_display_destroy(struct wl_display *display)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400408{
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400409 wl_connection_destroy(display->connection);
410 close(display->fd);
411 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400412}
413
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500414WL_EXPORT uint32_t
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500415wl_display_get_object_id(struct wl_display *display,
416 const char *interface, uint32_t version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500417{
418 struct wl_global *global;
419
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500420 wl_list_for_each(global, &display->global_list, link)
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500421 if (strcmp(global->interface, interface) == 0 &&
422 global->version >= version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500423 return global->id;
424
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500425 return 0;
426}
427
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500428WL_EXPORT int
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500429wl_display_get_fd(struct wl_display *display,
430 wl_display_update_func_t update, void *data)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400431{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500432 display->update = update;
433 display->update_data = data;
434
435 display->update(display->mask, display->update_data);
436
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400437 return display->fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400438}
439
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500440static void
Kristian Høgsberg40979232008-11-25 22:40:39 -0500441handle_event(struct wl_display *display,
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500442 uint32_t id, uint32_t opcode, uint32_t size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400443{
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500444 uint32_t p[32];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500445 struct wl_listener *listener;
446 struct wl_proxy *proxy;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400447
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500448 wl_connection_copy(display->connection, p, size);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500449 if (id == 1)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500450 proxy = &display->proxy;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500451 else
Kristian Høgsbergf52e03f2010-02-26 11:42:59 -0500452 proxy = (struct wl_proxy *)
453 wl_hash_table_lookup(display->objects, id);
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500454
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500455 if (proxy != NULL) {
456 if (wl_list_empty(&proxy->listener_list)) {
457 printf("proxy found for object %d, opcode %d, but no listeners\n",
458 id, opcode);
459 }
460
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500461 wl_list_for_each(listener, &proxy->listener_list, link)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500462 wl_connection_demarshal(display->connection,
463 size,
464 display->objects,
465 listener->implementation[opcode],
466 listener->data,
467 &proxy->base,
468 &proxy->base.interface->events[opcode]);
469
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500470 }
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500471
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500472 wl_connection_consume(display->connection, size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400473}
474
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500475WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400476wl_display_iterate(struct wl_display *display, uint32_t mask)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400477{
Kristian Høgsberg40979232008-11-25 22:40:39 -0500478 uint32_t p[2], object, opcode, size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400479 int len;
480
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400481 len = wl_connection_data(display->connection, mask);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400482 while (len > 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400483 if (len < sizeof p)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400484 break;
485
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400486 wl_connection_copy(display->connection, p, sizeof p);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500487 object = p[0];
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400488 opcode = p[1] & 0xffff;
489 size = p[1] >> 16;
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400490 if (len < size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400491 break;
492
Kristian Høgsberg40979232008-11-25 22:40:39 -0500493 handle_event(display, object, opcode, size);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500494 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400495 }
496
497 if (len < 0) {
498 fprintf(stderr, "read error: %m\n");
499 exit(EXIT_FAILURE);
500 }
501}
502
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500503WL_EXPORT uint32_t
504wl_display_allocate_id(struct wl_display *display)
505{
Kristian Høgsberg97079ad2008-12-21 22:45:33 -0500506 if (display->id_count == 0) {
507 display->id_count = 256;
508 display->id = display->next_range;
509 }
510
511 display->id_count--;
512
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500513 return display->id++;
514}
515
516WL_EXPORT void
517wl_display_write(struct wl_display *display, const void *data, size_t count)
518{
519 wl_connection_write(display->connection, data, count);
520}
521
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500522WL_EXPORT struct wl_compositor *
523wl_display_get_compositor(struct wl_display *display)
524{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500525 return display->compositor;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500526}
527
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500528WL_EXPORT int
529wl_compositor_add_listener(struct wl_compositor *compositor,
530 const struct wl_compositor_listener *listener,
531 void *data)
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500532{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500533 return wl_proxy_add_listener(&compositor->proxy,
534 (void (**)(void)) listener, data);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500535}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400536
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500537WL_EXPORT struct wl_surface *
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500538wl_compositor_create_surface(struct wl_compositor *compositor)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400539{
540 struct wl_surface *surface;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400541
542 surface = malloc(sizeof *surface);
543 if (surface == NULL)
544 return NULL;
545
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500546 surface->proxy.base.interface = &wl_surface_interface;
547 surface->proxy.base.id = wl_display_allocate_id(compositor->proxy.display);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500548 surface->proxy.display = compositor->proxy.display;
Kristian Høgsbergf52e03f2010-02-26 11:42:59 -0500549 wl_hash_table_insert(compositor->proxy.display->objects,
550 surface->proxy.base.id, surface);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500551 wl_proxy_marshal(&compositor->proxy,
552 WL_COMPOSITOR_CREATE_SURFACE, surface);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400553
554 return surface;
555}
556
Kristian Høgsberg40979232008-11-25 22:40:39 -0500557WL_EXPORT void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500558wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
Kristian Høgsberg40979232008-11-25 22:40:39 -0500559{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500560 wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500561}
562
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500563WL_EXPORT void
564wl_surface_destroy(struct wl_surface *surface)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400565{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500566 wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400567}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400568
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500569WL_EXPORT void
570wl_surface_attach(struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500571 int32_t width, int32_t height, uint32_t stride,
572 struct wl_visual *visual)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400573{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500574 wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
575 name, width, height, stride, visual);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400576}
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400577
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500578WL_EXPORT void
579wl_surface_map(struct wl_surface *surface,
580 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400581{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500582 wl_proxy_marshal(&surface->proxy,
583 WL_SURFACE_MAP, x, y, width, height);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400584}
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500585
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500586WL_EXPORT void
587wl_surface_copy(struct wl_surface *surface, int32_t dst_x, int32_t dst_y,
588 uint32_t name, uint32_t stride,
589 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500590{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500591 wl_proxy_marshal(&surface->proxy, WL_SURFACE_COPY,
592 dst_x, dst_y, name, stride, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500593}
594
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500595WL_EXPORT void
596wl_surface_damage(struct wl_surface *surface,
597 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500598{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500599 wl_proxy_marshal(&surface->proxy,
600 WL_SURFACE_DAMAGE, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500601}
Kristian Høgsbergecf65fe2009-09-18 09:49:21 -0400602
603WL_EXPORT void
604wl_surface_set_user_data(struct wl_surface *surface, void *user_data)
605{
606 surface->proxy.user_data = user_data;
607}
608
609WL_EXPORT void *
610wl_surface_get_user_data(struct wl_surface *surface)
611{
612 return surface->proxy.user_data;
613}