2016-06-14 23:32:35 -04:00
|
|
|
#include <X11/Xlib-xcb.h>
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/command_line.hpp"
|
|
|
|
#include "components/config.hpp"
|
|
|
|
#include "components/controller.hpp"
|
|
|
|
#include "components/logger.hpp"
|
|
|
|
#include "config.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "utils/env.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "utils/inotify.hpp"
|
2016-12-03 09:46:48 -05:00
|
|
|
#include "utils/process.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "x11/xutils.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
using namespace polybar;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-09 03:02:47 -05:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
|
2016-11-25 02:42:31 -05:00
|
|
|
struct exit_success {};
|
|
|
|
struct exit_failure {};
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-25 02:42:31 -05:00
|
|
|
int main(int argc, char** argv) {
|
2016-06-14 23:32:35 -04:00
|
|
|
// clang-format off
|
|
|
|
const command_line::options opts{
|
2016-12-09 06:22:58 -05:00
|
|
|
command_line::option{"-h", "--help", "Show help options"},
|
2016-06-14 23:32:35 -04:00
|
|
|
command_line::option{"-v", "--version", "Print version information"},
|
2016-12-09 06:22:58 -05:00
|
|
|
command_line::option{"-l", "--log", "Set the logging verbosity (default: WARNING)", "LEVEL", {"error", "warning", "info", "trace"}},
|
2016-06-14 23:32:35 -04:00
|
|
|
command_line::option{"-q", "--quiet", "Be quiet (will override -l)"},
|
|
|
|
command_line::option{"-c", "--config", "Path to the configuration file", "FILE"},
|
|
|
|
command_line::option{"-r", "--reload", "Reload when the configuration has been modified"},
|
|
|
|
command_line::option{"-d", "--dump", "Show value of PARAM in section [bar_name]", "PARAM"},
|
|
|
|
command_line::option{"-w", "--print-wmname", "Print the generated WM_NAME"},
|
|
|
|
command_line::option{"-s", "--stdout", "Output data to stdout instead of drawing the X window"},
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
2016-12-03 10:44:08 -05:00
|
|
|
uint8_t exit_code{EXIT_SUCCESS};
|
|
|
|
bool reload{false};
|
|
|
|
|
2016-12-09 06:22:58 -05:00
|
|
|
logger& logger{const_cast<decltype(logger)>(logger::make(loglevel::WARNING))};
|
2016-12-05 14:41:00 -05:00
|
|
|
|
2016-12-03 09:46:48 -05:00
|
|
|
try {
|
|
|
|
//==================================================
|
|
|
|
// Connect to X server
|
|
|
|
//==================================================
|
|
|
|
XInitThreads();
|
2016-12-05 14:41:00 -05:00
|
|
|
xcb_connection_t* xcbconn{nullptr};
|
2016-11-25 02:42:31 -05:00
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
if ((xcbconn = xutils::get_connection()) == nullptr) {
|
2016-12-03 09:46:48 -05:00
|
|
|
logger.err("A connection to X could not be established... ");
|
|
|
|
throw exit_failure{};
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
connection{xcbconn}.preload_atoms();
|
|
|
|
connection{xcbconn}.query_extensions();
|
|
|
|
|
|
|
|
//==================================================
|
|
|
|
// Block all signals by default
|
|
|
|
//==================================================
|
|
|
|
sigset_t blockmask;
|
|
|
|
sigfillset(&blockmask);
|
|
|
|
|
|
|
|
if (pthread_sigmask(SIG_BLOCK, &blockmask, nullptr) == -1) {
|
|
|
|
throw system_error("Failed to block signals");
|
|
|
|
}
|
|
|
|
|
2016-12-03 09:46:48 -05:00
|
|
|
//==================================================
|
|
|
|
// Parse command line arguments
|
|
|
|
//==================================================
|
|
|
|
string scriptname{argv[0]};
|
2016-12-09 05:32:41 -05:00
|
|
|
vector<string> args{argv + 1, argv + argc};
|
|
|
|
|
|
|
|
cliparser::make_type cli{cliparser::make(move(scriptname), opts)};
|
2016-12-03 09:46:48 -05:00
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
cli->process_input(args);
|
2016-12-03 09:46:48 -05:00
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
if (cli->has("quiet")) {
|
2016-12-03 09:46:48 -05:00
|
|
|
logger.verbosity(loglevel::ERROR);
|
2016-12-05 14:41:00 -05:00
|
|
|
} else if (cli->has("log")) {
|
2016-12-09 06:22:58 -05:00
|
|
|
logger.verbosity(logger::parse_verbosity(cli->get("log")));
|
2016-12-03 09:46:48 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
if (cli->has("help")) {
|
|
|
|
cli->usage();
|
2016-12-03 09:46:48 -05:00
|
|
|
throw exit_success{};
|
2016-12-05 14:41:00 -05:00
|
|
|
} else if (cli->has("version")) {
|
2016-12-03 09:46:48 -05:00
|
|
|
print_build_info(version_details(args));
|
|
|
|
throw exit_success{};
|
|
|
|
} else if (args.empty() || args[0][0] == '-') {
|
2016-12-05 14:41:00 -05:00
|
|
|
cli->usage();
|
2016-12-03 09:46:48 -05:00
|
|
|
throw exit_failure{};
|
|
|
|
}
|
|
|
|
|
|
|
|
//==================================================
|
|
|
|
// Load user configuration
|
|
|
|
//==================================================
|
2016-12-09 05:32:41 -05:00
|
|
|
string confpath;
|
2016-12-03 09:46:48 -05:00
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
if (cli->has("config")) {
|
2016-12-09 05:32:41 -05:00
|
|
|
confpath = cli->get("config");
|
2016-12-03 09:46:48 -05:00
|
|
|
} else if (env_util::has("XDG_CONFIG_HOME")) {
|
2016-12-09 05:32:41 -05:00
|
|
|
confpath = env_util::get("XDG_CONFIG_HOME") + "/polybar/config";
|
2016-12-03 09:46:48 -05:00
|
|
|
} else if (env_util::has("HOME")) {
|
2016-12-09 05:32:41 -05:00
|
|
|
confpath = env_util::get("HOME") + "/.config/polybar/config";
|
2016-12-03 09:46:48 -05:00
|
|
|
} else {
|
|
|
|
throw application_error("Define configuration using --config=PATH");
|
|
|
|
}
|
|
|
|
|
2016-12-09 05:32:41 -05:00
|
|
|
config::make_type conf{config::make(move(confpath), args[0])};
|
|
|
|
|
2016-12-03 09:46:48 -05:00
|
|
|
//==================================================
|
|
|
|
// Dump requested data
|
|
|
|
//==================================================
|
2016-12-05 14:41:00 -05:00
|
|
|
if (cli->has("dump")) {
|
2016-12-09 03:02:47 -05:00
|
|
|
cout << conf.get<string>(conf.bar_section(), cli->get("dump")) << endl;
|
2016-12-03 09:46:48 -05:00
|
|
|
throw exit_success{};
|
|
|
|
}
|
|
|
|
|
|
|
|
//==================================================
|
2016-12-05 14:41:00 -05:00
|
|
|
// Create controller and run application
|
2016-12-03 09:46:48 -05:00
|
|
|
//==================================================
|
2016-12-05 14:41:00 -05:00
|
|
|
unique_ptr<controller> ctrl;
|
2016-12-09 03:40:46 -05:00
|
|
|
string path_confwatch;
|
2016-12-09 03:02:47 -05:00
|
|
|
bool enable_ipc{false};
|
2016-12-05 14:41:00 -05:00
|
|
|
|
2016-12-09 03:02:47 -05:00
|
|
|
if (!cli->has("print-wmname")) {
|
|
|
|
enable_ipc = conf.get<bool>(conf.bar_section(), "enable-ipc", false);
|
|
|
|
}
|
|
|
|
if (!cli->has("print-wmname") && cli->has("reload")) {
|
2016-12-09 03:40:46 -05:00
|
|
|
path_confwatch = conf.filepath();
|
2016-12-03 09:46:48 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 03:40:46 -05:00
|
|
|
ctrl = controller::make(move(path_confwatch), enable_ipc, cli->has("stdout"));
|
2016-11-25 02:42:31 -05:00
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
if (cli->has("print-wmname")) {
|
2016-12-09 03:02:47 -05:00
|
|
|
cout << ctrl->opts().wmname << endl;
|
2016-12-03 09:46:48 -05:00
|
|
|
throw exit_success{};
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
2016-12-03 09:46:48 -05:00
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
ctrl->setup();
|
|
|
|
|
2016-12-03 09:46:48 -05:00
|
|
|
if (!ctrl->run()) {
|
|
|
|
reload = true;
|
|
|
|
}
|
2016-12-05 14:41:00 -05:00
|
|
|
|
|
|
|
//==================================================
|
|
|
|
// Unblock signals
|
|
|
|
//==================================================
|
|
|
|
if (pthread_sigmask(SIG_UNBLOCK, &blockmask, nullptr) == -1) {
|
|
|
|
throw system_error("Failed to unblock signals");
|
|
|
|
}
|
2016-12-03 09:46:48 -05:00
|
|
|
} catch (const exit_success& term) {
|
|
|
|
exit_code = EXIT_SUCCESS;
|
|
|
|
} catch (const exit_failure& term) {
|
|
|
|
exit_code = EXIT_FAILURE;
|
|
|
|
} catch (const exception& err) {
|
|
|
|
logger.err(err.what());
|
|
|
|
exit_code = EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reload) {
|
|
|
|
logger.info("Reached end of application...");
|
|
|
|
return exit_code;
|
|
|
|
}
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-03 09:46:48 -05:00
|
|
|
try {
|
|
|
|
logger.info("Reload application...");
|
|
|
|
process_util::exec(argv[0], argv);
|
|
|
|
} catch (const system_error& err) {
|
|
|
|
logger.err("execlp() failed (%s)", strerror(errno));
|
|
|
|
}
|
2016-11-25 02:42:31 -05:00
|
|
|
|
2016-12-03 09:46:48 -05:00
|
|
|
return EXIT_FAILURE;
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|