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