blob: 6a94c765f18830fd6a2b3706526262485e1c0b6b [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>
Giulio Camuffofba27fb2016-06-02 21:48:10 +030040#include <sys/socket.h>
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +030041#include <libinput.h>
Giulio Camuffobab996e2014-10-12 00:24:25 +030042
43#ifdef HAVE_LIBUNWIND
44#define UNW_LOCAL_ONLY
45#include <libunwind.h>
46#endif
47
48#include "compositor.h"
49#include "../shared/os-compatibility.h"
50#include "../shared/helpers.h"
51#include "git-version.h"
52#include "version.h"
Giulio Camuffofba27fb2016-06-02 21:48:10 +030053#include "weston.h"
Giulio Camuffobab996e2014-10-12 00:24:25 +030054
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070055#include "compositor-drm.h"
Benoit Gschwind3c530942016-04-15 20:28:32 -070056#include "compositor-headless.h"
Benoit Gschwindbd573102016-04-22 17:05:26 +020057#include "compositor-rdp.h"
Benoit Gschwind934e89a2016-04-27 23:56:42 +020058#include "compositor-fbdev.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070059#include "compositor-x11.h"
Benoit Gschwind3ff10da2016-05-10 22:47:49 +020060#include "compositor-wayland.h"
61
62#define WINDOW_TITLE "Weston Compositor"
Benoit Gschwind3c530942016-04-15 20:28:32 -070063
Giulio Camuffobab996e2014-10-12 00:24:25 +030064static struct wl_list child_process_list;
65static struct weston_compositor *segv_compositor;
66
67static int
68sigchld_handler(int signal_number, void *data)
69{
70 struct weston_process *p;
71 int status;
72 pid_t pid;
73
74 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
75 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
81 weston_log("unknown child process exited\n");
82 continue;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87 }
88
89 if (pid < 0 && errno != ECHILD)
90 weston_log("waitpid error %m\n");
91
92 return 1;
93}
94
95#ifdef HAVE_LIBUNWIND
96
97static void
98print_backtrace(void)
99{
100 unw_cursor_t cursor;
101 unw_context_t context;
102 unw_word_t off;
103 unw_proc_info_t pip;
104 int ret, i = 0;
105 char procname[256];
106 const char *filename;
107 Dl_info dlinfo;
108
109 pip.unwind_info = NULL;
110 ret = unw_getcontext(&context);
111 if (ret) {
112 weston_log("unw_getcontext: %d\n", ret);
113 return;
114 }
115
116 ret = unw_init_local(&cursor, &context);
117 if (ret) {
118 weston_log("unw_init_local: %d\n", ret);
119 return;
120 }
121
122 ret = unw_step(&cursor);
123 while (ret > 0) {
124 ret = unw_get_proc_info(&cursor, &pip);
125 if (ret) {
126 weston_log("unw_get_proc_info: %d\n", ret);
127 break;
128 }
129
130 ret = unw_get_proc_name(&cursor, procname, 256, &off);
131 if (ret && ret != -UNW_ENOMEM) {
132 if (ret != -UNW_EUNSPEC)
133 weston_log("unw_get_proc_name: %d\n", ret);
134 procname[0] = '?';
135 procname[1] = 0;
136 }
137
138 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
139 *dlinfo.dli_fname)
140 filename = dlinfo.dli_fname;
141 else
142 filename = "?";
143
144 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
145 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
146
147 ret = unw_step(&cursor);
148 if (ret < 0)
149 weston_log("unw_step: %d\n", ret);
150 }
151}
152
153#else
154
155static void
156print_backtrace(void)
157{
158 void *buffer[32];
159 int i, count;
160 Dl_info info;
161
162 count = backtrace(buffer, ARRAY_LENGTH(buffer));
163 for (i = 0; i < count; i++) {
164 dladdr(buffer[i], &info);
165 weston_log(" [%016lx] %s (%s)\n",
166 (long) buffer[i],
167 info.dli_sname ? info.dli_sname : "--",
168 info.dli_fname);
169 }
170}
171
172#endif
173
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300174static void
175child_client_exec(int sockfd, const char *path)
176{
177 int clientfd;
178 char s[32];
179 sigset_t allsigs;
180
181 /* do not give our signal mask to the new process */
182 sigfillset(&allsigs);
183 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
184
185 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
186 if (seteuid(getuid()) == -1) {
187 weston_log("compositor: failed seteuid\n");
188 return;
189 }
190
191 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
192 * non-CLOEXEC fd to pass through exec. */
193 clientfd = dup(sockfd);
194 if (clientfd == -1) {
195 weston_log("compositor: dup failed: %m\n");
196 return;
197 }
198
199 snprintf(s, sizeof s, "%d", clientfd);
200 setenv("WAYLAND_SOCKET", s, 1);
201
202 if (execl(path, path, NULL) < 0)
203 weston_log("compositor: executing '%s' failed: %m\n",
204 path);
205}
206
207WL_EXPORT struct wl_client *
208weston_client_launch(struct weston_compositor *compositor,
209 struct weston_process *proc,
210 const char *path,
211 weston_process_cleanup_func_t cleanup)
212{
213 int sv[2];
214 pid_t pid;
215 struct wl_client *client;
216
217 weston_log("launching '%s'\n", path);
218
219 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
220 weston_log("weston_client_launch: "
221 "socketpair failed while launching '%s': %m\n",
222 path);
223 return NULL;
224 }
225
226 pid = fork();
227 if (pid == -1) {
228 close(sv[0]);
229 close(sv[1]);
230 weston_log("weston_client_launch: "
231 "fork failed while launching '%s': %m\n", path);
232 return NULL;
233 }
234
235 if (pid == 0) {
236 child_client_exec(sv[1], path);
237 _exit(-1);
238 }
239
240 close(sv[1]);
241
242 client = wl_client_create(compositor->wl_display, sv[0]);
243 if (!client) {
244 close(sv[0]);
245 weston_log("weston_client_launch: "
246 "wl_client_create failed while launching '%s'.\n",
247 path);
248 return NULL;
249 }
250
251 proc->pid = pid;
252 proc->cleanup = cleanup;
253 weston_watch_process(proc);
254
255 return client;
256}
257
Giulio Camuffobab996e2014-10-12 00:24:25 +0300258WL_EXPORT void
259weston_watch_process(struct weston_process *process)
260{
261 wl_list_insert(&child_process_list, &process->link);
262}
263
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300264struct process_info {
265 struct weston_process proc;
266 char *path;
267};
268
269static void
270process_handle_sigchld(struct weston_process *process, int status)
271{
272 struct process_info *pinfo =
273 container_of(process, struct process_info, proc);
274
275 /*
276 * There are no guarantees whether this runs before or after
277 * the wl_client destructor.
278 */
279
280 if (WIFEXITED(status)) {
281 weston_log("%s exited with status %d\n", pinfo->path,
282 WEXITSTATUS(status));
283 } else if (WIFSIGNALED(status)) {
284 weston_log("%s died on signal %d\n", pinfo->path,
285 WTERMSIG(status));
286 } else {
287 weston_log("%s disappeared\n", pinfo->path);
288 }
289
290 free(pinfo->path);
291 free(pinfo);
292}
293
294WL_EXPORT struct wl_client *
295weston_client_start(struct weston_compositor *compositor, const char *path)
296{
297 struct process_info *pinfo;
298 struct wl_client *client;
299
300 pinfo = zalloc(sizeof *pinfo);
301 if (!pinfo)
302 return NULL;
303
304 pinfo->path = strdup(path);
305 if (!pinfo->path)
306 goto out_free;
307
308 client = weston_client_launch(compositor, &pinfo->proc, path,
309 process_handle_sigchld);
310 if (!client)
311 goto out_str;
312
313 return client;
314
315out_str:
316 free(pinfo->path);
317
318out_free:
319 free(pinfo);
320
321 return NULL;
322}
323
Giulio Camuffobab996e2014-10-12 00:24:25 +0300324static void
325log_uname(void)
326{
327 struct utsname usys;
328
329 uname(&usys);
330
331 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
332 usys.version, usys.machine);
333}
334
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300335WL_EXPORT struct weston_config *
336wet_get_config(struct weston_compositor *compositor)
337{
338 return weston_compositor_get_user_data(compositor);
339}
340
Giulio Camuffobab996e2014-10-12 00:24:25 +0300341static const char xdg_error_message[] =
342 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
343
344static const char xdg_wrong_message[] =
345 "fatal: environment variable XDG_RUNTIME_DIR\n"
346 "is set to \"%s\", which is not a directory.\n";
347
348static const char xdg_wrong_mode_message[] =
349 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
350 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
351 "and must be owned by the user (current owner is UID %d).\n";
352
353static const char xdg_detail_message[] =
354 "Refer to your distribution on how to get it, or\n"
355 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
356 "on how to implement it.\n";
357
358static void
359verify_xdg_runtime_dir(void)
360{
361 char *dir = getenv("XDG_RUNTIME_DIR");
362 struct stat s;
363
364 if (!dir) {
365 weston_log(xdg_error_message);
366 weston_log_continue(xdg_detail_message);
367 exit(EXIT_FAILURE);
368 }
369
370 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
371 weston_log(xdg_wrong_message, dir);
372 weston_log_continue(xdg_detail_message);
373 exit(EXIT_FAILURE);
374 }
375
376 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
377 weston_log(xdg_wrong_mode_message,
378 dir, s.st_mode & 0777, s.st_uid);
379 weston_log_continue(xdg_detail_message);
380 }
381}
382
383static int
384usage(int error_code)
385{
386 fprintf(stderr,
387 "Usage: weston [OPTIONS]\n\n"
388 "This is weston version " VERSION ", the Wayland reference compositor.\n"
389 "Weston supports multiple backends, and depending on which backend is in use\n"
390 "different options will be accepted.\n\n"
391
392
393 "Core options:\n\n"
394 " --version\t\tPrint weston version\n"
395 " -B, --backend=MODULE\tBackend module, one of\n"
396#if defined(BUILD_DRM_COMPOSITOR)
397 "\t\t\t\tdrm-backend.so\n"
398#endif
399#if defined(BUILD_FBDEV_COMPOSITOR)
400 "\t\t\t\tfbdev-backend.so\n"
401#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300402#if defined(BUILD_HEADLESS_COMPOSITOR)
403 "\t\t\t\theadless-backend.so\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300404#endif
405#if defined(BUILD_RDP_COMPOSITOR)
406 "\t\t\t\trdp-backend.so\n"
407#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300408#if defined(BUILD_WAYLAND_COMPOSITOR)
409 "\t\t\t\twayland-backend.so\n"
410#endif
411#if defined(BUILD_X11_COMPOSITOR)
412 "\t\t\t\tx11-backend.so\n"
413#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300414 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
415 " -S, --socket=NAME\tName of socket to listen on\n"
416 " -i, --idle-time=SECS\tIdle time in seconds\n"
417 " --modules\t\tLoad the comma-separated list of modules\n"
418 " --log=FILE\t\tLog to the given file\n"
419 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
420 " --no-config\t\tDo not read weston.ini\n"
421 " -h, --help\t\tThis help message\n\n");
422
423#if defined(BUILD_DRM_COMPOSITOR)
424 fprintf(stderr,
425 "Options for drm-backend.so:\n\n"
426 " --connector=ID\tBring up only this connector\n"
427 " --seat=SEAT\t\tThe seat that weston should run on\n"
428 " --tty=TTY\t\tThe tty to use\n"
429 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
430 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
431#endif
432
433#if defined(BUILD_FBDEV_COMPOSITOR)
434 fprintf(stderr,
435 "Options for fbdev-backend.so:\n\n"
436 " --tty=TTY\t\tThe tty to use\n"
437 " --device=DEVICE\tThe framebuffer device to use\n"
438 " --use-gl\t\tUse the GL renderer\n\n");
439#endif
440
Dawid Gajownik71f57042015-07-31 17:39:00 -0300441#if defined(BUILD_HEADLESS_COMPOSITOR)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300442 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300443 "Options for headless-backend.so:\n\n"
444 " --width=WIDTH\t\tWidth of memory surface\n"
445 " --height=HEIGHT\tHeight of memory surface\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300446 " --transform=TR\tThe output transformation, TR is one of:\n"
447 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Dawid Gajownik71f57042015-07-31 17:39:00 -0300448 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300449#endif
450
451#if defined(BUILD_RDP_COMPOSITOR)
452 fprintf(stderr,
453 "Options for rdp-backend.so:\n\n"
454 " --width=WIDTH\t\tWidth of desktop\n"
455 " --height=HEIGHT\tHeight of desktop\n"
Dawid Gajownikd99a0502015-07-31 14:49:57 -0300456 " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300457 " --address=ADDR\tThe address to bind\n"
458 " --port=PORT\t\tThe port to listen on\n"
459 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
460 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
461 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
462 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
463 "\n");
464#endif
465
Dawid Gajownik71f57042015-07-31 17:39:00 -0300466#if defined(BUILD_WAYLAND_COMPOSITOR)
467 fprintf(stderr,
468 "Options for wayland-backend.so:\n\n"
469 " --width=WIDTH\t\tWidth of Wayland surface\n"
470 " --height=HEIGHT\tHeight of Wayland surface\n"
471 " --scale=SCALE\t\tScale factor of output\n"
472 " --fullscreen\t\tRun in fullscreen mode\n"
473 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
474 " --output-count=COUNT\tCreate multiple outputs\n"
475 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
476 " --display=DISPLAY\tWayland display to connect to\n\n");
477#endif
478
479#if defined(BUILD_X11_COMPOSITOR)
480 fprintf(stderr,
481 "Options for x11-backend.so:\n\n"
482 " --width=WIDTH\t\tWidth of X window\n"
483 " --height=HEIGHT\tHeight of X window\n"
484 " --scale=SCALE\t\tScale factor of output\n"
485 " --fullscreen\t\tRun in fullscreen mode\n"
486 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
487 " --output-count=COUNT\tCreate multiple outputs\n"
488 " --no-input\t\tDont create input devices\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300489#endif
490
491 exit(error_code);
492}
493
494static int on_term_signal(int signal_number, void *data)
495{
496 struct wl_display *display = data;
497
498 weston_log("caught signal %d\n", signal_number);
499 wl_display_terminate(display);
500
501 return 1;
502}
503
504static void
505on_caught_signal(int s, siginfo_t *siginfo, void *context)
506{
507 /* This signal handler will do a best-effort backtrace, and
508 * then call the backend restore function, which will switch
509 * back to the vt we launched from or ungrab X etc and then
510 * raise SIGTRAP. If we run weston under gdb from X or a
511 * different vt, and tell gdb "handle *s* nostop", this
512 * will allow weston to switch back to gdb on crash and then
513 * gdb will catch the crash with SIGTRAP.*/
514
515 weston_log("caught signal: %d\n", s);
516
517 print_backtrace();
518
519 segv_compositor->backend->restore(segv_compositor);
520
521 raise(SIGTRAP);
522}
523
524static void
525catch_signals(void)
526{
527 struct sigaction action;
528
529 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
530 action.sa_sigaction = on_caught_signal;
531 sigemptyset(&action.sa_mask);
532 sigaction(SIGSEGV, &action, NULL);
533 sigaction(SIGABRT, &action, NULL);
534}
535
536static const char *
537clock_name(clockid_t clk_id)
538{
539 static const char *names[] = {
540 [CLOCK_REALTIME] = "CLOCK_REALTIME",
541 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
542 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
543 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
544 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500545#ifdef CLOCK_BOOTTIME
Giulio Camuffobab996e2014-10-12 00:24:25 +0300546 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500547#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300548 };
549
550 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
551 return "unknown";
552
553 return names[clk_id];
554}
555
556static const struct {
557 uint32_t bit; /* enum weston_capability */
558 const char *desc;
559} capability_strings[] = {
560 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
561 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
562};
563
564static void
565weston_compositor_log_capabilities(struct weston_compositor *compositor)
566{
567 unsigned i;
568 int yes;
569
570 weston_log("Compositor capabilities:\n");
571 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
572 yes = compositor->capabilities & capability_strings[i].bit;
573 weston_log_continue(STAMP_SPACE "%s %s\n",
574 capability_strings[i].desc,
575 yes ? "yes" : "no");
576 }
577
578 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
579 clock_name(compositor->presentation_clock),
580 compositor->presentation_clock);
581}
582
583static void
584handle_primary_client_destroyed(struct wl_listener *listener, void *data)
585{
586 struct wl_client *client = data;
587
588 weston_log("Primary client died. Closing...\n");
589
590 wl_display_terminate(wl_client_get_display(client));
591}
592
593static int
594weston_create_listening_socket(struct wl_display *display, const char *socket_name)
595{
596 if (socket_name) {
597 if (wl_display_add_socket(display, socket_name)) {
598 weston_log("fatal: failed to add socket: %m\n");
599 return -1;
600 }
601 } else {
602 socket_name = wl_display_add_socket_auto(display);
603 if (!socket_name) {
604 weston_log("fatal: failed to add socket: %m\n");
605 return -1;
606 }
607 }
608
609 setenv("WAYLAND_DISPLAY", socket_name, 1);
610
611 return 0;
612}
613
614static int
615load_modules(struct weston_compositor *ec, const char *modules,
616 int *argc, char *argv[])
617{
618 const char *p, *end;
619 char buffer[256];
620 int (*module_init)(struct weston_compositor *ec,
621 int *argc, char *argv[]);
622
623 if (modules == NULL)
624 return 0;
625
626 p = modules;
627 while (*p) {
628 end = strchrnul(p, ',');
629 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
630 module_init = weston_load_module(buffer, "module_init");
631 if (!module_init)
632 return -1;
633 if (module_init(ec, argc, argv) < 0)
634 return -1;
635 p = end;
636 while (*p == ',')
637 p++;
638
639 }
640
641 return 0;
642}
643
644static int
645weston_compositor_init_config(struct weston_compositor *ec,
646 struct weston_config *config)
647{
648 struct xkb_rule_names xkb_names;
649 struct weston_config_section *s;
650 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000651 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300652
653 s = weston_config_get_section(config, "keyboard", NULL, NULL);
654 weston_config_section_get_string(s, "keymap_rules",
655 (char **) &xkb_names.rules, NULL);
656 weston_config_section_get_string(s, "keymap_model",
657 (char **) &xkb_names.model, NULL);
658 weston_config_section_get_string(s, "keymap_layout",
659 (char **) &xkb_names.layout, NULL);
660 weston_config_section_get_string(s, "keymap_variant",
661 (char **) &xkb_names.variant, NULL);
662 weston_config_section_get_string(s, "keymap_options",
663 (char **) &xkb_names.options, NULL);
664
Giulio Camuffo0358af42016-06-02 21:48:08 +0300665 if (weston_compositor_set_xkb_rule_names(ec, &xkb_names) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300666 return -1;
667
668 weston_config_section_get_int(s, "repeat-rate",
669 &ec->kb_repeat_rate, 40);
670 weston_config_section_get_int(s, "repeat-delay",
671 &ec->kb_repeat_delay, 400);
672
Bob Ham91880f12016-01-12 10:21:47 +0000673 weston_config_section_get_bool(s, "vt-switching",
674 &vt_switching, true);
675 ec->vt_switching = vt_switching;
676
Giulio Camuffobab996e2014-10-12 00:24:25 +0300677 s = weston_config_get_section(config, "core", NULL, NULL);
678 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
679 ec->repaint_msec);
680 if (repaint_msec < -10 || repaint_msec > 1000) {
681 weston_log("Invalid repaint_window value in config: %d\n",
682 repaint_msec);
683 } else {
684 ec->repaint_msec = repaint_msec;
685 }
686 weston_log("Output repaint window is %d ms maximum.\n",
687 ec->repaint_msec);
688
689 return 0;
690}
691
692static char *
693weston_choose_default_backend(void)
694{
695 char *backend = NULL;
696
697 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
698 backend = strdup("wayland-backend.so");
699 else if (getenv("DISPLAY"))
700 backend = strdup("x11-backend.so");
701 else
702 backend = strdup(WESTON_NATIVE_BACKEND);
703
704 return backend;
705}
706
707static const struct { const char *name; uint32_t token; } transforms[] = {
708 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
709 { "90", WL_OUTPUT_TRANSFORM_90 },
710 { "180", WL_OUTPUT_TRANSFORM_180 },
711 { "270", WL_OUTPUT_TRANSFORM_270 },
712 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
713 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
714 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
715 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
716};
717
718WL_EXPORT int
719weston_parse_transform(const char *transform, uint32_t *out)
720{
721 unsigned int i;
722
723 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
724 if (strcmp(transforms[i].name, transform) == 0) {
725 *out = transforms[i].token;
726 return 0;
727 }
728
729 *out = WL_OUTPUT_TRANSFORM_NORMAL;
730 return -1;
731}
732
733WL_EXPORT const char *
734weston_transform_to_string(uint32_t output_transform)
735{
736 unsigned int i;
737
738 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
739 if (transforms[i].token == output_transform)
740 return transforms[i].name;
741
742 return "<illegal value>";
743}
744
745static int
746load_configuration(struct weston_config **config, int32_t noconfig,
747 const char *config_file)
748{
749 const char *file = "weston.ini";
750 const char *full_path;
751
752 *config = NULL;
753
754 if (config_file)
755 file = config_file;
756
757 if (noconfig == 0)
758 *config = weston_config_parse(file);
759
760 if (*config) {
761 full_path = weston_config_get_full_path(*config);
762
763 weston_log("Using config file '%s'\n", full_path);
764 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
765
766 return 0;
767 }
768
769 if (config_file && noconfig == 0) {
770 weston_log("fatal: error opening or reading config file"
771 " '%s'.\n", config_file);
772
773 return -1;
774 }
775
776 weston_log("Starting with no config file.\n");
777 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
778
779 return 0;
780}
781
782static void
783handle_exit(struct weston_compositor *c)
784{
785 wl_display_terminate(c->wl_display);
786}
787
Giulio Camuffo43008c72015-10-17 19:24:15 +0300788/* Temporary function to be removed when all backends are converted. */
789static int
790load_backend_old(struct weston_compositor *compositor, const char *backend,
791 int *argc, char **argv, struct weston_config *wc)
792{
793 int (*backend_init)(struct weston_compositor *c,
794 int *argc, char *argv[],
795 struct weston_config *config,
796 struct weston_backend_config *config_base);
797
798 backend_init = weston_load_module(backend, "backend_init");
799 if (!backend_init)
800 return -1;
801
802 return backend_init(compositor, argc, argv, wc, NULL);
803}
804
Bryce Harrington98ab08b2016-04-15 20:28:36 -0700805/** Main module call-point for backends.
806 *
807 * All backends should use this routine to access their init routine.
808 * Backends may subclass weston_backend_config to add their own
809 * configuration data, setting the major/minor version in config_base
810 * accordingly.
811 *
812 * The config_base object should be treated as temporary, and any data
813 * copied out of it by backend_init before returning. The load_backend_new
814 * callers may then free the config_base object.
815 *
816 * NOTE: This is a temporary function intended to eventually be replaced
817 * by weston_compositor_load_backend().
818 */
Bryce Harrington6ca25b32016-04-15 20:28:27 -0700819static int
820load_backend_new(struct weston_compositor *compositor, const char *backend,
821 struct weston_backend_config *config_base)
822{
823 int (*backend_init)(struct weston_compositor *c,
824 int *argc, char *argv[],
825 struct weston_config *config,
826 struct weston_backend_config *config_base);
827
828 backend_init = weston_load_module(backend, "backend_init");
829 if (!backend_init)
830 return -1;
831
832 return backend_init(compositor, NULL, NULL, NULL, config_base);
833}
834
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700835static enum weston_drm_backend_output_mode
836drm_configure_output(struct weston_compositor *c,
837 bool use_current_mode,
838 const char *name,
839 struct weston_drm_backend_output_config *config)
840{
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300841 struct weston_config *wc = wet_get_config(c);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700842 struct weston_config_section *section;
843 char *s;
844 int scale;
845 enum weston_drm_backend_output_mode mode =
846 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
847
848 section = weston_config_get_section(wc, "output", "name", name);
849 weston_config_section_get_string(section, "mode", &s, "preferred");
850 if (strcmp(s, "off") == 0) {
851 free(s);
852 return WESTON_DRM_BACKEND_OUTPUT_OFF;
853 }
854
855 if (use_current_mode || strcmp(s, "current") == 0) {
856 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
857 } else if (strcmp(s, "preferred") != 0) {
858 config->modeline = s;
859 s = NULL;
860 }
861 free(s);
862
863 weston_config_section_get_int(section, "scale", &scale, 1);
864 config->base.scale = scale >= 1 ? scale : 1;
865 weston_config_section_get_string(section, "transform", &s, "normal");
866 if (weston_parse_transform(s, &config->base.transform) < 0)
867 weston_log("Invalid transform \"%s\" for output %s\n",
868 s, name);
869 free(s);
870
871 weston_config_section_get_string(section,
872 "gbm-format", &config->gbm_format, NULL);
873 weston_config_section_get_string(section, "seat", &config->seat, "");
874 return mode;
875}
876
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300877static void
878configure_input_device(struct weston_compositor *compositor,
879 struct libinput_device *device)
880{
881 struct weston_config_section *s;
882 struct weston_config *config = wet_get_config(compositor);
883 int enable_tap;
884 int enable_tap_default;
885
886 s = weston_config_get_section(config,
887 "libinput", NULL, NULL);
888
889 if (libinput_device_config_tap_get_finger_count(device) > 0) {
890 enable_tap_default =
891 libinput_device_config_tap_get_default_enabled(
892 device);
893 weston_config_section_get_bool(s, "enable_tap",
894 &enable_tap,
895 enable_tap_default);
896 libinput_device_config_tap_set_enabled(device,
897 enable_tap);
898 }
899}
900
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700901static int
902load_drm_backend(struct weston_compositor *c, const char *backend,
903 int *argc, char **argv, struct weston_config *wc)
904{
905 struct weston_drm_backend_config config = {{ 0, }};
906 struct weston_config_section *section;
907 int ret = 0;
908
909 const struct weston_option options[] = {
910 { WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
911 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
912 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
913 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
914 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
915 };
916
917 parse_options(options, ARRAY_LENGTH(options), argc, argv);
918
919 section = weston_config_get_section(wc, "core", NULL, NULL);
920 weston_config_section_get_string(section,
921 "gbm-format", &config.gbm_format,
922 NULL);
923
924 config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
925 config.base.struct_size = sizeof(struct weston_drm_backend_config);
926 config.configure_output = drm_configure_output;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300927 config.configure_device = configure_input_device;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700928
929 ret = load_backend_new(c, backend, &config.base);
930
931 free(config.gbm_format);
932 free(config.seat_id);
933
934 return ret;
935}
936
Giulio Camuffo43008c72015-10-17 19:24:15 +0300937static int
Benoit Gschwind3c530942016-04-15 20:28:32 -0700938load_headless_backend(struct weston_compositor *c, char const * backend,
939 int *argc, char **argv, struct weston_config *wc)
940{
941 struct weston_headless_backend_config config = {{ 0, }};
942 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +0200943 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -0700944
945 config.width = 1024;
946 config.height = 640;
947
948 const struct weston_option options[] = {
949 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
950 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
951 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
952 { WESTON_OPTION_STRING, "transform", 0, &transform },
953 };
954
955 parse_options(options, ARRAY_LENGTH(options), argc, argv);
956
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +0200957 config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
958 if (transform) {
959 if (weston_parse_transform(transform, &config.transform) < 0)
960 weston_log("Invalid transform \"%s\"\n", transform);
961 free(transform);
962 }
Benoit Gschwind3c530942016-04-15 20:28:32 -0700963
964 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
965 config.base.struct_size = sizeof(struct weston_headless_backend_config);
966
967 /* load the actual wayland backend and configure it */
968 ret = load_backend_new(c, backend, &config.base);
969
970 return ret;
971}
972
Benoit Gschwindbd573102016-04-22 17:05:26 +0200973static void
974weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
975{
976 config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
977 config->base.struct_size = sizeof(struct weston_rdp_backend_config);
978
979 config->width = 640;
980 config->height = 480;
981 config->bind_address = NULL;
982 config->port = 3389;
983 config->rdp_key = NULL;
984 config->server_cert = NULL;
985 config->server_key = NULL;
986 config->env_socket = 0;
987 config->no_clients_resize = 0;
988}
989
990static int
991load_rdp_backend(struct weston_compositor *c, char const * backend,
992 int *argc, char *argv[], struct weston_config *wc)
993{
994 struct weston_rdp_backend_config config = {{ 0, }};
995 int ret = 0;
996
997 weston_rdp_backend_config_init(&config);
998
999 const struct weston_option rdp_options[] = {
1000 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1001 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1002 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1003 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
1004 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1005 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1006 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
1007 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
1008 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
1009 };
1010
1011 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1012
1013 ret = load_backend_new(c, backend, &config.base);
1014
1015 free(config.bind_address);
1016 free(config.rdp_key);
1017 free(config.server_cert);
1018 free(config.server_key);
1019 return ret;
1020}
1021
Benoit Gschwind3c530942016-04-15 20:28:32 -07001022static int
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001023load_fbdev_backend(struct weston_compositor *c, char const * backend,
1024 int *argc, char **argv, struct weston_config *wc)
1025{
1026 struct weston_fbdev_backend_config config = {{ 0, }};
1027 struct weston_config_section *section;
1028 char *s = NULL;
1029 int ret = 0;
1030
1031 const struct weston_option fbdev_options[] = {
1032 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1033 { WESTON_OPTION_STRING, "device", 0, &config.device },
1034 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &config.use_gl },
1035 };
1036
1037 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
1038
1039 if (!config.device)
1040 config.device = strdup("/dev/fb0");
1041
1042 section = weston_config_get_section(wc, "output", "name", "fbdev");
1043 weston_config_section_get_string(section, "transform", &s, "normal");
1044 if (weston_parse_transform(s, &config.output_transform) < 0)
1045 weston_log("Invalid transform \"%s\" for output fbdev\n", s);
1046 free(s);
1047
1048 config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
1049 config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001050 config.configure_device = configure_input_device;
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001051
1052 /* load the actual wayland backend and configure it */
1053 ret = load_backend_new(c, backend, &config.base);
1054
1055 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001056
1057 return ret;
1058}
1059
1060static int
1061weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
1062 struct weston_x11_backend_output_config *output_config) {
1063 struct weston_x11_backend_output_config *new_outputs;
1064
1065 new_outputs = realloc(config->outputs, (config->num_outputs+1) *
1066 sizeof(struct weston_x11_backend_output_config));
1067 if (new_outputs == NULL)
1068 return -1;
1069
1070 config->outputs = new_outputs;
1071 config->outputs[config->num_outputs].width = output_config->width;
1072 config->outputs[config->num_outputs].height = output_config->height;
1073 config->outputs[config->num_outputs].transform = output_config->transform;
1074 config->outputs[config->num_outputs].scale = output_config->scale;
1075 config->outputs[config->num_outputs].name = strdup(output_config->name);
1076 config->num_outputs++;
1077
1078 return 0;
1079}
1080
1081static int
1082load_x11_backend(struct weston_compositor *c, char const * backend,
1083 int *argc, char **argv, struct weston_config *wc)
1084{
1085 struct weston_x11_backend_output_config default_output;
1086 struct weston_x11_backend_config config = {{ 0, }};
1087 struct weston_config_section *section;
1088 int ret = 0;
1089 int option_width = 0;
1090 int option_height = 0;
1091 int option_scale = 0;
1092 int option_count = 1;
1093 int output_count = 0;
1094 char const *section_name;
1095 int i;
1096 uint32_t j;
1097
1098 const struct weston_option options[] = {
1099 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1100 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1101 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
1102 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
1103 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1104 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
1105 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1106 };
1107
1108 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1109
1110 section = NULL;
1111 while (weston_config_next_section(wc, &section, &section_name)) {
1112 struct weston_x11_backend_output_config current_output = { 0, };
1113 char *t;
1114 char *mode;
1115
1116 if (strcmp(section_name, "output") != 0) {
1117 continue;
1118 }
1119
1120 weston_config_section_get_string(section, "name", &current_output.name, NULL);
1121 if (current_output.name == NULL || current_output.name[0] != 'X') {
1122 free(current_output.name);
1123 continue;
1124 }
1125
1126 weston_config_section_get_string(section, "mode", &mode, "1024x600");
1127 if (sscanf(mode, "%dx%d", &current_output.width,
1128 &current_output.height) != 2) {
1129 weston_log("Invalid mode \"%s\" for output %s\n",
1130 mode, current_output.name);
1131 current_output.width = 1024;
1132 current_output.height = 600;
1133 }
1134 free(mode);
1135 if (current_output.width < 1)
1136 current_output.width = 1024;
1137 if (current_output.height < 1)
1138 current_output.height = 600;
1139 if (option_width)
1140 current_output.width = option_width;
1141 if (option_height)
1142 current_output.height = option_height;
1143
1144 weston_config_section_get_int(section, "scale", &current_output.scale, 1);
1145 if (option_scale)
1146 current_output.scale = option_scale;
1147
1148 weston_config_section_get_string(section,
1149 "transform", &t, "normal");
1150 if (weston_parse_transform(t, &current_output.transform) < 0)
1151 weston_log("Invalid transform \"%s\" for output %s\n",
1152 t, current_output.name);
1153 free(t);
1154
1155 if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
1156 ret = -1;
1157 goto out;
1158 }
1159
1160 output_count++;
Bryce Harringtonaa258982016-05-03 01:34:23 -07001161 if (output_count >= option_count)
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001162 break;
1163 }
1164
1165 default_output.name = NULL;
1166 default_output.width = option_width ? option_width : 1024;
1167 default_output.height = option_height ? option_height : 600;
1168 default_output.scale = option_scale ? option_scale : 1;
1169 default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1170
1171 for (i = output_count; i < option_count; i++) {
1172 if (asprintf(&default_output.name, "screen%d", i) < 0) {
1173 ret = -1;
1174 goto out;
1175 }
1176
1177 if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
1178 ret = -1;
1179 free(default_output.name);
1180 goto out;
1181 }
1182 free(default_output.name);
1183 }
1184
1185 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
1186 config.base.struct_size = sizeof(struct weston_x11_backend_config);
1187
1188 /* load the actual backend and configure it */
1189 ret = load_backend_new(c, backend, &config.base);
1190
1191out:
1192 for (j = 0; j < config.num_outputs; ++j)
1193 free(config.outputs[j].name);
1194 free(config.outputs);
1195
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001196 return ret;
1197}
1198
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001199static void
1200weston_wayland_output_config_init(struct weston_wayland_backend_output_config *output_config,
1201 struct weston_config_section *config_section,
1202 int option_width, int option_height,
1203 int option_scale)
1204{
1205 char *mode, *t, *str;
1206 unsigned int slen;
1207
1208 weston_config_section_get_string(config_section, "name", &output_config->name,
1209 NULL);
1210 if (output_config->name) {
1211 slen = strlen(output_config->name);
1212 slen += strlen(WINDOW_TITLE " - ");
1213 str = malloc(slen + 1);
1214 if (str)
1215 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1216 output_config->name);
1217 free(output_config->name);
1218 output_config->name = str;
1219 }
1220 if (!output_config->name)
1221 output_config->name = strdup(WINDOW_TITLE);
1222
1223 weston_config_section_get_string(config_section,
1224 "mode", &mode, "1024x600");
1225 if (sscanf(mode, "%dx%d", &output_config->width, &output_config->height) != 2) {
1226 weston_log("Invalid mode \"%s\" for output %s\n",
1227 mode, output_config->name);
1228 output_config->width = 1024;
1229 output_config->height = 640;
1230 }
1231 free(mode);
1232
1233 if (option_width)
1234 output_config->width = option_width;
1235 if (option_height)
1236 output_config->height = option_height;
1237
1238 weston_config_section_get_int(config_section, "scale", &output_config->scale, 1);
1239
1240 if (option_scale)
1241 output_config->scale = option_scale;
1242
1243 weston_config_section_get_string(config_section,
1244 "transform", &t, "normal");
1245 if (weston_parse_transform(t, &output_config->transform) < 0)
1246 weston_log("Invalid transform \"%s\" for output %s\n",
1247 t, output_config->name);
1248 free(t);
1249
1250}
1251
1252static void
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001253weston_wayland_backend_config_release(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001254{
1255 int i;
1256
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001257 for (i = 0; i < config->num_outputs; ++i) {
1258 free(config->outputs[i].name);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001259 }
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001260 free(config->cursor_theme);
1261 free(config->display_name);
1262 free(config->outputs);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001263}
1264
1265/*
1266 * Append a new output struct at the end of new_config.outputs and return a
1267 * pointer to the newly allocated structure or NULL if fail. The allocated
1268 * structure is NOT cleared nor set to default values.
1269 */
1270static struct weston_wayland_backend_output_config *
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001271weston_wayland_backend_config_add_new_output(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001272{
1273 struct weston_wayland_backend_output_config *outputs;
1274 const size_t element_size = sizeof(struct weston_wayland_backend_output_config);
1275
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001276 outputs = realloc(config->outputs,
1277 (config->num_outputs + 1) * element_size);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001278 if (!outputs)
1279 return NULL;
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001280 config->num_outputs += 1;
1281 config->outputs = outputs;
1282 return &(config->outputs[config->num_outputs - 1]);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001283}
1284
1285static int
1286load_wayland_backend_config(struct weston_compositor *compositor, int *argc,
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001287 char *argv[], struct weston_config *wc,
Benoit Gschwind55a22882016-05-10 22:47:51 +02001288 struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001289{
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001290 struct weston_config_section *section;
1291 struct weston_wayland_backend_output_config *oc;
1292 int count, width, height, scale;
1293 const char *section_name;
1294 char *name;
1295
1296 const struct weston_option wayland_options[] = {
1297 { WESTON_OPTION_INTEGER, "width", 0, &width },
1298 { WESTON_OPTION_INTEGER, "height", 0, &height },
1299 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001300 { WESTON_OPTION_STRING, "display", 0, &config->display_name },
1301 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config->use_pixman },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001302 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001303 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config->fullscreen },
1304 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &config->sprawl },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001305 };
1306
1307 width = 0;
1308 height = 0;
1309 scale = 0;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001310 config->display_name = NULL;
1311 config->use_pixman = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001312 count = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001313 config->fullscreen = 0;
1314 config->sprawl = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001315 parse_options(wayland_options,
1316 ARRAY_LENGTH(wayland_options), argc, argv);
1317
Benoit Gschwind55a22882016-05-10 22:47:51 +02001318 config->cursor_size = 32;
1319 config->cursor_theme = NULL;
1320 config->base.struct_size = sizeof(struct weston_wayland_backend_config);
1321 config->base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001322
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001323 section = weston_config_get_section(wc, "shell", NULL, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001324 weston_config_section_get_string(section, "cursor-theme",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001325 &config->cursor_theme, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001326 weston_config_section_get_int(section, "cursor-size",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001327 &config->cursor_size, 32);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001328
Benoit Gschwind55a22882016-05-10 22:47:51 +02001329 if (config->sprawl) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001330 /* do nothing, everything is already set */
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001331 return 0;
1332 }
1333
Benoit Gschwind55a22882016-05-10 22:47:51 +02001334 if (config->fullscreen) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001335 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001336 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001337 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001338
1339 oc->width = width;
1340 oc->height = height;
1341 oc->name = NULL;
1342 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1343 oc->scale = 1;
1344
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001345 return 0;
1346 }
1347
1348 section = NULL;
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001349 while (weston_config_next_section(wc, &section, &section_name)) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001350 if (!section_name || strcmp(section_name, "output") != 0)
1351 continue;
1352 weston_config_section_get_string(section, "name", &name, NULL);
1353 if (name == NULL)
1354 continue;
1355
1356 if (name[0] != 'W' || name[1] != 'L') {
1357 free(name);
1358 continue;
1359 }
1360 free(name);
1361
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001362 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001363 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001364 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001365
1366 weston_wayland_output_config_init(oc, section, width,
1367 height, scale);
1368 --count;
1369 }
1370
1371 if (!width)
1372 width = 1024;
1373 if (!height)
1374 height = 640;
1375 if (!scale)
1376 scale = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001377
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001378 while (count > 0) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001379 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001380 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001381 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001382
1383 oc->width = width;
1384 oc->height = height;
1385 oc->name = NULL;
1386 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1387 oc->scale = scale;
1388
1389 --count;
1390 }
1391
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001392 return 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001393}
1394
1395static int
1396load_wayland_backend(struct weston_compositor *c, char const * backend,
1397 int *argc, char **argv, struct weston_config *wc)
1398{
1399 struct weston_wayland_backend_config config = {{ 0, }};
1400 int ret = 0;
1401
1402 ret = load_wayland_backend_config(c, argc, argv, wc, &config);
1403 if(ret < 0) {
Benoit Gschwind53753842016-05-10 22:47:57 +02001404 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001405 return ret;
1406 }
1407
1408 /* load the actual wayland backend and configure it */
1409 ret = load_backend_new(c, backend, &config.base);
Benoit Gschwind68d6a6c2016-05-10 22:47:53 +02001410 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001411 return ret;
1412}
1413
1414
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001415static int
Giulio Camuffo43008c72015-10-17 19:24:15 +03001416load_backend(struct weston_compositor *compositor, const char *backend,
1417 int *argc, char **argv, struct weston_config *config)
1418{
Benoit Gschwind3c530942016-04-15 20:28:32 -07001419 if (strstr(backend, "headless-backend.so"))
1420 return load_headless_backend(compositor, backend, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001421 else if (strstr(backend, "rdp-backend.so"))
1422 return load_rdp_backend(compositor, backend, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001423 else if (strstr(backend, "fbdev-backend.so"))
1424 return load_fbdev_backend(compositor, backend, argc, argv, config);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001425 else if (strstr(backend, "drm-backend.so"))
1426 return load_drm_backend(compositor, backend, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001427 else if (strstr(backend, "x11-backend.so"))
1428 return load_x11_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001429 else if (strstr(backend, "wayland-backend.so"))
1430 return load_wayland_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001431
1432 return load_backend_old(compositor, backend, argc, argv, config);
1433}
1434
Giulio Camuffobab996e2014-10-12 00:24:25 +03001435int main(int argc, char *argv[])
1436{
1437 int ret = EXIT_FAILURE;
1438 struct wl_display *display;
1439 struct weston_compositor *ec;
1440 struct wl_event_source *signals[4];
1441 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001442 int i, fd;
1443 char *backend = NULL;
1444 char *shell = NULL;
1445 char *modules = NULL;
1446 char *option_modules = NULL;
1447 char *log = NULL;
1448 char *server_socket = NULL, *end;
1449 int32_t idle_time = -1;
1450 int32_t help = 0;
1451 char *socket_name = NULL;
1452 int32_t version = 0;
1453 int32_t noconfig = 0;
1454 int32_t numlock_on;
1455 char *config_file = NULL;
1456 struct weston_config *config = NULL;
1457 struct weston_config_section *section;
1458 struct wl_client *primary_client;
1459 struct wl_listener primary_client_destroyed;
1460 struct weston_seat *seat;
1461
1462 const struct weston_option core_options[] = {
1463 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1464 { WESTON_OPTION_STRING, "shell", 0, &shell },
1465 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1466 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
1467 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1468 { WESTON_OPTION_STRING, "log", 0, &log },
1469 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1470 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1471 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1472 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1473 };
1474
1475 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1476
1477 if (help)
1478 usage(EXIT_SUCCESS);
1479
1480 if (version) {
1481 printf(PACKAGE_STRING "\n");
1482 return EXIT_SUCCESS;
1483 }
1484
1485 weston_log_file_open(log);
1486
1487 weston_log("%s\n"
1488 STAMP_SPACE "%s\n"
1489 STAMP_SPACE "Bug reports to: %s\n"
1490 STAMP_SPACE "Build: %s\n",
1491 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1492 BUILD_ID);
1493 log_uname();
1494
1495 verify_xdg_runtime_dir();
1496
1497 display = wl_display_create();
1498
1499 loop = wl_display_get_event_loop(display);
1500 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1501 display);
1502 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1503 display);
1504 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1505 display);
1506
1507 wl_list_init(&child_process_list);
1508 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1509 NULL);
1510
1511 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1512 goto out_signals;
1513
1514 if (load_configuration(&config, noconfig, config_file) < 0)
1515 goto out_signals;
1516
1517 section = weston_config_get_section(config, "core", NULL, NULL);
1518
1519 if (!backend) {
1520 weston_config_section_get_string(section, "backend", &backend,
1521 NULL);
1522 if (!backend)
1523 backend = weston_choose_default_backend();
1524 }
1525
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001526 ec = weston_compositor_create(display, config);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001527 if (ec == NULL) {
1528 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001529 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001530 }
1531
Giulio Camuffobab996e2014-10-12 00:24:25 +03001532 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001533 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001534
Giulio Camuffo43008c72015-10-17 19:24:15 +03001535 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001536 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001537 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001538 }
1539
1540 catch_signals();
1541 segv_compositor = ec;
1542
1543 if (idle_time < 0)
1544 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1545 if (idle_time < 0)
1546 idle_time = 300; /* default idle timeout, in seconds */
1547
1548 ec->idle_time = idle_time;
1549 ec->default_pointer_grab = NULL;
1550 ec->exit = handle_exit;
1551
1552 weston_compositor_log_capabilities(ec);
1553
1554 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1555 if (server_socket) {
1556 weston_log("Running with single client\n");
1557 fd = strtol(server_socket, &end, 0);
1558 if (*end != '\0')
1559 fd = -1;
1560 } else {
1561 fd = -1;
1562 }
1563
1564 if (fd != -1) {
1565 primary_client = wl_client_create(display, fd);
1566 if (!primary_client) {
1567 weston_log("fatal: failed to add client: %m\n");
1568 goto out;
1569 }
1570 primary_client_destroyed.notify =
1571 handle_primary_client_destroyed;
1572 wl_client_add_destroy_listener(primary_client,
1573 &primary_client_destroyed);
1574 } else if (weston_create_listening_socket(display, socket_name)) {
1575 goto out;
1576 }
1577
1578 if (!shell)
1579 weston_config_section_get_string(section, "shell", &shell,
1580 "desktop-shell.so");
1581
1582 if (load_modules(ec, shell, &argc, argv) < 0)
1583 goto out;
1584
1585 weston_config_section_get_string(section, "modules", &modules, "");
1586 if (load_modules(ec, modules, &argc, argv) < 0)
1587 goto out;
1588
1589 if (load_modules(ec, option_modules, &argc, argv) < 0)
1590 goto out;
1591
1592 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1593 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1594 if (numlock_on) {
1595 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001596 struct weston_keyboard *keyboard =
1597 weston_seat_get_keyboard(seat);
1598
1599 if (keyboard)
1600 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001601 WESTON_NUM_LOCK,
1602 WESTON_NUM_LOCK);
1603 }
1604 }
1605
1606 for (i = 1; i < argc; i++)
1607 weston_log("fatal: unhandled option: %s\n", argv[i]);
1608 if (argc > 1)
1609 goto out;
1610
1611 weston_compositor_wake(ec);
1612
1613 wl_display_run(display);
1614
1615 /* Allow for setting return exit code after
1616 * wl_display_run returns normally. This is
1617 * useful for devs/testers and automated tests
1618 * that want to indicate failure status to
1619 * testing infrastructure above
1620 */
1621 ret = ec->exit_code;
1622
1623out:
1624 weston_compositor_destroy(ec);
1625
1626out_signals:
1627 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1628 if (signals[i])
1629 wl_event_source_remove(signals[i]);
1630
1631 wl_display_destroy(display);
1632
1633 weston_log_file_close();
1634
1635 if (config)
1636 weston_config_destroy(config);
1637 free(config_file);
1638 free(backend);
1639 free(shell);
1640 free(socket_name);
1641 free(option_modules);
1642 free(log);
1643 free(modules);
1644
1645 return ret;
1646}