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 count_heads(struct weston_output *output)
120 struct weston_head *iter = NULL;
123 while ((iter = weston_output_iterate_heads(output, iter)))
130 handle_head_destroy(struct wl_listener *listener, void *data)
132 struct weston_head *head = data;
133 struct weston_output *output;
135 wl_list_remove(&listener->link);
138 output = weston_head_get_output(head);
140 /* On shutdown path, the output might be already gone. */
144 /* We're the last head */
145 if (count_heads(output) <= 1)
146 weston_output_destroy(output);
150 add_head_destroyed_listener(struct weston_head *head)
152 /* We already have a destroy listener */
153 if (weston_head_get_destroy_listener(head, handle_head_destroy))
156 struct wl_listener *listener = zalloc(sizeof *listener);
160 listener->notify = handle_head_destroy;
161 weston_head_add_destroy_listener(head, listener);
165 drm_configure_output(struct ivi_output *output)
167 struct ivi_compositor *ivi = output->ivi;
168 struct weston_config_section *section = output->config;
169 enum weston_drm_backend_output_mode mode =
170 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
171 char *modeline = NULL;
172 char *gbm_format = NULL;
177 weston_config_section_get_string(section, "mode", &m, "preferred");
179 /* This should have been handled earlier */
180 assert(strcmp(m, "off") != 0);
182 if (ivi->cmdline.use_current_mode || strcmp(m, "current") == 0) {
183 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
184 } else if (strcmp(m, "preferred") != 0) {
190 weston_config_section_get_string(section, "gbm-format",
193 weston_config_section_get_string(section, "seat", &seat, "");
196 if (ivi->drm_api->set_mode(output->output, mode, modeline) < 0) {
197 weston_log("Cannot configure output using weston_drm_output_api.\n");
203 ivi->drm_api->set_gbm_format(output->output, gbm_format);
206 ivi->drm_api->set_seat(output->output, seat);
212 #define WINDOWED_DEFAULT_WIDTH 1024
213 #define WINDOWED_DEFAULT_HEIGHT 768
216 windowed_configure_output(struct ivi_output *output)
218 struct ivi_compositor *ivi = output->ivi;
219 struct weston_config_section *section = output->config;
220 int width = WINDOWED_DEFAULT_WIDTH;
221 int height = WINDOWED_DEFAULT_HEIGHT;
226 weston_config_section_get_string(section, "mode", &mode, NULL);
227 if (!mode || sscanf(mode, "%dx%d", &width, &height) != 2) {
228 weston_log("Invalid mode for output %s. Using defaults.\n",
230 width = WINDOWED_DEFAULT_WIDTH;
231 height = WINDOWED_DEFAULT_HEIGHT;
236 if (ivi->cmdline.width)
237 width = ivi->cmdline.width;
238 if (ivi->cmdline.height)
239 height = ivi->cmdline.height;
240 if (ivi->cmdline.scale)
241 weston_output_set_scale(output->output, ivi->cmdline.scale);
243 if (ivi->window_api->output_set_size(output->output, width, height) < 0) {
244 weston_log("Cannot configure output '%s' using weston_windowed_output_api.\n",
253 parse_transform(const char *transform, uint32_t *out)
255 static const struct { const char *name; uint32_t token; } transforms[] = {
256 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
257 { "90", WL_OUTPUT_TRANSFORM_90 },
258 { "180", WL_OUTPUT_TRANSFORM_180 },
259 { "270", WL_OUTPUT_TRANSFORM_270 },
260 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
261 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
262 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
263 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
266 for (size_t i = 0; i < ARRAY_LENGTH(transforms); i++)
267 if (strcmp(transforms[i].name, transform) == 0) {
268 *out = transforms[i].token;
272 *out = WL_OUTPUT_TRANSFORM_NORMAL;
277 configure_output(struct ivi_output *output)
279 struct ivi_compositor *ivi = output->ivi;
280 struct weston_config_section *section = output->config;
282 uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
285 * This can happen with the wayland backend with 'sprawl'. The config
286 * is hard-coded, so we don't need to do anything.
288 if (!ivi->drm_api && !ivi->window_api)
294 weston_config_section_get_int(section, "scale", &scale, 1);
295 weston_config_section_get_string(section, "transform", &t, "normal");
296 if (parse_transform(t, &transform) < 0)
297 weston_log("Invalid transform \"%s\" for output %s\n",
302 weston_output_set_scale(output->output, scale);
303 weston_output_set_transform(output->output, transform);
306 return drm_configure_output(output);
308 return windowed_configure_output(output);
312 * Reorgainizes the output's add array into two sections.
313 * add[0..ret-1] are the heads that failed to get attached.
314 * add[ret..add_len] are the heads that were successfully attached.
316 * The order between elements in each section is stable.
319 try_attach_heads(struct ivi_output *output)
323 for (size_t i = 0; i < output->add_len; ++i) {
324 if (weston_output_attach_head(output->output, output->add[i]) < 0) {
325 struct weston_head *tmp = output->add[i];
326 memmove(&output->add[fail_len + 1], output->add[fail_len],
327 sizeof output->add[0] * (i - fail_len));
328 output->add[fail_len++] = tmp;
336 * Like try_attach_heads, this reorganizes the output's add array into a failed
337 * and successful section.
338 * i is the number of heads that already failed the previous step.
341 try_enable_output(struct ivi_output *output, size_t i)
343 for (; i < output->add_len; ++i) {
344 struct weston_head *head;
346 if (weston_output_enable(output->output) == 0)
349 head = output->add[output->add_len - 1];
350 memmove(&output->add[i + 1], &output->add[i],
351 sizeof output->add[0] * (output->add_len - i));
352 output->add[i] = head;
354 weston_head_detach(head);
361 try_attach_enable_heads(struct ivi_output *output)
364 assert(!output->output->enabled);
366 fail_len = try_attach_heads(output);
368 if (configure_output(output) < 0)
371 fail_len = try_enable_output(output, fail_len);
373 /* All heads failed to be attached */
374 if (fail_len == output->add_len)
377 /* For each successful head attached */
378 for (size_t i = fail_len; i < output->add_len; ++i)
379 add_head_destroyed_listener(output->add[i]);
381 output->add_len = fail_len;
386 process_output(struct ivi_output *output)
388 if (output->output->enabled) {
389 output->add_len = try_attach_heads(output);
390 return output->add_len == 0 ? 0 : -1;
393 return try_attach_enable_heads(output);
397 head_disable(struct ivi_compositor *ivi, struct weston_head *head)
399 struct weston_output *output;
400 struct ivi_output *ivi_output;
401 struct wl_listener *listener;
403 output = weston_head_get_output(head);
406 listener = weston_output_get_destroy_listener(output,
407 handle_output_destroy);
410 ivi_output = wl_container_of(listener, ivi_output, output_destroy);
411 assert(ivi_output->output == output);
413 weston_head_detach(head);
414 if (count_heads(ivi_output->output) == 0) {
415 weston_output_disable(ivi_output->output);
419 static struct weston_config_section *
420 find_controlling_output_config(struct weston_config *config,
423 struct weston_config_section *section;
427 same_as = strdup(name);
429 section = weston_config_get_section(config, "output",
431 if (!section && depth > 0)
432 weston_log("Configuration error: output section reffered"
433 "to by same-as=%s not found.\n", same_as);
440 weston_log("Configuration error: same-as nested too "
441 "deep for output '%s'.\n", name);
445 weston_config_section_get_string(section, "same-as",
453 head_prepare_enable(struct ivi_compositor *ivi, struct weston_head *head)
455 const char *name = weston_head_get_name(head);
456 struct weston_config_section *section;
457 struct ivi_output *output;
458 char *output_name = NULL;
460 section = find_controlling_output_config(ivi->config, name);
464 weston_config_section_get_string(section, "mode", &mode, NULL);
465 if (mode && strcmp(mode, "off") == 0) {
471 weston_config_section_get_string(section, "name",
474 output_name = strdup(name);
480 output = ivi_ensure_output(ivi, output_name, section);
484 if (output->add_len >= ARRAY_LENGTH(output->add))
487 output->add[output->add_len++] = head;
491 heads_changed(struct wl_listener *listener, void *arg)
493 struct weston_compositor *compositor = arg;
494 struct weston_head *head = NULL;
495 struct ivi_compositor *ivi = to_ivi_compositor(compositor);
496 struct ivi_output *output;
498 while ((head = weston_compositor_iterate_heads(ivi->compositor, head))) {
499 bool connected = weston_head_is_connected(head);
500 bool enabled = weston_head_is_enabled(head);
501 bool changed = weston_head_is_device_changed(head);
502 bool non_desktop = weston_head_is_non_desktop(head);
504 if (connected && !enabled && !non_desktop)
505 head_prepare_enable(ivi, head);
506 else if (!connected && enabled)
507 head_disable(ivi, head);
508 else if (enabled && changed)
509 weston_log("Detected a monitor change on head '%s', "
510 "not bothering to do anything about it.\n",
511 weston_head_get_name(head));
513 weston_head_reset_device_changed(head);
516 wl_list_for_each(output, &ivi->outputs, link) {
517 if (output->add_len == 0)
520 if (process_output(output) < 0) {
522 ivi->init_failed = true;
528 load_drm_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
530 struct weston_drm_backend_config config = {
532 .struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION,
533 .struct_size = sizeof config,
536 struct weston_config_section *section;
537 int use_current_mode = 0;
542 const struct weston_option options[] = {
543 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
544 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
545 { WESTON_OPTION_STRING, "drm-device", 0, &config.specific_device },
546 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &use_current_mode },
547 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
550 parse_options(options, ARRAY_LENGTH(options), argc, argv);
551 config.use_pixman = use_pixman;
552 ivi->cmdline.use_current_mode = use_current_mode;
554 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
555 weston_config_section_get_string(section, "gbm-format",
556 &config.gbm_format, NULL);
557 weston_config_section_get_uint(section, "pageflip-timeout",
558 &config.pageflip_timeout, 0);
559 weston_config_section_get_bool(section, "pixman-shadow", &use_shadow, 1);
560 config.use_pixman_shadow = use_shadow;
562 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_DRM,
567 ivi->drm_api = weston_drm_output_get_api(ivi->compositor);
569 weston_log("Cannot use drm output api.\n");
575 free(config.gbm_format);
576 free(config.seat_id);
581 windowed_parse_common_options(struct ivi_compositor *ivi, int *argc, char *argv[],
582 bool *use_pixman, bool *fullscreen, int *output_count)
584 struct weston_config_section *section;
588 const struct weston_option options[] = {
589 { WESTON_OPTION_INTEGER, "width", 0, &ivi->cmdline.width },
590 { WESTON_OPTION_INTEGER, "height", 0, &ivi->cmdline.height },
591 { WESTON_OPTION_INTEGER, "scale", 0, &ivi->cmdline.scale },
592 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &pixman },
593 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fs },
594 { WESTON_OPTION_INTEGER, "output-count", 0, output_count },
597 section = weston_config_get_section(ivi->config, "core", NULL, NULL);
598 weston_config_section_get_bool(section, "use-pixman", &pixman, 0);
601 parse_options(options, ARRAY_LENGTH(options), argc, argv);
602 *use_pixman = pixman;
607 windowed_create_outputs(struct ivi_compositor *ivi, int output_count,
608 const char *match_prefix, const char *name_prefix)
610 struct weston_config_section *section = NULL;
611 const char *section_name;
612 char *default_output = NULL;
614 size_t match_len = strlen(match_prefix);
616 while (weston_config_next_section(ivi->config, §ion, §ion_name)) {
619 if (i >= output_count)
622 if (strcmp(section_name, "output") != 0)
625 weston_config_section_get_string(section, "name", &output_name, NULL);
626 if (output_name == NULL)
628 if (strncmp(output_name, match_prefix, match_len) != 0) {
633 if (ivi->window_api->create_head(ivi->compositor, output_name) < 0) {
642 for (; i < output_count; ++i) {
643 if (asprintf(&default_output, "%s%d", name_prefix, i) < 0)
646 if (ivi->window_api->create_head(ivi->compositor, default_output) < 0) {
647 free(default_output);
651 free(default_output);
658 load_wayland_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
660 struct weston_wayland_backend_config config = {
662 .struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION,
663 .struct_size = sizeof config,
666 struct weston_config_section *section;
671 const struct weston_option options[] = {
672 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
673 { WESTON_OPTION_STRING, "sprawl", 0, &sprawl },
676 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
677 &config.fullscreen, &output_count);
679 parse_options(options, ARRAY_LENGTH(options), argc, argv);
680 config.sprawl = sprawl;
682 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
683 weston_config_section_get_string(section, "cursor-theme",
684 &config.cursor_theme, NULL);
685 weston_config_section_get_int(section, "cursor-size",
686 &config.cursor_size, 32);
688 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_WAYLAND,
691 free(config.cursor_theme);
692 free(config.display_name);
697 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
700 * We will just assume if load_backend() finished cleanly and
701 * windowed_output_api is not present that wayland backend is started
702 * with --sprawl or runs on fullscreen-shell. In this case, all values
703 * are hardcoded, so nothing can be configured; simply create and
706 if (ivi->window_api == NULL)
709 return windowed_create_outputs(ivi, output_count, "WL", "wayland");
713 load_x11_backend(struct ivi_compositor *ivi, int *argc, char *argv[])
715 struct weston_x11_backend_config config = {
717 .struct_version = WESTON_X11_BACKEND_CONFIG_VERSION,
718 .struct_size = sizeof config,
725 const struct weston_option options[] = {
726 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
729 windowed_parse_common_options(ivi, argc, argv, &config.use_pixman,
730 &config.fullscreen, &output_count);
732 parse_options(options, ARRAY_LENGTH(options), argc, argv);
733 config.no_input = no_input;
735 ret = weston_compositor_load_backend(ivi->compositor, WESTON_BACKEND_X11,
741 ivi->window_api = weston_windowed_output_get_api(ivi->compositor);
742 if (!ivi->window_api) {
743 weston_log("Cannot use weston_windowed_output_api.\n");
747 return windowed_create_outputs(ivi, output_count, "X", "screen");
751 load_backend(struct ivi_compositor *ivi, const char *backend,
752 int *argc, char *argv[])
754 if (strcmp(backend, "drm-backend.so") == 0) {
755 return load_drm_backend(ivi, argc, argv);
756 } else if (strcmp(backend, "wayland-backend.so") == 0) {
757 return load_wayland_backend(ivi, argc, argv);
758 } else if (strcmp(backend, "x11-backend.so") == 0) {
759 return load_x11_backend(ivi, argc, argv);
762 weston_log("fatal: unknown backend '%s'.\n", backend);
767 choose_default_backend(void)
769 char *backend = NULL;
771 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
772 backend = strdup("wayland-backend.so");
773 else if (getenv("DISPLAY"))
774 backend = strdup("x11-backend.so");
776 backend = strdup("drm-backend.so");
782 compositor_init_config(struct weston_compositor *compositor,
783 struct weston_config *config)
785 struct xkb_rule_names xkb_names;
786 struct weston_config_section *section;
791 /* agl-compositor.ini [keyboard] */
792 section = weston_config_get_section(config, "keyboard", NULL, NULL);
793 weston_config_section_get_string(section, "keymap_rules",
794 (char **) &xkb_names.rules, NULL);
795 weston_config_section_get_string(section, "keymap_model",
796 (char **) &xkb_names.model, NULL);
797 weston_config_section_get_string(section, "keymap_layout",
798 (char **) &xkb_names.layout, NULL);
799 weston_config_section_get_string(section, "keymap_variant",
800 (char **) &xkb_names.variant, NULL);
801 weston_config_section_get_string(section, "keymap_options",
802 (char **) &xkb_names.options, NULL);
804 if (weston_compositor_set_xkb_rule_names(compositor, &xkb_names) < 0)
807 weston_config_section_get_int(section, "repeat-rate",
808 &compositor->kb_repeat_rate, 40);
809 weston_config_section_get_int(section, "repeat-delay",
810 &compositor->kb_repeat_delay, 400);
812 weston_config_section_get_bool(section, "vt-switching",
813 &vt_switching, true);
814 compositor->vt_switching = vt_switching;
816 /* agl-compositor.ini [core] */
817 section = weston_config_get_section(config, "core", NULL, NULL);
819 weston_config_section_get_bool(section, "require-input", &require_input, true);
820 compositor->require_input = require_input;
822 weston_config_section_get_int(section, "repaint-window", &repaint_msec,
823 compositor->repaint_msec);
824 if (repaint_msec < -10 || repaint_msec > 1000) {
825 weston_log("Invalid repaint_window value in config: %d\n",
828 compositor->repaint_msec = repaint_msec;
830 weston_log("Output repaint window is %d ms maximum.\n",
831 compositor->repaint_msec);
837 to_ivi_surface(struct weston_surface *surface)
839 struct weston_desktop_surface *dsurface;
841 dsurface = weston_surface_get_desktop_surface(surface);
845 return weston_desktop_surface_get_user_data(dsurface);
849 activate_binding(struct weston_seat *seat,
850 struct weston_view *focus_view)
852 struct weston_surface *focus = focus_view->surface;
853 struct weston_surface *main_surface =
854 weston_surface_get_main_surface(focus);
855 struct ivi_surface *surface;
857 surface = to_ivi_surface(main_surface);
861 weston_seat_set_keyboard_focus(seat, focus);
865 click_to_activate_binding(struct weston_pointer *pointer,
866 const struct timespec *time,
867 uint32_t button, void *data)
869 if (pointer->grab != &pointer->default_grab)
871 if (pointer->focus == NULL)
874 activate_binding(pointer->seat, pointer->focus);
878 touch_to_activate_binding(struct weston_touch *touch,
879 const struct timespec *time,
882 if (touch->grab != &touch->default_grab)
884 if (touch->focus == NULL)
887 activate_binding(touch->seat, touch->focus);
891 add_bindings(struct weston_compositor *compositor)
893 weston_compositor_add_button_binding(compositor, BTN_LEFT, 0,
894 click_to_activate_binding,
896 weston_compositor_add_button_binding(compositor, BTN_RIGHT, 0,
897 click_to_activate_binding,
899 weston_compositor_add_touch_binding(compositor, 0,
900 touch_to_activate_binding,
905 create_listening_socket(struct wl_display *display, const char *socket_name)
908 if (wl_display_add_socket(display, socket_name)) {
909 weston_log("fatal: failed to add socket: %s\n",
914 socket_name = wl_display_add_socket_auto(display);
916 weston_log("fatal: failed to add socket: %s\n",
922 setenv("WAYLAND_DISPLAY", socket_name, 1);
928 global_filter(const struct wl_client *client, const struct wl_global *global,
935 load_config(struct weston_config **config, bool no_config,
936 const char *config_file)
938 const char *file = "agl-compositor.ini";
939 const char *full_path;
945 *config = weston_config_parse(file);
948 full_path = weston_config_get_full_path(*config);
950 weston_log("Using config file '%s'.\n", full_path);
951 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
956 if (config_file && !no_config) {
957 weston_log("fatal: error opening or reading config file '%s'.\n",
963 weston_log("Starting with no config file.\n");
964 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
969 static FILE *logfile;
970 //static struct weston_log_scope *log_scope;
971 //static struct weston_log_scope *protocol_scope;
976 static int cached_tm_mday = -1;
978 struct tm brokendown_time;
981 clock_gettime(CLOCK_REALTIME, &ts);
982 if (!localtime_r(&ts.tv_sec, &brokendown_time))
983 return fprintf(logfile, "[(NULL)localtime] ");
985 if (brokendown_time.tm_mday != cached_tm_mday) {
986 strftime(buf, sizeof buf, "%Y-%m-%d %Z", &brokendown_time);
987 fprintf(logfile, "Date: %s\n", buf);
989 cached_tm_mday = brokendown_time.tm_mday;
992 strftime(buf, sizeof buf, "%H:%M:%S", &brokendown_time);
994 return fprintf(logfile, "[%s.%03ld] ", buf, ts.tv_nsec / 1000000);
998 custom_handler(const char *fmt, va_list arg)
1001 fprintf(logfile, "libwayland: ");
1002 vfprintf(logfile, fmt, arg);
1006 log_file_open(const char *filename)
1008 wl_log_set_handler_server(custom_handler);
1011 logfile = fopen(filename, "a");
1016 os_fd_set_cloexec(fileno(logfile));
1017 setvbuf(logfile, NULL, _IOLBF, 256);
1022 log_file_close(void)
1024 if (logfile && logfile != stderr)
1030 vlog(const char *fmt, va_list ap)
1034 l = log_timestamp();
1035 l += vfprintf(logfile, fmt, ap);
1041 vlog_continue(const char *fmt, va_list ap)
1043 return vfprintf(logfile, fmt, ap);
1047 on_term_signal(int signo, void *data)
1049 struct wl_display *display = data;
1051 weston_log("caught signal %d\n", signo);
1052 wl_display_terminate(display);
1058 handle_exit(struct weston_compositor *compositor)
1060 wl_display_terminate(compositor->wl_display);
1064 usage(int error_code)
1066 FILE *out = error_code == EXIT_SUCCESS ? stdout : stderr;
1068 "Usage: agl-compositor [OPTIONS]\n"
1070 "This is " PACKAGE_STRING ", the reference compositor for\n"
1071 "Automotive Grade Linux. Weston-ivi supports multiple backends, and depending\n"
1072 "on which backend is in use different options will be accepted.\n"
1076 " --version\t\tPrint agl-compositor version\n"
1077 " -B, --backend=MODULE\tBackend module, one of\n"
1078 "\t\t\t\tdrm-backend.so\n"
1079 "\t\t\t\twayland-backend.so\n"
1080 "\t\t\t\tx11-backend.so\n"
1081 " -S, --socket=NAME\tName of socket to listen on\n"
1082 " --log=FILE\t\tLog to the given file\n"
1083 " -c, --config=FILE\tConfig file to load, defaults to agl-compositor.ini\n"
1084 " --no-config\t\tDo not read agl-compositor.ini\n"
1085 " --debug\t\tEnable debug extension\n"
1086 " -h, --help\t\tThis help message\n"
1092 ivi_compositor_get_quirks(struct ivi_compositor *ivi)
1094 struct weston_config_section *section;
1099 section = weston_config_get_section(ivi->config, "shell", NULL, NULL);
1100 weston_config_section_get_bool(section, "activate-by-default",
1101 &ivi->quirks.activate_apps_by_default, 0);
1105 int main(int argc, char *argv[])
1107 struct ivi_compositor ivi = { 0 };
1108 struct wl_display *display = NULL;
1109 struct wl_event_loop *loop;
1110 struct wl_event_source *signals[3] = { 0 };
1111 struct weston_config_section *section;
1112 /* Command line options */
1113 char *backend = NULL;
1114 char *socket_name = NULL;
1119 char *config_file = NULL;
1120 int debug_protocol = 0;
1122 const struct weston_option core_options[] = {
1123 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1124 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1125 { WESTON_OPTION_STRING, "log", 0, &log },
1126 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1127 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1128 { WESTON_OPTION_BOOLEAN, "no-config", 0, &no_config },
1129 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1130 { WESTON_OPTION_BOOLEAN, "debug", 0, &debug_protocol },
1133 wl_list_init(&ivi.outputs);
1134 wl_list_init(&ivi.surfaces);
1135 wl_list_init(&ivi.pending_surfaces);
1137 /* Prevent any clients we spawn getting our stdin */
1138 os_fd_set_cloexec(STDIN_FILENO);
1140 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1143 usage(EXIT_SUCCESS);
1146 printf(PACKAGE_STRING "\n");
1147 return EXIT_SUCCESS;
1151 weston_log_set_handler(vlog, vlog_continue);
1153 if (load_config(&ivi.config, no_config, config_file) < 0)
1155 section = weston_config_get_section(ivi.config, "core", NULL, NULL);
1157 weston_config_section_get_string(section, "backend", &backend,
1160 backend = choose_default_backend();
1163 ivi_compositor_get_quirks(&ivi);
1165 display = wl_display_create();
1166 loop = wl_display_get_event_loop(display);
1168 wl_display_set_global_filter(display,
1169 global_filter, &ivi);
1171 /* Register signal handlers so we shut down cleanly */
1173 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1175 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1177 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1180 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1184 ivi.compositor = weston_compositor_create(display, &ivi);
1185 if (!ivi.compositor) {
1186 weston_log("fatal: failed to create compositor.\n");
1190 if (compositor_init_config(ivi.compositor, ivi.config) < 0)
1191 goto error_compositor;
1193 if (load_backend(&ivi, backend, &argc, argv) < 0) {
1194 weston_log("fatal: failed to create compositor backend.\n");
1195 goto error_compositor;
1198 ivi.heads_changed.notify = heads_changed;
1199 weston_compositor_add_heads_changed_listener(ivi.compositor,
1200 &ivi.heads_changed);
1202 if (ivi_desktop_init(&ivi) < 0)
1203 goto error_compositor;
1205 if (ivi_shell_init(&ivi) < 0)
1206 goto error_compositor;
1208 add_bindings(ivi.compositor);
1210 weston_compositor_flush_heads_changed(ivi.compositor);
1212 ivi_shell_init_black_fs(&ivi);
1214 if (create_listening_socket(display, socket_name) < 0)
1215 goto error_compositor;
1217 ivi.compositor->exit = handle_exit;
1219 weston_compositor_wake(ivi.compositor);
1221 ivi_shell_create_global(&ivi);
1222 ivi_launch_shell_client(&ivi);
1223 ivi_agl_systemd_notify(&ivi);
1225 wl_display_run(display);
1227 wl_display_destroy_clients(display);
1230 weston_compositor_destroy(ivi.compositor);
1233 for (size_t i = 0; i < ARRAY_LENGTH(signals); ++i)
1235 wl_event_source_remove(signals[i]);
1237 wl_display_destroy(display);
1241 weston_config_destroy(ivi.config);