2 * Copyright © 2008 Kristian Høgsberg
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.
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
30 #include <sys/socket.h>
36 #include "wayland-protocol.h"
37 #include "connection.h"
38 #include "wayland-util.h"
39 #include "wayland-client.h"
41 static const char socket_name[] = "\0wayland";
47 struct wl_proxy *proxy;
51 struct wl_global_listener {
52 wl_display_global_func_t handler;
58 void (**implementation)(void);
64 struct wl_object base;
65 struct wl_display *display;
66 struct wl_list listener_list;
69 struct wl_compositor {
70 struct wl_proxy proxy;
74 struct wl_proxy proxy;
78 struct wl_proxy proxy;
82 struct wl_proxy proxy;
83 struct wl_listener listener;
84 int32_t width, height;
87 struct wl_input_device {
88 struct wl_proxy proxy;
92 struct wl_proxy proxy;
93 struct wl_connection *connection;
95 uint32_t id, id_count, next_range;
97 struct wl_hash *objects;
98 struct wl_list global_list;
99 struct wl_listener listener;
100 struct wl_list global_listener_list;
102 struct wl_visual *argb_visual;
103 struct wl_visual *premultiplied_argb_visual;
104 struct wl_visual *rgb_visual;
106 wl_display_update_func_t update;
109 wl_display_global_func_t global_handler;
110 void *global_handler_data;
112 struct wl_compositor *compositor;
116 connection_update(struct wl_connection *connection,
117 uint32_t mask, void *data)
119 struct wl_display *display = data;
121 display->mask = mask;
123 return display->update(display->mask,
124 display->update_data);
130 wl_object_implements(struct wl_object *object,
131 const char *interface, int version)
133 return strcmp(object->interface->name, interface) == 0 &&
134 object->interface->version >= version;
137 WL_EXPORT struct wl_global_listener *
138 wl_display_add_global_listener(struct wl_display *display,
139 wl_display_global_func_t handler, void *data)
141 struct wl_global_listener *listener;
142 struct wl_global *global;
144 listener = malloc(sizeof *listener);
145 if (listener == NULL)
148 listener->handler = handler;
149 listener->data = data;
150 wl_list_insert(display->global_listener_list.prev, &listener->link);
152 /* FIXME: Need a destructor for void *data? */
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);
160 global = container_of(global->link.next,
161 struct wl_global, link);
168 wl_display_remove_global_listener(struct wl_display *display,
169 struct wl_global_listener *listener)
171 wl_list_remove(&listener->link);
175 static struct wl_proxy *
176 wl_proxy_create_for_global(struct wl_display *display,
177 struct wl_global *global,
178 const struct wl_interface *interface)
180 struct wl_proxy *proxy;
181 struct wl_global_listener *listener;
183 proxy = malloc(sizeof *proxy);
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);
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);
206 wl_proxy_add_listener(struct wl_proxy *proxy, void (**implementation)(void), void *data)
208 struct wl_listener *listener;
210 listener = malloc(sizeof *listener);
211 if (listener == NULL)
214 listener->implementation = (void (**)(void)) implementation;
215 listener->data = data;
216 wl_list_insert(proxy->listener_list.prev, &listener->link);
222 wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
226 va_start(ap, opcode);
227 wl_connection_vmarshal(proxy->display->connection,
228 &proxy->base, opcode, ap,
229 &proxy->base.interface->methods[opcode]);
234 wl_output_add_listener(struct wl_output *output,
235 const struct wl_output_listener *listener,
238 return wl_proxy_add_listener(&output->proxy,
239 (void (**)(void)) listener, data);
243 add_visual(struct wl_display *display, struct wl_global *global)
245 struct wl_visual *visual;
247 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;
255 display->rgb_visual = visual;
258 WL_EXPORT struct wl_visual *
259 wl_display_get_argb_visual(struct wl_display *display)
261 return display->argb_visual;
264 WL_EXPORT struct wl_visual *
265 wl_display_get_premultiplied_argb_visual(struct wl_display *display)
267 return display->premultiplied_argb_visual;
270 WL_EXPORT struct wl_visual *
271 wl_display_get_rgb_visual(struct wl_display *display)
273 return display->rgb_visual;
277 wl_input_device_add_listener(struct wl_input_device *input_device,
278 const struct wl_input_device_listener *listener,
281 return wl_proxy_add_listener(&input_device->proxy,
282 (void (**)(void)) listener, data);
286 display_handle_invalid_object(void *data,
287 struct wl_display *display, uint32_t id)
289 fprintf(stderr, "sent request to invalid object\n");
293 display_handle_invalid_method(void *data,
294 struct wl_display *display,
295 uint32_t id, uint32_t opcode)
297 fprintf(stderr, "sent invalid request opcode\n");
301 display_handle_no_memory(void *data,
302 struct wl_display *display)
304 fprintf(stderr, "server out of memory\n");
308 display_handle_global(void *data,
309 struct wl_display *display,
310 uint32_t id, const char *interface, uint32_t version)
312 struct wl_global *global;
314 global = malloc(sizeof *global);
319 global->interface = strdup(interface);
320 global->version = version;
321 wl_list_insert(display->global_list.prev, &global->link);
323 if (strcmp(global->interface, "display") == 0)
324 wl_hash_insert(display->objects, &display->proxy.base);
325 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)
330 add_visual(display, global);
331 else if (strcmp(global->interface, "output") == 0)
332 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);
340 display_handle_range(void *data,
341 struct wl_display *display, uint32_t range)
343 display->next_range = range;
346 struct wl_display_listener {
347 void (*invalid_object)(void *data,
348 struct wl_display *display, uint32_t id);
349 void (*invalid_method)(void *data, struct wl_display *display,
350 uint32_t id, uint32_t opcode);
351 void (*no_memory)(void *data,
352 struct wl_display *display);
353 void (*global)(void *data, struct wl_display *display,
354 uint32_t id, const char *interface, uint32_t version);
355 void (*range)(void *data,
356 struct wl_display *display, uint32_t range);
359 static 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,
367 WL_EXPORT struct wl_display *
368 wl_display_create(const char *name, size_t name_size)
370 struct wl_display *display;
371 struct sockaddr_un addr;
374 display = malloc(sizeof *display);
378 memset(display, 0, sizeof *display);
379 display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
380 if (display->fd < 0) {
385 addr.sun_family = AF_LOCAL;
386 memcpy(addr.sun_path, name, name_size);
388 size = offsetof (struct sockaddr_un, sun_path) + name_size;
390 if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
396 display->objects = wl_hash_create();
397 wl_list_init(&display->global_list);
398 wl_list_init(&display->global_listener_list);
400 display->proxy.base.interface = &wl_display_interface;
401 display->proxy.base.id = 1;
402 display->proxy.display = display;
403 wl_list_init(&display->proxy.listener_list);
405 display->listener.implementation = (void(**)(void)) &display_listener;
406 wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
408 display->connection = wl_connection_create(display->fd,
412 /* Process connection events. */
413 wl_display_iterate(display, WL_CONNECTION_READABLE);
419 wl_display_destroy(struct wl_display *display)
421 wl_connection_destroy(display->connection);
427 wl_display_get_object_id(struct wl_display *display,
428 const char *interface, uint32_t version)
430 struct wl_global *global;
432 global = container_of(display->global_list.next,
433 struct wl_global, link);
434 while (&global->link != &display->global_list) {
435 if (strcmp(global->interface, interface) == 0 &&
436 global->version >= version)
439 global = container_of(global->link.next,
440 struct wl_global, link);
447 wl_display_get_fd(struct wl_display *display,
448 wl_display_update_func_t update, void *data)
450 display->update = update;
451 display->update_data = data;
453 display->update(display->mask, display->update_data);
459 handle_event(struct wl_display *display,
460 uint32_t id, uint32_t opcode, uint32_t size)
463 struct wl_listener *listener;
464 struct wl_proxy *proxy;
466 wl_connection_copy(display->connection, p, size);
468 proxy = &display->proxy;
470 proxy = (struct wl_proxy *) wl_hash_lookup(display->objects, id);
473 if (wl_list_empty(&proxy->listener_list)) {
474 printf("proxy found for object %d, opcode %d, but no listeners\n",
478 listener = container_of(proxy->listener_list.next,
479 struct wl_listener, link);
480 while (&listener->link != &proxy->listener_list) {
482 wl_connection_demarshal(display->connection,
485 listener->implementation[opcode],
488 &proxy->base.interface->events[opcode]);
490 listener = container_of(listener->link.next,
491 struct wl_listener, link);
495 wl_connection_consume(display->connection, size);
499 wl_display_iterate(struct wl_display *display, uint32_t mask)
501 uint32_t p[2], object, opcode, size;
504 len = wl_connection_data(display->connection, mask);
509 wl_connection_copy(display->connection, p, sizeof p);
511 opcode = p[1] & 0xffff;
516 handle_event(display, object, opcode, size);
521 fprintf(stderr, "read error: %m\n");
527 wl_display_allocate_id(struct wl_display *display)
529 if (display->id_count == 0) {
530 display->id_count = 256;
531 display->id = display->next_range;
536 return display->id++;
540 wl_display_write(struct wl_display *display, const void *data, size_t count)
542 wl_connection_write(display->connection, data, count);
545 WL_EXPORT struct wl_compositor *
546 wl_display_get_compositor(struct wl_display *display)
548 return display->compositor;
552 wl_compositor_add_listener(struct wl_compositor *compositor,
553 const struct wl_compositor_listener *listener,
556 return wl_proxy_add_listener(&compositor->proxy,
557 (void (**)(void)) listener, data);
560 WL_EXPORT struct wl_surface *
561 wl_compositor_create_surface(struct wl_compositor *compositor)
563 struct wl_surface *surface;
565 surface = malloc(sizeof *surface);
569 surface->proxy.base.interface = &wl_surface_interface;
570 surface->proxy.base.id = wl_display_allocate_id(compositor->proxy.display);
571 surface->proxy.display = compositor->proxy.display;
572 wl_proxy_marshal(&compositor->proxy,
573 WL_COMPOSITOR_CREATE_SURFACE, surface);
579 wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
581 wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
585 wl_surface_destroy(struct wl_surface *surface)
587 wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
591 wl_surface_attach(struct wl_surface *surface, uint32_t name,
592 int32_t width, int32_t height, uint32_t stride,
593 struct wl_visual *visual)
595 wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
596 name, width, height, stride, visual);
600 wl_surface_map(struct wl_surface *surface,
601 int32_t x, int32_t y, int32_t width, int32_t height)
603 wl_proxy_marshal(&surface->proxy,
604 WL_SURFACE_MAP, x, y, width, height);
608 wl_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)
612 wl_proxy_marshal(&surface->proxy, WL_SURFACE_COPY,
613 dst_x, dst_y, name, stride, x, y, width, height);
617 wl_surface_damage(struct wl_surface *surface,
618 int32_t x, int32_t y, int32_t width, int32_t height)
620 wl_proxy_marshal(&surface->proxy,
621 WL_SURFACE_DAMAGE, x, y, width, height);