blob: 80e283248fd1d9423fd3ab4b2ab15b4f4ab571d2 [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
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);
187 wl_hash_insert(display->objects, &proxy->base);
188
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)
314 wl_hash_insert(display->objects, &display->proxy.base);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500315 else if (strcmp(global->interface, "compositor") == 0)
316 display->compositor = (struct wl_compositor *)
317 wl_proxy_create_for_global(display, global,
318 &wl_compositor_interface);
319 else if (strcmp(global->interface, "visual") == 0)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500320 add_visual(display, global);
321 else if (strcmp(global->interface, "output") == 0)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500322 wl_proxy_create_for_global(display, global,
323 &wl_output_interface);
324 else if (strcmp(global->interface, "input_device") == 0)
325 wl_proxy_create_for_global(display, global,
326 &wl_input_device_interface);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500327}
328
329static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500330display_handle_range(void *data,
331 struct wl_display *display, uint32_t range)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500332{
333 display->next_range = range;
334}
335
336struct wl_display_listener {
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500337 void (*invalid_object)(void *data,
338 struct wl_display *display, uint32_t id);
339 void (*invalid_method)(void *data, struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500340 uint32_t id, uint32_t opcode);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500341 void (*no_memory)(void *data,
342 struct wl_display *display);
343 void (*global)(void *data, struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500344 uint32_t id, const char *interface, uint32_t version);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500345 void (*range)(void *data,
346 struct wl_display *display, uint32_t range);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500347};
348
349static const struct wl_display_listener display_listener = {
350 display_handle_invalid_object,
351 display_handle_invalid_method,
352 display_handle_no_memory,
353 display_handle_global,
354 display_handle_range
355};
356
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500357WL_EXPORT struct wl_display *
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500358wl_display_create(const char *name, size_t name_size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400359{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400360 struct wl_display *display;
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500361 struct sockaddr_un addr;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400362 socklen_t size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400363
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400364 display = malloc(sizeof *display);
365 if (display == NULL)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400366 return NULL;
367
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400368 memset(display, 0, sizeof *display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400369 display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
370 if (display->fd < 0) {
371 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400372 return NULL;
373 }
374
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500375 addr.sun_family = AF_LOCAL;
376 memcpy(addr.sun_path, name, name_size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400377
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500378 size = offsetof (struct sockaddr_un, sun_path) + name_size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400379
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500380 if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400381 close(display->fd);
382 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400383 return NULL;
384 }
385
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500386 display->objects = wl_hash_create();
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500387 wl_list_init(&display->global_list);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500388 wl_list_init(&display->global_listener_list);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400389
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500390 display->proxy.base.interface = &wl_display_interface;
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500391 display->proxy.base.id = 1;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500392 display->proxy.display = display;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500393 wl_list_init(&display->proxy.listener_list);
394
395 display->listener.implementation = (void(**)(void)) &display_listener;
396 wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400397
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400398 display->connection = wl_connection_create(display->fd,
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500399 connection_update,
400 display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400401
402 return display;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400403}
404
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500405WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400406wl_display_destroy(struct wl_display *display)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400407{
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400408 wl_connection_destroy(display->connection);
409 close(display->fd);
410 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400411}
412
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500413WL_EXPORT uint32_t
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500414wl_display_get_object_id(struct wl_display *display,
415 const char *interface, uint32_t version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500416{
417 struct wl_global *global;
418
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500419 wl_list_for_each(global, &display->global_list, link)
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500420 if (strcmp(global->interface, interface) == 0 &&
421 global->version >= version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500422 return global->id;
423
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500424 return 0;
425}
426
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500427WL_EXPORT int
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500428wl_display_get_fd(struct wl_display *display,
429 wl_display_update_func_t update, void *data)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400430{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500431 display->update = update;
432 display->update_data = data;
433
434 display->update(display->mask, display->update_data);
435
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400436 return display->fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400437}
438
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500439static void
Kristian Høgsberg40979232008-11-25 22:40:39 -0500440handle_event(struct wl_display *display,
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500441 uint32_t id, uint32_t opcode, uint32_t size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400442{
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500443 uint32_t p[32];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500444 struct wl_listener *listener;
445 struct wl_proxy *proxy;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400446
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500447 wl_connection_copy(display->connection, p, size);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500448 if (id == 1)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500449 proxy = &display->proxy;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500450 else
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500451 proxy = (struct wl_proxy *) wl_hash_lookup(display->objects, id);
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500452
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500453 if (proxy != NULL) {
454 if (wl_list_empty(&proxy->listener_list)) {
455 printf("proxy found for object %d, opcode %d, but no listeners\n",
456 id, opcode);
457 }
458
Kristian Høgsberga5db5892010-02-26 10:28:44 -0500459 wl_list_for_each(listener, &proxy->listener_list, link)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500460 wl_connection_demarshal(display->connection,
461 size,
462 display->objects,
463 listener->implementation[opcode],
464 listener->data,
465 &proxy->base,
466 &proxy->base.interface->events[opcode]);
467
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500468 }
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500469
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500470 wl_connection_consume(display->connection, size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400471}
472
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500473WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400474wl_display_iterate(struct wl_display *display, uint32_t mask)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400475{
Kristian Høgsberg40979232008-11-25 22:40:39 -0500476 uint32_t p[2], object, opcode, size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400477 int len;
478
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400479 len = wl_connection_data(display->connection, mask);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400480 while (len > 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400481 if (len < sizeof p)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400482 break;
483
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400484 wl_connection_copy(display->connection, p, sizeof p);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500485 object = p[0];
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400486 opcode = p[1] & 0xffff;
487 size = p[1] >> 16;
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400488 if (len < size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400489 break;
490
Kristian Høgsberg40979232008-11-25 22:40:39 -0500491 handle_event(display, object, opcode, size);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500492 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400493 }
494
495 if (len < 0) {
496 fprintf(stderr, "read error: %m\n");
497 exit(EXIT_FAILURE);
498 }
499}
500
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500501WL_EXPORT uint32_t
502wl_display_allocate_id(struct wl_display *display)
503{
Kristian Høgsberg97079ad2008-12-21 22:45:33 -0500504 if (display->id_count == 0) {
505 display->id_count = 256;
506 display->id = display->next_range;
507 }
508
509 display->id_count--;
510
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500511 return display->id++;
512}
513
514WL_EXPORT void
515wl_display_write(struct wl_display *display, const void *data, size_t count)
516{
517 wl_connection_write(display->connection, data, count);
518}
519
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500520WL_EXPORT struct wl_compositor *
521wl_display_get_compositor(struct wl_display *display)
522{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500523 return display->compositor;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500524}
525
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500526WL_EXPORT int
527wl_compositor_add_listener(struct wl_compositor *compositor,
528 const struct wl_compositor_listener *listener,
529 void *data)
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500530{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500531 return wl_proxy_add_listener(&compositor->proxy,
532 (void (**)(void)) listener, data);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500533}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400534
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500535WL_EXPORT struct wl_surface *
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500536wl_compositor_create_surface(struct wl_compositor *compositor)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400537{
538 struct wl_surface *surface;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400539
540 surface = malloc(sizeof *surface);
541 if (surface == NULL)
542 return NULL;
543
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500544 surface->proxy.base.interface = &wl_surface_interface;
545 surface->proxy.base.id = wl_display_allocate_id(compositor->proxy.display);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500546 surface->proxy.display = compositor->proxy.display;
Kristian Høgsberg2c837482009-02-22 20:58:29 -0500547 wl_hash_insert(compositor->proxy.display->objects, &surface->proxy.base);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500548 wl_proxy_marshal(&compositor->proxy,
549 WL_COMPOSITOR_CREATE_SURFACE, surface);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400550
551 return surface;
552}
553
Kristian Høgsberg40979232008-11-25 22:40:39 -0500554WL_EXPORT void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500555wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
Kristian Høgsberg40979232008-11-25 22:40:39 -0500556{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500557 wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500558}
559
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500560WL_EXPORT void
561wl_surface_destroy(struct wl_surface *surface)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400562{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500563 wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400564}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400565
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500566WL_EXPORT void
567wl_surface_attach(struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500568 int32_t width, int32_t height, uint32_t stride,
569 struct wl_visual *visual)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400570{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500571 wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
572 name, width, height, stride, visual);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400573}
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400574
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500575WL_EXPORT void
576wl_surface_map(struct wl_surface *surface,
577 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400578{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500579 wl_proxy_marshal(&surface->proxy,
580 WL_SURFACE_MAP, x, y, width, height);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400581}
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500582
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500583WL_EXPORT void
584wl_surface_copy(struct wl_surface *surface, int32_t dst_x, int32_t dst_y,
585 uint32_t name, uint32_t stride,
586 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500587{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500588 wl_proxy_marshal(&surface->proxy, WL_SURFACE_COPY,
589 dst_x, dst_y, name, stride, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500590}
591
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500592WL_EXPORT void
593wl_surface_damage(struct wl_surface *surface,
594 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500595{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500596 wl_proxy_marshal(&surface->proxy,
597 WL_SURFACE_DAMAGE, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500598}
Kristian Høgsbergecf65fe2009-09-18 09:49:21 -0400599
600WL_EXPORT void
601wl_surface_set_user_data(struct wl_surface *surface, void *user_data)
602{
603 surface->proxy.user_data = user_data;
604}
605
606WL_EXPORT void *
607wl_surface_get_user_data(struct wl_surface *surface)
608{
609 return surface->proxy.user_data;
610}