labwc/src/main.c

175 lines
3.9 KiB
C
Raw Normal View History

2021-09-24 21:45:48 +01:00
// SPDX-License-Identifier: GPL-2.0-only
#define _POSIX_C_SOURCE 200809L
2022-10-06 21:54:49 +01:00
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "common/dir.h"
#include "common/fd_util.h"
#include "common/font.h"
#include "common/mem.h"
2020-11-01 22:22:15 +00:00
#include "common/spawn.h"
#include "config/session.h"
2019-11-19 21:00:26 +00:00
#include "labwc.h"
2021-02-21 22:18:34 +00:00
#include "theme.h"
2020-10-19 22:14:17 +01:00
#include "menu/menu.h"
2019-05-11 21:26:59 +01:00
2020-06-05 23:04:54 +01:00
struct rcxml rc = { 0 };
2019-12-16 21:19:50 +00:00
2022-10-06 21:54:26 +01:00
static const struct option long_options[] = {
{"config", required_argument, NULL, 'c'},
{"config-dir", required_argument, NULL, 'C'},
{"debug", no_argument, NULL, 'd'},
2022-10-06 21:54:49 +01:00
{"exit", no_argument, NULL, 'e'},
2022-10-06 21:54:26 +01:00
{"help", no_argument, NULL, 'h'},
2022-10-06 21:54:49 +01:00
{"reconfigure", no_argument, NULL, 'r'},
2022-10-06 21:54:26 +01:00
{"startup", required_argument, NULL, 's'},
{"version", no_argument, NULL, 'v'},
{"verbose", no_argument, NULL, 'V'},
{0, 0, 0, 0}
};
2020-07-16 20:16:43 +01:00
static const char labwc_usage[] =
"Usage: labwc [options...]\n"
2022-10-06 21:54:26 +01:00
" -c, --config <file> Specify config file (with path)\n"
" -C, --config-dir <dir> Specify config directory\n"
" -d, --debug Enable full logging, including debug information\n"
2022-10-06 21:54:49 +01:00
" -e, --exit Exit the compositor\n"
2022-10-06 21:54:26 +01:00
" -h, --help Show help message and quit\n"
2022-10-06 21:54:49 +01:00
" -r, --reconfigure Reload the compositor configuration\n"
2022-10-06 21:54:26 +01:00
" -s, --startup <command> Run command on startup\n"
" -v, --version Show version number and quit\n"
" -V, --verbose Enable more verbose logging\n";
2020-07-16 20:16:43 +01:00
static void
usage(void)
2020-07-16 20:16:43 +01:00
{
printf("%s", labwc_usage);
exit(0);
}
2022-10-06 21:54:49 +01:00
static void
send_signal_to_labwc_pid(int signal)
{
char *labwc_pid = getenv("LABWC_PID");
if (!labwc_pid) {
wlr_log(WLR_ERROR, "LABWC_PID not set");
exit(EXIT_FAILURE);
}
int pid = atoi(labwc_pid);
if (!pid) {
wlr_log(WLR_ERROR, "should not send signal to pid 0");
exit(EXIT_FAILURE);
}
kill(pid, signal);
}
int
main(int argc, char *argv[])
2019-12-27 21:22:45 +00:00
{
#if HAVE_NLS
setlocale(LC_ALL, "");
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
textdomain(GETTEXT_PACKAGE);
#endif
2019-05-11 21:26:59 +01:00
char *startup_cmd = NULL;
2020-07-18 11:28:39 +01:00
char *config_file = NULL;
enum wlr_log_importance verbosity = WLR_ERROR;
2020-05-11 06:59:27 +01:00
2019-05-11 21:26:59 +01:00
int c;
2022-10-06 21:54:26 +01:00
while (1) {
int index = 0;
2022-10-06 21:54:49 +01:00
c = getopt_long(argc, argv, "c:C:dehrs:vV", long_options, &index);
2022-10-06 21:54:26 +01:00
if (c == -1) {
break;
}
2019-05-11 21:26:59 +01:00
switch (c) {
2020-07-18 11:28:39 +01:00
case 'c':
config_file = optarg;
break;
case 'C':
rc.config_dir = xstrdup(optarg);
break;
case 'd':
verbosity = WLR_DEBUG;
break;
2022-10-06 21:54:49 +01:00
case 'e':
send_signal_to_labwc_pid(SIGTERM);
exit(0);
case 'r':
send_signal_to_labwc_pid(SIGHUP);
exit(0);
2020-09-25 19:42:40 +01:00
case 's':
startup_cmd = optarg;
break;
2020-10-23 20:08:56 +01:00
case 'v':
printf("labwc " LABWC_VERSION "\n");
exit(0);
case 'V':
verbosity = WLR_INFO;
2020-10-23 20:08:56 +01:00
break;
2020-09-25 19:42:40 +01:00
case 'h':
2019-05-11 21:26:59 +01:00
default:
2020-07-16 20:16:43 +01:00
usage();
2019-05-11 21:26:59 +01:00
}
}
if (optind < argc) {
2020-07-16 20:16:43 +01:00
usage();
}
2019-05-11 21:26:59 +01:00
wlr_log_init(verbosity, NULL);
if (!rc.config_dir) {
rc.config_dir = config_dir();
}
wlr_log(WLR_INFO, "using config dir (%s)\n", rc.config_dir);
session_environment_init(rc.config_dir);
2020-07-18 11:28:39 +01:00
rcxml_read(config_file);
2020-06-05 23:04:54 +01:00
/*
* Set environment variable LABWC_PID to the pid of the compositor
* so that SIGHUP and SIGTERM can be sent to specific instances using
* `kill -s <signal> <pid>` rather than `killall -s <signal> labwc`
*/
char pid[32];
snprintf(pid, sizeof(pid), "%d", getpid());
if (setenv("LABWC_PID", pid, true) < 0) {
wlr_log_errno(WLR_ERROR, "unable to set LABWC_PID");
} else {
wlr_log(WLR_DEBUG, "LABWC_PID=%s", pid);
}
2020-05-11 06:59:27 +01:00
if (!getenv("XDG_RUNTIME_DIR")) {
2021-07-22 21:30:17 +01:00
wlr_log(WLR_ERROR, "XDG_RUNTIME_DIR is unset");
exit(EXIT_FAILURE);
2020-05-11 06:59:27 +01:00
}
increase_nofile_limit();
struct server server = { 0 };
server_init(&server);
server_start(&server);
2020-05-11 06:59:27 +01:00
2021-02-25 22:14:07 +00:00
struct theme theme = { 0 };
theme_init(&theme, rc.theme_name);
rc.theme = &theme;
2021-02-21 21:54:40 +00:00
server.theme = &theme;
2020-06-29 19:27:59 +01:00
menu_init_rootmenu(&server);
2022-01-26 00:07:10 +01:00
menu_init_windowmenu(&server);
2020-10-19 22:14:17 +01:00
session_autostart_init(rc.config_dir);
if (startup_cmd) {
2020-06-19 22:29:54 +01:00
spawn_async_no_shell(startup_cmd);
}
2020-07-16 20:16:43 +01:00
2019-05-11 21:26:59 +01:00
wl_display_run(server.wl_display);
server_finish(&server);
2021-02-21 22:03:14 +00:00
menu_finish();
2021-02-21 22:03:14 +00:00
theme_finish(&theme);
2020-08-13 20:18:48 +01:00
rcxml_finish();
font_finish();
2019-05-11 21:26:59 +01:00
return 0;
}