Waybar/src/modules/sway/workspaces.cpp

233 lines
6.7 KiB
C++
Raw Normal View History

2018-08-15 18:17:17 +00:00
#include "modules/sway/workspaces.hpp"
2018-08-08 21:54:58 +00:00
2018-08-20 12:50:45 +00:00
waybar::modules::sway::Workspaces::Workspaces(Bar& bar,
const Json::Value& config)
: bar_(bar), config_(config), scrolling_(false)
2018-08-08 21:54:58 +00:00
{
2018-08-16 12:29:41 +00:00
box_.set_name("workspaces");
2018-08-20 12:50:45 +00:00
ipc_.connect();
ipc_.subscribe("[ \"workspace\" ]");
// Launch worker
worker();
}
void waybar::modules::sway::Workspaces::worker()
{
2018-08-16 12:29:41 +00:00
thread_ = [this] {
2018-08-13 19:23:43 +00:00
try {
// Wait for the name of the output
2018-08-19 11:39:57 +00:00
if (!config_["all-outputs"].asBool() && bar_.output_name.empty()) {
while (bar_.output_name.empty()) {
2018-08-16 12:29:41 +00:00
thread_.sleep_for(chrono::milliseconds(150));
}
} else if (!workspaces_.empty()) {
2018-08-20 12:50:45 +00:00
ipc_.handleEvent();
2018-08-16 12:29:41 +00:00
}
2018-08-20 12:50:45 +00:00
{
std::lock_guard<std::mutex> lock(mutex_);
auto res = ipc_.sendCmd(IPC_GET_WORKSPACES);
workspaces_ = parser_.parse(res.payload);
}
dp.emit();
2018-08-13 19:23:43 +00:00
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
2018-08-09 18:22:01 +00:00
};
2018-08-08 21:54:58 +00:00
}
2018-08-15 18:17:17 +00:00
auto waybar::modules::sway::Workspaces::update() -> void
2018-08-08 21:54:58 +00:00
{
2018-08-10 16:02:12 +00:00
bool needReorder = false;
2018-08-20 12:50:45 +00:00
std::lock_guard<std::mutex> lock(mutex_);
2018-08-16 12:29:41 +00:00
for (auto it = buttons_.begin(); it != buttons_.end();) {
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(),
2018-08-08 21:54:58 +00:00
[it](auto node) -> bool { return node["num"].asInt() == it->first; });
2018-08-16 12:29:41 +00:00
if (ws == workspaces_.end()) {
it = buttons_.erase(it);
2018-08-10 16:02:12 +00:00
needReorder = true;
2018-08-16 12:29:41 +00:00
} else {
2018-08-15 12:30:01 +00:00
++it;
2018-08-16 12:29:41 +00:00
}
2018-08-08 21:54:58 +00:00
}
2018-09-04 21:50:08 +00:00
for (auto const& node : workspaces_) {
2018-08-16 12:29:41 +00:00
if (!config_["all-outputs"].asBool()
2018-08-19 11:39:57 +00:00
&& bar_.output_name != node["output"].asString()) {
2018-08-13 19:23:43 +00:00
continue;
2018-08-16 12:29:41 +00:00
}
auto it = buttons_.find(node["num"].asInt());
if (it == buttons_.end()) {
addWorkspace(node);
2018-08-10 16:02:12 +00:00
needReorder = true;
2018-08-08 21:54:58 +00:00
} else {
2018-08-13 19:23:43 +00:00
auto &button = it->second;
2018-08-16 12:29:41 +00:00
if (node["focused"].asBool()) {
button.get_style_context()->add_class("focused");
2018-08-16 12:29:41 +00:00
} else {
button.get_style_context()->remove_class("focused");
2018-08-16 12:29:41 +00:00
}
if (node["visible"].asBool()) {
button.get_style_context()->add_class("visible");
2018-08-16 12:29:41 +00:00
} else {
button.get_style_context()->remove_class("visible");
2018-08-16 12:29:41 +00:00
}
if (node["urgent"].asBool()) {
button.get_style_context()->add_class("urgent");
2018-08-16 12:29:41 +00:00
} else {
button.get_style_context()->remove_class("urgent");
2018-08-16 12:29:41 +00:00
}
if (needReorder) {
box_.reorder_child(button, node["num"].asInt());
}
auto icon = getIcon(node["name"].asString(), node);
2018-10-26 07:27:16 +00:00
if (config_["format"].isString()) {
2018-09-18 18:58:11 +00:00
auto format = config_["format"].asString();
button.set_label(fmt::format(format, fmt::arg("icon", icon),
2018-10-22 08:41:52 +00:00
fmt::arg("name", node["name"].asString()),
fmt::arg("index", node["num"].asString())));
2018-09-18 18:58:11 +00:00
} else {
button.set_label(icon);
}
2018-08-13 19:23:43 +00:00
button.show();
2018-08-08 21:54:58 +00:00
}
}
2018-08-16 12:29:41 +00:00
if (scrolling_) {
scrolling_ = false;
}
2018-08-08 21:54:58 +00:00
}
2018-08-16 12:29:41 +00:00
void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
2018-08-08 21:54:58 +00:00
{
auto icon = getIcon(node["name"].asString(), node);
2018-10-26 07:27:16 +00:00
auto format = config_["format"].isString()
2018-09-18 18:58:11 +00:00
? fmt::format(config_["format"].asString(), fmt::arg("icon", icon),
2018-10-25 08:22:11 +00:00
fmt::arg("name", node["name"].asString()),
fmt::arg("index", node["num"].asString()))
2018-09-18 18:58:11 +00:00
: icon;
auto pair = buttons_.emplace(node["num"].asInt(), format);
2018-08-08 21:54:58 +00:00
auto &button = pair.first->second;
2018-08-16 12:29:41 +00:00
box_.pack_start(button, false, false, 0);
2018-08-08 21:54:58 +00:00
button.set_relief(Gtk::RELIEF_NONE);
button.signal_clicked().connect([this, pair] {
2018-08-11 00:09:39 +00:00
try {
2018-08-16 12:29:41 +00:00
std::lock_guard<std::mutex> lock(mutex_);
2018-08-18 14:01:56 +00:00
auto cmd = fmt::format("workspace \"{}\"", pair.first->first);
2018-08-20 12:50:45 +00:00
ipc_.sendCmd(IPC_COMMAND, cmd);
2018-08-11 00:09:39 +00:00
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
2018-08-08 21:54:58 +00:00
});
2018-08-14 09:26:06 +00:00
button.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
if (!config_["disable-scroll"].asBool()) {
button.signal_scroll_event()
.connect(sigc::mem_fun(*this, &Workspaces::handleScroll));
}
2018-08-16 12:29:41 +00:00
box_.reorder_child(button, node["num"].asInt());
if (node["focused"].asBool()) {
button.get_style_context()->add_class("focused");
2018-08-16 12:29:41 +00:00
}
if (node["visible"].asBool()) {
button.get_style_context()->add_class("visible");
2018-08-16 12:29:41 +00:00
}
if (node["urgent"].asBool()) {
button.get_style_context()->add_class("urgent");
2018-08-16 12:29:41 +00:00
}
2018-08-08 21:54:58 +00:00
button.show();
}
std::string waybar::modules::sway::Workspaces::getIcon(std::string name,
Json::Value node)
2018-08-15 12:48:08 +00:00
{
std::vector<std::string> keys = {
name, "urgent", "focused", "visible", "default"};
for (auto const& key : keys) {
if (key == "focused" || key == "visible" || key == "urgent") {
2018-10-26 07:27:16 +00:00
if (config_["format-icons"][key].isString() && node[key].asBool()) {
return config_["format-icons"][key].asString();
}
2018-10-26 07:27:16 +00:00
} else if (config_["format-icons"][key].isString()) {
return config_["format-icons"][key].asString();
}
2018-08-16 12:29:41 +00:00
}
2018-08-15 12:48:08 +00:00
return name;
}
2018-08-16 12:29:41 +00:00
bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
2018-08-14 09:26:06 +00:00
{
// Avoid concurrent scroll event
2018-08-16 12:29:41 +00:00
if (scrolling_) {
2018-08-14 09:26:06 +00:00
return false;
2018-08-16 12:29:41 +00:00
}
scrolling_ = true;
2018-08-14 09:26:06 +00:00
int id = -1;
uint16_t idx = 0;
2018-08-20 12:50:45 +00:00
{
std::lock_guard<std::mutex> lock(mutex_);
for (; idx < workspaces_.size(); idx += 1) {
if (workspaces_[idx]["focused"].asBool()) {
id = workspaces_[idx]["num"].asInt();
break;
}
2018-08-14 09:26:06 +00:00
}
2018-08-16 12:29:41 +00:00
}
2018-08-14 09:26:06 +00:00
if (id == -1) {
2018-08-16 12:29:41 +00:00
scrolling_ = false;
2018-08-14 09:26:06 +00:00
return false;
}
2018-08-16 12:29:41 +00:00
if (e->direction == GDK_SCROLL_UP) {
id = getNextWorkspace();
}
if (e->direction == GDK_SCROLL_DOWN) {
id = getPrevWorkspace();
}
2018-08-14 09:26:06 +00:00
if (e->direction == GDK_SCROLL_SMOOTH) {
gdouble delta_x, delta_y;
2018-08-16 12:29:41 +00:00
gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent *>(e),
&delta_x, &delta_y);
if (delta_y < 0) {
id = getNextWorkspace();
} else if (delta_y > 0) {
id = getPrevWorkspace();
}
}
2018-08-20 12:50:45 +00:00
{
std::lock_guard<std::mutex> lock(mutex_);
if (id == workspaces_[idx]["num"].asInt()) {
scrolling_ = false;
return false;
}
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", id));
std::this_thread::sleep_for(std::chrono::milliseconds(150));
2018-08-14 09:26:06 +00:00
}
return true;
}
2018-08-16 12:29:41 +00:00
int waybar::modules::sway::Workspaces::getPrevWorkspace()
2018-08-14 09:26:06 +00:00
{
2018-08-16 12:29:41 +00:00
for (uint16_t i = 0; i != workspaces_.size(); i += 1) {
if (workspaces_[i]["focused"].asBool()) {
if (i > 0) {
return workspaces_[i - 1]["num"].asInt();
}
return workspaces_[workspaces_.size() - 1]["num"].asInt();
2018-08-14 09:26:06 +00:00
}
2018-08-16 12:29:41 +00:00
}
return -1;
2018-08-14 09:26:06 +00:00
}
2018-08-16 12:29:41 +00:00
int waybar::modules::sway::Workspaces::getNextWorkspace()
2018-08-14 09:26:06 +00:00
{
2018-08-16 12:29:41 +00:00
for (uint16_t i = 0; i != workspaces_.size(); i += 1) {
if (workspaces_[i]["focused"].asBool()) {
if (i + 1U < workspaces_.size()) {
return workspaces_[i + 1]["num"].asInt();
}
return workspaces_[0]["num"].asInt();
2018-08-14 09:26:06 +00:00
}
2018-08-16 12:29:41 +00:00
}
return -1;
2018-08-14 09:26:06 +00:00
}
2018-08-15 18:17:17 +00:00
waybar::modules::sway::Workspaces::operator Gtk::Widget &() {
2018-08-16 12:29:41 +00:00
return box_;
2018-08-08 21:54:58 +00:00
}