Drop libdrm CFLAGS where no longer necessary.
[profile/ivi/wayland.git] / wayland-client.c
1 /*
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
23 #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>
33 #include <assert.h>
34 #include <sys/poll.h>
35
36 #include "wayland-protocol.h"
37 #include "connection.h"
38 #include "wayland-util.h"
39 #include "wayland-client.h"
40
41 static const char socket_name[] = "\0wayland";
42
43 struct wl_global {
44         uint32_t id;
45         char *interface;
46         uint32_t version;
47         struct wl_proxy *proxy;
48         struct wl_list link;
49 };
50
51 struct wl_global_listener {
52         wl_display_global_func_t handler;
53         void *data;
54         struct wl_list link;
55 };
56
57 struct wl_listener {
58         void (**implementation)(void);
59         void *data;
60         struct wl_list link;
61 };
62
63 struct wl_proxy {
64         struct wl_object base;
65         struct wl_display *display;
66         struct wl_list listener_list;
67 };
68
69 struct wl_compositor {
70         struct wl_proxy proxy;
71 };
72
73 struct wl_surface {
74         struct wl_proxy proxy;
75 };
76
77 struct wl_visual {
78         struct wl_proxy proxy;
79 };
80
81 struct wl_output {
82         struct wl_proxy proxy;
83         struct wl_listener listener;
84         int32_t width, height;
85 };
86
87 struct wl_input_device {
88         struct wl_proxy proxy;
89 };
90
91 struct 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
115 static int
116 connection_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
129 WL_EXPORT int
130 wl_object_implements(struct wl_object *object,
131                      const char *interface, int version)
132 {
133         return strcmp(object->interface->name, interface) == 0 &&
134                 object->interface->version >= version;
135 }
136
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)
140 {
141         struct wl_global_listener *listener;
142         struct wl_global *global;
143
144         listener = malloc(sizeof *listener);
145         if (listener == NULL)
146                 return NULL;
147
148         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;
165 }
166
167 WL_EXPORT void
168 wl_display_remove_global_listener(struct wl_display *display,
169                                   struct wl_global_listener *listener)
170 {
171         wl_list_remove(&listener->link);
172         free(listener);
173 }
174
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)
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
205 static int
206 wl_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
221 static void
222 wl_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
233 WL_EXPORT int
234 wl_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);
240 }
241
242 static void
243 add_visual(struct wl_display *display, struct wl_global *global)
244 {
245         struct wl_visual *visual;
246
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;
254         else
255                 display->rgb_visual = visual;
256 }
257
258 WL_EXPORT struct wl_visual *
259 wl_display_get_argb_visual(struct wl_display *display)
260 {
261         return display->argb_visual;
262 }
263
264 WL_EXPORT struct wl_visual *
265 wl_display_get_premultiplied_argb_visual(struct wl_display *display)
266 {
267         return display->premultiplied_argb_visual;
268 }
269
270 WL_EXPORT struct wl_visual *
271 wl_display_get_rgb_visual(struct wl_display *display)
272 {
273         return display->rgb_visual;
274 }
275
276 WL_EXPORT int
277 wl_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);
283 }
284
285 static void
286 display_handle_invalid_object(void *data,
287                               struct wl_display *display, uint32_t id)
288 {
289         fprintf(stderr, "sent request to invalid object\n");
290 }
291                               
292 static void
293 display_handle_invalid_method(void *data, 
294                               struct wl_display *display,
295                               uint32_t id, uint32_t opcode)
296 {
297         fprintf(stderr, "sent invalid request opcode\n");
298 }
299
300 static void
301 display_handle_no_memory(void *data,
302                          struct wl_display *display)
303 {
304         fprintf(stderr, "server out of memory\n");
305 }
306
307 static void
308 display_handle_global(void *data,
309                       struct wl_display *display,
310                       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);
322
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);
337 }
338
339 static void
340 display_handle_range(void *data,
341                      struct wl_display *display, uint32_t range)
342 {
343         display->next_range = range;
344 }
345
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);
357 };
358
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,
364         display_handle_range
365 };
366
367 WL_EXPORT struct wl_display *
368 wl_display_create(const char *name, size_t name_size)
369 {
370         struct wl_display *display;
371         struct sockaddr_un addr;
372         socklen_t size;
373
374         display = malloc(sizeof *display);
375         if (display == NULL)
376                 return NULL;
377
378         memset(display, 0, sizeof *display);
379         display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
380         if (display->fd < 0) {
381                 free(display);
382                 return NULL;
383         }
384
385         addr.sun_family = AF_LOCAL;
386         memcpy(addr.sun_path, name, name_size);
387
388         size = offsetof (struct sockaddr_un, sun_path) + name_size;
389
390         if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
391                 close(display->fd);
392                 free(display);
393                 return NULL;
394         }
395
396         display->objects = wl_hash_create();
397         wl_list_init(&display->global_list);
398         wl_list_init(&display->global_listener_list);
399
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);
404
405         display->listener.implementation = (void(**)(void)) &display_listener;
406         wl_list_insert(display->proxy.listener_list.prev, &display->listener.link);
407
408         display->connection = wl_connection_create(display->fd,
409                                                    connection_update,
410                                                    display);
411
412         /* Process connection events. */
413         wl_display_iterate(display, WL_CONNECTION_READABLE);
414
415         return display;
416 }
417
418 WL_EXPORT void
419 wl_display_destroy(struct wl_display *display)
420 {
421         wl_connection_destroy(display->connection);
422         close(display->fd);
423         free(display);
424 }
425
426 WL_EXPORT uint32_t
427 wl_display_get_object_id(struct wl_display *display,
428                          const char *interface, uint32_t version)
429 {
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) {
435                 if (strcmp(global->interface, interface) == 0 &&
436                     global->version >= version)
437                         return global->id;
438
439                 global = container_of(global->link.next,
440                                       struct wl_global, link);
441         }
442
443         return 0;
444 }
445
446 WL_EXPORT int
447 wl_display_get_fd(struct wl_display *display,
448                   wl_display_update_func_t update, void *data)
449 {
450         display->update = update;
451         display->update_data = data;
452
453         display->update(display->mask, display->update_data);
454
455         return display->fd;
456 }
457
458 static void
459 handle_event(struct wl_display *display,
460              uint32_t id, uint32_t opcode, uint32_t size)
461 {
462         uint32_t p[32];
463         struct wl_listener *listener;
464         struct wl_proxy *proxy;
465
466         wl_connection_copy(display->connection, p, size);
467         if (id == 1)
468                 proxy = &display->proxy;
469         else
470                 proxy = (struct wl_proxy *) wl_hash_lookup(display->objects, id);
471
472         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         }
494
495         wl_connection_consume(display->connection, size);
496 }
497
498 WL_EXPORT void
499 wl_display_iterate(struct wl_display *display, uint32_t mask)
500 {
501         uint32_t p[2], object, opcode, size;
502         int len;
503
504         len = wl_connection_data(display->connection, mask);
505         while (len > 0) {
506                 if (len < sizeof p)
507                         break;
508                 
509                 wl_connection_copy(display->connection, p, sizeof p);
510                 object = p[0];
511                 opcode = p[1] & 0xffff;
512                 size = p[1] >> 16;
513                 if (len < size)
514                         break;
515
516                 handle_event(display, object, opcode, size);
517                 len -= size;
518         }
519
520         if (len < 0) {
521                 fprintf(stderr, "read error: %m\n");
522                 exit(EXIT_FAILURE);
523         }
524 }
525
526 WL_EXPORT uint32_t
527 wl_display_allocate_id(struct wl_display *display)
528 {
529         if (display->id_count == 0) {
530                 display->id_count = 256;
531                 display->id = display->next_range;
532         }
533
534         display->id_count--;
535
536         return display->id++;
537 }
538
539 WL_EXPORT void
540 wl_display_write(struct wl_display *display, const void *data, size_t count)
541 {
542         wl_connection_write(display->connection, data, count);
543 }
544
545 WL_EXPORT struct wl_compositor *
546 wl_display_get_compositor(struct wl_display *display)
547 {
548         return display->compositor;
549 }
550
551 WL_EXPORT int
552 wl_compositor_add_listener(struct wl_compositor *compositor,
553                            const struct wl_compositor_listener *listener,
554                            void *data)
555 {
556         return wl_proxy_add_listener(&compositor->proxy,
557                                      (void (**)(void)) listener, data);
558 }
559
560 WL_EXPORT struct wl_surface *
561 wl_compositor_create_surface(struct wl_compositor *compositor)
562 {
563         struct wl_surface *surface;
564
565         surface = malloc(sizeof *surface);
566         if (surface == NULL)
567                 return NULL;
568
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);
574
575         return surface;
576 }
577
578 WL_EXPORT void
579 wl_compositor_commit(struct wl_compositor *compositor, uint32_t key)
580 {
581         wl_proxy_marshal(&compositor->proxy, WL_COMPOSITOR_COMMIT, key);
582 }
583
584 WL_EXPORT void
585 wl_surface_destroy(struct wl_surface *surface)
586 {
587         wl_proxy_marshal(&surface->proxy, WL_SURFACE_DESTROY);
588 }
589
590 WL_EXPORT void
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)
594 {
595         wl_proxy_marshal(&surface->proxy, WL_SURFACE_ATTACH,
596                          name, width, height, stride, visual);
597 }
598
599 WL_EXPORT void
600 wl_surface_map(struct wl_surface *surface,
601                int32_t x, int32_t y, int32_t width, int32_t height)
602 {
603         wl_proxy_marshal(&surface->proxy,
604                          WL_SURFACE_MAP, x, y, width, height);
605 }
606
607 WL_EXPORT void
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)
611 {
612         wl_proxy_marshal(&surface->proxy, WL_SURFACE_COPY,
613                          dst_x, dst_y, name, stride, x, y, width, height);
614 }
615
616 WL_EXPORT void
617 wl_surface_damage(struct wl_surface *surface,
618                   int32_t x, int32_t y, int32_t width, int32_t height)
619 {
620         wl_proxy_marshal(&surface->proxy,
621                          WL_SURFACE_DAMAGE, x, y, width, height);
622 }