2018-08-15 18:17:17 +00:00
|
|
|
#include "modules/sway/workspaces.hpp"
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
waybar::modules::sway::Workspaces::Workspaces(const std::string& id, const Bar& bar,
|
2018-08-20 12:50:45 +00:00
|
|
|
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-12-18 16:30:54 +00:00
|
|
|
if (!id.empty()) {
|
|
|
|
box_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-08-20 12:50:45 +00:00
|
|
|
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 {
|
2019-02-01 23:36:52 +00:00
|
|
|
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);
|
2019-01-13 21:23:09 +00:00
|
|
|
if (thread_.isRunning()) {
|
|
|
|
workspaces_ = parser_.parse(res.payload);
|
|
|
|
}
|
2018-08-20 12:50:45 +00:00
|
|
|
}
|
|
|
|
dp.emit();
|
2018-08-13 19:23:43 +00:00
|
|
|
} catch (const std::exception& e) {
|
2018-12-09 09:49:28 +00:00
|
|
|
std::cerr << "Workspaces: " << e.what() << std::endl;
|
2018-08-13 19:23:43 +00:00
|
|
|
}
|
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-11-05 19:16:19 +00:00
|
|
|
[it](auto node) -> bool { return node["name"].asString() == it->first; });
|
2018-11-05 19:59:28 +00:00
|
|
|
if (ws == workspaces_.end() ||
|
2019-02-01 23:36:52 +00:00
|
|
|
(!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output_name)) {
|
2018-08-16 12:29:41 +00:00
|
|
|
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
|
|
|
}
|
2018-11-05 19:16:19 +00:00
|
|
|
auto it = buttons_.find(node["name"].asString());
|
2018-08-16 12:29:41 +00:00
|
|
|
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()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("focused");
|
2018-08-16 12:29:41 +00:00
|
|
|
} else {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->remove_class("focused");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (node["visible"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("visible");
|
2018-08-16 12:29:41 +00:00
|
|
|
} else {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->remove_class("visible");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (node["urgent"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("urgent");
|
2018-08-16 12:29:41 +00:00
|
|
|
} else {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->remove_class("urgent");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (needReorder) {
|
2019-02-01 23:36:52 +00:00
|
|
|
box_.reorder_child(button, getWorkspaceIndex(node["name"].asString()));
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-09-04 22:16:56 +00:00
|
|
|
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-12-28 17:35:21 +00:00
|
|
|
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
|
2018-10-22 08:41:52 +00:00
|
|
|
fmt::arg("index", node["num"].asString())));
|
2018-09-18 18:58:11 +00:00
|
|
|
} else {
|
|
|
|
button.set_label(icon);
|
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
onButtonReady(node, button);
|
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
|
|
|
}
|
|
|
|
|
2019-02-01 23:36:52 +00:00
|
|
|
void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node)
|
2018-08-08 21:54:58 +00:00
|
|
|
{
|
2018-09-04 22:16:56 +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-12-28 17:35:21 +00:00
|
|
|
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
|
2018-10-25 08:22:11 +00:00
|
|
|
fmt::arg("index", node["num"].asString()))
|
2018-09-18 18:58:11 +00:00
|
|
|
: icon;
|
2018-11-05 19:16:19 +00:00
|
|
|
auto pair = buttons_.emplace(node["name"].asString(), 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-16 15:12:45 +00:00
|
|
|
if (!config_["disable-scroll"].asBool()) {
|
2019-03-18 13:44:07 +00:00
|
|
|
button.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
|
2018-08-16 15:12:45 +00:00
|
|
|
button.signal_scroll_event()
|
|
|
|
.connect(sigc::mem_fun(*this, &Workspaces::handleScroll));
|
|
|
|
}
|
2019-02-01 23:36:52 +00:00
|
|
|
box_.reorder_child(button, getWorkspaceIndex(node["name"].asString()));
|
2018-08-16 12:29:41 +00:00
|
|
|
if (node["focused"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("focused");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (node["visible"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("visible");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (node["urgent"].asBool()) {
|
2018-08-15 13:03:51 +00:00
|
|
|
button.get_style_context()->add_class("urgent");
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2019-03-03 10:35:32 +00:00
|
|
|
|
|
|
|
onButtonReady(node, button);
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 23:36:52 +00:00
|
|
|
std::string waybar::modules::sway::Workspaces::getIcon(const std::string &name,
|
|
|
|
const Json::Value &node)
|
2018-08-15 12:48:08 +00:00
|
|
|
{
|
2019-02-01 23:36:52 +00:00
|
|
|
std::vector<std::string> keys = { name, "urgent", "focused", "visible", "default" };
|
2018-09-04 22:16:56 +00:00
|
|
|
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()) {
|
2018-09-04 22:16:56 +00:00
|
|
|
return config_["format-icons"][key].asString();
|
|
|
|
}
|
2018-10-26 07:27:16 +00:00
|
|
|
} else if (config_["format-icons"][key].isString()) {
|
2018-09-04 22:16:56 +00:00
|
|
|
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
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
uint8_t idx;
|
2018-08-16 12:29:41 +00:00
|
|
|
scrolling_ = true;
|
2019-03-18 13:44:07 +00:00
|
|
|
for (idx = 0; idx < workspaces_.size(); idx += 1) {
|
|
|
|
if (workspaces_[idx]["focused"].asBool()) {
|
|
|
|
break;
|
2018-08-14 09:26:06 +00:00
|
|
|
}
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
if (idx == workspaces_.size()) {
|
2018-08-16 12:29:41 +00:00
|
|
|
scrolling_ = false;
|
2018-08-14 09:26:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
std::string name;
|
2018-08-16 12:29:41 +00:00
|
|
|
if (e->direction == GDK_SCROLL_UP) {
|
2019-03-18 13:44:07 +00:00
|
|
|
name = getCycleWorkspace(idx, false);
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
if (e->direction == GDK_SCROLL_DOWN) {
|
2019-03-18 13:44:07 +00:00
|
|
|
name = getCycleWorkspace(idx, true);
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
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) {
|
2019-03-18 13:44:07 +00:00
|
|
|
name = getCycleWorkspace(idx, false);
|
2018-08-16 12:29:41 +00:00
|
|
|
} else if (delta_y > 0) {
|
2019-03-18 13:44:07 +00:00
|
|
|
name = getCycleWorkspace(idx, true);
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
if (name.empty() || name == workspaces_[idx]["name"].asString()) {
|
|
|
|
scrolling_ = false;
|
|
|
|
return false;
|
2018-08-14 09:26:06 +00:00
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", name));
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(150));
|
2018-08-14 09:26:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-18 13:44:07 +00:00
|
|
|
const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(
|
|
|
|
uint8_t focused_workspace, bool prev) const
|
2018-08-14 09:26:06 +00:00
|
|
|
{
|
2019-03-18 13:44:07 +00:00
|
|
|
auto inc = prev ? -1 : 1;
|
|
|
|
int size = workspaces_.size();
|
|
|
|
uint8_t idx = 0;
|
|
|
|
for (int i = focused_workspace; i < size && i >= 0; i += inc) {
|
|
|
|
bool same_output = (workspaces_[i]["output"].asString() == bar_.output_name
|
|
|
|
&& !config_["all-outputs"].asBool()) || config_["all-outputs"].asBool();
|
|
|
|
bool same_name =
|
|
|
|
workspaces_[i]["name"].asString() == workspaces_[focused_workspace]["name"].asString();
|
|
|
|
if (same_output && !same_name) {
|
|
|
|
return workspaces_[i]["name"].asString();
|
2018-08-14 09:26:06 +00:00
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
if (prev && i - 1 < 0) {
|
|
|
|
i = size;
|
|
|
|
} else if (!prev && i + 1 >= size) {
|
|
|
|
i = -1;
|
|
|
|
} else if (idx >= workspaces_.size()) {
|
|
|
|
return "";
|
2018-08-14 09:26:06 +00:00
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
idx += 1;
|
2018-08-16 12:29:41 +00:00
|
|
|
}
|
2018-11-05 19:16:19 +00:00
|
|
|
return "";
|
2018-08-14 09:26:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 13:44:07 +00:00
|
|
|
uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name) const
|
2019-02-01 23:36:52 +00:00
|
|
|
{
|
2019-02-02 11:07:59 +00:00
|
|
|
uint16_t idx = 0;
|
|
|
|
for (const auto &workspace : workspaces_) {
|
|
|
|
if (workspace["name"].asString() == name) {
|
|
|
|
return idx;
|
|
|
|
}
|
2019-03-18 13:44:07 +00:00
|
|
|
if (!(!config_["all-outputs"].asBool() && workspace["output"].asString() != bar_.output_name)) {
|
2019-02-02 11:07:59 +00:00
|
|
|
idx += 1;
|
2019-02-01 23:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return workspaces_.size();
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
2018-12-28 17:35:21 +00:00
|
|
|
|
|
|
|
std::string waybar::modules::sway::Workspaces::trimWorkspaceName(std::string name)
|
|
|
|
{
|
|
|
|
std::size_t found = name.find(":");
|
|
|
|
if (found!=std::string::npos) {
|
|
|
|
return name.substr(found+1);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
2019-02-01 23:36:52 +00:00
|
|
|
|
2019-03-03 10:35:32 +00:00
|
|
|
void waybar::modules::sway::Workspaces::onButtonReady(const Json::Value& node, Gtk::Button& button)
|
|
|
|
{
|
|
|
|
if (config_["current-only"].asBool()) {
|
|
|
|
if (node["focused"].asBool()) {
|
|
|
|
button.show();
|
|
|
|
} else {
|
|
|
|
button.hide();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
button.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 23:36:52 +00:00
|
|
|
waybar::modules::sway::Workspaces::operator Gtk::Widget &() {
|
|
|
|
return box_;
|
|
|
|
}
|