compositor: Fix RDP loading code
[src/agl-compositor.git] / src / compositor.c
1 /*
2  * Copyright © 2012-2024 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <linux/input.h>
39
40 #include <libweston/backend-drm.h>
41 #include <libweston/backend-wayland.h>
42 #ifdef HAVE_BACKEND_RDP
43 #include <libweston/backend-rdp.h>
44 #endif
45 #ifdef HAVE_BACKEND_X11
46 #include <libweston/backend-x11.h>
47 #endif
48 #include <libweston/libweston.h>
49 #include <libweston/windowed-output-api.h>
50 #include <libweston/config-parser.h>
51 #include <libweston/weston-log.h>
52 #include <weston.h>
53
54 #include "shared/os-compatibility.h"
55 #include "shared/helpers.h"
56
57 #include "config.h"
58 #include "agl-shell-server-protocol.h"
59
60 #ifdef HAVE_REMOTING
61 #include "remote.h"
62 #endif
63
64 #define WINDOWED_DEFAULT_WIDTH 1024
65 #define WINDOWED_DEFAULT_HEIGHT 768
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 static void
97 sigint_helper(int sig)
98 {
99        raise(SIGUSR2);
100 }
101
102 struct {
103         char *name;
104         enum weston_renderer_type renderer;
105 } renderer_name_map[] = {
106         { "auto", WESTON_RENDERER_AUTO },
107         { "gl", WESTON_RENDERER_GL },
108         { "noop", WESTON_RENDERER_NOOP },
109         { "pixman", WESTON_RENDERER_PIXMAN },
110 };
111
112 struct {
113         char *short_name;
114         char *long_name;
115         enum weston_compositor_backend backend;
116 } backend_name_map[] = {
117         { "drm", "drm-backend.so", WESTON_BACKEND_DRM },
118         { "rdp", "rdp-backend.so", WESTON_BACKEND_RDP },
119         { "wayland", "wayland-backend.so", WESTON_BACKEND_WAYLAND },
120         { "x11", "x11-backend.so", WESTON_BACKEND_X11 },
121 };
122
123 bool
124 get_backend_from_string(const char *name, enum weston_compositor_backend *backend)
125 {
126         size_t i;
127
128         for (i = 0; i < ARRAY_LENGTH(backend_name_map); i++) {
129                 if (strcmp(name, backend_name_map[i].short_name) == 0 ||
130                     strcmp(name, backend_name_map[i].long_name) == 0) {
131                         *backend = backend_name_map[i].backend;
132                         return true;
133                 }
134         }
135
136         return false;
137 }
138
139 bool
140 get_renderer_from_string(const char *name, enum weston_renderer_type *renderer)
141 {
142         size_t i;
143
144         if (!name)
145                 name = "auto";
146
147         for (i = 0; i < ARRAY_LENGTH(renderer_name_map); i++) {
148                 if (strcmp(name, renderer_name_map[i].name) == 0) {
149                         *renderer = renderer_name_map[i].renderer;
150                         return true;
151                 }
152         }
153
154         return false;
155 }
156
157 static void
158 handle_output_destroy(struct wl_listener *listener, void *data)
159 {
160         struct ivi_output *output;
161
162         output = wl_container_of(listener, output, output_destroy);
163         assert(output->output == data);
164
165         if (output->fullscreen_view.fs &&
166             output->fullscreen_view.fs->view) {
167                 weston_surface_unref(output->fullscreen_view.fs->view->surface);
168                 weston_buffer_destroy_solid(output->fullscreen_view.buffer_ref);
169                 output->fullscreen_view.fs->view = NULL;
170         }
171
172         ivi_layout_save(output->ivi, output);
173
174         output->output = NULL;
175         wl_list_remove(&output->output_destroy.link);
176 }
177
178 struct ivi_output *
179 to_ivi_output(struct weston_output *o)
180 {
181         struct wl_listener *listener;
182         struct ivi_output *output;
183
184         listener = weston_output_get_destroy_listener(o, handle_output_destroy);
185         if (!listener)
186                 return NULL;
187
188         output = wl_container_of(listener, output, output_destroy);
189
190         return output;
191 }
192
193 static void
194 ivi_output_configure_app_id(struct ivi_output *ivi_output)
195 {
196         if (ivi_output->config) {
197                 if (ivi_output->app_ids != NULL)
198                         return;
199
200                 weston_config_section_get_string(ivi_output->config,
201                                                  "agl-shell-app-id",
202                                                  &ivi_output->app_ids,
203                                                  NULL);
204
205                 if (ivi_output->app_ids == NULL)
206                         return;
207
208                 weston_log("Will place app_id %s on output %s\n",
209                                 ivi_output->app_ids, ivi_output->name);
210         }
211 }
212
213 static struct ivi_output *
214 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
215                   struct weston_config_section *config,
216                   struct weston_head *head)
217 {
218         struct ivi_output *output = NULL;
219         wl_list_for_each(output, &ivi->outputs, link) {
220                 if (strcmp(output->name, name) == 0) {
221                         free(name);
222                         return output;
223                 }
224         }
225
226         output = zalloc(sizeof *output);
227         if (!output) {
228                 free(name);
229                 return NULL;
230         }
231
232         output->ivi = ivi;
233         output->name = name;
234         output->config = config;
235
236         output->output =
237                 weston_compositor_create_output(ivi->compositor, head, head->name);
238         if (!output->output) {
239                 free(output->name);
240                 free(output);
241                 return NULL;
242         }
243
244         /* simple_output_configure might assume we have an ivi_output created
245          * by this point, which we do but we can only link it to a
246          * weston_output through the destroy listener, so install it earlier
247          * before actually running the callback handler */
248         output->output_destroy.notify = handle_output_destroy;
249         weston_output_add_destroy_listener(output->output,
250                                            &output->output_destroy);
251
252         if (ivi->simple_output_configure) {
253                 int ret = ivi->simple_output_configure(output->output);
254                 if (ret < 0) {
255                         weston_log("Configuring output \"%s\" failed.\n",
256                                         weston_head_get_name(head));
257                         weston_output_destroy(output->output);
258                         ivi->init_failed = true;
259                         return NULL;
260                 }
261
262                 if (weston_output_enable(output->output) < 0) {
263                         weston_log("Enabling output \"%s\" failed.\n",
264                                         weston_head_get_name(head));
265                         weston_output_destroy(output->output);
266                         ivi->init_failed = true;
267                         return NULL;
268                 }
269         }
270
271
272         wl_list_insert(&ivi->outputs, &output->link);
273         ivi_output_configure_app_id(output);
274
275         return output;
276 }
277
278 static int
279 count_heads(struct weston_output *output)
280 {
281         struct weston_head *iter = NULL;
282         int n = 0;
283
284         while ((iter = weston_output_iterate_heads(output, iter)))
285                 ++n;
286
287         return n;
288 }
289
290 static void
291 handle_head_destroy(struct wl_listener *listener, void *data)
292 {
293         struct weston_head *head = data;
294         struct weston_output *output;
295
296         wl_list_remove(&listener->link);
297         free(listener);
298
299         output = weston_head_get_output(head);
300
301         /* On shutdown path, the output might be already gone. */
302         if (!output)
303                 return;
304
305         /* We're the last head */
306         if (count_heads(output) <= 1)
307                 weston_output_destroy(output);
308 }
309
310 static void
311 add_head_destroyed_listener(struct weston_head *head)
312 {
313         /* We already have a destroy listener */
314         if (weston_head_get_destroy_listener(head, handle_head_destroy))
315                 return;
316
317         struct wl_listener *listener = zalloc(sizeof *listener);
318         if (!listener)
319                 return;
320
321         listener->notify = handle_head_destroy;
322         weston_head_add_destroy_listener(head, listener);
323 }
324
325 static int
326 ivi_configure_windowed_output_from_config(struct ivi_output *output,
327                                           struct ivi_output_config *defaults)
328 {
329         struct ivi_compositor *ivi = output->ivi;
330         struct weston_config_section *section = output->config;
331         int width;
332         int height;
333         int scale;
334
335         if (section) {
336                 char *mode;
337
338                 weston_config_section_get_string(section, "mode", &mode, NULL);
339                 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
340                         weston_log("Invalid mode for output %s. Using defaults.\n",
341                                    output->name);
342                         width = defaults->width;
343                         height = defaults->height;
344                 }
345                 free(mode);
346         } else {
347                 width = defaults->width;
348                 height = defaults->height;
349         }
350
351         scale = defaults->scale;
352
353         if (ivi->cmdline.width)
354                 width = ivi->cmdline.width;
355         if (ivi->cmdline.height)
356                 height = ivi->cmdline.height;
357         if (ivi->cmdline.scale)
358                 scale = ivi->cmdline.scale;
359
360         weston_output_set_scale(output->output, scale);
361         weston_output_set_transform(output->output, defaults->transform);
362
363         if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
364                 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
365                            output->name);
366                 return -1;
367         }
368
369
370         weston_log("Configured windowed_output_api to %dx%d, scale %d\n",
371                    width, height, scale);
372
373         return 0;
374 }
375
376 static int
377 parse_transform(const char *transform, uint32_t *out)
378 {
379         static const struct { const char *name; uint32_t token; } transforms[] = {
380                 { "normal",             WL_OUTPUT_TRANSFORM_NORMAL },
381                 { "rotate-90",          WL_OUTPUT_TRANSFORM_90 },
382                 { "rotate-180",         WL_OUTPUT_TRANSFORM_180 },
383                 { "rotate-270",         WL_OUTPUT_TRANSFORM_270 },
384                 { "flipped",            WL_OUTPUT_TRANSFORM_FLIPPED },
385                 { "flipped-rotate-90",  WL_OUTPUT_TRANSFORM_FLIPPED_90 },
386                 { "flipped-rotate-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
387                 { "flipped-rotate-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
388         };
389
390         for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
391                 if (strcmp(transforms[i].name, transform) == 0) {
392                         *out = transforms[i].token;
393                         return 0;
394                 }
395
396         *out = WL_OUTPUT_TRANSFORM_NORMAL;
397         return -1;
398 }
399
400 int
401 parse_activation_area(const char *geometry, struct ivi_output *output)
402 {
403         int n;
404         unsigned width, height, x, y;
405
406         n = sscanf(geometry, "%ux%u+%u,%u", &width, &height, &x, &y);
407         if (n != 4) {
408                 return -1;
409         }
410         output->area_activation.width = width;
411         output->area_activation.height = height;
412         output->area_activation.x = x;
413         output->area_activation.y = y;
414         return 0;
415 }
416
417 static int
418 drm_configure_output(struct ivi_output *output)
419 {
420         struct ivi_compositor *ivi = output->ivi;
421         struct weston_config_section *section = output->config;
422         int32_t scale = 1;
423         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
424         enum weston_drm_backend_output_mode mode =
425                 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
426         char *modeline = NULL;
427         char *gbm_format = NULL;
428         char *seat = NULL;
429
430         if (section) {
431                 char *t;
432
433                 weston_config_section_get_int(section, "scale", &scale, 1);
434                 weston_config_section_get_string(section, "transform", &t, "normal");
435                 if (parse_transform(t, &transform) < 0)
436                         weston_log("Invalid transform \"%s\" for output %s\n",
437                                    t, output->name);
438                 weston_config_section_get_string(section, "activation-area", &t, "");
439                 if (parse_activation_area(t, output) < 0)
440                         weston_log("Invalid activation-area \"%s\" for output %s\n",
441                                    t, output->name);
442                 free(t);
443         }
444
445         weston_output_set_scale(output->output, scale);
446         weston_output_set_transform(output->output, transform);
447
448         if (section) {
449                 char *m;
450                 weston_config_section_get_string(section, "mode", &m, "preferred");
451
452                 /* This should have been handled earlier */
453                 assert(strcmp(m, "off") != 0);
454
455                 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
456                         mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
457                 } else if (strcmp(m, "preferred") != 0) {
458                         modeline = m;
459                         m = NULL;
460                 }
461                 free(m);
462
463                 weston_config_section_get_string(section, "gbm-format",
464                                                  &gbm_format, NULL);
465
466                 weston_config_section_get_string(section, "seat", &seat, "");
467         }
468
469         if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
470                 weston_log("Cannot configure output using weston_drm_output_api.\n");
471                 free(modeline);
472                 return -1;
473         }
474         free(modeline);
475
476         ivi->drm_api->set_gbm_format(output->output, gbm_format);
477         free(gbm_format);
478
479         ivi->drm_api->set_seat(output->output, seat);
480         free(seat);
481
482         return 0;
483 }
484
485 /*
486  * Reorgainizes the output's add array into two sections.
487  * add[0..ret-1] are the heads that failed to get attached.
488  * add[ret..add_len] are the heads that were successfully attached.
489  *
490  * The order between elements in each section is stable.
491  */
492 static size_t
493 try_attach_heads(struct ivi_output *output)
494 {
495         size_t fail_len = 0;
496
497         for (size_t i = 1; i < output->add_len; i++) {
498                 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
499                         struct weston_head *tmp = output->add[i];
500                         memmove(&output->add[fail_len + 1], output->add[fail_len],
501                                 sizeof output->add[0] * (i - fail_len));
502                         output->add[fail_len++] = tmp;
503                 }
504         }
505         return fail_len;
506 }
507
508 /* Place output exactly to the right of the most recently enabled output.
509  *
510  * Historically, we haven't given much thought to output placement,
511  * simply adding outputs in a horizontal line as they're enabled. This
512  * function simply sets an output's x coordinate to the right of the
513  * most recently enabled output, and its y to zero.
514  *
515  * If you're adding new calls to this function, you're also not giving
516  * much thought to output placement, so please consider carefully if
517  * it's really doing what you want.
518  *
519  * You especially don't want to use this for any code that won't
520  * immediately enable the passed output.
521  */
522 static void
523 weston_output_lazy_align(struct weston_output *output)
524 {
525         struct weston_compositor *c;
526         struct weston_output *peer;
527         int next_x = 0;
528
529         /* Put this output to the right of the most recently enabled output */
530         c = output->compositor;
531         if (!wl_list_empty(&c->output_list)) {
532                 peer = container_of(c->output_list.prev,
533                                 struct weston_output, link);
534                 next_x = peer->pos.c.x + peer->width;
535         }
536         output->pos.c.x = next_x;
537         output->pos.c.y = 0;
538 }
539
540
541 /*
542  * Like try_attach_heads, this reorganizes the output's add array into a failed
543  * and successful section.
544  * i is the number of heads that already failed the previous step.
545  */
546 static size_t
547 try_enable_output(struct ivi_output *output, size_t i)
548 {
549         for (; i < output->add_len; ++i) {
550                 struct weston_head *head;
551
552                 weston_output_lazy_align(output->output);
553
554                 if (weston_output_enable(output->output) == 0)
555                         break;
556
557                 head = output->add[output->add_len - 1];
558                 memmove(&output->add[i + 1], &output->add[i],
559                         sizeof output->add[0] * (output->add_len - i));
560                 output->add[i] = head;
561
562                 weston_head_detach(head);
563         }
564
565         return i;
566 }
567
568 static int
569 try_attach_enable_heads(struct ivi_output *output)
570 {
571         size_t fail_len;
572         assert(!output->output->enabled);
573
574         fail_len = try_attach_heads(output);
575
576         if (drm_configure_output(output) < 0)
577                 return -1;
578
579         fail_len = try_enable_output(output, fail_len);
580
581         /* All heads failed to be attached */
582         if (fail_len == output->add_len)
583                 return -1;
584
585         /* For each successful head attached */
586         for (size_t i = fail_len; i < output->add_len; ++i)
587                 add_head_destroyed_listener(output->add[i]);
588
589         ivi_layout_restore(output->ivi, output);
590
591         output->add_len = fail_len;
592         return 0;
593 }
594
595 static int
596 process_output(struct ivi_output *output)
597 {
598         if (output->output->enabled) {
599                 output->add_len = try_attach_heads(output);
600                 return output->add_len == 0 ? 0 : -1;
601         }
602
603         return try_attach_enable_heads(output);
604 }
605
606 static void
607 drm_head_disable(struct ivi_compositor *ivi, struct weston_head *head)
608 {
609         struct weston_output *output;
610         struct ivi_output *ivi_output;
611         struct wl_listener *listener;
612
613         output = weston_head_get_output(head);
614         assert(output);
615
616         listener = weston_output_get_destroy_listener(output,
617                                                       handle_output_destroy);
618         assert(listener);
619
620         ivi_output = wl_container_of(listener, ivi_output, output_destroy);
621         assert(ivi_output->output == output);
622
623         weston_head_detach(head);
624         if (count_heads(ivi_output->output) == 0) {
625                 if (ivi_output->output) {
626                         /* ivi_output->output destruction may be deferred in
627                          * some cases (see drm_output_destroy()), so we need to
628                          * forcibly trigger the destruction callback now, or
629                          * otherwise would later access data that we are about
630                          * to free
631                          */
632                         struct weston_output *save = ivi_output->output;
633
634                         handle_output_destroy(&ivi_output->output_destroy, save);
635                         weston_output_destroy(save);
636                 }
637         }
638         wl_list_remove(&ivi_output->link);
639         free(ivi_output->app_ids);
640         free(ivi_output);
641 }
642
643 static struct weston_config_section *
644 find_controlling_output_config(struct weston_config *config,
645                                const char *name)
646 {
647         struct weston_config_section *section;
648         char *same_as;
649         int depth = 0;
650
651         same_as = strdup(name);
652         do {
653                 section = weston_config_get_section(config, "output",
654                                                     "name", same_as);
655                 if (!section && depth > 0)
656                         weston_log("Configuration error: output section reffered"
657                                    "to by same-as=%s not found.\n", same_as);
658                 free(same_as);
659
660                 if (!section)
661                         return NULL;
662
663                 if (depth++ > 8) {
664                         weston_log("Configuration error: same-as nested too "
665                                    "deep for output '%s'.\n", name);
666                         return NULL;
667                 }
668
669                 weston_config_section_get_string(section, "same-as",
670                                                  &same_as, NULL);
671         } while (same_as);
672
673         return section;
674 }
675
676 static void
677 drm_head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
678 {
679         const char *name = weston_head_get_name(head);
680         struct weston_config_section *section;
681         struct ivi_output *output;
682         char *output_name = NULL;
683
684
685         section = find_controlling_output_config(ivi->config, name);
686         if (section) {
687                 char *mode;
688
689                 weston_config_section_get_string(section, "mode", &mode, NULL);
690                 if (mode && strcmp(mode, "off") == 0) {
691                         free(mode);
692                         return;
693                 }
694                 free(mode);
695
696                 weston_config_section_get_string(section, "name",
697                                                  &output_name, NULL);
698         } else {
699                 output_name = strdup(name);
700         }
701
702         if (!output_name)
703                 return;
704
705         output = ivi_ensure_output(ivi, output_name, section, head);
706         if (!output)
707                 return;
708
709         if (output->add_len >= ARRAY_LENGTH(output->add))
710                 return;
711
712         output->add[output->add_len++] = head;
713 }
714
715 static void
716 drm_heads_changed(struct wl_listener *listener, void *arg)
717 {
718         struct weston_compositor *compositor = arg;
719         struct weston_head *head = NULL;
720         struct ivi_compositor *ivi = to_ivi_compositor(compositor);
721         struct ivi_output *output;
722
723         while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
724                 bool connected = weston_head_is_connected(head);
725                 bool enabled = weston_head_is_enabled(head);
726                 bool changed = weston_head_is_device_changed(head);
727                 bool non_desktop = weston_head_is_non_desktop(head);
728
729                 if (connected && !enabled && !non_desktop)
730                         drm_head_prepare_enable(ivi, head);
731                 else if (!connected && enabled)
732                         drm_head_disable(ivi, head);
733                 else if (enabled && changed)
734                         weston_log("Detected a monitor change on head '%s', "
735                                    "not bothering to do anything about it.\n",
736                                    weston_head_get_name(head));
737
738                 weston_head_reset_device_changed(head);
739         }
740
741         wl_list_for_each(output, &ivi->outputs, link) {
742                 if (output->add_len == 0)
743                         continue;
744
745                 if (process_output(output) < 0) {
746                         output->add_len = 0;
747                         ivi->init_failed = true;
748                 }
749         }
750 }
751
752 static void
753 simple_head_enable(struct ivi_compositor *ivi, struct weston_head *head)
754 {
755         struct ivi_output *output;
756         struct weston_config_section *section;
757         char *output_name = NULL;
758         const char *name = weston_head_get_name(head);
759
760         section = find_controlling_output_config(ivi->config, name);
761         if (section) {
762                 char *mode;
763
764                 weston_config_section_get_string(section, "mode", &mode, NULL);
765                 if (mode && strcmp(mode, "off") == 0) {
766                         free(mode);
767                         return;
768                 }
769                 free(mode);
770
771                 weston_config_section_get_string(section, "name",
772                                                  &output_name, NULL);
773         } else {
774                 output_name = strdup(name);
775         }
776
777         if (!output_name)
778                 return;
779
780         output = ivi_ensure_output(ivi, output_name, section, head);
781         if (!output) {
782                 weston_log("Failed to create output %s\n", output_name);
783                 return;
784         }
785
786         add_head_destroyed_listener(head);
787 }
788
789
790 static void
791 simple_head_disable(struct weston_head *head)
792 {
793         struct weston_output *output;
794         struct wl_listener *listener;
795
796         listener = weston_head_get_destroy_listener(head, handle_head_destroy);
797         wl_list_empty(&listener->link);
798
799         output = weston_head_get_output(head);
800         assert(output);
801         weston_output_destroy(output);
802 }
803
804
805 static void
806 simple_heads_changed(struct wl_listener *listener, void *arg)
807 {
808         struct weston_compositor *compositor = arg;
809         struct ivi_compositor *ivi = to_ivi_compositor(compositor);
810         struct weston_head *head = NULL;
811         bool connected;
812         bool enabled;
813         bool changed;
814         bool non_desktop;
815
816         while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
817                 connected = weston_head_is_connected(head);
818                 enabled = weston_head_is_enabled(head);
819                 changed = weston_head_is_device_changed(head);
820                 non_desktop = weston_head_is_non_desktop(head);
821
822                 if (connected && !enabled && !non_desktop) {
823                         simple_head_enable(ivi, head);
824                 } else if (!connected && enabled) {
825                         simple_head_disable(head);
826                 } else if (enabled && changed) {
827                         weston_log("Detected a monitor change on head '%s', "
828                                         "not bothering to do anything about it.\n",
829                                         weston_head_get_name(head));
830                 }
831                 weston_head_reset_device_changed(head);
832         }
833 }
834
835
836 #ifdef HAVE_REMOTING
837 static int
838 drm_backend_remoted_output_configure(struct weston_output *output,
839                                      struct weston_config_section *section,
840                                      char *modeline,
841                                      const struct weston_remoting_api *api)
842 {
843         char *gbm_format = NULL;
844         char *seat = NULL;
845         char *host = NULL;
846         char *pipeline = NULL;
847         int port, ret;
848         int32_t scale = 1;
849         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
850         char *trans;
851
852         ret = api->set_mode(output, modeline);
853         if (ret < 0) {
854                 weston_log("Cannot configure an output \"%s\" using "
855                                 "weston_remoting_api. Invalid mode\n",
856                                 output->name);
857                 return -1;
858         }
859
860         weston_config_section_get_int(section, "scale", &scale, 1);
861         weston_output_set_scale(output, scale);
862
863         weston_config_section_get_string(section, "transform", &trans, "normal");
864         if (parse_transform(trans, &transform) < 0) {
865                 weston_log("Invalid transform \"%s\" for output %s\n",
866                            trans, output->name);
867         }
868         weston_output_set_transform(output, transform);
869
870         weston_config_section_get_string(section, "gbm-format",
871                                          &gbm_format, NULL);
872         api->set_gbm_format(output, gbm_format);
873         free(gbm_format);
874
875         weston_config_section_get_string(section, "seat", &seat, "");
876
877         api->set_seat(output, seat);
878         free(seat);
879
880         weston_config_section_get_string(section, "gst-pipeline", &pipeline,
881                         NULL);
882         if (pipeline) {
883                 api->set_gst_pipeline(output, pipeline);
884                 free(pipeline);
885                 return 0;
886         }
887
888         weston_config_section_get_string(section, "host", &host, NULL);
889         weston_config_section_get_int(section, "port", &port, 0);
890         if (!host || port <= 0 || 65533 < port) {
891                 weston_log("Cannot configure an output \"%s\". "
892                                 "Need to specify gst-pipeline or "
893                                 "host and port (1-65533).\n", output->name);
894         }
895         api->set_host(output, host);
896         free(host);
897         api->set_port(output, port);
898
899         return 0;
900 }
901
902
903 static int
904 remote_output_init(struct ivi_output *ivi_output,
905                    struct weston_compositor *compositor,
906                    struct weston_config_section *section,
907                    const struct weston_remoting_api *api)
908 {
909         char *output_name, *modeline = NULL;
910         int ret = -1;
911
912         weston_config_section_get_string(section, "name", &output_name, NULL);
913         if (!output_name)
914                 return ret;
915
916         weston_config_section_get_string(section, "mode", &modeline, "off");
917         if (strcmp(modeline, "off") == 0)
918                 goto err;
919
920         ivi_output->output = api->create_output(compositor, output_name);
921         if (!ivi_output->output) {
922                 weston_log("Cannot create remoted output \"%s\".\n",
923                                 output_name);
924                 goto err;
925         }
926
927         ret = drm_backend_remoted_output_configure(ivi_output->output, section,
928                                                    modeline, api);
929         if (ret < 0) {
930                 weston_log("Cannot configure remoted output \"%s\".\n",
931                                 output_name);
932                 goto err;
933         }
934
935         if ((ret = weston_output_enable(ivi_output->output)) < 0) {
936                 weston_log("Enabling remoted output \"%s\" failed.\n",
937                                 output_name);
938                 goto err;
939         }
940
941         free(modeline);
942         free(output_name);
943         weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
944
945         return 0;
946
947 err:
948         free(modeline);
949         free(output_name);
950         if (ivi_output->output)
951                 weston_output_destroy(ivi_output->output);
952
953         return ret;
954 }
955
956 static void
957 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
958 {
959         struct weston_config_section *remote_section = NULL;
960         const char *section_name;
961         struct weston_config *config = ivi->config;
962
963         while (weston_config_next_section(config, &remote_section, &section_name)) {
964                 if (strcmp(section_name, "remote-output"))
965                         continue;
966
967                 struct ivi_output *ivi_output = NULL;
968                 bool output_found = false;
969                 char *_name = NULL;
970
971                 weston_config_section_get_string(remote_section,
972                                                  "name", &_name, NULL);
973                 wl_list_for_each(ivi_output, &ivi->outputs, link) {
974                         if (!strcmp(ivi_output->name, _name)) {
975                                 output_found = true;
976                                 break;
977                         }
978                 }
979
980                 if (output_found) {
981                         free(_name);
982                         continue;
983                 }
984
985                 ivi_output = zalloc(sizeof(*ivi_output));
986                 if (!ivi_output) {
987                         free(_name);
988                         continue;
989                 }
990
991                 ivi_output->ivi = ivi;
992                 ivi_output->name = _name;
993                 ivi_output->config = remote_section;
994                 ivi_output->type = OUTPUT_REMOTE;
995
996                 if (remote_output_init(ivi_output, ivi->compositor,
997                                        remote_section, ivi->remoting_api)) {
998                         free(ivi_output->name);
999                         free(ivi_output);
1000                         continue;
1001                 }
1002
1003                 ivi_output->output_destroy.notify = handle_output_destroy;
1004                 weston_output_add_destroy_listener(ivi_output->output,
1005                                                    &ivi_output->output_destroy);
1006
1007                 wl_list_insert(&ivi->outputs, &ivi_output->link);
1008                 ivi_output_configure_app_id(ivi_output);
1009         }
1010 }
1011
1012 static int
1013 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
1014 {
1015         struct weston_compositor *compositor = ivi->compositor;
1016         int (*module_init)(struct weston_compositor *wc);
1017
1018         module_init = weston_load_module("remoting-plugin.so",
1019                                          "weston_module_init",
1020                                          LIBWESTON_MODULEDIR);
1021         if (!module_init)
1022                 return -1;
1023
1024         if (module_init(compositor) < 0)
1025                 return -1;
1026
1027         ivi->remoting_api = weston_remoting_get_api(compositor);
1028         if (!ivi->remoting_api)
1029                 return -1;
1030         return 0;
1031 }
1032 #else
1033 static int
1034 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
1035 {
1036         return -1;
1037 }
1038 #endif
1039
1040 static int
1041 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[],
1042                  enum weston_renderer_type renderer)
1043 {
1044         struct weston_drm_backend_config config = {
1045                 .base = {
1046                         .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
1047                         .struct_size = sizeof config,
1048                 },
1049         };
1050         struct weston_config_section *section;
1051         int use_current_mode = 0;
1052         bool force_pixman = false;
1053         bool use_shadow;
1054         bool without_input = false;
1055
1056         const struct weston_option options[] = {
1057                 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
1058                 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
1059                 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
1060                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &force_pixman },
1061                 { WESTON_OPTION_BOOLEAN, "continue-without-input", false, &without_input }
1062         };
1063
1064         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1065
1066         if (force_pixman)
1067                 config.renderer = WESTON_RENDERER_PIXMAN;
1068         else
1069                 config.renderer = WESTON_RENDERER_AUTO;
1070
1071         ivi->cmdline.use_current_mode = use_current_mode;
1072
1073         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
1074         weston_config_section_get_string(section, "gbm-format",
1075                                          &config.gbm_format, NULL);
1076         weston_config_section_get_uint(section, "pageflip-timeout",
1077                                        &config.pageflip_timeout, 0);
1078         weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
1079         config.use_pixman_shadow = use_shadow;
1080
1081         if (without_input)
1082                 ivi->compositor->require_input = !without_input;
1083
1084         ivi->heads_changed.notify = drm_heads_changed;
1085         weston_compositor_add_heads_changed_listener(ivi->compositor,
1086                                                      &ivi->heads_changed);
1087
1088         if (!weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
1089                                             &config.base)) {
1090                 weston_log("Failed to load DRM backend\n");
1091                 return -1;
1092         }
1093
1094         ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
1095         if (!ivi->drm_api) {
1096                 weston_log("Cannot use drm output api.\n");
1097                 goto error;
1098         }
1099
1100         load_remoting_plugin(ivi, ivi->config);
1101
1102         return 0;
1103
1104 error:
1105         free(config.gbm_format);
1106         free(config.seat_id);
1107         return -1;
1108 }
1109
1110 static void
1111 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
1112                               bool *force_pixman, bool *fullscreen, int *output_count)
1113 {
1114         struct weston_config_section *section;
1115         bool pixman;
1116         int fs = 0;
1117
1118         const struct weston_option options[] = {
1119                 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
1120                 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
1121                 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
1122                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &force_pixman },
1123                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
1124                 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
1125         };
1126
1127         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
1128         weston_config_section_get_bool(section, "use-pixman", &pixman, false);
1129
1130         *output_count = 1;
1131         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1132         *force_pixman = pixman;
1133         *fullscreen = fs;
1134 }
1135
1136 static int
1137 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
1138                         const char *match_prefix, const char *name_prefix)
1139 {
1140         struct weston_config_section *section = NULL;
1141         const char *section_name;
1142         char *default_output = NULL;
1143         int i = 0;
1144         size_t match_len = strlen(match_prefix);
1145
1146         while (weston_config_next_section(ivi->config, &section, &section_name)) {
1147                 char *output_name;
1148
1149                 if (i >= output_count)
1150                         break;
1151
1152                 if (strcmp(section_name, "output") != 0)
1153                         continue;
1154
1155                 weston_config_section_get_string(section, "name", &output_name, NULL);
1156                 if (output_name == NULL)
1157                         continue;
1158                 if (strncmp(output_name, match_prefix, match_len) != 0) {
1159                         free(output_name);
1160                         continue;
1161                 }
1162
1163                 if (ivi->window_api->create_head(ivi->backend, output_name) < 0) {
1164                         free(output_name);
1165                         return -1;
1166                 }
1167
1168                 free(output_name);
1169                 ++i;
1170         }
1171
1172         for (; i < output_count; ++i) {
1173                 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
1174                         return -1;
1175
1176                 if (ivi->window_api->create_head(ivi->backend, default_output) < 0) {
1177                         free(default_output);
1178                         return -1;
1179                 }
1180
1181                 free(default_output);
1182         }
1183
1184         return 0;
1185 }
1186
1187
1188 static int
1189 wayland_backend_output_configure(struct weston_output *output)
1190 {
1191         struct ivi_output *ivi_output = to_ivi_output(output);
1192
1193         struct ivi_output_config defaults = {
1194                 .width = WINDOWED_DEFAULT_WIDTH,
1195                 .height = WINDOWED_DEFAULT_HEIGHT,
1196                 .scale = 1,
1197                 .transform = WL_OUTPUT_TRANSFORM_NORMAL
1198         };
1199
1200         if (!ivi_output) {
1201                 weston_log("Failed to configure and enable Wayland output. No ivi-output available!\n");
1202                 return -1;
1203         }
1204
1205         return ivi_configure_windowed_output_from_config(to_ivi_output(output), &defaults);
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->simple_output_configure = wayland_backend_output_configure;
1241         ivi->heads_changed.notify = simple_heads_changed;
1242         weston_compositor_add_heads_changed_listener(ivi->compositor,
1243                                                      &ivi->heads_changed);
1244
1245         ivi->backend = weston_compositor_load_backend(ivi->compositor,
1246                                                       WESTON_BACKEND_WAYLAND,
1247                                                       &config.base);
1248         if (!ivi->backend) {
1249                 weston_log("Failed to create Wayland backend!\n");
1250         }
1251
1252         free(config.cursor_theme);
1253         free(config.display_name);
1254
1255         if (!ivi->backend)
1256                 return -1;
1257
1258         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1259
1260         /*
1261          * We will just assume if load_backend() finished cleanly and
1262          * windowed_output_api is not present that wayland backend is started
1263          * with --sprawl or runs on fullscreen-shell. In this case, all values
1264          * are hardcoded, so nothing can be configured; simply create and
1265          * enable an output.
1266          */
1267         if (ivi->window_api == NULL)
1268                 return 0;
1269
1270         return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1271 }
1272
1273 #ifdef HAVE_BACKEND_X11
1274 static int
1275 x11_backend_output_configure(struct weston_output *output)
1276 {
1277         struct ivi_output *ivi_output = to_ivi_output(output);
1278
1279         struct ivi_output_config defaults = {
1280                 .width = WINDOWED_DEFAULT_WIDTH,
1281                 .height = WINDOWED_DEFAULT_HEIGHT,
1282                 .scale = 1,
1283                 .transform = WL_OUTPUT_TRANSFORM_NORMAL
1284         };
1285
1286         if (!ivi_output) {
1287                 weston_log("Failed to configure and enable X11 output. No ivi-output available!\n");
1288                 return -1;
1289         }
1290
1291
1292         return ivi_configure_windowed_output_from_config(ivi_output, &defaults);
1293 }
1294
1295 static int
1296 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[],
1297                  enum weston_renderer_type renderer)
1298 {
1299         struct weston_x11_backend_config config = {
1300                 .base = {
1301                         .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1302                         .struct_size = sizeof config,
1303                 },
1304         };
1305         int no_input = 0;
1306         int output_count;
1307         bool force_pixman = false;
1308
1309         const struct weston_option options[] = {
1310                { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1311         };
1312
1313         windowed_parse_common_options(ivi, argc, argv, &force_pixman,
1314                                       &config.fullscreen, &output_count);
1315
1316         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1317         if (force_pixman)
1318                 config.renderer = WESTON_RENDERER_PIXMAN;
1319         else
1320                 config.renderer = WESTON_RENDERER_AUTO;
1321         config.no_input = no_input;
1322
1323         ivi->simple_output_configure = x11_backend_output_configure;
1324
1325         ivi->heads_changed.notify = simple_heads_changed;
1326         weston_compositor_add_heads_changed_listener(ivi->compositor,
1327                                                      &ivi->heads_changed);
1328
1329         ivi->backend = weston_compositor_load_backend(ivi->compositor,
1330                                                       WESTON_BACKEND_X11,
1331                                                       &config.base);
1332         if (!ivi->backend) {
1333                 weston_log("Failed to create X11 backend!\n");
1334                 return -1;
1335         }
1336
1337         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1338         if (!ivi->window_api) {
1339                 weston_log("Cannot use weston_windowed_output_api.\n");
1340                 return -1;
1341         }
1342
1343         return windowed_create_outputs(ivi, output_count, "X", "screen");
1344 }
1345 #else
1346 static int
1347 load_x11_backend(struct ivi_compositor *ivi, int *argc, char **argv,
1348                  enum weston_renderer_type renderer)
1349 {
1350         return -1;
1351 }
1352 #endif
1353
1354 #ifdef HAVE_BACKEND_RDP
1355 static void
1356 weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
1357 {
1358         config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
1359         config->base.struct_size = sizeof(struct weston_rdp_backend_config);
1360
1361         config->renderer = WESTON_RENDERER_AUTO;
1362         config->bind_address = NULL;
1363         config->port = 3389;
1364         config->rdp_key = NULL;
1365         config->server_cert = NULL;
1366         config->server_key = NULL;
1367         config->env_socket = 0;
1368         config->external_listener_fd = -1;
1369         config->no_clients_resize = 0;
1370         config->force_no_compression = 0;
1371         config->remotefx_codec = true;
1372         config->refresh_rate = RDP_DEFAULT_FREQ;
1373
1374 }
1375
1376 static int
1377 rdp_backend_output_configure(struct weston_output *output)
1378 {
1379         struct ivi_compositor *ivi = to_ivi_compositor(output->compositor);
1380         struct ivi_output_config *parsed_options = ivi->parsed_options;
1381         const struct weston_rdp_output_api *api =
1382                 weston_rdp_output_get_api(output->compositor);
1383         int width = 640;
1384         int height = 480;
1385         struct weston_config_section *section;
1386         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
1387         char *transform_string;
1388         struct weston_mode new_mode = {};
1389
1390         assert(parsed_options);
1391
1392         if (!api) {
1393                 weston_log("Cannot use weston_rdp_output_api.\n");
1394                 return -1;
1395         }
1396
1397         section = weston_config_get_section(ivi->config, "rdp", NULL, NULL);
1398
1399         if (parsed_options->width)
1400                 width = parsed_options->width;
1401
1402         if (parsed_options->height)
1403                 height = parsed_options->height;
1404
1405         weston_output_set_scale(output, 1);
1406
1407         weston_config_section_get_int(section, "width",
1408                         &width, width);
1409
1410         weston_config_section_get_int(section, "height",
1411                         &height, height);
1412
1413         if (parsed_options->transform)
1414                 transform = parsed_options->transform;
1415
1416         weston_config_section_get_string(section, "transform",
1417                         &transform_string, "normal");
1418
1419         if (parse_transform(transform_string, &transform) < 0) {
1420                 weston_log("Invalid transform \"%s\" for output %s\n",
1421                            transform_string, output->name);
1422                 return -1;
1423         }
1424
1425
1426         new_mode.width = width;
1427         new_mode.height = height;
1428
1429         weston_log("Setting modeline to %dx%d\n", width, height);
1430
1431         api->output_set_mode(output, &new_mode);
1432         weston_output_set_transform(output, transform);
1433
1434         return 0;
1435 }
1436
1437 static int
1438 load_rdp_backend(struct ivi_compositor *ivi, int *argc, char **argv,
1439                  enum weston_renderer_type renderer)
1440 {
1441         struct weston_rdp_backend_config config = {};
1442         struct weston_config_section *section;
1443         bool no_remotefx_codec = false;
1444
1445         struct ivi_output_config *parsed_options = ivi_init_parsed_options(ivi->compositor);
1446         if (!parsed_options)
1447                 return -1;
1448
1449         weston_rdp_backend_config_init(&config);
1450
1451         const struct weston_option rdp_options[] = {
1452                 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1453                 { WESTON_OPTION_INTEGER, "external-listener-fd", 0, &config.external_listener_fd },
1454                 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1455                 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
1456                 { WESTON_OPTION_STRING,  "address", 0, &config.bind_address },
1457                 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1458                 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1459                 { WESTON_OPTION_STRING,  "rdp4-key", 0, &config.rdp_key },
1460                 { WESTON_OPTION_STRING,  "rdp-tls-cert", 0, &config.server_cert },
1461                 { WESTON_OPTION_STRING,  "rdp-tls-key", 0, &config.server_key },
1462                 { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
1463                 { WESTON_OPTION_BOOLEAN, "force-no-compression", 0, &config.force_no_compression },
1464                 { WESTON_OPTION_BOOLEAN, "no-remotefx-codec", 0, &no_remotefx_codec },
1465         };
1466
1467         config.remotefx_codec = !no_remotefx_codec;
1468         config.renderer = renderer;
1469
1470         section = weston_config_get_section(ivi->config, "rdp", NULL, NULL);
1471
1472         weston_config_section_get_int(section, "refresh-rate",
1473                         &config.refresh_rate, RDP_DEFAULT_FREQ);
1474
1475         weston_config_section_get_string(section, "tls-cert",
1476                         &config.server_cert, config.server_cert);
1477
1478         weston_config_section_get_string(section, "tls-key",
1479                         &config.server_key, config.server_key);
1480
1481
1482         parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1483         weston_log("No clients resize: %d\n", config.no_clients_resize);
1484
1485         ivi->simple_output_configure = rdp_backend_output_configure;
1486
1487         ivi->heads_changed.notify = simple_heads_changed;
1488         weston_compositor_add_heads_changed_listener(ivi->compositor,
1489                                                      &ivi->heads_changed);
1490
1491         if (!weston_compositor_load_backend(ivi->compositor,
1492                                             WESTON_BACKEND_RDP, &config.base)) {
1493                 weston_log("Failed to create RDP backend\n");
1494                 return -1;
1495         }
1496
1497         free(config.bind_address);
1498         free(config.rdp_key);
1499         free(config.server_cert);
1500         free(config.server_key);
1501
1502         return 0;
1503 }
1504 #else
1505 static int
1506 load_rdp_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1507 {
1508         return -1;
1509 }
1510 #endif
1511
1512
1513 static int
1514 load_backend(struct ivi_compositor *ivi, int *argc, char **argv,
1515              const char *backend_name, const char *renderer_name)
1516 {
1517         enum weston_compositor_backend backend;
1518         enum weston_renderer_type renderer;
1519
1520         if (!get_backend_from_string(backend_name, &backend)) {
1521                 weston_log("Error: unknown backend \"%s\"\n", backend_name);
1522                 return -1;
1523         }
1524
1525         if (!get_renderer_from_string(renderer_name, &renderer)) {
1526                 weston_log("Error: unknown renderer \"%s\"\n", renderer_name);
1527                 return -1;
1528         }
1529
1530         switch (backend) {
1531         case WESTON_BACKEND_DRM:
1532                 return load_drm_backend(ivi, argc, argv, renderer);
1533         case WESTON_BACKEND_RDP:
1534                 return load_rdp_backend(ivi, argc, argv, renderer);
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 }