blob: f9614f57cfdebe13b25e491883f08e203da3e2b2 [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ć78a36372016-08-01 18:51:46 +020081};
82
Giulio Camuffobe2b11a2016-06-02 21:48:13 +030083static FILE *weston_logfile = NULL;
84
85static int cached_tm_mday = -1;
86
87static int weston_log_timestamp(void)
88{
89 struct timeval tv;
90 struct tm *brokendown_time;
91 char string[128];
92
93 gettimeofday(&tv, NULL);
94
95 brokendown_time = localtime(&tv.tv_sec);
96 if (brokendown_time == NULL)
97 return fprintf(weston_logfile, "[(NULL)localtime] ");
98
99 if (brokendown_time->tm_mday != cached_tm_mday) {
100 strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
101 fprintf(weston_logfile, "Date: %s\n", string);
102
103 cached_tm_mday = brokendown_time->tm_mday;
104 }
105
106 strftime(string, sizeof string, "%H:%M:%S", brokendown_time);
107
108 return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
109}
110
111static void
112custom_handler(const char *fmt, va_list arg)
113{
114 weston_log_timestamp();
115 fprintf(weston_logfile, "libwayland: ");
116 vfprintf(weston_logfile, fmt, arg);
117}
118
119static void
120weston_log_file_open(const char *filename)
121{
122 wl_log_set_handler_server(custom_handler);
123
124 if (filename != NULL) {
125 weston_logfile = fopen(filename, "a");
126 if (weston_logfile)
127 os_fd_set_cloexec(fileno(weston_logfile));
128 }
129
130 if (weston_logfile == NULL)
131 weston_logfile = stderr;
132 else
133 setvbuf(weston_logfile, NULL, _IOLBF, 256);
134}
135
136static void
137weston_log_file_close(void)
138{
139 if ((weston_logfile != stderr) && (weston_logfile != NULL))
140 fclose(weston_logfile);
141 weston_logfile = stderr;
142}
143
144static int
145vlog(const char *fmt, va_list ap)
146{
147 int l;
148
149 l = weston_log_timestamp();
150 l += vfprintf(weston_logfile, fmt, ap);
151
152 return l;
153}
154
155static int
156vlog_continue(const char *fmt, va_list argp)
157{
158 return vfprintf(weston_logfile, fmt, argp);
159}
160
Giulio Camuffobab996e2014-10-12 00:24:25 +0300161static struct wl_list child_process_list;
162static struct weston_compositor *segv_compositor;
163
164static int
165sigchld_handler(int signal_number, void *data)
166{
167 struct weston_process *p;
168 int status;
169 pid_t pid;
170
171 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
172 wl_list_for_each(p, &child_process_list, link) {
173 if (p->pid == pid)
174 break;
175 }
176
177 if (&p->link == &child_process_list) {
178 weston_log("unknown child process exited\n");
179 continue;
180 }
181
182 wl_list_remove(&p->link);
183 p->cleanup(p, status);
184 }
185
186 if (pid < 0 && errno != ECHILD)
187 weston_log("waitpid error %m\n");
188
189 return 1;
190}
191
192#ifdef HAVE_LIBUNWIND
193
194static void
195print_backtrace(void)
196{
197 unw_cursor_t cursor;
198 unw_context_t context;
199 unw_word_t off;
200 unw_proc_info_t pip;
201 int ret, i = 0;
202 char procname[256];
203 const char *filename;
204 Dl_info dlinfo;
205
206 pip.unwind_info = NULL;
207 ret = unw_getcontext(&context);
208 if (ret) {
209 weston_log("unw_getcontext: %d\n", ret);
210 return;
211 }
212
213 ret = unw_init_local(&cursor, &context);
214 if (ret) {
215 weston_log("unw_init_local: %d\n", ret);
216 return;
217 }
218
219 ret = unw_step(&cursor);
220 while (ret > 0) {
221 ret = unw_get_proc_info(&cursor, &pip);
222 if (ret) {
223 weston_log("unw_get_proc_info: %d\n", ret);
224 break;
225 }
226
227 ret = unw_get_proc_name(&cursor, procname, 256, &off);
228 if (ret && ret != -UNW_ENOMEM) {
229 if (ret != -UNW_EUNSPEC)
230 weston_log("unw_get_proc_name: %d\n", ret);
231 procname[0] = '?';
232 procname[1] = 0;
233 }
234
235 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
236 *dlinfo.dli_fname)
237 filename = dlinfo.dli_fname;
238 else
239 filename = "?";
240
241 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
242 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
243
244 ret = unw_step(&cursor);
245 if (ret < 0)
246 weston_log("unw_step: %d\n", ret);
247 }
248}
249
250#else
251
252static void
253print_backtrace(void)
254{
255 void *buffer[32];
256 int i, count;
257 Dl_info info;
258
259 count = backtrace(buffer, ARRAY_LENGTH(buffer));
260 for (i = 0; i < count; i++) {
261 dladdr(buffer[i], &info);
262 weston_log(" [%016lx] %s (%s)\n",
263 (long) buffer[i],
264 info.dli_sname ? info.dli_sname : "--",
265 info.dli_fname);
266 }
267}
268
269#endif
270
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300271static void
272child_client_exec(int sockfd, const char *path)
273{
274 int clientfd;
275 char s[32];
276 sigset_t allsigs;
277
278 /* do not give our signal mask to the new process */
279 sigfillset(&allsigs);
280 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
281
282 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
283 if (seteuid(getuid()) == -1) {
284 weston_log("compositor: failed seteuid\n");
285 return;
286 }
287
288 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
289 * non-CLOEXEC fd to pass through exec. */
290 clientfd = dup(sockfd);
291 if (clientfd == -1) {
292 weston_log("compositor: dup failed: %m\n");
293 return;
294 }
295
296 snprintf(s, sizeof s, "%d", clientfd);
297 setenv("WAYLAND_SOCKET", s, 1);
298
299 if (execl(path, path, NULL) < 0)
300 weston_log("compositor: executing '%s' failed: %m\n",
301 path);
302}
303
304WL_EXPORT struct wl_client *
305weston_client_launch(struct weston_compositor *compositor,
306 struct weston_process *proc,
307 const char *path,
308 weston_process_cleanup_func_t cleanup)
309{
310 int sv[2];
311 pid_t pid;
312 struct wl_client *client;
313
314 weston_log("launching '%s'\n", path);
315
316 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
317 weston_log("weston_client_launch: "
318 "socketpair failed while launching '%s': %m\n",
319 path);
320 return NULL;
321 }
322
323 pid = fork();
324 if (pid == -1) {
325 close(sv[0]);
326 close(sv[1]);
327 weston_log("weston_client_launch: "
328 "fork failed while launching '%s': %m\n", path);
329 return NULL;
330 }
331
332 if (pid == 0) {
333 child_client_exec(sv[1], path);
334 _exit(-1);
335 }
336
337 close(sv[1]);
338
339 client = wl_client_create(compositor->wl_display, sv[0]);
340 if (!client) {
341 close(sv[0]);
342 weston_log("weston_client_launch: "
343 "wl_client_create failed while launching '%s'.\n",
344 path);
345 return NULL;
346 }
347
348 proc->pid = pid;
349 proc->cleanup = cleanup;
350 weston_watch_process(proc);
351
352 return client;
353}
354
Giulio Camuffobab996e2014-10-12 00:24:25 +0300355WL_EXPORT void
356weston_watch_process(struct weston_process *process)
357{
358 wl_list_insert(&child_process_list, &process->link);
359}
360
Giulio Camuffofba27fb2016-06-02 21:48:10 +0300361struct process_info {
362 struct weston_process proc;
363 char *path;
364};
365
366static void
367process_handle_sigchld(struct weston_process *process, int status)
368{
369 struct process_info *pinfo =
370 container_of(process, struct process_info, proc);
371
372 /*
373 * There are no guarantees whether this runs before or after
374 * the wl_client destructor.
375 */
376
377 if (WIFEXITED(status)) {
378 weston_log("%s exited with status %d\n", pinfo->path,
379 WEXITSTATUS(status));
380 } else if (WIFSIGNALED(status)) {
381 weston_log("%s died on signal %d\n", pinfo->path,
382 WTERMSIG(status));
383 } else {
384 weston_log("%s disappeared\n", pinfo->path);
385 }
386
387 free(pinfo->path);
388 free(pinfo);
389}
390
391WL_EXPORT struct wl_client *
392weston_client_start(struct weston_compositor *compositor, const char *path)
393{
394 struct process_info *pinfo;
395 struct wl_client *client;
396
397 pinfo = zalloc(sizeof *pinfo);
398 if (!pinfo)
399 return NULL;
400
401 pinfo->path = strdup(path);
402 if (!pinfo->path)
403 goto out_free;
404
405 client = weston_client_launch(compositor, &pinfo->proc, path,
406 process_handle_sigchld);
407 if (!client)
408 goto out_str;
409
410 return client;
411
412out_str:
413 free(pinfo->path);
414
415out_free:
416 free(pinfo);
417
418 return NULL;
419}
420
Giulio Camuffobab996e2014-10-12 00:24:25 +0300421static void
422log_uname(void)
423{
424 struct utsname usys;
425
426 uname(&usys);
427
428 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
429 usys.version, usys.machine);
430}
431
Armin Krezović78a36372016-08-01 18:51:46 +0200432static struct wet_compositor *
433to_wet_compositor(struct weston_compositor *compositor)
Giulio Camuffod52f3b72016-06-02 21:48:11 +0300434{
435 return weston_compositor_get_user_data(compositor);
436}
437
Armin Krezovića3666d22016-09-30 14:11:04 +0200438static void
439wet_set_pending_output_handler(struct weston_compositor *ec,
440 wl_notify_func_t handler)
441{
442 struct wet_compositor *compositor = to_wet_compositor(ec);
443
444 compositor->pending_output_listener.notify = handler;
445 wl_signal_add(&ec->output_pending_signal, &compositor->pending_output_listener);
446}
447
448static struct wet_output_config *
449wet_init_parsed_options(struct weston_compositor *ec)
450{
451 struct wet_compositor *compositor = to_wet_compositor(ec);
452 struct wet_output_config *config;
453
454 config = zalloc(sizeof *config);
455
456 if (!config) {
457 perror("out of memory");
458 return NULL;
459 }
460
461 config->width = 0;
462 config->height = 0;
463 config->scale = 0;
464 config->transform = UINT32_MAX;
465
466 compositor->parsed_options = config;
467
468 return config;
469}
470
Armin Krezović78a36372016-08-01 18:51:46 +0200471WL_EXPORT struct weston_config *
472wet_get_config(struct weston_compositor *ec)
473{
474 struct wet_compositor *compositor = to_wet_compositor(ec);
475
476 return compositor->config;
477}
478
Giulio Camuffobab996e2014-10-12 00:24:25 +0300479static const char xdg_error_message[] =
480 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
481
482static const char xdg_wrong_message[] =
483 "fatal: environment variable XDG_RUNTIME_DIR\n"
484 "is set to \"%s\", which is not a directory.\n";
485
486static const char xdg_wrong_mode_message[] =
487 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
488 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
489 "and must be owned by the user (current owner is UID %d).\n";
490
491static const char xdg_detail_message[] =
492 "Refer to your distribution on how to get it, or\n"
493 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
494 "on how to implement it.\n";
495
496static void
497verify_xdg_runtime_dir(void)
498{
499 char *dir = getenv("XDG_RUNTIME_DIR");
500 struct stat s;
501
502 if (!dir) {
503 weston_log(xdg_error_message);
504 weston_log_continue(xdg_detail_message);
505 exit(EXIT_FAILURE);
506 }
507
508 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
509 weston_log(xdg_wrong_message, dir);
510 weston_log_continue(xdg_detail_message);
511 exit(EXIT_FAILURE);
512 }
513
514 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
515 weston_log(xdg_wrong_mode_message,
516 dir, s.st_mode & 0777, s.st_uid);
517 weston_log_continue(xdg_detail_message);
518 }
519}
520
521static int
522usage(int error_code)
523{
524 fprintf(stderr,
525 "Usage: weston [OPTIONS]\n\n"
526 "This is weston version " VERSION ", the Wayland reference compositor.\n"
527 "Weston supports multiple backends, and depending on which backend is in use\n"
528 "different options will be accepted.\n\n"
529
530
531 "Core options:\n\n"
532 " --version\t\tPrint weston version\n"
533 " -B, --backend=MODULE\tBackend module, one of\n"
534#if defined(BUILD_DRM_COMPOSITOR)
535 "\t\t\t\tdrm-backend.so\n"
536#endif
537#if defined(BUILD_FBDEV_COMPOSITOR)
538 "\t\t\t\tfbdev-backend.so\n"
539#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300540#if defined(BUILD_HEADLESS_COMPOSITOR)
541 "\t\t\t\theadless-backend.so\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300542#endif
543#if defined(BUILD_RDP_COMPOSITOR)
544 "\t\t\t\trdp-backend.so\n"
545#endif
Dawid Gajownik71f57042015-07-31 17:39:00 -0300546#if defined(BUILD_WAYLAND_COMPOSITOR)
547 "\t\t\t\twayland-backend.so\n"
548#endif
549#if defined(BUILD_X11_COMPOSITOR)
550 "\t\t\t\tx11-backend.so\n"
551#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300552 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
553 " -S, --socket=NAME\tName of socket to listen on\n"
554 " -i, --idle-time=SECS\tIdle time in seconds\n"
555 " --modules\t\tLoad the comma-separated list of modules\n"
556 " --log=FILE\t\tLog to the given file\n"
557 " -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
558 " --no-config\t\tDo not read weston.ini\n"
559 " -h, --help\t\tThis help message\n\n");
560
561#if defined(BUILD_DRM_COMPOSITOR)
562 fprintf(stderr,
563 "Options for drm-backend.so:\n\n"
564 " --connector=ID\tBring up only this connector\n"
565 " --seat=SEAT\t\tThe seat that weston should run on\n"
566 " --tty=TTY\t\tThe tty to use\n"
567 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
568 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
569#endif
570
571#if defined(BUILD_FBDEV_COMPOSITOR)
572 fprintf(stderr,
573 "Options for fbdev-backend.so:\n\n"
574 " --tty=TTY\t\tThe tty to use\n"
575 " --device=DEVICE\tThe framebuffer device to use\n"
Pekka Paalanene77f8ad2016-06-08 17:39:37 +0300576 "\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300577#endif
578
Dawid Gajownik71f57042015-07-31 17:39:00 -0300579#if defined(BUILD_HEADLESS_COMPOSITOR)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300580 fprintf(stderr,
Dawid Gajownik71f57042015-07-31 17:39:00 -0300581 "Options for headless-backend.so:\n\n"
582 " --width=WIDTH\t\tWidth of memory surface\n"
583 " --height=HEIGHT\tHeight of memory surface\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300584 " --transform=TR\tThe output transformation, TR is one of:\n"
585 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Armin Krezovićd84deeb2016-06-23 11:59:29 +0200586 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n"
587 " --no-outputs\t\tDo not create any virtual outputs\n"
588 "\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300589#endif
590
591#if defined(BUILD_RDP_COMPOSITOR)
592 fprintf(stderr,
593 "Options for rdp-backend.so:\n\n"
594 " --width=WIDTH\t\tWidth of desktop\n"
595 " --height=HEIGHT\tHeight of desktop\n"
Dawid Gajownikd99a0502015-07-31 14:49:57 -0300596 " --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
Giulio Camuffobab996e2014-10-12 00:24:25 +0300597 " --address=ADDR\tThe address to bind\n"
598 " --port=PORT\t\tThe port to listen on\n"
599 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
600 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
601 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
602 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
603 "\n");
604#endif
605
Dawid Gajownik71f57042015-07-31 17:39:00 -0300606#if defined(BUILD_WAYLAND_COMPOSITOR)
607 fprintf(stderr,
608 "Options for wayland-backend.so:\n\n"
609 " --width=WIDTH\t\tWidth of Wayland surface\n"
610 " --height=HEIGHT\tHeight of Wayland surface\n"
611 " --scale=SCALE\t\tScale factor of output\n"
612 " --fullscreen\t\tRun in fullscreen mode\n"
613 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
614 " --output-count=COUNT\tCreate multiple outputs\n"
615 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
616 " --display=DISPLAY\tWayland display to connect to\n\n");
617#endif
618
619#if defined(BUILD_X11_COMPOSITOR)
620 fprintf(stderr,
621 "Options for x11-backend.so:\n\n"
622 " --width=WIDTH\t\tWidth of X window\n"
623 " --height=HEIGHT\tHeight of X window\n"
624 " --scale=SCALE\t\tScale factor of output\n"
625 " --fullscreen\t\tRun in fullscreen mode\n"
626 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
627 " --output-count=COUNT\tCreate multiple outputs\n"
628 " --no-input\t\tDont create input devices\n\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300629#endif
630
631 exit(error_code);
632}
633
634static int on_term_signal(int signal_number, void *data)
635{
636 struct wl_display *display = data;
637
638 weston_log("caught signal %d\n", signal_number);
639 wl_display_terminate(display);
640
641 return 1;
642}
643
644static void
645on_caught_signal(int s, siginfo_t *siginfo, void *context)
646{
647 /* This signal handler will do a best-effort backtrace, and
648 * then call the backend restore function, which will switch
649 * back to the vt we launched from or ungrab X etc and then
650 * raise SIGTRAP. If we run weston under gdb from X or a
651 * different vt, and tell gdb "handle *s* nostop", this
652 * will allow weston to switch back to gdb on crash and then
653 * gdb will catch the crash with SIGTRAP.*/
654
655 weston_log("caught signal: %d\n", s);
656
657 print_backtrace();
658
659 segv_compositor->backend->restore(segv_compositor);
660
661 raise(SIGTRAP);
662}
663
664static void
665catch_signals(void)
666{
667 struct sigaction action;
668
669 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
670 action.sa_sigaction = on_caught_signal;
671 sigemptyset(&action.sa_mask);
672 sigaction(SIGSEGV, &action, NULL);
673 sigaction(SIGABRT, &action, NULL);
674}
675
676static const char *
677clock_name(clockid_t clk_id)
678{
679 static const char *names[] = {
680 [CLOCK_REALTIME] = "CLOCK_REALTIME",
681 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
682 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
683 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
684 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500685#ifdef CLOCK_BOOTTIME
Giulio Camuffobab996e2014-10-12 00:24:25 +0300686 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500687#endif
Giulio Camuffobab996e2014-10-12 00:24:25 +0300688 };
689
690 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
691 return "unknown";
692
693 return names[clk_id];
694}
695
696static const struct {
697 uint32_t bit; /* enum weston_capability */
698 const char *desc;
699} capability_strings[] = {
700 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
701 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
702};
703
704static void
705weston_compositor_log_capabilities(struct weston_compositor *compositor)
706{
707 unsigned i;
708 int yes;
Pekka Paalanenbe112d42016-04-18 15:53:38 +0300709 struct timespec res;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300710
711 weston_log("Compositor capabilities:\n");
712 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
713 yes = compositor->capabilities & capability_strings[i].bit;
714 weston_log_continue(STAMP_SPACE "%s %s\n",
715 capability_strings[i].desc,
716 yes ? "yes" : "no");
717 }
718
719 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
720 clock_name(compositor->presentation_clock),
721 compositor->presentation_clock);
Pekka Paalanenbe112d42016-04-18 15:53:38 +0300722
723 if (clock_getres(compositor->presentation_clock, &res) == 0)
724 weston_log_continue(STAMP_SPACE
725 "presentation clock resolution: %d.%09ld s\n",
726 (int)res.tv_sec, res.tv_nsec);
727 else
728 weston_log_continue(STAMP_SPACE
729 "presentation clock resolution: N/A\n");
Giulio Camuffobab996e2014-10-12 00:24:25 +0300730}
731
732static void
733handle_primary_client_destroyed(struct wl_listener *listener, void *data)
734{
735 struct wl_client *client = data;
736
737 weston_log("Primary client died. Closing...\n");
738
739 wl_display_terminate(wl_client_get_display(client));
740}
741
742static int
743weston_create_listening_socket(struct wl_display *display, const char *socket_name)
744{
745 if (socket_name) {
746 if (wl_display_add_socket(display, socket_name)) {
747 weston_log("fatal: failed to add socket: %m\n");
748 return -1;
749 }
750 } else {
751 socket_name = wl_display_add_socket_auto(display);
752 if (!socket_name) {
753 weston_log("fatal: failed to add socket: %m\n");
754 return -1;
755 }
756 }
757
758 setenv("WAYLAND_DISPLAY", socket_name, 1);
759
760 return 0;
761}
762
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300763WL_EXPORT void *
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100764wet_load_module_entrypoint(const char *name, const char *entrypoint)
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300765{
766 const char *builddir = getenv("WESTON_BUILD_DIR");
767 char path[PATH_MAX];
768 void *module, *init;
Daniel Stonebeb97e52016-11-28 12:13:54 +0000769 size_t len;
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300770
771 if (name == NULL)
772 return NULL;
773
774 if (name[0] != '/') {
775 if (builddir)
Daniel Stonebeb97e52016-11-28 12:13:54 +0000776 len = snprintf(path, sizeof path, "%s/.libs/%s", builddir,
777 name);
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300778 else
Daniel Stonebeb97e52016-11-28 12:13:54 +0000779 len = snprintf(path, sizeof path, "%s/%s", MODULEDIR,
780 name);
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300781 } else {
Daniel Stonebeb97e52016-11-28 12:13:54 +0000782 len = snprintf(path, sizeof path, "%s", name);
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300783 }
784
Daniel Stonebeb97e52016-11-28 12:13:54 +0000785 /* snprintf returns the length of the string it would've written,
786 * _excluding_ the NUL byte. So even being equal to the size of
787 * our buffer is an error here. */
788 if (len >= sizeof path)
789 return NULL;
790
Giulio Camuffo179fcda2016-06-02 21:48:14 +0300791 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
792 if (module) {
793 weston_log("Module '%s' already loaded\n", path);
794 dlclose(module);
795 return NULL;
796 }
797
798 weston_log("Loading module '%s'\n", path);
799 module = dlopen(path, RTLD_NOW);
800 if (!module) {
801 weston_log("Failed to load module: %s\n", dlerror());
802 return NULL;
803 }
804
805 init = dlsym(module, entrypoint);
806 if (!init) {
807 weston_log("Failed to lookup init function: %s\n", dlerror());
808 dlclose(module);
809 return NULL;
810 }
811
812 return init;
813}
814
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100815
816WL_EXPORT int
817wet_load_module(struct weston_compositor *compositor,
818 const char *name, int *argc, char *argv[])
819{
820 int (*module_init)(struct weston_compositor *ec,
821 int *argc, char *argv[]);
822
823 module_init = wet_load_module_entrypoint(name, "wet_module_init");
824 if (!module_init)
825 return -1;
826 if (module_init(compositor, argc, argv) < 0)
827 return -1;
828 return 0;
829}
830
Giulio Camuffobab996e2014-10-12 00:24:25 +0300831static int
Quentin Glidicda01c1d2016-12-02 14:17:08 +0100832wet_load_shell(struct weston_compositor *compositor,
833 const char *name, int *argc, char *argv[])
834{
835 int (*shell_init)(struct weston_compositor *ec,
836 int *argc, char *argv[]);
837
838 shell_init = wet_load_module_entrypoint(name, "wet_shell_init");
839 if (!shell_init)
840 return -1;
841 if (shell_init(compositor, argc, argv) < 0)
842 return -1;
843 return 0;
844}
845
846static int
Giulio Camuffobab996e2014-10-12 00:24:25 +0300847load_modules(struct weston_compositor *ec, const char *modules,
Quentin Glidic6d3887b2016-07-04 14:34:48 +0200848 int *argc, char *argv[], int32_t *xwayland)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300849{
850 const char *p, *end;
851 char buffer[256];
Giulio Camuffobab996e2014-10-12 00:24:25 +0300852
853 if (modules == NULL)
854 return 0;
855
856 p = modules;
857 while (*p) {
858 end = strchrnul(p, ',');
859 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200860
861 if (strstr(buffer, "xwayland.so")) {
Quentin Glidic6d3887b2016-07-04 14:34:48 +0200862 weston_log("Old Xwayland module loading detected:"
863 "Please use --xwayland command line option"
864 "or weston.ini xwayland=true\n");
865 *xwayland = 1;
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200866 } else {
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100867 if (wet_load_module(ec, buffer, argc, argv) < 0)
Giulio Camuffo9c764df2016-06-29 11:54:27 +0200868 return -1;
869 }
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100870
Giulio Camuffobab996e2014-10-12 00:24:25 +0300871 p = end;
872 while (*p == ',')
873 p++;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300874 }
875
876 return 0;
877}
878
879static int
880weston_compositor_init_config(struct weston_compositor *ec,
881 struct weston_config *config)
882{
883 struct xkb_rule_names xkb_names;
884 struct weston_config_section *s;
885 int repaint_msec;
Bob Ham91880f12016-01-12 10:21:47 +0000886 int vt_switching;
Giulio Camuffobab996e2014-10-12 00:24:25 +0300887
888 s = weston_config_get_section(config, "keyboard", NULL, NULL);
889 weston_config_section_get_string(s, "keymap_rules",
890 (char **) &xkb_names.rules, NULL);
891 weston_config_section_get_string(s, "keymap_model",
892 (char **) &xkb_names.model, NULL);
893 weston_config_section_get_string(s, "keymap_layout",
894 (char **) &xkb_names.layout, NULL);
895 weston_config_section_get_string(s, "keymap_variant",
896 (char **) &xkb_names.variant, NULL);
897 weston_config_section_get_string(s, "keymap_options",
898 (char **) &xkb_names.options, NULL);
899
Giulio Camuffo0358af42016-06-02 21:48:08 +0300900 if (weston_compositor_set_xkb_rule_names(ec, &xkb_names) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +0300901 return -1;
902
903 weston_config_section_get_int(s, "repeat-rate",
904 &ec->kb_repeat_rate, 40);
905 weston_config_section_get_int(s, "repeat-delay",
906 &ec->kb_repeat_delay, 400);
907
Bob Ham91880f12016-01-12 10:21:47 +0000908 weston_config_section_get_bool(s, "vt-switching",
909 &vt_switching, true);
910 ec->vt_switching = vt_switching;
911
Giulio Camuffobab996e2014-10-12 00:24:25 +0300912 s = weston_config_get_section(config, "core", NULL, NULL);
913 weston_config_section_get_int(s, "repaint-window", &repaint_msec,
914 ec->repaint_msec);
915 if (repaint_msec < -10 || repaint_msec > 1000) {
916 weston_log("Invalid repaint_window value in config: %d\n",
917 repaint_msec);
918 } else {
919 ec->repaint_msec = repaint_msec;
920 }
921 weston_log("Output repaint window is %d ms maximum.\n",
922 ec->repaint_msec);
923
924 return 0;
925}
926
927static char *
928weston_choose_default_backend(void)
929{
930 char *backend = NULL;
931
932 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
933 backend = strdup("wayland-backend.so");
934 else if (getenv("DISPLAY"))
935 backend = strdup("x11-backend.so");
936 else
937 backend = strdup(WESTON_NATIVE_BACKEND);
938
939 return backend;
940}
941
942static const struct { const char *name; uint32_t token; } transforms[] = {
943 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
944 { "90", WL_OUTPUT_TRANSFORM_90 },
945 { "180", WL_OUTPUT_TRANSFORM_180 },
946 { "270", WL_OUTPUT_TRANSFORM_270 },
947 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
948 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
949 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
950 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
951};
952
953WL_EXPORT int
954weston_parse_transform(const char *transform, uint32_t *out)
955{
956 unsigned int i;
957
958 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
959 if (strcmp(transforms[i].name, transform) == 0) {
960 *out = transforms[i].token;
961 return 0;
962 }
963
964 *out = WL_OUTPUT_TRANSFORM_NORMAL;
965 return -1;
966}
967
968WL_EXPORT const char *
969weston_transform_to_string(uint32_t output_transform)
970{
971 unsigned int i;
972
973 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
974 if (transforms[i].token == output_transform)
975 return transforms[i].name;
976
977 return "<illegal value>";
978}
979
980static int
981load_configuration(struct weston_config **config, int32_t noconfig,
982 const char *config_file)
983{
984 const char *file = "weston.ini";
985 const char *full_path;
986
987 *config = NULL;
988
989 if (config_file)
990 file = config_file;
991
992 if (noconfig == 0)
993 *config = weston_config_parse(file);
994
995 if (*config) {
996 full_path = weston_config_get_full_path(*config);
997
998 weston_log("Using config file '%s'\n", full_path);
999 setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);
1000
1001 return 0;
1002 }
1003
1004 if (config_file && noconfig == 0) {
1005 weston_log("fatal: error opening or reading config file"
1006 " '%s'.\n", config_file);
1007
1008 return -1;
1009 }
1010
1011 weston_log("Starting with no config file.\n");
1012 setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);
1013
1014 return 0;
1015}
1016
1017static void
1018handle_exit(struct weston_compositor *c)
1019{
1020 wl_display_terminate(c->wl_display);
1021}
1022
Armin Krezovića3666d22016-09-30 14:11:04 +02001023static void
1024wet_output_set_scale(struct weston_output *output,
1025 struct weston_config_section *section,
1026 int32_t default_scale,
1027 int32_t parsed_scale)
1028{
1029 int32_t scale = default_scale;
1030
1031 if (section)
1032 weston_config_section_get_int(section, "scale", &scale, default_scale);
1033
1034 if (parsed_scale)
1035 scale = parsed_scale;
1036
1037 weston_output_set_scale(output, scale);
1038}
1039
1040/* UINT32_MAX is treated as invalid because 0 is a valid
1041 * enumeration value and the parameter is unsigned
1042 */
1043static void
1044wet_output_set_transform(struct weston_output *output,
1045 struct weston_config_section *section,
1046 uint32_t default_transform,
1047 uint32_t parsed_transform)
1048{
1049 char *t;
1050 uint32_t transform = default_transform;
1051
1052 if (section) {
1053 weston_config_section_get_string(section,
1054 "transform", &t, "normal");
1055
1056 if (weston_parse_transform(t, &transform) < 0) {
1057 weston_log("Invalid transform \"%s\" for output %s\n",
1058 t, output->name);
1059 transform = default_transform;
1060 }
1061 free(t);
1062 }
1063
1064 if (parsed_transform != UINT32_MAX)
1065 transform = parsed_transform;
1066
1067 weston_output_set_transform(output, transform);
1068}
1069
1070static int
1071wet_configure_windowed_output_from_config(struct weston_output *output,
1072 struct wet_output_config *defaults)
1073{
1074 const struct weston_windowed_output_api *api =
1075 weston_windowed_output_get_api(output->compositor);
1076
1077 struct weston_config *wc = wet_get_config(output->compositor);
1078 struct weston_config_section *section = NULL;
1079 struct wet_compositor *compositor = to_wet_compositor(output->compositor);
1080 struct wet_output_config *parsed_options = compositor->parsed_options;
1081 int width = defaults->width;
1082 int height = defaults->height;
1083
1084 assert(parsed_options);
1085
1086 if (!api) {
1087 weston_log("Cannot use weston_windowed_output_api.\n");
1088 return -1;
1089 }
1090
1091 section = weston_config_get_section(wc, "output", "name", output->name);
1092
1093 if (section) {
1094 char *mode;
1095
1096 weston_config_section_get_string(section, "mode", &mode, NULL);
1097 if (!mode || sscanf(mode, "%dx%d", &width,
1098 &height) != 2) {
1099 weston_log("Invalid mode for output %s. Using defaults.\n",
1100 output->name);
1101 width = defaults->width;
1102 height = defaults->height;
1103 }
1104 free(mode);
1105 }
1106
1107 if (parsed_options->width)
1108 width = parsed_options->width;
1109
1110 if (parsed_options->height)
1111 height = parsed_options->height;
1112
1113 wet_output_set_scale(output, section, defaults->scale, parsed_options->scale);
1114 wet_output_set_transform(output, section, defaults->transform, parsed_options->transform);
1115
1116 if (api->output_set_size(output, width, height) < 0) {
1117 weston_log("Cannot configure output \"%s\" using weston_windowed_output_api.\n",
1118 output->name);
1119 return -1;
1120 }
1121
1122 weston_output_enable(output);
1123
1124 return 0;
1125}
1126
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001127static void
1128configure_input_device(struct weston_compositor *compositor,
1129 struct libinput_device *device)
1130{
1131 struct weston_config_section *s;
1132 struct weston_config *config = wet_get_config(compositor);
1133 int enable_tap;
1134 int enable_tap_default;
1135
1136 s = weston_config_get_section(config,
1137 "libinput", NULL, NULL);
1138
1139 if (libinput_device_config_tap_get_finger_count(device) > 0) {
1140 enable_tap_default =
1141 libinput_device_config_tap_get_default_enabled(
1142 device);
1143 weston_config_section_get_bool(s, "enable_tap",
1144 &enable_tap,
1145 enable_tap_default);
1146 libinput_device_config_tap_set_enabled(device,
1147 enable_tap);
1148 }
1149}
1150
Armin Krezović08368132016-09-30 14:11:05 +02001151static void
1152drm_backend_output_configure(struct wl_listener *listener, void *data)
1153{
1154 struct weston_output *output = data;
1155 struct weston_config *wc = wet_get_config(output->compositor);
1156 struct weston_config_section *section;
1157 const struct weston_drm_output_api *api = weston_drm_output_get_api(output->compositor);
1158 enum weston_drm_backend_output_mode mode =
1159 WESTON_DRM_BACKEND_OUTPUT_PREFERRED;
1160
1161 char *s;
1162 char *modeline = NULL;
1163 char *gbm_format = NULL;
1164 char *seat = NULL;
1165
1166 if (!api) {
1167 weston_log("Cannot use weston_drm_output_api.\n");
1168 return;
1169 }
1170
1171 section = weston_config_get_section(wc, "output", "name", output->name);
1172 weston_config_section_get_string(section, "mode", &s, "preferred");
1173
1174 if (strcmp(s, "off") == 0) {
1175 weston_output_disable(output);
1176 free(s);
1177 return;
1178 } else if (strcmp(s, "current") == 0) {
1179 mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
1180 } else if (strcmp(s, "preferred") != 0) {
1181 modeline = s;
1182 s = NULL;
1183 }
1184 free(s);
1185
1186 if (api->set_mode(output, mode, modeline) < 0) {
1187 weston_log("Cannot configure an output using weston_drm_output_api.\n");
1188 free(modeline);
1189 return;
1190 }
1191 free(modeline);
1192
1193 wet_output_set_scale(output, section, 1, 0);
1194 wet_output_set_transform(output, section, WL_OUTPUT_TRANSFORM_NORMAL, UINT32_MAX);
1195
1196 weston_config_section_get_string(section,
1197 "gbm-format", &gbm_format, NULL);
1198
1199 api->set_gbm_format(output, gbm_format);
1200 free(gbm_format);
1201
1202 weston_config_section_get_string(section, "seat", &seat, "");
1203
1204 api->set_seat(output, seat);
1205 free(seat);
1206
1207 weston_output_enable(output);
1208}
1209
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001210static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001211load_drm_backend(struct weston_compositor *c,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001212 int *argc, char **argv, struct weston_config *wc)
1213{
1214 struct weston_drm_backend_config config = {{ 0, }};
1215 struct weston_config_section *section;
1216 int ret = 0;
1217
1218 const struct weston_option options[] = {
1219 { WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
1220 { WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
1221 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1222 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
1223 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1224 };
1225
1226 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1227
1228 section = weston_config_get_section(wc, "core", NULL, NULL);
1229 weston_config_section_get_string(section,
1230 "gbm-format", &config.gbm_format,
1231 NULL);
1232
1233 config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
1234 config.base.struct_size = sizeof(struct weston_drm_backend_config);
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001235 config.configure_device = configure_input_device;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001236
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001237 ret = weston_compositor_load_backend(c, WESTON_BACKEND_DRM,
1238 &config.base);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001239
Armin Krezović08368132016-09-30 14:11:05 +02001240 wet_set_pending_output_handler(c, drm_backend_output_configure);
1241
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001242 free(config.gbm_format);
1243 free(config.seat_id);
1244
1245 return ret;
1246}
1247
Armin Krezović7fb17752016-09-30 14:11:07 +02001248static void
1249headless_backend_output_configure(struct wl_listener *listener, void *data)
1250{
1251 struct weston_output *output = data;
1252 struct wet_output_config defaults = {
1253 .width = 1024,
1254 .height = 640,
1255 .scale = 1,
1256 .transform = WL_OUTPUT_TRANSFORM_NORMAL
1257 };
1258
1259 if (wet_configure_windowed_output_from_config(output, &defaults) < 0)
1260 weston_log("Cannot configure output \"%s\".\n", output->name);
1261}
1262
Giulio Camuffo43008c72015-10-17 19:24:15 +03001263static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001264load_headless_backend(struct weston_compositor *c,
Benoit Gschwind3c530942016-04-15 20:28:32 -07001265 int *argc, char **argv, struct weston_config *wc)
1266{
Armin Krezović7fb17752016-09-30 14:11:07 +02001267 const struct weston_windowed_output_api *api;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001268 struct weston_headless_backend_config config = {{ 0, }};
Armin Krezović7fb17752016-09-30 14:11:07 +02001269 int no_outputs = 0;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001270 int ret = 0;
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001271 char *transform = NULL;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001272
Armin Krezović7fb17752016-09-30 14:11:07 +02001273 struct wet_output_config *parsed_options = wet_init_parsed_options(c);
1274 if (!parsed_options)
1275 return -1;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001276
1277 const struct weston_option options[] = {
Armin Krezović7fb17752016-09-30 14:11:07 +02001278 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1279 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
Benoit Gschwind3c530942016-04-15 20:28:32 -07001280 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1281 { WESTON_OPTION_STRING, "transform", 0, &transform },
Armin Krezović7fb17752016-09-30 14:11:07 +02001282 { WESTON_OPTION_BOOLEAN, "no-outputs", 0, &no_outputs },
Benoit Gschwind3c530942016-04-15 20:28:32 -07001283 };
1284
1285 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1286
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001287 if (transform) {
Armin Krezović7fb17752016-09-30 14:11:07 +02001288 if (weston_parse_transform(transform, &parsed_options->transform) < 0) {
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001289 weston_log("Invalid transform \"%s\"\n", transform);
Armin Krezović7fb17752016-09-30 14:11:07 +02001290 parsed_options->transform = UINT32_MAX;
1291 }
Benoit Gschwind2da6d0c2016-04-29 15:21:54 +02001292 free(transform);
1293 }
Benoit Gschwind3c530942016-04-15 20:28:32 -07001294
1295 config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
1296 config.base.struct_size = sizeof(struct weston_headless_backend_config);
1297
1298 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001299 ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
1300 &config.base);
Benoit Gschwind3c530942016-04-15 20:28:32 -07001301
Armin Krezović7fb17752016-09-30 14:11:07 +02001302 if (ret < 0)
1303 return ret;
1304
1305 wet_set_pending_output_handler(c, headless_backend_output_configure);
1306
1307 if (!no_outputs) {
1308 api = weston_windowed_output_get_api(c);
1309
1310 if (!api) {
1311 weston_log("Cannot use weston_windowed_output_api.\n");
1312 return -1;
1313 }
1314
1315 if (api->output_create(c, "headless") < 0)
1316 return -1;
1317 }
1318
1319 return 0;
Benoit Gschwind3c530942016-04-15 20:28:32 -07001320}
1321
Benoit Gschwindbd573102016-04-22 17:05:26 +02001322static void
Armin Krezović8f1dca12016-09-30 14:11:08 +02001323rdp_backend_output_configure(struct wl_listener *listener, void *data)
1324{
1325 struct weston_output *output = data;
1326 struct wet_compositor *compositor = to_wet_compositor(output->compositor);
1327 struct wet_output_config *parsed_options = compositor->parsed_options;
1328 const struct weston_rdp_output_api *api = weston_rdp_output_get_api(output->compositor);
1329 int width = 640;
1330 int height = 480;
1331
1332 assert(parsed_options);
1333
1334 if (!api) {
1335 weston_log("Cannot use weston_rdp_output_api.\n");
1336 return;
1337 }
1338
1339 if (parsed_options->width)
1340 width = parsed_options->width;
1341
1342 if (parsed_options->height)
1343 height = parsed_options->height;
1344
1345 weston_output_set_scale(output, 1);
1346 weston_output_set_transform(output, WL_OUTPUT_TRANSFORM_NORMAL);
1347
1348 if (api->output_set_size(output, width, height) < 0) {
1349 weston_log("Cannot configure output \"%s\" using weston_rdp_output_api.\n",
1350 output->name);
1351 return;
1352 }
1353
1354 weston_output_enable(output);
1355}
1356
1357static void
Benoit Gschwindbd573102016-04-22 17:05:26 +02001358weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
1359{
1360 config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
1361 config->base.struct_size = sizeof(struct weston_rdp_backend_config);
1362
Benoit Gschwindbd573102016-04-22 17:05:26 +02001363 config->bind_address = NULL;
1364 config->port = 3389;
1365 config->rdp_key = NULL;
1366 config->server_cert = NULL;
1367 config->server_key = NULL;
1368 config->env_socket = 0;
1369 config->no_clients_resize = 0;
1370}
1371
1372static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001373load_rdp_backend(struct weston_compositor *c,
Benoit Gschwindbd573102016-04-22 17:05:26 +02001374 int *argc, char *argv[], struct weston_config *wc)
1375{
1376 struct weston_rdp_backend_config config = {{ 0, }};
1377 int ret = 0;
1378
Armin Krezović8f1dca12016-09-30 14:11:08 +02001379 struct wet_output_config *parsed_options = wet_init_parsed_options(c);
1380 if (!parsed_options)
1381 return -1;
1382
Benoit Gschwindbd573102016-04-22 17:05:26 +02001383 weston_rdp_backend_config_init(&config);
1384
1385 const struct weston_option rdp_options[] = {
1386 { WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
Armin Krezović8f1dca12016-09-30 14:11:08 +02001387 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1388 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
Benoit Gschwindbd573102016-04-22 17:05:26 +02001389 { WESTON_OPTION_STRING, "address", 0, &config.bind_address },
1390 { WESTON_OPTION_INTEGER, "port", 0, &config.port },
1391 { WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
1392 { WESTON_OPTION_STRING, "rdp4-key", 0, &config.rdp_key },
1393 { WESTON_OPTION_STRING, "rdp-tls-cert", 0, &config.server_cert },
1394 { WESTON_OPTION_STRING, "rdp-tls-key", 0, &config.server_key }
1395 };
1396
1397 parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);
1398
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001399 ret = weston_compositor_load_backend(c, WESTON_BACKEND_RDP,
1400 &config.base);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001401
Armin Krezović8f1dca12016-09-30 14:11:08 +02001402 if (ret < 0)
1403 goto out;
1404
1405 wet_set_pending_output_handler(c, rdp_backend_output_configure);
1406
1407out:
Benoit Gschwindbd573102016-04-22 17:05:26 +02001408 free(config.bind_address);
1409 free(config.rdp_key);
1410 free(config.server_cert);
1411 free(config.server_key);
Armin Krezović8f1dca12016-09-30 14:11:08 +02001412
Benoit Gschwindbd573102016-04-22 17:05:26 +02001413 return ret;
1414}
1415
Armin Krezović6ba369d2016-09-30 14:11:06 +02001416static void
1417fbdev_backend_output_configure(struct wl_listener *listener, void *data)
1418{
1419 struct weston_output *output = data;
1420 struct weston_config *wc = wet_get_config(output->compositor);
1421 struct weston_config_section *section;
1422
1423 section = weston_config_get_section(wc, "output", "name", "fbdev");
1424
1425 wet_output_set_transform(output, section, WL_OUTPUT_TRANSFORM_NORMAL, UINT32_MAX);
1426 weston_output_set_scale(output, 1);
1427
1428 weston_output_enable(output);
1429}
1430
Benoit Gschwind3c530942016-04-15 20:28:32 -07001431static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001432load_fbdev_backend(struct weston_compositor *c,
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001433 int *argc, char **argv, struct weston_config *wc)
1434{
1435 struct weston_fbdev_backend_config config = {{ 0, }};
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001436 int ret = 0;
1437
1438 const struct weston_option fbdev_options[] = {
1439 { WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
1440 { WESTON_OPTION_STRING, "device", 0, &config.device },
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001441 };
1442
1443 parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);
1444
1445 if (!config.device)
1446 config.device = strdup("/dev/fb0");
1447
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001448 config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
1449 config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03001450 config.configure_device = configure_input_device;
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001451
1452 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001453 ret = weston_compositor_load_backend(c, WESTON_BACKEND_FBDEV,
1454 &config.base);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001455
Armin Krezović6ba369d2016-09-30 14:11:06 +02001456 if (ret < 0)
1457 goto out;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001458
Armin Krezović6ba369d2016-09-30 14:11:06 +02001459 wet_set_pending_output_handler(c, fbdev_backend_output_configure);
1460
1461out:
1462 free(config.device);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001463 return ret;
1464}
1465
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001466static void
1467x11_backend_output_configure(struct wl_listener *listener, void *data)
1468{
1469 struct weston_output *output = data;
1470 struct wet_output_config defaults = {
1471 .width = 1024,
1472 .height = 600,
1473 .scale = 1,
1474 .transform = WL_OUTPUT_TRANSFORM_NORMAL
1475 };
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001476
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001477 if (wet_configure_windowed_output_from_config(output, &defaults) < 0)
1478 weston_log("Cannot configure output \"%s\".\n", output->name);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001479}
1480
1481static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001482load_x11_backend(struct weston_compositor *c,
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001483 int *argc, char **argv, struct weston_config *wc)
1484{
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001485 char *default_output;
1486 const struct weston_windowed_output_api *api;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001487 struct weston_x11_backend_config config = {{ 0, }};
1488 struct weston_config_section *section;
1489 int ret = 0;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001490 int option_count = 1;
1491 int output_count = 0;
1492 char const *section_name;
1493 int i;
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001494
1495 struct wet_output_config *parsed_options = wet_init_parsed_options(c);
1496 if (!parsed_options)
1497 return -1;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001498
1499 const struct weston_option options[] = {
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001500 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1501 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
1502 { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001503 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
1504 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1505 { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
1506 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1507 };
1508
1509 parse_options(options, ARRAY_LENGTH(options), argc, argv);
1510
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001511 config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
1512 config.base.struct_size = sizeof(struct weston_x11_backend_config);
1513
1514 /* load the actual backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001515 ret = weston_compositor_load_backend(c, WESTON_BACKEND_X11,
1516 &config.base);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001517
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001518 if (ret < 0)
1519 return ret;
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001520
Armin Krezovićc3d2f962016-09-30 14:11:10 +02001521 wet_set_pending_output_handler(c, x11_backend_output_configure);
1522
1523 api = weston_windowed_output_get_api(c);
1524
1525 if (!api) {
1526 weston_log("Cannot use weston_windowed_output_api.\n");
1527 return -1;
1528 }
1529
1530 section = NULL;
1531 while (weston_config_next_section(wc, &section, &section_name)) {
1532 char *output_name;
1533
1534 if (output_count >= option_count)
1535 break;
1536
1537 if (strcmp(section_name, "output") != 0) {
1538 continue;
1539 }
1540
1541 weston_config_section_get_string(section, "name", &output_name, NULL);
1542 if (output_name == NULL || output_name[0] != 'X') {
1543 free(output_name);
1544 continue;
1545 }
1546
1547 if (api->output_create(c, output_name) < 0) {
1548 free(output_name);
1549 return -1;
1550 }
1551 free(output_name);
1552
1553 output_count++;
1554 }
1555
1556 default_output = NULL;
1557
1558 for (i = output_count; i < option_count; i++) {
1559 if (asprintf(&default_output, "screen%d", i) < 0) {
1560 return -1;
1561 }
1562
1563 if (api->output_create(c, default_output) < 0) {
1564 free(default_output);
1565 return -1;
1566 }
1567 free(default_output);
1568 }
1569
1570 return 0;
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001571}
1572
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001573static void
Armin Krezović174448a2016-09-30 14:11:09 +02001574wayland_backend_output_configure_hotplug(struct wl_listener *listener, void *data)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001575{
Armin Krezović174448a2016-09-30 14:11:09 +02001576 struct weston_output *output = data;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001577
Armin Krezović174448a2016-09-30 14:11:09 +02001578 /* This backend has all values hardcoded, so nothing can be configured here */
1579 weston_output_enable(output);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001580}
1581
1582static void
Armin Krezović174448a2016-09-30 14:11:09 +02001583wayland_backend_output_configure(struct wl_listener *listener, void *data)
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001584{
Armin Krezović174448a2016-09-30 14:11:09 +02001585 struct weston_output *output = data;
1586 struct wet_output_config defaults = {
1587 .width = 1024,
1588 .height = 640,
1589 .scale = 1,
1590 .transform = WL_OUTPUT_TRANSFORM_NORMAL
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001591 };
1592
Armin Krezović174448a2016-09-30 14:11:09 +02001593 if (wet_configure_windowed_output_from_config(output, &defaults) < 0)
1594 weston_log("Cannot configure output \"%s\".\n", output->name);
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001595}
1596
1597static int
Pekka Paalanen321ede72016-06-03 15:28:40 +03001598load_wayland_backend(struct weston_compositor *c,
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001599 int *argc, char **argv, struct weston_config *wc)
1600{
1601 struct weston_wayland_backend_config config = {{ 0, }};
Armin Krezović174448a2016-09-30 14:11:09 +02001602 struct weston_config_section *section;
1603 const struct weston_windowed_output_api *api;
1604 const char *section_name;
1605 char *output_name = NULL;
1606 int count = 1;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001607 int ret = 0;
Armin Krezović174448a2016-09-30 14:11:09 +02001608 int i;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001609
Armin Krezović174448a2016-09-30 14:11:09 +02001610 struct wet_output_config *parsed_options = wet_init_parsed_options(c);
1611 if (!parsed_options)
1612 return -1;
1613
1614 config.cursor_size = 32;
1615 config.cursor_theme = NULL;
1616 config.display_name = NULL;
Armin Krezović20450162016-10-13 12:01:43 +02001617 config.fullscreen = false;
Armin Krezović7f1c0b82016-10-09 17:30:23 +02001618 config.sprawl = false;
Armin Krezović7e71b872016-10-09 17:30:22 +02001619 config.use_pixman = false;
Armin Krezović174448a2016-09-30 14:11:09 +02001620
1621 const struct weston_option wayland_options[] = {
1622 { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
1623 { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
1624 { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
1625 { WESTON_OPTION_STRING, "display", 0, &config.display_name },
1626 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
1627 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
1628 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config.fullscreen },
1629 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &config.sprawl },
1630 };
1631
1632 parse_options(wayland_options, ARRAY_LENGTH(wayland_options), argc, argv);
1633
1634 section = weston_config_get_section(wc, "shell", NULL, NULL);
1635 weston_config_section_get_string(section, "cursor-theme",
1636 &config.cursor_theme, NULL);
1637 weston_config_section_get_int(section, "cursor-size",
1638 &config.cursor_size, 32);
1639
1640 config.base.struct_size = sizeof(struct weston_wayland_backend_config);
1641 config.base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001642
1643 /* load the actual wayland backend and configure it */
Pekka Paalanen50dbf382016-06-03 15:23:46 +03001644 ret = weston_compositor_load_backend(c, WESTON_BACKEND_WAYLAND,
1645 &config.base);
Armin Krezović174448a2016-09-30 14:11:09 +02001646
1647 free(config.cursor_theme);
1648 free(config.display_name);
1649
1650 if (ret < 0)
1651 return ret;
1652
1653 api = weston_windowed_output_get_api(c);
1654
1655 if (api == NULL) {
1656 /* We will just assume if load_backend() finished cleanly and
1657 * windowed_output_api is not present that wayland backend is
1658 * started with --sprawl or runs on fullscreen-shell. */
1659 wet_set_pending_output_handler(c, wayland_backend_output_configure_hotplug);
1660
1661 return 0;
1662 }
1663
1664 wet_set_pending_output_handler(c, wayland_backend_output_configure);
1665
1666 section = NULL;
1667 while (weston_config_next_section(wc, &section, &section_name)) {
1668 if (count == 0)
1669 break;
1670
1671 if (strcmp(section_name, "output") != 0) {
1672 continue;
1673 }
1674
1675 weston_config_section_get_string(section, "name", &output_name, NULL);
1676
1677 if (output_name == NULL)
1678 continue;
1679
1680 if (output_name[0] != 'W' || output_name[1] != 'L') {
1681 free(output_name);
1682 continue;
1683 }
1684
1685 if (api->output_create(c, output_name) < 0) {
1686 free(output_name);
1687 return -1;
1688 }
1689 free(output_name);
1690
1691 --count;
1692 }
1693
1694 for (i = 0; i < count; i++) {
1695 if (asprintf(&output_name, "wayland%d", i) < 0)
1696 return -1;
1697
1698 if (api->output_create(c, output_name) < 0) {
1699 free(output_name);
1700 return -1;
1701 }
1702 free(output_name);
1703 }
1704
1705 return 0;
Benoit Gschwind3ff10da2016-05-10 22:47:49 +02001706}
1707
1708
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001709static int
Giulio Camuffo43008c72015-10-17 19:24:15 +03001710load_backend(struct weston_compositor *compositor, const char *backend,
1711 int *argc, char **argv, struct weston_config *config)
1712{
Benoit Gschwind3c530942016-04-15 20:28:32 -07001713 if (strstr(backend, "headless-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001714 return load_headless_backend(compositor, argc, argv, config);
Benoit Gschwindbd573102016-04-22 17:05:26 +02001715 else if (strstr(backend, "rdp-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001716 return load_rdp_backend(compositor, argc, argv, config);
Benoit Gschwind934e89a2016-04-27 23:56:42 +02001717 else if (strstr(backend, "fbdev-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001718 return load_fbdev_backend(compositor, argc, argv, config);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07001719 else if (strstr(backend, "drm-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001720 return load_drm_backend(compositor, argc, argv, config);
Benoit Gschwinde16acab2016-04-15 20:28:31 -07001721 else if (strstr(backend, "x11-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001722 return load_x11_backend(compositor, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001723 else if (strstr(backend, "wayland-backend.so"))
Pekka Paalanen321ede72016-06-03 15:28:40 +03001724 return load_wayland_backend(compositor, argc, argv, config);
Giulio Camuffo43008c72015-10-17 19:24:15 +03001725
Pekka Paalanen808b0062016-06-03 13:56:17 +03001726 weston_log("Error: unknown backend \"%s\"\n", backend);
1727 return -1;
Giulio Camuffo43008c72015-10-17 19:24:15 +03001728}
1729
Pekka Paalanen20436e22016-02-11 14:42:21 +02001730static char *
1731copy_command_line(int argc, char * const argv[])
1732{
1733 FILE *fp;
1734 char *str = NULL;
1735 size_t size = 0;
1736 int i;
1737
1738 fp = open_memstream(&str, &size);
1739 if (!fp)
1740 return NULL;
1741
1742 fprintf(fp, "%s", argv[0]);
1743 for (i = 1; i < argc; i++)
1744 fprintf(fp, " %s", argv[i]);
1745 fclose(fp);
1746
1747 return str;
1748}
1749
Giulio Camuffobab996e2014-10-12 00:24:25 +03001750int main(int argc, char *argv[])
1751{
1752 int ret = EXIT_FAILURE;
Pekka Paalanen20436e22016-02-11 14:42:21 +02001753 char *cmdline;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001754 struct wl_display *display;
1755 struct weston_compositor *ec;
1756 struct wl_event_source *signals[4];
1757 struct wl_event_loop *loop;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001758 int i, fd;
1759 char *backend = NULL;
1760 char *shell = NULL;
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001761 int32_t xwayland = 0;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001762 char *modules = NULL;
1763 char *option_modules = NULL;
1764 char *log = NULL;
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07001765 char *server_socket = NULL;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001766 int32_t idle_time = -1;
1767 int32_t help = 0;
1768 char *socket_name = NULL;
1769 int32_t version = 0;
1770 int32_t noconfig = 0;
1771 int32_t numlock_on;
1772 char *config_file = NULL;
1773 struct weston_config *config = NULL;
1774 struct weston_config_section *section;
1775 struct wl_client *primary_client;
1776 struct wl_listener primary_client_destroyed;
1777 struct weston_seat *seat;
Armin Krezović78a36372016-08-01 18:51:46 +02001778 struct wet_compositor user_data;
Daniel Díaz75b71972016-10-21 14:03:13 -05001779 int require_input;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001780
1781 const struct weston_option core_options[] = {
1782 { WESTON_OPTION_STRING, "backend", 'B', &backend },
1783 { WESTON_OPTION_STRING, "shell", 0, &shell },
1784 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
1785 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001786 { WESTON_OPTION_BOOLEAN, "xwayland", 0, &xwayland },
Giulio Camuffobab996e2014-10-12 00:24:25 +03001787 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
1788 { WESTON_OPTION_STRING, "log", 0, &log },
1789 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
1790 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
1791 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
1792 { WESTON_OPTION_STRING, "config", 'c', &config_file },
1793 };
1794
Pekka Paalanen20436e22016-02-11 14:42:21 +02001795 cmdline = copy_command_line(argc, argv);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001796 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
1797
Pekka Paalanen20436e22016-02-11 14:42:21 +02001798 if (help) {
1799 free(cmdline);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001800 usage(EXIT_SUCCESS);
Pekka Paalanen20436e22016-02-11 14:42:21 +02001801 }
Giulio Camuffobab996e2014-10-12 00:24:25 +03001802
1803 if (version) {
1804 printf(PACKAGE_STRING "\n");
Pekka Paalanen20436e22016-02-11 14:42:21 +02001805 free(cmdline);
1806
Giulio Camuffobab996e2014-10-12 00:24:25 +03001807 return EXIT_SUCCESS;
1808 }
1809
Giulio Camuffobe2b11a2016-06-02 21:48:13 +03001810 weston_log_set_handler(vlog, vlog_continue);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001811 weston_log_file_open(log);
1812
1813 weston_log("%s\n"
1814 STAMP_SPACE "%s\n"
1815 STAMP_SPACE "Bug reports to: %s\n"
1816 STAMP_SPACE "Build: %s\n",
1817 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
1818 BUILD_ID);
Pekka Paalanen20436e22016-02-11 14:42:21 +02001819 weston_log("Command line: %s\n", cmdline);
1820 free(cmdline);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001821 log_uname();
1822
1823 verify_xdg_runtime_dir();
1824
1825 display = wl_display_create();
1826
1827 loop = wl_display_get_event_loop(display);
1828 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
1829 display);
1830 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
1831 display);
1832 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
1833 display);
1834
1835 wl_list_init(&child_process_list);
1836 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
1837 NULL);
1838
1839 if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
1840 goto out_signals;
1841
1842 if (load_configuration(&config, noconfig, config_file) < 0)
1843 goto out_signals;
Armin Krezović78a36372016-08-01 18:51:46 +02001844 user_data.config = config;
Armin Krezovića3666d22016-09-30 14:11:04 +02001845 user_data.parsed_options = NULL;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001846
1847 section = weston_config_get_section(config, "core", NULL, NULL);
1848
1849 if (!backend) {
1850 weston_config_section_get_string(section, "backend", &backend,
1851 NULL);
1852 if (!backend)
1853 backend = weston_choose_default_backend();
1854 }
1855
Armin Krezović78a36372016-08-01 18:51:46 +02001856 ec = weston_compositor_create(display, &user_data);
Giulio Camuffobab996e2014-10-12 00:24:25 +03001857 if (ec == NULL) {
1858 weston_log("fatal: failed to create compositor\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001859 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001860 }
1861
Giulio Camuffobab996e2014-10-12 00:24:25 +03001862 if (weston_compositor_init_config(ec, config) < 0)
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001863 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001864
Daniel Díaz75b71972016-10-21 14:03:13 -05001865 weston_config_section_get_bool(section, "require-input",
1866 &require_input, true);
1867 ec->require_input = require_input;
1868
Giulio Camuffo43008c72015-10-17 19:24:15 +03001869 if (load_backend(ec, backend, &argc, argv, config) < 0) {
Giulio Camuffobab996e2014-10-12 00:24:25 +03001870 weston_log("fatal: failed to create compositor backend\n");
Giulio Camuffo3c241b12015-10-03 16:25:16 +03001871 goto out;
Giulio Camuffobab996e2014-10-12 00:24:25 +03001872 }
1873
Armin Krezovića3666d22016-09-30 14:11:04 +02001874 weston_pending_output_coldplug(ec);
1875
Giulio Camuffobab996e2014-10-12 00:24:25 +03001876 catch_signals();
1877 segv_compositor = ec;
1878
1879 if (idle_time < 0)
1880 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
1881 if (idle_time < 0)
1882 idle_time = 300; /* default idle timeout, in seconds */
1883
1884 ec->idle_time = idle_time;
1885 ec->default_pointer_grab = NULL;
1886 ec->exit = handle_exit;
1887
1888 weston_compositor_log_capabilities(ec);
1889
1890 server_socket = getenv("WAYLAND_SERVER_SOCKET");
1891 if (server_socket) {
1892 weston_log("Running with single client\n");
Bryce Harrington25a2bdd2016-08-03 17:40:52 -07001893 if (!safe_strtoint(server_socket, &fd))
Giulio Camuffobab996e2014-10-12 00:24:25 +03001894 fd = -1;
1895 } else {
1896 fd = -1;
1897 }
1898
1899 if (fd != -1) {
1900 primary_client = wl_client_create(display, fd);
1901 if (!primary_client) {
1902 weston_log("fatal: failed to add client: %m\n");
1903 goto out;
1904 }
1905 primary_client_destroyed.notify =
1906 handle_primary_client_destroyed;
1907 wl_client_add_destroy_listener(primary_client,
1908 &primary_client_destroyed);
1909 } else if (weston_create_listening_socket(display, socket_name)) {
1910 goto out;
1911 }
1912
1913 if (!shell)
1914 weston_config_section_get_string(section, "shell", &shell,
1915 "desktop-shell.so");
1916
Quentin Glidicda01c1d2016-12-02 14:17:08 +01001917 if (wet_load_shell(ec, shell, &argc, argv) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +03001918 goto out;
1919
1920 weston_config_section_get_string(section, "modules", &modules, "");
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001921 if (load_modules(ec, modules, &argc, argv, &xwayland) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +03001922 goto out;
1923
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001924 if (load_modules(ec, option_modules, &argc, argv, &xwayland) < 0)
Giulio Camuffobab996e2014-10-12 00:24:25 +03001925 goto out;
1926
Quentin Glidic6d3887b2016-07-04 14:34:48 +02001927 if (!xwayland)
1928 weston_config_section_get_bool(section, "xwayland", &xwayland,
1929 false);
1930 if (xwayland) {
1931 if (wet_load_xwayland(ec) < 0)
1932 goto out;
1933 }
1934
Giulio Camuffobab996e2014-10-12 00:24:25 +03001935 section = weston_config_get_section(config, "keyboard", NULL, NULL);
1936 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
1937 if (numlock_on) {
1938 wl_list_for_each(seat, &ec->seat_list, link) {
Derek Foreman1281a362015-07-31 16:55:32 -05001939 struct weston_keyboard *keyboard =
1940 weston_seat_get_keyboard(seat);
1941
1942 if (keyboard)
1943 weston_keyboard_set_locks(keyboard,
Giulio Camuffobab996e2014-10-12 00:24:25 +03001944 WESTON_NUM_LOCK,
1945 WESTON_NUM_LOCK);
1946 }
1947 }
1948
1949 for (i = 1; i < argc; i++)
1950 weston_log("fatal: unhandled option: %s\n", argv[i]);
1951 if (argc > 1)
1952 goto out;
1953
1954 weston_compositor_wake(ec);
1955
1956 wl_display_run(display);
1957
1958 /* Allow for setting return exit code after
1959 * wl_display_run returns normally. This is
1960 * useful for devs/testers and automated tests
1961 * that want to indicate failure status to
1962 * testing infrastructure above
1963 */
1964 ret = ec->exit_code;
1965
1966out:
Armin Krezovića3666d22016-09-30 14:11:04 +02001967 /* free(NULL) is valid, and it won't be NULL if it's used */
1968 free(user_data.parsed_options);
1969
Giulio Camuffobab996e2014-10-12 00:24:25 +03001970 weston_compositor_destroy(ec);
1971
1972out_signals:
1973 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
1974 if (signals[i])
1975 wl_event_source_remove(signals[i]);
1976
1977 wl_display_destroy(display);
1978
1979 weston_log_file_close();
1980
1981 if (config)
1982 weston_config_destroy(config);
1983 free(config_file);
1984 free(backend);
1985 free(shell);
1986 free(socket_name);
1987 free(option_modules);
1988 free(log);
1989 free(modules);
1990
1991 return ret;
1992}