blob: ec3d799a57bcc8f07d2184531c1cf269c32b7205 [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"
Pekka Paalanene77f8ad2016-06-08 17:39:37 +0300519 "\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300520#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;
Pekka Paalanenbe112d42016-04-18 15:53:38 +0300650 struct timespec res;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300651
652 weston_log("Compositor capabilities:\n");
653 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
654 yes = compositor->capabilities & capability_strings[i].bit;
655 weston_log_continue(STAMP_SPACE "%s %s\n",
656 capability_strings[i].desc,
657 yes ? "yes" : "no");
658 }
659
660 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
661 clock_name(compositor->presentation_clock),
662 compositor->presentation_clock);
Pekka Paalanenbe112d42016-04-18 15:53:38 +0300663
664 if (clock_getres(compositor->presentation_clock, &res) == 0)
665 weston_log_continue(STAMP_SPACE
666 "presentation clock resolution: %d.%09ld s\n",
667 (int)res.tv_sec, res.tv_nsec);
668 else
669 weston_log_continue(STAMP_SPACE
670 "presentation clock resolution: N/A\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300671}
672
673static void
674handle_primary_client_destroyed(struct wl_listener *listener, void *data)
675{
676 struct wl_client *client = data;
677
678 weston_log("Primary client died. Closing...\n");
679
680 wl_display_terminate(wl_client_get_display(client));
681}
682
683static int
684weston_create_listening_socket(struct wl_display *display, const char *socket_name)
685{
686 if (socket_name) {
687 if (wl_display_add_socket(display, socket_name)) {
688 weston_log("fatal: failed to add socket: %m\n");
689 return -1;
690 }
691 } else {
692 socket_name = wl_display_add_socket_auto(display);
693 if (!socket_name) {
694 weston_log("fatal: failed to add socket: %m\n");
695 return -1;
696 }
697 }
698
699 setenv("WAYLAND_DISPLAY", socket_name, 1);
700
701 return 0;
702}
703
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300704WL_EXPORT void *
705wet_load_module(const char *name, const char *entrypoint)
706{
707 const char *builddir = getenv("WESTON_BUILD_DIR");
708 char path[PATH_MAX];
709 void *module, *init;
710
711 if (name == NULL)
712 return NULL;
713
714 if (name[0] != '/') {
715 if (builddir)
716 snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
717 else
718 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
719 } else {
720 snprintf(path, sizeof path, "%s", name);
721 }
722
723 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
724 if (module) {
725 weston_log("Module '%s' already loaded\n", path);
726 dlclose(module);
727 return NULL;
728 }
729
730 weston_log("Loading module '%s'\n", path);
731 module = dlopen(path, RTLD_NOW);
732 if (!module) {
733 weston_log("Failed to load module: %s\n", dlerror());
734 return NULL;
735 }
736
737 init = dlsym(module, entrypoint);
738 if (!init) {
739 weston_log("Failed to lookup init function: %s\n", dlerror());
740 dlclose(module);
741 return NULL;
742 }
743
744 return init;
745}
746
Giulio Camuffobab996e2014-10-12 00:24:25 +0300747static int
748load_modules(struct weston_compositor *ec, const char *modules,
749 int *argc, char *argv[])
750{
751 const char *p, *end;
752 char buffer[256];
753 int (*module_init)(struct weston_compositor *ec,
754 int *argc, char *argv[]);
755
756 if (modules == NULL)
757 return 0;
758
759 p = modules;
760 while (*p) {
761 end = strchrnul(p, ',');
762 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300763 module_init = wet_load_module(buffer, "module_init");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300764 if (!module_init)
765 return -1;
766 if (module_init(ec, argc, argv) < 0)
767 return -1;
768 p = end;
769 while (*p == ',')
770 p++;
771
772 }
773
774 return 0;
775}
776
777static int
778weston_compositor_init_config(struct weston_compositor *ec,
779 struct weston_config *config)
780{
781 struct xkb_rule_names xkb_names;
782 struct weston_config_section *s;
783 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000784 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300785
786 s = weston_config_get_section(config, "keyboard", NULL, NULL);
787 weston_config_section_get_string(s, "keymap_rules",
788 (char **) &xkb_names.rules, NULL);
789 weston_config_section_get_string(s, "keymap_model",
790 (char **) &xkb_names.model, NULL);
791 weston_config_section_get_string(s, "keymap_layout",
792 (char **) &xkb_names.layout, NULL);
793 weston_config_section_get_string(s, "keymap_variant",
794 (char **) &xkb_names.variant, NULL);
795 weston_config_section_get_string(s, "keymap_options",
796 (char **) &xkb_names.options, NULL);
797
Giulio Camuffo0358af42016-06-02 21:48:08 +0300798 if (weston_compositor_set_xkb_rule_names(ec, &xkb_names) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300799 return -1;
800
801 weston_config_section_get_int(s, "repeat-rate",
802 &ec->kb_repeat_rate, 40);
803 weston_config_section_get_int(s, "repeat-delay",
804 &ec->kb_repeat_delay, 400);
805
Bob Ham91880f12016-01-12 10:21:47 +0000806 weston_config_section_get_bool(s, "vt-switching",
807 &vt_switching, true);
808 ec->vt_switching = vt_switching;
809
Giulio Camuffobab996e2014-10-12 00:24:25 +0300810 s = weston_config_get_section(config, "core", NULL, NULL);
811 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
812 ec->repaint_msec);
813 if (repaint_msec < -10 || repaint_msec > 1000) {
814 weston_log("Invalid repaint_window value in config: %d\n",
815 repaint_msec);
816 } else {
817 ec->repaint_msec = repaint_msec;
818 }
819 weston_log("Output repaint window is %d ms maximum.\n",
820 ec->repaint_msec);
821
822 return 0;
823}
824
825static char *
826weston_choose_default_backend(void)
827{
828 char *backend = NULL;
829
830 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
831 backend = strdup("wayland-backend.so");
832 else if (getenv("DISPLAY"))
833 backend = strdup("x11-backend.so");
834 else
835 backend = strdup(WESTON_NATIVE_BACKEND);
836
837 return backend;
838}
839
840static const struct { const char *name; uint32_t token; } transforms[] = {
841 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
842 { "90", WL_OUTPUT_TRANSFORM_90 },
843 { "180", WL_OUTPUT_TRANSFORM_180 },
844 { "270", WL_OUTPUT_TRANSFORM_270 },
845 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
846 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
847 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
848 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
849};
850
851WL_EXPORT int
852weston_parse_transform(const char *transform, uint32_t *out)
853{
854 unsigned int i;
855
856 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
857 if (strcmp(transforms[i].name, transform) == 0) {
858 *out = transforms[i].token;
859 return 0;
860 }
861
862 *out = WL_OUTPUT_TRANSFORM_NORMAL;
863 return -1;
864}
865
866WL_EXPORT const char *
867weston_transform_to_string(uint32_t output_transform)
868{
869 unsigned int i;
870
871 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
872 if (transforms[i].token == output_transform)
873 return transforms[i].name;
874
875 return "<illegal value>";
876}
877
878static int
879load_configuration(struct weston_config **config, int32_t noconfig,
880 const char *config_file)
881{
882 const char *file = "weston.ini";
883 const char *full_path;
884
885 *config = NULL;
886
887 if (config_file)
888 file = config_file;
889
890 if (noconfig == 0)
891 *config = weston_config_parse(file);
892
893 if (*config) {
894 full_path = weston_config_get_full_path(*config);
895
896 weston_log("Using config file '%s'\n", full_path);
897 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
898
899 return 0;
900 }
901
902 if (config_file && noconfig == 0) {
903 weston_log("fatal: error opening or reading config file"
904 " '%s'.\n", config_file);
905
906 return -1;
907 }
908
909 weston_log("Starting with no config file.\n");
910 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
911
912 return 0;
913}
914
915static void
916handle_exit(struct weston_compositor *c)
917{
918 wl_display_terminate(c->wl_display);
919}
920
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700921static enum weston_drm_backend_output_mode
922drm_configure_output(struct weston_compositor *c,
923 bool use_current_mode,
924 const char *name,
925 struct weston_drm_backend_output_config *config)
926{
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300927 struct weston_config *wc = wet_get_config(c);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700928 struct weston_config_section *section;
929 char *s;
930 int scale;
931 enum weston_drm_backend_output_mode mode =
932 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
933
934 section = weston_config_get_section(wc, "output", "name", name);
935 weston_config_section_get_string(section, "mode", &s, "preferred");
936 if (strcmp(s, "off") == 0) {
937 free(s);
938 return WESTON_DRM_BACKEND_OUTPUT_OFF;
939 }
940
941 if (use_current_mode || strcmp(s, "current") == 0) {
942 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
943 } else if (strcmp(s, "preferred") != 0) {
944 config->modeline = s;
945 s = NULL;
946 }
947 free(s);
948
949 weston_config_section_get_int(section, "scale", &scale, 1);
950 config->base.scale = scale >= 1 ? scale : 1;
951 weston_config_section_get_string(section, "transform", &s, "normal");
952 if (weston_parse_transform(s, &config->base.transform) < 0)
953 weston_log("Invalid transform \"%s\" for output %s\n",
954 s, name);
955 free(s);
956
957 weston_config_section_get_string(section,
958 "gbm-format", &config->gbm_format, NULL);
959 weston_config_section_get_string(section, "seat", &config->seat, "");
960 return mode;
961}
962
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300963static void
964configure_input_device(struct weston_compositor *compositor,
965 struct libinput_device *device)
966{
967 struct weston_config_section *s;
968 struct weston_config *config = wet_get_config(compositor);
969 int enable_tap;
970 int enable_tap_default;
971
972 s = weston_config_get_section(config,
973 "libinput", NULL, NULL);
974
975 if (libinput_device_config_tap_get_finger_count(device) > 0) {
976 enable_tap_default =
977 libinput_device_config_tap_get_default_enabled(
978 device);
979 weston_config_section_get_bool(s, "enable_tap",
980 &enable_tap,
981 enable_tap_default);
982 libinput_device_config_tap_set_enabled(device,
983 enable_tap);
984 }
985}
986
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700987static int
Pekka Paalanen321ede72016-06-03 15:28:40 +0300988load_drm_backend(struct weston_compositor *c,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700989 int *argc, char **argv, struct weston_config *wc)
990{
991 struct weston_drm_backend_config config = {{ 0, }};
992 struct weston_config_section *section;
993 int ret = 0;
994
995 const struct weston_option options[] = {
996 { WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
997 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
998 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
999 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
1000 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1001 };
1002
1003 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1004
1005 section = weston_config_get_section(wc, "core", NULL, NULL);
1006 weston_config_section_get_string(section,
1007 "gbm-format", &config.gbm_format,
1008 NULL);
1009
1010 config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
1011 config.base.struct_size = sizeof(struct weston_drm_backend_config);
1012 config.configure_output = drm_configure_output;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001013 config.configure_device = configure_input_device;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001014
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001015 ret = weston_compositor_load_backend(c, WESTON_BACKEND_DRM,
1016 &config.base);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001017
1018 free(config.gbm_format);
1019 free(config.seat_id);
1020
1021 return ret;
1022}
1023
Giulio Camuffo43008c72015-10-17 19:24:15 +03001024static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001025load_headless_backend(struct weston_compositor *c,
Benoit Gschwind3c530942016-04-15 20:28:32 -07001026 int *argc, char **argv, struct weston_config *wc)
1027{
1028 struct weston_headless_backend_config config = {{ 0, }};
1029 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001030 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001031
1032 config.width = 1024;
1033 config.height = 640;
1034
1035 const struct weston_option options[] = {
1036 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1037 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1038 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1039 { WESTON_OPTION_STRING, "transform", 0, &transform },
1040 };
1041
1042 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1043
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001044 config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1045 if (transform) {
1046 if (weston_parse_transform(transform, &config.transform) < 0)
1047 weston_log("Invalid transform \"%s\"\n", transform);
1048 free(transform);
1049 }
Benoit Gschwind3c530942016-04-15 20:28:32 -07001050
1051 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1052 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1053
1054 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001055 ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1056 &config.base);
Benoit Gschwind3c530942016-04-15 20:28:32 -07001057
1058 return ret;
1059}
1060
Benoit Gschwindbd573102016-04-22 17:05:26 +02001061static void
1062weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
1063{
1064 config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
1065 config->base.struct_size = sizeof(struct weston_rdp_backend_config);
1066
1067 config->width = 640;
1068 config->height = 480;
1069 config->bind_address = NULL;
1070 config->port = 3389;
1071 config->rdp_key = NULL;
1072 config->server_cert = NULL;
1073 config->server_key = NULL;
1074 config->env_socket = 0;
1075 config->no_clients_resize = 0;
1076}
1077
1078static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001079load_rdp_backend(struct weston_compositor *c,
Benoit Gschwindbd573102016-04-22 17:05:26 +02001080 int *argc, char *argv[], struct weston_config *wc)
1081{
1082 struct weston_rdp_backend_config config = {{ 0, }};
1083 int ret = 0;
1084
1085 weston_rdp_backend_config_init(&config);
1086
1087 const struct weston_option rdp_options[] = {
1088 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1089 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1090 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1091 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
1092 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1093 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1094 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
1095 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
1096 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
1097 };
1098
1099 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1100
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001101 ret = weston_compositor_load_backend(c, WESTON_BACKEND_RDP,
1102 &config.base);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001103
1104 free(config.bind_address);
1105 free(config.rdp_key);
1106 free(config.server_cert);
1107 free(config.server_key);
1108 return ret;
1109}
1110
Benoit Gschwind3c530942016-04-15 20:28:32 -07001111static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001112load_fbdev_backend(struct weston_compositor *c,
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001113 int *argc, char **argv, struct weston_config *wc)
1114{
1115 struct weston_fbdev_backend_config config = {{ 0, }};
1116 struct weston_config_section *section;
1117 char *s = NULL;
1118 int ret = 0;
1119
1120 const struct weston_option fbdev_options[] = {
1121 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1122 { WESTON_OPTION_STRING, "device", 0, &config.device },
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001123 };
1124
1125 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
1126
1127 if (!config.device)
1128 config.device = strdup("/dev/fb0");
1129
1130 section = weston_config_get_section(wc, "output", "name", "fbdev");
1131 weston_config_section_get_string(section, "transform", &s, "normal");
1132 if (weston_parse_transform(s, &config.output_transform) < 0)
1133 weston_log("Invalid transform \"%s\" for output fbdev\n", s);
1134 free(s);
1135
1136 config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
1137 config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001138 config.configure_device = configure_input_device;
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001139
1140 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001141 ret = weston_compositor_load_backend(c, WESTON_BACKEND_FBDEV,
1142 &config.base);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001143
1144 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001145
1146 return ret;
1147}
1148
1149static int
1150weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
1151 struct weston_x11_backend_output_config *output_config) {
1152 struct weston_x11_backend_output_config *new_outputs;
1153
1154 new_outputs = realloc(config->outputs, (config->num_outputs+1) *
1155 sizeof(struct weston_x11_backend_output_config));
1156 if (new_outputs == NULL)
1157 return -1;
1158
1159 config->outputs = new_outputs;
1160 config->outputs[config->num_outputs].width = output_config->width;
1161 config->outputs[config->num_outputs].height = output_config->height;
1162 config->outputs[config->num_outputs].transform = output_config->transform;
1163 config->outputs[config->num_outputs].scale = output_config->scale;
1164 config->outputs[config->num_outputs].name = strdup(output_config->name);
1165 config->num_outputs++;
1166
1167 return 0;
1168}
1169
1170static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001171load_x11_backend(struct weston_compositor *c,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001172 int *argc, char **argv, struct weston_config *wc)
1173{
1174 struct weston_x11_backend_output_config default_output;
1175 struct weston_x11_backend_config config = {{ 0, }};
1176 struct weston_config_section *section;
1177 int ret = 0;
1178 int option_width = 0;
1179 int option_height = 0;
1180 int option_scale = 0;
1181 int option_count = 1;
1182 int output_count = 0;
1183 char const *section_name;
1184 int i;
1185 uint32_t j;
1186
1187 const struct weston_option options[] = {
1188 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1189 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1190 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
1191 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
1192 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1193 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
1194 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1195 };
1196
1197 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1198
1199 section = NULL;
1200 while (weston_config_next_section(wc, &section, &section_name)) {
1201 struct weston_x11_backend_output_config current_output = { 0, };
1202 char *t;
1203 char *mode;
1204
1205 if (strcmp(section_name, "output") != 0) {
1206 continue;
1207 }
1208
1209 weston_config_section_get_string(section, "name", &current_output.name, NULL);
1210 if (current_output.name == NULL || current_output.name[0] != 'X') {
1211 free(current_output.name);
1212 continue;
1213 }
1214
1215 weston_config_section_get_string(section, "mode", &mode, "1024x600");
1216 if (sscanf(mode, "%dx%d", &current_output.width,
1217 &current_output.height) != 2) {
1218 weston_log("Invalid mode \"%s\" for output %s\n",
1219 mode, current_output.name);
1220 current_output.width = 1024;
1221 current_output.height = 600;
1222 }
1223 free(mode);
1224 if (current_output.width < 1)
1225 current_output.width = 1024;
1226 if (current_output.height < 1)
1227 current_output.height = 600;
1228 if (option_width)
1229 current_output.width = option_width;
1230 if (option_height)
1231 current_output.height = option_height;
1232
1233 weston_config_section_get_int(section, "scale", &current_output.scale, 1);
1234 if (option_scale)
1235 current_output.scale = option_scale;
1236
1237 weston_config_section_get_string(section,
1238 "transform", &t, "normal");
1239 if (weston_parse_transform(t, &current_output.transform) < 0)
1240 weston_log("Invalid transform \"%s\" for output %s\n",
1241 t, current_output.name);
1242 free(t);
1243
1244 if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
1245 ret = -1;
1246 goto out;
1247 }
1248
1249 output_count++;
Bryce Harringtonaa258982016-05-03 01:34:23 -07001250 if (output_count >= option_count)
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001251 break;
1252 }
1253
1254 default_output.name = NULL;
1255 default_output.width = option_width ? option_width : 1024;
1256 default_output.height = option_height ? option_height : 600;
1257 default_output.scale = option_scale ? option_scale : 1;
1258 default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1259
1260 for (i = output_count; i < option_count; i++) {
1261 if (asprintf(&default_output.name, "screen%d", i) < 0) {
1262 ret = -1;
1263 goto out;
1264 }
1265
1266 if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
1267 ret = -1;
1268 free(default_output.name);
1269 goto out;
1270 }
1271 free(default_output.name);
1272 }
1273
1274 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
1275 config.base.struct_size = sizeof(struct weston_x11_backend_config);
1276
1277 /* load the actual backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001278 ret = weston_compositor_load_backend(c, WESTON_BACKEND_X11,
1279 &config.base);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001280
1281out:
1282 for (j = 0; j < config.num_outputs; ++j)
1283 free(config.outputs[j].name);
1284 free(config.outputs);
1285
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001286 return ret;
1287}
1288
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001289static void
1290weston_wayland_output_config_init(struct weston_wayland_backend_output_config *output_config,
1291 struct weston_config_section *config_section,
1292 int option_width, int option_height,
1293 int option_scale)
1294{
1295 char *mode, *t, *str;
1296 unsigned int slen;
1297
1298 weston_config_section_get_string(config_section, "name", &output_config->name,
1299 NULL);
1300 if (output_config->name) {
1301 slen = strlen(output_config->name);
1302 slen += strlen(WINDOW_TITLE " - ");
1303 str = malloc(slen + 1);
1304 if (str)
1305 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1306 output_config->name);
1307 free(output_config->name);
1308 output_config->name = str;
1309 }
1310 if (!output_config->name)
1311 output_config->name = strdup(WINDOW_TITLE);
1312
1313 weston_config_section_get_string(config_section,
1314 "mode", &mode, "1024x600");
1315 if (sscanf(mode, "%dx%d", &output_config->width, &output_config->height) != 2) {
1316 weston_log("Invalid mode \"%s\" for output %s\n",
1317 mode, output_config->name);
1318 output_config->width = 1024;
1319 output_config->height = 640;
1320 }
1321 free(mode);
1322
1323 if (option_width)
1324 output_config->width = option_width;
1325 if (option_height)
1326 output_config->height = option_height;
1327
1328 weston_config_section_get_int(config_section, "scale", &output_config->scale, 1);
1329
1330 if (option_scale)
1331 output_config->scale = option_scale;
1332
1333 weston_config_section_get_string(config_section,
1334 "transform", &t, "normal");
1335 if (weston_parse_transform(t, &output_config->transform) < 0)
1336 weston_log("Invalid transform \"%s\" for output %s\n",
1337 t, output_config->name);
1338 free(t);
1339
1340}
1341
1342static void
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001343weston_wayland_backend_config_release(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001344{
1345 int i;
1346
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001347 for (i = 0; i < config->num_outputs; ++i) {
1348 free(config->outputs[i].name);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001349 }
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001350 free(config->cursor_theme);
1351 free(config->display_name);
1352 free(config->outputs);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001353}
1354
1355/*
1356 * Append a new output struct at the end of new_config.outputs and return a
1357 * pointer to the newly allocated structure or NULL if fail. The allocated
1358 * structure is NOT cleared nor set to default values.
1359 */
1360static struct weston_wayland_backend_output_config *
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001361weston_wayland_backend_config_add_new_output(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001362{
1363 struct weston_wayland_backend_output_config *outputs;
1364 const size_t element_size = sizeof(struct weston_wayland_backend_output_config);
1365
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001366 outputs = realloc(config->outputs,
1367 (config->num_outputs + 1) * element_size);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001368 if (!outputs)
1369 return NULL;
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001370 config->num_outputs += 1;
1371 config->outputs = outputs;
1372 return &(config->outputs[config->num_outputs - 1]);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001373}
1374
1375static int
1376load_wayland_backend_config(struct weston_compositor *compositor, int *argc,
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001377 char *argv[], struct weston_config *wc,
Benoit Gschwind55a22882016-05-10 22:47:51 +02001378 struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001379{
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001380 struct weston_config_section *section;
1381 struct weston_wayland_backend_output_config *oc;
1382 int count, width, height, scale;
1383 const char *section_name;
1384 char *name;
1385
1386 const struct weston_option wayland_options[] = {
1387 { WESTON_OPTION_INTEGER, "width", 0, &width },
1388 { WESTON_OPTION_INTEGER, "height", 0, &height },
1389 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001390 { WESTON_OPTION_STRING, "display", 0, &config->display_name },
1391 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config->use_pixman },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001392 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001393 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config->fullscreen },
1394 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &config->sprawl },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001395 };
1396
1397 width = 0;
1398 height = 0;
1399 scale = 0;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001400 config->display_name = NULL;
1401 config->use_pixman = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001402 count = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001403 config->fullscreen = 0;
1404 config->sprawl = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001405 parse_options(wayland_options,
1406 ARRAY_LENGTH(wayland_options), argc, argv);
1407
Benoit Gschwind55a22882016-05-10 22:47:51 +02001408 config->cursor_size = 32;
1409 config->cursor_theme = NULL;
1410 config->base.struct_size = sizeof(struct weston_wayland_backend_config);
1411 config->base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001412
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001413 section = weston_config_get_section(wc, "shell", NULL, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001414 weston_config_section_get_string(section, "cursor-theme",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001415 &config->cursor_theme, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001416 weston_config_section_get_int(section, "cursor-size",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001417 &config->cursor_size, 32);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001418
Benoit Gschwind55a22882016-05-10 22:47:51 +02001419 if (config->sprawl) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001420 /* do nothing, everything is already set */
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001421 return 0;
1422 }
1423
Benoit Gschwind55a22882016-05-10 22:47:51 +02001424 if (config->fullscreen) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001425 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001426 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001427 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001428
1429 oc->width = width;
1430 oc->height = height;
1431 oc->name = NULL;
1432 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1433 oc->scale = 1;
1434
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001435 return 0;
1436 }
1437
1438 section = NULL;
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001439 while (weston_config_next_section(wc, &section, &section_name)) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001440 if (!section_name || strcmp(section_name, "output") != 0)
1441 continue;
1442 weston_config_section_get_string(section, "name", &name, NULL);
1443 if (name == NULL)
1444 continue;
1445
1446 if (name[0] != 'W' || name[1] != 'L') {
1447 free(name);
1448 continue;
1449 }
1450 free(name);
1451
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001452 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001453 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001454 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001455
1456 weston_wayland_output_config_init(oc, section, width,
1457 height, scale);
1458 --count;
1459 }
1460
1461 if (!width)
1462 width = 1024;
1463 if (!height)
1464 height = 640;
1465 if (!scale)
1466 scale = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001467
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001468 while (count > 0) {
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 oc->width = width;
1474 oc->height = height;
1475 oc->name = NULL;
1476 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1477 oc->scale = scale;
1478
1479 --count;
1480 }
1481
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001482 return 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001483}
1484
1485static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001486load_wayland_backend(struct weston_compositor *c,
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001487 int *argc, char **argv, struct weston_config *wc)
1488{
1489 struct weston_wayland_backend_config config = {{ 0, }};
1490 int ret = 0;
1491
1492 ret = load_wayland_backend_config(c, argc, argv, wc, &config);
1493 if(ret < 0) {
Benoit Gschwind53753842016-05-10 22:47:57 +02001494 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001495 return ret;
1496 }
1497
1498 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001499 ret = weston_compositor_load_backend(c, WESTON_BACKEND_WAYLAND,
1500 &config.base);
Benoit Gschwind68d6a6c2016-05-10 22:47:53 +02001501 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001502 return ret;
1503}
1504
1505
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001506static int
Giulio Camuffo43008c72015-10-17 19:24:15 +03001507load_backend(struct weston_compositor *compositor, const char *backend,
1508 int *argc, char **argv, struct weston_config *config)
1509{
Benoit Gschwind3c530942016-04-15 20:28:32 -07001510 if (strstr(backend, "headless-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001511 return load_headless_backend(compositor, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001512 else if (strstr(backend, "rdp-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001513 return load_rdp_backend(compositor, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001514 else if (strstr(backend, "fbdev-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001515 return load_fbdev_backend(compositor, argc, argv, config);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001516 else if (strstr(backend, "drm-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001517 return load_drm_backend(compositor, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001518 else if (strstr(backend, "x11-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001519 return load_x11_backend(compositor, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001520 else if (strstr(backend, "wayland-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001521 return load_wayland_backend(compositor, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001522
Pekka Paalanen808b0062016-06-03 13:56:17 +03001523 weston_log("Error: unknown backend \"%s\"\n", backend);
1524 return -1;
Giulio Camuffo43008c72015-10-17 19:24:15 +03001525}
1526
Giulio Camuffobab996e2014-10-12 00:24:25 +03001527int main(int argc, char *argv[])
1528{
1529 int ret = EXIT_FAILURE;
1530 struct wl_display *display;
1531 struct weston_compositor *ec;
1532 struct wl_event_source *signals[4];
1533 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001534 int i, fd;
1535 char *backend = NULL;
1536 char *shell = NULL;
1537 char *modules = NULL;
1538 char *option_modules = NULL;
1539 char *log = NULL;
1540 char *server_socket = NULL, *end;
1541 int32_t idle_time = -1;
1542 int32_t help = 0;
1543 char *socket_name = NULL;
1544 int32_t version = 0;
1545 int32_t noconfig = 0;
1546 int32_t numlock_on;
1547 char *config_file = NULL;
1548 struct weston_config *config = NULL;
1549 struct weston_config_section *section;
1550 struct wl_client *primary_client;
1551 struct wl_listener primary_client_destroyed;
1552 struct weston_seat *seat;
1553
1554 const struct weston_option core_options[] = {
1555 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1556 { WESTON_OPTION_STRING, "shell", 0, &shell },
1557 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1558 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
1559 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1560 { WESTON_OPTION_STRING, "log", 0, &log },
1561 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1562 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1563 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1564 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1565 };
1566
1567 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1568
1569 if (help)
1570 usage(EXIT_SUCCESS);
1571
1572 if (version) {
1573 printf(PACKAGE_STRING "\n");
1574 return EXIT_SUCCESS;
1575 }
1576
Giulio Camuffobe2b11a2016-06-02 21:48:13 +03001577 weston_log_set_handler(vlog, vlog_continue);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001578 weston_log_file_open(log);
1579
1580 weston_log("%s\n"
1581 STAMP_SPACE "%s\n"
1582 STAMP_SPACE "Bug reports to: %s\n"
1583 STAMP_SPACE "Build: %s\n",
1584 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1585 BUILD_ID);
1586 log_uname();
1587
1588 verify_xdg_runtime_dir();
1589
1590 display = wl_display_create();
1591
1592 loop = wl_display_get_event_loop(display);
1593 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1594 display);
1595 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1596 display);
1597 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1598 display);
1599
1600 wl_list_init(&child_process_list);
1601 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1602 NULL);
1603
1604 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1605 goto out_signals;
1606
1607 if (load_configuration(&config, noconfig, config_file) < 0)
1608 goto out_signals;
1609
1610 section = weston_config_get_section(config, "core", NULL, NULL);
1611
1612 if (!backend) {
1613 weston_config_section_get_string(section, "backend", &backend,
1614 NULL);
1615 if (!backend)
1616 backend = weston_choose_default_backend();
1617 }
1618
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001619 ec = weston_compositor_create(display, config);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001620 if (ec == NULL) {
1621 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001622 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001623 }
1624
Giulio Camuffobab996e2014-10-12 00:24:25 +03001625 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001626 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001627
Giulio Camuffo43008c72015-10-17 19:24:15 +03001628 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001629 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001630 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001631 }
1632
1633 catch_signals();
1634 segv_compositor = ec;
1635
1636 if (idle_time < 0)
1637 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1638 if (idle_time < 0)
1639 idle_time = 300; /* default idle timeout, in seconds */
1640
1641 ec->idle_time = idle_time;
1642 ec->default_pointer_grab = NULL;
1643 ec->exit = handle_exit;
1644
1645 weston_compositor_log_capabilities(ec);
1646
1647 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1648 if (server_socket) {
1649 weston_log("Running with single client\n");
1650 fd = strtol(server_socket, &end, 0);
1651 if (*end != '\0')
1652 fd = -1;
1653 } else {
1654 fd = -1;
1655 }
1656
1657 if (fd != -1) {
1658 primary_client = wl_client_create(display, fd);
1659 if (!primary_client) {
1660 weston_log("fatal: failed to add client: %m\n");
1661 goto out;
1662 }
1663 primary_client_destroyed.notify =
1664 handle_primary_client_destroyed;
1665 wl_client_add_destroy_listener(primary_client,
1666 &primary_client_destroyed);
1667 } else if (weston_create_listening_socket(display, socket_name)) {
1668 goto out;
1669 }
1670
1671 if (!shell)
1672 weston_config_section_get_string(section, "shell", &shell,
1673 "desktop-shell.so");
1674
1675 if (load_modules(ec, shell, &argc, argv) < 0)
1676 goto out;
1677
1678 weston_config_section_get_string(section, "modules", &modules, "");
1679 if (load_modules(ec, modules, &argc, argv) < 0)
1680 goto out;
1681
1682 if (load_modules(ec, option_modules, &argc, argv) < 0)
1683 goto out;
1684
1685 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1686 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1687 if (numlock_on) {
1688 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001689 struct weston_keyboard *keyboard =
1690 weston_seat_get_keyboard(seat);
1691
1692 if (keyboard)
1693 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001694 WESTON_NUM_LOCK,
1695 WESTON_NUM_LOCK);
1696 }
1697 }
1698
1699 for (i = 1; i < argc; i++)
1700 weston_log("fatal: unhandled option: %s\n", argv[i]);
1701 if (argc > 1)
1702 goto out;
1703
1704 weston_compositor_wake(ec);
1705
1706 wl_display_run(display);
1707
1708 /* Allow for setting return exit code after
1709 * wl_display_run returns normally. This is
1710 * useful for devs/testers and automated tests
1711 * that want to indicate failure status to
1712 * testing infrastructure above
1713 */
1714 ret = ec->exit_code;
1715
1716out:
1717 weston_compositor_destroy(ec);
1718
1719out_signals:
1720 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1721 if (signals[i])
1722 wl_event_source_remove(signals[i]);
1723
1724 wl_display_destroy(display);
1725
1726 weston_log_file_close();
1727
1728 if (config)
1729 weston_config_destroy(config);
1730 free(config_file);
1731 free(backend);
1732 free(shell);
1733 free(socket_name);
1734 free(option_modules);
1735 free(log);
1736 free(modules);
1737
1738 return ret;
1739}