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