meson.build, src: update for weston 11
[src/agl-compositor.git] / src / compositor.c
1 /*
2  * Copyright © 2012-2021 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <linux/input.h>
39
40 #include <libweston/backend-drm.h>
41 #include <libweston/backend-wayland.h>
42 #ifdef HAVE_BACKEND_RDP
43 #include <libweston/backend-rdp.h>
44 #endif
45 #ifdef HAVE_BACKEND_HEADLESS
46 #include <libweston/backend-headless.h>
47 #endif
48 #ifdef HAVE_BACKEND_X11
49 #include <libweston/backend-x11.h>
50 #endif
51 #include <libweston/libweston.h>
52 #include <libweston/windowed-output-api.h>
53 #include <libweston/config-parser.h>
54 #include <libweston/weston-log.h>
55 #include <weston.h>
56
57 #include "shared/os-compatibility.h"
58 #include "shared/helpers.h"
59
60 #include "agl-shell-server-protocol.h"
61
62 #ifdef HAVE_REMOTING
63 #include "remote.h"
64 #endif
65
66 static int cached_tm_mday = -1;
67 static struct weston_log_scope *log_scope;
68
69 struct ivi_compositor *
70 to_ivi_compositor(struct weston_compositor *ec)
71 {
72         return weston_compositor_get_user_data(ec);
73 }
74
75 struct ivi_output_config *
76 ivi_init_parsed_options(struct weston_compositor *compositor)
77 {
78         struct ivi_compositor *ivi = to_ivi_compositor(compositor);
79         struct ivi_output_config *config;
80
81         config = zalloc(sizeof *config);
82         if (!config)
83                 return NULL;
84
85         config->width = 0;
86         config->height = 0;
87         config->scale = 0;
88         config->transform = UINT32_MAX;
89
90         ivi->parsed_options = config;
91
92         return config;
93 }
94
95
96
97 static void
98 sigint_helper(int sig)
99 {
100        raise(SIGUSR2);
101 }
102
103 void
104 ivi_layout_save(struct ivi_compositor *ivi, struct ivi_output *output)
105 {
106         struct ivi_output *new_output;
107         ivi->need_ivi_output_relayout = true;
108
109         new_output = zalloc(sizeof(*new_output));
110
111         new_output->ivi = ivi;
112         new_output->background = output->background;
113
114         new_output->top = output->top;
115         new_output->bottom = output->bottom;
116         new_output->left = output->left;
117         new_output->right = output->right;
118
119         new_output->active = output->active;
120         new_output->previous_active = output->previous_active;
121         new_output->name = strdup(output->name);
122         if (output->app_ids)
123                 new_output->app_ids = strdup(output->app_ids);
124
125         new_output->area = output->area;
126         new_output->area_saved = output->area_saved;
127         new_output->area_activation = output->area_activation;
128
129         weston_log("saving output layout for output %s\n", new_output->name);
130
131         wl_list_insert(&ivi->saved_outputs, &new_output->link);
132 }
133
134 void
135 ivi_layout_restore(struct ivi_compositor *ivi, struct ivi_output *n_output)
136 {
137         struct ivi_output *output = NULL;
138         struct ivi_output *iter_output;
139
140         if (!ivi->need_ivi_output_relayout)
141                 return;
142
143         ivi->need_ivi_output_relayout = false;
144
145         wl_list_for_each(iter_output, &ivi->saved_outputs, link) {
146                 if (strcmp(n_output->name, iter_output->name) == 0) {
147                         output = iter_output;
148                         break;
149                 }
150         }
151
152         if (!output)
153                 return;
154
155         weston_log("restoring output layout for output %s\n", output->name);
156         n_output->background = output->background;
157
158         n_output->top = output->top;
159         n_output->bottom = output->bottom;
160         n_output->left = output->left;
161         n_output->right = output->right;
162
163         n_output->active = output->active;
164         n_output->previous_active = output->previous_active;
165         if (output->app_ids)
166                 n_output->app_ids = strdup(output->app_ids);
167
168         n_output->area = output->area;
169         n_output->area_saved = output->area_saved;
170         n_output->area_activation = output->area_activation;
171
172         free(output->app_ids);
173         free(output->name);
174         wl_list_remove(&output->link);
175         free(output);
176 }
177
178 void
179 ivi_layout_destroy_saved_outputs(struct ivi_compositor *ivi)
180 {
181         struct ivi_output *output, *output_next;
182
183         wl_list_for_each_safe(output, output_next, &ivi->saved_outputs, link) {
184                 free(output->app_ids);
185                 free(output->name);
186
187                 wl_list_remove(&output->link);
188                 free(output);
189         }
190 }
191
192 static void
193 handle_output_destroy(struct wl_listener *listener, void *data)
194 {
195         struct ivi_output *output;
196
197         output = wl_container_of(listener, output, output_destroy);
198         assert(output->output == data);
199
200         if (output->fullscreen_view.fs &&
201             output->fullscreen_view.fs->view) {
202                 weston_surface_unref(output->fullscreen_view.fs->view->surface);
203                 weston_buffer_destroy_solid(output->fullscreen_view.buffer_ref);
204                 output->fullscreen_view.fs->view = NULL;
205         }
206
207         ivi_layout_save(output->ivi, output);
208
209         output->output = NULL;
210         wl_list_remove(&output->output_destroy.link);
211 }
212
213 struct ivi_output *
214 to_ivi_output(struct weston_output *o)
215 {
216         struct wl_listener *listener;
217         struct ivi_output *output;
218
219         listener = weston_output_get_destroy_listener(o, handle_output_destroy);
220         output = wl_container_of(listener, output, output_destroy);
221
222         return output;
223 }
224
225 static void
226 ivi_output_configure_app_id(struct ivi_output *ivi_output)
227 {
228         if (ivi_output->config) {
229                 if (ivi_output->app_ids != NULL)
230                         return;
231
232                 weston_config_section_get_string(ivi_output->config,
233                                                  "agl-shell-app-id",
234                                                  &ivi_output->app_ids,
235                                                  NULL);
236
237                 if (ivi_output->app_ids == NULL)
238                         return;
239
240                 weston_log("Will place app_id %s on output %s\n",
241                                 ivi_output->app_ids, ivi_output->name);
242         }
243 }
244
245 static struct ivi_output *
246 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
247                   struct weston_config_section *config,
248                   struct weston_head *head)
249 {
250         struct ivi_output *output = NULL;
251         wl_list_for_each(output, &ivi->outputs, link) {
252                 if (strcmp(output->name, name) == 0) {
253                         free(name);
254                         return output;
255                 }
256         }
257
258         output = zalloc(sizeof *output);
259         if (!output) {
260                 free(name);
261                 return NULL;
262         }
263
264         output->ivi = ivi;
265         output->name = name;
266         output->config = config;
267
268         if (ivi->simple_output_configure) {
269                 output->output =
270                         weston_compositor_create_output_with_head(ivi->compositor,
271                                                                   head);
272                 if (!output->output) {
273                         free(output->name);
274                         free(output);
275                         return NULL;
276                 }
277
278                 int ret = ivi->simple_output_configure(output->output);
279                 if (ret < 0) {
280                         weston_log("Configuring output \"%s\" failed.\n",
281                                         weston_head_get_name(head));
282                         weston_output_destroy(output->output);
283                         ivi->init_failed = true;
284                         return NULL;
285                 }
286
287                 if (weston_output_enable(output->output) < 0) {
288                         weston_log("Enabling output \"%s\" failed.\n",
289                                         weston_head_get_name(head));
290                         weston_output_destroy(output->output);
291                         ivi->init_failed = true;
292                         return NULL;
293                 }
294
295         } else {
296                 output->output =
297                         weston_compositor_create_output(ivi->compositor, name);
298                 if (!output->output) {
299                         free(output->name);
300                         free(output);
301                         return NULL;
302                 }
303         }
304
305         output->output_destroy.notify = handle_output_destroy;
306         weston_output_add_destroy_listener(output->output,
307                                            &output->output_destroy);
308
309         wl_list_insert(&ivi->outputs, &output->link);
310         ivi_output_configure_app_id(output);
311         return output;
312 }
313
314 static int
315 count_heads(struct weston_output *output)
316 {
317         struct weston_head *iter = NULL;
318         int n = 0;
319
320         while ((iter = weston_output_iterate_heads(output, iter)))
321                 ++n;
322
323         return n;
324 }
325
326 static void
327 handle_head_destroy(struct wl_listener *listener, void *data)
328 {
329         struct weston_head *head = data;
330         struct weston_output *output;
331
332         wl_list_remove(&listener->link);
333         free(listener);
334
335         output = weston_head_get_output(head);
336
337         /* On shutdown path, the output might be already gone. */
338         if (!output)
339                 return;
340
341         /* We're the last head */
342         if (count_heads(output) <= 1)
343                 weston_output_destroy(output);
344 }
345
346 static void
347 add_head_destroyed_listener(struct weston_head *head)
348 {
349         /* We already have a destroy listener */
350         if (weston_head_get_destroy_listener(head, handle_head_destroy))
351                 return;
352
353         struct wl_listener *listener = zalloc(sizeof *listener);
354         if (!listener)
355                 return;
356
357         listener->notify = handle_head_destroy;
358         weston_head_add_destroy_listener(head, listener);
359 }
360
361 static int
362 drm_configure_output(struct ivi_output *output)
363 {
364         struct ivi_compositor *ivi = output->ivi;
365         struct weston_config_section *section = output->config;
366         enum weston_drm_backend_output_mode mode =
367                 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
368         char *modeline = NULL;
369         char *gbm_format = NULL;
370         char *seat = NULL;
371
372         if (section) {
373                 char *m;
374                 weston_config_section_get_string(section, "mode", &m, "preferred");
375
376                 /* This should have been handled earlier */
377                 assert(strcmp(m, "off") != 0);
378
379                 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
380                         mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
381                 } else if (strcmp(m, "preferred") != 0) {
382                         modeline = m;
383                         m = NULL;
384                 }
385                 free(m);
386
387                 weston_config_section_get_string(section, "gbm-format",
388                                                  &gbm_format, NULL);
389
390                 weston_config_section_get_string(section, "seat", &seat, "");
391         }
392
393         if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
394                 weston_log("Cannot configure output using weston_drm_output_api.\n");
395                 free(modeline);
396                 return -1;
397         }
398         free(modeline);
399
400         ivi->drm_api->set_gbm_format(output->output, gbm_format);
401         free(gbm_format);
402
403         ivi->drm_api->set_seat(output->output, seat);
404         free(seat);
405
406         return 0;
407 }
408
409 #define WINDOWED_DEFAULT_WIDTH 1024
410 #define WINDOWED_DEFAULT_HEIGHT 768
411
412 static int
413 windowed_configure_output(struct ivi_output *output)
414 {
415         struct ivi_compositor *ivi = output->ivi;
416         struct weston_config_section *section = output->config;
417         int width = WINDOWED_DEFAULT_WIDTH;
418         int height = WINDOWED_DEFAULT_HEIGHT;
419
420         if (section) {
421                 char *mode;
422
423                 weston_config_section_get_string(section, "mode", &mode, NULL);
424                 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
425                         weston_log("Invalid mode for output %s. Using defaults.\n",
426                                    output->name);
427                         width = WINDOWED_DEFAULT_WIDTH;
428                         height = WINDOWED_DEFAULT_HEIGHT;
429                 }
430                 free(mode);
431         }
432
433         if (ivi->cmdline.width)
434                 width = ivi->cmdline.width;
435         if (ivi->cmdline.height)
436                 height = ivi->cmdline.height;
437         if (ivi->cmdline.scale)
438                 weston_output_set_scale(output->output, ivi->cmdline.scale);
439
440         if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
441                 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
442                            output->name);
443                 return -1;
444         }
445
446         weston_log("Configured windowed_output_api to %dx%d\n", width, height);
447
448         return 0;
449 }
450
451 static int
452 parse_transform(const char *transform, uint32_t *out)
453 {
454         static const struct { const char *name; uint32_t token; } transforms[] = {
455                 { "normal",             WL_OUTPUT_TRANSFORM_NORMAL },
456                 { "rotate-90",          WL_OUTPUT_TRANSFORM_90 },
457                 { "rotate-180",         WL_OUTPUT_TRANSFORM_180 },
458                 { "rotate-270",         WL_OUTPUT_TRANSFORM_270 },
459                 { "flipped",            WL_OUTPUT_TRANSFORM_FLIPPED },
460                 { "flipped-rotate-90",  WL_OUTPUT_TRANSFORM_FLIPPED_90 },
461                 { "flipped-rotate-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
462                 { "flipped-rotate-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
463         };
464
465         for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
466                 if (strcmp(transforms[i].name, transform) == 0) {
467                         *out = transforms[i].token;
468                         return 0;
469                 }
470
471         *out = WL_OUTPUT_TRANSFORM_NORMAL;
472         return -1;
473 }
474
475 int
476 parse_activation_area(const char *geometry, struct ivi_output *output)
477 {
478         int n;
479         unsigned width, height, x, y;
480
481         n = sscanf(geometry, "%ux%u+%u,%u", &width, &height, &x, &y);
482         if (n != 4) {
483                 return -1;
484         }
485         output->area_activation.width = width;
486         output->area_activation.height = height;
487         output->area_activation.x = x;
488         output->area_activation.y = y;
489         return 0;
490 }
491
492 static int
493 configure_output(struct ivi_output *output)
494 {
495         struct ivi_compositor *ivi = output->ivi;
496         struct weston_config_section *section = output->config;
497         int32_t scale = 1;
498         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
499
500         /*
501          * This can happen with the wayland backend with 'sprawl'. The config
502          * is hard-coded, so we don't need to do anything.
503          */
504         if (!ivi->drm_api && !ivi->window_api)
505                 return 0;
506
507         if (section) {
508                 char *t;
509
510                 weston_config_section_get_int(section, "scale", &scale, 1);
511                 weston_config_section_get_string(section, "transform", &t, "normal");
512                 if (parse_transform(t, &transform) < 0)
513                         weston_log("Invalid transform \"%s\" for output %s\n",
514                                    t, output->name);
515                 weston_config_section_get_string(section, "activation-area", &t, "");
516                 if (parse_activation_area(t, output) < 0)
517                         weston_log("Invalid activation-area \"%s\" for output %s\n",
518                                    t, output->name);
519                 free(t);
520         }
521
522         weston_output_set_scale(output->output, scale);
523         weston_output_set_transform(output->output, transform);
524
525         if (ivi->drm_api)
526                 return drm_configure_output(output);
527         else
528                 return windowed_configure_output(output);
529 }
530
531 /*
532  * Reorgainizes the output's add array into two sections.
533  * add[0..ret-1] are the heads that failed to get attached.
534  * add[ret..add_len] are the heads that were successfully attached.
535  *
536  * The order between elements in each section is stable.
537  */
538 static size_t
539 try_attach_heads(struct ivi_output *output)
540 {
541         size_t fail_len = 0;
542
543         for (size_t i = 1; i < output->add_len; i++) {
544                 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
545                         struct weston_head *tmp = output->add[i];
546                         memmove(&output->add[fail_len + 1], output->add[fail_len],
547                                 sizeof output->add[0] * (i - fail_len));
548                         output->add[fail_len++] = tmp;
549                 }
550         }
551         return fail_len;
552 }
553
554 /* Place output exactly to the right of the most recently enabled output.
555  *
556  * Historically, we haven't given much thought to output placement,
557  * simply adding outputs in a horizontal line as they're enabled. This
558  * function simply sets an output's x coordinate to the right of the
559  * most recently enabled output, and its y to zero.
560  *
561  * If you're adding new calls to this function, you're also not giving
562  * much thought to output placement, so please consider carefully if
563  * it's really doing what you want.
564  *
565  * You especially don't want to use this for any code that won't
566  * immediately enable the passed output.
567  */
568 static void
569 weston_output_lazy_align(struct weston_output *output)
570 {
571         struct weston_compositor *c;
572         struct weston_output *peer;
573         int next_x = 0;
574
575         /* Put this output to the right of the most recently enabled output */
576         c = output->compositor;
577         if (!wl_list_empty(&c->output_list)) {
578                 peer = container_of(c->output_list.prev,
579                                 struct weston_output, link);
580                 next_x = peer->x + peer->width;
581         }
582         output->x = next_x;
583         output->y = 0;
584 }
585
586
587 /*
588  * Like try_attach_heads, this reorganizes the output's add array into a failed
589  * and successful section.
590  * i is the number of heads that already failed the previous step.
591  */
592 static size_t
593 try_enable_output(struct ivi_output *output, size_t i)
594 {
595         for (; i < output->add_len; ++i) {
596                 struct weston_head *head;
597
598                 weston_output_lazy_align(output->output);
599
600                 if (weston_output_enable(output->output) == 0)
601                         break;
602
603                 head = output->add[output->add_len - 1];
604                 memmove(&output->add[i + 1], &output->add[i],
605                         sizeof output->add[0] * (output->add_len - i));
606                 output->add[i] = head;
607
608                 weston_head_detach(head);
609         }
610
611         return i;
612 }
613
614 static int
615 try_attach_enable_heads(struct ivi_output *output)
616 {
617         size_t fail_len;
618         assert(!output->output->enabled);
619
620         fail_len = try_attach_heads(output);
621
622         if (configure_output(output) < 0)
623                 return -1;
624
625         fail_len = try_enable_output(output, fail_len);
626
627         /* All heads failed to be attached */
628         if (fail_len == output->add_len)
629                 return -1;
630
631         /* For each successful head attached */
632         for (size_t i = fail_len; i < output->add_len; ++i)
633                 add_head_destroyed_listener(output->add[i]);
634
635         ivi_layout_restore(output->ivi, output);
636
637         output->add_len = fail_len;
638         return 0;
639 }
640
641 static int
642 process_output(struct ivi_output *output)
643 {
644         if (output->output->enabled) {
645                 output->add_len = try_attach_heads(output);
646                 return output->add_len == 0 ? 0 : -1;
647         }
648
649         return try_attach_enable_heads(output);
650 }
651
652 static void
653 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
654 {
655         struct weston_output *output;
656         struct ivi_output *ivi_output;
657         struct wl_listener *listener;
658
659         output = weston_head_get_output(head);
660         assert(output);
661
662         listener = weston_output_get_destroy_listener(output,
663                                                       handle_output_destroy);
664         assert(listener);
665
666         ivi_output = wl_container_of(listener, ivi_output, output_destroy);
667         assert(ivi_output->output == output);
668
669         weston_head_detach(head);
670         if (count_heads(ivi_output->output) == 0) {
671                 if (ivi_output->output) {
672                         /* ivi_output->output destruction may be deferred in
673                          * some cases (see drm_output_destroy()), so we need to
674                          * forcibly trigger the destruction callback now, or
675                          * otherwise would later access data that we are about
676                          * to free
677                          */
678                         struct weston_output *save = ivi_output->output;
679
680                         handle_output_destroy(&ivi_output->output_destroy, save);
681                         weston_output_destroy(save);
682                 }
683         }
684         wl_list_remove(&ivi_output->link);
685         free(ivi_output->app_ids);
686         free(ivi_output);
687 }
688
689 static struct weston_config_section *
690 find_controlling_output_config(struct weston_config *config,
691                                const char *name)
692 {
693         struct weston_config_section *section;
694         char *same_as;
695         int depth = 0;
696
697         same_as = strdup(name);
698         do {
699                 section = weston_config_get_section(config, "output",
700                                                     "name", same_as);
701                 if (!section && depth > 0)
702                         weston_log("Configuration error: output section reffered"
703                                    "to by same-as=%s not found.\n", same_as);
704                 free(same_as);
705
706                 if (!section)
707                         return NULL;
708
709                 if (depth++ > 8) {
710                         weston_log("Configuration error: same-as nested too "
711                                    "deep for output '%s'.\n", name);
712                         return NULL;
713                 }
714
715                 weston_config_section_get_string(section, "same-as",
716                                                  &same_as, NULL);
717         } while (same_as);
718
719         return section;
720 }
721
722 static void
723 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
724 {
725         const char *name = weston_head_get_name(head);
726         struct weston_config_section *section;
727         struct ivi_output *output;
728         char *output_name = NULL;
729
730
731         section = find_controlling_output_config(ivi->config, name);
732         if (section) {
733                 char *mode;
734
735                 weston_config_section_get_string(section, "mode", &mode, NULL);
736                 if (mode && strcmp(mode, "off") == 0) {
737                         free(mode);
738                         return;
739                 }
740                 free(mode);
741
742                 weston_config_section_get_string(section, "name",
743                                                  &output_name, NULL);
744         } else {
745                 output_name = strdup(name);
746         }
747
748         if (!output_name)
749                 return;
750
751         output = ivi_ensure_output(ivi, output_name, section, head);
752         if (!output)
753                 return;
754
755         if (output->add_len >= ARRAY_LENGTH(output->add))
756                 return;
757
758         output->add[output->add_len++] = head;
759 }
760
761 static void
762 heads_changed(struct wl_listener *listener, void *arg)
763 {
764         struct weston_compositor *compositor = arg;
765         struct weston_head *head = NULL;
766         struct ivi_compositor *ivi = to_ivi_compositor(compositor);
767         struct ivi_output *output;
768
769         while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
770                 bool connected = weston_head_is_connected(head);
771                 bool enabled = weston_head_is_enabled(head);
772                 bool changed = weston_head_is_device_changed(head);
773                 bool non_desktop = weston_head_is_non_desktop(head);
774
775                 if (connected && !enabled && !non_desktop)
776                         head_prepare_enable(ivi, head);
777                 else if (!connected && enabled)
778                         head_disable(ivi, head);
779                 else if (enabled && changed)
780                         weston_log("Detected a monitor change on head '%s', "
781                                    "not bothering to do anything about it.\n",
782                                    weston_head_get_name(head));
783
784                 weston_head_reset_device_changed(head);
785         }
786
787         wl_list_for_each(output, &ivi->outputs, link) {
788                 if (output->add_len == 0)
789                         continue;
790
791                 if (process_output(output) < 0) {
792                         output->add_len = 0;
793                         ivi->init_failed = true;
794                 }
795         }
796 }
797
798 #ifdef HAVE_REMOTING
799 static int
800 drm_backend_remoted_output_configure(struct weston_output *output,
801                                      struct weston_config_section *section,
802                                      char *modeline,
803                                      const struct weston_remoting_api *api)
804 {
805         char *gbm_format = NULL;
806         char *seat = NULL;
807         char *host = NULL;
808         char *pipeline = NULL;
809         int port, ret;
810         int32_t scale = 1;
811         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
812         char *trans;
813
814         ret = api->set_mode(output, modeline);
815         if (ret < 0) {
816                 weston_log("Cannot configure an output \"%s\" using "
817                                 "weston_remoting_api. Invalid mode\n",
818                                 output->name);
819                 return -1;
820         }
821
822         weston_config_section_get_int(section, "scale", &scale, 1);
823         weston_output_set_scale(output, scale);
824
825         weston_config_section_get_string(section, "transform", &trans, "normal");
826         if (parse_transform(trans, &transform) < 0) {
827                 weston_log("Invalid transform \"%s\" for output %s\n",
828                            trans, output->name);
829         }
830         weston_output_set_transform(output, transform);
831
832         weston_config_section_get_string(section, "gbm-format",
833                                          &gbm_format, NULL);
834         api->set_gbm_format(output, gbm_format);
835         free(gbm_format);
836
837         weston_config_section_get_string(section, "seat", &seat, "");
838
839         api->set_seat(output, seat);
840         free(seat);
841
842         weston_config_section_get_string(section, "gst-pipeline", &pipeline,
843                         NULL);
844         if (pipeline) {
845                 api->set_gst_pipeline(output, pipeline);
846                 free(pipeline);
847                 return 0;
848         }
849
850         weston_config_section_get_string(section, "host", &host, NULL);
851         weston_config_section_get_int(section, "port", &port, 0);
852         if (!host || port <= 0 || 65533 < port) {
853                 weston_log("Cannot configure an output \"%s\". "
854                                 "Need to specify gst-pipeline or "
855                                 "host and port (1-65533).\n", output->name);
856         }
857         api->set_host(output, host);
858         free(host);
859         api->set_port(output, port);
860
861         return 0;
862 }
863
864
865 static int
866 remote_output_init(struct ivi_output *ivi_output,
867                    struct weston_compositor *compositor,
868                    struct weston_config_section *section,
869                    const struct weston_remoting_api *api)
870 {
871         char *output_name, *modeline = NULL;
872         int ret = -1;
873
874         weston_config_section_get_string(section, "name", &output_name, NULL);
875         if (!output_name)
876                 return ret;
877
878         weston_config_section_get_string(section, "mode", &modeline, "off");
879         if (strcmp(modeline, "off") == 0)
880                 goto err;
881
882         ivi_output->output = api->create_output(compositor, output_name);
883         if (!ivi_output->output) {
884                 weston_log("Cannot create remoted output \"%s\".\n",
885                                 output_name);
886                 goto err;
887         }
888
889         ret = drm_backend_remoted_output_configure(ivi_output->output, section,
890                                                    modeline, api);
891         if (ret < 0) {
892                 weston_log("Cannot configure remoted output \"%s\".\n",
893                                 output_name);
894                 goto err;
895         }
896
897         if ((ret = weston_output_enable(ivi_output->output)) < 0) {
898                 weston_log("Enabling remoted output \"%s\" failed.\n",
899                                 output_name);
900                 goto err;
901         }
902
903         free(modeline);
904         free(output_name);
905         weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
906
907         return 0;
908
909 err:
910         free(modeline);
911         free(output_name);
912         if (ivi_output->output)
913                 weston_output_destroy(ivi_output->output);
914
915         return ret;
916 }
917
918 static void
919 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
920 {
921         struct weston_config_section *remote_section = NULL;
922         const char *section_name;
923         struct weston_config *config = ivi->config;
924
925         while (weston_config_next_section(config, &remote_section, &section_name)) {
926                 if (strcmp(section_name, "remote-output"))
927                         continue;
928
929                 struct ivi_output *ivi_output = NULL;
930                 bool output_found = false;
931                 char *_name = NULL;
932
933                 weston_config_section_get_string(remote_section,
934                                                  "name", &_name, NULL);
935                 wl_list_for_each(ivi_output, &ivi->outputs, link) {
936                         if (!strcmp(ivi_output->name, _name)) {
937                                 output_found = true;
938                                 break;
939                         }
940                 }
941
942                 if (output_found) {
943                         free(_name);
944                         continue;
945                 }
946
947                 ivi_output = zalloc(sizeof(*ivi_output));
948                 if (!ivi_output) {
949                         free(_name);
950                         continue;
951                 }
952
953                 ivi_output->ivi = ivi;
954                 ivi_output->name = _name;
955                 ivi_output->config = remote_section;
956                 ivi_output->type = OUTPUT_REMOTE;
957
958                 if (remote_output_init(ivi_output, ivi->compositor,
959                                        remote_section, ivi->remoting_api)) {
960                         free(ivi_output->name);
961                         free(ivi_output);
962                         continue;
963                 }
964
965                 ivi_output->output_destroy.notify = handle_output_destroy;
966                 weston_output_add_destroy_listener(ivi_output->output,
967                                                    &ivi_output->output_destroy);
968
969                 wl_list_insert(&ivi->outputs, &ivi_output->link);
970                 ivi_output_configure_app_id(ivi_output);
971         }
972 }
973
974 static int
975 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
976 {
977         struct weston_compositor *compositor = ivi->compositor;
978         int (*module_init)(struct weston_compositor *wc);
979
980         module_init = weston_load_module("remoting-plugin.so",
981                                          "weston_module_init");
982         if (!module_init)
983                 return -1;
984
985         if (module_init(compositor) < 0)
986                 return -1;
987
988         ivi->remoting_api = weston_remoting_get_api(compositor);
989         if (!ivi->remoting_api)
990                 return -1;
991         return 0;
992 }
993 #else
994 static int
995 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
996 {
997         return -1;
998 }
999 #endif
1000
1001 static int
1002 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1003 {
1004         struct weston_drm_backend_config config = {
1005                 .base = {
1006                         .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
1007                         .struct_size = sizeof config,
1008                 },
1009         };
1010         struct weston_config_section *section;
1011         int use_current_mode = 0;
1012         int use_pixman = 0;
1013         bool use_shadow;
1014         bool without_input = false;
1015         int ret;
1016
1017         const struct weston_option options[] = {
1018                 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
1019                 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
1020                 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
1021                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1022                 { WESTON_OPTION_BOOLEAN, "continue-without-input", false, &without_input }
1023         };
1024
1025         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1026         config.use_pixman = use_pixman;
1027         ivi->cmdline.use_current_mode = use_current_mode;
1028
1029         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
1030         weston_config_section_get_string(section, "gbm-format",
1031                                          &config.gbm_format, NULL);
1032         weston_config_section_get_uint(section, "pageflip-timeout",
1033                                        &config.pageflip_timeout, 0);
1034         weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
1035         config.use_pixman_shadow = use_shadow;
1036
1037         if (without_input)
1038                 ivi->compositor->require_input = !without_input;
1039
1040         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
1041                                              &config.base);
1042         if (ret < 0)
1043                 return ret;
1044
1045         ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
1046         if (!ivi->drm_api) {
1047                 weston_log("Cannot use drm output api.\n");
1048                 ret = -1;
1049                 goto error;
1050         }
1051
1052         load_remoting_plugin(ivi, ivi->config);
1053
1054 error:
1055         free(config.gbm_format);
1056         free(config.seat_id);
1057         return ret;
1058 }
1059
1060 static void
1061 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
1062                               bool *use_pixman, bool *fullscreen, int *output_count)
1063 {
1064         struct weston_config_section *section;
1065         bool pixman;
1066         int fs = 0;
1067
1068         const struct weston_option options[] = {
1069                 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
1070                 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
1071                 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
1072                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
1073                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
1074                 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
1075         };
1076
1077         section = weston_config_get_section(ivi->config, "core", NULL, NULL);
1078         weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
1079
1080         *output_count = 1;
1081         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1082         *use_pixman = pixman;
1083         *fullscreen = fs;
1084 }
1085
1086 static int
1087 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
1088                         const char *match_prefix, const char *name_prefix)
1089 {
1090         struct weston_config_section *section = NULL;
1091         const char *section_name;
1092         char *default_output = NULL;
1093         int i = 0;
1094         size_t match_len = strlen(match_prefix);
1095
1096         while (weston_config_next_section(ivi->config, &section, &section_name)) {
1097                 char *output_name;
1098
1099                 if (i >= output_count)
1100                         break;
1101
1102                 if (strcmp(section_name, "output") != 0)
1103                         continue;
1104
1105                 weston_config_section_get_string(section, "name", &output_name, NULL);
1106                 if (output_name == NULL)
1107                         continue;
1108                 if (strncmp(output_name, match_prefix, match_len) != 0) {
1109                         free(output_name);
1110                         continue;
1111                 }
1112
1113                 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
1114                         free(output_name);
1115                         return -1;
1116                 }
1117
1118                 free(output_name);
1119                 ++i;
1120         }
1121
1122         for (; i < output_count; ++i) {
1123                 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
1124                         return -1;
1125
1126                 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
1127                         free(default_output);
1128                         return -1;
1129                 }
1130
1131                 free(default_output);
1132         }
1133
1134         return 0;
1135 }
1136
1137 static int
1138 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1139 {
1140         struct weston_wayland_backend_config config = {
1141                 .base = {
1142                         .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
1143                         .struct_size = sizeof config,
1144                 },
1145         };
1146         struct weston_config_section *section;
1147         int sprawl = 0;
1148         int output_count;
1149         int ret;
1150
1151         const struct weston_option options[] = {
1152                 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1153                 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1154         };
1155
1156         windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1157                                       &config.fullscreen, &output_count);
1158
1159         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1160         config.sprawl = sprawl;
1161
1162         section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1163         weston_config_section_get_string(section, "cursor-theme",
1164                                          &config.cursor_theme, NULL);
1165         weston_config_section_get_int(section, "cursor-size",
1166                                       &config.cursor_size, 32);
1167
1168         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1169                                              &config.base);
1170
1171         free(config.cursor_theme);
1172         free(config.display_name);
1173
1174         if (ret < 0)
1175                 return ret;
1176
1177         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1178
1179         /*
1180          * We will just assume if load_backend() finished cleanly and
1181          * windowed_output_api is not present that wayland backend is started
1182          * with --sprawl or runs on fullscreen-shell. In this case, all values
1183          * are hardcoded, so nothing can be configured; simply create and
1184          * enable an output.
1185          */
1186         if (ivi->window_api == NULL)
1187                 return 0;
1188
1189         return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1190 }
1191
1192 #ifdef HAVE_BACKEND_X11
1193 static int
1194 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1195 {
1196         struct weston_x11_backend_config config = {
1197                 .base = {
1198                         .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1199                         .struct_size = sizeof config,
1200                 },
1201         };
1202         int no_input = 0;
1203         int output_count;
1204         int ret;
1205
1206         const struct weston_option options[] = {
1207                { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1208         };
1209
1210         windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1211                                       &config.fullscreen, &output_count);
1212
1213         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1214         config.no_input = no_input;
1215
1216         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1217                                              &config.base);
1218
1219         if (ret < 0)
1220                 return ret;
1221
1222         ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1223         if (!ivi->window_api) {
1224                 weston_log("Cannot use weston_windowed_output_api.\n");
1225                 return -1;
1226         }
1227
1228         return windowed_create_outputs(ivi, output_count, "X", "screen");
1229 }
1230 #else
1231 static int
1232 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1233 {
1234         return -1;
1235 }
1236 #endif
1237
1238 #ifdef HAVE_BACKEND_HEADLESS
1239 static int
1240 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1241 {
1242         struct weston_headless_backend_config config = {};
1243         int ret = 0;
1244
1245         bool use_pixman;
1246         bool fullscreen;
1247         bool use_gl;
1248         int output_count;
1249
1250         struct weston_compositor *c = ivi->compositor;
1251
1252         const struct weston_option options[] = {
1253                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1254                 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &use_gl },
1255         };
1256
1257         windowed_parse_common_options(ivi, argc, argv, &use_pixman,
1258                         &fullscreen, &output_count);
1259
1260         parse_options(options, ARRAY_LENGTH(options), argc, argv);
1261         config.use_pixman = use_pixman;
1262         config.use_gl = use_gl;
1263
1264         config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1265         config.base.struct_size = sizeof(struct weston_headless_backend_config);
1266
1267         /* load the actual headless-backend and configure it */
1268         ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1269                                              &config.base);
1270         if (ret < 0)
1271                 return ret;
1272
1273         ivi->window_api = weston_windowed_output_get_api(c);
1274         if (!ivi->window_api) {
1275                 weston_log("Cannot use weston_windowed_output_api.\n");
1276                 return -1;
1277         }
1278
1279         if (ivi->window_api->create_head(c, "headless") < 0) {
1280                 weston_log("Cannot create headless back-end\n");
1281                 return -1;
1282         }
1283
1284         return 0;
1285 }
1286 #else
1287 static int
1288 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1289 {
1290         return -1;
1291 }
1292 #endif
1293
1294 #ifdef HAVE_BACKEND_RDP
1295 static void
1296 weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
1297 {
1298         config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
1299         config->base.struct_size = sizeof(struct weston_rdp_backend_config);
1300
1301         config->bind_address = NULL;
1302         config->port = 3389;
1303         config->rdp_key = NULL;
1304         config->server_cert = NULL;
1305         config->server_key = NULL;
1306         config->env_socket = 0;
1307         config->no_clients_resize = 1;
1308         config->force_no_compression = 0;
1309 }
1310
1311 static int
1312 rdp_backend_output_configure(struct weston_output *output)
1313 {
1314         struct ivi_compositor *ivi = to_ivi_compositor(output->compositor);
1315         struct ivi_output_config *parsed_options = ivi->parsed_options;
1316         const struct weston_rdp_output_api *api =
1317                 weston_rdp_output_get_api(output->compositor);
1318         int width = 640;
1319         int height = 480;
1320         struct weston_config_section *section;
1321         uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
1322         char *transform_string;
1323
1324         assert(parsed_options);
1325
1326         if (!api) {
1327                 weston_log("Cannot use weston_rdp_output_api.\n");
1328                 return -1;
1329         }
1330
1331         section = weston_config_get_section(ivi->config, "rdp", NULL, NULL);
1332
1333         if (parsed_options->width)
1334                 width = parsed_options->width;
1335
1336         if (parsed_options->height)
1337                 height = parsed_options->height;
1338
1339         weston_output_set_scale(output, 1);
1340
1341         weston_config_section_get_int(section, "width",
1342                         &width, width);
1343
1344         weston_config_section_get_int(section, "height",
1345                         &height, height);
1346
1347         if (parsed_options->transform)
1348                 transform = parsed_options->transform;
1349
1350         weston_config_section_get_string(section, "transform",
1351                         &transform_string, "normal");
1352
1353         if (parse_transform(transform_string, &transform) < 0) {
1354                 weston_log("Invalid transform \"%s\" for output %s\n",
1355                            transform_string, output->name);
1356                 return -1;
1357         }
1358
1359         weston_output_set_transform(output, transform);
1360
1361         if (api->output_set_size(output, width, height) < 0) {
1362                 weston_log("Cannot configure output \"%s\" using weston_rdp_output_api.\n",
1363                                 output->name);
1364                 return -1;
1365         }
1366
1367         return 0;
1368 }
1369
1370 static int
1371 load_rdp_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1372 {
1373         struct weston_rdp_backend_config config = {};
1374         int ret = 0;
1375         struct weston_config_section *section;
1376
1377         struct ivi_output_config *parsed_options = ivi_init_parsed_options(ivi->compositor);
1378         if (!parsed_options)
1379                 return -1;
1380
1381         weston_rdp_backend_config_init(&config);
1382
1383         const struct weston_option rdp_options[] = {
1384                 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1385                 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1386                 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
1387                 { WESTON_OPTION_INTEGER, "transform", 0, &parsed_options->transform },
1388                 { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
1389                 { WESTON_OPTION_STRING,  "address", 0, &config.bind_address },
1390                 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1391                 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1392                 { WESTON_OPTION_STRING,  "rdp4-key", 0, &config.rdp_key },
1393                 { WESTON_OPTION_STRING,  "rdp-tls-cert", 0, &config.server_cert },
1394                 { WESTON_OPTION_STRING,  "rdp-tls-key", 0, &config.server_key },
1395                 { WESTON_OPTION_BOOLEAN, "force-no-compression", 0, &config.force_no_compression },
1396         };
1397
1398         section = weston_config_get_section(ivi->config, "rdp", NULL, NULL);
1399
1400         weston_config_section_get_string(section, "tls-cert",
1401                         &config.server_cert, config.server_cert);
1402
1403         weston_config_section_get_string(section, "tls-key",
1404                         &config.server_key, config.server_key);
1405
1406
1407         parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1408
1409         ivi->simple_output_configure = rdp_backend_output_configure;
1410         ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_RDP, &config.base);
1411
1412         free(config.bind_address);
1413         free(config.rdp_key);
1414         free(config.server_cert);
1415         free(config.server_key);
1416
1417         return ret;
1418 }
1419 #else
1420 static int
1421 load_rdp_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1422 {
1423         return -1;
1424 }
1425 #endif
1426
1427
1428 static int
1429 load_backend(struct ivi_compositor *ivi, const char *backend,
1430              int *argc, char *argv[])
1431 {
1432         if (strcmp(backend, "drm-backend.so") == 0) {
1433                 return load_drm_backend(ivi, argc, argv);
1434         } else if (strcmp(backend, "wayland-backend.so") == 0) {
1435                 return load_wayland_backend(ivi, argc, argv);
1436         } else if (strcmp(backend, "x11-backend.so") == 0) {
1437                 return load_x11_backend(ivi, argc, argv);
1438         } else if (strcmp(backend, "headless-backend.so") == 0) {
1439                 return load_headless_backend(ivi, argc, argv);
1440         } else if (strcmp(backend, "rdp-backend.so") == 0) {
1441                 return load_rdp_backend(ivi, argc, argv);
1442         }
1443
1444         weston_log("fatal: unknown backend '%s'.\n", backend);
1445         return -1;
1446 }
1447
1448 static int
1449 load_modules(struct ivi_compositor *ivi, const char *modules,
1450              int *argc, char *argv[], bool *xwayland)
1451 {
1452         const char *p, *end;
1453         char buffer[256];
1454         int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1455
1456         if (modules == NULL)
1457                 return 0;
1458
1459         p = modules;
1460         while (*p) {
1461                 end = strchrnul(p, ',');
1462                 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1463
1464                 if (strstr(buffer, "xwayland.so")) {
1465                         *xwayland = true;
1466                 } else if (strstr(buffer, "systemd-notify.so")) {
1467                         weston_log("systemd-notify plug-in already loaded!\n");
1468                 } else {
1469                         module_init = weston_load_module(buffer, "wet_module_init");
1470                         if (!module_init)
1471                                 return -1;
1472
1473                         if (module_init(ivi->compositor, *argc, argv) < 0)
1474                                 return -1;
1475
1476                 }
1477
1478                 p = end;
1479                 while (*p == ',')
1480                         p++;
1481         }
1482
1483         return 0;
1484 }
1485
1486
1487 static char *
1488 choose_default_backend(void)
1489 {
1490         char *backend = NULL;
1491
1492         if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1493                 backend = strdup("wayland-backend.so");
1494         else if (getenv("DISPLAY"))
1495                 backend = strdup("x11-backend.so");
1496         else
1497                 backend = strdup("drm-backend.so");
1498
1499         return backend;
1500 }
1501
1502 static int
1503 compositor_init_config(struct ivi_compositor *ivi)
1504 {
1505         struct xkb_rule_names xkb_names;
1506         struct weston_config_section *section;
1507         struct weston_compositor *compositor = ivi->compositor;
1508         struct weston_config *config = ivi->config;
1509         int repaint_msec;
1510         bool vt_switching;
1511         bool require_input;
1512
1513         /* agl-compositor.ini [keyboard] */
1514         section = weston_config_get_section(config, "keyboard", NULL, NULL);
1515         weston_config_section_get_string(section, "keymap_rules",
1516                                          (char **) &xkb_names.rules, NULL);
1517         weston_config_section_get_string(section, "keymap_model",
1518                                          (char **) &xkb_names.model, NULL);
1519         weston_config_section_get_string(section, "keymap_layout",
1520                                          (char **) &xkb_names.layout, NULL);
1521         weston_config_section_get_string(section, "keymap_variant",
1522                                          (char **) &xkb_names.variant, NULL);
1523         weston_config_section_get_string(section, "keymap_options",
1524                                          (char **) &xkb_names.options, NULL);
1525
1526         if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1527                 return -1;
1528
1529         weston_config_section_get_int(section, "repeat-rate",
1530                                       &compositor->kb_repeat_rate, 40);
1531         weston_config_section_get_int(section, "repeat-delay",
1532                                       &compositor->kb_repeat_delay, 400);
1533
1534         weston_config_section_get_bool(section, "vt-switching",
1535                                        &vt_switching, false);
1536         compositor->vt_switching = vt_switching;
1537
1538         /* agl-compositor.ini [core] */
1539         section = weston_config_get_section(config, "core", NULL, NULL);
1540
1541         weston_config_section_get_bool(section, "disable-cursor",
1542                                        &ivi->disable_cursor, false);
1543         weston_config_section_get_bool(section, "activate-by-default",
1544                                        &ivi->activate_by_default, true);
1545
1546         weston_config_section_get_bool(section, "require-input", &require_input, true);
1547         compositor->require_input = require_input;
1548
1549         weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1550                                       compositor->repaint_msec);
1551         if (repaint_msec < -10 || repaint_msec > 1000) {
1552                 weston_log("Invalid repaint_window value in config: %d\n",
1553                            repaint_msec);
1554         } else {
1555                 compositor->repaint_msec = repaint_msec;
1556         }
1557         weston_log("Output repaint window is %d ms maximum.\n",
1558                    compositor->repaint_msec);
1559
1560         return 0;
1561 }
1562
1563 struct ivi_surface *
1564 to_ivi_surface(struct weston_surface *surface)
1565 {
1566         struct weston_desktop_surface *dsurface;
1567
1568         dsurface = weston_surface_get_desktop_surface(surface);
1569         if (!dsurface)
1570                 return NULL;
1571
1572         return weston_desktop_surface_get_user_data(dsurface);
1573 }
1574
1575 static void
1576 activate_binding(struct weston_seat *seat,
1577                  struct weston_view *focus_view, uint32_t flags)
1578 {
1579         struct weston_surface *focus_surface;
1580         struct weston_surface *main_surface;
1581         struct ivi_surface *ivi_surface;
1582         struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(seat);
1583
1584         if (!focus_view)
1585                 return;
1586
1587         focus_surface = focus_view->surface;
1588         main_surface = weston_surface_get_main_surface(focus_surface);
1589
1590         ivi_surface = to_ivi_surface(main_surface);
1591         if (!ivi_surface)
1592                 return;
1593
1594         if (ivi_seat)
1595                 ivi_shell_activate_surface(ivi_surface, ivi_seat, flags);
1596 }
1597
1598 static void
1599 click_to_activate_binding(struct weston_pointer *pointer,
1600                           const struct timespec *time,
1601                           uint32_t button, void *data)
1602 {
1603         if (pointer->grab != &pointer->default_grab)
1604                 return;
1605         if (pointer->focus == NULL)
1606                 return;
1607
1608         activate_binding(pointer->seat, pointer->focus,
1609                          WESTON_ACTIVATE_FLAG_CLICKED);
1610 }
1611
1612 static void
1613 touch_to_activate_binding(struct weston_touch *touch,
1614                           const struct timespec *time,
1615                           void *data)
1616 {
1617         if (touch->grab != &touch->default_grab)
1618                 return;
1619         if (touch->focus == NULL)
1620                 return;
1621
1622         activate_binding(touch->seat, touch->focus,
1623                          WESTON_ACTIVATE_FLAG_NONE);
1624 }
1625
1626 static void
1627 add_bindings(struct weston_compositor *compositor)
1628 {
1629         weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1630                                              click_to_activate_binding,
1631                                              NULL);
1632         weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1633                                              click_to_activate_binding,
1634                                              NULL);
1635         weston_compositor_add_touch_binding(compositor, 0,
1636                                             touch_to_activate_binding,
1637                                             NULL);
1638 }
1639
1640 static int
1641 create_listening_socket(struct wl_display *display, const char *socket_name)
1642 {
1643         if (socket_name) {
1644                 if (wl_display_add_socket(display, socket_name)) {
1645                         weston_log("fatal: failed to add socket: %s\n",
1646                                    strerror(errno));
1647                         return -1;
1648                 }
1649         } else {
1650                 socket_name = wl_display_add_socket_auto(display);
1651                 if (!socket_name) {
1652                         weston_log("fatal: failed to add socket: %s\n",
1653                                    strerror(errno));
1654                         return -1;
1655                 }
1656         }
1657
1658         setenv("WAYLAND_DISPLAY", socket_name, 1);
1659
1660         return 0;
1661 }
1662
1663 static bool
1664 global_filter(const struct wl_client *client, const struct wl_global *global,
1665               void *data)
1666 {
1667         return true;
1668 }
1669
1670 static int
1671 load_config(struct weston_config **config, bool no_config,
1672             const char *config_file)
1673 {
1674         const char *file = "agl-compositor.ini";
1675         const char *full_path;
1676
1677         if (config_file)
1678                 file = config_file;
1679
1680         if (!no_config)
1681                 *config = weston_config_parse(file);
1682
1683         if (*config) {
1684                 full_path = weston_config_get_full_path(*config);
1685
1686                 weston_log("Using config file '%s'.\n", full_path);
1687                 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1688
1689                 return 0;
1690         }
1691
1692         if (config_file && !no_config) {
1693                 weston_log("fatal: error opening or reading config file '%s'.\n",
1694                         config_file);
1695
1696                 return -1;
1697         }
1698
1699         weston_log("Starting with no config file.\n");
1700         setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1701
1702         return 0;
1703 }
1704
1705 static FILE *logfile;
1706
1707 static char *
1708 log_timestamp(char *buf, size_t len)
1709 {
1710         struct timeval tv;
1711         struct tm *brokendown_time;
1712         char datestr[128];
1713         char timestr[128];
1714
1715         gettimeofday(&tv, NULL);
1716
1717         brokendown_time = localtime(&tv.tv_sec);
1718         if (brokendown_time == NULL) {
1719                 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1720                 return buf;
1721         }
1722
1723         memset(datestr, 0, sizeof(datestr));
1724         if (brokendown_time->tm_mday != cached_tm_mday) {
1725                 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1726                                 brokendown_time);
1727                 cached_tm_mday = brokendown_time->tm_mday;
1728         }
1729
1730         strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1731         /* if datestr is empty it prints only timestr*/
1732         snprintf(buf, len, "%s[%s.%03li]", datestr,
1733                         timestr, (tv.tv_usec / 1000));
1734
1735         return buf;
1736 }
1737
1738 static void
1739 custom_handler(const char *fmt, va_list arg)
1740 {
1741         char timestr[512];
1742
1743         weston_log_scope_printf(log_scope, "%s libwayland: ",
1744                         log_timestamp(timestr, sizeof(timestr)));
1745         weston_log_scope_vprintf(log_scope, fmt, arg);
1746 }
1747
1748 static void
1749 log_file_open(const char *filename)
1750 {
1751         wl_log_set_handler_server(custom_handler);
1752
1753         if (filename)
1754                 logfile = fopen(filename, "a");
1755
1756         if (!logfile) {
1757                 logfile = stderr;
1758         } else {
1759                 os_fd_set_cloexec(fileno(logfile));
1760                 setvbuf(logfile, NULL, _IOLBF, 256);
1761         }
1762 }
1763
1764 static void
1765 log_file_close(void)
1766 {
1767         if (logfile && logfile != stderr)
1768                 fclose(logfile);
1769         logfile = stderr;
1770 }
1771
1772 static int
1773 vlog(const char *fmt, va_list ap)
1774 {
1775         const char *oom = "Out of memory";
1776         char timestr[128];
1777         int len = 0;
1778         char *str;
1779
1780         if (weston_log_scope_is_enabled(log_scope)) {
1781                 int len_va;
1782                 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1783                 len_va = vasprintf(&str, fmt, ap);
1784                 if (len_va >= 0) {
1785                         len = weston_log_scope_printf(log_scope, "%s %s",
1786                                         xlog_timestamp, str);
1787                         free(str);
1788                 } else {
1789                         len = weston_log_scope_printf(log_scope, "%s %s",
1790                                         xlog_timestamp, oom);
1791                 }
1792         }
1793
1794         return len;
1795 }
1796
1797
1798 static int
1799 vlog_continue(const char *fmt, va_list ap)
1800 {
1801         return weston_log_scope_vprintf(log_scope, fmt, ap);
1802 }
1803
1804 static int
1805 on_term_signal(int signo, void *data)
1806 {
1807         struct wl_display *display = data;
1808
1809         weston_log("caught signal %d\n", signo);
1810         wl_display_terminate(display);
1811
1812         return 1;
1813 }
1814
1815 static void
1816 handle_exit(struct weston_compositor *compositor)
1817 {
1818         wl_display_terminate(compositor->wl_display);
1819 }
1820
1821 static void
1822 usage(int error_code)
1823 {
1824         FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1825         fprintf(out,
1826                 "Usage: agl-compositor [OPTIONS]\n"
1827                 "\n"
1828                 "This is " PACKAGE_STRING ", the reference compositor for\n"
1829                 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1830                 "backends,\nand depending on which backend is in use different "
1831                 "options will be accepted.\n"
1832                 "\n"
1833                 "Core options:\n"
1834                 "\n"
1835                 "  --version\t\tPrint agl-compositor version\n"
1836                 "  -B, --backend=MODULE\tBackend module, one of\n"
1837                         "\t\t\t\tdrm-backend.so\n"
1838                         "\t\t\t\twayland-backend.so\n"
1839                         "\t\t\t\tx11-backend.so\n"
1840                         "\t\t\t\theadless-backend.so\n"
1841                 "  -S, --socket=NAME\tName of socket to listen on\n"
1842                 "  --log=FILE\t\tLog to the given file\n"
1843                 "  -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1844                 "  --no-config\t\tDo not read agl-compositor.ini\n"
1845                 "  --debug\t\tEnable debug extension(s)\n"
1846                 "  -h, --help\t\tThis help message\n"
1847                 "\n");
1848         exit(error_code);
1849 }
1850
1851 static char *
1852 copy_command_line(int argc, char * const argv[])
1853 {
1854         FILE *fp;
1855         char *str = NULL;
1856         size_t size = 0;
1857         int i;
1858
1859         fp = open_memstream(&str, &size);
1860         if (!fp)
1861                 return NULL;
1862
1863         fprintf(fp, "%s", argv[0]);
1864         for (i = 1; i < argc; i++)
1865                 fprintf(fp, " %s", argv[i]);
1866         fclose(fp);
1867
1868         return str;
1869 }
1870
1871 #if !defined(BUILD_XWAYLAND)
1872 int
1873 wet_load_xwayland(struct weston_compositor *comp)
1874 {
1875         weston_log("Attempted to load xwayland library but compositor "
1876                    "was *not* built with xwayland support!\n");
1877         return -1;
1878 }
1879 #endif
1880
1881 static void
1882 weston_log_setup_scopes(struct weston_log_context *log_ctx,
1883                         struct weston_log_subscriber *subscriber,
1884                         const char *names)
1885 {
1886         assert(log_ctx);
1887         assert(subscriber);
1888
1889         char *tokenize = strdup(names);
1890         char *token = strtok(tokenize, ",");
1891         while (token) {
1892                 weston_log_subscribe(log_ctx, subscriber, token);
1893                 token = strtok(NULL, ",");
1894         }
1895         free(tokenize);
1896 }
1897
1898 static void
1899 weston_log_subscribe_to_scopes(struct weston_log_context *log_ctx,
1900                                struct weston_log_subscriber *logger,
1901                                const char *debug_scopes)
1902 {
1903         if (logger && debug_scopes)
1904                 weston_log_setup_scopes(log_ctx, logger, debug_scopes);
1905         else
1906                 weston_log_subscribe(log_ctx, logger, "log");
1907 }
1908
1909 WL_EXPORT
1910 int wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
1911 {
1912         struct ivi_compositor ivi = { 0 };
1913         char *cmdline;
1914         struct wl_display *display = NULL;
1915         struct wl_event_loop *loop;
1916         struct wl_event_source *signals[3] = { 0 };
1917         struct weston_config_section *section;
1918         /* Command line options */
1919         char *backend = NULL;
1920         char *socket_name = NULL;
1921         char *log = NULL;
1922         char *modules = NULL;
1923         char *option_modules = NULL;
1924         char *debug_scopes = NULL;
1925         int help = 0;
1926         int version = 0;
1927         int no_config = 0;
1928         int debug = 0;
1929         bool list_debug_scopes = false;
1930         char *config_file = NULL;
1931         struct weston_log_context *log_ctx = NULL;
1932         struct weston_log_subscriber *logger;
1933         int ret = EXIT_FAILURE;
1934         bool xwayland = false;
1935         struct sigaction action;
1936
1937         const struct weston_option core_options[] = {
1938                 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1939                 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1940                 { WESTON_OPTION_STRING, "log", 0, &log },
1941                 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1942                 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1943                 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1944                 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1945                 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1946                 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1947                 { WESTON_OPTION_STRING, "debug-scopes", 'l', &debug_scopes },
1948                 { WESTON_OPTION_STRING, "list-debug-scopes", 'L', &list_debug_scopes },
1949         };
1950
1951         wl_list_init(&ivi.outputs);
1952         wl_list_init(&ivi.saved_outputs);
1953         wl_list_init(&ivi.surfaces);
1954         wl_list_init(&ivi.pending_surfaces);
1955         wl_list_init(&ivi.popup_pending_apps);
1956         wl_list_init(&ivi.fullscreen_pending_apps);
1957         wl_list_init(&ivi.split_pending_apps);
1958         wl_list_init(&ivi.remote_pending_apps);
1959         wl_list_init(&ivi.desktop_clients);
1960         wl_list_init(&ivi.child_process_list);
1961         wl_list_init(&ivi.pending_apps);
1962
1963         /* Prevent any clients we spawn getting our stdin */
1964         os_fd_set_cloexec(STDIN_FILENO);
1965
1966         cmdline = copy_command_line(argc, argv);
1967         parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1968
1969         if (help)
1970                 usage(EXIT_SUCCESS);
1971
1972         if (version) {
1973                 printf(PACKAGE_STRING "\n");
1974                 ret = EXIT_SUCCESS;
1975                 goto exit_signals;
1976         }
1977
1978         log_ctx = weston_log_ctx_create();
1979         if (!log_ctx) {
1980                 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1981                 goto exit_signals;
1982         }
1983
1984         log_scope = weston_log_ctx_add_log_scope(log_ctx, "log",
1985                                                  "agl-compositor log\n",
1986                                                  NULL, NULL, NULL);
1987
1988         log_file_open(log);
1989         weston_log_set_handler(vlog, vlog_continue);
1990
1991         logger = weston_log_subscriber_create_log(logfile);
1992         weston_log_subscribe_to_scopes(log_ctx, logger, debug_scopes);
1993
1994         weston_log("Command line: %s\n", cmdline);
1995         free(cmdline);
1996
1997         if (load_config(&ivi.config, no_config, config_file) < 0)
1998                 goto error_signals;
1999         section = weston_config_get_section(ivi.config, "core", NULL, NULL);
2000         if (!backend) {
2001                 weston_config_section_get_string(section, "backend", &backend,
2002                                                  NULL);
2003                 if (!backend)
2004                         backend = choose_default_backend();
2005         }
2006
2007         display = wl_display_create();
2008         loop = wl_display_get_event_loop(display);
2009
2010         wl_display_set_global_filter(display,
2011                                      global_filter, &ivi);
2012
2013         /* Register signal handlers so we shut down cleanly */
2014
2015         signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
2016                                               display);
2017         signals[1] = wl_event_loop_add_signal(loop, SIGUSR2, on_term_signal,
2018                                               display);
2019         signals[2] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
2020                                               display);
2021
2022         /* When debugging the compositor, if use wl_event_loop_add_signal() to
2023          * catch SIGINT, the debugger can't catch it, and attempting to stop
2024          * the compositor from within the debugger results in weston exiting
2025          * cleanly.
2026          *
2027          * Instead, use the sigaction() function, which sets up the signal in a
2028          * way that gdb can successfully catch, but have the handler for SIGINT
2029          * send SIGUSR2 (xwayland uses SIGUSR1), which we catch via
2030          * wl_event_loop_add_signal().
2031          */
2032         action.sa_handler = sigint_helper;
2033         sigemptyset(&action.sa_mask);
2034         action.sa_flags = 0;
2035         sigaction(SIGINT, &action, NULL);
2036
2037         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
2038                 if (!signals[i])
2039                         goto error_signals;
2040
2041         ivi.compositor = weston_compositor_create(display, log_ctx, &ivi, test_data);
2042         if (!ivi.compositor) {
2043                 weston_log("fatal: failed to create compositor.\n");
2044                 goto error_signals;
2045         }
2046
2047         if (compositor_init_config(&ivi) < 0)
2048                 goto error_compositor;
2049
2050         if (load_backend(&ivi, backend, &argc, argv) < 0) {
2051                 weston_log("fatal: failed to create compositor backend.\n");
2052                 goto error_compositor;
2053         }
2054
2055         ivi.heads_changed.notify = heads_changed;
2056         weston_compositor_add_heads_changed_listener(ivi.compositor,
2057                                                      &ivi.heads_changed);
2058
2059         weston_compositor_flush_heads_changed(ivi.compositor);
2060         if (ivi_desktop_init(&ivi) < 0)
2061                 goto error_compositor;
2062
2063         ivi_seat_init(&ivi);
2064
2065         /* load additional modules */
2066         weston_config_section_get_string(section, "modules", &modules, "");
2067         if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
2068                 goto error_compositor;
2069
2070         if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
2071                 goto error_compositor;
2072
2073         if (!xwayland) {
2074                 weston_config_section_get_bool(section, "xwayland", &xwayland,
2075                                                false);
2076         }
2077
2078         if (ivi_shell_init(&ivi) < 0)
2079                 goto error_compositor;
2080
2081         if (xwayland) {
2082                 if (wet_load_xwayland(ivi.compositor) < 0)
2083                         goto error_compositor;
2084         }
2085
2086         if (ivi_policy_init(&ivi) < 0)
2087                 goto error_compositor;
2088
2089
2090         if (list_debug_scopes) {
2091                 struct weston_log_scope *nscope = NULL;
2092
2093                 weston_log("Printing available debug scopes:\n");
2094
2095                 while ((nscope = weston_log_scopes_iterate(log_ctx, nscope))) {
2096                         weston_log("\tscope name: %s, desc: %s",
2097                                         weston_log_scope_get_name(nscope),
2098                                         weston_log_scope_get_description(nscope));
2099                 }
2100
2101                 weston_log("\n");
2102
2103                 goto error_compositor;
2104         }
2105
2106         add_bindings(ivi.compositor);
2107
2108         if (ivi.remoting_api)
2109                 ivi_enable_remote_outputs(&ivi);
2110
2111         if (create_listening_socket(display, socket_name) < 0)
2112                 goto error_compositor;
2113
2114         ivi_shell_init_black_fs(&ivi);
2115
2116         ivi.compositor->exit = handle_exit;
2117
2118         weston_compositor_wake(ivi.compositor);
2119
2120         ivi_shell_create_global(&ivi);
2121
2122         ivi_launch_shell_client(&ivi, "shell-client",
2123                                 &ivi.shell_client.client);
2124         ivi_launch_shell_client(&ivi, "shell-client-ext",
2125                                 &ivi.shell_client_ext.client);
2126
2127         if (debug)
2128                 ivi_screenshooter_create(&ivi);
2129         ivi_agl_systemd_notify(&ivi);
2130
2131         wl_display_run(display);
2132
2133         ret = ivi.compositor->exit_code;
2134
2135         wl_display_destroy_clients(display);
2136
2137 error_compositor:
2138         free(backend);
2139         backend = NULL;
2140         free(modules);
2141         modules = NULL;
2142
2143         weston_compositor_destroy(ivi.compositor);
2144
2145         weston_log_scope_destroy(log_scope);
2146         log_scope = NULL;
2147
2148         weston_log_subscriber_destroy(logger);
2149         weston_log_ctx_destroy(log_ctx);
2150
2151         ivi_policy_destroy(ivi.policy);
2152
2153 error_signals:
2154         for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
2155                 if (signals[i])
2156                         wl_event_source_remove(signals[i]);
2157
2158         wl_display_destroy(display);
2159
2160         log_file_close();
2161         if (ivi.config)
2162                 weston_config_destroy(ivi.config);
2163
2164 exit_signals:
2165         free(log);
2166         free(config_file);
2167         free(socket_name);
2168         free(option_modules);
2169         return ret;
2170 }