blob: e34f8f575515a3d26fc536cb1888957ac1558467 [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 Camuffobab996e2014-10-12 00:24:25 +030043
44#ifdef HAVE_LIBUNWIND
45#define UNW_LOCAL_ONLY
46#include <libunwind.h>
47#endif
48
49#include "compositor.h"
50#include "../shared/os-compatibility.h"
51#include "../shared/helpers.h"
52#include "git-version.h"
53#include "version.h"
Giulio Camuffofba27fb2016-06-02 21:48:10 +030054#include "weston.h"
Giulio Camuffobab996e2014-10-12 00:24:25 +030055
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070056#include "compositor-drm.h"
Benoit Gschwind3c530942016-04-15 20:28:32 -070057#include "compositor-headless.h"
Benoit Gschwindbd573102016-04-22 17:05:26 +020058#include "compositor-rdp.h"
Benoit Gschwind934e89a2016-04-27 23:56:42 +020059#include "compositor-fbdev.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070060#include "compositor-x11.h"
Benoit Gschwind3ff10da2016-05-10 22:47:49 +020061#include "compositor-wayland.h"
62
63#define WINDOW_TITLE "Weston Compositor"
Benoit Gschwind3c530942016-04-15 20:28:32 -070064
Giulio Camuffobe2b11a2016-06-02 21:48:13 +030065static FILE *weston_logfile = NULL;
66
67static int cached_tm_mday = -1;
68
69static int weston_log_timestamp(void)
70{
71 struct timeval tv;
72 struct tm *brokendown_time;
73 char string[128];
74
75 gettimeofday(&tv, NULL);
76
77 brokendown_time = localtime(&tv.tv_sec);
78 if (brokendown_time == NULL)
79 return fprintf(weston_logfile, "[(NULL)localtime] ");
80
81 if (brokendown_time->tm_mday != cached_tm_mday) {
82 strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
83 fprintf(weston_logfile, "Date: %s\n", string);
84
85 cached_tm_mday = brokendown_time->tm_mday;
86 }
87
88 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
89
90 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
91}
92
93static void
94custom_handler(const char *fmt, va_list arg)
95{
96 weston_log_timestamp();
97 fprintf(weston_logfile, "libwayland: ");
98 vfprintf(weston_logfile, fmt, arg);
99}
100
101static void
102weston_log_file_open(const char *filename)
103{
104 wl_log_set_handler_server(custom_handler);
105
106 if (filename != NULL) {
107 weston_logfile = fopen(filename, "a");
108 if (weston_logfile)
109 os_fd_set_cloexec(fileno(weston_logfile));
110 }
111
112 if (weston_logfile == NULL)
113 weston_logfile = stderr;
114 else
115 setvbuf(weston_logfile, NULL, _IOLBF, 256);
116}
117
118static void
119weston_log_file_close(void)
120{
121 if ((weston_logfile != stderr) && (weston_logfile != NULL))
122 fclose(weston_logfile);
123 weston_logfile = stderr;
124}
125
126static int
127vlog(const char *fmt, va_list ap)
128{
129 int l;
130
131 l = weston_log_timestamp();
132 l += vfprintf(weston_logfile, fmt, ap);
133
134 return l;
135}
136
137static int
138vlog_continue(const char *fmt, va_list argp)
139{
140 return vfprintf(weston_logfile, fmt, argp);
141}
142
Giulio Camuffobab996e2014-10-12 00:24:25 +0300143static struct wl_list child_process_list;
144static struct weston_compositor *segv_compositor;
145
146static int
147sigchld_handler(int signal_number, void *data)
148{
149 struct weston_process *p;
150 int status;
151 pid_t pid;
152
153 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
154 wl_list_for_each(p, &child_process_list, link) {
155 if (p->pid == pid)
156 break;
157 }
158
159 if (&p->link == &child_process_list) {
160 weston_log("unknown child process exited\n");
161 continue;
162 }
163
164 wl_list_remove(&p->link);
165 p->cleanup(p, status);
166 }
167
168 if (pid < 0 && errno != ECHILD)
169 weston_log("waitpid error %m\n");
170
171 return 1;
172}
173
174#ifdef HAVE_LIBUNWIND
175
176static void
177print_backtrace(void)
178{
179 unw_cursor_t cursor;
180 unw_context_t context;
181 unw_word_t off;
182 unw_proc_info_t pip;
183 int ret, i = 0;
184 char procname[256];
185 const char *filename;
186 Dl_info dlinfo;
187
188 pip.unwind_info = NULL;
189 ret = unw_getcontext(&context);
190 if (ret) {
191 weston_log("unw_getcontext: %d\n", ret);
192 return;
193 }
194
195 ret = unw_init_local(&cursor, &context);
196 if (ret) {
197 weston_log("unw_init_local: %d\n", ret);
198 return;
199 }
200
201 ret = unw_step(&cursor);
202 while (ret > 0) {
203 ret = unw_get_proc_info(&cursor, &pip);
204 if (ret) {
205 weston_log("unw_get_proc_info: %d\n", ret);
206 break;
207 }
208
209 ret = unw_get_proc_name(&cursor, procname, 256, &off);
210 if (ret && ret != -UNW_ENOMEM) {
211 if (ret != -UNW_EUNSPEC)
212 weston_log("unw_get_proc_name: %d\n", ret);
213 procname[0] = '?';
214 procname[1] = 0;
215 }
216
217 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
218 *dlinfo.dli_fname)
219 filename = dlinfo.dli_fname;
220 else
221 filename = "?";
222
223 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
224 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
225
226 ret = unw_step(&cursor);
227 if (ret < 0)
228 weston_log("unw_step: %d\n", ret);
229 }
230}
231
232#else
233
234static void
235print_backtrace(void)
236{
237 void *buffer[32];
238 int i, count;
239 Dl_info info;
240
241 count = backtrace(buffer, ARRAY_LENGTH(buffer));
242 for (i = 0; i < count; i++) {
243 dladdr(buffer[i], &info);
244 weston_log(" [%016lx] %s (%s)\n",
245 (long) buffer[i],
246 info.dli_sname ? info.dli_sname : "--",
247 info.dli_fname);
248 }
249}
250
251#endif
252
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300253static void
254child_client_exec(int sockfd, const char *path)
255{
256 int clientfd;
257 char s[32];
258 sigset_t allsigs;
259
260 /* do not give our signal mask to the new process */
261 sigfillset(&allsigs);
262 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
263
264 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
265 if (seteuid(getuid()) == -1) {
266 weston_log("compositor: failed seteuid\n");
267 return;
268 }
269
270 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
271 * non-CLOEXEC fd to pass through exec. */
272 clientfd = dup(sockfd);
273 if (clientfd == -1) {
274 weston_log("compositor: dup failed: %m\n");
275 return;
276 }
277
278 snprintf(s, sizeof s, "%d", clientfd);
279 setenv("WAYLAND_SOCKET", s, 1);
280
281 if (execl(path, path, NULL) < 0)
282 weston_log("compositor: executing '%s' failed: %m\n",
283 path);
284}
285
286WL_EXPORT struct wl_client *
287weston_client_launch(struct weston_compositor *compositor,
288 struct weston_process *proc,
289 const char *path,
290 weston_process_cleanup_func_t cleanup)
291{
292 int sv[2];
293 pid_t pid;
294 struct wl_client *client;
295
296 weston_log("launching '%s'\n", path);
297
298 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
299 weston_log("weston_client_launch: "
300 "socketpair failed while launching '%s': %m\n",
301 path);
302 return NULL;
303 }
304
305 pid = fork();
306 if (pid == -1) {
307 close(sv[0]);
308 close(sv[1]);
309 weston_log("weston_client_launch: "
310 "fork failed while launching '%s': %m\n", path);
311 return NULL;
312 }
313
314 if (pid == 0) {
315 child_client_exec(sv[1], path);
316 _exit(-1);
317 }
318
319 close(sv[1]);
320
321 client = wl_client_create(compositor->wl_display, sv[0]);
322 if (!client) {
323 close(sv[0]);
324 weston_log("weston_client_launch: "
325 "wl_client_create failed while launching '%s'.\n",
326 path);
327 return NULL;
328 }
329
330 proc->pid = pid;
331 proc->cleanup = cleanup;
332 weston_watch_process(proc);
333
334 return client;
335}
336
Giulio Camuffobab996e2014-10-12 00:24:25 +0300337WL_EXPORT void
338weston_watch_process(struct weston_process *process)
339{
340 wl_list_insert(&child_process_list, &process->link);
341}
342
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300343struct process_info {
344 struct weston_process proc;
345 char *path;
346};
347
348static void
349process_handle_sigchld(struct weston_process *process, int status)
350{
351 struct process_info *pinfo =
352 container_of(process, struct process_info, proc);
353
354 /*
355 * There are no guarantees whether this runs before or after
356 * the wl_client destructor.
357 */
358
359 if (WIFEXITED(status)) {
360 weston_log("%s exited with status %d\n", pinfo->path,
361 WEXITSTATUS(status));
362 } else if (WIFSIGNALED(status)) {
363 weston_log("%s died on signal %d\n", pinfo->path,
364 WTERMSIG(status));
365 } else {
366 weston_log("%s disappeared\n", pinfo->path);
367 }
368
369 free(pinfo->path);
370 free(pinfo);
371}
372
373WL_EXPORT struct wl_client *
374weston_client_start(struct weston_compositor *compositor, const char *path)
375{
376 struct process_info *pinfo;
377 struct wl_client *client;
378
379 pinfo = zalloc(sizeof *pinfo);
380 if (!pinfo)
381 return NULL;
382
383 pinfo->path = strdup(path);
384 if (!pinfo->path)
385 goto out_free;
386
387 client = weston_client_launch(compositor, &pinfo->proc, path,
388 process_handle_sigchld);
389 if (!client)
390 goto out_str;
391
392 return client;
393
394out_str:
395 free(pinfo->path);
396
397out_free:
398 free(pinfo);
399
400 return NULL;
401}
402
Giulio Camuffobab996e2014-10-12 00:24:25 +0300403static void
404log_uname(void)
405{
406 struct utsname usys;
407
408 uname(&usys);
409
410 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
411 usys.version, usys.machine);
412}
413
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300414WL_EXPORT struct weston_config *
415wet_get_config(struct weston_compositor *compositor)
416{
417 return weston_compositor_get_user_data(compositor);
418}
419
Giulio Camuffobab996e2014-10-12 00:24:25 +0300420static const char xdg_error_message[] =
421 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
422
423static const char xdg_wrong_message[] =
424 "fatal: environment variable XDG_RUNTIME_DIR\n"
425 "is set to \"%s\", which is not a directory.\n";
426
427static const char xdg_wrong_mode_message[] =
428 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
429 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
430 "and must be owned by the user (current owner is UID %d).\n";
431
432static const char xdg_detail_message[] =
433 "Refer to your distribution on how to get it, or\n"
434 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
435 "on how to implement it.\n";
436
437static void
438verify_xdg_runtime_dir(void)
439{
440 char *dir = getenv("XDG_RUNTIME_DIR");
441 struct stat s;
442
443 if (!dir) {
444 weston_log(xdg_error_message);
445 weston_log_continue(xdg_detail_message);
446 exit(EXIT_FAILURE);
447 }
448
449 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
450 weston_log(xdg_wrong_message, dir);
451 weston_log_continue(xdg_detail_message);
452 exit(EXIT_FAILURE);
453 }
454
455 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
456 weston_log(xdg_wrong_mode_message,
457 dir, s.st_mode & 0777, s.st_uid);
458 weston_log_continue(xdg_detail_message);
459 }
460}
461
462static int
463usage(int error_code)
464{
465 fprintf(stderr,
466 "Usage: weston [OPTIONS]\n\n"
467 "This is weston version " VERSION ", the Wayland reference compositor.\n"
468 "Weston supports multiple backends, and depending on which backend is in use\n"
469 "different options will be accepted.\n\n"
470
471
472 "Core options:\n\n"
473 " --version\t\tPrint weston version\n"
474 " -B, --backend=MODULE\tBackend module, one of\n"
475#if defined(BUILD_DRM_COMPOSITOR)
476 "\t\t\t\tdrm-backend.so\n"
477#endif
478#if defined(BUILD_FBDEV_COMPOSITOR)
479 "\t\t\t\tfbdev-backend.so\n"
480#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300481#if defined(BUILD_HEADLESS_COMPOSITOR)
482 "\t\t\t\theadless-backend.so\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300483#endif
484#if defined(BUILD_RDP_COMPOSITOR)
485 "\t\t\t\trdp-backend.so\n"
486#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300487#if defined(BUILD_WAYLAND_COMPOSITOR)
488 "\t\t\t\twayland-backend.so\n"
489#endif
490#if defined(BUILD_X11_COMPOSITOR)
491 "\t\t\t\tx11-backend.so\n"
492#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300493 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
494 " -S, --socket=NAME\tName of socket to listen on\n"
495 " -i, --idle-time=SECS\tIdle time in seconds\n"
496 " --modules\t\tLoad the comma-separated list of modules\n"
497 " --log=FILE\t\tLog to the given file\n"
498 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
499 " --no-config\t\tDo not read weston.ini\n"
500 " -h, --help\t\tThis help message\n\n");
501
502#if defined(BUILD_DRM_COMPOSITOR)
503 fprintf(stderr,
504 "Options for drm-backend.so:\n\n"
505 " --connector=ID\tBring up only this connector\n"
506 " --seat=SEAT\t\tThe seat that weston should run on\n"
507 " --tty=TTY\t\tThe tty to use\n"
508 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
509 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
510#endif
511
512#if defined(BUILD_FBDEV_COMPOSITOR)
513 fprintf(stderr,
514 "Options for fbdev-backend.so:\n\n"
515 " --tty=TTY\t\tThe tty to use\n"
516 " --device=DEVICE\tThe framebuffer device to use\n"
517 " --use-gl\t\tUse the GL renderer\n\n");
518#endif
519
Dawid Gajownik71f57042015-07-31 17:39:00 -0300520#if defined(BUILD_HEADLESS_COMPOSITOR)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300521 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300522 "Options for headless-backend.so:\n\n"
523 " --width=WIDTH\t\tWidth of memory surface\n"
524 " --height=HEIGHT\tHeight of memory surface\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300525 " --transform=TR\tThe output transformation, TR is one of:\n"
526 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Dawid Gajownik71f57042015-07-31 17:39:00 -0300527 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300528#endif
529
530#if defined(BUILD_RDP_COMPOSITOR)
531 fprintf(stderr,
532 "Options for rdp-backend.so:\n\n"
533 " --width=WIDTH\t\tWidth of desktop\n"
534 " --height=HEIGHT\tHeight of desktop\n"
Dawid Gajownikd99a0502015-07-31 14:49:57 -0300535 " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300536 " --address=ADDR\tThe address to bind\n"
537 " --port=PORT\t\tThe port to listen on\n"
538 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
539 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
540 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
541 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
542 "\n");
543#endif
544
Dawid Gajownik71f57042015-07-31 17:39:00 -0300545#if defined(BUILD_WAYLAND_COMPOSITOR)
546 fprintf(stderr,
547 "Options for wayland-backend.so:\n\n"
548 " --width=WIDTH\t\tWidth of Wayland surface\n"
549 " --height=HEIGHT\tHeight of Wayland surface\n"
550 " --scale=SCALE\t\tScale factor of output\n"
551 " --fullscreen\t\tRun in fullscreen mode\n"
552 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
553 " --output-count=COUNT\tCreate multiple outputs\n"
554 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
555 " --display=DISPLAY\tWayland display to connect to\n\n");
556#endif
557
558#if defined(BUILD_X11_COMPOSITOR)
559 fprintf(stderr,
560 "Options for x11-backend.so:\n\n"
561 " --width=WIDTH\t\tWidth of X window\n"
562 " --height=HEIGHT\tHeight of X window\n"
563 " --scale=SCALE\t\tScale factor of output\n"
564 " --fullscreen\t\tRun in fullscreen mode\n"
565 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
566 " --output-count=COUNT\tCreate multiple outputs\n"
567 " --no-input\t\tDont create input devices\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300568#endif
569
570 exit(error_code);
571}
572
573static int on_term_signal(int signal_number, void *data)
574{
575 struct wl_display *display = data;
576
577 weston_log("caught signal %d\n", signal_number);
578 wl_display_terminate(display);
579
580 return 1;
581}
582
583static void
584on_caught_signal(int s, siginfo_t *siginfo, void *context)
585{
586 /* This signal handler will do a best-effort backtrace, and
587 * then call the backend restore function, which will switch
588 * back to the vt we launched from or ungrab X etc and then
589 * raise SIGTRAP. If we run weston under gdb from X or a
590 * different vt, and tell gdb "handle *s* nostop", this
591 * will allow weston to switch back to gdb on crash and then
592 * gdb will catch the crash with SIGTRAP.*/
593
594 weston_log("caught signal: %d\n", s);
595
596 print_backtrace();
597
598 segv_compositor->backend->restore(segv_compositor);
599
600 raise(SIGTRAP);
601}
602
603static void
604catch_signals(void)
605{
606 struct sigaction action;
607
608 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
609 action.sa_sigaction = on_caught_signal;
610 sigemptyset(&action.sa_mask);
611 sigaction(SIGSEGV, &action, NULL);
612 sigaction(SIGABRT, &action, NULL);
613}
614
615static const char *
616clock_name(clockid_t clk_id)
617{
618 static const char *names[] = {
619 [CLOCK_REALTIME] = "CLOCK_REALTIME",
620 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
621 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
622 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
623 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500624#ifdef CLOCK_BOOTTIME
Giulio Camuffobab996e2014-10-12 00:24:25 +0300625 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500626#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300627 };
628
629 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
630 return "unknown";
631
632 return names[clk_id];
633}
634
635static const struct {
636 uint32_t bit; /* enum weston_capability */
637 const char *desc;
638} capability_strings[] = {
639 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
640 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
641};
642
643static void
644weston_compositor_log_capabilities(struct weston_compositor *compositor)
645{
646 unsigned i;
647 int yes;
648
649 weston_log("Compositor capabilities:\n");
650 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
651 yes = compositor->capabilities & capability_strings[i].bit;
652 weston_log_continue(STAMP_SPACE "%s %s\n",
653 capability_strings[i].desc,
654 yes ? "yes" : "no");
655 }
656
657 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
658 clock_name(compositor->presentation_clock),
659 compositor->presentation_clock);
660}
661
662static void
663handle_primary_client_destroyed(struct wl_listener *listener, void *data)
664{
665 struct wl_client *client = data;
666
667 weston_log("Primary client died. Closing...\n");
668
669 wl_display_terminate(wl_client_get_display(client));
670}
671
672static int
673weston_create_listening_socket(struct wl_display *display, const char *socket_name)
674{
675 if (socket_name) {
676 if (wl_display_add_socket(display, socket_name)) {
677 weston_log("fatal: failed to add socket: %m\n");
678 return -1;
679 }
680 } else {
681 socket_name = wl_display_add_socket_auto(display);
682 if (!socket_name) {
683 weston_log("fatal: failed to add socket: %m\n");
684 return -1;
685 }
686 }
687
688 setenv("WAYLAND_DISPLAY", socket_name, 1);
689
690 return 0;
691}
692
693static int
694load_modules(struct weston_compositor *ec, const char *modules,
695 int *argc, char *argv[])
696{
697 const char *p, *end;
698 char buffer[256];
699 int (*module_init)(struct weston_compositor *ec,
700 int *argc, char *argv[]);
701
702 if (modules == NULL)
703 return 0;
704
705 p = modules;
706 while (*p) {
707 end = strchrnul(p, ',');
708 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
709 module_init = weston_load_module(buffer, "module_init");
710 if (!module_init)
711 return -1;
712 if (module_init(ec, argc, argv) < 0)
713 return -1;
714 p = end;
715 while (*p == ',')
716 p++;
717
718 }
719
720 return 0;
721}
722
723static int
724weston_compositor_init_config(struct weston_compositor *ec,
725 struct weston_config *config)
726{
727 struct xkb_rule_names xkb_names;
728 struct weston_config_section *s;
729 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000730 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300731
732 s = weston_config_get_section(config, "keyboard", NULL, NULL);
733 weston_config_section_get_string(s, "keymap_rules",
734 (char **) &xkb_names.rules, NULL);
735 weston_config_section_get_string(s, "keymap_model",
736 (char **) &xkb_names.model, NULL);
737 weston_config_section_get_string(s, "keymap_layout",
738 (char **) &xkb_names.layout, NULL);
739 weston_config_section_get_string(s, "keymap_variant",
740 (char **) &xkb_names.variant, NULL);
741 weston_config_section_get_string(s, "keymap_options",
742 (char **) &xkb_names.options, NULL);
743
Giulio Camuffo0358af42016-06-02 21:48:08 +0300744 if (weston_compositor_set_xkb_rule_names(ec, &xkb_names) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300745 return -1;
746
747 weston_config_section_get_int(s, "repeat-rate",
748 &ec->kb_repeat_rate, 40);
749 weston_config_section_get_int(s, "repeat-delay",
750 &ec->kb_repeat_delay, 400);
751
Bob Ham91880f12016-01-12 10:21:47 +0000752 weston_config_section_get_bool(s, "vt-switching",
753 &vt_switching, true);
754 ec->vt_switching = vt_switching;
755
Giulio Camuffobab996e2014-10-12 00:24:25 +0300756 s = weston_config_get_section(config, "core", NULL, NULL);
757 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
758 ec->repaint_msec);
759 if (repaint_msec < -10 || repaint_msec > 1000) {
760 weston_log("Invalid repaint_window value in config: %d\n",
761 repaint_msec);
762 } else {
763 ec->repaint_msec = repaint_msec;
764 }
765 weston_log("Output repaint window is %d ms maximum.\n",
766 ec->repaint_msec);
767
768 return 0;
769}
770
771static char *
772weston_choose_default_backend(void)
773{
774 char *backend = NULL;
775
776 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
777 backend = strdup("wayland-backend.so");
778 else if (getenv("DISPLAY"))
779 backend = strdup("x11-backend.so");
780 else
781 backend = strdup(WESTON_NATIVE_BACKEND);
782
783 return backend;
784}
785
786static const struct { const char *name; uint32_t token; } transforms[] = {
787 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
788 { "90", WL_OUTPUT_TRANSFORM_90 },
789 { "180", WL_OUTPUT_TRANSFORM_180 },
790 { "270", WL_OUTPUT_TRANSFORM_270 },
791 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
792 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
793 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
794 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
795};
796
797WL_EXPORT int
798weston_parse_transform(const char *transform, uint32_t *out)
799{
800 unsigned int i;
801
802 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
803 if (strcmp(transforms[i].name, transform) == 0) {
804 *out = transforms[i].token;
805 return 0;
806 }
807
808 *out = WL_OUTPUT_TRANSFORM_NORMAL;
809 return -1;
810}
811
812WL_EXPORT const char *
813weston_transform_to_string(uint32_t output_transform)
814{
815 unsigned int i;
816
817 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
818 if (transforms[i].token == output_transform)
819 return transforms[i].name;
820
821 return "<illegal value>";
822}
823
824static int
825load_configuration(struct weston_config **config, int32_t noconfig,
826 const char *config_file)
827{
828 const char *file = "weston.ini";
829 const char *full_path;
830
831 *config = NULL;
832
833 if (config_file)
834 file = config_file;
835
836 if (noconfig == 0)
837 *config = weston_config_parse(file);
838
839 if (*config) {
840 full_path = weston_config_get_full_path(*config);
841
842 weston_log("Using config file '%s'\n", full_path);
843 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
844
845 return 0;
846 }
847
848 if (config_file && noconfig == 0) {
849 weston_log("fatal: error opening or reading config file"
850 " '%s'.\n", config_file);
851
852 return -1;
853 }
854
855 weston_log("Starting with no config file.\n");
856 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
857
858 return 0;
859}
860
861static void
862handle_exit(struct weston_compositor *c)
863{
864 wl_display_terminate(c->wl_display);
865}
866
Giulio Camuffo43008c72015-10-17 19:24:15 +0300867/* Temporary function to be removed when all backends are converted. */
868static int
869load_backend_old(struct weston_compositor *compositor, const char *backend,
870 int *argc, char **argv, struct weston_config *wc)
871{
872 int (*backend_init)(struct weston_compositor *c,
873 int *argc, char *argv[],
874 struct weston_config *config,
875 struct weston_backend_config *config_base);
876
877 backend_init = weston_load_module(backend, "backend_init");
878 if (!backend_init)
879 return -1;
880
881 return backend_init(compositor, argc, argv, wc, NULL);
882}
883
Bryce Harrington98ab08b2016-04-15 20:28:36 -0700884/** Main module call-point for backends.
885 *
886 * All backends should use this routine to access their init routine.
887 * Backends may subclass weston_backend_config to add their own
888 * configuration data, setting the major/minor version in config_base
889 * accordingly.
890 *
891 * The config_base object should be treated as temporary, and any data
892 * copied out of it by backend_init before returning. The load_backend_new
893 * callers may then free the config_base object.
894 *
895 * NOTE: This is a temporary function intended to eventually be replaced
896 * by weston_compositor_load_backend().
897 */
Bryce Harrington6ca25b32016-04-15 20:28:27 -0700898static int
899load_backend_new(struct weston_compositor *compositor, const char *backend,
900 struct weston_backend_config *config_base)
901{
902 int (*backend_init)(struct weston_compositor *c,
903 int *argc, char *argv[],
904 struct weston_config *config,
905 struct weston_backend_config *config_base);
906
907 backend_init = weston_load_module(backend, "backend_init");
908 if (!backend_init)
909 return -1;
910
911 return backend_init(compositor, NULL, NULL, NULL, config_base);
912}
913
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700914static enum weston_drm_backend_output_mode
915drm_configure_output(struct weston_compositor *c,
916 bool use_current_mode,
917 const char *name,
918 struct weston_drm_backend_output_config *config)
919{
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300920 struct weston_config *wc = wet_get_config(c);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700921 struct weston_config_section *section;
922 char *s;
923 int scale;
924 enum weston_drm_backend_output_mode mode =
925 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
926
927 section = weston_config_get_section(wc, "output", "name", name);
928 weston_config_section_get_string(section, "mode", &s, "preferred");
929 if (strcmp(s, "off") == 0) {
930 free(s);
931 return WESTON_DRM_BACKEND_OUTPUT_OFF;
932 }
933
934 if (use_current_mode || strcmp(s, "current") == 0) {
935 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
936 } else if (strcmp(s, "preferred") != 0) {
937 config->modeline = s;
938 s = NULL;
939 }
940 free(s);
941
942 weston_config_section_get_int(section, "scale", &scale, 1);
943 config->base.scale = scale >= 1 ? scale : 1;
944 weston_config_section_get_string(section, "transform", &s, "normal");
945 if (weston_parse_transform(s, &config->base.transform) < 0)
946 weston_log("Invalid transform \"%s\" for output %s\n",
947 s, name);
948 free(s);
949
950 weston_config_section_get_string(section,
951 "gbm-format", &config->gbm_format, NULL);
952 weston_config_section_get_string(section, "seat", &config->seat, "");
953 return mode;
954}
955
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +0300956static void
957configure_input_device(struct weston_compositor *compositor,
958 struct libinput_device *device)
959{
960 struct weston_config_section *s;
961 struct weston_config *config = wet_get_config(compositor);
962 int enable_tap;
963 int enable_tap_default;
964
965 s = weston_config_get_section(config,
966 "libinput", NULL, NULL);
967
968 if (libinput_device_config_tap_get_finger_count(device) > 0) {
969 enable_tap_default =
970 libinput_device_config_tap_get_default_enabled(
971 device);
972 weston_config_section_get_bool(s, "enable_tap",
973 &enable_tap,
974 enable_tap_default);
975 libinput_device_config_tap_set_enabled(device,
976 enable_tap);
977 }
978}
979
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700980static int
981load_drm_backend(struct weston_compositor *c, const char *backend,
982 int *argc, char **argv, struct weston_config *wc)
983{
984 struct weston_drm_backend_config config = {{ 0, }};
985 struct weston_config_section *section;
986 int ret = 0;
987
988 const struct weston_option options[] = {
989 { WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
990 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
991 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
992 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
993 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
994 };
995
996 parse_options(options, ARRAY_LENGTH(options), argc, argv);
997
998 section = weston_config_get_section(wc, "core", NULL, NULL);
999 weston_config_section_get_string(section,
1000 "gbm-format", &config.gbm_format,
1001 NULL);
1002
1003 config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
1004 config.base.struct_size = sizeof(struct weston_drm_backend_config);
1005 config.configure_output = drm_configure_output;
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001006 config.configure_device = configure_input_device;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001007
1008 ret = load_backend_new(c, backend, &config.base);
1009
1010 free(config.gbm_format);
1011 free(config.seat_id);
1012
1013 return ret;
1014}
1015
Giulio Camuffo43008c72015-10-17 19:24:15 +03001016static int
Benoit Gschwind3c530942016-04-15 20:28:32 -07001017load_headless_backend(struct weston_compositor *c, char const * backend,
1018 int *argc, char **argv, struct weston_config *wc)
1019{
1020 struct weston_headless_backend_config config = {{ 0, }};
1021 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001022 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001023
1024 config.width = 1024;
1025 config.height = 640;
1026
1027 const struct weston_option options[] = {
1028 { WESTON_OPTION_INTEGER, "width", 0, &config.width },
1029 { WESTON_OPTION_INTEGER, "height", 0, &config.height },
1030 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1031 { WESTON_OPTION_STRING, "transform", 0, &transform },
1032 };
1033
1034 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1035
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001036 config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1037 if (transform) {
1038 if (weston_parse_transform(transform, &config.transform) < 0)
1039 weston_log("Invalid transform \"%s\"\n", transform);
1040 free(transform);
1041 }
Benoit Gschwind3c530942016-04-15 20:28:32 -07001042
1043 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1044 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1045
1046 /* load the actual wayland backend and configure it */
1047 ret = load_backend_new(c, backend, &config.base);
1048
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
1070load_rdp_backend(struct weston_compositor *c, char const * backend,
1071 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
1092 ret = load_backend_new(c, backend, &config.base);
1093
1094 free(config.bind_address);
1095 free(config.rdp_key);
1096 free(config.server_cert);
1097 free(config.server_key);
1098 return ret;
1099}
1100
Benoit Gschwind3c530942016-04-15 20:28:32 -07001101static int
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001102load_fbdev_backend(struct weston_compositor *c, char const * backend,
1103 int *argc, char **argv, struct weston_config *wc)
1104{
1105 struct weston_fbdev_backend_config config = {{ 0, }};
1106 struct weston_config_section *section;
1107 char *s = NULL;
1108 int ret = 0;
1109
1110 const struct weston_option fbdev_options[] = {
1111 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1112 { WESTON_OPTION_STRING, "device", 0, &config.device },
1113 { WESTON_OPTION_BOOLEAN, "use-gl", 0, &config.use_gl },
1114 };
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 */
1132 ret = load_backend_new(c, backend, &config.base);
1133
1134 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001135
1136 return ret;
1137}
1138
1139static int
1140weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
1141 struct weston_x11_backend_output_config *output_config) {
1142 struct weston_x11_backend_output_config *new_outputs;
1143
1144 new_outputs = realloc(config->outputs, (config->num_outputs+1) *
1145 sizeof(struct weston_x11_backend_output_config));
1146 if (new_outputs == NULL)
1147 return -1;
1148
1149 config->outputs = new_outputs;
1150 config->outputs[config->num_outputs].width = output_config->width;
1151 config->outputs[config->num_outputs].height = output_config->height;
1152 config->outputs[config->num_outputs].transform = output_config->transform;
1153 config->outputs[config->num_outputs].scale = output_config->scale;
1154 config->outputs[config->num_outputs].name = strdup(output_config->name);
1155 config->num_outputs++;
1156
1157 return 0;
1158}
1159
1160static int
1161load_x11_backend(struct weston_compositor *c, char const * backend,
1162 int *argc, char **argv, struct weston_config *wc)
1163{
1164 struct weston_x11_backend_output_config default_output;
1165 struct weston_x11_backend_config config = {{ 0, }};
1166 struct weston_config_section *section;
1167 int ret = 0;
1168 int option_width = 0;
1169 int option_height = 0;
1170 int option_scale = 0;
1171 int option_count = 1;
1172 int output_count = 0;
1173 char const *section_name;
1174 int i;
1175 uint32_t j;
1176
1177 const struct weston_option options[] = {
1178 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1179 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1180 { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
1181 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
1182 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1183 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
1184 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1185 };
1186
1187 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1188
1189 section = NULL;
1190 while (weston_config_next_section(wc, &section, &section_name)) {
1191 struct weston_x11_backend_output_config current_output = { 0, };
1192 char *t;
1193 char *mode;
1194
1195 if (strcmp(section_name, "output") != 0) {
1196 continue;
1197 }
1198
1199 weston_config_section_get_string(section, "name", &current_output.name, NULL);
1200 if (current_output.name == NULL || current_output.name[0] != 'X') {
1201 free(current_output.name);
1202 continue;
1203 }
1204
1205 weston_config_section_get_string(section, "mode", &mode, "1024x600");
1206 if (sscanf(mode, "%dx%d", &current_output.width,
1207 &current_output.height) != 2) {
1208 weston_log("Invalid mode \"%s\" for output %s\n",
1209 mode, current_output.name);
1210 current_output.width = 1024;
1211 current_output.height = 600;
1212 }
1213 free(mode);
1214 if (current_output.width < 1)
1215 current_output.width = 1024;
1216 if (current_output.height < 1)
1217 current_output.height = 600;
1218 if (option_width)
1219 current_output.width = option_width;
1220 if (option_height)
1221 current_output.height = option_height;
1222
1223 weston_config_section_get_int(section, "scale", &current_output.scale, 1);
1224 if (option_scale)
1225 current_output.scale = option_scale;
1226
1227 weston_config_section_get_string(section,
1228 "transform", &t, "normal");
1229 if (weston_parse_transform(t, &current_output.transform) < 0)
1230 weston_log("Invalid transform \"%s\" for output %s\n",
1231 t, current_output.name);
1232 free(t);
1233
1234 if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
1235 ret = -1;
1236 goto out;
1237 }
1238
1239 output_count++;
Bryce Harringtonaa258982016-05-03 01:34:23 -07001240 if (output_count >= option_count)
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001241 break;
1242 }
1243
1244 default_output.name = NULL;
1245 default_output.width = option_width ? option_width : 1024;
1246 default_output.height = option_height ? option_height : 600;
1247 default_output.scale = option_scale ? option_scale : 1;
1248 default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
1249
1250 for (i = output_count; i < option_count; i++) {
1251 if (asprintf(&default_output.name, "screen%d", i) < 0) {
1252 ret = -1;
1253 goto out;
1254 }
1255
1256 if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
1257 ret = -1;
1258 free(default_output.name);
1259 goto out;
1260 }
1261 free(default_output.name);
1262 }
1263
1264 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
1265 config.base.struct_size = sizeof(struct weston_x11_backend_config);
1266
1267 /* load the actual backend and configure it */
1268 ret = load_backend_new(c, backend, &config.base);
1269
1270out:
1271 for (j = 0; j < config.num_outputs; ++j)
1272 free(config.outputs[j].name);
1273 free(config.outputs);
1274
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001275 return ret;
1276}
1277
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001278static void
1279weston_wayland_output_config_init(struct weston_wayland_backend_output_config *output_config,
1280 struct weston_config_section *config_section,
1281 int option_width, int option_height,
1282 int option_scale)
1283{
1284 char *mode, *t, *str;
1285 unsigned int slen;
1286
1287 weston_config_section_get_string(config_section, "name", &output_config->name,
1288 NULL);
1289 if (output_config->name) {
1290 slen = strlen(output_config->name);
1291 slen += strlen(WINDOW_TITLE " - ");
1292 str = malloc(slen + 1);
1293 if (str)
1294 snprintf(str, slen + 1, WINDOW_TITLE " - %s",
1295 output_config->name);
1296 free(output_config->name);
1297 output_config->name = str;
1298 }
1299 if (!output_config->name)
1300 output_config->name = strdup(WINDOW_TITLE);
1301
1302 weston_config_section_get_string(config_section,
1303 "mode", &mode, "1024x600");
1304 if (sscanf(mode, "%dx%d", &output_config->width, &output_config->height) != 2) {
1305 weston_log("Invalid mode \"%s\" for output %s\n",
1306 mode, output_config->name);
1307 output_config->width = 1024;
1308 output_config->height = 640;
1309 }
1310 free(mode);
1311
1312 if (option_width)
1313 output_config->width = option_width;
1314 if (option_height)
1315 output_config->height = option_height;
1316
1317 weston_config_section_get_int(config_section, "scale", &output_config->scale, 1);
1318
1319 if (option_scale)
1320 output_config->scale = option_scale;
1321
1322 weston_config_section_get_string(config_section,
1323 "transform", &t, "normal");
1324 if (weston_parse_transform(t, &output_config->transform) < 0)
1325 weston_log("Invalid transform \"%s\" for output %s\n",
1326 t, output_config->name);
1327 free(t);
1328
1329}
1330
1331static void
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001332weston_wayland_backend_config_release(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001333{
1334 int i;
1335
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001336 for (i = 0; i < config->num_outputs; ++i) {
1337 free(config->outputs[i].name);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001338 }
Benoit Gschwinde48ab5f2016-05-10 22:47:55 +02001339 free(config->cursor_theme);
1340 free(config->display_name);
1341 free(config->outputs);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001342}
1343
1344/*
1345 * Append a new output struct at the end of new_config.outputs and return a
1346 * pointer to the newly allocated structure or NULL if fail. The allocated
1347 * structure is NOT cleared nor set to default values.
1348 */
1349static struct weston_wayland_backend_output_config *
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001350weston_wayland_backend_config_add_new_output(struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001351{
1352 struct weston_wayland_backend_output_config *outputs;
1353 const size_t element_size = sizeof(struct weston_wayland_backend_output_config);
1354
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001355 outputs = realloc(config->outputs,
1356 (config->num_outputs + 1) * element_size);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001357 if (!outputs)
1358 return NULL;
Benoit Gschwind44e302b2016-05-10 22:47:56 +02001359 config->num_outputs += 1;
1360 config->outputs = outputs;
1361 return &(config->outputs[config->num_outputs - 1]);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001362}
1363
1364static int
1365load_wayland_backend_config(struct weston_compositor *compositor, int *argc,
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001366 char *argv[], struct weston_config *wc,
Benoit Gschwind55a22882016-05-10 22:47:51 +02001367 struct weston_wayland_backend_config *config)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001368{
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001369 struct weston_config_section *section;
1370 struct weston_wayland_backend_output_config *oc;
1371 int count, width, height, scale;
1372 const char *section_name;
1373 char *name;
1374
1375 const struct weston_option wayland_options[] = {
1376 { WESTON_OPTION_INTEGER, "width", 0, &width },
1377 { WESTON_OPTION_INTEGER, "height", 0, &height },
1378 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001379 { WESTON_OPTION_STRING, "display", 0, &config->display_name },
1380 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config->use_pixman },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001381 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Benoit Gschwind55a22882016-05-10 22:47:51 +02001382 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config->fullscreen },
1383 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &config->sprawl },
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001384 };
1385
1386 width = 0;
1387 height = 0;
1388 scale = 0;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001389 config->display_name = NULL;
1390 config->use_pixman = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001391 count = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001392 config->fullscreen = 0;
1393 config->sprawl = 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001394 parse_options(wayland_options,
1395 ARRAY_LENGTH(wayland_options), argc, argv);
1396
Benoit Gschwind55a22882016-05-10 22:47:51 +02001397 config->cursor_size = 32;
1398 config->cursor_theme = NULL;
1399 config->base.struct_size = sizeof(struct weston_wayland_backend_config);
1400 config->base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001401
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001402 section = weston_config_get_section(wc, "shell", NULL, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001403 weston_config_section_get_string(section, "cursor-theme",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001404 &config->cursor_theme, NULL);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001405 weston_config_section_get_int(section, "cursor-size",
Benoit Gschwind55a22882016-05-10 22:47:51 +02001406 &config->cursor_size, 32);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001407
Benoit Gschwind55a22882016-05-10 22:47:51 +02001408 if (config->sprawl) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001409 /* do nothing, everything is already set */
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001410 return 0;
1411 }
1412
Benoit Gschwind55a22882016-05-10 22:47:51 +02001413 if (config->fullscreen) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001414 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001415 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001416 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001417
1418 oc->width = width;
1419 oc->height = height;
1420 oc->name = NULL;
1421 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1422 oc->scale = 1;
1423
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001424 return 0;
1425 }
1426
1427 section = NULL;
Benoit Gschwind6c1cd2f2016-05-10 22:47:50 +02001428 while (weston_config_next_section(wc, &section, &section_name)) {
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001429 if (!section_name || strcmp(section_name, "output") != 0)
1430 continue;
1431 weston_config_section_get_string(section, "name", &name, NULL);
1432 if (name == NULL)
1433 continue;
1434
1435 if (name[0] != 'W' || name[1] != 'L') {
1436 free(name);
1437 continue;
1438 }
1439 free(name);
1440
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001441 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001442 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001443 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001444
1445 weston_wayland_output_config_init(oc, section, width,
1446 height, scale);
1447 --count;
1448 }
1449
1450 if (!width)
1451 width = 1024;
1452 if (!height)
1453 height = 640;
1454 if (!scale)
1455 scale = 1;
Benoit Gschwind55a22882016-05-10 22:47:51 +02001456
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001457 while (count > 0) {
Benoit Gschwind390af6d2016-05-10 22:47:52 +02001458 oc = weston_wayland_backend_config_add_new_output(config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001459 if (!oc)
Benoit Gschwind53753842016-05-10 22:47:57 +02001460 return -1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001461
1462 oc->width = width;
1463 oc->height = height;
1464 oc->name = NULL;
1465 oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1466 oc->scale = scale;
1467
1468 --count;
1469 }
1470
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001471 return 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001472}
1473
1474static int
1475load_wayland_backend(struct weston_compositor *c, char const * backend,
1476 int *argc, char **argv, struct weston_config *wc)
1477{
1478 struct weston_wayland_backend_config config = {{ 0, }};
1479 int ret = 0;
1480
1481 ret = load_wayland_backend_config(c, argc, argv, wc, &config);
1482 if(ret < 0) {
Benoit Gschwind53753842016-05-10 22:47:57 +02001483 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001484 return ret;
1485 }
1486
1487 /* load the actual wayland backend and configure it */
1488 ret = load_backend_new(c, backend, &config.base);
Benoit Gschwind68d6a6c2016-05-10 22:47:53 +02001489 weston_wayland_backend_config_release(&config);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001490 return ret;
1491}
1492
1493
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001494static int
Giulio Camuffo43008c72015-10-17 19:24:15 +03001495load_backend(struct weston_compositor *compositor, const char *backend,
1496 int *argc, char **argv, struct weston_config *config)
1497{
Benoit Gschwind3c530942016-04-15 20:28:32 -07001498 if (strstr(backend, "headless-backend.so"))
1499 return load_headless_backend(compositor, backend, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001500 else if (strstr(backend, "rdp-backend.so"))
1501 return load_rdp_backend(compositor, backend, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001502 else if (strstr(backend, "fbdev-backend.so"))
1503 return load_fbdev_backend(compositor, backend, argc, argv, config);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001504 else if (strstr(backend, "drm-backend.so"))
1505 return load_drm_backend(compositor, backend, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001506 else if (strstr(backend, "x11-backend.so"))
1507 return load_x11_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001508 else if (strstr(backend, "wayland-backend.so"))
1509 return load_wayland_backend(compositor, backend, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001510
1511 return load_backend_old(compositor, backend, argc, argv, config);
1512}
1513
Giulio Camuffobab996e2014-10-12 00:24:25 +03001514int main(int argc, char *argv[])
1515{
1516 int ret = EXIT_FAILURE;
1517 struct wl_display *display;
1518 struct weston_compositor *ec;
1519 struct wl_event_source *signals[4];
1520 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001521 int i, fd;
1522 char *backend = NULL;
1523 char *shell = NULL;
1524 char *modules = NULL;
1525 char *option_modules = NULL;
1526 char *log = NULL;
1527 char *server_socket = NULL, *end;
1528 int32_t idle_time = -1;
1529 int32_t help = 0;
1530 char *socket_name = NULL;
1531 int32_t version = 0;
1532 int32_t noconfig = 0;
1533 int32_t numlock_on;
1534 char *config_file = NULL;
1535 struct weston_config *config = NULL;
1536 struct weston_config_section *section;
1537 struct wl_client *primary_client;
1538 struct wl_listener primary_client_destroyed;
1539 struct weston_seat *seat;
1540
1541 const struct weston_option core_options[] = {
1542 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1543 { WESTON_OPTION_STRING, "shell", 0, &shell },
1544 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1545 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
1546 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1547 { WESTON_OPTION_STRING, "log", 0, &log },
1548 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1549 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1550 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1551 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1552 };
1553
1554 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1555
1556 if (help)
1557 usage(EXIT_SUCCESS);
1558
1559 if (version) {
1560 printf(PACKAGE_STRING "\n");
1561 return EXIT_SUCCESS;
1562 }
1563
Giulio Camuffobe2b11a2016-06-02 21:48:13 +03001564 weston_log_set_handler(vlog, vlog_continue);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001565 weston_log_file_open(log);
1566
1567 weston_log("%s\n"
1568 STAMP_SPACE "%s\n"
1569 STAMP_SPACE "Bug reports to: %s\n"
1570 STAMP_SPACE "Build: %s\n",
1571 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1572 BUILD_ID);
1573 log_uname();
1574
1575 verify_xdg_runtime_dir();
1576
1577 display = wl_display_create();
1578
1579 loop = wl_display_get_event_loop(display);
1580 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1581 display);
1582 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1583 display);
1584 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1585 display);
1586
1587 wl_list_init(&child_process_list);
1588 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1589 NULL);
1590
1591 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1592 goto out_signals;
1593
1594 if (load_configuration(&config, noconfig, config_file) < 0)
1595 goto out_signals;
1596
1597 section = weston_config_get_section(config, "core", NULL, NULL);
1598
1599 if (!backend) {
1600 weston_config_section_get_string(section, "backend", &backend,
1601 NULL);
1602 if (!backend)
1603 backend = weston_choose_default_backend();
1604 }
1605
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001606 ec = weston_compositor_create(display, config);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001607 if (ec == NULL) {
1608 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001609 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001610 }
1611
Giulio Camuffobab996e2014-10-12 00:24:25 +03001612 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001613 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001614
Giulio Camuffo43008c72015-10-17 19:24:15 +03001615 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001616 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001617 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001618 }
1619
1620 catch_signals();
1621 segv_compositor = ec;
1622
1623 if (idle_time < 0)
1624 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1625 if (idle_time < 0)
1626 idle_time = 300; /* default idle timeout, in seconds */
1627
1628 ec->idle_time = idle_time;
1629 ec->default_pointer_grab = NULL;
1630 ec->exit = handle_exit;
1631
1632 weston_compositor_log_capabilities(ec);
1633
1634 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1635 if (server_socket) {
1636 weston_log("Running with single client\n");
1637 fd = strtol(server_socket, &end, 0);
1638 if (*end != '\0')
1639 fd = -1;
1640 } else {
1641 fd = -1;
1642 }
1643
1644 if (fd != -1) {
1645 primary_client = wl_client_create(display, fd);
1646 if (!primary_client) {
1647 weston_log("fatal: failed to add client: %m\n");
1648 goto out;
1649 }
1650 primary_client_destroyed.notify =
1651 handle_primary_client_destroyed;
1652 wl_client_add_destroy_listener(primary_client,
1653 &primary_client_destroyed);
1654 } else if (weston_create_listening_socket(display, socket_name)) {
1655 goto out;
1656 }
1657
1658 if (!shell)
1659 weston_config_section_get_string(section, "shell", &shell,
1660 "desktop-shell.so");
1661
1662 if (load_modules(ec, shell, &argc, argv) < 0)
1663 goto out;
1664
1665 weston_config_section_get_string(section, "modules", &modules, "");
1666 if (load_modules(ec, modules, &argc, argv) < 0)
1667 goto out;
1668
1669 if (load_modules(ec, option_modules, &argc, argv) < 0)
1670 goto out;
1671
1672 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1673 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1674 if (numlock_on) {
1675 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001676 struct weston_keyboard *keyboard =
1677 weston_seat_get_keyboard(seat);
1678
1679 if (keyboard)
1680 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001681 WESTON_NUM_LOCK,
1682 WESTON_NUM_LOCK);
1683 }
1684 }
1685
1686 for (i = 1; i < argc; i++)
1687 weston_log("fatal: unhandled option: %s\n", argv[i]);
1688 if (argc > 1)
1689 goto out;
1690
1691 weston_compositor_wake(ec);
1692
1693 wl_display_run(display);
1694
1695 /* Allow for setting return exit code after
1696 * wl_display_run returns normally. This is
1697 * useful for devs/testers and automated tests
1698 * that want to indicate failure status to
1699 * testing infrastructure above
1700 */
1701 ret = ec->exit_code;
1702
1703out:
1704 weston_compositor_destroy(ec);
1705
1706out_signals:
1707 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1708 if (signals[i])
1709 wl_event_source_remove(signals[i]);
1710
1711 wl_display_destroy(display);
1712
1713 weston_log_file_close();
1714
1715 if (config)
1716 weston_config_destroy(config);
1717 free(config_file);
1718 free(backend);
1719 free(shell);
1720 free(socket_name);
1721 free(option_modules);
1722 free(log);
1723 free(modules);
1724
1725 return ret;
1726}