Waybar/src/modules/wlr/workspace_manager.cpp

398 lines
11 KiB
C++
Raw Normal View History

2020-08-05 20:10:08 +00:00
#include "modules/wlr/workspace_manager.hpp"
#include <gdk/gdkwayland.h>
2020-08-05 20:10:08 +00:00
#include <gtkmm.h>
#include <spdlog/spdlog.h>
#include <algorithm>
#include "modules/wlr/workspace_manager_binding.hpp"
namespace waybar::modules::wlr {
2020-08-07 20:46:47 +00:00
uint32_t WorkspaceGroup::workspace_global_id = 0;
uint32_t WorkspaceManager::group_global_id = 0;
2020-08-06 23:45:08 +00:00
std::map<std::string, std::string> Workspace::icons_map_;
2020-08-05 20:10:08 +00:00
WorkspaceManager::WorkspaceManager(const std::string &id, const waybar::Bar &bar,
const Json::Value &config)
2020-08-05 22:42:57 +00:00
: waybar::AModule(config, "workspaces", id, false, false),
2020-08-05 20:10:08 +00:00
bar_(bar),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0) {
box_.set_name("workspaces");
if (!id.empty()) {
box_.get_style_context()->add_class(id);
}
event_box_.add(box_);
add_registry_listener(this);
if (!workspace_manager_) {
return;
}
}
auto WorkspaceManager::register_manager(wl_registry *registry, uint32_t name, uint32_t version)
-> void {
if (workspace_manager_) {
spdlog::warn("Register workspace manager again although already registered!");
return;
}
if (version != 1) {
spdlog::warn("Using different workspace manager protocol version: {}", version);
}
workspace_manager_ = workspace_manager_bind(registry, name, version, this);
}
auto WorkspaceManager::handle_workspace_group_create(
2021-11-21 12:01:25 +00:00
zext_workspace_group_handle_v1 *workspace_group_handle) -> void {
2020-08-07 20:46:47 +00:00
auto new_id = ++group_global_id;
groups_.push_back(
std::make_unique<WorkspaceGroup>(bar_, box_, config_, *this, workspace_group_handle, new_id));
spdlog::debug("Workspace group {} created", new_id);
2020-08-05 20:10:08 +00:00
}
auto WorkspaceManager::handle_finished() -> void {
2021-11-21 12:01:25 +00:00
zext_workspace_manager_v1_destroy(workspace_manager_);
2020-08-05 20:10:08 +00:00
workspace_manager_ = nullptr;
}
auto WorkspaceManager::handle_done() -> void {
2020-08-06 23:45:08 +00:00
for (auto &group : groups_) {
group->handle_done();
}
2020-08-05 22:42:57 +00:00
dp.emit();
2020-08-05 20:10:08 +00:00
}
auto WorkspaceManager::update() -> void {
for (auto &group : groups_) {
group->update();
}
AModule::update();
}
WorkspaceManager::~WorkspaceManager() {
if (!workspace_manager_) {
return;
}
2021-11-21 12:01:25 +00:00
zext_workspace_manager_v1_destroy(workspace_manager_);
2020-08-05 20:10:08 +00:00
workspace_manager_ = nullptr;
}
2020-08-05 20:10:08 +00:00
auto WorkspaceManager::remove_workspace_group(uint32_t id) -> void {
auto it = std::find_if(groups_.begin(),
groups_.end(),
[id](const std::unique_ptr<WorkspaceGroup> &g) { return g->id() == id; });
if (it == groups_.end()) {
spdlog::warn("Can't find group with id {}", id);
return;
}
groups_.erase(it);
}
2021-11-21 12:01:25 +00:00
auto WorkspaceManager::commit() -> void { zext_workspace_manager_v1_commit(workspace_manager_); }
2020-08-05 20:10:08 +00:00
2020-08-07 20:46:47 +00:00
WorkspaceGroup::WorkspaceGroup(const Bar &bar, Gtk::Box &box, const Json::Value &config,
WorkspaceManager & manager,
2021-11-21 12:01:25 +00:00
zext_workspace_group_handle_v1 *workspace_group_handle, uint32_t id)
2020-08-05 20:10:08 +00:00
: bar_(bar),
2020-08-07 20:46:47 +00:00
box_(box),
2020-08-05 20:10:08 +00:00
config_(config),
workspace_manager_(manager),
workspace_group_handle_(workspace_group_handle),
2020-08-07 20:46:47 +00:00
id_(id) {
2020-08-05 20:10:08 +00:00
add_workspace_group_listener(workspace_group_handle, this);
2020-08-07 21:09:35 +00:00
auto config_sort_by_name = config_["sort-by-name"];
2020-08-07 20:46:47 +00:00
if (config_sort_by_name.isBool()) {
sort_by_name = config_sort_by_name.asBool();
}
2020-08-07 21:09:35 +00:00
auto config_sort_by_coordinates = config_["sort-by-coordinates"];
2020-08-07 20:46:47 +00:00
if (config_sort_by_coordinates.isBool()) {
sort_by_coordinates = config_sort_by_coordinates.asBool();
}
2020-08-05 20:10:08 +00:00
}
2020-08-05 22:42:57 +00:00
auto WorkspaceGroup::add_button(Gtk::Button &button) -> void {
2020-08-07 20:46:47 +00:00
box_.pack_start(button, false, false);
2020-08-05 22:42:57 +00:00
}
2020-08-05 20:10:08 +00:00
2020-08-05 22:42:57 +00:00
WorkspaceGroup::~WorkspaceGroup() {
2020-08-05 20:10:08 +00:00
if (!workspace_group_handle_) {
return;
}
2021-11-21 12:01:25 +00:00
zext_workspace_group_handle_v1_destroy(workspace_group_handle_);
2020-08-05 20:10:08 +00:00
workspace_group_handle_ = nullptr;
}
2021-11-21 12:01:25 +00:00
auto WorkspaceGroup::handle_workspace_create(zext_workspace_handle_v1 *workspace) -> void {
2020-08-07 20:46:47 +00:00
auto new_id = ++workspace_global_id;
workspaces_.push_back(std::make_unique<Workspace>(bar_, config_, *this, workspace, new_id));
spdlog::debug("Workspace {} created", new_id);
2020-08-05 20:10:08 +00:00
}
auto WorkspaceGroup::handle_remove() -> void {
2021-11-21 12:01:25 +00:00
zext_workspace_group_handle_v1_destroy(workspace_group_handle_);
2020-08-05 20:10:08 +00:00
workspace_group_handle_ = nullptr;
workspace_manager_.remove_workspace_group(id_);
}
auto WorkspaceGroup::handle_output_enter(wl_output *output) -> void {
spdlog::debug("Output {} assigned to {} group", (void *)output, id_);
output_ = output;
if (!is_visible()) {
return;
}
2020-08-07 20:46:47 +00:00
for (auto &workspace : workspaces_) {
add_button(workspace->get_button_ref());
2020-08-07 20:46:47 +00:00
}
2020-08-05 20:10:08 +00:00
}
auto WorkspaceGroup::is_visible() const -> bool {
return output_ != nullptr && output_ == gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj());
}
2020-08-05 20:10:08 +00:00
auto WorkspaceGroup::handle_output_leave() -> void {
spdlog::debug("Output {} remove from {} group", (void *)output_, id_);
output_ = nullptr;
if (output_ != gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj())) {
return;
}
2020-08-07 20:46:47 +00:00
for (auto &workspace : workspaces_) {
remove_button(workspace->get_button_ref());
2020-08-07 20:46:47 +00:00
}
2020-08-05 20:10:08 +00:00
}
auto WorkspaceGroup::update() -> void {
for (auto &workspace : workspaces_) {
workspace->update();
}
}
auto WorkspaceGroup::remove_workspace(uint32_t id) -> void {
auto it = std::find_if(workspaces_.begin(),
workspaces_.end(),
[id](const std::unique_ptr<Workspace> &w) { return w->id() == id; });
if (it == workspaces_.end()) {
2020-08-05 22:42:57 +00:00
spdlog::warn("Can't find workspace with id {}", id);
2020-08-05 20:10:08 +00:00
return;
}
workspaces_.erase(it);
}
2020-08-07 20:46:47 +00:00
2020-08-06 23:45:08 +00:00
auto WorkspaceGroup::handle_done() -> void {
need_to_sort = false;
if (!is_visible()) {
return;
}
2020-08-07 20:46:47 +00:00
for (auto &workspace : workspaces_) {
workspace->handle_done();
}
sort_workspaces();
2020-08-07 20:46:47 +00:00
}
2020-08-07 20:46:47 +00:00
auto WorkspaceGroup::commit() -> void { workspace_manager_.commit(); }
auto WorkspaceGroup::sort_workspaces() -> void {
auto cmp = [=](std::unique_ptr<Workspace> &lhs, std::unique_ptr<Workspace> &rhs) {
2020-08-07 21:09:35 +00:00
auto is_name_less = lhs->get_name() < rhs->get_name();
auto is_name_eq = lhs->get_name() == rhs->get_name();
auto is_coords_less = lhs->get_coords() < rhs->get_coords();
if (sort_by_name) {
if (sort_by_coordinates) {
return is_name_eq ? is_coords_less : is_name_less;
}
else {
return is_name_less;
}
2020-08-07 20:46:47 +00:00
}
2020-08-07 21:09:35 +00:00
if (sort_by_coordinates) {
return is_coords_less;
}
return lhs->id() < rhs->id();
2020-08-07 20:46:47 +00:00
};
2020-08-07 21:09:35 +00:00
2020-08-07 20:46:47 +00:00
std::sort(workspaces_.begin(), workspaces_.end(), cmp);
for (size_t i = 0; i < workspaces_.size(); ++i) {
box_.reorder_child(workspaces_[i]->get_button_ref(), i);
2020-08-06 23:45:08 +00:00
}
}
2020-08-05 20:10:08 +00:00
auto WorkspaceGroup::remove_button(Gtk::Button &button) -> void {
box_.remove(button);
}
2020-08-05 20:10:08 +00:00
Workspace::Workspace(const Bar &bar, const Json::Value &config, WorkspaceGroup &workspace_group,
2021-11-21 12:01:25 +00:00
zext_workspace_handle_v1 *workspace, uint32_t id)
2020-08-05 20:10:08 +00:00
: bar_(bar),
config_(config),
workspace_group_(workspace_group),
workspace_handle_(workspace),
2020-08-07 20:46:47 +00:00
id_(id) {
2020-08-05 20:10:08 +00:00
add_workspace_listener(workspace, this);
2020-08-06 23:45:08 +00:00
auto config_format = config["format"];
format_ = config_format.isString() ? config_format.asString() : "{name}";
with_icon_ = format_.find("{icon}") != std::string::npos;
if (with_icon_ && icons_map_.empty()) {
auto format_icons = config["format-icons"];
for (auto &name : format_icons.getMemberNames()) {
icons_map_.emplace(name, format_icons[name].asString());
}
}
/* Handle click events if configured */
if (config_["on-click"].isString() || config_["on-click-middle"].isString()
|| config_["on-click-right"].isString()) {
button_.add_events(Gdk::BUTTON_PRESS_MASK);
button_.signal_button_press_event().connect(
sigc::mem_fun(*this, &Workspace::handle_clicked), false);
}
2020-08-06 23:45:08 +00:00
2020-08-05 22:42:57 +00:00
button_.set_relief(Gtk::RELIEF_NONE);
2020-08-06 23:45:08 +00:00
content_.set_center_widget(label_);
2020-08-05 22:42:57 +00:00
button_.add(content_);
2020-08-07 20:46:47 +00:00
if (!workspace_group.is_visible()) {
return;
}
workspace_group.add_button(button_);
2020-08-05 22:42:57 +00:00
button_.show();
2020-08-06 23:45:08 +00:00
label_.show();
content_.show();
2020-08-05 20:10:08 +00:00
}
Workspace::~Workspace() {
workspace_group_.remove_button(button_);
2020-08-05 20:10:08 +00:00
if (!workspace_handle_) {
return;
}
2021-11-21 12:01:25 +00:00
zext_workspace_handle_v1_destroy(workspace_handle_);
2020-08-05 20:10:08 +00:00
workspace_handle_ = nullptr;
}
2020-08-05 22:42:57 +00:00
auto Workspace::update() -> void {
2020-08-06 23:45:08 +00:00
label_.set_markup(fmt::format(
format_, fmt::arg("name", name_), fmt::arg("icon", with_icon_ ? get_icon() : "")));
2020-08-05 22:42:57 +00:00
}
2020-08-05 20:10:08 +00:00
auto Workspace::handle_state(const std::vector<uint32_t> &state) -> void {
state_ = 0;
for (auto state_entry : state) {
switch (state_entry) {
2021-11-21 12:01:25 +00:00
case ZEXT_WORKSPACE_HANDLE_V1_STATE_ACTIVE:
2020-08-05 20:10:08 +00:00
state_ |= (uint32_t)State::ACTIVE;
break;
case ZEXT_WORKSPACE_HANDLE_V1_STATE_URGENT:
state_ |= (uint32_t)State::URGENT;
break;
case ZEXT_WORKSPACE_HANDLE_V1_STATE_HIDDEN:
state_ |= (uint32_t)State::URGENT;
break;
2020-08-05 20:10:08 +00:00
}
}
}
auto Workspace::handle_remove() -> void {
2021-11-21 12:01:25 +00:00
zext_workspace_handle_v1_destroy(workspace_handle_);
2020-08-05 20:10:08 +00:00
workspace_handle_ = nullptr;
workspace_group_.remove_workspace(id_);
}
2020-08-07 20:46:47 +00:00
auto 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);
}
}
2020-08-06 23:45:08 +00:00
auto Workspace::handle_done() -> void {
spdlog::debug("Workspace {} changed to state {}", id_, state_);
auto style_context = button_.get_style_context();
add_or_remove_class(style_context, is_active(), "active");
add_or_remove_class(style_context, is_urgent(), "urgent");
add_or_remove_class(style_context, is_hidden(), "hidden");
2020-08-06 23:45:08 +00:00
}
2020-08-07 20:46:47 +00:00
2020-08-06 23:45:08 +00:00
auto Workspace::get_icon() -> std::string {
if (is_active()) {
auto active_icon_it = icons_map_.find("active");
if (active_icon_it != icons_map_.end()) {
return active_icon_it->second;
2020-08-06 23:45:08 +00:00
}
}
auto named_icon_it = icons_map_.find(name_);
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 name_;
}
2020-08-07 20:46:47 +00:00
auto Workspace::handle_clicked(GdkEventButton *bt) -> bool {
std::string action;
if (config_["on-click"].isString() && bt->button == 1) {
action = config_["on-click"].asString();
}
else if (config_["on-click-middle"].isString() && bt->button == 2) {
action = config_["on-click-middle"].asString();
}
else if (config_["on-click-right"].isString() && bt->button == 3) {
action = config_["on-click-right"].asString();
}
if (action.empty())
return true;
else if (action == "activate") {
zext_workspace_handle_v1_activate(workspace_handle_);
}
else if (action == "close") {
zext_workspace_handle_v1_remove(workspace_handle_);
}
else {
spdlog::warn("Unknown action {}", action);
}
2020-08-06 23:45:08 +00:00
workspace_group_.commit();
return true;
2020-08-06 23:45:08 +00:00
}
2020-08-07 20:46:47 +00:00
auto Workspace::handle_name(const std::string &name) -> void {
if (name_ != name) {
workspace_group_.set_need_to_sort();
}
2020-08-07 20:46:47 +00:00
name_ = name;
}
auto Workspace::handle_coordinates(const std::vector<uint32_t> &coordinates) -> void {
if (coordinates_ != coordinates) {
workspace_group_.set_need_to_sort();
}
2020-08-07 20:46:47 +00:00
coordinates_ = coordinates;
}
2021-11-21 12:01:25 +00:00
} // namespace waybar::modules::wlr