meson.build: Conditionally build with headless support
[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
758                 ivi_output->ivi = ivi;
759                 ivi_output->name = _name;
760                 ivi_output->config = remote_section;
761                 ivi_output->type = OUTPUT_REMOTE;
762
763                 if (remote_output_init(ivi_output, ivi->compositor,
764                                        remote_section, ivi->remoting_api)) {
765                         free(ivi_output->name);
766                         free(ivi_output);
767                         continue;
768                 }
769
770                 ivi_output->output_destroy.notify = handle_output_destroy;
771                 weston_output_add_destroy_listener(ivi_output->output,
772                                                    &ivi_output->output_destroy);
773
774                 wl_list_insert(&ivi->outputs, &ivi_output->link);
775                 ivi_output_configure_app_id(ivi_output);
776         }
777 }
778
779 static void
780 ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
781 {
782         struct weston_config_section *transmitter_section = NULL;
783         const char *sect_name;
784         struct weston_config *config = ivi->config;
785
786         while (weston_config_next_section(config, &transmitter_section, &sect_name)) {
787                 if (strcmp(sect_name, "transmitter-output"))
788                         continue;
789
790                 struct ivi_output *ivi_output = NULL;
791                 bool output_found = false;
792                 char *_name = NULL;
793
794                 weston_config_section_get_string(transmitter_section,
795                                 "name", &_name, NULL);
796                 wl_list_for_each(ivi_output, &ivi->outputs, link) {
797                         if (!strcmp(ivi_output->name, _name)) {
798                                 output_found = true;
799                                 break;
800                         }
801                 }
802
803                 if (output_found) {
804                         free(_name);
805                         continue;
806                 }
807
808                 ivi_output = zalloc(sizeof(*ivi_output));
809
810                 ivi_output->ivi = ivi;
811                 ivi_output->name = _name;
812                 ivi_output->config = transmitter_section;
813
814                 if (remote_output_init(ivi_output, ivi->compositor,
815                                         transmitter_section, ivi->remoting_api)) {
816                         free(ivi_output->name);
817                         free(ivi_output);
818                         continue;
819                 }
820
821                 ivi_output->type = OUTPUT_WALTHAM;
822                 ivi_output->output_destroy.notify = handle_output_destroy;
823                 weston_output_add_destroy_listener(ivi_output->output,
824                                 &ivi_output->output_destroy);
825
826                 wl_list_insert(&ivi->outputs, &ivi_output->link);
827                 ivi_output_configure_app_id(ivi_output);
828         }
829 }
830
831 static int
832 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
833 {
834         struct weston_compositor *compositor = ivi->compositor;
835         int (*module_init)(struct weston_compositor *wc);
836
837         module_init = weston_load_module("remoting-plugin.so",
838                                          "weston_module_init");
839         if (!module_init)
840                 return -1;
841
842         if (module_init(compositor) < 0)
843                 return -1;
844
845         ivi->remoting_api = weston_remoting_get_api(compositor);
846         if (!ivi->remoting_api)
847                 return -1;
848         return 0;
849 }
850 #else
851 static int
852 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
853 {
854         return -1;
855 }
856 #endif
857
858 static int
859 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
860 {
861         struct weston_drm_backend_config config = {
862                 .base = {
863                         .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
864                         .struct_size = sizeof config,
865                 },
866         };
867         struct weston_config_section *section;
868         int use_current_mode = 0;
869         int use_pixman = 0;
870         bool use_shadow;
871         int ret;
872
873         const struct weston_option options[] = {
874                 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
875                 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
876                 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
877                 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
878                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
879         };
880
881         parse_options(options, ARRAY_LENGTH(options), argc, argv);
882         config.use_pixman = use_pixman;
883         ivi->cmdline.use_current_mode = use_current_mode;
884
885         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
886         weston_config_section_get_string(section, "gbm-format",
887                                          &config.gbm_format, NULL);
888         weston_config_section_get_uint(section, "pageflip-timeout",
889                                        &config.pageflip_timeout, 0);
890         weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
891         config.use_pixman_shadow = use_shadow;
892
893         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
894                                              &config.base);
895         if (ret < 0)
896                 return ret;
897
898         ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
899         if (!ivi->drm_api) {
900                 weston_log("Cannot use drm output api.\n");
901                 ret = -1;
902                 goto error;
903         }
904
905         load_remoting_plugin(ivi, ivi->config);
906         load_waltham_plugin(ivi, ivi->config);
907
908 error:
909         free(config.gbm_format);
910         free(config.seat_id);
911         return ret;
912 }
913
914 static void
915 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
916                               bool *use_pixman, bool *fullscreen, int *output_count)
917 {
918         struct weston_config_section *section;
919         bool pixman;
920         int fs = 0;
921
922         const struct weston_option options[] = {
923                 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
924                 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
925                 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
926                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
927                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
928                 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
929         };
930
931         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
932         weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
933
934         *output_count = 1;
935         parse_options(options, ARRAY_LENGTH(options), argc, argv);
936         *use_pixman = pixman;
937         *fullscreen = fs;
938 }
939
940 static int
941 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
942                         const char *match_prefix, const char *name_prefix)
943 {
944         struct weston_config_section *section = NULL;
945         const char *section_name;
946         char *default_output = NULL;
947         int i = 0;
948         size_t match_len = strlen(match_prefix);
949
950         while (weston_config_next_section(ivi->config, &section, &section_name)) {
951                 char *output_name;
952
953                 if (i >= output_count)
954                         break;
955
956                 if (strcmp(section_name, "output") != 0)
957                         continue;
958
959                 weston_config_section_get_string(section, "name", &output_name, NULL);
960                 if (output_name == NULL)
961                         continue;
962                 if (strncmp(output_name, match_prefix, match_len) != 0) {
963                         free(output_name);
964                         continue;
965                 }
966
967                 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
968                         free(output_name);
969                         return -1;
970                 }
971
972                 free(output_name);
973                 ++i;
974         }
975
976         for (; i < output_count; ++i) {
977                 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
978                         return -1;
979
980                 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
981                         free(default_output);
982                         return -1;
983                 }
984
985                 free(default_output);
986         }
987
988         return 0;
989 }
990
991 static int
992 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
993 {
994         struct weston_wayland_backend_config config = {
995                 .base = {
996                         .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
997                         .struct_size = sizeof config,
998                 },
999         };
1000         struct weston_config_section *section;
1001         int sprawl = 0;
1002         int output_count;
1003         int ret;
1004
1005         const struct weston_option options[] = {
1006                 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1007                 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1008         };
1009
1010         windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1011                                       &config.fullscreen, &output_count);
1012
1013         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1014         config.sprawl = sprawl;
1015
1016         section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1017         weston_config_section_get_string(section, "cursor-theme",
1018                                          &config.cursor_theme, NULL);
1019         weston_config_section_get_int(section, "cursor-size",
1020                                       &config.cursor_size, 32);
1021
1022         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1023                                              &config.base);
1024
1025         free(config.cursor_theme);
1026         free(config.display_name);
1027
1028         if (ret < 0)
1029                 return ret;
1030
1031         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1032
1033         /*
1034          * We will just assume if load_backend() finished cleanly and
1035          * windowed_output_api is not present that wayland backend is started
1036          * with --sprawl or runs on fullscreen-shell. In this case, all values
1037          * are hardcoded, so nothing can be configured; simply create and
1038          * enable an output.
1039          */
1040         if (ivi->window_api == NULL)
1041                 return 0;
1042
1043         return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1044 }
1045
1046 #ifdef HAVE_BACKEND_X11
1047 static int
1048 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1049 {
1050         struct weston_x11_backend_config config = {
1051                 .base = {
1052                         .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1053                         .struct_size = sizeof config,
1054                 },
1055         };
1056         int no_input = 0;
1057         int output_count;
1058         int ret;
1059
1060         const struct weston_option options[] = {
1061                { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1062         };
1063
1064         windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1065                                       &config.fullscreen, &output_count);
1066
1067         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1068         config.no_input = no_input;
1069
1070         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1071                                              &config.base);
1072
1073         if (ret < 0)
1074                 return ret;
1075
1076         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1077         if (!ivi->window_api) {
1078                 weston_log("Cannot use weston_windowed_output_api.\n");
1079                 return -1;
1080         }
1081
1082         return windowed_create_outputs(ivi, output_count, "X", "screen");
1083 }
1084 #else
1085 static int
1086 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1087 {
1088         return -1;
1089 }
1090 #endif
1091
1092 #ifdef HAVE_BACKEND_HEADLESS
1093 static int
1094 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1095 {
1096         struct weston_headless_backend_config config = {};
1097         int ret = 0;
1098
1099         bool use_pixman;
1100         bool fullscreen;
1101         bool use_gl;
1102         int output_count;
1103
1104         struct weston_compositor *c = ivi->compositor;
1105
1106         const struct weston_option options[] = {
1107                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1108                 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &use_gl },
1109         };
1110
1111         windowed_parse_common_options(ivi, argc, argv, &use_pixman,
1112                         &fullscreen, &output_count);
1113
1114         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1115         config.use_pixman = use_pixman;
1116         config.use_gl = use_gl;
1117
1118         config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1119         config.base.struct_size = sizeof(struct weston_headless_backend_config);
1120
1121         /* load the actual headless-backend and configure it */
1122         ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1123                                              &config.base);
1124         if (ret < 0)
1125                 return ret;
1126
1127         ivi->window_api = weston_windowed_output_get_api(c);
1128         if (!ivi->window_api) {
1129                 weston_log("Cannot use weston_windowed_output_api.\n");
1130                 return -1;
1131         }
1132
1133         if (ivi->window_api->create_head(c, "headless") < 0) {
1134                 weston_log("Cannot create headless back-end\n");
1135                 return -1;
1136         }
1137
1138         return 0;
1139 }
1140 #else
1141 static int
1142 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1143 {
1144         return -1;
1145 }
1146 #endif
1147
1148 static int
1149 load_backend(struct ivi_compositor *ivi, const char *backend,
1150              int *argc, char *argv[])
1151 {
1152         if (strcmp(backend, "drm-backend.so") == 0) {
1153                 return load_drm_backend(ivi, argc, argv);
1154         } else if (strcmp(backend, "wayland-backend.so") == 0) {
1155                 return load_wayland_backend(ivi, argc, argv);
1156         } else if (strcmp(backend, "x11-backend.so") == 0) {
1157                 return load_x11_backend(ivi, argc, argv);
1158         } else if (strcmp(backend, "headless-backend.so") == 0) {
1159                 return load_headless_backend(ivi, argc, argv);
1160         }
1161
1162         weston_log("fatal: unknown backend '%s'.\n", backend);
1163         return -1;
1164 }
1165
1166 static int
1167 load_modules(struct ivi_compositor *ivi, const char *modules,
1168              int *argc, char *argv[], bool *xwayland)
1169 {
1170         const char *p, *end;
1171         char buffer[256];
1172         int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1173
1174         if (modules == NULL)
1175                 return 0;
1176
1177         p = modules;
1178         while (*p) {
1179                 end = strchrnul(p, ',');
1180                 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1181
1182                 if (strstr(buffer, "xwayland.so")) {
1183                         weston_log("Xwayland plug-in not supported!\n");
1184                         p = end;
1185                         while (*p == ',')
1186                                 p++;
1187                         continue;
1188                 }
1189
1190                 if (strstr(buffer, "systemd-notify.so")) {
1191                         weston_log("systemd-notify plug-in already loaded!\n");
1192                         p = end;
1193                         while (*p == ',')
1194                                 p++;
1195                         continue;
1196                 }
1197
1198                 module_init = weston_load_module(buffer, "wet_module_init");
1199                 if (!module_init)
1200                         return -1;
1201
1202                 if (module_init(ivi->compositor, *argc, argv) < 0)
1203                         return -1;
1204
1205                 p = end;
1206                 while (*p == ',')
1207                         p++;
1208         }
1209
1210         return 0;
1211 }
1212
1213
1214 static char *
1215 choose_default_backend(void)
1216 {
1217         char *backend = NULL;
1218
1219         if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1220                 backend = strdup("wayland-backend.so");
1221         else if (getenv("DISPLAY"))
1222                 backend = strdup("x11-backend.so");
1223         else
1224                 backend = strdup("drm-backend.so");
1225
1226         return backend;
1227 }
1228
1229 static int
1230 compositor_init_config(struct weston_compositor *compositor,
1231                        struct weston_config *config)
1232 {
1233         struct xkb_rule_names xkb_names;
1234         struct weston_config_section *section;
1235         int repaint_msec;
1236         bool vt_switching;
1237         bool require_input;
1238
1239         /* agl-compositor.ini [keyboard] */
1240         section = weston_config_get_section(config, "keyboard", NULL, NULL);
1241         weston_config_section_get_string(section, "keymap_rules",
1242                                          (char **) &xkb_names.rules, NULL);
1243         weston_config_section_get_string(section, "keymap_model",
1244                                          (char **) &xkb_names.model, NULL);
1245         weston_config_section_get_string(section, "keymap_layout",
1246                                          (char **) &xkb_names.layout, NULL);
1247         weston_config_section_get_string(section, "keymap_variant",
1248                                          (char **) &xkb_names.variant, NULL);
1249         weston_config_section_get_string(section, "keymap_options",
1250                                          (char **) &xkb_names.options, NULL);
1251
1252         if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1253                 return -1;
1254
1255         weston_config_section_get_int(section, "repeat-rate",
1256                                       &compositor->kb_repeat_rate, 40);
1257         weston_config_section_get_int(section, "repeat-delay",
1258                                       &compositor->kb_repeat_delay, 400);
1259
1260         weston_config_section_get_bool(section, "vt-switching",
1261                                        &vt_switching, false);
1262         compositor->vt_switching = vt_switching;
1263
1264         /* agl-compositor.ini [core] */
1265         section = weston_config_get_section(config, "core", NULL, NULL);
1266
1267         weston_config_section_get_bool(section, "require-input", &require_input, true);
1268         compositor->require_input = require_input;
1269
1270         weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1271                                       compositor->repaint_msec);
1272         if (repaint_msec < -10 || repaint_msec > 1000) {
1273                 weston_log("Invalid repaint_window value in config: %d\n",
1274                            repaint_msec);
1275         } else {
1276                 compositor->repaint_msec = repaint_msec;
1277         }
1278         weston_log("Output repaint window is %d ms maximum.\n",
1279                    compositor->repaint_msec);
1280
1281         return 0;
1282 }
1283
1284 struct ivi_surface *
1285 to_ivi_surface(struct weston_surface *surface)
1286 {
1287         struct weston_desktop_surface *dsurface;
1288
1289         dsurface = weston_surface_get_desktop_surface(surface);
1290         if (!dsurface)
1291                 return NULL;
1292
1293         return weston_desktop_surface_get_user_data(dsurface);
1294 }
1295
1296 static void
1297 activate_binding(struct weston_seat *seat,
1298                  struct weston_view *focus_view)
1299 {
1300         struct weston_surface *focus = focus_view->surface;
1301         struct weston_surface *main_surface =
1302                 weston_surface_get_main_surface(focus);
1303         struct ivi_surface *surface;
1304
1305         surface = to_ivi_surface(main_surface);
1306         if (!surface)
1307                 return;
1308
1309         weston_seat_set_keyboard_focus(seat, focus);
1310 }
1311
1312 static void
1313 click_to_activate_binding(struct weston_pointer *pointer,
1314                           const struct timespec *time,
1315                           uint32_t button, void *data)
1316 {
1317         if (pointer->grab != &pointer->default_grab)
1318                 return;
1319         if (pointer->focus == NULL)
1320                 return;
1321
1322         activate_binding(pointer->seat, pointer->focus);
1323 }
1324
1325 static void
1326 touch_to_activate_binding(struct weston_touch *touch,
1327                           const struct timespec *time,
1328                           void *data)
1329 {
1330         if (touch->grab != &touch->default_grab)
1331                 return;
1332         if (touch->focus == NULL)
1333                 return;
1334
1335         activate_binding(touch->seat, touch->focus);
1336 }
1337
1338 static void
1339 add_bindings(struct weston_compositor *compositor)
1340 {
1341         weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1342                                              click_to_activate_binding,
1343                                              NULL);
1344         weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1345                                              click_to_activate_binding,
1346                                              NULL);
1347         weston_compositor_add_touch_binding(compositor, 0,
1348                                             touch_to_activate_binding,
1349                                             NULL);
1350 }
1351
1352 static int
1353 create_listening_socket(struct wl_display *display, const char *socket_name)
1354 {
1355         if (socket_name) {
1356                 if (wl_display_add_socket(display, socket_name)) {
1357                         weston_log("fatal: failed to add socket: %s\n",
1358                                    strerror(errno));
1359                         return -1;
1360                 }
1361         } else {
1362                 socket_name = wl_display_add_socket_auto(display);
1363                 if (!socket_name) {
1364                         weston_log("fatal: failed to add socket: %s\n",
1365                                    strerror(errno));
1366                         return -1;
1367                 }
1368         }
1369
1370         setenv("WAYLAND_DISPLAY", socket_name, 1);
1371
1372         return 0;
1373 }
1374
1375 static bool
1376 global_filter(const struct wl_client *client, const struct wl_global *global,
1377               void *data)
1378 {
1379         return true;
1380 }
1381
1382 static int
1383 load_config(struct weston_config **config, bool no_config,
1384             const char *config_file)
1385 {
1386         const char *file = "agl-compositor.ini";
1387         const char *full_path;
1388
1389         if (config_file)
1390                 file = config_file;
1391
1392         if (!no_config)
1393                 *config = weston_config_parse(file);
1394
1395         if (*config) {
1396                 full_path = weston_config_get_full_path(*config);
1397
1398                 weston_log("Using config file '%s'.\n", full_path);
1399                 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1400
1401                 return 0;
1402         }
1403
1404         if (config_file && !no_config) {
1405                 weston_log("fatal: error opening or reading config file '%s'.\n",
1406                         config_file);
1407
1408                 return -1;
1409         }
1410
1411         weston_log("Starting with no config file.\n");
1412         setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1413
1414         return 0;
1415 }
1416
1417 static FILE *logfile;
1418
1419 static char *
1420 log_timestamp(char *buf, size_t len)
1421 {
1422         struct timeval tv;
1423         struct tm *brokendown_time;
1424         char datestr[128];
1425         char timestr[128];
1426
1427         gettimeofday(&tv, NULL);
1428
1429         brokendown_time = localtime(&tv.tv_sec);
1430         if (brokendown_time == NULL) {
1431                 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1432                 return buf;
1433         }
1434
1435         memset(datestr, 0, sizeof(datestr));
1436         if (brokendown_time->tm_mday != cached_tm_mday) {
1437                 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1438                                 brokendown_time);
1439                 cached_tm_mday = brokendown_time->tm_mday;
1440         }
1441
1442         strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1443         /* if datestr is empty it prints only timestr*/
1444         snprintf(buf, len, "%s[%s.%03li]", datestr,
1445                         timestr, (tv.tv_usec / 1000));
1446
1447         return buf;
1448 }
1449
1450 static void
1451 custom_handler(const char *fmt, va_list arg)
1452 {
1453         char timestr[512];
1454
1455         weston_log_scope_printf(log_scope, "%s libwayland: ",
1456                         log_timestamp(timestr, sizeof(timestr)));
1457         weston_log_scope_vprintf(log_scope, fmt, arg);
1458 }
1459
1460 static void
1461 log_file_open(const char *filename)
1462 {
1463         wl_log_set_handler_server(custom_handler);
1464
1465         if (filename)
1466                 logfile = fopen(filename, "a");
1467
1468         if (!logfile) {
1469                 logfile = stderr;
1470         } else {
1471                 os_fd_set_cloexec(fileno(logfile));
1472                 setvbuf(logfile, NULL, _IOLBF, 256);
1473         }
1474 }
1475
1476 static void
1477 log_file_close(void)
1478 {
1479         if (logfile && logfile != stderr)
1480                 fclose(logfile);
1481         logfile = stderr;
1482 }
1483
1484 static int
1485 vlog(const char *fmt, va_list ap)
1486 {
1487         const char *oom = "Out of memory";
1488         char timestr[128];
1489         int len = 0;
1490         char *str;
1491
1492         if (weston_log_scope_is_enabled(log_scope)) {
1493                 int len_va;
1494                 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1495                 len_va = vasprintf(&str, fmt, ap);
1496                 if (len_va >= 0) {
1497                         len = weston_log_scope_printf(log_scope, "%s %s",
1498                                         xlog_timestamp, str);
1499                         free(str);
1500                 } else {
1501                         len = weston_log_scope_printf(log_scope, "%s %s",
1502                                         xlog_timestamp, oom);
1503                 }
1504         }
1505
1506         return len;
1507 }
1508
1509
1510 static int
1511 vlog_continue(const char *fmt, va_list ap)
1512 {
1513         return weston_log_scope_vprintf(log_scope, fmt, ap);
1514 }
1515
1516 static int
1517 on_term_signal(int signo, void *data)
1518 {
1519         struct wl_display *display = data;
1520
1521         weston_log("caught signal %d\n", signo);
1522         wl_display_terminate(display);
1523
1524         return 1;
1525 }
1526
1527 static void
1528 handle_exit(struct weston_compositor *compositor)
1529 {
1530         wl_display_terminate(compositor->wl_display);
1531 }
1532
1533 static void
1534 usage(int error_code)
1535 {
1536         FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1537         fprintf(out,
1538                 "Usage: agl-compositor [OPTIONS]\n"
1539                 "\n"
1540                 "This is " PACKAGE_STRING ", the reference compositor for\n"
1541                 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1542                 "backends,\nand depending on which backend is in use different "
1543                 "options will be accepted.\n"
1544                 "\n"
1545                 "Core options:\n"
1546                 "\n"
1547                 "  --version\t\tPrint agl-compositor version\n"
1548                 "  -B, --backend=MODULE\tBackend module, one of\n"
1549                         "\t\t\t\tdrm-backend.so\n"
1550                         "\t\t\t\twayland-backend.so\n"
1551                         "\t\t\t\tx11-backend.so\n"
1552                         "\t\t\t\theadless-backend.so\n"
1553                 "  -S, --socket=NAME\tName of socket to listen on\n"
1554                 "  --log=FILE\t\tLog to the given file\n"
1555                 "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1556                 "  --no-config\t\tDo not read agl-compositor.ini\n"
1557                 "  --debug\t\tEnable debug extension(s)\n"
1558                 "  -h, --help\t\tThis help message\n"
1559                 "\n");
1560         exit(error_code);
1561 }
1562
1563 static char *
1564 copy_command_line(int argc, char * const argv[])
1565 {
1566         FILE *fp;
1567         char *str = NULL;
1568         size_t size = 0;
1569         int i;
1570
1571         fp = open_memstream(&str, &size);
1572         if (!fp)
1573                 return NULL;
1574
1575         fprintf(fp, "%s", argv[0]);
1576         for (i = 1; i < argc; i++)
1577                 fprintf(fp, " %s", argv[i]);
1578         fclose(fp);
1579
1580         return str;
1581 }
1582
1583 WL_EXPORT
1584 int wet_main(int argc, char *argv[])
1585 {
1586         struct ivi_compositor ivi = { 0 };
1587         char *cmdline;
1588         struct wl_display *display = NULL;
1589         struct wl_event_loop *loop;
1590         struct wl_event_source *signals[3] = { 0 };
1591         struct weston_config_section *section;
1592         /* Command line options */
1593         char *backend = NULL;
1594         char *socket_name = NULL;
1595         char *log = NULL;
1596         char *modules = NULL;
1597         char *option_modules = NULL;
1598         int help = 0;
1599         int version = 0;
1600         int no_config = 0;
1601         int debug = 0;
1602         char *config_file = NULL;
1603         struct weston_log_context *log_ctx = NULL;
1604         struct weston_log_subscriber *logger;
1605         int ret = EXIT_FAILURE;
1606         bool xwayland = false;
1607
1608         const struct weston_option core_options[] = {
1609                 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1610                 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1611                 { WESTON_OPTION_STRING, "log", 0, &log },
1612                 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1613                 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1614                 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1615                 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1616                 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1617                 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1618         };
1619
1620         wl_list_init(&ivi.outputs);
1621         wl_list_init(&ivi.surfaces);
1622         wl_list_init(&ivi.pending_surfaces);
1623         wl_list_init(&ivi.popup_pending_apps);
1624         wl_list_init(&ivi.fullscreen_pending_apps);
1625         wl_list_init(&ivi.split_pending_apps);
1626         wl_list_init(&ivi.remote_pending_apps);
1627         wl_list_init(&ivi.desktop_clients);
1628
1629         /* Prevent any clients we spawn getting our stdin */
1630         os_fd_set_cloexec(STDIN_FILENO);
1631
1632         cmdline = copy_command_line(argc, argv);
1633         parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1634
1635         if (help)
1636                 usage(EXIT_SUCCESS);
1637
1638         if (version) {
1639                 printf(PACKAGE_STRING "\n");
1640                 return EXIT_SUCCESS;
1641         }
1642
1643         log_ctx = weston_log_ctx_compositor_create();
1644         if (!log_ctx) {
1645                 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1646                 return ret;
1647         }
1648
1649         log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1650                                                     "agl-compositor log\n",
1651                                                     NULL, NULL, NULL);
1652
1653         log_file_open(log);
1654         weston_log_set_handler(vlog, vlog_continue);
1655
1656         logger = weston_log_subscriber_create_log(logfile);
1657         weston_log_subscribe(log_ctx, logger, "log");
1658
1659         weston_log("Command line: %s\n", cmdline);
1660         free(cmdline);
1661
1662         if (load_config(&ivi.config, no_config, config_file) < 0)
1663                 goto error_signals;
1664         section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1665         if (!backend) {
1666                 weston_config_section_get_string(section, "backend", &backend,
1667                                                  NULL);
1668                 if (!backend)
1669                         backend = choose_default_backend();
1670         }
1671         /* from [core] */
1672         weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
1673         weston_config_section_get_bool(section, "activate-by-default", &ivi.activate_by_default, true);
1674
1675         display = wl_display_create();
1676         loop = wl_display_get_event_loop(display);
1677
1678         wl_display_set_global_filter(display,
1679                                      global_filter, &ivi);
1680
1681         /* Register signal handlers so we shut down cleanly */
1682
1683         signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1684                                               display);
1685         signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1686                                               display);
1687         signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1688                                               display);
1689
1690         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1691                 if (!signals[i])
1692                         goto error_signals;
1693
1694         ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1695         if (!ivi.compositor) {
1696                 weston_log("fatal: failed to create compositor.\n");
1697                 goto error_signals;
1698         }
1699
1700         if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1701                 goto error_compositor;
1702
1703         if (load_backend(&ivi, backend, &argc, argv) < 0) {
1704                 weston_log("fatal: failed to create compositor backend.\n");
1705                 goto error_compositor;
1706         }
1707
1708         ivi.heads_changed.notify = heads_changed;
1709         weston_compositor_add_heads_changed_listener(ivi.compositor,
1710                                                      &ivi.heads_changed);
1711
1712         if (ivi_desktop_init(&ivi) < 0)
1713                 goto error_compositor;
1714
1715         ivi_seat_init(&ivi);
1716
1717         /* load additional modules */
1718         weston_config_section_get_string(section, "modules", &modules, "");
1719         if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
1720                 goto error_compositor;
1721
1722         if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
1723                 goto error_compositor;
1724
1725         if (ivi_policy_init(&ivi) < 0)
1726                 goto error_compositor;
1727
1728         if (ivi_shell_init(&ivi) < 0)
1729                 goto error_compositor;
1730
1731         add_bindings(ivi.compositor);
1732
1733         weston_compositor_flush_heads_changed(ivi.compositor);
1734
1735         if (ivi.remoting_api)
1736                 ivi_enable_remote_outputs(&ivi);
1737
1738         if (ivi.waltham_transmitter_api)
1739                 ivi_enable_waltham_outputs(&ivi);
1740
1741         if (create_listening_socket(display, socket_name) < 0)
1742                 goto error_compositor;
1743
1744         ivi_shell_init_black_fs(&ivi);
1745
1746         ivi.compositor->exit = handle_exit;
1747
1748         weston_compositor_wake(ivi.compositor);
1749
1750         ivi_shell_create_global(&ivi);
1751         ivi_launch_shell_client(&ivi);
1752         if (debug)
1753                 ivi_screenshooter_create(&ivi);
1754         ivi_agl_systemd_notify(&ivi);
1755
1756         wl_display_run(display);
1757
1758         ret = ivi.compositor->exit_code;
1759
1760         wl_display_destroy_clients(display);
1761
1762 error_compositor:
1763         weston_compositor_tear_down(ivi.compositor);
1764
1765         weston_compositor_log_scope_destroy(log_scope);
1766         log_scope = NULL;
1767
1768         weston_log_ctx_compositor_destroy(ivi.compositor);
1769         weston_compositor_destroy(ivi.compositor);
1770
1771         weston_log_subscriber_destroy_log(logger);
1772
1773         ivi_policy_destroy(ivi.policy);
1774
1775 error_signals:
1776         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1777                 if (signals[i])
1778                         wl_event_source_remove(signals[i]);
1779
1780         wl_display_destroy(display);
1781
1782         log_file_close();
1783         if (ivi.config)
1784                 weston_config_destroy(ivi.config);
1785
1786         return ret;
1787 }