2018-08-08 23:54:58 +02:00
|
|
|
#include "client.hpp"
|
2019-08-28 05:57:23 +02:00
|
|
|
#include <fmt/ostream.h>
|
2019-05-23 10:13:49 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-04-18 17:43:16 +02:00
|
|
|
#include <fstream>
|
2018-11-15 14:44:43 +01:00
|
|
|
#include <iostream>
|
2019-04-18 17:43:16 +02:00
|
|
|
#include "util/clara.hpp"
|
|
|
|
#include "util/json.hpp"
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
waybar::Client *waybar::Client::inst() {
|
2019-04-24 12:37:24 +02:00
|
|
|
static auto c = new Client();
|
2019-04-18 17:43:16 +02:00
|
|
|
return c;
|
2018-08-09 14:04:48 +02:00
|
|
|
}
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2019-05-24 09:49:09 +02:00
|
|
|
const std::string waybar::Client::getValidPath(const std::vector<std::string> &paths) const {
|
2018-11-24 11:04:56 +01:00
|
|
|
wordexp_t p;
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
for (const std::string &path : paths) {
|
2018-11-24 11:04:56 +01:00
|
|
|
if (wordexp(path.c_str(), &p, 0) == 0) {
|
|
|
|
if (access(*p.we_wordv, F_OK) == 0) {
|
|
|
|
std::string result = *p.we_wordv;
|
|
|
|
wordfree(&p);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
wordfree(&p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
void waybar::Client::handleGlobal(void *data, struct wl_registry *registry, uint32_t name,
|
|
|
|
const char *interface, uint32_t version) {
|
|
|
|
auto client = static_cast<Client *>(data);
|
2018-08-16 14:29:41 +02:00
|
|
|
if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
|
2019-04-18 17:43:16 +02:00
|
|
|
client->layer_shell = static_cast<struct zwlr_layer_shell_v1 *>(
|
|
|
|
wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, version));
|
|
|
|
} else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 &&
|
|
|
|
version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) {
|
|
|
|
client->xdg_output_manager = static_cast<struct zxdg_output_manager_v1 *>(wl_registry_bind(
|
|
|
|
registry, name, &zxdg_output_manager_v1_interface, ZXDG_OUTPUT_V1_NAME_SINCE_VERSION));
|
2019-02-17 15:27:54 +01:00
|
|
|
} else if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) {
|
2019-04-19 11:56:40 +02:00
|
|
|
client->idle_inhibit_manager = static_cast<struct zwp_idle_inhibit_manager_v1 *>(
|
|
|
|
wl_registry_bind(registry, name, &zwp_idle_inhibit_manager_v1_interface, 1));
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-09 20:22:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
void waybar::Client::handleGlobalRemove(void * data, struct wl_registry * /*registry*/,
|
2019-04-18 17:43:16 +02:00
|
|
|
uint32_t name) {
|
2019-08-28 05:57:23 +02:00
|
|
|
// Nothing here
|
2019-04-18 17:43:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 08:27:57 +01:00
|
|
|
void waybar::Client::handleOutput(struct waybar_output &output) {
|
2019-04-18 17:43:16 +02:00
|
|
|
static const struct zxdg_output_v1_listener xdgOutputListener = {
|
2019-08-28 05:57:23 +02:00
|
|
|
.logical_position = [](void *, struct zxdg_output_v1 *, int32_t, int32_t) {},
|
|
|
|
.logical_size = [](void *, struct zxdg_output_v1 *, int32_t, int32_t) {},
|
|
|
|
.done = [](void *, struct zxdg_output_v1 *) {},
|
|
|
|
.name = &handleOutputName,
|
|
|
|
.description = [](void *, struct zxdg_output_v1 *, const char *) {},
|
2019-04-18 17:43:16 +02:00
|
|
|
};
|
2019-08-28 05:57:23 +02:00
|
|
|
// owned by output->monitor; no need to destroy
|
2020-01-14 08:27:57 +01:00
|
|
|
auto wl_output = gdk_wayland_monitor_get_wl_output(output.monitor->gobj());
|
|
|
|
output.xdg_output.reset(zxdg_output_manager_v1_get_xdg_output(xdg_output_manager, wl_output));
|
|
|
|
zxdg_output_v1_add_listener(output.xdg_output.get(), &xdgOutputListener, &output);
|
2019-04-18 17:43:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 08:27:57 +01:00
|
|
|
bool waybar::Client::isValidOutput(const Json::Value &config, struct waybar_output &output) {
|
2020-02-27 21:49:09 +01:00
|
|
|
if (config["output"].isArray()) {
|
|
|
|
for (auto const &output_conf : config["output"]) {
|
|
|
|
if (output_conf.isString() && output_conf.asString() == output.name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 23:41:32 +02:00
|
|
|
return false;
|
|
|
|
} else if (config["output"].isString()) {
|
2020-02-27 21:49:09 +01:00
|
|
|
auto config_output_name = config["output"].asString();
|
2020-02-18 22:09:37 +01:00
|
|
|
if (!config_output_name.empty()) {
|
|
|
|
if (config_output_name.substr(0, 1) == "!") {
|
|
|
|
return config_output_name.substr(1) != output.name;
|
|
|
|
}
|
|
|
|
return config_output_name == output.name;
|
|
|
|
}
|
2018-08-19 13:39:57 +02:00
|
|
|
}
|
2020-02-18 22:09:37 +01:00
|
|
|
|
2020-02-28 07:54:00 +01:00
|
|
|
return true;
|
2018-08-09 20:22:01 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 08:27:57 +01:00
|
|
|
struct waybar::waybar_output &waybar::Client::getOutput(void *addr) {
|
|
|
|
auto it = std::find_if(
|
|
|
|
outputs_.begin(), outputs_.end(), [&addr](const auto &output) { return &output == addr; });
|
2019-04-25 13:25:06 +02:00
|
|
|
if (it == outputs_.end()) {
|
|
|
|
throw std::runtime_error("Unable to find valid output");
|
|
|
|
}
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
2020-01-14 08:27:57 +01:00
|
|
|
std::vector<Json::Value> waybar::Client::getOutputConfigs(struct waybar_output &output) {
|
2019-04-25 13:25:06 +02:00
|
|
|
std::vector<Json::Value> configs;
|
|
|
|
if (config_.isArray()) {
|
|
|
|
for (auto const &config : config_) {
|
|
|
|
if (config.isObject() && isValidOutput(config, output)) {
|
|
|
|
configs.push_back(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isValidOutput(config_, output)) {
|
|
|
|
configs.push_back(config_);
|
|
|
|
}
|
|
|
|
return configs;
|
|
|
|
}
|
|
|
|
|
2019-08-28 05:57:23 +02:00
|
|
|
void waybar::Client::handleOutputName(void * data, struct zxdg_output_v1 * /*xdg_output*/,
|
|
|
|
const char *name) {
|
2019-04-18 17:43:16 +02:00
|
|
|
auto client = waybar::Client::inst();
|
2019-04-25 13:25:06 +02:00
|
|
|
try {
|
2019-08-28 05:57:23 +02:00
|
|
|
auto &output = client->getOutput(data);
|
2020-01-14 08:27:57 +01:00
|
|
|
output.name = name;
|
2019-08-28 05:57:23 +02:00
|
|
|
spdlog::debug("Output detected: {} ({} {})",
|
|
|
|
name,
|
2020-01-14 08:27:57 +01:00
|
|
|
output.monitor->get_manufacturer(),
|
|
|
|
output.monitor->get_model());
|
2019-04-25 13:25:06 +02:00
|
|
|
auto configs = client->getOutputConfigs(output);
|
|
|
|
if (configs.empty()) {
|
2020-01-14 08:27:57 +01:00
|
|
|
output.xdg_output.reset();
|
2019-04-25 13:25:06 +02:00
|
|
|
} else {
|
2019-05-17 13:40:04 +02:00
|
|
|
wl_display_roundtrip(client->wl_display);
|
2019-04-25 13:25:06 +02:00
|
|
|
for (const auto &config : configs) {
|
2020-01-14 08:27:57 +01:00
|
|
|
client->bars.emplace_back(std::make_unique<Bar>(&output, config));
|
2019-04-25 13:25:06 +02:00
|
|
|
Glib::RefPtr<Gdk::Screen> screen = client->bars.back()->window.get_screen();
|
|
|
|
client->style_context_->add_provider_for_screen(
|
|
|
|
screen, client->css_provider_, GTK_STYLE_PROVIDER_PRIORITY_USER);
|
2019-04-18 17:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 13:25:06 +02:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
2019-04-18 17:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 05:57:23 +02:00
|
|
|
void waybar::Client::handleMonitorAdded(Glib::RefPtr<Gdk::Monitor> monitor) {
|
2020-01-14 08:27:57 +01:00
|
|
|
auto &output = outputs_.emplace_back();
|
|
|
|
output.monitor = monitor;
|
2019-08-28 05:57:23 +02:00
|
|
|
handleOutput(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::Client::handleMonitorRemoved(Glib::RefPtr<Gdk::Monitor> monitor) {
|
|
|
|
spdlog::debug("Output removed: {} {}", monitor->get_manufacturer(), monitor->get_model());
|
|
|
|
for (auto it = bars.begin(); it != bars.end();) {
|
|
|
|
if ((*it)->output->monitor == monitor) {
|
|
|
|
auto output_name = (*it)->output->name;
|
|
|
|
(*it)->window.close();
|
|
|
|
it = bars.erase(it);
|
|
|
|
spdlog::info("Bar removed from output: {}", output_name);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2020-01-14 08:27:57 +01:00
|
|
|
outputs_.remove_if([&monitor](const auto &output) { return output.monitor == monitor; });
|
2019-04-18 17:43:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 09:49:09 +02:00
|
|
|
std::tuple<const std::string, const std::string> waybar::Client::getConfigs(
|
|
|
|
const std::string &config, const std::string &style) const {
|
|
|
|
auto config_file = config.empty() ? getValidPath({
|
|
|
|
"$XDG_CONFIG_HOME/waybar/config",
|
|
|
|
"$HOME/.config/waybar/config",
|
|
|
|
"$HOME/waybar/config",
|
build: drop -Dout in favor of --prefix
$ meson --prefix=/tmp/foo _build
$ ninja install -C _build
[49/50] Installing files.
Installing waybar to /tmp/foo/bin
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py", line 127, in run
return options.run_func(options)
File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 514, in run
installer.do_install(datafilename)
File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 346, in do_install
self.install_data(d)
File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 375, in install_data
d.dirmaker.makedirs(outdir, exist_ok=True)
File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 55, in makedirs
os.makedirs(path, exist_ok=exist_ok)
File "/usr/lib/python3.6/os.py", line 210, in makedirs
makedirs(head, mode, exist_ok)
File "/usr/lib/python3.6/os.py", line 220, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/etc/xdg'
FAILED: meson-install
2019-08-11 12:09:55 +02:00
|
|
|
SYSCONFDIR "/xdg/waybar/config",
|
2019-05-24 09:49:09 +02:00
|
|
|
"./resources/config",
|
|
|
|
})
|
|
|
|
: config;
|
|
|
|
auto css_file = style.empty() ? getValidPath({
|
|
|
|
"$XDG_CONFIG_HOME/waybar/style.css",
|
|
|
|
"$HOME/.config/waybar/style.css",
|
|
|
|
"$HOME/waybar/style.css",
|
build: drop -Dout in favor of --prefix
$ meson --prefix=/tmp/foo _build
$ ninja install -C _build
[49/50] Installing files.
Installing waybar to /tmp/foo/bin
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py", line 127, in run
return options.run_func(options)
File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 514, in run
installer.do_install(datafilename)
File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 346, in do_install
self.install_data(d)
File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 375, in install_data
d.dirmaker.makedirs(outdir, exist_ok=True)
File "/usr/lib/python3.6/site-packages/mesonbuild/minstall.py", line 55, in makedirs
os.makedirs(path, exist_ok=exist_ok)
File "/usr/lib/python3.6/os.py", line 210, in makedirs
makedirs(head, mode, exist_ok)
File "/usr/lib/python3.6/os.py", line 220, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/etc/xdg'
FAILED: meson-install
2019-08-11 12:09:55 +02:00
|
|
|
SYSCONFDIR "/xdg/waybar/style.css",
|
2019-05-24 09:49:09 +02:00
|
|
|
"./resources/style.css",
|
2019-04-18 17:43:16 +02:00
|
|
|
})
|
2019-05-24 09:49:09 +02:00
|
|
|
: style;
|
|
|
|
if (css_file.empty() || config_file.empty()) {
|
2018-12-18 17:30:54 +01:00
|
|
|
throw std::runtime_error("Missing required resources files");
|
|
|
|
}
|
2019-05-24 09:49:09 +02:00
|
|
|
spdlog::info("Resources files: {}, {}", config_file, css_file);
|
|
|
|
return {config_file, css_file};
|
2018-12-18 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
2019-05-24 09:49:09 +02:00
|
|
|
auto waybar::Client::setupConfig(const std::string &config_file) -> void {
|
|
|
|
std::ifstream file(config_file);
|
2019-04-18 17:43:16 +02:00
|
|
|
if (!file.is_open()) {
|
|
|
|
throw std::runtime_error("Can't open config file");
|
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
std::string str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
2019-04-18 17:43:16 +02:00
|
|
|
util::JsonParser parser;
|
|
|
|
config_ = parser.parse(str);
|
|
|
|
}
|
|
|
|
|
2019-05-24 09:49:09 +02:00
|
|
|
auto waybar::Client::setupCss(const std::string &css_file) -> void {
|
2019-04-18 17:43:16 +02:00
|
|
|
css_provider_ = Gtk::CssProvider::create();
|
|
|
|
style_context_ = Gtk::StyleContext::create();
|
|
|
|
|
|
|
|
// Load our css file, wherever that may be hiding
|
2019-05-24 09:49:09 +02:00
|
|
|
if (!css_provider_->load_from_path(css_file)) {
|
2019-04-18 17:43:16 +02:00
|
|
|
throw std::runtime_error("Can't open style file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::Client::bindInterfaces() {
|
2018-08-16 14:29:41 +02:00
|
|
|
registry = wl_display_get_registry(wl_display);
|
2018-08-09 20:22:01 +02:00
|
|
|
static const struct wl_registry_listener registry_listener = {
|
2019-04-18 17:43:16 +02:00
|
|
|
.global = handleGlobal,
|
|
|
|
.global_remove = handleGlobalRemove,
|
2018-08-09 20:22:01 +02:00
|
|
|
};
|
2018-08-08 23:54:58 +02:00
|
|
|
wl_registry_add_listener(registry, ®istry_listener, this);
|
2018-08-16 14:29:41 +02:00
|
|
|
wl_display_roundtrip(wl_display);
|
2019-04-24 12:37:24 +02:00
|
|
|
if (layer_shell == nullptr || xdg_output_manager == nullptr) {
|
2018-11-16 10:02:12 +01:00
|
|
|
throw std::runtime_error("Failed to acquire required resources.");
|
|
|
|
}
|
2019-08-28 05:57:23 +02:00
|
|
|
// add existing outputs and subscribe to updates
|
|
|
|
for (auto i = 0; i < gdk_display->get_n_monitors(); ++i) {
|
|
|
|
auto monitor = gdk_display->get_monitor(i);
|
|
|
|
handleMonitorAdded(monitor);
|
|
|
|
}
|
|
|
|
gdk_display->signal_monitor_added().connect(sigc::mem_fun(*this, &Client::handleMonitorAdded));
|
|
|
|
gdk_display->signal_monitor_removed().connect(
|
|
|
|
sigc::mem_fun(*this, &Client::handleMonitorRemoved));
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
int waybar::Client::main(int argc, char *argv[]) {
|
2019-04-18 17:52:00 +02:00
|
|
|
bool show_help = false;
|
|
|
|
bool show_version = false;
|
2018-12-18 17:30:54 +01:00
|
|
|
std::string config;
|
|
|
|
std::string style;
|
2019-01-28 19:26:16 +01:00
|
|
|
std::string bar_id;
|
2019-05-20 13:57:41 +02:00
|
|
|
std::string log_level;
|
2019-04-18 17:52:00 +02:00
|
|
|
auto cli = clara::detail::Help(show_help) |
|
2019-04-18 17:43:16 +02:00
|
|
|
clara::detail::Opt(show_version)["-v"]["--version"]("Show version") |
|
|
|
|
clara::detail::Opt(config, "config")["-c"]["--config"]("Config path") |
|
|
|
|
clara::detail::Opt(style, "style")["-s"]["--style"]("Style path") |
|
2019-05-20 13:57:41 +02:00
|
|
|
clara::detail::Opt(
|
|
|
|
log_level,
|
|
|
|
"trace|debug|info|warning|error|critical|off")["-l"]["--log-level"]("Log level") |
|
2019-04-18 17:43:16 +02:00
|
|
|
clara::detail::Opt(bar_id, "id")["-b"]["--bar"]("Bar id");
|
2018-12-18 17:30:54 +01:00
|
|
|
auto res = cli.parse(clara::detail::Args(argc, argv));
|
|
|
|
if (!res) {
|
2019-05-19 01:44:45 +02:00
|
|
|
spdlog::error("Error in command line: {}", res.errorMessage());
|
2018-12-18 17:30:54 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (show_help) {
|
|
|
|
std::cout << cli << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (show_version) {
|
|
|
|
std::cout << "Waybar v" << VERSION << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-20 13:57:41 +02:00
|
|
|
if (!log_level.empty()) {
|
|
|
|
spdlog::set_level(spdlog::level::from_str(log_level));
|
|
|
|
}
|
2019-05-23 10:13:49 +02:00
|
|
|
gtk_app = Gtk::Application::create(argc, argv, "fr.arouillard.waybar");
|
|
|
|
gdk_display = Gdk::Display::get_default();
|
|
|
|
if (!gdk_display) {
|
|
|
|
throw std::runtime_error("Can't find display");
|
|
|
|
}
|
|
|
|
if (!GDK_IS_WAYLAND_DISPLAY(gdk_display->gobj())) {
|
|
|
|
throw std::runtime_error("Bar need to run under Wayland");
|
|
|
|
}
|
|
|
|
wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj());
|
2019-05-24 09:49:09 +02:00
|
|
|
auto [config_file, css_file] = getConfigs(config, style);
|
|
|
|
setupConfig(config_file);
|
|
|
|
setupCss(css_file);
|
2018-08-16 14:29:41 +02:00
|
|
|
bindInterfaces();
|
2019-04-15 11:42:16 +02:00
|
|
|
gtk_app->hold();
|
|
|
|
gtk_app->run();
|
2018-08-17 20:28:26 +02:00
|
|
|
bars.clear();
|
|
|
|
zxdg_output_manager_v1_destroy(xdg_output_manager);
|
|
|
|
zwlr_layer_shell_v1_destroy(layer_shell);
|
2019-02-17 15:27:54 +01:00
|
|
|
zwp_idle_inhibit_manager_v1_destroy(idle_inhibit_manager);
|
2018-08-17 20:28:26 +02:00
|
|
|
wl_registry_destroy(registry);
|
|
|
|
wl_display_disconnect(wl_display);
|
2018-08-08 23:54:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|