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>
48 #include "shared/os-compatibility.h"
50 #include "agl-shell-server-protocol.h"
52 struct ivi_compositor *
53 to_ivi_compositor(struct weston_compositor *ec)
55 return weston_compositor_get_user_data(ec);
59 handle_output_destroy(struct wl_listener *listener, void *data)
61 struct ivi_output *output;
63 output = wl_container_of(listener, output, output_destroy);
64 assert(output->output == data);
66 output->output = NULL;
67 wl_list_remove(&output->output_destroy.link);
71 to_ivi_output(struct weston_output *o)
73 struct wl_listener *listener;
74 struct ivi_output *output;
76 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
77 output = wl_container_of(listener, output, output_destroy);
82 static struct ivi_output *
83 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
84 struct weston_config_section *config)
86 struct ivi_output *output = NULL;
87 wl_list_for_each(output, &ivi->outputs, link) {
88 if (strcmp(output->name, name) == 0) {
94 output = zalloc(sizeof *output);
102 output->config = config;
104 output->output = weston_compositor_create_output(ivi->compositor, name);
105 if (!output->output) {
111 output->output_destroy.notify = handle_output_destroy;
112 weston_output_add_destroy_listener(output->output,
113 &output->output_destroy);
115 wl_list_insert(&ivi->outputs, &output->link);
120 count_heads(struct weston_output *output)
122 struct weston_head *iter = NULL;
125 while ((iter = weston_output_iterate_heads(output, iter)))
132 handle_head_destroy(struct wl_listener *listener, void *data)
134 struct weston_head *head = data;
135 struct weston_output *output;
137 wl_list_remove(&listener->link);
140 output = weston_head_get_output(head);
142 /* On shutdown path, the output might be already gone. */
146 /* We're the last head */
147 if (count_heads(output) <= 1)
148 weston_output_destroy(output);
152 add_head_destroyed_listener(struct weston_head *head)
154 /* We already have a destroy listener */
155 if (weston_head_get_destroy_listener(head, handle_head_destroy))
158 struct wl_listener *listener = zalloc(sizeof *listener);
162 listener->notify = handle_head_destroy;
163 weston_head_add_destroy_listener(head, listener);
167 drm_configure_output(struct ivi_output *output)
169 struct ivi_compositor *ivi = output->ivi;
170 struct weston_config_section *section = output->config;
171 enum weston_drm_backend_output_mode mode =
172 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
173 char *modeline = NULL;
174 char *gbm_format = NULL;
179 weston_config_section_get_string(section, "mode", &m, "preferred");
181 /* This should have been handled earlier */
182 assert(strcmp(m, "off") != 0);
184 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
185 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
186 } else if (strcmp(m, "preferred") != 0) {
192 weston_config_section_get_string(section, "gbm-format",
195 weston_config_section_get_string(section, "seat", &seat, "");
198 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
199 weston_log("Cannot configure output using weston_drm_output_api.\n");
205 ivi->drm_api->set_gbm_format(output->output, gbm_format);
208 ivi->drm_api->set_seat(output->output, seat);
214 #define WINDOWED_DEFAULT_WIDTH 1024
215 #define WINDOWED_DEFAULT_HEIGHT 768
218 windowed_configure_output(struct ivi_output *output)
220 struct ivi_compositor *ivi = output->ivi;
221 struct weston_config_section *section = output->config;
222 int width = WINDOWED_DEFAULT_WIDTH;
223 int height = WINDOWED_DEFAULT_HEIGHT;
228 weston_config_section_get_string(section, "mode", &mode, NULL);
229 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
230 weston_log("Invalid mode for output %s. Using defaults.\n",
232 width = WINDOWED_DEFAULT_WIDTH;
233 height = WINDOWED_DEFAULT_HEIGHT;
238 if (ivi->cmdline.width)
239 width = ivi->cmdline.width;
240 if (ivi->cmdline.height)
241 height = ivi->cmdline.height;
242 if (ivi->cmdline.scale)
243 weston_output_set_scale(output->output, ivi->cmdline.scale);
245 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
246 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
255 parse_transform(const char *transform, uint32_t *out)
257 static const struct { const char *name; uint32_t token; } transforms[] = {
258 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
259 { "90", WL_OUTPUT_TRANSFORM_90 },
260 { "180", WL_OUTPUT_TRANSFORM_180 },
261 { "270", WL_OUTPUT_TRANSFORM_270 },
262 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
263 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
264 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
265 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
268 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
269 if (strcmp(transforms[i].name, transform) == 0) {
270 *out = transforms[i].token;
274 *out = WL_OUTPUT_TRANSFORM_NORMAL;
279 configure_output(struct ivi_output *output)
281 struct ivi_compositor *ivi = output->ivi;
282 struct weston_config_section *section = output->config;
284 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
287 * This can happen with the wayland backend with 'sprawl'. The config
288 * is hard-coded, so we don't need to do anything.
290 if (!ivi->drm_api && !ivi->window_api)
296 weston_config_section_get_int(section, "scale", &scale, 1);
297 weston_config_section_get_string(section, "transform", &t, "normal");
298 if (parse_transform(t, &transform) < 0)
299 weston_log("Invalid transform \"%s\" for output %s\n",
304 weston_output_set_scale(output->output, scale);
305 weston_output_set_transform(output->output, transform);
308 return drm_configure_output(output);
310 return windowed_configure_output(output);
314 * Reorgainizes the output's add array into two sections.
315 * add[0..ret-1] are the heads that failed to get attached.
316 * add[ret..add_len] are the heads that were successfully attached.
318 * The order between elements in each section is stable.
321 try_attach_heads(struct ivi_output *output)
325 for (size_t i = 0; i < output->add_len; ++i) {
326 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
327 struct weston_head *tmp = output->add[i];
328 memmove(&output->add[fail_len + 1], output->add[fail_len],
329 sizeof output->add[0] * (i - fail_len));
330 output->add[fail_len++] = tmp;
338 * Like try_attach_heads, this reorganizes the output's add array into a failed
339 * and successful section.
340 * i is the number of heads that already failed the previous step.
343 try_enable_output(struct ivi_output *output, size_t i)
345 for (; i < output->add_len; ++i) {
346 struct weston_head *head;
348 if (weston_output_enable(output->output) == 0)
351 head = output->add[output->add_len - 1];
352 memmove(&output->add[i + 1], &output->add[i],
353 sizeof output->add[0] * (output->add_len - i));
354 output->add[i] = head;
356 weston_head_detach(head);
363 try_attach_enable_heads(struct ivi_output *output)
366 assert(!output->output->enabled);
368 fail_len = try_attach_heads(output);
370 if (configure_output(output) < 0)
373 fail_len = try_enable_output(output, fail_len);
375 /* All heads failed to be attached */
376 if (fail_len == output->add_len)
379 /* For each successful head attached */
380 for (size_t i = fail_len; i < output->add_len; ++i)
381 add_head_destroyed_listener(output->add[i]);
383 output->add_len = fail_len;
388 process_output(struct ivi_output *output)
390 if (output->output->enabled) {
391 output->add_len = try_attach_heads(output);
392 return output->add_len == 0 ? 0 : -1;
395 return try_attach_enable_heads(output);
399 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
401 struct weston_output *output;
402 struct ivi_output *ivi_output;
403 struct wl_listener *listener;
405 output = weston_head_get_output(head);
408 listener = weston_output_get_destroy_listener(output,
409 handle_output_destroy);
412 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
413 assert(ivi_output->output == output);
415 weston_head_detach(head);
416 if (count_heads(ivi_output->output) == 0) {
417 weston_output_disable(ivi_output->output);
421 static struct weston_config_section *
422 find_controlling_output_config(struct weston_config *config,
425 struct weston_config_section *section;
429 same_as = strdup(name);
431 section = weston_config_get_section(config, "output",
433 if (!section && depth > 0)
434 weston_log("Configuration error: output section reffered"
435 "to by same-as=%s not found.\n", same_as);
442 weston_log("Configuration error: same-as nested too "
443 "deep for output '%s'.\n", name);
447 weston_config_section_get_string(section, "same-as",
455 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
457 const char *name = weston_head_get_name(head);
458 struct weston_config_section *section;
459 struct ivi_output *output;
460 char *output_name = NULL;
462 section = find_controlling_output_config(ivi->config, name);
466 weston_config_section_get_string(section, "mode", &mode, NULL);
467 if (mode && strcmp(mode, "off") == 0) {
473 weston_config_section_get_string(section, "name",
476 output_name = strdup(name);
482 output = ivi_ensure_output(ivi, output_name, section);
486 if (output->add_len >= ARRAY_LENGTH(output->add))
489 output->add[output->add_len++] = head;
493 heads_changed(struct wl_listener *listener, void *arg)
495 struct weston_compositor *compositor = arg;
496 struct weston_head *head = NULL;
497 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
498 struct ivi_output *output;
500 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
501 bool connected = weston_head_is_connected(head);
502 bool enabled = weston_head_is_enabled(head);
503 bool changed = weston_head_is_device_changed(head);
504 bool non_desktop = weston_head_is_non_desktop(head);
506 if (connected && !enabled && !non_desktop)
507 head_prepare_enable(ivi, head);
508 else if (!connected && enabled)
509 head_disable(ivi, head);
510 else if (enabled && changed)
511 weston_log("Detected a monitor change on head '%s', "
512 "not bothering to do anything about it.\n",
513 weston_head_get_name(head));
515 weston_head_reset_device_changed(head);
518 wl_list_for_each(output, &ivi->outputs, link) {
519 if (output->add_len == 0)
522 if (process_output(output) < 0) {
524 ivi->init_failed = true;
530 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
532 struct weston_drm_backend_config config = {
534 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
535 .struct_size = sizeof config,
538 struct weston_config_section *section;
539 int use_current_mode = 0;
544 const struct weston_option options[] = {
545 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
546 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
547 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
548 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
549 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
552 parse_options(options, ARRAY_LENGTH(options), argc, argv);
553 config.use_pixman = use_pixman;
554 ivi->cmdline.use_current_mode = use_current_mode;
556 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
557 weston_config_section_get_string(section, "gbm-format",
558 &config.gbm_format, NULL);
559 weston_config_section_get_uint(section, "pageflip-timeout",
560 &config.pageflip_timeout, 0);
561 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
562 config.use_pixman_shadow = use_shadow;
564 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
569 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
571 weston_log("Cannot use drm output api.\n");
577 free(config.gbm_format);
578 free(config.seat_id);
583 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
584 bool *use_pixman, bool *fullscreen, int *output_count)
586 struct weston_config_section *section;
590 const struct weston_option options[] = {
591 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
592 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
593 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
594 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
595 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
596 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
599 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
600 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
603 parse_options(options, ARRAY_LENGTH(options), argc, argv);
604 *use_pixman = pixman;
609 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
610 const char *match_prefix, const char *name_prefix)
612 struct weston_config_section *section = NULL;
613 const char *section_name;
614 char *default_output = NULL;
616 size_t match_len = strlen(match_prefix);
618 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
621 if (i >= output_count)
624 if (strcmp(section_name, "output") != 0)
627 weston_config_section_get_string(section, "name", &output_name, NULL);
628 if (output_name == NULL)
630 if (strncmp(output_name, match_prefix, match_len) != 0) {
635 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
644 for (; i < output_count; ++i) {
645 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
648 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
649 free(default_output);
653 free(default_output);
660 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
662 struct weston_wayland_backend_config config = {
664 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
665 .struct_size = sizeof config,
668 struct weston_config_section *section;
673 const struct weston_option options[] = {
674 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
675 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
678 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
679 &config.fullscreen, &output_count);
681 parse_options(options, ARRAY_LENGTH(options), argc, argv);
682 config.sprawl = sprawl;
684 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
685 weston_config_section_get_string(section, "cursor-theme",
686 &config.cursor_theme, NULL);
687 weston_config_section_get_int(section, "cursor-size",
688 &config.cursor_size, 32);
690 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
693 free(config.cursor_theme);
694 free(config.display_name);
699 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
702 * We will just assume if load_backend() finished cleanly and
703 * windowed_output_api is not present that wayland backend is started
704 * with --sprawl or runs on fullscreen-shell. In this case, all values
705 * are hardcoded, so nothing can be configured; simply create and
708 if (ivi->window_api == NULL)
711 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
714 #ifdef HAVE_BACKEND_X11
716 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
718 struct weston_x11_backend_config config = {
720 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
721 .struct_size = sizeof config,
728 const struct weston_option options[] = {
729 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
732 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
733 &config.fullscreen, &output_count);
735 parse_options(options, ARRAY_LENGTH(options), argc, argv);
736 config.no_input = no_input;
738 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
744 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
745 if (!ivi->window_api) {
746 weston_log("Cannot use weston_windowed_output_api.\n");
750 return windowed_create_outputs(ivi, output_count, "X", "screen");
754 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
761 load_backend(struct ivi_compositor *ivi, const char *backend,
762 int *argc, char *argv[])
764 if (strcmp(backend, "drm-backend.so") == 0) {
765 return load_drm_backend(ivi, argc, argv);
766 } else if (strcmp(backend, "wayland-backend.so") == 0) {
767 return load_wayland_backend(ivi, argc, argv);
768 } else if (strcmp(backend, "x11-backend.so") == 0) {
769 return load_x11_backend(ivi, argc, argv);
772 weston_log("fatal: unknown backend '%s'.\n", backend);
777 choose_default_backend(void)
779 char *backend = NULL;
781 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
782 backend = strdup("wayland-backend.so");
783 else if (getenv("DISPLAY"))
784 backend = strdup("x11-backend.so");
786 backend = strdup("drm-backend.so");
792 compositor_init_config(struct weston_compositor *compositor,
793 struct weston_config *config)
795 struct xkb_rule_names xkb_names;
796 struct weston_config_section *section;
801 /* agl-compositor.ini [keyboard] */
802 section = weston_config_get_section(config, "keyboard", NULL, NULL);
803 weston_config_section_get_string(section, "keymap_rules",
804 (char **) &xkb_names.rules, NULL);
805 weston_config_section_get_string(section, "keymap_model",
806 (char **) &xkb_names.model, NULL);
807 weston_config_section_get_string(section, "keymap_layout",
808 (char **) &xkb_names.layout, NULL);
809 weston_config_section_get_string(section, "keymap_variant",
810 (char **) &xkb_names.variant, NULL);
811 weston_config_section_get_string(section, "keymap_options",
812 (char **) &xkb_names.options, NULL);
814 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
817 weston_config_section_get_int(section, "repeat-rate",
818 &compositor->kb_repeat_rate, 40);
819 weston_config_section_get_int(section, "repeat-delay",
820 &compositor->kb_repeat_delay, 400);
822 weston_config_section_get_bool(section, "vt-switching",
823 &vt_switching, true);
824 compositor->vt_switching = vt_switching;
826 /* agl-compositor.ini [core] */
827 section = weston_config_get_section(config, "core", NULL, NULL);
829 weston_config_section_get_bool(section, "require-input", &require_input, true);
830 compositor->require_input = require_input;
832 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
833 compositor->repaint_msec);
834 if (repaint_msec < -10 || repaint_msec > 1000) {
835 weston_log("Invalid repaint_window value in config: %d\n",
838 compositor->repaint_msec = repaint_msec;
840 weston_log("Output repaint window is %d ms maximum.\n",
841 compositor->repaint_msec);
847 to_ivi_surface(struct weston_surface *surface)
849 struct weston_desktop_surface *dsurface;
851 dsurface = weston_surface_get_desktop_surface(surface);
855 return weston_desktop_surface_get_user_data(dsurface);
859 activate_binding(struct weston_seat *seat,
860 struct weston_view *focus_view)
862 struct weston_surface *focus = focus_view->surface;
863 struct weston_surface *main_surface =
864 weston_surface_get_main_surface(focus);
865 struct ivi_surface *surface;
867 surface = to_ivi_surface(main_surface);
871 weston_seat_set_keyboard_focus(seat, focus);
875 click_to_activate_binding(struct weston_pointer *pointer,
876 const struct timespec *time,
877 uint32_t button, void *data)
879 if (pointer->grab != &pointer->default_grab)
881 if (pointer->focus == NULL)
884 activate_binding(pointer->seat, pointer->focus);
888 touch_to_activate_binding(struct weston_touch *touch,
889 const struct timespec *time,
892 if (touch->grab != &touch->default_grab)
894 if (touch->focus == NULL)
897 activate_binding(touch->seat, touch->focus);
901 add_bindings(struct weston_compositor *compositor)
903 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
904 click_to_activate_binding,
906 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
907 click_to_activate_binding,
909 weston_compositor_add_touch_binding(compositor, 0,
910 touch_to_activate_binding,
915 create_listening_socket(struct wl_display *display, const char *socket_name)
918 if (wl_display_add_socket(display, socket_name)) {
919 weston_log("fatal: failed to add socket: %s\n",
924 socket_name = wl_display_add_socket_auto(display);
926 weston_log("fatal: failed to add socket: %s\n",
932 setenv("WAYLAND_DISPLAY", socket_name, 1);
938 global_filter(const struct wl_client *client, const struct wl_global *global,
945 load_config(struct weston_config **config, bool no_config,
946 const char *config_file)
948 const char *file = "agl-compositor.ini";
949 const char *full_path;
955 *config = weston_config_parse(file);
958 full_path = weston_config_get_full_path(*config);
960 weston_log("Using config file '%s'.\n", full_path);
961 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
966 if (config_file && !no_config) {
967 weston_log("fatal: error opening or reading config file '%s'.\n",
973 weston_log("Starting with no config file.\n");
974 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
979 static FILE *logfile;
980 //static struct weston_log_scope *log_scope;
981 //static struct weston_log_scope *protocol_scope;
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;
1132 const struct weston_option core_options[] = {
1133 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1134 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1135 { WESTON_OPTION_STRING, "log", 0, &log },
1136 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1137 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1138 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1139 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1140 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug_protocol },
1143 wl_list_init(&ivi.outputs);
1144 wl_list_init(&ivi.surfaces);
1145 wl_list_init(&ivi.pending_surfaces);
1147 /* Prevent any clients we spawn getting our stdin */
1148 os_fd_set_cloexec(STDIN_FILENO);
1150 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1153 usage(EXIT_SUCCESS);
1156 printf(PACKAGE_STRING "\n");
1157 return EXIT_SUCCESS;
1161 weston_log_set_handler(vlog, vlog_continue);
1163 if (load_config(&ivi.config, no_config, config_file) < 0)
1165 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1167 weston_config_section_get_string(section, "backend", &backend,
1170 backend = choose_default_backend();
1173 ivi_compositor_get_quirks(&ivi);
1175 display = wl_display_create();
1176 loop = wl_display_get_event_loop(display);
1178 wl_display_set_global_filter(display,
1179 global_filter, &ivi);
1181 /* Register signal handlers so we shut down cleanly */
1183 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1185 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1187 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1190 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1194 ivi.compositor = weston_compositor_create(display, &ivi);
1195 if (!ivi.compositor) {
1196 weston_log("fatal: failed to create compositor.\n");
1200 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1201 goto error_compositor;
1203 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1204 weston_log("fatal: failed to create compositor backend.\n");
1205 goto error_compositor;
1208 ivi.heads_changed.notify = heads_changed;
1209 weston_compositor_add_heads_changed_listener(ivi.compositor,
1210 &ivi.heads_changed);
1212 if (ivi_desktop_init(&ivi) < 0)
1213 goto error_compositor;
1215 if (ivi_shell_init(&ivi) < 0)
1216 goto error_compositor;
1218 add_bindings(ivi.compositor);
1220 weston_compositor_flush_heads_changed(ivi.compositor);
1222 ivi_shell_init_black_fs(&ivi);
1224 if (create_listening_socket(display, socket_name) < 0)
1225 goto error_compositor;
1227 ivi.compositor->exit = handle_exit;
1229 weston_compositor_wake(ivi.compositor);
1231 ivi_shell_create_global(&ivi);
1232 ivi_launch_shell_client(&ivi);
1233 ivi_agl_systemd_notify(&ivi);
1235 wl_display_run(display);
1237 wl_display_destroy_clients(display);
1240 weston_compositor_destroy(ivi.compositor);
1243 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1245 wl_event_source_remove(signals[i]);
1247 wl_display_destroy(display);
1251 weston_config_destroy(ivi.config);