Giulio Camuffo | bab996e | 2014-10-12 00:24:25 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010-2011 Intel Corporation |
| 3 | * Copyright © 2008-2011 Kristian Høgsberg |
| 4 | * Copyright © 2012-2015 Collabora, Ltd. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining |
| 7 | * a copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial |
| 16 | * portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | |
| 30 | #include <unistd.h> |
| 31 | #include <signal.h> |
| 32 | #include <errno.h> |
| 33 | #include <dlfcn.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/utsname.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/wait.h> |
| 38 | |
| 39 | #ifdef HAVE_LIBUNWIND |
| 40 | #define UNW_LOCAL_ONLY |
| 41 | #include <libunwind.h> |
| 42 | #endif |
| 43 | |
| 44 | #include "compositor.h" |
| 45 | #include "../shared/os-compatibility.h" |
| 46 | #include "../shared/helpers.h" |
| 47 | #include "git-version.h" |
| 48 | #include "version.h" |
| 49 | |
| 50 | static struct wl_list child_process_list; |
| 51 | static struct weston_compositor *segv_compositor; |
| 52 | |
| 53 | static int |
| 54 | sigchld_handler(int signal_number, void *data) |
| 55 | { |
| 56 | struct weston_process *p; |
| 57 | int status; |
| 58 | pid_t pid; |
| 59 | |
| 60 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 61 | wl_list_for_each(p, &child_process_list, link) { |
| 62 | if (p->pid == pid) |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | if (&p->link == &child_process_list) { |
| 67 | weston_log("unknown child process exited\n"); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | wl_list_remove(&p->link); |
| 72 | p->cleanup(p, status); |
| 73 | } |
| 74 | |
| 75 | if (pid < 0 && errno != ECHILD) |
| 76 | weston_log("waitpid error %m\n"); |
| 77 | |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | #ifdef HAVE_LIBUNWIND |
| 82 | |
| 83 | static void |
| 84 | print_backtrace(void) |
| 85 | { |
| 86 | unw_cursor_t cursor; |
| 87 | unw_context_t context; |
| 88 | unw_word_t off; |
| 89 | unw_proc_info_t pip; |
| 90 | int ret, i = 0; |
| 91 | char procname[256]; |
| 92 | const char *filename; |
| 93 | Dl_info dlinfo; |
| 94 | |
| 95 | pip.unwind_info = NULL; |
| 96 | ret = unw_getcontext(&context); |
| 97 | if (ret) { |
| 98 | weston_log("unw_getcontext: %d\n", ret); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | ret = unw_init_local(&cursor, &context); |
| 103 | if (ret) { |
| 104 | weston_log("unw_init_local: %d\n", ret); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | ret = unw_step(&cursor); |
| 109 | while (ret > 0) { |
| 110 | ret = unw_get_proc_info(&cursor, &pip); |
| 111 | if (ret) { |
| 112 | weston_log("unw_get_proc_info: %d\n", ret); |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | ret = unw_get_proc_name(&cursor, procname, 256, &off); |
| 117 | if (ret && ret != -UNW_ENOMEM) { |
| 118 | if (ret != -UNW_EUNSPEC) |
| 119 | weston_log("unw_get_proc_name: %d\n", ret); |
| 120 | procname[0] = '?'; |
| 121 | procname[1] = 0; |
| 122 | } |
| 123 | |
| 124 | if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname && |
| 125 | *dlinfo.dli_fname) |
| 126 | filename = dlinfo.dli_fname; |
| 127 | else |
| 128 | filename = "?"; |
| 129 | |
| 130 | weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname, |
| 131 | ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off)); |
| 132 | |
| 133 | ret = unw_step(&cursor); |
| 134 | if (ret < 0) |
| 135 | weston_log("unw_step: %d\n", ret); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #else |
| 140 | |
| 141 | static void |
| 142 | print_backtrace(void) |
| 143 | { |
| 144 | void *buffer[32]; |
| 145 | int i, count; |
| 146 | Dl_info info; |
| 147 | |
| 148 | count = backtrace(buffer, ARRAY_LENGTH(buffer)); |
| 149 | for (i = 0; i < count; i++) { |
| 150 | dladdr(buffer[i], &info); |
| 151 | weston_log(" [%016lx] %s (%s)\n", |
| 152 | (long) buffer[i], |
| 153 | info.dli_sname ? info.dli_sname : "--", |
| 154 | info.dli_fname); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #endif |
| 159 | |
| 160 | WL_EXPORT void |
| 161 | weston_watch_process(struct weston_process *process) |
| 162 | { |
| 163 | wl_list_insert(&child_process_list, &process->link); |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | log_uname(void) |
| 168 | { |
| 169 | struct utsname usys; |
| 170 | |
| 171 | uname(&usys); |
| 172 | |
| 173 | weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release, |
| 174 | usys.version, usys.machine); |
| 175 | } |
| 176 | |
| 177 | static const char xdg_error_message[] = |
| 178 | "fatal: environment variable XDG_RUNTIME_DIR is not set.\n"; |
| 179 | |
| 180 | static const char xdg_wrong_message[] = |
| 181 | "fatal: environment variable XDG_RUNTIME_DIR\n" |
| 182 | "is set to \"%s\", which is not a directory.\n"; |
| 183 | |
| 184 | static const char xdg_wrong_mode_message[] = |
| 185 | "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n" |
| 186 | "correctly. Unix access mode must be 0700 (current mode is %o),\n" |
| 187 | "and must be owned by the user (current owner is UID %d).\n"; |
| 188 | |
| 189 | static const char xdg_detail_message[] = |
| 190 | "Refer to your distribution on how to get it, or\n" |
| 191 | "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n" |
| 192 | "on how to implement it.\n"; |
| 193 | |
| 194 | static void |
| 195 | verify_xdg_runtime_dir(void) |
| 196 | { |
| 197 | char *dir = getenv("XDG_RUNTIME_DIR"); |
| 198 | struct stat s; |
| 199 | |
| 200 | if (!dir) { |
| 201 | weston_log(xdg_error_message); |
| 202 | weston_log_continue(xdg_detail_message); |
| 203 | exit(EXIT_FAILURE); |
| 204 | } |
| 205 | |
| 206 | if (stat(dir, &s) || !S_ISDIR(s.st_mode)) { |
| 207 | weston_log(xdg_wrong_message, dir); |
| 208 | weston_log_continue(xdg_detail_message); |
| 209 | exit(EXIT_FAILURE); |
| 210 | } |
| 211 | |
| 212 | if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) { |
| 213 | weston_log(xdg_wrong_mode_message, |
| 214 | dir, s.st_mode & 0777, s.st_uid); |
| 215 | weston_log_continue(xdg_detail_message); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static int |
| 220 | usage(int error_code) |
| 221 | { |
| 222 | fprintf(stderr, |
| 223 | "Usage: weston [OPTIONS]\n\n" |
| 224 | "This is weston version " VERSION ", the Wayland reference compositor.\n" |
| 225 | "Weston supports multiple backends, and depending on which backend is in use\n" |
| 226 | "different options will be accepted.\n\n" |
| 227 | |
| 228 | |
| 229 | "Core options:\n\n" |
| 230 | " --version\t\tPrint weston version\n" |
| 231 | " -B, --backend=MODULE\tBackend module, one of\n" |
| 232 | #if defined(BUILD_DRM_COMPOSITOR) |
| 233 | "\t\t\t\tdrm-backend.so\n" |
| 234 | #endif |
| 235 | #if defined(BUILD_FBDEV_COMPOSITOR) |
| 236 | "\t\t\t\tfbdev-backend.so\n" |
| 237 | #endif |
| 238 | #if defined(BUILD_X11_COMPOSITOR) |
| 239 | "\t\t\t\tx11-backend.so\n" |
| 240 | #endif |
| 241 | #if defined(BUILD_WAYLAND_COMPOSITOR) |
| 242 | "\t\t\t\twayland-backend.so\n" |
| 243 | #endif |
| 244 | #if defined(BUILD_RDP_COMPOSITOR) |
| 245 | "\t\t\t\trdp-backend.so\n" |
| 246 | #endif |
| 247 | #if defined(BUILD_HEADLESS_COMPOSITOR) |
| 248 | "\t\t\t\theadless-backend.so\n" |
| 249 | #endif |
| 250 | #if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST) |
| 251 | "\t\t\t\trpi-backend.so\n" |
| 252 | #endif |
| 253 | " --shell=MODULE\tShell module, defaults to desktop-shell.so\n" |
| 254 | " -S, --socket=NAME\tName of socket to listen on\n" |
| 255 | " -i, --idle-time=SECS\tIdle time in seconds\n" |
| 256 | " --modules\t\tLoad the comma-separated list of modules\n" |
| 257 | " --log=FILE\t\tLog to the given file\n" |
| 258 | " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n" |
| 259 | " --no-config\t\tDo not read weston.ini\n" |
| 260 | " -h, --help\t\tThis help message\n\n"); |
| 261 | |
| 262 | #if defined(BUILD_DRM_COMPOSITOR) |
| 263 | fprintf(stderr, |
| 264 | "Options for drm-backend.so:\n\n" |
| 265 | " --connector=ID\tBring up only this connector\n" |
| 266 | " --seat=SEAT\t\tThe seat that weston should run on\n" |
| 267 | " --tty=TTY\t\tThe tty to use\n" |
| 268 | " --use-pixman\t\tUse the pixman (CPU) renderer\n" |
| 269 | " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n"); |
| 270 | #endif |
| 271 | |
| 272 | #if defined(BUILD_FBDEV_COMPOSITOR) |
| 273 | fprintf(stderr, |
| 274 | "Options for fbdev-backend.so:\n\n" |
| 275 | " --tty=TTY\t\tThe tty to use\n" |
| 276 | " --device=DEVICE\tThe framebuffer device to use\n" |
| 277 | " --use-gl\t\tUse the GL renderer\n\n"); |
| 278 | #endif |
| 279 | |
| 280 | #if defined(BUILD_X11_COMPOSITOR) |
| 281 | fprintf(stderr, |
| 282 | "Options for x11-backend.so:\n\n" |
| 283 | " --width=WIDTH\t\tWidth of X window\n" |
| 284 | " --height=HEIGHT\tHeight of X window\n" |
| 285 | " --scale=SCALE\t\tScale factor of output\n" |
| 286 | " --fullscreen\t\tRun in fullscreen mode\n" |
| 287 | " --use-pixman\t\tUse the pixman (CPU) renderer\n" |
| 288 | " --output-count=COUNT\tCreate multiple outputs\n" |
| 289 | " --no-input\t\tDont create input devices\n\n"); |
| 290 | #endif |
| 291 | |
| 292 | #if defined(BUILD_WAYLAND_COMPOSITOR) |
| 293 | fprintf(stderr, |
| 294 | "Options for wayland-backend.so:\n\n" |
| 295 | " --width=WIDTH\t\tWidth of Wayland surface\n" |
| 296 | " --height=HEIGHT\tHeight of Wayland surface\n" |
| 297 | " --scale=SCALE\t\tScale factor of output\n" |
| 298 | " --fullscreen\t\tRun in fullscreen mode\n" |
| 299 | " --use-pixman\t\tUse the pixman (CPU) renderer\n" |
| 300 | " --output-count=COUNT\tCreate multiple outputs\n" |
| 301 | " --sprawl\t\tCreate one fullscreen output for every parent output\n" |
| 302 | " --display=DISPLAY\tWayland display to connect to\n\n"); |
| 303 | #endif |
| 304 | |
| 305 | #if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST) |
| 306 | fprintf(stderr, |
| 307 | "Options for rpi-backend.so:\n\n" |
| 308 | " --tty=TTY\t\tThe tty to use\n" |
| 309 | " --single-buffer\tUse single-buffered Dispmanx elements.\n" |
| 310 | " --transform=TR\tThe output transformation, TR is one of:\n" |
| 311 | "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n" |
| 312 | " --opaque-regions\tEnable support for opaque regions, can be " |
| 313 | "very slow without support in the GPU firmware.\n" |
| 314 | "\n"); |
| 315 | #endif |
| 316 | |
| 317 | #if defined(BUILD_RDP_COMPOSITOR) |
| 318 | fprintf(stderr, |
| 319 | "Options for rdp-backend.so:\n\n" |
| 320 | " --width=WIDTH\t\tWidth of desktop\n" |
| 321 | " --height=HEIGHT\tHeight of desktop\n" |
Dawid Gajownik | d99a050 | 2015-07-31 14:49:57 -0300 | [diff] [blame^] | 322 | " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n" |
Giulio Camuffo | bab996e | 2014-10-12 00:24:25 +0300 | [diff] [blame] | 323 | " --address=ADDR\tThe address to bind\n" |
| 324 | " --port=PORT\t\tThe port to listen on\n" |
| 325 | " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n" |
| 326 | " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n" |
| 327 | " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n" |
| 328 | " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n" |
| 329 | "\n"); |
| 330 | #endif |
| 331 | |
| 332 | #if defined(BUILD_HEADLESS_COMPOSITOR) |
| 333 | fprintf(stderr, |
| 334 | "Options for headless-backend.so:\n\n" |
| 335 | " --width=WIDTH\t\tWidth of memory surface\n" |
| 336 | " --height=HEIGHT\tHeight of memory surface\n" |
| 337 | " --transform=TR\tThe output transformation, TR is one of:\n" |
| 338 | "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n" |
| 339 | " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n"); |
| 340 | #endif |
| 341 | |
| 342 | exit(error_code); |
| 343 | } |
| 344 | |
| 345 | static int on_term_signal(int signal_number, void *data) |
| 346 | { |
| 347 | struct wl_display *display = data; |
| 348 | |
| 349 | weston_log("caught signal %d\n", signal_number); |
| 350 | wl_display_terminate(display); |
| 351 | |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | static void |
| 356 | on_caught_signal(int s, siginfo_t *siginfo, void *context) |
| 357 | { |
| 358 | /* This signal handler will do a best-effort backtrace, and |
| 359 | * then call the backend restore function, which will switch |
| 360 | * back to the vt we launched from or ungrab X etc and then |
| 361 | * raise SIGTRAP. If we run weston under gdb from X or a |
| 362 | * different vt, and tell gdb "handle *s* nostop", this |
| 363 | * will allow weston to switch back to gdb on crash and then |
| 364 | * gdb will catch the crash with SIGTRAP.*/ |
| 365 | |
| 366 | weston_log("caught signal: %d\n", s); |
| 367 | |
| 368 | print_backtrace(); |
| 369 | |
| 370 | segv_compositor->backend->restore(segv_compositor); |
| 371 | |
| 372 | raise(SIGTRAP); |
| 373 | } |
| 374 | |
| 375 | static void |
| 376 | catch_signals(void) |
| 377 | { |
| 378 | struct sigaction action; |
| 379 | |
| 380 | action.sa_flags = SA_SIGINFO | SA_RESETHAND; |
| 381 | action.sa_sigaction = on_caught_signal; |
| 382 | sigemptyset(&action.sa_mask); |
| 383 | sigaction(SIGSEGV, &action, NULL); |
| 384 | sigaction(SIGABRT, &action, NULL); |
| 385 | } |
| 386 | |
| 387 | static const char * |
| 388 | clock_name(clockid_t clk_id) |
| 389 | { |
| 390 | static const char *names[] = { |
| 391 | [CLOCK_REALTIME] = "CLOCK_REALTIME", |
| 392 | [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC", |
| 393 | [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW", |
| 394 | [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE", |
| 395 | [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE", |
| 396 | [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME", |
| 397 | }; |
| 398 | |
| 399 | if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names)) |
| 400 | return "unknown"; |
| 401 | |
| 402 | return names[clk_id]; |
| 403 | } |
| 404 | |
| 405 | static const struct { |
| 406 | uint32_t bit; /* enum weston_capability */ |
| 407 | const char *desc; |
| 408 | } capability_strings[] = { |
| 409 | { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" }, |
| 410 | { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" }, |
| 411 | }; |
| 412 | |
| 413 | static void |
| 414 | weston_compositor_log_capabilities(struct weston_compositor *compositor) |
| 415 | { |
| 416 | unsigned i; |
| 417 | int yes; |
| 418 | |
| 419 | weston_log("Compositor capabilities:\n"); |
| 420 | for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) { |
| 421 | yes = compositor->capabilities & capability_strings[i].bit; |
| 422 | weston_log_continue(STAMP_SPACE "%s %s\n", |
| 423 | capability_strings[i].desc, |
| 424 | yes ? "yes" : "no"); |
| 425 | } |
| 426 | |
| 427 | weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n", |
| 428 | clock_name(compositor->presentation_clock), |
| 429 | compositor->presentation_clock); |
| 430 | } |
| 431 | |
| 432 | static void |
| 433 | handle_primary_client_destroyed(struct wl_listener *listener, void *data) |
| 434 | { |
| 435 | struct wl_client *client = data; |
| 436 | |
| 437 | weston_log("Primary client died. Closing...\n"); |
| 438 | |
| 439 | wl_display_terminate(wl_client_get_display(client)); |
| 440 | } |
| 441 | |
| 442 | static int |
| 443 | weston_create_listening_socket(struct wl_display *display, const char *socket_name) |
| 444 | { |
| 445 | if (socket_name) { |
| 446 | if (wl_display_add_socket(display, socket_name)) { |
| 447 | weston_log("fatal: failed to add socket: %m\n"); |
| 448 | return -1; |
| 449 | } |
| 450 | } else { |
| 451 | socket_name = wl_display_add_socket_auto(display); |
| 452 | if (!socket_name) { |
| 453 | weston_log("fatal: failed to add socket: %m\n"); |
| 454 | return -1; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | setenv("WAYLAND_DISPLAY", socket_name, 1); |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static int |
| 464 | load_modules(struct weston_compositor *ec, const char *modules, |
| 465 | int *argc, char *argv[]) |
| 466 | { |
| 467 | const char *p, *end; |
| 468 | char buffer[256]; |
| 469 | int (*module_init)(struct weston_compositor *ec, |
| 470 | int *argc, char *argv[]); |
| 471 | |
| 472 | if (modules == NULL) |
| 473 | return 0; |
| 474 | |
| 475 | p = modules; |
| 476 | while (*p) { |
| 477 | end = strchrnul(p, ','); |
| 478 | snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p); |
| 479 | module_init = weston_load_module(buffer, "module_init"); |
| 480 | if (!module_init) |
| 481 | return -1; |
| 482 | if (module_init(ec, argc, argv) < 0) |
| 483 | return -1; |
| 484 | p = end; |
| 485 | while (*p == ',') |
| 486 | p++; |
| 487 | |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int |
| 494 | weston_compositor_init_config(struct weston_compositor *ec, |
| 495 | struct weston_config *config) |
| 496 | { |
| 497 | struct xkb_rule_names xkb_names; |
| 498 | struct weston_config_section *s; |
| 499 | int repaint_msec; |
| 500 | |
| 501 | s = weston_config_get_section(config, "keyboard", NULL, NULL); |
| 502 | weston_config_section_get_string(s, "keymap_rules", |
| 503 | (char **) &xkb_names.rules, NULL); |
| 504 | weston_config_section_get_string(s, "keymap_model", |
| 505 | (char **) &xkb_names.model, NULL); |
| 506 | weston_config_section_get_string(s, "keymap_layout", |
| 507 | (char **) &xkb_names.layout, NULL); |
| 508 | weston_config_section_get_string(s, "keymap_variant", |
| 509 | (char **) &xkb_names.variant, NULL); |
| 510 | weston_config_section_get_string(s, "keymap_options", |
| 511 | (char **) &xkb_names.options, NULL); |
| 512 | |
| 513 | if (weston_compositor_xkb_init(ec, &xkb_names) < 0) |
| 514 | return -1; |
| 515 | |
| 516 | weston_config_section_get_int(s, "repeat-rate", |
| 517 | &ec->kb_repeat_rate, 40); |
| 518 | weston_config_section_get_int(s, "repeat-delay", |
| 519 | &ec->kb_repeat_delay, 400); |
| 520 | |
| 521 | s = weston_config_get_section(config, "core", NULL, NULL); |
| 522 | weston_config_section_get_int(s, "repaint-window", &repaint_msec, |
| 523 | ec->repaint_msec); |
| 524 | if (repaint_msec < -10 || repaint_msec > 1000) { |
| 525 | weston_log("Invalid repaint_window value in config: %d\n", |
| 526 | repaint_msec); |
| 527 | } else { |
| 528 | ec->repaint_msec = repaint_msec; |
| 529 | } |
| 530 | weston_log("Output repaint window is %d ms maximum.\n", |
| 531 | ec->repaint_msec); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static char * |
| 537 | weston_choose_default_backend(void) |
| 538 | { |
| 539 | char *backend = NULL; |
| 540 | |
| 541 | if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET")) |
| 542 | backend = strdup("wayland-backend.so"); |
| 543 | else if (getenv("DISPLAY")) |
| 544 | backend = strdup("x11-backend.so"); |
| 545 | else |
| 546 | backend = strdup(WESTON_NATIVE_BACKEND); |
| 547 | |
| 548 | return backend; |
| 549 | } |
| 550 | |
| 551 | static const struct { const char *name; uint32_t token; } transforms[] = { |
| 552 | { "normal", WL_OUTPUT_TRANSFORM_NORMAL }, |
| 553 | { "90", WL_OUTPUT_TRANSFORM_90 }, |
| 554 | { "180", WL_OUTPUT_TRANSFORM_180 }, |
| 555 | { "270", WL_OUTPUT_TRANSFORM_270 }, |
| 556 | { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED }, |
| 557 | { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 }, |
| 558 | { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 }, |
| 559 | { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 }, |
| 560 | }; |
| 561 | |
| 562 | WL_EXPORT int |
| 563 | weston_parse_transform(const char *transform, uint32_t *out) |
| 564 | { |
| 565 | unsigned int i; |
| 566 | |
| 567 | for (i = 0; i < ARRAY_LENGTH(transforms); i++) |
| 568 | if (strcmp(transforms[i].name, transform) == 0) { |
| 569 | *out = transforms[i].token; |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | *out = WL_OUTPUT_TRANSFORM_NORMAL; |
| 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | WL_EXPORT const char * |
| 578 | weston_transform_to_string(uint32_t output_transform) |
| 579 | { |
| 580 | unsigned int i; |
| 581 | |
| 582 | for (i = 0; i < ARRAY_LENGTH(transforms); i++) |
| 583 | if (transforms[i].token == output_transform) |
| 584 | return transforms[i].name; |
| 585 | |
| 586 | return "<illegal value>"; |
| 587 | } |
| 588 | |
| 589 | static int |
| 590 | load_configuration(struct weston_config **config, int32_t noconfig, |
| 591 | const char *config_file) |
| 592 | { |
| 593 | const char *file = "weston.ini"; |
| 594 | const char *full_path; |
| 595 | |
| 596 | *config = NULL; |
| 597 | |
| 598 | if (config_file) |
| 599 | file = config_file; |
| 600 | |
| 601 | if (noconfig == 0) |
| 602 | *config = weston_config_parse(file); |
| 603 | |
| 604 | if (*config) { |
| 605 | full_path = weston_config_get_full_path(*config); |
| 606 | |
| 607 | weston_log("Using config file '%s'\n", full_path); |
| 608 | setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1); |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | if (config_file && noconfig == 0) { |
| 614 | weston_log("fatal: error opening or reading config file" |
| 615 | " '%s'.\n", config_file); |
| 616 | |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | weston_log("Starting with no config file.\n"); |
| 621 | setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static void |
| 627 | handle_exit(struct weston_compositor *c) |
| 628 | { |
| 629 | wl_display_terminate(c->wl_display); |
| 630 | } |
| 631 | |
| 632 | int main(int argc, char *argv[]) |
| 633 | { |
| 634 | int ret = EXIT_FAILURE; |
| 635 | struct wl_display *display; |
| 636 | struct weston_compositor *ec; |
| 637 | struct wl_event_source *signals[4]; |
| 638 | struct wl_event_loop *loop; |
| 639 | int (*backend_init)(struct weston_compositor *c, |
| 640 | int *argc, char *argv[], |
| 641 | struct weston_config *config); |
| 642 | int i, fd; |
| 643 | char *backend = NULL; |
| 644 | char *shell = NULL; |
| 645 | char *modules = NULL; |
| 646 | char *option_modules = NULL; |
| 647 | char *log = NULL; |
| 648 | char *server_socket = NULL, *end; |
| 649 | int32_t idle_time = -1; |
| 650 | int32_t help = 0; |
| 651 | char *socket_name = NULL; |
| 652 | int32_t version = 0; |
| 653 | int32_t noconfig = 0; |
| 654 | int32_t numlock_on; |
| 655 | char *config_file = NULL; |
| 656 | struct weston_config *config = NULL; |
| 657 | struct weston_config_section *section; |
| 658 | struct wl_client *primary_client; |
| 659 | struct wl_listener primary_client_destroyed; |
| 660 | struct weston_seat *seat; |
| 661 | |
| 662 | const struct weston_option core_options[] = { |
| 663 | { WESTON_OPTION_STRING, "backend", 'B', &backend }, |
| 664 | { WESTON_OPTION_STRING, "shell", 0, &shell }, |
| 665 | { WESTON_OPTION_STRING, "socket", 'S', &socket_name }, |
| 666 | { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time }, |
| 667 | { WESTON_OPTION_STRING, "modules", 0, &option_modules }, |
| 668 | { WESTON_OPTION_STRING, "log", 0, &log }, |
| 669 | { WESTON_OPTION_BOOLEAN, "help", 'h', &help }, |
| 670 | { WESTON_OPTION_BOOLEAN, "version", 0, &version }, |
| 671 | { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig }, |
| 672 | { WESTON_OPTION_STRING, "config", 'c', &config_file }, |
| 673 | }; |
| 674 | |
| 675 | parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv); |
| 676 | |
| 677 | if (help) |
| 678 | usage(EXIT_SUCCESS); |
| 679 | |
| 680 | if (version) { |
| 681 | printf(PACKAGE_STRING "\n"); |
| 682 | return EXIT_SUCCESS; |
| 683 | } |
| 684 | |
| 685 | weston_log_file_open(log); |
| 686 | |
| 687 | weston_log("%s\n" |
| 688 | STAMP_SPACE "%s\n" |
| 689 | STAMP_SPACE "Bug reports to: %s\n" |
| 690 | STAMP_SPACE "Build: %s\n", |
| 691 | PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT, |
| 692 | BUILD_ID); |
| 693 | log_uname(); |
| 694 | |
| 695 | verify_xdg_runtime_dir(); |
| 696 | |
| 697 | display = wl_display_create(); |
| 698 | |
| 699 | loop = wl_display_get_event_loop(display); |
| 700 | signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal, |
| 701 | display); |
| 702 | signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal, |
| 703 | display); |
| 704 | signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal, |
| 705 | display); |
| 706 | |
| 707 | wl_list_init(&child_process_list); |
| 708 | signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler, |
| 709 | NULL); |
| 710 | |
| 711 | if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) |
| 712 | goto out_signals; |
| 713 | |
| 714 | if (load_configuration(&config, noconfig, config_file) < 0) |
| 715 | goto out_signals; |
| 716 | |
| 717 | section = weston_config_get_section(config, "core", NULL, NULL); |
| 718 | |
| 719 | if (!backend) { |
| 720 | weston_config_section_get_string(section, "backend", &backend, |
| 721 | NULL); |
| 722 | if (!backend) |
| 723 | backend = weston_choose_default_backend(); |
| 724 | } |
| 725 | |
| 726 | backend_init = weston_load_module(backend, "backend_init"); |
| 727 | if (!backend_init) |
| 728 | goto out_signals; |
| 729 | |
| 730 | ec = weston_compositor_create(display, NULL); |
| 731 | if (ec == NULL) { |
| 732 | weston_log("fatal: failed to create compositor\n"); |
| 733 | goto out_signals; |
| 734 | } |
| 735 | |
| 736 | ec->config = config; |
| 737 | if (weston_compositor_init_config(ec, config) < 0) |
| 738 | goto out_signals; |
| 739 | |
| 740 | if (backend_init(ec, &argc, argv, config) < 0) { |
| 741 | weston_log("fatal: failed to create compositor backend\n"); |
| 742 | goto out_signals; |
| 743 | } |
| 744 | |
| 745 | catch_signals(); |
| 746 | segv_compositor = ec; |
| 747 | |
| 748 | if (idle_time < 0) |
| 749 | weston_config_section_get_int(section, "idle-time", &idle_time, -1); |
| 750 | if (idle_time < 0) |
| 751 | idle_time = 300; /* default idle timeout, in seconds */ |
| 752 | |
| 753 | ec->idle_time = idle_time; |
| 754 | ec->default_pointer_grab = NULL; |
| 755 | ec->exit = handle_exit; |
| 756 | |
| 757 | weston_compositor_log_capabilities(ec); |
| 758 | |
| 759 | server_socket = getenv("WAYLAND_SERVER_SOCKET"); |
| 760 | if (server_socket) { |
| 761 | weston_log("Running with single client\n"); |
| 762 | fd = strtol(server_socket, &end, 0); |
| 763 | if (*end != '\0') |
| 764 | fd = -1; |
| 765 | } else { |
| 766 | fd = -1; |
| 767 | } |
| 768 | |
| 769 | if (fd != -1) { |
| 770 | primary_client = wl_client_create(display, fd); |
| 771 | if (!primary_client) { |
| 772 | weston_log("fatal: failed to add client: %m\n"); |
| 773 | goto out; |
| 774 | } |
| 775 | primary_client_destroyed.notify = |
| 776 | handle_primary_client_destroyed; |
| 777 | wl_client_add_destroy_listener(primary_client, |
| 778 | &primary_client_destroyed); |
| 779 | } else if (weston_create_listening_socket(display, socket_name)) { |
| 780 | goto out; |
| 781 | } |
| 782 | |
| 783 | if (!shell) |
| 784 | weston_config_section_get_string(section, "shell", &shell, |
| 785 | "desktop-shell.so"); |
| 786 | |
| 787 | if (load_modules(ec, shell, &argc, argv) < 0) |
| 788 | goto out; |
| 789 | |
| 790 | weston_config_section_get_string(section, "modules", &modules, ""); |
| 791 | if (load_modules(ec, modules, &argc, argv) < 0) |
| 792 | goto out; |
| 793 | |
| 794 | if (load_modules(ec, option_modules, &argc, argv) < 0) |
| 795 | goto out; |
| 796 | |
| 797 | section = weston_config_get_section(config, "keyboard", NULL, NULL); |
| 798 | weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0); |
| 799 | if (numlock_on) { |
| 800 | wl_list_for_each(seat, &ec->seat_list, link) { |
| 801 | if (seat->keyboard) |
| 802 | weston_keyboard_set_locks(seat->keyboard, |
| 803 | WESTON_NUM_LOCK, |
| 804 | WESTON_NUM_LOCK); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | for (i = 1; i < argc; i++) |
| 809 | weston_log("fatal: unhandled option: %s\n", argv[i]); |
| 810 | if (argc > 1) |
| 811 | goto out; |
| 812 | |
| 813 | weston_compositor_wake(ec); |
| 814 | |
| 815 | wl_display_run(display); |
| 816 | |
| 817 | /* Allow for setting return exit code after |
| 818 | * wl_display_run returns normally. This is |
| 819 | * useful for devs/testers and automated tests |
| 820 | * that want to indicate failure status to |
| 821 | * testing infrastructure above |
| 822 | */ |
| 823 | ret = ec->exit_code; |
| 824 | |
| 825 | out: |
| 826 | weston_compositor_destroy(ec); |
| 827 | |
| 828 | out_signals: |
| 829 | for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--) |
| 830 | if (signals[i]) |
| 831 | wl_event_source_remove(signals[i]); |
| 832 | |
| 833 | wl_display_destroy(display); |
| 834 | |
| 835 | weston_log_file_close(); |
| 836 | |
| 837 | if (config) |
| 838 | weston_config_destroy(config); |
| 839 | free(config_file); |
| 840 | free(backend); |
| 841 | free(shell); |
| 842 | free(socket_name); |
| 843 | free(option_modules); |
| 844 | free(log); |
| 845 | free(modules); |
| 846 | |
| 847 | return ret; |
| 848 | } |