blob: e60d028f540290cb5a2f63b1ade8417d219b2a82 [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;
98 struct wl_hash *objects;
99 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
155 global = container_of(display->global_list.next,
156 struct wl_global, link);
157 while (&global->link != &display->global_list) {
158 if (global->proxy != NULL)
159 (*handler)(display, &global->proxy->base, data);
160
161 global = container_of(global->link.next,
162 struct wl_global, link);
163 }
164
165 return listener;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500166}
167
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500168WL_EXPORT void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500169wl_display_remove_global_listener(struct wl_display *display,
170 struct wl_global_listener *listener)
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500171{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500172 wl_list_remove(&listener->link);
173 free(listener);
174}
175
176static struct wl_proxy *
177wl_proxy_create_for_global(struct wl_display *display,
178 struct wl_global *global,
179 const struct wl_interface *interface)
180{
181 struct wl_proxy *proxy;
182 struct wl_global_listener *listener;
183
184 proxy = malloc(sizeof *proxy);
185 if (proxy == NULL)
186 return NULL;
187
188 proxy->base.interface = interface;
189 proxy->base.id = global->id;
190 proxy->display = display;
191 global->proxy = proxy;
192 wl_list_init(&proxy->listener_list);
193 wl_hash_insert(display->objects, &proxy->base);
194
195 listener = container_of(display->global_listener_list.next,
196 struct wl_global_listener, link);
197 while (&listener->link != &display->global_listener_list) {
198 (*listener->handler)(display, &proxy->base, listener->data);
199 listener = container_of(listener->link.next,
200 struct wl_global_listener, link);
201 }
202
203 return proxy;
204}
205
206static int
207wl_proxy_add_listener(struct wl_proxy *proxy, void (**implementation)(void), void *data)
208{
209 struct wl_listener *listener;
210
211 listener = malloc(sizeof *listener);
212 if (listener == NULL)
213 return -1;
214
215 listener->implementation = (void (**)(void)) implementation;
216 listener->data = data;
217 wl_list_insert(proxy->listener_list.prev, &listener->link);
218
219 return 0;
220}
221
222static void
223wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
224{
225 va_list ap;
226
227 va_start(ap, opcode);
228 wl_connection_vmarshal(proxy->display->connection,
229 &proxy->base, opcode, ap,
230 &proxy->base.interface->methods[opcode]);
231 va_end(ap);
232}
233
234WL_EXPORT int
235wl_output_add_listener(struct wl_output *output,
236 const struct wl_output_listener *listener,
237 void *data)
238{
239 return wl_proxy_add_listener(&output->proxy,
240 (void (**)(void)) listener, data);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500241}
242
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500243static void
244add_visual(struct wl_display *display, struct wl_global *global)
245{
246 struct wl_visual *visual;
247
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500248 visual = (struct wl_visual *)
249 wl_proxy_create_for_global(display, global,
250 &wl_visual_interface);
251 if (display->argb_visual == NULL)
252 display->argb_visual = visual;
253 else if (display->premultiplied_argb_visual == NULL)
254 display->premultiplied_argb_visual = visual;
255 else
256 display->rgb_visual = visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500257}
258
259WL_EXPORT struct wl_visual *
260wl_display_get_argb_visual(struct wl_display *display)
261{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500262 return display->argb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500263}
264
265WL_EXPORT struct wl_visual *
266wl_display_get_premultiplied_argb_visual(struct wl_display *display)
267{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500268 return display->premultiplied_argb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500269}
270
271WL_EXPORT struct wl_visual *
272wl_display_get_rgb_visual(struct wl_display *display)
273{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500274 return display->rgb_visual;
275}
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500276
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500277WL_EXPORT int
278wl_input_device_add_listener(struct wl_input_device *input_device,
279 const struct wl_input_device_listener *listener,
280 void *data)
281{
282 return wl_proxy_add_listener(&input_device->proxy,
283 (void (**)(void)) listener, data);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500284}
285
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500286static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500287display_handle_invalid_object(void *data,
288 struct wl_display *display, uint32_t id)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500289{
290 fprintf(stderr, "sent request to invalid object\n");
291}
292
293static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500294display_handle_invalid_method(void *data,
295 struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500296 uint32_t id, uint32_t opcode)
297{
298 fprintf(stderr, "sent invalid request opcode\n");
299}
300
301static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500302display_handle_no_memory(void *data,
303 struct wl_display *display)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500304{
305 fprintf(stderr, "server out of memory\n");
306}
307
308static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500309display_handle_global(void *data,
310 struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500311 uint32_t id, const char *interface, uint32_t version)
312{
313 struct wl_global *global;
314
315 global = malloc(sizeof *global);
316 if (global == NULL)
317 return;
318
319 global->id = id;
320 global->interface = strdup(interface);
321 global->version = version;
322 wl_list_insert(display->global_list.prev, &global->link);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500323
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500324 if (strcmp(global->interface, "display") == 0)
325 wl_hash_insert(display->objects, &display->proxy.base);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500326 else if (strcmp(global->interface, "compositor") == 0)
327 display->compositor = (struct wl_compositor *)
328 wl_proxy_create_for_global(display, global,
329 &wl_compositor_interface);
330 else if (strcmp(global->interface, "visual") == 0)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500331 add_visual(display, global);
332 else if (strcmp(global->interface, "output") == 0)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500333 wl_proxy_create_for_global(display, global,
334 &wl_output_interface);
335 else if (strcmp(global->interface, "input_device") == 0)
336 wl_proxy_create_for_global(display, global,
337 &wl_input_device_interface);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500338}
339
340static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500341display_handle_range(void *data,
342 struct wl_display *display, uint32_t range)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500343{
344 display->next_range = range;
345}
346
347struct wl_display_listener {
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500348 void (*invalid_object)(void *data,
349 struct wl_display *display, uint32_t id);
350 void (*invalid_method)(void *data, struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500351 uint32_t id, uint32_t opcode);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500352 void (*no_memory)(void *data,
353 struct wl_display *display);
354 void (*global)(void *data, struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500355 uint32_t id, const char *interface, uint32_t version);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500356 void (*range)(void *data,
357 struct wl_display *display, uint32_t range);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500358};
359
360static const struct wl_display_listener display_listener = {
361 display_handle_invalid_object,
362 display_handle_invalid_method,
363 display_handle_no_memory,
364 display_handle_global,
365 display_handle_range
366};
367
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500368WL_EXPORT struct wl_display *
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500369wl_display_create(const char *name, size_t name_size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400370{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400371 struct wl_display *display;
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500372 struct sockaddr_un addr;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400373 socklen_t size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400374
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400375 display = malloc(sizeof *display);
376 if (display == NULL)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400377 return NULL;
378
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400379 memset(display, 0, sizeof *display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400380 display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
381 if (display->fd < 0) {
382 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400383 return NULL;
384 }
385
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500386 addr.sun_family = AF_LOCAL;
387 memcpy(addr.sun_path, name, name_size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400388
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500389 size = offsetof (struct sockaddr_un, sun_path) + name_size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400390
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500391 if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400392 close(display->fd);
393 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400394 return NULL;
395 }
396
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500397 display->objects = wl_hash_create();
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500398 wl_list_init(&display->global_list);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500399 wl_list_init(&display->global_listener_list);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400400
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500401 display->proxy.base.interface = &wl_display_interface;
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500402 display->proxy.base.id = 1;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500403 display->proxy.display = display;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500404 wl_list_init(&display->proxy.listener_list);
405
406 display->listener.implementation = (void(**)(void)) &display_listener;
407 wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400408
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400409 display->connection = wl_connection_create(display->fd,
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500410 connection_update,
411 display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400412
413 return display;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400414}
415
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500416WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400417wl_display_destroy(struct wl_display *display)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400418{
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400419 wl_connection_destroy(display->connection);
420 close(display->fd);
421 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400422}
423
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500424WL_EXPORT uint32_t
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500425wl_display_get_object_id(struct wl_display *display,
426 const char *interface, uint32_t version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500427{
428 struct wl_global *global;
429
430 global = container_of(display->global_list.next,
431 struct wl_global, link);
432 while (&global->link != &display->global_list) {
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500433 if (strcmp(global->interface, interface) == 0 &&
434 global->version >= version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500435 return global->id;
436
437 global = container_of(global->link.next,
438 struct wl_global, link);
439 }
440
441 return 0;
442}
443
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500444WL_EXPORT int
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500445wl_display_get_fd(struct wl_display *display,
446 wl_display_update_func_t update, void *data)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400447{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500448 display->update = update;
449 display->update_data = data;
450
451 display->update(display->mask, display->update_data);
452
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400453 return display->fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400454}
455
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500456static void
Kristian Høgsberg40979232008-11-25 22:40:39 -0500457handle_event(struct wl_display *display,
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500458 uint32_t id, uint32_t opcode, uint32_t size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400459{
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500460 uint32_t p[32];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500461 struct wl_listener *listener;
462 struct wl_proxy *proxy;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400463
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500464 wl_connection_copy(display->connection, p, size);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500465 if (id == 1)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500466 proxy = &display->proxy;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500467 else
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500468 proxy = (struct wl_proxy *) wl_hash_lookup(display->objects, id);
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500469
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500470 if (proxy != NULL) {
471 if (wl_list_empty(&proxy->listener_list)) {
472 printf("proxy found for object %d, opcode %d, but no listeners\n",
473 id, opcode);
474 }
475
476 listener = container_of(proxy->listener_list.next,
477 struct wl_listener, link);
478 while (&listener->link != &proxy->listener_list) {
479
480 wl_connection_demarshal(display->connection,
481 size,
482 display->objects,
483 listener->implementation[opcode],
484 listener->data,
485 &proxy->base,
486 &proxy->base.interface->events[opcode]);
487
488 listener = container_of(listener->link.next,
489 struct wl_listener, link);
490 }
491 }
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500492
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500493 wl_connection_consume(display->connection, size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400494}
495
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500496WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400497wl_display_iterate(struct wl_display *display, uint32_t mask)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400498{
Kristian Høgsberg40979232008-11-25 22:40:39 -0500499 uint32_t p[2], object, opcode, size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400500 int len;
501
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400502 len = wl_connection_data(display->connection, mask);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400503 while (len > 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400504 if (len < sizeof p)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400505 break;
506
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400507 wl_connection_copy(display->connection, p, sizeof p);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500508 object = p[0];
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400509 opcode = p[1] & 0xffff;
510 size = p[1] >> 16;
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400511 if (len < size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400512 break;
513
Kristian Høgsberg40979232008-11-25 22:40:39 -0500514 handle_event(display, object, opcode, size);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500515 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400516 }
517
518 if (len < 0) {
519 fprintf(stderr, "read error: %m\n");
520 exit(EXIT_FAILURE);
521 }
522}
523
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500524WL_EXPORT uint32_t
525wl_display_allocate_id(struct wl_display *display)
526{
Kristian Høgsberg97079ad2008-12-21 22:45:33 -0500527 if (display->id_count == 0) {
528 display->id_count = 256;
529 display->id = display->next_range;
530 }
531
532 display->id_count--;
533
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500534 return display->id++;
535}
536
537WL_EXPORT void
538wl_display_write(struct wl_display *display, const void *data, size_t count)
539{
540 wl_connection_write(display->connection, data, count);
541}
542
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500543WL_EXPORT struct wl_compositor *
544wl_display_get_compositor(struct wl_display *display)
545{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500546 return display->compositor;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500547}
548
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500549WL_EXPORT int
550wl_compositor_add_listener(struct wl_compositor *compositor,
551 const struct wl_compositor_listener *listener,
552 void *data)
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500553{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500554 return wl_proxy_add_listener(&compositor->proxy,
555 (void (**)(void)) listener, data);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500556}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400557
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500558WL_EXPORT struct wl_surface *
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500559wl_compositor_create_surface(struct wl_compositor *compositor)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400560{
561 struct wl_surface *surface;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400562
563 surface = malloc(sizeof *surface);
564 if (surface == NULL)
565 return NULL;
566
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500567 surface->proxy.base.interface = &wl_surface_interface;
568 surface->proxy.base.id = wl_display_allocate_id(compositor->proxy.display);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500569 surface->proxy.display = compositor->proxy.display;
Kristian Høgsberg2c837482009-02-22 20:58:29 -0500570 wl_hash_insert(compositor->proxy.display->objects, &surface->proxy.base);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500571 wl_proxy_marshal(&compositor->proxy,
572 WL_COMPOSITOR_CREATE_SURFACE, surface);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400573
574 return surface;
575}
576
Kristian Høgsberg40979232008-11-25 22:40:39 -0500577WL_EXPORT void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500578wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
Kristian Høgsberg40979232008-11-25 22:40:39 -0500579{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500580 wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500581}
582
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500583WL_EXPORT void
584wl_surface_destroy(struct wl_surface *surface)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400585{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500586 wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400587}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400588
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500589WL_EXPORT void
590wl_surface_attach(struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500591 int32_t width, int32_t height, uint32_t stride,
592 struct wl_visual *visual)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400593{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500594 wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
595 name, width, height, stride, visual);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400596}
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400597
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500598WL_EXPORT void
599wl_surface_map(struct wl_surface *surface,
600 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400601{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500602 wl_proxy_marshal(&surface->proxy,
603 WL_SURFACE_MAP, x, y, width, height);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400604}
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500605
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500606WL_EXPORT void
607wl_surface_copy(struct wl_surface *surface, int32_t dst_x, int32_t dst_y,
608 uint32_t name, uint32_t stride,
609 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500610{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500611 wl_proxy_marshal(&surface->proxy, WL_SURFACE_COPY,
612 dst_x, dst_y, name, stride, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500613}
614
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500615WL_EXPORT void
616wl_surface_damage(struct wl_surface *surface,
617 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500618{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500619 wl_proxy_marshal(&surface->proxy,
620 WL_SURFACE_DAMAGE, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500621}
Kristian Høgsbergecf65fe2009-09-18 09:49:21 -0400622
623WL_EXPORT void
624wl_surface_set_user_data(struct wl_surface *surface, void *user_data)
625{
626 surface->proxy.user_data = user_data;
627}
628
629WL_EXPORT void *
630wl_surface_get_user_data(struct wl_surface *surface)
631{
632 return surface->proxy.user_data;
633}