compositor: Do not allow vt-switching by default
[src/agl-compositor.git] / src / compositor.c
1 /*
2  * Copyright © 2012-2021 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 #include <libweston/backend-headless.h>
43 #ifdef HAVE_BACKEND_X11
44 #include <libweston/backend-x11.h>
45 #endif
46 #include <libweston/libweston.h>
47 #include <libweston/windowed-output-api.h>
48 #include <libweston/config-parser.h>
49 #include <libweston/weston-log.h>
50 #include <weston/weston.h>
51
52 #include "shared/os-compatibility.h"
53 #include "shared/helpers.h"
54
55 #include "agl-shell-server-protocol.h"
56
57 #ifdef HAVE_REMOTING
58 #include "remote.h"
59 #endif
60
61 #ifdef HAVE_WALTHAM
62 #include <waltham-transmitter/transmitter_api.h>
63 #endif
64
65 static int cached_tm_mday = -1;
66 static struct weston_log_scope *log_scope;
67
68 struct ivi_compositor *
69 to_ivi_compositor(struct weston_compositor *ec)
70 {
71         return weston_compositor_get_user_data(ec);
72 }
73
74 static void
75 handle_output_destroy(struct wl_listener *listener, void *data)
76 {
77         struct ivi_output *output;
78
79         output = wl_container_of(listener, output, output_destroy);
80         assert(output->output == data);
81
82         if (output->fullscreen_view.fs->view) {
83                 weston_surface_destroy(output->fullscreen_view.fs->view->surface);
84                 output->fullscreen_view.fs->view = NULL;
85         }
86
87         output->output = NULL;
88         wl_list_remove(&output->output_destroy.link);
89 }
90
91 struct ivi_output *
92 to_ivi_output(struct weston_output *o)
93 {
94         struct wl_listener *listener;
95         struct ivi_output *output;
96
97         listener = weston_output_get_destroy_listener(o, handle_output_destroy);
98         output = wl_container_of(listener, output, output_destroy);
99
100         return output;
101 }
102
103 static void
104 ivi_output_configure_app_id(struct ivi_output *ivi_output)
105 {
106         if (ivi_output->config) {
107                 if (ivi_output->app_id != NULL)
108                         return;
109
110                 weston_config_section_get_string(ivi_output->config,
111                                                  "agl-shell-app-id",
112                                                  &ivi_output->app_id,
113                                                  NULL);
114
115                 if (ivi_output->app_id == NULL)
116                         return;
117
118                 weston_log("Will place app_id %s on output %s\n",
119                                 ivi_output->app_id, ivi_output->name);
120         }
121 }
122
123 static struct ivi_output *
124 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
125                   struct weston_config_section *config)
126 {
127         struct ivi_output *output = NULL;
128         wl_list_for_each(output, &ivi->outputs, link) {
129                 if (strcmp(output->name, name) == 0) {
130                         free(name);
131                         return output;
132                 }
133         }
134
135         output = zalloc(sizeof *output);
136         if (!output) {
137                 free(name);
138                 return NULL;
139         }
140
141         output->ivi = ivi;
142         output->name = name;
143         output->config = config;
144
145         output->output = weston_compositor_create_output(ivi->compositor, name);
146         if (!output->output) {
147                 free(output->name);
148                 free(output);
149                 return NULL;
150         }
151
152         output->output_destroy.notify = handle_output_destroy;
153         weston_output_add_destroy_listener(output->output,
154                                            &output->output_destroy);
155
156         wl_list_insert(&ivi->outputs, &output->link);
157         ivi_output_configure_app_id(output);
158         return output;
159 }
160
161 static int
162 count_heads(struct weston_output *output)
163 {
164         struct weston_head *iter = NULL;
165         int n = 0;
166
167         while ((iter = weston_output_iterate_heads(output, iter)))
168                 ++n;
169
170         return n;
171 }
172
173 static void
174 handle_head_destroy(struct wl_listener *listener, void *data)
175 {
176         struct weston_head *head = data;
177         struct weston_output *output;
178
179         wl_list_remove(&listener->link);
180         free(listener);
181
182         output = weston_head_get_output(head);
183
184         /* On shutdown path, the output might be already gone. */
185         if (!output)
186                 return;
187
188         /* We're the last head */
189         if (count_heads(output) <= 1)
190                 weston_output_destroy(output);
191 }
192
193 static void
194 add_head_destroyed_listener(struct weston_head *head)
195 {
196         /* We already have a destroy listener */
197         if (weston_head_get_destroy_listener(head, handle_head_destroy))
198                 return;
199
200         struct wl_listener *listener = zalloc(sizeof *listener);
201         if (!listener)
202                 return;
203
204         listener->notify = handle_head_destroy;
205         weston_head_add_destroy_listener(head, listener);
206 }
207
208 static int
209 drm_configure_output(struct ivi_output *output)
210 {
211         struct ivi_compositor *ivi = output->ivi;
212         struct weston_config_section *section = output->config;
213         enum weston_drm_backend_output_mode mode =
214                 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
215         char *modeline = NULL;
216         char *gbm_format = NULL;
217         char *seat = NULL;
218
219         if (section) {
220                 char *m;
221                 weston_config_section_get_string(section, "mode", &m, "preferred");
222
223                 /* This should have been handled earlier */
224                 assert(strcmp(m, "off") != 0);
225
226                 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
227                         mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
228                 } else if (strcmp(m, "preferred") != 0) {
229                         modeline = m;
230                         m = NULL;
231                 }
232                 free(m);
233
234                 weston_config_section_get_string(section, "gbm-format",
235                                                  &gbm_format, NULL);
236
237                 weston_config_section_get_string(section, "seat", &seat, "");
238         }
239
240         if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
241                 weston_log("Cannot configure output using weston_drm_output_api.\n");
242                 free(modeline);
243                 return -1;
244         }
245         free(modeline);
246
247         ivi->drm_api->set_gbm_format(output->output, gbm_format);
248         free(gbm_format);
249
250         ivi->drm_api->set_seat(output->output, seat);
251         free(seat);
252
253         return 0;
254 }
255
256 #define WINDOWED_DEFAULT_WIDTH 1024
257 #define WINDOWED_DEFAULT_HEIGHT 768
258
259 static int
260 windowed_configure_output(struct ivi_output *output)
261 {
262         struct ivi_compositor *ivi = output->ivi;
263         struct weston_config_section *section = output->config;
264         int width = WINDOWED_DEFAULT_WIDTH;
265         int height = WINDOWED_DEFAULT_HEIGHT;
266
267         if (section) {
268                 char *mode;
269
270                 weston_config_section_get_string(section, "mode", &mode, NULL);
271                 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
272                         weston_log("Invalid mode for output %s. Using defaults.\n",
273                                    output->name);
274                         width = WINDOWED_DEFAULT_WIDTH;
275                         height = WINDOWED_DEFAULT_HEIGHT;
276                 }
277                 free(mode);
278         }
279
280         if (ivi->cmdline.width)
281                 width = ivi->cmdline.width;
282         if (ivi->cmdline.height)
283                 height = ivi->cmdline.height;
284         if (ivi->cmdline.scale)
285                 weston_output_set_scale(output->output, ivi->cmdline.scale);
286
287         if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
288                 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
289                            output->name);
290                 return -1;
291         }
292
293         weston_log("Configured windowed_output_api to %dx%d\n", width, height);
294
295         return 0;
296 }
297
298 static int
299 parse_transform(const char *transform, uint32_t *out)
300 {
301         static const struct { const char *name; uint32_t token; } transforms[] = {
302                 { "normal",     WL_OUTPUT_TRANSFORM_NORMAL },
303                 { "90",         WL_OUTPUT_TRANSFORM_90 },
304                 { "180",        WL_OUTPUT_TRANSFORM_180 },
305                 { "270",        WL_OUTPUT_TRANSFORM_270 },
306                 { "flipped",    WL_OUTPUT_TRANSFORM_FLIPPED },
307                 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
308                 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
309                 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
310         };
311
312         for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
313                 if (strcmp(transforms[i].name, transform) == 0) {
314                         *out = transforms[i].token;
315                         return 0;
316                 }
317
318         *out = WL_OUTPUT_TRANSFORM_NORMAL;
319         return -1;
320 }
321
322 static int
323 configure_output(struct ivi_output *output)
324 {
325         struct ivi_compositor *ivi = output->ivi;
326         struct weston_config_section *section = output->config;
327         int32_t scale = 1;
328         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
329
330         /*
331          * This can happen with the wayland backend with 'sprawl'. The config
332          * is hard-coded, so we don't need to do anything.
333          */
334         if (!ivi->drm_api && !ivi->window_api)
335                 return 0;
336
337         if (section) {
338                 char *t;
339
340                 weston_config_section_get_int(section, "scale", &scale, 1);
341                 weston_config_section_get_string(section, "transform", &t, "normal");
342                 if (parse_transform(t, &transform) < 0)
343                         weston_log("Invalid transform \"%s\" for output %s\n",
344                                    t, output->name);
345                 free(t);
346         }
347
348         weston_output_set_scale(output->output, scale);
349         weston_output_set_transform(output->output, transform);
350
351         if (ivi->drm_api)
352                 return drm_configure_output(output);
353         else
354                 return windowed_configure_output(output);
355 }
356
357 /*
358  * Reorgainizes the output's add array into two sections.
359  * add[0..ret-1] are the heads that failed to get attached.
360  * add[ret..add_len] are the heads that were successfully attached.
361  *
362  * The order between elements in each section is stable.
363  */
364 static size_t
365 try_attach_heads(struct ivi_output *output)
366 {
367         size_t fail_len = 0;
368
369         for (size_t i = 0; i < output->add_len; ++i) {
370                 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
371                         struct weston_head *tmp = output->add[i];
372                         memmove(&output->add[fail_len + 1], output->add[fail_len],
373                                 sizeof output->add[0] * (i - fail_len));
374                         output->add[fail_len++] = tmp;
375                 }
376         }
377
378         return fail_len;
379 }
380
381 /*
382  * Like try_attach_heads, this reorganizes the output's add array into a failed
383  * and successful section.
384  * i is the number of heads that already failed the previous step.
385  */
386 static size_t
387 try_enable_output(struct ivi_output *output, size_t i)
388 {
389         for (; i < output->add_len; ++i) {
390                 struct weston_head *head;
391
392                 if (weston_output_enable(output->output) == 0)
393                         break;
394
395                 head = output->add[output->add_len - 1];
396                 memmove(&output->add[i + 1], &output->add[i],
397                         sizeof output->add[0] * (output->add_len - i));
398                 output->add[i] = head;
399
400                 weston_head_detach(head);
401         }
402
403         return i;
404 }
405
406 static int
407 try_attach_enable_heads(struct ivi_output *output)
408 {
409         size_t fail_len;
410         assert(!output->output->enabled);
411
412         fail_len = try_attach_heads(output);
413
414         if (configure_output(output) < 0)
415                 return -1;
416
417         fail_len = try_enable_output(output, fail_len);
418
419         /* All heads failed to be attached */
420         if (fail_len == output->add_len)
421                 return -1;
422
423         /* For each successful head attached */
424         for (size_t i = fail_len; i < output->add_len; ++i)
425                 add_head_destroyed_listener(output->add[i]);
426
427         output->add_len = fail_len;
428         return 0;
429 }
430
431 static int
432 process_output(struct ivi_output *output)
433 {
434         if (output->output->enabled) {
435                 output->add_len = try_attach_heads(output);
436                 return output->add_len == 0 ? 0 : -1;
437         }
438
439         return try_attach_enable_heads(output);
440 }
441
442 static void
443 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
444 {
445         struct weston_output *output;
446         struct ivi_output *ivi_output;
447         struct wl_listener *listener;
448
449         output = weston_head_get_output(head);
450         assert(output);
451
452         listener = weston_output_get_destroy_listener(output,
453                                                       handle_output_destroy);
454         assert(listener);
455
456         ivi_output = wl_container_of(listener, ivi_output, output_destroy);
457         assert(ivi_output->output == output);
458
459         weston_head_detach(head);
460         if (count_heads(ivi_output->output) == 0) {
461                 weston_output_disable(ivi_output->output);
462         }
463 }
464
465 static struct weston_config_section *
466 find_controlling_output_config(struct weston_config *config,
467                                const char *name)
468 {
469         struct weston_config_section *section;
470         char *same_as;
471         int depth = 0;
472
473         same_as = strdup(name);
474         do {
475                 section = weston_config_get_section(config, "output",
476                                                     "name", same_as);
477                 if (!section && depth > 0)
478                         weston_log("Configuration error: output section reffered"
479                                    "to by same-as=%s not found.\n", same_as);
480                 free(same_as);
481
482                 if (!section)
483                         return NULL;
484
485                 if (depth++ > 8) {
486                         weston_log("Configuration error: same-as nested too "
487                                    "deep for output '%s'.\n", name);
488                         return NULL;
489                 }
490
491                 weston_config_section_get_string(section, "same-as",
492                                                  &same_as, NULL);
493         } while (same_as);
494
495         return section;
496 }
497
498 static void
499 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
500 {
501         const char *name = weston_head_get_name(head);
502         struct weston_config_section *section;
503         struct ivi_output *output;
504         char *output_name = NULL;
505
506         section = find_controlling_output_config(ivi->config, name);
507         if (section) {
508                 char *mode;
509
510                 weston_config_section_get_string(section, "mode", &mode, NULL);
511                 if (mode && strcmp(mode, "off") == 0) {
512                         free(mode);
513                         return;
514                 }
515                 free(mode);
516
517                 weston_config_section_get_string(section, "name",
518                                                  &output_name, NULL);
519         } else {
520                 output_name = strdup(name);
521         }
522
523         if (!output_name)
524                 return;
525
526         output = ivi_ensure_output(ivi, output_name, section);
527         if (!output)
528                 return;
529
530         if (output->add_len >= ARRAY_LENGTH(output->add))
531                 return;
532
533         output->add[output->add_len++] = head;
534 }
535
536 static void
537 heads_changed(struct wl_listener *listener, void *arg)
538 {
539         struct weston_compositor *compositor = arg;
540         struct weston_head *head = NULL;
541         struct ivi_compositor *ivi = to_ivi_compositor(compositor);
542         struct ivi_output *output;
543
544         while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
545                 bool connected = weston_head_is_connected(head);
546                 bool enabled = weston_head_is_enabled(head);
547                 bool changed = weston_head_is_device_changed(head);
548                 bool non_desktop = weston_head_is_non_desktop(head);
549
550                 if (connected && !enabled && !non_desktop)
551                         head_prepare_enable(ivi, head);
552                 else if (!connected && enabled)
553                         head_disable(ivi, head);
554                 else if (enabled && changed)
555                         weston_log("Detected a monitor change on head '%s', "
556                                    "not bothering to do anything about it.\n",
557                                    weston_head_get_name(head));
558
559                 weston_head_reset_device_changed(head);
560         }
561
562         wl_list_for_each(output, &ivi->outputs, link) {
563                 if (output->add_len == 0)
564                         continue;
565
566                 if (process_output(output) < 0) {
567                         output->add_len = 0;
568                         ivi->init_failed = true;
569                 }
570         }
571 }
572
573 #ifdef HAVE_WALTHAM
574 static int
575 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
576 {
577         struct weston_compositor *compositor = ivi->compositor;
578         int (*module_init)(struct weston_compositor *wc);
579
580         module_init = weston_load_module("waltham-transmitter.so",
581                                          "wet_module_init");
582         if (!module_init)
583                 return -1;
584
585         if (module_init(compositor) < 0)
586                 return -1;
587
588         ivi->waltham_transmitter_api = weston_get_transmitter_api(compositor);
589         if (!ivi->waltham_transmitter_api) {
590                 weston_log("Failed to load waltham-transmitter plugin.\n");
591                 return -1;
592         }
593
594         weston_log("waltham-transmitter plug-in loaded\n");
595         return 0;
596 }
597 #else
598 static int
599 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
600 {
601         return -1;
602 }
603 #endif
604
605 #ifdef HAVE_REMOTING
606 static int
607 drm_backend_remoted_output_configure(struct weston_output *output,
608                                      struct weston_config_section *section,
609                                      char *modeline,
610                                      const struct weston_remoting_api *api)
611 {
612         char *gbm_format = NULL;
613         char *seat = NULL;
614         char *host = NULL;
615         char *pipeline = NULL;
616         int port, ret;
617         int32_t scale = 1;
618         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
619         char *trans;
620
621         ret = api->set_mode(output, modeline);
622         if (ret < 0) {
623                 weston_log("Cannot configure an output \"%s\" using "
624                                 "weston_remoting_api. Invalid mode\n",
625                                 output->name);
626                 return -1;
627         }
628
629         weston_config_section_get_int(section, "scale", &scale, 1);
630         weston_output_set_scale(output, scale);
631
632         weston_config_section_get_string(section, "transform", &trans, "normal");
633         if (parse_transform(trans, &transform) < 0) {
634                 weston_log("Invalid transform \"%s\" for output %s\n",
635                            trans, output->name);
636         }
637         weston_output_set_transform(output, transform);
638
639         weston_config_section_get_string(section, "gbm-format",
640                                          &gbm_format, NULL);
641         api->set_gbm_format(output, gbm_format);
642         free(gbm_format);
643
644         weston_config_section_get_string(section, "seat", &seat, "");
645
646         api->set_seat(output, seat);
647         free(seat);
648
649         weston_config_section_get_string(section, "gst-pipeline", &pipeline,
650                         NULL);
651         if (pipeline) {
652                 api->set_gst_pipeline(output, pipeline);
653                 free(pipeline);
654                 return 0;
655         }
656
657         weston_config_section_get_string(section, "host", &host, NULL);
658         weston_config_section_get_int(section, "port", &port, 0);
659         if (!host || port <= 0 || 65533 < port) {
660                 weston_log("Cannot configure an output \"%s\". "
661                                 "Need to specify gst-pipeline or "
662                                 "host and port (1-65533).\n", output->name);
663         }
664         api->set_host(output, host);
665         free(host);
666         api->set_port(output, port);
667
668         return 0;
669 }
670
671
672 static int
673 remote_output_init(struct ivi_output *ivi_output,
674                    struct weston_compositor *compositor,
675                    struct weston_config_section *section,
676                    const struct weston_remoting_api *api)
677 {
678         char *output_name, *modeline = NULL;
679         int ret = -1;
680
681         weston_config_section_get_string(section, "name", &output_name, NULL);
682         if (!output_name)
683                 return ret;
684
685         weston_config_section_get_string(section, "mode", &modeline, "off");
686         if (strcmp(modeline, "off") == 0)
687                 goto err;
688
689         ivi_output->output = api->create_output(compositor, output_name);
690         if (!ivi_output->output) {
691                 weston_log("Cannot create remoted output \"%s\".\n",
692                                 output_name);
693                 goto err;
694         }
695
696         ret = drm_backend_remoted_output_configure(ivi_output->output, section,
697                                                    modeline, api);
698         if (ret < 0) {
699                 weston_log("Cannot configure remoted output \"%s\".\n",
700                                 output_name);
701                 goto err;
702         }
703
704         if (weston_output_enable(ivi_output->output) < 0) {
705                 weston_log("Enabling remoted output \"%s\" failed.\n",
706                                 output_name);
707                 goto err;
708         }
709
710         free(modeline);
711         free(output_name);
712         weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
713
714         return 0;
715
716 err:
717         free(modeline);
718         free(output_name);
719         if (ivi_output->output)
720                 weston_output_destroy(ivi_output->output);
721
722         return ret;
723 }
724
725 static void
726 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
727 {
728         struct weston_config_section *remote_section = NULL;
729         const char *section_name;
730         struct weston_config *config = ivi->config;
731
732         while (weston_config_next_section(config, &remote_section, &section_name)) {
733                 if (strcmp(section_name, "remote-output"))
734                         continue;
735
736                 struct ivi_output *ivi_output = NULL;
737                 bool output_found = false;
738                 char *_name = NULL;
739
740                 weston_config_section_get_string(remote_section,
741                                                  "name", &_name, NULL);
742                 wl_list_for_each(ivi_output, &ivi->outputs, link) {
743                         if (!strcmp(ivi_output->name, _name)) {
744                                 output_found = true;
745                                 break;
746                         }
747                 }
748
749                 if (output_found) {
750                         free(_name);
751                         continue;
752                 }
753
754                 ivi_output = zalloc(sizeof(*ivi_output));
755
756                 ivi_output->ivi = ivi;
757                 ivi_output->name = _name;
758                 ivi_output->config = remote_section;
759                 ivi_output->type = OUTPUT_REMOTE;
760
761                 if (remote_output_init(ivi_output, ivi->compositor,
762                                        remote_section, ivi->remoting_api)) {
763                         free(ivi_output->name);
764                         free(ivi_output);
765                         continue;
766                 }
767
768                 ivi_output->output_destroy.notify = handle_output_destroy;
769                 weston_output_add_destroy_listener(ivi_output->output,
770                                                    &ivi_output->output_destroy);
771
772                 wl_list_insert(&ivi->outputs, &ivi_output->link);
773                 ivi_output_configure_app_id(ivi_output);
774         }
775 }
776
777 static void
778 ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
779 {
780         struct weston_config_section *transmitter_section = NULL;
781         const char *sect_name;
782         struct weston_config *config = ivi->config;
783
784         while (weston_config_next_section(config, &transmitter_section, &sect_name)) {
785                 if (strcmp(sect_name, "transmitter-output"))
786                         continue;
787
788                 struct ivi_output *ivi_output = NULL;
789                 bool output_found = false;
790                 char *_name = NULL;
791
792                 weston_config_section_get_string(transmitter_section,
793                                 "name", &_name, NULL);
794                 wl_list_for_each(ivi_output, &ivi->outputs, link) {
795                         if (!strcmp(ivi_output->name, _name)) {
796                                 output_found = true;
797                                 break;
798                         }
799                 }
800
801                 if (output_found) {
802                         free(_name);
803                         continue;
804                 }
805
806                 ivi_output = zalloc(sizeof(*ivi_output));
807
808                 ivi_output->ivi = ivi;
809                 ivi_output->name = _name;
810                 ivi_output->config = transmitter_section;
811
812                 if (remote_output_init(ivi_output, ivi->compositor,
813                                         transmitter_section, ivi->remoting_api)) {
814                         free(ivi_output->name);
815                         free(ivi_output);
816                         continue;
817                 }
818
819                 ivi_output->type = OUTPUT_WALTHAM;
820                 ivi_output->output_destroy.notify = handle_output_destroy;
821                 weston_output_add_destroy_listener(ivi_output->output,
822                                 &ivi_output->output_destroy);
823
824                 wl_list_insert(&ivi->outputs, &ivi_output->link);
825                 ivi_output_configure_app_id(ivi_output);
826         }
827 }
828
829 static int
830 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
831 {
832         struct weston_compositor *compositor = ivi->compositor;
833         int (*module_init)(struct weston_compositor *wc);
834
835         module_init = weston_load_module("remoting-plugin.so",
836                                          "weston_module_init");
837         if (!module_init)
838                 return -1;
839
840         if (module_init(compositor) < 0)
841                 return -1;
842
843         ivi->remoting_api = weston_remoting_get_api(compositor);
844         if (!ivi->remoting_api)
845                 return -1;
846         return 0;
847 }
848 #else
849 static int
850 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
851 {
852         return -1;
853 }
854 #endif
855
856 static int
857 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
858 {
859         struct weston_drm_backend_config config = {
860                 .base = {
861                         .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
862                         .struct_size = sizeof config,
863                 },
864         };
865         struct weston_config_section *section;
866         int use_current_mode = 0;
867         int use_pixman = 0;
868         bool use_shadow;
869         int ret;
870
871         const struct weston_option options[] = {
872                 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
873                 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
874                 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
875                 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
876                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
877         };
878
879         parse_options(options, ARRAY_LENGTH(options), argc, argv);
880         config.use_pixman = use_pixman;
881         ivi->cmdline.use_current_mode = use_current_mode;
882
883         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
884         weston_config_section_get_string(section, "gbm-format",
885                                          &config.gbm_format, NULL);
886         weston_config_section_get_uint(section, "pageflip-timeout",
887                                        &config.pageflip_timeout, 0);
888         weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
889         config.use_pixman_shadow = use_shadow;
890
891         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
892                                              &config.base);
893         if (ret < 0)
894                 return ret;
895
896         ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
897         if (!ivi->drm_api) {
898                 weston_log("Cannot use drm output api.\n");
899                 ret = -1;
900                 goto error;
901         }
902
903         load_remoting_plugin(ivi, ivi->config);
904         load_waltham_plugin(ivi, ivi->config);
905
906 error:
907         free(config.gbm_format);
908         free(config.seat_id);
909         return ret;
910 }
911
912 static void
913 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
914                               bool *use_pixman, bool *fullscreen, int *output_count)
915 {
916         struct weston_config_section *section;
917         bool pixman;
918         int fs = 0;
919
920         const struct weston_option options[] = {
921                 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
922                 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
923                 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
924                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
925                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
926                 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
927         };
928
929         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
930         weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
931
932         *output_count = 1;
933         parse_options(options, ARRAY_LENGTH(options), argc, argv);
934         *use_pixman = pixman;
935         *fullscreen = fs;
936 }
937
938 static int
939 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
940                         const char *match_prefix, const char *name_prefix)
941 {
942         struct weston_config_section *section = NULL;
943         const char *section_name;
944         char *default_output = NULL;
945         int i = 0;
946         size_t match_len = strlen(match_prefix);
947
948         while (weston_config_next_section(ivi->config, &section, &section_name)) {
949                 char *output_name;
950
951                 if (i >= output_count)
952                         break;
953
954                 if (strcmp(section_name, "output") != 0)
955                         continue;
956
957                 weston_config_section_get_string(section, "name", &output_name, NULL);
958                 if (output_name == NULL)
959                         continue;
960                 if (strncmp(output_name, match_prefix, match_len) != 0) {
961                         free(output_name);
962                         continue;
963                 }
964
965                 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
966                         free(output_name);
967                         return -1;
968                 }
969
970                 free(output_name);
971                 ++i;
972         }
973
974         for (; i < output_count; ++i) {
975                 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
976                         return -1;
977
978                 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
979                         free(default_output);
980                         return -1;
981                 }
982
983                 free(default_output);
984         }
985
986         return 0;
987 }
988
989 static int
990 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
991 {
992         struct weston_wayland_backend_config config = {
993                 .base = {
994                         .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
995                         .struct_size = sizeof config,
996                 },
997         };
998         struct weston_config_section *section;
999         int sprawl = 0;
1000         int output_count;
1001         int ret;
1002
1003         const struct weston_option options[] = {
1004                 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1005                 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1006         };
1007
1008         windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1009                                       &config.fullscreen, &output_count);
1010
1011         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1012         config.sprawl = sprawl;
1013
1014         section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1015         weston_config_section_get_string(section, "cursor-theme",
1016                                          &config.cursor_theme, NULL);
1017         weston_config_section_get_int(section, "cursor-size",
1018                                       &config.cursor_size, 32);
1019
1020         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1021                                              &config.base);
1022
1023         free(config.cursor_theme);
1024         free(config.display_name);
1025
1026         if (ret < 0)
1027                 return ret;
1028
1029         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1030
1031         /*
1032          * We will just assume if load_backend() finished cleanly and
1033          * windowed_output_api is not present that wayland backend is started
1034          * with --sprawl or runs on fullscreen-shell. In this case, all values
1035          * are hardcoded, so nothing can be configured; simply create and
1036          * enable an output.
1037          */
1038         if (ivi->window_api == NULL)
1039                 return 0;
1040
1041         return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1042 }
1043
1044 #ifdef HAVE_BACKEND_X11
1045 static int
1046 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1047 {
1048         struct weston_x11_backend_config config = {
1049                 .base = {
1050                         .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1051                         .struct_size = sizeof config,
1052                 },
1053         };
1054         int no_input = 0;
1055         int output_count;
1056         int ret;
1057
1058         const struct weston_option options[] = {
1059                { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1060         };
1061
1062         windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1063                                       &config.fullscreen, &output_count);
1064
1065         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1066         config.no_input = no_input;
1067
1068         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1069                                              &config.base);
1070
1071         if (ret < 0)
1072                 return ret;
1073
1074         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1075         if (!ivi->window_api) {
1076                 weston_log("Cannot use weston_windowed_output_api.\n");
1077                 return -1;
1078         }
1079
1080         return windowed_create_outputs(ivi, output_count, "X", "screen");
1081 }
1082 #else
1083 static int
1084 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1085 {
1086         return -1;
1087 }
1088 #endif
1089
1090 static int
1091 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1092 {
1093         struct weston_headless_backend_config config = {};
1094         int ret = 0;
1095
1096         bool use_pixman;
1097         bool fullscreen;
1098         bool use_gl;
1099         int output_count;
1100
1101         struct weston_compositor *c = ivi->compositor;
1102
1103         const struct weston_option options[] = {
1104                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1105                 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &use_gl },
1106         };
1107
1108         windowed_parse_common_options(ivi, argc, argv, &use_pixman,
1109                         &fullscreen, &output_count);
1110
1111         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1112         config.use_pixman = use_pixman;
1113         config.use_gl = use_gl;
1114
1115         config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1116         config.base.struct_size = sizeof(struct weston_headless_backend_config);
1117
1118         /* load the actual headless-backend and configure it */
1119         ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1120                                              &config.base);
1121         if (ret < 0)
1122                 return ret;
1123
1124         ivi->window_api = weston_windowed_output_get_api(c);
1125         if (!ivi->window_api) {
1126                 weston_log("Cannot use weston_windowed_output_api.\n");
1127                 return -1;
1128         }
1129
1130         if (ivi->window_api->create_head(c, "headless") < 0) {
1131                 weston_log("Cannot create headless back-end\n");
1132                 return -1;
1133         }
1134
1135         return 0;
1136 }
1137
1138 static int
1139 load_backend(struct ivi_compositor *ivi, const char *backend,
1140              int *argc, char *argv[])
1141 {
1142         if (strcmp(backend, "drm-backend.so") == 0) {
1143                 return load_drm_backend(ivi, argc, argv);
1144         } else if (strcmp(backend, "wayland-backend.so") == 0) {
1145                 return load_wayland_backend(ivi, argc, argv);
1146         } else if (strcmp(backend, "x11-backend.so") == 0) {
1147                 return load_x11_backend(ivi, argc, argv);
1148         } else if (strcmp(backend, "headless-backend.so") == 0) {
1149                 return load_headless_backend(ivi, argc, argv);
1150         }
1151
1152         weston_log("fatal: unknown backend '%s'.\n", backend);
1153         return -1;
1154 }
1155
1156 static int
1157 load_modules(struct ivi_compositor *ivi, const char *modules,
1158              int *argc, char *argv[], bool *xwayland)
1159 {
1160         const char *p, *end;
1161         char buffer[256];
1162         int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1163
1164         if (modules == NULL)
1165                 return 0;
1166
1167         p = modules;
1168         while (*p) {
1169                 end = strchrnul(p, ',');
1170                 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1171
1172                 if (strstr(buffer, "xwayland.so")) {
1173                         weston_log("Xwayland plug-in not supported!\n");
1174                         p = end;
1175                         while (*p == ',')
1176                                 p++;
1177                         continue;
1178                 }
1179
1180                 if (strstr(buffer, "systemd-notify.so")) {
1181                         weston_log("systemd-notify plug-in already loaded!\n");
1182                         p = end;
1183                         while (*p == ',')
1184                                 p++;
1185                         continue;
1186                 }
1187
1188                 module_init = weston_load_module(buffer, "wet_module_init");
1189                 if (!module_init)
1190                         return -1;
1191
1192                 if (module_init(ivi->compositor, *argc, argv) < 0)
1193                         return -1;
1194
1195                 p = end;
1196                 while (*p == ',')
1197                         p++;
1198         }
1199
1200         return 0;
1201 }
1202
1203
1204 static char *
1205 choose_default_backend(void)
1206 {
1207         char *backend = NULL;
1208
1209         if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1210                 backend = strdup("wayland-backend.so");
1211         else if (getenv("DISPLAY"))
1212                 backend = strdup("x11-backend.so");
1213         else
1214                 backend = strdup("drm-backend.so");
1215
1216         return backend;
1217 }
1218
1219 static int
1220 compositor_init_config(struct weston_compositor *compositor,
1221                        struct weston_config *config)
1222 {
1223         struct xkb_rule_names xkb_names;
1224         struct weston_config_section *section;
1225         int repaint_msec;
1226         bool vt_switching;
1227         bool require_input;
1228
1229         /* agl-compositor.ini [keyboard] */
1230         section = weston_config_get_section(config, "keyboard", NULL, NULL);
1231         weston_config_section_get_string(section, "keymap_rules",
1232                                          (char **) &xkb_names.rules, NULL);
1233         weston_config_section_get_string(section, "keymap_model",
1234                                          (char **) &xkb_names.model, NULL);
1235         weston_config_section_get_string(section, "keymap_layout",
1236                                          (char **) &xkb_names.layout, NULL);
1237         weston_config_section_get_string(section, "keymap_variant",
1238                                          (char **) &xkb_names.variant, NULL);
1239         weston_config_section_get_string(section, "keymap_options",
1240                                          (char **) &xkb_names.options, NULL);
1241
1242         if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1243                 return -1;
1244
1245         weston_config_section_get_int(section, "repeat-rate",
1246                                       &compositor->kb_repeat_rate, 40);
1247         weston_config_section_get_int(section, "repeat-delay",
1248                                       &compositor->kb_repeat_delay, 400);
1249
1250         weston_config_section_get_bool(section, "vt-switching",
1251                                        &vt_switching, false);
1252         compositor->vt_switching = vt_switching;
1253
1254         /* agl-compositor.ini [core] */
1255         section = weston_config_get_section(config, "core", NULL, NULL);
1256
1257         weston_config_section_get_bool(section, "require-input", &require_input, true);
1258         compositor->require_input = require_input;
1259
1260         weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1261                                       compositor->repaint_msec);
1262         if (repaint_msec < -10 || repaint_msec > 1000) {
1263                 weston_log("Invalid repaint_window value in config: %d\n",
1264                            repaint_msec);
1265         } else {
1266                 compositor->repaint_msec = repaint_msec;
1267         }
1268         weston_log("Output repaint window is %d ms maximum.\n",
1269                    compositor->repaint_msec);
1270
1271         return 0;
1272 }
1273
1274 struct ivi_surface *
1275 to_ivi_surface(struct weston_surface *surface)
1276 {
1277         struct weston_desktop_surface *dsurface;
1278
1279         dsurface = weston_surface_get_desktop_surface(surface);
1280         if (!dsurface)
1281                 return NULL;
1282
1283         return weston_desktop_surface_get_user_data(dsurface);
1284 }
1285
1286 static void
1287 activate_binding(struct weston_seat *seat,
1288                  struct weston_view *focus_view)
1289 {
1290         struct weston_surface *focus = focus_view->surface;
1291         struct weston_surface *main_surface =
1292                 weston_surface_get_main_surface(focus);
1293         struct ivi_surface *surface;
1294
1295         surface = to_ivi_surface(main_surface);
1296         if (!surface)
1297                 return;
1298
1299         weston_seat_set_keyboard_focus(seat, focus);
1300 }
1301
1302 static void
1303 click_to_activate_binding(struct weston_pointer *pointer,
1304                           const struct timespec *time,
1305                           uint32_t button, void *data)
1306 {
1307         if (pointer->grab != &pointer->default_grab)
1308                 return;
1309         if (pointer->focus == NULL)
1310                 return;
1311
1312         activate_binding(pointer->seat, pointer->focus);
1313 }
1314
1315 static void
1316 touch_to_activate_binding(struct weston_touch *touch,
1317                           const struct timespec *time,
1318                           void *data)
1319 {
1320         if (touch->grab != &touch->default_grab)
1321                 return;
1322         if (touch->focus == NULL)
1323                 return;
1324
1325         activate_binding(touch->seat, touch->focus);
1326 }
1327
1328 static void
1329 add_bindings(struct weston_compositor *compositor)
1330 {
1331         weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1332                                              click_to_activate_binding,
1333                                              NULL);
1334         weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1335                                              click_to_activate_binding,
1336                                              NULL);
1337         weston_compositor_add_touch_binding(compositor, 0,
1338                                             touch_to_activate_binding,
1339                                             NULL);
1340 }
1341
1342 static int
1343 create_listening_socket(struct wl_display *display, const char *socket_name)
1344 {
1345         if (socket_name) {
1346                 if (wl_display_add_socket(display, socket_name)) {
1347                         weston_log("fatal: failed to add socket: %s\n",
1348                                    strerror(errno));
1349                         return -1;
1350                 }
1351         } else {
1352                 socket_name = wl_display_add_socket_auto(display);
1353                 if (!socket_name) {
1354                         weston_log("fatal: failed to add socket: %s\n",
1355                                    strerror(errno));
1356                         return -1;
1357                 }
1358         }
1359
1360         setenv("WAYLAND_DISPLAY", socket_name, 1);
1361
1362         return 0;
1363 }
1364
1365 static bool
1366 global_filter(const struct wl_client *client, const struct wl_global *global,
1367               void *data)
1368 {
1369         return true;
1370 }
1371
1372 static int
1373 load_config(struct weston_config **config, bool no_config,
1374             const char *config_file)
1375 {
1376         const char *file = "agl-compositor.ini";
1377         const char *full_path;
1378
1379         if (config_file)
1380                 file = config_file;
1381
1382         if (!no_config)
1383                 *config = weston_config_parse(file);
1384
1385         if (*config) {
1386                 full_path = weston_config_get_full_path(*config);
1387
1388                 weston_log("Using config file '%s'.\n", full_path);
1389                 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1390
1391                 return 0;
1392         }
1393
1394         if (config_file && !no_config) {
1395                 weston_log("fatal: error opening or reading config file '%s'.\n",
1396                         config_file);
1397
1398                 return -1;
1399         }
1400
1401         weston_log("Starting with no config file.\n");
1402         setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1403
1404         return 0;
1405 }
1406
1407 static FILE *logfile;
1408
1409 static char *
1410 log_timestamp(char *buf, size_t len)
1411 {
1412         struct timeval tv;
1413         struct tm *brokendown_time;
1414         char datestr[128];
1415         char timestr[128];
1416
1417         gettimeofday(&tv, NULL);
1418
1419         brokendown_time = localtime(&tv.tv_sec);
1420         if (brokendown_time == NULL) {
1421                 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1422                 return buf;
1423         }
1424
1425         memset(datestr, 0, sizeof(datestr));
1426         if (brokendown_time->tm_mday != cached_tm_mday) {
1427                 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1428                                 brokendown_time);
1429                 cached_tm_mday = brokendown_time->tm_mday;
1430         }
1431
1432         strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1433         /* if datestr is empty it prints only timestr*/
1434         snprintf(buf, len, "%s[%s.%03li]", datestr,
1435                         timestr, (tv.tv_usec / 1000));
1436
1437         return buf;
1438 }
1439
1440 static void
1441 custom_handler(const char *fmt, va_list arg)
1442 {
1443         char timestr[512];
1444
1445         weston_log_scope_printf(log_scope, "%s libwayland: ",
1446                         log_timestamp(timestr, sizeof(timestr)));
1447         weston_log_scope_vprintf(log_scope, fmt, arg);
1448 }
1449
1450 static void
1451 log_file_open(const char *filename)
1452 {
1453         wl_log_set_handler_server(custom_handler);
1454
1455         if (filename)
1456                 logfile = fopen(filename, "a");
1457
1458         if (!logfile) {
1459                 logfile = stderr;
1460         } else {
1461                 os_fd_set_cloexec(fileno(logfile));
1462                 setvbuf(logfile, NULL, _IOLBF, 256);
1463         }
1464 }
1465
1466 static void
1467 log_file_close(void)
1468 {
1469         if (logfile && logfile != stderr)
1470                 fclose(logfile);
1471         logfile = stderr;
1472 }
1473
1474 static int
1475 vlog(const char *fmt, va_list ap)
1476 {
1477         const char *oom = "Out of memory";
1478         char timestr[128];
1479         int len = 0;
1480         char *str;
1481
1482         if (weston_log_scope_is_enabled(log_scope)) {
1483                 int len_va;
1484                 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1485                 len_va = vasprintf(&str, fmt, ap);
1486                 if (len_va >= 0) {
1487                         len = weston_log_scope_printf(log_scope, "%s %s",
1488                                         xlog_timestamp, str);
1489                         free(str);
1490                 } else {
1491                         len = weston_log_scope_printf(log_scope, "%s %s",
1492                                         xlog_timestamp, oom);
1493                 }
1494         }
1495
1496         return len;
1497 }
1498
1499
1500 static int
1501 vlog_continue(const char *fmt, va_list ap)
1502 {
1503         return weston_log_scope_vprintf(log_scope, fmt, ap);
1504 }
1505
1506 static int
1507 on_term_signal(int signo, void *data)
1508 {
1509         struct wl_display *display = data;
1510
1511         weston_log("caught signal %d\n", signo);
1512         wl_display_terminate(display);
1513
1514         return 1;
1515 }
1516
1517 static void
1518 handle_exit(struct weston_compositor *compositor)
1519 {
1520         wl_display_terminate(compositor->wl_display);
1521 }
1522
1523 static void
1524 usage(int error_code)
1525 {
1526         FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1527         fprintf(out,
1528                 "Usage: agl-compositor [OPTIONS]\n"
1529                 "\n"
1530                 "This is " PACKAGE_STRING ", the reference compositor for\n"
1531                 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1532                 "backends,\nand depending on which backend is in use different "
1533                 "options will be accepted.\n"
1534                 "\n"
1535                 "Core options:\n"
1536                 "\n"
1537                 "  --version\t\tPrint agl-compositor version\n"
1538                 "  -B, --backend=MODULE\tBackend module, one of\n"
1539                         "\t\t\t\tdrm-backend.so\n"
1540                         "\t\t\t\twayland-backend.so\n"
1541                         "\t\t\t\tx11-backend.so\n"
1542                         "\t\t\t\theadless-backend.so\n"
1543                 "  -S, --socket=NAME\tName of socket to listen on\n"
1544                 "  --log=FILE\t\tLog to the given file\n"
1545                 "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1546                 "  --no-config\t\tDo not read agl-compositor.ini\n"
1547                 "  --debug\t\tEnable debug extension(s)\n"
1548                 "  -h, --help\t\tThis help message\n"
1549                 "\n");
1550         exit(error_code);
1551 }
1552
1553 static char *
1554 copy_command_line(int argc, char * const argv[])
1555 {
1556         FILE *fp;
1557         char *str = NULL;
1558         size_t size = 0;
1559         int i;
1560
1561         fp = open_memstream(&str, &size);
1562         if (!fp)
1563                 return NULL;
1564
1565         fprintf(fp, "%s", argv[0]);
1566         for (i = 1; i < argc; i++)
1567                 fprintf(fp, " %s", argv[i]);
1568         fclose(fp);
1569
1570         return str;
1571 }
1572
1573 WL_EXPORT
1574 int wet_main(int argc, char *argv[])
1575 {
1576         struct ivi_compositor ivi = { 0 };
1577         char *cmdline;
1578         struct wl_display *display = NULL;
1579         struct wl_event_loop *loop;
1580         struct wl_event_source *signals[3] = { 0 };
1581         struct weston_config_section *section;
1582         /* Command line options */
1583         char *backend = NULL;
1584         char *socket_name = NULL;
1585         char *log = NULL;
1586         char *modules = NULL;
1587         char *option_modules = NULL;
1588         int help = 0;
1589         int version = 0;
1590         int no_config = 0;
1591         int debug = 0;
1592         char *config_file = NULL;
1593         struct weston_log_context *log_ctx = NULL;
1594         struct weston_log_subscriber *logger;
1595         int ret = EXIT_FAILURE;
1596         bool xwayland = false;
1597
1598         const struct weston_option core_options[] = {
1599                 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1600                 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1601                 { WESTON_OPTION_STRING, "log", 0, &log },
1602                 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1603                 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1604                 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1605                 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1606                 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1607                 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1608         };
1609
1610         wl_list_init(&ivi.outputs);
1611         wl_list_init(&ivi.surfaces);
1612         wl_list_init(&ivi.pending_surfaces);
1613         wl_list_init(&ivi.popup_pending_apps);
1614         wl_list_init(&ivi.fullscreen_pending_apps);
1615         wl_list_init(&ivi.split_pending_apps);
1616         wl_list_init(&ivi.remote_pending_apps);
1617         wl_list_init(&ivi.desktop_clients);
1618
1619         /* Prevent any clients we spawn getting our stdin */
1620         os_fd_set_cloexec(STDIN_FILENO);
1621
1622         cmdline = copy_command_line(argc, argv);
1623         parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1624
1625         if (help)
1626                 usage(EXIT_SUCCESS);
1627
1628         if (version) {
1629                 printf(PACKAGE_STRING "\n");
1630                 return EXIT_SUCCESS;
1631         }
1632
1633         log_ctx = weston_log_ctx_compositor_create();
1634         if (!log_ctx) {
1635                 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1636                 return ret;
1637         }
1638
1639         log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1640                                                     "agl-compositor log\n",
1641                                                     NULL, NULL, NULL);
1642
1643         log_file_open(log);
1644         weston_log_set_handler(vlog, vlog_continue);
1645
1646         logger = weston_log_subscriber_create_log(logfile);
1647         weston_log_subscribe(log_ctx, logger, "log");
1648
1649         weston_log("Command line: %s\n", cmdline);
1650         free(cmdline);
1651
1652         if (load_config(&ivi.config, no_config, config_file) < 0)
1653                 goto error_signals;
1654         section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1655         if (!backend) {
1656                 weston_config_section_get_string(section, "backend", &backend,
1657                                                  NULL);
1658                 if (!backend)
1659                         backend = choose_default_backend();
1660         }
1661         /* from [core] */
1662         weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
1663         weston_config_section_get_bool(section, "activate-by-default", &ivi.activate_by_default, true);
1664
1665         display = wl_display_create();
1666         loop = wl_display_get_event_loop(display);
1667
1668         wl_display_set_global_filter(display,
1669                                      global_filter, &ivi);
1670
1671         /* Register signal handlers so we shut down cleanly */
1672
1673         signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1674                                               display);
1675         signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1676                                               display);
1677         signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1678                                               display);
1679
1680         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1681                 if (!signals[i])
1682                         goto error_signals;
1683
1684         ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1685         if (!ivi.compositor) {
1686                 weston_log("fatal: failed to create compositor.\n");
1687                 goto error_signals;
1688         }
1689
1690         if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1691                 goto error_compositor;
1692
1693         if (load_backend(&ivi, backend, &argc, argv) < 0) {
1694                 weston_log("fatal: failed to create compositor backend.\n");
1695                 goto error_compositor;
1696         }
1697
1698         ivi.heads_changed.notify = heads_changed;
1699         weston_compositor_add_heads_changed_listener(ivi.compositor,
1700                                                      &ivi.heads_changed);
1701
1702         if (ivi_desktop_init(&ivi) < 0)
1703                 goto error_compositor;
1704
1705         ivi_seat_init(&ivi);
1706
1707         /* load additional modules */
1708         weston_config_section_get_string(section, "modules", &modules, "");
1709         if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
1710                 goto error_compositor;
1711
1712         if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
1713                 goto error_compositor;
1714
1715         if (ivi_policy_init(&ivi) < 0)
1716                 goto error_compositor;
1717
1718         if (ivi_shell_init(&ivi) < 0)
1719                 goto error_compositor;
1720
1721         add_bindings(ivi.compositor);
1722
1723         weston_compositor_flush_heads_changed(ivi.compositor);
1724
1725         if (ivi.remoting_api)
1726                 ivi_enable_remote_outputs(&ivi);
1727
1728         if (ivi.waltham_transmitter_api)
1729                 ivi_enable_waltham_outputs(&ivi);
1730
1731         if (create_listening_socket(display, socket_name) < 0)
1732                 goto error_compositor;
1733
1734         ivi_shell_init_black_fs(&ivi);
1735
1736         ivi.compositor->exit = handle_exit;
1737
1738         weston_compositor_wake(ivi.compositor);
1739
1740         ivi_shell_create_global(&ivi);
1741         ivi_launch_shell_client(&ivi);
1742         if (debug)
1743                 ivi_screenshooter_create(&ivi);
1744         ivi_agl_systemd_notify(&ivi);
1745
1746         wl_display_run(display);
1747
1748         ret = ivi.compositor->exit_code;
1749
1750         wl_display_destroy_clients(display);
1751
1752 error_compositor:
1753         weston_compositor_tear_down(ivi.compositor);
1754
1755         weston_compositor_log_scope_destroy(log_scope);
1756         log_scope = NULL;
1757
1758         weston_log_ctx_compositor_destroy(ivi.compositor);
1759         weston_compositor_destroy(ivi.compositor);
1760
1761         weston_log_subscriber_destroy_log(logger);
1762
1763         ivi_policy_destroy(ivi.policy);
1764
1765 error_signals:
1766         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1767                 if (signals[i])
1768                         wl_event_source_remove(signals[i]);
1769
1770         wl_display_destroy(display);
1771
1772         log_file_close();
1773         if (ivi.config)
1774                 weston_config_destroy(ivi.config);
1775
1776         return ret;
1777 }