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>
50 #include "shared/os-compatibility.h"
51 #include "shared/helpers.h"
53 #include "agl-shell-server-protocol.h"
55 static int cached_tm_mday = -1;
56 static struct weston_log_scope *log_scope;
58 struct ivi_compositor *
59 to_ivi_compositor(struct weston_compositor *ec)
61 return weston_compositor_get_user_data(ec);
65 handle_output_destroy(struct wl_listener *listener, void *data)
67 struct ivi_output *output;
69 output = wl_container_of(listener, output, output_destroy);
70 assert(output->output == data);
72 output->output = NULL;
73 wl_list_remove(&output->output_destroy.link);
77 to_ivi_output(struct weston_output *o)
79 struct wl_listener *listener;
80 struct ivi_output *output;
82 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
83 output = wl_container_of(listener, output, output_destroy);
88 static struct ivi_output *
89 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
90 struct weston_config_section *config)
92 struct ivi_output *output = NULL;
93 wl_list_for_each(output, &ivi->outputs, link) {
94 if (strcmp(output->name, name) == 0) {
100 output = zalloc(sizeof *output);
108 output->config = config;
110 output->output = weston_compositor_create_output(ivi->compositor, name);
111 if (!output->output) {
117 output->output_destroy.notify = handle_output_destroy;
118 weston_output_add_destroy_listener(output->output,
119 &output->output_destroy);
121 wl_list_insert(&ivi->outputs, &output->link);
126 count_heads(struct weston_output *output)
128 struct weston_head *iter = NULL;
131 while ((iter = weston_output_iterate_heads(output, iter)))
138 handle_head_destroy(struct wl_listener *listener, void *data)
140 struct weston_head *head = data;
141 struct weston_output *output;
143 wl_list_remove(&listener->link);
146 output = weston_head_get_output(head);
148 /* On shutdown path, the output might be already gone. */
152 /* We're the last head */
153 if (count_heads(output) <= 1)
154 weston_output_destroy(output);
158 add_head_destroyed_listener(struct weston_head *head)
160 /* We already have a destroy listener */
161 if (weston_head_get_destroy_listener(head, handle_head_destroy))
164 struct wl_listener *listener = zalloc(sizeof *listener);
168 listener->notify = handle_head_destroy;
169 weston_head_add_destroy_listener(head, listener);
173 drm_configure_output(struct ivi_output *output)
175 struct ivi_compositor *ivi = output->ivi;
176 struct weston_config_section *section = output->config;
177 enum weston_drm_backend_output_mode mode =
178 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
179 char *modeline = NULL;
180 char *gbm_format = NULL;
185 weston_config_section_get_string(section, "mode", &m, "preferred");
187 /* This should have been handled earlier */
188 assert(strcmp(m, "off") != 0);
190 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
191 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
192 } else if (strcmp(m, "preferred") != 0) {
198 weston_config_section_get_string(section, "gbm-format",
201 weston_config_section_get_string(section, "seat", &seat, "");
204 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
205 weston_log("Cannot configure output using weston_drm_output_api.\n");
211 ivi->drm_api->set_gbm_format(output->output, gbm_format);
214 ivi->drm_api->set_seat(output->output, seat);
220 #define WINDOWED_DEFAULT_WIDTH 1024
221 #define WINDOWED_DEFAULT_HEIGHT 768
224 windowed_configure_output(struct ivi_output *output)
226 struct ivi_compositor *ivi = output->ivi;
227 struct weston_config_section *section = output->config;
228 int width = WINDOWED_DEFAULT_WIDTH;
229 int height = WINDOWED_DEFAULT_HEIGHT;
234 weston_config_section_get_string(section, "mode", &mode, NULL);
235 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
236 weston_log("Invalid mode for output %s. Using defaults.\n",
238 width = WINDOWED_DEFAULT_WIDTH;
239 height = WINDOWED_DEFAULT_HEIGHT;
244 if (ivi->cmdline.width)
245 width = ivi->cmdline.width;
246 if (ivi->cmdline.height)
247 height = ivi->cmdline.height;
248 if (ivi->cmdline.scale)
249 weston_output_set_scale(output->output, ivi->cmdline.scale);
251 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
252 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
261 parse_transform(const char *transform, uint32_t *out)
263 static const struct { const char *name; uint32_t token; } transforms[] = {
264 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
265 { "90", WL_OUTPUT_TRANSFORM_90 },
266 { "180", WL_OUTPUT_TRANSFORM_180 },
267 { "270", WL_OUTPUT_TRANSFORM_270 },
268 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
269 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
270 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
271 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
274 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
275 if (strcmp(transforms[i].name, transform) == 0) {
276 *out = transforms[i].token;
280 *out = WL_OUTPUT_TRANSFORM_NORMAL;
285 configure_output(struct ivi_output *output)
287 struct ivi_compositor *ivi = output->ivi;
288 struct weston_config_section *section = output->config;
290 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
293 * This can happen with the wayland backend with 'sprawl'. The config
294 * is hard-coded, so we don't need to do anything.
296 if (!ivi->drm_api && !ivi->window_api)
302 weston_config_section_get_int(section, "scale", &scale, 1);
303 weston_config_section_get_string(section, "transform", &t, "normal");
304 if (parse_transform(t, &transform) < 0)
305 weston_log("Invalid transform \"%s\" for output %s\n",
310 weston_output_set_scale(output->output, scale);
311 weston_output_set_transform(output->output, transform);
314 return drm_configure_output(output);
316 return windowed_configure_output(output);
320 * Reorgainizes the output's add array into two sections.
321 * add[0..ret-1] are the heads that failed to get attached.
322 * add[ret..add_len] are the heads that were successfully attached.
324 * The order between elements in each section is stable.
327 try_attach_heads(struct ivi_output *output)
331 for (size_t i = 0; i < output->add_len; ++i) {
332 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
333 struct weston_head *tmp = output->add[i];
334 memmove(&output->add[fail_len + 1], output->add[fail_len],
335 sizeof output->add[0] * (i - fail_len));
336 output->add[fail_len++] = tmp;
344 * Like try_attach_heads, this reorganizes the output's add array into a failed
345 * and successful section.
346 * i is the number of heads that already failed the previous step.
349 try_enable_output(struct ivi_output *output, size_t i)
351 for (; i < output->add_len; ++i) {
352 struct weston_head *head;
354 if (weston_output_enable(output->output) == 0)
357 head = output->add[output->add_len - 1];
358 memmove(&output->add[i + 1], &output->add[i],
359 sizeof output->add[0] * (output->add_len - i));
360 output->add[i] = head;
362 weston_head_detach(head);
369 try_attach_enable_heads(struct ivi_output *output)
372 assert(!output->output->enabled);
374 fail_len = try_attach_heads(output);
376 if (configure_output(output) < 0)
379 fail_len = try_enable_output(output, fail_len);
381 /* All heads failed to be attached */
382 if (fail_len == output->add_len)
385 /* For each successful head attached */
386 for (size_t i = fail_len; i < output->add_len; ++i)
387 add_head_destroyed_listener(output->add[i]);
389 output->add_len = fail_len;
394 process_output(struct ivi_output *output)
396 if (output->output->enabled) {
397 output->add_len = try_attach_heads(output);
398 return output->add_len == 0 ? 0 : -1;
401 return try_attach_enable_heads(output);
405 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
407 struct weston_output *output;
408 struct ivi_output *ivi_output;
409 struct wl_listener *listener;
411 output = weston_head_get_output(head);
414 listener = weston_output_get_destroy_listener(output,
415 handle_output_destroy);
418 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
419 assert(ivi_output->output == output);
421 weston_head_detach(head);
422 if (count_heads(ivi_output->output) == 0) {
423 weston_output_disable(ivi_output->output);
427 static struct weston_config_section *
428 find_controlling_output_config(struct weston_config *config,
431 struct weston_config_section *section;
435 same_as = strdup(name);
437 section = weston_config_get_section(config, "output",
439 if (!section && depth > 0)
440 weston_log("Configuration error: output section reffered"
441 "to by same-as=%s not found.\n", same_as);
448 weston_log("Configuration error: same-as nested too "
449 "deep for output '%s'.\n", name);
453 weston_config_section_get_string(section, "same-as",
461 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
463 const char *name = weston_head_get_name(head);
464 struct weston_config_section *section;
465 struct ivi_output *output;
466 char *output_name = NULL;
468 section = find_controlling_output_config(ivi->config, name);
472 weston_config_section_get_string(section, "mode", &mode, NULL);
473 if (mode && strcmp(mode, "off") == 0) {
479 weston_config_section_get_string(section, "name",
482 output_name = strdup(name);
488 output = ivi_ensure_output(ivi, output_name, section);
492 if (output->add_len >= ARRAY_LENGTH(output->add))
495 output->add[output->add_len++] = head;
499 heads_changed(struct wl_listener *listener, void *arg)
501 struct weston_compositor *compositor = arg;
502 struct weston_head *head = NULL;
503 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
504 struct ivi_output *output;
506 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
507 bool connected = weston_head_is_connected(head);
508 bool enabled = weston_head_is_enabled(head);
509 bool changed = weston_head_is_device_changed(head);
510 bool non_desktop = weston_head_is_non_desktop(head);
512 if (connected && !enabled && !non_desktop)
513 head_prepare_enable(ivi, head);
514 else if (!connected && enabled)
515 head_disable(ivi, head);
516 else if (enabled && changed)
517 weston_log("Detected a monitor change on head '%s', "
518 "not bothering to do anything about it.\n",
519 weston_head_get_name(head));
521 weston_head_reset_device_changed(head);
524 wl_list_for_each(output, &ivi->outputs, link) {
525 if (output->add_len == 0)
528 if (process_output(output) < 0) {
530 ivi->init_failed = true;
536 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
538 struct weston_drm_backend_config config = {
540 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
541 .struct_size = sizeof config,
544 struct weston_config_section *section;
545 int use_current_mode = 0;
550 const struct weston_option options[] = {
551 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
552 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
553 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
554 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
555 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
558 parse_options(options, ARRAY_LENGTH(options), argc, argv);
559 config.use_pixman = use_pixman;
560 ivi->cmdline.use_current_mode = use_current_mode;
562 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
563 weston_config_section_get_string(section, "gbm-format",
564 &config.gbm_format, NULL);
565 weston_config_section_get_uint(section, "pageflip-timeout",
566 &config.pageflip_timeout, 0);
567 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
568 config.use_pixman_shadow = use_shadow;
570 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
575 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
577 weston_log("Cannot use drm output api.\n");
583 free(config.gbm_format);
584 free(config.seat_id);
589 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
590 bool *use_pixman, bool *fullscreen, int *output_count)
592 struct weston_config_section *section;
596 const struct weston_option options[] = {
597 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
598 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
599 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
600 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
601 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
602 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
605 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
606 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
609 parse_options(options, ARRAY_LENGTH(options), argc, argv);
610 *use_pixman = pixman;
615 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
616 const char *match_prefix, const char *name_prefix)
618 struct weston_config_section *section = NULL;
619 const char *section_name;
620 char *default_output = NULL;
622 size_t match_len = strlen(match_prefix);
624 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
627 if (i >= output_count)
630 if (strcmp(section_name, "output") != 0)
633 weston_config_section_get_string(section, "name", &output_name, NULL);
634 if (output_name == NULL)
636 if (strncmp(output_name, match_prefix, match_len) != 0) {
641 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
650 for (; i < output_count; ++i) {
651 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
654 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
655 free(default_output);
659 free(default_output);
666 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
668 struct weston_wayland_backend_config config = {
670 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
671 .struct_size = sizeof config,
674 struct weston_config_section *section;
679 const struct weston_option options[] = {
680 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
681 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
684 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
685 &config.fullscreen, &output_count);
687 parse_options(options, ARRAY_LENGTH(options), argc, argv);
688 config.sprawl = sprawl;
690 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
691 weston_config_section_get_string(section, "cursor-theme",
692 &config.cursor_theme, NULL);
693 weston_config_section_get_int(section, "cursor-size",
694 &config.cursor_size, 32);
696 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
699 free(config.cursor_theme);
700 free(config.display_name);
705 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
708 * We will just assume if load_backend() finished cleanly and
709 * windowed_output_api is not present that wayland backend is started
710 * with --sprawl or runs on fullscreen-shell. In this case, all values
711 * are hardcoded, so nothing can be configured; simply create and
714 if (ivi->window_api == NULL)
717 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
720 #ifdef HAVE_BACKEND_X11
722 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
724 struct weston_x11_backend_config config = {
726 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
727 .struct_size = sizeof config,
734 const struct weston_option options[] = {
735 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
738 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
739 &config.fullscreen, &output_count);
741 parse_options(options, ARRAY_LENGTH(options), argc, argv);
742 config.no_input = no_input;
744 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
750 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
751 if (!ivi->window_api) {
752 weston_log("Cannot use weston_windowed_output_api.\n");
756 return windowed_create_outputs(ivi, output_count, "X", "screen");
760 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
767 load_backend(struct ivi_compositor *ivi, const char *backend,
768 int *argc, char *argv[])
770 if (strcmp(backend, "drm-backend.so") == 0) {
771 return load_drm_backend(ivi, argc, argv);
772 } else if (strcmp(backend, "wayland-backend.so") == 0) {
773 return load_wayland_backend(ivi, argc, argv);
774 } else if (strcmp(backend, "x11-backend.so") == 0) {
775 return load_x11_backend(ivi, argc, argv);
778 weston_log("fatal: unknown backend '%s'.\n", backend);
783 choose_default_backend(void)
785 char *backend = NULL;
787 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
788 backend = strdup("wayland-backend.so");
789 else if (getenv("DISPLAY"))
790 backend = strdup("x11-backend.so");
792 backend = strdup("drm-backend.so");
798 compositor_init_config(struct weston_compositor *compositor,
799 struct weston_config *config)
801 struct xkb_rule_names xkb_names;
802 struct weston_config_section *section;
807 /* agl-compositor.ini [keyboard] */
808 section = weston_config_get_section(config, "keyboard", NULL, NULL);
809 weston_config_section_get_string(section, "keymap_rules",
810 (char **) &xkb_names.rules, NULL);
811 weston_config_section_get_string(section, "keymap_model",
812 (char **) &xkb_names.model, NULL);
813 weston_config_section_get_string(section, "keymap_layout",
814 (char **) &xkb_names.layout, NULL);
815 weston_config_section_get_string(section, "keymap_variant",
816 (char **) &xkb_names.variant, NULL);
817 weston_config_section_get_string(section, "keymap_options",
818 (char **) &xkb_names.options, NULL);
820 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
823 weston_config_section_get_int(section, "repeat-rate",
824 &compositor->kb_repeat_rate, 40);
825 weston_config_section_get_int(section, "repeat-delay",
826 &compositor->kb_repeat_delay, 400);
828 weston_config_section_get_bool(section, "vt-switching",
829 &vt_switching, true);
830 compositor->vt_switching = vt_switching;
832 /* agl-compositor.ini [core] */
833 section = weston_config_get_section(config, "core", NULL, NULL);
835 weston_config_section_get_bool(section, "require-input", &require_input, true);
836 compositor->require_input = require_input;
838 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
839 compositor->repaint_msec);
840 if (repaint_msec < -10 || repaint_msec > 1000) {
841 weston_log("Invalid repaint_window value in config: %d\n",
844 compositor->repaint_msec = repaint_msec;
846 weston_log("Output repaint window is %d ms maximum.\n",
847 compositor->repaint_msec);
853 to_ivi_surface(struct weston_surface *surface)
855 struct weston_desktop_surface *dsurface;
857 dsurface = weston_surface_get_desktop_surface(surface);
861 return weston_desktop_surface_get_user_data(dsurface);
865 activate_binding(struct weston_seat *seat,
866 struct weston_view *focus_view)
868 struct weston_surface *focus = focus_view->surface;
869 struct weston_surface *main_surface =
870 weston_surface_get_main_surface(focus);
871 struct ivi_surface *surface;
873 surface = to_ivi_surface(main_surface);
877 weston_seat_set_keyboard_focus(seat, focus);
881 click_to_activate_binding(struct weston_pointer *pointer,
882 const struct timespec *time,
883 uint32_t button, void *data)
885 if (pointer->grab != &pointer->default_grab)
887 if (pointer->focus == NULL)
890 activate_binding(pointer->seat, pointer->focus);
894 touch_to_activate_binding(struct weston_touch *touch,
895 const struct timespec *time,
898 if (touch->grab != &touch->default_grab)
900 if (touch->focus == NULL)
903 activate_binding(touch->seat, touch->focus);
907 add_bindings(struct weston_compositor *compositor)
909 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
910 click_to_activate_binding,
912 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
913 click_to_activate_binding,
915 weston_compositor_add_touch_binding(compositor, 0,
916 touch_to_activate_binding,
921 create_listening_socket(struct wl_display *display, const char *socket_name)
924 if (wl_display_add_socket(display, socket_name)) {
925 weston_log("fatal: failed to add socket: %s\n",
930 socket_name = wl_display_add_socket_auto(display);
932 weston_log("fatal: failed to add socket: %s\n",
938 setenv("WAYLAND_DISPLAY", socket_name, 1);
944 global_filter(const struct wl_client *client, const struct wl_global *global,
951 load_config(struct weston_config **config, bool no_config,
952 const char *config_file)
954 const char *file = "agl-compositor.ini";
955 const char *full_path;
961 *config = weston_config_parse(file);
964 full_path = weston_config_get_full_path(*config);
966 weston_log("Using config file '%s'.\n", full_path);
967 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
972 if (config_file && !no_config) {
973 weston_log("fatal: error opening or reading config file '%s'.\n",
979 weston_log("Starting with no config file.\n");
980 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
985 static FILE *logfile;
988 log_timestamp(char *buf, size_t len)
991 struct tm *brokendown_time;
995 gettimeofday(&tv, NULL);
997 brokendown_time = localtime(&tv.tv_sec);
998 if (brokendown_time == NULL) {
999 snprintf(buf, len, "%s", "[(NULL)localtime] ");
1003 memset(datestr, 0, sizeof(datestr));
1004 if (brokendown_time->tm_mday != cached_tm_mday) {
1005 strftime(datestr, sizeof(datestr), "Date: %Y-%m-%d %Z\n",
1007 cached_tm_mday = brokendown_time->tm_mday;
1010 strftime(timestr, sizeof(timestr), "%H:%M:%S", brokendown_time);
1011 /* if datestr is empty it prints only timestr*/
1012 snprintf(buf, len, "%s[%s.%03li]", datestr,
1013 timestr, (tv.tv_usec / 1000));
1019 custom_handler(const char *fmt, va_list arg)
1023 weston_log_scope_printf(log_scope, "%s libwayland: ",
1024 log_timestamp(timestr, sizeof(timestr)));
1025 weston_log_scope_vprintf(log_scope, fmt, arg);
1029 log_file_open(const char *filename)
1031 wl_log_set_handler_server(custom_handler);
1034 logfile = fopen(filename, "a");
1039 os_fd_set_cloexec(fileno(logfile));
1040 setvbuf(logfile, NULL, _IOLBF, 256);
1045 log_file_close(void)
1047 if (logfile && logfile != stderr)
1053 vlog(const char *fmt, va_list ap)
1055 const char *oom = "Out of memory";
1060 if (weston_log_scope_is_enabled(log_scope)) {
1062 char *xlog_timestamp = log_timestamp(timestr, sizeof(timestr));
1063 len_va = vasprintf(&str, fmt, ap);
1065 len = weston_log_scope_printf(log_scope, "%s %s",
1066 xlog_timestamp, str);
1069 len = weston_log_scope_printf(log_scope, "%s %s",
1070 xlog_timestamp, oom);
1079 vlog_continue(const char *fmt, va_list ap)
1081 return weston_log_scope_vprintf(log_scope, fmt, ap);
1085 on_term_signal(int signo, void *data)
1087 struct wl_display *display = data;
1089 weston_log("caught signal %d\n", signo);
1090 wl_display_terminate(display);
1096 handle_exit(struct weston_compositor *compositor)
1098 wl_display_terminate(compositor->wl_display);
1102 usage(int error_code)
1104 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1106 "Usage: agl-compositor [OPTIONS]\n"
1108 "This is " PACKAGE_STRING ", the reference compositor for\n"
1109 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1110 "on which backend is in use different options will be accepted.\n"
1114 " --version\t\tPrint agl-compositor version\n"
1115 " -B, --backend=MODULE\tBackend module, one of\n"
1116 "\t\t\t\tdrm-backend.so\n"
1117 "\t\t\t\twayland-backend.so\n"
1118 "\t\t\t\tx11-backend.so\n"
1119 " -S, --socket=NAME\tName of socket to listen on\n"
1120 " --log=FILE\t\tLog to the given file\n"
1121 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1122 " --no-config\t\tDo not read agl-compositor.ini\n"
1123 " --debug\t\tEnable debug extension\n"
1124 " -h, --help\t\tThis help message\n"
1130 ivi_compositor_get_quirks(struct ivi_compositor *ivi)
1132 struct weston_config_section *section;
1137 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1138 weston_config_section_get_bool(section, "activate-by-default",
1139 &ivi->quirks.activate_apps_by_default, 0);
1143 int main(int argc, char *argv[])
1145 struct ivi_compositor ivi = { 0 };
1146 struct wl_display *display = NULL;
1147 struct wl_event_loop *loop;
1148 struct wl_event_source *signals[3] = { 0 };
1149 struct weston_config_section *section;
1150 /* Command line options */
1151 char *backend = NULL;
1152 char *socket_name = NULL;
1157 char *config_file = NULL;
1158 struct weston_log_context *log_ctx = NULL;
1159 struct weston_log_subscriber *logger;
1160 int ret = EXIT_FAILURE;
1162 const struct weston_option core_options[] = {
1163 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1164 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1165 { WESTON_OPTION_STRING, "log", 0, &log },
1166 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1167 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1168 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1169 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1172 wl_list_init(&ivi.outputs);
1173 wl_list_init(&ivi.surfaces);
1174 wl_list_init(&ivi.pending_surfaces);
1175 wl_list_init(&ivi.popup_pending_apps);
1176 wl_list_init(&ivi.fullscreen_pending_apps);
1177 wl_list_init(&ivi.split_pending_apps);
1178 wl_list_init(&ivi.desktop_clients);
1180 /* Prevent any clients we spawn getting our stdin */
1181 os_fd_set_cloexec(STDIN_FILENO);
1183 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1186 usage(EXIT_SUCCESS);
1189 printf(PACKAGE_STRING "\n");
1190 return EXIT_SUCCESS;
1193 log_ctx = weston_log_ctx_compositor_create();
1195 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1199 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1200 "agl-compositor log\n",
1204 weston_log_set_handler(vlog, vlog_continue);
1206 logger = weston_log_subscriber_create_log(logfile);
1207 weston_log_subscribe(log_ctx, logger, "log");
1209 if (load_config(&ivi.config, no_config, config_file) < 0)
1211 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1213 weston_config_section_get_string(section, "backend", &backend,
1216 backend = choose_default_backend();
1219 ivi_compositor_get_quirks(&ivi);
1221 display = wl_display_create();
1222 loop = wl_display_get_event_loop(display);
1224 wl_display_set_global_filter(display,
1225 global_filter, &ivi);
1227 /* Register signal handlers so we shut down cleanly */
1229 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1231 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1233 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1236 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1240 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1241 if (!ivi.compositor) {
1242 weston_log("fatal: failed to create compositor.\n");
1246 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1247 goto error_compositor;
1249 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1250 weston_log("fatal: failed to create compositor backend.\n");
1251 goto error_compositor;
1254 ivi.heads_changed.notify = heads_changed;
1255 weston_compositor_add_heads_changed_listener(ivi.compositor,
1256 &ivi.heads_changed);
1258 if (ivi_desktop_init(&ivi) < 0)
1259 goto error_compositor;
1261 if (ivi_policy_init(&ivi) < 0)
1262 goto error_compositor;
1264 if (ivi_shell_init(&ivi) < 0)
1265 goto error_compositor;
1267 add_bindings(ivi.compositor);
1269 weston_compositor_flush_heads_changed(ivi.compositor);
1271 ivi_shell_init_black_fs(&ivi);
1273 if (create_listening_socket(display, socket_name) < 0)
1274 goto error_compositor;
1276 ivi.compositor->exit = handle_exit;
1278 weston_compositor_wake(ivi.compositor);
1280 ivi_shell_create_global(&ivi);
1281 ivi_launch_shell_client(&ivi);
1282 ivi_agl_systemd_notify(&ivi);
1284 wl_display_run(display);
1286 ret = ivi.compositor->exit_code;
1288 wl_display_destroy_clients(display);
1291 weston_compositor_tear_down(ivi.compositor);
1293 weston_compositor_log_scope_destroy(log_scope);
1296 weston_log_ctx_compositor_destroy(ivi.compositor);
1297 weston_compositor_destroy(ivi.compositor);
1299 weston_log_subscriber_destroy_log(logger);
1301 ivi_policy_destroy(ivi.policy);
1304 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1306 wl_event_source_remove(signals[i]);
1308 wl_display_destroy(display);
1312 weston_config_destroy(ivi.config);