compositor: Fix loading nested X11/Wayland backends
[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->bind_address = NULL;
1362         config->port = 3389;
1363         config->rdp_key = NULL;
1364         config->server_cert = NULL;
1365         config->server_key = NULL;
1366         config->env_socket = 0;
1367         config->no_clients_resize = 1;
1368         config->force_no_compression = 0;
1369 }
1370
1371 static int
1372 rdp_backend_output_configure(struct weston_output *output)
1373 {
1374         struct ivi_compositor *ivi = to_ivi_compositor(output->compositor);
1375         struct ivi_output_config *parsed_options = ivi->parsed_options;
1376         const struct weston_rdp_output_api *api =
1377                 weston_rdp_output_get_api(output->compositor);
1378         int width = 640;
1379         int height = 480;
1380         struct weston_config_section *section;
1381         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
1382         char *transform_string;
1383         struct weston_mode new_mode = {};
1384
1385         assert(parsed_options);
1386
1387         if (!api) {
1388                 weston_log("Cannot use weston_rdp_output_api.\n");
1389                 return -1;
1390         }
1391
1392         section = weston_config_get_section(ivi->config, "rdp", NULL, NULL);
1393
1394         if (parsed_options->width)
1395                 width = parsed_options->width;
1396
1397         if (parsed_options->height)
1398                 height = parsed_options->height;
1399
1400         weston_output_set_scale(output, 1);
1401
1402         weston_config_section_get_int(section, "width",
1403                         &width, width);
1404
1405         weston_config_section_get_int(section, "height",
1406                         &height, height);
1407
1408         if (parsed_options->transform)
1409                 transform = parsed_options->transform;
1410
1411         weston_config_section_get_string(section, "transform",
1412                         &transform_string, "normal");
1413
1414         if (parse_transform(transform_string, &transform) < 0) {
1415                 weston_log("Invalid transform \"%s\" for output %s\n",
1416                            transform_string, output->name);
1417                 return -1;
1418         }
1419
1420
1421         new_mode.width = width;
1422         new_mode.height = height;
1423
1424         api->output_set_mode(output, &new_mode);
1425         weston_output_set_transform(output, transform);
1426
1427         return 0;
1428 }
1429
1430 static int
1431 load_rdp_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1432 {
1433         struct weston_rdp_backend_config config = {};
1434         struct weston_config_section *section;
1435
1436         struct ivi_output_config *parsed_options = ivi_init_parsed_options(ivi->compositor);
1437         if (!parsed_options)
1438                 return -1;
1439
1440         weston_rdp_backend_config_init(&config);
1441
1442         const struct weston_option rdp_options[] = {
1443                 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1444                 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1445                 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
1446                 { WESTON_OPTION_INTEGER, "transform", 0, &parsed_options->transform },
1447                 { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
1448                 { WESTON_OPTION_STRING,  "address", 0, &config.bind_address },
1449                 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1450                 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1451                 { WESTON_OPTION_STRING,  "rdp4-key", 0, &config.rdp_key },
1452                 { WESTON_OPTION_STRING,  "rdp-tls-cert", 0, &config.server_cert },
1453                 { WESTON_OPTION_STRING,  "rdp-tls-key", 0, &config.server_key },
1454                 { WESTON_OPTION_BOOLEAN, "force-no-compression", 0, &config.force_no_compression },
1455         };
1456
1457         section = weston_config_get_section(ivi->config, "rdp", NULL, NULL);
1458
1459         weston_config_section_get_string(section, "tls-cert",
1460                         &config.server_cert, config.server_cert);
1461
1462         weston_config_section_get_string(section, "tls-key",
1463                         &config.server_key, config.server_key);
1464
1465
1466         parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1467
1468         ivi->simple_output_configure = rdp_backend_output_configure;
1469
1470         ivi->heads_changed.notify = simple_heads_changed;
1471         weston_compositor_add_heads_changed_listener(ivi->compositor,
1472                                                      &ivi->heads_changed);
1473
1474         if (!weston_compositor_load_backend(ivi->compositor,
1475                                             WESTON_BACKEND_RDP, &config.base)) {
1476                 weston_log("Failed to create RDP backend\n");
1477                 return -1;
1478         }
1479
1480         free(config.bind_address);
1481         free(config.rdp_key);
1482         free(config.server_cert);
1483         free(config.server_key);
1484
1485         return 0;
1486 }
1487 #else
1488 static int
1489 load_rdp_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1490 {
1491         return -1;
1492 }
1493 #endif
1494
1495
1496 static int
1497 load_backend(struct ivi_compositor *ivi, int *argc, char **argv,
1498              const char *backend_name, const char *renderer_name)
1499 {
1500         enum weston_compositor_backend backend;
1501         enum weston_renderer_type renderer;
1502
1503         if (!get_backend_from_string(backend_name, &backend)) {
1504                 weston_log("Error: unknown backend \"%s\"\n", backend_name);
1505                 return -1;
1506         }
1507
1508         if (!get_renderer_from_string(renderer_name, &renderer)) {
1509                 weston_log("Error: unknown renderer \"%s\"\n", renderer_name);
1510                 return -1;
1511         }
1512
1513         switch (backend) {
1514         case WESTON_BACKEND_DRM:
1515                 return load_drm_backend(ivi, argc, argv, renderer);
1516         case WESTON_BACKEND_RDP:
1517                 return load_rdp_backend(ivi, argc, argv);
1518         case WESTON_BACKEND_WAYLAND:
1519                 return load_wayland_backend(ivi, argc, argv, renderer);
1520         case WESTON_BACKEND_X11:
1521                 return load_x11_backend(ivi, argc, argv, renderer);
1522         default:
1523                 assert(!"unknown backend type in load_backend()");
1524         }
1525
1526         return 0;
1527 }
1528
1529 static int
1530 load_modules(struct ivi_compositor *ivi, const char *modules,
1531              int *argc, char *argv[], bool *xwayland)
1532 {
1533         const char *p, *end;
1534         char buffer[256];
1535         int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1536
1537         if (modules == NULL)
1538                 return 0;
1539
1540         p = modules;
1541         while (*p) {
1542                 end = strchrnul(p, ',');
1543                 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1544
1545                 if (strstr(buffer, "xwayland.so")) {
1546                         *xwayland = true;
1547                 } else if (strstr(buffer, "systemd-notify.so")) {
1548                         weston_log("systemd-notify plug-in already loaded!\n");
1549                 } else {
1550                         module_init = weston_load_module(buffer, "wet_module_init", WESTON_MODULEDIR);
1551                         if (!module_init)
1552                                 return -1;
1553
1554                         if (module_init(ivi->compositor, *argc, argv) < 0)
1555                                 return -1;
1556
1557                 }
1558
1559                 p = end;
1560                 while (*p == ',')
1561                         p++;
1562         }
1563
1564         return 0;
1565 }
1566
1567
1568 static char *
1569 choose_default_backend(void)
1570 {
1571         char *backend = NULL;
1572
1573         if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1574                 backend = strdup("wayland-backend.so");
1575         else if (getenv("DISPLAY"))
1576                 backend = strdup("x11-backend.so");
1577         else
1578                 backend = strdup("drm-backend.so");
1579
1580         return backend;
1581 }
1582
1583 static int
1584 compositor_init_config(struct ivi_compositor *ivi)
1585 {
1586         struct xkb_rule_names xkb_names;
1587         struct weston_config_section *section;
1588         struct weston_compositor *compositor = ivi->compositor;
1589         struct weston_config *config = ivi->config;
1590         int repaint_msec;
1591         bool vt_switching;
1592         bool require_input;
1593
1594         /* agl-compositor.ini [keyboard] */
1595         section = weston_config_get_section(config, "keyboard", NULL, NULL);
1596         weston_config_section_get_string(section, "keymap_rules",
1597                                          (char **) &xkb_names.rules, NULL);
1598         weston_config_section_get_string(section, "keymap_model",
1599                                          (char **) &xkb_names.model, NULL);
1600         weston_config_section_get_string(section, "keymap_layout",
1601                                          (char **) &xkb_names.layout, NULL);
1602         weston_config_section_get_string(section, "keymap_variant",
1603                                          (char **) &xkb_names.variant, NULL);
1604         weston_config_section_get_string(section, "keymap_options",
1605                                          (char **) &xkb_names.options, NULL);
1606
1607         if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1608                 return -1;
1609
1610         weston_config_section_get_int(section, "repeat-rate",
1611                                       &compositor->kb_repeat_rate, 40);
1612         weston_config_section_get_int(section, "repeat-delay",
1613                                       &compositor->kb_repeat_delay, 400);
1614
1615         weston_config_section_get_bool(section, "vt-switching",
1616                                        &vt_switching, false);
1617         compositor->vt_switching = vt_switching;
1618
1619         /* agl-compositor.ini [core] */
1620         section = weston_config_get_section(config, "core", NULL, NULL);
1621
1622         weston_config_section_get_bool(section, "disable-cursor",
1623                                        &ivi->disable_cursor, false);
1624         weston_config_section_get_bool(section, "activate-by-default",
1625                                        &ivi->activate_by_default, true);
1626
1627         weston_config_section_get_bool(section, "require-input", &require_input, true);
1628         compositor->require_input = require_input;
1629
1630         weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1631                                       compositor->repaint_msec);
1632         if (repaint_msec < -10 || repaint_msec > 1000) {
1633                 weston_log("Invalid repaint_window value in config: %d\n",
1634                            repaint_msec);
1635         } else {
1636                 compositor->repaint_msec = repaint_msec;
1637         }
1638         weston_log("Output repaint window is %d ms maximum.\n",
1639                    compositor->repaint_msec);
1640
1641         return 0;
1642 }
1643
1644 struct ivi_surface *
1645 to_ivi_surface(struct weston_surface *surface)
1646 {
1647         struct weston_desktop_surface *dsurface;
1648
1649         dsurface = weston_surface_get_desktop_surface(surface);
1650         if (!dsurface)
1651                 return NULL;
1652
1653         return weston_desktop_surface_get_user_data(dsurface);
1654 }
1655
1656 static void
1657 activate_binding(struct weston_seat *seat,
1658                  struct weston_view *focus_view, uint32_t flags)
1659 {
1660         struct weston_surface *focus_surface;
1661         struct weston_surface *main_surface;
1662         struct ivi_surface *ivi_surface;
1663         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(seat);
1664
1665         if (!focus_view)
1666                 return;
1667
1668         focus_surface = focus_view->surface;
1669         main_surface = weston_surface_get_main_surface(focus_surface);
1670
1671         ivi_surface = to_ivi_surface(main_surface);
1672         if (!ivi_surface)
1673                 return;
1674
1675         if (ivi_seat)
1676                 ivi_shell_activate_surface(ivi_surface, ivi_seat, flags);
1677 }
1678
1679 static void
1680 click_to_activate_binding(struct weston_pointer *pointer,
1681                           const struct timespec *time,
1682                           uint32_t button, void *data)
1683 {
1684         if (pointer->grab != &pointer->default_grab)
1685                 return;
1686         if (pointer->focus == NULL)
1687                 return;
1688
1689         activate_binding(pointer->seat, pointer->focus,
1690                          WESTON_ACTIVATE_FLAG_CLICKED);
1691 }
1692
1693 static void
1694 touch_to_activate_binding(struct weston_touch *touch,
1695                           const struct timespec *time,
1696                           void *data)
1697 {
1698         if (touch->grab != &touch->default_grab)
1699                 return;
1700         if (touch->focus == NULL)
1701                 return;
1702
1703         activate_binding(touch->seat, touch->focus,
1704                          WESTON_ACTIVATE_FLAG_NONE);
1705 }
1706
1707 static void
1708 add_bindings(struct weston_compositor *compositor)
1709 {
1710         weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1711                                              click_to_activate_binding,
1712                                              NULL);
1713         weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1714                                              click_to_activate_binding,
1715                                              NULL);
1716         weston_compositor_add_touch_binding(compositor, 0,
1717                                             touch_to_activate_binding,
1718                                             NULL);
1719 }
1720
1721 static int
1722 create_listening_socket(struct wl_display *display, const char *socket_name)
1723 {
1724         if (socket_name) {
1725                 if (wl_display_add_socket(display, socket_name)) {
1726                         weston_log("fatal: failed to add socket: %s\n",
1727                                    strerror(errno));
1728                         return -1;
1729                 }
1730         } else {
1731                 socket_name = wl_display_add_socket_auto(display);
1732                 if (!socket_name) {
1733                         weston_log("fatal: failed to add socket: %s\n",
1734                                    strerror(errno));
1735                         return -1;
1736                 }
1737         }
1738
1739         setenv("WAYLAND_DISPLAY", socket_name, 1);
1740
1741         return 0;
1742 }
1743
1744 static bool
1745 global_filter(const struct wl_client *client, const struct wl_global *global,
1746               void *data)
1747 {
1748         return true;
1749 }
1750
1751 static int
1752 load_config(struct weston_config **config, bool no_config,
1753             const char *config_file)
1754 {
1755         const char *file = "agl-compositor.ini";
1756         const char *full_path;
1757
1758         if (config_file)
1759                 file = config_file;
1760
1761         if (!no_config)
1762                 *config = weston_config_parse(file);
1763
1764         if (*config) {
1765                 full_path = weston_config_get_full_path(*config);
1766
1767                 weston_log("Using config file '%s'.\n", full_path);
1768                 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1769
1770                 return 0;
1771         }
1772
1773         if (config_file && !no_config) {
1774                 weston_log("fatal: error opening or reading config file '%s'.\n",
1775                         config_file);
1776
1777                 return -1;
1778         }
1779
1780         weston_log("Starting with no config file.\n");
1781         setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1782
1783         return 0;
1784 }
1785
1786 static FILE *logfile;
1787
1788 static char *
1789 log_timestamp(char *buf, size_t len)
1790 {
1791         struct timeval tv;
1792         struct tm *brokendown_time;
1793         char datestr[128];
1794         char timestr[128];
1795
1796         gettimeofday(&tv, NULL);
1797
1798         brokendown_time = localtime(&tv.tv_sec);
1799         if (brokendown_time == NULL) {
1800                 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1801                 return buf;
1802         }
1803
1804         memset(datestr, 0, sizeof(datestr));
1805         if (brokendown_time->tm_mday != cached_tm_mday) {
1806                 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1807                                 brokendown_time);
1808                 cached_tm_mday = brokendown_time->tm_mday;
1809         }
1810
1811         strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1812         /* if datestr is empty it prints only timestr*/
1813         snprintf(buf, len, "%s[%s.%03li]", datestr,
1814                         timestr, (tv.tv_usec / 1000));
1815
1816         return buf;
1817 }
1818
1819 static void
1820 custom_handler(const char *fmt, va_list arg)
1821 {
1822         char timestr[512];
1823
1824         weston_log_scope_printf(log_scope, "%s libwayland: ",
1825                         log_timestamp(timestr, sizeof(timestr)));
1826         weston_log_scope_vprintf(log_scope, fmt, arg);
1827 }
1828
1829 static void
1830 log_file_open(const char *filename)
1831 {
1832         wl_log_set_handler_server(custom_handler);
1833
1834         if (filename)
1835                 logfile = fopen(filename, "a");
1836
1837         if (!logfile) {
1838                 logfile = stderr;
1839         } else {
1840                 os_fd_set_cloexec(fileno(logfile));
1841                 setvbuf(logfile, NULL, _IOLBF, 256);
1842         }
1843 }
1844
1845 static void
1846 log_file_close(void)
1847 {
1848         if (logfile && logfile != stderr)
1849                 fclose(logfile);
1850         logfile = stderr;
1851 }
1852
1853 static int
1854 vlog(const char *fmt, va_list ap)
1855 {
1856         const char *oom = "Out of memory";
1857         char timestr[128];
1858         int len = 0;
1859         char *str;
1860
1861         if (weston_log_scope_is_enabled(log_scope)) {
1862                 int len_va;
1863                 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1864                 len_va = vasprintf(&str, fmt, ap);
1865                 if (len_va >= 0) {
1866                         len = weston_log_scope_printf(log_scope, "%s %s",
1867                                         xlog_timestamp, str);
1868                         free(str);
1869                 } else {
1870                         len = weston_log_scope_printf(log_scope, "%s %s",
1871                                         xlog_timestamp, oom);
1872                 }
1873         }
1874
1875         return len;
1876 }
1877
1878
1879 static int
1880 vlog_continue(const char *fmt, va_list ap)
1881 {
1882         return weston_log_scope_vprintf(log_scope, fmt, ap);
1883 }
1884
1885 static int
1886 on_term_signal(int signo, void *data)
1887 {
1888         struct wl_display *display = data;
1889
1890         weston_log("caught signal %d\n", signo);
1891         wl_display_terminate(display);
1892
1893         return 1;
1894 }
1895
1896 static void
1897 handle_exit(struct weston_compositor *compositor)
1898 {
1899         wl_display_terminate(compositor->wl_display);
1900 }
1901
1902 static void
1903 usage(int error_code)
1904 {
1905         FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1906         fprintf(out,
1907                 "Usage: agl-compositor [OPTIONS]\n"
1908                 "\n"
1909                 "This is " PACKAGE_STRING ", the reference compositor for\n"
1910                 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1911                 "backends,\nand depending on which backend is in use different "
1912                 "options will be accepted.\n"
1913                 "\n"
1914                 "Core options:\n"
1915                 "\n"
1916                 "  --version\t\tPrint agl-compositor version\n"
1917                 "  -B, --backend=MODULE\tBackend module, one of\n"
1918                         "\t\t\t\tdrm-backend.so\n"
1919                         "\t\t\t\twayland-backend.so\n"
1920                         "\t\t\t\tx11-backend.so\n"
1921                         "\t\t\t\theadless-backend.so\n"
1922                 "  -r, --renderer=NAME\tName of renderer to use: auto, gl, noop, pixman\n"
1923                 "  -S, --socket=NAME\tName of socket to listen on\n"
1924                 "  --log=FILE\t\tLog to the given file\n"
1925                 "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1926                 "  --no-config\t\tDo not read agl-compositor.ini\n"
1927                 "  --debug\t\tEnable debug extension(s)\n"
1928                 "  -h, --help\t\tThis help message\n"
1929                 "\n");
1930         exit(error_code);
1931 }
1932
1933 static char *
1934 copy_command_line(int argc, char * const argv[])
1935 {
1936         FILE *fp;
1937         char *str = NULL;
1938         size_t size = 0;
1939         int i;
1940
1941         fp = open_memstream(&str, &size);
1942         if (!fp)
1943                 return NULL;
1944
1945         fprintf(fp, "%s", argv[0]);
1946         for (i = 1; i < argc; i++)
1947                 fprintf(fp, " %s", argv[i]);
1948         fclose(fp);
1949
1950         return str;
1951 }
1952
1953 #if !defined(BUILD_XWAYLAND)
1954 void *
1955 wet_load_xwayland(struct weston_compositor *comp)
1956 {
1957         weston_log("Attempted to load xwayland library but compositor "
1958                    "was *not* built with xwayland support!\n");
1959         return NULL;
1960 }
1961 #endif
1962
1963 static void
1964 weston_log_setup_scopes(struct weston_log_context *log_ctx,
1965                         struct weston_log_subscriber *subscriber,
1966                         const char *names)
1967 {
1968         assert(log_ctx);
1969         assert(subscriber);
1970
1971         char *tokenize = strdup(names);
1972         char *token = strtok(tokenize, ",");
1973         while (token) {
1974                 weston_log_subscribe(log_ctx, subscriber, token);
1975                 token = strtok(NULL, ",");
1976         }
1977         free(tokenize);
1978 }
1979
1980 static void
1981 weston_log_subscribe_to_scopes(struct weston_log_context *log_ctx,
1982                                struct weston_log_subscriber *logger,
1983                                const char *debug_scopes)
1984 {
1985         if (logger && debug_scopes)
1986                 weston_log_setup_scopes(log_ctx, logger, debug_scopes);
1987         else
1988                 weston_log_subscribe(log_ctx, logger, "log");
1989 }
1990
1991 WL_EXPORT
1992 int wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
1993 {
1994         struct ivi_compositor ivi = { 0 };
1995         char *cmdline;
1996         struct wl_display *display = NULL;
1997         struct wl_event_loop *loop;
1998         struct wl_event_source *signals[3] = { 0 };
1999         struct weston_config_section *section;
2000         /* Command line options */
2001         char *backend = NULL;
2002         char *socket_name = NULL;
2003         char *log = NULL;
2004         char *modules = NULL;
2005         char *option_modules = NULL;
2006         char *debug_scopes = NULL;
2007         int help = 0;
2008         int version = 0;
2009         int no_config = 0;
2010         int debug = 0;
2011         bool list_debug_scopes = false;
2012         char *config_file = NULL;
2013         struct weston_log_context *log_ctx = NULL;
2014         struct weston_log_subscriber *logger;
2015         int ret = EXIT_FAILURE;
2016         bool xwayland = false;
2017         struct sigaction action;
2018         char *renderer = NULL;
2019
2020         const struct weston_option core_options[] = {
2021                 { WESTON_OPTION_STRING, "renderer", 'r', &renderer },
2022                 { WESTON_OPTION_STRING, "backend", 'B', &backend },
2023                 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
2024                 { WESTON_OPTION_STRING, "log", 0, &log },
2025                 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
2026                 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
2027                 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
2028                 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
2029                 { WESTON_OPTION_STRING, "config", 'c', &config_file },
2030                 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
2031                 { WESTON_OPTION_STRING, "debug-scopes", 'l', &debug_scopes },
2032                 { WESTON_OPTION_STRING, "list-debug-scopes", 'L', &list_debug_scopes },
2033         };
2034
2035         wl_list_init(&ivi.outputs);
2036         wl_list_init(&ivi.saved_outputs);
2037         wl_list_init(&ivi.surfaces);
2038         wl_list_init(&ivi.pending_surfaces);
2039         wl_list_init(&ivi.popup_pending_apps);
2040         wl_list_init(&ivi.fullscreen_pending_apps);
2041         wl_list_init(&ivi.split_pending_apps);
2042         wl_list_init(&ivi.remote_pending_apps);
2043         wl_list_init(&ivi.desktop_clients);
2044         wl_list_init(&ivi.child_process_list);
2045         wl_list_init(&ivi.pending_apps);
2046
2047         /* Prevent any clients we spawn getting our stdin */
2048         os_fd_set_cloexec(STDIN_FILENO);
2049
2050         cmdline = copy_command_line(argc, argv);
2051         parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
2052
2053         if (help)
2054                 usage(EXIT_SUCCESS);
2055
2056         if (version) {
2057                 printf(PACKAGE_STRING "\n");
2058                 ret = EXIT_SUCCESS;
2059                 goto exit_signals;
2060         }
2061
2062         log_ctx = weston_log_ctx_create();
2063         if (!log_ctx) {
2064                 fprintf(stderr, "Failed to initialize weston debug framework.\n");
2065                 goto exit_signals;
2066         }
2067
2068         log_scope = weston_log_ctx_add_log_scope(log_ctx, "log",
2069                                                  "agl-compositor log\n",
2070                                                  NULL, NULL, NULL);
2071
2072         log_file_open(log);
2073         weston_log_set_handler(vlog, vlog_continue);
2074
2075         logger = weston_log_subscriber_create_log(logfile);
2076         weston_log_subscribe_to_scopes(log_ctx, logger, debug_scopes);
2077
2078         weston_log("Command line: %s\n", cmdline);
2079         free(cmdline);
2080
2081         if (load_config(&ivi.config, no_config, config_file) < 0)
2082                 goto error_signals;
2083         section = weston_config_get_section(ivi.config, "core", NULL, NULL);
2084         if (!backend) {
2085                 weston_config_section_get_string(section, "backend", &backend,
2086                                                  NULL);
2087                 if (!backend)
2088                         backend = choose_default_backend();
2089         }
2090
2091         display = wl_display_create();
2092         loop = wl_display_get_event_loop(display);
2093
2094         wl_display_set_global_filter(display,
2095                                      global_filter, &ivi);
2096
2097         /* Register signal handlers so we shut down cleanly */
2098
2099         signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2100                                               display);
2101         signals[1] = wl_event_loop_add_signal(loop, SIGUSR2, on_term_signal,
2102                                               display);
2103         signals[2] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2104                                               display);
2105
2106         /* When debugging the compositor, if use wl_event_loop_add_signal() to
2107          * catch SIGINT, the debugger can't catch it, and attempting to stop
2108          * the compositor from within the debugger results in weston exiting
2109          * cleanly.
2110          *
2111          * Instead, use the sigaction() function, which sets up the signal in a
2112          * way that gdb can successfully catch, but have the handler for SIGINT
2113          * send SIGUSR2 (xwayland uses SIGUSR1), which we catch via
2114          * wl_event_loop_add_signal().
2115          */
2116         action.sa_handler = sigint_helper;
2117         sigemptyset(&action.sa_mask);
2118         action.sa_flags = 0;
2119         sigaction(SIGINT, &action, NULL);
2120
2121         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
2122                 if (!signals[i])
2123                         goto error_signals;
2124
2125         ivi.compositor = weston_compositor_create(display, log_ctx, &ivi, test_data);
2126         if (!ivi.compositor) {
2127                 weston_log("fatal: failed to create compositor.\n");
2128                 goto error_signals;
2129         }
2130
2131         if (compositor_init_config(&ivi) < 0)
2132                 goto error_compositor;
2133
2134         if (load_backend(&ivi, &argc, argv, backend, renderer) < 0) {
2135                 weston_log("fatal: failed to create compositor backend.\n");
2136                 goto error_compositor;
2137         }
2138
2139         if (weston_compositor_backends_loaded(ivi.compositor) < 0)
2140                 goto error_compositor;
2141
2142         weston_compositor_flush_heads_changed(ivi.compositor);
2143
2144         if (ivi_desktop_init(&ivi) < 0)
2145                 goto error_compositor;
2146
2147         ivi_seat_init(&ivi);
2148
2149         /* load additional modules */
2150         weston_config_section_get_string(section, "modules", &modules, "");
2151         if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
2152                 goto error_compositor;
2153
2154         if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
2155                 goto error_compositor;
2156
2157         if (!xwayland) {
2158                 weston_config_section_get_bool(section, "xwayland", &xwayland,
2159                                                false);
2160         }
2161
2162         if (ivi_shell_init(&ivi) < 0)
2163                 goto error_compositor;
2164
2165         if (xwayland) {
2166                 if (!wet_load_xwayland(ivi.compositor))
2167                         goto error_compositor;
2168         }
2169
2170         if (ivi_policy_init(&ivi) < 0)
2171                 goto error_compositor;
2172
2173
2174         if (list_debug_scopes) {
2175                 struct weston_log_scope *nscope = NULL;
2176
2177                 weston_log("Printing available debug scopes:\n");
2178
2179                 while ((nscope = weston_log_scopes_iterate(log_ctx, nscope))) {
2180                         weston_log("\tscope name: %s, desc: %s",
2181                                         weston_log_scope_get_name(nscope),
2182                                         weston_log_scope_get_description(nscope));
2183                 }
2184
2185                 weston_log("\n");
2186
2187                 goto error_compositor;
2188         }
2189
2190         add_bindings(ivi.compositor);
2191
2192         if (ivi.remoting_api)
2193                 ivi_enable_remote_outputs(&ivi);
2194
2195         if (create_listening_socket(display, socket_name) < 0)
2196                 goto error_compositor;
2197
2198         ivi_shell_init_black_fs(&ivi);
2199
2200         ivi.compositor->exit = handle_exit;
2201
2202         weston_compositor_wake(ivi.compositor);
2203
2204         ivi_shell_create_global(&ivi);
2205
2206         ivi_launch_shell_client(&ivi, "shell-client",
2207                                 &ivi.shell_client.client);
2208         ivi_launch_shell_client(&ivi, "shell-client-ext",
2209                                 &ivi.shell_client_ext.client);
2210
2211         if (debug)
2212                 ivi_screenshooter_create(&ivi);
2213         ivi_agl_systemd_notify(&ivi);
2214
2215         wl_display_run(display);
2216
2217         ret = ivi.compositor->exit_code;
2218
2219         wl_display_destroy_clients(display);
2220
2221 error_compositor:
2222         free(backend);
2223         backend = NULL;
2224         free(modules);
2225         modules = NULL;
2226
2227         weston_compositor_destroy(ivi.compositor);
2228
2229         weston_log_scope_destroy(log_scope);
2230         log_scope = NULL;
2231
2232         weston_log_subscriber_destroy(logger);
2233         weston_log_ctx_destroy(log_ctx);
2234
2235         ivi_policy_destroy(ivi.policy);
2236
2237 error_signals:
2238         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
2239                 if (signals[i])
2240                         wl_event_source_remove(signals[i]);
2241
2242         wl_display_destroy(display);
2243
2244         log_file_close();
2245         if (ivi.config)
2246                 weston_config_destroy(ivi.config);
2247
2248 exit_signals:
2249         free(log);
2250         free(config_file);
2251         free(socket_name);
2252         free(option_modules);
2253         return ret;
2254 }