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"
37 #include <linux/input.h>
39 #include <libweston/backend-drm.h>
40 #include <libweston/backend-wayland.h>
41 #ifdef HAVE_BACKEND_X11
42 #include <libweston/backend-x11.h>
44 #include <libweston/libweston.h>
45 #include <libweston/windowed-output-api.h>
46 #include <libweston/config-parser.h>
47 #include <libweston/weston-log.h>
49 #include "shared/os-compatibility.h"
51 #include "agl-shell-server-protocol.h"
53 struct ivi_compositor *
54 to_ivi_compositor(struct weston_compositor *ec)
56 return weston_compositor_get_user_data(ec);
60 handle_output_destroy(struct wl_listener *listener, void *data)
62 struct ivi_output *output;
64 output = wl_container_of(listener, output, output_destroy);
65 assert(output->output == data);
67 output->output = NULL;
68 wl_list_remove(&output->output_destroy.link);
72 to_ivi_output(struct weston_output *o)
74 struct wl_listener *listener;
75 struct ivi_output *output;
77 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
78 output = wl_container_of(listener, output, output_destroy);
83 static struct ivi_output *
84 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
85 struct weston_config_section *config)
87 struct ivi_output *output = NULL;
88 wl_list_for_each(output, &ivi->outputs, link) {
89 if (strcmp(output->name, name) == 0) {
95 output = zalloc(sizeof *output);
103 output->config = config;
105 output->output = weston_compositor_create_output(ivi->compositor, name);
106 if (!output->output) {
112 output->output_destroy.notify = handle_output_destroy;
113 weston_output_add_destroy_listener(output->output,
114 &output->output_destroy);
116 wl_list_insert(&ivi->outputs, &output->link);
121 count_heads(struct weston_output *output)
123 struct weston_head *iter = NULL;
126 while ((iter = weston_output_iterate_heads(output, iter)))
133 handle_head_destroy(struct wl_listener *listener, void *data)
135 struct weston_head *head = data;
136 struct weston_output *output;
138 wl_list_remove(&listener->link);
141 output = weston_head_get_output(head);
143 /* On shutdown path, the output might be already gone. */
147 /* We're the last head */
148 if (count_heads(output) <= 1)
149 weston_output_destroy(output);
153 add_head_destroyed_listener(struct weston_head *head)
155 /* We already have a destroy listener */
156 if (weston_head_get_destroy_listener(head, handle_head_destroy))
159 struct wl_listener *listener = zalloc(sizeof *listener);
163 listener->notify = handle_head_destroy;
164 weston_head_add_destroy_listener(head, listener);
168 drm_configure_output(struct ivi_output *output)
170 struct ivi_compositor *ivi = output->ivi;
171 struct weston_config_section *section = output->config;
172 enum weston_drm_backend_output_mode mode =
173 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
174 char *modeline = NULL;
175 char *gbm_format = NULL;
180 weston_config_section_get_string(section, "mode", &m, "preferred");
182 /* This should have been handled earlier */
183 assert(strcmp(m, "off") != 0);
185 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
186 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
187 } else if (strcmp(m, "preferred") != 0) {
193 weston_config_section_get_string(section, "gbm-format",
196 weston_config_section_get_string(section, "seat", &seat, "");
199 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
200 weston_log("Cannot configure output using weston_drm_output_api.\n");
206 ivi->drm_api->set_gbm_format(output->output, gbm_format);
209 ivi->drm_api->set_seat(output->output, seat);
215 #define WINDOWED_DEFAULT_WIDTH 1024
216 #define WINDOWED_DEFAULT_HEIGHT 768
219 windowed_configure_output(struct ivi_output *output)
221 struct ivi_compositor *ivi = output->ivi;
222 struct weston_config_section *section = output->config;
223 int width = WINDOWED_DEFAULT_WIDTH;
224 int height = WINDOWED_DEFAULT_HEIGHT;
229 weston_config_section_get_string(section, "mode", &mode, NULL);
230 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
231 weston_log("Invalid mode for output %s. Using defaults.\n",
233 width = WINDOWED_DEFAULT_WIDTH;
234 height = WINDOWED_DEFAULT_HEIGHT;
239 if (ivi->cmdline.width)
240 width = ivi->cmdline.width;
241 if (ivi->cmdline.height)
242 height = ivi->cmdline.height;
243 if (ivi->cmdline.scale)
244 weston_output_set_scale(output->output, ivi->cmdline.scale);
246 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
247 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
256 parse_transform(const char *transform, uint32_t *out)
258 static const struct { const char *name; uint32_t token; } transforms[] = {
259 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
260 { "90", WL_OUTPUT_TRANSFORM_90 },
261 { "180", WL_OUTPUT_TRANSFORM_180 },
262 { "270", WL_OUTPUT_TRANSFORM_270 },
263 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
264 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
265 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
266 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
269 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
270 if (strcmp(transforms[i].name, transform) == 0) {
271 *out = transforms[i].token;
275 *out = WL_OUTPUT_TRANSFORM_NORMAL;
280 configure_output(struct ivi_output *output)
282 struct ivi_compositor *ivi = output->ivi;
283 struct weston_config_section *section = output->config;
285 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
288 * This can happen with the wayland backend with 'sprawl'. The config
289 * is hard-coded, so we don't need to do anything.
291 if (!ivi->drm_api && !ivi->window_api)
297 weston_config_section_get_int(section, "scale", &scale, 1);
298 weston_config_section_get_string(section, "transform", &t, "normal");
299 if (parse_transform(t, &transform) < 0)
300 weston_log("Invalid transform \"%s\" for output %s\n",
305 weston_output_set_scale(output->output, scale);
306 weston_output_set_transform(output->output, transform);
309 return drm_configure_output(output);
311 return windowed_configure_output(output);
315 * Reorgainizes the output's add array into two sections.
316 * add[0..ret-1] are the heads that failed to get attached.
317 * add[ret..add_len] are the heads that were successfully attached.
319 * The order between elements in each section is stable.
322 try_attach_heads(struct ivi_output *output)
326 for (size_t i = 0; i < output->add_len; ++i) {
327 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
328 struct weston_head *tmp = output->add[i];
329 memmove(&output->add[fail_len + 1], output->add[fail_len],
330 sizeof output->add[0] * (i - fail_len));
331 output->add[fail_len++] = tmp;
339 * Like try_attach_heads, this reorganizes the output's add array into a failed
340 * and successful section.
341 * i is the number of heads that already failed the previous step.
344 try_enable_output(struct ivi_output *output, size_t i)
346 for (; i < output->add_len; ++i) {
347 struct weston_head *head;
349 if (weston_output_enable(output->output) == 0)
352 head = output->add[output->add_len - 1];
353 memmove(&output->add[i + 1], &output->add[i],
354 sizeof output->add[0] * (output->add_len - i));
355 output->add[i] = head;
357 weston_head_detach(head);
364 try_attach_enable_heads(struct ivi_output *output)
367 assert(!output->output->enabled);
369 fail_len = try_attach_heads(output);
371 if (configure_output(output) < 0)
374 fail_len = try_enable_output(output, fail_len);
376 /* All heads failed to be attached */
377 if (fail_len == output->add_len)
380 /* For each successful head attached */
381 for (size_t i = fail_len; i < output->add_len; ++i)
382 add_head_destroyed_listener(output->add[i]);
384 output->add_len = fail_len;
389 process_output(struct ivi_output *output)
391 if (output->output->enabled) {
392 output->add_len = try_attach_heads(output);
393 return output->add_len == 0 ? 0 : -1;
396 return try_attach_enable_heads(output);
400 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
402 struct weston_output *output;
403 struct ivi_output *ivi_output;
404 struct wl_listener *listener;
406 output = weston_head_get_output(head);
409 listener = weston_output_get_destroy_listener(output,
410 handle_output_destroy);
413 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
414 assert(ivi_output->output == output);
416 weston_head_detach(head);
417 if (count_heads(ivi_output->output) == 0) {
418 weston_output_disable(ivi_output->output);
422 static struct weston_config_section *
423 find_controlling_output_config(struct weston_config *config,
426 struct weston_config_section *section;
430 same_as = strdup(name);
432 section = weston_config_get_section(config, "output",
434 if (!section && depth > 0)
435 weston_log("Configuration error: output section reffered"
436 "to by same-as=%s not found.\n", same_as);
443 weston_log("Configuration error: same-as nested too "
444 "deep for output '%s'.\n", name);
448 weston_config_section_get_string(section, "same-as",
456 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
458 const char *name = weston_head_get_name(head);
459 struct weston_config_section *section;
460 struct ivi_output *output;
461 char *output_name = NULL;
463 section = find_controlling_output_config(ivi->config, name);
467 weston_config_section_get_string(section, "mode", &mode, NULL);
468 if (mode && strcmp(mode, "off") == 0) {
474 weston_config_section_get_string(section, "name",
477 output_name = strdup(name);
483 output = ivi_ensure_output(ivi, output_name, section);
487 if (output->add_len >= ARRAY_LENGTH(output->add))
490 output->add[output->add_len++] = head;
494 heads_changed(struct wl_listener *listener, void *arg)
496 struct weston_compositor *compositor = arg;
497 struct weston_head *head = NULL;
498 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
499 struct ivi_output *output;
501 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
502 bool connected = weston_head_is_connected(head);
503 bool enabled = weston_head_is_enabled(head);
504 bool changed = weston_head_is_device_changed(head);
505 bool non_desktop = weston_head_is_non_desktop(head);
507 if (connected && !enabled && !non_desktop)
508 head_prepare_enable(ivi, head);
509 else if (!connected && enabled)
510 head_disable(ivi, head);
511 else if (enabled && changed)
512 weston_log("Detected a monitor change on head '%s', "
513 "not bothering to do anything about it.\n",
514 weston_head_get_name(head));
516 weston_head_reset_device_changed(head);
519 wl_list_for_each(output, &ivi->outputs, link) {
520 if (output->add_len == 0)
523 if (process_output(output) < 0) {
525 ivi->init_failed = true;
531 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
533 struct weston_drm_backend_config config = {
535 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
536 .struct_size = sizeof config,
539 struct weston_config_section *section;
540 int use_current_mode = 0;
545 const struct weston_option options[] = {
546 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
547 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
548 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
549 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
550 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
553 parse_options(options, ARRAY_LENGTH(options), argc, argv);
554 config.use_pixman = use_pixman;
555 ivi->cmdline.use_current_mode = use_current_mode;
557 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
558 weston_config_section_get_string(section, "gbm-format",
559 &config.gbm_format, NULL);
560 weston_config_section_get_uint(section, "pageflip-timeout",
561 &config.pageflip_timeout, 0);
562 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
563 config.use_pixman_shadow = use_shadow;
565 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
570 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
572 weston_log("Cannot use drm output api.\n");
578 free(config.gbm_format);
579 free(config.seat_id);
584 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
585 bool *use_pixman, bool *fullscreen, int *output_count)
587 struct weston_config_section *section;
591 const struct weston_option options[] = {
592 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
593 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
594 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
595 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
596 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
597 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
600 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
601 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
604 parse_options(options, ARRAY_LENGTH(options), argc, argv);
605 *use_pixman = pixman;
610 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
611 const char *match_prefix, const char *name_prefix)
613 struct weston_config_section *section = NULL;
614 const char *section_name;
615 char *default_output = NULL;
617 size_t match_len = strlen(match_prefix);
619 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
622 if (i >= output_count)
625 if (strcmp(section_name, "output") != 0)
628 weston_config_section_get_string(section, "name", &output_name, NULL);
629 if (output_name == NULL)
631 if (strncmp(output_name, match_prefix, match_len) != 0) {
636 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
645 for (; i < output_count; ++i) {
646 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
649 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
650 free(default_output);
654 free(default_output);
661 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
663 struct weston_wayland_backend_config config = {
665 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
666 .struct_size = sizeof config,
669 struct weston_config_section *section;
674 const struct weston_option options[] = {
675 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
676 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
679 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
680 &config.fullscreen, &output_count);
682 parse_options(options, ARRAY_LENGTH(options), argc, argv);
683 config.sprawl = sprawl;
685 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
686 weston_config_section_get_string(section, "cursor-theme",
687 &config.cursor_theme, NULL);
688 weston_config_section_get_int(section, "cursor-size",
689 &config.cursor_size, 32);
691 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
694 free(config.cursor_theme);
695 free(config.display_name);
700 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
703 * We will just assume if load_backend() finished cleanly and
704 * windowed_output_api is not present that wayland backend is started
705 * with --sprawl or runs on fullscreen-shell. In this case, all values
706 * are hardcoded, so nothing can be configured; simply create and
709 if (ivi->window_api == NULL)
712 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
715 #ifdef HAVE_BACKEND_X11
717 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
719 struct weston_x11_backend_config config = {
721 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
722 .struct_size = sizeof config,
729 const struct weston_option options[] = {
730 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
733 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
734 &config.fullscreen, &output_count);
736 parse_options(options, ARRAY_LENGTH(options), argc, argv);
737 config.no_input = no_input;
739 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
745 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
746 if (!ivi->window_api) {
747 weston_log("Cannot use weston_windowed_output_api.\n");
751 return windowed_create_outputs(ivi, output_count, "X", "screen");
755 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
762 load_backend(struct ivi_compositor *ivi, const char *backend,
763 int *argc, char *argv[])
765 if (strcmp(backend, "drm-backend.so") == 0) {
766 return load_drm_backend(ivi, argc, argv);
767 } else if (strcmp(backend, "wayland-backend.so") == 0) {
768 return load_wayland_backend(ivi, argc, argv);
769 } else if (strcmp(backend, "x11-backend.so") == 0) {
770 return load_x11_backend(ivi, argc, argv);
773 weston_log("fatal: unknown backend '%s'.\n", backend);
778 choose_default_backend(void)
780 char *backend = NULL;
782 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
783 backend = strdup("wayland-backend.so");
784 else if (getenv("DISPLAY"))
785 backend = strdup("x11-backend.so");
787 backend = strdup("drm-backend.so");
793 compositor_init_config(struct weston_compositor *compositor,
794 struct weston_config *config)
796 struct xkb_rule_names xkb_names;
797 struct weston_config_section *section;
802 /* agl-compositor.ini [keyboard] */
803 section = weston_config_get_section(config, "keyboard", NULL, NULL);
804 weston_config_section_get_string(section, "keymap_rules",
805 (char **) &xkb_names.rules, NULL);
806 weston_config_section_get_string(section, "keymap_model",
807 (char **) &xkb_names.model, NULL);
808 weston_config_section_get_string(section, "keymap_layout",
809 (char **) &xkb_names.layout, NULL);
810 weston_config_section_get_string(section, "keymap_variant",
811 (char **) &xkb_names.variant, NULL);
812 weston_config_section_get_string(section, "keymap_options",
813 (char **) &xkb_names.options, NULL);
815 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
818 weston_config_section_get_int(section, "repeat-rate",
819 &compositor->kb_repeat_rate, 40);
820 weston_config_section_get_int(section, "repeat-delay",
821 &compositor->kb_repeat_delay, 400);
823 weston_config_section_get_bool(section, "vt-switching",
824 &vt_switching, true);
825 compositor->vt_switching = vt_switching;
827 /* agl-compositor.ini [core] */
828 section = weston_config_get_section(config, "core", NULL, NULL);
830 weston_config_section_get_bool(section, "require-input", &require_input, true);
831 compositor->require_input = require_input;
833 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
834 compositor->repaint_msec);
835 if (repaint_msec < -10 || repaint_msec > 1000) {
836 weston_log("Invalid repaint_window value in config: %d\n",
839 compositor->repaint_msec = repaint_msec;
841 weston_log("Output repaint window is %d ms maximum.\n",
842 compositor->repaint_msec);
848 to_ivi_surface(struct weston_surface *surface)
850 struct weston_desktop_surface *dsurface;
852 dsurface = weston_surface_get_desktop_surface(surface);
856 return weston_desktop_surface_get_user_data(dsurface);
860 activate_binding(struct weston_seat *seat,
861 struct weston_view *focus_view)
863 struct weston_surface *focus = focus_view->surface;
864 struct weston_surface *main_surface =
865 weston_surface_get_main_surface(focus);
866 struct ivi_surface *surface;
868 surface = to_ivi_surface(main_surface);
872 weston_seat_set_keyboard_focus(seat, focus);
876 click_to_activate_binding(struct weston_pointer *pointer,
877 const struct timespec *time,
878 uint32_t button, void *data)
880 if (pointer->grab != &pointer->default_grab)
882 if (pointer->focus == NULL)
885 activate_binding(pointer->seat, pointer->focus);
889 touch_to_activate_binding(struct weston_touch *touch,
890 const struct timespec *time,
893 if (touch->grab != &touch->default_grab)
895 if (touch->focus == NULL)
898 activate_binding(touch->seat, touch->focus);
902 add_bindings(struct weston_compositor *compositor)
904 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
905 click_to_activate_binding,
907 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
908 click_to_activate_binding,
910 weston_compositor_add_touch_binding(compositor, 0,
911 touch_to_activate_binding,
916 create_listening_socket(struct wl_display *display, const char *socket_name)
919 if (wl_display_add_socket(display, socket_name)) {
920 weston_log("fatal: failed to add socket: %s\n",
925 socket_name = wl_display_add_socket_auto(display);
927 weston_log("fatal: failed to add socket: %s\n",
933 setenv("WAYLAND_DISPLAY", socket_name, 1);
939 global_filter(const struct wl_client *client, const struct wl_global *global,
946 load_config(struct weston_config **config, bool no_config,
947 const char *config_file)
949 const char *file = "agl-compositor.ini";
950 const char *full_path;
956 *config = weston_config_parse(file);
959 full_path = weston_config_get_full_path(*config);
961 weston_log("Using config file '%s'.\n", full_path);
962 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
967 if (config_file && !no_config) {
968 weston_log("fatal: error opening or reading config file '%s'.\n",
974 weston_log("Starting with no config file.\n");
975 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
980 static FILE *logfile;
985 static int cached_tm_mday = -1;
987 struct tm brokendown_time;
990 clock_gettime(CLOCK_REALTIME, &ts);
991 if (!localtime_r(&ts.tv_sec, &brokendown_time))
992 return fprintf(logfile, "[(NULL)localtime] ");
994 if (brokendown_time.tm_mday != cached_tm_mday) {
995 strftime(buf, sizeof buf, "%Y-%m-%d %Z", &brokendown_time);
996 fprintf(logfile, "Date: %s\n", buf);
998 cached_tm_mday = brokendown_time.tm_mday;
1001 strftime(buf, sizeof buf, "%H:%M:%S", &brokendown_time);
1003 return fprintf(logfile, "[%s.%03ld] ", buf, ts.tv_nsec / 1000000);
1007 custom_handler(const char *fmt, va_list arg)
1010 fprintf(logfile, "libwayland: ");
1011 vfprintf(logfile, fmt, arg);
1015 log_file_open(const char *filename)
1017 wl_log_set_handler_server(custom_handler);
1020 logfile = fopen(filename, "a");
1025 os_fd_set_cloexec(fileno(logfile));
1026 setvbuf(logfile, NULL, _IOLBF, 256);
1031 log_file_close(void)
1033 if (logfile && logfile != stderr)
1039 vlog(const char *fmt, va_list ap)
1043 l = log_timestamp();
1044 l += vfprintf(logfile, fmt, ap);
1050 vlog_continue(const char *fmt, va_list ap)
1052 return vfprintf(logfile, fmt, ap);
1056 on_term_signal(int signo, void *data)
1058 struct wl_display *display = data;
1060 weston_log("caught signal %d\n", signo);
1061 wl_display_terminate(display);
1067 handle_exit(struct weston_compositor *compositor)
1069 wl_display_terminate(compositor->wl_display);
1073 usage(int error_code)
1075 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1077 "Usage: agl-compositor [OPTIONS]\n"
1079 "This is " PACKAGE_STRING ", the reference compositor for\n"
1080 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1081 "on which backend is in use different options will be accepted.\n"
1085 " --version\t\tPrint agl-compositor version\n"
1086 " -B, --backend=MODULE\tBackend module, one of\n"
1087 "\t\t\t\tdrm-backend.so\n"
1088 "\t\t\t\twayland-backend.so\n"
1089 "\t\t\t\tx11-backend.so\n"
1090 " -S, --socket=NAME\tName of socket to listen on\n"
1091 " --log=FILE\t\tLog to the given file\n"
1092 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1093 " --no-config\t\tDo not read agl-compositor.ini\n"
1094 " --debug\t\tEnable debug extension\n"
1095 " -h, --help\t\tThis help message\n"
1101 ivi_compositor_get_quirks(struct ivi_compositor *ivi)
1103 struct weston_config_section *section;
1108 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1109 weston_config_section_get_bool(section, "activate-by-default",
1110 &ivi->quirks.activate_apps_by_default, 0);
1114 int main(int argc, char *argv[])
1116 struct ivi_compositor ivi = { 0 };
1117 struct wl_display *display = NULL;
1118 struct wl_event_loop *loop;
1119 struct wl_event_source *signals[3] = { 0 };
1120 struct weston_config_section *section;
1121 /* Command line options */
1122 char *backend = NULL;
1123 char *socket_name = NULL;
1128 char *config_file = NULL;
1129 int debug_protocol = 0;
1130 struct weston_log_context *log_ctx = NULL;
1131 struct weston_log_scope *log_scope;
1132 struct weston_log_subscriber *logger;
1134 const struct weston_option core_options[] = {
1135 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1136 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1137 { WESTON_OPTION_STRING, "log", 0, &log },
1138 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1139 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1140 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1141 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1142 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug_protocol },
1145 wl_list_init(&ivi.outputs);
1146 wl_list_init(&ivi.surfaces);
1147 wl_list_init(&ivi.pending_surfaces);
1148 wl_list_init(&ivi.desktop_clients);
1150 /* Prevent any clients we spawn getting our stdin */
1151 os_fd_set_cloexec(STDIN_FILENO);
1153 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1156 usage(EXIT_SUCCESS);
1159 printf(PACKAGE_STRING "\n");
1160 return EXIT_SUCCESS;
1163 log_ctx = weston_log_ctx_compositor_create();
1165 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1166 return EXIT_FAILURE;
1169 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1170 "agl-compositor log\n",
1174 weston_log_set_handler(vlog, vlog_continue);
1176 logger = weston_log_subscriber_create_log(logfile);
1178 if (load_config(&ivi.config, no_config, config_file) < 0)
1180 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1182 weston_config_section_get_string(section, "backend", &backend,
1185 backend = choose_default_backend();
1188 ivi_compositor_get_quirks(&ivi);
1190 display = wl_display_create();
1191 loop = wl_display_get_event_loop(display);
1193 wl_display_set_global_filter(display,
1194 global_filter, &ivi);
1196 /* Register signal handlers so we shut down cleanly */
1198 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1200 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1202 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1205 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1209 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1210 if (!ivi.compositor) {
1211 weston_log("fatal: failed to create compositor.\n");
1215 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1216 goto error_compositor;
1218 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1219 weston_log("fatal: failed to create compositor backend.\n");
1220 goto error_compositor;
1223 ivi.heads_changed.notify = heads_changed;
1224 weston_compositor_add_heads_changed_listener(ivi.compositor,
1225 &ivi.heads_changed);
1227 if (ivi_desktop_init(&ivi) < 0)
1228 goto error_compositor;
1230 if (ivi_shell_init(&ivi) < 0)
1231 goto error_compositor;
1233 add_bindings(ivi.compositor);
1235 weston_compositor_flush_heads_changed(ivi.compositor);
1237 ivi_shell_init_black_fs(&ivi);
1239 if (create_listening_socket(display, socket_name) < 0)
1240 goto error_compositor;
1242 ivi.compositor->exit = handle_exit;
1244 weston_compositor_wake(ivi.compositor);
1246 ivi_shell_create_global(&ivi);
1247 ivi_launch_shell_client(&ivi);
1248 ivi_agl_systemd_notify(&ivi);
1250 wl_display_run(display);
1252 wl_display_destroy_clients(display);
1255 weston_compositor_tear_down(ivi.compositor);
1257 weston_compositor_log_scope_destroy(log_scope);
1260 weston_log_ctx_compositor_destroy(ivi.compositor);
1261 weston_compositor_destroy(ivi.compositor);
1263 weston_log_subscriber_destroy_log(logger);
1266 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1268 wl_event_source_remove(signals[i]);
1270 wl_display_destroy(display);
1274 weston_config_destroy(ivi.config);