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 struct ivi_compositor *
56 to_ivi_compositor(struct weston_compositor *ec)
58 return weston_compositor_get_user_data(ec);
62 handle_output_destroy(struct wl_listener *listener, void *data)
64 struct ivi_output *output;
66 output = wl_container_of(listener, output, output_destroy);
67 assert(output->output == data);
69 output->output = NULL;
70 wl_list_remove(&output->output_destroy.link);
74 to_ivi_output(struct weston_output *o)
76 struct wl_listener *listener;
77 struct ivi_output *output;
79 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
80 output = wl_container_of(listener, output, output_destroy);
85 static struct ivi_output *
86 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
87 struct weston_config_section *config)
89 struct ivi_output *output = NULL;
90 wl_list_for_each(output, &ivi->outputs, link) {
91 if (strcmp(output->name, name) == 0) {
97 output = zalloc(sizeof *output);
105 output->config = config;
107 output->output = weston_compositor_create_output(ivi->compositor, name);
108 if (!output->output) {
114 output->output_destroy.notify = handle_output_destroy;
115 weston_output_add_destroy_listener(output->output,
116 &output->output_destroy);
118 wl_list_insert(&ivi->outputs, &output->link);
123 count_heads(struct weston_output *output)
125 struct weston_head *iter = NULL;
128 while ((iter = weston_output_iterate_heads(output, iter)))
135 handle_head_destroy(struct wl_listener *listener, void *data)
137 struct weston_head *head = data;
138 struct weston_output *output;
140 wl_list_remove(&listener->link);
143 output = weston_head_get_output(head);
145 /* On shutdown path, the output might be already gone. */
149 /* We're the last head */
150 if (count_heads(output) <= 1)
151 weston_output_destroy(output);
155 add_head_destroyed_listener(struct weston_head *head)
157 /* We already have a destroy listener */
158 if (weston_head_get_destroy_listener(head, handle_head_destroy))
161 struct wl_listener *listener = zalloc(sizeof *listener);
165 listener->notify = handle_head_destroy;
166 weston_head_add_destroy_listener(head, listener);
170 drm_configure_output(struct ivi_output *output)
172 struct ivi_compositor *ivi = output->ivi;
173 struct weston_config_section *section = output->config;
174 enum weston_drm_backend_output_mode mode =
175 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
176 char *modeline = NULL;
177 char *gbm_format = NULL;
182 weston_config_section_get_string(section, "mode", &m, "preferred");
184 /* This should have been handled earlier */
185 assert(strcmp(m, "off") != 0);
187 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
188 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
189 } else if (strcmp(m, "preferred") != 0) {
195 weston_config_section_get_string(section, "gbm-format",
198 weston_config_section_get_string(section, "seat", &seat, "");
201 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
202 weston_log("Cannot configure output using weston_drm_output_api.\n");
208 ivi->drm_api->set_gbm_format(output->output, gbm_format);
211 ivi->drm_api->set_seat(output->output, seat);
217 #define WINDOWED_DEFAULT_WIDTH 1024
218 #define WINDOWED_DEFAULT_HEIGHT 768
221 windowed_configure_output(struct ivi_output *output)
223 struct ivi_compositor *ivi = output->ivi;
224 struct weston_config_section *section = output->config;
225 int width = WINDOWED_DEFAULT_WIDTH;
226 int height = WINDOWED_DEFAULT_HEIGHT;
231 weston_config_section_get_string(section, "mode", &mode, NULL);
232 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
233 weston_log("Invalid mode for output %s. Using defaults.\n",
235 width = WINDOWED_DEFAULT_WIDTH;
236 height = WINDOWED_DEFAULT_HEIGHT;
241 if (ivi->cmdline.width)
242 width = ivi->cmdline.width;
243 if (ivi->cmdline.height)
244 height = ivi->cmdline.height;
245 if (ivi->cmdline.scale)
246 weston_output_set_scale(output->output, ivi->cmdline.scale);
248 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
249 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
258 parse_transform(const char *transform, uint32_t *out)
260 static const struct { const char *name; uint32_t token; } transforms[] = {
261 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
262 { "90", WL_OUTPUT_TRANSFORM_90 },
263 { "180", WL_OUTPUT_TRANSFORM_180 },
264 { "270", WL_OUTPUT_TRANSFORM_270 },
265 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
266 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
267 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
268 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
271 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
272 if (strcmp(transforms[i].name, transform) == 0) {
273 *out = transforms[i].token;
277 *out = WL_OUTPUT_TRANSFORM_NORMAL;
282 configure_output(struct ivi_output *output)
284 struct ivi_compositor *ivi = output->ivi;
285 struct weston_config_section *section = output->config;
287 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
290 * This can happen with the wayland backend with 'sprawl'. The config
291 * is hard-coded, so we don't need to do anything.
293 if (!ivi->drm_api && !ivi->window_api)
299 weston_config_section_get_int(section, "scale", &scale, 1);
300 weston_config_section_get_string(section, "transform", &t, "normal");
301 if (parse_transform(t, &transform) < 0)
302 weston_log("Invalid transform \"%s\" for output %s\n",
307 weston_output_set_scale(output->output, scale);
308 weston_output_set_transform(output->output, transform);
311 return drm_configure_output(output);
313 return windowed_configure_output(output);
317 * Reorgainizes the output's add array into two sections.
318 * add[0..ret-1] are the heads that failed to get attached.
319 * add[ret..add_len] are the heads that were successfully attached.
321 * The order between elements in each section is stable.
324 try_attach_heads(struct ivi_output *output)
328 for (size_t i = 0; i < output->add_len; ++i) {
329 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
330 struct weston_head *tmp = output->add[i];
331 memmove(&output->add[fail_len + 1], output->add[fail_len],
332 sizeof output->add[0] * (i - fail_len));
333 output->add[fail_len++] = tmp;
341 * Like try_attach_heads, this reorganizes the output's add array into a failed
342 * and successful section.
343 * i is the number of heads that already failed the previous step.
346 try_enable_output(struct ivi_output *output, size_t i)
348 for (; i < output->add_len; ++i) {
349 struct weston_head *head;
351 if (weston_output_enable(output->output) == 0)
354 head = output->add[output->add_len - 1];
355 memmove(&output->add[i + 1], &output->add[i],
356 sizeof output->add[0] * (output->add_len - i));
357 output->add[i] = head;
359 weston_head_detach(head);
366 try_attach_enable_heads(struct ivi_output *output)
369 assert(!output->output->enabled);
371 fail_len = try_attach_heads(output);
373 if (configure_output(output) < 0)
376 fail_len = try_enable_output(output, fail_len);
378 /* All heads failed to be attached */
379 if (fail_len == output->add_len)
382 /* For each successful head attached */
383 for (size_t i = fail_len; i < output->add_len; ++i)
384 add_head_destroyed_listener(output->add[i]);
386 output->add_len = fail_len;
391 process_output(struct ivi_output *output)
393 if (output->output->enabled) {
394 output->add_len = try_attach_heads(output);
395 return output->add_len == 0 ? 0 : -1;
398 return try_attach_enable_heads(output);
402 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
404 struct weston_output *output;
405 struct ivi_output *ivi_output;
406 struct wl_listener *listener;
408 output = weston_head_get_output(head);
411 listener = weston_output_get_destroy_listener(output,
412 handle_output_destroy);
415 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
416 assert(ivi_output->output == output);
418 weston_head_detach(head);
419 if (count_heads(ivi_output->output) == 0) {
420 weston_output_disable(ivi_output->output);
424 static struct weston_config_section *
425 find_controlling_output_config(struct weston_config *config,
428 struct weston_config_section *section;
432 same_as = strdup(name);
434 section = weston_config_get_section(config, "output",
436 if (!section && depth > 0)
437 weston_log("Configuration error: output section reffered"
438 "to by same-as=%s not found.\n", same_as);
445 weston_log("Configuration error: same-as nested too "
446 "deep for output '%s'.\n", name);
450 weston_config_section_get_string(section, "same-as",
458 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
460 const char *name = weston_head_get_name(head);
461 struct weston_config_section *section;
462 struct ivi_output *output;
463 char *output_name = NULL;
465 section = find_controlling_output_config(ivi->config, name);
469 weston_config_section_get_string(section, "mode", &mode, NULL);
470 if (mode && strcmp(mode, "off") == 0) {
476 weston_config_section_get_string(section, "name",
479 output_name = strdup(name);
485 output = ivi_ensure_output(ivi, output_name, section);
489 if (output->add_len >= ARRAY_LENGTH(output->add))
492 output->add[output->add_len++] = head;
496 heads_changed(struct wl_listener *listener, void *arg)
498 struct weston_compositor *compositor = arg;
499 struct weston_head *head = NULL;
500 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
501 struct ivi_output *output;
503 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
504 bool connected = weston_head_is_connected(head);
505 bool enabled = weston_head_is_enabled(head);
506 bool changed = weston_head_is_device_changed(head);
507 bool non_desktop = weston_head_is_non_desktop(head);
509 if (connected && !enabled && !non_desktop)
510 head_prepare_enable(ivi, head);
511 else if (!connected && enabled)
512 head_disable(ivi, head);
513 else if (enabled && changed)
514 weston_log("Detected a monitor change on head '%s', "
515 "not bothering to do anything about it.\n",
516 weston_head_get_name(head));
518 weston_head_reset_device_changed(head);
521 wl_list_for_each(output, &ivi->outputs, link) {
522 if (output->add_len == 0)
525 if (process_output(output) < 0) {
527 ivi->init_failed = true;
533 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
535 struct weston_drm_backend_config config = {
537 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
538 .struct_size = sizeof config,
541 struct weston_config_section *section;
542 int use_current_mode = 0;
547 const struct weston_option options[] = {
548 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
549 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
550 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
551 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
552 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
555 parse_options(options, ARRAY_LENGTH(options), argc, argv);
556 config.use_pixman = use_pixman;
557 ivi->cmdline.use_current_mode = use_current_mode;
559 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
560 weston_config_section_get_string(section, "gbm-format",
561 &config.gbm_format, NULL);
562 weston_config_section_get_uint(section, "pageflip-timeout",
563 &config.pageflip_timeout, 0);
564 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
565 config.use_pixman_shadow = use_shadow;
567 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
572 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
574 weston_log("Cannot use drm output api.\n");
580 free(config.gbm_format);
581 free(config.seat_id);
586 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
587 bool *use_pixman, bool *fullscreen, int *output_count)
589 struct weston_config_section *section;
593 const struct weston_option options[] = {
594 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
595 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
596 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
597 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
598 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
599 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
602 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
603 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
606 parse_options(options, ARRAY_LENGTH(options), argc, argv);
607 *use_pixman = pixman;
612 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
613 const char *match_prefix, const char *name_prefix)
615 struct weston_config_section *section = NULL;
616 const char *section_name;
617 char *default_output = NULL;
619 size_t match_len = strlen(match_prefix);
621 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
624 if (i >= output_count)
627 if (strcmp(section_name, "output") != 0)
630 weston_config_section_get_string(section, "name", &output_name, NULL);
631 if (output_name == NULL)
633 if (strncmp(output_name, match_prefix, match_len) != 0) {
638 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
647 for (; i < output_count; ++i) {
648 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
651 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
652 free(default_output);
656 free(default_output);
663 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
665 struct weston_wayland_backend_config config = {
667 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
668 .struct_size = sizeof config,
671 struct weston_config_section *section;
676 const struct weston_option options[] = {
677 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
678 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
681 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
682 &config.fullscreen, &output_count);
684 parse_options(options, ARRAY_LENGTH(options), argc, argv);
685 config.sprawl = sprawl;
687 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
688 weston_config_section_get_string(section, "cursor-theme",
689 &config.cursor_theme, NULL);
690 weston_config_section_get_int(section, "cursor-size",
691 &config.cursor_size, 32);
693 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
696 free(config.cursor_theme);
697 free(config.display_name);
702 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
705 * We will just assume if load_backend() finished cleanly and
706 * windowed_output_api is not present that wayland backend is started
707 * with --sprawl or runs on fullscreen-shell. In this case, all values
708 * are hardcoded, so nothing can be configured; simply create and
711 if (ivi->window_api == NULL)
714 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
717 #ifdef HAVE_BACKEND_X11
719 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
721 struct weston_x11_backend_config config = {
723 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
724 .struct_size = sizeof config,
731 const struct weston_option options[] = {
732 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
735 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
736 &config.fullscreen, &output_count);
738 parse_options(options, ARRAY_LENGTH(options), argc, argv);
739 config.no_input = no_input;
741 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
747 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
748 if (!ivi->window_api) {
749 weston_log("Cannot use weston_windowed_output_api.\n");
753 return windowed_create_outputs(ivi, output_count, "X", "screen");
757 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
764 load_backend(struct ivi_compositor *ivi, const char *backend,
765 int *argc, char *argv[])
767 if (strcmp(backend, "drm-backend.so") == 0) {
768 return load_drm_backend(ivi, argc, argv);
769 } else if (strcmp(backend, "wayland-backend.so") == 0) {
770 return load_wayland_backend(ivi, argc, argv);
771 } else if (strcmp(backend, "x11-backend.so") == 0) {
772 return load_x11_backend(ivi, argc, argv);
775 weston_log("fatal: unknown backend '%s'.\n", backend);
780 choose_default_backend(void)
782 char *backend = NULL;
784 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
785 backend = strdup("wayland-backend.so");
786 else if (getenv("DISPLAY"))
787 backend = strdup("x11-backend.so");
789 backend = strdup("drm-backend.so");
795 compositor_init_config(struct weston_compositor *compositor,
796 struct weston_config *config)
798 struct xkb_rule_names xkb_names;
799 struct weston_config_section *section;
804 /* agl-compositor.ini [keyboard] */
805 section = weston_config_get_section(config, "keyboard", NULL, NULL);
806 weston_config_section_get_string(section, "keymap_rules",
807 (char **) &xkb_names.rules, NULL);
808 weston_config_section_get_string(section, "keymap_model",
809 (char **) &xkb_names.model, NULL);
810 weston_config_section_get_string(section, "keymap_layout",
811 (char **) &xkb_names.layout, NULL);
812 weston_config_section_get_string(section, "keymap_variant",
813 (char **) &xkb_names.variant, NULL);
814 weston_config_section_get_string(section, "keymap_options",
815 (char **) &xkb_names.options, NULL);
817 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
820 weston_config_section_get_int(section, "repeat-rate",
821 &compositor->kb_repeat_rate, 40);
822 weston_config_section_get_int(section, "repeat-delay",
823 &compositor->kb_repeat_delay, 400);
825 weston_config_section_get_bool(section, "vt-switching",
826 &vt_switching, true);
827 compositor->vt_switching = vt_switching;
829 /* agl-compositor.ini [core] */
830 section = weston_config_get_section(config, "core", NULL, NULL);
832 weston_config_section_get_bool(section, "require-input", &require_input, true);
833 compositor->require_input = require_input;
835 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
836 compositor->repaint_msec);
837 if (repaint_msec < -10 || repaint_msec > 1000) {
838 weston_log("Invalid repaint_window value in config: %d\n",
841 compositor->repaint_msec = repaint_msec;
843 weston_log("Output repaint window is %d ms maximum.\n",
844 compositor->repaint_msec);
850 to_ivi_surface(struct weston_surface *surface)
852 struct weston_desktop_surface *dsurface;
854 dsurface = weston_surface_get_desktop_surface(surface);
858 return weston_desktop_surface_get_user_data(dsurface);
862 activate_binding(struct weston_seat *seat,
863 struct weston_view *focus_view)
865 struct weston_surface *focus = focus_view->surface;
866 struct weston_surface *main_surface =
867 weston_surface_get_main_surface(focus);
868 struct ivi_surface *surface;
870 surface = to_ivi_surface(main_surface);
874 weston_seat_set_keyboard_focus(seat, focus);
878 click_to_activate_binding(struct weston_pointer *pointer,
879 const struct timespec *time,
880 uint32_t button, void *data)
882 if (pointer->grab != &pointer->default_grab)
884 if (pointer->focus == NULL)
887 activate_binding(pointer->seat, pointer->focus);
891 touch_to_activate_binding(struct weston_touch *touch,
892 const struct timespec *time,
895 if (touch->grab != &touch->default_grab)
897 if (touch->focus == NULL)
900 activate_binding(touch->seat, touch->focus);
904 add_bindings(struct weston_compositor *compositor)
906 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
907 click_to_activate_binding,
909 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
910 click_to_activate_binding,
912 weston_compositor_add_touch_binding(compositor, 0,
913 touch_to_activate_binding,
918 create_listening_socket(struct wl_display *display, const char *socket_name)
921 if (wl_display_add_socket(display, socket_name)) {
922 weston_log("fatal: failed to add socket: %s\n",
927 socket_name = wl_display_add_socket_auto(display);
929 weston_log("fatal: failed to add socket: %s\n",
935 setenv("WAYLAND_DISPLAY", socket_name, 1);
941 global_filter(const struct wl_client *client, const struct wl_global *global,
948 load_config(struct weston_config **config, bool no_config,
949 const char *config_file)
951 const char *file = "agl-compositor.ini";
952 const char *full_path;
958 *config = weston_config_parse(file);
961 full_path = weston_config_get_full_path(*config);
963 weston_log("Using config file '%s'.\n", full_path);
964 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
969 if (config_file && !no_config) {
970 weston_log("fatal: error opening or reading config file '%s'.\n",
976 weston_log("Starting with no config file.\n");
977 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
982 static FILE *logfile;
987 static int cached_tm_mday = -1;
989 struct tm brokendown_time;
992 clock_gettime(CLOCK_REALTIME, &ts);
993 if (!localtime_r(&ts.tv_sec, &brokendown_time))
994 return fprintf(logfile, "[(NULL)localtime] ");
996 if (brokendown_time.tm_mday != cached_tm_mday) {
997 strftime(buf, sizeof buf, "%Y-%m-%d %Z", &brokendown_time);
998 fprintf(logfile, "Date: %s\n", buf);
1000 cached_tm_mday = brokendown_time.tm_mday;
1003 strftime(buf, sizeof buf, "%H:%M:%S", &brokendown_time);
1005 return fprintf(logfile, "[%s.%03ld] ", buf, ts.tv_nsec / 1000000);
1009 custom_handler(const char *fmt, va_list arg)
1012 fprintf(logfile, "libwayland: ");
1013 vfprintf(logfile, fmt, arg);
1017 log_file_open(const char *filename)
1019 wl_log_set_handler_server(custom_handler);
1022 logfile = fopen(filename, "a");
1027 os_fd_set_cloexec(fileno(logfile));
1028 setvbuf(logfile, NULL, _IOLBF, 256);
1033 log_file_close(void)
1035 if (logfile && logfile != stderr)
1041 vlog(const char *fmt, va_list ap)
1045 l = log_timestamp();
1046 l += vfprintf(logfile, fmt, ap);
1052 vlog_continue(const char *fmt, va_list ap)
1054 return vfprintf(logfile, fmt, ap);
1058 on_term_signal(int signo, void *data)
1060 struct wl_display *display = data;
1062 weston_log("caught signal %d\n", signo);
1063 wl_display_terminate(display);
1069 handle_exit(struct weston_compositor *compositor)
1071 wl_display_terminate(compositor->wl_display);
1075 usage(int error_code)
1077 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1079 "Usage: agl-compositor [OPTIONS]\n"
1081 "This is " PACKAGE_STRING ", the reference compositor for\n"
1082 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1083 "on which backend is in use different options will be accepted.\n"
1087 " --version\t\tPrint agl-compositor version\n"
1088 " -B, --backend=MODULE\tBackend module, one of\n"
1089 "\t\t\t\tdrm-backend.so\n"
1090 "\t\t\t\twayland-backend.so\n"
1091 "\t\t\t\tx11-backend.so\n"
1092 " -S, --socket=NAME\tName of socket to listen on\n"
1093 " --log=FILE\t\tLog to the given file\n"
1094 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1095 " --no-config\t\tDo not read agl-compositor.ini\n"
1096 " --debug\t\tEnable debug extension\n"
1097 " -h, --help\t\tThis help message\n"
1103 ivi_compositor_get_quirks(struct ivi_compositor *ivi)
1105 struct weston_config_section *section;
1110 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1111 weston_config_section_get_bool(section, "activate-by-default",
1112 &ivi->quirks.activate_apps_by_default, 0);
1116 int main(int argc, char *argv[])
1118 struct ivi_compositor ivi = { 0 };
1119 struct wl_display *display = NULL;
1120 struct wl_event_loop *loop;
1121 struct wl_event_source *signals[3] = { 0 };
1122 struct weston_config_section *section;
1123 /* Command line options */
1124 char *backend = NULL;
1125 char *socket_name = NULL;
1130 char *config_file = NULL;
1131 int debug_protocol = 0;
1132 struct weston_log_context *log_ctx = NULL;
1133 struct weston_log_scope *log_scope;
1134 struct weston_log_subscriber *logger;
1136 const struct weston_option core_options[] = {
1137 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1138 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1139 { WESTON_OPTION_STRING, "log", 0, &log },
1140 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1141 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1142 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1143 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1144 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug_protocol },
1147 wl_list_init(&ivi.outputs);
1148 wl_list_init(&ivi.surfaces);
1149 wl_list_init(&ivi.pending_surfaces);
1150 wl_list_init(&ivi.desktop_clients);
1152 /* Prevent any clients we spawn getting our stdin */
1153 os_fd_set_cloexec(STDIN_FILENO);
1155 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1158 usage(EXIT_SUCCESS);
1161 printf(PACKAGE_STRING "\n");
1162 return EXIT_SUCCESS;
1165 log_ctx = weston_log_ctx_compositor_create();
1167 fprintf(stderr, "Failed to initialize weston debug framework.\n");
1168 return EXIT_FAILURE;
1171 log_scope = weston_compositor_add_log_scope(log_ctx, "log",
1172 "agl-compositor log\n",
1176 weston_log_set_handler(vlog, vlog_continue);
1178 logger = weston_log_subscriber_create_log(logfile);
1180 if (load_config(&ivi.config, no_config, config_file) < 0)
1182 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1184 weston_config_section_get_string(section, "backend", &backend,
1187 backend = choose_default_backend();
1190 ivi_compositor_get_quirks(&ivi);
1192 display = wl_display_create();
1193 loop = wl_display_get_event_loop(display);
1195 wl_display_set_global_filter(display,
1196 global_filter, &ivi);
1198 /* Register signal handlers so we shut down cleanly */
1200 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1202 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1204 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1207 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1211 ivi.compositor = weston_compositor_create(display, log_ctx, &ivi);
1212 if (!ivi.compositor) {
1213 weston_log("fatal: failed to create compositor.\n");
1217 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1218 goto error_compositor;
1220 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1221 weston_log("fatal: failed to create compositor backend.\n");
1222 goto error_compositor;
1225 ivi.heads_changed.notify = heads_changed;
1226 weston_compositor_add_heads_changed_listener(ivi.compositor,
1227 &ivi.heads_changed);
1229 if (ivi_desktop_init(&ivi) < 0)
1230 goto error_compositor;
1232 if (ivi_policy_init(&ivi) < 0)
1233 goto error_compositor;
1235 if (ivi_shell_init(&ivi) < 0)
1236 goto error_compositor;
1238 add_bindings(ivi.compositor);
1240 weston_compositor_flush_heads_changed(ivi.compositor);
1242 ivi_shell_init_black_fs(&ivi);
1244 if (create_listening_socket(display, socket_name) < 0)
1245 goto error_compositor;
1247 ivi.compositor->exit = handle_exit;
1249 weston_compositor_wake(ivi.compositor);
1251 ivi_shell_create_global(&ivi);
1252 ivi_launch_shell_client(&ivi);
1253 ivi_agl_systemd_notify(&ivi);
1255 wl_display_run(display);
1257 wl_display_destroy_clients(display);
1260 weston_compositor_tear_down(ivi.compositor);
1262 weston_compositor_log_scope_destroy(log_scope);
1265 weston_log_ctx_compositor_destroy(ivi.compositor);
1266 weston_compositor_destroy(ivi.compositor);
1268 weston_log_subscriber_destroy_log(logger);
1270 ivi_policy_destroy(ivi.policy);
1273 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1275 wl_event_source_remove(signals[i]);
1277 wl_display_destroy(display);
1281 weston_config_destroy(ivi.config);