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-6/compositor-drm.h>
40 #include <libweston-6/compositor-wayland.h>
41 #include <libweston-6/compositor-x11.h>
42 #include <libweston-6/compositor.h>
43 #include <libweston-6/windowed-output-api.h>
44 #include <libweston-6/config-parser.h>
46 #include "shared/os-compatibility.h"
48 #include "agl-shell-server-protocol.h"
50 struct ivi_compositor *
51 to_ivi_compositor(struct weston_compositor *ec)
53 return weston_compositor_get_user_data(ec);
57 handle_output_destroy(struct wl_listener *listener, void *data)
59 struct ivi_output *output;
61 output = wl_container_of(listener, output, output_destroy);
62 assert(output->output == data);
64 output->output = NULL;
65 wl_list_remove(&output->output_destroy.link);
69 to_ivi_output(struct weston_output *o)
71 struct wl_listener *listener;
72 struct ivi_output *output;
74 listener = weston_output_get_destroy_listener(o, handle_output_destroy);
75 output = wl_container_of(listener, output, output_destroy);
80 static struct ivi_output *
81 ivi_ensure_output(struct ivi_compositor *ivi, char *name,
82 struct weston_config_section *config)
84 struct ivi_output *output = NULL;
85 wl_list_for_each(output, &ivi->outputs, link) {
86 if (strcmp(output->name, name) == 0) {
92 output = zalloc(sizeof *output);
100 output->config = config;
102 output->output = weston_compositor_create_output(ivi->compositor, name);
103 if (!output->output) {
109 output->output_destroy.notify = handle_output_destroy;
110 weston_output_add_destroy_listener(output->output,
111 &output->output_destroy);
113 wl_list_insert(&ivi->outputs, &output->link);
118 ivi_output_destroy(struct ivi_output *output)
120 weston_output_destroy(output->output);
122 wl_list_remove(&output->link);
127 count_heads(struct weston_output *output)
129 struct weston_head *iter = NULL;
132 while ((iter = weston_output_iterate_heads(output, iter)))
139 handle_head_destroy(struct wl_listener *listener, void *data)
141 struct weston_head *head = data;
142 struct weston_output *output;
144 wl_list_remove(&listener->link);
147 output = weston_head_get_output(head);
149 /* On shutdown path, the output might be already gone. */
153 /* We're the last head */
154 if (count_heads(output) <= 1)
155 weston_output_destroy(output);
159 add_head_destroyed_listener(struct weston_head *head)
161 /* We already have a destroy listener */
162 if (weston_head_get_destroy_listener(head, handle_head_destroy))
165 struct wl_listener *listener = zalloc(sizeof *listener);
169 listener->notify = handle_head_destroy;
170 weston_head_add_destroy_listener(head, listener);
174 drm_configure_output(struct ivi_output *output)
176 struct ivi_compositor *ivi = output->ivi;
177 struct weston_config_section *section = output->config;
178 enum weston_drm_backend_output_mode mode =
179 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
180 char *modeline = NULL;
181 char *gbm_format = NULL;
186 weston_config_section_get_string(section, "mode", &m, "preferred");
188 /* This should have been handled earlier */
189 assert(strcmp(m, "off") != 0);
191 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
192 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
193 } else if (strcmp(m, "preferred") != 0) {
199 weston_config_section_get_string(section, "gbm-format",
202 weston_config_section_get_string(section, "seat", &seat, "");
205 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
206 weston_log("Cannot configure output using weston_drm_output_api.\n");
212 ivi->drm_api->set_gbm_format(output->output, gbm_format);
215 ivi->drm_api->set_seat(output->output, seat);
221 #define WINDOWED_DEFAULT_WIDTH 1024
222 #define WINDOWED_DEFAULT_HEIGHT 768
225 windowed_configure_output(struct ivi_output *output)
227 struct ivi_compositor *ivi = output->ivi;
228 struct weston_config_section *section = output->config;
229 int width = WINDOWED_DEFAULT_WIDTH;
230 int height = WINDOWED_DEFAULT_HEIGHT;
235 weston_config_section_get_string(section, "mode", &mode, NULL);
236 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
237 weston_log("Invalid mode for output %s. Using defaults.\n",
239 width = WINDOWED_DEFAULT_WIDTH;
240 height = WINDOWED_DEFAULT_HEIGHT;
245 if (ivi->cmdline.width)
246 width = ivi->cmdline.width;
247 if (ivi->cmdline.height)
248 height = ivi->cmdline.height;
249 if (ivi->cmdline.scale)
250 weston_output_set_scale(output->output, ivi->cmdline.scale);
252 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
253 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
262 parse_transform(const char *transform, uint32_t *out)
264 static const struct { const char *name; uint32_t token; } transforms[] = {
265 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
266 { "90", WL_OUTPUT_TRANSFORM_90 },
267 { "180", WL_OUTPUT_TRANSFORM_180 },
268 { "270", WL_OUTPUT_TRANSFORM_270 },
269 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
270 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
271 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
272 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
275 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
276 if (strcmp(transforms[i].name, transform) == 0) {
277 *out = transforms[i].token;
281 *out = WL_OUTPUT_TRANSFORM_NORMAL;
286 configure_output(struct ivi_output *output)
288 struct ivi_compositor *ivi = output->ivi;
289 struct weston_config_section *section = output->config;
291 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
294 * This can happen with the wayland backend with 'sprawl'. The config
295 * is hard-coded, so we don't need to do anything.
297 if (!ivi->drm_api && !ivi->window_api)
303 weston_config_section_get_int(section, "scale", &scale, 1);
304 weston_config_section_get_string(section, "transform", &t, "normal");
305 if (parse_transform(t, &transform) < 0)
306 weston_log("Invalid transform \"%s\" for output %s\n",
311 weston_output_set_scale(output->output, scale);
312 weston_output_set_transform(output->output, transform);
315 return drm_configure_output(output);
317 return windowed_configure_output(output);
321 * Reorgainizes the output's add array into two sections.
322 * add[0..ret-1] are the heads that failed to get attached.
323 * add[ret..add_len] are the heads that were successfully attached.
325 * The order between elements in each section is stable.
328 try_attach_heads(struct ivi_output *output)
332 for (size_t i = 0; i < output->add_len; ++i) {
333 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
334 struct weston_head *tmp = output->add[i];
335 memmove(&output->add[fail_len + 1], output->add[fail_len],
336 sizeof output->add[0] * (i - fail_len));
337 output->add[fail_len++] = tmp;
345 * Like try_attach_heads, this reorganizes the output's add array into a failed
346 * and successful section.
347 * i is the number of heads that already failed the previous step.
350 try_enable_output(struct ivi_output *output, size_t i)
352 for (; i < output->add_len; ++i) {
353 struct weston_head *head;
355 if (weston_output_enable(output->output) == 0)
358 head = output->add[output->add_len - 1];
359 memmove(&output->add[i + 1], &output->add[i],
360 sizeof output->add[0] * (output->add_len - i));
361 output->add[i] = head;
363 weston_head_detach(head);
370 try_attach_enable_heads(struct ivi_output *output)
373 assert(!output->output->enabled);
375 fail_len = try_attach_heads(output);
377 if (configure_output(output) < 0)
380 fail_len = try_enable_output(output, fail_len);
382 /* All heads failed to be attached */
383 if (fail_len == output->add_len)
386 /* For each successful head attached */
387 for (size_t i = fail_len; i < output->add_len; ++i)
388 add_head_destroyed_listener(output->add[i]);
390 output->add_len = fail_len;
395 process_output(struct ivi_output *output)
397 if (output->output->enabled) {
398 output->add_len = try_attach_heads(output);
399 return output->add_len == 0 ? 0 : -1;
402 return try_attach_enable_heads(output);
406 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
408 struct weston_output *output;
409 struct ivi_output *ivi_output;
410 struct wl_listener *listener;
412 output = weston_head_get_output(head);
415 listener = weston_output_get_destroy_listener(output,
416 handle_output_destroy);
419 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
420 assert(ivi_output->output == output);
422 weston_head_detach(head);
423 if (count_heads(ivi_output->output) == 0) {
424 ivi_output_destroy(ivi_output);
425 weston_output_disable(ivi_output->output);
429 static struct weston_config_section *
430 find_controlling_output_config(struct weston_config *config,
433 struct weston_config_section *section;
437 same_as = strdup(name);
439 section = weston_config_get_section(config, "output",
441 if (!section && depth > 0)
442 weston_log("Configuration error: output section reffered"
443 "to by same-as=%s not found.\n", same_as);
450 weston_log("Configuration error: same-as nested too "
451 "deep for output '%s'.\n", name);
455 weston_config_section_get_string(section, "same-as",
463 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
465 const char *name = weston_head_get_name(head);
466 struct weston_config_section *section;
467 struct ivi_output *output;
468 char *output_name = NULL;
470 section = find_controlling_output_config(ivi->config, name);
474 weston_config_section_get_string(section, "mode", &mode, NULL);
475 if (mode && strcmp(mode, "off") == 0) {
481 weston_config_section_get_string(section, "name",
484 output_name = strdup(name);
490 output = ivi_ensure_output(ivi, output_name, section);
494 if (output->add_len >= ARRAY_LENGTH(output->add))
497 output->add[output->add_len++] = head;
501 heads_changed(struct wl_listener *listener, void *arg)
503 struct weston_compositor *compositor = arg;
504 struct weston_head *head = NULL;
505 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
506 struct ivi_output *output;
508 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
509 bool connected = weston_head_is_connected(head);
510 bool enabled = weston_head_is_enabled(head);
511 bool changed = weston_head_is_device_changed(head);
512 bool non_desktop = weston_head_is_non_desktop(head);
514 if (connected && !enabled && !non_desktop)
515 head_prepare_enable(ivi, head);
516 else if (!connected && enabled)
517 head_disable(ivi, head);
518 else if (enabled && changed)
519 weston_log("Detected a monitor change on head '%s', "
520 "not bothering to do anything about it.\n",
521 weston_head_get_name(head));
523 weston_head_reset_device_changed(head);
526 wl_list_for_each(output, &ivi->outputs, link) {
527 if (output->add_len == 0)
530 if (process_output(output) < 0) {
532 ivi->init_failed = true;
538 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
540 struct weston_drm_backend_config config = {
542 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
543 .struct_size = sizeof config,
546 struct weston_config_section *section;
547 int use_current_mode = 0;
552 const struct weston_option options[] = {
553 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
554 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
555 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
556 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
557 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
560 parse_options(options, ARRAY_LENGTH(options), argc, argv);
561 config.use_pixman = use_pixman;
562 ivi->cmdline.use_current_mode = use_current_mode;
564 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
565 weston_config_section_get_string(section, "gbm-format",
566 &config.gbm_format, NULL);
567 weston_config_section_get_uint(section, "pageflip-timeout",
568 &config.pageflip_timeout, 0);
569 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
570 config.use_pixman_shadow = use_shadow;
572 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
577 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
579 weston_log("Cannot use drm output api.\n");
585 free(config.gbm_format);
586 free(config.seat_id);
591 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
592 bool *use_pixman, bool *fullscreen, int *output_count)
594 struct weston_config_section *section;
598 const struct weston_option options[] = {
599 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
600 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
601 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
602 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
603 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
604 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
607 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
608 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
611 parse_options(options, ARRAY_LENGTH(options), argc, argv);
612 *use_pixman = pixman;
617 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
618 const char *match_prefix, const char *name_prefix)
620 struct weston_config_section *section = NULL;
621 const char *section_name;
622 char *default_output = NULL;
624 size_t match_len = strlen(match_prefix);
626 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
629 if (i >= output_count)
632 if (strcmp(section_name, "output") != 0)
635 weston_config_section_get_string(section, "name", &output_name, NULL);
636 if (output_name == NULL)
638 if (strncmp(output_name, match_prefix, match_len) != 0) {
643 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
652 for (; i < output_count; ++i) {
653 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
656 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
657 free(default_output);
661 free(default_output);
668 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
670 struct weston_wayland_backend_config config = {
672 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
673 .struct_size = sizeof config,
676 struct weston_config_section *section;
681 const struct weston_option options[] = {
682 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
683 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
686 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
687 &config.fullscreen, &output_count);
689 parse_options(options, ARRAY_LENGTH(options), argc, argv);
690 config.sprawl = sprawl;
692 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
693 weston_config_section_get_string(section, "cursor-theme",
694 &config.cursor_theme, NULL);
695 weston_config_section_get_int(section, "cursor-size",
696 &config.cursor_size, 32);
698 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
701 free(config.cursor_theme);
702 free(config.display_name);
707 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
710 * We will just assume if load_backend() finished cleanly and
711 * windowed_output_api is not present that wayland backend is started
712 * with --sprawl or runs on fullscreen-shell. In this case, all values
713 * are hardcoded, so nothing can be configured; simply create and
716 if (ivi->window_api == NULL)
719 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
723 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
725 struct weston_x11_backend_config config = {
727 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
728 .struct_size = sizeof config,
735 const struct weston_option options[] = {
736 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
739 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
740 &config.fullscreen, &output_count);
742 parse_options(options, ARRAY_LENGTH(options), argc, argv);
743 config.no_input = no_input;
745 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
751 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
752 if (!ivi->window_api) {
753 weston_log("Cannot use weston_windowed_output_api.\n");
757 return windowed_create_outputs(ivi, output_count, "X", "screen");
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);
868 if (!surface || surface->role != IVI_SURFACE_ROLE_DESKTOP)
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,
942 struct ivi_compositor *ivi = data;
943 const struct wl_interface *iface = wl_global_get_interface(global);
945 if (iface == &agl_shell_interface)
946 return client == ivi->shell_client.client;
953 load_config(struct weston_config **config, bool no_config,
954 const char *config_file)
956 const char *file = "agl-compositor.ini";
957 const char *full_path;
963 *config = weston_config_parse(file);
966 full_path = weston_config_get_full_path(*config);
968 weston_log("Using config file '%s'.\n", full_path);
969 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
974 if (config_file && !no_config) {
975 weston_log("fatal: error opening or reading config file '%s'.\n",
981 weston_log("Starting with no config file.\n");
982 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
987 static FILE *logfile;
988 //static struct weston_log_scope *log_scope;
989 //static struct weston_log_scope *protocol_scope;
994 static int cached_tm_mday = -1;
996 struct tm brokendown_time;
999 clock_gettime(CLOCK_REALTIME, &ts);
1000 if (!localtime_r(&ts.tv_sec, &brokendown_time))
1001 return fprintf(logfile, "[(NULL)localtime] ");
1003 if (brokendown_time.tm_mday != cached_tm_mday) {
1004 strftime(buf, sizeof buf, "%Y-%m-%d %Z", &brokendown_time);
1005 fprintf(logfile, "Date: %s\n", buf);
1007 cached_tm_mday = brokendown_time.tm_mday;
1010 strftime(buf, sizeof buf, "%H:%M:%S", &brokendown_time);
1012 return fprintf(logfile, "[%s.%03ld] ", buf, ts.tv_nsec / 1000000);
1016 custom_handler(const char *fmt, va_list arg)
1019 fprintf(logfile, "libwayland: ");
1020 vfprintf(logfile, fmt, arg);
1024 log_file_open(const char *filename)
1026 wl_log_set_handler_server(custom_handler);
1029 logfile = fopen(filename, "a");
1034 os_fd_set_cloexec(fileno(logfile));
1035 setvbuf(logfile, NULL, _IOLBF, 256);
1040 log_file_close(void)
1042 if (logfile && logfile != stderr)
1048 vlog(const char *fmt, va_list ap)
1052 l = log_timestamp();
1053 l += vfprintf(logfile, fmt, ap);
1059 vlog_continue(const char *fmt, va_list ap)
1061 return vfprintf(logfile, fmt, ap);
1065 on_term_signal(int signo, void *data)
1067 struct wl_display *display = data;
1069 weston_log("caught signal %d\n", signo);
1070 wl_display_terminate(display);
1076 handle_exit(struct weston_compositor *compositor)
1078 wl_display_terminate(compositor->wl_display);
1082 usage(int error_code)
1084 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1086 "Usage: agl-compositor [OPTIONS]\n"
1088 "This is " PACKAGE_STRING ", the reference compositor for\n"
1089 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1090 "on which backend is in use different options will be accepted.\n"
1094 " --version\t\tPrint agl-compositor version\n"
1095 " -B, --backend=MODULE\tBackend module, one of\n"
1096 "\t\t\t\tdrm-backend.so\n"
1097 "\t\t\t\twayland-backend.so\n"
1098 "\t\t\t\tx11-backend.so\n"
1099 " -S, --socket=NAME\tName of socket to listen on\n"
1100 " --log=FILE\t\tLog to the given file\n"
1101 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1102 " --no-config\t\tDo not read agl-compositor.ini\n"
1103 " --debug\t\tEnable debug extension\n"
1104 " -h, --help\t\tThis help message\n"
1109 int main(int argc, char *argv[])
1111 struct ivi_compositor ivi = { 0 };
1112 struct wl_display *display;
1113 struct wl_event_loop *loop;
1114 struct wl_event_source *signals[3] = { 0 };
1115 struct weston_config_section *section;
1116 /* Command line options */
1117 char *backend = NULL;
1118 char *socket_name = NULL;
1123 char *config_file = NULL;
1124 int debug_protocol = 0;
1126 const struct weston_option core_options[] = {
1127 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1128 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1129 { WESTON_OPTION_STRING, "log", 0, &log },
1130 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1131 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1132 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1133 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1134 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug_protocol },
1137 wl_list_init(&ivi.outputs);
1138 wl_list_init(&ivi.surfaces);
1139 wl_list_init(&ivi.shell_clients);
1140 wl_list_init(&ivi.pending_surfaces);
1142 /* Prevent any clients we spawn getting our stdin */
1143 os_fd_set_cloexec(STDIN_FILENO);
1145 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1148 usage(EXIT_SUCCESS);
1151 printf(PACKAGE_STRING "\n");
1152 return EXIT_SUCCESS;
1156 weston_log_set_handler(vlog, vlog_continue);
1158 if (load_config(&ivi.config, no_config, config_file) < 0)
1160 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1162 weston_config_section_get_string(section, "backend", &backend,
1165 backend = choose_default_backend();
1168 display = wl_display_create();
1169 loop = wl_display_get_event_loop(display);
1171 wl_display_set_global_filter(display,
1172 global_filter, &ivi);
1174 /* Register signal handlers so we shut down cleanly */
1176 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1178 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1180 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1183 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1188 log_ctx = weston_log_ctx_compositor_create();
1190 weston_log("Failed to initialize weston debug framework.\n");
1195 ivi.compositor = weston_compositor_create(display, &ivi);
1196 if (!ivi.compositor) {
1197 weston_log("fatal: failed to create compositor.\n");
1203 weston_compositor_enable_debug_protocol(ivi.compositor);
1206 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1207 goto error_compositor;
1209 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1210 weston_log("fatal: failed to create compositor backend.\n");
1211 goto error_compositor;
1214 ivi.heads_changed.notify = heads_changed;
1215 weston_compositor_add_heads_changed_listener(ivi.compositor,
1216 &ivi.heads_changed);
1218 if (ivi_desktop_init(&ivi) < 0)
1219 goto error_compositor;
1221 if (ivi_shell_init(&ivi) < 0)
1222 goto error_compositor;
1224 add_bindings(ivi.compositor);
1226 weston_compositor_flush_heads_changed(ivi.compositor);
1228 if (create_listening_socket(display, socket_name) < 0)
1229 goto error_compositor;
1231 ivi.compositor->exit = handle_exit;
1233 weston_compositor_wake(ivi.compositor);
1235 ivi_shell_create_global(&ivi);
1236 ivi_launch_shell_client(&ivi);
1238 wl_display_run(display);
1240 wl_display_destroy_clients(display);
1243 weston_compositor_destroy(ivi.compositor);
1246 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1248 wl_event_source_remove(signals[i]);
1250 wl_display_destroy(display);
1254 weston_config_destroy(ivi.config);