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"
50 #include "shared/helpers.h"
52 #include "agl-shell-server-protocol.h"
54 struct ivi_compositor *
55 to_ivi_compositor(struct weston_compositor *ec)
57 return weston_compositor_get_user_data(ec);
61 handle_output_destroy(struct wl_listener *listener, void *data)
63 struct ivi_output *output;
65 output = wl_container_of(listener, output, output_destroy);
66 assert(output->output == data);
68 output->output = NULL;
69 wl_list_remove(&output->output_destroy.link);
73 to_ivi_output(struct weston_output *o)
75 struct wl_listener *listener;
76 struct ivi_output *output;
78 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
79 output = wl_container_of(listener, output, output_destroy);
84 static struct ivi_output *
85 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
86 struct weston_config_section *config)
88 struct ivi_output *output = NULL;
89 wl_list_for_each(output, &ivi->outputs, link) {
90 if (strcmp(output->name, name) == 0) {
96 output = zalloc(sizeof *output);
104 output->config = config;
106 output->output = weston_compositor_create_output(ivi->compositor, name);
107 if (!output->output) {
113 output->output_destroy.notify = handle_output_destroy;
114 weston_output_add_destroy_listener(output->output,
115 &output->output_destroy);
117 wl_list_insert(&ivi->outputs, &output->link);
122 count_heads(struct weston_output *output)
124 struct weston_head *iter = NULL;
127 while ((iter = weston_output_iterate_heads(output, iter)))
134 handle_head_destroy(struct wl_listener *listener, void *data)
136 struct weston_head *head = data;
137 struct weston_output *output;
139 wl_list_remove(&listener->link);
142 output = weston_head_get_output(head);
144 /* On shutdown path, the output might be already gone. */
148 /* We're the last head */
149 if (count_heads(output) <= 1)
150 weston_output_destroy(output);
154 add_head_destroyed_listener(struct weston_head *head)
156 /* We already have a destroy listener */
157 if (weston_head_get_destroy_listener(head, handle_head_destroy))
160 struct wl_listener *listener = zalloc(sizeof *listener);
164 listener->notify = handle_head_destroy;
165 weston_head_add_destroy_listener(head, listener);
169 drm_configure_output(struct ivi_output *output)
171 struct ivi_compositor *ivi = output->ivi;
172 struct weston_config_section *section = output->config;
173 enum weston_drm_backend_output_mode mode =
174 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
175 char *modeline = NULL;
176 char *gbm_format = NULL;
181 weston_config_section_get_string(section, "mode", &m, "preferred");
183 /* This should have been handled earlier */
184 assert(strcmp(m, "off") != 0);
186 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
187 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
188 } else if (strcmp(m, "preferred") != 0) {
194 weston_config_section_get_string(section, "gbm-format",
197 weston_config_section_get_string(section, "seat", &seat, "");
200 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
201 weston_log("Cannot configure output using weston_drm_output_api.\n");
207 ivi->drm_api->set_gbm_format(output->output, gbm_format);
210 ivi->drm_api->set_seat(output->output, seat);
216 #define WINDOWED_DEFAULT_WIDTH 1024
217 #define WINDOWED_DEFAULT_HEIGHT 768
220 windowed_configure_output(struct ivi_output *output)
222 struct ivi_compositor *ivi = output->ivi;
223 struct weston_config_section *section = output->config;
224 int width = WINDOWED_DEFAULT_WIDTH;
225 int height = WINDOWED_DEFAULT_HEIGHT;
230 weston_config_section_get_string(section, "mode", &mode, NULL);
231 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
232 weston_log("Invalid mode for output %s. Using defaults.\n",
234 width = WINDOWED_DEFAULT_WIDTH;
235 height = WINDOWED_DEFAULT_HEIGHT;
240 if (ivi->cmdline.width)
241 width = ivi->cmdline.width;
242 if (ivi->cmdline.height)
243 height = ivi->cmdline.height;
244 if (ivi->cmdline.scale)
245 weston_output_set_scale(output->output, ivi->cmdline.scale);
247 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
248 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
257 parse_transform(const char *transform, uint32_t *out)
259 static const struct { const char *name; uint32_t token; } transforms[] = {
260 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
261 { "90", WL_OUTPUT_TRANSFORM_90 },
262 { "180", WL_OUTPUT_TRANSFORM_180 },
263 { "270", WL_OUTPUT_TRANSFORM_270 },
264 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
265 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
266 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
267 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
270 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
271 if (strcmp(transforms[i].name, transform) == 0) {
272 *out = transforms[i].token;
276 *out = WL_OUTPUT_TRANSFORM_NORMAL;
281 configure_output(struct ivi_output *output)
283 struct ivi_compositor *ivi = output->ivi;
284 struct weston_config_section *section = output->config;
286 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
289 * This can happen with the wayland backend with 'sprawl'. The config
290 * is hard-coded, so we don't need to do anything.
292 if (!ivi->drm_api && !ivi->window_api)
298 weston_config_section_get_int(section, "scale", &scale, 1);
299 weston_config_section_get_string(section, "transform", &t, "normal");
300 if (parse_transform(t, &transform) < 0)
301 weston_log("Invalid transform \"%s\" for output %s\n",
306 weston_output_set_scale(output->output, scale);
307 weston_output_set_transform(output->output, transform);
310 return drm_configure_output(output);
312 return windowed_configure_output(output);
316 * Reorgainizes the output's add array into two sections.
317 * add[0..ret-1] are the heads that failed to get attached.
318 * add[ret..add_len] are the heads that were successfully attached.
320 * The order between elements in each section is stable.
323 try_attach_heads(struct ivi_output *output)
327 for (size_t i = 0; i < output->add_len; ++i) {
328 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
329 struct weston_head *tmp = output->add[i];
330 memmove(&output->add[fail_len + 1], output->add[fail_len],
331 sizeof output->add[0] * (i - fail_len));
332 output->add[fail_len++] = tmp;
340 * Like try_attach_heads, this reorganizes the output's add array into a failed
341 * and successful section.
342 * i is the number of heads that already failed the previous step.
345 try_enable_output(struct ivi_output *output, size_t i)
347 for (; i < output->add_len; ++i) {
348 struct weston_head *head;
350 if (weston_output_enable(output->output) == 0)
353 head = output->add[output->add_len - 1];
354 memmove(&output->add[i + 1], &output->add[i],
355 sizeof output->add[0] * (output->add_len - i));
356 output->add[i] = head;
358 weston_head_detach(head);
365 try_attach_enable_heads(struct ivi_output *output)
368 assert(!output->output->enabled);
370 fail_len = try_attach_heads(output);
372 if (configure_output(output) < 0)
375 fail_len = try_enable_output(output, fail_len);
377 /* All heads failed to be attached */
378 if (fail_len == output->add_len)
381 /* For each successful head attached */
382 for (size_t i = fail_len; i < output->add_len; ++i)
383 add_head_destroyed_listener(output->add[i]);
385 output->add_len = fail_len;
390 process_output(struct ivi_output *output)
392 if (output->output->enabled) {
393 output->add_len = try_attach_heads(output);
394 return output->add_len == 0 ? 0 : -1;
397 return try_attach_enable_heads(output);
401 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
403 struct weston_output *output;
404 struct ivi_output *ivi_output;
405 struct wl_listener *listener;
407 output = weston_head_get_output(head);
410 listener = weston_output_get_destroy_listener(output,
411 handle_output_destroy);
414 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
415 assert(ivi_output->output == output);
417 weston_head_detach(head);
418 if (count_heads(ivi_output->output) == 0) {
419 weston_output_disable(ivi_output->output);
423 static struct weston_config_section *
424 find_controlling_output_config(struct weston_config *config,
427 struct weston_config_section *section;
431 same_as = strdup(name);
433 section = weston_config_get_section(config, "output",
435 if (!section && depth > 0)
436 weston_log("Configuration error: output section reffered"
437 "to by same-as=%s not found.\n", same_as);
444 weston_log("Configuration error: same-as nested too "
445 "deep for output '%s'.\n", name);
449 weston_config_section_get_string(section, "same-as",
457 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
459 const char *name = weston_head_get_name(head);
460 struct weston_config_section *section;
461 struct ivi_output *output;
462 char *output_name = NULL;
464 section = find_controlling_output_config(ivi->config, name);
468 weston_config_section_get_string(section, "mode", &mode, NULL);
469 if (mode && strcmp(mode, "off") == 0) {
475 weston_config_section_get_string(section, "name",
478 output_name = strdup(name);
484 output = ivi_ensure_output(ivi, output_name, section);
488 if (output->add_len >= ARRAY_LENGTH(output->add))
491 output->add[output->add_len++] = head;
495 heads_changed(struct wl_listener *listener, void *arg)
497 struct weston_compositor *compositor = arg;
498 struct weston_head *head = NULL;
499 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
500 struct ivi_output *output;
502 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
503 bool connected = weston_head_is_connected(head);
504 bool enabled = weston_head_is_enabled(head);
505 bool changed = weston_head_is_device_changed(head);
506 bool non_desktop = weston_head_is_non_desktop(head);
508 if (connected && !enabled && !non_desktop)
509 head_prepare_enable(ivi, head);
510 else if (!connected && enabled)
511 head_disable(ivi, head);
512 else if (enabled && changed)
513 weston_log("Detected a monitor change on head '%s', "
514 "not bothering to do anything about it.\n",
515 weston_head_get_name(head));
517 weston_head_reset_device_changed(head);
520 wl_list_for_each(output, &ivi->outputs, link) {
521 if (output->add_len == 0)
524 if (process_output(output) < 0) {
526 ivi->init_failed = true;
532 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
534 struct weston_drm_backend_config config = {
536 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
537 .struct_size = sizeof config,
540 struct weston_config_section *section;
541 int use_current_mode = 0;
546 const struct weston_option options[] = {
547 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
548 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
549 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
550 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
551 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
554 parse_options(options, ARRAY_LENGTH(options), argc, argv);
555 config.use_pixman = use_pixman;
556 ivi->cmdline.use_current_mode = use_current_mode;
558 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
559 weston_config_section_get_string(section, "gbm-format",
560 &config.gbm_format, NULL);
561 weston_config_section_get_uint(section, "pageflip-timeout",
562 &config.pageflip_timeout, 0);
563 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
564 config.use_pixman_shadow = use_shadow;
566 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
571 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
573 weston_log("Cannot use drm output api.\n");
579 free(config.gbm_format);
580 free(config.seat_id);
585 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
586 bool *use_pixman, bool *fullscreen, int *output_count)
588 struct weston_config_section *section;
592 const struct weston_option options[] = {
593 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
594 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
595 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
596 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
597 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
598 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
601 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
602 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
605 parse_options(options, ARRAY_LENGTH(options), argc, argv);
606 *use_pixman = pixman;
611 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
612 const char *match_prefix, const char *name_prefix)
614 struct weston_config_section *section = NULL;
615 const char *section_name;
616 char *default_output = NULL;
618 size_t match_len = strlen(match_prefix);
620 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
623 if (i >= output_count)
626 if (strcmp(section_name, "output") != 0)
629 weston_config_section_get_string(section, "name", &output_name, NULL);
630 if (output_name == NULL)
632 if (strncmp(output_name, match_prefix, match_len) != 0) {
637 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
646 for (; i < output_count; ++i) {
647 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
650 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
651 free(default_output);
655 free(default_output);
662 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
664 struct weston_wayland_backend_config config = {
666 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
667 .struct_size = sizeof config,
670 struct weston_config_section *section;
675 const struct weston_option options[] = {
676 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
677 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
680 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
681 &config.fullscreen, &output_count);
683 parse_options(options, ARRAY_LENGTH(options), argc, argv);
684 config.sprawl = sprawl;
686 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
687 weston_config_section_get_string(section, "cursor-theme",
688 &config.cursor_theme, NULL);
689 weston_config_section_get_int(section, "cursor-size",
690 &config.cursor_size, 32);
692 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
695 free(config.cursor_theme);
696 free(config.display_name);
701 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
704 * We will just assume if load_backend() finished cleanly and
705 * windowed_output_api is not present that wayland backend is started
706 * with --sprawl or runs on fullscreen-shell. In this case, all values
707 * are hardcoded, so nothing can be configured; simply create and
710 if (ivi->window_api == NULL)
713 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
716 #ifdef HAVE_BACKEND_X11
718 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
720 struct weston_x11_backend_config config = {
722 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
723 .struct_size = sizeof config,
730 const struct weston_option options[] = {
731 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
734 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
735 &config.fullscreen, &output_count);
737 parse_options(options, ARRAY_LENGTH(options), argc, argv);
738 config.no_input = no_input;
740 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
746 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
747 if (!ivi->window_api) {
748 weston_log("Cannot use weston_windowed_output_api.\n");
752 return windowed_create_outputs(ivi, output_count, "X", "screen");
756 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
763 load_backend(struct ivi_compositor *ivi, const char *backend,
764 int *argc, char *argv[])
766 if (strcmp(backend, "drm-backend.so") == 0) {
767 return load_drm_backend(ivi, argc, argv);
768 } else if (strcmp(backend, "wayland-backend.so") == 0) {
769 return load_wayland_backend(ivi, argc, argv);
770 } else if (strcmp(backend, "x11-backend.so") == 0) {
771 return load_x11_backend(ivi, argc, argv);
774 weston_log("fatal: unknown backend '%s'.\n", backend);
779 choose_default_backend(void)
781 char *backend = NULL;
783 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
784 backend = strdup("wayland-backend.so");
785 else if (getenv("DISPLAY"))
786 backend = strdup("x11-backend.so");
788 backend = strdup("drm-backend.so");
794 compositor_init_config(struct weston_compositor *compositor,
795 struct weston_config *config)
797 struct xkb_rule_names xkb_names;
798 struct weston_config_section *section;
803 /* agl-compositor.ini [keyboard] */
804 section = weston_config_get_section(config, "keyboard", NULL, NULL);
805 weston_config_section_get_string(section, "keymap_rules",
806 (char **) &xkb_names.rules, NULL);
807 weston_config_section_get_string(section, "keymap_model",
808 (char **) &xkb_names.model, NULL);
809 weston_config_section_get_string(section, "keymap_layout",
810 (char **) &xkb_names.layout, NULL);
811 weston_config_section_get_string(section, "keymap_variant",
812 (char **) &xkb_names.variant, NULL);
813 weston_config_section_get_string(section, "keymap_options",
814 (char **) &xkb_names.options, NULL);
816 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
819 weston_config_section_get_int(section, "repeat-rate",
820 &compositor->kb_repeat_rate, 40);
821 weston_config_section_get_int(section, "repeat-delay",
822 &compositor->kb_repeat_delay, 400);
824 weston_config_section_get_bool(section, "vt-switching",
825 &vt_switching, true);
826 compositor->vt_switching = vt_switching;
828 /* agl-compositor.ini [core] */
829 section = weston_config_get_section(config, "core", NULL, NULL);
831 weston_config_section_get_bool(section, "require-input", &require_input, true);
832 compositor->require_input = require_input;
834 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
835 compositor->repaint_msec);
836 if (repaint_msec < -10 || repaint_msec > 1000) {
837 weston_log("Invalid repaint_window value in config: %d\n",
840 compositor->repaint_msec = repaint_msec;
842 weston_log("Output repaint window is %d ms maximum.\n",
843 compositor->repaint_msec);
849 to_ivi_surface(struct weston_surface *surface)
851 struct weston_desktop_surface *dsurface;
853 dsurface = weston_surface_get_desktop_surface(surface);
857 return weston_desktop_surface_get_user_data(dsurface);
861 activate_binding(struct weston_seat *seat,
862 struct weston_view *focus_view)
864 struct weston_surface *focus = focus_view->surface;
865 struct weston_surface *main_surface =
866 weston_surface_get_main_surface(focus);
867 struct ivi_surface *surface;
869 surface = to_ivi_surface(main_surface);
873 weston_seat_set_keyboard_focus(seat, focus);
877 click_to_activate_binding(struct weston_pointer *pointer,
878 const struct timespec *time,
879 uint32_t button, void *data)
881 if (pointer->grab != &pointer->default_grab)
883 if (pointer->focus == NULL)
886 activate_binding(pointer->seat, pointer->focus);
890 touch_to_activate_binding(struct weston_touch *touch,
891 const struct timespec *time,
894 if (touch->grab != &touch->default_grab)
896 if (touch->focus == NULL)
899 activate_binding(touch->seat, touch->focus);
903 add_bindings(struct weston_compositor *compositor)
905 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
906 click_to_activate_binding,
908 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
909 click_to_activate_binding,
911 weston_compositor_add_touch_binding(compositor, 0,
912 touch_to_activate_binding,
917 create_listening_socket(struct wl_display *display, const char *socket_name)
920 if (wl_display_add_socket(display, socket_name)) {
921 weston_log("fatal: failed to add socket: %s\n",
926 socket_name = wl_display_add_socket_auto(display);
928 weston_log("fatal: failed to add socket: %s\n",
934 setenv("WAYLAND_DISPLAY", socket_name, 1);
940 global_filter(const struct wl_client *client, const struct wl_global *global,
947 load_config(struct weston_config **config, bool no_config,
948 const char *config_file)
950 const char *file = "agl-compositor.ini";
951 const char *full_path;
957 *config = weston_config_parse(file);
960 full_path = weston_config_get_full_path(*config);
962 weston_log("Using config file '%s'.\n", full_path);
963 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
968 if (config_file && !no_config) {
969 weston_log("fatal: error opening or reading config file '%s'.\n",
975 weston_log("Starting with no config file.\n");
976 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
981 static FILE *logfile;
986 static int cached_tm_mday = -1;
988 struct tm brokendown_time;
991 clock_gettime(CLOCK_REALTIME, &ts);
992 if (!localtime_r(&ts.tv_sec, &brokendown_time))
993 return fprintf(logfile, "[(NULL)localtime] ");
995 if (brokendown_time.tm_mday != cached_tm_mday) {
996 strftime(buf, sizeof buf, "%Y-%m-%d %Z", &brokendown_time);
997 fprintf(logfile, "Date: %s\n", buf);
999 cached_tm_mday = brokendown_time.tm_mday;
1002 strftime(buf, sizeof buf, "%H:%M:%S", &brokendown_time);
1004 return fprintf(logfile, "[%s.%03ld] ", buf, ts.tv_nsec / 1000000);
1008 custom_handler(const char *fmt, va_list arg)
1011 fprintf(logfile, "libwayland: ");
1012 vfprintf(logfile, fmt, arg);
1016 log_file_open(const char *filename)
1018 wl_log_set_handler_server(custom_handler);
1021 logfile = fopen(filename, "a");
1026 os_fd_set_cloexec(fileno(logfile));
1027 setvbuf(logfile, NULL, _IOLBF, 256);
1032 log_file_close(void)
1034 if (logfile && logfile != stderr)
1040 vlog(const char *fmt, va_list ap)
1044 l = log_timestamp();
1045 l += vfprintf(logfile, fmt, ap);
1051 vlog_continue(const char *fmt, va_list ap)
1053 return vfprintf(logfile, fmt, ap);
1057 on_term_signal(int signo, void *data)
1059 struct wl_display *display = data;
1061 weston_log("caught signal %d\n", signo);
1062 wl_display_terminate(display);
1068 handle_exit(struct weston_compositor *compositor)
1070 wl_display_terminate(compositor->wl_display);
1074 usage(int error_code)
1076 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1078 "Usage: agl-compositor [OPTIONS]\n"
1080 "This is " PACKAGE_STRING ", the reference compositor for\n"
1081 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1082 "on which backend is in use different options will be accepted.\n"
1086 " --version\t\tPrint agl-compositor version\n"
1087 " -B, --backend=MODULE\tBackend module, one of\n"
1088 "\t\t\t\tdrm-backend.so\n"
1089 "\t\t\t\twayland-backend.so\n"
1090 "\t\t\t\tx11-backend.so\n"
1091 " -S, --socket=NAME\tName of socket to listen on\n"
1092 " --log=FILE\t\tLog to the given file\n"
1093 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1094 " --no-config\t\tDo not read agl-compositor.ini\n"
1095 " --debug\t\tEnable debug extension\n"
1096 " -h, --help\t\tThis help message\n"
1102 ivi_compositor_get_quirks(struct ivi_compositor *ivi)
1104 struct weston_config_section *section;
1109 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1110 weston_config_section_get_bool(section, "activate-by-default",
1111 &ivi->quirks.activate_apps_by_default, 0);
1115 int main(int argc, char *argv[])
1117 struct ivi_compositor ivi = { 0 };
1118 struct wl_display *display = NULL;
1119 struct wl_event_loop *loop;
1120 struct wl_event_source *signals[3] = { 0 };
1121 struct weston_config_section *section;
1122 /* Command line options */
1123 char *backend = NULL;
1124 char *socket_name = NULL;
1129 char *config_file = NULL;
1130 int debug_protocol = 0;
1131 struct weston_log_context *log_ctx = NULL;
1132 struct weston_log_scope *log_scope;
1133 struct weston_log_subscriber *logger;
1135 const struct weston_option core_options[] = {
1136 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1137 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1138 { WESTON_OPTION_STRING, "log", 0, &log },
1139 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1140 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1141 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1142 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1143 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug_protocol },
1146 wl_list_init(&ivi.outputs);
1147 wl_list_init(&ivi.surfaces);
1148 wl_list_init(&ivi.pending_surfaces);
1149 wl_list_init(&ivi.desktop_clients);
1151 /* Prevent any clients we spawn getting our stdin */
1152 os_fd_set_cloexec(STDIN_FILENO);
1154 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1157 usage(EXIT_SUCCESS);
1160 printf(PACKAGE_STRING "\n");
1161 return EXIT_SUCCESS;
1164 log_ctx = weston_log_ctx_compositor_create();
1166 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1167 return EXIT_FAILURE;
1170 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1171 "agl-compositor log\n",
1175 weston_log_set_handler(vlog, vlog_continue);
1177 logger = weston_log_subscriber_create_log(logfile);
1179 if (load_config(&ivi.config, no_config, config_file) < 0)
1181 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1183 weston_config_section_get_string(section, "backend", &backend,
1186 backend = choose_default_backend();
1189 ivi_compositor_get_quirks(&ivi);
1191 display = wl_display_create();
1192 loop = wl_display_get_event_loop(display);
1194 wl_display_set_global_filter(display,
1195 global_filter, &ivi);
1197 /* Register signal handlers so we shut down cleanly */
1199 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1201 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1203 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1206 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1210 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1211 if (!ivi.compositor) {
1212 weston_log("fatal: failed to create compositor.\n");
1216 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1217 goto error_compositor;
1219 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1220 weston_log("fatal: failed to create compositor backend.\n");
1221 goto error_compositor;
1224 ivi.heads_changed.notify = heads_changed;
1225 weston_compositor_add_heads_changed_listener(ivi.compositor,
1226 &ivi.heads_changed);
1228 if (ivi_desktop_init(&ivi) < 0)
1229 goto error_compositor;
1231 if (ivi_shell_init(&ivi) < 0)
1232 goto error_compositor;
1234 add_bindings(ivi.compositor);
1236 weston_compositor_flush_heads_changed(ivi.compositor);
1238 ivi_shell_init_black_fs(&ivi);
1240 if (create_listening_socket(display, socket_name) < 0)
1241 goto error_compositor;
1243 ivi.compositor->exit = handle_exit;
1245 weston_compositor_wake(ivi.compositor);
1247 ivi_shell_create_global(&ivi);
1248 ivi_launch_shell_client(&ivi);
1249 ivi_agl_systemd_notify(&ivi);
1251 wl_display_run(display);
1253 wl_display_destroy_clients(display);
1256 weston_compositor_tear_down(ivi.compositor);
1258 weston_compositor_log_scope_destroy(log_scope);
1261 weston_log_ctx_compositor_destroy(ivi.compositor);
1262 weston_compositor_destroy(ivi.compositor);
1264 weston_log_subscriber_destroy_log(logger);
1267 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1269 wl_event_source_remove(signals[i]);
1271 wl_display_destroy(display);
1275 weston_config_destroy(ivi.config);