main.c 46.4 KB
Newer Older
1 2 3 4
/*
 * Copyright © 2010-2011 Intel Corporation
 * Copyright © 2008-2011 Kristian Høgsberg
 * Copyright © 2012-2015 Collabora, Ltd.
5 6
 * Copyright © 2010-2011 Benjamin Franzke
 * Copyright © 2013 Jason Ekstrand
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <dlfcn.h>
36
#include <stdint.h>
37 38 39 40
#include <string.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/wait.h>
41
#include <sys/socket.h>
42
#include <libinput.h>
43
#include <sys/time.h>
44
#include <linux/limits.h>
45 46 47 48 49 50

#ifdef HAVE_LIBUNWIND
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#endif

51
#include "weston.h"
52 53 54 55 56
#include "compositor.h"
#include "../shared/os-compatibility.h"
#include "../shared/helpers.h"
#include "git-version.h"
#include "version.h"
57
#include "weston.h"
58

59
#include "compositor-drm.h"
60
#include "compositor-headless.h"
61
#include "compositor-rdp.h"
62
#include "compositor-fbdev.h"
63
#include "compositor-x11.h"
64 65 66
#include "compositor-wayland.h"

#define WINDOW_TITLE "Weston Compositor"
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static FILE *weston_logfile = NULL;

static int cached_tm_mday = -1;

static int weston_log_timestamp(void)
{
	struct timeval tv;
	struct tm *brokendown_time;
	char string[128];

	gettimeofday(&tv, NULL);

	brokendown_time = localtime(&tv.tv_sec);
	if (brokendown_time == NULL)
		return fprintf(weston_logfile, "[(NULL)localtime] ");

	if (brokendown_time->tm_mday != cached_tm_mday) {
		strftime(string, sizeof string, "%Y-%m-%d %Z", brokendown_time);
		fprintf(weston_logfile, "Date: %s\n", string);

		cached_tm_mday = brokendown_time->tm_mday;
	}

	strftime(string, sizeof string, "%H:%M:%S", brokendown_time);

	return fprintf(weston_logfile, "[%s.%03li] ", string, tv.tv_usec/1000);
}

static void
custom_handler(const char *fmt, va_list arg)
{
	weston_log_timestamp();
	fprintf(weston_logfile, "libwayland: ");
	vfprintf(weston_logfile, fmt, arg);
}

static void
weston_log_file_open(const char *filename)
{
	wl_log_set_handler_server(custom_handler);

	if (filename != NULL) {
		weston_logfile = fopen(filename, "a");
		if (weston_logfile)
			os_fd_set_cloexec(fileno(weston_logfile));
	}

	if (weston_logfile == NULL)
		weston_logfile = stderr;
	else
		setvbuf(weston_logfile, NULL, _IOLBF, 256);
}

static void
weston_log_file_close(void)
{
	if ((weston_logfile != stderr) && (weston_logfile != NULL))
		fclose(weston_logfile);
	weston_logfile = stderr;
}

static int
vlog(const char *fmt, va_list ap)
{
	int l;

	l = weston_log_timestamp();
	l += vfprintf(weston_logfile, fmt, ap);

	return l;
}

static int
vlog_continue(const char *fmt, va_list argp)
{
	return vfprintf(weston_logfile, fmt, argp);
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static struct wl_list child_process_list;
static struct weston_compositor *segv_compositor;

static int
sigchld_handler(int signal_number, void *data)
{
	struct weston_process *p;
	int status;
	pid_t pid;

	while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
		wl_list_for_each(p, &child_process_list, link) {
			if (p->pid == pid)
				break;
		}

		if (&p->link == &child_process_list) {
			weston_log("unknown child process exited\n");
			continue;
		}

		wl_list_remove(&p->link);
		p->cleanup(p, status);
	}

	if (pid < 0 && errno != ECHILD)
		weston_log("waitpid error %m\n");

	return 1;
}

#ifdef HAVE_LIBUNWIND

static void
print_backtrace(void)
{
	unw_cursor_t cursor;
	unw_context_t context;
	unw_word_t off;
	unw_proc_info_t pip;
	int ret, i = 0;
	char procname[256];
	const char *filename;
	Dl_info dlinfo;

	pip.unwind_info = NULL;
	ret = unw_getcontext(&context);
	if (ret) {
		weston_log("unw_getcontext: %d\n", ret);
		return;
	}

	ret = unw_init_local(&cursor, &context);
	if (ret) {
		weston_log("unw_init_local: %d\n", ret);
		return;
	}

	ret = unw_step(&cursor);
	while (ret > 0) {
		ret = unw_get_proc_info(&cursor, &pip);
		if (ret) {
			weston_log("unw_get_proc_info: %d\n", ret);
			break;
		}

		ret = unw_get_proc_name(&cursor, procname, 256, &off);
		if (ret && ret != -UNW_ENOMEM) {
			if (ret != -UNW_EUNSPEC)
				weston_log("unw_get_proc_name: %d\n", ret);
			procname[0] = '?';
			procname[1] = 0;
		}

		if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
		    *dlinfo.dli_fname)
			filename = dlinfo.dli_fname;
		else
			filename = "?";

		weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
			   ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));

		ret = unw_step(&cursor);
		if (ret < 0)
			weston_log("unw_step: %d\n", ret);
	}
}

#else

static void
print_backtrace(void)
{
	void *buffer[32];
	int i, count;
	Dl_info info;

	count = backtrace(buffer, ARRAY_LENGTH(buffer));
	for (i = 0; i < count; i++) {
		dladdr(buffer[i], &info);
		weston_log("  [%016lx]  %s  (%s)\n",
			(long) buffer[i],
			info.dli_sname ? info.dli_sname : "--",
			info.dli_fname);
	}
}

#endif

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
static void
child_client_exec(int sockfd, const char *path)
{
	int clientfd;
	char s[32];
	sigset_t allsigs;

	/* do not give our signal mask to the new process */
	sigfillset(&allsigs);
	sigprocmask(SIG_UNBLOCK, &allsigs, NULL);

	/* Launch clients as the user. Do not lauch clients with wrong euid.*/
	if (seteuid(getuid()) == -1) {
		weston_log("compositor: failed seteuid\n");
		return;
	}

	/* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
	 * non-CLOEXEC fd to pass through exec. */
	clientfd = dup(sockfd);
	if (clientfd == -1) {
		weston_log("compositor: dup failed: %m\n");
		return;
	}

	snprintf(s, sizeof s, "%d", clientfd);
	setenv("WAYLAND_SOCKET", s, 1);

	if (execl(path, path, NULL) < 0)
		weston_log("compositor: executing '%s' failed: %m\n",
			path);
}

