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