2018-08-15 18:17:17 +00:00
|
|
|
#include "modules/sway/window.hpp"
|
|
|
|
|
2019-04-19 09:09:06 +00:00
|
|
|
namespace waybar::modules::sway {
|
|
|
|
|
|
|
|
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
2019-04-18 15:52:00 +00:00
|
|
|
: ALabel(config, "{}"), bar_(bar), windowId_(-1) {
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.set_name("window");
|
2018-12-18 16:30:54 +00:00
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-11-20 22:24:33 +00:00
|
|
|
if (label_.get_max_width_chars() == -1) {
|
|
|
|
label_.set_hexpand(true);
|
|
|
|
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
|
|
|
|
}
|
2019-04-24 10:37:24 +00:00
|
|
|
ipc_.subscribe(R"(["window","workspace"])");
|
2019-04-19 09:09:06 +00:00
|
|
|
ipc_.signal_event.connect(sigc::mem_fun(*this, &Window::onEvent));
|
|
|
|
ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Window::onCmd));
|
2019-04-25 14:47:51 +00:00
|
|
|
// Get Initial focused window
|
|
|
|
getTree();
|
2018-08-20 12:50:45 +00:00
|
|
|
// Launch worker
|
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
2019-05-07 11:43:48 +00:00
|
|
|
void Window::onEvent(const struct Ipc::ipc_response& res) { getTree(); }
|
2019-04-19 09:09:06 +00:00
|
|
|
|
2019-04-24 10:37:24 +00:00
|
|
|
void Window::onCmd(const struct Ipc::ipc_response& res) {
|
2019-05-07 11:43:48 +00:00
|
|
|
auto payload = parser_.parse(res.payload);
|
|
|
|
auto [nb, id, name, app_id] = getFocusedNode(payload);
|
2019-04-25 14:47:51 +00:00
|
|
|
if (!app_id_.empty()) {
|
|
|
|
bar_.window.get_style_context()->remove_class(app_id_);
|
|
|
|
}
|
2019-04-26 13:29:54 +00:00
|
|
|
if (nb == 0) {
|
|
|
|
bar_.window.get_style_context()->add_class("empty");
|
|
|
|
} else if (nb == 1) {
|
2019-04-25 14:47:51 +00:00
|
|
|
bar_.window.get_style_context()->add_class("solo");
|
|
|
|
if (!app_id.empty()) {
|
|
|
|
bar_.window.get_style_context()->add_class(app_id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bar_.window.get_style_context()->remove_class("solo");
|
2019-04-26 13:29:54 +00:00
|
|
|
bar_.window.get_style_context()->remove_class("empty");
|
2019-04-25 14:47:51 +00:00
|
|
|
}
|
|
|
|
app_id_ = app_id;
|
|
|
|
if (windowId_ != id || window_ != name) {
|
|
|
|
windowId_ = id;
|
|
|
|
window_ = name;
|
|
|
|
dp.emit();
|
|
|
|
}
|
2019-04-19 09:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::worker() {
|
2018-08-16 12:29:41 +00:00
|
|
|
thread_ = [this] {
|
2018-08-15 18:17:17 +00:00
|
|
|
try {
|
2019-04-19 09:09:06 +00:00
|
|
|
ipc_.handleEvent();
|
2018-08-15 18:17:17 +00:00
|
|
|
} catch (const std::exception& e) {
|
2018-12-09 09:49:28 +00:00
|
|
|
std::cerr << "Window: " << e.what() << std::endl;
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-19 09:09:06 +00:00
|
|
|
auto Window::update() -> void {
|
2018-11-21 19:49:09 +00:00
|
|
|
label_.set_markup(fmt::format(format_, window_));
|
2019-02-22 10:35:26 +00:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
label_.set_tooltip_text(window_);
|
|
|
|
}
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:47:51 +00:00
|
|
|
std::tuple<std::size_t, int, std::string, std::string> Window::getFocusedNode(
|
|
|
|
const Json::Value& nodes) {
|
|
|
|
for (auto const& node : nodes["nodes"]) {
|
2018-08-19 18:37:33 +00:00
|
|
|
if (node["focused"].asBool() && node["type"] == "con") {
|
2019-04-25 14:47:51 +00:00
|
|
|
if ((!config_["all-outputs"].asBool() && nodes["output"] == bar_.output->name) ||
|
|
|
|
config_["all-outputs"].asBool()) {
|
|
|
|
auto app_id = node["app_id"].isString() ? node["app_id"].asString()
|
|
|
|
: node["window_properties"]["instance"].asString();
|
|
|
|
return {nodes["nodes"].size(),
|
|
|
|
node["id"].asInt(),
|
|
|
|
Glib::Markup::escape_text(node["name"].asString()),
|
|
|
|
app_id};
|
|
|
|
}
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2019-04-25 14:47:51 +00:00
|
|
|
auto [nb, id, name, app_id] = getFocusedNode(node);
|
2018-09-18 19:16:35 +00:00
|
|
|
if (id > -1 && !name.empty()) {
|
2019-04-25 14:47:51 +00:00
|
|
|
return {nb, id, name, app_id};
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
2019-04-25 14:47:51 +00:00
|
|
|
return {0, -1, "", ""};
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 14:47:51 +00:00
|
|
|
void Window::getTree() {
|
2018-08-15 18:17:17 +00:00
|
|
|
try {
|
2019-04-19 09:09:06 +00:00
|
|
|
ipc_.sendCmd(IPC_GET_TREE);
|
2019-04-18 15:52:00 +00:00
|
|
|
} catch (const std::exception& e) {
|
2018-08-15 18:17:17 +00:00
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 09:09:06 +00:00
|
|
|
|
|
|
|
} // namespace waybar::modules::sway
|