WL_EXPORT struct wl_client *
weston_client_launch(struct weston_compositor *compositor,
		     struct weston_process *proc,
		     const char *path,
		     weston_process_cleanup_func_t cleanup)
{
	int sv[2];
	pid_t pid;
	struct wl_client *client;

	weston_log("launching '%s'\n", path);

	if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
		weston_log("weston_client_launch: "
			"socketpair failed while launching '%s': %m\n",
			path);
		return NULL;
	}

	pid = fork();
	if (pid == -1) {
		close(sv[0]);
		close(sv[1]);
		weston_log("weston_client_launch: "
			"fork failed while launching '%s': %m\n", path);
		return NULL;
	}

	if (pid == 0) {
		child_client_exec(sv[1], path);
		_exit(-1);
	}

	close(sv[1]);

	client = wl_client_create(compositor->wl_display, sv[0]);
	if (!client) {
		close(sv[0]);
		weston_log("weston_client_launch: "
			"wl_client_create failed while launching '%s'.\n",
			path);
		return NULL;
	}

	proc->pid = pid;
	proc->cleanup = cleanup;
	weston_watch_process(proc);

	return client;
}

340 341 342 343 344 345
WL_EXPORT void
weston_watch_process(struct weston_process *process)
{
	wl_list_insert(&child_process_list, &process->link);
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
struct process_info {
	struct weston_process proc;
	char *path;
};

static void
process_handle_sigchld(struct weston_process *process, int status)
{
	struct process_info *pinfo =
		container_of(process, struct process_info, proc);

	/*
	 * There are no guarantees whether this runs before or after
	 * the wl_client destructor.
	 */

	if (WIFEXITED(status)) {
		weston_log("%s exited with status %d\n", pinfo->path,
			   WEXITSTATUS(status));
	} else if (WIFSIGNALED(status)) {
		weston_log("%s died on signal %d\n", pinfo->path,
			   WTERMSIG(status));
	} else {
		weston_log("%s disappeared\n", pinfo->path);
	}

	free(pinfo->path);
	free(pinfo);
}

WL_EXPORT struct wl_client *
weston_client_start(struct weston_compositor *compositor, const char *path)
{
	struct process_info *pinfo;
	struct wl_client *client;

	pinfo = zalloc(sizeof *pinfo);
	if (!pinfo)
		return NULL;

	pinfo->path = strdup(path);
	if (!pinfo->path)
		goto out_free;

	client = weston_client_launch(compositor, &pinfo->proc, path,
				      process_handle_sigchld);
	if (!client)
		goto out_str;

	return client;

out_str:
	free(pinfo->path);

out_free:
	free(pinfo);

	return NULL;
}

406 407 408 409 410 411 412 413 414 415 416
static void
log_uname(void)
{
	struct utsname usys;

	uname(&usys);

	weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
						usys.version, usys.machine);
}

417 418 419 420 421 422
WL_EXPORT struct weston_config *
wet_get_config(struct weston_compositor *compositor)
{
	return weston_compositor_get_user_data(compositor);
}

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
static const char xdg_error_message[] =
	"fatal: environment variable XDG_RUNTIME_DIR is not set.\n";

static const char xdg_wrong_message[] =
	"fatal: environment variable XDG_RUNTIME_DIR\n"
	"is set to \"%s\", which is not a directory.\n";

static const char xdg_wrong_mode_message[] =
	"warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
	"correctly.  Unix access mode must be 0700 (current mode is %o),\n"
	"and must be owned by the user (current owner is UID %d).\n";

static const char xdg_detail_message[] =
	"Refer to your distribution on how to get it, or\n"
	"http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
	"on how to implement it.\n";

static void
verify_xdg_runtime_dir(void)
{
	char *dir = getenv("XDG_RUNTIME_DIR");
	struct stat s;

	if (!dir) {
		weston_log(xdg_error_message);
		weston_log_continue(xdg_detail_message);
		exit(EXIT_FAILURE);
	}

	if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
		weston_log(xdg_wrong_message, dir);
		weston_log_continue(xdg_detail_message);
		exit(EXIT_FAILURE);
	}

	if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
		weston_log(xdg_wrong_mode_message,
			   dir, s.st_mode & 0777, s.st_uid);
		weston_log_continue(xdg_detail_message);
	}
}

