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 #include <libweston/backend-headless.h>
43 #ifdef HAVE_BACKEND_X11
44 #include <libweston/backend-x11.h>
46 #include <libweston/libweston.h>
47 #include <libweston/windowed-output-api.h>
48 #include <libweston/config-parser.h>
49 #include <libweston/weston-log.h>
50 #include <weston/weston.h>
52 #include "shared/os-compatibility.h"
53 #include "shared/helpers.h"
55 #include "agl-shell-server-protocol.h"
62 #include <waltham-transmitter/transmitter_api.h>
65 static int cached_tm_mday = -1;
66 static struct weston_log_scope *log_scope;
68 struct ivi_compositor *
69 to_ivi_compositor(struct weston_compositor *ec)
71 return weston_compositor_get_user_data(ec);
75 handle_output_destroy(struct wl_listener *listener, void *data)
77 struct ivi_output *output;
79 output = wl_container_of(listener, output, output_destroy);
80 assert(output->output == data);
82 if (output->fullscreen_view.fs->view) {
83 weston_surface_destroy(output->fullscreen_view.fs->view->surface);
84 output->fullscreen_view.fs->view = NULL;
87 output->output = NULL;
88 wl_list_remove(&output->output_destroy.link);
92 to_ivi_output(struct weston_output *o)
94 struct wl_listener *listener;
95 struct ivi_output *output;
97 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
98 output = wl_container_of(listener, output, output_destroy);
104 ivi_output_configure_app_id(struct ivi_output *ivi_output)
106 if (ivi_output->config) {
107 if (ivi_output->app_id != NULL)
110 weston_config_section_get_string(ivi_output->config,
115 if (ivi_output->app_id == NULL)
118 weston_log("Will place app_id %s on output %s\n",
119 ivi_output->app_id, ivi_output->name);
123 static struct ivi_output *
124 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
125 struct weston_config_section *config)
127 struct ivi_output *output = NULL;
128 wl_list_for_each(output, &ivi->outputs, link) {
129 if (strcmp(output->name, name) == 0) {
135 output = zalloc(sizeof *output);
143 output->config = config;
145 output->output = weston_compositor_create_output(ivi->compositor, name);
146 if (!output->output) {
152 output->output_destroy.notify = handle_output_destroy;
153 weston_output_add_destroy_listener(output->output,
154 &output->output_destroy);
156 wl_list_insert(&ivi->outputs, &output->link);
157 ivi_output_configure_app_id(output);
162 count_heads(struct weston_output *output)
164 struct weston_head *iter = NULL;
167 while ((iter = weston_output_iterate_heads(output, iter)))
174 handle_head_destroy(struct wl_listener *listener, void *data)
176 struct weston_head *head = data;
177 struct weston_output *output;
179 wl_list_remove(&listener->link);
182 output = weston_head_get_output(head);
184 /* On shutdown path, the output might be already gone. */
188 /* We're the last head */
189 if (count_heads(output) <= 1)
190 weston_output_destroy(output);
194 add_head_destroyed_listener(struct weston_head *head)
196 /* We already have a destroy listener */
197 if (weston_head_get_destroy_listener(head, handle_head_destroy))
200 struct wl_listener *listener = zalloc(sizeof *listener);
204 listener->notify = handle_head_destroy;
205 weston_head_add_destroy_listener(head, listener);
209 drm_configure_output(struct ivi_output *output)
211 struct ivi_compositor *ivi = output->ivi;
212 struct weston_config_section *section = output->config;
213 enum weston_drm_backend_output_mode mode =
214 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
215 char *modeline = NULL;
216 char *gbm_format = NULL;
221 weston_config_section_get_string(section, "mode", &m, "preferred");
223 /* This should have been handled earlier */
224 assert(strcmp(m, "off") != 0);
226 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
227 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
228 } else if (strcmp(m, "preferred") != 0) {
234 weston_config_section_get_string(section, "gbm-format",
237 weston_config_section_get_string(section, "seat", &seat, "");
240 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
241 weston_log("Cannot configure output using weston_drm_output_api.\n");
247 ivi->drm_api->set_gbm_format(output->output, gbm_format);
250 ivi->drm_api->set_seat(output->output, seat);
256 #define WINDOWED_DEFAULT_WIDTH 1024
257 #define WINDOWED_DEFAULT_HEIGHT 768
260 windowed_configure_output(struct ivi_output *output)
262 struct ivi_compositor *ivi = output->ivi;
263 struct weston_config_section *section = output->config;
264 int width = WINDOWED_DEFAULT_WIDTH;
265 int height = WINDOWED_DEFAULT_HEIGHT;
270 weston_config_section_get_string(section, "mode", &mode, NULL);
271 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
272 weston_log("Invalid mode for output %s. Using defaults.\n",
274 width = WINDOWED_DEFAULT_WIDTH;
275 height = WINDOWED_DEFAULT_HEIGHT;
280 if (ivi->cmdline.width)
281 width = ivi->cmdline.width;
282 if (ivi->cmdline.height)
283 height = ivi->cmdline.height;
284 if (ivi->cmdline.scale)
285 weston_output_set_scale(output->output, ivi->cmdline.scale);
287 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
288 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
293 weston_log("Configured windowed_output_api to %dx%d\n", width, height);
299 parse_transform(const char *transform, uint32_t *out)
301 static const struct { const char *name; uint32_t token; } transforms[] = {
302 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
303 { "rotate-90", WL_OUTPUT_TRANSFORM_90 },
304 { "rotate-180", WL_OUTPUT_TRANSFORM_180 },
305 { "rotate-270", WL_OUTPUT_TRANSFORM_270 },
306 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
307 { "flipped-rotate-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
308 { "flipped-rotate-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
309 { "flipped-rotate-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
312 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
313 if (strcmp(transforms[i].name, transform) == 0) {
314 *out = transforms[i].token;
318 *out = WL_OUTPUT_TRANSFORM_NORMAL;
323 configure_output(struct ivi_output *output)
325 struct ivi_compositor *ivi = output->ivi;
326 struct weston_config_section *section = output->config;
328 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
331 * This can happen with the wayland backend with 'sprawl'. The config
332 * is hard-coded, so we don't need to do anything.
334 if (!ivi->drm_api && !ivi->window_api)
340 weston_config_section_get_int(section, "scale", &scale, 1);
341 weston_config_section_get_string(section, "transform", &t, "normal");
342 if (parse_transform(t, &transform) < 0)
343 weston_log("Invalid transform \"%s\" for output %s\n",
348 weston_output_set_scale(output->output, scale);
349 weston_output_set_transform(output->output, transform);
352 return drm_configure_output(output);
354 return windowed_configure_output(output);
358 * Reorgainizes the output's add array into two sections.
359 * add[0..ret-1] are the heads that failed to get attached.
360 * add[ret..add_len] are the heads that were successfully attached.
362 * The order between elements in each section is stable.
365 try_attach_heads(struct ivi_output *output)
369 for (size_t i = 0; i < output->add_len; ++i) {
370 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
371 struct weston_head *tmp = output->add[i];
372 memmove(&output->add[fail_len + 1], output->add[fail_len],
373 sizeof output->add[0] * (i - fail_len));
374 output->add[fail_len++] = tmp;
382 * Like try_attach_heads, this reorganizes the output's add array into a failed
383 * and successful section.
384 * i is the number of heads that already failed the previous step.
387 try_enable_output(struct ivi_output *output, size_t i)
389 for (; i < output->add_len; ++i) {
390 struct weston_head *head;
392 if (weston_output_enable(output->output) == 0)
395 head = output->add[output->add_len - 1];
396 memmove(&output->add[i + 1], &output->add[i],
397 sizeof output->add[0] * (output->add_len - i));
398 output->add[i] = head;
400 weston_head_detach(head);
407 try_attach_enable_heads(struct ivi_output *output)
410 assert(!output->output->enabled);
412 fail_len = try_attach_heads(output);
414 if (configure_output(output) < 0)
417 fail_len = try_enable_output(output, fail_len);
419 /* All heads failed to be attached */
420 if (fail_len == output->add_len)
423 /* For each successful head attached */
424 for (size_t i = fail_len; i < output->add_len; ++i)
425 add_head_destroyed_listener(output->add[i]);
427 output->add_len = fail_len;
432 process_output(struct ivi_output *output)
434 if (output->output->enabled) {
435 output->add_len = try_attach_heads(output);
436 return output->add_len == 0 ? 0 : -1;
439 return try_attach_enable_heads(output);
443 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
445 struct weston_output *output;
446 struct ivi_output *ivi_output;
447 struct wl_listener *listener;
449 output = weston_head_get_output(head);
452 listener = weston_output_get_destroy_listener(output,
453 handle_output_destroy);
456 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
457 assert(ivi_output->output == output);
459 weston_head_detach(head);
460 if (count_heads(ivi_output->output) == 0) {
461 weston_output_disable(ivi_output->output);
465 static struct weston_config_section *
466 find_controlling_output_config(struct weston_config *config,
469 struct weston_config_section *section;
473 same_as = strdup(name);
475 section = weston_config_get_section(config, "output",
477 if (!section && depth > 0)
478 weston_log("Configuration error: output section reffered"
479 "to by same-as=%s not found.\n", same_as);
486 weston_log("Configuration error: same-as nested too "
487 "deep for output '%s'.\n", name);
491 weston_config_section_get_string(section, "same-as",
499 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
501 const char *name = weston_head_get_name(head);
502 struct weston_config_section *section;
503 struct ivi_output *output;
504 char *output_name = NULL;
506 section = find_controlling_output_config(ivi->config, name);
510 weston_config_section_get_string(section, "mode", &mode, NULL);
511 if (mode && strcmp(mode, "off") == 0) {
517 weston_config_section_get_string(section, "name",
520 output_name = strdup(name);
526 output = ivi_ensure_output(ivi, output_name, section);
530 if (output->add_len >= ARRAY_LENGTH(output->add))
533 output->add[output->add_len++] = head;
537 heads_changed(struct wl_listener *listener, void *arg)
539 struct weston_compositor *compositor = arg;
540 struct weston_head *head = NULL;
541 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
542 struct ivi_output *output;
544 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
545 bool connected = weston_head_is_connected(head);
546 bool enabled = weston_head_is_enabled(head);
547 bool changed = weston_head_is_device_changed(head);
548 bool non_desktop = weston_head_is_non_desktop(head);
550 if (connected && !enabled && !non_desktop)
551 head_prepare_enable(ivi, head);
552 else if (!connected && enabled)
553 head_disable(ivi, head);
554 else if (enabled && changed)
555 weston_log("Detected a monitor change on head '%s', "
556 "not bothering to do anything about it.\n",
557 weston_head_get_name(head));
559 weston_head_reset_device_changed(head);
562 wl_list_for_each(output, &ivi->outputs, link) {
563 if (output->add_len == 0)
566 if (process_output(output) < 0) {
568 ivi->init_failed = true;
575 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
577 struct weston_compositor *compositor = ivi->compositor;
578 int (*module_init)(struct weston_compositor *wc);
580 module_init = weston_load_module("waltham-transmitter.so",
585 if (module_init(compositor) < 0)
588 ivi->waltham_transmitter_api = weston_get_transmitter_api(compositor);
589 if (!ivi->waltham_transmitter_api) {
590 weston_log("Failed to load waltham-transmitter plugin.\n");
594 weston_log("waltham-transmitter plug-in loaded\n");
599 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
607 drm_backend_remoted_output_configure(struct weston_output *output,
608 struct weston_config_section *section,
610 const struct weston_remoting_api *api)
612 char *gbm_format = NULL;
615 char *pipeline = NULL;
618 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
621 ret = api->set_mode(output, modeline);
623 weston_log("Cannot configure an output \"%s\" using "
624 "weston_remoting_api. Invalid mode\n",
629 weston_config_section_get_int(section, "scale", &scale, 1);
630 weston_output_set_scale(output, scale);
632 weston_config_section_get_string(section, "transform", &trans, "normal");
633 if (parse_transform(trans, &transform) < 0) {
634 weston_log("Invalid transform \"%s\" for output %s\n",
635 trans, output->name);
637 weston_output_set_transform(output, transform);
639 weston_config_section_get_string(section, "gbm-format",
641 api->set_gbm_format(output, gbm_format);
644 weston_config_section_get_string(section, "seat", &seat, "");
646 api->set_seat(output, seat);
649 weston_config_section_get_string(section, "gst-pipeline", &pipeline,
652 api->set_gst_pipeline(output, pipeline);
657 weston_config_section_get_string(section, "host", &host, NULL);
658 weston_config_section_get_int(section, "port", &port, 0);
659 if (!host || port <= 0 || 65533 < port) {
660 weston_log("Cannot configure an output \"%s\". "
661 "Need to specify gst-pipeline or "
662 "host and port (1-65533).\n", output->name);
664 api->set_host(output, host);
666 api->set_port(output, port);
673 remote_output_init(struct ivi_output *ivi_output,
674 struct weston_compositor *compositor,
675 struct weston_config_section *section,
676 const struct weston_remoting_api *api)
678 char *output_name, *modeline = NULL;
681 weston_config_section_get_string(section, "name", &output_name, NULL);
685 weston_config_section_get_string(section, "mode", &modeline, "off");
686 if (strcmp(modeline, "off") == 0)
689 ivi_output->output = api->create_output(compositor, output_name);
690 if (!ivi_output->output) {
691 weston_log("Cannot create remoted output \"%s\".\n",
696 ret = drm_backend_remoted_output_configure(ivi_output->output, section,
699 weston_log("Cannot configure remoted output \"%s\".\n",
704 if (weston_output_enable(ivi_output->output) < 0) {
705 weston_log("Enabling remoted output \"%s\" failed.\n",
712 weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
719 if (ivi_output->output)
720 weston_output_destroy(ivi_output->output);
726 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
728 struct weston_config_section *remote_section = NULL;
729 const char *section_name;
730 struct weston_config *config = ivi->config;
732 while (weston_config_next_section(config, &remote_section, §ion_name)) {
733 if (strcmp(section_name, "remote-output"))
736 struct ivi_output *ivi_output = NULL;
737 bool output_found = false;
740 weston_config_section_get_string(remote_section,
741 "name", &_name, NULL);
742 wl_list_for_each(ivi_output, &ivi->outputs, link) {
743 if (!strcmp(ivi_output->name, _name)) {
754 ivi_output = zalloc(sizeof(*ivi_output));
756 ivi_output->ivi = ivi;
757 ivi_output->name = _name;
758 ivi_output->config = remote_section;
759 ivi_output->type = OUTPUT_REMOTE;
761 if (remote_output_init(ivi_output, ivi->compositor,
762 remote_section, ivi->remoting_api)) {
763 free(ivi_output->name);
768 ivi_output->output_destroy.notify = handle_output_destroy;
769 weston_output_add_destroy_listener(ivi_output->output,
770 &ivi_output->output_destroy);
772 wl_list_insert(&ivi->outputs, &ivi_output->link);
773 ivi_output_configure_app_id(ivi_output);
778 ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
780 struct weston_config_section *transmitter_section = NULL;
781 const char *sect_name;
782 struct weston_config *config = ivi->config;
784 while (weston_config_next_section(config, &transmitter_section, §_name)) {
785 if (strcmp(sect_name, "transmitter-output"))
788 struct ivi_output *ivi_output = NULL;
789 bool output_found = false;
792 weston_config_section_get_string(transmitter_section,
793 "name", &_name, NULL);
794 wl_list_for_each(ivi_output, &ivi->outputs, link) {
795 if (!strcmp(ivi_output->name, _name)) {
806 ivi_output = zalloc(sizeof(*ivi_output));
808 ivi_output->ivi = ivi;
809 ivi_output->name = _name;
810 ivi_output->config = transmitter_section;
812 if (remote_output_init(ivi_output, ivi->compositor,
813 transmitter_section, ivi->remoting_api)) {
814 free(ivi_output->name);
819 ivi_output->type = OUTPUT_WALTHAM;
820 ivi_output->output_destroy.notify = handle_output_destroy;
821 weston_output_add_destroy_listener(ivi_output->output,
822 &ivi_output->output_destroy);
824 wl_list_insert(&ivi->outputs, &ivi_output->link);
825 ivi_output_configure_app_id(ivi_output);
830 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
832 struct weston_compositor *compositor = ivi->compositor;
833 int (*module_init)(struct weston_compositor *wc);
835 module_init = weston_load_module("remoting-plugin.so",
836 "weston_module_init");
840 if (module_init(compositor) < 0)
843 ivi->remoting_api = weston_remoting_get_api(compositor);
844 if (!ivi->remoting_api)
850 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
857 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
859 struct weston_drm_backend_config config = {
861 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
862 .struct_size = sizeof config,
865 struct weston_config_section *section;
866 int use_current_mode = 0;
871 const struct weston_option options[] = {
872 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
873 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
874 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
875 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
876 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
879 parse_options(options, ARRAY_LENGTH(options), argc, argv);
880 config.use_pixman = use_pixman;
881 ivi->cmdline.use_current_mode = use_current_mode;
883 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
884 weston_config_section_get_string(section, "gbm-format",
885 &config.gbm_format, NULL);
886 weston_config_section_get_uint(section, "pageflip-timeout",
887 &config.pageflip_timeout, 0);
888 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
889 config.use_pixman_shadow = use_shadow;
891 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
896 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
898 weston_log("Cannot use drm output api.\n");
903 load_remoting_plugin(ivi, ivi->config);
904 load_waltham_plugin(ivi, ivi->config);
907 free(config.gbm_format);
908 free(config.seat_id);
913 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
914 bool *use_pixman, bool *fullscreen, int *output_count)
916 struct weston_config_section *section;
920 const struct weston_option options[] = {
921 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
922 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
923 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
924 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
925 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
926 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
929 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
930 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
933 parse_options(options, ARRAY_LENGTH(options), argc, argv);
934 *use_pixman = pixman;
939 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
940 const char *match_prefix, const char *name_prefix)
942 struct weston_config_section *section = NULL;
943 const char *section_name;
944 char *default_output = NULL;
946 size_t match_len = strlen(match_prefix);
948 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
951 if (i >= output_count)
954 if (strcmp(section_name, "output") != 0)
957 weston_config_section_get_string(section, "name", &output_name, NULL);
958 if (output_name == NULL)
960 if (strncmp(output_name, match_prefix, match_len) != 0) {
965 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
974 for (; i < output_count; ++i) {
975 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
978 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
979 free(default_output);
983 free(default_output);
990 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
992 struct weston_wayland_backend_config config = {
994 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
995 .struct_size = sizeof config,
998 struct weston_config_section *section;
1003 const struct weston_option options[] = {
1004 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1005 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1008 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1009 &config.fullscreen, &output_count);
1011 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1012 config.sprawl = sprawl;
1014 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1015 weston_config_section_get_string(section, "cursor-theme",
1016 &config.cursor_theme, NULL);
1017 weston_config_section_get_int(section, "cursor-size",
1018 &config.cursor_size, 32);
1020 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1023 free(config.cursor_theme);
1024 free(config.display_name);
1029 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1032 * We will just assume if load_backend() finished cleanly and
1033 * windowed_output_api is not present that wayland backend is started
1034 * with --sprawl or runs on fullscreen-shell. In this case, all values
1035 * are hardcoded, so nothing can be configured; simply create and
1038 if (ivi->window_api == NULL)
1041 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1044 #ifdef HAVE_BACKEND_X11
1046 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1048 struct weston_x11_backend_config config = {
1050 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1051 .struct_size = sizeof config,
1058 const struct weston_option options[] = {
1059 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1062 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1063 &config.fullscreen, &output_count);
1065 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1066 config.no_input = no_input;
1068 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1074 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1075 if (!ivi->window_api) {
1076 weston_log("Cannot use weston_windowed_output_api.\n");
1080 return windowed_create_outputs(ivi, output_count, "X", "screen");
1084 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1091 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1093 struct weston_headless_backend_config config = {};
1101 struct weston_compositor *c = ivi->compositor;
1103 const struct weston_option options[] = {
1104 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1105 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &use_gl },
1108 windowed_parse_common_options(ivi, argc, argv, &use_pixman,
1109 &fullscreen, &output_count);
1111 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1112 config.use_pixman = use_pixman;
1113 config.use_gl = use_gl;
1115 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1116 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1118 /* load the actual headless-backend and configure it */
1119 ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1124 ivi->window_api = weston_windowed_output_get_api(c);
1125 if (!ivi->window_api) {
1126 weston_log("Cannot use weston_windowed_output_api.\n");
1130 if (ivi->window_api->create_head(c, "headless") < 0) {
1131 weston_log("Cannot create headless back-end\n");
1139 load_backend(struct ivi_compositor *ivi, const char *backend,
1140 int *argc, char *argv[])
1142 if (strcmp(backend, "drm-backend.so") == 0) {
1143 return load_drm_backend(ivi, argc, argv);
1144 } else if (strcmp(backend, "wayland-backend.so") == 0) {
1145 return load_wayland_backend(ivi, argc, argv);
1146 } else if (strcmp(backend, "x11-backend.so") == 0) {
1147 return load_x11_backend(ivi, argc, argv);
1148 } else if (strcmp(backend, "headless-backend.so") == 0) {
1149 return load_headless_backend(ivi, argc, argv);
1152 weston_log("fatal: unknown backend '%s'.\n", backend);
1157 load_modules(struct ivi_compositor *ivi, const char *modules,
1158 int *argc, char *argv[], bool *xwayland)
1160 const char *p, *end;
1162 int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1164 if (modules == NULL)
1169 end = strchrnul(p, ',');
1170 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1172 if (strstr(buffer, "xwayland.so")) {
1173 weston_log("Xwayland plug-in not supported!\n");
1180 module_init = weston_load_module(buffer, "wet_module_init");
1184 if (module_init(ivi->compositor, *argc, argv) < 0)
1197 choose_default_backend(void)
1199 char *backend = NULL;
1201 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1202 backend = strdup("wayland-backend.so");
1203 else if (getenv("DISPLAY"))
1204 backend = strdup("x11-backend.so");
1206 backend = strdup("drm-backend.so");
1212 compositor_init_config(struct weston_compositor *compositor,
1213 struct weston_config *config)
1215 struct xkb_rule_names xkb_names;
1216 struct weston_config_section *section;
1221 /* agl-compositor.ini [keyboard] */
1222 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1223 weston_config_section_get_string(section, "keymap_rules",
1224 (char **) &xkb_names.rules, NULL);
1225 weston_config_section_get_string(section, "keymap_model",
1226 (char **) &xkb_names.model, NULL);
1227 weston_config_section_get_string(section, "keymap_layout",
1228 (char **) &xkb_names.layout, NULL);
1229 weston_config_section_get_string(section, "keymap_variant",
1230 (char **) &xkb_names.variant, NULL);
1231 weston_config_section_get_string(section, "keymap_options",
1232 (char **) &xkb_names.options, NULL);
1234 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1237 weston_config_section_get_int(section, "repeat-rate",
1238 &compositor->kb_repeat_rate, 40);
1239 weston_config_section_get_int(section, "repeat-delay",
1240 &compositor->kb_repeat_delay, 400);
1242 weston_config_section_get_bool(section, "vt-switching",
1243 &vt_switching, true);
1244 compositor->vt_switching = vt_switching;
1246 /* agl-compositor.ini [core] */
1247 section = weston_config_get_section(config, "core", NULL, NULL);
1249 weston_config_section_get_bool(section, "require-input", &require_input, true);
1250 compositor->require_input = require_input;
1252 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1253 compositor->repaint_msec);
1254 if (repaint_msec < -10 || repaint_msec > 1000) {
1255 weston_log("Invalid repaint_window value in config: %d\n",
1258 compositor->repaint_msec = repaint_msec;
1260 weston_log("Output repaint window is %d ms maximum.\n",
1261 compositor->repaint_msec);
1266 struct ivi_surface *
1267 to_ivi_surface(struct weston_surface *surface)
1269 struct weston_desktop_surface *dsurface;
1271 dsurface = weston_surface_get_desktop_surface(surface);
1275 return weston_desktop_surface_get_user_data(dsurface);
1279 activate_binding(struct weston_seat *seat,
1280 struct weston_view *focus_view)
1282 struct weston_surface *focus = focus_view->surface;
1283 struct weston_surface *main_surface =
1284 weston_surface_get_main_surface(focus);
1285 struct ivi_surface *surface;
1287 surface = to_ivi_surface(main_surface);
1291 weston_seat_set_keyboard_focus(seat, focus);
1295 click_to_activate_binding(struct weston_pointer *pointer,
1296 const struct timespec *time,
1297 uint32_t button, void *data)
1299 if (pointer->grab != &pointer->default_grab)
1301 if (pointer->focus == NULL)
1304 activate_binding(pointer->seat, pointer->focus);
1308 touch_to_activate_binding(struct weston_touch *touch,
1309 const struct timespec *time,
1312 if (touch->grab != &touch->default_grab)
1314 if (touch->focus == NULL)
1317 activate_binding(touch->seat, touch->focus);
1321 add_bindings(struct weston_compositor *compositor)
1323 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1324 click_to_activate_binding,
1326 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1327 click_to_activate_binding,
1329 weston_compositor_add_touch_binding(compositor, 0,
1330 touch_to_activate_binding,
1335 create_listening_socket(struct wl_display *display, const char *socket_name)
1338 if (wl_display_add_socket(display, socket_name)) {
1339 weston_log("fatal: failed to add socket: %s\n",
1344 socket_name = wl_display_add_socket_auto(display);
1346 weston_log("fatal: failed to add socket: %s\n",
1352 setenv("WAYLAND_DISPLAY", socket_name, 1);
1358 global_filter(const struct wl_client *client, const struct wl_global *global,
1365 load_config(struct weston_config **config, bool no_config,
1366 const char *config_file)
1368 const char *file = "agl-compositor.ini";
1369 const char *full_path;
1375 *config = weston_config_parse(file);
1378 full_path = weston_config_get_full_path(*config);
1380 weston_log("Using config file '%s'.\n", full_path);
1381 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1386 if (config_file && !no_config) {
1387 weston_log("fatal: error opening or reading config file '%s'.\n",
1393 weston_log("Starting with no config file.\n");
1394 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1399 static FILE *logfile;
1402 log_timestamp(char *buf, size_t len)
1405 struct tm *brokendown_time;
1409 gettimeofday(&tv, NULL);
1411 brokendown_time = localtime(&tv.tv_sec);
1412 if (brokendown_time == NULL) {
1413 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1417 memset(datestr, 0, sizeof(datestr));
1418 if (brokendown_time->tm_mday != cached_tm_mday) {
1419 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1421 cached_tm_mday = brokendown_time->tm_mday;
1424 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1425 /* if datestr is empty it prints only timestr*/
1426 snprintf(buf, len, "%s[%s.%03li]", datestr,
1427 timestr, (tv.tv_usec / 1000));
1433 custom_handler(const char *fmt, va_list arg)
1437 weston_log_scope_printf(log_scope, "%s libwayland: ",
1438 log_timestamp(timestr, sizeof(timestr)));
1439 weston_log_scope_vprintf(log_scope, fmt, arg);
1443 log_file_open(const char *filename)
1445 wl_log_set_handler_server(custom_handler);
1448 logfile = fopen(filename, "a");
1453 os_fd_set_cloexec(fileno(logfile));
1454 setvbuf(logfile, NULL, _IOLBF, 256);
1459 log_file_close(void)
1461 if (logfile && logfile != stderr)
1467 vlog(const char *fmt, va_list ap)
1469 const char *oom = "Out of memory";
1474 if (weston_log_scope_is_enabled(log_scope)) {
1476 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1477 len_va = vasprintf(&str, fmt, ap);
1479 len = weston_log_scope_printf(log_scope, "%s %s",
1480 xlog_timestamp, str);
1483 len = weston_log_scope_printf(log_scope, "%s %s",
1484 xlog_timestamp, oom);
1493 vlog_continue(const char *fmt, va_list ap)
1495 return weston_log_scope_vprintf(log_scope, fmt, ap);
1499 on_term_signal(int signo, void *data)
1501 struct wl_display *display = data;
1503 weston_log("caught signal %d\n", signo);
1504 wl_display_terminate(display);
1510 handle_exit(struct weston_compositor *compositor)
1512 wl_display_terminate(compositor->wl_display);
1516 usage(int error_code)
1518 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1520 "Usage: agl-compositor [OPTIONS]\n"
1522 "This is " PACKAGE_STRING ", the reference compositor for\n"
1523 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1524 "backends,\nand depending on which backend is in use different "
1525 "options will be accepted.\n"
1529 " --version\t\tPrint agl-compositor version\n"
1530 " -B, --backend=MODULE\tBackend module, one of\n"
1531 "\t\t\t\tdrm-backend.so\n"
1532 "\t\t\t\twayland-backend.so\n"
1533 "\t\t\t\tx11-backend.so\n"
1534 "\t\t\t\theadless-backend.so\n"
1535 " -S, --socket=NAME\tName of socket to listen on\n"
1536 " --log=FILE\t\tLog to the given file\n"
1537 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1538 " --no-config\t\tDo not read agl-compositor.ini\n"
1539 " --debug\t\tEnable debug extension(s)\n"
1540 " -h, --help\t\tThis help message\n"
1546 copy_command_line(int argc, char * const argv[])
1553 fp = open_memstream(&str, &size);
1557 fprintf(fp, "%s", argv[0]);
1558 for (i = 1; i < argc; i++)
1559 fprintf(fp, " %s", argv[i]);
1566 int wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
1568 struct ivi_compositor ivi = { 0 };
1570 struct wl_display *display = NULL;
1571 struct wl_event_loop *loop;
1572 struct wl_event_source *signals[3] = { 0 };
1573 struct weston_config_section *section;
1574 /* Command line options */
1575 char *backend = NULL;
1576 char *socket_name = NULL;
1578 char *modules = NULL;
1579 char *option_modules = NULL;
1584 char *config_file = NULL;
1585 struct weston_log_context *log_ctx = NULL;
1586 struct weston_log_subscriber *logger;
1587 int ret = EXIT_FAILURE;
1588 bool xwayland = false;
1590 const struct weston_option core_options[] = {
1591 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1592 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1593 { WESTON_OPTION_STRING, "log", 0, &log },
1594 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1595 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1596 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1597 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1598 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1599 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1602 wl_list_init(&ivi.outputs);
1603 wl_list_init(&ivi.surfaces);
1604 wl_list_init(&ivi.pending_surfaces);
1605 wl_list_init(&ivi.popup_pending_apps);
1606 wl_list_init(&ivi.fullscreen_pending_apps);
1607 wl_list_init(&ivi.split_pending_apps);
1608 wl_list_init(&ivi.remote_pending_apps);
1609 wl_list_init(&ivi.desktop_clients);
1611 /* Prevent any clients we spawn getting our stdin */
1612 os_fd_set_cloexec(STDIN_FILENO);
1614 cmdline = copy_command_line(argc, argv);
1615 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1618 usage(EXIT_SUCCESS);
1621 printf(PACKAGE_STRING "\n");
1622 return EXIT_SUCCESS;
1625 log_ctx = weston_log_ctx_create();
1627 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1631 log_scope = weston_log_ctx_add_log_scope(log_ctx, "log",
1632 "agl-compositor log\n",
1636 weston_log_set_handler(vlog, vlog_continue);
1638 logger = weston_log_subscriber_create_log(logfile);
1639 weston_log_subscribe(log_ctx, logger, "log");
1641 weston_log("Command line: %s\n", cmdline);
1644 if (load_config(&ivi.config, no_config, config_file) < 0)
1646 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1648 weston_config_section_get_string(section, "backend", &backend,
1651 backend = choose_default_backend();
1654 weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
1655 weston_config_section_get_bool(section, "activate-by-default", &ivi.activate_by_default, true);
1657 display = wl_display_create();
1658 loop = wl_display_get_event_loop(display);
1660 wl_display_set_global_filter(display,
1661 global_filter, &ivi);
1663 /* Register signal handlers so we shut down cleanly */
1665 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1667 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1669 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1672 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1676 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi, test_data);
1677 if (!ivi.compositor) {
1678 weston_log("fatal: failed to create compositor.\n");
1682 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1683 goto error_compositor;
1685 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1686 weston_log("fatal: failed to create compositor backend.\n");
1687 goto error_compositor;
1690 ivi.heads_changed.notify = heads_changed;
1691 weston_compositor_add_heads_changed_listener(ivi.compositor,
1692 &ivi.heads_changed);
1694 if (ivi_desktop_init(&ivi) < 0)
1695 goto error_compositor;
1697 ivi_seat_init(&ivi);
1699 /* load additional modules */
1700 weston_config_section_get_string(section, "modules", &modules, "");
1701 if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
1702 goto error_compositor;
1704 if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
1705 goto error_compositor;
1707 if (ivi_policy_init(&ivi) < 0)
1708 goto error_compositor;
1710 if (ivi_shell_init(&ivi) < 0)
1711 goto error_compositor;
1713 add_bindings(ivi.compositor);
1715 weston_compositor_flush_heads_changed(ivi.compositor);
1717 if (ivi.remoting_api)
1718 ivi_enable_remote_outputs(&ivi);
1720 if (ivi.waltham_transmitter_api)
1721 ivi_enable_waltham_outputs(&ivi);
1723 if (create_listening_socket(display, socket_name) < 0)
1724 goto error_compositor;
1726 ivi_shell_init_black_fs(&ivi);
1728 ivi.compositor->exit = handle_exit;
1730 weston_compositor_wake(ivi.compositor);
1732 ivi_shell_create_global(&ivi);
1733 ivi_launch_shell_client(&ivi);
1735 ivi_screenshooter_create(&ivi);
1736 ivi_agl_systemd_notify(&ivi);
1738 wl_display_run(display);
1740 ret = ivi.compositor->exit_code;
1742 wl_display_destroy_clients(display);
1745 weston_compositor_destroy(ivi.compositor);
1746 weston_log_scope_destroy(log_scope);
1749 weston_log_subscriber_destroy(logger);
1750 weston_log_ctx_destroy(log_ctx);
1752 ivi_policy_destroy(ivi.policy);
1755 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1757 wl_event_source_remove(signals[i]);
1759 wl_display_destroy(display);
1763 weston_config_destroy(ivi.config);