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-client-protocol.h"
37 #include "connection.h"
38 #include "wayland-util.h"
39 #include "wayland-client.h"
41 static const char socket_name[] = "\0wayland";
43 struct wl_global_listener {
44 wl_display_global_func_t handler;
50 void (**implementation)(void);
56 struct wl_object base;
57 struct wl_display *display;
58 struct wl_list listener_list;
63 struct wl_proxy proxy;
64 struct wl_connection *connection;
66 uint32_t id, id_count, next_range;
68 struct wl_hash_table *objects;
69 struct wl_listener listener;
70 struct wl_list global_listener_list;
72 struct wl_visual *argb_visual;
73 struct wl_visual *premultiplied_argb_visual;
74 struct wl_visual *rgb_visual;
76 wl_display_update_func_t update;
79 wl_display_global_func_t global_handler;
80 void *global_handler_data;
84 connection_update(struct wl_connection *connection,
85 uint32_t mask, void *data)
87 struct wl_display *display = data;
91 return display->update(display->mask,
92 display->update_data);
97 WL_EXPORT struct wl_global_listener *
98 wl_display_add_global_listener(struct wl_display *display,
99 wl_display_global_func_t handler, void *data)
101 struct wl_global_listener *listener;
103 listener = malloc(sizeof *listener);
104 if (listener == NULL)
107 listener->handler = handler;
108 listener->data = data;
109 wl_list_insert(display->global_listener_list.prev, &listener->link);
115 wl_display_remove_global_listener(struct wl_display *display,
116 struct wl_global_listener *listener)
118 wl_list_remove(&listener->link);
122 WL_EXPORT struct wl_proxy *
123 wl_proxy_create_for_id(struct wl_display *display,
124 const struct wl_interface *interface, uint32_t id)
126 struct wl_proxy *proxy;
128 proxy = malloc(sizeof *proxy);
132 proxy->base.interface = interface;
134 proxy->display = display;
135 wl_list_init(&proxy->listener_list);
136 wl_hash_table_insert(display->objects, proxy->base.id, proxy);
141 WL_EXPORT struct wl_proxy *
142 wl_proxy_create(struct wl_proxy *factory,
143 const struct wl_interface *interface)
145 return wl_proxy_create_for_id(factory->display, interface,
146 wl_display_allocate_id(factory->display));
150 wl_proxy_add_listener(struct wl_proxy *proxy,
151 void (**implementation)(void), void *data)
153 struct wl_listener *listener;
155 listener = malloc(sizeof *listener);
156 if (listener == NULL)
159 listener->implementation = (void (**)(void)) implementation;
160 listener->data = data;
161 wl_list_insert(proxy->listener_list.prev, &listener->link);
167 wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
171 va_start(ap, opcode);
172 wl_connection_vmarshal(proxy->display->connection,
173 &proxy->base, opcode, ap,
174 &proxy->base.interface->methods[opcode]);
179 add_visual(struct wl_display *display, uint32_t id)
181 struct wl_visual *visual;
183 visual = (struct wl_visual *)
184 wl_proxy_create_for_id(display, &wl_visual_interface, id);
185 if (display->argb_visual == NULL)
186 display->argb_visual = visual;
187 else if (display->premultiplied_argb_visual == NULL)
188 display->premultiplied_argb_visual = visual;
190 display->rgb_visual = visual;
193 WL_EXPORT struct wl_visual *
194 wl_display_get_argb_visual(struct wl_display *display)
196 return display->argb_visual;
199 WL_EXPORT struct wl_visual *
200 wl_display_get_premultiplied_argb_visual(struct wl_display *display)
202 return display->premultiplied_argb_visual;
205 WL_EXPORT struct wl_visual *
206 wl_display_get_rgb_visual(struct wl_display *display)
208 return display->rgb_visual;
212 display_handle_invalid_object(void *data,
213 struct wl_display *display, uint32_t id)
215 fprintf(stderr, "sent request to invalid object\n");
220 display_handle_invalid_method(void *data,
221 struct wl_display *display,
222 uint32_t id, uint32_t opcode)
224 fprintf(stderr, "sent invalid request opcode\n");
229 display_handle_no_memory(void *data,
230 struct wl_display *display)
232 fprintf(stderr, "server out of memory\n");
237 display_handle_global(void *data,
238 struct wl_display *display,
239 uint32_t id, const char *interface, uint32_t version)
241 struct wl_global_listener *listener;
243 if (strcmp(interface, "display") == 0)
244 wl_hash_table_insert(display->objects,
245 id, &display->proxy.base);
246 else if (strcmp(interface, "visual") == 0)
247 add_visual(display, id);
249 wl_list_for_each(listener, &display->global_listener_list, link)
250 (*listener->handler)(display,
251 id, interface, version, listener->data);
255 display_handle_range(void *data,
256 struct wl_display *display, uint32_t range)
258 display->next_range = range;
261 static const struct wl_display_listener display_listener = {
262 display_handle_invalid_object,
263 display_handle_invalid_method,
264 display_handle_no_memory,
265 display_handle_global,
269 WL_EXPORT struct wl_display *
270 wl_display_create(const char *name, size_t name_size)
272 struct wl_display *display;
273 struct sockaddr_un addr;
276 display = malloc(sizeof *display);
280 memset(display, 0, sizeof *display);
281 display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
282 if (display->fd < 0) {
287 addr.sun_family = AF_LOCAL;
288 memcpy(addr.sun_path, name, name_size);
290 size = offsetof (struct sockaddr_un, sun_path) + name_size;
292 if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
298 display->objects = wl_hash_table_create();
299 wl_list_init(&display->global_listener_list);
301 display->proxy.base.interface = &wl_display_interface;
302 display->proxy.base.id = 1;
303 display->proxy.display = display;
304 wl_list_init(&display->proxy.listener_list);
306 display->listener.implementation = (void(**)(void)) &display_listener;
307 wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
309 display->connection = wl_connection_create(display->fd,
317 wl_display_destroy(struct wl_display *display)
319 wl_connection_destroy(display->connection);
325 wl_display_get_fd(struct wl_display *display,
326 wl_display_update_func_t update, void *data)
328 display->update = update;
329 display->update_data = data;
331 display->update(display->mask, display->update_data);
337 handle_event(struct wl_display *display,
338 uint32_t id, uint32_t opcode, uint32_t size)
341 struct wl_listener *listener;
342 struct wl_proxy *proxy;
344 wl_connection_copy(display->connection, p, size);
346 proxy = &display->proxy;
348 proxy = (struct wl_proxy *)
349 wl_hash_table_lookup(display->objects, id);
352 if (wl_list_empty(&proxy->listener_list)) {
353 printf("proxy found for object %d, opcode %d, but no listeners\n",
357 wl_list_for_each(listener, &proxy->listener_list, link)
358 wl_connection_demarshal(display->connection,
361 listener->implementation[opcode],
364 &proxy->base.interface->events[opcode]);
368 wl_connection_consume(display->connection, size);
372 wl_display_iterate(struct wl_display *display, uint32_t mask)
374 uint32_t p[2], object, opcode, size;
377 len = wl_connection_data(display->connection, mask);
382 wl_connection_copy(display->connection, p, sizeof p);
384 opcode = p[1] & 0xffff;
389 handle_event(display, object, opcode, size);
394 fprintf(stderr, "read error: %m\n");
400 wl_display_allocate_id(struct wl_display *display)
402 if (display->id_count == 0) {
403 display->id_count = 256;
404 display->id = display->next_range;
409 return display->id++;
413 wl_surface_set_user_data(struct wl_surface *surface, void *user_data)
415 struct wl_proxy *proxy = (struct wl_proxy *) surface;
417 proxy->user_data = user_data;
421 wl_surface_get_user_data(struct wl_surface *surface)
423 struct wl_proxy *proxy = (struct wl_proxy *) surface;
425 return proxy->user_data;