Pass object ID not pointer when sending a global announce event
[profile/ivi/wayland.git] / wayland / 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-server.h"
45 #include "wayland-server-protocol.h"
46 #include "connection.h"
47
48 struct wl_socket {
49         int fd;
50         int fd_lock;
51         struct sockaddr_un addr;
52         char lock_addr[113];
53         struct wl_list link;
54 };
55
56 struct wl_client {
57         struct wl_connection *connection;
58         struct wl_event_source *source;
59         struct wl_display *display;
60         struct wl_list resource_list;
61         uint32_t id_count;
62         uint32_t mask;
63         struct wl_list link;
64 };
65
66 struct wl_display {
67         struct wl_object object;
68         struct wl_event_loop *loop;
69         struct wl_hash_table *objects;
70         int run;
71
72         struct wl_list frame_list;
73         uint32_t client_id_range;
74         uint32_t id;
75
76         struct wl_list global_list;
77         struct wl_list socket_list;
78         struct wl_list client_list;
79 };
80
81 struct wl_frame_listener {
82         struct wl_resource resource;
83         struct wl_client *client;
84         uint32_t key;
85         struct wl_surface *surface;
86         struct wl_list link;
87 };
88
89 struct wl_global {
90         struct wl_object *object;
91         wl_global_bind_func_t func;
92         struct wl_list link;
93 };
94
95 static int wl_debug = 0;
96
97 WL_EXPORT void
98 wl_client_post_event(struct wl_client *client, struct wl_object *sender,
99                      uint32_t opcode, ...)
100 {
101         struct wl_closure *closure;
102         va_list ap;
103
104         va_start(ap, opcode);
105         closure = wl_connection_vmarshal(client->connection,
106                                          sender, opcode, ap,
107                                          &sender->interface->events[opcode]);
108         va_end(ap);
109
110         wl_closure_send(closure, client->connection);
111
112         if (wl_debug)
113                 wl_closure_print(closure, sender, true);
114
115         wl_closure_destroy(closure);
116 }
117
118 WL_EXPORT void
119 wl_client_post_error(struct wl_client *client, struct wl_object *object,
120                      uint32_t code, const char *msg, ...)
121 {
122         char buffer[128];
123         va_list ap;
124
125         va_start(ap, msg);
126         vsnprintf(buffer, sizeof buffer, msg, ap);
127         va_end(ap);
128
129         wl_client_post_event(client, &client->display->object,
130                              WL_DISPLAY_ERROR, object, code, buffer);
131 }
132
133 static int
134 wl_client_connection_data(int fd, uint32_t mask, void *data)
135 {
136         struct wl_client *client = data;
137         struct wl_connection *connection = client->connection;
138         struct wl_object *object;
139         struct wl_closure *closure;
140         const struct wl_message *message;
141         uint32_t p[2], opcode, size;
142         uint32_t cmask = 0;
143         int len;
144
145         if (mask & WL_EVENT_READABLE)
146                 cmask |= WL_CONNECTION_READABLE;
147         if (mask & WL_EVENT_WRITEABLE)
148                 cmask |= WL_CONNECTION_WRITABLE;
149
150         len = wl_connection_data(connection, cmask);
151         if (len < 0) {
152                 wl_client_destroy(client);
153                 return 1;
154         }
155
156         while (len >= sizeof p) {
157                 wl_connection_copy(connection, p, sizeof p);
158                 opcode = p[1] & 0xffff;
159                 size = p[1] >> 16;
160                 if (len < size)
161                         break;
162
163                 object = wl_hash_table_lookup(client->display->objects, p[0]);
164                 if (object == NULL) {
165                         wl_client_post_error(client, &client->display->object,
166                                              WL_DISPLAY_ERROR_INVALID_OBJECT,
167                                              "invalid object %d", p[0]);
168                         wl_connection_consume(connection, size);
169                         len -= size;
170                         continue;
171                 }
172
173                 if (opcode >= object->interface->method_count) {
174                         wl_client_post_error(client, &client->display->object,
175                                              WL_DISPLAY_ERROR_INVALID_METHOD,
176                                              "invalid method %d, object %s@%d",
177                                              object->interface->name,
178                                              object->id, opcode);
179                         wl_connection_consume(connection, size);
180                         len -= size;
181                         continue;
182                 }
183
184                 message = &object->interface->methods[opcode];
185                 closure = wl_connection_demarshal(client->connection, size,
186                                                   client->display->objects,
187                                                   message);
188                 len -= size;
189
190                 if (closure == NULL && errno == EINVAL) {
191                         wl_client_post_error(client, &client->display->object,
192                                              WL_DISPLAY_ERROR_INVALID_METHOD,
193                                              "invalid arguments for %s@%d.%s",
194                                              object->interface->name,
195                                              object->id, message->name);
196                         continue;
197                 } else if (closure == NULL && errno == ENOMEM) {
198                         wl_client_post_no_memory(client);
199                         continue;
200                 }
201
202
203                 if (wl_debug)
204                         wl_closure_print(closure, object, false);
205
206                 wl_closure_invoke(closure, object,
207                                   object->implementation[opcode], client);
208
209                 wl_closure_destroy(closure);
210         }
211
212         return 1;
213 }
214
215 static int
216 wl_client_connection_update(struct wl_connection *connection,
217                             uint32_t mask, void *data)
218 {
219         struct wl_client *client = data;
220         uint32_t emask = 0;
221
222         client->mask = mask;
223         if (mask & WL_CONNECTION_READABLE)
224                 emask |= WL_EVENT_READABLE;
225         if (mask & WL_CONNECTION_WRITABLE)
226                 emask |= WL_EVENT_WRITEABLE;
227
228         return wl_event_source_fd_update(client->source, emask);
229 }
230
231 WL_EXPORT void
232 wl_client_flush(struct wl_client *client)
233 {
234         if (client->mask & WL_CONNECTION_WRITABLE)
235                 wl_connection_data(client->connection, WL_CONNECTION_WRITABLE);
236 }
237
238 WL_EXPORT struct wl_display *
239 wl_client_get_display(struct wl_client *client)
240 {
241         return client->display;
242 }
243
244 static void
245 wl_display_post_range(struct wl_display *display, struct wl_client *client)
246 {
247         wl_client_post_event(client, &client->display->object,
248                              WL_DISPLAY_RANGE, display->client_id_range);
249         display->client_id_range += 256;
250         client->id_count += 256;
251 }
252
253 WL_EXPORT struct wl_client *
254 wl_client_create(struct wl_display *display, int fd)
255 {
256         struct wl_client *client;
257         struct wl_global *global;
258
259         client = malloc(sizeof *client);
260         if (client == NULL)
261                 return NULL;
262
263         memset(client, 0, sizeof *client);
264         client->display = display;
265         client->source = wl_event_loop_add_fd(display->loop, fd,
266                                               WL_EVENT_READABLE,
267                                               wl_client_connection_data, client);
268         client->connection =
269                 wl_connection_create(fd, wl_client_connection_update, client);
270         if (client->connection == NULL) {
271                 free(client);
272                 return NULL;
273         }
274
275         wl_list_insert(display->client_list.prev, &client->link);
276
277         wl_list_init(&client->resource_list);
278
279         wl_display_post_range(display, client);
280
281         wl_list_for_each(global, &display->global_list, link)
282                 wl_client_post_global(client, global->object);
283
284         return client;
285 }
286
287 WL_EXPORT void
288 wl_client_add_resource(struct wl_client *client,
289                        struct wl_resource *resource)
290 {
291         struct wl_display *display = client->display;
292
293         if (client->id_count-- < 64)
294                 wl_display_post_range(display, client);
295
296         wl_list_init(&resource->destroy_listener_list);
297
298         wl_hash_table_insert(client->display->objects,
299                              resource->object.id, resource);
300         wl_list_insert(client->resource_list.prev, &resource->link);
301 }
302
303 WL_EXPORT void
304 wl_client_post_no_memory(struct wl_client *client)
305 {
306         wl_client_post_error(client, &client->display->object,
307                              WL_DISPLAY_ERROR_NO_MEMORY, "no memory");
308 }
309
310 WL_EXPORT void
311 wl_client_post_global(struct wl_client *client, struct wl_object *object)
312 {
313         wl_client_post_event(client,
314                              &client->display->object,
315                              WL_DISPLAY_GLOBAL,
316                              object->id,
317                              object->interface->name,
318                              object->interface->version);
319 }
320
321 WL_EXPORT void
322 wl_resource_destroy(struct wl_resource *resource,
323                     struct wl_client *client, uint32_t time)
324 {
325         struct wl_display *display = client->display;
326         struct wl_listener *l, *next;
327
328         wl_list_for_each_safe(l, next,
329                               &resource->destroy_listener_list, link)
330                 l->func(l, resource, time);
331
332         wl_list_remove(&resource->link);
333         if (resource->object.id > 0)
334                 wl_hash_table_remove(display->objects, resource->object.id);
335         resource->destroy(resource, client);
336 }
337
338 WL_EXPORT void
339 wl_client_destroy(struct wl_client *client)
340 {
341         struct wl_resource *resource, *tmp;
342
343         printf("disconnect from client %p\n", client);
344
345         wl_list_for_each_safe(resource, tmp, &client->resource_list, link)
346                 wl_resource_destroy(resource, client, 0);
347
348         wl_event_source_remove(client->source);
349         wl_connection_destroy(client->connection);
350         wl_list_remove(&client->link);
351         free(client);
352 }
353
354 static void
355 lose_pointer_focus(struct wl_listener *listener,
356                    struct wl_resource *resource, uint32_t time)
357 {
358         struct wl_input_device *device =
359                 container_of(listener, struct wl_input_device,
360                              pointer_focus_listener);
361
362         wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
363 }
364
365 static void
366 lose_keyboard_focus(struct wl_listener *listener,
367                     struct wl_resource *resource, uint32_t time)
368 {
369         struct wl_input_device *device =
370                 container_of(listener, struct wl_input_device,
371                              keyboard_focus_listener);
372
373         wl_input_device_set_keyboard_focus(device, NULL, time);
374 }
375
376 WL_EXPORT void
377 wl_input_device_init(struct wl_input_device *device,
378                      struct wl_compositor *compositor)
379 {
380         wl_list_init(&device->pointer_focus_listener.link);
381         device->pointer_focus_listener.func = lose_pointer_focus;
382         wl_list_init(&device->keyboard_focus_listener.link);
383         device->keyboard_focus_listener.func = lose_keyboard_focus;
384
385         device->x = 100;
386         device->y = 100;
387         device->compositor = compositor;
388 }
389
390 WL_EXPORT void
391 wl_input_device_set_pointer_focus(struct wl_input_device *device,
392                                   struct wl_surface *surface,
393                                   uint32_t time,
394                                   int32_t x, int32_t y,
395                                   int32_t sx, int32_t sy)
396 {
397         if (device->pointer_focus == surface)
398                 return;
399
400         if (device->pointer_focus &&
401             (!surface || device->pointer_focus->client != surface->client))
402                 wl_client_post_event(device->pointer_focus->client,
403                                      &device->object,
404                                      WL_INPUT_DEVICE_POINTER_FOCUS,
405                                      time, NULL, 0, 0, 0, 0);
406         if (device->pointer_focus)
407                 wl_list_remove(&device->pointer_focus_listener.link);
408
409         if (surface) {
410                 wl_client_post_event(surface->client,
411                                      &device->object,
412                                      WL_INPUT_DEVICE_POINTER_FOCUS,
413                                      time, surface, x, y, sx, sy);
414                 wl_list_insert(surface->resource.destroy_listener_list.prev,
415                                &device->pointer_focus_listener.link);
416         }
417
418         device->pointer_focus = surface;
419         device->pointer_focus_time = time;
420
421 }
422
423 WL_EXPORT void
424 wl_input_device_set_keyboard_focus(struct wl_input_device *device,
425                                    struct wl_surface *surface,
426                                    uint32_t time)
427 {
428         if (device->keyboard_focus == surface)
429                 return;
430
431         if (device->keyboard_focus &&
432             (!surface || device->keyboard_focus->client != surface->client))
433                 wl_client_post_event(device->keyboard_focus->client,
434                                      &device->object,
435                                      WL_INPUT_DEVICE_KEYBOARD_FOCUS,
436                                      time, NULL, &device->keys);
437         if (device->keyboard_focus)
438                 wl_list_remove(&device->keyboard_focus_listener.link);
439
440         if (surface) {
441                 wl_client_post_event(surface->client,
442                                      &device->object,
443                                      WL_INPUT_DEVICE_KEYBOARD_FOCUS,
444                                      time, surface, &device->keys);
445                 wl_list_insert(surface->resource.destroy_listener_list.prev,
446                                &device->keyboard_focus_listener.link);
447         }
448
449         device->keyboard_focus = surface;
450         device->keyboard_focus_time = time;
451 }
452
453 WL_EXPORT void
454 wl_input_device_end_grab(struct wl_input_device *device, uint32_t time)
455 {
456         const struct wl_grab_interface *interface;
457
458         interface = device->grab->interface;
459         interface->end(device->grab, time);
460         device->grab->input_device = NULL;
461         device->grab = NULL;
462
463         wl_list_remove(&device->grab_listener.link);
464 }
465
466 static void
467 lose_grab_surface(struct wl_listener *listener,
468                   struct wl_resource *resource, uint32_t time)
469 {
470         struct wl_input_device *device =
471                 container_of(listener,
472                              struct wl_input_device, grab_listener);
473
474         wl_input_device_end_grab(device, time);
475 }
476
477 WL_EXPORT void
478 wl_input_device_start_grab(struct wl_input_device *device,
479                            struct wl_grab *grab,
480                            uint32_t button, uint32_t time)
481 {
482         struct wl_surface *focus = device->pointer_focus;
483
484         device->grab = grab;
485         device->grab_button = button;
486         device->grab_time = time;
487         device->grab_x = device->x;
488         device->grab_y = device->y;
489
490         device->grab_listener.func = lose_grab_surface;
491         wl_list_insert(focus->resource.destroy_listener_list.prev,
492                        &device->grab_listener.link);
493
494         grab->input_device = device;
495 }
496
497 WL_EXPORT int
498 wl_input_device_update_grab(struct wl_input_device *device,
499                             struct wl_grab *grab,
500                             struct wl_surface *surface, uint32_t time)
501 {
502         if (device->grab != &device->motion_grab ||
503             device->grab_time != time ||
504             device->pointer_focus != surface)
505                 return -1;
506
507         device->grab = grab;
508         grab->input_device = device;
509
510         return 0;
511 }
512
513 static void
514 display_bind(struct wl_client *client,
515              struct wl_display *display, uint32_t id,
516              const char *interface, uint32_t version)
517 {
518         struct wl_global *global;
519
520         wl_list_for_each(global, &display->global_list, link)
521                 if (global->object->id == id)
522                         break;
523
524         if (&global->link == &display->global_list)
525                 wl_client_post_error(client, &client->display->object,
526                                      WL_DISPLAY_ERROR_INVALID_OBJECT,
527                                      "invalid object %d", id);
528         else if (global->func)
529                 global->func(client, global->object, version);
530 }
531
532 static void
533 display_sync(struct wl_client *client,
534                struct wl_display *display, uint32_t key)
535 {
536         wl_client_post_event(client, &display->object, WL_DISPLAY_KEY, key, 0);
537 }
538
539 static void
540 destroy_frame_listener(struct wl_resource *resource, struct wl_client *client)
541 {
542         struct wl_frame_listener *listener =
543                 container_of(resource, struct wl_frame_listener, resource);
544
545         wl_list_remove(&listener->link);
546         free(listener);
547 }
548
549 static void
550 display_frame(struct wl_client *client,
551               struct wl_display *display,
552               struct wl_surface *surface,
553               uint32_t key)
554 {
555         struct wl_frame_listener *listener;
556
557         listener = malloc(sizeof *listener);
558         if (listener == NULL) {
559                 wl_client_post_no_memory(client);
560                 return;
561         }
562
563         /* The listener is a resource so we destroy it when the client
564          * goes away. */
565         listener->resource.destroy = destroy_frame_listener;
566         listener->resource.object.id = 0;
567         listener->client = client;
568         listener->key = key;
569         listener->surface = surface;
570         wl_list_init(&listener->resource.destroy_listener_list);
571         wl_list_insert(client->resource_list.prev, &listener->resource.link);
572         wl_list_insert(display->frame_list.prev, &listener->link);
573 }
574
575 struct wl_display_interface display_interface = {
576         display_bind,
577         display_sync,
578         display_frame
579 };
580
581
582 WL_EXPORT struct wl_display *
583 wl_display_create(void)
584 {
585         struct wl_display *display;
586         const char *debug;
587
588         debug = getenv("WAYLAND_DEBUG");
589         if (debug)
590                 wl_debug = 1;
591
592         display = malloc(sizeof *display);
593         if (display == NULL)
594                 return NULL;
595
596         display->loop = wl_event_loop_create();
597         if (display->loop == NULL) {
598                 free(display);
599                 return NULL;
600         }
601
602         display->objects = wl_hash_table_create();
603         if (display->objects == NULL) {
604                 wl_event_loop_destroy(display->loop);
605                 free(display);
606                 return NULL;
607         }
608
609         wl_list_init(&display->frame_list);
610         wl_list_init(&display->global_list);
611         wl_list_init(&display->socket_list);
612         wl_list_init(&display->client_list);
613
614         display->client_id_range = 256; /* Gah, arbitrary... */
615
616         display->id = 1;
617         display->object.interface = &wl_display_interface;
618         display->object.implementation = (void (**)(void)) &display_interface;
619         wl_display_add_object(display, &display->object);
620         if (wl_display_add_global(display, &display->object, NULL)) {
621                 wl_hash_table_destroy(display->objects);
622                 wl_event_loop_destroy(display->loop);
623                 free(display);
624                 return NULL;
625         }
626
627         return display;
628 }
629
630 WL_EXPORT void
631 wl_display_destroy(struct wl_display *display)
632 {
633         struct wl_socket *s, *next;
634         struct wl_global *global, *gnext;
635
636         wl_event_loop_destroy(display->loop);
637         wl_hash_table_destroy(display->objects);
638         wl_list_for_each_safe(s, next, &display->socket_list, link) {
639                 close(s->fd);
640                 unlink(s->addr.sun_path);
641                 close(s->fd_lock);
642                 unlink(s->lock_addr);
643                 free(s);
644         }
645
646         wl_list_for_each_safe(global, gnext, &display->global_list, link)
647                 free(global);
648
649         free(display);
650 }
651
652 WL_EXPORT void
653 wl_display_add_object(struct wl_display *display, struct wl_object *object)
654 {
655         object->id = display->id++;
656         wl_hash_table_insert(display->objects, object->id, object);
657 }
658
659 WL_EXPORT int
660 wl_display_add_global(struct wl_display *display,
661                       struct wl_object *object, wl_global_bind_func_t func)
662 {
663         struct wl_global *global;
664
665         global = malloc(sizeof *global);
666         if (global == NULL)
667                 return -1;
668
669         global->object = object;
670         global->func = func;
671         wl_list_insert(display->global_list.prev, &global->link);
672
673         return 0;
674 }
675
676 WL_EXPORT int
677 wl_display_remove_global(struct wl_display *display,
678                          struct wl_object *object)
679 {
680         struct wl_global *global;
681         struct wl_client *client;
682
683         wl_list_for_each(global, &display->global_list, link)
684                 if (global->object == object)
685                         break;
686
687         if (&global->link == &display->global_list)
688                 return -1;
689
690         wl_list_for_each(client, &display->client_list, link)
691                 wl_client_post_event(client,
692                                      &client->display->object,
693                                      WL_DISPLAY_GLOBAL_REMOVE,
694                                      global->object->id);
695         wl_list_remove(&global->link);
696         free(global);
697
698         return 0;
699 }
700
701 WL_EXPORT void
702 wl_display_post_frame(struct wl_display *display, struct wl_surface *surface,
703                       uint32_t time)
704 {
705         struct wl_frame_listener *listener, *next;
706
707         wl_list_for_each_safe(listener, next, &display->frame_list, link) {
708                 if (listener->surface != surface)
709                         continue;
710                 wl_client_post_event(listener->client, &display->object,
711                                      WL_DISPLAY_KEY, listener->key, time);
712                 wl_resource_destroy(&listener->resource, listener->client, 0);
713         }
714 }
715
716 WL_EXPORT struct wl_event_loop *
717 wl_display_get_event_loop(struct wl_display *display)
718 {
719         return display->loop;
720 }
721
722 WL_EXPORT void
723 wl_display_terminate(struct wl_display *display)
724 {
725         display->run = 0;
726 }
727
728 WL_EXPORT void
729 wl_display_run(struct wl_display *display)
730 {
731         display->run = 1;
732
733         while (display->run)
734                 wl_event_loop_dispatch(display->loop, -1);
735 }
736
737 static int
738 socket_data(int fd, uint32_t mask, void *data)
739 {
740         struct wl_display *display = data;
741         struct sockaddr_un name;
742         socklen_t length;
743         int client_fd;
744
745         length = sizeof name;
746         client_fd =
747                 accept4(fd, (struct sockaddr *) &name, &length, SOCK_CLOEXEC);
748         if (client_fd < 0 && errno == ENOSYS) {
749                 client_fd = accept(fd, (struct sockaddr *) &name, &length);
750                 if (client_fd >= 0 && fcntl(client_fd, F_SETFD, FD_CLOEXEC) == -1)
751                         fprintf(stderr, "failed to set FD_CLOEXEC flag on client fd, errno: %d\n", errno);
752         }
753
754         if (client_fd < 0)
755                 fprintf(stderr, "failed to accept, errno: %d\n", errno);
756
757         wl_client_create(display, client_fd);
758
759         return 1;
760 }
761
762 static int
763 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
764 {
765         struct stat socket_stat;
766         int lock_size = name_size + 5;
767
768         snprintf(socket->lock_addr, lock_size,
769                  "%s.lock", socket->addr.sun_path);
770
771         socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
772                                (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
773
774         if (socket->fd_lock < 0) {
775                 fprintf(stderr,
776                         "unable to open lockfile %s check permissions\n",
777                         socket->lock_addr);
778                 return -1;
779         }
780
781         if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
782                 fprintf(stderr,
783                         "unable to lock lockfile %s, maybe another compositor is running\n",
784                         socket->lock_addr);
785                 close(socket->fd_lock);
786                 return -1;
787         }
788
789         if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
790                 if (errno != ENOENT) {
791                         fprintf(stderr, "did not manage to stat file %s\n",
792                                 socket->addr.sun_path);
793                         close(socket->fd_lock);
794                         return -1;
795                 }
796         } else if (socket_stat.st_mode & S_IWUSR ||
797                    socket_stat.st_mode & S_IWGRP) {
798                 unlink(socket->addr.sun_path);
799         }
800
801         return 0;
802 }
803
804 WL_EXPORT int
805 wl_display_add_socket(struct wl_display *display, const char *name)
806 {
807         struct wl_socket *s;
808         socklen_t size, name_size;
809         const char *runtime_dir;
810
811         s = malloc(sizeof *s);
812         if (s == NULL)
813                 return -1;
814
815         s->fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
816         if (s->fd < 0) {
817                 free(s);
818                 return -1;
819         }
820
821         runtime_dir = getenv("XDG_RUNTIME_DIR");
822         if (runtime_dir == NULL) {
823                 runtime_dir = ".";
824                 fprintf(stderr,
825                         "XDG_RUNTIME_DIR not set, falling back to %s\n",
826                         runtime_dir);
827         }
828
829         if (name == NULL)
830                 name = getenv("WAYLAND_DISPLAY");
831         if (name == NULL)
832                 name = "wayland-0";
833
834         memset(&s->addr, 0, sizeof s->addr);
835         s->addr.sun_family = AF_LOCAL;
836         name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
837                              "%s/%s", runtime_dir, name) + 1;
838         fprintf(stderr, "using socket %s\n", s->addr.sun_path);
839
840         if (get_socket_lock(s,name_size) < 0) {
841                 close(s->fd);
842                 free(s);
843                 return -1;
844         }
845
846         size = offsetof (struct sockaddr_un, sun_path) + name_size;
847         if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
848                 close(s->fd);
849                 free(s);
850                 return -1;
851         }
852
853         if (listen(s->fd, 1) < 0) {
854                 close(s->fd);
855                 unlink(s->addr.sun_path);
856                 free(s);
857                 return -1;
858         }
859
860         if (wl_event_loop_add_fd(display->loop, s->fd,
861                                  WL_EVENT_READABLE,
862                                  socket_data, display) == NULL) {
863                 close(s->fd);
864                 unlink(s->addr.sun_path);
865                 free(s);
866                 return -1;
867         }
868         wl_list_insert(display->socket_list.prev, &s->link);
869
870         return 0;
871 }
872
873 static void
874 compositor_bind(struct wl_client *client,
875                 struct wl_object *global, uint32_t version)
876 {
877         struct wl_compositor *compositor =
878                 container_of(global, struct wl_compositor, object);
879
880         wl_client_post_event(client, global,
881                              WL_COMPOSITOR_TOKEN_VISUAL,
882                              &compositor->argb_visual.object,
883                              WL_COMPOSITOR_VISUAL_ARGB32);
884
885         wl_client_post_event(client, global,
886                              WL_COMPOSITOR_TOKEN_VISUAL,
887                              &compositor->premultiplied_argb_visual.object,
888                              WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32);
889
890         wl_client_post_event(client, global,
891                              WL_COMPOSITOR_TOKEN_VISUAL,
892                              &compositor->rgb_visual.object,
893                              WL_COMPOSITOR_VISUAL_XRGB32);
894 }
895
896 WL_EXPORT int
897 wl_compositor_init(struct wl_compositor *compositor,
898                    const struct wl_compositor_interface *interface,
899                    struct wl_display *display)
900 {
901         compositor->object.interface = &wl_compositor_interface;
902         compositor->object.implementation = (void (**)(void)) interface;
903         wl_display_add_object(display, &compositor->object);
904         if (wl_display_add_global(display,
905                                   &compositor->object, compositor_bind))
906                 return -1;
907
908         compositor->argb_visual.object.interface = &wl_visual_interface;
909         compositor->argb_visual.object.implementation = NULL;
910         wl_display_add_object(display, &compositor->argb_visual.object);
911         if (wl_display_add_global(display,
912                                   &compositor->argb_visual.object, NULL))
913                 return -1;
914
915         compositor->premultiplied_argb_visual.object.interface =
916                 &wl_visual_interface;
917         compositor->premultiplied_argb_visual.object.implementation = NULL;
918         wl_display_add_object(display,
919                               &compositor->premultiplied_argb_visual.object);
920        if (wl_display_add_global(display,
921                                  &compositor->premultiplied_argb_visual.object,
922                                  NULL))
923                return -1;
924
925         compositor->rgb_visual.object.interface = &wl_visual_interface;
926         compositor->rgb_visual.object.implementation = NULL;
927         wl_display_add_object(display, &compositor->rgb_visual.object);
928        if (wl_display_add_global(display,
929                                  &compositor->rgb_visual.object, NULL))
930                return -1;
931
932         return 0;
933 }