blob: aeb2ac5e698b7a64c29654f77017bde776cc037c [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
Benoit Gschwind3c530942016-04-15 20:28:32 -070050#include "compositor-headless.h"
Benoit Gschwindbd573102016-04-22 17:05:26 +020051#include "compositor-rdp.h"
Benoit Gschwind934e89a2016-04-27 23:56:42 +020052#include "compositor-fbdev.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070053#include "compositor-x11.h"
Benoit Gschwind3c530942016-04-15 20:28:32 -070054
Giulio Camuffobab996e2014-10-12 00:24:25 +030055static struct wl_list child_process_list;
56static struct weston_compositor *segv_compositor;
57
58static int
59sigchld_handler(int signal_number, void *data)
60{
61 struct weston_process *p;
62 int status;
63 pid_t pid;
64
65 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
66 wl_list_for_each(p, &child_process_list, link) {
67 if (p->pid == pid)
68 break;
69 }
70
71 if (&p->link == &child_process_list) {
72 weston_log("unknown child process exited\n");
73 continue;
74 }
75
76 wl_list_remove(&p->link);
77 p->cleanup(p, status);
78 }
79
80 if (pid < 0 && errno != ECHILD)
81 weston_log("waitpid error %m\n");
82
83 return 1;
84}
85
86#ifdef HAVE_LIBUNWIND
87
88static void
89print_backtrace(void)
90{
91 unw_cursor_t cursor;
92 unw_context_t context;
93 unw_word_t off;
94 unw_proc_info_t pip;
95 int ret, i = 0;
96 char procname[256];
97 const char *filename;
98 Dl_info dlinfo;
99
100 pip.unwind_info = NULL;
101 ret = unw_getcontext(&context);
102 if (ret) {
103 weston_log("unw_getcontext: %d\n", ret);
104 return;
105 }
106
107 ret = unw_init_local(&cursor, &context);
108 if (ret) {
109 weston_log("unw_init_local: %d\n", ret);
110 return;
111 }
112
113 ret = unw_step(&cursor);
114 while (ret > 0) {
115 ret = unw_get_proc_info(&cursor, &pip);
116 if (ret) {
117 weston_log("unw_get_proc_info: %d\n", ret);
118 break;
119 }
120
121 ret = unw_get_proc_name(&cursor, procname, 256, &off);
122 if (ret && ret != -UNW_ENOMEM) {
123 if (ret != -UNW_EUNSPEC)
124 weston_log("unw_get_proc_name: %d\n", ret);
125 procname[0] = '?';
126 procname[1] = 0;
127 }
128
129 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
130 *dlinfo.dli_fname)
131 filename = dlinfo.dli_fname;
132 else
133 filename = "?";
134
135 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
136 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
137
138 ret = unw_step(&cursor);
139 if (ret < 0)
140 weston_log("unw_step: %d\n", ret);
141 }
142}
143
144#else
145
146static void
147print_backtrace(void)
148{
149 void *buffer[32];
150 int i, count;
151 Dl_info info;
152
153 count = backtrace(buffer, ARRAY_LENGTH(buffer));
154 for (i = 0; i < count; i++) {
155 dladdr(buffer[i], &info);
156 weston_log(" [%016lx] %s (%s)\n",
157 (long) buffer[i],
158 info.dli_sname ? info.dli_sname : "--",
159 info.dli_fname);
160 }
161}
162
163#endif
164
165WL_EXPORT void
166weston_watch_process(struct weston_process *process)
167{
168 wl_list_insert(&child_process_list, &process->link);
169}
170
171static void
172log_uname(void)
173{
174 struct utsname usys;
175
176 uname(&usys);
177
178 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
179 usys.version, usys.machine);
180}
181
182static const char xdg_error_message[] =
183 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
184
185static const char xdg_wrong_message[] =
186 "fatal: environment variable XDG_RUNTIME_DIR\n"
187 "is set to \"%s\", which is not a directory.\n";
188
189static const char xdg_wrong_mode_message[] =
190 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
191 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
192 "and must be owned by the user (current owner is UID %d).\n";
193
194static const char xdg_detail_message[] =
195 "Refer to your distribution on how to get it, or\n"
196 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
197 "on how to implement it.\n";
198
199static void
200verify_xdg_runtime_dir(void)
201{
202 char *dir = getenv("XDG_RUNTIME_DIR");
203 struct stat s;
204
205 if (!dir) {
206 weston_log(xdg_error_message);
207 weston_log_continue(xdg_detail_message);
208 exit(EXIT_FAILURE);
209 }
210
211 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
212 weston_log(xdg_wrong_message, dir);
213 weston_log_continue(xdg_detail_message);
214 exit(EXIT_FAILURE);
215 }
216
217 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
218 weston_log(xdg_wrong_mode_message,
219 dir, s.st_mode & 0777, s.st_uid);
220 weston_log_continue(xdg_detail_message);
221 }
222}
223
224static int
225usage(int error_code)
226{
227 fprintf(stderr,
228 "Usage: weston [OPTIONS]\n\n"
229 "This is weston version " VERSION ", the Wayland reference compositor.\n"
230 "Weston supports multiple backends, and depending on which backend is in use\n"
231 "different options will be accepted.\n\n"
232
233
234 "Core options:\n\n"
235 " --version\t\tPrint weston version\n"
236 " -B, --backend=MODULE\tBackend module, one of\n"
237#if defined(BUILD_DRM_COMPOSITOR)
238 "\t\t\t\tdrm-backend.so\n"
239#endif
240#if defined(BUILD_FBDEV_COMPOSITOR)
241 "\t\t\t\tfbdev-backend.so\n"
242#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300243#if defined(BUILD_HEADLESS_COMPOSITOR)
244 "\t\t\t\theadless-backend.so\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300245#endif
246#if defined(BUILD_RDP_COMPOSITOR)
247 "\t\t\t\trdp-backend.so\n"
248#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300249#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
250 "\t\t\t\trpi-backend.so\n"
251#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300252#if defined(BUILD_WAYLAND_COMPOSITOR)
253 "\t\t\t\twayland-backend.so\n"
254#endif
255#if defined(BUILD_X11_COMPOSITOR)
256 "\t\t\t\tx11-backend.so\n"
257#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300258 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
259 " -S, --socket=NAME\tName of socket to listen on\n"
260 " -i, --idle-time=SECS\tIdle time in seconds\n"
261 " --modules\t\tLoad the comma-separated list of modules\n"
262 " --log=FILE\t\tLog to the given file\n"
263 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
264 " --no-config\t\tDo not read weston.ini\n"
265 " -h, --help\t\tThis help message\n\n");
266
267#if defined(BUILD_DRM_COMPOSITOR)
268 fprintf(stderr,
269 "Options for drm-backend.so:\n\n"
270 " --connector=ID\tBring up only this connector\n"
271 " --seat=SEAT\t\tThe seat that weston should run on\n"
272 " --tty=TTY\t\tThe tty to use\n"
273 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
274 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
275#endif
276
277#if defined(BUILD_FBDEV_COMPOSITOR)
278 fprintf(stderr,
279 "Options for fbdev-backend.so:\n\n"
280 " --tty=TTY\t\tThe tty to use\n"
281 " --device=DEVICE\tThe framebuffer device to use\n"
282 " --use-gl\t\tUse the GL renderer\n\n");
283#endif
284
Dawid Gajownik71f57042015-07-31 17:39:00 -0300285#if defined(BUILD_HEADLESS_COMPOSITOR)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300286 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300287 "Options for headless-backend.so:\n\n"
288 " --width=WIDTH\t\tWidth of memory surface\n"
289 " --height=HEIGHT\tHeight of memory surface\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300290 " --transform=TR\tThe output transformation, TR is one of:\n"
291 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Dawid Gajownik71f57042015-07-31 17:39:00 -0300292 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300293#endif
294
295#if defined(BUILD_RDP_COMPOSITOR)
296 fprintf(stderr,
297 "Options for rdp-backend.so:\n\n"
298 " --width=WIDTH\t\tWidth of desktop\n"
299 " --height=HEIGHT\tHeight of desktop\n"
Dawid Gajownikd99a0502015-07-31 14:49:57 -0300300 " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300301 " --address=ADDR\tThe address to bind\n"
302 " --port=PORT\t\tThe port to listen on\n"
303 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
304 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
305 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
306 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
307 "\n");
308#endif
309
Dawid Gajownik71f57042015-07-31 17:39:00 -0300310#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300311 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300312 "Options for rpi-backend.so:\n\n"
313 " --tty=TTY\t\tThe tty to use\n"
314 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300315 " --transform=TR\tThe output transformation, TR is one of:\n"
316 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Dawid Gajownik71f57042015-07-31 17:39:00 -0300317 " --opaque-regions\tEnable support for opaque regions, can be "
318 "very slow without support in the GPU firmware.\n"
319 "\n");
320#endif
321
322#if defined(BUILD_WAYLAND_COMPOSITOR)
323 fprintf(stderr,
324 "Options for wayland-backend.so:\n\n"
325 " --width=WIDTH\t\tWidth of Wayland surface\n"
326 " --height=HEIGHT\tHeight of Wayland surface\n"
327 " --scale=SCALE\t\tScale factor of output\n"
328 " --fullscreen\t\tRun in fullscreen mode\n"
329 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
330 " --output-count=COUNT\tCreate multiple outputs\n"
331 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
332 " --display=DISPLAY\tWayland display to connect to\n\n");
333#endif
334
335#if defined(BUILD_X11_COMPOSITOR)
336 fprintf(stderr,
337 "Options for x11-backend.so:\n\n"
338 " --width=WIDTH\t\tWidth of X window\n"
339 " --height=HEIGHT\tHeight of X window\n"
340 " --scale=SCALE\t\tScale factor of output\n"
341 " --fullscreen\t\tRun in fullscreen mode\n"
342 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
343 " --output-count=COUNT\tCreate multiple outputs\n"
344 " --no-input\t\tDont create input devices\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300345#endif
346
347 exit(error_code);
348}
349
350static int on_term_signal(int signal_number, void *data)
351{
352 struct wl_display *display = data;
353
354 weston_log("caught signal %d\n", signal_number);
355 wl_display_terminate(display);
356
357 return 1;
358}
359
360static void
361on_caught_signal(int s, siginfo_t *siginfo, void *context)
362{
363 /* This signal handler will do a best-effort backtrace, and
364 * then call the backend restore function, which will switch
365 * back to the vt we launched from or ungrab X etc and then
366 * raise SIGTRAP. If we run weston under gdb from X or a
367 * different vt, and tell gdb "handle *s* nostop", this
368 * will allow weston to switch back to gdb on crash and then
369 * gdb will catch the crash with SIGTRAP.*/
370
371 weston_log("caught signal: %d\n", s);
372
373 print_backtrace();
374
375 segv_compositor->backend->restore(segv_compositor);
376
377 raise(SIGTRAP);
378}
379
380static void
381catch_signals(void)
382{
383 struct sigaction action;
384
385 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
386 action.sa_sigaction = on_caught_signal;
387 sigemptyset(&action.sa_mask);
388 sigaction(SIGSEGV, &action, NULL);
389 sigaction(SIGABRT, &action, NULL);
390}
391
392static const char *
393clock_name(clockid_t clk_id)
394{
395 static const char *names[] = {
396 [CLOCK_REALTIME] = "CLOCK_REALTIME",
397 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
398 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
399 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
400 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500401#ifdef CLOCK_BOOTTIME
Giulio Camuffobab996e2014-10-12 00:24:25 +0300402 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500403#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300404 };
405
406 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
407 return "unknown";
408
409 return names[clk_id];
410}
411
412static const struct {
413 uint32_t bit; /* enum weston_capability */
414 const char *desc;
415} capability_strings[] = {
416 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
417 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
418};
419
420static void
421weston_compositor_log_capabilities(struct weston_compositor *compositor)
422{
423 unsigned i;
424 int yes;
425
426 weston_log("Compositor capabilities:\n");
427 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
428 yes = compositor->capabilities & capability_strings[i].bit;
429 weston_log_continue(STAMP_SPACE "%s %s\n",
430 capability_strings[i].desc,
431 yes ? "yes" : "no");
432 }
433
434 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
435 clock_name(compositor->presentation_clock),
436 compositor->presentation_clock);
437}
438
439static void
440handle_primary_client_destroyed(struct wl_listener *listener, void *data)
441{
442 struct wl_client *client = data;
443
444 weston_log("Primary client died. Closing...\n");
445
446 wl_display_terminate(wl_client_get_display(client));
447}
448
449static int
450weston_create_listening_socket(struct wl_display *display, const char *socket_name)
451{
452 if (socket_name) {
453 if (wl_display_add_socket(display, socket_name)) {
454 weston_log("fatal: failed to add socket: %m\n");
455 return -1;
456 }
457 } else {
458 socket_name = wl_display_add_socket_auto(display);
459 if (!socket_name) {
460 weston_log("fatal: failed to add socket: %m\n");
461 return -1;
462 }
463 }
464
465 setenv("WAYLAND_DISPLAY", socket_name, 1);
466
467 return 0;
468}
469
470static int
471load_modules(struct weston_compositor *ec, const char *modules,
472 int *argc, char *argv[])
473{
474 const char *p, *end;
475 char buffer[256];
476 int (*module_init)(struct weston_compositor *ec,
477 int *argc, char *argv[]);
478
479 if (modules == NULL)
480 return 0;
481
482 p = modules;
483 while (*p) {
484 end = strchrnul(p, ',');
485 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
486 module_init = weston_load_module(buffer, "module_init");
487 if (!module_init)
488 return -1;
489 if (module_init(ec, argc, argv) < 0)
490 return -1;
491 p = end;
492 while (*p == ',')
493 p++;
494
495 }
496
497 return 0;
498}
499
500static int
501weston_compositor_init_config(struct weston_compositor *ec,
502 struct weston_config *config)
503{
504 struct xkb_rule_names xkb_names;
505 struct weston_config_section *s;
506 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000507 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300508
509 s = weston_config_get_section(config, "keyboard", NULL, NULL);
510 weston_config_section_get_string(s, "keymap_rules",
511 (char **) &xkb_names.rules, NULL);
512 weston_config_section_get_string(s, "keymap_model",
513 (char **) &xkb_names.model, NULL);
514 weston_config_section_get_string(s, "keymap_layout",
515 (char **) &xkb_names.layout, NULL);
516 weston_config_section_get_string(s, "keymap_variant",
517 (char **) &xkb_names.variant, NULL);
518 weston_config_section_get_string(s, "keymap_options",
519 (char **) &xkb_names.options, NULL);
520
521 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
522 return -1;
523
524 weston_config_section_get_int(s, "repeat-rate",
525 &ec->kb_repeat_rate, 40);
526 weston_config_section_get_int(s, "repeat-delay",
527 &ec->kb_repeat_delay, 400);
528
Bob Ham91880f12016-01-12 10:21:47 +0000529 weston_config_section_get_bool(s, "vt-switching",
530 &vt_switching, true);
531 ec->vt_switching = vt_switching;
532
Giulio Camuffobab996e2014-10-12 00:24:25 +0300533 s = weston_config_get_section(config, "core", NULL, NULL);
534 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
535 ec->repaint_msec);
536 if (repaint_msec < -10 || repaint_msec > 1000) {
537 weston_log("Invalid repaint_window value in config: %d\n",
538 repaint_msec);
539 } else {
540 ec->repaint_msec = repaint_msec;
541 }
542 weston_log("Output repaint window is %d ms maximum.\n",
543 ec->repaint_msec);
544
545 return 0;
546}
547
548static char *
549weston_choose_default_backend(void)
550{
551 char *backend = NULL;
552
553 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
554 backend = strdup("wayland-backend.so");
555 else if (getenv("DISPLAY"))
556 backend = strdup("x11-backend.so");
557 else
558 backend = strdup(WESTON_NATIVE_BACKEND);
559
560 return backend;
561}
562
563static const struct { const char *name; uint32_t token; } transforms[] = {
564 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
565 { "90", WL_OUTPUT_TRANSFORM_90 },
566 { "180", WL_OUTPUT_TRANSFORM_180 },
567 { "270", WL_OUTPUT_TRANSFORM_270 },
568 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
569 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
570 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
571 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
572};
573
574WL_EXPORT int
575weston_parse_transform(const char *transform, uint32_t *out)
576{
577 unsigned int i;
578
579 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
580 if (strcmp(transforms[i].name, transform) == 0) {
581 *out = transforms[i].token;
582 return 0;
583 }
584
585 *out = WL_OUTPUT_TRANSFORM_NORMAL;
586 return -1;
587}
588
589WL_EXPORT const char *
590weston_transform_to_string(uint32_t output_transform)
591{
592 unsigned int i;
593
594 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
595 if (transforms[i].token == output_transform)
596 return transforms[i].name;
597
598 return "<illegal value>";
599}
600
601static int
602load_configuration(struct weston_config **config, int32_t noconfig,
603 const char *config_file)
604{
605 const char *file = "weston.ini";
606 const char *full_path;
607
608 *config = NULL;
609
610 if (config_file)
611 file = config_file;
612
613 if (noconfig == 0)
614 *config = weston_config_parse(file);
615
616 if (*config) {
617 full_path = weston_config_get_full_path(*config);
618
619 weston_log("Using config file '%s'\n", full_path);
620 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
621
622 return 0;
623 }
624
625 if (config_file && noconfig == 0) {
626 weston_log("fatal: error opening or reading config file"
627 " '%s'.\n", config_file);
628
629 return -1;
630 }
631
632 weston_log("Starting with no config file.\n");
633 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
634
635 return 0;
636}
637
638static void
639handle_exit(struct weston_compositor *c)
640{
641 wl_display_terminate(c->wl_display);
642}
643
Giulio Camuffo43008c72015-10-17 19:24:15 +0300644/* Temporary function to be removed when all backends are converted. */
645static int
646load_backend_old(struct weston_compositor *compositor, const char *backend,
647 int *argc, char **argv, struct weston_config *wc)
648{
649 int (*backend_init)(struct weston_compositor *c,
650 int *argc, char *argv[],
651 struct weston_config *config,
652 struct weston_backend_config *config_base);
653
654 backend_init = weston_load_module(backend, "backend_init");
655 if (!backend_init)
656 return -1;
657
658 return backend_init(compositor, argc, argv, wc, NULL);
659}
660
Bryce Harrington98ab08b2016-04-15 20:28:36 -0700661/** Main module call-point for backends.
662 *
663 * All backends should use this routine to access their init routine.
664 * Backends may subclass weston_backend_config to add their own
665 * configuration data, setting the major/minor version in config_base
666 * accordingly.
667 *
668 * The config_base object should be treated as temporary, and any data
669 * copied out of it by backend_init before returning. The load_backend_new
670 * callers may then free the config_base object.
671 *
672 * NOTE: This is a temporary function intended to eventually be replaced
673 * by weston_compositor_load_backend().
674 */
Bryce Harrington6ca25b32016-04-15 20:28:27 -0700675static int
676load_backend_new(struct weston_compositor *compositor, const char *backend,
677 struct weston_backend_config *config_base)
678{
679 int (*backend_init)(struct weston_compositor *c,
680 int *argc, char *argv[],
681 struct weston_config *config,
682 struct weston_backend_config *config_base);
683
684 backend_init = weston_load_module(backend, "backend_init");
685 if (!backend_init)
686 return -1;
687
688 return backend_init(compositor, NULL, NULL, NULL, config_base);
689}
690
Giulio Camuffo43008c72015-10-17 19:24:15 +0300691static int
Benoit Gschwind3c530942016-04-15 20:28:32 -0700692load_headless_backend(struct weston_compositor *c, char const * backend,
693 int *argc, char **argv, struct weston_config *wc)
694{
695 struct weston_headless_backend_config config = {{ 0, }};
696 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +0200697 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -0700698
699 config.width = 1024;
700 config.height = 640;
701
702 const struct weston_option options[] = {
703 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
704 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
705 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
706 { WESTON_OPTION_STRING, "transform", 0, &transform },
707 };
708
709 parse_options(options, ARRAY_LENGTH(options), argc, argv);
710
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +0200711 config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
712 if (transform) {
713 if (weston_parse_transform(transform, &config.transform) < 0)
714 weston_log("Invalid transform \"%s\"\n", transform);
715 free(transform);
716 }
Benoit Gschwind3c530942016-04-15 20:28:32 -0700717
718 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
719 config.base.struct_size = sizeof(struct weston_headless_backend_config);
720
721 /* load the actual wayland backend and configure it */
722 ret = load_backend_new(c, backend, &config.base);
723
724 return ret;
725}
726
Benoit Gschwindbd573102016-04-22 17:05:26 +0200727static void
728weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
729{
730 config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
731 config->base.struct_size = sizeof(struct weston_rdp_backend_config);
732
733 config->width = 640;
734 config->height = 480;
735 config->bind_address = NULL;
736 config->port = 3389;
737 config->rdp_key = NULL;
738 config->server_cert = NULL;
739 config->server_key = NULL;
740 config->env_socket = 0;
741 config->no_clients_resize = 0;
742}
743
744static int
745load_rdp_backend(struct weston_compositor *c, char const * backend,
746 int *argc, char *argv[], struct weston_config *wc)
747{
748 struct weston_rdp_backend_config config = {{ 0, }};
749 int ret = 0;
750
751 weston_rdp_backend_config_init(&config);
752
753 const struct weston_option rdp_options[] = {
754 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
755 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
756 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
757 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
758 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
759 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
760 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
761 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
762 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
763 };
764
765 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
766
767 ret = load_backend_new(c, backend, &config.base);
768
769 free(config.bind_address);
770 free(config.rdp_key);
771 free(config.server_cert);
772 free(config.server_key);
773 return ret;
774}
775
Benoit Gschwind3c530942016-04-15 20:28:32 -0700776static int
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200777load_fbdev_backend(struct weston_compositor *c, char const * backend,
778 int *argc, char **argv, struct weston_config *wc)
779{
780 struct weston_fbdev_backend_config config = {{ 0, }};
781 struct weston_config_section *section;
782 char *s = NULL;
783 int ret = 0;
784
785 const struct weston_option fbdev_options[] = {
786 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
787 { WESTON_OPTION_STRING, "device", 0, &config.device },
788 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &config.use_gl },
789 };
790
791 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
792
793 if (!config.device)
794 config.device = strdup("/dev/fb0");
795
796 section = weston_config_get_section(wc, "output", "name", "fbdev");
797 weston_config_section_get_string(section, "transform", &s, "normal");
798 if (weston_parse_transform(s, &config.output_transform) < 0)
799 weston_log("Invalid transform \"%s\" for output fbdev\n", s);
800 free(s);
801
802 config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
803 config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
804
805 /* load the actual wayland backend and configure it */
806 ret = load_backend_new(c, backend, &config.base);
807
808 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -0700809
810 return ret;
811}
812
813static int
814weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
815 struct weston_x11_backend_output_config *output_config) {
816 struct weston_x11_backend_output_config *new_outputs;
817
818 new_outputs = realloc(config->outputs, (config->num_outputs+1) *
819 sizeof(struct weston_x11_backend_output_config));
820 if (new_outputs == NULL)
821 return -1;
822
823 config->outputs = new_outputs;
824 config->outputs[config->num_outputs].width = output_config->width;
825 config->outputs[config->num_outputs].height = output_config->height;
826 config->outputs[config->num_outputs].transform = output_config->transform;
827 config->outputs[config->num_outputs].scale = output_config->scale;
828 config->outputs[config->num_outputs].name = strdup(output_config->name);
829 config->num_outputs++;
830
831 return 0;
832}
833
834static int
835load_x11_backend(struct weston_compositor *c, char const * backend,
836 int *argc, char **argv, struct weston_config *wc)
837{
838 struct weston_x11_backend_output_config default_output;
839 struct weston_x11_backend_config config = {{ 0, }};
840 struct weston_config_section *section;
841 int ret = 0;
842 int option_width = 0;
843 int option_height = 0;
844 int option_scale = 0;
845 int option_count = 1;
846 int output_count = 0;
847 char const *section_name;
848 int i;
849 uint32_t j;
850
851 const struct weston_option options[] = {
852 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
853 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
854 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
855 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
856 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
857 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
858 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
859 };
860
861 parse_options(options, ARRAY_LENGTH(options), argc, argv);
862
863 section = NULL;
864 while (weston_config_next_section(wc, &section, &section_name)) {
865 struct weston_x11_backend_output_config current_output = { 0, };
866 char *t;
867 char *mode;
868
869 if (strcmp(section_name, "output") != 0) {
870 continue;
871 }
872
873 weston_config_section_get_string(section, "name", &current_output.name, NULL);
874 if (current_output.name == NULL || current_output.name[0] != 'X') {
875 free(current_output.name);
876 continue;
877 }
878
879 weston_config_section_get_string(section, "mode", &mode, "1024x600");
880 if (sscanf(mode, "%dx%d", &current_output.width,
881 &current_output.height) != 2) {
882 weston_log("Invalid mode \"%s\" for output %s\n",
883 mode, current_output.name);
884 current_output.width = 1024;
885 current_output.height = 600;
886 }
887 free(mode);
888 if (current_output.width < 1)
889 current_output.width = 1024;
890 if (current_output.height < 1)
891 current_output.height = 600;
892 if (option_width)
893 current_output.width = option_width;
894 if (option_height)
895 current_output.height = option_height;
896
897 weston_config_section_get_int(section, "scale", &current_output.scale, 1);
898 if (option_scale)
899 current_output.scale = option_scale;
900
901 weston_config_section_get_string(section,
902 "transform", &t, "normal");
903 if (weston_parse_transform(t, &current_output.transform) < 0)
904 weston_log("Invalid transform \"%s\" for output %s\n",
905 t, current_output.name);
906 free(t);
907
908 if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
909 ret = -1;
910 goto out;
911 }
912
913 output_count++;
914 if (option_count && output_count >= option_count)
915 break;
916 }
917
918 default_output.name = NULL;
919 default_output.width = option_width ? option_width : 1024;
920 default_output.height = option_height ? option_height : 600;
921 default_output.scale = option_scale ? option_scale : 1;
922 default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
923
924 for (i = output_count; i < option_count; i++) {
925 if (asprintf(&default_output.name, "screen%d", i) < 0) {
926 ret = -1;
927 goto out;
928 }
929
930 if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
931 ret = -1;
932 free(default_output.name);
933 goto out;
934 }
935 free(default_output.name);
936 }
937
938 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
939 config.base.struct_size = sizeof(struct weston_x11_backend_config);
940
941 /* load the actual backend and configure it */
942 ret = load_backend_new(c, backend, &config.base);
943
944out:
945 for (j = 0; j < config.num_outputs; ++j)
946 free(config.outputs[j].name);
947 free(config.outputs);
948
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200949 return ret;
950}
951
952static int
Giulio Camuffo43008c72015-10-17 19:24:15 +0300953load_backend(struct weston_compositor *compositor, const char *backend,
954 int *argc, char **argv, struct weston_config *config)
955{
Benoit Gschwind3c530942016-04-15 20:28:32 -0700956 if (strstr(backend, "headless-backend.so"))
957 return load_headless_backend(compositor, backend, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +0200958 else if (strstr(backend, "rdp-backend.so"))
959 return load_rdp_backend(compositor, backend, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200960 else if (strstr(backend, "fbdev-backend.so"))
961 return load_fbdev_backend(compositor, backend, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -0700962 else if (strstr(backend, "x11-backend.so"))
963 return load_x11_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +0300964#if 0
Benoit Gschwind3c530942016-04-15 20:28:32 -0700965 else if (strstr(backend, "drm-backend.so"))
Giulio Camuffo43008c72015-10-17 19:24:15 +0300966 return load_drm_backend(compositor, backend, argc, argv, config);
967 else if (strstr(backend, "wayland-backend.so"))
968 return load_wayland_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +0300969 else if (strstr(backend, "rpi-backend.so"))
970 return load_rpi_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +0300971#endif
972
973 return load_backend_old(compositor, backend, argc, argv, config);
974}
975
Giulio Camuffobab996e2014-10-12 00:24:25 +0300976int main(int argc, char *argv[])
977{
978 int ret = EXIT_FAILURE;
979 struct wl_display *display;
980 struct weston_compositor *ec;
981 struct wl_event_source *signals[4];
982 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300983 int i, fd;
984 char *backend = NULL;
985 char *shell = NULL;
986 char *modules = NULL;
987 char *option_modules = NULL;
988 char *log = NULL;
989 char *server_socket = NULL, *end;
990 int32_t idle_time = -1;
991 int32_t help = 0;
992 char *socket_name = NULL;
993 int32_t version = 0;
994 int32_t noconfig = 0;
995 int32_t numlock_on;
996 char *config_file = NULL;
997 struct weston_config *config = NULL;
998 struct weston_config_section *section;
999 struct wl_client *primary_client;
1000 struct wl_listener primary_client_destroyed;
1001 struct weston_seat *seat;
1002
1003 const struct weston_option core_options[] = {
1004 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1005 { WESTON_OPTION_STRING, "shell", 0, &shell },
1006 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1007 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
1008 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1009 { WESTON_OPTION_STRING, "log", 0, &log },
1010 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1011 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1012 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1013 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1014 };
1015
1016 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1017
1018 if (help)
1019 usage(EXIT_SUCCESS);
1020
1021 if (version) {
1022 printf(PACKAGE_STRING "\n");
1023 return EXIT_SUCCESS;
1024 }
1025
1026 weston_log_file_open(log);
1027
1028 weston_log("%s\n"
1029 STAMP_SPACE "%s\n"
1030 STAMP_SPACE "Bug reports to: %s\n"
1031 STAMP_SPACE "Build: %s\n",
1032 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1033 BUILD_ID);
1034 log_uname();
1035
1036 verify_xdg_runtime_dir();
1037
1038 display = wl_display_create();
1039
1040 loop = wl_display_get_event_loop(display);
1041 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1042 display);
1043 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1044 display);
1045 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1046 display);
1047
1048 wl_list_init(&child_process_list);
1049 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1050 NULL);
1051
1052 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1053 goto out_signals;
1054
1055 if (load_configuration(&config, noconfig, config_file) < 0)
1056 goto out_signals;
1057
1058 section = weston_config_get_section(config, "core", NULL, NULL);
1059
1060 if (!backend) {
1061 weston_config_section_get_string(section, "backend", &backend,
1062 NULL);
1063 if (!backend)
1064 backend = weston_choose_default_backend();
1065 }
1066
Giulio Camuffobab996e2014-10-12 00:24:25 +03001067 ec = weston_compositor_create(display, NULL);
1068 if (ec == NULL) {
1069 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001070 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001071 }
1072
1073 ec->config = config;
1074 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001075 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001076
Giulio Camuffo43008c72015-10-17 19:24:15 +03001077 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001078 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001079 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001080 }
1081
1082 catch_signals();
1083 segv_compositor = ec;
1084
1085 if (idle_time < 0)
1086 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1087 if (idle_time < 0)
1088 idle_time = 300; /* default idle timeout, in seconds */
1089
1090 ec->idle_time = idle_time;
1091 ec->default_pointer_grab = NULL;
1092 ec->exit = handle_exit;
1093
1094 weston_compositor_log_capabilities(ec);
1095
1096 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1097 if (server_socket) {
1098 weston_log("Running with single client\n");
1099 fd = strtol(server_socket, &end, 0);
1100 if (*end != '\0')
1101 fd = -1;
1102 } else {
1103 fd = -1;
1104 }
1105
1106 if (fd != -1) {
1107 primary_client = wl_client_create(display, fd);
1108 if (!primary_client) {
1109 weston_log("fatal: failed to add client: %m\n");
1110 goto out;
1111 }
1112 primary_client_destroyed.notify =
1113 handle_primary_client_destroyed;
1114 wl_client_add_destroy_listener(primary_client,
1115 &primary_client_destroyed);
1116 } else if (weston_create_listening_socket(display, socket_name)) {
1117 goto out;
1118 }
1119
1120 if (!shell)
1121 weston_config_section_get_string(section, "shell", &shell,
1122 "desktop-shell.so");
1123
1124 if (load_modules(ec, shell, &argc, argv) < 0)
1125 goto out;
1126
1127 weston_config_section_get_string(section, "modules", &modules, "");
1128 if (load_modules(ec, modules, &argc, argv) < 0)
1129 goto out;
1130
1131 if (load_modules(ec, option_modules, &argc, argv) < 0)
1132 goto out;
1133
1134 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1135 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1136 if (numlock_on) {
1137 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001138 struct weston_keyboard *keyboard =
1139 weston_seat_get_keyboard(seat);
1140
1141 if (keyboard)
1142 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001143 WESTON_NUM_LOCK,
1144 WESTON_NUM_LOCK);
1145 }
1146 }
1147
1148 for (i = 1; i < argc; i++)
1149 weston_log("fatal: unhandled option: %s\n", argv[i]);
1150 if (argc > 1)
1151 goto out;
1152
1153 weston_compositor_wake(ec);
1154
1155 wl_display_run(display);
1156
1157 /* Allow for setting return exit code after
1158 * wl_display_run returns normally. This is
1159 * useful for devs/testers and automated tests
1160 * that want to indicate failure status to
1161 * testing infrastructure above
1162 */
1163 ret = ec->exit_code;
1164
1165out:
1166 weston_compositor_destroy(ec);
1167
1168out_signals:
1169 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1170 if (signals[i])
1171 wl_event_source_remove(signals[i]);
1172
1173 wl_display_destroy(display);
1174
1175 weston_log_file_close();
1176
1177 if (config)
1178 weston_config_destroy(config);
1179 free(config_file);
1180 free(backend);
1181 free(shell);
1182 free(socket_name);
1183 free(option_modules);
1184 free(log);
1185 free(modules);
1186
1187 return ret;
1188}