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