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