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