2 * Copyright © 2012-2021 Collabora, Ltd.
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:
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.
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
26 #include "ivi-compositor.h"
38 #include <linux/input.h>
40 #include <libweston/backend-drm.h>
41 #include <libweston/backend-wayland.h>
42 #ifdef HAVE_BACKEND_HEADLESS
43 #include <libweston/backend-headless.h>
45 #ifdef HAVE_BACKEND_X11
46 #include <libweston/backend-x11.h>
48 #include <libweston/libweston.h>
49 #include <libweston/windowed-output-api.h>
50 #include <libweston/config-parser.h>
51 #include <libweston/weston-log.h>
52 #include <weston/weston.h>
54 #include "shared/os-compatibility.h"
55 #include "shared/helpers.h"
57 #include "agl-shell-server-protocol.h"
64 #include <waltham-transmitter/transmitter_api.h>
67 static int cached_tm_mday = -1;
68 static struct weston_log_scope *log_scope;
70 struct ivi_compositor *
71 to_ivi_compositor(struct weston_compositor *ec)
73 return weston_compositor_get_user_data(ec);
77 handle_output_destroy(struct wl_listener *listener, void *data)
79 struct ivi_output *output;
81 output = wl_container_of(listener, output, output_destroy);
82 assert(output->output == data);
84 if (output->fullscreen_view.fs->view) {
85 weston_surface_destroy(output->fullscreen_view.fs->view->surface);
86 output->fullscreen_view.fs->view = NULL;
89 output->output = NULL;
90 wl_list_remove(&output->output_destroy.link);
94 to_ivi_output(struct weston_output *o)
96 struct wl_listener *listener;
97 struct ivi_output *output;
99 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
100 output = wl_container_of(listener, output, output_destroy);
106 ivi_output_configure_app_id(struct ivi_output *ivi_output)
108 if (ivi_output->config) {
109 if (ivi_output->app_id != NULL)
112 weston_config_section_get_string(ivi_output->config,
117 if (ivi_output->app_id == NULL)
120 weston_log("Will place app_id %s on output %s\n",
121 ivi_output->app_id, ivi_output->name);
125 static struct ivi_output *
126 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
127 struct weston_config_section *config)
129 struct ivi_output *output = NULL;
130 wl_list_for_each(output, &ivi->outputs, link) {
131 if (strcmp(output->name, name) == 0) {
137 output = zalloc(sizeof *output);
145 output->config = config;
147 output->output = weston_compositor_create_output(ivi->compositor, name);
148 if (!output->output) {
154 output->output_destroy.notify = handle_output_destroy;
155 weston_output_add_destroy_listener(output->output,
156 &output->output_destroy);
158 wl_list_insert(&ivi->outputs, &output->link);
159 ivi_output_configure_app_id(output);
164 count_heads(struct weston_output *output)
166 struct weston_head *iter = NULL;
169 while ((iter = weston_output_iterate_heads(output, iter)))
176 handle_head_destroy(struct wl_listener *listener, void *data)
178 struct weston_head *head = data;
179 struct weston_output *output;
181 wl_list_remove(&listener->link);
184 output = weston_head_get_output(head);
186 /* On shutdown path, the output might be already gone. */
190 /* We're the last head */
191 if (count_heads(output) <= 1)
192 weston_output_destroy(output);
196 add_head_destroyed_listener(struct weston_head *head)
198 /* We already have a destroy listener */
199 if (weston_head_get_destroy_listener(head, handle_head_destroy))
202 struct wl_listener *listener = zalloc(sizeof *listener);
206 listener->notify = handle_head_destroy;
207 weston_head_add_destroy_listener(head, listener);
211 drm_configure_output(struct ivi_output *output)
213 struct ivi_compositor *ivi = output->ivi;
214 struct weston_config_section *section = output->config;
215 enum weston_drm_backend_output_mode mode =
216 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
217 char *modeline = NULL;
218 char *gbm_format = NULL;
223 weston_config_section_get_string(section, "mode", &m, "preferred");
225 /* This should have been handled earlier */
226 assert(strcmp(m, "off") != 0);
228 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
229 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
230 } else if (strcmp(m, "preferred") != 0) {
236 weston_config_section_get_string(section, "gbm-format",
239 weston_config_section_get_string(section, "seat", &seat, "");
242 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
243 weston_log("Cannot configure output using weston_drm_output_api.\n");
249 ivi->drm_api->set_gbm_format(output->output, gbm_format);
252 ivi->drm_api->set_seat(output->output, seat);
258 #define WINDOWED_DEFAULT_WIDTH 1024
259 #define WINDOWED_DEFAULT_HEIGHT 768
262 windowed_configure_output(struct ivi_output *output)
264 struct ivi_compositor *ivi = output->ivi;
265 struct weston_config_section *section = output->config;
266 int width = WINDOWED_DEFAULT_WIDTH;
267 int height = WINDOWED_DEFAULT_HEIGHT;
272 weston_config_section_get_string(section, "mode", &mode, NULL);
273 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
274 weston_log("Invalid mode for output %s. Using defaults.\n",
276 width = WINDOWED_DEFAULT_WIDTH;
277 height = WINDOWED_DEFAULT_HEIGHT;
282 if (ivi->cmdline.width)
283 width = ivi->cmdline.width;
284 if (ivi->cmdline.height)
285 height = ivi->cmdline.height;
286 if (ivi->cmdline.scale)
287 weston_output_set_scale(output->output, ivi->cmdline.scale);
289 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
290 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
295 weston_log("Configured windowed_output_api to %dx%d\n", width, height);
301 parse_transform(const char *transform, uint32_t *out)
303 static const struct { const char *name; uint32_t token; } transforms[] = {
304 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
305 { "rotate-90", WL_OUTPUT_TRANSFORM_90 },
306 { "rotate-180", WL_OUTPUT_TRANSFORM_180 },
307 { "rotate-270", WL_OUTPUT_TRANSFORM_270 },
308 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
309 { "flipped-rotate-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
310 { "flipped-rotate-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
311 { "flipped-rotate-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
314 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
315 if (strcmp(transforms[i].name, transform) == 0) {
316 *out = transforms[i].token;
320 *out = WL_OUTPUT_TRANSFORM_NORMAL;
325 configure_output(struct ivi_output *output)
327 struct ivi_compositor *ivi = output->ivi;
328 struct weston_config_section *section = output->config;
330 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
333 * This can happen with the wayland backend with 'sprawl'. The config
334 * is hard-coded, so we don't need to do anything.
336 if (!ivi->drm_api && !ivi->window_api)
342 weston_config_section_get_int(section, "scale", &scale, 1);
343 weston_config_section_get_string(section, "transform", &t, "normal");
344 if (parse_transform(t, &transform) < 0)
345 weston_log("Invalid transform \"%s\" for output %s\n",
350 weston_output_set_scale(output->output, scale);
351 weston_output_set_transform(output->output, transform);
354 return drm_configure_output(output);
356 return windowed_configure_output(output);
360 * Reorgainizes the output's add array into two sections.
361 * add[0..ret-1] are the heads that failed to get attached.
362 * add[ret..add_len] are the heads that were successfully attached.
364 * The order between elements in each section is stable.
367 try_attach_heads(struct ivi_output *output)
371 for (size_t i = 0; i < output->add_len; ++i) {
372 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
373 struct weston_head *tmp = output->add[i];
374 memmove(&output->add[fail_len + 1], output->add[fail_len],
375 sizeof output->add[0] * (i - fail_len));
376 output->add[fail_len++] = tmp;
384 * Like try_attach_heads, this reorganizes the output's add array into a failed
385 * and successful section.
386 * i is the number of heads that already failed the previous step.
389 try_enable_output(struct ivi_output *output, size_t i)
391 for (; i < output->add_len; ++i) {
392 struct weston_head *head;
394 if (weston_output_enable(output->output) == 0)
397 head = output->add[output->add_len - 1];
398 memmove(&output->add[i + 1], &output->add[i],
399 sizeof output->add[0] * (output->add_len - i));
400 output->add[i] = head;
402 weston_head_detach(head);
409 try_attach_enable_heads(struct ivi_output *output)
412 assert(!output->output->enabled);
414 fail_len = try_attach_heads(output);
416 if (configure_output(output) < 0)
419 fail_len = try_enable_output(output, fail_len);
421 /* All heads failed to be attached */
422 if (fail_len == output->add_len)
425 /* For each successful head attached */
426 for (size_t i = fail_len; i < output->add_len; ++i)
427 add_head_destroyed_listener(output->add[i]);
429 output->add_len = fail_len;
434 process_output(struct ivi_output *output)
436 if (output->output->enabled) {
437 output->add_len = try_attach_heads(output);
438 return output->add_len == 0 ? 0 : -1;
441 return try_attach_enable_heads(output);
445 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
447 struct weston_output *output;
448 struct ivi_output *ivi_output;
449 struct wl_listener *listener;
451 output = weston_head_get_output(head);
454 listener = weston_output_get_destroy_listener(output,
455 handle_output_destroy);
458 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
459 assert(ivi_output->output == output);
461 weston_head_detach(head);
462 if (count_heads(ivi_output->output) == 0) {
463 weston_output_disable(ivi_output->output);
467 static struct weston_config_section *
468 find_controlling_output_config(struct weston_config *config,
471 struct weston_config_section *section;
475 same_as = strdup(name);
477 section = weston_config_get_section(config, "output",
479 if (!section && depth > 0)
480 weston_log("Configuration error: output section reffered"
481 "to by same-as=%s not found.\n", same_as);
488 weston_log("Configuration error: same-as nested too "
489 "deep for output '%s'.\n", name);
493 weston_config_section_get_string(section, "same-as",
501 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
503 const char *name = weston_head_get_name(head);
504 struct weston_config_section *section;
505 struct ivi_output *output;
506 char *output_name = NULL;
508 section = find_controlling_output_config(ivi->config, name);
512 weston_config_section_get_string(section, "mode", &mode, NULL);
513 if (mode && strcmp(mode, "off") == 0) {
519 weston_config_section_get_string(section, "name",
522 output_name = strdup(name);
528 output = ivi_ensure_output(ivi, output_name, section);
532 if (output->add_len >= ARRAY_LENGTH(output->add))
535 output->add[output->add_len++] = head;
539 heads_changed(struct wl_listener *listener, void *arg)
541 struct weston_compositor *compositor = arg;
542 struct weston_head *head = NULL;
543 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
544 struct ivi_output *output;
546 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
547 bool connected = weston_head_is_connected(head);
548 bool enabled = weston_head_is_enabled(head);
549 bool changed = weston_head_is_device_changed(head);
550 bool non_desktop = weston_head_is_non_desktop(head);
552 if (connected && !enabled && !non_desktop)
553 head_prepare_enable(ivi, head);
554 else if (!connected && enabled)
555 head_disable(ivi, head);
556 else if (enabled && changed)
557 weston_log("Detected a monitor change on head '%s', "
558 "not bothering to do anything about it.\n",
559 weston_head_get_name(head));
561 weston_head_reset_device_changed(head);
564 wl_list_for_each(output, &ivi->outputs, link) {
565 if (output->add_len == 0)
568 if (process_output(output) < 0) {
570 ivi->init_failed = true;
577 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
579 struct weston_compositor *compositor = ivi->compositor;
580 int (*module_init)(struct weston_compositor *wc);
582 module_init = weston_load_module("waltham-transmitter.so",
587 if (module_init(compositor) < 0)
590 ivi->waltham_transmitter_api = weston_get_transmitter_api(compositor);
591 if (!ivi->waltham_transmitter_api) {
592 weston_log("Failed to load waltham-transmitter plugin.\n");
596 weston_log("waltham-transmitter plug-in loaded\n");
601 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
609 drm_backend_remoted_output_configure(struct weston_output *output,
610 struct weston_config_section *section,
612 const struct weston_remoting_api *api)
614 char *gbm_format = NULL;
617 char *pipeline = NULL;
620 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
623 ret = api->set_mode(output, modeline);
625 weston_log("Cannot configure an output \"%s\" using "
626 "weston_remoting_api. Invalid mode\n",
631 weston_config_section_get_int(section, "scale", &scale, 1);
632 weston_output_set_scale(output, scale);
634 weston_config_section_get_string(section, "transform", &trans, "normal");
635 if (parse_transform(trans, &transform) < 0) {
636 weston_log("Invalid transform \"%s\" for output %s\n",
637 trans, output->name);
639 weston_output_set_transform(output, transform);
641 weston_config_section_get_string(section, "gbm-format",
643 api->set_gbm_format(output, gbm_format);
646 weston_config_section_get_string(section, "seat", &seat, "");
648 api->set_seat(output, seat);
651 weston_config_section_get_string(section, "gst-pipeline", &pipeline,
654 api->set_gst_pipeline(output, pipeline);
659 weston_config_section_get_string(section, "host", &host, NULL);
660 weston_config_section_get_int(section, "port", &port, 0);
661 if (!host || port <= 0 || 65533 < port) {
662 weston_log("Cannot configure an output \"%s\". "
663 "Need to specify gst-pipeline or "
664 "host and port (1-65533).\n", output->name);
666 api->set_host(output, host);
668 api->set_port(output, port);
675 remote_output_init(struct ivi_output *ivi_output,
676 struct weston_compositor *compositor,
677 struct weston_config_section *section,
678 const struct weston_remoting_api *api)
680 char *output_name, *modeline = NULL;
683 weston_config_section_get_string(section, "name", &output_name, NULL);
687 weston_config_section_get_string(section, "mode", &modeline, "off");
688 if (strcmp(modeline, "off") == 0)
691 ivi_output->output = api->create_output(compositor, output_name);
692 if (!ivi_output->output) {
693 weston_log("Cannot create remoted output \"%s\".\n",
698 ret = drm_backend_remoted_output_configure(ivi_output->output, section,
701 weston_log("Cannot configure remoted output \"%s\".\n",
706 if (weston_output_enable(ivi_output->output) < 0) {
707 weston_log("Enabling remoted output \"%s\" failed.\n",
714 weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
721 if (ivi_output->output)
722 weston_output_destroy(ivi_output->output);
728 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
730 struct weston_config_section *remote_section = NULL;
731 const char *section_name;
732 struct weston_config *config = ivi->config;
734 while (weston_config_next_section(config, &remote_section, §ion_name)) {
735 if (strcmp(section_name, "remote-output"))
738 struct ivi_output *ivi_output = NULL;
739 bool output_found = false;
742 weston_config_section_get_string(remote_section,
743 "name", &_name, NULL);
744 wl_list_for_each(ivi_output, &ivi->outputs, link) {
745 if (!strcmp(ivi_output->name, _name)) {
756 ivi_output = zalloc(sizeof(*ivi_output));
762 ivi_output->ivi = ivi;
763 ivi_output->name = _name;
764 ivi_output->config = remote_section;
765 ivi_output->type = OUTPUT_REMOTE;
767 if (remote_output_init(ivi_output, ivi->compositor,
768 remote_section, ivi->remoting_api)) {
769 free(ivi_output->name);
774 ivi_output->output_destroy.notify = handle_output_destroy;
775 weston_output_add_destroy_listener(ivi_output->output,
776 &ivi_output->output_destroy);
778 wl_list_insert(&ivi->outputs, &ivi_output->link);
779 ivi_output_configure_app_id(ivi_output);
784 ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
786 struct weston_config_section *transmitter_section = NULL;
787 const char *sect_name;
788 struct weston_config *config = ivi->config;
790 while (weston_config_next_section(config, &transmitter_section, §_name)) {
791 if (strcmp(sect_name, "transmitter-output"))
794 struct ivi_output *ivi_output = NULL;
795 bool output_found = false;
798 weston_config_section_get_string(transmitter_section,
799 "name", &_name, NULL);
800 wl_list_for_each(ivi_output, &ivi->outputs, link) {
801 if (!strcmp(ivi_output->name, _name)) {
812 ivi_output = zalloc(sizeof(*ivi_output));
818 ivi_output->ivi = ivi;
819 ivi_output->name = _name;
820 ivi_output->config = transmitter_section;
822 if (remote_output_init(ivi_output, ivi->compositor,
823 transmitter_section, ivi->remoting_api)) {
824 free(ivi_output->name);
829 ivi_output->type = OUTPUT_WALTHAM;
830 ivi_output->output_destroy.notify = handle_output_destroy;
831 weston_output_add_destroy_listener(ivi_output->output,
832 &ivi_output->output_destroy);
834 wl_list_insert(&ivi->outputs, &ivi_output->link);
835 ivi_output_configure_app_id(ivi_output);
840 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
842 struct weston_compositor *compositor = ivi->compositor;
843 int (*module_init)(struct weston_compositor *wc);
845 module_init = weston_load_module("remoting-plugin.so",
846 "weston_module_init");
850 if (module_init(compositor) < 0)
853 ivi->remoting_api = weston_remoting_get_api(compositor);
854 if (!ivi->remoting_api)
860 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
867 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
869 struct weston_drm_backend_config config = {
871 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
872 .struct_size = sizeof config,
875 struct weston_config_section *section;
876 int use_current_mode = 0;
881 const struct weston_option options[] = {
882 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
883 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
884 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
885 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
886 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
889 parse_options(options, ARRAY_LENGTH(options), argc, argv);
890 config.use_pixman = use_pixman;
891 ivi->cmdline.use_current_mode = use_current_mode;
893 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
894 weston_config_section_get_string(section, "gbm-format",
895 &config.gbm_format, NULL);
896 weston_config_section_get_uint(section, "pageflip-timeout",
897 &config.pageflip_timeout, 0);
898 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
899 config.use_pixman_shadow = use_shadow;
901 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
906 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
908 weston_log("Cannot use drm output api.\n");
913 load_remoting_plugin(ivi, ivi->config);
914 load_waltham_plugin(ivi, ivi->config);
917 free(config.gbm_format);
918 free(config.seat_id);
923 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
924 bool *use_pixman, bool *fullscreen, int *output_count)
926 struct weston_config_section *section;
930 const struct weston_option options[] = {
931 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
932 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
933 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
934 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
935 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
936 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
939 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
940 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
943 parse_options(options, ARRAY_LENGTH(options), argc, argv);
944 *use_pixman = pixman;
949 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
950 const char *match_prefix, const char *name_prefix)
952 struct weston_config_section *section = NULL;
953 const char *section_name;
954 char *default_output = NULL;
956 size_t match_len = strlen(match_prefix);
958 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
961 if (i >= output_count)
964 if (strcmp(section_name, "output") != 0)
967 weston_config_section_get_string(section, "name", &output_name, NULL);
968 if (output_name == NULL)
970 if (strncmp(output_name, match_prefix, match_len) != 0) {
975 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
984 for (; i < output_count; ++i) {
985 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
988 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
989 free(default_output);
993 free(default_output);
1000 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1002 struct weston_wayland_backend_config config = {
1004 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
1005 .struct_size = sizeof config,
1008 struct weston_config_section *section;
1013 const struct weston_option options[] = {
1014 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1015 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1018 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1019 &config.fullscreen, &output_count);
1021 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1022 config.sprawl = sprawl;
1024 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1025 weston_config_section_get_string(section, "cursor-theme",
1026 &config.cursor_theme, NULL);
1027 weston_config_section_get_int(section, "cursor-size",
1028 &config.cursor_size, 32);
1030 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1033 free(config.cursor_theme);
1034 free(config.display_name);
1039 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1042 * We will just assume if load_backend() finished cleanly and
1043 * windowed_output_api is not present that wayland backend is started
1044 * with --sprawl or runs on fullscreen-shell. In this case, all values
1045 * are hardcoded, so nothing can be configured; simply create and
1048 if (ivi->window_api == NULL)
1051 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1054 #ifdef HAVE_BACKEND_X11
1056 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1058 struct weston_x11_backend_config config = {
1060 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1061 .struct_size = sizeof config,
1068 const struct weston_option options[] = {
1069 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1072 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1073 &config.fullscreen, &output_count);
1075 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1076 config.no_input = no_input;
1078 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1084 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1085 if (!ivi->window_api) {
1086 weston_log("Cannot use weston_windowed_output_api.\n");
1090 return windowed_create_outputs(ivi, output_count, "X", "screen");
1094 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1100 #ifdef HAVE_BACKEND_HEADLESS
1102 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1104 struct weston_headless_backend_config config = {};
1112 struct weston_compositor *c = ivi->compositor;
1114 const struct weston_option options[] = {
1115 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1116 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &use_gl },
1119 windowed_parse_common_options(ivi, argc, argv, &use_pixman,
1120 &fullscreen, &output_count);
1122 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1123 config.use_pixman = use_pixman;
1124 config.use_gl = use_gl;
1126 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1127 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1129 /* load the actual headless-backend and configure it */
1130 ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1135 ivi->window_api = weston_windowed_output_get_api(c);
1136 if (!ivi->window_api) {
1137 weston_log("Cannot use weston_windowed_output_api.\n");
1141 if (ivi->window_api->create_head(c, "headless") < 0) {
1142 weston_log("Cannot create headless back-end\n");
1150 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1157 load_backend(struct ivi_compositor *ivi, const char *backend,
1158 int *argc, char *argv[])
1160 if (strcmp(backend, "drm-backend.so") == 0) {
1161 return load_drm_backend(ivi, argc, argv);
1162 } else if (strcmp(backend, "wayland-backend.so") == 0) {
1163 return load_wayland_backend(ivi, argc, argv);
1164 } else if (strcmp(backend, "x11-backend.so") == 0) {
1165 return load_x11_backend(ivi, argc, argv);
1166 } else if (strcmp(backend, "headless-backend.so") == 0) {
1167 return load_headless_backend(ivi, argc, argv);
1170 weston_log("fatal: unknown backend '%s'.\n", backend);
1175 load_modules(struct ivi_compositor *ivi, const char *modules,
1176 int *argc, char *argv[], bool *xwayland)
1178 const char *p, *end;
1180 int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1182 if (modules == NULL)
1187 end = strchrnul(p, ',');
1188 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1190 if (strstr(buffer, "xwayland.so")) {
1191 weston_log("Xwayland plug-in not supported!\n");
1198 if (strstr(buffer, "systemd-notify.so")) {
1199 weston_log("systemd-notify plug-in already loaded!\n");
1206 module_init = weston_load_module(buffer, "wet_module_init");
1210 if (module_init(ivi->compositor, *argc, argv) < 0)
1223 choose_default_backend(void)
1225 char *backend = NULL;
1227 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1228 backend = strdup("wayland-backend.so");
1229 else if (getenv("DISPLAY"))
1230 backend = strdup("x11-backend.so");
1232 backend = strdup("drm-backend.so");
1238 compositor_init_config(struct weston_compositor *compositor,
1239 struct weston_config *config)
1241 struct xkb_rule_names xkb_names;
1242 struct weston_config_section *section;
1247 /* agl-compositor.ini [keyboard] */
1248 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1249 weston_config_section_get_string(section, "keymap_rules",
1250 (char **) &xkb_names.rules, NULL);
1251 weston_config_section_get_string(section, "keymap_model",
1252 (char **) &xkb_names.model, NULL);
1253 weston_config_section_get_string(section, "keymap_layout",
1254 (char **) &xkb_names.layout, NULL);
1255 weston_config_section_get_string(section, "keymap_variant",
1256 (char **) &xkb_names.variant, NULL);
1257 weston_config_section_get_string(section, "keymap_options",
1258 (char **) &xkb_names.options, NULL);
1260 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1263 weston_config_section_get_int(section, "repeat-rate",
1264 &compositor->kb_repeat_rate, 40);
1265 weston_config_section_get_int(section, "repeat-delay",
1266 &compositor->kb_repeat_delay, 400);
1268 weston_config_section_get_bool(section, "vt-switching",
1269 &vt_switching, false);
1270 compositor->vt_switching = vt_switching;
1272 /* agl-compositor.ini [core] */
1273 section = weston_config_get_section(config, "core", NULL, NULL);
1275 weston_config_section_get_bool(section, "require-input", &require_input, true);
1276 compositor->require_input = require_input;
1278 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1279 compositor->repaint_msec);
1280 if (repaint_msec < -10 || repaint_msec > 1000) {
1281 weston_log("Invalid repaint_window value in config: %d\n",
1284 compositor->repaint_msec = repaint_msec;
1286 weston_log("Output repaint window is %d ms maximum.\n",
1287 compositor->repaint_msec);
1292 struct ivi_surface *
1293 to_ivi_surface(struct weston_surface *surface)
1295 struct weston_desktop_surface *dsurface;
1297 dsurface = weston_surface_get_desktop_surface(surface);
1301 return weston_desktop_surface_get_user_data(dsurface);
1305 activate_binding(struct weston_seat *seat,
1306 struct weston_view *focus_view)
1308 struct weston_surface *focus = focus_view->surface;
1309 struct weston_surface *main_surface =
1310 weston_surface_get_main_surface(focus);
1311 struct ivi_surface *surface;
1313 surface = to_ivi_surface(main_surface);
1317 weston_seat_set_keyboard_focus(seat, focus);
1321 click_to_activate_binding(struct weston_pointer *pointer,
1322 const struct timespec *time,
1323 uint32_t button, void *data)
1325 if (pointer->grab != &pointer->default_grab)
1327 if (pointer->focus == NULL)
1330 activate_binding(pointer->seat, pointer->focus);
1334 touch_to_activate_binding(struct weston_touch *touch,
1335 const struct timespec *time,
1338 if (touch->grab != &touch->default_grab)
1340 if (touch->focus == NULL)
1343 activate_binding(touch->seat, touch->focus);
1347 add_bindings(struct weston_compositor *compositor)
1349 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1350 click_to_activate_binding,
1352 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1353 click_to_activate_binding,
1355 weston_compositor_add_touch_binding(compositor, 0,
1356 touch_to_activate_binding,
1361 create_listening_socket(struct wl_display *display, const char *socket_name)
1364 if (wl_display_add_socket(display, socket_name)) {
1365 weston_log("fatal: failed to add socket: %s\n",
1370 socket_name = wl_display_add_socket_auto(display);
1372 weston_log("fatal: failed to add socket: %s\n",
1378 setenv("WAYLAND_DISPLAY", socket_name, 1);
1384 global_filter(const struct wl_client *client, const struct wl_global *global,
1391 load_config(struct weston_config **config, bool no_config,
1392 const char *config_file)
1394 const char *file = "agl-compositor.ini";
1395 const char *full_path;
1401 *config = weston_config_parse(file);
1404 full_path = weston_config_get_full_path(*config);
1406 weston_log("Using config file '%s'.\n", full_path);
1407 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1412 if (config_file && !no_config) {
1413 weston_log("fatal: error opening or reading config file '%s'.\n",
1419 weston_log("Starting with no config file.\n");
1420 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1425 static FILE *logfile;
1428 log_timestamp(char *buf, size_t len)
1431 struct tm *brokendown_time;
1435 gettimeofday(&tv, NULL);
1437 brokendown_time = localtime(&tv.tv_sec);
1438 if (brokendown_time == NULL) {
1439 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1443 memset(datestr, 0, sizeof(datestr));
1444 if (brokendown_time->tm_mday != cached_tm_mday) {
1445 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1447 cached_tm_mday = brokendown_time->tm_mday;
1450 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1451 /* if datestr is empty it prints only timestr*/
1452 snprintf(buf, len, "%s[%s.%03li]", datestr,
1453 timestr, (tv.tv_usec / 1000));
1459 custom_handler(const char *fmt, va_list arg)
1463 weston_log_scope_printf(log_scope, "%s libwayland: ",
1464 log_timestamp(timestr, sizeof(timestr)));
1465 weston_log_scope_vprintf(log_scope, fmt, arg);
1469 log_file_open(const char *filename)
1471 wl_log_set_handler_server(custom_handler);
1474 logfile = fopen(filename, "a");
1479 os_fd_set_cloexec(fileno(logfile));
1480 setvbuf(logfile, NULL, _IOLBF, 256);
1485 log_file_close(void)
1487 if (logfile && logfile != stderr)
1493 vlog(const char *fmt, va_list ap)
1495 const char *oom = "Out of memory";
1500 if (weston_log_scope_is_enabled(log_scope)) {
1502 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1503 len_va = vasprintf(&str, fmt, ap);
1505 len = weston_log_scope_printf(log_scope, "%s %s",
1506 xlog_timestamp, str);
1509 len = weston_log_scope_printf(log_scope, "%s %s",
1510 xlog_timestamp, oom);
1519 vlog_continue(const char *fmt, va_list ap)
1521 return weston_log_scope_vprintf(log_scope, fmt, ap);
1525 on_term_signal(int signo, void *data)
1527 struct wl_display *display = data;
1529 weston_log("caught signal %d\n", signo);
1530 wl_display_terminate(display);
1536 handle_exit(struct weston_compositor *compositor)
1538 wl_display_terminate(compositor->wl_display);
1542 usage(int error_code)
1544 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1546 "Usage: agl-compositor [OPTIONS]\n"
1548 "This is " PACKAGE_STRING ", the reference compositor for\n"
1549 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1550 "backends,\nand depending on which backend is in use different "
1551 "options will be accepted.\n"
1555 " --version\t\tPrint agl-compositor version\n"
1556 " -B, --backend=MODULE\tBackend module, one of\n"
1557 "\t\t\t\tdrm-backend.so\n"
1558 "\t\t\t\twayland-backend.so\n"
1559 "\t\t\t\tx11-backend.so\n"
1560 "\t\t\t\theadless-backend.so\n"
1561 " -S, --socket=NAME\tName of socket to listen on\n"
1562 " --log=FILE\t\tLog to the given file\n"
1563 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1564 " --no-config\t\tDo not read agl-compositor.ini\n"
1565 " --debug\t\tEnable debug extension(s)\n"
1566 " -h, --help\t\tThis help message\n"
1572 copy_command_line(int argc, char * const argv[])
1579 fp = open_memstream(&str, &size);
1583 fprintf(fp, "%s", argv[0]);
1584 for (i = 1; i < argc; i++)
1585 fprintf(fp, " %s", argv[i]);
1592 int wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
1594 struct ivi_compositor ivi = { 0 };
1596 struct wl_display *display = NULL;
1597 struct wl_event_loop *loop;
1598 struct wl_event_source *signals[3] = { 0 };
1599 struct weston_config_section *section;
1600 /* Command line options */
1601 char *backend = NULL;
1602 char *socket_name = NULL;
1604 char *modules = NULL;
1605 char *option_modules = NULL;
1610 char *config_file = NULL;
1611 struct weston_log_context *log_ctx = NULL;
1612 struct weston_log_subscriber *logger;
1613 int ret = EXIT_FAILURE;
1614 bool xwayland = false;
1616 const struct weston_option core_options[] = {
1617 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1618 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1619 { WESTON_OPTION_STRING, "log", 0, &log },
1620 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1621 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1622 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1623 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1624 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1625 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1628 wl_list_init(&ivi.outputs);
1629 wl_list_init(&ivi.surfaces);
1630 wl_list_init(&ivi.pending_surfaces);
1631 wl_list_init(&ivi.popup_pending_apps);
1632 wl_list_init(&ivi.fullscreen_pending_apps);
1633 wl_list_init(&ivi.split_pending_apps);
1634 wl_list_init(&ivi.remote_pending_apps);
1635 wl_list_init(&ivi.desktop_clients);
1637 /* Prevent any clients we spawn getting our stdin */
1638 os_fd_set_cloexec(STDIN_FILENO);
1640 cmdline = copy_command_line(argc, argv);
1641 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1644 usage(EXIT_SUCCESS);
1647 printf(PACKAGE_STRING "\n");
1652 log_ctx = weston_log_ctx_create();
1654 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1658 log_scope = weston_log_ctx_add_log_scope(log_ctx, "log",
1659 "agl-compositor log\n",
1663 weston_log_set_handler(vlog, vlog_continue);
1665 logger = weston_log_subscriber_create_log(logfile);
1666 weston_log_subscribe(log_ctx, logger, "log");
1668 weston_log("Command line: %s\n", cmdline);
1671 if (load_config(&ivi.config, no_config, config_file) < 0)
1673 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1675 weston_config_section_get_string(section, "backend", &backend,
1678 backend = choose_default_backend();
1681 weston_config_section_get_bool(section, "hide-cursor",
1682 &ivi.hide_cursor, false);
1683 weston_config_section_get_bool(section, "activate-by-default",
1684 &ivi.activate_by_default, true);
1686 display = wl_display_create();
1687 loop = wl_display_get_event_loop(display);
1689 wl_display_set_global_filter(display,
1690 global_filter, &ivi);
1692 /* Register signal handlers so we shut down cleanly */
1694 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1696 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1698 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1701 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1705 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi, test_data);
1706 if (!ivi.compositor) {
1707 weston_log("fatal: failed to create compositor.\n");
1711 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1712 goto error_compositor;
1714 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1715 weston_log("fatal: failed to create compositor backend.\n");
1716 goto error_compositor;
1719 ivi.heads_changed.notify = heads_changed;
1720 weston_compositor_add_heads_changed_listener(ivi.compositor,
1721 &ivi.heads_changed);
1723 if (ivi_desktop_init(&ivi) < 0)
1724 goto error_compositor;
1726 ivi_seat_init(&ivi);
1728 /* load additional modules */
1729 weston_config_section_get_string(section, "modules", &modules, "");
1730 if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
1731 goto error_compositor;
1733 if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
1734 goto error_compositor;
1736 if (ivi_policy_init(&ivi) < 0)
1737 goto error_compositor;
1739 if (ivi_shell_init(&ivi) < 0)
1740 goto error_compositor;
1742 add_bindings(ivi.compositor);
1744 weston_compositor_flush_heads_changed(ivi.compositor);
1746 if (ivi.remoting_api)
1747 ivi_enable_remote_outputs(&ivi);
1749 if (ivi.waltham_transmitter_api)
1750 ivi_enable_waltham_outputs(&ivi);
1752 if (create_listening_socket(display, socket_name) < 0)
1753 goto error_compositor;
1755 ivi_shell_init_black_fs(&ivi);
1757 ivi.compositor->exit = handle_exit;
1759 weston_compositor_wake(ivi.compositor);
1761 ivi_shell_create_global(&ivi);
1762 ivi_launch_shell_client(&ivi);
1764 ivi_screenshooter_create(&ivi);
1765 ivi_agl_systemd_notify(&ivi);
1767 wl_display_run(display);
1769 ret = ivi.compositor->exit_code;
1771 wl_display_destroy_clients(display);
1779 weston_compositor_destroy(ivi.compositor);
1781 weston_log_scope_destroy(log_scope);
1784 weston_log_subscriber_destroy(logger);
1785 weston_log_ctx_destroy(log_ctx);
1787 ivi_policy_destroy(ivi.policy);
1790 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1792 wl_event_source_remove(signals[i]);
1794 wl_display_destroy(display);
1798 weston_config_destroy(ivi.config);
1804 free(option_modules);