2023-06-27 23:52:01 +00:00
|
|
|
#include "modules/hyprland/workspaces.hpp"
|
|
|
|
|
2023-06-30 21:18:57 +00:00
|
|
|
#include <json/value.h>
|
2023-06-27 23:52:01 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2023-06-30 21:18:57 +00:00
|
|
|
#include <charconv>
|
2023-07-03 22:17:26 +00:00
|
|
|
#include <memory>
|
2023-06-27 23:52:01 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace waybar::modules::hyprland {
|
2023-06-30 21:18:57 +00:00
|
|
|
|
2023-06-27 23:52:01 +00:00
|
|
|
Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value &config)
|
|
|
|
: AModule(config, "workspaces", id, false, false),
|
|
|
|
bar_(bar),
|
|
|
|
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0) {
|
2023-06-30 21:18:57 +00:00
|
|
|
Json::Value config_format = config["format"];
|
|
|
|
|
|
|
|
format_ = config_format.isString() ? config_format.asString() : "{id}";
|
|
|
|
with_icon_ = format_.find("{icon}") != std::string::npos;
|
|
|
|
|
|
|
|
if (with_icon_ && icons_map_.empty()) {
|
|
|
|
Json::Value format_icons = config["format-icons"];
|
|
|
|
for (std::string &name : format_icons.getMemberNames()) {
|
|
|
|
icons_map_.emplace(name, format_icons[name].asString());
|
|
|
|
}
|
|
|
|
|
|
|
|
icons_map_.emplace("", "");
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:43:22 +00:00
|
|
|
auto config_all_outputs = config_["all-outputs"];
|
|
|
|
if (config_all_outputs.isBool()) {
|
|
|
|
all_outputs_ = config_all_outputs.asBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto config_show_special = config_["show-special"];
|
|
|
|
if (config_show_special.isBool()) {
|
|
|
|
show_special_ = config_show_special.asBool();
|
|
|
|
}
|
|
|
|
|
2023-06-27 23:52:01 +00:00
|
|
|
box_.set_name("workspaces");
|
|
|
|
if (!id.empty()) {
|
|
|
|
box_.get_style_context()->add_class(id);
|
|
|
|
}
|
|
|
|
event_box_.add(box_);
|
|
|
|
modulesReady = true;
|
|
|
|
if (!gIPC.get()) {
|
|
|
|
gIPC = std::make_unique<IPC>();
|
|
|
|
}
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
2023-06-30 21:18:57 +00:00
|
|
|
gIPC->registerForIPC("workspace", this);
|
2023-06-27 23:52:01 +00:00
|
|
|
gIPC->registerForIPC("createworkspace", this);
|
|
|
|
gIPC->registerForIPC("destroyworkspace", this);
|
2023-07-15 17:43:22 +00:00
|
|
|
gIPC->registerForIPC("focusedmon", this);
|
|
|
|
gIPC->registerForIPC("moveworkspace", this);
|
2023-06-27 23:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto Workspaces::update() -> void {
|
2023-07-15 17:43:22 +00:00
|
|
|
for (std::string workspace_to_remove : workspaces_to_remove_) {
|
2023-07-03 22:17:26 +00:00
|
|
|
remove_workspace(workspace_to_remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
workspaces_to_remove_.clear();
|
|
|
|
|
2023-07-16 01:43:25 +00:00
|
|
|
for (Json::Value &workspace_to_create : workspaces_to_create_) {
|
2023-07-03 22:17:26 +00:00
|
|
|
create_workspace(workspace_to_create);
|
|
|
|
}
|
|
|
|
|
2023-07-16 01:43:25 +00:00
|
|
|
workspaces_to_create_.clear();
|
|
|
|
|
2023-07-15 17:43:22 +00:00
|
|
|
for (auto &workspace : workspaces_) {
|
|
|
|
workspace->set_active(workspace->name() == active_workspace_name);
|
2023-06-30 21:18:57 +00:00
|
|
|
std::string &workspace_icon = icons_map_[""];
|
|
|
|
if (with_icon_) {
|
2023-07-03 22:17:26 +00:00
|
|
|
workspace_icon = workspace->select_icon(icons_map_);
|
2023-06-30 21:18:57 +00:00
|
|
|
}
|
2023-07-03 22:17:26 +00:00
|
|
|
workspace->update(format_, workspace_icon);
|
2023-06-27 23:52:01 +00:00
|
|
|
}
|
|
|
|
AModule::update();
|
|
|
|
}
|
|
|
|
|
2023-06-30 21:18:57 +00:00
|
|
|
void Workspaces::onEvent(const std::string &ev) {
|
2023-07-03 22:17:26 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2023-06-30 21:18:57 +00:00
|
|
|
std::string eventName(begin(ev), begin(ev) + ev.find_first_of('>'));
|
|
|
|
std::string payload = ev.substr(eventName.size() + 2);
|
2023-07-15 17:43:22 +00:00
|
|
|
|
2023-06-30 21:18:57 +00:00
|
|
|
if (eventName == "workspace") {
|
2023-07-15 17:43:22 +00:00
|
|
|
active_workspace_name = payload;
|
|
|
|
|
2023-06-30 21:18:57 +00:00
|
|
|
} else if (eventName == "destroyworkspace") {
|
2023-07-15 17:43:22 +00:00
|
|
|
workspaces_to_remove_.push_back(payload);
|
|
|
|
|
2023-06-30 21:18:57 +00:00
|
|
|
} else if (eventName == "createworkspace") {
|
2023-07-15 17:43:22 +00:00
|
|
|
const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces");
|
2023-07-16 01:43:25 +00:00
|
|
|
for (Json::Value workspace_json : workspaces_json) {
|
2023-07-15 17:43:22 +00:00
|
|
|
if (workspace_json["name"].asString() == payload &&
|
|
|
|
(all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) &&
|
|
|
|
(workspace_json["name"].asString().find("special:") != 0 || show_special()))
|
|
|
|
create_workspace(workspace_json);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (eventName == "focusedmon") {
|
|
|
|
active_workspace_name = payload.substr(payload.find(",") + 1);
|
|
|
|
|
2023-07-16 01:18:41 +00:00
|
|
|
} else if (eventName == "moveworkspace" && !all_outputs()) {
|
2023-07-15 17:43:22 +00:00
|
|
|
std::string workspace = payload.substr(0, payload.find(","));
|
2023-07-16 01:18:41 +00:00
|
|
|
std::string new_output = payload.substr(payload.find(",") + 1);
|
|
|
|
if (bar_.output->name == new_output) { // TODO: implement this better
|
|
|
|
const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces");
|
2023-07-16 01:43:25 +00:00
|
|
|
for (Json::Value workspace_json : workspaces_json) {
|
2023-07-16 01:18:41 +00:00
|
|
|
if (workspace_json["name"].asString() == workspace &&
|
|
|
|
bar_.output->name == workspace_json["monitor"].asString()) {
|
2023-07-16 01:43:25 +00:00
|
|
|
workspaces_to_create_.push_back(workspace_json);
|
2023-07-16 01:18:41 +00:00
|
|
|
break;
|
2023-07-15 17:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-16 01:18:41 +00:00
|
|
|
} else {
|
|
|
|
workspaces_to_remove_.push_back(workspace);
|
2023-07-15 17:43:22 +00:00
|
|
|
}
|
2023-06-30 21:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dp.emit();
|
|
|
|
}
|
2023-06-27 23:52:01 +00:00
|
|
|
|
2023-07-16 01:43:25 +00:00
|
|
|
void Workspaces::create_workspace(Json::Value &value) {
|
2023-07-15 17:43:22 +00:00
|
|
|
workspaces_.push_back(std::make_unique<Workspace>(value));
|
2023-07-03 22:17:26 +00:00
|
|
|
Gtk::Button &new_workspace_button = workspaces_.back()->button();
|
|
|
|
box_.pack_start(new_workspace_button, false, false);
|
|
|
|
sort_workspaces();
|
|
|
|
new_workspace_button.show_all();
|
|
|
|
}
|
|
|
|
|
2023-07-15 17:43:22 +00:00
|
|
|
void Workspaces::remove_workspace(std::string name) {
|
2023-07-03 22:24:34 +00:00
|
|
|
auto workspace = std::find_if(workspaces_.begin(), workspaces_.end(),
|
2023-07-15 17:43:22 +00:00
|
|
|
[&](std::unique_ptr<Workspace> &x) { return x->name() == name; });
|
2023-07-03 22:17:26 +00:00
|
|
|
|
2023-07-03 22:24:34 +00:00
|
|
|
if (workspace == workspaces_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-03 22:17:26 +00:00
|
|
|
|
2023-07-03 22:24:34 +00:00
|
|
|
box_.remove(workspace->get()->button());
|
|
|
|
workspaces_.erase(workspace);
|
2023-07-03 22:17:26 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 23:52:01 +00:00
|
|
|
void Workspaces::init() {
|
2023-07-15 17:43:22 +00:00
|
|
|
active_workspace_name = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString();
|
|
|
|
|
2023-06-27 23:52:01 +00:00
|
|
|
const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces");
|
2023-07-16 01:43:25 +00:00
|
|
|
for (Json::Value workspace_json : workspaces_json) {
|
2023-07-15 17:43:22 +00:00
|
|
|
if ((all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) &&
|
|
|
|
(workspace_json["name"].asString().find("special") != 0 || show_special()))
|
|
|
|
create_workspace(workspace_json);
|
2023-06-27 23:52:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 21:18:57 +00:00
|
|
|
sort_workspaces();
|
|
|
|
|
2023-06-27 23:52:01 +00:00
|
|
|
dp.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
Workspaces::~Workspaces() {
|
|
|
|
gIPC->unregisterForIPC(this);
|
|
|
|
// wait for possible event handler to finish
|
|
|
|
std::lock_guard<std::mutex> lg(mutex_);
|
|
|
|
}
|
|
|
|
|
2023-07-15 21:48:12 +00:00
|
|
|
Workspace::Workspace(const Json::Value &value)
|
|
|
|
: id_(value["id"].asInt()),
|
|
|
|
name_(value["name"].asString()),
|
2023-07-16 01:18:41 +00:00
|
|
|
output_(value["monitor"].asString()), // TODO:allow using monitor desc
|
2023-07-15 21:48:12 +00:00
|
|
|
windows_(value["id"].asInt()) {
|
|
|
|
active_ = true;
|
|
|
|
is_special_ = false;
|
2023-07-15 17:43:22 +00:00
|
|
|
|
|
|
|
if (name_.find("name:") == 0) {
|
|
|
|
name_ = name_.substr(5);
|
|
|
|
} else if (name_.find("special") == 0) {
|
|
|
|
name_ = id_ == -99 ? name_ : name_.substr(13);
|
|
|
|
is_special_ = 1;
|
|
|
|
}
|
2023-06-27 23:52:01 +00:00
|
|
|
|
2023-07-15 17:43:22 +00:00
|
|
|
button_.add_events(Gdk::BUTTON_PRESS_MASK);
|
|
|
|
button_.signal_button_press_event().connect(sigc::mem_fun(*this, &Workspace::handle_clicked),
|
|
|
|
false);
|
2023-06-30 21:18:57 +00:00
|
|
|
|
2023-06-27 23:52:01 +00:00
|
|
|
button_.set_relief(Gtk::RELIEF_NONE);
|
|
|
|
content_.set_center_widget(label_);
|
|
|
|
button_.add(content_);
|
|
|
|
};
|
|
|
|
|
2023-06-30 21:18:57 +00:00
|
|
|
void add_or_remove_class(Glib::RefPtr<Gtk::StyleContext> context, bool condition,
|
|
|
|
const std::string &class_name) {
|
|
|
|
if (condition) {
|
|
|
|
context->add_class(class_name);
|
|
|
|
} else {
|
|
|
|
context->remove_class(class_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Workspace::update(const std::string &format, const std::string &icon) {
|
|
|
|
Glib::RefPtr<Gtk::StyleContext> style_context = button_.get_style_context();
|
|
|
|
add_or_remove_class(style_context, active(), "active");
|
|
|
|
|
2023-07-15 17:43:22 +00:00
|
|
|
label_.set_markup(fmt::format(fmt::runtime(format), fmt::arg("id", id()),
|
|
|
|
fmt::arg("name", name()), fmt::arg("icon", icon)));
|
2023-06-30 21:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Workspaces::sort_workspaces() {
|
|
|
|
std::sort(workspaces_.begin(), workspaces_.end(),
|
2023-07-15 17:43:22 +00:00
|
|
|
[](std::unique_ptr<Workspace> &a, std::unique_ptr<Workspace> &b) {
|
|
|
|
// normal -> named -> special -> named special
|
|
|
|
if (a->id() > 0 && b->id() > 0) {
|
|
|
|
return a->id() < b->id();
|
|
|
|
}
|
|
|
|
if (a->id() < 0 && b->id() < 0) {
|
|
|
|
if ((a->is_special()) ^ (a->is_special())) {
|
|
|
|
return a->id() > b->id();
|
|
|
|
} else {
|
|
|
|
return a->id() < b->id();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((a->id() > 0) ^ (b->id() > 0)) {
|
|
|
|
return a->id() > b->id();
|
|
|
|
}
|
|
|
|
spdlog::error("huh!!?");
|
|
|
|
return false;
|
2023-07-03 22:17:26 +00:00
|
|
|
});
|
2023-06-30 21:18:57 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < workspaces_.size(); ++i) {
|
2023-07-03 22:17:26 +00:00
|
|
|
box_.reorder_child(workspaces_[i]->button(), i);
|
2023-06-30 21:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string &Workspace::select_icon(std::map<std::string, std::string> &icons_map) {
|
|
|
|
if (active()) {
|
|
|
|
auto active_icon_it = icons_map.find("active");
|
|
|
|
if (active_icon_it != icons_map.end()) {
|
|
|
|
return active_icon_it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto named_icon_it = icons_map.find(std::to_string(id()));
|
|
|
|
if (named_icon_it != icons_map.end()) {
|
|
|
|
return named_icon_it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto default_icon_it = icons_map.find("default");
|
|
|
|
if (default_icon_it != icons_map.end()) {
|
|
|
|
return default_icon_it->second;
|
|
|
|
}
|
|
|
|
return icons_map[""];
|
|
|
|
}
|
2023-07-15 17:43:22 +00:00
|
|
|
|
|
|
|
auto Workspace::handle_clicked(GdkEventButton *bt) -> bool {
|
2023-07-15 21:48:12 +00:00
|
|
|
try {
|
|
|
|
if (id() > 0) { // normal
|
|
|
|
gIPC->getSocket1Reply("dispatch workspace " + std::to_string(id()));
|
|
|
|
} else if (!is_special()) { // named normal
|
|
|
|
gIPC->getSocket1Reply("dispatch workspace name" + name());
|
|
|
|
} else if (id() != -99) { // named special
|
|
|
|
gIPC->getSocket1Reply("dispatch togglespecialworkspace name" + name());
|
|
|
|
} else { // special
|
|
|
|
gIPC->getSocket1Reply("dispatch togglespecialworkspace special");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
spdlog::error("Failed to dispatch workspace: {}", e.what());
|
2023-07-15 17:43:22 +00:00
|
|
|
}
|
2023-07-15 21:48:12 +00:00
|
|
|
return false;
|
2023-07-15 17:43:22 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 23:52:01 +00:00
|
|
|
} // namespace waybar::modules::hyprland
|