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