d0c31bf6a30965292b2458a9418144e61a0c499f
[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_input_device *device =
457                 container_of(listener, struct wl_input_device,
458                              pointer_focus_listener);
459
460         device->pointer_focus_resource = NULL;
461 }
462
463 static void
464 lose_keyboard_focus(struct wl_listener *listener, void *data)
465 {
466         struct wl_input_device *device =
467                 container_of(listener, struct wl_input_device,
468                              keyboard_focus_listener);
469
470         device->keyboard_focus_resource = NULL;
471 }
472
473 static void
474 default_grab_focus(struct wl_pointer_grab *grab,
475                    struct wl_surface *surface, int32_t x, int32_t y)
476 {
477         struct wl_input_device *device = grab->input_device;
478
479         if (device->button_count > 0)
480                 return;
481
482         wl_input_device_set_pointer_focus(device, surface, x, y);
483 }
484
485 static void
486 default_grab_motion(struct wl_pointer_grab *grab,
487                     uint32_t time, int32_t x, int32_t y)
488 {
489         struct wl_resource *resource;
490
491         resource = grab->input_device->pointer_focus_resource;
492         if (resource)
493                 wl_input_device_send_motion(resource, time, x, y);
494 }
495
496 static void
497 default_grab_button(struct wl_pointer_grab *grab,
498                     uint32_t time, uint32_t button, int32_t state)
499 {
500         struct wl_input_device *device = grab->input_device;
501         struct wl_resource *resource;
502         uint32_t serial;
503
504         resource = device->pointer_focus_resource;
505         if (resource) {
506                 serial = wl_display_next_serial(resource->client->display);
507                 wl_input_device_send_button(resource, serial, time,
508                                             button, state);
509         }
510
511         if (device->button_count == 0 && state == 0)
512                 wl_input_device_set_pointer_focus(device,
513                                                   device->current,
514                                                   device->current_x,
515                                                   device->current_y);
516 }
517
518 static const struct wl_pointer_grab_interface
519                                 default_pointer_grab_interface = {
520         default_grab_focus,
521         default_grab_motion,
522         default_grab_button
523 };
524
525 static void
526 default_grab_key(struct wl_keyboard_grab *grab,
527                  uint32_t time, uint32_t key, int32_t state)
528 {
529         struct wl_input_device *device = grab->input_device;
530         struct wl_resource *resource;
531         uint32_t serial;
532
533         resource = device->keyboard_focus_resource;
534         if (resource) {
535                 serial = wl_display_next_serial(resource->client->display);
536                 wl_input_device_send_key(resource, serial, time, key, state);
537         }
538 }
539
540 static const struct wl_keyboard_grab_interface
541                                 default_keyboard_grab_interface = {
542         default_grab_key
543 };
544
545 WL_EXPORT void
546 wl_input_device_init(struct wl_input_device *device)
547 {
548         memset(device, 0, sizeof *device);
549         wl_list_init(&device->resource_list);
550         wl_array_init(&device->keys);
551         device->pointer_focus_listener.notify = lose_pointer_focus;
552         device->keyboard_focus_listener.notify = lose_keyboard_focus;
553
554         device->default_pointer_grab.interface = &default_pointer_grab_interface;
555         device->default_pointer_grab.input_device = device;
556         device->pointer_grab = &device->default_pointer_grab;
557
558         device->default_keyboard_grab.interface = &default_keyboard_grab_interface;
559         device->default_keyboard_grab.input_device = device;
560         device->keyboard_grab = &device->default_keyboard_grab;
561
562         wl_list_init(&device->drag_resource_list);
563         device->selection_data_source = NULL;
564         wl_signal_init(&device->selection_signal);
565         wl_signal_init(&device->drag_icon_signal);
566
567         device->x = 100;
568         device->y = 100;
569 }
570
571 WL_EXPORT void
572 wl_input_device_release(struct wl_input_device *device)
573 {
574         if (device->keyboard_focus_resource)
575                 wl_list_remove(&device->keyboard_focus_listener.link);
576
577         if (device->pointer_focus_resource)
578                 wl_list_remove(&device->pointer_focus_listener.link);
579
580         /* XXX: What about device->resource_list? */
581
582         wl_array_release(&device->keys);
583 }
584
585 static struct wl_resource *
586 find_resource_for_surface(struct wl_list *list, struct wl_surface *surface)
587 {
588         struct wl_resource *r;
589
590         if (!surface)
591                 return NULL;
592
593         wl_list_for_each(r, list, link) {
594                 if (r->client == surface->resource.client)
595                         return r;
596         }
597
598         return NULL;
599 }
600
601 WL_EXPORT void
602 wl_input_device_set_pointer_focus(struct wl_input_device *device,
603                                   struct wl_surface *surface,
604                                   int32_t sx, int32_t sy)
605 {
606         struct wl_resource *resource;
607         uint32_t serial;
608
609         if (device->pointer_focus == surface)
610                 return;
611
612         resource = device->pointer_focus_resource;
613         if (resource) {
614                 serial = wl_display_next_serial(resource->client->display);
615                 wl_input_device_send_pointer_leave(resource, serial,
616                         &device->pointer_focus->resource);
617                 wl_list_remove(&device->pointer_focus_listener.link);
618         }
619
620
621         resource = find_resource_for_surface(&device->resource_list, surface);
622         if (resource) {
623                 serial = wl_display_next_serial(resource->client->display);
624                 wl_input_device_send_pointer_enter(resource, serial,
625                                                    &surface->resource,
626                                                    sx, sy);
627                 wl_signal_add(&resource->destroy_signal,
628                               &device->pointer_focus_listener);
629                 device->pointer_focus_serial = serial;
630         }
631
632         device->pointer_focus_resource = resource;
633         device->pointer_focus = surface;
634         device->default_pointer_grab.focus = surface;
635 }
636
637 WL_EXPORT void
638 wl_input_device_set_keyboard_focus(struct wl_input_device *device,
639                                    struct wl_surface *surface)
640 {
641         struct wl_resource *resource;
642         uint32_t serial;
643
644         if (device->keyboard_focus == surface)
645                 return;
646
647         if (device->keyboard_focus_resource) {
648                 resource = device->keyboard_focus_resource;
649                 serial = wl_display_next_serial(resource->client->display);
650                 wl_input_device_send_keyboard_leave(resource, serial,
651                                                     &device->keyboard_focus->resource);
652                 wl_list_remove(&device->keyboard_focus_listener.link);
653         }
654
655         resource = find_resource_for_surface(&device->resource_list, surface);
656         if (resource) {
657                 serial = wl_display_next_serial(resource->client->display);
658                 wl_input_device_send_keyboard_enter(resource, serial,
659                                                     &surface->resource,
660                                                     &device->keys);
661                 wl_signal_add(&resource->destroy_signal,
662                               &device->keyboard_focus_listener);
663                 device->keyboard_focus_serial = serial;
664         }
665
666         device->keyboard_focus_resource = resource;
667         device->keyboard_focus = surface;
668 }
669
670 WL_EXPORT void
671 wl_input_device_start_keyboard_grab(struct wl_input_device *device,
672                                     struct wl_keyboard_grab *grab)
673 {
674         device->keyboard_grab = grab;
675         grab->input_device = device;
676
677 }
678
679 WL_EXPORT void
680 wl_input_device_end_keyboard_grab(struct wl_input_device *device)
681 {
682         device->keyboard_grab = &device->default_keyboard_grab;
683 }
684
685 WL_EXPORT void
686 wl_input_device_start_pointer_grab(struct wl_input_device *device,
687                                    struct wl_pointer_grab *grab)
688 {
689         const struct wl_pointer_grab_interface *interface;
690
691         device->pointer_grab = grab;
692         interface = device->pointer_grab->interface;
693         grab->input_device = device;
694
695         if (device->current)
696                 interface->focus(device->pointer_grab, device->current,
697                                  device->current_x, device->current_y);
698 }
699
700 WL_EXPORT void
701 wl_input_device_end_pointer_grab(struct wl_input_device *device)
702 {
703         const struct wl_pointer_grab_interface *interface;
704
705         device->pointer_grab = &device->default_pointer_grab;
706         interface = device->pointer_grab->interface;
707         interface->focus(device->pointer_grab, device->current,
708                          device->current_x, device->current_y);
709 }
710
711 static void
712 display_bind(struct wl_client *client,
713              struct wl_resource *resource, uint32_t name,
714              const char *interface, uint32_t version, uint32_t id)
715 {
716         struct wl_global *global;
717         struct wl_display *display = resource->data;
718
719         wl_list_for_each(global, &display->global_list, link)
720                 if (global->name == name)
721                         break;
722
723         if (&global->link == &display->global_list)
724                 wl_resource_post_error(resource,
725                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
726                                        "invalid global %d", name);
727         else
728                 global->bind(client, global->data, version, id);
729 }
730
731 static void
732 display_sync(struct wl_client *client,
733              struct wl_resource *resource, uint32_t id)
734 {
735         struct wl_resource *callback;
736         uint32_t serial;
737
738         callback = wl_client_add_object(client,
739                                         &wl_callback_interface, NULL, id, NULL);
740         serial = wl_display_get_serial(client->display);
741         wl_callback_send_done(callback, serial);
742         wl_resource_destroy(callback);
743 }
744
745 struct wl_display_interface display_interface = {
746         display_bind,
747         display_sync,
748 };
749
750 static void
751 destroy_client_display_resource(struct wl_resource *resource)
752 {
753         resource->client->display_resource = NULL;
754         free(resource);
755 }
756
757 static void
758 bind_display(struct wl_client *client,
759              void *data, uint32_t version, uint32_t id)
760 {
761         struct wl_display *display = data;
762         struct wl_global *global;
763
764         client->display_resource =
765                 wl_client_add_object(client, &wl_display_interface,
766                                      &display_interface, id, display);
767         client->display_resource->destroy = destroy_client_display_resource;
768
769         wl_list_for_each(global, &display->global_list, link)
770                 wl_resource_post_event(client->display_resource,
771                                        WL_DISPLAY_GLOBAL,
772                                        global->name,
773                                        global->interface->name,
774                                        global->interface->version);
775 }
776
777 WL_EXPORT struct wl_display *
778 wl_display_create(void)
779 {
780         struct wl_display *display;
781         const char *debug;
782
783         debug = getenv("WAYLAND_DEBUG");
784         if (debug)
785                 wl_debug = 1;
786
787         display = malloc(sizeof *display);
788         if (display == NULL)
789                 return NULL;
790
791         display->loop = wl_event_loop_create();
792         if (display->loop == NULL) {
793                 free(display);
794                 return NULL;
795         }
796
797         wl_list_init(&display->global_list);
798         wl_list_init(&display->socket_list);
799         wl_list_init(&display->client_list);
800
801         display->id = 1;
802
803         if (!wl_display_add_global(display, &wl_display_interface, 
804                                    display, bind_display)) {
805                 wl_event_loop_destroy(display->loop);
806                 free(display);
807                 return NULL;
808         }
809
810         return display;
811 }
812
813 WL_EXPORT void
814 wl_display_destroy(struct wl_display *display)
815 {
816         struct wl_socket *s, *next;
817         struct wl_global *global, *gnext;
818
819         wl_list_for_each_safe(s, next, &display->socket_list, link) {
820                 wl_event_source_remove(s->source);
821                 close(s->fd);
822                 unlink(s->addr.sun_path);
823                 close(s->fd_lock);
824                 unlink(s->lock_addr);
825                 free(s);
826         }
827         wl_event_loop_destroy(display->loop);
828
829         wl_list_for_each_safe(global, gnext, &display->global_list, link)
830                 free(global);
831
832         free(display);
833 }
834
835 WL_EXPORT struct wl_global *
836 wl_display_add_global(struct wl_display *display,
837                       const struct wl_interface *interface,
838                       void *data, wl_global_bind_func_t bind)
839 {
840         struct wl_global *global;
841         struct wl_client *client;
842
843         global = malloc(sizeof *global);
844         if (global == NULL)
845                 return NULL;
846
847         global->name = display->id++;
848         global->interface = interface;
849         global->data = data;
850         global->bind = bind;
851         wl_list_insert(display->global_list.prev, &global->link);
852
853         wl_list_for_each(client, &display->client_list, link)
854                 wl_resource_post_event(client->display_resource,
855                                        WL_DISPLAY_GLOBAL,
856                                        global->name,
857                                        global->interface->name,
858                                        global->interface->version);
859
860         return global;
861 }
862
863 WL_EXPORT void
864 wl_display_remove_global(struct wl_display *display, struct wl_global *global)
865 {
866         struct wl_client *client;
867
868         wl_list_for_each(client, &display->client_list, link)
869                 wl_resource_post_event(client->display_resource,
870                                        WL_DISPLAY_GLOBAL_REMOVE, global->name);
871         wl_list_remove(&global->link);
872         free(global);
873 }
874
875 WL_EXPORT uint32_t
876 wl_display_get_serial(struct wl_display *display)
877 {
878         return display->serial;
879 }
880
881 WL_EXPORT uint32_t
882 wl_display_next_serial(struct wl_display *display)
883 {
884         display->serial++;
885
886         return display->serial;
887 }
888
889 WL_EXPORT struct wl_event_loop *
890 wl_display_get_event_loop(struct wl_display *display)
891 {
892         return display->loop;
893 }
894
895 WL_EXPORT void
896 wl_display_terminate(struct wl_display *display)
897 {
898         display->run = 0;
899 }
900
901 WL_EXPORT void
902 wl_display_run(struct wl_display *display)
903 {
904         display->run = 1;
905
906         while (display->run)
907                 wl_event_loop_dispatch(display->loop, -1);
908 }
909
910 static int
911 socket_data(int fd, uint32_t mask, void *data)
912 {
913         struct wl_display *display = data;
914         struct sockaddr_un name;
915         socklen_t length;
916         int client_fd;
917
918         length = sizeof name;
919         client_fd = wl_os_accept_cloexec(fd, (struct sockaddr *) &name,
920                                          &length);
921         if (client_fd < 0)
922                 fprintf(stderr, "failed to accept, errno: %d\n", errno);
923         else
924                 wl_client_create(display, client_fd);
925
926         return 1;
927 }
928
929 static int
930 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
931 {
932         struct stat socket_stat;
933         int lock_size = name_size + 5;
934
935         snprintf(socket->lock_addr, lock_size,
936                  "%s.lock", socket->addr.sun_path);
937
938         socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
939                                (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
940
941         if (socket->fd_lock < 0) {
942                 fprintf(stderr,
943                         "unable to open lockfile %s check permissions\n",
944                         socket->lock_addr);
945                 return -1;
946         }
947
948         if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
949                 fprintf(stderr,
950                         "unable to lock lockfile %s, maybe another compositor is running\n",
951                         socket->lock_addr);
952                 close(socket->fd_lock);
953                 return -1;
954         }
955
956         if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
957                 if (errno != ENOENT) {
958                         fprintf(stderr, "did not manage to stat file %s\n",
959                                 socket->addr.sun_path);
960                         close(socket->fd_lock);
961                         return -1;
962                 }
963         } else if (socket_stat.st_mode & S_IWUSR ||
964                    socket_stat.st_mode & S_IWGRP) {
965                 unlink(socket->addr.sun_path);
966         }
967
968         return 0;
969 }
970
971 WL_EXPORT int
972 wl_display_add_socket(struct wl_display *display, const char *name)
973 {
974         struct wl_socket *s;
975         socklen_t size, name_size;
976         const char *runtime_dir;
977
978         s = malloc(sizeof *s);
979         if (s == NULL)
980                 return -1;
981
982         s->fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
983         if (s->fd < 0) {
984                 free(s);
985                 return -1;
986         }
987
988         runtime_dir = getenv("XDG_RUNTIME_DIR");
989         if (runtime_dir == NULL) {
990                 runtime_dir = ".";
991                 fprintf(stderr,
992                         "XDG_RUNTIME_DIR not set, falling back to %s\n",
993                         runtime_dir);
994         }
995
996         if (name == NULL)
997                 name = getenv("WAYLAND_DISPLAY");
998         if (name == NULL)
999                 name = "wayland-0";
1000
1001         memset(&s->addr, 0, sizeof s->addr);
1002         s->addr.sun_family = AF_LOCAL;
1003         name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
1004                              "%s/%s", runtime_dir, name) + 1;
1005         fprintf(stderr, "using socket %s\n", s->addr.sun_path);
1006
1007         if (get_socket_lock(s,name_size) < 0) {
1008                 close(s->fd);
1009                 free(s);
1010                 return -1;
1011         }
1012
1013         size = offsetof (struct sockaddr_un, sun_path) + name_size;
1014         if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
1015                 close(s->fd);
1016                 free(s);
1017                 return -1;
1018         }
1019
1020         if (listen(s->fd, 1) < 0) {
1021                 close(s->fd);
1022                 unlink(s->addr.sun_path);
1023                 free(s);
1024                 return -1;
1025         }
1026
1027         s->source = wl_event_loop_add_fd(display->loop, s->fd,
1028                                          WL_EVENT_READABLE,
1029                                          socket_data, display);
1030         if (s->source == NULL) {
1031                 close(s->fd);
1032                 unlink(s->addr.sun_path);
1033                 free(s);
1034                 return -1;
1035         }
1036         wl_list_insert(display->socket_list.prev, &s->link);
1037
1038         return 0;
1039 }
1040
1041 WL_EXPORT struct wl_resource *
1042 wl_client_add_object(struct wl_client *client,
1043                      const struct wl_interface *interface,
1044                      const void *implementation,
1045                      uint32_t id, void *data)
1046 {
1047         struct wl_resource *resource;
1048
1049         resource = malloc(sizeof *resource);
1050         if (resource == NULL) {
1051                 wl_resource_post_no_memory(client->display_resource);
1052                 return NULL;
1053         }
1054
1055         resource->object.interface = interface;
1056         resource->object.implementation = implementation;
1057         resource->object.id = id;
1058         resource->client = client;
1059         resource->data = data;
1060         resource->destroy = (void *) free;
1061         wl_signal_init(&resource->destroy_signal);
1062
1063         if (wl_map_insert_at(&client->objects, resource->object.id, resource) < 0) {
1064                 wl_resource_post_no_memory(client->display_resource);
1065                 free(resource);
1066                 return NULL;
1067         }
1068
1069         return resource;
1070 }
1071
1072 WL_EXPORT struct wl_resource *
1073 wl_client_new_object(struct wl_client *client,
1074                      const struct wl_interface *interface,
1075                      const void *implementation, void *data)
1076 {
1077         uint32_t id;
1078
1079         id = wl_map_insert_new(&client->objects, WL_MAP_SERVER_SIDE, NULL);
1080         return wl_client_add_object(client,
1081                                     interface, implementation, id, data);
1082
1083 }