blob: f8a60e976c216789e3c1fd2e8e7d8f49c9afe9c0 [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030036#include <stdint.h>
Giulio Camuffobab996e2014-10-12 00:24:25 +030037#include <string.h>
38#include <sys/utsname.h>
39#include <sys/stat.h>
40#include <sys/wait.h>
Giulio Camuffofba27fb2016-06-02 21:48:10 +030041#include <sys/socket.h>
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +030042#include <libinput.h>
Giulio Camuffobe2b11a2016-06-02 21:48:13 +030043#include <sys/time.h>
Giulio Camuffo179fcda2016-06-02 21:48:14 +030044#include <linux/limits.h>
Giulio Camuffobab996e2014-10-12 00:24:25 +030045
46#ifdef HAVE_LIBUNWIND
47#define UNW_LOCAL_ONLY
48#include <libunwind.h>
49#endif
50
Giulio Camuffo179fcda2016-06-02 21:48:14 +030051#include "weston.h"
Giulio Camuffobab996e2014-10-12 00:24:25 +030052#include "compositor.h"
53#include "../shared/os-compatibility.h"
54#include "../shared/helpers.h"
Bryce Harrington25a2bdd2016-08-03 17:40:52 -070055#include "../shared/string-helpers.h"
Giulio Camuffobab996e2014-10-12 00:24:25 +030056#include "git-version.h"
57#include "version.h"
Giulio Camuffofba27fb2016-06-02 21:48:10 +030058#include "weston.h"
Giulio Camuffobab996e2014-10-12 00:24:25 +030059
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070060#include "compositor-drm.h"
Benoit Gschwind3c530942016-04-15 20:28:32 -070061#include "compositor-headless.h"
Benoit Gschwindbd573102016-04-22 17:05:26 +020062#include "compositor-rdp.h"
Benoit Gschwind934e89a2016-04-27 23:56:42 +020063#include "compositor-fbdev.h"
Benoit Gschwinde16acab2016-04-15 20:28:31 -070064#include "compositor-x11.h"
Benoit Gschwind3ff10da2016-05-10 22:47:49 +020065#include "compositor-wayland.h"
Armin Krezovića3666d22016-09-30 14:11:04 +020066#include "windowed-output-api.h"
Benoit Gschwind3ff10da2016-05-10 22:47:49 +020067
68#define WINDOW_TITLE "Weston Compositor"
Benoit Gschwind3c530942016-04-15 20:28:32 -070069
Armin Krezovića3666d22016-09-30 14:11:04 +020070struct wet_output_config {
71 int width;
72 int height;
73 int32_t scale;
74 uint32_t transform;
75};
76
Armin Krezović78a36372016-08-01 18:51:46 +020077struct wet_compositor {
78 struct weston_config *config;
Armin Krezovića3666d22016-09-30 14:11:04 +020079 struct wet_output_config *parsed_options;
80 struct wl_listener pending_output_listener;
Armin Krezović605ac8e2016-10-09 23:48:17 +020081 bool drm_use_current_mode;
Armin Krezović78a36372016-08-01 18:51:46 +020082};
83
Giulio Camuffobe2b11a2016-06-02 21:48:13 +030084static FILE *weston_logfile = NULL;
85
86static int cached_tm_mday = -1;
87
88static int weston_log_timestamp(void)
89{
90 struct timeval tv;
91 struct tm *brokendown_time;
92 char string[128];
93
94 gettimeofday(&tv, NULL);
95
96 brokendown_time = localtime(&tv.tv_sec);
97 if (brokendown_time == NULL)
98 return fprintf(weston_logfile, "[(NULL)localtime] ");
99
100 if (brokendown_time->tm_mday != cached_tm_mday) {
101 strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
102 fprintf(weston_logfile, "Date: %s\n", string);
103
104 cached_tm_mday = brokendown_time->tm_mday;
105 }
106
107 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
108
109 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
110}
111
112static void
113custom_handler(const char *fmt, va_list arg)
114{
115 weston_log_timestamp();
116 fprintf(weston_logfile, "libwayland: ");
117 vfprintf(weston_logfile, fmt, arg);
118}
119
120static void
121weston_log_file_open(const char *filename)
122{
123 wl_log_set_handler_server(custom_handler);
124
125 if (filename != NULL) {
126 weston_logfile = fopen(filename, "a");
127 if (weston_logfile)
128 os_fd_set_cloexec(fileno(weston_logfile));
129 }
130
131 if (weston_logfile == NULL)
132 weston_logfile = stderr;
133 else
134 setvbuf(weston_logfile, NULL, _IOLBF, 256);
135}
136
137static void
138weston_log_file_close(void)
139{
140 if ((weston_logfile != stderr) && (weston_logfile != NULL))
141 fclose(weston_logfile);
142 weston_logfile = stderr;
143}
144
145static int
146vlog(const char *fmt, va_list ap)
147{
148 int l;
149
150 l = weston_log_timestamp();
151 l += vfprintf(weston_logfile, fmt, ap);
152
153 return l;
154}
155
156static int
157vlog_continue(const char *fmt, va_list argp)
158{
159 return vfprintf(weston_logfile, fmt, argp);
160}
161
Giulio Camuffobab996e2014-10-12 00:24:25 +0300162static struct wl_list child_process_list;
163static struct weston_compositor *segv_compositor;
164
165static int
166sigchld_handler(int signal_number, void *data)
167{
168 struct weston_process *p;
169 int status;
170 pid_t pid;
171
172 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
173 wl_list_for_each(p, &child_process_list, link) {
174 if (p->pid == pid)
175 break;
176 }
177
178 if (&p->link == &child_process_list) {
179 weston_log("unknown child process exited\n");
180 continue;
181 }
182
183 wl_list_remove(&p->link);
184 p->cleanup(p, status);
185 }
186
187 if (pid < 0 && errno != ECHILD)
188 weston_log("waitpid error %m\n");
189
190 return 1;
191}
192
193#ifdef HAVE_LIBUNWIND
194
195static void
196print_backtrace(void)
197{
198 unw_cursor_t cursor;
199 unw_context_t context;
200 unw_word_t off;
201 unw_proc_info_t pip;
202 int ret, i = 0;
203 char procname[256];
204 const char *filename;
205 Dl_info dlinfo;
206
207 pip.unwind_info = NULL;
208 ret = unw_getcontext(&context);
209 if (ret) {
210 weston_log("unw_getcontext: %d\n", ret);
211 return;
212 }
213
214 ret = unw_init_local(&cursor, &context);
215 if (ret) {
216 weston_log("unw_init_local: %d\n", ret);
217 return;
218 }
219
220 ret = unw_step(&cursor);
221 while (ret > 0) {
222 ret = unw_get_proc_info(&cursor, &pip);
223 if (ret) {
224 weston_log("unw_get_proc_info: %d\n", ret);
225 break;
226 }
227
228 ret = unw_get_proc_name(&cursor, procname, 256, &off);
229 if (ret && ret != -UNW_ENOMEM) {
230 if (ret != -UNW_EUNSPEC)
231 weston_log("unw_get_proc_name: %d\n", ret);
232 procname[0] = '?';
233 procname[1] = 0;
234 }
235
236 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
237 *dlinfo.dli_fname)
238 filename = dlinfo.dli_fname;
239 else
240 filename = "?";
241
242 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
243 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
244
245 ret = unw_step(&cursor);
246 if (ret < 0)
247 weston_log("unw_step: %d\n", ret);
248 }
249}
250
251#else
252
253static void
254print_backtrace(void)
255{
256 void *buffer[32];
257 int i, count;
258 Dl_info info;
259
260 count = backtrace(buffer, ARRAY_LENGTH(buffer));
261 for (i = 0; i < count; i++) {
262 dladdr(buffer[i], &info);
263 weston_log(" [%016lx] %s (%s)\n",
264 (long) buffer[i],
265 info.dli_sname ? info.dli_sname : "--",
266 info.dli_fname);
267 }
268}
269
270#endif
271
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300272static void
273child_client_exec(int sockfd, const char *path)
274{
275 int clientfd;
276 char s[32];
277 sigset_t allsigs;
278
279 /* do not give our signal mask to the new process */
280 sigfillset(&allsigs);
281 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
282
283 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
284 if (seteuid(getuid()) == -1) {
285 weston_log("compositor: failed seteuid\n");
286 return;
287 }
288
289 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
290 * non-CLOEXEC fd to pass through exec. */
291 clientfd = dup(sockfd);
292 if (clientfd == -1) {
293 weston_log("compositor: dup failed: %m\n");
294 return;
295 }
296
297 snprintf(s, sizeof s, "%d", clientfd);
298 setenv("WAYLAND_SOCKET", s, 1);
299
300 if (execl(path, path, NULL) < 0)
301 weston_log("compositor: executing '%s' failed: %m\n",
302 path);
303}
304
305WL_EXPORT struct wl_client *
306weston_client_launch(struct weston_compositor *compositor,
307 struct weston_process *proc,
308 const char *path,
309 weston_process_cleanup_func_t cleanup)
310{
311 int sv[2];
312 pid_t pid;
313 struct wl_client *client;
314
315 weston_log("launching '%s'\n", path);
316
317 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
318 weston_log("weston_client_launch: "
319 "socketpair failed while launching '%s': %m\n",
320 path);
321 return NULL;
322 }
323
324 pid = fork();
325 if (pid == -1) {
326 close(sv[0]);
327 close(sv[1]);
328 weston_log("weston_client_launch: "
329 "fork failed while launching '%s': %m\n", path);
330 return NULL;
331 }
332
333 if (pid == 0) {
334 child_client_exec(sv[1], path);
335 _exit(-1);
336 }
337
338 close(sv[1]);
339
340 client = wl_client_create(compositor->wl_display, sv[0]);
341 if (!client) {
342 close(sv[0]);
343 weston_log("weston_client_launch: "
344 "wl_client_create failed while launching '%s'.\n",
345 path);
346 return NULL;
347 }
348
349 proc->pid = pid;
350 proc->cleanup = cleanup;
351 weston_watch_process(proc);
352
353 return client;
354}
355
Giulio Camuffobab996e2014-10-12 00:24:25 +0300356WL_EXPORT void
357weston_watch_process(struct weston_process *process)
358{
359 wl_list_insert(&child_process_list, &process->link);
360}
361
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300362struct process_info {
363 struct weston_process proc;
364 char *path;
365};
366
367static void
368process_handle_sigchld(struct weston_process *process, int status)
369{
370 struct process_info *pinfo =
371 container_of(process, struct process_info, proc);
372
373 /*
374 * There are no guarantees whether this runs before or after
375 * the wl_client destructor.
376 */
377
378 if (WIFEXITED(status)) {
379 weston_log("%s exited with status %d\n", pinfo->path,
380 WEXITSTATUS(status));
381 } else if (WIFSIGNALED(status)) {
382 weston_log("%s died on signal %d\n", pinfo->path,
383 WTERMSIG(status));
384 } else {
385 weston_log("%s disappeared\n", pinfo->path);
386 }
387
388 free(pinfo->path);
389 free(pinfo);
390}
391
392WL_EXPORT struct wl_client *
393weston_client_start(struct weston_compositor *compositor, const char *path)
394{
395 struct process_info *pinfo;
396 struct wl_client *client;
397
398 pinfo = zalloc(sizeof *pinfo);
399 if (!pinfo)
400 return NULL;
401
402 pinfo->path = strdup(path);
403 if (!pinfo->path)
404 goto out_free;
405
406 client = weston_client_launch(compositor, &pinfo->proc, path,
407 process_handle_sigchld);
408 if (!client)
409 goto out_str;
410
411 return client;
412
413out_str:
414 free(pinfo->path);
415
416out_free:
417 free(pinfo);
418
419 return NULL;
420}
421
Giulio Camuffobab996e2014-10-12 00:24:25 +0300422static void
423log_uname(void)
424{
425 struct utsname usys;
426
427 uname(&usys);
428
429 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
430 usys.version, usys.machine);
431}
432
Armin Krezović78a36372016-08-01 18:51:46 +0200433static struct wet_compositor *
434to_wet_compositor(struct weston_compositor *compositor)
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300435{
436 return weston_compositor_get_user_data(compositor);
437}
438
Armin Krezovića3666d22016-09-30 14:11:04 +0200439static void
440wet_set_pending_output_handler(struct weston_compositor *ec,
441 wl_notify_func_t handler)
442{
443 struct wet_compositor *compositor = to_wet_compositor(ec);
444
445 compositor->pending_output_listener.notify = handler;
446 wl_signal_add(&ec->output_pending_signal, &compositor->pending_output_listener);
447}
448
449static struct wet_output_config *
450wet_init_parsed_options(struct weston_compositor *ec)
451{
452 struct wet_compositor *compositor = to_wet_compositor(ec);
453 struct wet_output_config *config;
454
455 config = zalloc(sizeof *config);
456
457 if (!config) {
458 perror("out of memory");
459 return NULL;
460 }
461
462 config->width = 0;
463 config->height = 0;
464 config->scale = 0;
465 config->transform = UINT32_MAX;
466
467 compositor->parsed_options = config;
468
469 return config;
470}
471
Armin Krezović78a36372016-08-01 18:51:46 +0200472WL_EXPORT struct weston_config *
473wet_get_config(struct weston_compositor *ec)
474{
475 struct wet_compositor *compositor = to_wet_compositor(ec);
476
477 return compositor->config;
478}
479
Giulio Camuffobab996e2014-10-12 00:24:25 +0300480static const char xdg_error_message[] =
481 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
482
483static const char xdg_wrong_message[] =
484 "fatal: environment variable XDG_RUNTIME_DIR\n"
485 "is set to \"%s\", which is not a directory.\n";
486
487static const char xdg_wrong_mode_message[] =
488 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
489 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
490 "and must be owned by the user (current owner is UID %d).\n";
491
492static const char xdg_detail_message[] =
493 "Refer to your distribution on how to get it, or\n"
494 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
495 "on how to implement it.\n";
496
497static void
498verify_xdg_runtime_dir(void)
499{
500 char *dir = getenv("XDG_RUNTIME_DIR");
501 struct stat s;
502
503 if (!dir) {
504 weston_log(xdg_error_message);
505 weston_log_continue(xdg_detail_message);
506 exit(EXIT_FAILURE);
507 }
508
509 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
510 weston_log(xdg_wrong_message, dir);
511 weston_log_continue(xdg_detail_message);
512 exit(EXIT_FAILURE);
513 }
514
515 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
516 weston_log(xdg_wrong_mode_message,
517 dir, s.st_mode & 0777, s.st_uid);
518 weston_log_continue(xdg_detail_message);
519 }
520}
521
522static int
523usage(int error_code)
524{
525 fprintf(stderr,
526 "Usage: weston [OPTIONS]\n\n"
527 "This is weston version " VERSION ", the Wayland reference compositor.\n"
528 "Weston supports multiple backends, and depending on which backend is in use\n"
529 "different options will be accepted.\n\n"
530
531
532 "Core options:\n\n"
533 " --version\t\tPrint weston version\n"
534 " -B, --backend=MODULE\tBackend module, one of\n"
535#if defined(BUILD_DRM_COMPOSITOR)
536 "\t\t\t\tdrm-backend.so\n"
537#endif
538#if defined(BUILD_FBDEV_COMPOSITOR)
539 "\t\t\t\tfbdev-backend.so\n"
540#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300541#if defined(BUILD_HEADLESS_COMPOSITOR)
542 "\t\t\t\theadless-backend.so\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300543#endif
544#if defined(BUILD_RDP_COMPOSITOR)
545 "\t\t\t\trdp-backend.so\n"
546#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300547#if defined(BUILD_WAYLAND_COMPOSITOR)
548 "\t\t\t\twayland-backend.so\n"
549#endif
550#if defined(BUILD_X11_COMPOSITOR)
551 "\t\t\t\tx11-backend.so\n"
552#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300553 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
554 " -S, --socket=NAME\tName of socket to listen on\n"
555 " -i, --idle-time=SECS\tIdle time in seconds\n"
556 " --modules\t\tLoad the comma-separated list of modules\n"
557 " --log=FILE\t\tLog to the given file\n"
558 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
559 " --no-config\t\tDo not read weston.ini\n"
560 " -h, --help\t\tThis help message\n\n");
561
562#if defined(BUILD_DRM_COMPOSITOR)
563 fprintf(stderr,
564 "Options for drm-backend.so:\n\n"
565 " --connector=ID\tBring up only this connector\n"
566 " --seat=SEAT\t\tThe seat that weston should run on\n"
567 " --tty=TTY\t\tThe tty to use\n"
568 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
569 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
570#endif
571
572#if defined(BUILD_FBDEV_COMPOSITOR)
573 fprintf(stderr,
574 "Options for fbdev-backend.so:\n\n"
575 " --tty=TTY\t\tThe tty to use\n"
576 " --device=DEVICE\tThe framebuffer device to use\n"
Pekka Paalanene77f8ad2016-06-08 17:39:37 +0300577 "\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300578#endif
579
Dawid Gajownik71f57042015-07-31 17:39:00 -0300580#if defined(BUILD_HEADLESS_COMPOSITOR)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300581 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300582 "Options for headless-backend.so:\n\n"
583 " --width=WIDTH\t\tWidth of memory surface\n"
584 " --height=HEIGHT\tHeight of memory surface\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300585 " --transform=TR\tThe output transformation, TR is one of:\n"
586 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Armin Krezovićd84deeb2016-06-23 11:59:29 +0200587 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n"
588 " --no-outputs\t\tDo not create any virtual outputs\n"
589 "\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300590#endif
591
592#if defined(BUILD_RDP_COMPOSITOR)
593 fprintf(stderr,
594 "Options for rdp-backend.so:\n\n"
595 " --width=WIDTH\t\tWidth of desktop\n"
596 " --height=HEIGHT\tHeight of desktop\n"
Dawid Gajownikd99a0502015-07-31 14:49:57 -0300597 " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300598 " --address=ADDR\tThe address to bind\n"
599 " --port=PORT\t\tThe port to listen on\n"
600 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
601 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
602 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
603 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
604 "\n");
605#endif
606
Dawid Gajownik71f57042015-07-31 17:39:00 -0300607#if defined(BUILD_WAYLAND_COMPOSITOR)
608 fprintf(stderr,
609 "Options for wayland-backend.so:\n\n"
610 " --width=WIDTH\t\tWidth of Wayland surface\n"
611 " --height=HEIGHT\tHeight of Wayland surface\n"
612 " --scale=SCALE\t\tScale factor of output\n"
613 " --fullscreen\t\tRun in fullscreen mode\n"
614 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
615 " --output-count=COUNT\tCreate multiple outputs\n"
616 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
617 " --display=DISPLAY\tWayland display to connect to\n\n");
618#endif
619
620#if defined(BUILD_X11_COMPOSITOR)
621 fprintf(stderr,
622 "Options for x11-backend.so:\n\n"
623 " --width=WIDTH\t\tWidth of X window\n"
624 " --height=HEIGHT\tHeight of X window\n"
625 " --scale=SCALE\t\tScale factor of output\n"
626 " --fullscreen\t\tRun in fullscreen mode\n"
627 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
628 " --output-count=COUNT\tCreate multiple outputs\n"
629 " --no-input\t\tDont create input devices\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300630#endif
631
632 exit(error_code);
633}
634
635static int on_term_signal(int signal_number, void *data)
636{
637 struct wl_display *display = data;
638
639 weston_log("caught signal %d\n", signal_number);
640 wl_display_terminate(display);
641
642 return 1;
643}
644
645static void
646on_caught_signal(int s, siginfo_t *siginfo, void *context)
647{
648 /* This signal handler will do a best-effort backtrace, and
649 * then call the backend restore function, which will switch
650 * back to the vt we launched from or ungrab X etc and then
651 * raise SIGTRAP. If we run weston under gdb from X or a
652 * different vt, and tell gdb "handle *s* nostop", this
653 * will allow weston to switch back to gdb on crash and then
654 * gdb will catch the crash with SIGTRAP.*/
655
656 weston_log("caught signal: %d\n", s);
657
658 print_backtrace();
659
660 segv_compositor->backend->restore(segv_compositor);
661
662 raise(SIGTRAP);
663}
664
665static void
666catch_signals(void)
667{
668 struct sigaction action;
669
670 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
671 action.sa_sigaction = on_caught_signal;
672 sigemptyset(&action.sa_mask);
673 sigaction(SIGSEGV, &action, NULL);
674 sigaction(SIGABRT, &action, NULL);
675}
676
677static const char *
678clock_name(clockid_t clk_id)
679{
680 static const char *names[] = {
681 [CLOCK_REALTIME] = "CLOCK_REALTIME",
682 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
683 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
684 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
685 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500686#ifdef CLOCK_BOOTTIME
Giulio Camuffobab996e2014-10-12 00:24:25 +0300687 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500688#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300689 };
690
691 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
692 return "unknown";
693
694 return names[clk_id];
695}
696
697static const struct {
698 uint32_t bit; /* enum weston_capability */
699 const char *desc;
700} capability_strings[] = {
701 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
702 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
703};
704
705static void
706weston_compositor_log_capabilities(struct weston_compositor *compositor)
707{
708 unsigned i;
709 int yes;
Pekka Paalanenbe112d42016-04-18 15:53:38 +0300710 struct timespec res;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300711
712 weston_log("Compositor capabilities:\n");
713 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
714 yes = compositor->capabilities & capability_strings[i].bit;
715 weston_log_continue(STAMP_SPACE "%s %s\n",
716 capability_strings[i].desc,
717 yes ? "yes" : "no");
718 }
719
720 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
721 clock_name(compositor->presentation_clock),
722 compositor->presentation_clock);
Pekka Paalanenbe112d42016-04-18 15:53:38 +0300723
724 if (clock_getres(compositor->presentation_clock, &res) == 0)
725 weston_log_continue(STAMP_SPACE
726 "presentation clock resolution: %d.%09ld s\n",
727 (int)res.tv_sec, res.tv_nsec);
728 else
729 weston_log_continue(STAMP_SPACE
730 "presentation clock resolution: N/A\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300731}
732
733static void
734handle_primary_client_destroyed(struct wl_listener *listener, void *data)
735{
736 struct wl_client *client = data;
737
738 weston_log("Primary client died. Closing...\n");
739
740 wl_display_terminate(wl_client_get_display(client));
741}
742
743static int
744weston_create_listening_socket(struct wl_display *display, const char *socket_name)
745{
746 if (socket_name) {
747 if (wl_display_add_socket(display, socket_name)) {
748 weston_log("fatal: failed to add socket: %m\n");
749 return -1;
750 }
751 } else {
752 socket_name = wl_display_add_socket_auto(display);
753 if (!socket_name) {
754 weston_log("fatal: failed to add socket: %m\n");
755 return -1;
756 }
757 }
758
759 setenv("WAYLAND_DISPLAY", socket_name, 1);
760
761 return 0;
762}
763
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300764WL_EXPORT void *
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100765wet_load_module_entrypoint(const char *name, const char *entrypoint)
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300766{
767 const char *builddir = getenv("WESTON_BUILD_DIR");
768 char path[PATH_MAX];
769 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +0000770 size_t len;
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300771
772 if (name == NULL)
773 return NULL;
774
775 if (name[0] != '/') {
776 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +0000777 len = snprintf(path, sizeof path, "%s/.libs/%s", builddir,
778 name);
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300779 else
Daniel Stonebeb97e52016-11-28 12:13:54 +0000780 len = snprintf(path, sizeof path, "%s/%s", MODULEDIR,
781 name);
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300782 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +0000783 len = snprintf(path, sizeof path, "%s", name);
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300784 }
785
Daniel Stonebeb97e52016-11-28 12:13:54 +0000786 /* snprintf returns the length of the string it would've written,
787 * _excluding_ the NUL byte. So even being equal to the size of
788 * our buffer is an error here. */
789 if (len >= sizeof path)
790 return NULL;
791
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300792 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
793 if (module) {
794 weston_log("Module '%s' already loaded\n", path);
795 dlclose(module);
796 return NULL;
797 }
798
799 weston_log("Loading module '%s'\n", path);
800 module = dlopen(path, RTLD_NOW);
801 if (!module) {
802 weston_log("Failed to load module: %s\n", dlerror());
803 return NULL;
804 }
805
806 init = dlsym(module, entrypoint);
807 if (!init) {
808 weston_log("Failed to lookup init function: %s\n", dlerror());
809 dlclose(module);
810 return NULL;
811 }
812
813 return init;
814}
815
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100816
817WL_EXPORT int
818wet_load_module(struct weston_compositor *compositor,
819 const char *name, int *argc, char *argv[])
820{
821 int (*module_init)(struct weston_compositor *ec,
822 int *argc, char *argv[]);
823
824 module_init = wet_load_module_entrypoint(name, "wet_module_init");
825 if (!module_init)
826 return -1;
827 if (module_init(compositor, argc, argv) < 0)
828 return -1;
829 return 0;
830}
831
Giulio Camuffobab996e2014-10-12 00:24:25 +0300832static int
Quentin Glidicda01c1d2016-12-02 14:17:08 +0100833wet_load_shell(struct weston_compositor *compositor,
834 const char *name, int *argc, char *argv[])
835{
836 int (*shell_init)(struct weston_compositor *ec,
837 int *argc, char *argv[]);
838
839 shell_init = wet_load_module_entrypoint(name, "wet_shell_init");
840 if (!shell_init)
841 return -1;
842 if (shell_init(compositor, argc, argv) < 0)
843 return -1;
844 return 0;
845}
846
847static int
Giulio Camuffobab996e2014-10-12 00:24:25 +0300848load_modules(struct weston_compositor *ec, const char *modules,
Quentin Glidic6d3887b2016-07-04 14:34:48 +0200849 int *argc, char *argv[], int32_t *xwayland)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300850{
851 const char *p, *end;
852 char buffer[256];
Giulio Camuffobab996e2014-10-12 00:24:25 +0300853
854 if (modules == NULL)
855 return 0;
856
857 p = modules;
858 while (*p) {
859 end = strchrnul(p, ',');
860 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200861
862 if (strstr(buffer, "xwayland.so")) {
Armin Krezoviće6b71362017-02-09 21:28:32 +0100863 weston_log("Old Xwayland module loading detected: "
864 "Please use --xwayland command line option "
865 "or set xwayland=true in the [core] section "
866 "in weston.ini\n");
Quentin Glidic6d3887b2016-07-04 14:34:48 +0200867 *xwayland = 1;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200868 } else {
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100869 if (wet_load_module(ec, buffer, argc, argv) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200870 return -1;
871 }
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100872
Giulio Camuffobab996e2014-10-12 00:24:25 +0300873 p = end;
874 while (*p == ',')
875 p++;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300876 }
877
878 return 0;
879}
880
881static int
882weston_compositor_init_config(struct weston_compositor *ec,
883 struct weston_config *config)
884{
885 struct xkb_rule_names xkb_names;
886 struct weston_config_section *s;
887 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000888 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300889
890 s = weston_config_get_section(config, "keyboard", NULL, NULL);
891 weston_config_section_get_string(s, "keymap_rules",
892 (char **) &xkb_names.rules, NULL);
893 weston_config_section_get_string(s, "keymap_model",
894 (char **) &xkb_names.model, NULL);
895 weston_config_section_get_string(s, "keymap_layout",
896 (char **) &xkb_names.layout, NULL);
897 weston_config_section_get_string(s, "keymap_variant",
898 (char **) &xkb_names.variant, NULL);
899 weston_config_section_get_string(s, "keymap_options",
900 (char **) &xkb_names.options, NULL);
901
Giulio Camuffo0358af42016-06-02 21:48:08 +0300902 if (weston_compositor_set_xkb_rule_names(ec, &xkb_names) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300903 return -1;
904
905 weston_config_section_get_int(s, "repeat-rate",
906 &ec->kb_repeat_rate, 40);
907 weston_config_section_get_int(s, "repeat-delay",
908 &ec->kb_repeat_delay, 400);
909
Bob Ham91880f12016-01-12 10:21:47 +0000910 weston_config_section_get_bool(s, "vt-switching",
911 &vt_switching, true);
912 ec->vt_switching = vt_switching;
913
Giulio Camuffobab996e2014-10-12 00:24:25 +0300914 s = weston_config_get_section(config, "core", NULL, NULL);
915 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
916 ec->repaint_msec);
917 if (repaint_msec < -10 || repaint_msec > 1000) {
918 weston_log("Invalid repaint_window value in config: %d\n",
919 repaint_msec);
920 } else {
921 ec->repaint_msec = repaint_msec;
922 }
923 weston_log("Output repaint window is %d ms maximum.\n",
924 ec->repaint_msec);
925
926 return 0;
927}
928
929static char *
930weston_choose_default_backend(void)
931{
932 char *backend = NULL;
933
934 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
935 backend = strdup("wayland-backend.so");
936 else if (getenv("DISPLAY"))
937 backend = strdup("x11-backend.so");
938 else
939 backend = strdup(WESTON_NATIVE_BACKEND);
940
941 return backend;
942}
943
944static const struct { const char *name; uint32_t token; } transforms[] = {
945 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
946 { "90", WL_OUTPUT_TRANSFORM_90 },
947 { "180", WL_OUTPUT_TRANSFORM_180 },
948 { "270", WL_OUTPUT_TRANSFORM_270 },
949 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
950 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
951 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
952 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
953};
954
955WL_EXPORT int
956weston_parse_transform(const char *transform, uint32_t *out)
957{
958 unsigned int i;
959
960 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
961 if (strcmp(transforms[i].name, transform) == 0) {
962 *out = transforms[i].token;
963 return 0;
964 }
965
966 *out = WL_OUTPUT_TRANSFORM_NORMAL;
967 return -1;
968}
969
970WL_EXPORT const char *
971weston_transform_to_string(uint32_t output_transform)
972{
973 unsigned int i;
974
975 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
976 if (transforms[i].token == output_transform)
977 return transforms[i].name;
978
979 return "<illegal value>";
980}
981
982static int
983load_configuration(struct weston_config **config, int32_t noconfig,
984 const char *config_file)
985{
986 const char *file = "weston.ini";
987 const char *full_path;
988
989 *config = NULL;
990
991 if (config_file)
992 file = config_file;
993
994 if (noconfig == 0)
995 *config = weston_config_parse(file);
996
997 if (*config) {
998 full_path = weston_config_get_full_path(*config);
999
1000 weston_log("Using config file '%s'\n", full_path);
1001 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1002
1003 return 0;
1004 }
1005
1006 if (config_file && noconfig == 0) {
1007 weston_log("fatal: error opening or reading config file"
1008 " '%s'.\n", config_file);
1009
1010 return -1;
1011 }
1012
1013 weston_log("Starting with no config file.\n");
1014 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1015
1016 return 0;
1017}
1018
1019static void
1020handle_exit(struct weston_compositor *c)
1021{
1022 wl_display_terminate(c->wl_display);
1023}
1024
Armin Krezovića3666d22016-09-30 14:11:04 +02001025static void
1026wet_output_set_scale(struct weston_output *output,
1027 struct weston_config_section *section,
1028 int32_t default_scale,
1029 int32_t parsed_scale)
1030{
1031 int32_t scale = default_scale;
1032
1033 if (section)
1034 weston_config_section_get_int(section, "scale", &scale, default_scale);
1035
1036 if (parsed_scale)
1037 scale = parsed_scale;
1038
1039 weston_output_set_scale(output, scale);
1040}
1041
1042/* UINT32_MAX is treated as invalid because 0 is a valid
1043 * enumeration value and the parameter is unsigned
1044 */
1045static void
1046wet_output_set_transform(struct weston_output *output,
1047 struct weston_config_section *section,
1048 uint32_t default_transform,
1049 uint32_t parsed_transform)
1050{
1051 char *t;
1052 uint32_t transform = default_transform;
1053
1054 if (section) {
1055 weston_config_section_get_string(section,
1056 "transform", &t, "normal");
1057
1058 if (weston_parse_transform(t, &transform) < 0) {
1059 weston_log("Invalid transform \"%s\" for output %s\n",
1060 t, output->name);
1061 transform = default_transform;
1062 }
1063 free(t);
1064 }
1065
1066 if (parsed_transform != UINT32_MAX)
1067 transform = parsed_transform;
1068
1069 weston_output_set_transform(output, transform);
1070}
1071
1072static int
1073wet_configure_windowed_output_from_config(struct weston_output *output,
1074 struct wet_output_config *defaults)
1075{
1076 const struct weston_windowed_output_api *api =
1077 weston_windowed_output_get_api(output->compositor);
1078
1079 struct weston_config *wc = wet_get_config(output->compositor);
1080 struct weston_config_section *section = NULL;
1081 struct wet_compositor *compositor = to_wet_compositor(output->compositor);
1082 struct wet_output_config *parsed_options = compositor->parsed_options;
1083 int width = defaults->width;
1084 int height = defaults->height;
1085
1086 assert(parsed_options);
1087
1088 if (!api) {
1089 weston_log("Cannot use weston_windowed_output_api.\n");
1090 return -1;
1091 }
1092
1093 section = weston_config_get_section(wc, "output", "name", output->name);
1094
1095 if (section) {
1096 char *mode;
1097
1098 weston_config_section_get_string(section, "mode", &mode, NULL);
1099 if (!mode || sscanf(mode, "%dx%d", &width,
1100 &height) != 2) {
1101 weston_log("Invalid mode for output %s. Using defaults.\n",
1102 output->name);
1103 width = defaults->width;
1104 height = defaults->height;
1105 }
1106 free(mode);
1107 }
1108
1109 if (parsed_options->width)
1110 width = parsed_options->width;
1111
1112 if (parsed_options->height)
1113 height = parsed_options->height;
1114
1115 wet_output_set_scale(output, section, defaults->scale, parsed_options->scale);
1116 wet_output_set_transform(output, section, defaults->transform, parsed_options->transform);
1117
1118 if (api->output_set_size(output, width, height) < 0) {
1119 weston_log("Cannot configure output \"%s\" using weston_windowed_output_api.\n",
1120 output->name);
1121 return -1;
1122 }
1123
1124 weston_output_enable(output);
1125
1126 return 0;
1127}
1128
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001129static void
1130configure_input_device(struct weston_compositor *compositor,
1131 struct libinput_device *device)
1132{
1133 struct weston_config_section *s;
1134 struct weston_config *config = wet_get_config(compositor);
1135 int enable_tap;
1136 int enable_tap_default;
1137
1138 s = weston_config_get_section(config,
1139 "libinput", NULL, NULL);
1140
1141 if (libinput_device_config_tap_get_finger_count(device) > 0) {
1142 enable_tap_default =
1143 libinput_device_config_tap_get_default_enabled(
1144 device);
1145 weston_config_section_get_bool(s, "enable_tap",
1146 &enable_tap,
1147 enable_tap_default);
1148 libinput_device_config_tap_set_enabled(device,
1149 enable_tap);
1150 }
1151}
1152
Armin Krezović08368132016-09-30 14:11:05 +02001153static void
1154drm_backend_output_configure(struct wl_listener *listener, void *data)
1155{
1156 struct weston_output *output = data;
1157 struct weston_config *wc = wet_get_config(output->compositor);
Armin Krezović605ac8e2016-10-09 23:48:17 +02001158 struct wet_compositor *wet = to_wet_compositor(output->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02001159 struct weston_config_section *section;
1160 const struct weston_drm_output_api *api = weston_drm_output_get_api(output->compositor);
1161 enum weston_drm_backend_output_mode mode =
1162 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
1163
1164 char *s;
1165 char *modeline = NULL;
1166 char *gbm_format = NULL;
1167 char *seat = NULL;
1168
1169 if (!api) {
1170 weston_log("Cannot use weston_drm_output_api.\n");
1171 return;
1172 }
1173
1174 section = weston_config_get_section(wc, "output", "name", output->name);
1175 weston_config_section_get_string(section, "mode", &s, "preferred");
1176
1177 if (strcmp(s, "off") == 0) {
1178 weston_output_disable(output);
1179 free(s);
1180 return;
Armin Krezović605ac8e2016-10-09 23:48:17 +02001181 } else if (wet->drm_use_current_mode || strcmp(s, "current") == 0) {
Armin Krezović08368132016-09-30 14:11:05 +02001182 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
1183 } else if (strcmp(s, "preferred") != 0) {
1184 modeline = s;
1185 s = NULL;
1186 }
1187 free(s);
1188
1189 if (api->set_mode(output, mode, modeline) < 0) {
1190 weston_log("Cannot configure an output using weston_drm_output_api.\n");
1191 free(modeline);
1192 return;
1193 }
1194 free(modeline);
1195
1196 wet_output_set_scale(output, section, 1, 0);
1197 wet_output_set_transform(output, section, WL_OUTPUT_TRANSFORM_NORMAL, UINT32_MAX);
1198
1199 weston_config_section_get_string(section,
1200 "gbm-format", &gbm_format, NULL);
1201
1202 api->set_gbm_format(output, gbm_format);
1203 free(gbm_format);
1204
1205 weston_config_section_get_string(section, "seat", &seat, "");
1206
1207 api->set_seat(output, seat);
1208 free(seat);
1209
1210 weston_output_enable(output);
1211}
1212
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001213static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001214load_drm_backend(struct weston_compositor *c,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001215 int *argc, char **argv, struct weston_config *wc)
1216{
1217 struct weston_drm_backend_config config = {{ 0, }};
1218 struct weston_config_section *section;
Armin Krezović605ac8e2016-10-09 23:48:17 +02001219 struct wet_compositor *wet = to_wet_compositor(c);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001220 int ret = 0;
1221
Armin Krezović605ac8e2016-10-09 23:48:17 +02001222 wet->drm_use_current_mode = false;
1223
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001224 const struct weston_option options[] = {
1225 { WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
1226 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
1227 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
Armin Krezović605ac8e2016-10-09 23:48:17 +02001228 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &wet->drm_use_current_mode },
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001229 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1230 };
1231
1232 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1233
1234 section = weston_config_get_section(wc, "core", NULL, NULL);
1235 weston_config_section_get_string(section,
1236 "gbm-format", &config.gbm_format,
1237 NULL);
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001238 weston_config_section_get_uint(section, "pageflip-timeout",
1239 &config.pageflip_timeout, 0);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001240
1241 config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
1242 config.base.struct_size = sizeof(struct weston_drm_backend_config);
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001243 config.configure_device = configure_input_device;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001244
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001245 ret = weston_compositor_load_backend(c, WESTON_BACKEND_DRM,
1246 &config.base);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001247
Armin Krezović08368132016-09-30 14:11:05 +02001248 wet_set_pending_output_handler(c, drm_backend_output_configure);
1249
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001250 free(config.gbm_format);
1251 free(config.seat_id);
1252
1253 return ret;
1254}
1255
Armin Krezović7fb17752016-09-30 14:11:07 +02001256static void
1257headless_backend_output_configure(struct wl_listener *listener, void *data)
1258{
1259 struct weston_output *output = data;
1260 struct wet_output_config defaults = {
1261 .width = 1024,
1262 .height = 640,
1263 .scale = 1,
1264 .transform = WL_OUTPUT_TRANSFORM_NORMAL
1265 };
1266
1267 if (wet_configure_windowed_output_from_config(output, &defaults) < 0)
1268 weston_log("Cannot configure output \"%s\".\n", output->name);
1269}
1270
Giulio Camuffo43008c72015-10-17 19:24:15 +03001271static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001272load_headless_backend(struct weston_compositor *c,
Benoit Gschwind3c530942016-04-15 20:28:32 -07001273 int *argc, char **argv, struct weston_config *wc)
1274{
Armin Krezović7fb17752016-09-30 14:11:07 +02001275 const struct weston_windowed_output_api *api;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001276 struct weston_headless_backend_config config = {{ 0, }};
Armin Krezović7fb17752016-09-30 14:11:07 +02001277 int no_outputs = 0;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001278 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001279 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001280
Armin Krezović7fb17752016-09-30 14:11:07 +02001281 struct wet_output_config *parsed_options = wet_init_parsed_options(c);
1282 if (!parsed_options)
1283 return -1;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001284
1285 const struct weston_option options[] = {
Armin Krezović7fb17752016-09-30 14:11:07 +02001286 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1287 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
Benoit Gschwind3c530942016-04-15 20:28:32 -07001288 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1289 { WESTON_OPTION_STRING, "transform", 0, &transform },
Armin Krezović7fb17752016-09-30 14:11:07 +02001290 { WESTON_OPTION_BOOLEAN, "no-outputs", 0, &no_outputs },
Benoit Gschwind3c530942016-04-15 20:28:32 -07001291 };
1292
1293 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1294
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001295 if (transform) {
Armin Krezović7fb17752016-09-30 14:11:07 +02001296 if (weston_parse_transform(transform, &parsed_options->transform) < 0) {
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001297 weston_log("Invalid transform \"%s\"\n", transform);
Armin Krezović7fb17752016-09-30 14:11:07 +02001298 parsed_options->transform = UINT32_MAX;
1299 }
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001300 free(transform);
1301 }
Benoit Gschwind3c530942016-04-15 20:28:32 -07001302
1303 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1304 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1305
1306 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001307 ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1308 &config.base);
Benoit Gschwind3c530942016-04-15 20:28:32 -07001309
Armin Krezović7fb17752016-09-30 14:11:07 +02001310 if (ret < 0)
1311 return ret;
1312
1313 wet_set_pending_output_handler(c, headless_backend_output_configure);
1314
1315 if (!no_outputs) {
1316 api = weston_windowed_output_get_api(c);
1317
1318 if (!api) {
1319 weston_log("Cannot use weston_windowed_output_api.\n");
1320 return -1;
1321 }
1322
1323 if (api->output_create(c, "headless") < 0)
1324 return -1;
1325 }
1326
1327 return 0;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001328}
1329
Benoit Gschwindbd573102016-04-22 17:05:26 +02001330static void
Armin Krezović8f1dca12016-09-30 14:11:08 +02001331rdp_backend_output_configure(struct wl_listener *listener, void *data)
1332{
1333 struct weston_output *output = data;
1334 struct wet_compositor *compositor = to_wet_compositor(output->compositor);
1335 struct wet_output_config *parsed_options = compositor->parsed_options;
1336 const struct weston_rdp_output_api *api = weston_rdp_output_get_api(output->compositor);
1337 int width = 640;
1338 int height = 480;
1339
1340 assert(parsed_options);
1341
1342 if (!api) {
1343 weston_log("Cannot use weston_rdp_output_api.\n");
1344 return;
1345 }
1346
1347 if (parsed_options->width)
1348 width = parsed_options->width;
1349
1350 if (parsed_options->height)
1351 height = parsed_options->height;
1352
1353 weston_output_set_scale(output, 1);
1354 weston_output_set_transform(output, WL_OUTPUT_TRANSFORM_NORMAL);
1355
1356 if (api->output_set_size(output, width, height) < 0) {
1357 weston_log("Cannot configure output \"%s\" using weston_rdp_output_api.\n",
1358 output->name);
1359 return;
1360 }
1361
1362 weston_output_enable(output);
1363}
1364
1365static void
Benoit Gschwindbd573102016-04-22 17:05:26 +02001366weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
1367{
1368 config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
1369 config->base.struct_size = sizeof(struct weston_rdp_backend_config);
1370
Benoit Gschwindbd573102016-04-22 17:05:26 +02001371 config->bind_address = NULL;
1372 config->port = 3389;
1373 config->rdp_key = NULL;
1374 config->server_cert = NULL;
1375 config->server_key = NULL;
1376 config->env_socket = 0;
1377 config->no_clients_resize = 0;
1378}
1379
1380static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001381load_rdp_backend(struct weston_compositor *c,
Benoit Gschwindbd573102016-04-22 17:05:26 +02001382 int *argc, char *argv[], struct weston_config *wc)
1383{
1384 struct weston_rdp_backend_config config = {{ 0, }};
1385 int ret = 0;
1386
Armin Krezović8f1dca12016-09-30 14:11:08 +02001387 struct wet_output_config *parsed_options = wet_init_parsed_options(c);
1388 if (!parsed_options)
1389 return -1;
1390
Benoit Gschwindbd573102016-04-22 17:05:26 +02001391 weston_rdp_backend_config_init(&config);
1392
1393 const struct weston_option rdp_options[] = {
1394 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
Armin Krezović8f1dca12016-09-30 14:11:08 +02001395 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1396 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
Benoit Gschwindbd573102016-04-22 17:05:26 +02001397 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
1398 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1399 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1400 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
1401 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
1402 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
1403 };
1404
1405 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1406
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001407 ret = weston_compositor_load_backend(c, WESTON_BACKEND_RDP,
1408 &config.base);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001409
Armin Krezović8f1dca12016-09-30 14:11:08 +02001410 if (ret < 0)
1411 goto out;
1412
1413 wet_set_pending_output_handler(c, rdp_backend_output_configure);
1414
1415out:
Benoit Gschwindbd573102016-04-22 17:05:26 +02001416 free(config.bind_address);
1417 free(config.rdp_key);
1418 free(config.server_cert);
1419 free(config.server_key);
Armin Krezović8f1dca12016-09-30 14:11:08 +02001420
Benoit Gschwindbd573102016-04-22 17:05:26 +02001421 return ret;
1422}
1423
Armin Krezović6ba369d2016-09-30 14:11:06 +02001424static void
1425fbdev_backend_output_configure(struct wl_listener *listener, void *data)
1426{
1427 struct weston_output *output = data;
1428 struct weston_config *wc = wet_get_config(output->compositor);
1429 struct weston_config_section *section;
1430
1431 section = weston_config_get_section(wc, "output", "name", "fbdev");
1432
1433 wet_output_set_transform(output, section, WL_OUTPUT_TRANSFORM_NORMAL, UINT32_MAX);
1434 weston_output_set_scale(output, 1);
1435
1436 weston_output_enable(output);
1437}
1438
Benoit Gschwind3c530942016-04-15 20:28:32 -07001439static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001440load_fbdev_backend(struct weston_compositor *c,
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001441 int *argc, char **argv, struct weston_config *wc)
1442{
1443 struct weston_fbdev_backend_config config = {{ 0, }};
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001444 int ret = 0;
1445
1446 const struct weston_option fbdev_options[] = {
1447 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1448 { WESTON_OPTION_STRING, "device", 0, &config.device },
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001449 };
1450
1451 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
1452
1453 if (!config.device)
1454 config.device = strdup("/dev/fb0");
1455
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001456 config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
1457 config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001458 config.configure_device = configure_input_device;
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001459
1460 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001461 ret = weston_compositor_load_backend(c, WESTON_BACKEND_FBDEV,
1462 &config.base);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001463
Armin Krezović6ba369d2016-09-30 14:11:06 +02001464 if (ret < 0)
1465 goto out;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001466
Armin Krezović6ba369d2016-09-30 14:11:06 +02001467 wet_set_pending_output_handler(c, fbdev_backend_output_configure);
1468
1469out:
1470 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001471 return ret;
1472}
1473
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001474static void
1475x11_backend_output_configure(struct wl_listener *listener, void *data)
1476{
1477 struct weston_output *output = data;
1478 struct wet_output_config defaults = {
1479 .width = 1024,
1480 .height = 600,
1481 .scale = 1,
1482 .transform = WL_OUTPUT_TRANSFORM_NORMAL
1483 };
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001484
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001485 if (wet_configure_windowed_output_from_config(output, &defaults) < 0)
1486 weston_log("Cannot configure output \"%s\".\n", output->name);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001487}
1488
1489static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001490load_x11_backend(struct weston_compositor *c,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001491 int *argc, char **argv, struct weston_config *wc)
1492{
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001493 char *default_output;
1494 const struct weston_windowed_output_api *api;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001495 struct weston_x11_backend_config config = {{ 0, }};
1496 struct weston_config_section *section;
1497 int ret = 0;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001498 int option_count = 1;
1499 int output_count = 0;
1500 char const *section_name;
1501 int i;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001502
1503 struct wet_output_config *parsed_options = wet_init_parsed_options(c);
1504 if (!parsed_options)
1505 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001506
1507 const struct weston_option options[] = {
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001508 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1509 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
1510 { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001511 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
1512 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1513 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
1514 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1515 };
1516
1517 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1518
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001519 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
1520 config.base.struct_size = sizeof(struct weston_x11_backend_config);
1521
1522 /* load the actual backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001523 ret = weston_compositor_load_backend(c, WESTON_BACKEND_X11,
1524 &config.base);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001525
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001526 if (ret < 0)
1527 return ret;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001528
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001529 wet_set_pending_output_handler(c, x11_backend_output_configure);
1530
1531 api = weston_windowed_output_get_api(c);
1532
1533 if (!api) {
1534 weston_log("Cannot use weston_windowed_output_api.\n");
1535 return -1;
1536 }
1537
1538 section = NULL;
1539 while (weston_config_next_section(wc, &section, &section_name)) {
1540 char *output_name;
1541
1542 if (output_count >= option_count)
1543 break;
1544
1545 if (strcmp(section_name, "output") != 0) {
1546 continue;
1547 }
1548
1549 weston_config_section_get_string(section, "name", &output_name, NULL);
1550 if (output_name == NULL || output_name[0] != 'X') {
1551 free(output_name);
1552 continue;
1553 }
1554
1555 if (api->output_create(c, output_name) < 0) {
1556 free(output_name);
1557 return -1;
1558 }
1559 free(output_name);
1560
1561 output_count++;
1562 }
1563
1564 default_output = NULL;
1565
1566 for (i = output_count; i < option_count; i++) {
1567 if (asprintf(&default_output, "screen%d", i) < 0) {
1568 return -1;
1569 }
1570
1571 if (api->output_create(c, default_output) < 0) {
1572 free(default_output);
1573 return -1;
1574 }
1575 free(default_output);
1576 }
1577
1578 return 0;
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001579}
1580
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001581static void
Armin Krezović174448a2016-09-30 14:11:09 +02001582wayland_backend_output_configure_hotplug(struct wl_listener *listener, void *data)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001583{
Armin Krezović174448a2016-09-30 14:11:09 +02001584 struct weston_output *output = data;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001585
Armin Krezović174448a2016-09-30 14:11:09 +02001586 /* This backend has all values hardcoded, so nothing can be configured here */
1587 weston_output_enable(output);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001588}
1589
1590static void
Armin Krezović174448a2016-09-30 14:11:09 +02001591wayland_backend_output_configure(struct wl_listener *listener, void *data)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001592{
Armin Krezović174448a2016-09-30 14:11:09 +02001593 struct weston_output *output = data;
1594 struct wet_output_config defaults = {
1595 .width = 1024,
1596 .height = 640,
1597 .scale = 1,
1598 .transform = WL_OUTPUT_TRANSFORM_NORMAL
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001599 };
1600
Armin Krezović174448a2016-09-30 14:11:09 +02001601 if (wet_configure_windowed_output_from_config(output, &defaults) < 0)
1602 weston_log("Cannot configure output \"%s\".\n", output->name);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001603}
1604
1605static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001606load_wayland_backend(struct weston_compositor *c,
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001607 int *argc, char **argv, struct weston_config *wc)
1608{
1609 struct weston_wayland_backend_config config = {{ 0, }};
Armin Krezović174448a2016-09-30 14:11:09 +02001610 struct weston_config_section *section;
1611 const struct weston_windowed_output_api *api;
1612 const char *section_name;
1613 char *output_name = NULL;
1614 int count = 1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001615 int ret = 0;
Armin Krezović174448a2016-09-30 14:11:09 +02001616 int i;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001617
Armin Krezović174448a2016-09-30 14:11:09 +02001618 struct wet_output_config *parsed_options = wet_init_parsed_options(c);
1619 if (!parsed_options)
1620 return -1;
1621
1622 config.cursor_size = 32;
1623 config.cursor_theme = NULL;
1624 config.display_name = NULL;
Armin Krezović20450162016-10-13 12:01:43 +02001625 config.fullscreen = false;
Armin Krezović7f1c0b82016-10-09 17:30:23 +02001626 config.sprawl = false;
Armin Krezović7e71b872016-10-09 17:30:22 +02001627 config.use_pixman = false;
Armin Krezović174448a2016-09-30 14:11:09 +02001628
1629 const struct weston_option wayland_options[] = {
1630 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1631 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
1632 { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
1633 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1634 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1635 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
1636 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config.fullscreen },
1637 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &config.sprawl },
1638 };
1639
1640 parse_options(wayland_options, ARRAY_LENGTH(wayland_options), argc, argv);
1641
1642 section = weston_config_get_section(wc, "shell", NULL, NULL);
1643 weston_config_section_get_string(section, "cursor-theme",
1644 &config.cursor_theme, NULL);
1645 weston_config_section_get_int(section, "cursor-size",
1646 &config.cursor_size, 32);
1647
1648 config.base.struct_size = sizeof(struct weston_wayland_backend_config);
1649 config.base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001650
1651 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001652 ret = weston_compositor_load_backend(c, WESTON_BACKEND_WAYLAND,
1653 &config.base);
Armin Krezović174448a2016-09-30 14:11:09 +02001654
1655 free(config.cursor_theme);
1656 free(config.display_name);
1657
1658 if (ret < 0)
1659 return ret;
1660
1661 api = weston_windowed_output_get_api(c);
1662
1663 if (api == NULL) {
1664 /* We will just assume if load_backend() finished cleanly and
1665 * windowed_output_api is not present that wayland backend is
1666 * started with --sprawl or runs on fullscreen-shell. */
1667 wet_set_pending_output_handler(c, wayland_backend_output_configure_hotplug);
1668
1669 return 0;
1670 }
1671
1672 wet_set_pending_output_handler(c, wayland_backend_output_configure);
1673
1674 section = NULL;
1675 while (weston_config_next_section(wc, &section, &section_name)) {
1676 if (count == 0)
1677 break;
1678
1679 if (strcmp(section_name, "output") != 0) {
1680 continue;
1681 }
1682
1683 weston_config_section_get_string(section, "name", &output_name, NULL);
1684
1685 if (output_name == NULL)
1686 continue;
1687
1688 if (output_name[0] != 'W' || output_name[1] != 'L') {
1689 free(output_name);
1690 continue;
1691 }
1692
1693 if (api->output_create(c, output_name) < 0) {
1694 free(output_name);
1695 return -1;
1696 }
1697 free(output_name);
1698
1699 --count;
1700 }
1701
1702 for (i = 0; i < count; i++) {
1703 if (asprintf(&output_name, "wayland%d", i) < 0)
1704 return -1;
1705
1706 if (api->output_create(c, output_name) < 0) {
1707 free(output_name);
1708 return -1;
1709 }
1710 free(output_name);
1711 }
1712
1713 return 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001714}
1715
1716
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001717static int
Giulio Camuffo43008c72015-10-17 19:24:15 +03001718load_backend(struct weston_compositor *compositor, const char *backend,
1719 int *argc, char **argv, struct weston_config *config)
1720{
Benoit Gschwind3c530942016-04-15 20:28:32 -07001721 if (strstr(backend, "headless-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001722 return load_headless_backend(compositor, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001723 else if (strstr(backend, "rdp-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001724 return load_rdp_backend(compositor, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001725 else if (strstr(backend, "fbdev-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001726 return load_fbdev_backend(compositor, argc, argv, config);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001727 else if (strstr(backend, "drm-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001728 return load_drm_backend(compositor, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001729 else if (strstr(backend, "x11-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001730 return load_x11_backend(compositor, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001731 else if (strstr(backend, "wayland-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001732 return load_wayland_backend(compositor, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001733
Pekka Paalanen808b0062016-06-03 13:56:17 +03001734 weston_log("Error: unknown backend \"%s\"\n", backend);
1735 return -1;
Giulio Camuffo43008c72015-10-17 19:24:15 +03001736}
1737
Pekka Paalanen20436e22016-02-11 14:42:21 +02001738static char *
1739copy_command_line(int argc, char * const argv[])
1740{
1741 FILE *fp;
1742 char *str = NULL;
1743 size_t size = 0;
1744 int i;
1745
1746 fp = open_memstream(&str, &size);
1747 if (!fp)
1748 return NULL;
1749
1750 fprintf(fp, "%s", argv[0]);
1751 for (i = 1; i < argc; i++)
1752 fprintf(fp, " %s", argv[i]);
1753 fclose(fp);
1754
1755 return str;
1756}
1757
Giulio Camuffobab996e2014-10-12 00:24:25 +03001758int main(int argc, char *argv[])
1759{
1760 int ret = EXIT_FAILURE;
Pekka Paalanen20436e22016-02-11 14:42:21 +02001761 char *cmdline;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001762 struct wl_display *display;
1763 struct weston_compositor *ec;
1764 struct wl_event_source *signals[4];
1765 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001766 int i, fd;
1767 char *backend = NULL;
1768 char *shell = NULL;
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001769 int32_t xwayland = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001770 char *modules = NULL;
1771 char *option_modules = NULL;
1772 char *log = NULL;
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07001773 char *server_socket = NULL;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001774 int32_t idle_time = -1;
1775 int32_t help = 0;
1776 char *socket_name = NULL;
1777 int32_t version = 0;
1778 int32_t noconfig = 0;
1779 int32_t numlock_on;
1780 char *config_file = NULL;
1781 struct weston_config *config = NULL;
1782 struct weston_config_section *section;
1783 struct wl_client *primary_client;
1784 struct wl_listener primary_client_destroyed;
1785 struct weston_seat *seat;
Armin Krezović78a36372016-08-01 18:51:46 +02001786 struct wet_compositor user_data;
Daniel Díaz75b71972016-10-21 14:03:13 -05001787 int require_input;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001788
1789 const struct weston_option core_options[] = {
1790 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1791 { WESTON_OPTION_STRING, "shell", 0, &shell },
1792 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1793 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001794 { WESTON_OPTION_BOOLEAN, "xwayland", 0, &xwayland },
Giulio Camuffobab996e2014-10-12 00:24:25 +03001795 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1796 { WESTON_OPTION_STRING, "log", 0, &log },
1797 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1798 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1799 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1800 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1801 };
1802
Derek Foremanf0d39b22017-03-24 09:41:12 -05001803 if (os_fd_set_cloexec(fileno(stdin))) {
1804 printf("Unable to set stdin as close on exec().\n");
1805 return EXIT_FAILURE;
1806 }
1807
Pekka Paalanen20436e22016-02-11 14:42:21 +02001808 cmdline = copy_command_line(argc, argv);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001809 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1810
Pekka Paalanen20436e22016-02-11 14:42:21 +02001811 if (help) {
1812 free(cmdline);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001813 usage(EXIT_SUCCESS);
Pekka Paalanen20436e22016-02-11 14:42:21 +02001814 }
Giulio Camuffobab996e2014-10-12 00:24:25 +03001815
1816 if (version) {
1817 printf(PACKAGE_STRING "\n");
Pekka Paalanen20436e22016-02-11 14:42:21 +02001818 free(cmdline);
1819
Giulio Camuffobab996e2014-10-12 00:24:25 +03001820 return EXIT_SUCCESS;
1821 }
1822
Giulio Camuffobe2b11a2016-06-02 21:48:13 +03001823 weston_log_set_handler(vlog, vlog_continue);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001824 weston_log_file_open(log);
1825
1826 weston_log("%s\n"
1827 STAMP_SPACE "%s\n"
1828 STAMP_SPACE "Bug reports to: %s\n"
1829 STAMP_SPACE "Build: %s\n",
1830 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1831 BUILD_ID);
Pekka Paalanen20436e22016-02-11 14:42:21 +02001832 weston_log("Command line: %s\n", cmdline);
1833 free(cmdline);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001834 log_uname();
1835
1836 verify_xdg_runtime_dir();
1837
1838 display = wl_display_create();
1839
1840 loop = wl_display_get_event_loop(display);
1841 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1842 display);
1843 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1844 display);
1845 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1846 display);
1847
1848 wl_list_init(&child_process_list);
1849 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1850 NULL);
1851
1852 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1853 goto out_signals;
1854
1855 if (load_configuration(&config, noconfig, config_file) < 0)
1856 goto out_signals;
Armin Krezović78a36372016-08-01 18:51:46 +02001857 user_data.config = config;
Armin Krezovića3666d22016-09-30 14:11:04 +02001858 user_data.parsed_options = NULL;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001859
1860 section = weston_config_get_section(config, "core", NULL, NULL);
1861
1862 if (!backend) {
1863 weston_config_section_get_string(section, "backend", &backend,
1864 NULL);
1865 if (!backend)
1866 backend = weston_choose_default_backend();
1867 }
1868
Armin Krezović78a36372016-08-01 18:51:46 +02001869 ec = weston_compositor_create(display, &user_data);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001870 if (ec == NULL) {
1871 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001872 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001873 }
1874
Giulio Camuffobab996e2014-10-12 00:24:25 +03001875 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001876 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001877
Daniel Díaz75b71972016-10-21 14:03:13 -05001878 weston_config_section_get_bool(section, "require-input",
1879 &require_input, true);
1880 ec->require_input = require_input;
1881
Giulio Camuffo43008c72015-10-17 19:24:15 +03001882 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001883 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001884 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001885 }
1886
Armin Krezovića3666d22016-09-30 14:11:04 +02001887 weston_pending_output_coldplug(ec);
1888
Giulio Camuffobab996e2014-10-12 00:24:25 +03001889 catch_signals();
1890 segv_compositor = ec;
1891
1892 if (idle_time < 0)
1893 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1894 if (idle_time < 0)
1895 idle_time = 300; /* default idle timeout, in seconds */
1896
1897 ec->idle_time = idle_time;
1898 ec->default_pointer_grab = NULL;
1899 ec->exit = handle_exit;
1900
1901 weston_compositor_log_capabilities(ec);
1902
1903 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1904 if (server_socket) {
1905 weston_log("Running with single client\n");
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07001906 if (!safe_strtoint(server_socket, &fd))
Giulio Camuffobab996e2014-10-12 00:24:25 +03001907 fd = -1;
1908 } else {
1909 fd = -1;
1910 }
1911
1912 if (fd != -1) {
1913 primary_client = wl_client_create(display, fd);
1914 if (!primary_client) {
1915 weston_log("fatal: failed to add client: %m\n");
1916 goto out;
1917 }
1918 primary_client_destroyed.notify =
1919 handle_primary_client_destroyed;
1920 wl_client_add_destroy_listener(primary_client,
1921 &primary_client_destroyed);
1922 } else if (weston_create_listening_socket(display, socket_name)) {
1923 goto out;
1924 }
1925
1926 if (!shell)
1927 weston_config_section_get_string(section, "shell", &shell,
1928 "desktop-shell.so");
1929
Quentin Glidicda01c1d2016-12-02 14:17:08 +01001930 if (wet_load_shell(ec, shell, &argc, argv) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +03001931 goto out;
1932
1933 weston_config_section_get_string(section, "modules", &modules, "");
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001934 if (load_modules(ec, modules, &argc, argv, &xwayland) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +03001935 goto out;
1936
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001937 if (load_modules(ec, option_modules, &argc, argv, &xwayland) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +03001938 goto out;
1939
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001940 if (!xwayland)
1941 weston_config_section_get_bool(section, "xwayland", &xwayland,
1942 false);
1943 if (xwayland) {
1944 if (wet_load_xwayland(ec) < 0)
1945 goto out;
1946 }
1947
Giulio Camuffobab996e2014-10-12 00:24:25 +03001948 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1949 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1950 if (numlock_on) {
1951 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001952 struct weston_keyboard *keyboard =
1953 weston_seat_get_keyboard(seat);
1954
1955 if (keyboard)
1956 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001957 WESTON_NUM_LOCK,
1958 WESTON_NUM_LOCK);
1959 }
1960 }
1961
1962 for (i = 1; i < argc; i++)
1963 weston_log("fatal: unhandled option: %s\n", argv[i]);
1964 if (argc > 1)
1965 goto out;
1966
1967 weston_compositor_wake(ec);
1968
1969 wl_display_run(display);
1970
1971 /* Allow for setting return exit code after
1972 * wl_display_run returns normally. This is
1973 * useful for devs/testers and automated tests
1974 * that want to indicate failure status to
1975 * testing infrastructure above
1976 */
1977 ret = ec->exit_code;
1978
1979out:
Armin Krezovića3666d22016-09-30 14:11:04 +02001980 /* free(NULL) is valid, and it won't be NULL if it's used */
1981 free(user_data.parsed_options);
1982
Giulio Camuffobab996e2014-10-12 00:24:25 +03001983 weston_compositor_destroy(ec);
1984
1985out_signals:
1986 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1987 if (signals[i])
1988 wl_event_source_remove(signals[i]);
1989
1990 wl_display_destroy(display);
1991
1992 weston_log_file_close();
1993
1994 if (config)
1995 weston_config_destroy(config);
1996 free(config_file);
1997 free(backend);
1998 free(shell);
1999 free(socket_name);
2000 free(option_modules);
2001 free(log);
2002 free(modules);
2003
2004 return ret;
2005}