static int
usage(int error_code)
{
	fprintf(stderr,
		"Usage: weston [OPTIONS]\n\n"
		"This is weston version " VERSION ", the Wayland reference compositor.\n"
		"Weston supports multiple backends, and depending on which backend is in use\n"
		"different options will be accepted.\n\n"


		"Core options:\n\n"
		"  --version\t\tPrint weston version\n"
		"  -B, --backend=MODULE\tBackend module, one of\n"
#if defined(BUILD_DRM_COMPOSITOR)
			"\t\t\t\tdrm-backend.so\n"
#endif
#if defined(BUILD_FBDEV_COMPOSITOR)
			"\t\t\t\tfbdev-backend.so\n"
#endif
484 485
#if defined(BUILD_HEADLESS_COMPOSITOR)
			"\t\t\t\theadless-backend.so\n"
486 487 488 489
#endif
#if defined(BUILD_RDP_COMPOSITOR)
			"\t\t\t\trdp-backend.so\n"
#endif
490 491 492 493 494
#if defined(BUILD_WAYLAND_COMPOSITOR)
			"\t\t\t\twayland-backend.so\n"
#endif
#if defined(BUILD_X11_COMPOSITOR)
			"\t\t\t\tx11-backend.so\n"
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
#endif
		"  --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
		"  -S, --socket=NAME\tName of socket to listen on\n"
		"  -i, --idle-time=SECS\tIdle time in seconds\n"
		"  --modules\t\tLoad the comma-separated list of modules\n"
		"  --log=FILE\t\tLog to the given file\n"
		"  -c, --config=FILE\tConfig file to load, defaults to weston.ini\n"
		"  --no-config\t\tDo not read weston.ini\n"
		"  -h, --help\t\tThis help message\n\n");

#if defined(BUILD_DRM_COMPOSITOR)
	fprintf(stderr,
		"Options for drm-backend.so:\n\n"
		"  --connector=ID\tBring up only this connector\n"
		"  --seat=SEAT\t\tThe seat that weston should run on\n"
		"  --tty=TTY\t\tThe tty to use\n"
		"  --use-pixman\t\tUse the pixman (CPU) renderer\n"
		"  --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
#endif

#if defined(BUILD_FBDEV_COMPOSITOR)
	fprintf(stderr,
		"Options for fbdev-backend.so:\n\n"
		"  --tty=TTY\t\tThe tty to use\n"
		"  --device=DEVICE\tThe framebuffer device to use\n"
520
		"\n");
521 522
#endif

523
#if defined(BUILD_HEADLESS_COMPOSITOR)
524
	fprintf(stderr,
525 526 527
		"Options for headless-backend.so:\n\n"
		"  --width=WIDTH\t\tWidth of memory surface\n"
		"  --height=HEIGHT\tHeight of memory surface\n"
528 529
		"  --transform=TR\tThe output transformation, TR is one of:\n"
		"\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
530 531 532
		"  --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n"
		"  --no-outputs\t\tDo not create any virtual outputs\n"
		"\n");
533 534 535 536 537 538 539
#endif

#if defined(BUILD_RDP_COMPOSITOR)
	fprintf(stderr,
		"Options for rdp-backend.so:\n\n"
		"  --width=WIDTH\t\tWidth of desktop\n"
		"  --height=HEIGHT\tHeight of desktop\n"
540
		"  --env-socket\t\tUse socket defined in RDP_FD env variable as peer connection\n"
541 542 543 544 545 546 547 548 549
		"  --address=ADDR\tThe address to bind\n"
		"  --port=PORT\t\tThe port to listen on\n"
		"  --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
		"  --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
		"  --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
		"  --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
		"\n");
#endif

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
#if defined(BUILD_WAYLAND_COMPOSITOR)
	fprintf(stderr,
		"Options for wayland-backend.so:\n\n"
		"  --width=WIDTH\t\tWidth of Wayland surface\n"
		"  --height=HEIGHT\tHeight of Wayland surface\n"
		"  --scale=SCALE\t\tScale factor of output\n"
		"  --fullscreen\t\tRun in fullscreen mode\n"
		"  --use-pixman\t\tUse the pixman (CPU) renderer\n"
		"  --output-count=COUNT\tCreate multiple outputs\n"
		"  --sprawl\t\tCreate one fullscreen output for every parent output\n"
		"  --display=DISPLAY\tWayland display to connect to\n\n");
#endif

#if defined(BUILD_X11_COMPOSITOR)
	fprintf(stderr,
		"Options for x11-backend.so:\n\n"
		"  --width=WIDTH\t\tWidth of X window\n"
		"  --height=HEIGHT\tHeight of X window\n"
		"  --scale=SCALE\t\tScale factor of output\n"
		"  --fullscreen\t\tRun in fullscreen mode\n"
		"  --use-pixman\t\tUse the pixman (CPU) renderer\n"
		"  --output-count=COUNT\tCreate multiple outputs\n"
		"  --no-input\t\tDont create input devices\n\n");
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
#endif

	exit(error_code);
}

static int on_term_signal(int signal_number, void *data)
{
	struct wl_display *display = data;

	weston_log("caught signal %d\n", signal_number);
	wl_display_terminate(display);

	return 1;
}

static void
on_caught_signal(int s, siginfo_t *siginfo, void *context)
{
	/* This signal handler will do a best-effort backtrace, and
	 * then call the backend restore function, which will switch
	 * back to the vt we launched from or ungrab X etc and then
	 * raise SIGTRAP.  If we run weston under gdb from X or a
	 * different vt, and tell gdb "handle *s* nostop", this
	 * will allow weston to switch back to gdb on crash and then
	 * gdb will catch the crash with SIGTRAP.*/

	weston_log("caught signal: %d\n", s);

	print_backtrace();

	segv_compositor->backend->restore(segv_compositor);

	raise(SIGTRAP);
}

static void
catch_signals(void)
{
	struct sigaction action;

	action.sa_flags = SA_SIGINFO | SA_RESETHAND;
	action.sa_sigaction = on_caught_signal;
	sigemptyset(&action.sa_mask);
	sigaction(SIGSEGV, &action, NULL);
	sigaction(SIGABRT, &action, NULL);
}

static const char *
clock_name(clockid_t clk_id)
{
	static const char *names[] = {
		[CLOCK_REALTIME] =		"CLOCK_REALTIME",
		[CLOCK_MONOTONIC] =		"CLOCK_MONOTONIC",
		[CLOCK_MONOTONIC_RAW] =		"CLOCK_MONOTONIC_RAW",
		[CLOCK_REALTIME_COARSE] =	"CLOCK_REALTIME_COARSE",
		[CLOCK_MONOTONIC_COARSE] =	"CLOCK_MONOTONIC_COARSE",
629
#ifdef CLOCK_BOOTTIME
630
		[CLOCK_BOOTTIME] =		"CLOCK_BOOTTIME",
631
#endif
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
	};

	if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
		return "unknown";

	return names[clk_id];
}

static const struct {
	uint32_t bit; /* enum weston_capability */
	const char *desc;
} capability_strings[] = {
	{ WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
	{ WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
};

static void
weston_compositor_log_capabilities(struct weston_compositor *compositor)
{
	unsigned i;
	int yes;
653
	struct timespec res;
654 655 656 657 658 659 660 661 662 663 664 665

	weston_log("Compositor capabilities:\n");
	for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
		yes = compositor->capabilities & capability_strings[i].bit;
		weston_log_continue(STAMP_SPACE "%s %s\n",
				    capability_strings[i].desc,
				    yes ? "yes" : "no");
	}

	weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
			    clock_name(compositor->presentation_clock),
			    compositor->presentation_clock);
666 667 668 669 670 671 672 673

	if (clock_getres(compositor->presentation_clock, &res) == 0)
		weston_log_continue(STAMP_SPACE
				"presentation clock resolution: %d.%09ld s\n",
				(int)res.tv_sec, res.tv_nsec);
	else
		weston_log_continue(STAMP_SPACE
				"presentation clock resolution: N/A\n");
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
}

static void
handle_primary_client_destroyed(struct wl_listener *listener, void *data)
{
	struct wl_client *client = data;

	weston_log("Primary client died.  Closing...\n");

	wl_display_terminate(wl_client_get_display(client));
}

static int
weston_create_listening_socket(struct wl_display *display, const char *socket_name)
{
	if (socket_name) {
		if (wl_display_add_socket(display, socket_name)) {
			weston_log("fatal: failed to add socket: %m\n");
			return -1;
		}
	} else {
		socket_name = wl_display_add_socket_auto(display);
		if (!socket_name) {
			weston_log("fatal: failed to add socket: %m\n");
			return -1;
		}
	}

	setenv("WAYLAND_DISPLAY", socket_name, 1);

	return 0;
}

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
WL_EXPORT void *
wet_load_module(const char *name, const char *entrypoint)
{
	const char *builddir = getenv("WESTON_BUILD_DIR");
	char path[PATH_MAX];
	void *module, *init;

	if (name == NULL)
		return NULL;

	if (name[0] != '/') {
		if (builddir)
			snprintf(path, sizeof path, "%s/.libs/%s", builddir, name);
		else
			snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
	} else {
		snprintf(path, sizeof path, "%s", name);
	}

	module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
	if (module) {
		weston_log("Module '%s' already loaded\n", path);
		dlclose(module);
		return NULL;
	}

	weston_log("Loading module '%s'\n", path);
	module = dlopen(path, RTLD_NOW);
	if (!module) {
		weston_log("Failed to load module: %s\n", dlerror());
		return NULL;
	}

	init = dlsym(module, entrypoint);
	if (!init) {
		weston_log("Failed to lookup init function: %s\n", dlerror());
		dlclose(module);
		return NULL;
	}

	return init;
}

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
static int
load_modules(struct weston_compositor *ec, const char *modules,
	     int *argc, char *argv[])
{
	const char *p, *end;
	char buffer[256];
	int (*module_init)(struct weston_compositor *ec,
			   int *argc, char *argv[]);

	if (modules == NULL)
		return 0;

	p = modules;
	while (*p) {
		end = strchrnul(p, ',');
		snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
766 767 768 769 770 771 772 773 774 775 776

		if (strstr(buffer, "xwayland.so")) {
			if (wet_load_xwayland(ec) < 0)
				return -1;
		} else {
			module_init = wet_load_module(buffer, "module_init");
			if (!module_init)
				return -1;
			if (module_init(ec, argc, argv) < 0)
				return -1;
		}
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
		p = end;
		while (*p == ',')
			p++;

	}

	return 0;
}

static int
weston_compositor_init_config(struct weston_compositor *ec,
			      struct weston_config *config)
{
	struct xkb_rule_names xkb_names;
	struct weston_config_section *s;
	int repaint_msec;
793
	int vt_switching;
794 795 796 797 798 799 800 801 802 803 804 805 806

	s = weston_config_get_section(config, "keyboard", NULL, NULL);
	weston_config_section_get_string(s, "keymap_rules",
					 (char **) &xkb_names.rules, NULL);
	weston_config_section_get_string(s, "keymap_model",
					 (char **) &xkb_names.model, NULL);
	weston_config_section_get_string(s, "keymap_layout",
					 (char **) &xkb_names.layout, NULL);
	weston_config_section_get_string(s, "keymap_variant",
					 (char **) &xkb_names.variant, NULL);
	weston_config_section_get_string(s, "keymap_options",
					 (char **) &xkb_names.options, NULL);

807
	if (weston_compositor_set_xkb_rule_names(ec, &xkb_names) < 0)
808 809 810 811 812 813 814
		return -1;

	weston_config_section_get_int(s, "repeat-rate",
				      &ec->kb_repeat_rate, 40);
	weston_config_section_get_int(s, "repeat-delay",
				      &ec->kb_repeat_delay, 400);

815 816 817 818
	weston_config_section_get_bool(s, "vt-switching",
				       &vt_switching, true);
	ec->vt_switching = vt_switching;

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
	s = weston_config_get_section(config, "core", NULL, NULL);
	weston_config_section_get_int(s, "repaint-window", &repaint_msec,
				      ec->repaint_msec);
	if (repaint_msec < -10 || repaint_msec > 1000) {
		weston_log("Invalid repaint_window value in config: %d\n",
			   repaint_msec);
	} else {
		ec->repaint_msec = repaint_msec;
	}
	weston_log("Output repaint window is %d ms maximum.\n",
		   ec->repaint_msec);

	return 0;
}

static char *
weston_choose_default_backend(void)
{
	char *backend = NULL;

	if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
		backend = strdup("wayland-backend.so");
	else if (getenv("DISPLAY"))
		backend = strdup("x11-backend.so");
	else
		backend = strdup(WESTON_NATIVE_BACKEND);

	return backend;
}

static const struct { const char *name; uint32_t token; } transforms[] = {
	{ "normal",     WL_OUTPUT_TRANSFORM_NORMAL },
	{ "90",         WL_OUTPUT_TRANSFORM_90 },
	{ "180",        WL_OUTPUT_TRANSFORM_180 },
	{ "270",        WL_OUTPUT_TRANSFORM_270 },
	{ "flipped",    WL_OUTPUT_TRANSFORM_FLIPPED },
	{ "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
	{ "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
	{ "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
};

WL_EXPORT int
weston_parse_transform(const char *transform, uint32_t *out)
{
	unsigned int i;

	for (i = 0; i < ARRAY_LENGTH(transforms); i++)
		if (strcmp(transforms[i].name, transform) == 0) {
			*out = transforms[i].token;
			return 0;
		}

	*out = WL_OUTPUT_TRANSFORM_NORMAL;
	return -1;
}

WL_EXPORT const char *
weston_transform_to_string(uint32_t output_transform)
{
	unsigned int i;

	for (i = 0; i < ARRAY_LENGTH(transforms); i++)
		if (transforms[i].token == output_transform)
			return transforms[i].name;

	return "<illegal value>";
}

static int
load_configuration(struct weston_config **config, int32_t noconfig,
		   const char *config_file)
{
	const char *file = "weston.ini";
	const char *full_path;

	*config = NULL;

	if (config_file)
		file = config_file;

	if (noconfig == 0)
		*config = weston_config_parse(file);

	if (*config) {
		full_path = weston_config_get_full_path(*config);

		weston_log("Using config file '%s'\n", full_path);
		setenv(WESTON_CONFIG_FILE_ENV_VAR, full_path, 1);

		return 0;
	}

	if (config_file && noconfig == 0) {
		weston_log("fatal: error opening or reading config file"
			   " '%s'.\n", config_file);

		return -1;
	}

	weston_log("Starting with no config file.\n");
	setenv(WESTON_CONFIG_FILE_ENV_VAR, "", 1);

	return 0;
}

static void
handle_exit(struct weston_compositor *c)
{
	wl_display_terminate(c->wl_display);
}

930 931 932 933 934 935
static enum weston_drm_backend_output_mode
drm_configure_output(struct weston_compositor *c,
		     bool use_current_mode,
		     const char *name,
		     struct weston_drm_backend_output_config *config)
{
936
	struct weston_config *wc = wet_get_config(c);
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
	struct weston_config_section *section;
	char *s;
	int scale;
	enum weston_drm_backend_output_mode mode =
		WESTON_DRM_BACKEND_OUTPUT_PREFERRED;

	section = weston_config_get_section(wc, "output", "name", name);
	weston_config_section_get_string(section, "mode", &s, "preferred");
	if (strcmp(s, "off") == 0) {
		free(s);
		return WESTON_DRM_BACKEND_OUTPUT_OFF;
	}

	if (use_current_mode || strcmp(s, "current") == 0) {
		mode = WESTON_DRM_BACKEND_OUTPUT_CURRENT;
	} else if (strcmp(s, "preferred") != 0) {
		config->modeline = s;
		s = NULL;
	}
	free(s);

	weston_config_section_get_int(section, "scale", &scale, 1);
	config->base.scale = scale >= 1 ? scale : 1;
	weston_config_section_get_string(section, "transform", &s, "normal");
	if (weston_parse_transform(s, &config->base.transform) < 0)
		weston_log("Invalid transform \"%s\" for output %s\n",
			   s, name);
	free(s);

	weston_config_section_get_string(section,
					 "gbm-format", &config->gbm_format, NULL);
	weston_config_section_get_string(section, "seat", &config->seat, "");
	return mode;
}

972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
static void
configure_input_device(struct weston_compositor *compositor,
		       struct libinput_device *device)
{
	struct weston_config_section *s;
	struct weston_config *config = wet_get_config(compositor);
	int enable_tap;
	int enable_tap_default;

	s = weston_config_get_section(config,
				      "libinput", NULL, NULL);

	if (libinput_device_config_tap_get_finger_count(device) > 0) {
		enable_tap_default =
			libinput_device_config_tap_get_default_enabled(
				device);
		weston_config_section_get_bool(s, "enable_tap",
					       &enable_tap,
					       enable_tap_default);
		libinput_device_config_tap_set_enabled(device,
						       enable_tap);
	}
}

996
static int
997
load_drm_backend(struct weston_compositor *c,
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
		 int *argc, char **argv, struct weston_config *wc)
{
	struct weston_drm_backend_config config = {{ 0, }};
	struct weston_config_section *section;
	int ret = 0;

	const struct weston_option options[] = {
		{ WESTON_OPTION_INTEGER, "connector", 0, &config.connector },
		{ WESTON_OPTION_STRING, "seat", 0, &config.seat_id },
		{ WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
		{ WESTON_OPTION_BOOLEAN, "current-mode", 0, &config.use_current_mode },
		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
	};

	parse_options(options, ARRAY_LENGTH(options), argc, argv);

	section = weston_config_get_section(wc, "core", NULL, NULL);
	weston_config_section_get_string(section,
					 "gbm-format", &config.gbm_format,
					 NULL);

	config.base.struct_version = WESTON_DRM_BACKEND_CONFIG_VERSION;
	config.base.struct_size = sizeof(struct weston_drm_backend_config);
	config.configure_output = drm_configure_output;
1022
	config.configure_device = configure_input_device;
1023

1024 1025
	ret = weston_compositor_load_backend(c, WESTON_BACKEND_DRM,
					     &config.base);
1026 1027 1028 1029 1030 1031 1032

	free(config.gbm_format);
	free(config.seat_id);

	return ret;
}

1033
static int
1034
load_headless_backend(struct weston_compositor *c,
1035 1036 1037 1038
		      int *argc, char **argv, struct weston_config *wc)
{
	struct weston_headless_backend_config config = {{ 0, }};
	int ret = 0;
1039
	char *transform = NULL;
1040 1041 1042 1043 1044 1045 1046 1047 1048

	config.width = 1024;
	config.height = 640;

	const struct weston_option options[] = {
		{ WESTON_OPTION_INTEGER, "width", 0, &config.width },
		{ WESTON_OPTION_INTEGER, "height", 0, &config.height },
		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
		{ WESTON_OPTION_STRING, "transform", 0, &transform },
1049
		{ WESTON_OPTION_BOOLEAN, "no-outputs", 0, &config.no_outputs },
1050 1051 1052 1053
	};

	parse_options(options, ARRAY_LENGTH(options), argc, argv);

1054 1055 1056 1057 1058 1059
	config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
	if (transform) {
		if (weston_parse_transform(transform, &config.transform) < 0)
			weston_log("Invalid transform \"%s\"\n", transform);
		free(transform);
	}
1060 1061 1062 1063 1064

	config.base.struct_version = WESTON_HEADLESS_BACKEND_CONFIG_VERSION;
	config.base.struct_size = sizeof(struct weston_headless_backend_config);

	/* load the actual wayland backend and configure it */
1065 1066
	ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
					     &config.base);
1067 1068 1069 1070

	return ret;
}

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
static void
weston_rdp_backend_config_init(struct weston_rdp_backend_config *config)
{
	config->base.struct_version = WESTON_RDP_BACKEND_CONFIG_VERSION;
	config->base.struct_size = sizeof(struct weston_rdp_backend_config);

	config->width = 640;
	config->height = 480;
	config->bind_address = NULL;
	config->port = 3389;
	config->rdp_key = NULL;
	config->server_cert = NULL;
	config->server_key = NULL;
	config->env_socket = 0;
	config->no_clients_resize = 0;
}

static int
1089
load_rdp_backend(struct weston_compositor *c,
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
		int *argc, char *argv[], struct weston_config *wc)
{
	struct weston_rdp_backend_config config  = {{ 0, }};
	int ret = 0;

	weston_rdp_backend_config_init(&config);

	const struct weston_option rdp_options[] = {
		{ WESTON_OPTION_BOOLEAN, "env-socket", 0, &config.env_socket },
		{ WESTON_OPTION_INTEGER, "width", 0, &config.width },
		{ WESTON_OPTION_INTEGER, "height", 0, &config.height },
		{ WESTON_OPTION_STRING,  "address", 0, &config.bind_address },
		{ WESTON_OPTION_INTEGER, "port", 0, &config.port },
		{ WESTON_OPTION_BOOLEAN, "no-clients-resize", 0, &config.no_clients_resize },
		{ WESTON_OPTION_STRING,  "rdp4-key", 0, &config.rdp_key },
		{ WESTON_OPTION_STRING,  "rdp-tls-cert", 0, &config.server_cert },
		{ WESTON_OPTION_STRING,  "rdp-tls-key", 0, &config.server_key }
	};

	parse_options(rdp_options, ARRAY_LENGTH(rdp_options), argc, argv);

1111 1112
	ret = weston_compositor_load_backend(c, WESTON_BACKEND_RDP,
					     &config.base);
1113 1114 1115 1116 1117 1118 1119 1120

	free(config.bind_address);
	free(config.rdp_key);
	free(config.server_cert);
	free(config.server_key);
	return ret;
}

1121
static int
1122
load_fbdev_backend(struct weston_compositor *c,
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
		      int *argc, char **argv, struct weston_config *wc)
{
	struct weston_fbdev_backend_config config = {{ 0, }};
	struct weston_config_section *section;
	char *s = NULL;
	int ret = 0;

	const struct weston_option fbdev_options[] = {
		{ WESTON_OPTION_INTEGER, "tty", 0, &config.tty },
		{ WESTON_OPTION_STRING, "device", 0, &config.device },
	};

	parse_options(fbdev_options, ARRAY_LENGTH(fbdev_options), argc, argv);

	if (!config.device)
		config.device = strdup("/dev/fb0");

	section = weston_config_get_section(wc, "output", "name", "fbdev");
	weston_config_section_get_string(section, "transform", &s, "normal");
	if (weston_parse_transform(s, &config.output_transform) < 0)
		weston_log("Invalid transform \"%s\" for output fbdev\n", s);
	free(s);

	config.base.struct_version = WESTON_FBDEV_BACKEND_CONFIG_VERSION;
	config.base.struct_size = sizeof(struct weston_fbdev_backend_config);
1148
	config.configure_device = configure_input_device;
1149 1150

	/* load the actual wayland backend and configure it */
1151 1152
	ret = weston_compositor_load_backend(c, WESTON_BACKEND_FBDEV,
					     &config.base);
1153 1154

	free(config.device);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

	return ret;
}

static int
weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
					       struct weston_x11_backend_output_config *output_config) {
	struct weston_x11_backend_output_config *new_outputs;

	new_outputs = realloc(config->outputs, (config->num_outputs+1) *
			      sizeof(struct weston_x11_backend_output_config));
	if (new_outputs == NULL)
		return -1;

	config->outputs = new_outputs;
	config->outputs[config->num_outputs].width = output_config->width;
	config->outputs[config->num_outputs].height = output_config->height;
	config->outputs[config->num_outputs].transform = output_config->transform;
	config->outputs[config->num_outputs].scale = output_config->scale;
	config->outputs[config->num_outputs].name = strdup(output_config->name);
	config->num_outputs++;

	return 0;
}

static int
1181
load_x11_backend(struct weston_compositor *c,
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
		 int *argc, char **argv, struct weston_config *wc)
{
	struct weston_x11_backend_output_config default_output;
	struct weston_x11_backend_config config = {{ 0, }};
	struct weston_config_section *section;
	int ret = 0;
	int option_width = 0;
	int option_height = 0;
	int option_scale = 0;
	int option_count = 1;
	int output_count = 0;
	char const *section_name;
	int i;
	uint32_t j;

	const struct weston_option options[] = {
	       { WESTON_OPTION_INTEGER, "width", 0, &option_width },
	       { WESTON_OPTION_INTEGER, "height", 0, &option_height },
	       { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
	       { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
	       { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
	       { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
	       { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
	};

	parse_options(options, ARRAY_LENGTH(options), argc, argv);

	section = NULL;
	while (weston_config_next_section(wc, &section, &section_name)) {
		struct weston_x11_backend_output_config current_output = { 0, };
		char *t;
		char *mode;

		if (strcmp(section_name, "output") != 0) {
			continue;
		}

		weston_config_section_get_string(section, "name", &current_output.name, NULL);
		if (current_output.name == NULL || current_output.name[0] != 'X') {
			free(current_output.name);
			continue;
		}

		weston_config_section_get_string(section, "mode", &mode, "1024x600");
		if (sscanf(mode, "%dx%d", &current_output.width,
			   &current_output.height) != 2) {
			weston_log("Invalid mode \"%s\" for output %s\n",
				   mode, current_output.name);
			current_output.width = 1024;
			current_output.height = 600;
		}
		free(mode);
		if (current_output.width < 1)
			current_output.width = 1024;
		if (current_output.height < 1)
			current_output.height = 600;
		if (option_width)
			current_output.width = option_width;
		if (option_height)
			current_output.height = option_height;

		weston_config_section_get_int(section, "scale", &current_output.scale, 1);
		if (option_scale)
			current_output.scale = option_scale;

		weston_config_section_get_string(section,
						 "transform", &t, "normal");
		if (weston_parse_transform(t, &current_output.transform) < 0)
			weston_log("Invalid transform \"%s\" for output %s\n",
				   t, current_output.name);
		free(t);

		if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
			ret = -1;
			goto out;
		}

		output_count++;
1260
		if (output_count >= option_count)
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
			break;
	}

	default_output.name = NULL;
	default_output.width = option_width ? option_width : 1024;
	default_output.height = option_height ? option_height : 600;
	default_output.scale = option_scale ? option_scale : 1;
	default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;

	for (i = output_count; i < option_count; i++) {
		if (asprintf(&default_output.name, "screen%d", i) < 0) {
			ret = -1;
			goto out;
		}

		if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
			ret = -1;
			free(default_output.name);
			goto out;
		}
		free(default_output.name);
	}

	config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
	config.base.struct_size = sizeof(struct weston_x11_backend_config);

	/* load the actual backend and configure it */
1288 1289
	ret = weston_compositor_load_backend(c, WESTON_BACKEND_X11,
					     &config.base);
1290 1291 1292 1293 1294 1295

out:
	for (j = 0; j < config.num_outputs; ++j)
		free(config.outputs[j].name);
	free(config.outputs);

1296 1297 1298
	return ret;
}

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
static void
weston_wayland_output_config_init(struct weston_wayland_backend_output_config *output_config,
				  struct weston_config_section *config_section,
				  int option_width, int option_height,
				  int option_scale)
{
	char *mode, *t, *str;
	unsigned int slen;

	weston_config_section_get_string(config_section, "name", &output_config->name,
					 NULL);
	if (output_config->name) {
		slen = strlen(output_config->name);
		slen += strlen(WINDOW_TITLE " - ");
		str = malloc(slen + 1);
		if (str)
			snprintf(str, slen + 1, WINDOW_TITLE " - %s",
				 output_config->name);
		free(output_config->name);
		output_config->name = str;
	}
	if (!output_config->name)
		output_config->name = strdup(WINDOW_TITLE);

	weston_config_section_get_string(config_section,
					 "mode", &mode, "1024x600");
	if (sscanf(mode, "%dx%d", &output_config->width, &output_config->height) != 2) {
		weston_log("Invalid mode \"%s\" for output %s\n",
			   mode, output_config->name);
		output_config->width = 1024;
		output_config->height = 640;
	}
	free(mode);

	if (option_width)
		output_config->width = option_width;
	if (option_height)
		output_config->height = option_height;

	weston_config_section_get_int(config_section, "scale", &output_config->scale, 1);

	if (option_scale)
		output_config->scale = option_scale;

	weston_config_section_get_string(config_section,
					 "transform", &t, "normal");
	if (weston_parse_transform(t, &output_config->transform) < 0)
		weston_log("Invalid transform \"%s\" for output %s\n",
			   t, output_config->name);
	free(t);

}

static void
1353
weston_wayland_backend_config_release(struct weston_wayland_backend_config *config)
1354 1355 1356
{
	int i;

1357 1358
	for (i = 0; i < config->num_outputs; ++i) {
		free(config->outputs[i].name);
1359
	}
1360 1361 1362
	free(config->cursor_theme);
	free(config->display_name);
	free(config->outputs);
1363 1364 1365 1366 1367 1368 1369 1370
}

/*
 * Append a new output struct at the end of new_config.outputs and return a
 * pointer to the newly allocated structure or NULL if fail. The allocated
 * structure is NOT cleared nor set to default values.
 */
static struct weston_wayland_backend_output_config *
1371
weston_wayland_backend_config_add_new_output(struct weston_wayland_backend_config *config)
1372 1373 1374 1375
{
	struct weston_wayland_backend_output_config *outputs;
	const size_t element_size = sizeof(struct weston_wayland_backend_output_config);

1376 1377
	outputs = realloc(config->outputs,
			  (config->num_outputs + 1) * element_size);
1378 1379
	if (!outputs)
		return NULL;
1380 1381 1382
	config->num_outputs += 1;
	config->outputs = outputs;
	return &(config->outputs[config->num_outputs - 1]);
1383 1384 1385 1386
}

static int
load_wayland_backend_config(struct weston_compositor *compositor, int *argc,
1387
			    char *argv[], struct weston_config *wc,
1388
			    struct weston_wayland_backend_config *config)
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
{
	struct weston_config_section *section;
	struct weston_wayland_backend_output_config *oc;
	int count, width, height, scale;
	const char *section_name;
	char *name;

	const struct weston_option wayland_options[] = {
		{ WESTON_OPTION_INTEGER, "width", 0, &width },
		{ WESTON_OPTION_INTEGER, "height", 0, &height },
		{ WESTON_OPTION_INTEGER, "scale", 0, &scale },
1400 1401
		{ WESTON_OPTION_STRING, "display", 0, &config->display_name },
		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config->use_pixman },
1402
		{ WESTON_OPTION_INTEGER, "output-count", 0, &count },
1403 1404
		{ WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config->fullscreen },
		{ WESTON_OPTION_BOOLEAN, "sprawl", 0, &config->sprawl },
1405 1406 1407 1408 1409
	};

	width = 0;
	height = 0;
	scale = 0;
1410 1411
	config->display_name = NULL;
	config->use_pixman = 0;
1412
	count = 1;
1413 1414
	config->fullscreen = 0;
	config->sprawl = 0;
1415 1416 1417
	parse_options(wayland_options,
		      ARRAY_LENGTH(wayland_options), argc, argv);

1418 1419 1420 1421
	config->cursor_size = 32;
	config->cursor_theme = NULL;
	config->base.struct_size = sizeof(struct weston_wayland_backend_config);
	config->base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
1422

1423
	section = weston_config_get_section(wc, "shell", NULL, NULL);
1424
	weston_config_section_get_string(section, "cursor-theme",
1425
					 &config->cursor_theme, NULL);
1426
	weston_config_section_get_int(section, "cursor-size",
1427
				      &config->cursor_size, 32);
1428

1429
	if (config->sprawl) {
1430 1431 1432 1433
		/* do nothing, everything is already set */
		return 0;
	}

1434
	if (config->fullscreen) {
1435
		oc = weston_wayland_backend_config_add_new_output(config);
1436
		if (!oc)
1437
			return -1;
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

		oc->width = width;
		oc->height = height;
		oc->name = NULL;
		oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
		oc->scale = 1;

		return 0;
	}

	section = NULL;
1449
	while (weston_config_next_section(wc, &section, &section_name)) {
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
		if (!section_name || strcmp(section_name, "output") != 0)
			continue;
		weston_config_section_get_string(section, "name", &name, NULL);
		if (name == NULL)
			continue;

		if (name[0] != 'W' || name[1] != 'L') {
			free(name);
			continue;
		}
		free(name);

1462
		oc = weston_wayland_backend_config_add_new_output(config);
1463
		if (!oc)
1464
			return -1;
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

		weston_wayland_output_config_init(oc, section, width,
						  height, scale);
		--count;
	}

	if (!width)
		width = 1024;
	if (!height)
		height = 640;
	if (!scale)
		scale = 1;

1478
	while (count > 0) {
1479
		oc = weston_wayland_backend_config_add_new_output(config);
1480
		if (!oc)
1481
			return -1;
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495

		oc->width = width;
		oc->height = height;
		oc->name = NULL;
		oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
		oc->scale = scale;

		--count;
	}

	return 0;
}

static int
1496
load_wayland_backend(struct weston_compositor *c,
1497 1498 1499 1500 1501 1502
		     int *argc, char **argv, struct weston_config *wc)
{
	struct weston_wayland_backend_config config = {{ 0, }};
	int ret = 0;

	ret = load_wayland_backend_config(c, argc, argv, wc, &config);
Bryce Harrington's avatar
Bryce Harrington committed
1503
	if (ret < 0) {
1504
		weston_wayland_backend_config_release(&config);
1505 1506 1507 1508
		return ret;
	}

	/* load the actual wayland backend and configure it */
1509 1510
	ret = weston_compositor_load_backend(c, WESTON_BACKEND_WAYLAND,
					     &config.base);
1511
	weston_wayland_backend_config_release(&config);
1512 1513 1514 1515
	return ret;
}


1516 1517 1518 1519
static int
load_backend(struct weston_compositor *compositor, const char *backend,
	     int *argc, char **argv, struct weston_config *config)
{
1520
	if (strstr(backend, "headless-backend.so"))
1521
		return load_headless_backend(compositor, argc, argv, config);
1522
	else if (strstr(backend, "rdp-backend.so"))
1523
		return load_rdp_backend(compositor, argc, argv, config);
1524
	else if (strstr(backend, "fbdev-backend.so"))
1525
		return load_fbdev_backend(compositor, argc, argv, config);
1526
	else if (strstr(backend, "drm-backend.so"))
1527
		return load_drm_backend(compositor, argc, argv, config);
1528
	else if (strstr(backend, "x11-backend.so"))
1529
		return load_x11_backend(compositor, argc, argv, config);
1530
	else if (strstr(backend, "wayland-backend.so"))
1531
		return load_wayland_backend(compositor, argc, argv, config);
1532

1533 1534
	weston_log("Error: unknown backend \"%s\"\n", backend);
	return -1;
1535 1536
}

Pekka Paalanen's avatar
Pekka Paalanen committed
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
static char *
copy_command_line(int argc, char * const argv[])
{
	FILE *fp;
	char *str = NULL;
	size_t size = 0;
	int i;

	fp = open_memstream(&str, &size);
	if (!fp)
		return NULL;

	fprintf(fp, "%s", argv[0]);
	for (i = 1; i < argc; i++)
		fprintf(fp, " %s", argv[i]);
	fclose(fp);

	return str;
}

1557 1558 1559
int main(int argc, char *argv[])
{
	int ret = EXIT_FAILURE;
Pekka Paalanen's avatar
Pekka Paalanen committed
1560
	char *cmdline;
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
	struct wl_display *display;
	struct weston_compositor *ec;
	struct wl_event_source *signals[4];
	struct wl_event_loop *loop;
	int i, fd;
	char *backend = NULL;
	char *shell = NULL;
	char *modules = NULL;
	char *option_modules = NULL;
	char *log = NULL;
	char *server_socket = NULL, *end;
	int32_t idle_time = -1;
	int32_t help = 0;
	char *socket_name = NULL;
	int32_t version = 0;
	int32_t noconfig = 0;
	int32_t numlock_on;
	char *config_file = NULL;
	struct weston_config *config = NULL;
	struct weston_config_section *section;
	struct wl_client *primary_client;
	struct wl_listener primary_client_destroyed;
	struct weston_seat *seat;

	const struct weston_option core_options[] = {
		{ WESTON_OPTION_STRING, "backend", 'B', &backend },
		{ WESTON_OPTION_STRING, "shell", 0, &shell },
		{ WESTON_OPTION_STRING, "socket", 'S', &socket_name },
		{ WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
		{ WESTON_OPTION_STRING, "modules", 0, &option_modules },
		{ WESTON_OPTION_STRING, "log", 0, &log },
		{ WESTON_OPTION_BOOLEAN, "help", 'h', &help },
		{ WESTON_OPTION_BOOLEAN, "version", 0, &version },
		{ WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
		{ WESTON_OPTION_STRING, "config", 'c', &config_file },
	};

Pekka Paalanen's avatar
Pekka Paalanen committed
1598
	cmdline = copy_command_line(argc, argv);
1599 1600
	parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);

Pekka Paalanen's avatar
Pekka Paalanen committed
1601 1602
	if (help) {
		free(cmdline);
1603
		usage(EXIT_SUCCESS);
Pekka Paalanen's avatar
Pekka Paalanen committed
1604
	}
1605 1606 1607

	if (version) {
		printf(PACKAGE_STRING "\n");
Pekka Paalanen's avatar
Pekka Paalanen committed
1608 1609
		free(cmdline);

1610 1611 1612
		return EXIT_SUCCESS;
	}

1613
	weston_log_set_handler(vlog, vlog_continue);
1614 1615 1616 1617 1618 1619 1620 1621
	weston_log_file_open(log);

	weston_log("%s\n"
		   STAMP_SPACE "%s\n"
		   STAMP_SPACE "Bug reports to: %s\n"
		   STAMP_SPACE "Build: %s\n",
		   PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
		   BUILD_ID);
Pekka Paalanen's avatar
Pekka Paalanen committed
1622 1623
	weston_log("Command line: %s\n", cmdline);
	free(cmdline);
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
	log_uname();

	verify_xdg_runtime_dir();

	display = wl_display_create();

	loop = wl_display_get_event_loop(display);
	signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
					      display);
	signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
					      display);
	signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
					      display);

	wl_list_init(&child_process_list);
	signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
					      NULL);

	if (!signals[0] || !signals[1] || !signals[2] || !signals[3])
		goto out_signals;

	if (load_configuration(&config, noconfig, config_file) < 0)
		goto out_signals;

	section = weston_config_get_section(config, "core", NULL, NULL);

	if (!backend) {
		weston_config_section_get_string(section, "backend", &backend,
						 NULL);
		if (!backend)
			backend = weston_choose_default_backend();
	}

1657
	ec = weston_compositor_create(display, config);
1658 1659
	if (ec == NULL) {
		weston_log("fatal: failed to create compositor\n");
1660
		goto out;
1661 1662 1663
	}

	if (weston_compositor_init_config(ec, config) < 0)
1664
		goto out;
1665

1666
	if (load_backend(ec, backend, &argc, argv, config) < 0) {
1667
		weston_log("fatal: failed to create compositor backend\n");
1668
		goto out;
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
	}

	catch_signals();
	segv_compositor = ec;

	if (idle_time < 0)
		weston_config_section_get_int(section, "idle-time", &idle_time, -1);
	if (idle_time < 0)
		idle_time = 300; /* default idle timeout, in seconds */

	ec->idle_time = idle_time;
	ec->default_pointer_grab = NULL;
	ec->exit = handle_exit;

	weston_compositor_log_capabilities(ec);

	server_socket = getenv("WAYLAND_SERVER_SOCKET");
	if (server_socket) {
		weston_log("Running with single client\n");
1688
		fd = strtol(server_socket, &end, 10);
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
		if (*end != '\0')
			fd = -1;
	} else {
		fd = -1;
	}

	if (fd != -1) {
		primary_client = wl_client_create(display, fd);
		if (!primary_client) {
			weston_log("fatal: failed to add client: %m\n");
			goto out;
		}
		primary_client_destroyed.notify =
			handle_primary_client_destroyed;
		wl_client_add_destroy_listener(primary_client,
					       &primary_client_destroyed);
	} else if (weston_create_listening_socket(display, socket_name)) {
		goto out;
	}

	if (!shell)
		weston_config_section_get_string(section, "shell", &shell,
						 "desktop-shell.so");

	if (load_modules(ec, shell, &argc, argv) < 0)
		goto out;

	weston_config_section_get_string(section, "modules", &modules, "");
	if (load_modules(ec, modules, &argc, argv) < 0)
		goto out;

	if (load_modules(ec, option_modules, &argc, argv) < 0)
		goto out;

	section = weston_config_get_section(config, "keyboard", NULL, NULL);
	weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
	if (numlock_on) {
		wl_list_for_each(seat, &ec->seat_list, link) {
1727 1728 1729 1730 1731
			struct weston_keyboard *keyboard =
				weston_seat_get_keyboard(seat);

			if (keyboard)
				weston_keyboard_set_locks(keyboard,
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
							  WESTON_NUM_LOCK,
							  WESTON_NUM_LOCK);
		}
	}

	for (i = 1; i < argc; i++)
		weston_log("fatal: unhandled option: %s\n", argv[i]);
	if (argc > 1)
		goto out;

	weston_compositor_wake(ec);

	wl_display_run(display);

	/* Allow for setting return exit code after
	* wl_display_run returns normally. This is
	* useful for devs/testers and automated tests
	* that want to indicate failure status to
	* testing infrastructure above
	*/
	ret = ec->exit_code;

out:
	weston_compositor_destroy(ec);

out_signals:
	for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
		if (signals[i])
			wl_event_source_remove(signals[i]);

	wl_display_destroy(display);

	weston_log_file_close();

	if (config)
		weston_config_destroy(config);
	free(config_file);
	free(backend);
	free(shell);
	free(socket_name);
	free(option_modules);
	free(log);
	free(modules);

	return ret;
}