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 sigint_helper(int sig)
83 handle_output_destroy(struct wl_listener *listener, void *data)
85 struct ivi_output *output;
87 output = wl_container_of(listener, output, output_destroy);
88 assert(output->output == data);
90 if (output->fullscreen_view.fs &&
91 output->fullscreen_view.fs->view) {
92 weston_surface_destroy(output->fullscreen_view.fs->view->surface);
93 output->fullscreen_view.fs->view = NULL;
96 output->output = NULL;
97 wl_list_remove(&output->output_destroy.link);
101 to_ivi_output(struct weston_output *o)
103 struct wl_listener *listener;
104 struct ivi_output *output;
106 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
107 output = wl_container_of(listener, output, output_destroy);
113 ivi_output_configure_app_id(struct ivi_output *ivi_output)
115 if (ivi_output->config) {
116 if (ivi_output->app_ids != NULL)
119 weston_config_section_get_string(ivi_output->config,
121 &ivi_output->app_ids,
124 if (ivi_output->app_ids == NULL)
127 weston_log("Will place app_id %s on output %s\n",
128 ivi_output->app_ids, ivi_output->name);
132 static struct ivi_output *
133 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
134 struct weston_config_section *config)
136 struct ivi_output *output = NULL;
137 wl_list_for_each(output, &ivi->outputs, link) {
138 if (strcmp(output->name, name) == 0) {
144 output = zalloc(sizeof *output);
152 output->config = config;
154 output->output = weston_compositor_create_output(ivi->compositor, name);
155 if (!output->output) {
161 output->output_destroy.notify = handle_output_destroy;
162 weston_output_add_destroy_listener(output->output,
163 &output->output_destroy);
165 wl_list_insert(&ivi->outputs, &output->link);
166 ivi_output_configure_app_id(output);
171 count_heads(struct weston_output *output)
173 struct weston_head *iter = NULL;
176 while ((iter = weston_output_iterate_heads(output, iter)))
183 handle_head_destroy(struct wl_listener *listener, void *data)
185 struct weston_head *head = data;
186 struct weston_output *output;
188 wl_list_remove(&listener->link);
191 output = weston_head_get_output(head);
193 /* On shutdown path, the output might be already gone. */
197 /* We're the last head */
198 if (count_heads(output) <= 1)
199 weston_output_destroy(output);
203 add_head_destroyed_listener(struct weston_head *head)
205 /* We already have a destroy listener */
206 if (weston_head_get_destroy_listener(head, handle_head_destroy))
209 struct wl_listener *listener = zalloc(sizeof *listener);
213 listener->notify = handle_head_destroy;
214 weston_head_add_destroy_listener(head, listener);
218 drm_configure_output(struct ivi_output *output)
220 struct ivi_compositor *ivi = output->ivi;
221 struct weston_config_section *section = output->config;
222 enum weston_drm_backend_output_mode mode =
223 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
224 char *modeline = NULL;
225 char *gbm_format = NULL;
230 weston_config_section_get_string(section, "mode", &m, "preferred");
232 /* This should have been handled earlier */
233 assert(strcmp(m, "off") != 0);
235 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
236 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
237 } else if (strcmp(m, "preferred") != 0) {
243 weston_config_section_get_string(section, "gbm-format",
246 weston_config_section_get_string(section, "seat", &seat, "");
249 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
250 weston_log("Cannot configure output using weston_drm_output_api.\n");
256 ivi->drm_api->set_gbm_format(output->output, gbm_format);
259 ivi->drm_api->set_seat(output->output, seat);
265 #define WINDOWED_DEFAULT_WIDTH 1024
266 #define WINDOWED_DEFAULT_HEIGHT 768
269 windowed_configure_output(struct ivi_output *output)
271 struct ivi_compositor *ivi = output->ivi;
272 struct weston_config_section *section = output->config;
273 int width = WINDOWED_DEFAULT_WIDTH;
274 int height = WINDOWED_DEFAULT_HEIGHT;
279 weston_config_section_get_string(section, "mode", &mode, NULL);
280 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
281 weston_log("Invalid mode for output %s. Using defaults.\n",
283 width = WINDOWED_DEFAULT_WIDTH;
284 height = WINDOWED_DEFAULT_HEIGHT;
289 if (ivi->cmdline.width)
290 width = ivi->cmdline.width;
291 if (ivi->cmdline.height)
292 height = ivi->cmdline.height;
293 if (ivi->cmdline.scale)
294 weston_output_set_scale(output->output, ivi->cmdline.scale);
296 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
297 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
302 weston_log("Configured windowed_output_api to %dx%d\n", width, height);
308 parse_transform(const char *transform, uint32_t *out)
310 static const struct { const char *name; uint32_t token; } transforms[] = {
311 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
312 { "rotate-90", WL_OUTPUT_TRANSFORM_90 },
313 { "rotate-180", WL_OUTPUT_TRANSFORM_180 },
314 { "rotate-270", WL_OUTPUT_TRANSFORM_270 },
315 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
316 { "flipped-rotate-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
317 { "flipped-rotate-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
318 { "flipped-rotate-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
321 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
322 if (strcmp(transforms[i].name, transform) == 0) {
323 *out = transforms[i].token;
327 *out = WL_OUTPUT_TRANSFORM_NORMAL;
332 parse_activation_area(const char *geometry, struct ivi_output *output)
335 unsigned width, height, x, y;
337 n = sscanf(geometry, "%ux%u+%u,%u", &width, &height, &x, &y);
341 output->area_activation.width = width;
342 output->area_activation.height = height;
343 output->area_activation.x = x;
344 output->area_activation.y = y;
349 configure_output(struct ivi_output *output)
351 struct ivi_compositor *ivi = output->ivi;
352 struct weston_config_section *section = output->config;
354 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
357 * This can happen with the wayland backend with 'sprawl'. The config
358 * is hard-coded, so we don't need to do anything.
360 if (!ivi->drm_api && !ivi->window_api)
366 weston_config_section_get_int(section, "scale", &scale, 1);
367 weston_config_section_get_string(section, "transform", &t, "normal");
368 if (parse_transform(t, &transform) < 0)
369 weston_log("Invalid transform \"%s\" for output %s\n",
371 weston_config_section_get_string(section, "activation-area", &t, "");
372 if (parse_activation_area(t, output) < 0)
373 weston_log("Invalid activation-area \"%s\" for output %s\n",
378 weston_output_set_scale(output->output, scale);
379 weston_output_set_transform(output->output, transform);
382 return drm_configure_output(output);
384 return windowed_configure_output(output);
388 * Reorgainizes the output's add array into two sections.
389 * add[0..ret-1] are the heads that failed to get attached.
390 * add[ret..add_len] are the heads that were successfully attached.
392 * The order between elements in each section is stable.
395 try_attach_heads(struct ivi_output *output)
399 for (size_t i = 0; i < output->add_len; ++i) {
400 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
401 struct weston_head *tmp = output->add[i];
402 memmove(&output->add[fail_len + 1], output->add[fail_len],
403 sizeof output->add[0] * (i - fail_len));
404 output->add[fail_len++] = tmp;
412 * Like try_attach_heads, this reorganizes the output's add array into a failed
413 * and successful section.
414 * i is the number of heads that already failed the previous step.
417 try_enable_output(struct ivi_output *output, size_t i)
419 for (; i < output->add_len; ++i) {
420 struct weston_head *head;
422 if (weston_output_enable(output->output) == 0)
425 head = output->add[output->add_len - 1];
426 memmove(&output->add[i + 1], &output->add[i],
427 sizeof output->add[0] * (output->add_len - i));
428 output->add[i] = head;
430 weston_head_detach(head);
437 try_attach_enable_heads(struct ivi_output *output)
440 assert(!output->output->enabled);
442 fail_len = try_attach_heads(output);
444 if (configure_output(output) < 0)
447 fail_len = try_enable_output(output, fail_len);
449 /* All heads failed to be attached */
450 if (fail_len == output->add_len)
453 /* For each successful head attached */
454 for (size_t i = fail_len; i < output->add_len; ++i)
455 add_head_destroyed_listener(output->add[i]);
457 output->add_len = fail_len;
462 process_output(struct ivi_output *output)
464 if (output->output->enabled) {
465 output->add_len = try_attach_heads(output);
466 return output->add_len == 0 ? 0 : -1;
469 return try_attach_enable_heads(output);
473 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
475 struct weston_output *output;
476 struct ivi_output *ivi_output;
477 struct wl_listener *listener;
479 output = weston_head_get_output(head);
482 listener = weston_output_get_destroy_listener(output,
483 handle_output_destroy);
486 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
487 assert(ivi_output->output == output);
489 weston_head_detach(head);
490 if (count_heads(ivi_output->output) == 0) {
491 if (ivi_output->output) {
492 /* ivi_output->output destruction may be deferred in
493 * some cases (see drm_output_destroy()), so we need to
494 * forcibly trigger the destruction callback now, or
495 * otherwise would later access data that we are about
498 struct weston_output *save = ivi_output->output;
500 handle_output_destroy(&ivi_output->output_destroy, save);
501 weston_output_destroy(save);
504 wl_list_remove(&ivi_output->link);
505 free(ivi_output->app_ids);
509 static struct weston_config_section *
510 find_controlling_output_config(struct weston_config *config,
513 struct weston_config_section *section;
517 same_as = strdup(name);
519 section = weston_config_get_section(config, "output",
521 if (!section && depth > 0)
522 weston_log("Configuration error: output section reffered"
523 "to by same-as=%s not found.\n", same_as);
530 weston_log("Configuration error: same-as nested too "
531 "deep for output '%s'.\n", name);
535 weston_config_section_get_string(section, "same-as",
543 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
545 const char *name = weston_head_get_name(head);
546 struct weston_config_section *section;
547 struct ivi_output *output;
548 char *output_name = NULL;
550 section = find_controlling_output_config(ivi->config, name);
554 weston_config_section_get_string(section, "mode", &mode, NULL);
555 if (mode && strcmp(mode, "off") == 0) {
561 weston_config_section_get_string(section, "name",
564 output_name = strdup(name);
570 output = ivi_ensure_output(ivi, output_name, section);
574 if (output->add_len >= ARRAY_LENGTH(output->add))
577 output->add[output->add_len++] = head;
581 heads_changed(struct wl_listener *listener, void *arg)
583 struct weston_compositor *compositor = arg;
584 struct weston_head *head = NULL;
585 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
586 struct ivi_output *output;
588 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
589 bool connected = weston_head_is_connected(head);
590 bool enabled = weston_head_is_enabled(head);
591 bool changed = weston_head_is_device_changed(head);
592 bool non_desktop = weston_head_is_non_desktop(head);
594 if (connected && !enabled && !non_desktop)
595 head_prepare_enable(ivi, head);
596 else if (!connected && enabled)
597 head_disable(ivi, head);
598 else if (enabled && changed)
599 weston_log("Detected a monitor change on head '%s', "
600 "not bothering to do anything about it.\n",
601 weston_head_get_name(head));
603 weston_head_reset_device_changed(head);
606 wl_list_for_each(output, &ivi->outputs, link) {
607 if (output->add_len == 0)
610 if (process_output(output) < 0) {
612 ivi->init_failed = true;
619 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
621 struct weston_compositor *compositor = ivi->compositor;
622 int (*module_init)(struct weston_compositor *wc);
624 module_init = weston_load_module("waltham-transmitter.so",
629 if (module_init(compositor) < 0)
632 ivi->waltham_transmitter_api = weston_get_transmitter_api(compositor);
633 if (!ivi->waltham_transmitter_api) {
634 weston_log("Failed to load waltham-transmitter plugin.\n");
638 weston_log("waltham-transmitter plug-in loaded\n");
643 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
651 drm_backend_remoted_output_configure(struct weston_output *output,
652 struct weston_config_section *section,
654 const struct weston_remoting_api *api)
656 char *gbm_format = NULL;
659 char *pipeline = NULL;
662 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
665 ret = api->set_mode(output, modeline);
667 weston_log("Cannot configure an output \"%s\" using "
668 "weston_remoting_api. Invalid mode\n",
673 weston_config_section_get_int(section, "scale", &scale, 1);
674 weston_output_set_scale(output, scale);
676 weston_config_section_get_string(section, "transform", &trans, "normal");
677 if (parse_transform(trans, &transform) < 0) {
678 weston_log("Invalid transform \"%s\" for output %s\n",
679 trans, output->name);
681 weston_output_set_transform(output, transform);
683 weston_config_section_get_string(section, "gbm-format",
685 api->set_gbm_format(output, gbm_format);
688 weston_config_section_get_string(section, "seat", &seat, "");
690 api->set_seat(output, seat);
693 weston_config_section_get_string(section, "gst-pipeline", &pipeline,
696 api->set_gst_pipeline(output, pipeline);
701 weston_config_section_get_string(section, "host", &host, NULL);
702 weston_config_section_get_int(section, "port", &port, 0);
703 if (!host || port <= 0 || 65533 < port) {
704 weston_log("Cannot configure an output \"%s\". "
705 "Need to specify gst-pipeline or "
706 "host and port (1-65533).\n", output->name);
708 api->set_host(output, host);
710 api->set_port(output, port);
717 remote_output_init(struct ivi_output *ivi_output,
718 struct weston_compositor *compositor,
719 struct weston_config_section *section,
720 const struct weston_remoting_api *api)
722 char *output_name, *modeline = NULL;
725 weston_config_section_get_string(section, "name", &output_name, NULL);
729 weston_config_section_get_string(section, "mode", &modeline, "off");
730 if (strcmp(modeline, "off") == 0)
733 ivi_output->output = api->create_output(compositor, output_name);
734 if (!ivi_output->output) {
735 weston_log("Cannot create remoted output \"%s\".\n",
740 ret = drm_backend_remoted_output_configure(ivi_output->output, section,
743 weston_log("Cannot configure remoted output \"%s\".\n",
748 if (weston_output_enable(ivi_output->output) < 0) {
749 weston_log("Enabling remoted output \"%s\" failed.\n",
756 weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
763 if (ivi_output->output)
764 weston_output_destroy(ivi_output->output);
770 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
772 struct weston_config_section *remote_section = NULL;
773 const char *section_name;
774 struct weston_config *config = ivi->config;
776 while (weston_config_next_section(config, &remote_section, §ion_name)) {
777 if (strcmp(section_name, "remote-output"))
780 struct ivi_output *ivi_output = NULL;
781 bool output_found = false;
784 weston_config_section_get_string(remote_section,
785 "name", &_name, NULL);
786 wl_list_for_each(ivi_output, &ivi->outputs, link) {
787 if (!strcmp(ivi_output->name, _name)) {
798 ivi_output = zalloc(sizeof(*ivi_output));
804 ivi_output->ivi = ivi;
805 ivi_output->name = _name;
806 ivi_output->config = remote_section;
807 ivi_output->type = OUTPUT_REMOTE;
809 if (remote_output_init(ivi_output, ivi->compositor,
810 remote_section, ivi->remoting_api)) {
811 free(ivi_output->name);
816 ivi_output->output_destroy.notify = handle_output_destroy;
817 weston_output_add_destroy_listener(ivi_output->output,
818 &ivi_output->output_destroy);
820 wl_list_insert(&ivi->outputs, &ivi_output->link);
821 ivi_output_configure_app_id(ivi_output);
826 ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
828 struct weston_config_section *transmitter_section = NULL;
829 const char *sect_name;
830 struct weston_config *config = ivi->config;
832 while (weston_config_next_section(config, &transmitter_section, §_name)) {
833 if (strcmp(sect_name, "transmitter-output"))
836 struct ivi_output *ivi_output = NULL;
837 bool output_found = false;
840 weston_config_section_get_string(transmitter_section,
841 "name", &_name, NULL);
842 wl_list_for_each(ivi_output, &ivi->outputs, link) {
843 if (!strcmp(ivi_output->name, _name)) {
854 ivi_output = zalloc(sizeof(*ivi_output));
860 ivi_output->ivi = ivi;
861 ivi_output->name = _name;
862 ivi_output->config = transmitter_section;
864 if (remote_output_init(ivi_output, ivi->compositor,
865 transmitter_section, ivi->remoting_api)) {
866 free(ivi_output->name);
871 ivi_output->type = OUTPUT_WALTHAM;
872 ivi_output->output_destroy.notify = handle_output_destroy;
873 weston_output_add_destroy_listener(ivi_output->output,
874 &ivi_output->output_destroy);
876 wl_list_insert(&ivi->outputs, &ivi_output->link);
877 ivi_output_configure_app_id(ivi_output);
882 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
884 struct weston_compositor *compositor = ivi->compositor;
885 int (*module_init)(struct weston_compositor *wc);
887 module_init = weston_load_module("remoting-plugin.so",
888 "weston_module_init");
892 if (module_init(compositor) < 0)
895 ivi->remoting_api = weston_remoting_get_api(compositor);
896 if (!ivi->remoting_api)
902 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
909 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
911 struct weston_drm_backend_config config = {
913 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
914 .struct_size = sizeof config,
917 struct weston_config_section *section;
918 int use_current_mode = 0;
921 bool without_input = false;
924 const struct weston_option options[] = {
925 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
926 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
927 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
928 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
929 { WESTON_OPTION_BOOLEAN, "continue-without-input", false, &without_input }
932 parse_options(options, ARRAY_LENGTH(options), argc, argv);
933 config.use_pixman = use_pixman;
934 ivi->cmdline.use_current_mode = use_current_mode;
936 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
937 weston_config_section_get_string(section, "gbm-format",
938 &config.gbm_format, NULL);
939 weston_config_section_get_uint(section, "pageflip-timeout",
940 &config.pageflip_timeout, 0);
941 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
942 config.use_pixman_shadow = use_shadow;
945 ivi->compositor->require_input = !without_input;
947 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
952 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
954 weston_log("Cannot use drm output api.\n");
959 load_remoting_plugin(ivi, ivi->config);
960 load_waltham_plugin(ivi, ivi->config);
963 free(config.gbm_format);
964 free(config.seat_id);
969 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
970 bool *use_pixman, bool *fullscreen, int *output_count)
972 struct weston_config_section *section;
976 const struct weston_option options[] = {
977 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
978 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
979 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
980 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
981 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
982 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
985 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
986 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
989 parse_options(options, ARRAY_LENGTH(options), argc, argv);
990 *use_pixman = pixman;
995 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
996 const char *match_prefix, const char *name_prefix)
998 struct weston_config_section *section = NULL;
999 const char *section_name;
1000 char *default_output = NULL;
1002 size_t match_len = strlen(match_prefix);
1004 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
1007 if (i >= output_count)
1010 if (strcmp(section_name, "output") != 0)
1013 weston_config_section_get_string(section, "name", &output_name, NULL);
1014 if (output_name == NULL)
1016 if (strncmp(output_name, match_prefix, match_len) != 0) {
1021 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
1030 for (; i < output_count; ++i) {
1031 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
1034 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
1035 free(default_output);
1039 free(default_output);
1046 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1048 struct weston_wayland_backend_config config = {
1050 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
1051 .struct_size = sizeof config,
1054 struct weston_config_section *section;
1059 const struct weston_option options[] = {
1060 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1061 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1064 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1065 &config.fullscreen, &output_count);
1067 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1068 config.sprawl = sprawl;
1070 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1071 weston_config_section_get_string(section, "cursor-theme",
1072 &config.cursor_theme, NULL);
1073 weston_config_section_get_int(section, "cursor-size",
1074 &config.cursor_size, 32);
1076 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1079 free(config.cursor_theme);
1080 free(config.display_name);
1085 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1088 * We will just assume if load_backend() finished cleanly and
1089 * windowed_output_api is not present that wayland backend is started
1090 * with --sprawl or runs on fullscreen-shell. In this case, all values
1091 * are hardcoded, so nothing can be configured; simply create and
1094 if (ivi->window_api == NULL)
1097 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1100 #ifdef HAVE_BACKEND_X11
1102 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1104 struct weston_x11_backend_config config = {
1106 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1107 .struct_size = sizeof config,
1114 const struct weston_option options[] = {
1115 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1118 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1119 &config.fullscreen, &output_count);
1121 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1122 config.no_input = no_input;
1124 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1130 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1131 if (!ivi->window_api) {
1132 weston_log("Cannot use weston_windowed_output_api.\n");
1136 return windowed_create_outputs(ivi, output_count, "X", "screen");
1140 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1146 #ifdef HAVE_BACKEND_HEADLESS
1148 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1150 struct weston_headless_backend_config config = {};
1158 struct weston_compositor *c = ivi->compositor;
1160 const struct weston_option options[] = {
1161 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1162 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &use_gl },
1165 windowed_parse_common_options(ivi, argc, argv, &use_pixman,
1166 &fullscreen, &output_count);
1168 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1169 config.use_pixman = use_pixman;
1170 config.use_gl = use_gl;
1172 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1173 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1175 /* load the actual headless-backend and configure it */
1176 ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1181 ivi->window_api = weston_windowed_output_get_api(c);
1182 if (!ivi->window_api) {
1183 weston_log("Cannot use weston_windowed_output_api.\n");
1187 if (ivi->window_api->create_head(c, "headless") < 0) {
1188 weston_log("Cannot create headless back-end\n");
1196 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1203 load_backend(struct ivi_compositor *ivi, const char *backend,
1204 int *argc, char *argv[])
1206 if (strcmp(backend, "drm-backend.so") == 0) {
1207 return load_drm_backend(ivi, argc, argv);
1208 } else if (strcmp(backend, "wayland-backend.so") == 0) {
1209 return load_wayland_backend(ivi, argc, argv);
1210 } else if (strcmp(backend, "x11-backend.so") == 0) {
1211 return load_x11_backend(ivi, argc, argv);
1212 } else if (strcmp(backend, "headless-backend.so") == 0) {
1213 return load_headless_backend(ivi, argc, argv);
1216 weston_log("fatal: unknown backend '%s'.\n", backend);
1221 load_modules(struct ivi_compositor *ivi, const char *modules,
1222 int *argc, char *argv[], bool *xwayland)
1224 const char *p, *end;
1226 int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1228 if (modules == NULL)
1233 end = strchrnul(p, ',');
1234 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1236 if (strstr(buffer, "xwayland.so")) {
1237 weston_log("Xwayland plug-in not supported!\n");
1244 if (strstr(buffer, "systemd-notify.so")) {
1245 weston_log("systemd-notify plug-in already loaded!\n");
1252 module_init = weston_load_module(buffer, "wet_module_init");
1256 if (module_init(ivi->compositor, *argc, argv) < 0)
1269 choose_default_backend(void)
1271 char *backend = NULL;
1273 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1274 backend = strdup("wayland-backend.so");
1275 else if (getenv("DISPLAY"))
1276 backend = strdup("x11-backend.so");
1278 backend = strdup("drm-backend.so");
1284 compositor_init_config(struct ivi_compositor *ivi)
1286 struct xkb_rule_names xkb_names;
1287 struct weston_config_section *section;
1288 struct weston_compositor *compositor = ivi->compositor;
1289 struct weston_config *config = ivi->config;
1294 /* agl-compositor.ini [keyboard] */
1295 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1296 weston_config_section_get_string(section, "keymap_rules",
1297 (char **) &xkb_names.rules, NULL);
1298 weston_config_section_get_string(section, "keymap_model",
1299 (char **) &xkb_names.model, NULL);
1300 weston_config_section_get_string(section, "keymap_layout",
1301 (char **) &xkb_names.layout, NULL);
1302 weston_config_section_get_string(section, "keymap_variant",
1303 (char **) &xkb_names.variant, NULL);
1304 weston_config_section_get_string(section, "keymap_options",
1305 (char **) &xkb_names.options, NULL);
1307 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1310 weston_config_section_get_int(section, "repeat-rate",
1311 &compositor->kb_repeat_rate, 40);
1312 weston_config_section_get_int(section, "repeat-delay",
1313 &compositor->kb_repeat_delay, 400);
1315 weston_config_section_get_bool(section, "vt-switching",
1316 &vt_switching, false);
1317 compositor->vt_switching = vt_switching;
1319 /* agl-compositor.ini [core] */
1320 section = weston_config_get_section(config, "core", NULL, NULL);
1322 weston_config_section_get_bool(section, "disable-cursor",
1323 &ivi->disable_cursor, false);
1324 weston_config_section_get_bool(section, "activate-by-default",
1325 &ivi->activate_by_default, true);
1327 weston_config_section_get_bool(section, "require-input", &require_input, true);
1328 compositor->require_input = require_input;
1330 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1331 compositor->repaint_msec);
1332 if (repaint_msec < -10 || repaint_msec > 1000) {
1333 weston_log("Invalid repaint_window value in config: %d\n",
1336 compositor->repaint_msec = repaint_msec;
1338 weston_log("Output repaint window is %d ms maximum.\n",
1339 compositor->repaint_msec);
1344 struct ivi_surface *
1345 to_ivi_surface(struct weston_surface *surface)
1347 struct weston_desktop_surface *dsurface;
1349 dsurface = weston_surface_get_desktop_surface(surface);
1353 return weston_desktop_surface_get_user_data(dsurface);
1357 activate_binding(struct weston_seat *seat,
1358 struct weston_view *focus_view, uint32_t flags)
1360 struct weston_surface *focus_surface;
1361 struct weston_surface *main_surface;
1362 struct ivi_surface *ivi_surface;
1363 struct ivi_shell_seat *ivi_seat = get_ivi_shell_seat(seat);
1368 focus_surface = focus_view->surface;
1369 main_surface = weston_surface_get_main_surface(focus_surface);
1371 ivi_surface = to_ivi_surface(main_surface);
1376 ivi_shell_activate_surface(ivi_surface, ivi_seat, flags);
1380 click_to_activate_binding(struct weston_pointer *pointer,
1381 const struct timespec *time,
1382 uint32_t button, void *data)
1384 if (pointer->grab != &pointer->default_grab)
1386 if (pointer->focus == NULL)
1389 activate_binding(pointer->seat, pointer->focus,
1390 WESTON_ACTIVATE_FLAG_CLICKED);
1394 touch_to_activate_binding(struct weston_touch *touch,
1395 const struct timespec *time,
1398 if (touch->grab != &touch->default_grab)
1400 if (touch->focus == NULL)
1403 activate_binding(touch->seat, touch->focus,
1404 WESTON_ACTIVATE_FLAG_NONE);
1408 add_bindings(struct weston_compositor *compositor)
1410 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1411 click_to_activate_binding,
1413 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1414 click_to_activate_binding,
1416 weston_compositor_add_touch_binding(compositor, 0,
1417 touch_to_activate_binding,
1422 create_listening_socket(struct wl_display *display, const char *socket_name)
1425 if (wl_display_add_socket(display, socket_name)) {
1426 weston_log("fatal: failed to add socket: %s\n",
1431 socket_name = wl_display_add_socket_auto(display);
1433 weston_log("fatal: failed to add socket: %s\n",
1439 setenv("WAYLAND_DISPLAY", socket_name, 1);
1445 global_filter(const struct wl_client *client, const struct wl_global *global,
1452 load_config(struct weston_config **config, bool no_config,
1453 const char *config_file)
1455 const char *file = "agl-compositor.ini";
1456 const char *full_path;
1462 *config = weston_config_parse(file);
1465 full_path = weston_config_get_full_path(*config);
1467 weston_log("Using config file '%s'.\n", full_path);
1468 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1473 if (config_file && !no_config) {
1474 weston_log("fatal: error opening or reading config file '%s'.\n",
1480 weston_log("Starting with no config file.\n");
1481 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1486 static FILE *logfile;
1489 log_timestamp(char *buf, size_t len)
1492 struct tm *brokendown_time;
1496 gettimeofday(&tv, NULL);
1498 brokendown_time = localtime(&tv.tv_sec);
1499 if (brokendown_time == NULL) {
1500 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1504 memset(datestr, 0, sizeof(datestr));
1505 if (brokendown_time->tm_mday != cached_tm_mday) {
1506 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1508 cached_tm_mday = brokendown_time->tm_mday;
1511 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1512 /* if datestr is empty it prints only timestr*/
1513 snprintf(buf, len, "%s[%s.%03li]", datestr,
1514 timestr, (tv.tv_usec / 1000));
1520 custom_handler(const char *fmt, va_list arg)
1524 weston_log_scope_printf(log_scope, "%s libwayland: ",
1525 log_timestamp(timestr, sizeof(timestr)));
1526 weston_log_scope_vprintf(log_scope, fmt, arg);
1530 log_file_open(const char *filename)
1532 wl_log_set_handler_server(custom_handler);
1535 logfile = fopen(filename, "a");
1540 os_fd_set_cloexec(fileno(logfile));
1541 setvbuf(logfile, NULL, _IOLBF, 256);
1546 log_file_close(void)
1548 if (logfile && logfile != stderr)
1554 vlog(const char *fmt, va_list ap)
1556 const char *oom = "Out of memory";
1561 if (weston_log_scope_is_enabled(log_scope)) {
1563 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1564 len_va = vasprintf(&str, fmt, ap);
1566 len = weston_log_scope_printf(log_scope, "%s %s",
1567 xlog_timestamp, str);
1570 len = weston_log_scope_printf(log_scope, "%s %s",
1571 xlog_timestamp, oom);
1580 vlog_continue(const char *fmt, va_list ap)
1582 return weston_log_scope_vprintf(log_scope, fmt, ap);
1586 on_term_signal(int signo, void *data)
1588 struct wl_display *display = data;
1590 weston_log("caught signal %d\n", signo);
1591 wl_display_terminate(display);
1597 handle_exit(struct weston_compositor *compositor)
1599 wl_display_terminate(compositor->wl_display);
1603 usage(int error_code)
1605 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1607 "Usage: agl-compositor [OPTIONS]\n"
1609 "This is " PACKAGE_STRING ", the reference compositor for\n"
1610 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1611 "backends,\nand depending on which backend is in use different "
1612 "options will be accepted.\n"
1616 " --version\t\tPrint agl-compositor version\n"
1617 " -B, --backend=MODULE\tBackend module, one of\n"
1618 "\t\t\t\tdrm-backend.so\n"
1619 "\t\t\t\twayland-backend.so\n"
1620 "\t\t\t\tx11-backend.so\n"
1621 "\t\t\t\theadless-backend.so\n"
1622 " -S, --socket=NAME\tName of socket to listen on\n"
1623 " --log=FILE\t\tLog to the given file\n"
1624 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1625 " --no-config\t\tDo not read agl-compositor.ini\n"
1626 " --debug\t\tEnable debug extension(s)\n"
1627 " -h, --help\t\tThis help message\n"
1633 copy_command_line(int argc, char * const argv[])
1640 fp = open_memstream(&str, &size);
1644 fprintf(fp, "%s", argv[0]);
1645 for (i = 1; i < argc; i++)
1646 fprintf(fp, " %s", argv[i]);
1653 int wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
1655 struct ivi_compositor ivi = { 0 };
1657 struct wl_display *display = NULL;
1658 struct wl_event_loop *loop;
1659 struct wl_event_source *signals[3] = { 0 };
1660 struct weston_config_section *section;
1661 /* Command line options */
1662 char *backend = NULL;
1663 char *socket_name = NULL;
1665 char *modules = NULL;
1666 char *option_modules = NULL;
1671 char *config_file = NULL;
1672 struct weston_log_context *log_ctx = NULL;
1673 struct weston_log_subscriber *logger;
1674 int ret = EXIT_FAILURE;
1675 bool xwayland = false;
1676 struct sigaction action;
1678 const struct weston_option core_options[] = {
1679 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1680 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1681 { WESTON_OPTION_STRING, "log", 0, &log },
1682 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1683 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1684 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1685 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1686 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1687 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1690 wl_list_init(&ivi.outputs);
1691 wl_list_init(&ivi.surfaces);
1692 wl_list_init(&ivi.pending_surfaces);
1693 wl_list_init(&ivi.popup_pending_apps);
1694 wl_list_init(&ivi.fullscreen_pending_apps);
1695 wl_list_init(&ivi.split_pending_apps);
1696 wl_list_init(&ivi.remote_pending_apps);
1697 wl_list_init(&ivi.desktop_clients);
1698 wl_list_init(&ivi.child_process_list);
1700 /* Prevent any clients we spawn getting our stdin */
1701 os_fd_set_cloexec(STDIN_FILENO);
1703 cmdline = copy_command_line(argc, argv);
1704 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1707 usage(EXIT_SUCCESS);
1710 printf(PACKAGE_STRING "\n");
1715 log_ctx = weston_log_ctx_create();
1717 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1721 log_scope = weston_log_ctx_add_log_scope(log_ctx, "log",
1722 "agl-compositor log\n",
1726 weston_log_set_handler(vlog, vlog_continue);
1728 logger = weston_log_subscriber_create_log(logfile);
1729 weston_log_subscribe(log_ctx, logger, "log");
1731 weston_log("Command line: %s\n", cmdline);
1734 if (load_config(&ivi.config, no_config, config_file) < 0)
1736 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1738 weston_config_section_get_string(section, "backend", &backend,
1741 backend = choose_default_backend();
1744 display = wl_display_create();
1745 loop = wl_display_get_event_loop(display);
1747 wl_display_set_global_filter(display,
1748 global_filter, &ivi);
1750 /* Register signal handlers so we shut down cleanly */
1752 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1754 signals[1] = wl_event_loop_add_signal(loop, SIGUSR2, on_term_signal,
1756 signals[2] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1759 /* When debugging the compositor, if use wl_event_loop_add_signal() to
1760 * catch SIGINT, the debugger can't catch it, and attempting to stop
1761 * the compositor from within the debugger results in weston exiting
1764 * Instead, use the sigaction() function, which sets up the signal in a
1765 * way that gdb can successfully catch, but have the handler for SIGINT
1766 * send SIGUSR2 (xwayland uses SIGUSR1), which we catch via
1767 * wl_event_loop_add_signal().
1769 action.sa_handler = sigint_helper;
1770 sigemptyset(&action.sa_mask);
1771 action.sa_flags = 0;
1772 sigaction(SIGINT, &action, NULL);
1774 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1778 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi, test_data);
1779 if (!ivi.compositor) {
1780 weston_log("fatal: failed to create compositor.\n");
1784 if (compositor_init_config(&ivi) < 0)
1785 goto error_compositor;
1787 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1788 weston_log("fatal: failed to create compositor backend.\n");
1789 goto error_compositor;
1792 ivi.heads_changed.notify = heads_changed;
1793 weston_compositor_add_heads_changed_listener(ivi.compositor,
1794 &ivi.heads_changed);
1796 if (ivi_desktop_init(&ivi) < 0)
1797 goto error_compositor;
1799 ivi_seat_init(&ivi);
1801 /* load additional modules */
1802 weston_config_section_get_string(section, "modules", &modules, "");
1803 if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
1804 goto error_compositor;
1806 if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
1807 goto error_compositor;
1809 if (ivi_policy_init(&ivi) < 0)
1810 goto error_compositor;
1812 if (ivi_shell_init(&ivi) < 0)
1813 goto error_compositor;
1815 add_bindings(ivi.compositor);
1817 weston_compositor_flush_heads_changed(ivi.compositor);
1819 if (ivi.remoting_api)
1820 ivi_enable_remote_outputs(&ivi);
1822 if (ivi.waltham_transmitter_api)
1823 ivi_enable_waltham_outputs(&ivi);
1825 if (create_listening_socket(display, socket_name) < 0)
1826 goto error_compositor;
1828 ivi_shell_init_black_fs(&ivi);
1830 ivi.compositor->exit = handle_exit;
1832 weston_compositor_wake(ivi.compositor);
1834 ivi_shell_create_global(&ivi);
1836 ivi_launch_shell_client(&ivi, "shell-client",
1837 &ivi.shell_client.client);
1838 ivi_launch_shell_client(&ivi, "shell-client-ext",
1839 &ivi.shell_client_ext.client);
1842 ivi_screenshooter_create(&ivi);
1843 ivi_agl_systemd_notify(&ivi);
1845 wl_display_run(display);
1847 ret = ivi.compositor->exit_code;
1849 wl_display_destroy_clients(display);
1857 weston_compositor_destroy(ivi.compositor);
1859 weston_log_scope_destroy(log_scope);
1862 weston_log_subscriber_destroy(logger);
1863 weston_log_ctx_destroy(log_ctx);
1865 ivi_policy_destroy(ivi.policy);
1868 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1870 wl_event_source_remove(signals[i]);
1872 wl_display_destroy(display);
1876 weston_config_destroy(ivi.config);
1882 free(option_modules);