2018-08-15 18:17:17 +00:00
|
|
|
#include "modules/sway/window.hpp"
|
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
waybar::modules::sway::Window::Window(const std::string& id, const Bar &bar, const Json::Value& config)
|
2018-09-18 19:16:35 +00:00
|
|
|
: ALabel(config, "{}"), bar_(bar), windowId_(-1)
|
2018-08-15 18:17:17 +00:00
|
|
|
{
|
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);
|
|
|
|
}
|
2018-10-29 20:52:53 +00:00
|
|
|
ipc_.subscribe("[\"window\",\"workspace\"]");
|
2018-08-16 12:29:41 +00:00
|
|
|
getFocusedWindow();
|
2018-08-20 12:50:45 +00:00
|
|
|
// Launch worker
|
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::sway::Window::worker()
|
|
|
|
{
|
2018-08-16 12:29:41 +00:00
|
|
|
thread_ = [this] {
|
2018-08-15 18:17:17 +00:00
|
|
|
try {
|
2018-08-20 12:50:45 +00:00
|
|
|
auto res = ipc_.handleEvent();
|
2018-08-16 12:29:41 +00:00
|
|
|
auto parsed = parser_.parse(res.payload);
|
2019-01-26 18:02:11 +00:00
|
|
|
// Check for waybar prevents flicker when hovering window module
|
2018-08-15 22:02:57 +00:00
|
|
|
if ((parsed["change"] == "focus" || parsed["change"] == "title")
|
2019-01-27 10:59:07 +00:00
|
|
|
&& parsed["container"]["focused"].asBool()
|
|
|
|
&& parsed["container"]["name"].asString() != "waybar") {
|
|
|
|
window_ = Glib::Markup::escape_text(parsed["container"]["name"].asString());
|
2018-09-18 19:16:35 +00:00
|
|
|
windowId_ = parsed["container"]["id"].asInt();
|
|
|
|
dp.emit();
|
2018-10-29 20:52:53 +00:00
|
|
|
} else if ((parsed["change"] == "close"
|
2018-09-18 19:16:35 +00:00
|
|
|
&& parsed["container"]["focused"].asBool()
|
2018-10-29 20:52:53 +00:00
|
|
|
&& windowId_ == parsed["container"]["id"].asInt())
|
|
|
|
|| (parsed["change"] == "focus" && parsed["current"]["focus"].isArray()
|
|
|
|
&& parsed["current"]["focus"].empty())) {
|
2018-09-18 19:16:35 +00:00
|
|
|
window_.clear();
|
|
|
|
windowId_ = -1;
|
2018-08-20 12:50:45 +00:00
|
|
|
dp.emit();
|
2018-08-15 22:02:57 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::modules::sway::Window::update() -> void
|
|
|
|
{
|
2018-11-21 19:49:09 +00:00
|
|
|
label_.set_markup(fmt::format(format_, window_));
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.set_tooltip_text(window_);
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 19:16:35 +00:00
|
|
|
std::tuple<int, std::string> waybar::modules::sway::Window::getFocusedNode(
|
|
|
|
Json::Value nodes)
|
2018-08-15 18:17:17 +00:00
|
|
|
{
|
2018-09-04 21:50:08 +00:00
|
|
|
for (auto const& node : nodes) {
|
2018-08-19 18:37:33 +00:00
|
|
|
if (node["focused"].asBool() && node["type"] == "con") {
|
2018-09-18 19:16:35 +00:00
|
|
|
return { node["id"].asInt(), node["name"].asString() };
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-09-18 19:16:35 +00:00
|
|
|
auto [id, name] = getFocusedNode(node["nodes"]);
|
|
|
|
if (id > -1 && !name.empty()) {
|
|
|
|
return { id, name };
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
2018-09-18 19:16:35 +00:00
|
|
|
return { -1, std::string() };
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 12:29:41 +00:00
|
|
|
void waybar::modules::sway::Window::getFocusedWindow()
|
2018-08-15 18:17:17 +00:00
|
|
|
{
|
|
|
|
try {
|
2018-08-20 12:50:45 +00:00
|
|
|
auto res = ipc_.sendCmd(IPC_GET_TREE);
|
2018-08-18 14:01:56 +00:00
|
|
|
auto parsed = parser_.parse(res.payload);
|
2018-09-18 19:16:35 +00:00
|
|
|
auto [id, name] = getFocusedNode(parsed["nodes"]);
|
|
|
|
windowId_ = id;
|
|
|
|
window_ = name;
|
2018-08-15 18:17:17 +00:00
|
|
|
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update));
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|