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