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