blob: d02261522c07324c05d4c8fc2a544676c7edb7a6 [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;
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
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700912static enum weston_drm_backend_output_mode
913drm_configure_output(struct weston_compositor *c,
914 bool use_current_mode,
915 const char *name,
916 struct weston_drm_backend_output_config *config)
917{
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300918 struct weston_config *wc = wet_get_config(c);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700919 struct weston_config_section *section;
920 char *s;
921 int scale;
922 enum weston_drm_backend_output_mode mode =
923 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
924
925 section = weston_config_get_section(wc, "output", "name", name);
926 weston_config_section_get_string(section, "mode", &s, "preferred");
927 if (strcmp(s, "off") == 0) {
928 free(s);
929 return WESTON_DRM_BACKEND_OUTPUT_OFF;
930 }
931
932 if (use_current_mode || strcmp(s, "current") == 0) {
933 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
934 } else if (strcmp(s, "preferred") != 0) {
935 config->modeline = s;
936 s = NULL;
937 }
938 free(s);
939
940 weston_config_section_get_int(section, "scale", &scale, 1);
941 config->base.scale = scale >= 1 ? scale : 1;
942 weston_config_section_get_string(section, "transform", &s, "normal");
943 if (weston_parse_transform(s, &config->base.transform) < 0)
944 weston_log("Invalid transform \"%s\" for output %s\n",
945 s, name);
946 free(s);
947
948 weston_config_section_get_string(section,
949 "gbm-format", &config->gbm_format, NULL);
950 weston_config_section_get_string(section, "seat", &config->seat, "");
951 return mode;
952}
953
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300954static void
955configure_input_device(struct weston_compositor *compositor,
956 struct libinput_device *device)
957{
958 struct weston_config_section *s;
959 struct weston_config *config = wet_get_config(compositor);
960 int enable_tap;
961 int enable_tap_default;
962
963 s = weston_config_get_section(config,
964 "libinput", NULL, NULL);
965
966 if (libinput_device_config_tap_get_finger_count(device) > 0) {
967 enable_tap_default =
968 libinput_device_config_tap_get_default_enabled(
969 device);
970 weston_config_section_get_bool(s, "enable_tap",
971 &enable_tap,
972 enable_tap_default);
973 libinput_device_config_tap_set_enabled(device,
974 enable_tap);
975 }
976}
977
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700978static int
Pekka Paalanen321ede72016-06-03 15:28:40 +0300979load_drm_backend(struct weston_compositor *c,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700980 int *argc, char **argv, struct weston_config *wc)
981{
982 struct weston_drm_backend_config config = {{ 0, }};
983 struct weston_config_section *section;
984 int ret = 0;
985
986 const struct weston_option options[] = {
987 { WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
988 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
989 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
990 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
991 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
992 };
993
994 parse_options(options, ARRAY_LENGTH(options), argc, argv);
995
996 section = weston_config_get_section(wc, "core", NULL, NULL);
997 weston_config_section_get_string(section,
998 "gbm-format", &config.gbm_format,
999 NULL);
1000
1001 config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
1002 config.base.struct_size = sizeof(struct weston_drm_backend_config);
1003 config.configure_output = drm_configure_output;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001004 config.configure_device = configure_input_device;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001005
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001006 ret = weston_compositor_load_backend(c, WESTON_BACKEND_DRM,
1007 &config.base);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001008
1009 free(config.gbm_format);
1010 free(config.seat_id);
1011
1012 return ret;
1013}
1014
Giulio Camuffo43008c72015-10-17 19:24:15 +03001015static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001016load_headless_backend(struct weston_compositor *c,
Benoit Gschwind3c530942016-04-15 20:28:32 -07001017 int *argc, char **argv, struct weston_config *wc)
1018{
1019 struct weston_headless_backend_config config = {{ 0, }};
1020 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001021 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001022
1023 config.width = 1024;
1024 config.height = 640;
1025
1026 const struct weston_option options[] = {
1027 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1028 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1029 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1030 { WESTON_OPTION_STRING, "transform", 0, &transform },
1031 };
1032
1033 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1034
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001035 config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1036 if (transform) {
1037 if (weston_parse_transform(transform, &config.transform) < 0)
1038 weston_log("Invalid transform \"%s\"\n", transform);
1039 free(transform);
1040 }
Benoit Gschwind3c530942016-04-15 20:28:32 -07001041
1042 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1043 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1044
1045 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001046 ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1047 &config.base);
Benoit Gschwind3c530942016-04-15 20:28:32 -07001048
1049 return ret;
1050}
1051
Benoit Gschwindbd573102016-04-22 17:05:26 +02001052static void
1053weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
1054{
1055 config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
1056 config->base.struct_size = sizeof(struct weston_rdp_backend_config);
1057
1058 config->width = 640;
1059 config->height = 480;
1060 config->bind_address = NULL;
1061 config->port = 3389;
1062 config->rdp_key = NULL;
1063 config->server_cert = NULL;
1064 config->server_key = NULL;
1065 config->env_socket = 0;
1066 config->no_clients_resize = 0;
1067}
1068
1069static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001070load_rdp_backend(struct weston_compositor *c,
Benoit Gschwindbd573102016-04-22 17:05:26 +02001071 int *argc, char *argv[], struct weston_config *wc)
1072{
1073 struct weston_rdp_backend_config config = {{ 0, }};
1074 int ret = 0;
1075
1076 weston_rdp_backend_config_init(&config);
1077
1078 const struct weston_option rdp_options[] = {
1079 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
1080 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1081 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1082 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
1083 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1084 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1085 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
1086 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
1087 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
1088 };
1089
1090 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1091
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001092 ret = weston_compositor_load_backend(c, WESTON_BACKEND_RDP,
1093 &config.base);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001094
1095 free(config.bind_address);
1096 free(config.rdp_key);
1097 free(config.server_cert);
1098 free(config.server_key);
1099 return ret;
1100}
1101
Benoit Gschwind3c530942016-04-15 20:28:32 -07001102static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001103load_fbdev_backend(struct weston_compositor *c,
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001104 int *argc, char **argv, struct weston_config *wc)
1105{
1106 struct weston_fbdev_backend_config config = {{ 0, }};
1107 struct weston_config_section *section;
1108 char *s = NULL;
1109 int ret = 0;
1110
1111 const struct weston_option fbdev_options[] = {
1112 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1113 { WESTON_OPTION_STRING, "device", 0, &config.device },
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001114 };
1115
1116 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
1117
1118 if (!config.device)
1119 config.device = strdup("/dev/fb0");
1120
1121 section = weston_config_get_section(wc, "output", "name", "fbdev");
1122 weston_config_section_get_string(section, "transform", &s, "normal");
1123 if (weston_parse_transform(s, &config.output_transform) < 0)
1124 weston_log("Invalid transform \"%s\" for output fbdev\n", s);
1125 free(s);
1126
1127 config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
1128 config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001129 config.configure_device = configure_input_device;
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001130
1131 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001132 ret = weston_compositor_load_backend(c, WESTON_BACKEND_FBDEV,
1133 &config.base);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001134
1135 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001136
1137 return ret;
1138}
1139
1140static int
1141weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
1142 struct weston_x11_backend_output_config *output_config) {
1143 struct weston_x11_backend_output_config *new_outputs;
1144
1145 new_outputs = realloc(config->outputs, (config->num_outputs+1) *
1146 sizeof(struct weston_x11_backend_output_config));
1147 if (new_outputs == NULL)
1148 return -1;
1149
1150 config->outputs = new_outputs;
1151 config->outputs[config->num_outputs].width = output_config->width;
1152 config->outputs[config->num_outputs].height = output_config->height;
1153 config->outputs[config->num_outputs].transform = output_config->transform;
1154 config->outputs[config->num_outputs].scale = output_config->scale;
1155 config->outputs[config->num_outputs].name = strdup(output_config->name);
1156 config->num_outputs++;
1157
1158 return 0;
1159}
1160
1161static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001162load_x11_backend(struct weston_compositor *c,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001163 int *argc, char **argv, struct weston_config *wc)
1164{
1165 struct weston_x11_backend_output_config default_output;
1166 struct weston_x11_backend_config config = {{ 0, }};
1167 struct weston_config_section *section;
1168 int ret = 0;
1169 int option_width = 0;
1170 int option_height = 0;
1171 int option_scale = 0;
1172 int option_count = 1;
1173 int output_count = 0;
1174 char const *section_name;
1175 int i;
1176 uint32_t j;
1177
1178 const struct weston_option options[] = {
1179 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1180 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1181 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
1182 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
1183 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1184 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
1185 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1186 };
1187
1188 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1189
1190 section = NULL;
1191 while (weston_config_next_section(wc, &section, &section_name)) {
1192 struct weston_x11_backend_output_config current_output = { 0, };
1193 char *t;
1194 char *mode;
1195
1196 if (strcmp(section_name, "output") != 0) {
1197 continue;
1198 }
1199
1200 weston_config_section_get_string(section, "name", &current_output.name, NULL);
1201 if (current_output.name == NULL || current_output.name[0] != 'X') {
1202 free(current_output.name);
1203 continue;
1204 }
1205
1206 weston_config_section_get_string(section, "mode", &mode, "1024x600");
1207 if (sscanf(mode, "%dx%d", &current_output.width,
1208 &current_output.height) != 2) {
1209 weston_log("Invalid mode \"%s\" for output %s\n",
1210 mode, current_output.name);
1211 current_output.width = 1024;
1212 current_output.height = 600;
1213 }
1214 free(mode);
1215 if (current_output.width < 1)
1216 current_output.width = 1024;
1217 if (current_output.height < 1)
1218 current_output.height = 600;
1219 if (option_width)
1220 current_output.width = option_width;
1221 if (option_height)
1222 current_output.height = option_height;
1223
1224 weston_config_section_get_int(section, "scale", &current_output.scale, 1);
1225 if (option_scale)
1226 current_output.scale = option_scale;
1227
1228 weston_config_section_get_string(section,
1229 "transform", &t, "normal");
1230 if (weston_parse_transform(t, &current_output.transform) < 0)
1231 weston_log("Invalid transform \"%s\" for output %s\n",
1232 t, current_output.name);
1233 free(t);
1234
1235 if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
1236 ret = -1;
1237 goto out;
1238 }
1239
1240 output_count++;
Bryce Harringtonaa258982016-05-03 01:34:23 -07001241 if (output_count >= option_count)
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001242 break;
1243 }
1244
1245 default_output.name = NULL;
1246 default_output.width = option_width ? option_width : 1024;
1247 default_output.height = option_height ? option_height : 600;
1248 default_output.scale = option_scale ? option_scale : 1;
1249 default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1250
1251 for (i = output_count; i < option_count; i++) {
1252 if (asprintf(&default_output.name, "screen%d", i) < 0) {
1253 ret = -1;
1254 goto out;
1255 }
1256
1257 if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
1258 ret = -1;
1259 free(default_output.name);
1260 goto out;
1261 }
1262 free(default_output.name);
1263 }
1264
1265 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
1266 config.base.struct_size = sizeof(struct weston_x11_backend_config);
1267
1268 /* load the actual backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001269 ret = weston_compositor_load_backend(c, WESTON_BACKEND_X11,
1270 &config.base);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001271
1272out:
1273 for (j = 0; j < config.num_outputs; ++j)
1274 free(config.outputs[j].name);
1275 free(config.outputs);
1276
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001277 return ret;
1278}
1279
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001280static void
1281weston_wayland_output_config_init(struct weston_wayland_backend_output_config *output_config,
1282 struct weston_config_section *config_section,
1283 int option_width, int option_height,
1284 int option_scale)
1285{
1286 char *mode, *t, *str;
1287 unsigned int slen;
1288
1289 weston_config_section_get_string(config_section, "name", &output_config->name,
1290 NULL);
1291 if (output_config->name) {
1292 slen = strlen(output_config->name);
1293 slen += strlen(WINDOW_TITLE " - ");
1294 str = malloc(slen + 1);
1295 if (str)
1296 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1297 output_config->name);
1298 free(output_config->name);
1299 output_config->name = str;
1300 }
1301 if (!output_config->name)
1302 output_config->name = strdup(WINDOW_TITLE);
1303
1304 weston_config_section_get_string(config_section,
1305 "mode", &mode, "1024x600");
1306 if (sscanf(mode, "%dx%d", &output_config->width, &output_config->height) != 2) {
1307 weston_log("Invalid mode \"%s\" for output %s\n",
1308 mode, output_config->name);
1309 output_config->width = 1024;
1310 output_config->height = 640;
1311 }
1312 free(mode);
1313
1314 if (option_width)
1315 output_config->width = option_width;
1316 if (option_height)
1317 output_config->height = option_height;
1318
1319 weston_config_section_get_int(config_section, "scale", &output_config->scale, 1);
1320
1321 if (option_scale)
1322 output_config->scale = option_scale;
1323
1324 weston_config_section_get_string(config_section,
1325 "transform", &t, "normal");
1326 if (weston_parse_transform(t, &output_config->transform) < 0)
1327 weston_log("Invalid transform \"%s\" for output %s\n",
1328 t, output_config->name);
1329 free(t);
1330
1331}
1332
1333static void
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001334weston_wayland_backend_config_release(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001335{
1336 int i;
1337
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001338 for (i = 0; i < config->num_outputs; ++i) {
1339 free(config->outputs[i].name);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001340 }
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001341 free(config->cursor_theme);
1342 free(config->display_name);
1343 free(config->outputs);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001344}
1345
1346/*
1347 * Append a new output struct at the end of new_config.outputs and return a
1348 * pointer to the newly allocated structure or NULL if fail. The allocated
1349 * structure is NOT cleared nor set to default values.
1350 */
1351static struct weston_wayland_backend_output_config *
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001352weston_wayland_backend_config_add_new_output(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001353{
1354 struct weston_wayland_backend_output_config *outputs;
1355 const size_t element_size = sizeof(struct weston_wayland_backend_output_config);
1356
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001357 outputs = realloc(config->outputs,
1358 (config->num_outputs + 1) * element_size);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001359 if (!outputs)
1360 return NULL;
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001361 config->num_outputs += 1;
1362 config->outputs = outputs;
1363 return &(config->outputs[config->num_outputs - 1]);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001364}
1365
1366static int
1367load_wayland_backend_config(struct weston_compositor *compositor, int *argc,
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001368 char *argv[], struct weston_config *wc,
Benoit Gschwind55a22882016-05-10 22:47:51 +02001369 struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001370{
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001371 struct weston_config_section *section;
1372 struct weston_wayland_backend_output_config *oc;
1373 int count, width, height, scale;
1374 const char *section_name;
1375 char *name;
1376
1377 const struct weston_option wayland_options[] = {
1378 { WESTON_OPTION_INTEGER, "width", 0, &width },
1379 { WESTON_OPTION_INTEGER, "height", 0, &height },
1380 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001381 { WESTON_OPTION_STRING, "display", 0, &config->display_name },
1382 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config->use_pixman },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001383 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001384 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config->fullscreen },
1385 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &config->sprawl },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001386 };
1387
1388 width = 0;
1389 height = 0;
1390 scale = 0;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001391 config->display_name = NULL;
1392 config->use_pixman = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001393 count = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001394 config->fullscreen = 0;
1395 config->sprawl = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001396 parse_options(wayland_options,
1397 ARRAY_LENGTH(wayland_options), argc, argv);
1398
Benoit Gschwind55a22882016-05-10 22:47:51 +02001399 config->cursor_size = 32;
1400 config->cursor_theme = NULL;
1401 config->base.struct_size = sizeof(struct weston_wayland_backend_config);
1402 config->base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001403
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001404 section = weston_config_get_section(wc, "shell", NULL, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001405 weston_config_section_get_string(section, "cursor-theme",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001406 &config->cursor_theme, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001407 weston_config_section_get_int(section, "cursor-size",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001408 &config->cursor_size, 32);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001409
Benoit Gschwind55a22882016-05-10 22:47:51 +02001410 if (config->sprawl) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001411 /* do nothing, everything is already set */
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001412 return 0;
1413 }
1414
Benoit Gschwind55a22882016-05-10 22:47:51 +02001415 if (config->fullscreen) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001416 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001417 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001418 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001419
1420 oc->width = width;
1421 oc->height = height;
1422 oc->name = NULL;
1423 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1424 oc->scale = 1;
1425
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001426 return 0;
1427 }
1428
1429 section = NULL;
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001430 while (weston_config_next_section(wc, &section, &section_name)) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001431 if (!section_name || strcmp(section_name, "output") != 0)
1432 continue;
1433 weston_config_section_get_string(section, "name", &name, NULL);
1434 if (name == NULL)
1435 continue;
1436
1437 if (name[0] != 'W' || name[1] != 'L') {
1438 free(name);
1439 continue;
1440 }
1441 free(name);
1442
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001443 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001444 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001445 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001446
1447 weston_wayland_output_config_init(oc, section, width,
1448 height, scale);
1449 --count;
1450 }
1451
1452 if (!width)
1453 width = 1024;
1454 if (!height)
1455 height = 640;
1456 if (!scale)
1457 scale = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001458
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001459 while (count > 0) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001460 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001461 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001462 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001463
1464 oc->width = width;
1465 oc->height = height;
1466 oc->name = NULL;
1467 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1468 oc->scale = scale;
1469
1470 --count;
1471 }
1472
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001473 return 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001474}
1475
1476static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001477load_wayland_backend(struct weston_compositor *c,
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001478 int *argc, char **argv, struct weston_config *wc)
1479{
1480 struct weston_wayland_backend_config config = {{ 0, }};
1481 int ret = 0;
1482
1483 ret = load_wayland_backend_config(c, argc, argv, wc, &config);
1484 if(ret < 0) {
Benoit Gschwind53753842016-05-10 22:47:57 +02001485 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001486 return ret;
1487 }
1488
1489 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001490 ret = weston_compositor_load_backend(c, WESTON_BACKEND_WAYLAND,
1491 &config.base);
Benoit Gschwind68d6a6c2016-05-10 22:47:53 +02001492 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001493 return ret;
1494}
1495
1496
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001497static int
Giulio Camuffo43008c72015-10-17 19:24:15 +03001498load_backend(struct weston_compositor *compositor, const char *backend,
1499 int *argc, char **argv, struct weston_config *config)
1500{
Benoit Gschwind3c530942016-04-15 20:28:32 -07001501 if (strstr(backend, "headless-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001502 return load_headless_backend(compositor, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001503 else if (strstr(backend, "rdp-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001504 return load_rdp_backend(compositor, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001505 else if (strstr(backend, "fbdev-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001506 return load_fbdev_backend(compositor, argc, argv, config);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001507 else if (strstr(backend, "drm-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001508 return load_drm_backend(compositor, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001509 else if (strstr(backend, "x11-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001510 return load_x11_backend(compositor, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001511 else if (strstr(backend, "wayland-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001512 return load_wayland_backend(compositor, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001513
Pekka Paalanen808b0062016-06-03 13:56:17 +03001514 weston_log("Error: unknown backend \"%s\"\n", backend);
1515 return -1;
Giulio Camuffo43008c72015-10-17 19:24:15 +03001516}
1517
Giulio Camuffobab996e2014-10-12 00:24:25 +03001518int main(int argc, char *argv[])
1519{
1520 int ret = EXIT_FAILURE;
1521 struct wl_display *display;
1522 struct weston_compositor *ec;
1523 struct wl_event_source *signals[4];
1524 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001525 int i, fd;
1526 char *backend = NULL;
1527 char *shell = NULL;
1528 char *modules = NULL;
1529 char *option_modules = NULL;
1530 char *log = NULL;
1531 char *server_socket = NULL, *end;
1532 int32_t idle_time = -1;
1533 int32_t help = 0;
1534 char *socket_name = NULL;
1535 int32_t version = 0;
1536 int32_t noconfig = 0;
1537 int32_t numlock_on;
1538 char *config_file = NULL;
1539 struct weston_config *config = NULL;
1540 struct weston_config_section *section;
1541 struct wl_client *primary_client;
1542 struct wl_listener primary_client_destroyed;
1543 struct weston_seat *seat;
1544
1545 const struct weston_option core_options[] = {
1546 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1547 { WESTON_OPTION_STRING, "shell", 0, &shell },
1548 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1549 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
1550 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1551 { WESTON_OPTION_STRING, "log", 0, &log },
1552 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1553 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1554 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1555 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1556 };
1557
1558 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1559
1560 if (help)
1561 usage(EXIT_SUCCESS);
1562
1563 if (version) {
1564 printf(PACKAGE_STRING "\n");
1565 return EXIT_SUCCESS;
1566 }
1567
Giulio Camuffobe2b11a2016-06-02 21:48:13 +03001568 weston_log_set_handler(vlog, vlog_continue);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001569 weston_log_file_open(log);
1570
1571 weston_log("%s\n"
1572 STAMP_SPACE "%s\n"
1573 STAMP_SPACE "Bug reports to: %s\n"
1574 STAMP_SPACE "Build: %s\n",
1575 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1576 BUILD_ID);
1577 log_uname();
1578
1579 verify_xdg_runtime_dir();
1580
1581 display = wl_display_create();
1582
1583 loop = wl_display_get_event_loop(display);
1584 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1585 display);
1586 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1587 display);
1588 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1589 display);
1590
1591 wl_list_init(&child_process_list);
1592 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1593 NULL);
1594
1595 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1596 goto out_signals;
1597
1598 if (load_configuration(&config, noconfig, config_file) < 0)
1599 goto out_signals;
1600
1601 section = weston_config_get_section(config, "core", NULL, NULL);
1602
1603 if (!backend) {
1604 weston_config_section_get_string(section, "backend", &backend,
1605 NULL);
1606 if (!backend)
1607 backend = weston_choose_default_backend();
1608 }
1609
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001610 ec = weston_compositor_create(display, config);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001611 if (ec == NULL) {
1612 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001613 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001614 }
1615
Giulio Camuffobab996e2014-10-12 00:24:25 +03001616 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001617 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001618
Giulio Camuffo43008c72015-10-17 19:24:15 +03001619 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001620 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001621 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001622 }
1623
1624 catch_signals();
1625 segv_compositor = ec;
1626
1627 if (idle_time < 0)
1628 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1629 if (idle_time < 0)
1630 idle_time = 300; /* default idle timeout, in seconds */
1631
1632 ec->idle_time = idle_time;
1633 ec->default_pointer_grab = NULL;
1634 ec->exit = handle_exit;
1635
1636 weston_compositor_log_capabilities(ec);
1637
1638 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1639 if (server_socket) {
1640 weston_log("Running with single client\n");
1641 fd = strtol(server_socket, &end, 0);
1642 if (*end != '\0')
1643 fd = -1;
1644 } else {
1645 fd = -1;
1646 }
1647
1648 if (fd != -1) {
1649 primary_client = wl_client_create(display, fd);
1650 if (!primary_client) {
1651 weston_log("fatal: failed to add client: %m\n");
1652 goto out;
1653 }
1654 primary_client_destroyed.notify =
1655 handle_primary_client_destroyed;
1656 wl_client_add_destroy_listener(primary_client,
1657 &primary_client_destroyed);
1658 } else if (weston_create_listening_socket(display, socket_name)) {
1659 goto out;
1660 }
1661
1662 if (!shell)
1663 weston_config_section_get_string(section, "shell", &shell,
1664 "desktop-shell.so");
1665
1666 if (load_modules(ec, shell, &argc, argv) < 0)
1667 goto out;
1668
1669 weston_config_section_get_string(section, "modules", &modules, "");
1670 if (load_modules(ec, modules, &argc, argv) < 0)
1671 goto out;
1672
1673 if (load_modules(ec, option_modules, &argc, argv) < 0)
1674 goto out;
1675
1676 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1677 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1678 if (numlock_on) {
1679 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001680 struct weston_keyboard *keyboard =
1681 weston_seat_get_keyboard(seat);
1682
1683 if (keyboard)
1684 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001685 WESTON_NUM_LOCK,
1686 WESTON_NUM_LOCK);
1687 }
1688 }
1689
1690 for (i = 1; i < argc; i++)
1691 weston_log("fatal: unhandled option: %s\n", argv[i]);
1692 if (argc > 1)
1693 goto out;
1694
1695 weston_compositor_wake(ec);
1696
1697 wl_display_run(display);
1698
1699 /* Allow for setting return exit code after
1700 * wl_display_run returns normally. This is
1701 * useful for devs/testers and automated tests
1702 * that want to indicate failure status to
1703 * testing infrastructure above
1704 */
1705 ret = ec->exit_code;
1706
1707out:
1708 weston_compositor_destroy(ec);
1709
1710out_signals:
1711 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1712 if (signals[i])
1713 wl_event_source_remove(signals[i]);
1714
1715 wl_display_destroy(display);
1716
1717 weston_log_file_close();
1718
1719 if (config)
1720 weston_config_destroy(config);
1721 free(config_file);
1722 free(backend);
1723 free(shell);
1724 free(socket_name);
1725 free(option_modules);
1726 free(log);
1727 free(modules);
1728
1729 return ret;
1730}