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 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
777 struct weston_compositor *compositor = ivi->compositor;
778 int (*module_init)(struct weston_compositor *wc);
780 module_init = weston_load_module("remoting-plugin.so",
781 "weston_module_init");
785 if (module_init(compositor) < 0)
788 ivi->remoting_api = weston_remoting_get_api(compositor);
789 if (!ivi->remoting_api)
795 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
802 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
804 struct weston_drm_backend_config config = {
806 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
807 .struct_size = sizeof config,
810 struct weston_config_section *section;
811 int use_current_mode = 0;
816 const struct weston_option options[] = {
817 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
818 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
819 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
820 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
821 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
824 parse_options(options, ARRAY_LENGTH(options), argc, argv);
825 config.use_pixman = use_pixman;
826 ivi->cmdline.use_current_mode = use_current_mode;
828 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
829 weston_config_section_get_string(section, "gbm-format",
830 &config.gbm_format, NULL);
831 weston_config_section_get_uint(section, "pageflip-timeout",
832 &config.pageflip_timeout, 0);
833 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
834 config.use_pixman_shadow = use_shadow;
836 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
841 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
843 weston_log("Cannot use drm output api.\n");
848 load_remoting_plugin(ivi, ivi->config);
849 load_waltham_plugin(ivi, ivi->config);
852 free(config.gbm_format);
853 free(config.seat_id);
858 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
859 bool *use_pixman, bool *fullscreen, int *output_count)
861 struct weston_config_section *section;
865 const struct weston_option options[] = {
866 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
867 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
868 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
869 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
870 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
871 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
874 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
875 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
878 parse_options(options, ARRAY_LENGTH(options), argc, argv);
879 *use_pixman = pixman;
884 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
885 const char *match_prefix, const char *name_prefix)
887 struct weston_config_section *section = NULL;
888 const char *section_name;
889 char *default_output = NULL;
891 size_t match_len = strlen(match_prefix);
893 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
896 if (i >= output_count)
899 if (strcmp(section_name, "output") != 0)
902 weston_config_section_get_string(section, "name", &output_name, NULL);
903 if (output_name == NULL)
905 if (strncmp(output_name, match_prefix, match_len) != 0) {
910 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
919 for (; i < output_count; ++i) {
920 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
923 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
924 free(default_output);
928 free(default_output);
935 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
937 struct weston_wayland_backend_config config = {
939 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
940 .struct_size = sizeof config,
943 struct weston_config_section *section;
948 const struct weston_option options[] = {
949 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
950 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
953 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
954 &config.fullscreen, &output_count);
956 parse_options(options, ARRAY_LENGTH(options), argc, argv);
957 config.sprawl = sprawl;
959 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
960 weston_config_section_get_string(section, "cursor-theme",
961 &config.cursor_theme, NULL);
962 weston_config_section_get_int(section, "cursor-size",
963 &config.cursor_size, 32);
965 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
968 free(config.cursor_theme);
969 free(config.display_name);
974 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
977 * We will just assume if load_backend() finished cleanly and
978 * windowed_output_api is not present that wayland backend is started
979 * with --sprawl or runs on fullscreen-shell. In this case, all values
980 * are hardcoded, so nothing can be configured; simply create and
983 if (ivi->window_api == NULL)
986 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
989 #ifdef HAVE_BACKEND_X11
991 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
993 struct weston_x11_backend_config config = {
995 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
996 .struct_size = sizeof config,
1003 const struct weston_option options[] = {
1004 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1007 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1008 &config.fullscreen, &output_count);
1010 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1011 config.no_input = no_input;
1013 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1019 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1020 if (!ivi->window_api) {
1021 weston_log("Cannot use weston_windowed_output_api.\n");
1025 return windowed_create_outputs(ivi, output_count, "X", "screen");
1029 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1036 load_backend(struct ivi_compositor *ivi, const char *backend,
1037 int *argc, char *argv[])
1039 if (strcmp(backend, "drm-backend.so") == 0) {
1040 return load_drm_backend(ivi, argc, argv);
1041 } else if (strcmp(backend, "wayland-backend.so") == 0) {
1042 return load_wayland_backend(ivi, argc, argv);
1043 } else if (strcmp(backend, "x11-backend.so") == 0) {
1044 return load_x11_backend(ivi, argc, argv);
1047 weston_log("fatal: unknown backend '%s'.\n", backend);
1052 choose_default_backend(void)
1054 char *backend = NULL;
1056 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1057 backend = strdup("wayland-backend.so");
1058 else if (getenv("DISPLAY"))
1059 backend = strdup("x11-backend.so");
1061 backend = strdup("drm-backend.so");
1067 compositor_init_config(struct weston_compositor *compositor,
1068 struct weston_config *config)
1070 struct xkb_rule_names xkb_names;
1071 struct weston_config_section *section;
1076 /* agl-compositor.ini [keyboard] */
1077 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1078 weston_config_section_get_string(section, "keymap_rules",
1079 (char **) &xkb_names.rules, NULL);
1080 weston_config_section_get_string(section, "keymap_model",
1081 (char **) &xkb_names.model, NULL);
1082 weston_config_section_get_string(section, "keymap_layout",
1083 (char **) &xkb_names.layout, NULL);
1084 weston_config_section_get_string(section, "keymap_variant",
1085 (char **) &xkb_names.variant, NULL);
1086 weston_config_section_get_string(section, "keymap_options",
1087 (char **) &xkb_names.options, NULL);
1089 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1092 weston_config_section_get_int(section, "repeat-rate",
1093 &compositor->kb_repeat_rate, 40);
1094 weston_config_section_get_int(section, "repeat-delay",
1095 &compositor->kb_repeat_delay, 400);
1097 weston_config_section_get_bool(section, "vt-switching",
1098 &vt_switching, true);
1099 compositor->vt_switching = vt_switching;
1101 /* agl-compositor.ini [core] */
1102 section = weston_config_get_section(config, "core", NULL, NULL);
1104 weston_config_section_get_bool(section, "require-input", &require_input, true);
1105 compositor->require_input = require_input;
1107 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1108 compositor->repaint_msec);
1109 if (repaint_msec < -10 || repaint_msec > 1000) {
1110 weston_log("Invalid repaint_window value in config: %d\n",
1113 compositor->repaint_msec = repaint_msec;
1115 weston_log("Output repaint window is %d ms maximum.\n",
1116 compositor->repaint_msec);
1121 struct ivi_surface *
1122 to_ivi_surface(struct weston_surface *surface)
1124 struct weston_desktop_surface *dsurface;
1126 dsurface = weston_surface_get_desktop_surface(surface);
1130 return weston_desktop_surface_get_user_data(dsurface);
1134 activate_binding(struct weston_seat *seat,
1135 struct weston_view *focus_view)
1137 struct weston_surface *focus = focus_view->surface;
1138 struct weston_surface *main_surface =
1139 weston_surface_get_main_surface(focus);
1140 struct ivi_surface *surface;
1142 surface = to_ivi_surface(main_surface);
1146 weston_seat_set_keyboard_focus(seat, focus);
1150 click_to_activate_binding(struct weston_pointer *pointer,
1151 const struct timespec *time,
1152 uint32_t button, void *data)
1154 if (pointer->grab != &pointer->default_grab)
1156 if (pointer->focus == NULL)
1159 activate_binding(pointer->seat, pointer->focus);
1163 touch_to_activate_binding(struct weston_touch *touch,
1164 const struct timespec *time,
1167 if (touch->grab != &touch->default_grab)
1169 if (touch->focus == NULL)
1172 activate_binding(touch->seat, touch->focus);
1176 add_bindings(struct weston_compositor *compositor)
1178 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1179 click_to_activate_binding,
1181 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1182 click_to_activate_binding,
1184 weston_compositor_add_touch_binding(compositor, 0,
1185 touch_to_activate_binding,
1190 create_listening_socket(struct wl_display *display, const char *socket_name)
1193 if (wl_display_add_socket(display, socket_name)) {
1194 weston_log("fatal: failed to add socket: %s\n",
1199 socket_name = wl_display_add_socket_auto(display);
1201 weston_log("fatal: failed to add socket: %s\n",
1207 setenv("WAYLAND_DISPLAY", socket_name, 1);
1213 global_filter(const struct wl_client *client, const struct wl_global *global,
1220 load_config(struct weston_config **config, bool no_config,
1221 const char *config_file)
1223 const char *file = "agl-compositor.ini";
1224 const char *full_path;
1230 *config = weston_config_parse(file);
1233 full_path = weston_config_get_full_path(*config);
1235 weston_log("Using config file '%s'.\n", full_path);
1236 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1241 if (config_file && !no_config) {
1242 weston_log("fatal: error opening or reading config file '%s'.\n",
1248 weston_log("Starting with no config file.\n");
1249 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1254 static FILE *logfile;
1257 log_timestamp(char *buf, size_t len)
1260 struct tm *brokendown_time;
1264 gettimeofday(&tv, NULL);
1266 brokendown_time = localtime(&tv.tv_sec);
1267 if (brokendown_time == NULL) {
1268 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1272 memset(datestr, 0, sizeof(datestr));
1273 if (brokendown_time->tm_mday != cached_tm_mday) {
1274 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1276 cached_tm_mday = brokendown_time->tm_mday;
1279 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1280 /* if datestr is empty it prints only timestr*/
1281 snprintf(buf, len, "%s[%s.%03li]", datestr,
1282 timestr, (tv.tv_usec / 1000));
1288 custom_handler(const char *fmt, va_list arg)
1292 weston_log_scope_printf(log_scope, "%s libwayland: ",
1293 log_timestamp(timestr, sizeof(timestr)));
1294 weston_log_scope_vprintf(log_scope, fmt, arg);
1298 log_file_open(const char *filename)
1300 wl_log_set_handler_server(custom_handler);
1303 logfile = fopen(filename, "a");
1308 os_fd_set_cloexec(fileno(logfile));
1309 setvbuf(logfile, NULL, _IOLBF, 256);
1314 log_file_close(void)
1316 if (logfile && logfile != stderr)
1322 vlog(const char *fmt, va_list ap)
1324 const char *oom = "Out of memory";
1329 if (weston_log_scope_is_enabled(log_scope)) {
1331 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1332 len_va = vasprintf(&str, fmt, ap);
1334 len = weston_log_scope_printf(log_scope, "%s %s",
1335 xlog_timestamp, str);
1338 len = weston_log_scope_printf(log_scope, "%s %s",
1339 xlog_timestamp, oom);
1348 vlog_continue(const char *fmt, va_list ap)
1350 return weston_log_scope_vprintf(log_scope, fmt, ap);
1354 on_term_signal(int signo, void *data)
1356 struct wl_display *display = data;
1358 weston_log("caught signal %d\n", signo);
1359 wl_display_terminate(display);
1365 handle_exit(struct weston_compositor *compositor)
1367 wl_display_terminate(compositor->wl_display);
1371 usage(int error_code)
1373 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1375 "Usage: agl-compositor [OPTIONS]\n"
1377 "This is " PACKAGE_STRING ", the reference compositor for\n"
1378 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1379 "on which backend is in use different options will be accepted.\n"
1383 " --version\t\tPrint agl-compositor version\n"
1384 " -B, --backend=MODULE\tBackend module, one of\n"
1385 "\t\t\t\tdrm-backend.so\n"
1386 "\t\t\t\twayland-backend.so\n"
1387 "\t\t\t\tx11-backend.so\n"
1388 " -S, --socket=NAME\tName of socket to listen on\n"
1389 " --log=FILE\t\tLog to the given file\n"
1390 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1391 " --no-config\t\tDo not read agl-compositor.ini\n"
1392 " --debug\t\tEnable debug extension(s)\n"
1393 " -h, --help\t\tThis help message\n"
1398 int main(int argc, char *argv[])
1400 struct ivi_compositor ivi = { 0 };
1401 struct wl_display *display = NULL;
1402 struct wl_event_loop *loop;
1403 struct wl_event_source *signals[3] = { 0 };
1404 struct weston_config_section *section;
1405 /* Command line options */
1406 char *backend = NULL;
1407 char *socket_name = NULL;
1413 char *config_file = NULL;
1414 struct weston_log_context *log_ctx = NULL;
1415 struct weston_log_subscriber *logger;
1416 int ret = EXIT_FAILURE;
1418 const struct weston_option core_options[] = {
1419 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1420 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1421 { WESTON_OPTION_STRING, "log", 0, &log },
1422 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1423 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1424 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1425 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1426 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1429 wl_list_init(&ivi.outputs);
1430 wl_list_init(&ivi.surfaces);
1431 wl_list_init(&ivi.pending_surfaces);
1432 wl_list_init(&ivi.popup_pending_apps);
1433 wl_list_init(&ivi.fullscreen_pending_apps);
1434 wl_list_init(&ivi.split_pending_apps);
1435 wl_list_init(&ivi.remote_pending_apps);
1436 wl_list_init(&ivi.desktop_clients);
1438 /* Prevent any clients we spawn getting our stdin */
1439 os_fd_set_cloexec(STDIN_FILENO);
1441 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1444 usage(EXIT_SUCCESS);
1447 printf(PACKAGE_STRING "\n");
1448 return EXIT_SUCCESS;
1451 log_ctx = weston_log_ctx_compositor_create();
1453 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1457 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1458 "agl-compositor log\n",
1462 weston_log_set_handler(vlog, vlog_continue);
1464 logger = weston_log_subscriber_create_log(logfile);
1465 weston_log_subscribe(log_ctx, logger, "log");
1467 if (load_config(&ivi.config, no_config, config_file) < 0)
1469 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1471 weston_config_section_get_string(section, "backend", &backend,
1474 backend = choose_default_backend();
1477 weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
1479 display = wl_display_create();
1480 loop = wl_display_get_event_loop(display);
1482 wl_display_set_global_filter(display,
1483 global_filter, &ivi);
1485 /* Register signal handlers so we shut down cleanly */
1487 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1489 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1491 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1494 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1498 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1499 if (!ivi.compositor) {
1500 weston_log("fatal: failed to create compositor.\n");
1504 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1505 goto error_compositor;
1507 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1508 weston_log("fatal: failed to create compositor backend.\n");
1509 goto error_compositor;
1512 ivi.heads_changed.notify = heads_changed;
1513 weston_compositor_add_heads_changed_listener(ivi.compositor,
1514 &ivi.heads_changed);
1516 if (ivi_desktop_init(&ivi) < 0)
1517 goto error_compositor;
1519 ivi_seat_init(&ivi);
1521 if (ivi_policy_init(&ivi) < 0)
1522 goto error_compositor;
1524 if (ivi_shell_init(&ivi) < 0)
1525 goto error_compositor;
1527 add_bindings(ivi.compositor);
1529 weston_compositor_flush_heads_changed(ivi.compositor);
1531 if (ivi.remoting_api)
1532 ivi_enable_remote_outputs(&ivi);
1534 if (create_listening_socket(display, socket_name) < 0)
1535 goto error_compositor;
1537 ivi_shell_init_black_fs(&ivi);
1539 ivi.compositor->exit = handle_exit;
1541 weston_compositor_wake(ivi.compositor);
1543 ivi_shell_create_global(&ivi);
1544 ivi_launch_shell_client(&ivi);
1546 ivi_screenshooter_create(&ivi);
1547 ivi_agl_systemd_notify(&ivi);
1549 wl_display_run(display);
1551 ret = ivi.compositor->exit_code;
1553 wl_display_destroy_clients(display);
1556 weston_compositor_tear_down(ivi.compositor);
1558 weston_compositor_log_scope_destroy(log_scope);
1561 weston_log_ctx_compositor_destroy(ivi.compositor);
1562 weston_compositor_destroy(ivi.compositor);
1564 weston_log_subscriber_destroy_log(logger);
1566 ivi_policy_destroy(ivi.policy);
1569 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1571 wl_event_source_remove(signals[i]);
1573 wl_display_destroy(display);
1577 weston_config_destroy(ivi.config);