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 { "90", WL_OUTPUT_TRANSFORM_90 },
306 { "180", WL_OUTPUT_TRANSFORM_180 },
307 { "270", WL_OUTPUT_TRANSFORM_270 },
308 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
309 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
310 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
311 { "flipped-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));
758 ivi_output->ivi = ivi;
759 ivi_output->name = _name;
760 ivi_output->config = remote_section;
761 ivi_output->type = OUTPUT_REMOTE;
763 if (remote_output_init(ivi_output, ivi->compositor,
764 remote_section, ivi->remoting_api)) {
765 free(ivi_output->name);
770 ivi_output->output_destroy.notify = handle_output_destroy;
771 weston_output_add_destroy_listener(ivi_output->output,
772 &ivi_output->output_destroy);
774 wl_list_insert(&ivi->outputs, &ivi_output->link);
775 ivi_output_configure_app_id(ivi_output);
780 ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
782 struct weston_config_section *transmitter_section = NULL;
783 const char *sect_name;
784 struct weston_config *config = ivi->config;
786 while (weston_config_next_section(config, &transmitter_section, §_name)) {
787 if (strcmp(sect_name, "transmitter-output"))
790 struct ivi_output *ivi_output = NULL;
791 bool output_found = false;
794 weston_config_section_get_string(transmitter_section,
795 "name", &_name, NULL);
796 wl_list_for_each(ivi_output, &ivi->outputs, link) {
797 if (!strcmp(ivi_output->name, _name)) {
808 ivi_output = zalloc(sizeof(*ivi_output));
810 ivi_output->ivi = ivi;
811 ivi_output->name = _name;
812 ivi_output->config = transmitter_section;
814 if (remote_output_init(ivi_output, ivi->compositor,
815 transmitter_section, ivi->remoting_api)) {
816 free(ivi_output->name);
821 ivi_output->type = OUTPUT_WALTHAM;
822 ivi_output->output_destroy.notify = handle_output_destroy;
823 weston_output_add_destroy_listener(ivi_output->output,
824 &ivi_output->output_destroy);
826 wl_list_insert(&ivi->outputs, &ivi_output->link);
827 ivi_output_configure_app_id(ivi_output);
832 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
834 struct weston_compositor *compositor = ivi->compositor;
835 int (*module_init)(struct weston_compositor *wc);
837 module_init = weston_load_module("remoting-plugin.so",
838 "weston_module_init");
842 if (module_init(compositor) < 0)
845 ivi->remoting_api = weston_remoting_get_api(compositor);
846 if (!ivi->remoting_api)
852 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
859 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
861 struct weston_drm_backend_config config = {
863 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
864 .struct_size = sizeof config,
867 struct weston_config_section *section;
868 int use_current_mode = 0;
873 const struct weston_option options[] = {
874 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
875 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
876 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
877 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
878 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
881 parse_options(options, ARRAY_LENGTH(options), argc, argv);
882 config.use_pixman = use_pixman;
883 ivi->cmdline.use_current_mode = use_current_mode;
885 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
886 weston_config_section_get_string(section, "gbm-format",
887 &config.gbm_format, NULL);
888 weston_config_section_get_uint(section, "pageflip-timeout",
889 &config.pageflip_timeout, 0);
890 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
891 config.use_pixman_shadow = use_shadow;
893 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
898 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
900 weston_log("Cannot use drm output api.\n");
905 load_remoting_plugin(ivi, ivi->config);
906 load_waltham_plugin(ivi, ivi->config);
909 free(config.gbm_format);
910 free(config.seat_id);
915 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
916 bool *use_pixman, bool *fullscreen, int *output_count)
918 struct weston_config_section *section;
922 const struct weston_option options[] = {
923 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
924 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
925 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
926 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
927 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
928 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
931 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
932 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
935 parse_options(options, ARRAY_LENGTH(options), argc, argv);
936 *use_pixman = pixman;
941 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
942 const char *match_prefix, const char *name_prefix)
944 struct weston_config_section *section = NULL;
945 const char *section_name;
946 char *default_output = NULL;
948 size_t match_len = strlen(match_prefix);
950 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
953 if (i >= output_count)
956 if (strcmp(section_name, "output") != 0)
959 weston_config_section_get_string(section, "name", &output_name, NULL);
960 if (output_name == NULL)
962 if (strncmp(output_name, match_prefix, match_len) != 0) {
967 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
976 for (; i < output_count; ++i) {
977 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
980 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
981 free(default_output);
985 free(default_output);
992 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
994 struct weston_wayland_backend_config config = {
996 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
997 .struct_size = sizeof config,
1000 struct weston_config_section *section;
1005 const struct weston_option options[] = {
1006 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1007 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1010 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1011 &config.fullscreen, &output_count);
1013 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1014 config.sprawl = sprawl;
1016 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1017 weston_config_section_get_string(section, "cursor-theme",
1018 &config.cursor_theme, NULL);
1019 weston_config_section_get_int(section, "cursor-size",
1020 &config.cursor_size, 32);
1022 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1025 free(config.cursor_theme);
1026 free(config.display_name);
1031 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1034 * We will just assume if load_backend() finished cleanly and
1035 * windowed_output_api is not present that wayland backend is started
1036 * with --sprawl or runs on fullscreen-shell. In this case, all values
1037 * are hardcoded, so nothing can be configured; simply create and
1040 if (ivi->window_api == NULL)
1043 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1046 #ifdef HAVE_BACKEND_X11
1048 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1050 struct weston_x11_backend_config config = {
1052 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1053 .struct_size = sizeof config,
1060 const struct weston_option options[] = {
1061 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
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.no_input = no_input;
1070 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1076 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1077 if (!ivi->window_api) {
1078 weston_log("Cannot use weston_windowed_output_api.\n");
1082 return windowed_create_outputs(ivi, output_count, "X", "screen");
1086 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1092 #ifdef HAVE_BACKEND_HEADLESS
1094 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1096 struct weston_headless_backend_config config = {};
1104 struct weston_compositor *c = ivi->compositor;
1106 const struct weston_option options[] = {
1107 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1108 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &use_gl },
1111 windowed_parse_common_options(ivi, argc, argv, &use_pixman,
1112 &fullscreen, &output_count);
1114 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1115 config.use_pixman = use_pixman;
1116 config.use_gl = use_gl;
1118 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1119 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1121 /* load the actual headless-backend and configure it */
1122 ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1127 ivi->window_api = weston_windowed_output_get_api(c);
1128 if (!ivi->window_api) {
1129 weston_log("Cannot use weston_windowed_output_api.\n");
1133 if (ivi->window_api->create_head(c, "headless") < 0) {
1134 weston_log("Cannot create headless back-end\n");
1142 load_headless_backend(struct ivi_compositor *ivi, int *argc, char **argv)
1149 load_backend(struct ivi_compositor *ivi, const char *backend,
1150 int *argc, char *argv[])
1152 if (strcmp(backend, "drm-backend.so") == 0) {
1153 return load_drm_backend(ivi, argc, argv);
1154 } else if (strcmp(backend, "wayland-backend.so") == 0) {
1155 return load_wayland_backend(ivi, argc, argv);
1156 } else if (strcmp(backend, "x11-backend.so") == 0) {
1157 return load_x11_backend(ivi, argc, argv);
1158 } else if (strcmp(backend, "headless-backend.so") == 0) {
1159 return load_headless_backend(ivi, argc, argv);
1162 weston_log("fatal: unknown backend '%s'.\n", backend);
1167 load_modules(struct ivi_compositor *ivi, const char *modules,
1168 int *argc, char *argv[], bool *xwayland)
1170 const char *p, *end;
1172 int (*module_init)(struct weston_compositor *wc, int argc, char *argv[]);
1174 if (modules == NULL)
1179 end = strchrnul(p, ',');
1180 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
1182 if (strstr(buffer, "xwayland.so")) {
1183 weston_log("Xwayland plug-in not supported!\n");
1190 if (strstr(buffer, "systemd-notify.so")) {
1191 weston_log("systemd-notify plug-in already loaded!\n");
1198 module_init = weston_load_module(buffer, "wet_module_init");
1202 if (module_init(ivi->compositor, *argc, argv) < 0)
1215 choose_default_backend(void)
1217 char *backend = NULL;
1219 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1220 backend = strdup("wayland-backend.so");
1221 else if (getenv("DISPLAY"))
1222 backend = strdup("x11-backend.so");
1224 backend = strdup("drm-backend.so");
1230 compositor_init_config(struct weston_compositor *compositor,
1231 struct weston_config *config)
1233 struct xkb_rule_names xkb_names;
1234 struct weston_config_section *section;
1239 /* agl-compositor.ini [keyboard] */
1240 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1241 weston_config_section_get_string(section, "keymap_rules",
1242 (char **) &xkb_names.rules, NULL);
1243 weston_config_section_get_string(section, "keymap_model",
1244 (char **) &xkb_names.model, NULL);
1245 weston_config_section_get_string(section, "keymap_layout",
1246 (char **) &xkb_names.layout, NULL);
1247 weston_config_section_get_string(section, "keymap_variant",
1248 (char **) &xkb_names.variant, NULL);
1249 weston_config_section_get_string(section, "keymap_options",
1250 (char **) &xkb_names.options, NULL);
1252 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1255 weston_config_section_get_int(section, "repeat-rate",
1256 &compositor->kb_repeat_rate, 40);
1257 weston_config_section_get_int(section, "repeat-delay",
1258 &compositor->kb_repeat_delay, 400);
1260 weston_config_section_get_bool(section, "vt-switching",
1261 &vt_switching, false);
1262 compositor->vt_switching = vt_switching;
1264 /* agl-compositor.ini [core] */
1265 section = weston_config_get_section(config, "core", NULL, NULL);
1267 weston_config_section_get_bool(section, "require-input", &require_input, true);
1268 compositor->require_input = require_input;
1270 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1271 compositor->repaint_msec);
1272 if (repaint_msec < -10 || repaint_msec > 1000) {
1273 weston_log("Invalid repaint_window value in config: %d\n",
1276 compositor->repaint_msec = repaint_msec;
1278 weston_log("Output repaint window is %d ms maximum.\n",
1279 compositor->repaint_msec);
1284 struct ivi_surface *
1285 to_ivi_surface(struct weston_surface *surface)
1287 struct weston_desktop_surface *dsurface;
1289 dsurface = weston_surface_get_desktop_surface(surface);
1293 return weston_desktop_surface_get_user_data(dsurface);
1297 activate_binding(struct weston_seat *seat,
1298 struct weston_view *focus_view)
1300 struct weston_surface *focus = focus_view->surface;
1301 struct weston_surface *main_surface =
1302 weston_surface_get_main_surface(focus);
1303 struct ivi_surface *surface;
1305 surface = to_ivi_surface(main_surface);
1309 weston_seat_set_keyboard_focus(seat, focus);
1313 click_to_activate_binding(struct weston_pointer *pointer,
1314 const struct timespec *time,
1315 uint32_t button, void *data)
1317 if (pointer->grab != &pointer->default_grab)
1319 if (pointer->focus == NULL)
1322 activate_binding(pointer->seat, pointer->focus);
1326 touch_to_activate_binding(struct weston_touch *touch,
1327 const struct timespec *time,
1330 if (touch->grab != &touch->default_grab)
1332 if (touch->focus == NULL)
1335 activate_binding(touch->seat, touch->focus);
1339 add_bindings(struct weston_compositor *compositor)
1341 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1342 click_to_activate_binding,
1344 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1345 click_to_activate_binding,
1347 weston_compositor_add_touch_binding(compositor, 0,
1348 touch_to_activate_binding,
1353 create_listening_socket(struct wl_display *display, const char *socket_name)
1356 if (wl_display_add_socket(display, socket_name)) {
1357 weston_log("fatal: failed to add socket: %s\n",
1362 socket_name = wl_display_add_socket_auto(display);
1364 weston_log("fatal: failed to add socket: %s\n",
1370 setenv("WAYLAND_DISPLAY", socket_name, 1);
1376 global_filter(const struct wl_client *client, const struct wl_global *global,
1383 load_config(struct weston_config **config, bool no_config,
1384 const char *config_file)
1386 const char *file = "agl-compositor.ini";
1387 const char *full_path;
1393 *config = weston_config_parse(file);
1396 full_path = weston_config_get_full_path(*config);
1398 weston_log("Using config file '%s'.\n", full_path);
1399 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1404 if (config_file && !no_config) {
1405 weston_log("fatal: error opening or reading config file '%s'.\n",
1411 weston_log("Starting with no config file.\n");
1412 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1417 static FILE *logfile;
1420 log_timestamp(char *buf, size_t len)
1423 struct tm *brokendown_time;
1427 gettimeofday(&tv, NULL);
1429 brokendown_time = localtime(&tv.tv_sec);
1430 if (brokendown_time == NULL) {
1431 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1435 memset(datestr, 0, sizeof(datestr));
1436 if (brokendown_time->tm_mday != cached_tm_mday) {
1437 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1439 cached_tm_mday = brokendown_time->tm_mday;
1442 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1443 /* if datestr is empty it prints only timestr*/
1444 snprintf(buf, len, "%s[%s.%03li]", datestr,
1445 timestr, (tv.tv_usec / 1000));
1451 custom_handler(const char *fmt, va_list arg)
1455 weston_log_scope_printf(log_scope, "%s libwayland: ",
1456 log_timestamp(timestr, sizeof(timestr)));
1457 weston_log_scope_vprintf(log_scope, fmt, arg);
1461 log_file_open(const char *filename)
1463 wl_log_set_handler_server(custom_handler);
1466 logfile = fopen(filename, "a");
1471 os_fd_set_cloexec(fileno(logfile));
1472 setvbuf(logfile, NULL, _IOLBF, 256);
1477 log_file_close(void)
1479 if (logfile && logfile != stderr)
1485 vlog(const char *fmt, va_list ap)
1487 const char *oom = "Out of memory";
1492 if (weston_log_scope_is_enabled(log_scope)) {
1494 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1495 len_va = vasprintf(&str, fmt, ap);
1497 len = weston_log_scope_printf(log_scope, "%s %s",
1498 xlog_timestamp, str);
1501 len = weston_log_scope_printf(log_scope, "%s %s",
1502 xlog_timestamp, oom);
1511 vlog_continue(const char *fmt, va_list ap)
1513 return weston_log_scope_vprintf(log_scope, fmt, ap);
1517 on_term_signal(int signo, void *data)
1519 struct wl_display *display = data;
1521 weston_log("caught signal %d\n", signo);
1522 wl_display_terminate(display);
1528 handle_exit(struct weston_compositor *compositor)
1530 wl_display_terminate(compositor->wl_display);
1534 usage(int error_code)
1536 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1538 "Usage: agl-compositor [OPTIONS]\n"
1540 "This is " PACKAGE_STRING ", the reference compositor for\n"
1541 "Automotive Grade Linux. " PACKAGE_STRING " supports multiple "
1542 "backends,\nand depending on which backend is in use different "
1543 "options will be accepted.\n"
1547 " --version\t\tPrint agl-compositor version\n"
1548 " -B, --backend=MODULE\tBackend module, one of\n"
1549 "\t\t\t\tdrm-backend.so\n"
1550 "\t\t\t\twayland-backend.so\n"
1551 "\t\t\t\tx11-backend.so\n"
1552 "\t\t\t\theadless-backend.so\n"
1553 " -S, --socket=NAME\tName of socket to listen on\n"
1554 " --log=FILE\t\tLog to the given file\n"
1555 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1556 " --no-config\t\tDo not read agl-compositor.ini\n"
1557 " --debug\t\tEnable debug extension(s)\n"
1558 " -h, --help\t\tThis help message\n"
1564 copy_command_line(int argc, char * const argv[])
1571 fp = open_memstream(&str, &size);
1575 fprintf(fp, "%s", argv[0]);
1576 for (i = 1; i < argc; i++)
1577 fprintf(fp, " %s", argv[i]);
1584 int wet_main(int argc, char *argv[])
1586 struct ivi_compositor ivi = { 0 };
1588 struct wl_display *display = NULL;
1589 struct wl_event_loop *loop;
1590 struct wl_event_source *signals[3] = { 0 };
1591 struct weston_config_section *section;
1592 /* Command line options */
1593 char *backend = NULL;
1594 char *socket_name = NULL;
1596 char *modules = NULL;
1597 char *option_modules = NULL;
1602 char *config_file = NULL;
1603 struct weston_log_context *log_ctx = NULL;
1604 struct weston_log_subscriber *logger;
1605 int ret = EXIT_FAILURE;
1606 bool xwayland = false;
1608 const struct weston_option core_options[] = {
1609 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1610 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1611 { WESTON_OPTION_STRING, "log", 0, &log },
1612 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1613 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1614 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1615 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1616 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1617 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1620 wl_list_init(&ivi.outputs);
1621 wl_list_init(&ivi.surfaces);
1622 wl_list_init(&ivi.pending_surfaces);
1623 wl_list_init(&ivi.popup_pending_apps);
1624 wl_list_init(&ivi.fullscreen_pending_apps);
1625 wl_list_init(&ivi.split_pending_apps);
1626 wl_list_init(&ivi.remote_pending_apps);
1627 wl_list_init(&ivi.desktop_clients);
1629 /* Prevent any clients we spawn getting our stdin */
1630 os_fd_set_cloexec(STDIN_FILENO);
1632 cmdline = copy_command_line(argc, argv);
1633 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1636 usage(EXIT_SUCCESS);
1639 printf(PACKAGE_STRING "\n");
1640 return EXIT_SUCCESS;
1643 log_ctx = weston_log_ctx_compositor_create();
1645 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1649 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1650 "agl-compositor log\n",
1654 weston_log_set_handler(vlog, vlog_continue);
1656 logger = weston_log_subscriber_create_log(logfile);
1657 weston_log_subscribe(log_ctx, logger, "log");
1659 weston_log("Command line: %s\n", cmdline);
1662 if (load_config(&ivi.config, no_config, config_file) < 0)
1664 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1666 weston_config_section_get_string(section, "backend", &backend,
1669 backend = choose_default_backend();
1672 weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
1673 weston_config_section_get_bool(section, "activate-by-default", &ivi.activate_by_default, true);
1675 display = wl_display_create();
1676 loop = wl_display_get_event_loop(display);
1678 wl_display_set_global_filter(display,
1679 global_filter, &ivi);
1681 /* Register signal handlers so we shut down cleanly */
1683 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1685 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1687 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1690 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1694 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1695 if (!ivi.compositor) {
1696 weston_log("fatal: failed to create compositor.\n");
1700 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1701 goto error_compositor;
1703 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1704 weston_log("fatal: failed to create compositor backend.\n");
1705 goto error_compositor;
1708 ivi.heads_changed.notify = heads_changed;
1709 weston_compositor_add_heads_changed_listener(ivi.compositor,
1710 &ivi.heads_changed);
1712 if (ivi_desktop_init(&ivi) < 0)
1713 goto error_compositor;
1715 ivi_seat_init(&ivi);
1717 /* load additional modules */
1718 weston_config_section_get_string(section, "modules", &modules, "");
1719 if (load_modules(&ivi, modules, &argc, argv, &xwayland) < 0)
1720 goto error_compositor;
1722 if (load_modules(&ivi, option_modules, &argc, argv, &xwayland) < 0)
1723 goto error_compositor;
1725 if (ivi_policy_init(&ivi) < 0)
1726 goto error_compositor;
1728 if (ivi_shell_init(&ivi) < 0)
1729 goto error_compositor;
1731 add_bindings(ivi.compositor);
1733 weston_compositor_flush_heads_changed(ivi.compositor);
1735 if (ivi.remoting_api)
1736 ivi_enable_remote_outputs(&ivi);
1738 if (ivi.waltham_transmitter_api)
1739 ivi_enable_waltham_outputs(&ivi);
1741 if (create_listening_socket(display, socket_name) < 0)
1742 goto error_compositor;
1744 ivi_shell_init_black_fs(&ivi);
1746 ivi.compositor->exit = handle_exit;
1748 weston_compositor_wake(ivi.compositor);
1750 ivi_shell_create_global(&ivi);
1751 ivi_launch_shell_client(&ivi);
1753 ivi_screenshooter_create(&ivi);
1754 ivi_agl_systemd_notify(&ivi);
1756 wl_display_run(display);
1758 ret = ivi.compositor->exit_code;
1760 wl_display_destroy_clients(display);
1763 weston_compositor_tear_down(ivi.compositor);
1765 weston_compositor_log_scope_destroy(log_scope);
1768 weston_log_ctx_compositor_destroy(ivi.compositor);
1769 weston_compositor_destroy(ivi.compositor);
1771 weston_log_subscriber_destroy_log(logger);
1773 ivi_policy_destroy(ivi.policy);
1776 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1778 wl_event_source_remove(signals[i]);
1780 wl_display_destroy(display);
1784 weston_config_destroy(ivi.config);