99512d5412d0e5cf92b2745403e640737807e387
[src/agl-compositor.git] / src / main.c
1 /*
2  * Copyright © 2012-2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <linux/input.h>
39
40 #include <libweston/backend-drm.h>
41 #include <libweston/backend-wayland.h>
42 #ifdef HAVE_BACKEND_X11
43 #include <libweston/backend-x11.h>
44 #endif
45 #include <libweston/libweston.h>
46 #include <libweston/windowed-output-api.h>
47 #include <libweston/config-parser.h>
48 #include <libweston/weston-log.h>
49 #include <weston/weston.h>
50
51 #include "shared/os-compatibility.h"
52 #include "shared/helpers.h"
53
54 #include "agl-shell-server-protocol.h"
55
56 #ifdef HAVE_REMOTING
57 #include "remote.h"
58 #endif
59
60 static int cached_tm_mday = -1;
61 static struct weston_log_scope *log_scope;
62
63 struct ivi_compositor *
64 to_ivi_compositor(struct weston_compositor *ec)
65 {
66         return weston_compositor_get_user_data(ec);
67 }
68
69 static void
70 handle_output_destroy(struct wl_listener *listener, void *data)
71 {
72         struct ivi_output *output;
73
74         output = wl_container_of(listener, output, output_destroy);
75         assert(output->output == data);
76
77         output->output = NULL;
78         wl_list_remove(&output->output_destroy.link);
79 }
80
81 struct ivi_output *
82 to_ivi_output(struct weston_output *o)
83 {
84         struct wl_listener *listener;
85         struct ivi_output *output;
86
87         listener = weston_output_get_destroy_listener(o, handle_output_destroy);
88         output = wl_container_of(listener, output, output_destroy);
89
90         return output;
91 }
92
93 static void
94 ivi_output_configure_app_id(struct ivi_output *ivi_output)
95 {
96         if (ivi_output->config) {
97                 if (ivi_output->app_id != NULL)
98                         return;
99
100                 weston_config_section_get_string(ivi_output->config,
101                                                  "agl-shell-app-id",
102                                                  &ivi_output->app_id,
103                                                  NULL);
104
105                 if (ivi_output->app_id == NULL)
106                         return;
107
108                 weston_log("Will place app_id %s on output %s\n",
109                                 ivi_output->app_id, ivi_output->name);
110         }
111 }
112
113 static struct ivi_output *
114 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
115                   struct weston_config_section *config)
116 {
117         struct ivi_output *output = NULL;
118         wl_list_for_each(output, &ivi->outputs, link) {
119                 if (strcmp(output->name, name) == 0) {
120                         free(name);
121                         return output;
122                 }
123         }
124
125         output = zalloc(sizeof *output);
126         if (!output) {
127                 free(name);
128                 return NULL;
129         }
130
131         output->ivi = ivi;
132         output->name = name;
133         output->config = config;
134
135         output->output = weston_compositor_create_output(ivi->compositor, name);
136         if (!output->output) {
137                 free(output->name);
138                 free(output);
139                 return NULL;
140         }
141
142         output->output_destroy.notify = handle_output_destroy;
143         weston_output_add_destroy_listener(output->output,
144                                            &output->output_destroy);
145
146         wl_list_insert(&ivi->outputs, &output->link);
147         ivi_output_configure_app_id(output);
148         return output;
149 }
150
151 static int
152 count_heads(struct weston_output *output)
153 {
154         struct weston_head *iter = NULL;
155         int n = 0;
156
157         while ((iter = weston_output_iterate_heads(output, iter)))
158                 ++n;
159
160         return n;
161 }
162
163 static void
164 handle_head_destroy(struct wl_listener *listener, void *data)
165 {
166         struct weston_head *head = data;
167         struct weston_output *output;
168
169         wl_list_remove(&listener->link);
170         free(listener);
171
172         output = weston_head_get_output(head);
173
174         /* On shutdown path, the output might be already gone. */
175         if (!output)
176                 return;
177
178         /* We're the last head */
179         if (count_heads(output) <= 1)
180                 weston_output_destroy(output);
181 }
182
183 static void
184 add_head_destroyed_listener(struct weston_head *head)
185 {
186         /* We already have a destroy listener */
187         if (weston_head_get_destroy_listener(head, handle_head_destroy))
188                 return;
189
190         struct wl_listener *listener = zalloc(sizeof *listener);
191         if (!listener)
192                 return;
193
194         listener->notify = handle_head_destroy;
195         weston_head_add_destroy_listener(head, listener);
196 }
197
198 static int
199 drm_configure_output(struct ivi_output *output)
200 {
201         struct ivi_compositor *ivi = output->ivi;
202         struct weston_config_section *section = output->config;
203         enum weston_drm_backend_output_mode mode =
204                 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
205         char *modeline = NULL;
206         char *gbm_format = NULL;
207         char *seat = NULL;
208
209         if (section) {
210                 char *m;
211                 weston_config_section_get_string(section, "mode", &m, "preferred");
212
213                 /* This should have been handled earlier */
214                 assert(strcmp(m, "off") != 0);
215
216                 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
217                         mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
218                 } else if (strcmp(m, "preferred") != 0) {
219                         modeline = m;
220                         m = NULL;
221                 }
222                 free(m);
223
224                 weston_config_section_get_string(section, "gbm-format",
225                                                  &gbm_format, NULL);
226
227                 weston_config_section_get_string(section, "seat", &seat, "");
228         }
229
230         if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
231                 weston_log("Cannot configure output using weston_drm_output_api.\n");
232                 free(modeline);
233                 return -1;
234         }
235         free(modeline);
236
237         ivi->drm_api->set_gbm_format(output->output, gbm_format);
238         free(gbm_format);
239
240         ivi->drm_api->set_seat(output->output, seat);
241         free(seat);
242
243         return 0;
244 }
245
246 #define WINDOWED_DEFAULT_WIDTH 1024
247 #define WINDOWED_DEFAULT_HEIGHT 768
248
249 static int
250 windowed_configure_output(struct ivi_output *output)
251 {
252         struct ivi_compositor *ivi = output->ivi;
253         struct weston_config_section *section = output->config;
254         int width = WINDOWED_DEFAULT_WIDTH;
255         int height = WINDOWED_DEFAULT_HEIGHT;
256
257         if (section) {
258                 char *mode;
259
260                 weston_config_section_get_string(section, "mode", &mode, NULL);
261                 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
262                         weston_log("Invalid mode for output %s. Using defaults.\n",
263                                    output->name);
264                         width = WINDOWED_DEFAULT_WIDTH;
265                         height = WINDOWED_DEFAULT_HEIGHT;
266                 }
267                 free(mode);
268         }
269
270         if (ivi->cmdline.width)
271                 width = ivi->cmdline.width;
272         if (ivi->cmdline.height)
273                 height = ivi->cmdline.height;
274         if (ivi->cmdline.scale)
275                 weston_output_set_scale(output->output, ivi->cmdline.scale);
276
277         if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
278                 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
279                            output->name);
280                 return -1;
281         }
282
283         return 0;
284 }
285
286 static int
287 parse_transform(const char *transform, uint32_t *out)
288 {
289         static const struct { const char *name; uint32_t token; } transforms[] = {
290                 { "normal",     WL_OUTPUT_TRANSFORM_NORMAL },
291                 { "90",         WL_OUTPUT_TRANSFORM_90 },
292                 { "180",        WL_OUTPUT_TRANSFORM_180 },
293                 { "270",        WL_OUTPUT_TRANSFORM_270 },
294                 { "flipped",    WL_OUTPUT_TRANSFORM_FLIPPED },
295                 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
296                 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
297                 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
298         };
299
300         for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
301                 if (strcmp(transforms[i].name, transform) == 0) {
302                         *out = transforms[i].token;
303                         return 0;
304                 }
305
306         *out = WL_OUTPUT_TRANSFORM_NORMAL;
307         return -1;
308 }
309
310 static int
311 configure_output(struct ivi_output *output)
312 {
313         struct ivi_compositor *ivi = output->ivi;
314         struct weston_config_section *section = output->config;
315         int32_t scale = 1;
316         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
317
318         /*
319          * This can happen with the wayland backend with 'sprawl'. The config
320          * is hard-coded, so we don't need to do anything.
321          */
322         if (!ivi->drm_api && !ivi->window_api)
323                 return 0;
324
325         if (section) {
326                 char *t;
327
328                 weston_config_section_get_int(section, "scale", &scale, 1);
329                 weston_config_section_get_string(section, "transform", &t, "normal");
330                 if (parse_transform(t, &transform) < 0)
331                         weston_log("Invalid transform \"%s\" for output %s\n",
332                                    t, output->name);
333                 free(t);
334         }
335
336         weston_output_set_scale(output->output, scale);
337         weston_output_set_transform(output->output, transform);
338
339         if (ivi->drm_api)
340                 return drm_configure_output(output);
341         else
342                 return windowed_configure_output(output);
343 }
344
345 /*
346  * Reorgainizes the output's add array into two sections.
347  * add[0..ret-1] are the heads that failed to get attached.
348  * add[ret..add_len] are the heads that were successfully attached.
349  *
350  * The order between elements in each section is stable.
351  */
352 static size_t
353 try_attach_heads(struct ivi_output *output)
354 {
355         size_t fail_len = 0;
356
357         for (size_t i = 0; i < output->add_len; ++i) {
358                 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
359                         struct weston_head *tmp = output->add[i];
360                         memmove(&output->add[fail_len + 1], output->add[fail_len],
361                                 sizeof output->add[0] * (i - fail_len));
362                         output->add[fail_len++] = tmp;
363                 }
364         }
365
366         return fail_len;
367 }
368
369 /*
370  * Like try_attach_heads, this reorganizes the output's add array into a failed
371  * and successful section.
372  * i is the number of heads that already failed the previous step.
373  */
374 static size_t
375 try_enable_output(struct ivi_output *output, size_t i)
376 {
377         for (; i < output->add_len; ++i) {
378                 struct weston_head *head;
379
380                 if (weston_output_enable(output->output) == 0)
381                         break;
382
383                 head = output->add[output->add_len - 1];
384                 memmove(&output->add[i + 1], &output->add[i],
385                         sizeof output->add[0] * (output->add_len - i));
386                 output->add[i] = head;
387
388                 weston_head_detach(head);
389         }
390
391         return i;
392 }
393
394 static int
395 try_attach_enable_heads(struct ivi_output *output)
396 {
397         size_t fail_len;
398         assert(!output->output->enabled);
399
400         fail_len = try_attach_heads(output);
401
402         if (configure_output(output) < 0)
403                 return -1;
404
405         fail_len = try_enable_output(output, fail_len);
406
407         /* All heads failed to be attached */
408         if (fail_len == output->add_len)
409                 return -1;
410
411         /* For each successful head attached */
412         for (size_t i = fail_len; i < output->add_len; ++i)
413                 add_head_destroyed_listener(output->add[i]);
414
415         output->add_len = fail_len;
416         return 0;
417 }
418
419 static int
420 process_output(struct ivi_output *output)
421 {
422         if (output->output->enabled) {
423                 output->add_len = try_attach_heads(output);
424                 return output->add_len == 0 ? 0 : -1;
425         }
426
427         return try_attach_enable_heads(output);
428 }
429
430 static void
431 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
432 {
433         struct weston_output *output;
434         struct ivi_output *ivi_output;
435         struct wl_listener *listener;
436
437         output = weston_head_get_output(head);
438         assert(output);
439
440         listener = weston_output_get_destroy_listener(output,
441                                                       handle_output_destroy);
442         assert(listener);
443
444         ivi_output = wl_container_of(listener, ivi_output, output_destroy);
445         assert(ivi_output->output == output);
446
447         weston_head_detach(head);
448         if (count_heads(ivi_output->output) == 0) {
449                 weston_output_disable(ivi_output->output);
450         }
451 }
452
453 static struct weston_config_section *
454 find_controlling_output_config(struct weston_config *config,
455                                const char *name)
456 {
457         struct weston_config_section *section;
458         char *same_as;
459         int depth = 0;
460
461         same_as = strdup(name);
462         do {
463                 section = weston_config_get_section(config, "output",
464                                                     "name", same_as);
465                 if (!section && depth > 0)
466                         weston_log("Configuration error: output section reffered"
467                                    "to by same-as=%s not found.\n", same_as);
468                 free(same_as);
469
470                 if (!section)
471                         return NULL;
472
473                 if (depth++ > 8) {
474                         weston_log("Configuration error: same-as nested too "
475                                    "deep for output '%s'.\n", name);
476                         return NULL;
477                 }
478
479                 weston_config_section_get_string(section, "same-as",
480                                                  &same_as, NULL);
481         } while (same_as);
482
483         return section;
484 }
485
486 static void
487 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
488 {
489         const char *name = weston_head_get_name(head);
490         struct weston_config_section *section;
491         struct ivi_output *output;
492         char *output_name = NULL;
493
494         section = find_controlling_output_config(ivi->config, name);
495         if (section) {
496                 char *mode;
497
498                 weston_config_section_get_string(section, "mode", &mode, NULL);
499                 if (mode && strcmp(mode, "off") == 0) {
500                         free(mode);
501                         return;
502                 }
503                 free(mode);
504
505                 weston_config_section_get_string(section, "name",
506                                                  &output_name, NULL);
507         } else {
508                 output_name = strdup(name);
509         }
510
511         if (!output_name)
512                 return;
513
514         output = ivi_ensure_output(ivi, output_name, section);
515         if (!output)
516                 return;
517
518         if (output->add_len >= ARRAY_LENGTH(output->add))
519                 return;
520
521         output->add[output->add_len++] = head;
522 }
523
524 static void
525 heads_changed(struct wl_listener *listener, void *arg)
526 {
527         struct weston_compositor *compositor = arg;
528         struct weston_head *head = NULL;
529         struct ivi_compositor *ivi = to_ivi_compositor(compositor);
530         struct ivi_output *output;
531
532         while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
533                 bool connected = weston_head_is_connected(head);
534                 bool enabled = weston_head_is_enabled(head);
535                 bool changed = weston_head_is_device_changed(head);
536                 bool non_desktop = weston_head_is_non_desktop(head);
537
538                 if (connected && !enabled && !non_desktop)
539                         head_prepare_enable(ivi, head);
540                 else if (!connected && enabled)
541                         head_disable(ivi, head);
542                 else if (enabled && changed)
543                         weston_log("Detected a monitor change on head '%s', "
544                                    "not bothering to do anything about it.\n",
545                                    weston_head_get_name(head));
546
547                 weston_head_reset_device_changed(head);
548         }
549
550         wl_list_for_each(output, &ivi->outputs, link) {
551                 if (output->add_len == 0)
552                         continue;
553
554                 if (process_output(output) < 0) {
555                         output->add_len = 0;
556                         ivi->init_failed = true;
557                 }
558         }
559 }
560
561 #ifdef HAVE_REMOTING
562 static int
563 drm_backend_remoted_output_configure(struct weston_output *output,
564                                      struct weston_config_section *section,
565                                      char *modeline,
566                                      const struct weston_remoting_api *api)
567 {
568         char *gbm_format = NULL;
569         char *seat = NULL;
570         char *host = NULL;
571         char *pipeline = NULL;
572         int port, ret;
573         int32_t scale = 1;
574         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
575         char *trans;
576
577         ret = api->set_mode(output, modeline);
578         if (ret < 0) {
579                 weston_log("Cannot configure an output \"%s\" using "
580                                 "weston_remoting_api. Invalid mode\n",
581                                 output->name);
582                 return -1;
583         }
584
585         weston_config_section_get_int(section, "scale", &scale, 1);
586         weston_output_set_scale(output, scale);
587
588         weston_config_section_get_string(section, "transform", &trans, "normal");
589         if (parse_transform(trans, &transform) < 0) {
590                 weston_log("Invalid transform \"%s\" for output %s\n",
591                            trans, output->name);
592         }
593         weston_output_set_transform(output, transform);
594
595         weston_config_section_get_string(section, "gbm-format",
596                                          &gbm_format, NULL);
597         api->set_gbm_format(output, gbm_format);
598         free(gbm_format);
599
600         weston_config_section_get_string(section, "seat", &seat, "");
601
602         api->set_seat(output, seat);
603         free(seat);
604
605         weston_config_section_get_string(section, "gst-pipeline", &pipeline,
606                         NULL);
607         if (pipeline) {
608                 api->set_gst_pipeline(output, pipeline);
609                 free(pipeline);
610                 return 0;
611         }
612
613         weston_config_section_get_string(section, "host", &host, NULL);
614         weston_config_section_get_int(section, "port", &port, 0);
615         if (!host || port <= 0 || 65533 < port) {
616                 weston_log("Cannot configure an output \"%s\". "
617                                 "Need to specify gst-pipeline or "
618                                 "host and port (1-65533).\n", output->name);
619         }
620         api->set_host(output, host);
621         free(host);
622         api->set_port(output, port);
623
624         return 0;
625 }
626
627
628 static int
629 remote_output_init(struct ivi_output *ivi_output,
630                    struct weston_compositor *compositor,
631                    struct weston_config_section *section,
632                    const struct weston_remoting_api *api)
633 {
634         char *output_name, *modeline = NULL;
635         int ret = -1;
636
637         weston_config_section_get_string(section, "name", &output_name, NULL);
638         if (!output_name)
639                 return ret;
640
641         weston_config_section_get_string(section, "mode", &modeline, "off");
642         if (strcmp(modeline, "off") == 0)
643                 goto err;
644
645         ivi_output->output = api->create_output(compositor, output_name);
646         if (!ivi_output->output) {
647                 weston_log("Cannot create remoted output \"%s\".\n",
648                                 output_name);
649                 goto err;
650         }
651
652         ret = drm_backend_remoted_output_configure(ivi_output->output, section,
653                                                    modeline, api);
654         if (ret < 0) {
655                 weston_log("Cannot configure remoted output \"%s\".\n",
656                                 output_name);
657                 goto err;
658         }
659
660         if (weston_output_enable(ivi_output->output) < 0) {
661                 weston_log("Enabling remoted output \"%s\" failed.\n",
662                                 output_name);
663                 goto err;
664         }
665
666         free(modeline);
667         free(output_name);
668         weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
669
670         return 0;
671
672 err:
673         free(modeline);
674         free(output_name);
675         if (ivi_output->output)
676                 weston_output_destroy(ivi_output->output);
677
678         return ret;
679 }
680
681 static void
682 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
683 {
684         struct weston_config_section *remote_section = NULL;
685         const char *section_name;
686         struct weston_config *config = ivi->config;
687
688         while (weston_config_next_section(config, &remote_section, &section_name)) {
689                 if (strcmp(section_name, "remote-output"))
690                         continue;
691
692                 struct ivi_output *ivi_output = NULL;
693                 bool output_found = false;
694                 char *_name = NULL;
695
696                 weston_config_section_get_string(remote_section,
697                                                  "name", &_name, NULL);
698                 wl_list_for_each(ivi_output, &ivi->outputs, link) {
699                         if (!strcmp(ivi_output->name, _name)) {
700                                 output_found = true;
701                                 break;
702                         }
703                 }
704
705                 if (output_found) {
706                         free(_name);
707                         continue;
708                 }
709
710                 ivi_output = zalloc(sizeof(*ivi_output));
711
712                 ivi_output->ivi = ivi;
713                 ivi_output->name = _name;
714                 ivi_output->config = remote_section;
715
716                 if (remote_output_init(ivi_output, ivi->compositor,
717                                        remote_section, ivi->remoting_api)) {
718                         free(ivi_output->name);
719                         free(ivi_output);
720                         continue;
721                 }
722
723                 ivi_output->output_destroy.notify = handle_output_destroy;
724                 weston_output_add_destroy_listener(ivi_output->output,
725                                                    &ivi_output->output_destroy);
726
727                 wl_list_insert(&ivi->outputs, &ivi_output->link);
728                 ivi_output_configure_app_id(ivi_output);
729         }
730 }
731
732 static int
733 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
734 {
735         struct weston_compositor *compositor = ivi->compositor;
736         int (*module_init)(struct weston_compositor *wc);
737
738         module_init = weston_load_module("remoting-plugin.so",
739                                          "weston_module_init");
740         if (!module_init)
741                 return -1;
742
743         if (module_init(compositor) < 0)
744                 return -1;
745
746         ivi->remoting_api = weston_remoting_get_api(compositor);
747         if (!ivi->remoting_api)
748                 return -1;
749         return 0;
750 }
751 #else
752 static int
753 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
754 {
755         return -1;
756 }
757 #endif
758
759 static int
760 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
761 {
762         struct weston_drm_backend_config config = {
763                 .base = {
764                         .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
765                         .struct_size = sizeof config,
766                 },
767         };
768         struct weston_config_section *section;
769         int use_current_mode = 0;
770         int use_pixman = 0;
771         bool use_shadow;
772         int ret;
773
774         const struct weston_option options[] = {
775                 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
776                 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
777                 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
778                 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
779                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
780         };
781
782         parse_options(options, ARRAY_LENGTH(options), argc, argv);
783         config.use_pixman = use_pixman;
784         ivi->cmdline.use_current_mode = use_current_mode;
785
786         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
787         weston_config_section_get_string(section, "gbm-format",
788                                          &config.gbm_format, NULL);
789         weston_config_section_get_uint(section, "pageflip-timeout",
790                                        &config.pageflip_timeout, 0);
791         weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
792         config.use_pixman_shadow = use_shadow;
793
794         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
795                                              &config.base);
796         if (ret < 0)
797                 return ret;
798
799         ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
800         if (!ivi->drm_api) {
801                 weston_log("Cannot use drm output api.\n");
802                 ret = -1;
803                 goto error;
804         }
805
806         load_remoting_plugin(ivi, ivi->config);
807
808 error:
809         free(config.gbm_format);
810         free(config.seat_id);
811         return ret;
812 }
813
814 static void
815 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
816                               bool *use_pixman, bool *fullscreen, int *output_count)
817 {
818         struct weston_config_section *section;
819         bool pixman;
820         int fs = 0;
821
822         const struct weston_option options[] = {
823                 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
824                 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
825                 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
826                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
827                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
828                 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
829         };
830
831         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
832         weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
833
834         *output_count = 1;
835         parse_options(options, ARRAY_LENGTH(options), argc, argv);
836         *use_pixman = pixman;
837         *fullscreen = fs;
838 }
839
840 static int
841 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
842                         const char *match_prefix, const char *name_prefix)
843 {
844         struct weston_config_section *section = NULL;
845         const char *section_name;
846         char *default_output = NULL;
847         int i = 0;
848         size_t match_len = strlen(match_prefix);
849
850         while (weston_config_next_section(ivi->config, &section, &section_name)) {
851                 char *output_name;
852
853                 if (i >= output_count)
854                         break;
855
856                 if (strcmp(section_name, "output") != 0)
857                         continue;
858
859                 weston_config_section_get_string(section, "name", &output_name, NULL);
860                 if (output_name == NULL)
861                         continue;
862                 if (strncmp(output_name, match_prefix, match_len) != 0) {
863                         free(output_name);
864                         continue;
865                 }
866
867                 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
868                         free(output_name);
869                         return -1;
870                 }
871
872                 free(output_name);
873                 ++i;
874         }
875
876         for (; i < output_count; ++i) {
877                 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
878                         return -1;
879
880                 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
881                         free(default_output);
882                         return -1;
883                 }
884
885                 free(default_output);
886         }
887
888         return 0;
889 }
890
891 static int
892 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
893 {
894         struct weston_wayland_backend_config config = {
895                 .base = {
896                         .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
897                         .struct_size = sizeof config,
898                 },
899         };
900         struct weston_config_section *section;
901         int sprawl = 0;
902         int output_count;
903         int ret;
904
905         const struct weston_option options[] = {
906                 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
907                 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
908         };
909
910         windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
911                                       &config.fullscreen, &output_count);
912
913         parse_options(options, ARRAY_LENGTH(options), argc, argv);
914         config.sprawl = sprawl;
915
916         section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
917         weston_config_section_get_string(section, "cursor-theme",
918                                          &config.cursor_theme, NULL);
919         weston_config_section_get_int(section, "cursor-size",
920                                       &config.cursor_size, 32);
921
922         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
923                                              &config.base);
924
925         free(config.cursor_theme);
926         free(config.display_name);
927
928         if (ret < 0)
929                 return ret;
930
931         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
932
933         /*
934          * We will just assume if load_backend() finished cleanly and
935          * windowed_output_api is not present that wayland backend is started
936          * with --sprawl or runs on fullscreen-shell. In this case, all values
937          * are hardcoded, so nothing can be configured; simply create and
938          * enable an output.
939          */
940         if (ivi->window_api == NULL)
941                 return 0;
942
943         return windowed_create_outputs(ivi, output_count, "WL", "wayland");
944 }
945
946 #ifdef HAVE_BACKEND_X11
947 static int
948 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
949 {
950         struct weston_x11_backend_config config = {
951                 .base = {
952                         .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
953                         .struct_size = sizeof config,
954                 },
955         };
956         int no_input = 0;
957         int output_count;
958         int ret;
959
960         const struct weston_option options[] = {
961                { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
962         };
963
964         windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
965                                       &config.fullscreen, &output_count);
966
967         parse_options(options, ARRAY_LENGTH(options), argc, argv);
968         config.no_input = no_input;
969
970         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
971                                              &config.base);
972
973         if (ret < 0)
974                 return ret;
975
976         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
977         if (!ivi->window_api) {
978                 weston_log("Cannot use weston_windowed_output_api.\n");
979                 return -1;
980         }
981
982         return windowed_create_outputs(ivi, output_count, "X", "screen");
983 }
984 #else
985 static int
986 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
987 {
988         return -1;
989 }
990 #endif
991
992 static int
993 load_backend(struct ivi_compositor *ivi, const char *backend,
994              int *argc, char *argv[])
995 {
996         if (strcmp(backend, "drm-backend.so") == 0) {
997                 return load_drm_backend(ivi, argc, argv);
998         } else if (strcmp(backend, "wayland-backend.so") == 0) {
999                 return load_wayland_backend(ivi, argc, argv);
1000         } else if (strcmp(backend, "x11-backend.so") == 0) {
1001                 return load_x11_backend(ivi, argc, argv);
1002         }
1003
1004         weston_log("fatal: unknown backend '%s'.\n", backend);
1005         return -1;
1006 }
1007
1008 static char *
1009 choose_default_backend(void)
1010 {
1011         char *backend = NULL;
1012
1013         if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1014                 backend = strdup("wayland-backend.so");
1015         else if (getenv("DISPLAY"))
1016                 backend = strdup("x11-backend.so");
1017         else
1018                 backend = strdup("drm-backend.so");
1019
1020         return backend;
1021 }
1022
1023 static int
1024 compositor_init_config(struct weston_compositor *compositor,
1025                        struct weston_config *config)
1026 {
1027         struct xkb_rule_names xkb_names;
1028         struct weston_config_section *section;
1029         int repaint_msec;
1030         bool vt_switching;
1031         bool require_input;
1032
1033         /* agl-compositor.ini [keyboard] */
1034         section = weston_config_get_section(config, "keyboard", NULL, NULL);
1035         weston_config_section_get_string(section, "keymap_rules",
1036                                          (char **) &xkb_names.rules, NULL);
1037         weston_config_section_get_string(section, "keymap_model",
1038                                          (char **) &xkb_names.model, NULL);
1039         weston_config_section_get_string(section, "keymap_layout",
1040                                          (char **) &xkb_names.layout, NULL);
1041         weston_config_section_get_string(section, "keymap_variant",
1042                                          (char **) &xkb_names.variant, NULL);
1043         weston_config_section_get_string(section, "keymap_options",
1044                                          (char **) &xkb_names.options, NULL);
1045
1046         if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1047                 return -1;
1048
1049         weston_config_section_get_int(section, "repeat-rate",
1050                                       &compositor->kb_repeat_rate, 40);
1051         weston_config_section_get_int(section, "repeat-delay",
1052                                       &compositor->kb_repeat_delay, 400);
1053
1054         weston_config_section_get_bool(section, "vt-switching",
1055                                        &vt_switching, true);
1056         compositor->vt_switching = vt_switching;
1057
1058         /* agl-compositor.ini [core] */
1059         section = weston_config_get_section(config, "core", NULL, NULL);
1060
1061         weston_config_section_get_bool(section, "require-input", &require_input, true);
1062         compositor->require_input = require_input;
1063
1064         weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1065                                       compositor->repaint_msec);
1066         if (repaint_msec < -10 || repaint_msec > 1000) {
1067                 weston_log("Invalid repaint_window value in config: %d\n",
1068                            repaint_msec);
1069         } else {
1070                 compositor->repaint_msec = repaint_msec;
1071         }
1072         weston_log("Output repaint window is %d ms maximum.\n",
1073                    compositor->repaint_msec);
1074
1075         return 0;
1076 }
1077
1078 struct ivi_surface *
1079 to_ivi_surface(struct weston_surface *surface)
1080 {
1081         struct weston_desktop_surface *dsurface;
1082
1083         dsurface = weston_surface_get_desktop_surface(surface);
1084         if (!dsurface)
1085                 return NULL;
1086
1087         return weston_desktop_surface_get_user_data(dsurface);
1088 }
1089
1090 static void
1091 activate_binding(struct weston_seat *seat,
1092                  struct weston_view *focus_view)
1093 {
1094         struct weston_surface *focus = focus_view->surface;
1095         struct weston_surface *main_surface =
1096                 weston_surface_get_main_surface(focus);
1097         struct ivi_surface *surface;
1098
1099         surface = to_ivi_surface(main_surface);
1100         if (!surface)
1101                 return;
1102
1103         weston_seat_set_keyboard_focus(seat, focus);
1104 }
1105
1106 static void
1107 click_to_activate_binding(struct weston_pointer *pointer,
1108                           const struct timespec *time,
1109                           uint32_t button, void *data)
1110 {
1111         if (pointer->grab != &pointer->default_grab)
1112                 return;
1113         if (pointer->focus == NULL)
1114                 return;
1115
1116         activate_binding(pointer->seat, pointer->focus);
1117 }
1118
1119 static void
1120 touch_to_activate_binding(struct weston_touch *touch,
1121                           const struct timespec *time,
1122                           void *data)
1123 {
1124         if (touch->grab != &touch->default_grab)
1125                 return;
1126         if (touch->focus == NULL)
1127                 return;
1128
1129         activate_binding(touch->seat, touch->focus);
1130 }
1131
1132 static void
1133 add_bindings(struct weston_compositor *compositor)
1134 {
1135         weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1136                                              click_to_activate_binding,
1137                                              NULL);
1138         weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1139                                              click_to_activate_binding,
1140                                              NULL);
1141         weston_compositor_add_touch_binding(compositor, 0,
1142                                             touch_to_activate_binding,
1143                                             NULL);
1144 }
1145
1146 static int
1147 create_listening_socket(struct wl_display *display, const char *socket_name)
1148 {
1149         if (socket_name) {
1150                 if (wl_display_add_socket(display, socket_name)) {
1151                         weston_log("fatal: failed to add socket: %s\n",
1152                                    strerror(errno));
1153                         return -1;
1154                 }
1155         } else {
1156                 socket_name = wl_display_add_socket_auto(display);
1157                 if (!socket_name) {
1158                         weston_log("fatal: failed to add socket: %s\n",
1159                                    strerror(errno));
1160                         return -1;
1161                 }
1162         }
1163
1164         setenv("WAYLAND_DISPLAY", socket_name, 1);
1165
1166         return 0;
1167 }
1168
1169 static bool
1170 global_filter(const struct wl_client *client, const struct wl_global *global,
1171               void *data)
1172 {
1173         return true;
1174 }
1175
1176 static int
1177 load_config(struct weston_config **config, bool no_config,
1178             const char *config_file)
1179 {
1180         const char *file = "agl-compositor.ini";
1181         const char *full_path;
1182
1183         if (config_file)
1184                 file = config_file;
1185
1186         if (!no_config)
1187                 *config = weston_config_parse(file);
1188
1189         if (*config) {
1190                 full_path = weston_config_get_full_path(*config);
1191
1192                 weston_log("Using config file '%s'.\n", full_path);
1193                 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1194
1195                 return 0;
1196         }
1197
1198         if (config_file && !no_config) {
1199                 weston_log("fatal: error opening or reading config file '%s'.\n",
1200                         config_file);
1201
1202                 return -1;
1203         }
1204
1205         weston_log("Starting with no config file.\n");
1206         setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1207
1208         return 0;
1209 }
1210
1211 static FILE *logfile;
1212
1213 static char *
1214 log_timestamp(char *buf, size_t len)
1215 {
1216         struct timeval tv;
1217         struct tm *brokendown_time;
1218         char datestr[128];
1219         char timestr[128];
1220
1221         gettimeofday(&tv, NULL);
1222
1223         brokendown_time = localtime(&tv.tv_sec);
1224         if (brokendown_time == NULL) {
1225                 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1226                 return buf;
1227         }
1228
1229         memset(datestr, 0, sizeof(datestr));
1230         if (brokendown_time->tm_mday != cached_tm_mday) {
1231                 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1232                                 brokendown_time);
1233                 cached_tm_mday = brokendown_time->tm_mday;
1234         }
1235
1236         strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1237         /* if datestr is empty it prints only timestr*/
1238         snprintf(buf, len, "%s[%s.%03li]", datestr,
1239                         timestr, (tv.tv_usec / 1000));
1240
1241         return buf;
1242 }
1243
1244 static void
1245 custom_handler(const char *fmt, va_list arg)
1246 {
1247         char timestr[512];
1248
1249         weston_log_scope_printf(log_scope, "%s libwayland: ",
1250                         log_timestamp(timestr, sizeof(timestr)));
1251         weston_log_scope_vprintf(log_scope, fmt, arg);
1252 }
1253
1254 static void
1255 log_file_open(const char *filename)
1256 {
1257         wl_log_set_handler_server(custom_handler);
1258
1259         if (filename)
1260                 logfile = fopen(filename, "a");
1261
1262         if (!logfile) {
1263                 logfile = stderr;
1264         } else {
1265                 os_fd_set_cloexec(fileno(logfile));
1266                 setvbuf(logfile, NULL, _IOLBF, 256);
1267         }
1268 }
1269
1270 static void
1271 log_file_close(void)
1272 {
1273         if (logfile && logfile != stderr)
1274                 fclose(logfile);
1275         logfile = stderr;
1276 }
1277
1278 static int
1279 vlog(const char *fmt, va_list ap)
1280 {
1281         const char *oom = "Out of memory";
1282         char timestr[128];
1283         int len = 0;
1284         char *str;
1285
1286         if (weston_log_scope_is_enabled(log_scope)) {
1287                 int len_va;
1288                 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1289                 len_va = vasprintf(&str, fmt, ap);
1290                 if (len_va >= 0) {
1291                         len = weston_log_scope_printf(log_scope, "%s %s",
1292                                         xlog_timestamp, str);
1293                         free(str);
1294                 } else {
1295                         len = weston_log_scope_printf(log_scope, "%s %s",
1296                                         xlog_timestamp, oom);
1297                 }
1298         }
1299
1300         return len;
1301 }
1302
1303
1304 static int
1305 vlog_continue(const char *fmt, va_list ap)
1306 {
1307         return weston_log_scope_vprintf(log_scope, fmt, ap);
1308 }
1309
1310 static int
1311 on_term_signal(int signo, void *data)
1312 {
1313         struct wl_display *display = data;
1314
1315         weston_log("caught signal %d\n", signo);
1316         wl_display_terminate(display);
1317
1318         return 1;
1319 }
1320
1321 static void
1322 handle_exit(struct weston_compositor *compositor)
1323 {
1324         wl_display_terminate(compositor->wl_display);
1325 }
1326
1327 static void
1328 usage(int error_code)
1329 {
1330         FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1331         fprintf(out,
1332                 "Usage: agl-compositor [OPTIONS]\n"
1333                 "\n"
1334                 "This is " PACKAGE_STRING ", the reference compositor for\n"
1335                 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1336                 "on which backend is in use different options will be accepted.\n"
1337                 "\n"
1338                 "Core options:\n"
1339                 "\n"
1340                 "  --version\t\tPrint agl-compositor version\n"
1341                 "  -B, --backend=MODULE\tBackend module, one of\n"
1342                         "\t\t\t\tdrm-backend.so\n"
1343                         "\t\t\t\twayland-backend.so\n"
1344                         "\t\t\t\tx11-backend.so\n"
1345                 "  -S, --socket=NAME\tName of socket to listen on\n"
1346                 "  --log=FILE\t\tLog to the given file\n"
1347                 "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1348                 "  --no-config\t\tDo not read agl-compositor.ini\n"
1349                 "  --debug\t\tEnable debug extension\n"
1350                 "  -h, --help\t\tThis help message\n"
1351                 "\n");
1352         exit(error_code);
1353 }
1354
1355 int main(int argc, char *argv[])
1356 {
1357         struct ivi_compositor ivi = { 0 };
1358         struct wl_display *display = NULL;
1359         struct wl_event_loop *loop;
1360         struct wl_event_source *signals[3] = { 0 };
1361         struct weston_config_section *section;
1362         /* Command line options */
1363         char *backend = NULL;
1364         char *socket_name = NULL;
1365         char *log = NULL;
1366         int help = 0;
1367         int version = 0;
1368         int no_config = 0;
1369         char *config_file = NULL;
1370         struct weston_log_context *log_ctx = NULL;
1371         struct weston_log_subscriber *logger;
1372         int ret = EXIT_FAILURE;
1373
1374         const struct weston_option core_options[] = {
1375                 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1376                 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1377                 { WESTON_OPTION_STRING, "log", 0, &log },
1378                 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1379                 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1380                 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1381                 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1382         };
1383
1384         wl_list_init(&ivi.outputs);
1385         wl_list_init(&ivi.surfaces);
1386         wl_list_init(&ivi.pending_surfaces);
1387         wl_list_init(&ivi.popup_pending_apps);
1388         wl_list_init(&ivi.fullscreen_pending_apps);
1389         wl_list_init(&ivi.split_pending_apps);
1390         wl_list_init(&ivi.remote_pending_apps);
1391         wl_list_init(&ivi.desktop_clients);
1392
1393         /* Prevent any clients we spawn getting our stdin */
1394         os_fd_set_cloexec(STDIN_FILENO);
1395
1396         parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1397
1398         if (help)
1399                 usage(EXIT_SUCCESS);
1400
1401         if (version) {
1402                 printf(PACKAGE_STRING "\n");
1403                 return EXIT_SUCCESS;
1404         }
1405
1406         log_ctx = weston_log_ctx_compositor_create();
1407         if (!log_ctx) {
1408                 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1409                 return ret;
1410         }
1411
1412         log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1413                                                     "agl-compositor log\n",
1414                                                     NULL, NULL, NULL);
1415
1416         log_file_open(log);
1417         weston_log_set_handler(vlog, vlog_continue);
1418
1419         logger = weston_log_subscriber_create_log(logfile);
1420         weston_log_subscribe(log_ctx, logger, "log");
1421
1422         if (load_config(&ivi.config, no_config, config_file) < 0)
1423                 goto error_signals;
1424         section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1425         if (!backend) {
1426                 weston_config_section_get_string(section, "backend", &backend,
1427                                                  NULL);
1428                 if (!backend)
1429                         backend = choose_default_backend();
1430         }
1431
1432         display = wl_display_create();
1433         loop = wl_display_get_event_loop(display);
1434
1435         wl_display_set_global_filter(display,
1436                                      global_filter, &ivi);
1437
1438         /* Register signal handlers so we shut down cleanly */
1439
1440         signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1441                                               display);
1442         signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1443                                               display);
1444         signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1445                                               display);
1446
1447         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1448                 if (!signals[i])
1449                         goto error_signals;
1450
1451         ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1452         if (!ivi.compositor) {
1453                 weston_log("fatal: failed to create compositor.\n");
1454                 goto error_signals;
1455         }
1456
1457         if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1458                 goto error_compositor;
1459
1460         if (load_backend(&ivi, backend, &argc, argv) < 0) {
1461                 weston_log("fatal: failed to create compositor backend.\n");
1462                 goto error_compositor;
1463         }
1464
1465         ivi.heads_changed.notify = heads_changed;
1466         weston_compositor_add_heads_changed_listener(ivi.compositor,
1467                                                      &ivi.heads_changed);
1468
1469         if (ivi_desktop_init(&ivi) < 0)
1470                 goto error_compositor;
1471
1472         if (ivi_policy_init(&ivi) < 0)
1473                 goto error_compositor;
1474
1475         if (ivi_shell_init(&ivi) < 0)
1476                 goto error_compositor;
1477
1478         add_bindings(ivi.compositor);
1479
1480         weston_compositor_flush_heads_changed(ivi.compositor);
1481
1482         if (ivi.remoting_api)
1483                 ivi_enable_remote_outputs(&ivi);
1484
1485         ivi_shell_init_black_fs(&ivi);
1486
1487         if (create_listening_socket(display, socket_name) < 0)
1488                 goto error_compositor;
1489
1490         ivi.compositor->exit = handle_exit;
1491
1492         weston_compositor_wake(ivi.compositor);
1493
1494         ivi_shell_create_global(&ivi);
1495         ivi_launch_shell_client(&ivi);
1496         ivi_agl_systemd_notify(&ivi);
1497
1498         wl_display_run(display);
1499
1500         ret = ivi.compositor->exit_code;
1501
1502         wl_display_destroy_clients(display);
1503
1504 error_compositor:
1505         weston_compositor_tear_down(ivi.compositor);
1506
1507         weston_compositor_log_scope_destroy(log_scope);
1508         log_scope = NULL;
1509
1510         weston_log_ctx_compositor_destroy(ivi.compositor);
1511         weston_compositor_destroy(ivi.compositor);
1512
1513         weston_log_subscriber_destroy_log(logger);
1514
1515         ivi_policy_destroy(ivi.policy);
1516
1517 error_signals:
1518         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1519                 if (signals[i])
1520                         wl_event_source_remove(signals[i]);
1521
1522         wl_display_destroy(display);
1523
1524         log_file_close();
1525         if (ivi.config)
1526                 weston_config_destroy(ivi.config);
1527
1528         return ret;
1529 }