blob: a898fd26df7eb0463ff467cbfa0e1b41ac2741c3 [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øgsberg1e4b86a2008-11-23 23:41:08 -050047 struct wl_list link;
48};
49
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040050struct wl_proxy {
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050051 const struct wl_interface *interface;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040052 uint32_t id;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -050053 struct wl_display *display;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040054};
55
56struct wl_display {
57 struct wl_proxy proxy;
Kristian Høgsberg427524a2008-10-08 13:32:07 -040058 struct wl_connection *connection;
59 int fd;
60 uint32_t id;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050061 uint32_t mask;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -050062 struct wl_list global_list;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050063 struct wl_list visual_list;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050064
65 wl_display_update_func_t update;
66 void *update_data;
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -050067
68 wl_display_event_func_t event_handler;
69 void *event_handler_data;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040070};
71
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050072struct wl_compositor {
73 struct wl_proxy proxy;
74};
75
Kristian Høgsberga67a71a2008-10-07 10:10:36 -040076struct wl_surface {
77 struct wl_proxy proxy;
78};
79
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050080struct wl_visual {
81 struct wl_proxy proxy;
82 struct wl_list link;
83};
84
Kristian Høgsbergfb590842008-11-07 14:27:23 -050085static int
86connection_update(struct wl_connection *connection,
87 uint32_t mask, void *data)
88{
89 struct wl_display *display = data;
90
91 display->mask = mask;
92 if (display->update)
93 return display->update(display->mask,
94 display->update_data);
95
96 return 0;
97}
98
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050099static void
100add_visual(struct wl_display *display, struct wl_global *global)
101{
102 struct wl_visual *visual;
103
104 visual = malloc(sizeof *visual);
105 if (visual == NULL)
106 return;
107
108 visual->proxy.display = display;
109 visual->proxy.id = global->id;
110 wl_list_insert(display->visual_list.prev, &visual->link);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500111}
112
113WL_EXPORT struct wl_visual *
114wl_display_get_argb_visual(struct wl_display *display)
115{
116 return container_of(display->visual_list.next,
117 struct wl_visual, link);
118}
119
120WL_EXPORT struct wl_visual *
121wl_display_get_premultiplied_argb_visual(struct wl_display *display)
122{
123 return container_of(display->visual_list.next->next,
124 struct wl_visual, link);
125}
126
127WL_EXPORT struct wl_visual *
128wl_display_get_rgb_visual(struct wl_display *display)
129{
130 /* FIXME: Where's cddar when you need it... */
131
132 return container_of(display->visual_list.next->next->next,
133 struct wl_visual, link);
134}
135
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500136WL_EXPORT struct wl_display *
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500137wl_display_create(const char *name, size_t name_size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400138{
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400139 struct wl_display *display;
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500140 struct sockaddr_un addr;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400141 socklen_t size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400142
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400143 display = malloc(sizeof *display);
144 if (display == NULL)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400145 return NULL;
146
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400147 memset(display, 0, sizeof *display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400148 display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
149 if (display->fd < 0) {
150 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400151 return NULL;
152 }
153
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500154 addr.sun_family = AF_LOCAL;
155 memcpy(addr.sun_path, name, name_size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400156
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500157 size = offsetof (struct sockaddr_un, sun_path) + name_size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400158
Kristian Høgsbergdc0f3552008-12-07 15:22:22 -0500159 if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400160 close(display->fd);
161 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400162 return NULL;
163 }
164
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400165 /* FIXME: We'll need a protocol for getting a new range, I
166 * guess... */
167 read(display->fd, &display->id, sizeof display->id);
168
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500169 wl_list_init(&display->global_list);
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500170 wl_list_init(&display->visual_list);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400171
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500172 display->proxy.interface = &wl_display_interface;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500173 display->proxy.id = wl_display_get_object_id(display, "display");
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500174 display->proxy.display = display;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400175
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400176 display->connection = wl_connection_create(display->fd,
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500177 connection_update,
178 display);
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400179
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500180 /* Process connection events. */
181 wl_display_iterate(display, WL_CONNECTION_READABLE);
182
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400183 return display;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400184}
185
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500186WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400187wl_display_destroy(struct wl_display *display)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400188{
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400189 wl_connection_destroy(display->connection);
190 close(display->fd);
191 free(display);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400192}
193
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500194WL_EXPORT uint32_t
195wl_display_get_object_id(struct wl_display *display, const char *interface)
196{
197 struct wl_global *global;
198
199 global = container_of(display->global_list.next,
200 struct wl_global, link);
201 while (&global->link != &display->global_list) {
202 if (strcmp(global->interface, interface) == 0)
203 return global->id;
204
205 global = container_of(global->link.next,
206 struct wl_global, link);
207 }
208
209 return 0;
210}
211
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500212WL_EXPORT int
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500213wl_display_get_fd(struct wl_display *display,
214 wl_display_update_func_t update, void *data)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400215{
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500216 display->update = update;
217 display->update_data = data;
218
219 display->update(display->mask, display->update_data);
220
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400221 return display->fd;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400222}
223
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500224static void
225handle_global(struct wl_display *display, uint32_t *p, uint32_t size)
226{
227 struct wl_global *global;
228 uint32_t length;
229
230 global = malloc(sizeof *global);
231 if (global == NULL)
232 return;
233
234 global->id = p[0];
235 length = p[1];
236 global->interface = malloc(length + 1);
237 if (global->interface == NULL) {
238 free(global);
239 return;
240 }
241 memcpy(global->interface, &p[2], length);
242 global->interface[length] = '\0';
243 global->version = p[2 + DIV_ROUNDUP(length, sizeof *p)];
244 wl_list_insert(display->global_list.prev, &global->link);
245
246 if (strcmp(global->interface, "visual") == 0)
247 add_visual(display, global);
248}
249
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400250static void
Kristian Høgsberg40979232008-11-25 22:40:39 -0500251handle_event(struct wl_display *display,
252 uint32_t object, uint32_t opcode, uint32_t size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400253{
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500254 uint32_t p[32];
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400255
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500256 wl_connection_copy(display->connection, p, size);
Kristian Høgsbergbf967b42008-12-21 20:25:16 -0500257 if (object == 1 && opcode == WL_DISPLAY_GLOBAL) {
258 handle_global(display, p + 2, size);
259 } else if (display->event_handler != NULL)
Kristian Høgsberg40979232008-11-25 22:40:39 -0500260 display->event_handler(display, object, opcode, size, p + 2,
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500261 display->event_handler_data);
262 wl_connection_consume(display->connection, size);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400263}
264
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500265WL_EXPORT void
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400266wl_display_iterate(struct wl_display *display, uint32_t mask)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400267{
Kristian Høgsberg40979232008-11-25 22:40:39 -0500268 uint32_t p[2], object, opcode, size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400269 int len;
270
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400271 len = wl_connection_data(display->connection, mask);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400272 while (len > 0) {
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400273 if (len < sizeof p)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400274 break;
275
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400276 wl_connection_copy(display->connection, p, sizeof p);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500277 object = p[0];
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400278 opcode = p[1] & 0xffff;
279 size = p[1] >> 16;
Kristian Høgsberg427524a2008-10-08 13:32:07 -0400280 if (len < size)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400281 break;
282
Kristian Høgsberg40979232008-11-25 22:40:39 -0500283 handle_event(display, object, opcode, size);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500284 len -= size;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400285 }
286
287 if (len < 0) {
288 fprintf(stderr, "read error: %m\n");
289 exit(EXIT_FAILURE);
290 }
291}
292
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500293WL_EXPORT void
Kristian Høgsberg5a27f3e2008-11-02 10:55:25 -0500294wl_display_set_event_handler(struct wl_display *display,
295 wl_display_event_func_t handler,
296 void *data)
297{
298 /* FIXME: This needs something more generic... */
299 display->event_handler = handler;
300 display->event_handler_data = data;
301}
302
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500303WL_EXPORT uint32_t
304wl_display_allocate_id(struct wl_display *display)
305{
306 return display->id++;
307}
308
309WL_EXPORT void
310wl_display_write(struct wl_display *display, const void *data, size_t count)
311{
312 wl_connection_write(display->connection, data, count);
313}
314
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500315WL_EXPORT struct wl_compositor *
316wl_display_get_compositor(struct wl_display *display)
317{
318 struct wl_compositor *compositor;
319 uint32_t id;
320
321 id = wl_display_get_object_id(display, "compositor");
322 if (id == 0)
323 return NULL;
324
325 compositor = malloc(sizeof *compositor);
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500326 compositor->proxy.interface = &wl_compositor_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500327 compositor->proxy.id = id;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500328 compositor->proxy.display = display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500329
330 return compositor;
331}
332
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500333static void
334wl_proxy_vmarshal(struct wl_proxy *target, uint32_t opcode, va_list ap)
335{
336 struct wl_proxy *proxy;
337 uint32_t args[32], length, *p, size;
338 const char *s, *signature;
339 int i, count;
340
341 signature = target->interface->methods[opcode].signature;
342 count = strlen(signature);
343 /* FIXME: Make sure we don't overwrite args array. */
344
345 p = &args[2];
346 for (i = 0; i < count; i++) {
347 switch (signature[i]) {
348 case 'u':
349 case 'i':
350 *p++ = va_arg(ap, uint32_t);
351 break;
352 case 's':
353 s = va_arg(ap, const char *);
354 length = strlen(s);
355 *p++ = length;
356 memcpy(p, s, length);
357 p += DIV_ROUNDUP(length, sizeof(*p));
358 break;
359 case 'n':
360 case 'o':
361 proxy = va_arg(ap, struct wl_proxy *);
362 *p++ = proxy->id;
363 break;
364 default:
365 assert(0);
366 break;
367 }
368 }
369
370 size = (p - args) * sizeof *p;
371 args[0] = target->id;
372 args[1] = opcode | (size << 16);
373 wl_connection_write(target->display->connection, args, size);
374}
375
376static void
377wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
378{
379 va_list ap;
380
381 va_start(ap, opcode);
382 wl_proxy_vmarshal(proxy, opcode, ap);
383 va_end(ap);
384}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400385
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500386WL_EXPORT struct wl_surface *
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500387wl_compositor_create_surface(struct wl_compositor *compositor)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400388{
389 struct wl_surface *surface;
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400390
391 surface = malloc(sizeof *surface);
392 if (surface == NULL)
393 return NULL;
394
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500395 surface->proxy.interface = &wl_surface_interface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500396 surface->proxy.id = wl_display_allocate_id(compositor->proxy.display);
397 surface->proxy.display = compositor->proxy.display;
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500398 wl_proxy_marshal(&compositor->proxy,
399 WL_COMPOSITOR_CREATE_SURFACE, surface);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400400
401 return surface;
402}
403
Kristian Høgsberg40979232008-11-25 22:40:39 -0500404WL_EXPORT void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500405wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
Kristian Høgsberg40979232008-11-25 22:40:39 -0500406{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500407 wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500408}
409
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500410WL_EXPORT void
411wl_surface_destroy(struct wl_surface *surface)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400412{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500413 wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400414}
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400415
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500416WL_EXPORT void
417wl_surface_attach(struct wl_surface *surface, uint32_t name,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500418 int32_t width, int32_t height, uint32_t stride,
419 struct wl_visual *visual)
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400420{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500421 wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
422 name, width, height, stride, visual);
Kristian Høgsberga67a71a2008-10-07 10:10:36 -0400423}
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400424
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500425WL_EXPORT void
426wl_surface_map(struct wl_surface *surface,
427 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400428{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500429 wl_proxy_marshal(&surface->proxy,
430 WL_SURFACE_MAP, x, y, width, height);
Kristian Høgsberg05eff512008-10-07 10:10:36 -0400431}
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500432
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500433WL_EXPORT void
434wl_surface_copy(struct wl_surface *surface, int32_t dst_x, int32_t dst_y,
435 uint32_t name, uint32_t stride,
436 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500437{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500438 wl_proxy_marshal(&surface->proxy, WL_SURFACE_COPY,
439 dst_x, dst_y, name, stride, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500440}
441
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500442WL_EXPORT void
443wl_surface_damage(struct wl_surface *surface,
444 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500445{
Kristian Høgsbergfe831a72008-12-21 21:50:23 -0500446 wl_proxy_marshal(&surface->proxy,
447 WL_SURFACE_DAMAGE, x, y, width, height);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500448}