blob: 39a13cfd94a5ed5d0a4424233993d5a352e213b8 [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.
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02005 * Copyright © 2010-2011 Benjamin Franzke
6 * Copyright © 2013 Jason Ekstrand
Giulio Camuffobab996e2014-10-12 00:24:25 +03007 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30#include "config.h"
31
32#include <unistd.h>
33#include <signal.h>
34#include <errno.h>
35#include <dlfcn.h>
36#include <string.h>
37#include <sys/utsname.h>
38#include <sys/stat.h>
39#include <sys/wait.h>
40
41#ifdef HAVE_LIBUNWIND
42#define UNW_LOCAL_ONLY
43#include <libunwind.h>
44#endif
45
46#include "compositor.h"
47#include "../shared/os-compatibility.h"
48#include "../shared/helpers.h"
49#include "git-version.h"
50#include "version.h"
51
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070052#include "compositor-drm.h"
Benoit Gschwind3c530942016-04-15 20:28:32 -070053#include "compositor-headless.h"
Benoit Gschwindbd573102016-04-22 17:05:26 +020054#include "compositor-rdp.h"
Benoit Gschwind934e89a2016-04-27 23:56:42 +020055#include "compositor-fbdev.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070056#include "compositor-x11.h"
Benoit Gschwind3ff10da2016-05-10 22:47:49 +020057#include "compositor-wayland.h"
58
59#define WINDOW_TITLE "Weston Compositor"
Benoit Gschwind3c530942016-04-15 20:28:32 -070060
Giulio Camuffobab996e2014-10-12 00:24:25 +030061static struct wl_list child_process_list;
62static struct weston_compositor *segv_compositor;
63
64static int
65sigchld_handler(int signal_number, void *data)
66{
67 struct weston_process *p;
68 int status;
69 pid_t pid;
70
71 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
72 wl_list_for_each(p, &child_process_list, link) {
73 if (p->pid == pid)
74 break;
75 }
76
77 if (&p->link == &child_process_list) {
78 weston_log("unknown child process exited\n");
79 continue;
80 }
81
82 wl_list_remove(&p->link);
83 p->cleanup(p, status);
84 }
85
86 if (pid < 0 && errno != ECHILD)
87 weston_log("waitpid error %m\n");
88
89 return 1;
90}
91
92#ifdef HAVE_LIBUNWIND
93
94static void
95print_backtrace(void)
96{
97 unw_cursor_t cursor;
98 unw_context_t context;
99 unw_word_t off;
100 unw_proc_info_t pip;
101 int ret, i = 0;
102 char procname[256];
103 const char *filename;
104 Dl_info dlinfo;
105
106 pip.unwind_info = NULL;
107 ret = unw_getcontext(&context);
108 if (ret) {
109 weston_log("unw_getcontext: %d\n", ret);
110 return;
111 }
112
113 ret = unw_init_local(&cursor, &context);
114 if (ret) {
115 weston_log("unw_init_local: %d\n", ret);
116 return;
117 }
118
119 ret = unw_step(&cursor);
120 while (ret > 0) {
121 ret = unw_get_proc_info(&cursor, &pip);
122 if (ret) {
123 weston_log("unw_get_proc_info: %d\n", ret);
124 break;
125 }
126
127 ret = unw_get_proc_name(&cursor, procname, 256, &off);
128 if (ret && ret != -UNW_ENOMEM) {
129 if (ret != -UNW_EUNSPEC)
130 weston_log("unw_get_proc_name: %d\n", ret);
131 procname[0] = '?';
132 procname[1] = 0;
133 }
134
135 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
136 *dlinfo.dli_fname)
137 filename = dlinfo.dli_fname;
138 else
139 filename = "?";
140
141 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
142 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
143
144 ret = unw_step(&cursor);
145 if (ret < 0)
146 weston_log("unw_step: %d\n", ret);
147 }
148}
149
150#else
151
152static void
153print_backtrace(void)
154{
155 void *buffer[32];
156 int i, count;
157 Dl_info info;
158
159 count = backtrace(buffer, ARRAY_LENGTH(buffer));
160 for (i = 0; i < count; i++) {
161 dladdr(buffer[i], &info);
162 weston_log(" [%016lx] %s (%s)\n",
163 (long) buffer[i],
164 info.dli_sname ? info.dli_sname : "--",
165 info.dli_fname);
166 }
167}
168
169#endif
170
171WL_EXPORT void
172weston_watch_process(struct weston_process *process)
173{
174 wl_list_insert(&child_process_list, &process->link);
175}
176
177static void
178log_uname(void)
179{
180 struct utsname usys;
181
182 uname(&usys);
183
184 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
185 usys.version, usys.machine);
186}
187
188static const char xdg_error_message[] =
189 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
190
191static const char xdg_wrong_message[] =
192 "fatal: environment variable XDG_RUNTIME_DIR\n"
193 "is set to \"%s\", which is not a directory.\n";
194
195static const char xdg_wrong_mode_message[] =
196 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
197 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
198 "and must be owned by the user (current owner is UID %d).\n";
199
200static const char xdg_detail_message[] =
201 "Refer to your distribution on how to get it, or\n"
202 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
203 "on how to implement it.\n";
204
205static void
206verify_xdg_runtime_dir(void)
207{
208 char *dir = getenv("XDG_RUNTIME_DIR");
209 struct stat s;
210
211 if (!dir) {
212 weston_log(xdg_error_message);
213 weston_log_continue(xdg_detail_message);
214 exit(EXIT_FAILURE);
215 }
216
217 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
218 weston_log(xdg_wrong_message, dir);
219 weston_log_continue(xdg_detail_message);
220 exit(EXIT_FAILURE);
221 }
222
223 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
224 weston_log(xdg_wrong_mode_message,
225 dir, s.st_mode & 0777, s.st_uid);
226 weston_log_continue(xdg_detail_message);
227 }
228}
229
230static int
231usage(int error_code)
232{
233 fprintf(stderr,
234 "Usage: weston [OPTIONS]\n\n"
235 "This is weston version " VERSION ", the Wayland reference compositor.\n"
236 "Weston supports multiple backends, and depending on which backend is in use\n"
237 "different options will be accepted.\n\n"
238
239
240 "Core options:\n\n"
241 " --version\t\tPrint weston version\n"
242 " -B, --backend=MODULE\tBackend module, one of\n"
243#if defined(BUILD_DRM_COMPOSITOR)
244 "\t\t\t\tdrm-backend.so\n"
245#endif
246#if defined(BUILD_FBDEV_COMPOSITOR)
247 "\t\t\t\tfbdev-backend.so\n"
248#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300249#if defined(BUILD_HEADLESS_COMPOSITOR)
250 "\t\t\t\theadless-backend.so\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300251#endif
252#if defined(BUILD_RDP_COMPOSITOR)
253 "\t\t\t\trdp-backend.so\n"
254#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300255#if defined(BUILD_WAYLAND_COMPOSITOR)
256 "\t\t\t\twayland-backend.so\n"
257#endif
258#if defined(BUILD_X11_COMPOSITOR)
259 "\t\t\t\tx11-backend.so\n"
260#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300261 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
262 " -S, --socket=NAME\tName of socket to listen on\n"
263 " -i, --idle-time=SECS\tIdle time in seconds\n"
264 " --modules\t\tLoad the comma-separated list of modules\n"
265 " --log=FILE\t\tLog to the given file\n"
266 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
267 " --no-config\t\tDo not read weston.ini\n"
268 " -h, --help\t\tThis help message\n\n");
269
270#if defined(BUILD_DRM_COMPOSITOR)
271 fprintf(stderr,
272 "Options for drm-backend.so:\n\n"
273 " --connector=ID\tBring up only this connector\n"
274 " --seat=SEAT\t\tThe seat that weston should run on\n"
275 " --tty=TTY\t\tThe tty to use\n"
276 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
277 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
278#endif
279
280#if defined(BUILD_FBDEV_COMPOSITOR)
281 fprintf(stderr,
282 "Options for fbdev-backend.so:\n\n"
283 " --tty=TTY\t\tThe tty to use\n"
284 " --device=DEVICE\tThe framebuffer device to use\n"
285 " --use-gl\t\tUse the GL renderer\n\n");
286#endif
287
Dawid Gajownik71f57042015-07-31 17:39:00 -0300288#if defined(BUILD_HEADLESS_COMPOSITOR)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300289 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300290 "Options for headless-backend.so:\n\n"
291 " --width=WIDTH\t\tWidth of memory surface\n"
292 " --height=HEIGHT\tHeight of memory surface\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300293 " --transform=TR\tThe output transformation, TR is one of:\n"
294 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Dawid Gajownik71f57042015-07-31 17:39:00 -0300295 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300296#endif
297
298#if defined(BUILD_RDP_COMPOSITOR)
299 fprintf(stderr,
300 "Options for rdp-backend.so:\n\n"
301 " --width=WIDTH\t\tWidth of desktop\n"
302 " --height=HEIGHT\tHeight of desktop\n"
Dawid Gajownikd99a0502015-07-31 14:49:57 -0300303 " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300304 " --address=ADDR\tThe address to bind\n"
305 " --port=PORT\t\tThe port to listen on\n"
306 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
307 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
308 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
309 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
310 "\n");
311#endif
312
Dawid Gajownik71f57042015-07-31 17:39:00 -0300313#if defined(BUILD_WAYLAND_COMPOSITOR)
314 fprintf(stderr,
315 "Options for wayland-backend.so:\n\n"
316 " --width=WIDTH\t\tWidth of Wayland surface\n"
317 " --height=HEIGHT\tHeight of Wayland surface\n"
318 " --scale=SCALE\t\tScale factor of output\n"
319 " --fullscreen\t\tRun in fullscreen mode\n"
320 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
321 " --output-count=COUNT\tCreate multiple outputs\n"
322 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
323 " --display=DISPLAY\tWayland display to connect to\n\n");
324#endif
325
326#if defined(BUILD_X11_COMPOSITOR)
327 fprintf(stderr,
328 "Options for x11-backend.so:\n\n"
329 " --width=WIDTH\t\tWidth of X window\n"
330 " --height=HEIGHT\tHeight of X window\n"
331 " --scale=SCALE\t\tScale factor of output\n"
332 " --fullscreen\t\tRun in fullscreen mode\n"
333 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
334 " --output-count=COUNT\tCreate multiple outputs\n"
335 " --no-input\t\tDont create input devices\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300336#endif
337
338 exit(error_code);
339}
340
341static int on_term_signal(int signal_number, void *data)
342{
343 struct wl_display *display = data;
344
345 weston_log("caught signal %d\n", signal_number);
346 wl_display_terminate(display);
347
348 return 1;
349}
350
351static void
352on_caught_signal(int s, siginfo_t *siginfo, void *context)
353{
354 /* This signal handler will do a best-effort backtrace, and
355 * then call the backend restore function, which will switch
356 * back to the vt we launched from or ungrab X etc and then
357 * raise SIGTRAP. If we run weston under gdb from X or a
358 * different vt, and tell gdb "handle *s* nostop", this
359 * will allow weston to switch back to gdb on crash and then
360 * gdb will catch the crash with SIGTRAP.*/
361
362 weston_log("caught signal: %d\n", s);
363
364 print_backtrace();
365
366 segv_compositor->backend->restore(segv_compositor);
367
368 raise(SIGTRAP);
369}
370
371static void
372catch_signals(void)
373{
374 struct sigaction action;
375
376 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
377 action.sa_sigaction = on_caught_signal;
378 sigemptyset(&action.sa_mask);
379 sigaction(SIGSEGV, &action, NULL);
380 sigaction(SIGABRT, &action, NULL);
381}
382
383static const char *
384clock_name(clockid_t clk_id)
385{
386 static const char *names[] = {
387 [CLOCK_REALTIME] = "CLOCK_REALTIME",
388 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
389 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
390 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
391 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500392#ifdef CLOCK_BOOTTIME
Giulio Camuffobab996e2014-10-12 00:24:25 +0300393 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500394#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300395 };
396
397 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
398 return "unknown";
399
400 return names[clk_id];
401}
402
403static const struct {
404 uint32_t bit; /* enum weston_capability */
405 const char *desc;
406} capability_strings[] = {
407 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
408 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
409};
410
411static void
412weston_compositor_log_capabilities(struct weston_compositor *compositor)
413{
414 unsigned i;
415 int yes;
416
417 weston_log("Compositor capabilities:\n");
418 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
419 yes = compositor->capabilities & capability_strings[i].bit;
420 weston_log_continue(STAMP_SPACE "%s %s\n",
421 capability_strings[i].desc,
422 yes ? "yes" : "no");
423 }
424
425 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
426 clock_name(compositor->presentation_clock),
427 compositor->presentation_clock);
428}
429
430static void
431handle_primary_client_destroyed(struct wl_listener *listener, void *data)
432{
433 struct wl_client *client = data;
434
435 weston_log("Primary client died. Closing...\n");
436
437 wl_display_terminate(wl_client_get_display(client));
438}
439
440static int
441weston_create_listening_socket(struct wl_display *display, const char *socket_name)
442{
443 if (socket_name) {
444 if (wl_display_add_socket(display, socket_name)) {
445 weston_log("fatal: failed to add socket: %m\n");
446 return -1;
447 }
448 } else {
449 socket_name = wl_display_add_socket_auto(display);
450 if (!socket_name) {
451 weston_log("fatal: failed to add socket: %m\n");
452 return -1;
453 }
454 }
455
456 setenv("WAYLAND_DISPLAY", socket_name, 1);
457
458 return 0;
459}
460
461static int
462load_modules(struct weston_compositor *ec, const char *modules,
463 int *argc, char *argv[])
464{
465 const char *p, *end;
466 char buffer[256];
467 int (*module_init)(struct weston_compositor *ec,
468 int *argc, char *argv[]);
469
470 if (modules == NULL)
471 return 0;
472
473 p = modules;
474 while (*p) {
475 end = strchrnul(p, ',');
476 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
477 module_init = weston_load_module(buffer, "module_init");
478 if (!module_init)
479 return -1;
480 if (module_init(ec, argc, argv) < 0)
481 return -1;
482 p = end;
483 while (*p == ',')
484 p++;
485
486 }
487
488 return 0;
489}
490
491static int
492weston_compositor_init_config(struct weston_compositor *ec,
493 struct weston_config *config)
494{
495 struct xkb_rule_names xkb_names;
496 struct weston_config_section *s;
497 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000498 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300499
500 s = weston_config_get_section(config, "keyboard", NULL, NULL);
501 weston_config_section_get_string(s, "keymap_rules",
502 (char **) &xkb_names.rules, NULL);
503 weston_config_section_get_string(s, "keymap_model",
504 (char **) &xkb_names.model, NULL);
505 weston_config_section_get_string(s, "keymap_layout",
506 (char **) &xkb_names.layout, NULL);
507 weston_config_section_get_string(s, "keymap_variant",
508 (char **) &xkb_names.variant, NULL);
509 weston_config_section_get_string(s, "keymap_options",
510 (char **) &xkb_names.options, NULL);
511
Giulio Camuffo0358af42016-06-02 21:48:08 +0300512 if (weston_compositor_set_xkb_rule_names(ec, &xkb_names) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300513 return -1;
514
515 weston_config_section_get_int(s, "repeat-rate",
516 &ec->kb_repeat_rate, 40);
517 weston_config_section_get_int(s, "repeat-delay",
518 &ec->kb_repeat_delay, 400);
519
Bob Ham91880f12016-01-12 10:21:47 +0000520 weston_config_section_get_bool(s, "vt-switching",
521 &vt_switching, true);
522 ec->vt_switching = vt_switching;
523
Giulio Camuffobab996e2014-10-12 00:24:25 +0300524 s = weston_config_get_section(config, "core", NULL, NULL);
525 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
526 ec->repaint_msec);
527 if (repaint_msec < -10 || repaint_msec > 1000) {
528 weston_log("Invalid repaint_window value in config: %d\n",
529 repaint_msec);
530 } else {
531 ec->repaint_msec = repaint_msec;
532 }
533 weston_log("Output repaint window is %d ms maximum.\n",
534 ec->repaint_msec);
535
536 return 0;
537}
538
539static char *
540weston_choose_default_backend(void)
541{
542 char *backend = NULL;
543
544 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
545 backend = strdup("wayland-backend.so");
546 else if (getenv("DISPLAY"))
547 backend = strdup("x11-backend.so");
548 else
549 backend = strdup(WESTON_NATIVE_BACKEND);
550
551 return backend;
552}
553
554static const struct { const char *name; uint32_t token; } transforms[] = {
555 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
556 { "90", WL_OUTPUT_TRANSFORM_90 },
557 { "180", WL_OUTPUT_TRANSFORM_180 },
558 { "270", WL_OUTPUT_TRANSFORM_270 },
559 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
560 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
561 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
562 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
563};
564
565WL_EXPORT int
566weston_parse_transform(const char *transform, uint32_t *out)
567{
568 unsigned int i;
569
570 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
571 if (strcmp(transforms[i].name, transform) == 0) {
572 *out = transforms[i].token;
573 return 0;
574 }
575
576 *out = WL_OUTPUT_TRANSFORM_NORMAL;
577 return -1;
578}
579
580WL_EXPORT const char *
581weston_transform_to_string(uint32_t output_transform)
582{
583 unsigned int i;
584
585 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
586 if (transforms[i].token == output_transform)
587 return transforms[i].name;
588
589 return "<illegal value>";
590}
591
592static int
593load_configuration(struct weston_config **config, int32_t noconfig,
594 const char *config_file)
595{
596 const char *file = "weston.ini";
597 const char *full_path;
598
599 *config = NULL;
600
601 if (config_file)
602 file = config_file;
603
604 if (noconfig == 0)
605 *config = weston_config_parse(file);
606
607 if (*config) {
608 full_path = weston_config_get_full_path(*config);
609
610 weston_log("Using config file '%s'\n", full_path);
611 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
612
613 return 0;
614 }
615
616 if (config_file && noconfig == 0) {
617 weston_log("fatal: error opening or reading config file"
618 " '%s'.\n", config_file);
619
620 return -1;
621 }
622
623 weston_log("Starting with no config file.\n");
624 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
625
626 return 0;
627}
628
629static void
630handle_exit(struct weston_compositor *c)
631{
632 wl_display_terminate(c->wl_display);
633}
634
Giulio Camuffo43008c72015-10-17 19:24:15 +0300635/* Temporary function to be removed when all backends are converted. */
636static int
637load_backend_old(struct weston_compositor *compositor, const char *backend,
638 int *argc, char **argv, struct weston_config *wc)
639{
640 int (*backend_init)(struct weston_compositor *c,
641 int *argc, char *argv[],
642 struct weston_config *config,
643 struct weston_backend_config *config_base);
644
645 backend_init = weston_load_module(backend, "backend_init");
646 if (!backend_init)
647 return -1;
648
649 return backend_init(compositor, argc, argv, wc, NULL);
650}
651
Bryce Harrington98ab08b2016-04-15 20:28:36 -0700652/** Main module call-point for backends.
653 *
654 * All backends should use this routine to access their init routine.
655 * Backends may subclass weston_backend_config to add their own
656 * configuration data, setting the major/minor version in config_base
657 * accordingly.
658 *
659 * The config_base object should be treated as temporary, and any data
660 * copied out of it by backend_init before returning. The load_backend_new
661 * callers may then free the config_base object.
662 *
663 * NOTE: This is a temporary function intended to eventually be replaced
664 * by weston_compositor_load_backend().
665 */
Bryce Harrington6ca25b32016-04-15 20:28:27 -0700666static int
667load_backend_new(struct weston_compositor *compositor, const char *backend,
668 struct weston_backend_config *config_base)
669{
670 int (*backend_init)(struct weston_compositor *c,
671 int *argc, char *argv[],
672 struct weston_config *config,
673 struct weston_backend_config *config_base);
674
675 backend_init = weston_load_module(backend, "backend_init");
676 if (!backend_init)
677 return -1;
678
679 return backend_init(compositor, NULL, NULL, NULL, config_base);
680}
681
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700682static enum weston_drm_backend_output_mode
683drm_configure_output(struct weston_compositor *c,
684 bool use_current_mode,
685 const char *name,
686 struct weston_drm_backend_output_config *config)
687{
688 struct weston_config *wc = weston_compositor_get_user_data(c);
689 struct weston_config_section *section;
690 char *s;
691 int scale;
692 enum weston_drm_backend_output_mode mode =
693 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
694
695 section = weston_config_get_section(wc, "output", "name", name);
696 weston_config_section_get_string(section, "mode", &s, "preferred");
697 if (strcmp(s, "off") == 0) {
698 free(s);
699 return WESTON_DRM_BACKEND_OUTPUT_OFF;
700 }
701
702 if (use_current_mode || strcmp(s, "current") == 0) {
703 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
704 } else if (strcmp(s, "preferred") != 0) {
705 config->modeline = s;
706 s = NULL;
707 }
708 free(s);
709
710 weston_config_section_get_int(section, "scale", &scale, 1);
711 config->base.scale = scale >= 1 ? scale : 1;
712 weston_config_section_get_string(section, "transform", &s, "normal");
713 if (weston_parse_transform(s, &config->base.transform) < 0)
714 weston_log("Invalid transform \"%s\" for output %s\n",
715 s, name);
716 free(s);
717
718 weston_config_section_get_string(section,
719 "gbm-format", &config->gbm_format, NULL);
720 weston_config_section_get_string(section, "seat", &config->seat, "");
721 return mode;
722}
723
724static int
725load_drm_backend(struct weston_compositor *c, const char *backend,
726 int *argc, char **argv, struct weston_config *wc)
727{
728 struct weston_drm_backend_config config = {{ 0, }};
729 struct weston_config_section *section;
730 int ret = 0;
731
732 const struct weston_option options[] = {
733 { WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
734 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
735 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
736 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
737 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
738 };
739
740 parse_options(options, ARRAY_LENGTH(options), argc, argv);
741
742 section = weston_config_get_section(wc, "core", NULL, NULL);
743 weston_config_section_get_string(section,
744 "gbm-format", &config.gbm_format,
745 NULL);
746
747 config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
748 config.base.struct_size = sizeof(struct weston_drm_backend_config);
749 config.configure_output = drm_configure_output;
750
751 ret = load_backend_new(c, backend, &config.base);
752
753 free(config.gbm_format);
754 free(config.seat_id);
755
756 return ret;
757}
758
Giulio Camuffo43008c72015-10-17 19:24:15 +0300759static int
Benoit Gschwind3c530942016-04-15 20:28:32 -0700760load_headless_backend(struct weston_compositor *c, char const * backend,
761 int *argc, char **argv, struct weston_config *wc)
762{
763 struct weston_headless_backend_config config = {{ 0, }};
764 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +0200765 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -0700766
767 config.width = 1024;
768 config.height = 640;
769
770 const struct weston_option options[] = {
771 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
772 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
773 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
774 { WESTON_OPTION_STRING, "transform", 0, &transform },
775 };
776
777 parse_options(options, ARRAY_LENGTH(options), argc, argv);
778
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +0200779 config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
780 if (transform) {
781 if (weston_parse_transform(transform, &config.transform) < 0)
782 weston_log("Invalid transform \"%s\"\n", transform);
783 free(transform);
784 }
Benoit Gschwind3c530942016-04-15 20:28:32 -0700785
786 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
787 config.base.struct_size = sizeof(struct weston_headless_backend_config);
788
789 /* load the actual wayland backend and configure it */
790 ret = load_backend_new(c, backend, &config.base);
791
792 return ret;
793}
794
Benoit Gschwindbd573102016-04-22 17:05:26 +0200795static void
796weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
797{
798 config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
799 config->base.struct_size = sizeof(struct weston_rdp_backend_config);
800
801 config->width = 640;
802 config->height = 480;
803 config->bind_address = NULL;
804 config->port = 3389;
805 config->rdp_key = NULL;
806 config->server_cert = NULL;
807 config->server_key = NULL;
808 config->env_socket = 0;
809 config->no_clients_resize = 0;
810}
811
812static int
813load_rdp_backend(struct weston_compositor *c, char const * backend,
814 int *argc, char *argv[], struct weston_config *wc)
815{
816 struct weston_rdp_backend_config config = {{ 0, }};
817 int ret = 0;
818
819 weston_rdp_backend_config_init(&config);
820
821 const struct weston_option rdp_options[] = {
822 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
823 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
824 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
825 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
826 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
827 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
828 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
829 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
830 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
831 };
832
833 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
834
835 ret = load_backend_new(c, backend, &config.base);
836
837 free(config.bind_address);
838 free(config.rdp_key);
839 free(config.server_cert);
840 free(config.server_key);
841 return ret;
842}
843
Benoit Gschwind3c530942016-04-15 20:28:32 -0700844static int
Benoit Gschwind934e89a2016-04-27 23:56:42 +0200845load_fbdev_backend(struct weston_compositor *c, char const * backend,
846 int *argc, char **argv, struct weston_config *wc)
847{
848 struct weston_fbdev_backend_config config = {{ 0, }};
849 struct weston_config_section *section;
850 char *s = NULL;
851 int ret = 0;
852
853 const struct weston_option fbdev_options[] = {
854 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
855 { WESTON_OPTION_STRING, "device", 0, &config.device },
856 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &config.use_gl },
857 };
858
859 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
860
861 if (!config.device)
862 config.device = strdup("/dev/fb0");
863
864 section = weston_config_get_section(wc, "output", "name", "fbdev");
865 weston_config_section_get_string(section, "transform", &s, "normal");
866 if (weston_parse_transform(s, &config.output_transform) < 0)
867 weston_log("Invalid transform \"%s\" for output fbdev\n", s);
868 free(s);
869
870 config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
871 config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
872
873 /* load the actual wayland backend and configure it */
874 ret = load_backend_new(c, backend, &config.base);
875
876 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -0700877
878 return ret;
879}
880
881static int
882weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
883 struct weston_x11_backend_output_config *output_config) {
884 struct weston_x11_backend_output_config *new_outputs;
885
886 new_outputs = realloc(config->outputs, (config->num_outputs+1) *
887 sizeof(struct weston_x11_backend_output_config));
888 if (new_outputs == NULL)
889 return -1;
890
891 config->outputs = new_outputs;
892 config->outputs[config->num_outputs].width = output_config->width;
893 config->outputs[config->num_outputs].height = output_config->height;
894 config->outputs[config->num_outputs].transform = output_config->transform;
895 config->outputs[config->num_outputs].scale = output_config->scale;
896 config->outputs[config->num_outputs].name = strdup(output_config->name);
897 config->num_outputs++;
898
899 return 0;
900}
901
902static int
903load_x11_backend(struct weston_compositor *c, char const * backend,
904 int *argc, char **argv, struct weston_config *wc)
905{
906 struct weston_x11_backend_output_config default_output;
907 struct weston_x11_backend_config config = {{ 0, }};
908 struct weston_config_section *section;
909 int ret = 0;
910 int option_width = 0;
911 int option_height = 0;
912 int option_scale = 0;
913 int option_count = 1;
914 int output_count = 0;
915 char const *section_name;
916 int i;
917 uint32_t j;
918
919 const struct weston_option options[] = {
920 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
921 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
922 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
923 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
924 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
925 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
926 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
927 };
928
929 parse_options(options, ARRAY_LENGTH(options), argc, argv);
930
931 section = NULL;
932 while (weston_config_next_section(wc, &section, &section_name)) {
933 struct weston_x11_backend_output_config current_output = { 0, };
934 char *t;
935 char *mode;
936
937 if (strcmp(section_name, "output") != 0) {
938 continue;
939 }
940
941 weston_config_section_get_string(section, "name", &current_output.name, NULL);
942 if (current_output.name == NULL || current_output.name[0] != 'X') {
943 free(current_output.name);
944 continue;
945 }
946
947 weston_config_section_get_string(section, "mode", &mode, "1024x600");
948 if (sscanf(mode, "%dx%d", &current_output.width,
949 &current_output.height) != 2) {
950 weston_log("Invalid mode \"%s\" for output %s\n",
951 mode, current_output.name);
952 current_output.width = 1024;
953 current_output.height = 600;
954 }
955 free(mode);
956 if (current_output.width < 1)
957 current_output.width = 1024;
958 if (current_output.height < 1)
959 current_output.height = 600;
960 if (option_width)
961 current_output.width = option_width;
962 if (option_height)
963 current_output.height = option_height;
964
965 weston_config_section_get_int(section, "scale", &current_output.scale, 1);
966 if (option_scale)
967 current_output.scale = option_scale;
968
969 weston_config_section_get_string(section,
970 "transform", &t, "normal");
971 if (weston_parse_transform(t, &current_output.transform) < 0)
972 weston_log("Invalid transform \"%s\" for output %s\n",
973 t, current_output.name);
974 free(t);
975
976 if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
977 ret = -1;
978 goto out;
979 }
980
981 output_count++;
Bryce Harringtonaa258982016-05-03 01:34:23 -0700982 if (output_count >= option_count)
Benoit Gschwinde16acab2016-04-15 20:28:31 -0700983 break;
984 }
985
986 default_output.name = NULL;
987 default_output.width = option_width ? option_width : 1024;
988 default_output.height = option_height ? option_height : 600;
989 default_output.scale = option_scale ? option_scale : 1;
990 default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
991
992 for (i = output_count; i < option_count; i++) {
993 if (asprintf(&default_output.name, "screen%d", i) < 0) {
994 ret = -1;
995 goto out;
996 }
997
998 if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
999 ret = -1;
1000 free(default_output.name);
1001 goto out;
1002 }
1003 free(default_output.name);
1004 }
1005
1006 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
1007 config.base.struct_size = sizeof(struct weston_x11_backend_config);
1008
1009 /* load the actual backend and configure it */
1010 ret = load_backend_new(c, backend, &config.base);
1011
1012out:
1013 for (j = 0; j < config.num_outputs; ++j)
1014 free(config.outputs[j].name);
1015 free(config.outputs);
1016
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001017 return ret;
1018}
1019
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001020static void
1021weston_wayland_output_config_init(struct weston_wayland_backend_output_config *output_config,
1022 struct weston_config_section *config_section,
1023 int option_width, int option_height,
1024 int option_scale)
1025{
1026 char *mode, *t, *str;
1027 unsigned int slen;
1028
1029 weston_config_section_get_string(config_section, "name", &output_config->name,
1030 NULL);
1031 if (output_config->name) {
1032 slen = strlen(output_config->name);
1033 slen += strlen(WINDOW_TITLE " - ");
1034 str = malloc(slen + 1);
1035 if (str)
1036 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1037 output_config->name);
1038 free(output_config->name);
1039 output_config->name = str;
1040 }
1041 if (!output_config->name)
1042 output_config->name = strdup(WINDOW_TITLE);
1043
1044 weston_config_section_get_string(config_section,
1045 "mode", &mode, "1024x600");
1046 if (sscanf(mode, "%dx%d", &output_config->width, &output_config->height) != 2) {
1047 weston_log("Invalid mode \"%s\" for output %s\n",
1048 mode, output_config->name);
1049 output_config->width = 1024;
1050 output_config->height = 640;
1051 }
1052 free(mode);
1053
1054 if (option_width)
1055 output_config->width = option_width;
1056 if (option_height)
1057 output_config->height = option_height;
1058
1059 weston_config_section_get_int(config_section, "scale", &output_config->scale, 1);
1060
1061 if (option_scale)
1062 output_config->scale = option_scale;
1063
1064 weston_config_section_get_string(config_section,
1065 "transform", &t, "normal");
1066 if (weston_parse_transform(t, &output_config->transform) < 0)
1067 weston_log("Invalid transform \"%s\" for output %s\n",
1068 t, output_config->name);
1069 free(t);
1070
1071}
1072
1073static void
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001074weston_wayland_backend_config_release(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001075{
1076 int i;
1077
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001078 for (i = 0; i < config->num_outputs; ++i) {
1079 free(config->outputs[i].name);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001080 }
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001081 free(config->cursor_theme);
1082 free(config->display_name);
1083 free(config->outputs);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001084}
1085
1086/*
1087 * Append a new output struct at the end of new_config.outputs and return a
1088 * pointer to the newly allocated structure or NULL if fail. The allocated
1089 * structure is NOT cleared nor set to default values.
1090 */
1091static struct weston_wayland_backend_output_config *
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001092weston_wayland_backend_config_add_new_output(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001093{
1094 struct weston_wayland_backend_output_config *outputs;
1095 const size_t element_size = sizeof(struct weston_wayland_backend_output_config);
1096
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001097 outputs = realloc(config->outputs,
1098 (config->num_outputs + 1) * element_size);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001099 if (!outputs)
1100 return NULL;
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001101 config->num_outputs += 1;
1102 config->outputs = outputs;
1103 return &(config->outputs[config->num_outputs - 1]);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001104}
1105
1106static int
1107load_wayland_backend_config(struct weston_compositor *compositor, int *argc,
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001108 char *argv[], struct weston_config *wc,
Benoit Gschwind55a22882016-05-10 22:47:51 +02001109 struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001110{
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001111 struct weston_config_section *section;
1112 struct weston_wayland_backend_output_config *oc;
1113 int count, width, height, scale;
1114 const char *section_name;
1115 char *name;
1116
1117 const struct weston_option wayland_options[] = {
1118 { WESTON_OPTION_INTEGER, "width", 0, &width },
1119 { WESTON_OPTION_INTEGER, "height", 0, &height },
1120 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001121 { WESTON_OPTION_STRING, "display", 0, &config->display_name },
1122 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config->use_pixman },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001123 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001124 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config->fullscreen },
1125 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &config->sprawl },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001126 };
1127
1128 width = 0;
1129 height = 0;
1130 scale = 0;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001131 config->display_name = NULL;
1132 config->use_pixman = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001133 count = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001134 config->fullscreen = 0;
1135 config->sprawl = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001136 parse_options(wayland_options,
1137 ARRAY_LENGTH(wayland_options), argc, argv);
1138
Benoit Gschwind55a22882016-05-10 22:47:51 +02001139 config->cursor_size = 32;
1140 config->cursor_theme = NULL;
1141 config->base.struct_size = sizeof(struct weston_wayland_backend_config);
1142 config->base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001143
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001144 section = weston_config_get_section(wc, "shell", NULL, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001145 weston_config_section_get_string(section, "cursor-theme",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001146 &config->cursor_theme, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001147 weston_config_section_get_int(section, "cursor-size",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001148 &config->cursor_size, 32);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001149
Benoit Gschwind55a22882016-05-10 22:47:51 +02001150 if (config->sprawl) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001151 /* do nothing, everything is already set */
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001152 return 0;
1153 }
1154
Benoit Gschwind55a22882016-05-10 22:47:51 +02001155 if (config->fullscreen) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001156 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001157 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001158 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001159
1160 oc->width = width;
1161 oc->height = height;
1162 oc->name = NULL;
1163 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1164 oc->scale = 1;
1165
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001166 return 0;
1167 }
1168
1169 section = NULL;
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001170 while (weston_config_next_section(wc, &section, &section_name)) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001171 if (!section_name || strcmp(section_name, "output") != 0)
1172 continue;
1173 weston_config_section_get_string(section, "name", &name, NULL);
1174 if (name == NULL)
1175 continue;
1176
1177 if (name[0] != 'W' || name[1] != 'L') {
1178 free(name);
1179 continue;
1180 }
1181 free(name);
1182
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001183 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001184 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001185 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001186
1187 weston_wayland_output_config_init(oc, section, width,
1188 height, scale);
1189 --count;
1190 }
1191
1192 if (!width)
1193 width = 1024;
1194 if (!height)
1195 height = 640;
1196 if (!scale)
1197 scale = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001198
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001199 while (count > 0) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001200 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001201 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001202 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001203
1204 oc->width = width;
1205 oc->height = height;
1206 oc->name = NULL;
1207 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1208 oc->scale = scale;
1209
1210 --count;
1211 }
1212
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001213 return 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001214}
1215
1216static int
1217load_wayland_backend(struct weston_compositor *c, char const * backend,
1218 int *argc, char **argv, struct weston_config *wc)
1219{
1220 struct weston_wayland_backend_config config = {{ 0, }};
1221 int ret = 0;
1222
1223 ret = load_wayland_backend_config(c, argc, argv, wc, &config);
1224 if(ret < 0) {
Benoit Gschwind53753842016-05-10 22:47:57 +02001225 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001226 return ret;
1227 }
1228
1229 /* load the actual wayland backend and configure it */
1230 ret = load_backend_new(c, backend, &config.base);
Benoit Gschwind68d6a6c2016-05-10 22:47:53 +02001231 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001232 return ret;
1233}
1234
1235
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001236static int
Giulio Camuffo43008c72015-10-17 19:24:15 +03001237load_backend(struct weston_compositor *compositor, const char *backend,
1238 int *argc, char **argv, struct weston_config *config)
1239{
Benoit Gschwind3c530942016-04-15 20:28:32 -07001240 if (strstr(backend, "headless-backend.so"))
1241 return load_headless_backend(compositor, backend, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001242 else if (strstr(backend, "rdp-backend.so"))
1243 return load_rdp_backend(compositor, backend, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001244 else if (strstr(backend, "fbdev-backend.so"))
1245 return load_fbdev_backend(compositor, backend, argc, argv, config);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001246 else if (strstr(backend, "drm-backend.so"))
1247 return load_drm_backend(compositor, backend, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001248 else if (strstr(backend, "x11-backend.so"))
1249 return load_x11_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001250 else if (strstr(backend, "wayland-backend.so"))
1251 return load_wayland_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001252
1253 return load_backend_old(compositor, backend, argc, argv, config);
1254}
1255
Giulio Camuffobab996e2014-10-12 00:24:25 +03001256int main(int argc, char *argv[])
1257{
1258 int ret = EXIT_FAILURE;
1259 struct wl_display *display;
1260 struct weston_compositor *ec;
1261 struct wl_event_source *signals[4];
1262 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001263 int i, fd;
1264 char *backend = NULL;
1265 char *shell = NULL;
1266 char *modules = NULL;
1267 char *option_modules = NULL;
1268 char *log = NULL;
1269 char *server_socket = NULL, *end;
1270 int32_t idle_time = -1;
1271 int32_t help = 0;
1272 char *socket_name = NULL;
1273 int32_t version = 0;
1274 int32_t noconfig = 0;
1275 int32_t numlock_on;
1276 char *config_file = NULL;
1277 struct weston_config *config = NULL;
1278 struct weston_config_section *section;
1279 struct wl_client *primary_client;
1280 struct wl_listener primary_client_destroyed;
1281 struct weston_seat *seat;
1282
1283 const struct weston_option core_options[] = {
1284 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1285 { WESTON_OPTION_STRING, "shell", 0, &shell },
1286 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1287 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
1288 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1289 { WESTON_OPTION_STRING, "log", 0, &log },
1290 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1291 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1292 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1293 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1294 };
1295
1296 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1297
1298 if (help)
1299 usage(EXIT_SUCCESS);
1300
1301 if (version) {
1302 printf(PACKAGE_STRING "\n");
1303 return EXIT_SUCCESS;
1304 }
1305
1306 weston_log_file_open(log);
1307
1308 weston_log("%s\n"
1309 STAMP_SPACE "%s\n"
1310 STAMP_SPACE "Bug reports to: %s\n"
1311 STAMP_SPACE "Build: %s\n",
1312 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1313 BUILD_ID);
1314 log_uname();
1315
1316 verify_xdg_runtime_dir();
1317
1318 display = wl_display_create();
1319
1320 loop = wl_display_get_event_loop(display);
1321 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1322 display);
1323 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1324 display);
1325 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1326 display);
1327
1328 wl_list_init(&child_process_list);
1329 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1330 NULL);
1331
1332 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1333 goto out_signals;
1334
1335 if (load_configuration(&config, noconfig, config_file) < 0)
1336 goto out_signals;
1337
1338 section = weston_config_get_section(config, "core", NULL, NULL);
1339
1340 if (!backend) {
1341 weston_config_section_get_string(section, "backend", &backend,
1342 NULL);
1343 if (!backend)
1344 backend = weston_choose_default_backend();
1345 }
1346
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001347 ec = weston_compositor_create(display, config);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001348 if (ec == NULL) {
1349 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001350 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001351 }
1352
1353 ec->config = config;
1354 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001355 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001356
Giulio Camuffo43008c72015-10-17 19:24:15 +03001357 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001358 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001359 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001360 }
1361
1362 catch_signals();
1363 segv_compositor = ec;
1364
1365 if (idle_time < 0)
1366 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1367 if (idle_time < 0)
1368 idle_time = 300; /* default idle timeout, in seconds */
1369
1370 ec->idle_time = idle_time;
1371 ec->default_pointer_grab = NULL;
1372 ec->exit = handle_exit;
1373
1374 weston_compositor_log_capabilities(ec);
1375
1376 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1377 if (server_socket) {
1378 weston_log("Running with single client\n");
1379 fd = strtol(server_socket, &end, 0);
1380 if (*end != '\0')
1381 fd = -1;
1382 } else {
1383 fd = -1;
1384 }
1385
1386 if (fd != -1) {
1387 primary_client = wl_client_create(display, fd);
1388 if (!primary_client) {
1389 weston_log("fatal: failed to add client: %m\n");
1390 goto out;
1391 }
1392 primary_client_destroyed.notify =
1393 handle_primary_client_destroyed;
1394 wl_client_add_destroy_listener(primary_client,
1395 &primary_client_destroyed);
1396 } else if (weston_create_listening_socket(display, socket_name)) {
1397 goto out;
1398 }
1399
1400 if (!shell)
1401 weston_config_section_get_string(section, "shell", &shell,
1402 "desktop-shell.so");
1403
1404 if (load_modules(ec, shell, &argc, argv) < 0)
1405 goto out;
1406
1407 weston_config_section_get_string(section, "modules", &modules, "");
1408 if (load_modules(ec, modules, &argc, argv) < 0)
1409 goto out;
1410
1411 if (load_modules(ec, option_modules, &argc, argv) < 0)
1412 goto out;
1413
1414 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1415 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1416 if (numlock_on) {
1417 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001418 struct weston_keyboard *keyboard =
1419 weston_seat_get_keyboard(seat);
1420
1421 if (keyboard)
1422 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001423 WESTON_NUM_LOCK,
1424 WESTON_NUM_LOCK);
1425 }
1426 }
1427
1428 for (i = 1; i < argc; i++)
1429 weston_log("fatal: unhandled option: %s\n", argv[i]);
1430 if (argc > 1)
1431 goto out;
1432
1433 weston_compositor_wake(ec);
1434
1435 wl_display_run(display);
1436
1437 /* Allow for setting return exit code after
1438 * wl_display_run returns normally. This is
1439 * useful for devs/testers and automated tests
1440 * that want to indicate failure status to
1441 * testing infrastructure above
1442 */
1443 ret = ec->exit_code;
1444
1445out:
1446 weston_compositor_destroy(ec);
1447
1448out_signals:
1449 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1450 if (signals[i])
1451 wl_event_source_remove(signals[i]);
1452
1453 wl_display_destroy(display);
1454
1455 weston_log_file_close();
1456
1457 if (config)
1458 weston_config_destroy(config);
1459 free(config_file);
1460 free(backend);
1461 free(shell);
1462 free(socket_name);
1463 free(option_modules);
1464 free(log);
1465 free(modules);
1466
1467 return ret;
1468}