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