blob: 3c977b2821f276946beb9e932520ebddebbf3652 [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
412 return display;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400413}
414
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500415WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400416wl_display_destroy(struct wl_display *display)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400417{
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400418 wl_connection_destroy(display->connection);
419 close(display->fd);
420 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400421}
422
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500423WL_EXPORT uint32_t
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500424wl_display_get_object_id(struct wl_display *display,
425 const char *interface, uint32_t version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500426{
427 struct wl_global *global;
428
429 global = container_of(display->global_list.next,
430 struct wl_global, link);
431 while (&global->link != &display->global_list) {
Kristian Høgsberg8049cbb2008-12-21 22:50:32 -0500432 if (strcmp(global->interface, interface) == 0 &&
433 global->version >= version)
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500434 return global->id;
435
436 global = container_of(global->link.next,
437 struct wl_global, link);
438 }
439
440 return 0;
441}
442
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500443WL_EXPORT int
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500444wl_display_get_fd(struct wl_display *display,
445 wl_display_update_func_t update, void *data)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400446{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500447 display->update = update;
448 display->update_data = data;
449
450 display->update(display->mask, display->update_data);
451
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400452 return display->fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400453}
454
Kristian Høgsbergee02ca62008-12-21 23:37:12 -0500455static void
Kristian Høgsberg40979232008-11-25 22:40:39 -0500456handle_event(struct wl_display *display,
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500457 uint32_t id, uint32_t opcode, uint32_t size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400458{
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500459 uint32_t p[32];
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500460 struct wl_listener *listener;
461 struct wl_proxy *proxy;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400462
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500463 wl_connection_copy(display->connection, p, size);
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500464 if (id == 1)
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500465 proxy = &display->proxy;
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500466 else
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500467 proxy = (struct wl_proxy *) wl_hash_lookup(display->objects, id);
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500468
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500469 if (proxy != NULL) {
470 if (wl_list_empty(&proxy->listener_list)) {
471 printf("proxy found for object %d, opcode %d, but no listeners\n",
472 id, opcode);
473 }
474
475 listener = container_of(proxy->listener_list.next,
476 struct wl_listener, link);
477 while (&listener->link != &proxy->listener_list) {
478
479 wl_connection_demarshal(display->connection,
480 size,
481 display->objects,
482 listener->implementation[opcode],
483 listener->data,
484 &proxy->base,
485 &proxy->base.interface->events[opcode]);
486
487 listener = container_of(listener->link.next,
488 struct wl_listener, link);
489 }
490 }
Kristian Høgsbergb3131d92008-12-24 19:30:25 -0500491
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500492 wl_connection_consume(display->connection, size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400493}
494
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500495WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400496wl_display_iterate(struct wl_display *display, uint32_t mask)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400497{
Kristian Høgsberg40979232008-11-25 22:40:39 -0500498 uint32_t p[2], object, opcode, size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400499 int len;
500
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400501 len = wl_connection_data(display->connection, mask);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400502 while (len > 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400503 if (len < sizeof p)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400504 break;
505
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400506 wl_connection_copy(display->connection, p, sizeof p);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500507 object = p[0];
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400508 opcode = p[1] & 0xffff;
509 size = p[1] >> 16;
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400510 if (len < size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400511 break;
512
Kristian Høgsberg40979232008-11-25 22:40:39 -0500513 handle_event(display, object, opcode, size);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500514 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400515 }
516
517 if (len < 0) {
518 fprintf(stderr, "read error: %m\n");
519 exit(EXIT_FAILURE);
520 }
521}
522
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500523WL_EXPORT uint32_t
524wl_display_allocate_id(struct wl_display *display)
525{
Kristian Høgsberg97079ad2008-12-21 22:45:33 -0500526 if (display->id_count == 0) {
527 display->id_count = 256;
528 display->id = display->next_range;
529 }
530
531 display->id_count--;
532
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500533 return display->id++;
534}
535
536WL_EXPORT void
537wl_display_write(struct wl_display *display, const void *data, size_t count)
538{
539 wl_connection_write(display->connection, data, count);
540}
541
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500542WL_EXPORT struct wl_compositor *
543wl_display_get_compositor(struct wl_display *display)
544{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500545 return display->compositor;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500546}
547
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500548WL_EXPORT int
549wl_compositor_add_listener(struct wl_compositor *compositor,
550 const struct wl_compositor_listener *listener,
551 void *data)
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500552{
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500553 return wl_proxy_add_listener(&compositor->proxy,
554 (void (**)(void)) listener, data);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500555}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400556
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500557WL_EXPORT struct wl_surface *
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500558wl_compositor_create_surface(struct wl_compositor *compositor)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400559{
560 struct wl_surface *surface;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400561
562 surface = malloc(sizeof *surface);
563 if (surface == NULL)
564 return NULL;
565
Kristian Høgsbergfabd4392008-12-22 18:06:49 -0500566 surface->proxy.base.interface = &wl_surface_interface;
567 surface->proxy.base.id = wl_display_allocate_id(compositor->proxy.display);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500568 surface->proxy.display = compositor->proxy.display;
Kristian Høgsberg2c837482009-02-22 20:58:29 -0500569 wl_hash_insert(compositor->proxy.display->objects, &surface->proxy.base);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500570 wl_proxy_marshal(&compositor->proxy,
571 WL_COMPOSITOR_CREATE_SURFACE, surface);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400572
573 return surface;
574}
575
Kristian Høgsberg40979232008-11-25 22:40:39 -0500576WL_EXPORT void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500577wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
Kristian Høgsberg40979232008-11-25 22:40:39 -0500578{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500579 wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500580}
581
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500582WL_EXPORT void
583wl_surface_destroy(struct wl_surface *surface)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400584{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500585 wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400586}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400587
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500588WL_EXPORT void
589wl_surface_attach(struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500590 int32_t width, int32_t height, uint32_t stride,
591 struct wl_visual *visual)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400592{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500593 wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
594 name, width, height, stride, visual);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400595}
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400596
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500597WL_EXPORT void
598wl_surface_map(struct wl_surface *surface,
599 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400600{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500601 wl_proxy_marshal(&surface->proxy,
602 WL_SURFACE_MAP, x, y, width, height);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400603}
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500604
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500605WL_EXPORT void
606wl_surface_copy(struct wl_surface *surface, int32_t dst_x, int32_t dst_y,
607 uint32_t name, uint32_t stride,
608 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500609{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500610 wl_proxy_marshal(&surface->proxy, WL_SURFACE_COPY,
611 dst_x, dst_y, name, stride, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500612}
613
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500614WL_EXPORT void
615wl_surface_damage(struct wl_surface *surface,
616 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500617{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500618 wl_proxy_marshal(&surface->proxy,
619 WL_SURFACE_DAMAGE, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500620}