2018-08-15 18:17:17 +00:00
|
|
|
#include "modules/sway/window.hpp"
|
|
|
|
#include "modules/sway/ipc/client.hpp"
|
|
|
|
|
|
|
|
waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
|
2018-08-18 09:43:48 +00:00
|
|
|
: ALabel(std::move(config)), bar_(bar)
|
2018-08-15 18:17:17 +00:00
|
|
|
{
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.set_name("window");
|
|
|
|
std::string socketPath = getSocketPath();
|
|
|
|
ipcfd_ = ipcOpenSocket(socketPath);
|
|
|
|
ipc_eventfd_ = ipcOpenSocket(socketPath);
|
2018-08-18 14:01:56 +00:00
|
|
|
ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, "[ \"window\" ]");
|
2018-08-16 12:29:41 +00:00
|
|
|
getFocusedWindow();
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_.sig_update.connect(sigc::mem_fun(*this, &Window::update));
|
2018-08-16 12:29:41 +00:00
|
|
|
thread_ = [this] {
|
2018-08-15 18:17:17 +00:00
|
|
|
try {
|
2018-08-16 12:29:41 +00:00
|
|
|
auto res = ipcRecvResponse(ipc_eventfd_);
|
|
|
|
auto parsed = parser_.parse(res.payload);
|
2018-08-15 22:02:57 +00:00
|
|
|
if ((parsed["change"] == "focus" || parsed["change"] == "title")
|
|
|
|
&& parsed["container"]["focused"].asBool()) {
|
2018-08-16 12:29:41 +00:00
|
|
|
window_ = parsed["container"]["name"].asString();
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_.sig_update.emit();
|
2018-08-15 22:02:57 +00:00
|
|
|
}
|
2018-08-15 18:17:17 +00:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-19 11:39:57 +00:00
|
|
|
waybar::modules::sway::Window::~Window()
|
|
|
|
{
|
|
|
|
close(ipcfd_);
|
|
|
|
close(ipc_eventfd_);
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:17:17 +00:00
|
|
|
auto waybar::modules::sway::Window::update() -> void
|
|
|
|
{
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.set_text(window_);
|
|
|
|
label_.set_tooltip_text(window_);
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 12:29:41 +00:00
|
|
|
std::string waybar::modules::sway::Window::getFocusedNode(Json::Value nodes)
|
2018-08-15 18:17:17 +00:00
|
|
|
{
|
|
|
|
for (auto &node : nodes) {
|
2018-08-16 12:29:41 +00:00
|
|
|
if (node["focused"].asBool()) {
|
2018-08-15 18:17:17 +00:00
|
|
|
return node["name"].asString();
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
auto res = getFocusedNode(node["nodes"]);
|
|
|
|
if (!res.empty()) {
|
2018-08-15 18:17:17 +00:00
|
|
|
return res;
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-08-15 18:17:17 +00:00
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
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-18 14:01:56 +00:00
|
|
|
auto res = ipcSingleCommand(ipcfd_, IPC_GET_TREE, "");
|
|
|
|
auto parsed = parser_.parse(res.payload);
|
2018-08-16 12:29:41 +00:00
|
|
|
window_ = getFocusedNode(parsed["nodes"]);
|
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;
|
|
|
|
}
|
|
|
|
}
|