Change scanner.c license to MIT
[profile/ivi/wayland.git] / src / 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 <stdbool.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33 #include <ctype.h>
34 #include <assert.h>
35 #include <fcntl.h>
36 #include <sys/poll.h>
37
38 #include "connection.h"
39 #include "wayland-util.h"
40 #include "wayland-client.h"
41
42 struct wl_global_listener {
43         wl_display_global_func_t handler;
44         void *data;
45         struct wl_list link;
46 };
47
48 struct wl_proxy {
49         struct wl_object object;
50         struct wl_display *display;
51         void *user_data;
52 };
53
54 struct wl_sync_handler {
55         wl_display_sync_func_t func;
56         uint32_t key;
57         void *data;
58         struct wl_list link;
59 };
60
61 struct wl_frame_handler {
62         wl_display_frame_func_t func;
63         uint32_t key;
64         void *data;
65         struct wl_surface *surface;
66         struct wl_list link;
67 };
68
69 struct wl_global {
70         uint32_t id;
71         char *interface;
72         uint32_t version;
73         struct wl_list link;
74 };
75
76 struct wl_display {
77         struct wl_proxy proxy;
78         struct wl_connection *connection;
79         int fd;
80         uint32_t id, id_count, next_range;
81         uint32_t mask;
82         struct wl_hash_table *objects;
83         struct wl_list global_listener_list;
84         struct wl_list global_list;
85
86         wl_display_update_func_t update;
87         void *update_data;
88
89         wl_display_global_func_t global_handler;
90         void *global_handler_data;
91
92         struct wl_list sync_list, frame_list;
93         uint32_t key;
94 };
95
96 static int wl_debug = 0;
97
98 static int
99 connection_update(struct wl_connection *connection,
100                   uint32_t mask, void *data)
101 {
102         struct wl_display *display = data;
103
104         display->mask = mask;
105         if (display->update)
106                 return display->update(display->mask,
107                                        display->update_data);
108
109         return 0;
110 }
111
112 WL_EXPORT struct wl_global_listener *
113 wl_display_add_global_listener(struct wl_display *display,
114                                wl_display_global_func_t handler, void *data)
115 {
116         struct wl_global_listener *listener;
117         struct wl_global *global;
118
119         listener = malloc(sizeof *listener);
120         if (listener == NULL)
121                 return NULL;
122
123         listener->handler = handler;
124         listener->data = data;
125         wl_list_insert(display->global_listener_list.prev, &listener->link);
126
127         wl_list_for_each(global, &display->global_list, link)
128                 (*listener->handler)(display, global->id, global->interface,
129                                      global->version, listener->data);
130
131         return listener;
132 }
133
134 WL_EXPORT void
135 wl_display_remove_global_listener(struct wl_display *display,
136                                   struct wl_global_listener *listener)
137 {
138         wl_list_remove(&listener->link);
139         free(listener);
140 }
141
142 WL_EXPORT struct wl_proxy *
143 wl_proxy_create_for_id(struct wl_display *display,
144                        const struct wl_interface *interface, uint32_t id)
145 {
146         struct wl_proxy *proxy;
147
148         proxy = malloc(sizeof *proxy);
149         if (proxy == NULL)
150                 return NULL;
151
152         proxy->object.interface = interface;
153         proxy->object.implementation = NULL;
154         proxy->object.id = id;
155         proxy->display = display;
156         wl_hash_table_insert(display->objects, proxy->object.id, proxy);
157
158         return proxy;
159 }
160
161 WL_EXPORT struct wl_proxy *
162 wl_proxy_create(struct wl_proxy *factory,
163                 const struct wl_interface *interface)
164 {
165         return wl_proxy_create_for_id(factory->display, interface,
166                                       wl_display_allocate_id(factory->display));
167 }
168
169 WL_EXPORT void
170 wl_proxy_destroy(struct wl_proxy *proxy)
171 {
172         wl_hash_table_remove(proxy->display->objects, proxy->object.id);
173         free(proxy);
174 }
175
176 WL_EXPORT int
177 wl_proxy_add_listener(struct wl_proxy *proxy,
178                       void (**implementation)(void), void *data)
179 {
180         if (proxy->object.implementation) {
181                 fprintf(stderr, "proxy already has listener\n");
182                 return -1;
183         }
184
185         proxy->object.implementation = implementation;
186         proxy->user_data = data;
187
188         return 0;
189 }
190
191 WL_EXPORT void
192 wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
193 {
194         struct wl_closure *closure;
195         va_list ap;
196
197         va_start(ap, opcode);
198         closure = wl_connection_vmarshal(proxy->display->connection,
199                                          &proxy->object, opcode, ap,
200                                          &proxy->object.interface->methods[opcode]);
201         va_end(ap);
202
203         wl_closure_send(closure, proxy->display->connection);
204
205         if (wl_debug)
206                 wl_closure_print(closure, &proxy->object, true);
207
208         wl_closure_destroy(closure);
209 }
210
211 /* Can't do this, there may be more than one instance of an
212  * interface... */
213 WL_EXPORT uint32_t
214 wl_display_get_global(struct wl_display *display,
215                       const char *interface, uint32_t version)
216 {
217         struct wl_global *global;
218
219         wl_list_for_each(global, &display->global_list, link)
220                 if (strcmp(interface, global->interface) == 0 &&
221                     version <= global->version)
222                         return global->id;
223
224         return 0;
225 }
226
227 static void
228 display_handle_error(void *data,
229                      struct wl_display *display, struct wl_object *object,
230                      uint32_t code, const char *message)
231 {
232         fprintf(stderr, "%s@%d: error %d: %s\n",
233                 object->interface->name, object->id, code, message);
234         abort();
235 }
236
237 static void
238 display_handle_global(void *data,
239                       struct wl_display *display,
240                       uint32_t id, const char *interface, uint32_t version)
241 {
242         struct wl_global_listener *listener;
243         struct wl_global *global;
244
245         if (strcmp(interface, "wl_display") == 0)
246                 wl_hash_table_insert(display->objects,
247                                      id, &display->proxy.object);
248
249         global = malloc(sizeof *global);
250         global->id = id;
251         global->interface = strdup(interface);
252         global->version = version;
253         wl_list_insert(display->global_list.prev, &global->link);
254
255         wl_list_for_each(listener, &display->global_listener_list, link)
256                 (*listener->handler)(display,
257                                      id, interface, version, listener->data);
258 }
259
260 static void
261 display_handle_global_remove(void *data,
262                              struct wl_display *display, uint32_t id)
263 {
264         struct wl_global *global;
265
266         wl_list_for_each(global, &display->global_list, link)
267                 if (global->id == id) {
268                         wl_list_remove(&global->link);
269                         free(global);
270                         break;
271                 }
272 }
273
274 static void
275 display_handle_range(void *data,
276                      struct wl_display *display, uint32_t range)
277 {
278         display->next_range = range;
279 }
280
281 static void
282 display_handle_key(void *data,
283                    struct wl_display *display, uint32_t key, uint32_t time)
284 {
285         struct wl_sync_handler *sync_handler;
286         struct wl_frame_handler *frame_handler;
287
288         sync_handler = container_of(display->sync_list.next,
289                                     struct wl_sync_handler, link);
290         if (!wl_list_empty(&display->sync_list) && sync_handler->key == key) {
291                 wl_list_remove(&sync_handler->link);
292                 sync_handler->func(sync_handler->data);
293                 free(sync_handler);
294                 return;
295         }
296
297         frame_handler = container_of(display->frame_list. next,
298                                      struct wl_frame_handler, link);
299         if (!wl_list_empty(&display->frame_list) &&
300             frame_handler->key == key) {
301                 wl_list_remove(&frame_handler->link);
302                 frame_handler->func(frame_handler->surface,
303                                     frame_handler->data, time);
304                 free(frame_handler);
305                 return;
306         }
307
308         fprintf(stderr, "unsolicited sync event, client gone?\n");
309 }
310
311 static const struct wl_display_listener display_listener = {
312         display_handle_error,
313         display_handle_global,
314         display_handle_global_remove,
315         display_handle_range,
316         display_handle_key
317 };
318
319 static int
320 connect_to_socket(struct wl_display *display, const char *name)
321 {
322         struct sockaddr_un addr;
323         socklen_t size;
324         const char *runtime_dir;
325         size_t name_size;
326
327         display->fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
328         if (display->fd < 0)
329                 return -1;
330
331         runtime_dir = getenv("XDG_RUNTIME_DIR");
332         if (runtime_dir == NULL) {
333                 runtime_dir = ".";
334                 fprintf(stderr,
335                         "XDG_RUNTIME_DIR not set, falling back to %s\n",
336                         runtime_dir);
337         }
338
339         if (name == NULL)
340                 name = getenv("WAYLAND_DISPLAY");
341         if (name == NULL)
342                 name = "wayland-0";
343
344         memset(&addr, 0, sizeof addr);
345         addr.sun_family = AF_LOCAL;
346         name_size =
347                 snprintf(addr.sun_path, sizeof addr.sun_path,
348                          "%s/%s", runtime_dir, name) + 1;
349
350         size = offsetof (struct sockaddr_un, sun_path) + name_size;
351
352         if (connect(display->fd, (struct sockaddr *) &addr, size) < 0) {
353                 close(display->fd);
354                 return -1;
355         }
356
357         return 0;
358 }
359
360 WL_EXPORT struct wl_display *
361 wl_display_connect(const char *name)
362 {
363         struct wl_display *display;
364         const char *debug;
365         char *connection, *end;
366         int flags;
367
368         debug = getenv("WAYLAND_DEBUG");
369         if (debug)
370                 wl_debug = 1;
371
372         display = malloc(sizeof *display);
373         if (display == NULL)
374                 return NULL;
375
376         memset(display, 0, sizeof *display);
377         connection = getenv("WAYLAND_SOCKET");
378         if (connection) {
379                 display->fd = strtol(connection, &end, 0);
380                 if (*end != '\0') {
381                         free(display);
382                         return NULL;
383                 }
384                 flags = fcntl(display->fd, F_GETFD);
385                 if (flags != -1)
386                         fcntl(display->fd, F_SETFD, flags | FD_CLOEXEC);
387         } else if (connect_to_socket(display, name) < 0) {
388                 free(display);
389                 return NULL;
390         }
391
392         display->objects = wl_hash_table_create();
393         if (display->objects == NULL) {
394                 close(display->fd);
395                 free(display);
396                 return NULL;
397         }
398         wl_list_init(&display->global_listener_list);
399         wl_list_init(&display->global_list);
400
401         display->proxy.object.interface = &wl_display_interface;
402         display->proxy.object.id = 1;
403         display->proxy.display = display;
404
405         wl_list_init(&display->sync_list);
406         wl_list_init(&display->frame_list);
407
408         display->proxy.object.implementation =
409                 (void(**)(void)) &display_listener;
410         display->proxy.user_data = display;
411
412         display->connection = wl_connection_create(display->fd,
413                                                    connection_update,
414                                                    display);
415         if (display->connection == NULL) {
416                 wl_hash_table_destroy(display->objects);
417                 close(display->fd);
418                 free(display);
419                 return NULL;
420         }
421
422         wl_display_bind(display, 1, "wl_display", 1);
423
424         return display;
425 }
426
427 WL_EXPORT void
428 wl_display_destroy(struct wl_display *display)
429 {
430         struct wl_global *global, *gnext;
431         struct wl_global_listener *listener, *lnext;
432
433         wl_connection_destroy(display->connection);
434         wl_hash_table_destroy(display->objects);
435         wl_list_for_each_safe(global, gnext,
436                               &display->global_list, link)
437                 free(global);
438         wl_list_for_each_safe(listener, lnext,
439                               &display->global_listener_list, link)
440                 free(listener);
441
442         close(display->fd);
443         free(display);
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 WL_EXPORT int
459 wl_display_sync_callback(struct wl_display *display,
460                          wl_display_sync_func_t func, void *data)
461 {
462         struct wl_sync_handler *handler;
463
464         handler = malloc(sizeof *handler);
465         if (handler == NULL)
466                 return -1;
467
468         handler->func = func;
469         handler->key = display->key++;
470         handler->data = data;
471
472         wl_list_insert(display->sync_list.prev, &handler->link);
473         wl_display_sync(display, handler->key);
474
475         return 0;
476 }
477
478 WL_EXPORT int
479 wl_display_frame_callback(struct wl_display *display,
480                           struct wl_surface *surface,
481                           wl_display_frame_func_t func, void *data)
482 {
483         struct wl_frame_handler *handler;
484
485         handler = malloc(sizeof *handler);
486         if (handler == NULL)
487                 return -1;
488
489         handler->func = func;
490         handler->key = display->key++;
491         handler->data = data;
492         handler->surface = surface;
493
494         wl_list_insert(display->frame_list.prev, &handler->link);
495         wl_display_frame(display, handler->surface, handler->key);
496
497         return 0;
498 }
499
500 static void
501 handle_event(struct wl_display *display,
502              uint32_t id, uint32_t opcode, uint32_t size)
503 {
504         uint32_t p[32];
505         struct wl_proxy *proxy;
506         struct wl_closure *closure;
507         const struct wl_message *message;
508
509         wl_connection_copy(display->connection, p, size);
510         if (id == 1)
511                 proxy = &display->proxy;
512         else
513                 proxy = wl_hash_table_lookup(display->objects, id);
514
515         if (proxy == NULL || proxy->object.implementation == NULL) {
516                 wl_connection_consume(display->connection, size);
517                 return;
518         }
519
520         message = &proxy->object.interface->events[opcode];
521         closure = wl_connection_demarshal(display->connection,
522                                           size, display->objects, message);
523
524         if (closure == NULL) {
525                 fprintf(stderr, "Error demarshalling event: %m\n");
526                 abort();
527         }
528
529         if (wl_debug)
530                 wl_closure_print(closure, &proxy->object, false);
531
532         wl_closure_invoke(closure, &proxy->object,
533                           proxy->object.implementation[opcode],
534                           proxy->user_data);
535
536         wl_closure_destroy(closure);
537 }
538
539 WL_EXPORT void
540 wl_display_iterate(struct wl_display *display, uint32_t mask)
541 {
542         uint32_t p[2], object, opcode, size;
543         int len;
544
545         mask &= display->mask;
546         if (mask == 0) {
547                 fprintf(stderr,
548                         "wl_display_iterate called with unsolicited flags");
549                 return;
550         }
551
552         len = wl_connection_data(display->connection, mask);
553         while (len > 0) {
554                 if (len < sizeof p)
555                         break;
556                 
557                 wl_connection_copy(display->connection, p, sizeof p);
558                 object = p[0];
559                 opcode = p[1] & 0xffff;
560                 size = p[1] >> 16;
561                 if (len < size)
562                         break;
563
564                 handle_event(display, object, opcode, size);
565                 len -= size;
566         }
567
568         if (len < 0) {
569                 fprintf(stderr, "read error: %m\n");
570                 exit(EXIT_FAILURE);
571         }
572 }
573
574 WL_EXPORT void
575 wl_display_flush(struct wl_display *display)
576 {
577         while (display->mask & WL_DISPLAY_WRITABLE)
578                 wl_display_iterate (display, WL_DISPLAY_WRITABLE);
579 }
580
581 WL_EXPORT uint32_t
582 wl_display_allocate_id(struct wl_display *display)
583 {
584         if (display->id_count == 0) {
585                 display->id_count = 256;
586                 display->id = display->next_range;
587         }
588
589         display->id_count--;
590
591         return display->id++;
592 }
593
594 WL_EXPORT void
595 wl_proxy_set_user_data(struct wl_proxy *proxy, void *user_data)
596 {
597         proxy->user_data = user_data;
598 }
599
600 WL_EXPORT void *
601 wl_proxy_get_user_data(struct wl_proxy *proxy)
602 {
603         return proxy->user_data;
604 }