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