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