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