compositor: Don't reuse weston_compositor_create_output
[src/agl-compositor.git] / src / compositor.c
1 /*
2  * Copyright © 2012-2024 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_RDP
43 #include <libweston/backend-rdp.h>
44 #endif
45 #ifdef HAVE_BACKEND_HEADLESS
46 #include <libweston/backend-headless.h>
47 #endif
48 #ifdef HAVE_BACKEND_X11
49 #include <libweston/backend-x11.h>
50 #endif
51 #include <libweston/libweston.h>
52 #include <libweston/windowed-output-api.h>
53 #include <libweston/config-parser.h>
54 #include <libweston/weston-log.h>
55 #include <weston.h>
56
57 #include "shared/os-compatibility.h"
58 #include "shared/helpers.h"
59
60 #include "config.h"
61 #include "agl-shell-server-protocol.h"
62
63 #ifdef HAVE_REMOTING
64 #include "remote.h"
65 #endif
66
67 static int cached_tm_mday = -1;
68 static struct weston_log_scope *log_scope;
69
70 struct ivi_compositor *
71 to_ivi_compositor(struct weston_compositor *ec)
72 {
73         return weston_compositor_get_user_data(ec);
74 }
75
76 struct ivi_output_config *
77 ivi_init_parsed_options(struct weston_compositor *compositor)
78 {
79         struct ivi_compositor *ivi = to_ivi_compositor(compositor);
80         struct ivi_output_config *config;
81
82         config = zalloc(sizeof *config);
83         if (!config)
84                 return NULL;
85
86         config->width = 0;
87         config->height = 0;
88         config->scale = 0;
89         config->transform = UINT32_MAX;
90
91         ivi->parsed_options = config;
92
93         return config;
94 }
95
96
97
98 static void
99 sigint_helper(int sig)
100 {
101        raise(SIGUSR2);
102 }
103
104 struct {
105         char *name;
106         enum weston_renderer_type renderer;
107 } renderer_name_map[] = {
108         { "auto", WESTON_RENDERER_AUTO },
109         { "gl", WESTON_RENDERER_GL },
110         { "noop", WESTON_RENDERER_NOOP },
111         { "pixman", WESTON_RENDERER_PIXMAN },
112 };
113
114 struct {
115         char *short_name;
116         char *long_name;
117         enum weston_compositor_backend backend;
118 } backend_name_map[] = {
119         { "drm", "drm-backend.so", WESTON_BACKEND_DRM },
120         { "headless", "headless-backend.so", WESTON_BACKEND_HEADLESS },
121         { "pipewire", "pipewire-backend.so", WESTON_BACKEND_PIPEWIRE },
122         { "rdp", "rdp-backend.so", WESTON_BACKEND_RDP },
123         { "vnc", "vnc-backend.so", WESTON_BACKEND_VNC },
124         { "wayland", "wayland-backend.so", WESTON_BACKEND_WAYLAND },
125         { "x11", "x11-backend.so", WESTON_BACKEND_X11 },
126 };
127
128 bool
129 get_backend_from_string(const char *name,
130                 enum weston_compositor_backend *backend)
131 {
132         size_t i;
133
134         for (i = 0; i < ARRAY_LENGTH(backend_name_map); i++) {
135                 if (strcmp(name, backend_name_map[i].short_name) == 0 ||
136                                 strcmp(name, backend_name_map[i].long_name) == 0) {
137                         *backend = backend_name_map[i].backend;
138                         return true;
139                 }
140         }
141
142         return false;
143 }
144
145
146 bool
147 get_renderer_from_string(const char *name, enum weston_renderer_type *renderer)
148 {
149         size_t i;
150
151         if (!name)
152                 name = "auto";
153
154         for (i = 0; i < ARRAY_LENGTH(renderer_name_map); i++) {
155                 if (strcmp(name, renderer_name_map[i].name) == 0) {
156                         *renderer = renderer_name_map[i].renderer;
157                         return true;
158                 }
159         }
160
161         return false;
162 }
163
164
165 void
166 ivi_layout_save(struct ivi_compositor *ivi, struct ivi_output *output)
167 {
168         struct ivi_output *new_output;
169         ivi->need_ivi_output_relayout = true;
170
171         new_output = zalloc(sizeof(*new_output));
172
173         new_output->ivi = ivi;
174         new_output->background = output->background;
175
176         new_output->top = output->top;
177         new_output->bottom = output->bottom;
178         new_output->left = output->left;
179         new_output->right = output->right;
180
181         new_output->active = output->active;
182         new_output->previous_active = output->previous_active;
183         new_output->name = strdup(output->name);
184         if (output->app_ids)
185                 new_output->app_ids = strdup(output->app_ids);
186
187         new_output->area = output->area;
188         new_output->area_saved = output->area_saved;
189         new_output->area_activation = output->area_activation;
190
191         weston_log("saving output layout for output %s\n", new_output->name);
192
193         wl_list_insert(&ivi->saved_outputs, &new_output->link);
194 }
195
196 void
197 ivi_layout_restore(struct ivi_compositor *ivi, struct ivi_output *n_output)
198 {
199         struct ivi_output *output = NULL;
200         struct ivi_output *iter_output;
201
202         if (!ivi->need_ivi_output_relayout)
203                 return;
204
205         ivi->need_ivi_output_relayout = false;
206
207         wl_list_for_each(iter_output, &ivi->saved_outputs, link) {
208                 if (strcmp(n_output->name, iter_output->name) == 0) {
209                         output = iter_output;
210                         break;
211                 }
212         }
213
214         if (!output)
215                 return;
216
217         weston_log("restoring output layout for output %s\n", output->name);
218         n_output->background = output->background;
219
220         n_output->top = output->top;
221         n_output->bottom = output->bottom;
222         n_output->left = output->left;
223         n_output->right = output->right;
224
225         n_output->active = output->active;
226         n_output->previous_active = output->previous_active;
227         if (output->app_ids)
228                 n_output->app_ids = strdup(output->app_ids);
229
230         n_output->area = output->area;
231         n_output->area_saved = output->area_saved;
232         n_output->area_activation = output->area_activation;
233
234         free(output->app_ids);
235         free(output->name);
236         wl_list_remove(&output->link);
237         free(output);
238 }
239
240 void
241 ivi_layout_destroy_saved_outputs(struct ivi_compositor *ivi)
242 {
243         struct ivi_output *output, *output_next;
244
245         wl_list_for_each_safe(output, output_next, &ivi->saved_outputs, link) {
246                 free(output->app_ids);
247                 free(output->name);
248
249                 wl_list_remove(&output->link);
250                 free(output);
251         }
252 }
253
254 static void
255 handle_output_destroy(struct wl_listener *listener, void *data)
256 {
257         struct ivi_output *output;
258
259         output = wl_container_of(listener, output, output_destroy);
260         assert(output->output == data);
261
262         if (output->fullscreen_view.fs &&
263             output->fullscreen_view.fs->view) {
264                 weston_surface_unref(output->fullscreen_view.fs->view->surface);
265                 weston_buffer_destroy_solid(output->fullscreen_view.buffer_ref);
266                 output->fullscreen_view.fs->view = NULL;
267         }
268
269         ivi_layout_save(output->ivi, output);
270
271         output->output = NULL;
272         wl_list_remove(&output->output_destroy.link);
273 }
274
275 struct ivi_output *
276 to_ivi_output(struct weston_output *o)
277 {
278         struct wl_listener *listener;
279         struct ivi_output *output;
280
281         listener = weston_output_get_destroy_listener(o, handle_output_destroy);
282         output = wl_container_of(listener, output, output_destroy);
283
284         return output;
285 }
286
287 static void
288 ivi_output_configure_app_id(struct ivi_output *ivi_output)
289 {
290         if (ivi_output->config) {
291                 if (ivi_output->app_ids != NULL)
292                         return;
293
294                 weston_config_section_get_string(ivi_output->config,
295                                                  "agl-shell-app-id",
296                                                  &ivi_output->app_ids,
297                                                  NULL);
298
299                 if (ivi_output->app_ids == NULL)
300                         return;
301
302                 weston_log("Will place app_id %s on output %s\n",
303                                 ivi_output->app_ids, ivi_output->name);
304         }
305 }
306
307 static struct ivi_output *
308 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
309                   struct weston_config_section *config,
310                   struct weston_head *head)
311 {
312         struct ivi_output *output = NULL;
313         wl_list_for_each(output, &ivi->outputs, link) {
314                 if (strcmp(output->name, name) == 0) {
315                         free(name);
316                         return output;
317                 }
318         }
319
320         output = zalloc(sizeof *output);
321         if (!output) {
322                 free(name);
323                 return NULL;
324         }
325
326         output->ivi = ivi;
327         output->name = name;
328         output->config = config;
329
330         output->output =
331                 weston_compositor_create_output(ivi->compositor, head, head->name);
332         if (!output->output) {
333                 free(output->name);
334                 free(output);
335                 return NULL;
336         }
337
338         if (ivi->simple_output_configure) {
339                 int ret = ivi->simple_output_configure(output->output);
340                 if (ret < 0) {
341                         weston_log("Configuring output \"%s\" failed.\n",
342                                         weston_head_get_name(head));
343                         weston_output_destroy(output->output);
344                         ivi->init_failed = true;
345                         return NULL;
346                 }
347
348                 if (weston_output_enable(output->output) < 0) {
349                         weston_log("Enabling output \"%s\" failed.\n",
350                                         weston_head_get_name(head));
351                         weston_output_destroy(output->output);
352                         ivi->init_failed = true;
353                         return NULL;
354                 }
355         }
356
357         output->output_destroy.notify = handle_output_destroy;
358         weston_output_add_destroy_listener(output->output,
359                                            &output->output_destroy);
360
361         wl_list_insert(&ivi->outputs, &output->link);
362         ivi_output_configure_app_id(output);
363         return output;
364 }
365
366 static int
367 count_heads(struct weston_output *output)
368 {
369         struct weston_head *iter = NULL;
370         int n = 0;
371
372         while ((iter = weston_output_iterate_heads(output, iter)))
373                 ++n;
374
375         return n;
376 }
377
378 static void
379 handle_head_destroy(struct wl_listener *listener, void *data)
380 {
381         struct weston_head *head = data;
382         struct weston_output *output;
383
384         wl_list_remove(&listener->link);
385         free(listener);
386
387         output = weston_head_get_output(head);
388
389         /* On shutdown path, the output might be already gone. */
390         if (!output)
391                 return;
392
393         /* We're the last head */
394         if (count_heads(output) <= 1)
395                 weston_output_destroy(output);
396 }
397
398 static void
399 add_head_destroyed_listener(struct weston_head *head)
400 {
401         /* We already have a destroy listener */
402         if (weston_head_get_destroy_listener(head, handle_head_destroy))
403                 return;
404
405         struct wl_listener *listener = zalloc(sizeof *listener);
406         if (!listener)
407                 return;
408
409         listener->notify = handle_head_destroy;
410         weston_head_add_destroy_listener(head, listener);
411 }
412
413 static int
414 drm_configure_output(struct ivi_output *output)
415 {
416         struct ivi_compositor *ivi = output->ivi;
417         struct weston_config_section *section = output->config;
418         enum weston_drm_backend_output_mode mode =
419                 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
420         char *modeline = NULL;
421         char *gbm_format = NULL;
422         char *seat = NULL;
423
424         if (section) {
425                 char *m;
426                 weston_config_section_get_string(section, "mode", &m, "preferred");
427
428                 /* This should have been handled earlier */
429                 assert(strcmp(m, "off") != 0);
430
431                 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
432                         mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
433                 } else if (strcmp(m, "preferred") != 0) {
434                         modeline = m;
435                         m = NULL;
436                 }
437                 free(m);
438
439                 weston_config_section_get_string(section, "gbm-format",
440                                                  &gbm_format, NULL);
441
442                 weston_config_section_get_string(section, "seat", &seat, "");
443         }
444
445         if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
446                 weston_log("Cannot configure output using weston_drm_output_api.\n");
447                 free(modeline);
448                 return -1;
449         }
450         free(modeline);
451
452         ivi->drm_api->set_gbm_format(output->output, gbm_format);
453         free(gbm_format);
454
455         ivi->drm_api->set_seat(output->output, seat);
456         free(seat);
457
458         return 0;
459 }
460
461 #define WINDOWED_DEFAULT_WIDTH 1024
462 #define WINDOWED_DEFAULT_HEIGHT 768
463
464 static int
465 windowed_configure_output(struct ivi_output *output)
466 {
467         struct ivi_compositor *ivi = output->ivi;
468         struct weston_config_section *section = output->config;
469         int width = WINDOWED_DEFAULT_WIDTH;
470         int height = WINDOWED_DEFAULT_HEIGHT;
471
472         if (section) {
473                 char *mode;
474
475                 weston_config_section_get_string(section, "mode", &mode, NULL);
476                 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
477                         weston_log("Invalid mode for output %s. Using defaults.\n",
478                                    output->name);
479                         width = WINDOWED_DEFAULT_WIDTH;
480                         height = WINDOWED_DEFAULT_HEIGHT;
481                 }
482                 free(mode);
483         }
484
485         if (ivi->cmdline.width)
486                 width = ivi->cmdline.width;
487         if (ivi->cmdline.height)
488                 height = ivi->cmdline.height;
489         if (ivi->cmdline.scale)
490                 weston_output_set_scale(output->output, ivi->cmdline.scale);
491
492         if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
493                 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
494                            output->name);
495                 return -1;
496         }
497
498         weston_log("Configured windowed_output_api to %dx%d\n", width, height);
499
500         return 0;
501 }
502
503 static int
504 parse_transform(const char *transform, uint32_t *out)
505 {
506         static const struct { const char *name; uint32_t token; } transforms[] = {
507                 { "normal",             WL_OUTPUT_TRANSFORM_NORMAL },
508                 { "rotate-90",          WL_OUTPUT_TRANSFORM_90 },
509                 { "rotate-180",         WL_OUTPUT_TRANSFORM_180 },
510                 { "rotate-270",         WL_OUTPUT_TRANSFORM_270 },
511                 { "flipped",            WL_OUTPUT_TRANSFORM_FLIPPED },
512                 { "flipped-rotate-90",  WL_OUTPUT_TRANSFORM_FLIPPED_90 },
513                 { "flipped-rotate-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
514                 { "flipped-rotate-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
515         };
516
517         for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
518                 if (strcmp(transforms[i].name, transform) == 0) {
519                         *out = transforms[i].token;
520                         return 0;
521                 }
522
523         *out = WL_OUTPUT_TRANSFORM_NORMAL;
524         return -1;
525 }
526
527 int
528 parse_activation_area(const char *geometry, struct ivi_output *output)
529 {
530         int n;
531         unsigned width, height, x, y;
532
533         n = sscanf(geometry, "%ux%u+%u,%u", &width, &height, &x, &y);
534         if (n != 4) {
535                 return -1;
536         }
537         output->area_activation.width = width;
538         output->area_activation.height = height;
539         output->area_activation.x = x;
540         output->area_activation.y = y;
541         return 0;
542 }
543
544 static int
545 configure_output(struct ivi_output *output)
546 {
547         struct ivi_compositor *ivi = output->ivi;
548         struct weston_config_section *section = output->config;
549         int32_t scale = 1;
550         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
551
552         /*
553          * This can happen with the wayland backend with 'sprawl'. The config
554          * is hard-coded, so we don't need to do anything.
555          */
556         if (!ivi->drm_api && !ivi->window_api)
557                 return 0;
558
559         if (section) {
560                 char *t;
561
562                 weston_config_section_get_int(section, "scale", &scale, 1);
563                 weston_config_section_get_string(section, "transform", &t, "normal");
564                 if (parse_transform(t, &transform) < 0)
565                         weston_log("Invalid transform \"%s\" for output %s\n",
566                                    t, output->name);
567                 weston_config_section_get_string(section, "activation-area", &t, "");
568                 if (parse_activation_area(t, output) < 0)
569                         weston_log("Invalid activation-area \"%s\" for output %s\n",
570                                    t, output->name);
571                 free(t);
572         }
573
574         weston_output_set_scale(output->output, scale);
575         weston_output_set_transform(output->output, transform);
576
577         if (ivi->drm_api)
578                 return drm_configure_output(output);
579         else
580                 return windowed_configure_output(output);
581 }
582
583 /*
584  * Reorgainizes the output's add array into two sections.
585  * add[0..ret-1] are the heads that failed to get attached.
586  * add[ret..add_len] are the heads that were successfully attached.
587  *
588  * The order between elements in each section is stable.
589  */
590 static size_t
591 try_attach_heads(struct ivi_output *output)
592 {
593         size_t fail_len = 0;
594
595         for (size_t i = 1; i < output->add_len; i++) {
596                 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
597                         struct weston_head *tmp = output->add[i];
598                         memmove(&output->add[fail_len + 1], output->add[fail_len],
599                                 sizeof output->add[0] * (i - fail_len));
600                         output->add[fail_len++] = tmp;
601                 }
602         }
603         return fail_len;
604 }
605
606 /* Place output exactly to the right of the most recently enabled output.
607  *
608  * Historically, we haven't given much thought to output placement,
609  * simply adding outputs in a horizontal line as they're enabled. This
610  * function simply sets an output's x coordinate to the right of the
611  * most recently enabled output, and its y to zero.
612  *
613  * If you're adding new calls to this function, you're also not giving
614  * much thought to output placement, so please consider carefully if
615  * it's really doing what you want.
616  *
617  * You especially don't want to use this for any code that won't
618  * immediately enable the passed output.
619  */
620 static void
621 weston_output_lazy_align(struct weston_output *output)
622 {
623         struct weston_compositor *c;
624         struct weston_output *peer;
625         int next_x = 0;
626
627         /* Put this output to the right of the most recently enabled output */
628         c = output->compositor;
629         if (!wl_list_empty(&c->output_list)) {
630                 peer = container_of(c->output_list.prev,
631                                 struct weston_output, link);
632                 next_x = peer->pos.c.x + peer->width;
633         }
634         output->pos.c.x = next_x;
635         output->pos.c.y = 0;
636 }
637
638
639 /*
640  * Like try_attach_heads, this reorganizes the output's add array into a failed
641  * and successful section.
642  * i is the number of heads that already failed the previous step.
643  */
644 static size_t
645 try_enable_output(struct ivi_output *output, size_t i)
646 {
647         for (; i < output->add_len; ++i) {
648                 struct weston_head *head;
649
650                 weston_output_lazy_align(output->output);
651
652                 if (weston_output_enable(output->output) == 0)
653                         break;
654
655                 head = output->add[output->add_len - 1];
656                 memmove(&output->add[i + 1], &output->add[i],
657                         sizeof output->add[0] * (output->add_len - i));
658                 output->add[i] = head;
659
660                 weston_head_detach(head);
661         }
662
663         return i;
664 }
665
666 static int
667 try_attach_enable_heads(struct ivi_output *output)
668 {
669         size_t fail_len;
670         assert(!output->output->enabled);
671
672         fail_len = try_attach_heads(output);
673
674         if (configure_output(output) < 0)
675                 return -1;
676
677         fail_len = try_enable_output(output, fail_len);
678
679         /* All heads failed to be attached */
680         if (fail_len == output->add_len)
681                 return -1;
682
683         /* For each successful head attached */
684         for (size_t i = fail_len; i < output->add_len; ++i)
685                 add_head_destroyed_listener(output->add[i]);
686
687         ivi_layout_restore(output->ivi, output);
688
689         output->add_len = fail_len;
690         return 0;
691 }
692
693 static int
694 process_output(struct ivi_output *output)
695 {
696         if (output->output->enabled) {
697                 output->add_len = try_attach_heads(output);
698                 return output->add_len == 0 ? 0 : -1;
699         }
700
701         return try_attach_enable_heads(output);
702 }
703
704 static void
705 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
706 {
707         struct weston_output *output;
708         struct ivi_output *ivi_output;
709         struct wl_listener *listener;
710
711         output = weston_head_get_output(head);
712         assert(output);
713
714         listener = weston_output_get_destroy_listener(output,
715                                                       handle_output_destroy);
716         assert(listener);
717
718         ivi_output = wl_container_of(listener, ivi_output, output_destroy);
719         assert(ivi_output->output == output);
720
721         weston_head_detach(head);
722         if (count_heads(ivi_output->output) == 0) {
723                 if (ivi_output->output) {
724                         /* ivi_output->output destruction may be deferred in
725                          * some cases (see drm_output_destroy()), so we need to
726                          * forcibly trigger the destruction callback now, or
727                          * otherwise would later access data that we are about
728                          * to free
729                          */
730                         struct weston_output *save = ivi_output->output;
731
732                         handle_output_destroy(&ivi_output->output_destroy, save);
733                         weston_output_destroy(save);
734                 }
735         }
736         wl_list_remove(&ivi_output->link);
737         free(ivi_output->app_ids);
738         free(ivi_output);
739 }
740
741 static struct weston_config_section *
742 find_controlling_output_config(struct weston_config *config,
743                                const char *name)
744 {
745         struct weston_config_section *section;
746         char *same_as;
747         int depth = 0;
748
749         same_as = strdup(name);
750         do {
751                 section = weston_config_get_section(config, "output",
752                                                     "name", same_as);
753                 if (!section && depth > 0)
754                         weston_log("Configuration error: output section reffered"
755                                    "to by same-as=%s not found.\n", same_as);
756                 free(same_as);
757
758                 if (!section)
759                         return NULL;
760
761                 if (depth++ > 8) {
762                         weston_log("Configuration error: same-as nested too "
763                                    "deep for output '%s'.\n", name);
764                         return NULL;
765                 }
766
767                 weston_config_section_get_string(section, "same-as",
768                                                  &same_as, NULL);
769         } while (same_as);
770
771         return section;
772 }
773
774 static void
775 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
776 {
777         const char *name = weston_head_get_name(head);
778         struct weston_config_section *section;
779         struct ivi_output *output;
780         char *output_name = NULL;
781
782
783         section = find_controlling_output_config(ivi->config, name);
784         if (section) {
785                 char *mode;
786
787                 weston_config_section_get_string(section, "mode", &mode, NULL);
788                 if (mode && strcmp(mode, "off") == 0) {
789                         free(mode);
790                         return;
791                 }
792                 free(mode);
793
794                 weston_config_section_get_string(section, "name",
795                                                  &output_name, NULL);
796         } else {
797                 output_name = strdup(name);
798         }
799
800         if (!output_name)
801                 return;
802
803         output = ivi_ensure_output(ivi, output_name, section, head);
804         if (!output)
805                 return;
806
807         if (output->add_len >= ARRAY_LENGTH(output->add))
808                 return;
809
810         output->add[output->add_len++] = head;
811 }
812
813 static void
814 heads_changed(struct wl_listener *listener, void *arg)
815 {
816         struct weston_compositor *compositor = arg;
817         struct weston_head *head = NULL;
818         struct ivi_compositor *ivi = to_ivi_compositor(compositor);
819         struct ivi_output *output;
820
821         while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
822                 bool connected = weston_head_is_connected(head);
823                 bool enabled = weston_head_is_enabled(head);
824                 bool changed = weston_head_is_device_changed(head);
825                 bool non_desktop = weston_head_is_non_desktop(head);
826
827                 if (connected && !enabled && !non_desktop)
828                         head_prepare_enable(ivi, head);
829                 else if (!connected && enabled)
830                         head_disable(ivi, head);
831                 else if (enabled && changed)
832                         weston_log("Detected a monitor change on head '%s', "
833                                    "not bothering to do anything about it.\n",
834                                    weston_head_get_name(head));
835
836                 weston_head_reset_device_changed(head);
837         }
838
839         wl_list_for_each(output, &ivi->outputs, link) {
840                 if (output->add_len == 0)
841                         continue;
842
843                 if (process_output(output) < 0) {
844                         output->add_len = 0;
845                         ivi->init_failed = true;
846                 }
847         }
848 }
849
850 #ifdef HAVE_REMOTING
851 static int
852 drm_backend_remoted_output_configure(struct weston_output *output,
853                                      struct weston_config_section *section,
854                                      char *modeline,
855                                      const struct weston_remoting_api *api)
856 {
857         char *gbm_format = NULL;
858         char *seat = NULL;
859         char *host = NULL;
860         char *pipeline = NULL;
861         int port, ret;
862         int32_t scale = 1;
863         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
864         char *trans;
865
866         ret = api->set_mode(output, modeline);
867         if (ret < 0) {
868                 weston_log("Cannot configure an output \"%s\" using "
869                                 "weston_remoting_api. Invalid mode\n",
870                                 output->name);
871                 return -1;
872         }
873
874         weston_config_section_get_int(section, "scale", &scale, 1);
875         weston_output_set_scale(output, scale);
876
877         weston_config_section_get_string(section, "transform", &trans, "normal");
878         if (parse_transform(trans, &transform) < 0) {
879                 weston_log("Invalid transform \"%s\" for output %s\n",
880                            trans, output->name);
881         }
882         weston_output_set_transform(output, transform);
883
884         weston_config_section_get_string(section, "gbm-format",
885                                          &gbm_format, NULL);
886         api->set_gbm_format(output, gbm_format);
887         free(gbm_format);
888
889         weston_config_section_get_string(section, "seat", &seat, "");
890
891         api->set_seat(output, seat);
892         free(seat);
893
894         weston_config_section_get_string(section, "gst-pipeline", &pipeline,
895                         NULL);
896         if (pipeline) {
897                 api->set_gst_pipeline(output, pipeline);
898                 free(pipeline);
899                 return 0;
900         }
901
902         weston_config_section_get_string(section, "host", &host, NULL);
903         weston_config_section_get_int(section, "port", &port, 0);
904         if (!host || port <= 0 || 65533 < port) {
905                 weston_log("Cannot configure an output \"%s\". "
906                                 "Need to specify gst-pipeline or "
907                                 "host and port (1-65533).\n", output->name);
908         }
909         api->set_host(output, host);
910         free(host);
911         api->set_port(output, port);
912
913         return 0;
914 }
915
916
917 static int
918 remote_output_init(struct ivi_output *ivi_output,
919                    struct weston_compositor *compositor,
920                    struct weston_config_section *section,
921                    const struct weston_remoting_api *api)
922 {
923         char *output_name, *modeline = NULL;
924         int ret = -1;
925
926         weston_config_section_get_string(section, "name", &output_name, NULL);
927         if (!output_name)
928                 return ret;
929
930         weston_config_section_get_string(section, "mode", &modeline, "off");
931         if (strcmp(modeline, "off") == 0)
932                 goto err;
933
934         ivi_output->output = api->create_output(compositor, output_name);
935         if (!ivi_output->output) {
936                 weston_log("Cannot create remoted output \"%s\".\n",
937                                 output_name);
938                 goto err;
939         }
940
941         ret = drm_backend_remoted_output_configure(ivi_output->output, section,
942                                                    modeline, api);
943         if (ret < 0) {
944                 weston_log("Cannot configure remoted output \"%s\".\n",
945                                 output_name);
946                 goto err;
947         }
948
949         if ((ret = weston_output_enable(ivi_output->output)) < 0) {
950                 weston_log("Enabling remoted output \"%s\" failed.\n",
951                                 output_name);
952                 goto err;
953         }
954
955         free(modeline);
956         free(output_name);
957         weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
958
959         return 0;
960
961 err:
962         free(modeline);
963         free(output_name);
964         if (ivi_output->output)
965                 weston_output_destroy(ivi_output->output);
966
967         return ret;
968 }
969
970 static void
971 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
972 {
973         struct weston_config_section *remote_section = NULL;
974         const char *section_name;
975         struct weston_config *config = ivi->config;
976
977         while (weston_config_next_section(config, &remote_section, &section_name)) {
978                 if (strcmp(section_name, "remote-output"))
979                         continue;
980
981                 struct ivi_output *ivi_output = NULL;
982                 bool output_found = false;
983                 char *_name = NULL;
984
985                 weston_config_section_get_string(remote_section,
986                                                  "name", &_name, NULL);
987                 wl_list_for_each(ivi_output, &ivi->outputs, link) {
988                         if (!strcmp(ivi_output->name, _name)) {
989                                 output_found = true;
990                                 break;
991                         }
992                 }
993
994                 if (output_found) {
995                         free(_name);
996                         continue;
997                 }
998
999                 ivi_output = zalloc(sizeof(*ivi_output));
1000                 if (!ivi_output) {
1001                         free(_name);
1002                         continue;
1003                 }
1004
1005                 ivi_output->ivi = ivi;
1006                 ivi_output->name = _name;
1007                 ivi_output->config = remote_section;
1008                 ivi_output->type = OUTPUT_REMOTE;
1009
1010                 if (remote_output_init(ivi_output, ivi->compositor,
1011                                        remote_section, ivi->remoting_api)) {
1012                         free(ivi_output->name);
1013                         free(ivi_output);
1014                         continue;
1015                 }
1016
1017                 ivi_output->output_destroy.notify = handle_output_destroy;
1018                 weston_output_add_destroy_listener(ivi_output->output,
1019                                                    &ivi_output->output_destroy);
1020
1021                 wl_list_insert(&ivi->outputs, &ivi_output->link);
1022                 ivi_output_configure_app_id(ivi_output);
1023         }
1024 }
1025
1026 static int
1027 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
1028 {
1029         struct weston_compositor *compositor = ivi->compositor;
1030         int (*module_init)(struct weston_compositor *wc);
1031
1032         module_init = weston_load_module("remoting-plugin.so",
1033                                          "weston_module_init",
1034                                          LIBWESTON_MODULEDIR);
1035         if (!module_init)
1036                 return -1;
1037
1038         if (module_init(compositor) < 0)
1039                 return -1;
1040
1041         ivi->remoting_api = weston_remoting_get_api(compositor);
1042         if (!ivi->remoting_api)
1043                 return -1;
1044         return 0;
1045 }
1046 #else
1047 static int
1048 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
1049 {
1050         return -1;
1051 }
1052 #endif
1053
1054 static int
1055 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[],
1056                  enum weston_renderer_type renderer)
1057 {
1058         struct weston_drm_backend_config config = {
1059                 .base = {
1060                         .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
1061                         .struct_size = sizeof config,
1062                 },
1063         };
1064         struct weston_config_section *section;
1065         int use_current_mode = 0;
1066         bool force_pixman = false;
1067         bool use_shadow;
1068         bool without_input = false;
1069
1070         const struct weston_option options[] = {
1071                 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
1072                 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
1073                 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
1074                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &force_pixman },
1075                 { WESTON_OPTION_BOOLEAN, "continue-without-input", false, &without_input }
1076         };
1077
1078         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1079
1080         if (force_pixman)
1081                 config.renderer = WESTON_RENDERER_PIXMAN;
1082         else
1083                 config.renderer = WESTON_RENDERER_AUTO;
1084
1085         ivi->cmdline.use_current_mode = use_current_mode;
1086
1087         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
1088         weston_config_section_get_string(section, "gbm-format",
1089                                          &config.gbm_format, NULL);
1090         weston_config_section_get_uint(section, "pageflip-timeout",
1091                                        &config.pageflip_timeout, 0);
1092         weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
1093         config.use_pixman_shadow = use_shadow;
1094
1095         if (without_input)
1096                 ivi->compositor->require_input = !without_input;
1097
1098         ivi->heads_changed.notify = heads_changed;
1099         weston_compositor_add_heads_changed_listener(ivi->compositor,
1100                                                      &ivi->heads_changed);
1101
1102         if (!weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
1103                                             &config.base))
1104                 return -1;
1105
1106         ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
1107         if (!ivi->drm_api) {
1108                 weston_log("Cannot use drm output api.\n");
1109                 goto error;
1110         }
1111
1112         load_remoting_plugin(ivi, ivi->config);
1113
1114         return 0;
1115
1116 error:
1117         free(config.gbm_format);
1118         free(config.seat_id);
1119         return -1;
1120 }
1121
1122 static void
1123 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
1124                               bool *force_pixman, bool *fullscreen, int *output_count)
1125 {
1126         struct weston_config_section *section;
1127         bool pixman;
1128         int fs = 0;
1129
1130         const struct weston_option options[] = {
1131                 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
1132                 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
1133                 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
1134                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &force_pixman },
1135                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
1136                 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
1137         };
1138
1139         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
1140         weston_config_section_get_bool(section, "use-pixman", &pixman, false);
1141
1142         *output_count = 1;
1143         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1144         *force_pixman = pixman;
1145         *fullscreen = fs;
1146 }
1147
1148 static int
1149 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
1150                         const char *match_prefix, const char *name_prefix)
1151 {
1152         struct weston_config_section *section = NULL;
1153         const char *section_name;
1154         char *default_output = NULL;
1155         int i = 0;
1156         size_t match_len = strlen(match_prefix);
1157
1158         while (weston_config_next_section(ivi->config, &section, &section_name)) {
1159                 char *output_name;
1160
1161                 if (i >= output_count)
1162                         break;
1163
1164                 if (strcmp(section_name, "output") != 0)
1165                         continue;
1166
1167                 weston_config_section_get_string(section, "name", &output_name, NULL);
1168                 if (output_name == NULL)
1169                         continue;
1170                 if (strncmp(output_name, match_prefix, match_len) != 0) {
1171                         free(output_name);
1172                         continue;
1173                 }
1174
1175                 if (ivi->window_api->create_head(ivi->backend, output_name) < 0) {
1176                         free(output_name);
1177                         return -1;
1178                 }
1179
1180                 free(output_name);
1181                 ++i;
1182         }
1183
1184         for (; i < output_count; ++i) {
1185                 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
1186                         return -1;
1187
1188                 if (ivi->window_api->create_head(ivi->backend, default_output) < 0) {
1189                         free(default_output);
1190                         return -1;
1191                 }
1192
1193                 free(default_output);
1194         }
1195
1196         return 0;
1197 }
1198
1199 static int
1200 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[],
1201                      enum weston_renderer_type renderer)
1202 {
1203         struct weston_wayland_backend_config config = {
1204                 .base = {
1205                         .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
1206                         .struct_size = sizeof config,
1207                 },
1208         };
1209         struct weston_config_section *section;
1210         int sprawl = 0;
1211         int output_count;
1212         bool force_pixman = false;
1213
1214         const struct weston_option options[] = {
1215                 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1216                 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1217         };
1218
1219         windowed_parse_common_options(ivi, argc, argv, &force_pixman,
1220                                       &config.fullscreen, &output_count);
1221
1222         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1223         config.sprawl = sprawl;
1224
1225         section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1226         weston_config_section_get_string(section, "cursor-theme",
1227                                          &config.cursor_theme, NULL);
1228         weston_config_section_get_int(section, "cursor-size",
1229                                       &config.cursor_size, 32);
1230
1231         ivi->backend = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1232                                                       &config.base);
1233
1234         free(config.cursor_theme);
1235         free(config.display_name);
1236
1237         if (!ivi->backend)
1238                 return -1;
1239
1240         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1241
1242         /*
1243          * We will just assume if load_backend() finished cleanly and
1244          * windowed_output_api is not present that wayland backend is started
1245          * with --sprawl or runs on fullscreen-shell. In this case, all values
1246          * are hardcoded, so nothing can be configured; simply create and
1247          * enable an output.
1248          */
1249         if (ivi->window_api == NULL)
1250                 return 0;
1251
1252         return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1253 }
1254
1255 #ifdef HAVE_BACKEND_X11
1256 static int
1257 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[],
1258                  enum weston_renderer_type renderer)
1259 {
1260         struct weston_x11_backend_config config = {
1261                 .base = {
1262                         .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1263                         .struct_size = sizeof config,
1264                 },
1265         };
1266         int no_input = 0;
1267         int output_count;
1268         int ret;
1269         bool force_pixman = false;
1270
1271         const struct weston_option options[] = {
1272                { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1273         };
1274
1275         windowed_parse_common_options(ivi, argc, argv, &force_pixman,
1276                                       &config.fullscreen, &output_count);
1277
1278         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1279         if (force_pixman)
1280                 config.renderer = WESTON_RENDERER_PIXMAN;
1281         else
1282                 config.renderer = WESTON_RENDERER_AUTO;
1283         config.no_input = no_input;
1284
1285         if (!weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11, &config.base))
1286                 return -1;
1287
1288         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1289         if (!ivi->window_api) {
1290                 weston_log("Cannot use weston_windowed_output_api.\n");
1291                 return -1;
1292         }
1293
1294         return windowed_create_outputs(ivi, output_count, "X", "screen");
1295 }
1296 #else
1297 static int
1298 load_x11_backend(struct ivi_compositor *ivi, int *argc, char **argv,
1299                  enum weston_renderer_type renderer)
1300 {
1301         return -1;
1302 }
1303 #endif
1304
1305 #ifdef HAVE_BACKEND_HEADLESS
1306 static int
1307 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv,
1308                       enum weston_renderer_type renderer)
1309 {
1310         struct weston_headless_backend_config config = {};
1311
1312         bool force_pixman = false;
1313         bool fullscreen;
1314         bool force_gl = false;
1315         int output_count;
1316
1317         struct weston_compositor *c = ivi->compositor;
1318         struct weston_backend *wb = NULL;
1319
1320         const struct weston_option options[] = {
1321                 { WESTON_OPTION_BOOLEAN, "use-pixman", false, &force_pixman },
1322                 { WESTON_OPTION_BOOLEAN, "use-gl", false, &force_gl },
1323         };
1324
1325         windowed_parse_common_options(ivi, argc, argv, &force_pixman,
1326                         &fullscreen, &output_count);
1327
1328         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1329
1330         if ((force_pixman && force_gl) ||
1331             (renderer != WESTON_RENDERER_AUTO && (force_pixman || force_gl))) {
1332                 weston_log("Conflicting renderer specifications\n");
1333                 return -1;
1334         } else if (force_pixman) {
1335                 config.renderer = WESTON_RENDERER_PIXMAN;
1336         } else if (force_gl) {
1337                 config.renderer = WESTON_RENDERER_GL;
1338         } else {
1339                 config.renderer = renderer;
1340         }
1341
1342         config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1343         config.base.struct_size = sizeof(struct weston_headless_backend_config);
1344
1345         /* load the actual headless-backend and configure it */
1346         if (!weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS, &config.base))
1347                 return -1;
1348
1349         ivi->window_api = weston_windowed_output_get_api(c);
1350         if (!ivi->window_api) {
1351                 weston_log("Cannot use weston_windowed_output_api.\n");
1352                 return -1;
1353         }
1354
1355         wb = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_HEADLESS, &config.base);
1356         if (!wb)
1357                 return -1;
1358
1359         if (ivi->window_api->create_head(wb, "headless") < 0) {
1360                 weston_log("Cannot create headless back-end\n");
1361                 return -1;
1362         }
1363
1364         return 0;
1365 }
1366 #else
1367 static int
1368 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv, enum weston_renderer_type renderer)
1369 {
1370         return -1;
1371 }
1372 #endif
1373
1374 #ifdef HAVE_BACKEND_RDP
1375 static void
1376 weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
1377 {
1378         config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
1379         config->base.struct_size = sizeof(struct weston_rdp_backend_config);
1380
1381         config->bind_address = NULL;
1382         config->port = 3389;
1383         config->rdp_key = NULL;
1384         config->server_cert = NULL;
1385         config->server_key = NULL;
1386         config->env_socket = 0;
1387         config->no_clients_resize = 1;
1388         config->force_no_compression = 0;
1389 }
1390
1391 static int
1392 rdp_backend_output_configure(struct weston_output *output)
1393 {
1394         struct ivi_compositor *ivi = to_ivi_compositor(output->compositor);
1395         struct ivi_output_config *parsed_options = ivi->parsed_options;
1396         const struct weston_rdp_output_api *api =
1397                 weston_rdp_output_get_api(output->compositor);
1398         int width = 640;
1399         int height = 480;
1400         struct weston_config_section *section;
1401         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
1402         char *transform_string;
1403         struct weston_mode new_mode = {};
1404
1405         assert(parsed_options);
1406
1407         if (!api) {
1408                 weston_log("Cannot use weston_rdp_output_api.\n");
1409                 return -1;
1410         }
1411
1412         section = weston_config_get_section(ivi->config, "rdp", NULL, NULL);
1413
1414         if (parsed_options->width)
1415                 width = parsed_options->width;
1416
1417         if (parsed_options->height)
1418                 height = parsed_options->height;
1419
1420         weston_output_set_scale(output, 1);
1421
1422         weston_config_section_get_int(section, "width",
1423                         &width, width);
1424
1425         weston_config_section_get_int(section, "height",
1426                         &height, height);
1427
1428         if (parsed_options->transform)
1429                 transform = parsed_options->transform;
1430
1431         weston_config_section_get_string(section, "transform",
1432                         &transform_string, "normal");
1433
1434         if (parse_transform(transform_string, &transform) < 0) {
1435                 weston_log("Invalid transform \"%s\" for output %s\n",
1436                            transform_string, output->name);
1437                 return -1;
1438         }
1439
1440
1441         new_mode.width = width;
1442         new_mode.height = height;
1443
1444         api->output_set_mode(output, &new_mode);
1445         weston_output_set_transform(output, transform);
1446
1447         return 0;
1448 }
1449
1450 static int
1451 load_rdp_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1452 {
1453         struct weston_rdp_backend_config config = {};
1454         struct weston_config_section *section;
1455
1456         struct ivi_output_config *parsed_options = ivi_init_parsed_options(ivi->compositor);
1457         if (!parsed_options)
1458                 return -1;
1459
1460         weston_rdp_backend_config_init(&config);
1461
1462         const struct weston_option rdp_options[] = {
1463                 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1464                 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1465                 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
1466                 { WESTON_OPTION_INTEGER, "transform", 0, &parsed_options->transform },
1467                 { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
1468                 { WESTON_OPTION_STRING,  "address", 0, &config.bind_address },
1469                 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1470                 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1471                 { WESTON_OPTION_STRING,  "rdp4-key", 0, &config.rdp_key },
1472                 { WESTON_OPTION_STRING,  "rdp-tls-cert", 0, &config.server_cert },
1473                 { WESTON_OPTION_STRING,  "rdp-tls-key", 0, &config.server_key },
1474                 { WESTON_OPTION_BOOLEAN, "force-no-compression", 0, &config.force_no_compression },
1475         };
1476
1477         section = weston_config_get_section(ivi->config, "rdp", NULL, NULL);
1478
1479         weston_config_section_get_string(section, "tls-cert",
1480                         &config.server_cert, config.server_cert);
1481
1482         weston_config_section_get_string(section, "tls-key",
1483                         &config.server_key, config.server_key);
1484
1485
1486         parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1487
1488         ivi->simple_output_configure = rdp_backend_output_configure;
1489         if (!weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_RDP, &config.base))
1490                 return -1;
1491
1492         free(config.bind_address);
1493         free(config.rdp_key);
1494         free(config.server_cert);
1495         free(config.server_key);
1496
1497         return 0;
1498 }
1499 #else
1500 static int
1501 load_rdp_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1502 {
1503         return -1;
1504 }
1505 #endif
1506
1507
1508 static int
1509 load_backend(struct ivi_compositor *ivi, int *argc, char **argv,
1510              const char *backend_name, const char *renderer_name)
1511 {
1512         enum weston_compositor_backend backend;
1513         enum weston_renderer_type renderer;
1514
1515         if (!get_backend_from_string(backend_name, &backend)) {
1516                 weston_log("Error: unknown backend \"%s\"\n", backend_name);
1517                 return -1;
1518         }
1519
1520         if (!get_renderer_from_string(renderer_name, &renderer)) {
1521                 weston_log("Error: unknown renderer \"%s\"\n", renderer_name);
1522                 return -1;
1523         }
1524
1525         switch (backend) {
1526         case WESTON_BACKEND_DRM:
1527                 return load_drm_backend(ivi, argc, argv, renderer);
1528         case WESTON_BACKEND_HEADLESS:
1529                 return load_headless_backend(ivi, argc, argv, renderer);
1530         case WESTON_BACKEND_RDP:
1531                 return load_rdp_backend(ivi, argc, argv);
1532         case WESTON_BACKEND_WAYLAND:
1533                 return load_wayland_backend(ivi, argc, argv, renderer);
1534         case WESTON_BACKEND_X11:
1535                 return load_x11_backend(ivi, argc, argv, renderer);
1536         default:
1537                 assert(!"unknown backend type in load_backend()");
1538         }
1539
1540         return 0;
1541 }
1542
1543 static int
1544 load_modules(struct ivi_compositor *ivi, const char *modules,
1545              int *argc, char *argv[], bool *xwayland)
1546 {
1547         const char *p, *end;
1548         char buffer[256];
1549         int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1550
1551         if (modules == NULL)
1552                 return 0;
1553
1554         p = modules;
1555         while (*p) {
1556                 end = strchrnul(p, ',');
1557                 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1558
1559                 if (strstr(buffer, "xwayland.so")) {
1560                         *xwayland = true;
1561                 } else if (strstr(buffer, "systemd-notify.so")) {
1562                         weston_log("systemd-notify plug-in already loaded!\n");
1563                 } else {
1564                         module_init = weston_load_module(buffer, "wet_module_init", WESTON_MODULEDIR);
1565                         if (!module_init)
1566                                 return -1;
1567
1568                         if (module_init(ivi->compositor, *argc, argv) < 0)
1569                                 return -1;
1570
1571                 }
1572
1573                 p = end;
1574                 while (*p == ',')
1575                         p++;
1576         }
1577
1578         return 0;
1579 }
1580
1581
1582 static char *
1583 choose_default_backend(void)
1584 {
1585         char *backend = NULL;
1586
1587         if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1588                 backend = strdup("wayland-backend.so");
1589         else if (getenv("DISPLAY"))
1590                 backend = strdup("x11-backend.so");
1591         else
1592                 backend = strdup("drm-backend.so");
1593
1594         return backend;
1595 }
1596
1597 static int
1598 compositor_init_config(struct ivi_compositor *ivi)
1599 {
1600         struct xkb_rule_names xkb_names;
1601         struct weston_config_section *section;
1602         struct weston_compositor *compositor = ivi->compositor;
1603         struct weston_config *config = ivi->config;
1604         int repaint_msec;
1605         bool vt_switching;
1606         bool require_input;
1607
1608         /* agl-compositor.ini [keyboard] */
1609         section = weston_config_get_section(config, "keyboard", NULL, NULL);
1610         weston_config_section_get_string(section, "keymap_rules",
1611                                          (char **) &xkb_names.rules, NULL);
1612         weston_config_section_get_string(section, "keymap_model",
1613                                          (char **) &xkb_names.model, NULL);
1614         weston_config_section_get_string(section, "keymap_layout",
1615                                          (char **) &xkb_names.layout, NULL);
1616         weston_config_section_get_string(section, "keymap_variant",
1617                                          (char **) &xkb_names.variant, NULL);
1618         weston_config_section_get_string(section, "keymap_options",
1619                                          (char **) &xkb_names.options, NULL);
1620
1621         if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1622                 return -1;
1623
1624         weston_config_section_get_int(section, "repeat-rate",
1625                                       &compositor->kb_repeat_rate, 40);
1626         weston_config_section_get_int(section, "repeat-delay",
1627                                       &compositor->kb_repeat_delay, 400);
1628
1629         weston_config_section_get_bool(section, "vt-switching",
1630                                        &vt_switching, false);
1631         compositor->vt_switching = vt_switching;
1632
1633         /* agl-compositor.ini [core] */
1634         section = weston_config_get_section(config, "core", NULL, NULL);
1635
1636         weston_config_section_get_bool(section, "disable-cursor",
1637                                        &ivi->disable_cursor, false);
1638         weston_config_section_get_bool(section, "activate-by-default",
1639                                        &ivi->activate_by_default, true);
1640
1641         weston_config_section_get_bool(section, "require-input", &require_input, true);
1642         compositor->require_input = require_input;
1643
1644         weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1645                                       compositor->repaint_msec);
1646         if (repaint_msec < -10 || repaint_msec > 1000) {
1647                 weston_log("Invalid repaint_window value in config: %d\n",
1648                            repaint_msec);
1649         } else {
1650                 compositor->repaint_msec = repaint_msec;
1651         }
1652         weston_log("Output repaint window is %d ms maximum.\n",
1653                    compositor->repaint_msec);
1654
1655         return 0;
1656 }
1657
1658 struct ivi_surface *
1659 to_ivi_surface(struct weston_surface *surface)
1660 {
1661         struct weston_desktop_surface *dsurface;
1662
1663         dsurface = weston_surface_get_desktop_surface(surface);
1664         if (!dsurface)
1665                 return NULL;
1666
1667         return weston_desktop_surface_get_user_data(dsurface);
1668 }
1669
1670 static void
1671 activate_binding(struct weston_seat *seat,
1672                  struct weston_view *focus_view, uint32_t flags)
1673 {
1674         struct weston_surface *focus_surface;
1675         struct weston_surface *main_surface;
1676         struct ivi_surface *ivi_surface;
1677         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(seat);
1678
1679         if (!focus_view)
1680                 return;
1681
1682         focus_surface = focus_view->surface;
1683         main_surface = weston_surface_get_main_surface(focus_surface);
1684
1685         ivi_surface = to_ivi_surface(main_surface);
1686         if (!ivi_surface)
1687                 return;
1688
1689         if (ivi_seat)
1690                 ivi_shell_activate_surface(ivi_surface, ivi_seat, flags);
1691 }
1692
1693 static void
1694 click_to_activate_binding(struct weston_pointer *pointer,
1695                           const struct timespec *time,
1696                           uint32_t button, void *data)
1697 {
1698         if (pointer->grab != &pointer->default_grab)
1699                 return;
1700         if (pointer->focus == NULL)
1701                 return;
1702
1703         activate_binding(pointer->seat, pointer->focus,
1704                          WESTON_ACTIVATE_FLAG_CLICKED);
1705 }
1706
1707 static void
1708 touch_to_activate_binding(struct weston_touch *touch,
1709                           const struct timespec *time,
1710                           void *data)
1711 {
1712         if (touch->grab != &touch->default_grab)
1713                 return;
1714         if (touch->focus == NULL)
1715                 return;
1716
1717         activate_binding(touch->seat, touch->focus,
1718                          WESTON_ACTIVATE_FLAG_NONE);
1719 }
1720
1721 static void
1722 add_bindings(struct weston_compositor *compositor)
1723 {
1724         weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1725                                              click_to_activate_binding,
1726                                              NULL);
1727         weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1728                                              click_to_activate_binding,
1729                                              NULL);
1730         weston_compositor_add_touch_binding(compositor, 0,
1731                                             touch_to_activate_binding,
1732                                             NULL);
1733 }
1734
1735 static int
1736 create_listening_socket(struct wl_display *display, const char *socket_name)
1737 {
1738         if (socket_name) {
1739                 if (wl_display_add_socket(display, socket_name)) {
1740                         weston_log("fatal: failed to add socket: %s\n",
1741                                    strerror(errno));
1742                         return -1;
1743                 }
1744         } else {
1745                 socket_name = wl_display_add_socket_auto(display);
1746                 if (!socket_name) {
1747                         weston_log("fatal: failed to add socket: %s\n",
1748                                    strerror(errno));
1749                         return -1;
1750                 }
1751         }
1752
1753         setenv("WAYLAND_DISPLAY", socket_name, 1);
1754
1755         return 0;
1756 }
1757
1758 static bool
1759 global_filter(const struct wl_client *client, const struct wl_global *global,
1760               void *data)
1761 {
1762         return true;
1763 }
1764
1765 static int
1766 load_config(struct weston_config **config, bool no_config,
1767             const char *config_file)
1768 {
1769         const char *file = "agl-compositor.ini";
1770         const char *full_path;
1771
1772         if (config_file)
1773                 file = config_file;
1774
1775         if (!no_config)
1776                 *config = weston_config_parse(file);
1777
1778         if (*config) {
1779                 full_path = weston_config_get_full_path(*config);
1780
1781                 weston_log("Using config file '%s'.\n", full_path);
1782                 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1783
1784                 return 0;
1785         }
1786
1787         if (config_file && !no_config) {
1788                 weston_log("fatal: error opening or reading config file '%s'.\n",
1789                         config_file);
1790
1791                 return -1;
1792         }
1793
1794         weston_log("Starting with no config file.\n");
1795         setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1796
1797         return 0;
1798 }
1799
1800 static FILE *logfile;
1801
1802 static char *
1803 log_timestamp(char *buf, size_t len)
1804 {
1805         struct timeval tv;
1806         struct tm *brokendown_time;
1807         char datestr[128];
1808         char timestr[128];
1809
1810         gettimeofday(&tv, NULL);
1811
1812         brokendown_time = localtime(&tv.tv_sec);
1813         if (brokendown_time == NULL) {
1814                 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1815                 return buf;
1816         }
1817
1818         memset(datestr, 0, sizeof(datestr));
1819         if (brokendown_time->tm_mday != cached_tm_mday) {
1820                 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1821                                 brokendown_time);
1822                 cached_tm_mday = brokendown_time->tm_mday;
1823         }
1824
1825         strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1826         /* if datestr is empty it prints only timestr*/
1827         snprintf(buf, len, "%s[%s.%03li]", datestr,
1828                         timestr, (tv.tv_usec / 1000));
1829
1830         return buf;
1831 }
1832
1833 static void
1834 custom_handler(const char *fmt, va_list arg)
1835 {
1836         char timestr[512];
1837
1838         weston_log_scope_printf(log_scope, "%s libwayland: ",
1839                         log_timestamp(timestr, sizeof(timestr)));
1840         weston_log_scope_vprintf(log_scope, fmt, arg);
1841 }
1842
1843 static void
1844 log_file_open(const char *filename)
1845 {
1846         wl_log_set_handler_server(custom_handler);
1847
1848         if (filename)
1849                 logfile = fopen(filename, "a");
1850
1851         if (!logfile) {
1852                 logfile = stderr;
1853         } else {
1854                 os_fd_set_cloexec(fileno(logfile));
1855                 setvbuf(logfile, NULL, _IOLBF, 256);
1856         }
1857 }
1858
1859 static void
1860 log_file_close(void)
1861 {
1862         if (logfile && logfile != stderr)
1863                 fclose(logfile);
1864         logfile = stderr;
1865 }
1866
1867 static int
1868 vlog(const char *fmt, va_list ap)
1869 {
1870         const char *oom = "Out of memory";
1871         char timestr[128];
1872         int len = 0;
1873         char *str;
1874
1875         if (weston_log_scope_is_enabled(log_scope)) {
1876                 int len_va;
1877                 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1878                 len_va = vasprintf(&str, fmt, ap);
1879                 if (len_va >= 0) {
1880                         len = weston_log_scope_printf(log_scope, "%s %s",
1881                                         xlog_timestamp, str);
1882                         free(str);
1883                 } else {
1884                         len = weston_log_scope_printf(log_scope, "%s %s",
1885                                         xlog_timestamp, oom);
1886                 }
1887         }
1888
1889         return len;
1890 }
1891
1892
1893 static int
1894 vlog_continue(const char *fmt, va_list ap)
1895 {
1896         return weston_log_scope_vprintf(log_scope, fmt, ap);
1897 }
1898
1899 static int
1900 on_term_signal(int signo, void *data)
1901 {
1902         struct wl_display *display = data;
1903
1904         weston_log("caught signal %d\n", signo);
1905         wl_display_terminate(display);
1906
1907         return 1;
1908 }
1909
1910 static void
1911 handle_exit(struct weston_compositor *compositor)
1912 {
1913         wl_display_terminate(compositor->wl_display);
1914 }
1915
1916 static void
1917 usage(int error_code)
1918 {
1919         FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1920         fprintf(out,
1921                 "Usage: agl-compositor [OPTIONS]\n"
1922                 "\n"
1923                 "This is " PACKAGE_STRING ", the reference compositor for\n"
1924                 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1925                 "backends,\nand depending on which backend is in use different "
1926                 "options will be accepted.\n"
1927                 "\n"
1928                 "Core options:\n"
1929                 "\n"
1930                 "  --version\t\tPrint agl-compositor version\n"
1931                 "  -B, --backend=MODULE\tBackend module, one of\n"
1932                         "\t\t\t\tdrm-backend.so\n"
1933                         "\t\t\t\twayland-backend.so\n"
1934                         "\t\t\t\tx11-backend.so\n"
1935                         "\t\t\t\theadless-backend.so\n"
1936                 "  -r, --renderer=NAME\tName of renderer to use: auto, gl, noop, pixman\n"
1937                 "  -S, --socket=NAME\tName of socket to listen on\n"
1938                 "  --log=FILE\t\tLog to the given file\n"
1939                 "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1940                 "  --no-config\t\tDo not read agl-compositor.ini\n"
1941                 "  --debug\t\tEnable debug extension(s)\n"
1942                 "  -h, --help\t\tThis help message\n"
1943                 "\n");
1944         exit(error_code);
1945 }
1946
1947 static char *
1948 copy_command_line(int argc, char * const argv[])
1949 {
1950         FILE *fp;
1951         char *str = NULL;
1952         size_t size = 0;
1953         int i;
1954
1955         fp = open_memstream(&str, &size);
1956         if (!fp)
1957                 return NULL;
1958
1959         fprintf(fp, "%s", argv[0]);
1960         for (i = 1; i < argc; i++)
1961                 fprintf(fp, " %s", argv[i]);
1962         fclose(fp);
1963
1964         return str;
1965 }
1966
1967 #if !defined(BUILD_XWAYLAND)
1968 void *
1969 wet_load_xwayland(struct weston_compositor *comp)
1970 {
1971         weston_log("Attempted to load xwayland library but compositor "
1972                    "was *not* built with xwayland support!\n");
1973         return NULL;
1974 }
1975 #endif
1976
1977 static void
1978 weston_log_setup_scopes(struct weston_log_context *log_ctx,
1979                         struct weston_log_subscriber *subscriber,
1980                         const char *names)
1981 {
1982         assert(log_ctx);
1983         assert(subscriber);
1984
1985         char *tokenize = strdup(names);
1986         char *token = strtok(tokenize, ",");
1987         while (token) {
1988                 weston_log_subscribe(log_ctx, subscriber, token);
1989                 token = strtok(NULL, ",");
1990         }
1991         free(tokenize);
1992 }
1993
1994 static void
1995 weston_log_subscribe_to_scopes(struct weston_log_context *log_ctx,
1996                                struct weston_log_subscriber *logger,
1997                                const char *debug_scopes)
1998 {
1999         if (logger && debug_scopes)
2000                 weston_log_setup_scopes(log_ctx, logger, debug_scopes);
2001         else
2002                 weston_log_subscribe(log_ctx, logger, "log");
2003 }
2004
2005 WL_EXPORT
2006 int wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
2007 {
2008         struct ivi_compositor ivi = { 0 };
2009         char *cmdline;
2010         struct wl_display *display = NULL;
2011         struct wl_event_loop *loop;
2012         struct wl_event_source *signals[3] = { 0 };
2013         struct weston_config_section *section;
2014         /* Command line options */
2015         char *backend = NULL;
2016         char *socket_name = NULL;
2017         char *log = NULL;
2018         char *modules = NULL;
2019         char *option_modules = NULL;
2020         char *debug_scopes = NULL;
2021         int help = 0;
2022         int version = 0;
2023         int no_config = 0;
2024         int debug = 0;
2025         bool list_debug_scopes = false;
2026         char *config_file = NULL;
2027         struct weston_log_context *log_ctx = NULL;
2028         struct weston_log_subscriber *logger;
2029         int ret = EXIT_FAILURE;
2030         bool xwayland = false;
2031         struct sigaction action;
2032         char *renderer = NULL;
2033
2034         const struct weston_option core_options[] = {
2035                 { WESTON_OPTION_STRING, "renderer", 'r', &renderer },
2036                 { WESTON_OPTION_STRING, "backend", 'B', &backend },
2037                 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
2038                 { WESTON_OPTION_STRING, "log", 0, &log },
2039                 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
2040                 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
2041                 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
2042                 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
2043                 { WESTON_OPTION_STRING, "config", 'c', &config_file },
2044                 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
2045                 { WESTON_OPTION_STRING, "debug-scopes", 'l', &debug_scopes },
2046                 { WESTON_OPTION_STRING, "list-debug-scopes", 'L', &list_debug_scopes },
2047         };
2048
2049         wl_list_init(&ivi.outputs);
2050         wl_list_init(&ivi.saved_outputs);
2051         wl_list_init(&ivi.surfaces);
2052         wl_list_init(&ivi.pending_surfaces);
2053         wl_list_init(&ivi.popup_pending_apps);
2054         wl_list_init(&ivi.fullscreen_pending_apps);
2055         wl_list_init(&ivi.split_pending_apps);
2056         wl_list_init(&ivi.remote_pending_apps);
2057         wl_list_init(&ivi.desktop_clients);
2058         wl_list_init(&ivi.child_process_list);
2059         wl_list_init(&ivi.pending_apps);
2060
2061         /* Prevent any clients we spawn getting our stdin */
2062         os_fd_set_cloexec(STDIN_FILENO);
2063
2064         cmdline = copy_command_line(argc, argv);
2065         parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
2066
2067         if (help)
2068                 usage(EXIT_SUCCESS);
2069
2070         if (version) {
2071                 printf(PACKAGE_STRING "\n");
2072                 ret = EXIT_SUCCESS;
2073                 goto exit_signals;
2074         }
2075
2076         log_ctx = weston_log_ctx_create();
2077         if (!log_ctx) {
2078                 fprintf(stderr, "Failed to initialize weston debug framework.\n");
2079                 goto exit_signals;
2080         }
2081
2082         log_scope = weston_log_ctx_add_log_scope(log_ctx, "log",
2083                                                  "agl-compositor log\n",
2084                                                  NULL, NULL, NULL);
2085
2086         log_file_open(log);
2087         weston_log_set_handler(vlog, vlog_continue);
2088
2089         logger = weston_log_subscriber_create_log(logfile);
2090         weston_log_subscribe_to_scopes(log_ctx, logger, debug_scopes);
2091
2092         weston_log("Command line: %s\n", cmdline);
2093         free(cmdline);
2094
2095         if (load_config(&ivi.config, no_config, config_file) < 0)
2096                 goto error_signals;
2097         section = weston_config_get_section(ivi.config, "core", NULL, NULL);
2098         if (!backend) {
2099                 weston_config_section_get_string(section, "backend", &backend,
2100                                                  NULL);
2101                 if (!backend)
2102                         backend = choose_default_backend();
2103         }
2104
2105         display = wl_display_create();
2106         loop = wl_display_get_event_loop(display);
2107
2108         wl_display_set_global_filter(display,
2109                                      global_filter, &ivi);
2110
2111         /* Register signal handlers so we shut down cleanly */
2112
2113         signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2114                                               display);
2115         signals[1] = wl_event_loop_add_signal(loop, SIGUSR2, on_term_signal,
2116                                               display);
2117         signals[2] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2118                                               display);
2119
2120         /* When debugging the compositor, if use wl_event_loop_add_signal() to
2121          * catch SIGINT, the debugger can't catch it, and attempting to stop
2122          * the compositor from within the debugger results in weston exiting
2123          * cleanly.
2124          *
2125          * Instead, use the sigaction() function, which sets up the signal in a
2126          * way that gdb can successfully catch, but have the handler for SIGINT
2127          * send SIGUSR2 (xwayland uses SIGUSR1), which we catch via
2128          * wl_event_loop_add_signal().
2129          */
2130         action.sa_handler = sigint_helper;
2131         sigemptyset(&action.sa_mask);
2132         action.sa_flags = 0;
2133         sigaction(SIGINT, &action, NULL);
2134
2135         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
2136                 if (!signals[i])
2137                         goto error_signals;
2138
2139         ivi.compositor = weston_compositor_create(display, log_ctx, &ivi, test_data);
2140         if (!ivi.compositor) {
2141                 weston_log("fatal: failed to create compositor.\n");
2142                 goto error_signals;
2143         }
2144
2145         if (compositor_init_config(&ivi) < 0)
2146                 goto error_compositor;
2147
2148         if (load_backend(&ivi, &argc, argv, backend, renderer) < 0) {
2149                 weston_log("fatal: failed to create compositor backend.\n");
2150                 goto error_compositor;
2151         }
2152
2153         if (weston_compositor_backends_loaded(ivi.compositor) < 0)
2154                 goto error_compositor;
2155
2156         weston_compositor_flush_heads_changed(ivi.compositor);
2157
2158         if (ivi_desktop_init(&ivi) < 0)
2159                 goto error_compositor;
2160
2161         ivi_seat_init(&ivi);
2162
2163         /* load additional modules */
2164         weston_config_section_get_string(section, "modules", &modules, "");
2165         if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
2166                 goto error_compositor;
2167
2168         if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
2169                 goto error_compositor;
2170
2171         if (!xwayland) {
2172                 weston_config_section_get_bool(section, "xwayland", &xwayland,
2173                                                false);
2174         }
2175
2176         if (ivi_shell_init(&ivi) < 0)
2177                 goto error_compositor;
2178
2179         if (xwayland) {
2180                 if (!wet_load_xwayland(ivi.compositor))
2181                         goto error_compositor;
2182         }
2183
2184         if (ivi_policy_init(&ivi) < 0)
2185                 goto error_compositor;
2186
2187
2188         if (list_debug_scopes) {
2189                 struct weston_log_scope *nscope = NULL;
2190
2191                 weston_log("Printing available debug scopes:\n");
2192
2193                 while ((nscope = weston_log_scopes_iterate(log_ctx, nscope))) {
2194                         weston_log("\tscope name: %s, desc: %s",
2195                                         weston_log_scope_get_name(nscope),
2196                                         weston_log_scope_get_description(nscope));
2197                 }
2198
2199                 weston_log("\n");
2200
2201                 goto error_compositor;
2202         }
2203
2204         add_bindings(ivi.compositor);
2205
2206         if (ivi.remoting_api)
2207                 ivi_enable_remote_outputs(&ivi);
2208
2209         if (create_listening_socket(display, socket_name) < 0)
2210                 goto error_compositor;
2211
2212         ivi_shell_init_black_fs(&ivi);
2213
2214         ivi.compositor->exit = handle_exit;
2215
2216         weston_compositor_wake(ivi.compositor);
2217
2218         ivi_shell_create_global(&ivi);
2219
2220         ivi_launch_shell_client(&ivi, "shell-client",
2221                                 &ivi.shell_client.client);
2222         ivi_launch_shell_client(&ivi, "shell-client-ext",
2223                                 &ivi.shell_client_ext.client);
2224
2225         if (debug)
2226                 ivi_screenshooter_create(&ivi);
2227         ivi_agl_systemd_notify(&ivi);
2228
2229         wl_display_run(display);
2230
2231         ret = ivi.compositor->exit_code;
2232
2233         wl_display_destroy_clients(display);
2234
2235 error_compositor:
2236         free(backend);
2237         backend = NULL;
2238         free(modules);
2239         modules = NULL;
2240
2241         weston_compositor_destroy(ivi.compositor);
2242
2243         weston_log_scope_destroy(log_scope);
2244         log_scope = NULL;
2245
2246         weston_log_subscriber_destroy(logger);
2247         weston_log_ctx_destroy(log_ctx);
2248
2249         ivi_policy_destroy(ivi.policy);
2250
2251 error_signals:
2252         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
2253                 if (signals[i])
2254                         wl_event_source_remove(signals[i]);
2255
2256         wl_display_destroy(display);
2257
2258         log_file_close();
2259         if (ivi.config)
2260                 weston_config_destroy(ivi.config);
2261
2262 exit_signals:
2263         free(log);
2264         free(config_file);
2265         free(socket_name);
2266         free(option_modules);
2267         return ret;
2268 }