Use wl_client_post_global() for connect events
[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_global(client, global->object);
248
249         return client;
250 }
251
252 WL_EXPORT void
253 wl_client_add_resource(struct wl_client *client,
254                        struct wl_resource *resource)
255 {
256         struct wl_display *display = client->display;
257
258         if (client->id_count-- < 64)
259                 wl_display_post_range(display, client);
260
261         wl_hash_table_insert(client->display->objects,
262                              resource->object.id, resource);
263         wl_list_insert(client->resource_list.prev, &resource->link);
264 }
265
266 WL_EXPORT void
267 wl_client_post_no_memory(struct wl_client *client)
268 {
269         wl_client_post_event(client,
270                              &client->display->object,
271                              WL_DISPLAY_NO_MEMORY);
272 }
273
274 WL_EXPORT void
275 wl_client_post_global(struct wl_client *client, struct wl_object *object)
276 {
277         wl_client_post_event(client,
278                              &client->display->object,
279                              WL_DISPLAY_GLOBAL,
280                              object,
281                              object->interface->name,
282                              object->interface->version);
283 }
284
285 WL_EXPORT void
286 wl_resource_destroy(struct wl_resource *resource, struct wl_client *client)
287 {
288         struct wl_display *display = client->display;
289
290         wl_list_remove(&resource->link);
291         if (resource->object.id > 0)
292                 wl_hash_table_remove(display->objects, resource->object.id);
293         resource->destroy(resource, client);
294 }
295
296 WL_EXPORT void
297 wl_client_destroy(struct wl_client *client)
298 {
299         struct wl_resource *resource, *tmp;
300
301         printf("disconnect from client %p\n", client);
302
303         wl_list_for_each_safe(resource, tmp, &client->resource_list, link)
304                 wl_resource_destroy(resource, client);
305
306         wl_event_source_remove(client->source);
307         wl_connection_destroy(client->connection);
308         free(client);
309 }
310
311 static void
312 lose_pointer_focus(struct wl_listener *listener,
313                    struct wl_surface *surface, uint32_t time)
314 {
315         struct wl_input_device *device =
316                 container_of(listener, struct wl_input_device,
317                              pointer_focus_listener);
318
319         wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
320 }
321
322 static void
323 lose_keyboard_focus(struct wl_listener *listener,
324                     struct wl_surface *surface, uint32_t time)
325 {
326         struct wl_input_device *device =
327                 container_of(listener, struct wl_input_device,
328                              keyboard_focus_listener);
329
330         wl_input_device_set_keyboard_focus(device, NULL, time);
331 }
332
333 WL_EXPORT void
334 wl_input_device_init(struct wl_input_device *device,
335                      struct wl_compositor *compositor)
336 {
337         wl_list_init(&device->pointer_focus_listener.link);
338         device->pointer_focus_listener.func = lose_pointer_focus;
339         wl_list_init(&device->keyboard_focus_listener.link);
340         device->keyboard_focus_listener.func = lose_keyboard_focus;
341
342         device->x = 100;
343         device->y = 100;
344         device->compositor = compositor;
345 }
346
347 WL_EXPORT void
348 wl_input_device_set_pointer_focus(struct wl_input_device *device,
349                                   struct wl_surface *surface,
350                                   uint32_t time,
351                                   int32_t x, int32_t y,
352                                   int32_t sx, int32_t sy)
353 {
354         if (device->pointer_focus == surface)
355                 return;
356
357         if (device->pointer_focus &&
358             (!surface || device->pointer_focus->client != surface->client))
359                 wl_client_post_event(device->pointer_focus->client,
360                                      &device->object,
361                                      WL_INPUT_DEVICE_POINTER_FOCUS,
362                                      time, NULL, 0, 0, 0, 0);
363         if (device->pointer_focus)
364                 wl_list_remove(&device->pointer_focus_listener.link);
365
366         if (surface) {
367                 wl_client_post_event(surface->client,
368                                      &device->object,
369                                      WL_INPUT_DEVICE_POINTER_FOCUS,
370                                      time, surface, x, y, sx, sy);
371                 wl_list_insert(surface->destroy_listener_list.prev,
372                                &device->pointer_focus_listener.link);
373         }
374
375         device->pointer_focus = surface;
376         device->pointer_focus_time = time;
377
378 }
379
380 WL_EXPORT void
381 wl_input_device_set_keyboard_focus(struct wl_input_device *device,
382                                    struct wl_surface *surface,
383                                    uint32_t time)
384 {
385         if (device->keyboard_focus == surface)
386                 return;
387
388         if (device->keyboard_focus &&
389             (!surface || device->keyboard_focus->client != surface->client))
390                 wl_client_post_event(device->keyboard_focus->client,
391                                      &device->object,
392                                      WL_INPUT_DEVICE_KEYBOARD_FOCUS,
393                                      time, NULL, &device->keys);
394         if (device->keyboard_focus)
395                 wl_list_remove(&device->keyboard_focus_listener.link);
396
397         if (surface) {
398                 wl_client_post_event(surface->client,
399                                      &device->object,
400                                      WL_INPUT_DEVICE_KEYBOARD_FOCUS,
401                                      time, surface, &device->keys);
402                 wl_list_insert(surface->destroy_listener_list.prev,
403                                &device->keyboard_focus_listener.link);
404         }
405
406         device->keyboard_focus = surface;
407         device->keyboard_focus_time = time;
408 }
409
410 WL_EXPORT void
411 wl_input_device_end_grab(struct wl_input_device *device, uint32_t time)
412 {
413         const struct wl_grab_interface *interface;
414
415         interface = device->grab->interface;
416         interface->end(device->grab, time);
417         device->grab->input_device = NULL;
418         device->grab = NULL;
419
420         wl_list_remove(&device->grab_listener.link);
421 }
422
423 static void
424 lose_grab_surface(struct wl_listener *listener,
425                   struct wl_surface *surface, uint32_t time)
426 {
427         struct wl_input_device *device =
428                 container_of(listener,
429                              struct wl_input_device, grab_listener);
430
431         wl_input_device_end_grab(device, time);
432 }
433
434 WL_EXPORT void
435 wl_input_device_start_grab(struct wl_input_device *device,
436                            struct wl_grab *grab,
437                            uint32_t button, uint32_t time)
438 {
439         struct wl_surface *focus = device->pointer_focus;
440
441         device->grab = grab;
442         device->grab_button = button;
443         device->grab_time = time;
444         device->grab_x = device->x;
445         device->grab_y = device->y;
446
447         device->grab_listener.func = lose_grab_surface;
448         wl_list_insert(focus->destroy_listener_list.prev,
449                        &device->grab_listener.link);
450
451         grab->input_device = device;
452 }
453
454 WL_EXPORT int
455 wl_input_device_update_grab(struct wl_input_device *device,
456                             struct wl_grab *grab,
457                             struct wl_surface *surface, uint32_t time)
458 {
459         if (device->grab != &device->motion_grab ||
460             device->grab_time != time ||
461             device->pointer_focus != surface)
462                 return -1;
463
464         device->grab = grab;
465         grab->input_device = device;
466
467         return 0;
468 }
469
470 static void
471 display_bind(struct wl_client *client,
472              struct wl_display *display, uint32_t id,
473              const char *interface, uint32_t version)
474 {
475         struct wl_global *global;
476
477         wl_list_for_each(global, &display->global_list, link)
478                 if (global->object->id == id && global->func)
479                         global->func(client, global->object);
480 }
481
482 static void
483 display_sync(struct wl_client *client,
484                struct wl_display *display, uint32_t key)
485 {
486         wl_client_post_event(client, &display->object, WL_DISPLAY_KEY, key, 0);
487 }
488
489 static void
490 destroy_frame_listener(struct wl_resource *resource, struct wl_client *client)
491 {
492         struct wl_frame_listener *listener =
493                 container_of(resource, struct wl_frame_listener, resource);
494
495         wl_list_remove(&listener->link);
496         free(listener);
497 }
498
499 static void
500 display_frame(struct wl_client *client,
501               struct wl_display *display,
502               struct wl_surface *surface,
503               uint32_t key)
504 {
505         struct wl_frame_listener *listener;
506
507         listener = malloc(sizeof *listener);
508         if (listener == NULL) {
509                 wl_client_post_no_memory(client);
510                 return;
511         }
512
513         /* The listener is a resource so we destroy it when the client
514          * goes away. */
515         listener->resource.destroy = destroy_frame_listener;
516         listener->resource.object.id = 0;
517         listener->client = client;
518         listener->key = key;
519         listener->surface = surface;
520         wl_list_insert(client->resource_list.prev, &listener->resource.link);
521         wl_list_insert(display->frame_list.prev, &listener->link);
522 }
523
524 struct wl_display_interface display_interface = {
525         display_bind,
526         display_sync,
527         display_frame
528 };
529
530
531 WL_EXPORT struct wl_display *
532 wl_display_create(void)
533 {
534         struct wl_display *display;
535         const char *debug;
536
537         debug = getenv("WAYLAND_DEBUG");
538         if (debug)
539                 wl_debug = 1;
540
541         display = malloc(sizeof *display);
542         if (display == NULL)
543                 return NULL;
544
545         display->loop = wl_event_loop_create();
546         if (display->loop == NULL) {
547                 free(display);
548                 return NULL;
549         }
550
551         display->objects = wl_hash_table_create();
552         if (display->objects == NULL) {
553                 wl_event_loop_destroy(display->loop);
554                 free(display);
555                 return NULL;
556         }
557
558         wl_list_init(&display->frame_list);
559         wl_list_init(&display->global_list);
560         wl_list_init(&display->socket_list);
561
562         display->client_id_range = 256; /* Gah, arbitrary... */
563
564         display->id = 1;
565         display->object.interface = &wl_display_interface;
566         display->object.implementation = (void (**)(void)) &display_interface;
567         wl_display_add_object(display, &display->object);
568         if (wl_display_add_global(display, &display->object, NULL)) {
569                 wl_hash_table_destroy(display->objects);
570                 wl_event_loop_destroy(display->loop);
571                 free(display);
572                 return NULL;
573         }
574
575         return display;
576 }
577
578 WL_EXPORT void
579 wl_display_destroy(struct wl_display *display)
580 {
581         struct wl_socket *s, *next;
582
583         wl_event_loop_destroy(display->loop);
584         wl_hash_table_destroy(display->objects);
585         
586         wl_list_for_each_safe(s, next, &display->socket_list, link) {
587                 close(s->fd);
588                 unlink(s->addr.sun_path);
589                 close(s->fd_lock);
590                 unlink(s->lock_addr);
591                 free(s);
592         }
593
594         free(display);
595 }
596
597 WL_EXPORT void
598 wl_display_add_object(struct wl_display *display, struct wl_object *object)
599 {
600         object->id = display->id++;
601         wl_hash_table_insert(display->objects, object->id, object);
602 }
603
604 WL_EXPORT int
605 wl_display_add_global(struct wl_display *display,
606                       struct wl_object *object, wl_client_connect_func_t func)
607 {
608         struct wl_global *global;
609
610         global = malloc(sizeof *global);
611         if (global == NULL)
612                 return -1;
613
614         global->object = object;
615         global->func = func;
616         wl_list_insert(display->global_list.prev, &global->link);
617
618         return 0;       
619 }
620
621 WL_EXPORT void
622 wl_display_post_frame(struct wl_display *display, struct wl_surface *surface,
623                       uint32_t time)
624 {
625         struct wl_frame_listener *listener, *next;
626
627         wl_list_for_each_safe(listener, next, &display->frame_list, link) {
628                 if (listener->surface != surface)
629                         continue;
630                 wl_client_post_event(listener->client, &display->object,
631                                      WL_DISPLAY_KEY, listener->key, time);
632                 wl_resource_destroy(&listener->resource, listener->client);
633         }
634 }
635
636 WL_EXPORT struct wl_event_loop *
637 wl_display_get_event_loop(struct wl_display *display)
638 {
639         return display->loop;
640 }
641
642 WL_EXPORT void
643 wl_display_terminate(struct wl_display *display)
644 {
645         display->run = 0;
646 }
647
648 WL_EXPORT void
649 wl_display_run(struct wl_display *display)
650 {
651         display->run = 1;
652
653         while (display->run)
654                 wl_event_loop_dispatch(display->loop, -1);
655 }
656
657 static void
658 socket_data(int fd, uint32_t mask, void *data)
659 {
660         struct wl_display *display = data;
661         struct sockaddr_un name;
662         socklen_t length;
663         int client_fd;
664
665         length = sizeof name;
666         client_fd =
667                 accept4(fd, (struct sockaddr *) &name, &length, SOCK_CLOEXEC);
668         if (client_fd < 0)
669                 fprintf(stderr, "failed to accept\n");
670
671         wl_client_create(display, client_fd);
672 }
673
674 static int
675 get_socket_lock(struct wl_socket *socket, socklen_t name_size)
676 {
677         struct stat socket_stat;
678         int lock_size = name_size + 5;
679
680         snprintf(socket->lock_addr, lock_size,
681                  "%s.lock", socket->addr.sun_path);
682
683         socket->fd_lock = open(socket->lock_addr, O_CREAT | O_CLOEXEC,
684                                (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
685
686         if (socket->fd_lock < 0) {
687                 fprintf(stderr,
688                         "unable to open lockfile %s check permissions\n",
689                         socket->lock_addr);
690                 return -1;
691         }
692
693         if (flock(socket->fd_lock, LOCK_EX | LOCK_NB) < 0) {
694                 fprintf(stderr,
695                         "unable to lock lockfile %s, maybe another compositor is running\n",
696                         socket->lock_addr);
697                 close(socket->fd_lock);
698                 return -1;
699         }
700
701         if (stat(socket->addr.sun_path, &socket_stat) < 0 ) {
702                 if (errno != ENOENT) {
703                         fprintf(stderr, "did not manage to stat file %s\n",
704                                 socket->addr.sun_path);
705                         close(socket->fd_lock);
706                         return -1;
707                 }
708         } else if (socket_stat.st_mode & S_IWUSR ||
709                    socket_stat.st_mode & S_IWGRP) {
710                 unlink(socket->addr.sun_path);
711         }
712
713         return 0;
714 }
715
716 WL_EXPORT int
717 wl_display_add_socket(struct wl_display *display, const char *name)
718 {
719         struct wl_socket *s;
720         socklen_t size, name_size;
721         const char *runtime_dir;
722
723         s = malloc(sizeof *s);
724         if (s == NULL)
725                 return -1;
726
727         s->fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
728         if (s->fd < 0) {
729                 free(s);
730                 return -1;
731         }
732
733         runtime_dir = getenv("XDG_RUNTIME_DIR");
734         if (runtime_dir == NULL) {
735                 runtime_dir = ".";
736                 fprintf(stderr,
737                         "XDG_RUNTIME_DIR not set, falling back to %s\n",
738                         runtime_dir);
739         }
740
741         if (name == NULL)
742                 name = getenv("WAYLAND_DISPLAY");
743         if (name == NULL)
744                 name = "wayland-0";
745
746         memset(&s->addr, 0, sizeof s->addr);
747         s->addr.sun_family = AF_LOCAL;
748         name_size = snprintf(s->addr.sun_path, sizeof s->addr.sun_path,
749                              "%s/%s", runtime_dir, name) + 1;
750         fprintf(stderr, "using socket %s\n", s->addr.sun_path);
751
752         if (get_socket_lock(s,name_size) < 0) {
753                 close(s->fd);
754                 free(s);
755                 return -1;
756         }
757
758         size = offsetof (struct sockaddr_un, sun_path) + name_size;
759         if (bind(s->fd, (struct sockaddr *) &s->addr, size) < 0) {
760                 close(s->fd);
761                 free(s);
762                 return -1;
763         }
764
765         if (listen(s->fd, 1) < 0) {
766                 close(s->fd);
767                 unlink(s->addr.sun_path);
768                 free(s);
769                 return -1;
770         }
771
772         if (wl_event_loop_add_fd(display->loop, s->fd,
773                                  WL_EVENT_READABLE,
774                                  socket_data, display) == NULL) {
775                 close(s->fd);
776                 unlink(s->addr.sun_path);
777                 free(s);
778                 return -1;
779         }
780         wl_list_insert(display->socket_list.prev, &s->link);
781
782         return 0;
783 }
784
785 WL_EXPORT int
786 wl_compositor_init(struct wl_compositor *compositor,
787                    const struct wl_compositor_interface *interface,
788                    struct wl_display *display)
789 {
790         compositor->object.interface = &wl_compositor_interface;
791         compositor->object.implementation = (void (**)(void)) interface;
792         wl_display_add_object(display, &compositor->object);
793         if (wl_display_add_global(display, &compositor->object, NULL))
794                 return -1;
795
796         compositor->argb_visual.object.interface = &wl_visual_interface;
797         compositor->argb_visual.object.implementation = NULL;
798         wl_display_add_object(display, &compositor->argb_visual.object);
799         if (wl_display_add_global(display, &compositor->argb_visual.object, NULL))
800                 return -1;
801
802         compositor->premultiplied_argb_visual.object.interface =
803                 &wl_visual_interface;
804         compositor->premultiplied_argb_visual.object.implementation = NULL;
805         wl_display_add_object(display,
806                               &compositor->premultiplied_argb_visual.object);
807         if (wl_display_add_global(display,
808                                   &compositor->premultiplied_argb_visual.object,
809                                   NULL))
810                 return -1;
811
812         compositor->rgb_visual.object.interface = &wl_visual_interface;
813         compositor->rgb_visual.object.implementation = NULL;
814         wl_display_add_object(display,
815                               &compositor->rgb_visual.object);
816         if (wl_display_add_global(display,
817                                   &compositor->rgb_visual.object, NULL))
818                 return -1;
819
820         return 0;
821 }