blob: 8bf824e50c350a6510445698b7f9c543c37399cb [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 Camuffobe2b11a2016-06-02 21:48:13 +030042#include <sys/time.h>
Giulio Camuffo179fcda2016-06-02 21:48:14 +030043#include <linux/limits.h>
Giulio Camuffobab996e2014-10-12 00:24:25 +030044
45#ifdef HAVE_LIBUNWIND
46#define UNW_LOCAL_ONLY
47#include <libunwind.h>
48#endif
49
Giulio Camuffo179fcda2016-06-02 21:48:14 +030050#include "weston.h"
Giulio Camuffobab996e2014-10-12 00:24:25 +030051#include "compositor.h"
52#include "../shared/os-compatibility.h"
53#include "../shared/helpers.h"
54#include "git-version.h"
55#include "version.h"
Giulio Camuffofba27fb2016-06-02 21:48:10 +030056#include "weston.h"
Giulio Camuffobab996e2014-10-12 00:24:25 +030057
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070058#include "compositor-drm.h"
Benoit Gschwind3c530942016-04-15 20:28:32 -070059#include "compositor-headless.h"
Benoit Gschwindbd573102016-04-22 17:05:26 +020060#include "compositor-rdp.h"
Benoit Gschwind934e89a2016-04-27 23:56:42 +020061#include "compositor-fbdev.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070062#include "compositor-x11.h"
Benoit Gschwind3ff10da2016-05-10 22:47:49 +020063#include "compositor-wayland.h"
64
65#define WINDOW_TITLE "Weston Compositor"
Benoit Gschwind3c530942016-04-15 20:28:32 -070066
Giulio Camuffobe2b11a2016-06-02 21:48:13 +030067static FILE *weston_logfile = NULL;
68
69static int cached_tm_mday = -1;
70
71static int weston_log_timestamp(void)
72{
73 struct timeval tv;
74 struct tm *brokendown_time;
75 char string[128];
76
77 gettimeofday(&tv, NULL);
78
79 brokendown_time = localtime(&tv.tv_sec);
80 if (brokendown_time == NULL)
81 return fprintf(weston_logfile, "[(NULL)localtime] ");
82
83 if (brokendown_time->tm_mday != cached_tm_mday) {
84 strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
85 fprintf(weston_logfile, "Date: %s\n", string);
86
87 cached_tm_mday = brokendown_time->tm_mday;
88 }
89
90 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
91
92 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
93}
94
95static void
96custom_handler(const char *fmt, va_list arg)
97{
98 weston_log_timestamp();
99 fprintf(weston_logfile, "libwayland: ");
100 vfprintf(weston_logfile, fmt, arg);
101}
102
103static void
104weston_log_file_open(const char *filename)
105{
106 wl_log_set_handler_server(custom_handler);
107
108 if (filename != NULL) {
109 weston_logfile = fopen(filename, "a");
110 if (weston_logfile)
111 os_fd_set_cloexec(fileno(weston_logfile));
112 }
113
114 if (weston_logfile == NULL)
115 weston_logfile = stderr;
116 else
117 setvbuf(weston_logfile, NULL, _IOLBF, 256);
118}
119
120static void
121weston_log_file_close(void)
122{
123 if ((weston_logfile != stderr) && (weston_logfile != NULL))
124 fclose(weston_logfile);
125 weston_logfile = stderr;
126}
127
128static int
129vlog(const char *fmt, va_list ap)
130{
131 int l;
132
133 l = weston_log_timestamp();
134 l += vfprintf(weston_logfile, fmt, ap);
135
136 return l;
137}
138
139static int
140vlog_continue(const char *fmt, va_list argp)
141{
142 return vfprintf(weston_logfile, fmt, argp);
143}
144
Giulio Camuffobab996e2014-10-12 00:24:25 +0300145static struct wl_list child_process_list;
146static struct weston_compositor *segv_compositor;
147
148static int
149sigchld_handler(int signal_number, void *data)
150{
151 struct weston_process *p;
152 int status;
153 pid_t pid;
154
155 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
156 wl_list_for_each(p, &child_process_list, link) {
157 if (p->pid == pid)
158 break;
159 }
160
161 if (&p->link == &child_process_list) {
162 weston_log("unknown child process exited\n");
163 continue;
164 }
165
166 wl_list_remove(&p->link);
167 p->cleanup(p, status);
168 }
169
170 if (pid < 0 && errno != ECHILD)
171 weston_log("waitpid error %m\n");
172
173 return 1;
174}
175
176#ifdef HAVE_LIBUNWIND
177
178static void
179print_backtrace(void)
180{
181 unw_cursor_t cursor;
182 unw_context_t context;
183 unw_word_t off;
184 unw_proc_info_t pip;
185 int ret, i = 0;
186 char procname[256];
187 const char *filename;
188 Dl_info dlinfo;
189
190 pip.unwind_info = NULL;
191 ret = unw_getcontext(&context);
192 if (ret) {
193 weston_log("unw_getcontext: %d\n", ret);
194 return;
195 }
196
197 ret = unw_init_local(&cursor, &context);
198 if (ret) {
199 weston_log("unw_init_local: %d\n", ret);
200 return;
201 }
202
203 ret = unw_step(&cursor);
204 while (ret > 0) {
205 ret = unw_get_proc_info(&cursor, &pip);
206 if (ret) {
207 weston_log("unw_get_proc_info: %d\n", ret);
208 break;
209 }
210
211 ret = unw_get_proc_name(&cursor, procname, 256, &off);
212 if (ret && ret != -UNW_ENOMEM) {
213 if (ret != -UNW_EUNSPEC)
214 weston_log("unw_get_proc_name: %d\n", ret);
215 procname[0] = '?';
216 procname[1] = 0;
217 }
218
219 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
220 *dlinfo.dli_fname)
221 filename = dlinfo.dli_fname;
222 else
223 filename = "?";
224
225 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
226 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
227
228 ret = unw_step(&cursor);
229 if (ret < 0)
230 weston_log("unw_step: %d\n", ret);
231 }
232}
233
234#else
235
236static void
237print_backtrace(void)
238{
239 void *buffer[32];
240 int i, count;
241 Dl_info info;
242
243 count = backtrace(buffer, ARRAY_LENGTH(buffer));
244 for (i = 0; i < count; i++) {
245 dladdr(buffer[i], &info);
246 weston_log(" [%016lx] %s (%s)\n",
247 (long) buffer[i],
248 info.dli_sname ? info.dli_sname : "--",
249 info.dli_fname);
250 }
251}
252
253#endif
254
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300255static void
256child_client_exec(int sockfd, const char *path)
257{
258 int clientfd;
259 char s[32];
260 sigset_t allsigs;
261
262 /* do not give our signal mask to the new process */
263 sigfillset(&allsigs);
264 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
265
266 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
267 if (seteuid(getuid()) == -1) {
268 weston_log("compositor: failed seteuid\n");
269 return;
270 }
271
272 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
273 * non-CLOEXEC fd to pass through exec. */
274 clientfd = dup(sockfd);
275 if (clientfd == -1) {
276 weston_log("compositor: dup failed: %m\n");
277 return;
278 }
279
280 snprintf(s, sizeof s, "%d", clientfd);
281 setenv("WAYLAND_SOCKET", s, 1);
282
283 if (execl(path, path, NULL) < 0)
284 weston_log("compositor: executing '%s' failed: %m\n",
285 path);
286}
287
288WL_EXPORT struct wl_client *
289weston_client_launch(struct weston_compositor *compositor,
290 struct weston_process *proc,
291 const char *path,
292 weston_process_cleanup_func_t cleanup)
293{
294 int sv[2];
295 pid_t pid;
296 struct wl_client *client;
297
298 weston_log("launching '%s'\n", path);
299
300 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
301 weston_log("weston_client_launch: "
302 "socketpair failed while launching '%s': %m\n",
303 path);
304 return NULL;
305 }
306
307 pid = fork();
308 if (pid == -1) {
309 close(sv[0]);
310 close(sv[1]);
311 weston_log("weston_client_launch: "
312 "fork failed while launching '%s': %m\n", path);
313 return NULL;
314 }
315
316 if (pid == 0) {
317 child_client_exec(sv[1], path);
318 _exit(-1);
319 }
320
321 close(sv[1]);
322
323 client = wl_client_create(compositor->wl_display, sv[0]);
324 if (!client) {
325 close(sv[0]);
326 weston_log("weston_client_launch: "
327 "wl_client_create failed while launching '%s'.\n",
328 path);
329 return NULL;
330 }
331
332 proc->pid = pid;
333 proc->cleanup = cleanup;
334 weston_watch_process(proc);
335
336 return client;
337}
338
Giulio Camuffobab996e2014-10-12 00:24:25 +0300339WL_EXPORT void
340weston_watch_process(struct weston_process *process)
341{
342 wl_list_insert(&child_process_list, &process->link);
343}
344
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300345struct process_info {
346 struct weston_process proc;
347 char *path;
348};
349
350static void
351process_handle_sigchld(struct weston_process *process, int status)
352{
353 struct process_info *pinfo =
354 container_of(process, struct process_info, proc);
355
356 /*
357 * There are no guarantees whether this runs before or after
358 * the wl_client destructor.
359 */
360
361 if (WIFEXITED(status)) {
362 weston_log("%s exited with status %d\n", pinfo->path,
363 WEXITSTATUS(status));
364 } else if (WIFSIGNALED(status)) {
365 weston_log("%s died on signal %d\n", pinfo->path,
366 WTERMSIG(status));
367 } else {
368 weston_log("%s disappeared\n", pinfo->path);
369 }
370
371 free(pinfo->path);
372 free(pinfo);
373}
374
375WL_EXPORT struct wl_client *
376weston_client_start(struct weston_compositor *compositor, const char *path)
377{
378 struct process_info *pinfo;
379 struct wl_client *client;
380
381 pinfo = zalloc(sizeof *pinfo);
382 if (!pinfo)
383 return NULL;
384
385 pinfo->path = strdup(path);
386 if (!pinfo->path)
387 goto out_free;
388
389 client = weston_client_launch(compositor, &pinfo->proc, path,
390 process_handle_sigchld);
391 if (!client)
392 goto out_str;
393
394 return client;
395
396out_str:
397 free(pinfo->path);
398
399out_free:
400 free(pinfo);
401
402 return NULL;
403}
404
Giulio Camuffobab996e2014-10-12 00:24:25 +0300405static void
406log_uname(void)
407{
408 struct utsname usys;
409
410 uname(&usys);
411
412 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
413 usys.version, usys.machine);
414}
415
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300416WL_EXPORT struct weston_config *
417wet_get_config(struct weston_compositor *compositor)
418{
419 return weston_compositor_get_user_data(compositor);
420}
421
Giulio Camuffobab996e2014-10-12 00:24:25 +0300422static const char xdg_error_message[] =
423 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
424
425static const char xdg_wrong_message[] =
426 "fatal: environment variable XDG_RUNTIME_DIR\n"
427 "is set to \"%s\", which is not a directory.\n";
428
429static const char xdg_wrong_mode_message[] =
430 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
431 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
432 "and must be owned by the user (current owner is UID %d).\n";
433
434static const char xdg_detail_message[] =
435 "Refer to your distribution on how to get it, or\n"
436 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
437 "on how to implement it.\n";
438
439static void
440verify_xdg_runtime_dir(void)
441{
442 char *dir = getenv("XDG_RUNTIME_DIR");
443 struct stat s;
444
445 if (!dir) {
446 weston_log(xdg_error_message);
447 weston_log_continue(xdg_detail_message);
448 exit(EXIT_FAILURE);
449 }
450
451 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
452 weston_log(xdg_wrong_message, dir);
453 weston_log_continue(xdg_detail_message);
454 exit(EXIT_FAILURE);
455 }
456
457 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
458 weston_log(xdg_wrong_mode_message,
459 dir, s.st_mode & 0777, s.st_uid);
460 weston_log_continue(xdg_detail_message);
461 }
462}
463
464static int
465usage(int error_code)
466{
467 fprintf(stderr,
468 "Usage: weston [OPTIONS]\n\n"
469 "This is weston version " VERSION ", the Wayland reference compositor.\n"
470 "Weston supports multiple backends, and depending on which backend is in use\n"
471 "different options will be accepted.\n\n"
472
473
474 "Core options:\n\n"
475 " --version\t\tPrint weston version\n"
476 " -B, --backend=MODULE\tBackend module, one of\n"
477#if defined(BUILD_DRM_COMPOSITOR)
478 "\t\t\t\tdrm-backend.so\n"
479#endif
480#if defined(BUILD_FBDEV_COMPOSITOR)
481 "\t\t\t\tfbdev-backend.so\n"
482#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300483#if defined(BUILD_HEADLESS_COMPOSITOR)
484 "\t\t\t\theadless-backend.so\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300485#endif
486#if defined(BUILD_RDP_COMPOSITOR)
487 "\t\t\t\trdp-backend.so\n"
488#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300489#if defined(BUILD_WAYLAND_COMPOSITOR)
490 "\t\t\t\twayland-backend.so\n"
491#endif
492#if defined(BUILD_X11_COMPOSITOR)
493 "\t\t\t\tx11-backend.so\n"
494#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300495 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
496 " -S, --socket=NAME\tName of socket to listen on\n"
497 " -i, --idle-time=SECS\tIdle time in seconds\n"
498 " --modules\t\tLoad the comma-separated list of modules\n"
499 " --log=FILE\t\tLog to the given file\n"
500 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
501 " --no-config\t\tDo not read weston.ini\n"
502 " -h, --help\t\tThis help message\n\n");
503
504#if defined(BUILD_DRM_COMPOSITOR)
505 fprintf(stderr,
506 "Options for drm-backend.so:\n\n"
507 " --connector=ID\tBring up only this connector\n"
508 " --seat=SEAT\t\tThe seat that weston should run on\n"
509 " --tty=TTY\t\tThe tty to use\n"
510 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
511 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
512#endif
513
514#if defined(BUILD_FBDEV_COMPOSITOR)
515 fprintf(stderr,
516 "Options for fbdev-backend.so:\n\n"
517 " --tty=TTY\t\tThe tty to use\n"
518 " --device=DEVICE\tThe framebuffer device to use\n"
519 " --use-gl\t\tUse the GL renderer\n\n");
520#endif
521
Dawid Gajownik71f57042015-07-31 17:39:00 -0300522#if defined(BUILD_HEADLESS_COMPOSITOR)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300523 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300524 "Options for headless-backend.so:\n\n"
525 " --width=WIDTH\t\tWidth of memory surface\n"
526 " --height=HEIGHT\tHeight of memory surface\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300527 " --transform=TR\tThe output transformation, TR is one of:\n"
528 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Dawid Gajownik71f57042015-07-31 17:39:00 -0300529 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300530#endif
531
532#if defined(BUILD_RDP_COMPOSITOR)
533 fprintf(stderr,
534 "Options for rdp-backend.so:\n\n"
535 " --width=WIDTH\t\tWidth of desktop\n"
536 " --height=HEIGHT\tHeight of desktop\n"
Dawid Gajownikd99a0502015-07-31 14:49:57 -0300537 " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300538 " --address=ADDR\tThe address to bind\n"
539 " --port=PORT\t\tThe port to listen on\n"
540 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
541 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
542 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
543 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
544 "\n");
545#endif
546
Dawid Gajownik71f57042015-07-31 17:39:00 -0300547#if defined(BUILD_WAYLAND_COMPOSITOR)
548 fprintf(stderr,
549 "Options for wayland-backend.so:\n\n"
550 " --width=WIDTH\t\tWidth of Wayland surface\n"
551 " --height=HEIGHT\tHeight of Wayland surface\n"
552 " --scale=SCALE\t\tScale factor of output\n"
553 " --fullscreen\t\tRun in fullscreen mode\n"
554 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
555 " --output-count=COUNT\tCreate multiple outputs\n"
556 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
557 " --display=DISPLAY\tWayland display to connect to\n\n");
558#endif
559
560#if defined(BUILD_X11_COMPOSITOR)
561 fprintf(stderr,
562 "Options for x11-backend.so:\n\n"
563 " --width=WIDTH\t\tWidth of X window\n"
564 " --height=HEIGHT\tHeight of X window\n"
565 " --scale=SCALE\t\tScale factor of output\n"
566 " --fullscreen\t\tRun in fullscreen mode\n"
567 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
568 " --output-count=COUNT\tCreate multiple outputs\n"
569 " --no-input\t\tDont create input devices\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300570#endif
571
572 exit(error_code);
573}
574
575static int on_term_signal(int signal_number, void *data)
576{
577 struct wl_display *display = data;
578
579 weston_log("caught signal %d\n", signal_number);
580 wl_display_terminate(display);
581
582 return 1;
583}
584
585static void
586on_caught_signal(int s, siginfo_t *siginfo, void *context)
587{
588 /* This signal handler will do a best-effort backtrace, and
589 * then call the backend restore function, which will switch
590 * back to the vt we launched from or ungrab X etc and then
591 * raise SIGTRAP. If we run weston under gdb from X or a
592 * different vt, and tell gdb "handle *s* nostop", this
593 * will allow weston to switch back to gdb on crash and then
594 * gdb will catch the crash with SIGTRAP.*/
595
596 weston_log("caught signal: %d\n", s);
597
598 print_backtrace();
599
600 segv_compositor->backend->restore(segv_compositor);
601
602 raise(SIGTRAP);
603}
604
605static void
606catch_signals(void)
607{
608 struct sigaction action;
609
610 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
611 action.sa_sigaction = on_caught_signal;
612 sigemptyset(&action.sa_mask);
613 sigaction(SIGSEGV, &action, NULL);
614 sigaction(SIGABRT, &action, NULL);
615}
616
617static const char *
618clock_name(clockid_t clk_id)
619{
620 static const char *names[] = {
621 [CLOCK_REALTIME] = "CLOCK_REALTIME",
622 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
623 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
624 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
625 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500626#ifdef CLOCK_BOOTTIME
Giulio Camuffobab996e2014-10-12 00:24:25 +0300627 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500628#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300629 };
630
631 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
632 return "unknown";
633
634 return names[clk_id];
635}
636
637static const struct {
638 uint32_t bit; /* enum weston_capability */
639 const char *desc;
640} capability_strings[] = {
641 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
642 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
643};
644
645static void
646weston_compositor_log_capabilities(struct weston_compositor *compositor)
647{
648 unsigned i;
649 int yes;
650
651 weston_log("Compositor capabilities:\n");
652 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
653 yes = compositor->capabilities & capability_strings[i].bit;
654 weston_log_continue(STAMP_SPACE "%s %s\n",
655 capability_strings[i].desc,
656 yes ? "yes" : "no");
657 }
658
659 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
660 clock_name(compositor->presentation_clock),
661 compositor->presentation_clock);
662}
663
664static void
665handle_primary_client_destroyed(struct wl_listener *listener, void *data)
666{
667 struct wl_client *client = data;
668
669 weston_log("Primary client died. Closing...\n");
670
671 wl_display_terminate(wl_client_get_display(client));
672}
673
674static int
675weston_create_listening_socket(struct wl_display *display, const char *socket_name)
676{
677 if (socket_name) {
678 if (wl_display_add_socket(display, socket_name)) {
679 weston_log("fatal: failed to add socket: %m\n");
680 return -1;
681 }
682 } else {
683 socket_name = wl_display_add_socket_auto(display);
684 if (!socket_name) {
685 weston_log("fatal: failed to add socket: %m\n");
686 return -1;
687 }
688 }
689
690 setenv("WAYLAND_DISPLAY", socket_name, 1);
691
692 return 0;
693}
694
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300695WL_EXPORT void *
696wet_load_module(const char *name, const char *entrypoint)
697{
698 const char *builddir = getenv("WESTON_BUILD_DIR");
699 char path[PATH_MAX];
700 void *module, *init;
701
702 if (name == NULL)
703 return NULL;
704
705 if (name[0] != '/') {
706 if (builddir)
707 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
708 else
709 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
710 } else {
711 snprintf(path, sizeof path, "%s", name);
712 }
713
714 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
715 if (module) {
716 weston_log("Module '%s' already loaded\n", path);
717 dlclose(module);
718 return NULL;
719 }
720
721 weston_log("Loading module '%s'\n", path);
722 module = dlopen(path, RTLD_NOW);
723 if (!module) {
724 weston_log("Failed to load module: %s\n", dlerror());
725 return NULL;
726 }
727
728 init = dlsym(module, entrypoint);
729 if (!init) {
730 weston_log("Failed to lookup init function: %s\n", dlerror());
731 dlclose(module);
732 return NULL;
733 }
734
735 return init;
736}
737
Giulio Camuffobab996e2014-10-12 00:24:25 +0300738static int
739load_modules(struct weston_compositor *ec, const char *modules,
740 int *argc, char *argv[])
741{
742 const char *p, *end;
743 char buffer[256];
744 int (*module_init)(struct weston_compositor *ec,
745 int *argc, char *argv[]);
746
747 if (modules == NULL)
748 return 0;
749
750 p = modules;
751 while (*p) {
752 end = strchrnul(p, ',');
753 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300754 module_init = wet_load_module(buffer, "module_init");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300755 if (!module_init)
756 return -1;
757 if (module_init(ec, argc, argv) < 0)
758 return -1;
759 p = end;
760 while (*p == ',')
761 p++;
762
763 }
764
765 return 0;
766}
767
768static int
769weston_compositor_init_config(struct weston_compositor *ec,
770 struct weston_config *config)
771{
772 struct xkb_rule_names xkb_names;
773 struct weston_config_section *s;
774 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000775 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300776
777 s = weston_config_get_section(config, "keyboard", NULL, NULL);
778 weston_config_section_get_string(s, "keymap_rules",
779 (char **) &xkb_names.rules, NULL);
780 weston_config_section_get_string(s, "keymap_model",
781 (char **) &xkb_names.model, NULL);
782 weston_config_section_get_string(s, "keymap_layout",
783 (char **) &xkb_names.layout, NULL);
784 weston_config_section_get_string(s, "keymap_variant",
785 (char **) &xkb_names.variant, NULL);
786 weston_config_section_get_string(s, "keymap_options",
787 (char **) &xkb_names.options, NULL);
788
Giulio Camuffo0358af42016-06-02 21:48:08 +0300789 if (weston_compositor_set_xkb_rule_names(ec, &xkb_names) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300790 return -1;
791
792 weston_config_section_get_int(s, "repeat-rate",
793 &ec->kb_repeat_rate, 40);
794 weston_config_section_get_int(s, "repeat-delay",
795 &ec->kb_repeat_delay, 400);
796
Bob Ham91880f12016-01-12 10:21:47 +0000797 weston_config_section_get_bool(s, "vt-switching",
798 &vt_switching, true);
799 ec->vt_switching = vt_switching;
800
Giulio Camuffobab996e2014-10-12 00:24:25 +0300801 s = weston_config_get_section(config, "core", NULL, NULL);
802 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
803 ec->repaint_msec);
804 if (repaint_msec < -10 || repaint_msec > 1000) {
805 weston_log("Invalid repaint_window value in config: %d\n",
806 repaint_msec);
807 } else {
808 ec->repaint_msec = repaint_msec;
809 }
810 weston_log("Output repaint window is %d ms maximum.\n",
811 ec->repaint_msec);
812
813 return 0;
814}
815
816static char *
817weston_choose_default_backend(void)
818{
819 char *backend = NULL;
820
821 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
822 backend = strdup("wayland-backend.so");
823 else if (getenv("DISPLAY"))
824 backend = strdup("x11-backend.so");
825 else
826 backend = strdup(WESTON_NATIVE_BACKEND);
827
828 return backend;
829}
830
831static const struct { const char *name; uint32_t token; } transforms[] = {
832 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
833 { "90", WL_OUTPUT_TRANSFORM_90 },
834 { "180", WL_OUTPUT_TRANSFORM_180 },
835 { "270", WL_OUTPUT_TRANSFORM_270 },
836 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
837 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
838 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
839 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
840};
841
842WL_EXPORT int
843weston_parse_transform(const char *transform, uint32_t *out)
844{
845 unsigned int i;
846
847 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
848 if (strcmp(transforms[i].name, transform) == 0) {
849 *out = transforms[i].token;
850 return 0;
851 }
852
853 *out = WL_OUTPUT_TRANSFORM_NORMAL;
854 return -1;
855}
856
857WL_EXPORT const char *
858weston_transform_to_string(uint32_t output_transform)
859{
860 unsigned int i;
861
862 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
863 if (transforms[i].token == output_transform)
864 return transforms[i].name;
865
866 return "<illegal value>";
867}
868
869static int
870load_configuration(struct weston_config **config, int32_t noconfig,
871 const char *config_file)
872{
873 const char *file = "weston.ini";
874 const char *full_path;
875
876 *config = NULL;
877
878 if (config_file)
879 file = config_file;
880
881 if (noconfig == 0)
882 *config = weston_config_parse(file);
883
884 if (*config) {
885 full_path = weston_config_get_full_path(*config);
886
887 weston_log("Using config file '%s'\n", full_path);
888 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
889
890 return 0;
891 }
892
893 if (config_file && noconfig == 0) {
894 weston_log("fatal: error opening or reading config file"
895 " '%s'.\n", config_file);
896
897 return -1;
898 }
899
900 weston_log("Starting with no config file.\n");
901 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
902
903 return 0;
904}
905
906static void
907handle_exit(struct weston_compositor *c)
908{
909 wl_display_terminate(c->wl_display);
910}
911
Bryce Harrington98ab08b2016-04-15 20:28:36 -0700912/** Main module call-point for backends.
913 *
914 * All backends should use this routine to access their init routine.
915 * Backends may subclass weston_backend_config to add their own
916 * configuration data, setting the major/minor version in config_base
917 * accordingly.
918 *
919 * The config_base object should be treated as temporary, and any data
920 * copied out of it by backend_init before returning. The load_backend_new
921 * callers may then free the config_base object.
922 *
923 * NOTE: This is a temporary function intended to eventually be replaced
924 * by weston_compositor_load_backend().
925 */
Bryce Harrington6ca25b32016-04-15 20:28:27 -0700926static int
927load_backend_new(struct weston_compositor *compositor, const char *backend,
928 struct weston_backend_config *config_base)
929{
930 int (*backend_init)(struct weston_compositor *c,
931 int *argc, char *argv[],
932 struct weston_config *config,
933 struct weston_backend_config *config_base);
934
935 backend_init = weston_load_module(backend, "backend_init");
936 if (!backend_init)
937 return -1;
938
939 return backend_init(compositor, NULL, NULL, NULL, config_base);
940}
941
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700942static enum weston_drm_backend_output_mode
943drm_configure_output(struct weston_compositor *c,
944 bool use_current_mode,
945 const char *name,
946 struct weston_drm_backend_output_config *config)
947{
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300948 struct weston_config *wc = wet_get_config(c);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700949 struct weston_config_section *section;
950 char *s;
951 int scale;
952 enum weston_drm_backend_output_mode mode =
953 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
954
955 section = weston_config_get_section(wc, "output", "name", name);
956 weston_config_section_get_string(section, "mode", &s, "preferred");
957 if (strcmp(s, "off") == 0) {
958 free(s);
959 return WESTON_DRM_BACKEND_OUTPUT_OFF;
960 }
961
962 if (use_current_mode || strcmp(s, "current") == 0) {
963 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
964 } else if (strcmp(s, "preferred") != 0) {
965 config->modeline = s;
966 s = NULL;
967 }
968 free(s);
969
970 weston_config_section_get_int(section, "scale", &scale, 1);
971 config->base.scale = scale >= 1 ? scale : 1;
972 weston_config_section_get_string(section, "transform", &s, "normal");
973 if (weston_parse_transform(s, &config->base.transform) < 0)
974 weston_log("Invalid transform \"%s\" for output %s\n",
975 s, name);
976 free(s);
977
978 weston_config_section_get_string(section,
979 "gbm-format", &config->gbm_format, NULL);
980 weston_config_section_get_string(section, "seat", &config->seat, "");
981 return mode;
982}
983
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300984static void
985configure_input_device(struct weston_compositor *compositor,
986 struct libinput_device *device)
987{
988 struct weston_config_section *s;
989 struct weston_config *config = wet_get_config(compositor);
990 int enable_tap;
991 int enable_tap_default;
992
993 s = weston_config_get_section(config,
994 "libinput", NULL, NULL);
995
996 if (libinput_device_config_tap_get_finger_count(device) > 0) {
997 enable_tap_default =
998 libinput_device_config_tap_get_default_enabled(
999 device);
1000 weston_config_section_get_bool(s, "enable_tap",
1001 &enable_tap,
1002 enable_tap_default);
1003 libinput_device_config_tap_set_enabled(device,
1004 enable_tap);
1005 }
1006}
1007
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001008static int
1009load_drm_backend(struct weston_compositor *c, const char *backend,
1010 int *argc, char **argv, struct weston_config *wc)
1011{
1012 struct weston_drm_backend_config config = {{ 0, }};
1013 struct weston_config_section *section;
1014 int ret = 0;
1015
1016 const struct weston_option options[] = {
1017 { WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
1018 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
1019 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1020 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
1021 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1022 };
1023
1024 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1025
1026 section = weston_config_get_section(wc, "core", NULL, NULL);
1027 weston_config_section_get_string(section,
1028 "gbm-format", &config.gbm_format,
1029 NULL);
1030
1031 config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
1032 config.base.struct_size = sizeof(struct weston_drm_backend_config);
1033 config.configure_output = drm_configure_output;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001034 config.configure_device = configure_input_device;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001035
1036 ret = load_backend_new(c, backend, &config.base);
1037
1038 free(config.gbm_format);
1039 free(config.seat_id);
1040
1041 return ret;
1042}
1043
Giulio Camuffo43008c72015-10-17 19:24:15 +03001044static int
Benoit Gschwind3c530942016-04-15 20:28:32 -07001045load_headless_backend(struct weston_compositor *c, char const * backend,
1046 int *argc, char **argv, struct weston_config *wc)
1047{
1048 struct weston_headless_backend_config config = {{ 0, }};
1049 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001050 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001051
1052 config.width = 1024;
1053 config.height = 640;
1054
1055 const struct weston_option options[] = {
1056 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1057 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1058 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1059 { WESTON_OPTION_STRING, "transform", 0, &transform },
1060 };
1061
1062 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1063
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001064 config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1065 if (transform) {
1066 if (weston_parse_transform(transform, &config.transform) < 0)
1067 weston_log("Invalid transform \"%s\"\n", transform);
1068 free(transform);
1069 }
Benoit Gschwind3c530942016-04-15 20:28:32 -07001070
1071 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1072 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1073
1074 /* load the actual wayland backend and configure it */
1075 ret = load_backend_new(c, backend, &config.base);
1076
1077 return ret;
1078}
1079
Benoit Gschwindbd573102016-04-22 17:05:26 +02001080static void
1081weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
1082{
1083 config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
1084 config->base.struct_size = sizeof(struct weston_rdp_backend_config);
1085
1086 config->width = 640;
1087 config->height = 480;
1088 config->bind_address = NULL;
1089 config->port = 3389;
1090 config->rdp_key = NULL;
1091 config->server_cert = NULL;
1092 config->server_key = NULL;
1093 config->env_socket = 0;
1094 config->no_clients_resize = 0;
1095}
1096
1097static int
1098load_rdp_backend(struct weston_compositor *c, char const * backend,
1099 int *argc, char *argv[], struct weston_config *wc)
1100{
1101 struct weston_rdp_backend_config config = {{ 0, }};
1102 int ret = 0;
1103
1104 weston_rdp_backend_config_init(&config);
1105
1106 const struct weston_option rdp_options[] = {
1107 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1108 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1109 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1110 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
1111 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1112 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1113 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
1114 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
1115 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
1116 };
1117
1118 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1119
1120 ret = load_backend_new(c, backend, &config.base);
1121
1122 free(config.bind_address);
1123 free(config.rdp_key);
1124 free(config.server_cert);
1125 free(config.server_key);
1126 return ret;
1127}
1128
Benoit Gschwind3c530942016-04-15 20:28:32 -07001129static int
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001130load_fbdev_backend(struct weston_compositor *c, char const * backend,
1131 int *argc, char **argv, struct weston_config *wc)
1132{
1133 struct weston_fbdev_backend_config config = {{ 0, }};
1134 struct weston_config_section *section;
1135 char *s = NULL;
1136 int ret = 0;
1137
1138 const struct weston_option fbdev_options[] = {
1139 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1140 { WESTON_OPTION_STRING, "device", 0, &config.device },
1141 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &config.use_gl },
1142 };
1143
1144 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
1145
1146 if (!config.device)
1147 config.device = strdup("/dev/fb0");
1148
1149 section = weston_config_get_section(wc, "output", "name", "fbdev");
1150 weston_config_section_get_string(section, "transform", &s, "normal");
1151 if (weston_parse_transform(s, &config.output_transform) < 0)
1152 weston_log("Invalid transform \"%s\" for output fbdev\n", s);
1153 free(s);
1154
1155 config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
1156 config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001157 config.configure_device = configure_input_device;
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001158
1159 /* load the actual wayland backend and configure it */
1160 ret = load_backend_new(c, backend, &config.base);
1161
1162 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001163
1164 return ret;
1165}
1166
1167static int
1168weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
1169 struct weston_x11_backend_output_config *output_config) {
1170 struct weston_x11_backend_output_config *new_outputs;
1171
1172 new_outputs = realloc(config->outputs, (config->num_outputs+1) *
1173 sizeof(struct weston_x11_backend_output_config));
1174 if (new_outputs == NULL)
1175 return -1;
1176
1177 config->outputs = new_outputs;
1178 config->outputs[config->num_outputs].width = output_config->width;
1179 config->outputs[config->num_outputs].height = output_config->height;
1180 config->outputs[config->num_outputs].transform = output_config->transform;
1181 config->outputs[config->num_outputs].scale = output_config->scale;
1182 config->outputs[config->num_outputs].name = strdup(output_config->name);
1183 config->num_outputs++;
1184
1185 return 0;
1186}
1187
1188static int
1189load_x11_backend(struct weston_compositor *c, char const * backend,
1190 int *argc, char **argv, struct weston_config *wc)
1191{
1192 struct weston_x11_backend_output_config default_output;
1193 struct weston_x11_backend_config config = {{ 0, }};
1194 struct weston_config_section *section;
1195 int ret = 0;
1196 int option_width = 0;
1197 int option_height = 0;
1198 int option_scale = 0;
1199 int option_count = 1;
1200 int output_count = 0;
1201 char const *section_name;
1202 int i;
1203 uint32_t j;
1204
1205 const struct weston_option options[] = {
1206 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1207 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1208 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
1209 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
1210 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1211 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
1212 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1213 };
1214
1215 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1216
1217 section = NULL;
1218 while (weston_config_next_section(wc, &section, &section_name)) {
1219 struct weston_x11_backend_output_config current_output = { 0, };
1220 char *t;
1221 char *mode;
1222
1223 if (strcmp(section_name, "output") != 0) {
1224 continue;
1225 }
1226
1227 weston_config_section_get_string(section, "name", &current_output.name, NULL);
1228 if (current_output.name == NULL || current_output.name[0] != 'X') {
1229 free(current_output.name);
1230 continue;
1231 }
1232
1233 weston_config_section_get_string(section, "mode", &mode, "1024x600");
1234 if (sscanf(mode, "%dx%d", &current_output.width,
1235 &current_output.height) != 2) {
1236 weston_log("Invalid mode \"%s\" for output %s\n",
1237 mode, current_output.name);
1238 current_output.width = 1024;
1239 current_output.height = 600;
1240 }
1241 free(mode);
1242 if (current_output.width < 1)
1243 current_output.width = 1024;
1244 if (current_output.height < 1)
1245 current_output.height = 600;
1246 if (option_width)
1247 current_output.width = option_width;
1248 if (option_height)
1249 current_output.height = option_height;
1250
1251 weston_config_section_get_int(section, "scale", &current_output.scale, 1);
1252 if (option_scale)
1253 current_output.scale = option_scale;
1254
1255 weston_config_section_get_string(section,
1256 "transform", &t, "normal");
1257 if (weston_parse_transform(t, &current_output.transform) < 0)
1258 weston_log("Invalid transform \"%s\" for output %s\n",
1259 t, current_output.name);
1260 free(t);
1261
1262 if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
1263 ret = -1;
1264 goto out;
1265 }
1266
1267 output_count++;
Bryce Harringtonaa258982016-05-03 01:34:23 -07001268 if (output_count >= option_count)
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001269 break;
1270 }
1271
1272 default_output.name = NULL;
1273 default_output.width = option_width ? option_width : 1024;
1274 default_output.height = option_height ? option_height : 600;
1275 default_output.scale = option_scale ? option_scale : 1;
1276 default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1277
1278 for (i = output_count; i < option_count; i++) {
1279 if (asprintf(&default_output.name, "screen%d", i) < 0) {
1280 ret = -1;
1281 goto out;
1282 }
1283
1284 if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
1285 ret = -1;
1286 free(default_output.name);
1287 goto out;
1288 }
1289 free(default_output.name);
1290 }
1291
1292 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
1293 config.base.struct_size = sizeof(struct weston_x11_backend_config);
1294
1295 /* load the actual backend and configure it */
1296 ret = load_backend_new(c, backend, &config.base);
1297
1298out:
1299 for (j = 0; j < config.num_outputs; ++j)
1300 free(config.outputs[j].name);
1301 free(config.outputs);
1302
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001303 return ret;
1304}
1305
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001306static void
1307weston_wayland_output_config_init(struct weston_wayland_backend_output_config *output_config,
1308 struct weston_config_section *config_section,
1309 int option_width, int option_height,
1310 int option_scale)
1311{
1312 char *mode, *t, *str;
1313 unsigned int slen;
1314
1315 weston_config_section_get_string(config_section, "name", &output_config->name,
1316 NULL);
1317 if (output_config->name) {
1318 slen = strlen(output_config->name);
1319 slen += strlen(WINDOW_TITLE " - ");
1320 str = malloc(slen + 1);
1321 if (str)
1322 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1323 output_config->name);
1324 free(output_config->name);
1325 output_config->name = str;
1326 }
1327 if (!output_config->name)
1328 output_config->name = strdup(WINDOW_TITLE);
1329
1330 weston_config_section_get_string(config_section,
1331 "mode", &mode, "1024x600");
1332 if (sscanf(mode, "%dx%d", &output_config->width, &output_config->height) != 2) {
1333 weston_log("Invalid mode \"%s\" for output %s\n",
1334 mode, output_config->name);
1335 output_config->width = 1024;
1336 output_config->height = 640;
1337 }
1338 free(mode);
1339
1340 if (option_width)
1341 output_config->width = option_width;
1342 if (option_height)
1343 output_config->height = option_height;
1344
1345 weston_config_section_get_int(config_section, "scale", &output_config->scale, 1);
1346
1347 if (option_scale)
1348 output_config->scale = option_scale;
1349
1350 weston_config_section_get_string(config_section,
1351 "transform", &t, "normal");
1352 if (weston_parse_transform(t, &output_config->transform) < 0)
1353 weston_log("Invalid transform \"%s\" for output %s\n",
1354 t, output_config->name);
1355 free(t);
1356
1357}
1358
1359static void
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001360weston_wayland_backend_config_release(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001361{
1362 int i;
1363
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001364 for (i = 0; i < config->num_outputs; ++i) {
1365 free(config->outputs[i].name);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001366 }
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001367 free(config->cursor_theme);
1368 free(config->display_name);
1369 free(config->outputs);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001370}
1371
1372/*
1373 * Append a new output struct at the end of new_config.outputs and return a
1374 * pointer to the newly allocated structure or NULL if fail. The allocated
1375 * structure is NOT cleared nor set to default values.
1376 */
1377static struct weston_wayland_backend_output_config *
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001378weston_wayland_backend_config_add_new_output(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001379{
1380 struct weston_wayland_backend_output_config *outputs;
1381 const size_t element_size = sizeof(struct weston_wayland_backend_output_config);
1382
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001383 outputs = realloc(config->outputs,
1384 (config->num_outputs + 1) * element_size);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001385 if (!outputs)
1386 return NULL;
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001387 config->num_outputs += 1;
1388 config->outputs = outputs;
1389 return &(config->outputs[config->num_outputs - 1]);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001390}
1391
1392static int
1393load_wayland_backend_config(struct weston_compositor *compositor, int *argc,
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001394 char *argv[], struct weston_config *wc,
Benoit Gschwind55a22882016-05-10 22:47:51 +02001395 struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001396{
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001397 struct weston_config_section *section;
1398 struct weston_wayland_backend_output_config *oc;
1399 int count, width, height, scale;
1400 const char *section_name;
1401 char *name;
1402
1403 const struct weston_option wayland_options[] = {
1404 { WESTON_OPTION_INTEGER, "width", 0, &width },
1405 { WESTON_OPTION_INTEGER, "height", 0, &height },
1406 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001407 { WESTON_OPTION_STRING, "display", 0, &config->display_name },
1408 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config->use_pixman },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001409 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001410 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config->fullscreen },
1411 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &config->sprawl },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001412 };
1413
1414 width = 0;
1415 height = 0;
1416 scale = 0;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001417 config->display_name = NULL;
1418 config->use_pixman = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001419 count = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001420 config->fullscreen = 0;
1421 config->sprawl = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001422 parse_options(wayland_options,
1423 ARRAY_LENGTH(wayland_options), argc, argv);
1424
Benoit Gschwind55a22882016-05-10 22:47:51 +02001425 config->cursor_size = 32;
1426 config->cursor_theme = NULL;
1427 config->base.struct_size = sizeof(struct weston_wayland_backend_config);
1428 config->base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001429
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001430 section = weston_config_get_section(wc, "shell", NULL, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001431 weston_config_section_get_string(section, "cursor-theme",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001432 &config->cursor_theme, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001433 weston_config_section_get_int(section, "cursor-size",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001434 &config->cursor_size, 32);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001435
Benoit Gschwind55a22882016-05-10 22:47:51 +02001436 if (config->sprawl) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001437 /* do nothing, everything is already set */
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001438 return 0;
1439 }
1440
Benoit Gschwind55a22882016-05-10 22:47:51 +02001441 if (config->fullscreen) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001442 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001443 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001444 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001445
1446 oc->width = width;
1447 oc->height = height;
1448 oc->name = NULL;
1449 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1450 oc->scale = 1;
1451
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001452 return 0;
1453 }
1454
1455 section = NULL;
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001456 while (weston_config_next_section(wc, &section, &section_name)) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001457 if (!section_name || strcmp(section_name, "output") != 0)
1458 continue;
1459 weston_config_section_get_string(section, "name", &name, NULL);
1460 if (name == NULL)
1461 continue;
1462
1463 if (name[0] != 'W' || name[1] != 'L') {
1464 free(name);
1465 continue;
1466 }
1467 free(name);
1468
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001469 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001470 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001471 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001472
1473 weston_wayland_output_config_init(oc, section, width,
1474 height, scale);
1475 --count;
1476 }
1477
1478 if (!width)
1479 width = 1024;
1480 if (!height)
1481 height = 640;
1482 if (!scale)
1483 scale = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001484
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001485 while (count > 0) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001486 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001487 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001488 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001489
1490 oc->width = width;
1491 oc->height = height;
1492 oc->name = NULL;
1493 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1494 oc->scale = scale;
1495
1496 --count;
1497 }
1498
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001499 return 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001500}
1501
1502static int
1503load_wayland_backend(struct weston_compositor *c, char const * backend,
1504 int *argc, char **argv, struct weston_config *wc)
1505{
1506 struct weston_wayland_backend_config config = {{ 0, }};
1507 int ret = 0;
1508
1509 ret = load_wayland_backend_config(c, argc, argv, wc, &config);
1510 if(ret < 0) {
Benoit Gschwind53753842016-05-10 22:47:57 +02001511 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001512 return ret;
1513 }
1514
1515 /* load the actual wayland backend and configure it */
1516 ret = load_backend_new(c, backend, &config.base);
Benoit Gschwind68d6a6c2016-05-10 22:47:53 +02001517 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001518 return ret;
1519}
1520
1521
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001522static int
Giulio Camuffo43008c72015-10-17 19:24:15 +03001523load_backend(struct weston_compositor *compositor, const char *backend,
1524 int *argc, char **argv, struct weston_config *config)
1525{
Benoit Gschwind3c530942016-04-15 20:28:32 -07001526 if (strstr(backend, "headless-backend.so"))
1527 return load_headless_backend(compositor, backend, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001528 else if (strstr(backend, "rdp-backend.so"))
1529 return load_rdp_backend(compositor, backend, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001530 else if (strstr(backend, "fbdev-backend.so"))
1531 return load_fbdev_backend(compositor, backend, argc, argv, config);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001532 else if (strstr(backend, "drm-backend.so"))
1533 return load_drm_backend(compositor, backend, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001534 else if (strstr(backend, "x11-backend.so"))
1535 return load_x11_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001536 else if (strstr(backend, "wayland-backend.so"))
1537 return load_wayland_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001538
Pekka Paalanen808b0062016-06-03 13:56:17 +03001539 weston_log("Error: unknown backend \"%s\"\n", backend);
1540 return -1;
Giulio Camuffo43008c72015-10-17 19:24:15 +03001541}
1542
Giulio Camuffobab996e2014-10-12 00:24:25 +03001543int main(int argc, char *argv[])
1544{
1545 int ret = EXIT_FAILURE;
1546 struct wl_display *display;
1547 struct weston_compositor *ec;
1548 struct wl_event_source *signals[4];
1549 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001550 int i, fd;
1551 char *backend = NULL;
1552 char *shell = NULL;
1553 char *modules = NULL;
1554 char *option_modules = NULL;
1555 char *log = NULL;
1556 char *server_socket = NULL, *end;
1557 int32_t idle_time = -1;
1558 int32_t help = 0;
1559 char *socket_name = NULL;
1560 int32_t version = 0;
1561 int32_t noconfig = 0;
1562 int32_t numlock_on;
1563 char *config_file = NULL;
1564 struct weston_config *config = NULL;
1565 struct weston_config_section *section;
1566 struct wl_client *primary_client;
1567 struct wl_listener primary_client_destroyed;
1568 struct weston_seat *seat;
1569
1570 const struct weston_option core_options[] = {
1571 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1572 { WESTON_OPTION_STRING, "shell", 0, &shell },
1573 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1574 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
1575 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1576 { WESTON_OPTION_STRING, "log", 0, &log },
1577 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1578 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1579 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1580 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1581 };
1582
1583 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1584
1585 if (help)
1586 usage(EXIT_SUCCESS);
1587
1588 if (version) {
1589 printf(PACKAGE_STRING "\n");
1590 return EXIT_SUCCESS;
1591 }
1592
Giulio Camuffobe2b11a2016-06-02 21:48:13 +03001593 weston_log_set_handler(vlog, vlog_continue);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001594 weston_log_file_open(log);
1595
1596 weston_log("%s\n"
1597 STAMP_SPACE "%s\n"
1598 STAMP_SPACE "Bug reports to: %s\n"
1599 STAMP_SPACE "Build: %s\n",
1600 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1601 BUILD_ID);
1602 log_uname();
1603
1604 verify_xdg_runtime_dir();
1605
1606 display = wl_display_create();
1607
1608 loop = wl_display_get_event_loop(display);
1609 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1610 display);
1611 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1612 display);
1613 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1614 display);
1615
1616 wl_list_init(&child_process_list);
1617 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1618 NULL);
1619
1620 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1621 goto out_signals;
1622
1623 if (load_configuration(&config, noconfig, config_file) < 0)
1624 goto out_signals;
1625
1626 section = weston_config_get_section(config, "core", NULL, NULL);
1627
1628 if (!backend) {
1629 weston_config_section_get_string(section, "backend", &backend,
1630 NULL);
1631 if (!backend)
1632 backend = weston_choose_default_backend();
1633 }
1634
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001635 ec = weston_compositor_create(display, config);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001636 if (ec == NULL) {
1637 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001638 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001639 }
1640
Giulio Camuffobab996e2014-10-12 00:24:25 +03001641 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001642 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001643
Giulio Camuffo43008c72015-10-17 19:24:15 +03001644 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001645 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001646 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001647 }
1648
1649 catch_signals();
1650 segv_compositor = ec;
1651
1652 if (idle_time < 0)
1653 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1654 if (idle_time < 0)
1655 idle_time = 300; /* default idle timeout, in seconds */
1656
1657 ec->idle_time = idle_time;
1658 ec->default_pointer_grab = NULL;
1659 ec->exit = handle_exit;
1660
1661 weston_compositor_log_capabilities(ec);
1662
1663 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1664 if (server_socket) {
1665 weston_log("Running with single client\n");
1666 fd = strtol(server_socket, &end, 0);
1667 if (*end != '\0')
1668 fd = -1;
1669 } else {
1670 fd = -1;
1671 }
1672
1673 if (fd != -1) {
1674 primary_client = wl_client_create(display, fd);
1675 if (!primary_client) {
1676 weston_log("fatal: failed to add client: %m\n");
1677 goto out;
1678 }
1679 primary_client_destroyed.notify =
1680 handle_primary_client_destroyed;
1681 wl_client_add_destroy_listener(primary_client,
1682 &primary_client_destroyed);
1683 } else if (weston_create_listening_socket(display, socket_name)) {
1684 goto out;
1685 }
1686
1687 if (!shell)
1688 weston_config_section_get_string(section, "shell", &shell,
1689 "desktop-shell.so");
1690
1691 if (load_modules(ec, shell, &argc, argv) < 0)
1692 goto out;
1693
1694 weston_config_section_get_string(section, "modules", &modules, "");
1695 if (load_modules(ec, modules, &argc, argv) < 0)
1696 goto out;
1697
1698 if (load_modules(ec, option_modules, &argc, argv) < 0)
1699 goto out;
1700
1701 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1702 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1703 if (numlock_on) {
1704 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001705 struct weston_keyboard *keyboard =
1706 weston_seat_get_keyboard(seat);
1707
1708 if (keyboard)
1709 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001710 WESTON_NUM_LOCK,
1711 WESTON_NUM_LOCK);
1712 }
1713 }
1714
1715 for (i = 1; i < argc; i++)
1716 weston_log("fatal: unhandled option: %s\n", argv[i]);
1717 if (argc > 1)
1718 goto out;
1719
1720 weston_compositor_wake(ec);
1721
1722 wl_display_run(display);
1723
1724 /* Allow for setting return exit code after
1725 * wl_display_run returns normally. This is
1726 * useful for devs/testers and automated tests
1727 * that want to indicate failure status to
1728 * testing infrastructure above
1729 */
1730 ret = ec->exit_code;
1731
1732out:
1733 weston_compositor_destroy(ec);
1734
1735out_signals:
1736 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1737 if (signals[i])
1738 wl_event_source_remove(signals[i]);
1739
1740 wl_display_destroy(display);
1741
1742 weston_log_file_close();
1743
1744 if (config)
1745 weston_config_destroy(config);
1746 free(config_file);
1747 free(backend);
1748 free(shell);
1749 free(socket_name);
1750 free(option_modules);
1751 free(log);
1752 free(modules);
1753
1754 return ret;
1755}