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