blob: 1850fa6335543e4909c7fb0ac779ae889818b066 [file] [log] [blame]
Giulio Camuffobab996e2014-10-12 00:24:25 +03001/*
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
50static struct wl_list child_process_list;
51static struct weston_compositor *segv_compositor;
52
53static int
54sigchld_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
83static void
84print_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
141static void
142print_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
160WL_EXPORT void
161weston_watch_process(struct weston_process *process)
162{
163 wl_list_insert(&child_process_list, &process->link);
164}
165
166static void
167log_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
177static const char xdg_error_message[] =
178 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
179
180static 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
184static 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
189static 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
194static void
195verify_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
219static int
220usage(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
Dawid Gajownik71f57042015-07-31 17:39:00 -0300238#if defined(BUILD_HEADLESS_COMPOSITOR)
239 "\t\t\t\theadless-backend.so\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300240#endif
241#if defined(BUILD_RDP_COMPOSITOR)
242 "\t\t\t\trdp-backend.so\n"
243#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300244#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
245 "\t\t\t\trpi-backend.so\n"
246#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300247#if defined(BUILD_WAYLAND_COMPOSITOR)
248 "\t\t\t\twayland-backend.so\n"
249#endif
250#if defined(BUILD_X11_COMPOSITOR)
251 "\t\t\t\tx11-backend.so\n"
252#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300253 " --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
Dawid Gajownik71f57042015-07-31 17:39:00 -0300280#if defined(BUILD_HEADLESS_COMPOSITOR)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300281 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300282 "Options for headless-backend.so:\n\n"
283 " --width=WIDTH\t\tWidth of memory surface\n"
284 " --height=HEIGHT\tHeight of memory surface\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300285 " --transform=TR\tThe output transformation, TR is one of:\n"
286 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Dawid Gajownik71f57042015-07-31 17:39:00 -0300287 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300288#endif
289
290#if defined(BUILD_RDP_COMPOSITOR)
291 fprintf(stderr,
292 "Options for rdp-backend.so:\n\n"
293 " --width=WIDTH\t\tWidth of desktop\n"
294 " --height=HEIGHT\tHeight of desktop\n"
Dawid Gajownikd99a0502015-07-31 14:49:57 -0300295 " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300296 " --address=ADDR\tThe address to bind\n"
297 " --port=PORT\t\tThe port to listen on\n"
298 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
299 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
300 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
301 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
302 "\n");
303#endif
304
Dawid Gajownik71f57042015-07-31 17:39:00 -0300305#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300306 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300307 "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"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300310 " --transform=TR\tThe output transformation, TR is one of:\n"
311 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Dawid Gajownik71f57042015-07-31 17:39:00 -0300312 " --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_WAYLAND_COMPOSITOR)
318 fprintf(stderr,
319 "Options for wayland-backend.so:\n\n"
320 " --width=WIDTH\t\tWidth of Wayland surface\n"
321 " --height=HEIGHT\tHeight of Wayland surface\n"
322 " --scale=SCALE\t\tScale factor of output\n"
323 " --fullscreen\t\tRun in fullscreen mode\n"
324 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
325 " --output-count=COUNT\tCreate multiple outputs\n"
326 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
327 " --display=DISPLAY\tWayland display to connect to\n\n");
328#endif
329
330#if defined(BUILD_X11_COMPOSITOR)
331 fprintf(stderr,
332 "Options for x11-backend.so:\n\n"
333 " --width=WIDTH\t\tWidth of X window\n"
334 " --height=HEIGHT\tHeight of X window\n"
335 " --scale=SCALE\t\tScale factor of output\n"
336 " --fullscreen\t\tRun in fullscreen mode\n"
337 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
338 " --output-count=COUNT\tCreate multiple outputs\n"
339 " --no-input\t\tDont create input devices\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300340#endif
341
342 exit(error_code);
343}
344
345static 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
355static void
356on_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
375static void
376catch_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
387static const char *
388clock_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",
Derek Foreman32838c92015-06-29 13:20:34 -0500396#ifdef CLOCK_BOOTTIME
Giulio Camuffobab996e2014-10-12 00:24:25 +0300397 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500398#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300399 };
400
401 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
402 return "unknown";
403
404 return names[clk_id];
405}
406
407static const struct {
408 uint32_t bit; /* enum weston_capability */
409 const char *desc;
410} capability_strings[] = {
411 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
412 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
413};
414
415static void
416weston_compositor_log_capabilities(struct weston_compositor *compositor)
417{
418 unsigned i;
419 int yes;
420
421 weston_log("Compositor capabilities:\n");
422 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
423 yes = compositor->capabilities & capability_strings[i].bit;
424 weston_log_continue(STAMP_SPACE "%s %s\n",
425 capability_strings[i].desc,
426 yes ? "yes" : "no");
427 }
428
429 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
430 clock_name(compositor->presentation_clock),
431 compositor->presentation_clock);
432}
433
434static void
435handle_primary_client_destroyed(struct wl_listener *listener, void *data)
436{
437 struct wl_client *client = data;
438
439 weston_log("Primary client died. Closing...\n");
440
441 wl_display_terminate(wl_client_get_display(client));
442}
443
444static int
445weston_create_listening_socket(struct wl_display *display, const char *socket_name)
446{
447 if (socket_name) {
448 if (wl_display_add_socket(display, socket_name)) {
449 weston_log("fatal: failed to add socket: %m\n");
450 return -1;
451 }
452 } else {
453 socket_name = wl_display_add_socket_auto(display);
454 if (!socket_name) {
455 weston_log("fatal: failed to add socket: %m\n");
456 return -1;
457 }
458 }
459
460 setenv("WAYLAND_DISPLAY", socket_name, 1);
461
462 return 0;
463}
464
465static int
466load_modules(struct weston_compositor *ec, const char *modules,
467 int *argc, char *argv[])
468{
469 const char *p, *end;
470 char buffer[256];
471 int (*module_init)(struct weston_compositor *ec,
472 int *argc, char *argv[]);
473
474 if (modules == NULL)
475 return 0;
476
477 p = modules;
478 while (*p) {
479 end = strchrnul(p, ',');
480 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
481 module_init = weston_load_module(buffer, "module_init");
482 if (!module_init)
483 return -1;
484 if (module_init(ec, argc, argv) < 0)
485 return -1;
486 p = end;
487 while (*p == ',')
488 p++;
489
490 }
491
492 return 0;
493}
494
495static int
496weston_compositor_init_config(struct weston_compositor *ec,
497 struct weston_config *config)
498{
499 struct xkb_rule_names xkb_names;
500 struct weston_config_section *s;
501 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000502 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300503
504 s = weston_config_get_section(config, "keyboard", NULL, NULL);
505 weston_config_section_get_string(s, "keymap_rules",
506 (char **) &xkb_names.rules, NULL);
507 weston_config_section_get_string(s, "keymap_model",
508 (char **) &xkb_names.model, NULL);
509 weston_config_section_get_string(s, "keymap_layout",
510 (char **) &xkb_names.layout, NULL);
511 weston_config_section_get_string(s, "keymap_variant",
512 (char **) &xkb_names.variant, NULL);
513 weston_config_section_get_string(s, "keymap_options",
514 (char **) &xkb_names.options, NULL);
515
516 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
517 return -1;
518
519 weston_config_section_get_int(s, "repeat-rate",
520 &ec->kb_repeat_rate, 40);
521 weston_config_section_get_int(s, "repeat-delay",
522 &ec->kb_repeat_delay, 400);
523
Bob Ham91880f12016-01-12 10:21:47 +0000524 weston_config_section_get_bool(s, "vt-switching",
525 &vt_switching, true);
526 ec->vt_switching = vt_switching;
527
Giulio Camuffobab996e2014-10-12 00:24:25 +0300528 s = weston_config_get_section(config, "core", NULL, NULL);
529 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
530 ec->repaint_msec);
531 if (repaint_msec < -10 || repaint_msec > 1000) {
532 weston_log("Invalid repaint_window value in config: %d\n",
533 repaint_msec);
534 } else {
535 ec->repaint_msec = repaint_msec;
536 }
537 weston_log("Output repaint window is %d ms maximum.\n",
538 ec->repaint_msec);
539
540 return 0;
541}
542
543static char *
544weston_choose_default_backend(void)
545{
546 char *backend = NULL;
547
548 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
549 backend = strdup("wayland-backend.so");
550 else if (getenv("DISPLAY"))
551 backend = strdup("x11-backend.so");
552 else
553 backend = strdup(WESTON_NATIVE_BACKEND);
554
555 return backend;
556}
557
558static const struct { const char *name; uint32_t token; } transforms[] = {
559 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
560 { "90", WL_OUTPUT_TRANSFORM_90 },
561 { "180", WL_OUTPUT_TRANSFORM_180 },
562 { "270", WL_OUTPUT_TRANSFORM_270 },
563 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
564 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
565 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
566 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
567};
568
569WL_EXPORT int
570weston_parse_transform(const char *transform, uint32_t *out)
571{
572 unsigned int i;
573
574 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
575 if (strcmp(transforms[i].name, transform) == 0) {
576 *out = transforms[i].token;
577 return 0;
578 }
579
580 *out = WL_OUTPUT_TRANSFORM_NORMAL;
581 return -1;
582}
583
584WL_EXPORT const char *
585weston_transform_to_string(uint32_t output_transform)
586{
587 unsigned int i;
588
589 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
590 if (transforms[i].token == output_transform)
591 return transforms[i].name;
592
593 return "<illegal value>";
594}
595
596static int
597load_configuration(struct weston_config **config, int32_t noconfig,
598 const char *config_file)
599{
600 const char *file = "weston.ini";
601 const char *full_path;
602
603 *config = NULL;
604
605 if (config_file)
606 file = config_file;
607
608 if (noconfig == 0)
609 *config = weston_config_parse(file);
610
611 if (*config) {
612 full_path = weston_config_get_full_path(*config);
613
614 weston_log("Using config file '%s'\n", full_path);
615 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
616
617 return 0;
618 }
619
620 if (config_file && noconfig == 0) {
621 weston_log("fatal: error opening or reading config file"
622 " '%s'.\n", config_file);
623
624 return -1;
625 }
626
627 weston_log("Starting with no config file.\n");
628 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
629
630 return 0;
631}
632
633static void
634handle_exit(struct weston_compositor *c)
635{
636 wl_display_terminate(c->wl_display);
637}
638
Giulio Camuffo43008c72015-10-17 19:24:15 +0300639/* Temporary function to be removed when all backends are converted. */
640static int
641load_backend_old(struct weston_compositor *compositor, const char *backend,
642 int *argc, char **argv, struct weston_config *wc)
643{
644 int (*backend_init)(struct weston_compositor *c,
645 int *argc, char *argv[],
646 struct weston_config *config,
647 struct weston_backend_config *config_base);
648
649 backend_init = weston_load_module(backend, "backend_init");
650 if (!backend_init)
651 return -1;
652
653 return backend_init(compositor, argc, argv, wc, NULL);
654}
655
Giulio Camuffo43008c72015-10-17 19:24:15 +0300656static int
657load_backend(struct weston_compositor *compositor, const char *backend,
658 int *argc, char **argv, struct weston_config *config)
659{
660#if 0
661 if (strstr(backend, "drm-backend.so"))
662 return load_drm_backend(compositor, backend, argc, argv, config);
663 else if (strstr(backend, "wayland-backend.so"))
664 return load_wayland_backend(compositor, backend, argc, argv, config);
665 else if (strstr(backend, "x11-backend.so"))
666 return load_x11_backend(compositor, backend, argc, argv, config);
667 else if (strstr(backend, "fbdev-backend.so"))
668 return load_fbdev_backend(compositor, backend, argc, argv, config);
669 else if (strstr(backend, "headless-backend.so"))
670 return load_headless_backend(compositor, backend, argc, argv, config);
671 else if (strstr(backend, "rpi-backend.so"))
672 return load_rpi_backend(compositor, backend, argc, argv, config);
673 else if (strstr(backend, "rdp-backend.so"))
674 return load_rdp_backend(compositor, backend, argc, argv, config);
675#endif
676
677 return load_backend_old(compositor, backend, argc, argv, config);
678}
679
Giulio Camuffobab996e2014-10-12 00:24:25 +0300680int main(int argc, char *argv[])
681{
682 int ret = EXIT_FAILURE;
683 struct wl_display *display;
684 struct weston_compositor *ec;
685 struct wl_event_source *signals[4];
686 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300687 int i, fd;
688 char *backend = NULL;
689 char *shell = NULL;
690 char *modules = NULL;
691 char *option_modules = NULL;
692 char *log = NULL;
693 char *server_socket = NULL, *end;
694 int32_t idle_time = -1;
695 int32_t help = 0;
696 char *socket_name = NULL;
697 int32_t version = 0;
698 int32_t noconfig = 0;
699 int32_t numlock_on;
700 char *config_file = NULL;
701 struct weston_config *config = NULL;
702 struct weston_config_section *section;
703 struct wl_client *primary_client;
704 struct wl_listener primary_client_destroyed;
705 struct weston_seat *seat;
706
707 const struct weston_option core_options[] = {
708 { WESTON_OPTION_STRING, "backend", 'B', &backend },
709 { WESTON_OPTION_STRING, "shell", 0, &shell },
710 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
711 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
712 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
713 { WESTON_OPTION_STRING, "log", 0, &log },
714 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
715 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
716 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
717 { WESTON_OPTION_STRING, "config", 'c', &config_file },
718 };
719
720 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
721
722 if (help)
723 usage(EXIT_SUCCESS);
724
725 if (version) {
726 printf(PACKAGE_STRING "\n");
727 return EXIT_SUCCESS;
728 }
729
730 weston_log_file_open(log);
731
732 weston_log("%s\n"
733 STAMP_SPACE "%s\n"
734 STAMP_SPACE "Bug reports to: %s\n"
735 STAMP_SPACE "Build: %s\n",
736 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
737 BUILD_ID);
738 log_uname();
739
740 verify_xdg_runtime_dir();
741
742 display = wl_display_create();
743
744 loop = wl_display_get_event_loop(display);
745 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
746 display);
747 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
748 display);
749 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
750 display);
751
752 wl_list_init(&child_process_list);
753 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
754 NULL);
755
756 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
757 goto out_signals;
758
759 if (load_configuration(&config, noconfig, config_file) < 0)
760 goto out_signals;
761
762 section = weston_config_get_section(config, "core", NULL, NULL);
763
764 if (!backend) {
765 weston_config_section_get_string(section, "backend", &backend,
766 NULL);
767 if (!backend)
768 backend = weston_choose_default_backend();
769 }
770
Giulio Camuffobab996e2014-10-12 00:24:25 +0300771 ec = weston_compositor_create(display, NULL);
772 if (ec == NULL) {
773 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +0300774 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300775 }
776
777 ec->config = config;
778 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +0300779 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300780
Giulio Camuffo43008c72015-10-17 19:24:15 +0300781 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +0300782 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +0300783 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300784 }
785
786 catch_signals();
787 segv_compositor = ec;
788
789 if (idle_time < 0)
790 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
791 if (idle_time < 0)
792 idle_time = 300; /* default idle timeout, in seconds */
793
794 ec->idle_time = idle_time;
795 ec->default_pointer_grab = NULL;
796 ec->exit = handle_exit;
797
798 weston_compositor_log_capabilities(ec);
799
800 server_socket = getenv("WAYLAND_SERVER_SOCKET");
801 if (server_socket) {
802 weston_log("Running with single client\n");
803 fd = strtol(server_socket, &end, 0);
804 if (*end != '\0')
805 fd = -1;
806 } else {
807 fd = -1;
808 }
809
810 if (fd != -1) {
811 primary_client = wl_client_create(display, fd);
812 if (!primary_client) {
813 weston_log("fatal: failed to add client: %m\n");
814 goto out;
815 }
816 primary_client_destroyed.notify =
817 handle_primary_client_destroyed;
818 wl_client_add_destroy_listener(primary_client,
819 &primary_client_destroyed);
820 } else if (weston_create_listening_socket(display, socket_name)) {
821 goto out;
822 }
823
824 if (!shell)
825 weston_config_section_get_string(section, "shell", &shell,
826 "desktop-shell.so");
827
828 if (load_modules(ec, shell, &argc, argv) < 0)
829 goto out;
830
831 weston_config_section_get_string(section, "modules", &modules, "");
832 if (load_modules(ec, modules, &argc, argv) < 0)
833 goto out;
834
835 if (load_modules(ec, option_modules, &argc, argv) < 0)
836 goto out;
837
838 section = weston_config_get_section(config, "keyboard", NULL, NULL);
839 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
840 if (numlock_on) {
841 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -0500842 struct weston_keyboard *keyboard =
843 weston_seat_get_keyboard(seat);
844
845 if (keyboard)
846 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +0300847 WESTON_NUM_LOCK,
848 WESTON_NUM_LOCK);
849 }
850 }
851
852 for (i = 1; i < argc; i++)
853 weston_log("fatal: unhandled option: %s\n", argv[i]);
854 if (argc > 1)
855 goto out;
856
857 weston_compositor_wake(ec);
858
859 wl_display_run(display);
860
861 /* Allow for setting return exit code after
862 * wl_display_run returns normally. This is
863 * useful for devs/testers and automated tests
864 * that want to indicate failure status to
865 * testing infrastructure above
866 */
867 ret = ec->exit_code;
868
869out:
870 weston_compositor_destroy(ec);
871
872out_signals:
873 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
874 if (signals[i])
875 wl_event_source_remove(signals[i]);
876
877 wl_display_destroy(display);
878
879 weston_log_file_close();
880
881 if (config)
882 weston_config_destroy(config);
883 free(config_file);
884 free(backend);
885 free(shell);
886 free(socket_name);
887 free(option_modules);
888 free(log);
889 free(modules);
890
891 return ret;
892}