22c49793016f512adc7231f8d47872c4589e2492
[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                 module_init = weston_load_module(buffer, "wet_module_init");
1181                 if (!module_init)
1182                         return -1;
1183
1184                 if (module_init(ivi->compositor, *argc, argv) < 0)
1185                         return -1;
1186
1187                 p = end;
1188                 while (*p == ',')
1189                         p++;
1190         }
1191
1192         return 0;
1193 }
1194
1195
1196 static char *
1197 choose_default_backend(void)
1198 {
1199         char *backend = NULL;
1200
1201         if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1202                 backend = strdup("wayland-backend.so");
1203         else if (getenv("DISPLAY"))
1204                 backend = strdup("x11-backend.so");
1205         else
1206                 backend = strdup("drm-backend.so");
1207
1208         return backend;
1209 }
1210
1211 static int
1212 compositor_init_config(struct weston_compositor *compositor,
1213                        struct weston_config *config)
1214 {
1215         struct xkb_rule_names xkb_names;
1216         struct weston_config_section *section;
1217         int repaint_msec;
1218         bool vt_switching;
1219         bool require_input;
1220
1221         /* agl-compositor.ini [keyboard] */
1222         section = weston_config_get_section(config, "keyboard", NULL, NULL);
1223         weston_config_section_get_string(section, "keymap_rules",
1224                                          (char **) &xkb_names.rules, NULL);
1225         weston_config_section_get_string(section, "keymap_model",
1226                                          (char **) &xkb_names.model, NULL);
1227         weston_config_section_get_string(section, "keymap_layout",
1228                                          (char **) &xkb_names.layout, NULL);
1229         weston_config_section_get_string(section, "keymap_variant",
1230                                          (char **) &xkb_names.variant, NULL);
1231         weston_config_section_get_string(section, "keymap_options",
1232                                          (char **) &xkb_names.options, NULL);
1233
1234         if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1235                 return -1;
1236
1237         weston_config_section_get_int(section, "repeat-rate",
1238                                       &compositor->kb_repeat_rate, 40);
1239         weston_config_section_get_int(section, "repeat-delay",
1240                                       &compositor->kb_repeat_delay, 400);
1241
1242         weston_config_section_get_bool(section, "vt-switching",
1243                                        &vt_switching, true);
1244         compositor->vt_switching = vt_switching;
1245
1246         /* agl-compositor.ini [core] */
1247         section = weston_config_get_section(config, "core", NULL, NULL);
1248
1249         weston_config_section_get_bool(section, "require-input", &require_input, true);
1250         compositor->require_input = require_input;
1251
1252         weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1253                                       compositor->repaint_msec);
1254         if (repaint_msec < -10 || repaint_msec > 1000) {
1255                 weston_log("Invalid repaint_window value in config: %d\n",
1256                            repaint_msec);
1257         } else {
1258                 compositor->repaint_msec = repaint_msec;
1259         }
1260         weston_log("Output repaint window is %d ms maximum.\n",
1261                    compositor->repaint_msec);
1262
1263         return 0;
1264 }
1265
1266 struct ivi_surface *
1267 to_ivi_surface(struct weston_surface *surface)
1268 {
1269         struct weston_desktop_surface *dsurface;
1270
1271         dsurface = weston_surface_get_desktop_surface(surface);
1272         if (!dsurface)
1273                 return NULL;
1274
1275         return weston_desktop_surface_get_user_data(dsurface);
1276 }
1277
1278 static void
1279 activate_binding(struct weston_seat *seat,
1280                  struct weston_view *focus_view)
1281 {
1282         struct weston_surface *focus = focus_view->surface;
1283         struct weston_surface *main_surface =
1284                 weston_surface_get_main_surface(focus);
1285         struct ivi_surface *surface;
1286
1287         surface = to_ivi_surface(main_surface);
1288         if (!surface)
1289                 return;
1290
1291         weston_seat_set_keyboard_focus(seat, focus);
1292 }
1293
1294 static void
1295 click_to_activate_binding(struct weston_pointer *pointer,
1296                           const struct timespec *time,
1297                           uint32_t button, void *data)
1298 {
1299         if (pointer->grab != &pointer->default_grab)
1300                 return;
1301         if (pointer->focus == NULL)
1302                 return;
1303
1304         activate_binding(pointer->seat, pointer->focus);
1305 }
1306
1307 static void
1308 touch_to_activate_binding(struct weston_touch *touch,
1309                           const struct timespec *time,
1310                           void *data)
1311 {
1312         if (touch->grab != &touch->default_grab)
1313                 return;
1314         if (touch->focus == NULL)
1315                 return;
1316
1317         activate_binding(touch->seat, touch->focus);
1318 }
1319
1320 static void
1321 add_bindings(struct weston_compositor *compositor)
1322 {
1323         weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1324                                              click_to_activate_binding,
1325                                              NULL);
1326         weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1327                                              click_to_activate_binding,
1328                                              NULL);
1329         weston_compositor_add_touch_binding(compositor, 0,
1330                                             touch_to_activate_binding,
1331                                             NULL);
1332 }
1333
1334 static int
1335 create_listening_socket(struct wl_display *display, const char *socket_name)
1336 {
1337         if (socket_name) {
1338                 if (wl_display_add_socket(display, socket_name)) {
1339                         weston_log("fatal: failed to add socket: %s\n",
1340                                    strerror(errno));
1341                         return -1;
1342                 }
1343         } else {
1344                 socket_name = wl_display_add_socket_auto(display);
1345                 if (!socket_name) {
1346                         weston_log("fatal: failed to add socket: %s\n",
1347                                    strerror(errno));
1348                         return -1;
1349                 }
1350         }
1351
1352         setenv("WAYLAND_DISPLAY", socket_name, 1);
1353
1354         return 0;
1355 }
1356
1357 static bool
1358 global_filter(const struct wl_client *client, const struct wl_global *global,
1359               void *data)
1360 {
1361         return true;
1362 }
1363
1364 static int
1365 load_config(struct weston_config **config, bool no_config,
1366             const char *config_file)
1367 {
1368         const char *file = "agl-compositor.ini";
1369         const char *full_path;
1370
1371         if (config_file)
1372                 file = config_file;
1373
1374         if (!no_config)
1375                 *config = weston_config_parse(file);
1376
1377         if (*config) {
1378                 full_path = weston_config_get_full_path(*config);
1379
1380                 weston_log("Using config file '%s'.\n", full_path);
1381                 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1382
1383                 return 0;
1384         }
1385
1386         if (config_file && !no_config) {
1387                 weston_log("fatal: error opening or reading config file '%s'.\n",
1388                         config_file);
1389
1390                 return -1;
1391         }
1392
1393         weston_log("Starting with no config file.\n");
1394         setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1395
1396         return 0;
1397 }
1398
1399 static FILE *logfile;
1400
1401 static char *
1402 log_timestamp(char *buf, size_t len)
1403 {
1404         struct timeval tv;
1405         struct tm *brokendown_time;
1406         char datestr[128];
1407         char timestr[128];
1408
1409         gettimeofday(&tv, NULL);
1410
1411         brokendown_time = localtime(&tv.tv_sec);
1412         if (brokendown_time == NULL) {
1413                 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1414                 return buf;
1415         }
1416
1417         memset(datestr, 0, sizeof(datestr));
1418         if (brokendown_time->tm_mday != cached_tm_mday) {
1419                 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1420                                 brokendown_time);
1421                 cached_tm_mday = brokendown_time->tm_mday;
1422         }
1423
1424         strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1425         /* if datestr is empty it prints only timestr*/
1426         snprintf(buf, len, "%s[%s.%03li]", datestr,
1427                         timestr, (tv.tv_usec / 1000));
1428
1429         return buf;
1430 }
1431
1432 static void
1433 custom_handler(const char *fmt, va_list arg)
1434 {
1435         char timestr[512];
1436
1437         weston_log_scope_printf(log_scope, "%s libwayland: ",
1438                         log_timestamp(timestr, sizeof(timestr)));
1439         weston_log_scope_vprintf(log_scope, fmt, arg);
1440 }
1441
1442 static void
1443 log_file_open(const char *filename)
1444 {
1445         wl_log_set_handler_server(custom_handler);
1446
1447         if (filename)
1448                 logfile = fopen(filename, "a");
1449
1450         if (!logfile) {
1451                 logfile = stderr;
1452         } else {
1453                 os_fd_set_cloexec(fileno(logfile));
1454                 setvbuf(logfile, NULL, _IOLBF, 256);
1455         }
1456 }
1457
1458 static void
1459 log_file_close(void)
1460 {
1461         if (logfile && logfile != stderr)
1462                 fclose(logfile);
1463         logfile = stderr;
1464 }
1465
1466 static int
1467 vlog(const char *fmt, va_list ap)
1468 {
1469         const char *oom = "Out of memory";
1470         char timestr[128];
1471         int len = 0;
1472         char *str;
1473
1474         if (weston_log_scope_is_enabled(log_scope)) {
1475                 int len_va;
1476                 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1477                 len_va = vasprintf(&str, fmt, ap);
1478                 if (len_va >= 0) {
1479                         len = weston_log_scope_printf(log_scope, "%s %s",
1480                                         xlog_timestamp, str);
1481                         free(str);
1482                 } else {
1483                         len = weston_log_scope_printf(log_scope, "%s %s",
1484                                         xlog_timestamp, oom);
1485                 }
1486         }
1487
1488         return len;
1489 }
1490
1491
1492 static int
1493 vlog_continue(const char *fmt, va_list ap)
1494 {
1495         return weston_log_scope_vprintf(log_scope, fmt, ap);
1496 }
1497
1498 static int
1499 on_term_signal(int signo, void *data)
1500 {
1501         struct wl_display *display = data;
1502
1503         weston_log("caught signal %d\n", signo);
1504         wl_display_terminate(display);
1505
1506         return 1;
1507 }
1508
1509 static void
1510 handle_exit(struct weston_compositor *compositor)
1511 {
1512         wl_display_terminate(compositor->wl_display);
1513 }
1514
1515 static void
1516 usage(int error_code)
1517 {
1518         FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1519         fprintf(out,
1520                 "Usage: agl-compositor [OPTIONS]\n"
1521                 "\n"
1522                 "This is " PACKAGE_STRING ", the reference compositor for\n"
1523                 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1524                 "backends,\nand depending on which backend is in use different "
1525                 "options will be accepted.\n"
1526                 "\n"
1527                 "Core options:\n"
1528                 "\n"
1529                 "  --version\t\tPrint agl-compositor version\n"
1530                 "  -B, --backend=MODULE\tBackend module, one of\n"
1531                         "\t\t\t\tdrm-backend.so\n"
1532                         "\t\t\t\twayland-backend.so\n"
1533                         "\t\t\t\tx11-backend.so\n"
1534                         "\t\t\t\theadless-backend.so\n"
1535                 "  -S, --socket=NAME\tName of socket to listen on\n"
1536                 "  --log=FILE\t\tLog to the given file\n"
1537                 "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1538                 "  --no-config\t\tDo not read agl-compositor.ini\n"
1539                 "  --debug\t\tEnable debug extension(s)\n"
1540                 "  -h, --help\t\tThis help message\n"
1541                 "\n");
1542         exit(error_code);
1543 }
1544
1545 static char *
1546 copy_command_line(int argc, char * const argv[])
1547 {
1548         FILE *fp;
1549         char *str = NULL;
1550         size_t size = 0;
1551         int i;
1552
1553         fp = open_memstream(&str, &size);
1554         if (!fp)
1555                 return NULL;
1556
1557         fprintf(fp, "%s", argv[0]);
1558         for (i = 1; i < argc; i++)
1559                 fprintf(fp, " %s", argv[i]);
1560         fclose(fp);
1561
1562         return str;
1563 }
1564
1565 WL_EXPORT
1566 int wet_main(int argc, char *argv[])
1567 {
1568         struct ivi_compositor ivi = { 0 };
1569         char *cmdline;
1570         struct wl_display *display = NULL;
1571         struct wl_event_loop *loop;
1572         struct wl_event_source *signals[3] = { 0 };
1573         struct weston_config_section *section;
1574         /* Command line options */
1575         char *backend = NULL;
1576         char *socket_name = NULL;
1577         char *log = NULL;
1578         char *modules = NULL;
1579         char *option_modules = NULL;
1580         int help = 0;
1581         int version = 0;
1582         int no_config = 0;
1583         int debug = 0;
1584         char *config_file = NULL;
1585         struct weston_log_context *log_ctx = NULL;
1586         struct weston_log_subscriber *logger;
1587         int ret = EXIT_FAILURE;
1588         bool xwayland = false;
1589
1590         const struct weston_option core_options[] = {
1591                 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1592                 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1593                 { WESTON_OPTION_STRING, "log", 0, &log },
1594                 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1595                 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1596                 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1597                 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1598                 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1599                 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1600         };
1601
1602         wl_list_init(&ivi.outputs);
1603         wl_list_init(&ivi.surfaces);
1604         wl_list_init(&ivi.pending_surfaces);
1605         wl_list_init(&ivi.popup_pending_apps);
1606         wl_list_init(&ivi.fullscreen_pending_apps);
1607         wl_list_init(&ivi.split_pending_apps);
1608         wl_list_init(&ivi.remote_pending_apps);
1609         wl_list_init(&ivi.desktop_clients);
1610
1611         /* Prevent any clients we spawn getting our stdin */
1612         os_fd_set_cloexec(STDIN_FILENO);
1613
1614         cmdline = copy_command_line(argc, argv);
1615         parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1616
1617         if (help)
1618                 usage(EXIT_SUCCESS);
1619
1620         if (version) {
1621                 printf(PACKAGE_STRING "\n");
1622                 return EXIT_SUCCESS;
1623         }
1624
1625         log_ctx = weston_log_ctx_compositor_create();
1626         if (!log_ctx) {
1627                 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1628                 return ret;
1629         }
1630
1631         log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1632                                                     "agl-compositor log\n",
1633                                                     NULL, NULL, NULL);
1634
1635         log_file_open(log);
1636         weston_log_set_handler(vlog, vlog_continue);
1637
1638         logger = weston_log_subscriber_create_log(logfile);
1639         weston_log_subscribe(log_ctx, logger, "log");
1640
1641         weston_log("Command line: %s\n", cmdline);
1642         free(cmdline);
1643
1644         if (load_config(&ivi.config, no_config, config_file) < 0)
1645                 goto error_signals;
1646         section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1647         if (!backend) {
1648                 weston_config_section_get_string(section, "backend", &backend,
1649                                                  NULL);
1650                 if (!backend)
1651                         backend = choose_default_backend();
1652         }
1653         /* from [core] */
1654         weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
1655         weston_config_section_get_bool(section, "activate-by-default", &ivi.activate_by_default, true);
1656
1657         display = wl_display_create();
1658         loop = wl_display_get_event_loop(display);
1659
1660         wl_display_set_global_filter(display,
1661                                      global_filter, &ivi);
1662
1663         /* Register signal handlers so we shut down cleanly */
1664
1665         signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1666                                               display);
1667         signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1668                                               display);
1669         signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1670                                               display);
1671
1672         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1673                 if (!signals[i])
1674                         goto error_signals;
1675
1676         ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1677         if (!ivi.compositor) {
1678                 weston_log("fatal: failed to create compositor.\n");
1679                 goto error_signals;
1680         }
1681
1682         if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1683                 goto error_compositor;
1684
1685         if (load_backend(&ivi, backend, &argc, argv) < 0) {
1686                 weston_log("fatal: failed to create compositor backend.\n");
1687                 goto error_compositor;
1688         }
1689
1690         ivi.heads_changed.notify = heads_changed;
1691         weston_compositor_add_heads_changed_listener(ivi.compositor,
1692                                                      &ivi.heads_changed);
1693
1694         if (ivi_desktop_init(&ivi) < 0)
1695                 goto error_compositor;
1696
1697         ivi_seat_init(&ivi);
1698
1699         /* load additional modules */
1700         weston_config_section_get_string(section, "modules", &modules, "");
1701         if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
1702                 goto error_compositor;
1703
1704         if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
1705                 goto error_compositor;
1706
1707         if (ivi_policy_init(&ivi) < 0)
1708                 goto error_compositor;
1709
1710         if (ivi_shell_init(&ivi) < 0)
1711                 goto error_compositor;
1712
1713         add_bindings(ivi.compositor);
1714
1715         weston_compositor_flush_heads_changed(ivi.compositor);
1716
1717         if (ivi.remoting_api)
1718                 ivi_enable_remote_outputs(&ivi);
1719
1720         if (ivi.waltham_transmitter_api)
1721                 ivi_enable_waltham_outputs(&ivi);
1722
1723         if (create_listening_socket(display, socket_name) < 0)
1724                 goto error_compositor;
1725
1726         ivi_shell_init_black_fs(&ivi);
1727
1728         ivi.compositor->exit = handle_exit;
1729
1730         weston_compositor_wake(ivi.compositor);
1731
1732         ivi_shell_create_global(&ivi);
1733         ivi_launch_shell_client(&ivi);
1734         if (debug)
1735                 ivi_screenshooter_create(&ivi);
1736         ivi_agl_systemd_notify(&ivi);
1737
1738         wl_display_run(display);
1739
1740         ret = ivi.compositor->exit_code;
1741
1742         wl_display_destroy_clients(display);
1743
1744 error_compositor:
1745         weston_compositor_tear_down(ivi.compositor);
1746
1747         weston_compositor_log_scope_destroy(log_scope);
1748         log_scope = NULL;
1749
1750         weston_log_ctx_compositor_destroy(ivi.compositor);
1751         weston_compositor_destroy(ivi.compositor);
1752
1753         weston_log_subscriber_destroy_log(logger);
1754
1755         ivi_policy_destroy(ivi.policy);
1756
1757 error_signals:
1758         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1759                 if (signals[i])
1760                         wl_event_source_remove(signals[i]);
1761
1762         wl_display_destroy(display);
1763
1764         log_file_close();
1765         if (ivi.config)
1766                 weston_config_destroy(ivi.config);
1767
1768         return ret;
1769 }