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 output->output = NULL;
82 wl_list_remove(&output->output_destroy.link);
86 to_ivi_output(struct weston_output *o)
88 struct wl_listener *listener;
89 struct ivi_output *output;
91 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
92 output = wl_container_of(listener, output, output_destroy);
98 ivi_output_configure_app_id(struct ivi_output *ivi_output)
100 if (ivi_output->config) {
101 if (ivi_output->app_id != NULL)
104 weston_config_section_get_string(ivi_output->config,
109 if (ivi_output->app_id == NULL)
112 weston_log("Will place app_id %s on output %s\n",
113 ivi_output->app_id, ivi_output->name);
117 static struct ivi_output *
118 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
119 struct weston_config_section *config)
121 struct ivi_output *output = NULL;
122 wl_list_for_each(output, &ivi->outputs, link) {
123 if (strcmp(output->name, name) == 0) {
129 output = zalloc(sizeof *output);
137 output->config = config;
139 output->output = weston_compositor_create_output(ivi->compositor, name);
140 if (!output->output) {
146 output->output_destroy.notify = handle_output_destroy;
147 weston_output_add_destroy_listener(output->output,
148 &output->output_destroy);
150 wl_list_insert(&ivi->outputs, &output->link);
151 ivi_output_configure_app_id(output);
156 count_heads(struct weston_output *output)
158 struct weston_head *iter = NULL;
161 while ((iter = weston_output_iterate_heads(output, iter)))
168 handle_head_destroy(struct wl_listener *listener, void *data)
170 struct weston_head *head = data;
171 struct weston_output *output;
173 wl_list_remove(&listener->link);
176 output = weston_head_get_output(head);
178 /* On shutdown path, the output might be already gone. */
182 /* We're the last head */
183 if (count_heads(output) <= 1)
184 weston_output_destroy(output);
188 add_head_destroyed_listener(struct weston_head *head)
190 /* We already have a destroy listener */
191 if (weston_head_get_destroy_listener(head, handle_head_destroy))
194 struct wl_listener *listener = zalloc(sizeof *listener);
198 listener->notify = handle_head_destroy;
199 weston_head_add_destroy_listener(head, listener);
203 drm_configure_output(struct ivi_output *output)
205 struct ivi_compositor *ivi = output->ivi;
206 struct weston_config_section *section = output->config;
207 enum weston_drm_backend_output_mode mode =
208 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
209 char *modeline = NULL;
210 char *gbm_format = NULL;
215 weston_config_section_get_string(section, "mode", &m, "preferred");
217 /* This should have been handled earlier */
218 assert(strcmp(m, "off") != 0);
220 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
221 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
222 } else if (strcmp(m, "preferred") != 0) {
228 weston_config_section_get_string(section, "gbm-format",
231 weston_config_section_get_string(section, "seat", &seat, "");
234 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
235 weston_log("Cannot configure output using weston_drm_output_api.\n");
241 ivi->drm_api->set_gbm_format(output->output, gbm_format);
244 ivi->drm_api->set_seat(output->output, seat);
250 #define WINDOWED_DEFAULT_WIDTH 1024
251 #define WINDOWED_DEFAULT_HEIGHT 768
254 windowed_configure_output(struct ivi_output *output)
256 struct ivi_compositor *ivi = output->ivi;
257 struct weston_config_section *section = output->config;
258 int width = WINDOWED_DEFAULT_WIDTH;
259 int height = WINDOWED_DEFAULT_HEIGHT;
264 weston_config_section_get_string(section, "mode", &mode, NULL);
265 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
266 weston_log("Invalid mode for output %s. Using defaults.\n",
268 width = WINDOWED_DEFAULT_WIDTH;
269 height = WINDOWED_DEFAULT_HEIGHT;
274 if (ivi->cmdline.width)
275 width = ivi->cmdline.width;
276 if (ivi->cmdline.height)
277 height = ivi->cmdline.height;
278 if (ivi->cmdline.scale)
279 weston_output_set_scale(output->output, ivi->cmdline.scale);
281 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
282 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
291 parse_transform(const char *transform, uint32_t *out)
293 static const struct { const char *name; uint32_t token; } transforms[] = {
294 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
295 { "90", WL_OUTPUT_TRANSFORM_90 },
296 { "180", WL_OUTPUT_TRANSFORM_180 },
297 { "270", WL_OUTPUT_TRANSFORM_270 },
298 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
299 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
300 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
301 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
304 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
305 if (strcmp(transforms[i].name, transform) == 0) {
306 *out = transforms[i].token;
310 *out = WL_OUTPUT_TRANSFORM_NORMAL;
315 configure_output(struct ivi_output *output)
317 struct ivi_compositor *ivi = output->ivi;
318 struct weston_config_section *section = output->config;
320 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
323 * This can happen with the wayland backend with 'sprawl'. The config
324 * is hard-coded, so we don't need to do anything.
326 if (!ivi->drm_api && !ivi->window_api)
332 weston_config_section_get_int(section, "scale", &scale, 1);
333 weston_config_section_get_string(section, "transform", &t, "normal");
334 if (parse_transform(t, &transform) < 0)
335 weston_log("Invalid transform \"%s\" for output %s\n",
340 weston_output_set_scale(output->output, scale);
341 weston_output_set_transform(output->output, transform);
344 return drm_configure_output(output);
346 return windowed_configure_output(output);
350 * Reorgainizes the output's add array into two sections.
351 * add[0..ret-1] are the heads that failed to get attached.
352 * add[ret..add_len] are the heads that were successfully attached.
354 * The order between elements in each section is stable.
357 try_attach_heads(struct ivi_output *output)
361 for (size_t i = 0; i < output->add_len; ++i) {
362 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
363 struct weston_head *tmp = output->add[i];
364 memmove(&output->add[fail_len + 1], output->add[fail_len],
365 sizeof output->add[0] * (i - fail_len));
366 output->add[fail_len++] = tmp;
374 * Like try_attach_heads, this reorganizes the output's add array into a failed
375 * and successful section.
376 * i is the number of heads that already failed the previous step.
379 try_enable_output(struct ivi_output *output, size_t i)
381 for (; i < output->add_len; ++i) {
382 struct weston_head *head;
384 if (weston_output_enable(output->output) == 0)
387 head = output->add[output->add_len - 1];
388 memmove(&output->add[i + 1], &output->add[i],
389 sizeof output->add[0] * (output->add_len - i));
390 output->add[i] = head;
392 weston_head_detach(head);
399 try_attach_enable_heads(struct ivi_output *output)
402 assert(!output->output->enabled);
404 fail_len = try_attach_heads(output);
406 if (configure_output(output) < 0)
409 fail_len = try_enable_output(output, fail_len);
411 /* All heads failed to be attached */
412 if (fail_len == output->add_len)
415 /* For each successful head attached */
416 for (size_t i = fail_len; i < output->add_len; ++i)
417 add_head_destroyed_listener(output->add[i]);
419 output->add_len = fail_len;
424 process_output(struct ivi_output *output)
426 if (output->output->enabled) {
427 output->add_len = try_attach_heads(output);
428 return output->add_len == 0 ? 0 : -1;
431 return try_attach_enable_heads(output);
435 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
437 struct weston_output *output;
438 struct ivi_output *ivi_output;
439 struct wl_listener *listener;
441 output = weston_head_get_output(head);
444 listener = weston_output_get_destroy_listener(output,
445 handle_output_destroy);
448 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
449 assert(ivi_output->output == output);
451 weston_head_detach(head);
452 if (count_heads(ivi_output->output) == 0) {
453 weston_output_disable(ivi_output->output);
457 static struct weston_config_section *
458 find_controlling_output_config(struct weston_config *config,
461 struct weston_config_section *section;
465 same_as = strdup(name);
467 section = weston_config_get_section(config, "output",
469 if (!section && depth > 0)
470 weston_log("Configuration error: output section reffered"
471 "to by same-as=%s not found.\n", same_as);
478 weston_log("Configuration error: same-as nested too "
479 "deep for output '%s'.\n", name);
483 weston_config_section_get_string(section, "same-as",
491 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
493 const char *name = weston_head_get_name(head);
494 struct weston_config_section *section;
495 struct ivi_output *output;
496 char *output_name = NULL;
498 section = find_controlling_output_config(ivi->config, name);
502 weston_config_section_get_string(section, "mode", &mode, NULL);
503 if (mode && strcmp(mode, "off") == 0) {
509 weston_config_section_get_string(section, "name",
512 output_name = strdup(name);
518 output = ivi_ensure_output(ivi, output_name, section);
522 if (output->add_len >= ARRAY_LENGTH(output->add))
525 output->add[output->add_len++] = head;
529 heads_changed(struct wl_listener *listener, void *arg)
531 struct weston_compositor *compositor = arg;
532 struct weston_head *head = NULL;
533 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
534 struct ivi_output *output;
536 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
537 bool connected = weston_head_is_connected(head);
538 bool enabled = weston_head_is_enabled(head);
539 bool changed = weston_head_is_device_changed(head);
540 bool non_desktop = weston_head_is_non_desktop(head);
542 if (connected && !enabled && !non_desktop)
543 head_prepare_enable(ivi, head);
544 else if (!connected && enabled)
545 head_disable(ivi, head);
546 else if (enabled && changed)
547 weston_log("Detected a monitor change on head '%s', "
548 "not bothering to do anything about it.\n",
549 weston_head_get_name(head));
551 weston_head_reset_device_changed(head);
554 wl_list_for_each(output, &ivi->outputs, link) {
555 if (output->add_len == 0)
558 if (process_output(output) < 0) {
560 ivi->init_failed = true;
567 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
569 struct weston_compositor *compositor = ivi->compositor;
570 int (*module_init)(struct weston_compositor *wc);
572 module_init = weston_load_module("waltham-transmitter.so",
577 if (module_init(compositor) < 0)
580 ivi->waltham_transmitter_api = weston_get_transmitter_api(compositor);
581 if (!ivi->waltham_transmitter_api) {
582 weston_log("Failed to load waltham-transmitter plugin.\n");
586 weston_log("waltham-transmitter plug-in loaded\n");
591 load_waltham_plugin(struct ivi_compositor *ivi, struct weston_config *config)
599 drm_backend_remoted_output_configure(struct weston_output *output,
600 struct weston_config_section *section,
602 const struct weston_remoting_api *api)
604 char *gbm_format = NULL;
607 char *pipeline = NULL;
610 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
613 ret = api->set_mode(output, modeline);
615 weston_log("Cannot configure an output \"%s\" using "
616 "weston_remoting_api. Invalid mode\n",
621 weston_config_section_get_int(section, "scale", &scale, 1);
622 weston_output_set_scale(output, scale);
624 weston_config_section_get_string(section, "transform", &trans, "normal");
625 if (parse_transform(trans, &transform) < 0) {
626 weston_log("Invalid transform \"%s\" for output %s\n",
627 trans, output->name);
629 weston_output_set_transform(output, transform);
631 weston_config_section_get_string(section, "gbm-format",
633 api->set_gbm_format(output, gbm_format);
636 weston_config_section_get_string(section, "seat", &seat, "");
638 api->set_seat(output, seat);
641 weston_config_section_get_string(section, "gst-pipeline", &pipeline,
644 api->set_gst_pipeline(output, pipeline);
649 weston_config_section_get_string(section, "host", &host, NULL);
650 weston_config_section_get_int(section, "port", &port, 0);
651 if (!host || port <= 0 || 65533 < port) {
652 weston_log("Cannot configure an output \"%s\". "
653 "Need to specify gst-pipeline or "
654 "host and port (1-65533).\n", output->name);
656 api->set_host(output, host);
658 api->set_port(output, port);
665 remote_output_init(struct ivi_output *ivi_output,
666 struct weston_compositor *compositor,
667 struct weston_config_section *section,
668 const struct weston_remoting_api *api)
670 char *output_name, *modeline = NULL;
673 weston_config_section_get_string(section, "name", &output_name, NULL);
677 weston_config_section_get_string(section, "mode", &modeline, "off");
678 if (strcmp(modeline, "off") == 0)
681 ivi_output->output = api->create_output(compositor, output_name);
682 if (!ivi_output->output) {
683 weston_log("Cannot create remoted output \"%s\".\n",
688 ret = drm_backend_remoted_output_configure(ivi_output->output, section,
691 weston_log("Cannot configure remoted output \"%s\".\n",
696 if (weston_output_enable(ivi_output->output) < 0) {
697 weston_log("Enabling remoted output \"%s\" failed.\n",
704 weston_log("remoted output '%s' enabled\n", ivi_output->output->name);
711 if (ivi_output->output)
712 weston_output_destroy(ivi_output->output);
718 ivi_enable_remote_outputs(struct ivi_compositor *ivi)
720 struct weston_config_section *remote_section = NULL;
721 const char *section_name;
722 struct weston_config *config = ivi->config;
724 while (weston_config_next_section(config, &remote_section, §ion_name)) {
725 if (strcmp(section_name, "remote-output"))
728 struct ivi_output *ivi_output = NULL;
729 bool output_found = false;
732 weston_config_section_get_string(remote_section,
733 "name", &_name, NULL);
734 wl_list_for_each(ivi_output, &ivi->outputs, link) {
735 if (!strcmp(ivi_output->name, _name)) {
746 ivi_output = zalloc(sizeof(*ivi_output));
748 ivi_output->ivi = ivi;
749 ivi_output->name = _name;
750 ivi_output->config = remote_section;
751 ivi_output->type = OUTPUT_REMOTE;
753 if (remote_output_init(ivi_output, ivi->compositor,
754 remote_section, ivi->remoting_api)) {
755 free(ivi_output->name);
760 ivi_output->output_destroy.notify = handle_output_destroy;
761 weston_output_add_destroy_listener(ivi_output->output,
762 &ivi_output->output_destroy);
764 wl_list_insert(&ivi->outputs, &ivi_output->link);
765 ivi_output_configure_app_id(ivi_output);
770 ivi_enable_waltham_outputs(struct ivi_compositor *ivi)
772 struct weston_config_section *transmitter_section = NULL;
773 const char *sect_name;
774 struct weston_config *config = ivi->config;
776 while (weston_config_next_section(config, &transmitter_section, §_name)) {
777 if (strcmp(sect_name, "transmitter-output"))
780 struct ivi_output *ivi_output = NULL;
781 bool output_found = false;
784 weston_config_section_get_string(transmitter_section,
785 "name", &_name, NULL);
786 wl_list_for_each(ivi_output, &ivi->outputs, link) {
787 if (!strcmp(ivi_output->name, _name)) {
798 ivi_output = zalloc(sizeof(*ivi_output));
800 ivi_output->ivi = ivi;
801 ivi_output->name = _name;
802 ivi_output->config = transmitter_section;
804 if (remote_output_init(ivi_output, ivi->compositor,
805 transmitter_section, ivi->remoting_api)) {
806 free(ivi_output->name);
811 ivi_output->type = OUTPUT_WALTHAM;
812 ivi_output->output_destroy.notify = handle_output_destroy;
813 weston_output_add_destroy_listener(ivi_output->output,
814 &ivi_output->output_destroy);
816 wl_list_insert(&ivi->outputs, &ivi_output->link);
817 ivi_output_configure_app_id(ivi_output);
822 load_remoting_plugin(struct ivi_compositor *ivi, struct weston_config *config)
824 struct weston_compositor *compositor = ivi->compositor;
825 int (*module_init)(struct weston_compositor *wc);
827 module_init = weston_load_module("remoting-plugin.so",
828 "weston_module_init");
832 if (module_init(compositor) < 0)
835 ivi->remoting_api = weston_remoting_get_api(compositor);
836 if (!ivi->remoting_api)
842 load_remoting_plugin(struct weston_compositor *compositor, struct weston_config *config)
849 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
851 struct weston_drm_backend_config config = {
853 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
854 .struct_size = sizeof config,
857 struct weston_config_section *section;
858 int use_current_mode = 0;
863 const struct weston_option options[] = {
864 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
865 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
866 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
867 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
868 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
871 parse_options(options, ARRAY_LENGTH(options), argc, argv);
872 config.use_pixman = use_pixman;
873 ivi->cmdline.use_current_mode = use_current_mode;
875 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
876 weston_config_section_get_string(section, "gbm-format",
877 &config.gbm_format, NULL);
878 weston_config_section_get_uint(section, "pageflip-timeout",
879 &config.pageflip_timeout, 0);
880 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
881 config.use_pixman_shadow = use_shadow;
883 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
888 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
890 weston_log("Cannot use drm output api.\n");
895 load_remoting_plugin(ivi, ivi->config);
896 load_waltham_plugin(ivi, ivi->config);
899 free(config.gbm_format);
900 free(config.seat_id);
905 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
906 bool *use_pixman, bool *fullscreen, int *output_count)
908 struct weston_config_section *section;
912 const struct weston_option options[] = {
913 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
914 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
915 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
916 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
917 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
918 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
921 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
922 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
925 parse_options(options, ARRAY_LENGTH(options), argc, argv);
926 *use_pixman = pixman;
931 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
932 const char *match_prefix, const char *name_prefix)
934 struct weston_config_section *section = NULL;
935 const char *section_name;
936 char *default_output = NULL;
938 size_t match_len = strlen(match_prefix);
940 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
943 if (i >= output_count)
946 if (strcmp(section_name, "output") != 0)
949 weston_config_section_get_string(section, "name", &output_name, NULL);
950 if (output_name == NULL)
952 if (strncmp(output_name, match_prefix, match_len) != 0) {
957 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
966 for (; i < output_count; ++i) {
967 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
970 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
971 free(default_output);
975 free(default_output);
982 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
984 struct weston_wayland_backend_config config = {
986 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
987 .struct_size = sizeof config,
990 struct weston_config_section *section;
995 const struct weston_option options[] = {
996 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
997 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
1000 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1001 &config.fullscreen, &output_count);
1003 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1004 config.sprawl = sprawl;
1006 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1007 weston_config_section_get_string(section, "cursor-theme",
1008 &config.cursor_theme, NULL);
1009 weston_config_section_get_int(section, "cursor-size",
1010 &config.cursor_size, 32);
1012 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
1015 free(config.cursor_theme);
1016 free(config.display_name);
1021 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1024 * We will just assume if load_backend() finished cleanly and
1025 * windowed_output_api is not present that wayland backend is started
1026 * with --sprawl or runs on fullscreen-shell. In this case, all values
1027 * are hardcoded, so nothing can be configured; simply create and
1030 if (ivi->window_api == NULL)
1033 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
1036 #ifdef HAVE_BACKEND_X11
1038 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1040 struct weston_x11_backend_config config = {
1042 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
1043 .struct_size = sizeof config,
1050 const struct weston_option options[] = {
1051 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1054 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
1055 &config.fullscreen, &output_count);
1057 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1058 config.no_input = no_input;
1060 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
1066 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
1067 if (!ivi->window_api) {
1068 weston_log("Cannot use weston_windowed_output_api.\n");
1072 return windowed_create_outputs(ivi, output_count, "X", "screen");
1076 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
1083 load_backend(struct ivi_compositor *ivi, const char *backend,
1084 int *argc, char *argv[])
1086 if (strcmp(backend, "drm-backend.so") == 0) {
1087 return load_drm_backend(ivi, argc, argv);
1088 } else if (strcmp(backend, "wayland-backend.so") == 0) {
1089 return load_wayland_backend(ivi, argc, argv);
1090 } else if (strcmp(backend, "x11-backend.so") == 0) {
1091 return load_x11_backend(ivi, argc, argv);
1094 weston_log("fatal: unknown backend '%s'.\n", backend);
1099 choose_default_backend(void)
1101 char *backend = NULL;
1103 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
1104 backend = strdup("wayland-backend.so");
1105 else if (getenv("DISPLAY"))
1106 backend = strdup("x11-backend.so");
1108 backend = strdup("drm-backend.so");
1114 compositor_init_config(struct weston_compositor *compositor,
1115 struct weston_config *config)
1117 struct xkb_rule_names xkb_names;
1118 struct weston_config_section *section;
1123 /* agl-compositor.ini [keyboard] */
1124 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1125 weston_config_section_get_string(section, "keymap_rules",
1126 (char **) &xkb_names.rules, NULL);
1127 weston_config_section_get_string(section, "keymap_model",
1128 (char **) &xkb_names.model, NULL);
1129 weston_config_section_get_string(section, "keymap_layout",
1130 (char **) &xkb_names.layout, NULL);
1131 weston_config_section_get_string(section, "keymap_variant",
1132 (char **) &xkb_names.variant, NULL);
1133 weston_config_section_get_string(section, "keymap_options",
1134 (char **) &xkb_names.options, NULL);
1136 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
1139 weston_config_section_get_int(section, "repeat-rate",
1140 &compositor->kb_repeat_rate, 40);
1141 weston_config_section_get_int(section, "repeat-delay",
1142 &compositor->kb_repeat_delay, 400);
1144 weston_config_section_get_bool(section, "vt-switching",
1145 &vt_switching, true);
1146 compositor->vt_switching = vt_switching;
1148 /* agl-compositor.ini [core] */
1149 section = weston_config_get_section(config, "core", NULL, NULL);
1151 weston_config_section_get_bool(section, "require-input", &require_input, true);
1152 compositor->require_input = require_input;
1154 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
1155 compositor->repaint_msec);
1156 if (repaint_msec < -10 || repaint_msec > 1000) {
1157 weston_log("Invalid repaint_window value in config: %d\n",
1160 compositor->repaint_msec = repaint_msec;
1162 weston_log("Output repaint window is %d ms maximum.\n",
1163 compositor->repaint_msec);
1168 struct ivi_surface *
1169 to_ivi_surface(struct weston_surface *surface)
1171 struct weston_desktop_surface *dsurface;
1173 dsurface = weston_surface_get_desktop_surface(surface);
1177 return weston_desktop_surface_get_user_data(dsurface);
1181 activate_binding(struct weston_seat *seat,
1182 struct weston_view *focus_view)
1184 struct weston_surface *focus = focus_view->surface;
1185 struct weston_surface *main_surface =
1186 weston_surface_get_main_surface(focus);
1187 struct ivi_surface *surface;
1189 surface = to_ivi_surface(main_surface);
1193 weston_seat_set_keyboard_focus(seat, focus);
1197 click_to_activate_binding(struct weston_pointer *pointer,
1198 const struct timespec *time,
1199 uint32_t button, void *data)
1201 if (pointer->grab != &pointer->default_grab)
1203 if (pointer->focus == NULL)
1206 activate_binding(pointer->seat, pointer->focus);
1210 touch_to_activate_binding(struct weston_touch *touch,
1211 const struct timespec *time,
1214 if (touch->grab != &touch->default_grab)
1216 if (touch->focus == NULL)
1219 activate_binding(touch->seat, touch->focus);
1223 add_bindings(struct weston_compositor *compositor)
1225 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
1226 click_to_activate_binding,
1228 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
1229 click_to_activate_binding,
1231 weston_compositor_add_touch_binding(compositor, 0,
1232 touch_to_activate_binding,
1237 create_listening_socket(struct wl_display *display, const char *socket_name)
1240 if (wl_display_add_socket(display, socket_name)) {
1241 weston_log("fatal: failed to add socket: %s\n",
1246 socket_name = wl_display_add_socket_auto(display);
1248 weston_log("fatal: failed to add socket: %s\n",
1254 setenv("WAYLAND_DISPLAY", socket_name, 1);
1260 global_filter(const struct wl_client *client, const struct wl_global *global,
1267 load_config(struct weston_config **config, bool no_config,
1268 const char *config_file)
1270 const char *file = "agl-compositor.ini";
1271 const char *full_path;
1277 *config = weston_config_parse(file);
1280 full_path = weston_config_get_full_path(*config);
1282 weston_log("Using config file '%s'.\n", full_path);
1283 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1288 if (config_file && !no_config) {
1289 weston_log("fatal: error opening or reading config file '%s'.\n",
1295 weston_log("Starting with no config file.\n");
1296 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1301 static FILE *logfile;
1304 log_timestamp(char *buf, size_t len)
1307 struct tm *brokendown_time;
1311 gettimeofday(&tv, NULL);
1313 brokendown_time = localtime(&tv.tv_sec);
1314 if (brokendown_time == NULL) {
1315 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1319 memset(datestr, 0, sizeof(datestr));
1320 if (brokendown_time->tm_mday != cached_tm_mday) {
1321 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1323 cached_tm_mday = brokendown_time->tm_mday;
1326 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1327 /* if datestr is empty it prints only timestr*/
1328 snprintf(buf, len, "%s[%s.%03li]", datestr,
1329 timestr, (tv.tv_usec / 1000));
1335 custom_handler(const char *fmt, va_list arg)
1339 weston_log_scope_printf(log_scope, "%s libwayland: ",
1340 log_timestamp(timestr, sizeof(timestr)));
1341 weston_log_scope_vprintf(log_scope, fmt, arg);
1345 log_file_open(const char *filename)
1347 wl_log_set_handler_server(custom_handler);
1350 logfile = fopen(filename, "a");
1355 os_fd_set_cloexec(fileno(logfile));
1356 setvbuf(logfile, NULL, _IOLBF, 256);
1361 log_file_close(void)
1363 if (logfile && logfile != stderr)
1369 vlog(const char *fmt, va_list ap)
1371 const char *oom = "Out of memory";
1376 if (weston_log_scope_is_enabled(log_scope)) {
1378 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1379 len_va = vasprintf(&str, fmt, ap);
1381 len = weston_log_scope_printf(log_scope, "%s %s",
1382 xlog_timestamp, str);
1385 len = weston_log_scope_printf(log_scope, "%s %s",
1386 xlog_timestamp, oom);
1395 vlog_continue(const char *fmt, va_list ap)
1397 return weston_log_scope_vprintf(log_scope, fmt, ap);
1401 on_term_signal(int signo, void *data)
1403 struct wl_display *display = data;
1405 weston_log("caught signal %d\n", signo);
1406 wl_display_terminate(display);
1412 handle_exit(struct weston_compositor *compositor)
1414 wl_display_terminate(compositor->wl_display);
1418 usage(int error_code)
1420 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1422 "Usage: agl-compositor [OPTIONS]\n"
1424 "This is " PACKAGE_STRING ", the reference compositor for\n"
1425 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1426 "on which backend is in use different options will be accepted.\n"
1430 " --version\t\tPrint agl-compositor version\n"
1431 " -B, --backend=MODULE\tBackend module, one of\n"
1432 "\t\t\t\tdrm-backend.so\n"
1433 "\t\t\t\twayland-backend.so\n"
1434 "\t\t\t\tx11-backend.so\n"
1435 " -S, --socket=NAME\tName of socket to listen on\n"
1436 " --log=FILE\t\tLog to the given file\n"
1437 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1438 " --no-config\t\tDo not read agl-compositor.ini\n"
1439 " --debug\t\tEnable debug extension(s)\n"
1440 " -h, --help\t\tThis help message\n"
1445 int main(int argc, char *argv[])
1447 struct ivi_compositor ivi = { 0 };
1448 struct wl_display *display = NULL;
1449 struct wl_event_loop *loop;
1450 struct wl_event_source *signals[3] = { 0 };
1451 struct weston_config_section *section;
1452 /* Command line options */
1453 char *backend = NULL;
1454 char *socket_name = NULL;
1460 char *config_file = NULL;
1461 struct weston_log_context *log_ctx = NULL;
1462 struct weston_log_subscriber *logger;
1463 int ret = EXIT_FAILURE;
1465 const struct weston_option core_options[] = {
1466 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1467 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1468 { WESTON_OPTION_STRING, "log", 0, &log },
1469 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1470 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1471 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1472 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug },
1473 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1476 wl_list_init(&ivi.outputs);
1477 wl_list_init(&ivi.surfaces);
1478 wl_list_init(&ivi.pending_surfaces);
1479 wl_list_init(&ivi.popup_pending_apps);
1480 wl_list_init(&ivi.fullscreen_pending_apps);
1481 wl_list_init(&ivi.split_pending_apps);
1482 wl_list_init(&ivi.remote_pending_apps);
1483 wl_list_init(&ivi.desktop_clients);
1485 /* Prevent any clients we spawn getting our stdin */
1486 os_fd_set_cloexec(STDIN_FILENO);
1488 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1491 usage(EXIT_SUCCESS);
1494 printf(PACKAGE_STRING "\n");
1495 return EXIT_SUCCESS;
1498 log_ctx = weston_log_ctx_compositor_create();
1500 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1504 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1505 "agl-compositor log\n",
1509 weston_log_set_handler(vlog, vlog_continue);
1511 logger = weston_log_subscriber_create_log(logfile);
1512 weston_log_subscribe(log_ctx, logger, "log");
1514 if (load_config(&ivi.config, no_config, config_file) < 0)
1516 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1518 weston_config_section_get_string(section, "backend", &backend,
1521 backend = choose_default_backend();
1524 weston_config_section_get_bool(section, "hide-cursor", &ivi.hide_cursor, false);
1526 display = wl_display_create();
1527 loop = wl_display_get_event_loop(display);
1529 wl_display_set_global_filter(display,
1530 global_filter, &ivi);
1532 /* Register signal handlers so we shut down cleanly */
1534 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1536 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1538 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1541 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1545 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1546 if (!ivi.compositor) {
1547 weston_log("fatal: failed to create compositor.\n");
1551 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1552 goto error_compositor;
1554 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1555 weston_log("fatal: failed to create compositor backend.\n");
1556 goto error_compositor;
1559 ivi.heads_changed.notify = heads_changed;
1560 weston_compositor_add_heads_changed_listener(ivi.compositor,
1561 &ivi.heads_changed);
1563 if (ivi_desktop_init(&ivi) < 0)
1564 goto error_compositor;
1566 ivi_seat_init(&ivi);
1568 if (ivi_policy_init(&ivi) < 0)
1569 goto error_compositor;
1571 if (ivi_shell_init(&ivi) < 0)
1572 goto error_compositor;
1574 add_bindings(ivi.compositor);
1576 weston_compositor_flush_heads_changed(ivi.compositor);
1578 if (ivi.remoting_api)
1579 ivi_enable_remote_outputs(&ivi);
1581 if (ivi.waltham_transmitter_api)
1582 ivi_enable_waltham_outputs(&ivi);
1584 if (create_listening_socket(display, socket_name) < 0)
1585 goto error_compositor;
1587 ivi_shell_init_black_fs(&ivi);
1589 ivi.compositor->exit = handle_exit;
1591 weston_compositor_wake(ivi.compositor);
1593 ivi_shell_create_global(&ivi);
1594 ivi_launch_shell_client(&ivi);
1596 ivi_screenshooter_create(&ivi);
1597 ivi_agl_systemd_notify(&ivi);
1599 wl_display_run(display);
1601 ret = ivi.compositor->exit_code;
1603 wl_display_destroy_clients(display);
1606 weston_compositor_tear_down(ivi.compositor);
1608 weston_compositor_log_scope_destroy(log_scope);
1611 weston_log_ctx_compositor_destroy(ivi.compositor);
1612 weston_compositor_destroy(ivi.compositor);
1614 weston_log_subscriber_destroy_log(logger);
1616 ivi_policy_destroy(ivi.policy);
1619 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1621 wl_event_source_remove(signals[i]);
1623 wl_display_destroy(display);
1627 weston_config_destroy(ivi.config);