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