9f07b88987c6c636e8c5a2cb0d913c832dac52d3
[profile/ivi/wayland.git] / src / wayland-server.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 #define _GNU_SOURCE
24
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <dlfcn.h>
37 #include <assert.h>
38 #include <sys/time.h>
39 #include <fcntl.h>
40 #include <sys/file.h>
41 #include <sys/stat.h>
42 #include <ffi.h>
43
44 #include "wayland-private.h"
45 #include "wayland-server.h"
46 #include "wayland-server-protocol.h"
47 #include "wayland-os.h"
48
49 struct wl_socket {
50         int fd;
51         int fd_lock;
52         struct sockaddr_un addr;
53         char lock_addr[113];
54         struct wl_list link;
55         struct wl_event_source *source;
56 };
57
58 struct wl_client {
59         struct wl_connection *connection;
60         struct wl_event_source *source;
61         struct wl_display *display;
62         struct wl_resource *display_resource;
63         uint32_t id_count;
64         uint32_t mask;
65         struct wl_list link;
66         struct wl_map objects;
67         struct wl_signal destroy_signal;
68         struct ucred ucred;
69         int error;
70 };
71
72 struct wl_display {
73         struct wl_event_loop *loop;
74         int run;
75
76         uint32_t id;
77         uint32_t serial;
78
79         struct wl_list global_list;
80         struct wl_list socket_list;
81         struct wl_list client_list;
82 };
83
84 struct wl_global {
85         const struct wl_interface *interface;
86         uint32_t name;
87         void *data;
88         wl_global_bind_func_t bind;
89         struct wl_list link;
90 };
91
92 static int wl_debug = 0;
93
94 static void
95 destroy_client(void *data)
96 {
97         struct wl_client *client = data;
98
99         wl_client_destroy(client);
100 }
101
102 WL_EXPORT void
103 wl_resource_post_event(struct wl_resource *resource, uint32_t opcode, ...)
104 {
105         struct wl_closure closure;
106         struct wl_object *object = &resource->object;
107         va_list ap;
108         int ret;
109
110         va_start(ap, opcode);
111         ret = wl_closure_vmarshal(&closure, object, opcode, ap,
112                                   &object->interface->events[opcode]);
113         va_end(ap);
114
115         if (ret)
116                 return;
117
118         if (wl_closure_send(&closure, resource->client->connection))
119                 wl_event_loop_add_idle(resource->client->display->loop,
120                                        destroy_client, resource->client);
121
122         if (wl_debug)
123                 wl_closure_print(&closure, object, true);
124
125         wl_closure_destroy(&closure);
126 }
127
128
129 WL_EXPORT void
130 wl_resource_queue_event(struct wl_resource *resource, uint32_t opcode, ...)
131 {
132         struct wl_closure closure;
133         struct wl_object *object = &resource->object;
134         va_list ap;
135         int ret;
136
137         va_start(ap, opcode);
138         ret = wl_closure_vmarshal(&closure, object, opcode, ap,
139                                   &object->interface->events[opcode]);
140         va_end(ap);
141
142         if (ret)
143                 return;
144
145         if (wl_closure_queue(&closure, resource->client->connection))
146                 wl_event_loop_add_idle(resource->client->display->loop,
147                                        destroy_client, resource->client);
148
149         if (wl_debug)
150                 wl_closure_print(&closure, object, true);
151
152         wl_closure_destroy(&closure);
153 }
154
155 WL_EXPORT void
156 wl_resource_post_error(struct wl_resource *resource,
157                        uint32_t code, const char *msg, ...)
158 {
159         struct wl_client *client = resource->client;
160         char buffer[128];
161         va_list ap;
162
163         va_start(ap, msg);
164         vsnprintf(buffer, sizeof buffer, msg, ap);
165         va_end(ap);
166
167         client->error = 1;
168
169         /*
170          * When a client aborts, its resources are destroyed in id order,
171          * which means the display resource is destroyed first. If destruction
172          * of any later resources results in a protocol error, we end up here
173          * with a NULL display_resource. Do not try to send errors to an
174          * already dead client.
175          */
176         if (!client->display_resource)
177                 return;
178
179         wl_resource_post_event(client->display_resource,
180                                WL_DISPLAY_ERROR, resource, code, buffer);
181 }
182
183 static int
184 wl_client_connection_data(int fd, uint32_t mask, void *data)
185 {
186         struct wl_client *client = data;
187         struct wl_connection *connection = client->connection;
188         struct wl_resource *resource;
189         struct wl_object *object;
190         struct wl_closure closure;
191         const struct wl_message *message;
192         uint32_t p[2];
193         int opcode, size;
194         uint32_t cmask = 0;
195         int len, ret;
196
197         if (mask & WL_EVENT_READABLE)
198                 cmask |= WL_CONNECTION_READABLE;
199         if (mask & WL_EVENT_WRITABLE)
200                 cmask |= WL_CONNECTION_WRITABLE;
201
202         len = wl_connection_data(connection, cmask);
203         if (len < 0) {
204                 wl_client_destroy(client);
205                 return 1;
206         }
207
208         while ((size_t) len >= sizeof p) {
209                 wl_connection_copy(connection, p, sizeof p);
210                 opcode = p[1] & 0xffff;
211                 size = p[1] >> 16;
212                 if (len < size)
213                         break;
214
215                 resource = wl_map_lookup(&client->objects, p[0]);
216                 if (resource == NULL) {
217                         wl_resource_post_error(client->display_resource,
218                                                WL_DISPLAY_ERROR_INVALID_OBJECT,
219                                                "invalid object %d", p[0]);
220                         break;
221                 }
222
223                 object = &resource->object;
224                 if (opcode >= object->interface->method_count) {
225                         wl_resource_post_error(client->display_resource,
226                                                WL_DISPLAY_ERROR_INVALID_METHOD,
227                                                "invalid method %d, object %s@%d",
228                                                opcode,
229                                                object->interface->name,
230                                                object->id);
231                         break;
232                 }
233
234                 message = &object->interface->methods[opcode];
235                 ret = wl_connection_demarshal(client->connection, &closure,
236                                               size, &client->objects, message);
237                 len -= size;
238
239                 if (ret && errno == EINVAL) {
240                         wl_resource_post_error(client->display_resource,
241                                                WL_DISPLAY_ERROR_INVALID_METHOD,
242                                                "invalid arguments for %s@%d.%s",
243                                                object->interface->name,
244                                                object->id,
245                                                message->name);
246                         break;
247                 } else if (ret && errno == ENOMEM) {
248                         wl_resource_post_no_memory(resource);
249                         break;
250                 }
251
252                 if (wl_debug)
253                         wl_closure_print(&closure, object, false);
254
255                 wl_closure_invoke(&closure, object,
256                                   object->implementation[opcode], client);
257
258                 wl_closure_destroy(&closure);
259
260                 if (client->error)
261                         break;
262         }
263
264         if (client->error)
265                 wl_client_destroy(client);
266
267         return 1;
268 }
269
270 static int
271 wl_client_connection_update(struct wl_connection *connection,
272                             uint32_t mask, void *data)
273 {
274         struct wl_client *client = data;
275         uint32_t emask = 0;
276
277         client->mask = mask;
278         if (mask & WL_CONNECTION_READABLE)
279                 emask |= WL_EVENT_READABLE;
280         if (mask & WL_CONNECTION_WRITABLE)
281                 emask |= WL_EVENT_WRITABLE;
282
283         return wl_event_source_fd_update(client->source, emask);
284 }
285
286 WL_EXPORT void
287 wl_client_flush(struct wl_client *client)
288 {
289         if (client->mask & WL_CONNECTION_WRITABLE)
290                 wl_connection_data(client->connection, WL_CONNECTION_WRITABLE);
291 }
292
293 WL_EXPORT struct wl_display *
294 wl_client_get_display(struct wl_client *client)
295 {
296         return client->display;
297 }
298
299 static void
300 bind_display(struct wl_client *client,
301              void *data, uint32_t version, uint32_t id);
302
303 WL_EXPORT struct wl_client *
304 wl_client_create(struct wl_display *display, int fd)
305 {
306         struct wl_client *client;
307         socklen_t len;
308
309         client = malloc(sizeof *client);
310         if (client == NULL)
311                 return NULL;
312
313         memset(client, 0, sizeof *client);
314         client->display = display;
315         client->source = wl_event_loop_add_fd(display->loop, fd,
316                                               WL_EVENT_READABLE,
317                                               wl_client_connection_data, client);
318
319         len = sizeof client->ucred;
320         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
321                        &client->ucred, &len) < 0) {
322                 free(client);
323                 return NULL;
324         }
325
326         client->connection =
327                 wl_connection_create(fd, wl_client_connection_update, client);
328         if (client->connection == NULL) {
329                 free(client);
330                 return NULL;
331         }
332
333         wl_map_init(&client->objects);
334
335         if (wl_map_insert_at(&client->objects, 0, NULL) < 0) {
336                 wl_map_release(&client->objects);
337                 free(client);
338                 return NULL;
339         }
340
341         wl_signal_init(&client->destroy_signal);
342         bind_display(client, display, 1, 1);
343
344         wl_list_insert(display->client_list.prev, &client->link);
345
346         return client;
347 }
348
349 WL_EXPORT void
350 wl_client_get_credentials(struct wl_client *client,
351                           pid_t *pid, uid_t *uid, gid_t *gid)
352 {
353         if (pid)
354                 *pid = client->ucred.pid;
355         if (uid)
356                 *uid = client->ucred.uid;
357         if (gid)
358                 *gid = client->ucred.gid;
359 }
360
361 WL_EXPORT void
362 wl_client_add_resource(struct wl_client *client,
363                        struct wl_resource *resource)
364 {
365         if (resource->object.id == 0)
366                 resource->object.id =
367                         wl_map_insert_new(&client->objects,
368                                           WL_MAP_SERVER_SIDE, resource);
369         else
370                 wl_map_insert_at(&client->objects,
371                                  resource->object.id, resource);
372
373         resource->client = client;
374         wl_signal_init(&resource->destroy_signal);
375 }
376
377 WL_EXPORT struct wl_resource *
378 wl_client_get_object(struct wl_client *client, uint32_t id)
379 {
380         return wl_map_lookup(&client->objects, id);
381 }
382
383 WL_EXPORT void
384 wl_resource_post_no_memory(struct wl_resource *resource)
385 {
386         wl_resource_post_error(resource->client->display_resource,
387                                WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
388 }
389
390 static void
391 destroy_resource(void *element, void *data)
392 {
393         struct wl_resource *resource = element;
394
395         wl_signal_emit(&resource->destroy_signal, resource);
396
397         if (resource->destroy)
398                 resource->destroy(resource);
399 }
400
401 WL_EXPORT void
402 wl_resource_destroy(struct wl_resource *resource)
403 {
404         struct wl_client *client = resource->client;
405         uint32_t id;
406
407         id = resource->object.id;
408         destroy_resource(resource, NULL);
409
410         if (id < WL_SERVER_ID_START) {
411                 if (client->display_resource) {
412                         wl_resource_queue_event(client->display_resource,
413                                                 WL_DISPLAY_DELETE_ID, id);
414                 }
415                 wl_map_insert_at(&client->objects, id, NULL);
416         } else {
417                 wl_map_remove(&client->objects, id);
418         }
419 }
420
421 WL_EXPORT void
422 wl_client_add_destroy_listener(struct wl_client *client,
423                                struct wl_listener *listener)
424 {
425         wl_signal_add(&client->destroy_signal, listener);
426 }
427
428 WL_EXPORT struct wl_listener *
429 wl_client_get_destroy_listener(struct wl_client *client,
430                                wl_notify_func_t notify)
431 {
432         return wl_signal_get(&client->destroy_signal, notify);
433 }
434
435 WL_EXPORT void
436 wl_client_destroy(struct wl_client *client)
437 {
438         uint32_t serial = 0;
439         
440         printf("disconnect from client %p\n", client);
441
442         wl_signal_emit(&client->destroy_signal, client);
443
444         wl_client_flush(client);
445         wl_map_for_each(&client->objects, destroy_resource, &serial);
446         wl_map_release(&client->objects);
447         wl_event_source_remove(client->source);
448         wl_connection_destroy(client->connection);
449         wl_list_remove(&client->link);
450         free(client);
451 }
452
453 static void
454 lose_pointer_focus(struct wl_listener *listener, void *data)
455 {
456         struct wl_pointer *pointer =
457                 container_of(listener, struct wl_pointer, focus_listener);
458
459         pointer->focus_resource = NULL;
460 }
461
462 static void
463 lose_keyboard_focus(struct wl_listener *listener, void *data)
464 {
465         struct wl_keyboard *keyboard =
466                 container_of(listener, struct wl_keyboard, focus_listener);
467
468         keyboard->focus_resource = NULL;
469 }
470
471 static void
472 default_grab_focus(struct wl_pointer_grab *grab,
473                    struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
474 {
475         struct wl_pointer *pointer = grab->pointer;
476
477         if (pointer->button_count > 0)
478                 return;
479
480         wl_pointer_set_focus(pointer, surface, x, y);
481 }
482
483 static void
484 default_grab_motion(struct wl_pointer_grab *grab,
485                     uint32_t time, wl_fixed_t x, wl_fixed_t y)
486 {
487         struct wl_resource *resource;
488
489         resource = grab->pointer->focus_resource;
490         if (resource)
491                 wl_pointer_send_motion(resource, time, x, y);
492 }
493
494 static void
495 default_grab_button(struct wl_pointer_grab *grab,
496                     uint32_t time, uint32_t button, uint32_t state)
497 {
498         struct wl_pointer *pointer = grab->pointer;
499         struct wl_resource *resource;
500         uint32_t serial;
501
502         resource = pointer->focus_resource;
503         if (resource) {
504                 serial = wl_display_next_serial(resource->client->display);
505                 wl_pointer_send_button(resource, serial, time, button, state);
506         }
507
508         if (pointer->button_count == 0 && state == 0)
509                 wl_pointer_set_focus(pointer, pointer->current,
510                                      pointer->current_x, pointer->current_y);
511 }
512
513 static const struct wl_pointer_grab_interface
514                                 default_pointer_grab_interface = {
515         default_grab_focus,
516         default_grab_motion,
517         default_grab_button
518 };
519
520 static void
521 default_grab_key(struct wl_keyboard_grab *grab,
522                  uint32_t time, uint32_t key, uint32_t state)
523 {
524         struct wl_keyboard *keyboard = grab->keyboard;
525         struct wl_resource *resource;
526         uint32_t serial;
527
528         resource = keyboard->focus_resource;
529         if (resource) {
530                 serial = wl_display_next_serial(resource->client->display);
531                 wl_keyboard_send_key(resource, serial, time, key, state);
532         }
533 }
534
535 static const struct wl_keyboard_grab_interface
536                                 default_keyboard_grab_interface = {
537         default_grab_key
538 };
539
540 WL_EXPORT void
541 wl_pointer_init(struct wl_pointer *pointer)
542 {
543         memset(pointer, 0, sizeof *pointer);
544         wl_list_init(&pointer->resource_list);
545         pointer->focus_listener.notify = lose_pointer_focus;
546         pointer->default_grab.interface = &default_pointer_grab_interface;
547         pointer->default_grab.pointer = pointer;
548         pointer->grab = &pointer->default_grab;
549
550         /* FIXME: Pick better co-ords. */
551         pointer->x = wl_fixed_from_int(100);
552         pointer->y = wl_fixed_from_int(100);
553 }
554
555 WL_EXPORT void
556 wl_pointer_release(struct wl_pointer *pointer)
557 {
558         /* XXX: What about pointer->resource_list? */
559         if (pointer->focus_resource)
560                 wl_list_remove(&pointer->focus_listener.link);
561 }
562
563 WL_EXPORT void
564 wl_keyboard_init(struct wl_keyboard *keyboard)
565 {
566         memset(keyboard, 0, sizeof *keyboard);
567         wl_list_init(&keyboard->resource_list);
568         wl_array_init(&keyboard->keys);
569         keyboard->focus_listener.notify = lose_keyboard_focus;
570         keyboard->default_grab.interface = &default_keyboard_grab_interface;
571         keyboard->default_grab.keyboard = keyboard;
572         keyboard->grab = &keyboard->default_grab;
573 }
574
575 WL_EXPORT void
576 wl_keyboard_release(struct wl_keyboard *keyboard)
577 {
578         /* XXX: What about keyboard->resource_list? */
579         if (keyboard->focus_resource)
580                 wl_list_remove(&keyboard->focus_listener.link);
581         wl_array_release(&keyboard->keys);
582 }
583
584 WL_EXPORT void
585 wl_touch_init(struct wl_touch *touch)
586 {
587         memset(touch, 0, sizeof *touch);
588         wl_list_init(&touch->resource_list);
589 }
590
591 WL_EXPORT void
592 wl_touch_release(struct wl_touch *touch)
593 {
594         /* XXX: What about touch->resource_list? */
595         if (touch->focus_resource)
596                 wl_list_remove(&touch->focus_listener.link);
597 }
598
599 WL_EXPORT void
600 wl_seat_init(struct wl_seat *seat)
601 {
602         memset(seat, 0, sizeof *seat);
603
604         seat->selection_data_source = NULL;
605         wl_list_init(&seat->base_resource_list);
606         wl_signal_init(&seat->selection_signal);
607         wl_list_init(&seat->drag_resource_list);
608         wl_signal_init(&seat->drag_icon_signal);
609 }
610
611 WL_EXPORT void
612 wl_seat_release(struct wl_seat *seat)
613 {
614         if (seat->pointer)
615                 wl_pointer_release(seat->pointer);
616         if (seat->keyboard)
617                 wl_keyboard_release(seat->keyboard);
618         if (seat->touch)
619                 wl_touch_release(seat->touch);
620 }
621
622 static void
623 seat_send_updated_caps(struct wl_seat *seat)
624 {
625         struct wl_resource *r;
626         enum wl_seat_capability caps = 0;
627
628         if (seat->pointer)
629                 caps |= WL_SEAT_CAPABILITY_POINTER;
630         if (seat->keyboard)
631                 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
632         if (seat->touch)
633                 caps |= WL_SEAT_CAPABILITY_TOUCH;
634
635         wl_list_for_each(r, &seat->base_resource_list, link)
636                 wl_seat_send_capabilities(r, caps);
637 }
638
639 WL_EXPORT void
640 wl_seat_set_pointer(struct wl_seat *seat, struct wl_pointer *pointer)
641 {
642         if (pointer && (seat->pointer || pointer->seat))
643                 return; /* XXX: error? */
644         if (!pointer && !seat->pointer)
645                 return;
646
647         seat->pointer = pointer;
648         if (pointer)
649                 pointer->seat = seat;
650
651         seat_send_updated_caps(seat);
652 }
653
654 WL_EXPORT void
655 wl_seat_set_keyboard(struct wl_seat *seat, struct wl_keyboard *keyboard)
656 {
657         if (keyboard && (seat->keyboard || keyboard->seat))
658                 return; /* XXX: error? */
659         if (!keyboard && seat->keyboard)
660                 return;
661
662         seat->keyboard = keyboard;
663         if (keyboard)
664                 keyboard->seat = seat;
665
666         seat_send_updated_caps(seat);
667 }
668
669 WL_EXPORT void
670 wl_seat_set_touch(struct wl_seat *seat, struct wl_touch *touch)
671 {
672         if (touch && (seat->touch || touch->seat))
673                 return; /* XXX: error? */
674         if (!touch && !seat->touch)
675                 return;
676
677         seat->touch = touch;
678         if (!touch)
679                 touch->seat = seat;
680
681         seat_send_updated_caps(seat);
682 }
683
684 static struct wl_resource *
685 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
686 {
687         struct wl_resource *r;
688
689         if (!surface)
690                 return NULL;
691
692         wl_list_for_each(r, list, link) {
693                 if (r->client == surface->resource.client)
694                         return r;
695         }
696
697         return NULL;
698 }
699
700 WL_EXPORT void
701 wl_pointer_set_focus(struct wl_pointer *pointer, struct wl_surface *surface,
702                      wl_fixed_t sx, wl_fixed_t sy)
703 {
704         struct wl_resource *resource;
705         uint32_t serial;
706
707         if (pointer->focus == surface)
708                 return;
709
710         resource = pointer->focus_resource;
711         if (resource) {
712                 serial = wl_display_next_serial(resource->client->display);
713                 wl_pointer_send_leave(resource, serial,
714                                       &pointer->focus->resource);
715                 wl_list_remove(&pointer->focus_listener.link);
716         }
717
718         resource = find_resource_for_surface(&pointer->resource_list,
719                                              surface);
720         if (resource) {
721                 serial = wl_display_next_serial(resource->client->display);
722                 wl_pointer_send_enter(resource, serial, &surface->resource,
723                                       sx, sy);
724                 wl_signal_add(&resource->destroy_signal,
725                               &pointer->focus_listener);
726                 pointer->focus_serial = serial;
727         }
728
729         pointer->focus_resource = resource;
730         pointer->focus = surface;
731         pointer->default_grab.focus = surface;
732 }
733
734 WL_EXPORT void
735 wl_keyboard_set_focus(struct wl_keyboard *keyboard, struct wl_surface *surface)
736 {
737         struct wl_resource *resource;
738         uint32_t serial;
739
740         if (keyboard->focus == surface)
741                 return;
742
743         if (keyboard->focus_resource) {
744                 resource = keyboard->focus_resource;
745                 serial = wl_display_next_serial(resource->client->display);
746                 wl_keyboard_send_leave(resource, serial,
747                                        &keyboard->focus->resource);
748                 wl_list_remove(&keyboard->focus_listener.link);
749         }
750
751         resource = find_resource_for_surface(&keyboard->resource_list,
752                                              surface);
753         if (resource) {
754                 serial = wl_display_next_serial(resource->client->display);
755                 wl_keyboard_send_enter(resource, serial, &surface->resource,
756                                        &keyboard->keys);
757                 wl_signal_add(&resource->destroy_signal,
758                               &keyboard->focus_listener);
759                 keyboard->focus_serial = serial;
760         }
761
762         keyboard->focus_resource = resource;
763         keyboard->focus = surface;
764 }
765
766 WL_EXPORT void
767 wl_keyboard_start_grab(struct wl_keyboard *keyboard,
768                        struct wl_keyboard_grab *grab)
769 {
770         keyboard->grab = grab;
771         grab->keyboard = keyboard;
772
773         /* XXX focus? */
774 }
775
776 WL_EXPORT void
777 wl_keyboard_end_grab(struct wl_keyboard *keyboard)
778 {
779         keyboard->grab = &keyboard->default_grab;
780 }
781
782 WL_EXPORT void
783 wl_pointer_start_grab(struct wl_pointer *pointer, struct wl_pointer_grab *grab)
784 {
785         const struct wl_pointer_grab_interface *interface;
786
787         pointer->grab = grab;
788         interface = pointer->grab->interface;
789         grab->pointer = pointer;
790
791         if (pointer->current)
792                 interface->focus(pointer->grab, pointer->current,
793                                  pointer->current_x, pointer->current_y);
794 }
795
796 WL_EXPORT void
797 wl_pointer_end_grab(struct wl_pointer *pointer)
798 {
799         const struct wl_pointer_grab_interface *interface;
800
801         pointer->grab = &pointer->default_grab;
802         interface = pointer->grab->interface;
803         interface->focus(pointer->grab, pointer->current,
804                          pointer->current_x, pointer->current_y);
805 }
806
807 static void
808 display_bind(struct wl_client *client,
809              struct wl_resource *resource, uint32_t name,
810              const char *interface, uint32_t version, uint32_t id)
811 {
812         struct wl_global *global;
813         struct wl_display *display = resource->data;
814
815         wl_list_for_each(global, &display->global_list, link)
816                 if (global->name == name)
817                         break;
818
819         if (&global->link == &display->global_list)
820                 wl_resource_post_error(resource,
821                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
822                                        "invalid global %d", name);
823         else
824                 global->bind(client, global->data, version, id);
825 }
826
827 static void
828 display_sync(struct wl_client *client,
829              struct wl_resource *resource, uint32_t id)
830 {
831         struct wl_resource *callback;
832         uint32_t serial;
833
834         callback = wl_client_add_object(client,
835                                         &wl_callback_interface, NULL, id, NULL);
836         serial = wl_display_get_serial(client->display);
837         wl_callback_send_done(callback, serial);
838         wl_resource_destroy(callback);
839 }
840
841 struct wl_display_interface display_interface = {
842         display_bind,
843         display_sync,
844 };
845
846 static void
847 destroy_client_display_resource(struct wl_resource *resource)
848 {
849         resource->client->display_resource = NULL;
850         free(resource);
851 }
852
853 static void
854 bind_display(struct wl_client *client,
855              void *data, uint32_t version, uint32_t id)
856 {
857         struct wl_display *display = data;
858         struct wl_global *global;
859
860         client->display_resource =
861                 wl_client_add_object(client, &wl_display_interface,
862                                      &display_interface, id, display);
863         client->display_resource->destroy = destroy_client_display_resource;
864
865         wl_list_for_each(global, &display->global_list, link)
866                 wl_resource_post_event(client->display_resource,
867                                        WL_DISPLAY_GLOBAL,
868                                        global->name,
869                                        global->interface->name,
870                                        global->interface->version);
871 }
872
873 WL_EXPORT struct wl_display *
874 wl_display_create(void)
875 {
876         struct wl_display *display;
877         const char *debug;
878
879         debug = getenv("WAYLAND_DEBUG");
880         if (debug)
881                 wl_debug = 1;
882
883         display = malloc(sizeof *display);
884         if (display == NULL)
885                 return NULL;
886
887         display->loop = wl_event_loop_create();
888         if (display->loop == NULL) {
889                 free(display);
890                 return NULL;
891         }
892
893         wl_list_init(&display->global_list);
894         wl_list_init(&display->socket_list);
895         wl_list_init(&display->client_list);
896
897         display->id = 1;
898         display->serial = 0;
899
900         if (!wl_display_add_global(display, &wl_display_interface, 
901                                    display, bind_display)) {
902                 wl_event_loop_destroy(display->loop);
903                 free(display);
904                 return NULL;
905         }
906
907         return display;
908 }
909
910 WL_EXPORT void
911 wl_display_destroy(struct wl_display *display)
912 {
913         struct wl_socket *s, *next;
914         struct wl_global *global, *gnext;
915
916         wl_list_for_each_safe(s, next, &display->socket_list, link) {
917                 wl_event_source_remove(s->source);
918                 close(s->fd);
919                 unlink(s->addr.sun_path);
920                 close(s->fd_lock);
921                 unlink(s->lock_addr);
922                 free(s);
923         }
924         wl_event_loop_destroy(display->loop);
925
926         wl_list_for_each_safe(global, gnext, &display->global_list, link)
927                 free(global);
928
929         free(display);
930 }
931
932 WL_EXPORT struct wl_global *
933 wl_display_add_global(struct wl_display *display,
934                       const struct wl_interface *interface,
935                       void *data, wl_global_bind_func_t bind)
936 {
937         struct wl_global *global;
938         struct wl_client *client;
939
940         global = malloc(sizeof *global);
941         if (global == NULL)
942                 return NULL;
943
944         global->name = display->id++;
945         global->interface = interface;
946         global->data = data;
947         global->bind = bind;
948         wl_list_insert(display->global_list.prev, &global->link);
949
950         wl_list_for_each(client, &display->client_list, link)
951                 wl_resource_post_event(client->display_resource,
952                                        WL_DISPLAY_GLOBAL,
953                                        global->name,
954                                        global->interface->name,
955                                        global->interface->version);
956
957         return global;
958 }
959
960 WL_EXPORT void
961 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
962 {
963         struct wl_client *client;
964
965         wl_list_for_each(client, &display->client_list, link)
966                 wl_resource_post_event(client->display_resource,
967                                        WL_DISPLAY_GLOBAL_REMOVE, global->name);
968         wl_list_remove(&global->link);
969         free(global);
970 }
971
972 WL_EXPORT uint32_t
973 wl_display_get_serial(struct wl_display *display)
974 {
975         return display->serial;
976 }
977
978 WL_EXPORT uint32_t
979 wl_display_next_serial(struct wl_display *display)
980 {
981         display->serial++;
982
983         return display->serial;
984 }
985
986 WL_EXPORT struct wl_event_loop *
987 wl_display_get_event_loop(struct wl_display *display)
988 {
989         return display->loop;
990 }
991
992 WL_EXPORT void
993 wl_display_terminate(struct wl_display *display)
994 {
995         display->run = 0;
996 }
997
998 WL_EXPORT void
999 wl_display_run(struct wl_display *display)
1000 {
1001         display->run = 1;
1002
1003         while (display->run)
1004                 wl_event_loop_dispatch(display->loop, -1);
1005 }
1006
1007 static int
1008 socket_data(int fd, uint32_t mask, void *data)
1009 {
1010         struct wl_display *display = data;
1011         struct sockaddr_un name;
1012         socklen_t length;
1013         int client_fd;
1014
1015         length = sizeof name;
1016         client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name,
1017                                          &length);
1018         if (client_fd < 0)
1019                 fprintf(stderr, "failed to accept, errno: %d\n", errno);
1020         else
1021                 wl_client_create(display, client_fd);
1022
1023         return 1;
1024 }
1025
1026 static int
1027 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
1028 {
1029         struct stat socket_stat;
1030         int lock_size = name_size + 5;
1031
1032         snprintf(socket->lock_addr, lock_size,
1033                  "%s.lock", socket->addr.sun_path);
1034
1035         socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
1036                                (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
1037
1038         if (socket->fd_lock < 0) {
1039                 fprintf(stderr,
1040                         "unable to open lockfile %s check permissions\n",
1041                         socket->lock_addr);
1042                 return -1;
1043         }
1044
1045         if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
1046                 fprintf(stderr,
1047                         "unable to lock lockfile %s, maybe another compositor is running\n",
1048                         socket->lock_addr);
1049                 close(socket->fd_lock);
1050                 return -1;
1051         }
1052
1053         if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
1054                 if (errno != ENOENT) {
1055                         fprintf(stderr, "did not manage to stat file %s\n",
1056                                 socket->addr.sun_path);
1057                         close(socket->fd_lock);
1058                         return -1;
1059                 }
1060         } else if (socket_stat.st_mode & S_IWUSR ||
1061                    socket_stat.st_mode & S_IWGRP) {
1062                 unlink(socket->addr.sun_path);
1063         }
1064
1065         return 0;
1066 }
1067
1068 WL_EXPORT int
1069 wl_display_add_socket(struct wl_display *display, const char *name)
1070 {
1071         struct wl_socket *s;
1072         socklen_t size, name_size;
1073         const char *runtime_dir;
1074
1075         s = malloc(sizeof *s);
1076         if (s == NULL)
1077                 return -1;
1078
1079         s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
1080         if (s->fd < 0) {
1081                 free(s);
1082                 return -1;
1083         }
1084
1085         runtime_dir = getenv("XDG_RUNTIME_DIR");
1086         if (runtime_dir == NULL) {
1087                 runtime_dir = ".";
1088                 fprintf(stderr,
1089                         "XDG_RUNTIME_DIR not set, falling back to %s\n",
1090                         runtime_dir);
1091         }
1092
1093         if (name == NULL)
1094                 name = getenv("WAYLAND_DISPLAY");
1095         if (name == NULL)
1096                 name = "wayland-0";
1097
1098         memset(&s->addr, 0, sizeof s->addr);
1099         s->addr.sun_family = AF_LOCAL;
1100         name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
1101                              "%s/%s", runtime_dir, name) + 1;
1102         fprintf(stderr, "using socket %s\n", s->addr.sun_path);
1103
1104         if (get_socket_lock(s,name_size) < 0) {
1105                 close(s->fd);
1106                 free(s);
1107                 return -1;
1108         }
1109
1110         size = offsetof (struct sockaddr_un, sun_path) + name_size;
1111         if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
1112                 close(s->fd);
1113                 free(s);
1114                 return -1;
1115         }
1116
1117         if (listen(s->fd, 1) < 0) {
1118                 close(s->fd);
1119                 unlink(s->addr.sun_path);
1120                 free(s);
1121                 return -1;
1122         }
1123
1124         s->source = wl_event_loop_add_fd(display->loop, s->fd,
1125                                          WL_EVENT_READABLE,
1126                                          socket_data, display);
1127         if (s->source == NULL) {
1128                 close(s->fd);
1129                 unlink(s->addr.sun_path);
1130                 free(s);
1131                 return -1;
1132         }
1133         wl_list_insert(display->socket_list.prev, &s->link);
1134
1135         return 0;
1136 }
1137
1138 WL_EXPORT struct wl_resource *
1139 wl_client_add_object(struct wl_client *client,
1140                      const struct wl_interface *interface,
1141                      const void *implementation,
1142                      uint32_t id, void *data)
1143 {
1144         struct wl_resource *resource;
1145
1146         resource = malloc(sizeof *resource);
1147         if (resource == NULL) {
1148                 wl_resource_post_no_memory(client->display_resource);
1149                 return NULL;
1150         }
1151
1152         resource->object.interface = interface;
1153         resource->object.implementation = implementation;
1154         resource->object.id = id;
1155         resource->client = client;
1156         resource->data = data;
1157         resource->destroy = (void *) free;
1158         wl_signal_init(&resource->destroy_signal);
1159
1160         if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
1161                 wl_resource_post_no_memory(client->display_resource);
1162                 free(resource);
1163                 return NULL;
1164         }
1165
1166         return resource;
1167 }
1168
1169 WL_EXPORT struct wl_resource *
1170 wl_client_new_object(struct wl_client *client,
1171                      const struct wl_interface *interface,
1172                      const void *implementation, void *data)
1173 {
1174         uint32_t id;
1175
1176         id = wl_map_insert_new(&client->objects, WL_MAP_SERVER_SIDE, NULL);
1177         return wl_client_add_object(client,
1178                                     interface, implementation, id, data);
1179
1180 }
1181
1182 WL_EXPORT void
1183 wl_log_set_handler_server(wl_log_func_t handler)
1184 {
1185         wl_log_handler = handler;
1186 }