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