2018-08-15 20:17:17 +02:00
|
|
|
#include "modules/sway/window.hpp"
|
|
|
|
|
2019-04-19 11:09:06 +02:00
|
|
|
namespace waybar::modules::sway {
|
|
|
|
|
|
|
|
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
2019-04-18 17:52:00 +02:00
|
|
|
: ALabel(config, "{}"), bar_(bar), windowId_(-1) {
|
2018-08-16 14:29:41 +02:00
|
|
|
label_.set_name("window");
|
2018-12-18 17:30:54 +01:00
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-11-20 23:24:33 +01:00
|
|
|
if (label_.get_max_width_chars() == -1) {
|
|
|
|
label_.set_hexpand(true);
|
|
|
|
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
|
|
|
|
}
|
2019-04-24 12:37:24 +02:00
|
|
|
ipc_.subscribe(R"(["window","workspace"])");
|
2019-04-19 11:09:06 +02:00
|
|
|
ipc_.signal_event.connect(sigc::mem_fun(*this, &Window::onEvent));
|
|
|
|
ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Window::onCmd));
|
2018-08-16 14:29:41 +02:00
|
|
|
getFocusedWindow();
|
2018-08-20 14:50:45 +02:00
|
|
|
// Launch worker
|
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:37:24 +02:00
|
|
|
void Window::onEvent(const struct Ipc::ipc_response& res) {
|
2019-04-23 11:41:49 +02:00
|
|
|
auto data = res.payload;
|
2019-04-19 11:09:06 +02:00
|
|
|
// Check for waybar prevents flicker when hovering window module
|
2019-04-23 11:41:49 +02:00
|
|
|
if ((data["change"] == "focus" || data["change"] == "title") &&
|
|
|
|
data["container"]["focused"].asBool() && data["container"]["name"].asString() != "waybar") {
|
|
|
|
window_ = Glib::Markup::escape_text(data["container"]["name"].asString());
|
|
|
|
windowId_ = data["container"]["id"].asInt();
|
2019-04-19 11:09:06 +02:00
|
|
|
dp.emit();
|
2019-04-23 11:41:49 +02:00
|
|
|
} else if ((data["change"] == "close" && data["container"]["focused"].asBool() &&
|
|
|
|
windowId_ == data["container"]["id"].asInt()) ||
|
|
|
|
(data["change"] == "focus" && data["current"]["focus"].isArray() &&
|
|
|
|
data["current"]["focus"].empty())) {
|
2019-04-19 11:09:06 +02:00
|
|
|
window_.clear();
|
|
|
|
windowId_ = -1;
|
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:37:24 +02:00
|
|
|
void Window::onCmd(const struct Ipc::ipc_response& res) {
|
2019-04-23 11:41:49 +02:00
|
|
|
auto [id, name] = getFocusedNode(res.payload["nodes"]);
|
2019-04-19 11:09:06 +02:00
|
|
|
windowId_ = id;
|
|
|
|
window_ = name;
|
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::worker() {
|
2018-08-16 14:29:41 +02:00
|
|
|
thread_ = [this] {
|
2018-08-15 20:17:17 +02:00
|
|
|
try {
|
2019-04-19 11:09:06 +02:00
|
|
|
ipc_.handleEvent();
|
2018-08-15 20:17:17 +02:00
|
|
|
} catch (const std::exception& e) {
|
2018-12-09 10:49:28 +01:00
|
|
|
std::cerr << "Window: " << e.what() << std::endl;
|
2018-08-15 20:17:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-19 11:09:06 +02:00
|
|
|
auto Window::update() -> void {
|
2018-11-21 20:49:09 +01:00
|
|
|
label_.set_markup(fmt::format(format_, window_));
|
2019-02-22 11:35:26 +01:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
label_.set_tooltip_text(window_);
|
|
|
|
}
|
2018-08-15 20:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 12:37:24 +02:00
|
|
|
std::tuple<int, std::string> Window::getFocusedNode(const Json::Value& nodes) {
|
2018-09-04 23:50:08 +02:00
|
|
|
for (auto const& node : nodes) {
|
2018-08-19 20:37:33 +02:00
|
|
|
if (node["focused"].asBool() && node["type"] == "con") {
|
2019-04-18 17:52:00 +02:00
|
|
|
return {node["id"].asInt(), node["name"].asString()};
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-09-18 21:16:35 +02:00
|
|
|
auto [id, name] = getFocusedNode(node["nodes"]);
|
|
|
|
if (id > -1 && !name.empty()) {
|
2019-04-18 17:52:00 +02:00
|
|
|
return {id, name};
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-15 20:17:17 +02:00
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
return {-1, std::string()};
|
2018-08-15 20:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 11:09:06 +02:00
|
|
|
void Window::getFocusedWindow() {
|
2018-08-15 20:17:17 +02:00
|
|
|
try {
|
2019-04-19 11:09:06 +02:00
|
|
|
ipc_.sendCmd(IPC_GET_TREE);
|
2019-04-18 17:52:00 +02:00
|
|
|
} catch (const std::exception& e) {
|
2018-08-15 20:17:17 +02:00
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 11:09:06 +02:00
|
|
|
|
|
|
|
} // namespace waybar::modules::sway
|