2 * Copyright © 2012-2019 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_X11
43 #include <libweston/backend-x11.h>
45 #include <libweston/libweston.h>
46 #include <libweston/windowed-output-api.h>
47 #include <libweston/config-parser.h>
48 #include <libweston/weston-log.h>
49 #include <weston/weston.h>
51 #include "shared/os-compatibility.h"
52 #include "shared/helpers.h"
54 #include "agl-shell-server-protocol.h"
61 #include <waltham-transmitter/transmitter_api.h>
64 static int cached_tm_mday = -1;
65 static struct weston_log_scope *log_scope;
67 struct ivi_compositor *
68 to_ivi_compositor(struct weston_compositor *ec)
70 return weston_compositor_get_user_data(ec);
74 handle_output_destroy(struct wl_listener *listener, void *data)
76 struct ivi_output *output;
78 output = wl_container_of(listener, output, output_destroy);
79 assert(output->output == data);
81 if (output->fullscreen_view.fs->view) {
82 weston_surface_destroy(output->fullscreen_view.fs->view->surface);
83 output->fullscreen_view.fs->view = NULL;
86 output->output = NULL;
87 wl_list_remove(&output->output_destroy.link);
91 to_ivi_output(struct weston_output *o)
93 struct wl_listener *listener;
94 struct ivi_output *output;
96 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
97 output = wl_container_of(listener, output, output_destroy);
103 ivi_output_configure_app_id(struct ivi_output *ivi_output)
105 if (ivi_output->config) {
106 if (ivi_output->app_id != NULL)
109 weston_config_section_get_string(ivi_output->config,
114 if (ivi_output->app_id == NULL)
117 weston_log("Will place app_id %s on output %s\n",
118 ivi_output->app_id, ivi_output->name);
122 static struct ivi_output *
123 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
124 struct weston_config_section *config)
126 struct ivi_output *output = NULL;
127 wl_list_for_each(output, &ivi->outputs, link) {
128 if (strcmp(output->name, name) == 0) {
134 output = zalloc(sizeof *output);
142 output->config = config;
144 output->output = weston_compositor_create_output(ivi->compositor, name);
145 if (!output->output) {
151 output->output_destroy.notify = handle_output_destroy;
152 weston_output_add_destroy_listener(output->output,
153 &output->output_destroy);
155 wl_list_insert(&ivi->outputs, &output->link);
156 ivi_output_configure_app_id(output);
161 count_heads(struct weston_output *output)
163 struct weston_head *iter = NULL;
166 while ((iter = weston_output_iterate_heads(output, iter)))
173 handle_head_destroy(struct wl_listener *listener, void *data)
175 struct weston_head *head = data;
176 struct weston_output *output;
178 wl_list_remove(&listener->link);
181 output = weston_head_get_output(head);
183 /* On shutdown path, the output might be already gone. */
187 /* We're the last head */
188 if (count_heads(output) <= 1)
189 weston_output_destroy(output);
193 add_head_destroyed_listener(struct weston_head *head)
195 /* We already have a destroy listener */
196 if (weston_head_get_destroy_listener(head, handle_head_destroy))
199 struct wl_listener *listener = zalloc(sizeof *listener);
203 listener->notify = handle_head_destroy;
204 weston_head_add_destroy_listener(head, listener);
208 drm_configure_output(struct ivi_output *output)
210 struct ivi_compositor *ivi = output->ivi;
211 struct weston_config_section *section = output->config;
212 enum weston_drm_backend_output_mode mode =
213 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
214 char *modeline = NULL;
215 char *gbm_format = NULL;
220 weston_config_section_get_string(section, "mode", &m, "preferred");
222 /* This should have been handled earlier */
223 assert(strcmp(m, "off") != 0);
225 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
226 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
227 } else if (strcmp(m, "preferred") != 0) {
233 weston_config_section_get_string(section, "gbm-format",
236 weston_config_section_get_string(section, "seat", &seat, "");
239 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
240 weston_log("Cannot configure output using weston_drm_output_api.\n");
246 ivi->drm_api->set_gbm_format(output->output, gbm_format);
249 ivi->drm_api->set_seat(output->output, seat);
255 #define WINDOWED_DEFAULT_WIDTH 1024
256 #define WINDOWED_DEFAULT_HEIGHT 768
259 windowed_configure_output(struct ivi_output *output)
261 struct ivi_compositor *ivi = output->ivi;
262 struct weston_config_section *section = output->config;
263 int width = WINDOWED_DEFAULT_WIDTH;
264 int height = WINDOWED_DEFAULT_HEIGHT;
269 weston_config_section_get_string(section, "mode", &mode, NULL);
270 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
271 weston_log("Invalid mode for output %s. Using defaults.\n",
273 width = WINDOWED_DEFAULT_WIDTH;
274 height = WINDOWED_DEFAULT_HEIGHT;
279 if (ivi->cmdline.width)
280 width = ivi->cmdline.width;
281 if (ivi->cmdline.height)
282 height = ivi->cmdline.height;
283 if (ivi->cmdline.scale)
284 weston_output_set_scale(output->output, ivi->cmdline.scale);
286 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
287 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
296 parse_transform(const char *transform, uint32_t *out)
298 static const struct { const char *name; uint32_t token; } transforms[] = {
299 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
300 { "90", WL_OUTPUT_TRANSFORM_90 },
301 { "180", WL_OUTPUT_TRANSFORM_180 },
302 { "270", WL_OUTPUT_TRANSFORM_270 },
303 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
304 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
305 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
306 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
309 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
310 if (strcmp(transforms[i].name, transform) == 0) {
311 *out = transforms[i].token;
315 *out = WL_OUTPUT_TRANSFORM_NORMAL;
320 configure_output(struct ivi_output *output)
322 struct ivi_compositor *ivi = output->ivi;
323 struct weston_config_section *section = output->config;
325 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
328 * This can happen with the wayland backend with 'sprawl'. The config
329 * is hard-coded, so we don't need to do anything.
331 if (!ivi->drm_api && !ivi->window_api)
337 weston_config_section_get_int(section, "scale", &scale, 1);
338 weston_config_section_get_string(section, "transform", &t, "normal");
339 if (parse_transform(t, &transform) < 0)
340 weston_log("Invalid transform \"%s\" for output %s\n",
345 weston_output_set_scale(output->output, scale);
346 weston_output_set_transform(output->output, transform);
349 return drm_configure_output(output);
351 return windowed_configure_output(output);
355 * Reorgainizes the output's add array into two sections.
356 * add[0..ret-1] are the heads that failed to get attached.
357 * add[ret..add_len] are the heads that were successfully attached.
359 * The order between elements in each section is stable.
362 try_attach_heads(struct ivi_output *output)
366 for (size_t i = 0; i < output->add_len; ++i) {
367 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
368 struct weston_head *tmp = output->add[i];
369 memmove(&output->add[fail_len + 1], output->add[fail_len],
370 sizeof output->add[0] * (i - fail_len));
371 output->add[fail_len++] = tmp;
379 * Like try_attach_heads, this reorganizes the output's add array into a failed
380 * and successful section.
381 * i is the number of heads that already failed the previous step.
384 try_enable_output(struct ivi_output *output, size_t i)
386 for (; i < output->add_len; ++i) {
387 struct weston_head *head;
389 if (weston_output_enable(output->output) == 0)
392 head = output->add[output->add_len - 1];
393 memmove(&output->add[i + 1], &output->add[i],
394 sizeof output->add[0] * (output->add_len - i));
395 output->add[i] = head;
397 weston_head_detach(head);
404 try_attach_enable_heads(struct ivi_output *output)
407 assert(!output->output->enabled);
409 fail_len = try_attach_heads(output);
411 if (configure_output(output) < 0)
414 fail_len = try_enable_output(output, fail_len);
416 /* All heads failed to be attached */
417 if (fail_len == output->add_len)
420 /* For each successful head attached */
421 for (size_t i = fail_len; i < output->add_len; ++i)
422 add_head_destroyed_listener(output->add[i]);
424 output->add_len = fail_len;
429 process_output(struct ivi_output *output)
431 if (output->output->enabled) {
432 output->add_len = try_attach_heads(output);
433 return output->add_len == 0 ? 0 : -1;
436 return try_attach_enable_heads(output);
440 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
442 struct weston_output *output;
443 struct ivi_output *ivi_output;
444 struct wl_listener *listener;
446 output = weston_head_get_output(head);
449 listener = weston_output_get_destroy_listener(output,
450 handle_output_destroy);
453 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
454 assert(ivi_output->output == output);
456 weston_head_detach(head);
457 if (count_heads(ivi_output->output) == 0) {
458 weston_output_disable(ivi_output->output);
462 static struct weston_config_section *
463 find_controlling_output_config(struct weston_config *config,
466 struct weston_config_section *section;
470 same_as = strdup(name);
472 section = weston_config_get_section(config, "output",
474 if (!section && depth > 0)
475 weston_log("Configuration error: output section reffered"
476 "to by same-as=%s not found.\n", same_as);
483 weston_log("Configuration error: same-as nested too "
484 "deep for output '%s'.\n", name);
488 weston_config_section_get_string(section, "same-as",
496 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
498 const char *name = weston_head_get_name(head);
499 struct weston_config_section *section;
500 struct ivi_output *output;
501 char *output_name = NULL;
503 section = find_controlling_output_config(ivi->config, name);
507 weston_config_section_get_string(section, "mode", &mode, NULL);
508 if (mode && strcmp(mode, "off") == 0) {
514 weston_config_section_get_string(section, "name",
517 output_name = strdup(name);
523 output = ivi_ensure_output(ivi, output_name, section);
527 if (output->add_len >= ARRAY_LENGTH(output->add))
530 output->add[output->add_len++] = head;
534 heads_changed(struct wl_listener *listener, void *arg)
536 struct weston_compositor *compositor = arg;
537 struct weston_head *head = NULL;
538 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
539 struct ivi_output *output;
541 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
542 bool connected = weston_head_is_connected(head);
543 bool enabled = weston_head_is_enabled(head);
544 bool changed = weston_head_is_device_changed(head);
545 bool non_desktop = weston_head_is_non_desktop(head);
547 if (connected && !enabled && !non_desktop)
548 head_prepare_enable(ivi, head);
549 else if (!connected && enabled)
550 head_disable(ivi, head);
551 else if (enabled && changed)
552 weston_log("Detected a monitor change on head '%s', "
553 "not bothering to do anything about it.\n",
554 weston_head_get_name(head));
556 weston_head_reset_device_changed(head);
559 wl_list_for_each(output, &ivi->outputs, link) {
560 if (output->add_len == 0)
563 if (process_output(output) < 0) {
565 ivi->init_failed = true;
572 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
574 struct weston_compositor *compositor = ivi->compositor;
575 int (*module_init)(struct weston_compositor *wc);
577 module_init = weston_load_module("waltham-transmitter.so",
582 if (module_init(compositor) < 0)
585 ivi->waltham_transmitter_api = weston_get_transmitter_api(compositor);
586 if (!ivi->waltham_transmitter_api) {
587 weston_log("Failed to load waltham-transmitter plugin.\n");
591 weston_log("waltham-transmitter plug-in loaded\n");
596 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
604 drm_backend_remoted_output_configure(struct weston_output *output,
605 struct weston_config_section *section,
607 const struct weston_remoting_api *api)
609 char *gbm_format = NULL;
612 char *pipeline = NULL;
615 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
618 ret = api->set_mode(output, modeline);
620 weston_log("Cannot configure an output \"%s\" using "
621 "weston_remoting_api. Invalid mode\n",
626 weston_config_section_get_int(section, "scale", &scale, 1);
627 weston_output_set_scale(output, scale);
629 weston_config_section_get_string(section, "transform", &trans, "normal");
630 if (parse_transform(trans, &transform) < 0) {
631 weston_log("Invalid transform \"%s\" for output %s\n",
632 trans, output->name);
634 weston_output_set_transform(output, transform);
636 weston_config_section_get_string(section, "gbm-format",
638 api->set_gbm_format(output, gbm_format);
641 weston_config_section_get_string(section, "seat", &seat, "");
643 api->set_seat(output, seat);
646 weston_config_section_get_string(section, "gst-pipeline", &pipeline,
649 api->set_gst_pipeline(output, pipeline);
654 weston_config_section_get_string(section, "host", &host, NULL);
655 weston_config_section_get_int(section, "port", &port, 0);
656 if (!host || port <= 0 || 65533 < port) {
657 weston_log("Cannot configure an output \"%s\". "
658 "Need to specify gst-pipeline or "
659 "host and port (1-65533).\n", output->name);
661 api->set_host(output, host);
663 api->set_port(output, port);
670 remote_output_init(struct ivi_output *ivi_output,
671 struct weston_compositor *compositor,
672 struct weston_config_section *section,
673 const struct weston_remoting_api *api)
675 char *output_name, *modeline = NULL;
678 weston_config_section_get_string(section, "name", &output_name, NULL);
682 weston_config_section_get_string(section, "mode", &modeline, "off");
683 if (strcmp(modeline, "off") == 0)
686 ivi_output->output = api->create_output(compositor, output_name);
687 if (!ivi_output->output) {
688 weston_log("Cannot create remoted output \"%s\".\n",
693 ret = drm_backend_remoted_output_configure(ivi_output->output, section,
696 weston_log("Cannot configure remoted output \"%s\".\n",
701 if (weston_output_enable(ivi_output->output) < 0) {
702 weston_log("Enabling remoted output \"%s\" failed.\n",
709 weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
716 if (ivi_output->output)
717 weston_output_destroy(ivi_output->output);
723 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
725 struct weston_config_section *remote_section = NULL;
726 const char *section_name;
727 struct weston_config *config = ivi->config;
729 while (weston_config_next_section(config, &remote_section, §ion_name)) {
730 if (strcmp(section_name, "remote-output"))
733 struct ivi_output *ivi_output = NULL;
734 bool output_found = false;
737 weston_config_section_get_string(remote_section,
738 "name", &_name, NULL);
739 wl_list_for_each(ivi_output, &ivi->outputs, link) {
740 if (!strcmp(ivi_output->name, _name)) {
751 ivi_output = zalloc(sizeof(*ivi_output));
753 ivi_output->ivi = ivi;
754 ivi_output->name = _name;
755 ivi_output->config = remote_section;
756 ivi_output->type = OUTPUT_REMOTE;
758 if (remote_output_init(ivi_output, ivi->compositor,
759 remote_section, ivi->remoting_api)) {
760 free(ivi_output->name);
765 ivi_output->output_destroy.notify = handle_output_destroy;
766 weston_output_add_destroy_listener(ivi_output->output,
767 &ivi_output->output_destroy);
769 wl_list_insert(&ivi->outputs, &ivi_output->link);
770 ivi_output_configure_app_id(ivi_output);
775 ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
777 struct weston_config_section *transmitter_section = NULL;
778 const char *sect_name;
779 struct weston_config *config = ivi->config;
781 while (weston_config_next_section(config, &transmitter_section, §_name)) {
782 if (strcmp(sect_name, "transmitter-output"))
785 struct ivi_output *ivi_output = NULL;
786 bool output_found = false;
789 weston_config_section_get_string(transmitter_section,
790 "name", &_name, NULL);
791 wl_list_for_each(ivi_output, &ivi->outputs, link) {
792 if (!strcmp(ivi_output->name, _name)) {
803 ivi_output = zalloc(sizeof(*ivi_output));
805 ivi_output->ivi = ivi;
806 ivi_output->name = _name;
807 ivi_output->config = transmitter_section;
809 if (remote_output_init(ivi_output, ivi->compositor,
810 transmitter_section, ivi->remoting_api)) {
811 free(ivi_output->name);
816 ivi_output->type = OUTPUT_WALTHAM;
817 ivi_output->output_destroy.notify = handle_output_destroy;
818 weston_output_add_destroy_listener(ivi_output->output,
819 &ivi_output->output_destroy);
821 wl_list_insert(&ivi->outputs, &ivi_output->link);
822 ivi_output_configure_app_id(ivi_output);
827 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
829 struct weston_compositor *compositor = ivi->compositor;
830 int (*module_init)(struct weston_compositor *wc);
832 module_init = weston_load_module("remoting-plugin.so",
833 "weston_module_init");
837 if (module_init(compositor) < 0)
840 ivi->remoting_api = weston_remoting_get_api(compositor);
841 if (!ivi->remoting_api)
847 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
854 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
856 struct weston_drm_backend_config config = {
858 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
859 .struct_size = sizeof config,
862 struct weston_config_section *section;
863 int use_current_mode = 0;
868 const struct weston_option options[] = {
869 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
870 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
871 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
872 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
873 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
876 parse_options(options, ARRAY_LENGTH(options), argc, argv);
877 config.use_pixman = use_pixman;
878 ivi->cmdline.use_current_mode = use_current_mode;
880 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
881 weston_config_section_get_string(section, "gbm-format",
882 &config.gbm_format, NULL);
883 weston_config_section_get_uint(section, "pageflip-timeout",
884 &config.pageflip_timeout, 0);
885 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
886 config.use_pixman_shadow = use_shadow;
888 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
893 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
895 weston_log("Cannot use drm output api.\n");
900 load_remoting_plugin(ivi, ivi->config);
901 load_waltham_plugin(ivi, ivi->config);
904 free(config.gbm_format);
905 free(config.seat_id);
910 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
911 bool *use_pixman, bool *fullscreen, int *output_count)
913 struct weston_config_section *section;
917 const struct weston_option options[] = {
918 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
919 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
920 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
921 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
922 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
923 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
926 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
927 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
930 parse_options(options, ARRAY_LENGTH(options), argc, argv);
931 *use_pixman = pixman;
936 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
937 const char *match_prefix, const char *name_prefix)
939 struct weston_config_section *section = NULL;
940 const char *section_name;
941 char *default_output = NULL;
943 size_t match_len = strlen(match_prefix);
945 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
948 if (i >= output_count)
951 if (strcmp(section_name, "output") != 0)
954 weston_config_section_get_string(section, "name", &output_name, NULL);
955 if (output_name == NULL)
957 if (strncmp(output_name, match_prefix, match_len) != 0) {
962 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
971 for (; i < output_count; ++i) {
972 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
975 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
976 free(default_output);
980 free(default_output);
987 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
989 struct weston_wayland_backend_config config = {
991 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
992 .struct_size = sizeof config,
995 struct weston_config_section *section;
1000 const struct weston_option options[] = {
1001 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1002 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1005 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1006 &config.fullscreen, &output_count);
1008 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1009 config.sprawl = sprawl;
1011 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1012 weston_config_section_get_string(section, "cursor-theme",
1013 &config.cursor_theme, NULL);
1014 weston_config_section_get_int(section, "cursor-size",
1015 &config.cursor_size, 32);
1017 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1020 free(config.cursor_theme);
1021 free(config.display_name);
1026 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1029 * We will just assume if load_backend() finished cleanly and
1030 * windowed_output_api is not present that wayland backend is started
1031 * with --sprawl or runs on fullscreen-shell. In this case, all values
1032 * are hardcoded, so nothing can be configured; simply create and
1035 if (ivi->window_api == NULL)
1038 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1041 #ifdef HAVE_BACKEND_X11
1043 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1045 struct weston_x11_backend_config config = {
1047 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1048 .struct_size = sizeof config,
1055 const struct weston_option options[] = {
1056 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1059 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1060 &config.fullscreen, &output_count);
1062 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1063 config.no_input = no_input;
1065 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1071 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1072 if (!ivi->window_api) {
1073 weston_log("Cannot use weston_windowed_output_api.\n");
1077 return windowed_create_outputs(ivi, output_count, "X", "screen");
1081 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1088 load_backend(struct ivi_compositor *ivi, const char *backend,
1089 int *argc, char *argv[])
1091 if (strcmp(backend, "drm-backend.so") == 0) {
1092 return load_drm_backend(ivi, argc, argv);
1093 } else if (strcmp(backend, "wayland-backend.so") == 0) {
1094 return load_wayland_backend(ivi, argc, argv);
1095 } else if (strcmp(backend, "x11-backend.so") == 0) {
1096 return load_x11_backend(ivi, argc, argv);
1099 weston_log("fatal: unknown backend '%s'.\n", backend);
1104 choose_default_backend(void)
1106 char *backend = NULL;
1108 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1109 backend = strdup("wayland-backend.so");
1110 else if (getenv("DISPLAY"))
1111 backend = strdup("x11-backend.so");
1113 backend = strdup("drm-backend.so");
1119 compositor_init_config(struct weston_compositor *compositor,
1120 struct weston_config *config)
1122 struct xkb_rule_names xkb_names;
1123 struct weston_config_section *section;
1128 /* agl-compositor.ini [keyboard] */
1129 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1130 weston_config_section_get_string(section, "keymap_rules",
1131 (char **) &xkb_names.rules, NULL);
1132 weston_config_section_get_string(section, "keymap_model",
1133 (char **) &xkb_names.model, NULL);
1134 weston_config_section_get_string(section, "keymap_layout",
1135 (char **) &xkb_names.layout, NULL);
1136 weston_config_section_get_string(section, "keymap_variant",
1137 (char **) &xkb_names.variant, NULL);
1138 weston_config_section_get_string(section, "keymap_options",
1139 (char **) &xkb_names.options, NULL);
1141 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1144 weston_config_section_get_int(section, "repeat-rate",
1145 &compositor->kb_repeat_rate, 40);
1146 weston_config_section_get_int(section, "repeat-delay",
1147 &compositor->kb_repeat_delay, 400);
1149 weston_config_section_get_bool(section, "vt-switching",
1150 &vt_switching, true);
1151 compositor->vt_switching = vt_switching;
1153 /* agl-compositor.ini [core] */
1154 section = weston_config_get_section(config, "core", NULL, NULL);
1156 weston_config_section_get_bool(section, "require-input", &require_input, true);
1157 compositor->require_input = require_input;
1159 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1160 compositor->repaint_msec);
1161 if (repaint_msec < -10 || repaint_msec > 1000) {
1162 weston_log("Invalid repaint_window value in config: %d\n",
1165 compositor->repaint_msec = repaint_msec;
1167 weston_log("Output repaint window is %d ms maximum.\n",
1168 compositor->repaint_msec);
1173 struct ivi_surface *
1174 to_ivi_surface(struct weston_surface *surface)
1176 struct weston_desktop_surface *dsurface;
1178 dsurface = weston_surface_get_desktop_surface(surface);
1182 return weston_desktop_surface_get_user_data(dsurface);
1186 activate_binding(struct weston_seat *seat,
1187 struct weston_view *focus_view)
1189 struct weston_surface *focus = focus_view->surface;
1190 struct weston_surface *main_surface =
1191 weston_surface_get_main_surface(focus);
1192 struct ivi_surface *surface;
1194 surface = to_ivi_surface(main_surface);
1198 weston_seat_set_keyboard_focus(seat, focus);
1202 click_to_activate_binding(struct weston_pointer *pointer,
1203 const struct timespec *time,
1204 uint32_t button, void *data)
1206 if (pointer->grab != &pointer->default_grab)
1208 if (pointer->focus == NULL)
1211 activate_binding(pointer->seat, pointer->focus);
1215 touch_to_activate_binding(struct weston_touch *touch,
1216 const struct timespec *time,
1219 if (touch->grab != &touch->default_grab)
1221 if (touch->focus == NULL)
1224 activate_binding(touch->seat, touch->focus);
1228 add_bindings(struct weston_compositor *compositor)
1230 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1231 click_to_activate_binding,
1233 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1234 click_to_activate_binding,
1236 weston_compositor_add_touch_binding(compositor, 0,
1237 touch_to_activate_binding,
1242 create_listening_socket(struct wl_display *display, const char *socket_name)
1245 if (wl_display_add_socket(display, socket_name)) {
1246 weston_log("fatal: failed to add socket: %s\n",
1251 socket_name = wl_display_add_socket_auto(display);
1253 weston_log("fatal: failed to add socket: %s\n",
1259 setenv("WAYLAND_DISPLAY", socket_name, 1);
1265 global_filter(const struct wl_client *client, const struct wl_global *global,
1272 load_config(struct weston_config **config, bool no_config,
1273 const char *config_file)
1275 const char *file = "agl-compositor.ini";
1276 const char *full_path;
1282 *config = weston_config_parse(file);
1285 full_path = weston_config_get_full_path(*config);
1287 weston_log("Using config file '%s'.\n", full_path);
1288 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1293 if (config_file && !no_config) {
1294 weston_log("fatal: error opening or reading config file '%s'.\n",
1300 weston_log("Starting with no config file.\n");
1301 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1306 static FILE *logfile;
1309 log_timestamp(char *buf, size_t len)
1312 struct tm *brokendown_time;
1316 gettimeofday(&tv, NULL);
1318 brokendown_time = localtime(&tv.tv_sec);
1319 if (brokendown_time == NULL) {
1320 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1324 memset(datestr, 0, sizeof(datestr));
1325 if (brokendown_time->tm_mday != cached_tm_mday) {
1326 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1328 cached_tm_mday = brokendown_time->tm_mday;
1331 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1332 /* if datestr is empty it prints only timestr*/
1333 snprintf(buf, len, "%s[%s.%03li]", datestr,
1334 timestr, (tv.tv_usec / 1000));
1340 custom_handler(const char *fmt, va_list arg)
1344 weston_log_scope_printf(log_scope, "%s libwayland: ",
1345 log_timestamp(timestr, sizeof(timestr)));
1346 weston_log_scope_vprintf(log_scope, fmt, arg);
1350 log_file_open(const char *filename)
1352 wl_log_set_handler_server(custom_handler);
1355 logfile = fopen(filename, "a");
1360 os_fd_set_cloexec(fileno(logfile));
1361 setvbuf(logfile, NULL, _IOLBF, 256);
1366 log_file_close(void)
1368 if (logfile && logfile != stderr)
1374 vlog(const char *fmt, va_list ap)
1376 const char *oom = "Out of memory";
1381 if (weston_log_scope_is_enabled(log_scope)) {
1383 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1384 len_va = vasprintf(&str, fmt, ap);
1386 len = weston_log_scope_printf(log_scope, "%s %s",
1387 xlog_timestamp, str);
1390 len = weston_log_scope_printf(log_scope, "%s %s",
1391 xlog_timestamp, oom);
1400 vlog_continue(const char *fmt, va_list ap)
1402 return weston_log_scope_vprintf(log_scope, fmt, ap);
1406 on_term_signal(int signo, void *data)
1408 struct wl_display *display = data;
1410 weston_log("caught signal %d\n", signo);
1411 wl_display_terminate(display);
1417 handle_exit(struct weston_compositor *compositor)
1419 wl_display_terminate(compositor->wl_display);
1423 usage(int error_code)
1425 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1427 "Usage: agl-compositor [OPTIONS]\n"
1429 "This is " PACKAGE_STRING ", the reference compositor for\n"
1430 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1431 "on which backend is in use different options will be accepted.\n"
1435 " --version\t\tPrint agl-compositor version\n"
1436 " -B, --backend=MODULE\tBackend module, one of\n"
1437 "\t\t\t\tdrm-backend.so\n"
1438 "\t\t\t\twayland-backend.so\n"
1439 "\t\t\t\tx11-backend.so\n"
1440 " -S, --socket=NAME\tName of socket to listen on\n"
1441 " --log=FILE\t\tLog to the given file\n"
1442 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1443 " --no-config\t\tDo not read agl-compositor.ini\n"
1444 " --debug\t\tEnable debug extension(s)\n"
1445 " -h, --help\t\tThis help message\n"
1450 int main(int argc, char *argv[])
1452 struct ivi_compositor ivi = { 0 };
1453 struct wl_display *display = NULL;
1454 struct wl_event_loop *loop;
1455 struct wl_event_source *signals[3] = { 0 };
1456 struct weston_config_section *section;
1457 /* Command line options */
1458 char *backend = NULL;
1459 char *socket_name = NULL;
1465 char *config_file = NULL;
1466 struct weston_log_context *log_ctx = NULL;
1467 struct weston_log_subscriber *logger;
1468 int ret = EXIT_FAILURE;
1470 const struct weston_option core_options[] = {
1471 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1472 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1473 { WESTON_OPTION_STRING, "log", 0, &log },
1474 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1475 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1476 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1477 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1478 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1481 wl_list_init(&ivi.outputs);
1482 wl_list_init(&ivi.surfaces);
1483 wl_list_init(&ivi.pending_surfaces);
1484 wl_list_init(&ivi.popup_pending_apps);
1485 wl_list_init(&ivi.fullscreen_pending_apps);
1486 wl_list_init(&ivi.split_pending_apps);
1487 wl_list_init(&ivi.remote_pending_apps);
1488 wl_list_init(&ivi.desktop_clients);
1490 /* Prevent any clients we spawn getting our stdin */
1491 os_fd_set_cloexec(STDIN_FILENO);
1493 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1496 usage(EXIT_SUCCESS);
1499 printf(PACKAGE_STRING "\n");
1500 return EXIT_SUCCESS;
1503 log_ctx = weston_log_ctx_compositor_create();
1505 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1509 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1510 "agl-compositor log\n",
1514 weston_log_set_handler(vlog, vlog_continue);
1516 logger = weston_log_subscriber_create_log(logfile);
1517 weston_log_subscribe(log_ctx, logger, "log");
1519 if (load_config(&ivi.config, no_config, config_file) < 0)
1521 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1523 weston_config_section_get_string(section, "backend", &backend,
1526 backend = choose_default_backend();
1529 weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
1530 weston_config_section_get_bool(section, "activate-by-default", &ivi.activate_by_default, true);
1532 display = wl_display_create();
1533 loop = wl_display_get_event_loop(display);
1535 wl_display_set_global_filter(display,
1536 global_filter, &ivi);
1538 /* Register signal handlers so we shut down cleanly */
1540 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1542 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1544 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1547 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1551 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1552 if (!ivi.compositor) {
1553 weston_log("fatal: failed to create compositor.\n");
1557 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1558 goto error_compositor;
1560 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1561 weston_log("fatal: failed to create compositor backend.\n");
1562 goto error_compositor;
1565 ivi.heads_changed.notify = heads_changed;
1566 weston_compositor_add_heads_changed_listener(ivi.compositor,
1567 &ivi.heads_changed);
1569 if (ivi_desktop_init(&ivi) < 0)
1570 goto error_compositor;
1572 ivi_seat_init(&ivi);
1574 if (ivi_policy_init(&ivi) < 0)
1575 goto error_compositor;
1577 if (ivi_shell_init(&ivi) < 0)
1578 goto error_compositor;
1580 add_bindings(ivi.compositor);
1582 weston_compositor_flush_heads_changed(ivi.compositor);
1584 if (ivi.remoting_api)
1585 ivi_enable_remote_outputs(&ivi);
1587 if (ivi.waltham_transmitter_api)
1588 ivi_enable_waltham_outputs(&ivi);
1590 if (create_listening_socket(display, socket_name) < 0)
1591 goto error_compositor;
1593 ivi_shell_init_black_fs(&ivi);
1595 ivi.compositor->exit = handle_exit;
1597 weston_compositor_wake(ivi.compositor);
1599 ivi_shell_create_global(&ivi);
1600 ivi_launch_shell_client(&ivi);
1602 ivi_screenshooter_create(&ivi);
1603 ivi_agl_systemd_notify(&ivi);
1605 wl_display_run(display);
1607 ret = ivi.compositor->exit_code;
1609 wl_display_destroy_clients(display);
1612 weston_compositor_tear_down(ivi.compositor);
1614 weston_compositor_log_scope_destroy(log_scope);
1617 weston_log_ctx_compositor_destroy(ivi.compositor);
1618 weston_compositor_destroy(ivi.compositor);
1620 weston_log_subscriber_destroy_log(logger);
1622 ivi_policy_destroy(ivi.policy);
1625 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1627 wl_event_source_remove(signals[i]);
1629 wl_display_destroy(display);
1633 weston_config_destroy(ivi.config);