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,
105 weston_log("Will place app_id %s on output %s\n",
106 ivi_output->app_id, ivi_output->name);
110 static struct ivi_output *
111 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
112 struct weston_config_section *config)
114 struct ivi_output *output = NULL;
115 wl_list_for_each(output, &ivi->outputs, link) {
116 if (strcmp(output->name, name) == 0) {
122 output = zalloc(sizeof *output);
130 output->config = config;
132 output->output = weston_compositor_create_output(ivi->compositor, name);
133 if (!output->output) {
139 output->output_destroy.notify = handle_output_destroy;
140 weston_output_add_destroy_listener(output->output,
141 &output->output_destroy);
143 wl_list_insert(&ivi->outputs, &output->link);
144 ivi_output_configure_app_id(output);
149 count_heads(struct weston_output *output)
151 struct weston_head *iter = NULL;
154 while ((iter = weston_output_iterate_heads(output, iter)))
161 handle_head_destroy(struct wl_listener *listener, void *data)
163 struct weston_head *head = data;
164 struct weston_output *output;
166 wl_list_remove(&listener->link);
169 output = weston_head_get_output(head);
171 /* On shutdown path, the output might be already gone. */
175 /* We're the last head */
176 if (count_heads(output) <= 1)
177 weston_output_destroy(output);
181 add_head_destroyed_listener(struct weston_head *head)
183 /* We already have a destroy listener */
184 if (weston_head_get_destroy_listener(head, handle_head_destroy))
187 struct wl_listener *listener = zalloc(sizeof *listener);
191 listener->notify = handle_head_destroy;
192 weston_head_add_destroy_listener(head, listener);
196 drm_configure_output(struct ivi_output *output)
198 struct ivi_compositor *ivi = output->ivi;
199 struct weston_config_section *section = output->config;
200 enum weston_drm_backend_output_mode mode =
201 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
202 char *modeline = NULL;
203 char *gbm_format = NULL;
208 weston_config_section_get_string(section, "mode", &m, "preferred");
210 /* This should have been handled earlier */
211 assert(strcmp(m, "off") != 0);
213 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
214 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
215 } else if (strcmp(m, "preferred") != 0) {
221 weston_config_section_get_string(section, "gbm-format",
224 weston_config_section_get_string(section, "seat", &seat, "");
227 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
228 weston_log("Cannot configure output using weston_drm_output_api.\n");
234 ivi->drm_api->set_gbm_format(output->output, gbm_format);
237 ivi->drm_api->set_seat(output->output, seat);
243 #define WINDOWED_DEFAULT_WIDTH 1024
244 #define WINDOWED_DEFAULT_HEIGHT 768
247 windowed_configure_output(struct ivi_output *output)
249 struct ivi_compositor *ivi = output->ivi;
250 struct weston_config_section *section = output->config;
251 int width = WINDOWED_DEFAULT_WIDTH;
252 int height = WINDOWED_DEFAULT_HEIGHT;
257 weston_config_section_get_string(section, "mode", &mode, NULL);
258 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
259 weston_log("Invalid mode for output %s. Using defaults.\n",
261 width = WINDOWED_DEFAULT_WIDTH;
262 height = WINDOWED_DEFAULT_HEIGHT;
267 if (ivi->cmdline.width)
268 width = ivi->cmdline.width;
269 if (ivi->cmdline.height)
270 height = ivi->cmdline.height;
271 if (ivi->cmdline.scale)
272 weston_output_set_scale(output->output, ivi->cmdline.scale);
274 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
275 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
284 parse_transform(const char *transform, uint32_t *out)
286 static const struct { const char *name; uint32_t token; } transforms[] = {
287 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
288 { "90", WL_OUTPUT_TRANSFORM_90 },
289 { "180", WL_OUTPUT_TRANSFORM_180 },
290 { "270", WL_OUTPUT_TRANSFORM_270 },
291 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
292 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
293 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
294 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
297 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
298 if (strcmp(transforms[i].name, transform) == 0) {
299 *out = transforms[i].token;
303 *out = WL_OUTPUT_TRANSFORM_NORMAL;
308 configure_output(struct ivi_output *output)
310 struct ivi_compositor *ivi = output->ivi;
311 struct weston_config_section *section = output->config;
313 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
316 * This can happen with the wayland backend with 'sprawl'. The config
317 * is hard-coded, so we don't need to do anything.
319 if (!ivi->drm_api && !ivi->window_api)
325 weston_config_section_get_int(section, "scale", &scale, 1);
326 weston_config_section_get_string(section, "transform", &t, "normal");
327 if (parse_transform(t, &transform) < 0)
328 weston_log("Invalid transform \"%s\" for output %s\n",
333 weston_output_set_scale(output->output, scale);
334 weston_output_set_transform(output->output, transform);
337 return drm_configure_output(output);
339 return windowed_configure_output(output);
343 * Reorgainizes the output's add array into two sections.
344 * add[0..ret-1] are the heads that failed to get attached.
345 * add[ret..add_len] are the heads that were successfully attached.
347 * The order between elements in each section is stable.
350 try_attach_heads(struct ivi_output *output)
354 for (size_t i = 0; i < output->add_len; ++i) {
355 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
356 struct weston_head *tmp = output->add[i];
357 memmove(&output->add[fail_len + 1], output->add[fail_len],
358 sizeof output->add[0] * (i - fail_len));
359 output->add[fail_len++] = tmp;
367 * Like try_attach_heads, this reorganizes the output's add array into a failed
368 * and successful section.
369 * i is the number of heads that already failed the previous step.
372 try_enable_output(struct ivi_output *output, size_t i)
374 for (; i < output->add_len; ++i) {
375 struct weston_head *head;
377 if (weston_output_enable(output->output) == 0)
380 head = output->add[output->add_len - 1];
381 memmove(&output->add[i + 1], &output->add[i],
382 sizeof output->add[0] * (output->add_len - i));
383 output->add[i] = head;
385 weston_head_detach(head);
392 try_attach_enable_heads(struct ivi_output *output)
395 assert(!output->output->enabled);
397 fail_len = try_attach_heads(output);
399 if (configure_output(output) < 0)
402 fail_len = try_enable_output(output, fail_len);
404 /* All heads failed to be attached */
405 if (fail_len == output->add_len)
408 /* For each successful head attached */
409 for (size_t i = fail_len; i < output->add_len; ++i)
410 add_head_destroyed_listener(output->add[i]);
412 output->add_len = fail_len;
417 process_output(struct ivi_output *output)
419 if (output->output->enabled) {
420 output->add_len = try_attach_heads(output);
421 return output->add_len == 0 ? 0 : -1;
424 return try_attach_enable_heads(output);
428 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
430 struct weston_output *output;
431 struct ivi_output *ivi_output;
432 struct wl_listener *listener;
434 output = weston_head_get_output(head);
437 listener = weston_output_get_destroy_listener(output,
438 handle_output_destroy);
441 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
442 assert(ivi_output->output == output);
444 weston_head_detach(head);
445 if (count_heads(ivi_output->output) == 0) {
446 weston_output_disable(ivi_output->output);
450 static struct weston_config_section *
451 find_controlling_output_config(struct weston_config *config,
454 struct weston_config_section *section;
458 same_as = strdup(name);
460 section = weston_config_get_section(config, "output",
462 if (!section && depth > 0)
463 weston_log("Configuration error: output section reffered"
464 "to by same-as=%s not found.\n", same_as);
471 weston_log("Configuration error: same-as nested too "
472 "deep for output '%s'.\n", name);
476 weston_config_section_get_string(section, "same-as",
484 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
486 const char *name = weston_head_get_name(head);
487 struct weston_config_section *section;
488 struct ivi_output *output;
489 char *output_name = NULL;
491 section = find_controlling_output_config(ivi->config, name);
495 weston_config_section_get_string(section, "mode", &mode, NULL);
496 if (mode && strcmp(mode, "off") == 0) {
502 weston_config_section_get_string(section, "name",
505 output_name = strdup(name);
511 output = ivi_ensure_output(ivi, output_name, section);
515 if (output->add_len >= ARRAY_LENGTH(output->add))
518 output->add[output->add_len++] = head;
522 heads_changed(struct wl_listener *listener, void *arg)
524 struct weston_compositor *compositor = arg;
525 struct weston_head *head = NULL;
526 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
527 struct ivi_output *output;
529 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
530 bool connected = weston_head_is_connected(head);
531 bool enabled = weston_head_is_enabled(head);
532 bool changed = weston_head_is_device_changed(head);
533 bool non_desktop = weston_head_is_non_desktop(head);
535 if (connected && !enabled && !non_desktop)
536 head_prepare_enable(ivi, head);
537 else if (!connected && enabled)
538 head_disable(ivi, head);
539 else if (enabled && changed)
540 weston_log("Detected a monitor change on head '%s', "
541 "not bothering to do anything about it.\n",
542 weston_head_get_name(head));
544 weston_head_reset_device_changed(head);
547 wl_list_for_each(output, &ivi->outputs, link) {
548 if (output->add_len == 0)
551 if (process_output(output) < 0) {
553 ivi->init_failed = true;
560 drm_backend_remoted_output_configure(struct weston_output *output,
561 struct weston_config_section *section,
563 const struct weston_remoting_api *api)
565 char *gbm_format = NULL;
568 char *pipeline = NULL;
571 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
574 ret = api->set_mode(output, modeline);
576 weston_log("Cannot configure an output \"%s\" using "
577 "weston_remoting_api. Invalid mode\n",
582 weston_config_section_get_int(section, "scale", &scale, 1);
583 weston_output_set_scale(output, scale);
585 weston_config_section_get_string(section, "transform", &trans, "normal");
586 if (parse_transform(trans, &transform) < 0) {
587 weston_log("Invalid transform \"%s\" for output %s\n",
588 trans, output->name);
590 weston_output_set_transform(output, transform);
592 weston_config_section_get_string(section, "gbm-format",
594 api->set_gbm_format(output, gbm_format);
597 weston_config_section_get_string(section, "seat", &seat, "");
599 api->set_seat(output, seat);
602 weston_config_section_get_string(section, "gst-pipeline", &pipeline,
605 api->set_gst_pipeline(output, pipeline);
610 weston_config_section_get_string(section, "host", &host, NULL);
611 weston_config_section_get_int(section, "port", &port, 0);
612 if (!host || port <= 0 || 65533 < port) {
613 weston_log("Cannot configure an output \"%s\". "
614 "Need to specify gst-pipeline or "
615 "host and port (1-65533).\n", output->name);
617 api->set_host(output, host);
619 api->set_port(output, port);
626 remote_output_init(struct ivi_output *ivi_output,
627 struct weston_compositor *compositor,
628 struct weston_config_section *section,
629 const struct weston_remoting_api *api)
631 char *output_name, *modeline = NULL;
634 weston_config_section_get_string(section, "name", &output_name, NULL);
638 weston_config_section_get_string(section, "mode", &modeline, "off");
639 if (strcmp(modeline, "off") == 0)
642 ivi_output->output = api->create_output(compositor, output_name);
643 if (!ivi_output->output) {
644 weston_log("Cannot create remoted output \"%s\".\n",
649 ret = drm_backend_remoted_output_configure(ivi_output->output, section,
652 weston_log("Cannot configure remoted output \"%s\".\n",
657 if (weston_output_enable(ivi_output->output) < 0) {
658 weston_log("Enabling remoted output \"%s\" failed.\n",
665 weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
672 if (ivi_output->output)
673 weston_output_destroy(ivi_output->output);
679 load_remoting(struct ivi_compositor *ivi, struct weston_config *config)
681 struct weston_compositor *compositor = ivi->compositor;
682 int (*module_init)(struct weston_compositor *wc);
683 struct weston_config_section *remote_section = NULL;
684 const char *section_name;
686 module_init = weston_load_module("remoting-plugin.so",
687 "weston_module_init");
691 if (module_init(compositor) < 0)
694 ivi->remoting_api = weston_remoting_get_api(compositor);
695 if (!ivi->remoting_api)
698 while (weston_config_next_section(config, &remote_section, §ion_name)) {
699 if (strcmp(section_name, "remote-output"))
702 struct ivi_output *ivi_output = NULL;
703 bool output_found = false;
706 weston_config_section_get_string(remote_section, "name", &_name, NULL);
707 wl_list_for_each(ivi_output, &ivi->outputs, link) {
708 if (!strcmp(ivi_output->name, _name)) {
719 ivi_output = zalloc(sizeof(*ivi_output));
721 ivi_output->ivi = ivi;
722 ivi_output->name = _name;
723 ivi_output->config = remote_section;
725 if (remote_output_init(ivi_output, compositor,
726 remote_section, ivi->remoting_api)) {
727 free(ivi_output->name);
732 ivi_output->output_destroy.notify = handle_output_destroy;
733 weston_output_add_destroy_listener(ivi_output->output,
734 &ivi_output->output_destroy);
736 wl_list_insert(&ivi->outputs, &ivi_output->link);
737 ivi_output_configure_app_id(ivi_output);
744 load_remoting(struct weston_compositor *compositor, struct weston_config *config)
751 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
753 struct weston_drm_backend_config config = {
755 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
756 .struct_size = sizeof config,
759 struct weston_config_section *section;
760 int use_current_mode = 0;
765 const struct weston_option options[] = {
766 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
767 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
768 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
769 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
770 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
773 parse_options(options, ARRAY_LENGTH(options), argc, argv);
774 config.use_pixman = use_pixman;
775 ivi->cmdline.use_current_mode = use_current_mode;
777 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
778 weston_config_section_get_string(section, "gbm-format",
779 &config.gbm_format, NULL);
780 weston_config_section_get_uint(section, "pageflip-timeout",
781 &config.pageflip_timeout, 0);
782 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
783 config.use_pixman_shadow = use_shadow;
785 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
790 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
792 weston_log("Cannot use drm output api.\n");
797 load_remoting(ivi, ivi->config);
800 free(config.gbm_format);
801 free(config.seat_id);
806 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
807 bool *use_pixman, bool *fullscreen, int *output_count)
809 struct weston_config_section *section;
813 const struct weston_option options[] = {
814 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
815 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
816 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
817 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
818 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
819 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
822 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
823 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
826 parse_options(options, ARRAY_LENGTH(options), argc, argv);
827 *use_pixman = pixman;
832 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
833 const char *match_prefix, const char *name_prefix)
835 struct weston_config_section *section = NULL;
836 const char *section_name;
837 char *default_output = NULL;
839 size_t match_len = strlen(match_prefix);
841 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
844 if (i >= output_count)
847 if (strcmp(section_name, "output") != 0)
850 weston_config_section_get_string(section, "name", &output_name, NULL);
851 if (output_name == NULL)
853 if (strncmp(output_name, match_prefix, match_len) != 0) {
858 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
867 for (; i < output_count; ++i) {
868 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
871 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
872 free(default_output);
876 free(default_output);
883 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
885 struct weston_wayland_backend_config config = {
887 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
888 .struct_size = sizeof config,
891 struct weston_config_section *section;
896 const struct weston_option options[] = {
897 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
898 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
901 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
902 &config.fullscreen, &output_count);
904 parse_options(options, ARRAY_LENGTH(options), argc, argv);
905 config.sprawl = sprawl;
907 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
908 weston_config_section_get_string(section, "cursor-theme",
909 &config.cursor_theme, NULL);
910 weston_config_section_get_int(section, "cursor-size",
911 &config.cursor_size, 32);
913 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
916 free(config.cursor_theme);
917 free(config.display_name);
922 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
925 * We will just assume if load_backend() finished cleanly and
926 * windowed_output_api is not present that wayland backend is started
927 * with --sprawl or runs on fullscreen-shell. In this case, all values
928 * are hardcoded, so nothing can be configured; simply create and
931 if (ivi->window_api == NULL)
934 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
937 #ifdef HAVE_BACKEND_X11
939 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
941 struct weston_x11_backend_config config = {
943 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
944 .struct_size = sizeof config,
951 const struct weston_option options[] = {
952 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
955 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
956 &config.fullscreen, &output_count);
958 parse_options(options, ARRAY_LENGTH(options), argc, argv);
959 config.no_input = no_input;
961 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
967 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
968 if (!ivi->window_api) {
969 weston_log("Cannot use weston_windowed_output_api.\n");
973 return windowed_create_outputs(ivi, output_count, "X", "screen");
977 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
984 load_backend(struct ivi_compositor *ivi, const char *backend,
985 int *argc, char *argv[])
987 if (strcmp(backend, "drm-backend.so") == 0) {
988 return load_drm_backend(ivi, argc, argv);
989 } else if (strcmp(backend, "wayland-backend.so") == 0) {
990 return load_wayland_backend(ivi, argc, argv);
991 } else if (strcmp(backend, "x11-backend.so") == 0) {
992 return load_x11_backend(ivi, argc, argv);
995 weston_log("fatal: unknown backend '%s'.\n", backend);
1000 choose_default_backend(void)
1002 char *backend = NULL;
1004 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1005 backend = strdup("wayland-backend.so");
1006 else if (getenv("DISPLAY"))
1007 backend = strdup("x11-backend.so");
1009 backend = strdup("drm-backend.so");
1015 compositor_init_config(struct weston_compositor *compositor,
1016 struct weston_config *config)
1018 struct xkb_rule_names xkb_names;
1019 struct weston_config_section *section;
1024 /* agl-compositor.ini [keyboard] */
1025 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1026 weston_config_section_get_string(section, "keymap_rules",
1027 (char **) &xkb_names.rules, NULL);
1028 weston_config_section_get_string(section, "keymap_model",
1029 (char **) &xkb_names.model, NULL);
1030 weston_config_section_get_string(section, "keymap_layout",
1031 (char **) &xkb_names.layout, NULL);
1032 weston_config_section_get_string(section, "keymap_variant",
1033 (char **) &xkb_names.variant, NULL);
1034 weston_config_section_get_string(section, "keymap_options",
1035 (char **) &xkb_names.options, NULL);
1037 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1040 weston_config_section_get_int(section, "repeat-rate",
1041 &compositor->kb_repeat_rate, 40);
1042 weston_config_section_get_int(section, "repeat-delay",
1043 &compositor->kb_repeat_delay, 400);
1045 weston_config_section_get_bool(section, "vt-switching",
1046 &vt_switching, true);
1047 compositor->vt_switching = vt_switching;
1049 /* agl-compositor.ini [core] */
1050 section = weston_config_get_section(config, "core", NULL, NULL);
1052 weston_config_section_get_bool(section, "require-input", &require_input, true);
1053 compositor->require_input = require_input;
1055 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1056 compositor->repaint_msec);
1057 if (repaint_msec < -10 || repaint_msec > 1000) {
1058 weston_log("Invalid repaint_window value in config: %d\n",
1061 compositor->repaint_msec = repaint_msec;
1063 weston_log("Output repaint window is %d ms maximum.\n",
1064 compositor->repaint_msec);
1069 struct ivi_surface *
1070 to_ivi_surface(struct weston_surface *surface)
1072 struct weston_desktop_surface *dsurface;
1074 dsurface = weston_surface_get_desktop_surface(surface);
1078 return weston_desktop_surface_get_user_data(dsurface);
1082 activate_binding(struct weston_seat *seat,
1083 struct weston_view *focus_view)
1085 struct weston_surface *focus = focus_view->surface;
1086 struct weston_surface *main_surface =
1087 weston_surface_get_main_surface(focus);
1088 struct ivi_surface *surface;
1090 surface = to_ivi_surface(main_surface);
1094 weston_seat_set_keyboard_focus(seat, focus);
1098 click_to_activate_binding(struct weston_pointer *pointer,
1099 const struct timespec *time,
1100 uint32_t button, void *data)
1102 if (pointer->grab != &pointer->default_grab)
1104 if (pointer->focus == NULL)
1107 activate_binding(pointer->seat, pointer->focus);
1111 touch_to_activate_binding(struct weston_touch *touch,
1112 const struct timespec *time,
1115 if (touch->grab != &touch->default_grab)
1117 if (touch->focus == NULL)
1120 activate_binding(touch->seat, touch->focus);
1124 add_bindings(struct weston_compositor *compositor)
1126 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1127 click_to_activate_binding,
1129 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1130 click_to_activate_binding,
1132 weston_compositor_add_touch_binding(compositor, 0,
1133 touch_to_activate_binding,
1138 create_listening_socket(struct wl_display *display, const char *socket_name)
1141 if (wl_display_add_socket(display, socket_name)) {
1142 weston_log("fatal: failed to add socket: %s\n",
1147 socket_name = wl_display_add_socket_auto(display);
1149 weston_log("fatal: failed to add socket: %s\n",
1155 setenv("WAYLAND_DISPLAY", socket_name, 1);
1161 global_filter(const struct wl_client *client, const struct wl_global *global,
1168 load_config(struct weston_config **config, bool no_config,
1169 const char *config_file)
1171 const char *file = "agl-compositor.ini";
1172 const char *full_path;
1178 *config = weston_config_parse(file);
1181 full_path = weston_config_get_full_path(*config);
1183 weston_log("Using config file '%s'.\n", full_path);
1184 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1189 if (config_file && !no_config) {
1190 weston_log("fatal: error opening or reading config file '%s'.\n",
1196 weston_log("Starting with no config file.\n");
1197 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1202 static FILE *logfile;
1205 log_timestamp(char *buf, size_t len)
1208 struct tm *brokendown_time;
1212 gettimeofday(&tv, NULL);
1214 brokendown_time = localtime(&tv.tv_sec);
1215 if (brokendown_time == NULL) {
1216 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1220 memset(datestr, 0, sizeof(datestr));
1221 if (brokendown_time->tm_mday != cached_tm_mday) {
1222 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1224 cached_tm_mday = brokendown_time->tm_mday;
1227 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1228 /* if datestr is empty it prints only timestr*/
1229 snprintf(buf, len, "%s[%s.%03li]", datestr,
1230 timestr, (tv.tv_usec / 1000));
1236 custom_handler(const char *fmt, va_list arg)
1240 weston_log_scope_printf(log_scope, "%s libwayland: ",
1241 log_timestamp(timestr, sizeof(timestr)));
1242 weston_log_scope_vprintf(log_scope, fmt, arg);
1246 log_file_open(const char *filename)
1248 wl_log_set_handler_server(custom_handler);
1251 logfile = fopen(filename, "a");
1256 os_fd_set_cloexec(fileno(logfile));
1257 setvbuf(logfile, NULL, _IOLBF, 256);
1262 log_file_close(void)
1264 if (logfile && logfile != stderr)
1270 vlog(const char *fmt, va_list ap)
1272 const char *oom = "Out of memory";
1277 if (weston_log_scope_is_enabled(log_scope)) {
1279 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1280 len_va = vasprintf(&str, fmt, ap);
1282 len = weston_log_scope_printf(log_scope, "%s %s",
1283 xlog_timestamp, str);
1286 len = weston_log_scope_printf(log_scope, "%s %s",
1287 xlog_timestamp, oom);
1296 vlog_continue(const char *fmt, va_list ap)
1298 return weston_log_scope_vprintf(log_scope, fmt, ap);
1302 on_term_signal(int signo, void *data)
1304 struct wl_display *display = data;
1306 weston_log("caught signal %d\n", signo);
1307 wl_display_terminate(display);
1313 handle_exit(struct weston_compositor *compositor)
1315 wl_display_terminate(compositor->wl_display);
1319 usage(int error_code)
1321 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1323 "Usage: agl-compositor [OPTIONS]\n"
1325 "This is " PACKAGE_STRING ", the reference compositor for\n"
1326 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1327 "on which backend is in use different options will be accepted.\n"
1331 " --version\t\tPrint agl-compositor version\n"
1332 " -B, --backend=MODULE\tBackend module, one of\n"
1333 "\t\t\t\tdrm-backend.so\n"
1334 "\t\t\t\twayland-backend.so\n"
1335 "\t\t\t\tx11-backend.so\n"
1336 " -S, --socket=NAME\tName of socket to listen on\n"
1337 " --log=FILE\t\tLog to the given file\n"
1338 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1339 " --no-config\t\tDo not read agl-compositor.ini\n"
1340 " --debug\t\tEnable debug extension\n"
1341 " -h, --help\t\tThis help message\n"
1346 int main(int argc, char *argv[])
1348 struct ivi_compositor ivi = { 0 };
1349 struct wl_display *display = NULL;
1350 struct wl_event_loop *loop;
1351 struct wl_event_source *signals[3] = { 0 };
1352 struct weston_config_section *section;
1353 /* Command line options */
1354 char *backend = NULL;
1355 char *socket_name = NULL;
1360 char *config_file = NULL;
1361 struct weston_log_context *log_ctx = NULL;
1362 struct weston_log_subscriber *logger;
1363 int ret = EXIT_FAILURE;
1365 const struct weston_option core_options[] = {
1366 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1367 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1368 { WESTON_OPTION_STRING, "log", 0, &log },
1369 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1370 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1371 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1372 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1375 wl_list_init(&ivi.outputs);
1376 wl_list_init(&ivi.surfaces);
1377 wl_list_init(&ivi.pending_surfaces);
1378 wl_list_init(&ivi.popup_pending_apps);
1379 wl_list_init(&ivi.fullscreen_pending_apps);
1380 wl_list_init(&ivi.split_pending_apps);
1381 wl_list_init(&ivi.remote_pending_apps);
1382 wl_list_init(&ivi.desktop_clients);
1384 /* Prevent any clients we spawn getting our stdin */
1385 os_fd_set_cloexec(STDIN_FILENO);
1387 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1390 usage(EXIT_SUCCESS);
1393 printf(PACKAGE_STRING "\n");
1394 return EXIT_SUCCESS;
1397 log_ctx = weston_log_ctx_compositor_create();
1399 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1403 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1404 "agl-compositor log\n",
1408 weston_log_set_handler(vlog, vlog_continue);
1410 logger = weston_log_subscriber_create_log(logfile);
1411 weston_log_subscribe(log_ctx, logger, "log");
1413 if (load_config(&ivi.config, no_config, config_file) < 0)
1415 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1417 weston_config_section_get_string(section, "backend", &backend,
1420 backend = choose_default_backend();
1423 display = wl_display_create();
1424 loop = wl_display_get_event_loop(display);
1426 wl_display_set_global_filter(display,
1427 global_filter, &ivi);
1429 /* Register signal handlers so we shut down cleanly */
1431 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1433 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1435 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1438 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1442 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1443 if (!ivi.compositor) {
1444 weston_log("fatal: failed to create compositor.\n");
1448 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1449 goto error_compositor;
1451 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1452 weston_log("fatal: failed to create compositor backend.\n");
1453 goto error_compositor;
1456 ivi.heads_changed.notify = heads_changed;
1457 weston_compositor_add_heads_changed_listener(ivi.compositor,
1458 &ivi.heads_changed);
1460 if (ivi_desktop_init(&ivi) < 0)
1461 goto error_compositor;
1463 if (ivi_policy_init(&ivi) < 0)
1464 goto error_compositor;
1466 if (ivi_shell_init(&ivi) < 0)
1467 goto error_compositor;
1469 add_bindings(ivi.compositor);
1471 weston_compositor_flush_heads_changed(ivi.compositor);
1473 ivi_shell_init_black_fs(&ivi);
1475 if (create_listening_socket(display, socket_name) < 0)
1476 goto error_compositor;
1478 ivi.compositor->exit = handle_exit;
1480 weston_compositor_wake(ivi.compositor);
1482 ivi_shell_create_global(&ivi);
1483 ivi_launch_shell_client(&ivi);
1484 ivi_agl_systemd_notify(&ivi);
1486 wl_display_run(display);
1488 ret = ivi.compositor->exit_code;
1490 wl_display_destroy_clients(display);
1493 weston_compositor_tear_down(ivi.compositor);
1495 weston_compositor_log_scope_destroy(log_scope);
1498 weston_log_ctx_compositor_destroy(ivi.compositor);
1499 weston_compositor_destroy(ivi.compositor);
1501 weston_log_subscriber_destroy_log(logger);
1503 ivi_policy_destroy(ivi.policy);
1506 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1508 wl_event_source_remove(signals[i]);
1510 wl_display_destroy(display);
1514 weston_config_destroy(ivi.config);