blob: 5412dcfceff08f6c47d610be95c994604f71d0d4 [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øgsberga67a71a2008-10-07 10:10:36 -040067};
68
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050069struct wl_compositor {
70 struct wl_proxy proxy;
71};
72
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040073struct wl_surface {
74 struct wl_proxy proxy;
75};
76
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050077struct wl_visual {
78 struct wl_proxy proxy;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050079};
80
Kristian Høgsberg12ea62e2008-12-22 16:37:07 -050081struct wl_output {
82 struct wl_proxy proxy;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050083 struct wl_listener listener;
Kristian Høgsberg12ea62e2008-12-22 16:37:07 -050084 int32_t width, height;
85};
86
Kristian Høgsberg94448c02008-12-30 11:03:33 -050087struct wl_input_device {
88 struct wl_proxy proxy;
89};
90
91struct wl_display {
92 struct wl_proxy proxy;
93 struct wl_connection *connection;
94 int fd;
95 uint32_t id, id_count, next_range;
96 uint32_t mask;
97 struct wl_hash *objects;
98 struct wl_list global_list;
99 struct wl_listener listener;
100 struct wl_list global_listener_list;
101
102 struct wl_visual *argb_visual;
103 struct wl_visual *premultiplied_argb_visual;
104 struct wl_visual *rgb_visual;
105
106 wl_display_update_func_t update;
107 void *update_data;
108
109 wl_display_global_func_t global_handler;
110 void *global_handler_data;
111
112 struct wl_compositor *compositor;
113};
114
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500115static int
116connection_update(struct wl_connection *connection,
117 uint32_t mask, void *data)
118{
119 struct wl_display *display = data;
120
121 display->mask = mask;
122 if (display->update)
123 return display->update(display->mask,
124 display->update_data);
125
126 return 0;
127}
128
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500129WL_EXPORT int
130wl_object_implements(struct wl_object *object,
131 const char *interface, int version)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500132{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500133 return strcmp(object->interface->name, interface) == 0 &&
134 object->interface->version >= version;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500135}
136
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500137WL_EXPORT struct wl_global_listener *
138wl_display_add_global_listener(struct wl_display *display,
139 wl_display_global_func_t handler, void *data)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500140{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500141 struct wl_global_listener *listener;
142 struct wl_global *global;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500143
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500144 listener = malloc(sizeof *listener);
145 if (listener == NULL)
146 return NULL;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500147
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500148 listener->handler = handler;
149 listener->data = data;
150 wl_list_insert(display->global_listener_list.prev, &listener->link);
151
152 /* FIXME: Need a destructor for void *data? */
153
154 global = container_of(display->global_list.next,
155 struct wl_global, link);
156 while (&global->link != &display->global_list) {
157 if (global->proxy != NULL)
158 (*handler)(display, &global->proxy->base, data);
159
160 global = container_of(global->link.next,
161 struct wl_global, link);
162 }
163
164 return listener;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500165}
166
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500167WL_EXPORT void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500168wl_display_remove_global_listener(struct wl_display *display,
169 struct wl_global_listener *listener)
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500170{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500171 wl_list_remove(&listener->link);
172 free(listener);
173}
174
175static struct wl_proxy *
176wl_proxy_create_for_global(struct wl_display *display,
177 struct wl_global *global,
178 const struct wl_interface *interface)
179{
180 struct wl_proxy *proxy;
181 struct wl_global_listener *listener;
182
183 proxy = malloc(sizeof *proxy);
184 if (proxy == NULL)
185 return NULL;
186
187 proxy->base.interface = interface;
188 proxy->base.id = global->id;
189 proxy->display = display;
190 global->proxy = proxy;
191 wl_list_init(&proxy->listener_list);
192 wl_hash_insert(display->objects, &proxy->base);
193
194 listener = container_of(display->global_listener_list.next,
195 struct wl_global_listener, link);
196 while (&listener->link != &display->global_listener_list) {
197 (*listener->handler)(display, &proxy->base, listener->data);
198 listener = container_of(listener->link.next,
199 struct wl_global_listener, link);
200 }
201
202 return proxy;
203}
204
205static int
206wl_proxy_add_listener(struct wl_proxy *proxy, void (**implementation)(void), void *data)
207{
208 struct wl_listener *listener;
209
210 listener = malloc(sizeof *listener);
211 if (listener == NULL)
212 return -1;
213
214 listener->implementation = (void (**)(void)) implementation;
215 listener->data = data;
216 wl_list_insert(proxy->listener_list.prev, &listener->link);
217
218 return 0;
219}
220
221static void
222wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
223{
224 va_list ap;
225
226 va_start(ap, opcode);
227 wl_connection_vmarshal(proxy->display->connection,
228 &proxy->base, opcode, ap,
229 &proxy->base.interface->methods[opcode]);
230 va_end(ap);
231}
232
233WL_EXPORT int
234wl_output_add_listener(struct wl_output *output,
235 const struct wl_output_listener *listener,
236 void *data)
237{
238 return wl_proxy_add_listener(&output->proxy,
239 (void (**)(void)) listener, data);
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500240}
241
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500242static void
243add_visual(struct wl_display *display, struct wl_global *global)
244{
245 struct wl_visual *visual;
246
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500247 visual = (struct wl_visual *)
248 wl_proxy_create_for_global(display, global,
249 &wl_visual_interface);
250 if (display->argb_visual == NULL)
251 display->argb_visual = visual;
252 else if (display->premultiplied_argb_visual == NULL)
253 display->premultiplied_argb_visual = visual;
254 else
255 display->rgb_visual = visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500256}
257
258WL_EXPORT struct wl_visual *
259wl_display_get_argb_visual(struct wl_display *display)
260{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500261 return display->argb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500262}
263
264WL_EXPORT struct wl_visual *
265wl_display_get_premultiplied_argb_visual(struct wl_display *display)
266{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500267 return display->premultiplied_argb_visual;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500268}
269
270WL_EXPORT struct wl_visual *
271wl_display_get_rgb_visual(struct wl_display *display)
272{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500273 return display->rgb_visual;
274}
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500275
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500276WL_EXPORT int
277wl_input_device_add_listener(struct wl_input_device *input_device,
278 const struct wl_input_device_listener *listener,
279 void *data)
280{
281 return wl_proxy_add_listener(&input_device->proxy,
282 (void (**)(void)) listener, data);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500283}
284
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500285static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500286display_handle_invalid_object(void *data,
287 struct wl_display *display, uint32_t id)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500288{
289 fprintf(stderr, "sent request to invalid object\n");
290}
291
292static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500293display_handle_invalid_method(void *data,
294 struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500295 uint32_t id, uint32_t opcode)
296{
297 fprintf(stderr, "sent invalid request opcode\n");
298}
299
300static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500301display_handle_no_memory(void *data,
302 struct wl_display *display)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500303{
304 fprintf(stderr, "server out of memory\n");
305}
306
307static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500308display_handle_global(void *data,
309 struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500310 uint32_t id, const char *interface, uint32_t version)
311{
312 struct wl_global *global;
313
314 global = malloc(sizeof *global);
315 if (global == NULL)
316 return;
317
318 global->id = id;
319 global->interface = strdup(interface);
320 global->version = version;
321 wl_list_insert(display->global_list.prev, &global->link);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500322
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500323 if (strcmp(global->interface, "display") == 0)
324 wl_hash_insert(display->objects, &display->proxy.base);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500325 else if (strcmp(global->interface, "compositor") == 0)
326 display->compositor = (struct wl_compositor *)
327 wl_proxy_create_for_global(display, global,
328 &wl_compositor_interface);
329 else if (strcmp(global->interface, "visual") == 0)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500330 add_visual(display, global);
331 else if (strcmp(global->interface, "output") == 0)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500332 wl_proxy_create_for_global(display, global,
333 &wl_output_interface);
334 else if (strcmp(global->interface, "input_device") == 0)
335 wl_proxy_create_for_global(display, global,
336 &wl_input_device_interface);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500337}
338
339static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500340display_handle_range(void *data,
341 struct wl_display *display, uint32_t range)
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500342{
343 display->next_range = range;
344}
345
346struct wl_display_listener {
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500347 void (*invalid_object)(void *data,
348 struct wl_display *display, uint32_t id);
349 void (*invalid_method)(void *data, struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500350 uint32_t id, uint32_t opcode);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500351 void (*no_memory)(void *data,
352 struct wl_display *display);
353 void (*global)(void *data, struct wl_display *display,
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500354 uint32_t id, const char *interface, uint32_t version);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500355 void (*range)(void *data,
356 struct wl_display *display, uint32_t range);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500357};
358
359static const struct wl_display_listener display_listener = {
360 display_handle_invalid_object,
361 display_handle_invalid_method,
362 display_handle_no_memory,
363 display_handle_global,
364 display_handle_range
365};
366
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500367WL_EXPORT struct wl_display *
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500368wl_display_create(const char *name, size_t name_size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400369{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400370 struct wl_display *display;
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500371 struct sockaddr_un addr;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400372 socklen_t size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400373
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400374 display = malloc(sizeof *display);
375 if (display == NULL)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400376 return NULL;
377
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400378 memset(display, 0, sizeof *display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400379 display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
380 if (display->fd < 0) {
381 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400382 return NULL;
383 }
384
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500385 addr.sun_family = AF_LOCAL;
386 memcpy(addr.sun_path, name, name_size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400387
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500388 size = offsetof (struct sockaddr_un, sun_path) + name_size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400389
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500390 if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400391 close(display->fd);
392 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400393 return NULL;
394 }
395
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500396 display->objects = wl_hash_create();
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500397 wl_list_init(&display->global_list);
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500398 wl_list_init(&display->global_listener_list);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400399
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500400 display->proxy.base.interface = &wl_display_interface;
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500401 display->proxy.base.id = 1;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500402 display->proxy.display = display;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500403 wl_list_init(&display->proxy.listener_list);
404
405 display->listener.implementation = (void(**)(void)) &display_listener;
406 wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400407
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400408 display->connection = wl_connection_create(display->fd,
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500409 connection_update,
410 display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400411
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500412 /* Process connection events. */
413 wl_display_iterate(display, WL_CONNECTION_READABLE);
414
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400415 return display;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400416}
417
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500418WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400419wl_display_destroy(struct wl_display *display)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400420{
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400421 wl_connection_destroy(display->connection);
422 close(display->fd);
423 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400424}
425
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500426WL_EXPORT uint32_t
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500427wl_display_get_object_id(struct wl_display *display,
428 const char *interface, uint32_t version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500429{
430 struct wl_global *global;
431
432 global = container_of(display->global_list.next,
433 struct wl_global, link);
434 while (&global->link != &display->global_list) {
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500435 if (strcmp(global->interface, interface) == 0 &&
436 global->version >= version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500437 return global->id;
438
439 global = container_of(global->link.next,
440 struct wl_global, link);
441 }
442
443 return 0;
444}
445
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500446WL_EXPORT int
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500447wl_display_get_fd(struct wl_display *display,
448 wl_display_update_func_t update, void *data)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400449{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500450 display->update = update;
451 display->update_data = data;
452
453 display->update(display->mask, display->update_data);
454
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400455 return display->fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400456}
457
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500458static void
Kristian Høgsberg40979232008-11-25 22:40:39 -0500459handle_event(struct wl_display *display,
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500460 uint32_t id, uint32_t opcode, uint32_t size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400461{
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500462 uint32_t p[32];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500463 struct wl_listener *listener;
464 struct wl_proxy *proxy;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400465
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500466 wl_connection_copy(display->connection, p, size);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500467 if (id == 1)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500468 proxy = &display->proxy;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500469 else
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500470 proxy = (struct wl_proxy *) wl_hash_lookup(display->objects, id);
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500471
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500472 if (proxy != NULL) {
473 if (wl_list_empty(&proxy->listener_list)) {
474 printf("proxy found for object %d, opcode %d, but no listeners\n",
475 id, opcode);
476 }
477
478 listener = container_of(proxy->listener_list.next,
479 struct wl_listener, link);
480 while (&listener->link != &proxy->listener_list) {
481
482 wl_connection_demarshal(display->connection,
483 size,
484 display->objects,
485 listener->implementation[opcode],
486 listener->data,
487 &proxy->base,
488 &proxy->base.interface->events[opcode]);
489
490 listener = container_of(listener->link.next,
491 struct wl_listener, link);
492 }
493 }
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500494
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500495 wl_connection_consume(display->connection, size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400496}
497
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500498WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400499wl_display_iterate(struct wl_display *display, uint32_t mask)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400500{
Kristian Høgsberg40979232008-11-25 22:40:39 -0500501 uint32_t p[2], object, opcode, size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400502 int len;
503
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400504 len = wl_connection_data(display->connection, mask);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400505 while (len > 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400506 if (len < sizeof p)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400507 break;
508
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400509 wl_connection_copy(display->connection, p, sizeof p);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500510 object = p[0];
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400511 opcode = p[1] & 0xffff;
512 size = p[1] >> 16;
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400513 if (len < size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400514 break;
515
Kristian Høgsberg40979232008-11-25 22:40:39 -0500516 handle_event(display, object, opcode, size);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500517 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400518 }
519
520 if (len < 0) {
521 fprintf(stderr, "read error: %m\n");
522 exit(EXIT_FAILURE);
523 }
524}
525
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500526WL_EXPORT uint32_t
527wl_display_allocate_id(struct wl_display *display)
528{
Kristian Høgsberg97079ad2008-12-21 22:45:33 -0500529 if (display->id_count == 0) {
530 display->id_count = 256;
531 display->id = display->next_range;
532 }
533
534 display->id_count--;
535
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500536 return display->id++;
537}
538
539WL_EXPORT void
540wl_display_write(struct wl_display *display, const void *data, size_t count)
541{
542 wl_connection_write(display->connection, data, count);
543}
544
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500545WL_EXPORT struct wl_compositor *
546wl_display_get_compositor(struct wl_display *display)
547{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500548 return display->compositor;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500549}
550
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500551WL_EXPORT int
552wl_compositor_add_listener(struct wl_compositor *compositor,
553 const struct wl_compositor_listener *listener,
554 void *data)
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500555{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500556 return wl_proxy_add_listener(&compositor->proxy,
557 (void (**)(void)) listener, data);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500558}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400559
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500560WL_EXPORT struct wl_surface *
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500561wl_compositor_create_surface(struct wl_compositor *compositor)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400562{
563 struct wl_surface *surface;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400564
565 surface = malloc(sizeof *surface);
566 if (surface == NULL)
567 return NULL;
568
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500569 surface->proxy.base.interface = &wl_surface_interface;
570 surface->proxy.base.id = wl_display_allocate_id(compositor->proxy.display);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500571 surface->proxy.display = compositor->proxy.display;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500572 wl_proxy_marshal(&compositor->proxy,
573 WL_COMPOSITOR_CREATE_SURFACE, surface);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400574
575 return surface;
576}
577
Kristian Høgsberg40979232008-11-25 22:40:39 -0500578WL_EXPORT void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500579wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
Kristian Høgsberg40979232008-11-25 22:40:39 -0500580{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500581 wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500582}
583
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500584WL_EXPORT void
585wl_surface_destroy(struct wl_surface *surface)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400586{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500587 wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400588}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400589
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500590WL_EXPORT void
591wl_surface_attach(struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500592 int32_t width, int32_t height, uint32_t stride,
593 struct wl_visual *visual)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400594{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500595 wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
596 name, width, height, stride, visual);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400597}
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400598
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500599WL_EXPORT void
600wl_surface_map(struct wl_surface *surface,
601 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400602{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500603 wl_proxy_marshal(&surface->proxy,
604 WL_SURFACE_MAP, x, y, width, height);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400605}
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500606
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500607WL_EXPORT void
608wl_surface_copy(struct wl_surface *surface, int32_t dst_x, int32_t dst_y,
609 uint32_t name, uint32_t stride,
610 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500611{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500612 wl_proxy_marshal(&surface->proxy, WL_SURFACE_COPY,
613 dst_x, dst_y, name, stride, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500614}
615
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500616WL_EXPORT void
617wl_surface_damage(struct wl_surface *surface,
618 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500619{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500620 wl_proxy_marshal(&surface->proxy,
621 WL_SURFACE_DAMAGE, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500622}