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"
60 static int cached_tm_mday = -1;
61 static struct weston_log_scope *log_scope;
63 struct ivi_compositor *
64 to_ivi_compositor(struct weston_compositor *ec)
66 return weston_compositor_get_user_data(ec);
70 handle_output_destroy(struct wl_listener *listener, void *data)
72 struct ivi_output *output;
74 output = wl_container_of(listener, output, output_destroy);
75 assert(output->output == data);
77 output->output = NULL;
78 wl_list_remove(&output->output_destroy.link);
82 to_ivi_output(struct weston_output *o)
84 struct wl_listener *listener;
85 struct ivi_output *output;
87 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
88 output = wl_container_of(listener, output, output_destroy);
94 ivi_output_configure_app_id(struct ivi_output *ivi_output)
96 if (ivi_output->config) {
97 if (ivi_output->app_id != NULL)
100 weston_config_section_get_string(ivi_output->config,
107 static struct ivi_output *
108 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
109 struct weston_config_section *config)
111 struct ivi_output *output = NULL;
112 wl_list_for_each(output, &ivi->outputs, link) {
113 if (strcmp(output->name, name) == 0) {
119 output = zalloc(sizeof *output);
127 output->config = config;
129 output->output = weston_compositor_create_output(ivi->compositor, name);
130 if (!output->output) {
136 output->output_destroy.notify = handle_output_destroy;
137 weston_output_add_destroy_listener(output->output,
138 &output->output_destroy);
140 wl_list_insert(&ivi->outputs, &output->link);
141 ivi_output_configure_app_id(output);
146 count_heads(struct weston_output *output)
148 struct weston_head *iter = NULL;
151 while ((iter = weston_output_iterate_heads(output, iter)))
158 handle_head_destroy(struct wl_listener *listener, void *data)
160 struct weston_head *head = data;
161 struct weston_output *output;
163 wl_list_remove(&listener->link);
166 output = weston_head_get_output(head);
168 /* On shutdown path, the output might be already gone. */
172 /* We're the last head */
173 if (count_heads(output) <= 1)
174 weston_output_destroy(output);
178 add_head_destroyed_listener(struct weston_head *head)
180 /* We already have a destroy listener */
181 if (weston_head_get_destroy_listener(head, handle_head_destroy))
184 struct wl_listener *listener = zalloc(sizeof *listener);
188 listener->notify = handle_head_destroy;
189 weston_head_add_destroy_listener(head, listener);
193 drm_configure_output(struct ivi_output *output)
195 struct ivi_compositor *ivi = output->ivi;
196 struct weston_config_section *section = output->config;
197 enum weston_drm_backend_output_mode mode =
198 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
199 char *modeline = NULL;
200 char *gbm_format = NULL;
205 weston_config_section_get_string(section, "mode", &m, "preferred");
207 /* This should have been handled earlier */
208 assert(strcmp(m, "off") != 0);
210 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
211 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
212 } else if (strcmp(m, "preferred") != 0) {
218 weston_config_section_get_string(section, "gbm-format",
221 weston_config_section_get_string(section, "seat", &seat, "");
224 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
225 weston_log("Cannot configure output using weston_drm_output_api.\n");
231 ivi->drm_api->set_gbm_format(output->output, gbm_format);
234 ivi->drm_api->set_seat(output->output, seat);
240 #define WINDOWED_DEFAULT_WIDTH 1024
241 #define WINDOWED_DEFAULT_HEIGHT 768
244 windowed_configure_output(struct ivi_output *output)
246 struct ivi_compositor *ivi = output->ivi;
247 struct weston_config_section *section = output->config;
248 int width = WINDOWED_DEFAULT_WIDTH;
249 int height = WINDOWED_DEFAULT_HEIGHT;
254 weston_config_section_get_string(section, "mode", &mode, NULL);
255 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
256 weston_log("Invalid mode for output %s. Using defaults.\n",
258 width = WINDOWED_DEFAULT_WIDTH;
259 height = WINDOWED_DEFAULT_HEIGHT;
264 if (ivi->cmdline.width)
265 width = ivi->cmdline.width;
266 if (ivi->cmdline.height)
267 height = ivi->cmdline.height;
268 if (ivi->cmdline.scale)
269 weston_output_set_scale(output->output, ivi->cmdline.scale);
271 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
272 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
281 parse_transform(const char *transform, uint32_t *out)
283 static const struct { const char *name; uint32_t token; } transforms[] = {
284 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
285 { "90", WL_OUTPUT_TRANSFORM_90 },
286 { "180", WL_OUTPUT_TRANSFORM_180 },
287 { "270", WL_OUTPUT_TRANSFORM_270 },
288 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
289 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
290 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
291 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
294 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
295 if (strcmp(transforms[i].name, transform) == 0) {
296 *out = transforms[i].token;
300 *out = WL_OUTPUT_TRANSFORM_NORMAL;
305 configure_output(struct ivi_output *output)
307 struct ivi_compositor *ivi = output->ivi;
308 struct weston_config_section *section = output->config;
310 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
313 * This can happen with the wayland backend with 'sprawl'. The config
314 * is hard-coded, so we don't need to do anything.
316 if (!ivi->drm_api && !ivi->window_api)
322 weston_config_section_get_int(section, "scale", &scale, 1);
323 weston_config_section_get_string(section, "transform", &t, "normal");
324 if (parse_transform(t, &transform) < 0)
325 weston_log("Invalid transform \"%s\" for output %s\n",
330 weston_output_set_scale(output->output, scale);
331 weston_output_set_transform(output->output, transform);
334 return drm_configure_output(output);
336 return windowed_configure_output(output);
340 * Reorgainizes the output's add array into two sections.
341 * add[0..ret-1] are the heads that failed to get attached.
342 * add[ret..add_len] are the heads that were successfully attached.
344 * The order between elements in each section is stable.
347 try_attach_heads(struct ivi_output *output)
351 for (size_t i = 0; i < output->add_len; ++i) {
352 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
353 struct weston_head *tmp = output->add[i];
354 memmove(&output->add[fail_len + 1], output->add[fail_len],
355 sizeof output->add[0] * (i - fail_len));
356 output->add[fail_len++] = tmp;
364 * Like try_attach_heads, this reorganizes the output's add array into a failed
365 * and successful section.
366 * i is the number of heads that already failed the previous step.
369 try_enable_output(struct ivi_output *output, size_t i)
371 for (; i < output->add_len; ++i) {
372 struct weston_head *head;
374 if (weston_output_enable(output->output) == 0)
377 head = output->add[output->add_len - 1];
378 memmove(&output->add[i + 1], &output->add[i],
379 sizeof output->add[0] * (output->add_len - i));
380 output->add[i] = head;
382 weston_head_detach(head);
389 try_attach_enable_heads(struct ivi_output *output)
392 assert(!output->output->enabled);
394 fail_len = try_attach_heads(output);
396 if (configure_output(output) < 0)
399 fail_len = try_enable_output(output, fail_len);
401 /* All heads failed to be attached */
402 if (fail_len == output->add_len)
405 /* For each successful head attached */
406 for (size_t i = fail_len; i < output->add_len; ++i)
407 add_head_destroyed_listener(output->add[i]);
409 output->add_len = fail_len;
414 process_output(struct ivi_output *output)
416 if (output->output->enabled) {
417 output->add_len = try_attach_heads(output);
418 return output->add_len == 0 ? 0 : -1;
421 return try_attach_enable_heads(output);
425 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
427 struct weston_output *output;
428 struct ivi_output *ivi_output;
429 struct wl_listener *listener;
431 output = weston_head_get_output(head);
434 listener = weston_output_get_destroy_listener(output,
435 handle_output_destroy);
438 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
439 assert(ivi_output->output == output);
441 weston_head_detach(head);
442 if (count_heads(ivi_output->output) == 0) {
443 weston_output_disable(ivi_output->output);
447 static struct weston_config_section *
448 find_controlling_output_config(struct weston_config *config,
451 struct weston_config_section *section;
455 same_as = strdup(name);
457 section = weston_config_get_section(config, "output",
459 if (!section && depth > 0)
460 weston_log("Configuration error: output section reffered"
461 "to by same-as=%s not found.\n", same_as);
468 weston_log("Configuration error: same-as nested too "
469 "deep for output '%s'.\n", name);
473 weston_config_section_get_string(section, "same-as",
481 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
483 const char *name = weston_head_get_name(head);
484 struct weston_config_section *section;
485 struct ivi_output *output;
486 char *output_name = NULL;
488 section = find_controlling_output_config(ivi->config, name);
492 weston_config_section_get_string(section, "mode", &mode, NULL);
493 if (mode && strcmp(mode, "off") == 0) {
499 weston_config_section_get_string(section, "name",
502 output_name = strdup(name);
508 output = ivi_ensure_output(ivi, output_name, section);
512 if (output->add_len >= ARRAY_LENGTH(output->add))
515 output->add[output->add_len++] = head;
519 heads_changed(struct wl_listener *listener, void *arg)
521 struct weston_compositor *compositor = arg;
522 struct weston_head *head = NULL;
523 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
524 struct ivi_output *output;
526 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
527 bool connected = weston_head_is_connected(head);
528 bool enabled = weston_head_is_enabled(head);
529 bool changed = weston_head_is_device_changed(head);
530 bool non_desktop = weston_head_is_non_desktop(head);
532 if (connected && !enabled && !non_desktop)
533 head_prepare_enable(ivi, head);
534 else if (!connected && enabled)
535 head_disable(ivi, head);
536 else if (enabled && changed)
537 weston_log("Detected a monitor change on head '%s', "
538 "not bothering to do anything about it.\n",
539 weston_head_get_name(head));
541 weston_head_reset_device_changed(head);
544 wl_list_for_each(output, &ivi->outputs, link) {
545 if (output->add_len == 0)
548 if (process_output(output) < 0) {
550 ivi->init_failed = true;
557 drm_backend_remoted_output_configure(struct weston_output *output,
558 struct weston_config_section *section,
560 const struct weston_remoting_api *api)
562 char *gbm_format = NULL;
565 char *pipeline = NULL;
568 ret = api->set_mode(output, modeline);
570 weston_log("Cannot configure an output \"%s\" using "
571 "weston_remoting_api. Invalid mode\n",
576 /* FIXME: retrieve the scale and the transform from config file */
577 weston_output_set_scale(output, 1);
578 weston_output_set_transform(output, WL_OUTPUT_TRANSFORM_NORMAL);
580 weston_config_section_get_string(section, "gbm-format",
582 api->set_gbm_format(output, gbm_format);
585 weston_config_section_get_string(section, "seat", &seat, "");
587 api->set_seat(output, seat);
590 weston_config_section_get_string(section, "gst-pipeline", &pipeline,
593 api->set_gst_pipeline(output, pipeline);
598 weston_config_section_get_string(section, "host", &host, NULL);
599 weston_config_section_get_int(section, "port", &port, 0);
600 if (!host || port <= 0 || 65533 < port) {
601 weston_log("Cannot configure an output \"%s\". "
602 "Need to specify gst-pipeline or "
603 "host and port (1-65533).\n", output->name);
605 api->set_host(output, host);
607 api->set_port(output, port);
614 remote_output_init(struct weston_compositor *compositor,
615 struct weston_config_section *section,
616 const struct weston_remoting_api *api)
618 struct weston_output *output = NULL;
619 char *output_name, *modeline = NULL;
622 weston_config_section_get_string(section, "name", &output_name, NULL);
626 weston_config_section_get_string(section, "mode", &modeline, "off");
627 if (strcmp(modeline, "off") == 0)
630 output = api->create_output(compositor, output_name);
632 weston_log("Cannot create remoted output \"%s\".\n",
637 ret = drm_backend_remoted_output_configure(output, section,
640 weston_log("Cannot configure remoted output \"%s\".\n",
645 if (weston_output_enable(output) < 0) {
646 weston_log("Enabling remoted output \"%s\" failed.\n",
653 weston_log("remoted output '%s' enabled\n", output->name);
660 weston_output_destroy(output);
665 load_remoting(struct weston_compositor *compositor, struct weston_config *config)
667 const struct weston_remoting_api *api = NULL;
668 int (*module_init)(struct weston_compositor *wc);
669 struct weston_config_section *remote_section = NULL;
670 const char *section_name;
672 module_init = weston_load_module("remoting-plugin.so",
673 "weston_module_init");
677 if (module_init(compositor) < 0)
680 api = weston_remoting_get_api(compositor);
684 while (weston_config_next_section(config, &remote_section, §ion_name)) {
685 if (strcmp(section_name, "remote-output"))
687 remote_output_init(compositor, remote_section, api);
694 load_remoting(struct weston_compositor *compositor, struct weston_config *config)
701 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
703 struct weston_drm_backend_config config = {
705 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
706 .struct_size = sizeof config,
709 struct weston_config_section *section;
710 int use_current_mode = 0;
715 const struct weston_option options[] = {
716 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
717 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
718 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
719 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
720 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
723 parse_options(options, ARRAY_LENGTH(options), argc, argv);
724 config.use_pixman = use_pixman;
725 ivi->cmdline.use_current_mode = use_current_mode;
727 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
728 weston_config_section_get_string(section, "gbm-format",
729 &config.gbm_format, NULL);
730 weston_config_section_get_uint(section, "pageflip-timeout",
731 &config.pageflip_timeout, 0);
732 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
733 config.use_pixman_shadow = use_shadow;
735 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
740 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
742 weston_log("Cannot use drm output api.\n");
747 load_remoting(ivi->compositor, ivi->config);
750 free(config.gbm_format);
751 free(config.seat_id);
756 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
757 bool *use_pixman, bool *fullscreen, int *output_count)
759 struct weston_config_section *section;
763 const struct weston_option options[] = {
764 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
765 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
766 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
767 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
768 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
769 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
772 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
773 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
776 parse_options(options, ARRAY_LENGTH(options), argc, argv);
777 *use_pixman = pixman;
782 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
783 const char *match_prefix, const char *name_prefix)
785 struct weston_config_section *section = NULL;
786 const char *section_name;
787 char *default_output = NULL;
789 size_t match_len = strlen(match_prefix);
791 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
794 if (i >= output_count)
797 if (strcmp(section_name, "output") != 0)
800 weston_config_section_get_string(section, "name", &output_name, NULL);
801 if (output_name == NULL)
803 if (strncmp(output_name, match_prefix, match_len) != 0) {
808 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
817 for (; i < output_count; ++i) {
818 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
821 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
822 free(default_output);
826 free(default_output);
833 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
835 struct weston_wayland_backend_config config = {
837 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
838 .struct_size = sizeof config,
841 struct weston_config_section *section;
846 const struct weston_option options[] = {
847 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
848 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
851 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
852 &config.fullscreen, &output_count);
854 parse_options(options, ARRAY_LENGTH(options), argc, argv);
855 config.sprawl = sprawl;
857 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
858 weston_config_section_get_string(section, "cursor-theme",
859 &config.cursor_theme, NULL);
860 weston_config_section_get_int(section, "cursor-size",
861 &config.cursor_size, 32);
863 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
866 free(config.cursor_theme);
867 free(config.display_name);
872 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
875 * We will just assume if load_backend() finished cleanly and
876 * windowed_output_api is not present that wayland backend is started
877 * with --sprawl or runs on fullscreen-shell. In this case, all values
878 * are hardcoded, so nothing can be configured; simply create and
881 if (ivi->window_api == NULL)
884 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
887 #ifdef HAVE_BACKEND_X11
889 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
891 struct weston_x11_backend_config config = {
893 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
894 .struct_size = sizeof config,
901 const struct weston_option options[] = {
902 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
905 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
906 &config.fullscreen, &output_count);
908 parse_options(options, ARRAY_LENGTH(options), argc, argv);
909 config.no_input = no_input;
911 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
917 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
918 if (!ivi->window_api) {
919 weston_log("Cannot use weston_windowed_output_api.\n");
923 return windowed_create_outputs(ivi, output_count, "X", "screen");
927 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
934 load_backend(struct ivi_compositor *ivi, const char *backend,
935 int *argc, char *argv[])
937 if (strcmp(backend, "drm-backend.so") == 0) {
938 return load_drm_backend(ivi, argc, argv);
939 } else if (strcmp(backend, "wayland-backend.so") == 0) {
940 return load_wayland_backend(ivi, argc, argv);
941 } else if (strcmp(backend, "x11-backend.so") == 0) {
942 return load_x11_backend(ivi, argc, argv);
945 weston_log("fatal: unknown backend '%s'.\n", backend);
950 choose_default_backend(void)
952 char *backend = NULL;
954 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
955 backend = strdup("wayland-backend.so");
956 else if (getenv("DISPLAY"))
957 backend = strdup("x11-backend.so");
959 backend = strdup("drm-backend.so");
965 compositor_init_config(struct weston_compositor *compositor,
966 struct weston_config *config)
968 struct xkb_rule_names xkb_names;
969 struct weston_config_section *section;
974 /* agl-compositor.ini [keyboard] */
975 section = weston_config_get_section(config, "keyboard", NULL, NULL);
976 weston_config_section_get_string(section, "keymap_rules",
977 (char **) &xkb_names.rules, NULL);
978 weston_config_section_get_string(section, "keymap_model",
979 (char **) &xkb_names.model, NULL);
980 weston_config_section_get_string(section, "keymap_layout",
981 (char **) &xkb_names.layout, NULL);
982 weston_config_section_get_string(section, "keymap_variant",
983 (char **) &xkb_names.variant, NULL);
984 weston_config_section_get_string(section, "keymap_options",
985 (char **) &xkb_names.options, NULL);
987 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
990 weston_config_section_get_int(section, "repeat-rate",
991 &compositor->kb_repeat_rate, 40);
992 weston_config_section_get_int(section, "repeat-delay",
993 &compositor->kb_repeat_delay, 400);
995 weston_config_section_get_bool(section, "vt-switching",
996 &vt_switching, true);
997 compositor->vt_switching = vt_switching;
999 /* agl-compositor.ini [core] */
1000 section = weston_config_get_section(config, "core", NULL, NULL);
1002 weston_config_section_get_bool(section, "require-input", &require_input, true);
1003 compositor->require_input = require_input;
1005 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1006 compositor->repaint_msec);
1007 if (repaint_msec < -10 || repaint_msec > 1000) {
1008 weston_log("Invalid repaint_window value in config: %d\n",
1011 compositor->repaint_msec = repaint_msec;
1013 weston_log("Output repaint window is %d ms maximum.\n",
1014 compositor->repaint_msec);
1019 struct ivi_surface *
1020 to_ivi_surface(struct weston_surface *surface)
1022 struct weston_desktop_surface *dsurface;
1024 dsurface = weston_surface_get_desktop_surface(surface);
1028 return weston_desktop_surface_get_user_data(dsurface);
1032 activate_binding(struct weston_seat *seat,
1033 struct weston_view *focus_view)
1035 struct weston_surface *focus = focus_view->surface;
1036 struct weston_surface *main_surface =
1037 weston_surface_get_main_surface(focus);
1038 struct ivi_surface *surface;
1040 surface = to_ivi_surface(main_surface);
1044 weston_seat_set_keyboard_focus(seat, focus);
1048 click_to_activate_binding(struct weston_pointer *pointer,
1049 const struct timespec *time,
1050 uint32_t button, void *data)
1052 if (pointer->grab != &pointer->default_grab)
1054 if (pointer->focus == NULL)
1057 activate_binding(pointer->seat, pointer->focus);
1061 touch_to_activate_binding(struct weston_touch *touch,
1062 const struct timespec *time,
1065 if (touch->grab != &touch->default_grab)
1067 if (touch->focus == NULL)
1070 activate_binding(touch->seat, touch->focus);
1074 add_bindings(struct weston_compositor *compositor)
1076 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1077 click_to_activate_binding,
1079 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1080 click_to_activate_binding,
1082 weston_compositor_add_touch_binding(compositor, 0,
1083 touch_to_activate_binding,
1088 create_listening_socket(struct wl_display *display, const char *socket_name)
1091 if (wl_display_add_socket(display, socket_name)) {
1092 weston_log("fatal: failed to add socket: %s\n",
1097 socket_name = wl_display_add_socket_auto(display);
1099 weston_log("fatal: failed to add socket: %s\n",
1105 setenv("WAYLAND_DISPLAY", socket_name, 1);
1111 global_filter(const struct wl_client *client, const struct wl_global *global,
1118 load_config(struct weston_config **config, bool no_config,
1119 const char *config_file)
1121 const char *file = "agl-compositor.ini";
1122 const char *full_path;
1128 *config = weston_config_parse(file);
1131 full_path = weston_config_get_full_path(*config);
1133 weston_log("Using config file '%s'.\n", full_path);
1134 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1139 if (config_file && !no_config) {
1140 weston_log("fatal: error opening or reading config file '%s'.\n",
1146 weston_log("Starting with no config file.\n");
1147 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1152 static FILE *logfile;
1155 log_timestamp(char *buf, size_t len)
1158 struct tm *brokendown_time;
1162 gettimeofday(&tv, NULL);
1164 brokendown_time = localtime(&tv.tv_sec);
1165 if (brokendown_time == NULL) {
1166 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1170 memset(datestr, 0, sizeof(datestr));
1171 if (brokendown_time->tm_mday != cached_tm_mday) {
1172 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1174 cached_tm_mday = brokendown_time->tm_mday;
1177 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1178 /* if datestr is empty it prints only timestr*/
1179 snprintf(buf, len, "%s[%s.%03li]", datestr,
1180 timestr, (tv.tv_usec / 1000));
1186 custom_handler(const char *fmt, va_list arg)
1190 weston_log_scope_printf(log_scope, "%s libwayland: ",
1191 log_timestamp(timestr, sizeof(timestr)));
1192 weston_log_scope_vprintf(log_scope, fmt, arg);
1196 log_file_open(const char *filename)
1198 wl_log_set_handler_server(custom_handler);
1201 logfile = fopen(filename, "a");
1206 os_fd_set_cloexec(fileno(logfile));
1207 setvbuf(logfile, NULL, _IOLBF, 256);
1212 log_file_close(void)
1214 if (logfile && logfile != stderr)
1220 vlog(const char *fmt, va_list ap)
1222 const char *oom = "Out of memory";
1227 if (weston_log_scope_is_enabled(log_scope)) {
1229 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1230 len_va = vasprintf(&str, fmt, ap);
1232 len = weston_log_scope_printf(log_scope, "%s %s",
1233 xlog_timestamp, str);
1236 len = weston_log_scope_printf(log_scope, "%s %s",
1237 xlog_timestamp, oom);
1246 vlog_continue(const char *fmt, va_list ap)
1248 return weston_log_scope_vprintf(log_scope, fmt, ap);
1252 on_term_signal(int signo, void *data)
1254 struct wl_display *display = data;
1256 weston_log("caught signal %d\n", signo);
1257 wl_display_terminate(display);
1263 handle_exit(struct weston_compositor *compositor)
1265 wl_display_terminate(compositor->wl_display);
1269 usage(int error_code)
1271 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1273 "Usage: agl-compositor [OPTIONS]\n"
1275 "This is " PACKAGE_STRING ", the reference compositor for\n"
1276 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1277 "on which backend is in use different options will be accepted.\n"
1281 " --version\t\tPrint agl-compositor version\n"
1282 " -B, --backend=MODULE\tBackend module, one of\n"
1283 "\t\t\t\tdrm-backend.so\n"
1284 "\t\t\t\twayland-backend.so\n"
1285 "\t\t\t\tx11-backend.so\n"
1286 " -S, --socket=NAME\tName of socket to listen on\n"
1287 " --log=FILE\t\tLog to the given file\n"
1288 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1289 " --no-config\t\tDo not read agl-compositor.ini\n"
1290 " --debug\t\tEnable debug extension\n"
1291 " -h, --help\t\tThis help message\n"
1296 int main(int argc, char *argv[])
1298 struct ivi_compositor ivi = { 0 };
1299 struct wl_display *display = NULL;
1300 struct wl_event_loop *loop;
1301 struct wl_event_source *signals[3] = { 0 };
1302 struct weston_config_section *section;
1303 /* Command line options */
1304 char *backend = NULL;
1305 char *socket_name = NULL;
1310 char *config_file = NULL;
1311 struct weston_log_context *log_ctx = NULL;
1312 struct weston_log_subscriber *logger;
1313 int ret = EXIT_FAILURE;
1315 const struct weston_option core_options[] = {
1316 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1317 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1318 { WESTON_OPTION_STRING, "log", 0, &log },
1319 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1320 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1321 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1322 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1325 wl_list_init(&ivi.outputs);
1326 wl_list_init(&ivi.surfaces);
1327 wl_list_init(&ivi.pending_surfaces);
1328 wl_list_init(&ivi.popup_pending_apps);
1329 wl_list_init(&ivi.fullscreen_pending_apps);
1330 wl_list_init(&ivi.split_pending_apps);
1331 wl_list_init(&ivi.remote_pending_apps);
1332 wl_list_init(&ivi.desktop_clients);
1334 /* Prevent any clients we spawn getting our stdin */
1335 os_fd_set_cloexec(STDIN_FILENO);
1337 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1340 usage(EXIT_SUCCESS);
1343 printf(PACKAGE_STRING "\n");
1344 return EXIT_SUCCESS;
1347 log_ctx = weston_log_ctx_compositor_create();
1349 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1353 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1354 "agl-compositor log\n",
1358 weston_log_set_handler(vlog, vlog_continue);
1360 logger = weston_log_subscriber_create_log(logfile);
1361 weston_log_subscribe(log_ctx, logger, "log");
1363 if (load_config(&ivi.config, no_config, config_file) < 0)
1365 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1367 weston_config_section_get_string(section, "backend", &backend,
1370 backend = choose_default_backend();
1373 display = wl_display_create();
1374 loop = wl_display_get_event_loop(display);
1376 wl_display_set_global_filter(display,
1377 global_filter, &ivi);
1379 /* Register signal handlers so we shut down cleanly */
1381 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1383 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1385 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1388 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1392 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1393 if (!ivi.compositor) {
1394 weston_log("fatal: failed to create compositor.\n");
1398 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1399 goto error_compositor;
1401 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1402 weston_log("fatal: failed to create compositor backend.\n");
1403 goto error_compositor;
1406 ivi.heads_changed.notify = heads_changed;
1407 weston_compositor_add_heads_changed_listener(ivi.compositor,
1408 &ivi.heads_changed);
1410 if (ivi_desktop_init(&ivi) < 0)
1411 goto error_compositor;
1413 if (ivi_policy_init(&ivi) < 0)
1414 goto error_compositor;
1416 if (ivi_shell_init(&ivi) < 0)
1417 goto error_compositor;
1419 add_bindings(ivi.compositor);
1421 weston_compositor_flush_heads_changed(ivi.compositor);
1423 ivi_shell_init_black_fs(&ivi);
1425 if (create_listening_socket(display, socket_name) < 0)
1426 goto error_compositor;
1428 ivi.compositor->exit = handle_exit;
1430 weston_compositor_wake(ivi.compositor);
1432 ivi_shell_create_global(&ivi);
1433 ivi_launch_shell_client(&ivi);
1434 ivi_agl_systemd_notify(&ivi);
1436 wl_display_run(display);
1438 ret = ivi.compositor->exit_code;
1440 wl_display_destroy_clients(display);
1443 weston_compositor_tear_down(ivi.compositor);
1445 weston_compositor_log_scope_destroy(log_scope);
1448 weston_log_ctx_compositor_destroy(ivi.compositor);
1449 weston_compositor_destroy(ivi.compositor);
1451 weston_log_subscriber_destroy_log(logger);
1453 ivi_policy_destroy(ivi.policy);
1456 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1458 wl_event_source_remove(signals[i]);
1460 wl_display_destroy(display);
1464 weston_config_destroy(ivi.config);