Merge remote-tracking branch 'origin/master' into pr-4537
# Conflicts: # include/modules/niri/workspaces.hpp # src/modules/niri/workspaces.cpp
This commit is contained in:
@@ -17,19 +17,21 @@
|
||||
#include "giomm/dataoutputstream.h"
|
||||
#include "giomm/unixinputstream.h"
|
||||
#include "giomm/unixoutputstream.h"
|
||||
#include "util/scoped_fd.hpp"
|
||||
|
||||
namespace waybar::modules::niri {
|
||||
|
||||
IPC::IPC() { startIPC(); }
|
||||
|
||||
int IPC::connectToSocket() {
|
||||
const char *socket_path = getenv("NIRI_SOCKET");
|
||||
const char* socket_path = getenv("NIRI_SOCKET");
|
||||
|
||||
if (socket_path == nullptr) {
|
||||
spdlog::warn("Niri is not running, niri IPC will not be available.");
|
||||
return -1;
|
||||
throw std::runtime_error("Niri IPC: NIRI_SOCKET was not set! (Is Niri running?)");
|
||||
}
|
||||
|
||||
struct sockaddr_un addr;
|
||||
int socketfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
util::ScopedFd socketfd(socket(AF_UNIX, SOCK_STREAM, 0));
|
||||
|
||||
if (socketfd == -1) {
|
||||
throw std::runtime_error("socketfd failed");
|
||||
@@ -43,27 +45,19 @@ int IPC::connectToSocket() {
|
||||
|
||||
int l = sizeof(struct sockaddr_un);
|
||||
|
||||
if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
|
||||
close(socketfd);
|
||||
if (connect(socketfd, (struct sockaddr*)&addr, l) == -1) {
|
||||
throw std::runtime_error("unable to connect");
|
||||
}
|
||||
|
||||
return socketfd;
|
||||
return socketfd.release();
|
||||
}
|
||||
|
||||
void IPC::startIPC() {
|
||||
// will start IPC and relay events to parseIPC
|
||||
|
||||
std::thread([&]() {
|
||||
int socketfd;
|
||||
try {
|
||||
socketfd = connectToSocket();
|
||||
} catch (std::exception &e) {
|
||||
spdlog::error("Niri IPC: failed to start, reason: {}", e.what());
|
||||
return;
|
||||
}
|
||||
if (socketfd == -1) return;
|
||||
int socketfd = connectToSocket();
|
||||
|
||||
std::thread([this, socketfd]() {
|
||||
spdlog::info("Niri IPC starting");
|
||||
|
||||
auto unix_istream = Gio::UnixInputStream::create(socketfd, true);
|
||||
@@ -87,7 +81,7 @@ void IPC::startIPC() {
|
||||
|
||||
try {
|
||||
parseIPC(line);
|
||||
} catch (std::exception &e) {
|
||||
} catch (std::exception& e) {
|
||||
spdlog::warn("Failed to parse IPC message: {}, reason: {}", line, e.what());
|
||||
} catch (...) {
|
||||
throw;
|
||||
@@ -98,7 +92,7 @@ void IPC::startIPC() {
|
||||
}).detach();
|
||||
}
|
||||
|
||||
void IPC::parseIPC(const std::string &line) {
|
||||
void IPC::parseIPC(const std::string& line) {
|
||||
const auto ev = parser_.parse(line);
|
||||
const auto members = ev.getMemberNames();
|
||||
if (members.size() != 1) throw std::runtime_error("Event must have a single member");
|
||||
@@ -106,28 +100,28 @@ void IPC::parseIPC(const std::string &line) {
|
||||
{
|
||||
auto lock = lockData();
|
||||
|
||||
if (const auto &payload = ev["WorkspacesChanged"]) {
|
||||
if (const auto& payload = ev["WorkspacesChanged"]) {
|
||||
workspaces_.clear();
|
||||
const auto &values = payload["workspaces"];
|
||||
const auto& values = payload["workspaces"];
|
||||
std::copy(values.begin(), values.end(), std::back_inserter(workspaces_));
|
||||
|
||||
std::sort(workspaces_.begin(), workspaces_.end(), [](const auto &a, const auto &b) {
|
||||
const auto &aOutput = a["output"].asString();
|
||||
const auto &bOutput = b["output"].asString();
|
||||
std::sort(workspaces_.begin(), workspaces_.end(), [](const auto& a, const auto& b) {
|
||||
const auto& aOutput = a["output"].asString();
|
||||
const auto& bOutput = b["output"].asString();
|
||||
const auto aIdx = a["idx"].asUInt();
|
||||
const auto bIdx = b["idx"].asUInt();
|
||||
if (aOutput == bOutput) return aIdx < bIdx;
|
||||
return aOutput < bOutput;
|
||||
});
|
||||
} else if (const auto &payload = ev["WorkspaceActivated"]) {
|
||||
} else if (const auto& payload = ev["WorkspaceActivated"]) {
|
||||
const auto id = payload["id"].asUInt64();
|
||||
const auto focused = payload["focused"].asBool();
|
||||
auto it = std::find_if(workspaces_.begin(), workspaces_.end(),
|
||||
[id](const auto &ws) { return ws["id"].asUInt64() == id; });
|
||||
[id](const auto& ws) { return ws["id"].asUInt64() == id; });
|
||||
if (it != workspaces_.end()) {
|
||||
const auto &ws = *it;
|
||||
const auto &output = ws["output"].asString();
|
||||
for (auto &ws : workspaces_) {
|
||||
const auto& ws = *it;
|
||||
const auto& output = ws["output"].asString();
|
||||
for (auto& ws : workspaces_) {
|
||||
const auto got_activated = (ws["id"].asUInt64() == id);
|
||||
if (ws["output"] == output) ws["is_active"] = got_activated;
|
||||
|
||||
@@ -136,85 +130,97 @@ void IPC::parseIPC(const std::string &line) {
|
||||
} else {
|
||||
spdlog::error("Activated unknown workspace");
|
||||
}
|
||||
} else if (const auto &payload = ev["WorkspaceActiveWindowChanged"]) {
|
||||
} else if (const auto& payload = ev["WorkspaceActiveWindowChanged"]) {
|
||||
const auto workspaceId = payload["workspace_id"].asUInt64();
|
||||
auto it = std::find_if(workspaces_.begin(), workspaces_.end(), [workspaceId](const auto &ws) {
|
||||
auto it = std::find_if(workspaces_.begin(), workspaces_.end(), [workspaceId](const auto& ws) {
|
||||
return ws["id"].asUInt64() == workspaceId;
|
||||
});
|
||||
if (it != workspaces_.end()) {
|
||||
auto &ws = *it;
|
||||
auto& ws = *it;
|
||||
ws["active_window_id"] = payload["active_window_id"];
|
||||
} else {
|
||||
spdlog::error("Active window changed on unknown workspace");
|
||||
}
|
||||
} else if (const auto &payload = ev["WorkspaceUrgencyChanged"]) {
|
||||
} else if (const auto& payload = ev["WorkspaceUrgencyChanged"]) {
|
||||
const auto id = payload["id"].asUInt64();
|
||||
const auto urgent = payload["urgent"].asBool();
|
||||
auto it = std::find_if(workspaces_.begin(), workspaces_.end(),
|
||||
[id](const auto &ws) { return ws["id"].asUInt64() == id; });
|
||||
[id](const auto& ws) { return ws["id"].asUInt64() == id; });
|
||||
if (it != workspaces_.end()) {
|
||||
auto &ws = *it;
|
||||
auto& ws = *it;
|
||||
ws["is_urgent"] = urgent;
|
||||
} else {
|
||||
spdlog::error("Urgency changed for unknown workspace");
|
||||
}
|
||||
} else if (const auto &payload = ev["KeyboardLayoutsChanged"]) {
|
||||
const auto &layouts = payload["keyboard_layouts"];
|
||||
const auto &names = layouts["names"];
|
||||
} else if (const auto& payload = ev["KeyboardLayoutsChanged"]) {
|
||||
const auto& layouts = payload["keyboard_layouts"];
|
||||
const auto& names = layouts["names"];
|
||||
keyboardLayoutCurrent_ = layouts["current_idx"].asUInt();
|
||||
|
||||
keyboardLayoutNames_.clear();
|
||||
for (const auto &fullName : names) keyboardLayoutNames_.push_back(fullName.asString());
|
||||
} else if (const auto &payload = ev["KeyboardLayoutSwitched"]) {
|
||||
for (const auto& fullName : names) keyboardLayoutNames_.push_back(fullName.asString());
|
||||
} else if (const auto& payload = ev["KeyboardLayoutSwitched"]) {
|
||||
keyboardLayoutCurrent_ = payload["idx"].asUInt();
|
||||
} else if (const auto &payload = ev["WindowsChanged"]) {
|
||||
} else if (const auto& payload = ev["WindowsChanged"]) {
|
||||
windows_.clear();
|
||||
const auto &values = payload["windows"];
|
||||
const auto& values = payload["windows"];
|
||||
std::copy(values.begin(), values.end(), std::back_inserter(windows_));
|
||||
} else if (const auto &payload = ev["WindowOpenedOrChanged"]) {
|
||||
const auto &window = payload["window"];
|
||||
} else if (const auto& payload = ev["WindowOpenedOrChanged"]) {
|
||||
const auto& window = payload["window"];
|
||||
const auto id = window["id"].asUInt64();
|
||||
auto it = std::find_if(windows_.begin(), windows_.end(),
|
||||
[id](const auto &win) { return win["id"].asUInt64() == id; });
|
||||
[id](const auto& win) { return win["id"].asUInt64() == id; });
|
||||
if (it == windows_.end()) {
|
||||
windows_.push_back(window);
|
||||
|
||||
if (window["is_focused"].asBool()) {
|
||||
for (auto &win : windows_) {
|
||||
for (auto& win : windows_) {
|
||||
win["is_focused"] = win["id"].asUInt64() == id;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
*it = window;
|
||||
}
|
||||
} else if (const auto &payload = ev["WindowClosed"]) {
|
||||
} else if (const auto& payload = ev["WindowClosed"]) {
|
||||
const auto id = payload["id"].asUInt64();
|
||||
auto it = std::find_if(windows_.begin(), windows_.end(),
|
||||
[id](const auto &win) { return win["id"].asUInt64() == id; });
|
||||
[id](const auto& win) { return win["id"].asUInt64() == id; });
|
||||
if (it != windows_.end()) {
|
||||
windows_.erase(it);
|
||||
} else {
|
||||
spdlog::error("Unknown window closed");
|
||||
}
|
||||
} else if (const auto &payload = ev["WindowFocusChanged"]) {
|
||||
} else if (const auto& payload = ev["WindowFocusChanged"]) {
|
||||
const auto focused = !payload["id"].isNull();
|
||||
const auto id = payload["id"].asUInt64();
|
||||
for (auto &win : windows_) {
|
||||
for (auto& win : windows_) {
|
||||
win["is_focused"] = focused && win["id"].asUInt64() == id;
|
||||
}
|
||||
} else if (const auto &payload = ev["WindowLayoutsChanged"]) {
|
||||
const auto &values = payload["changes"];
|
||||
for (const auto &changed : values) {
|
||||
const auto id = changed[0].asUInt64();
|
||||
const auto &change = changed[1];
|
||||
for (auto &win : windows_) {
|
||||
if (win["id"].asUInt64() == id) {
|
||||
win["layout"] = change;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_lock lock(callbackMutex_);
|
||||
|
||||
for (auto &[eventname, handler] : callbacks_) {
|
||||
for (auto& [eventname, handler] : callbacks_) {
|
||||
if (eventname == members[0]) {
|
||||
handler->onEvent(ev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IPC::registerForIPC(const std::string &ev, EventHandler *ev_handler) {
|
||||
void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
|
||||
if (ev_handler == nullptr) {
|
||||
return;
|
||||
}
|
||||
@@ -223,7 +229,7 @@ void IPC::registerForIPC(const std::string &ev, EventHandler *ev_handler) {
|
||||
callbacks_.emplace_back(ev, ev_handler);
|
||||
}
|
||||
|
||||
void IPC::unregisterForIPC(EventHandler *ev_handler) {
|
||||
void IPC::unregisterForIPC(EventHandler* ev_handler) {
|
||||
if (ev_handler == nullptr) {
|
||||
return;
|
||||
}
|
||||
@@ -231,7 +237,7 @@ void IPC::unregisterForIPC(EventHandler *ev_handler) {
|
||||
std::unique_lock lock(callbackMutex_);
|
||||
|
||||
for (auto it = callbacks_.begin(); it != callbacks_.end();) {
|
||||
auto &[eventname, handler] = *it;
|
||||
auto& [eventname, handler] = *it;
|
||||
if (handler == ev_handler) {
|
||||
it = callbacks_.erase(it);
|
||||
} else {
|
||||
@@ -240,9 +246,8 @@ void IPC::unregisterForIPC(EventHandler *ev_handler) {
|
||||
}
|
||||
}
|
||||
|
||||
Json::Value IPC::send(const Json::Value &request) {
|
||||
int socketfd = connectToSocket();
|
||||
if (socketfd == -1) throw std::runtime_error("Niri is not running");
|
||||
Json::Value IPC::send(const Json::Value& request) {
|
||||
util::ScopedFd socketfd(connectToSocket());
|
||||
|
||||
auto unix_istream = Gio::UnixInputStream::create(socketfd, true);
|
||||
auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
namespace waybar::modules::niri {
|
||||
|
||||
Language::Language(const std::string &id, const Bar &bar, const Json::Value &config)
|
||||
Language::Language(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
: ALabel(config, "language", id, "{}", 0, false), bar_(bar) {
|
||||
label_.hide();
|
||||
|
||||
@@ -32,7 +32,8 @@ void Language::updateFromIPC() {
|
||||
auto ipcLock = gIPC->lockData();
|
||||
|
||||
layouts_.clear();
|
||||
for (const auto &fullName : gIPC->keyboardLayoutNames()) layouts_.push_back(getLayout(fullName));
|
||||
layouts_.reserve(gIPC->keyboardLayoutNames().size());
|
||||
for (const auto& fullName : gIPC->keyboardLayoutNames()) layouts_.push_back(getLayout(fullName));
|
||||
|
||||
current_idx_ = gIPC->keyboardLayoutCurrent();
|
||||
}
|
||||
@@ -51,7 +52,7 @@ void Language::doUpdate() {
|
||||
label_.hide();
|
||||
return;
|
||||
}
|
||||
const auto &layout = layouts_[current_idx_];
|
||||
const auto& layout = layouts_[current_idx_];
|
||||
|
||||
spdlog::debug("niri language update with full name {}", layout.full_name);
|
||||
spdlog::debug("niri language update with short name {}", layout.short_name);
|
||||
@@ -84,7 +85,7 @@ void Language::doUpdate() {
|
||||
|
||||
spdlog::debug("niri language formatted layout name {}", layoutName);
|
||||
|
||||
if (!format_.empty()) {
|
||||
if (!layoutName.empty()) {
|
||||
label_.show();
|
||||
label_.set_markup(layoutName);
|
||||
} else {
|
||||
@@ -97,7 +98,7 @@ void Language::update() {
|
||||
ALabel::update();
|
||||
}
|
||||
|
||||
void Language::onEvent(const Json::Value &ev) {
|
||||
void Language::onEvent(const Json::Value& ev) {
|
||||
if (ev["KeyboardLayoutsChanged"]) {
|
||||
updateFromIPC();
|
||||
} else if (ev["KeyboardLayoutSwitched"]) {
|
||||
@@ -109,11 +110,11 @@ void Language::onEvent(const Json::Value &ev) {
|
||||
dp.emit();
|
||||
}
|
||||
|
||||
Language::Layout Language::getLayout(const std::string &fullName) {
|
||||
auto *const context = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
|
||||
Language::Layout Language::getLayout(const std::string& fullName) {
|
||||
auto* const context = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
|
||||
rxkb_context_parse_default_ruleset(context);
|
||||
|
||||
rxkb_layout *layout = rxkb_layout_first(context);
|
||||
rxkb_layout* layout = rxkb_layout_first(context);
|
||||
while (layout != nullptr) {
|
||||
std::string nameOfLayout = rxkb_layout_get_description(layout);
|
||||
|
||||
@@ -123,10 +124,10 @@ Language::Layout Language::getLayout(const std::string &fullName) {
|
||||
}
|
||||
|
||||
auto name = std::string(rxkb_layout_get_name(layout));
|
||||
const auto *variantPtr = rxkb_layout_get_variant(layout);
|
||||
const auto* variantPtr = rxkb_layout_get_variant(layout);
|
||||
std::string variant = variantPtr == nullptr ? "" : std::string(variantPtr);
|
||||
|
||||
const auto *descriptionPtr = rxkb_layout_get_brief(layout);
|
||||
const auto* descriptionPtr = rxkb_layout_get_brief(layout);
|
||||
std::string description = descriptionPtr == nullptr ? "" : std::string(descriptionPtr);
|
||||
|
||||
Layout info = Layout{nameOfLayout, name, variant, description};
|
||||
|
||||
+28
-12
@@ -9,7 +9,7 @@
|
||||
|
||||
namespace waybar::modules::niri {
|
||||
|
||||
Window::Window(const std::string &id, const Bar &bar, const Json::Value &config)
|
||||
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
: AAppIconLabel(config, "window", id, "{title}", 0, true), bar_(bar) {
|
||||
if (!gIPC) gIPC = std::make_unique<IPC>();
|
||||
|
||||
@@ -17,22 +17,23 @@ Window::Window(const std::string &id, const Bar &bar, const Json::Value &config)
|
||||
gIPC->registerForIPC("WindowOpenedOrChanged", this);
|
||||
gIPC->registerForIPC("WindowClosed", this);
|
||||
gIPC->registerForIPC("WindowFocusChanged", this);
|
||||
gIPC->registerForIPC("WindowLayoutsChanged", this);
|
||||
|
||||
dp.emit();
|
||||
}
|
||||
|
||||
Window::~Window() { gIPC->unregisterForIPC(this); }
|
||||
|
||||
void Window::onEvent(const Json::Value &ev) { dp.emit(); }
|
||||
void Window::onEvent(const Json::Value& ev) { dp.emit(); }
|
||||
|
||||
void Window::doUpdate() {
|
||||
auto ipcLock = gIPC->lockData();
|
||||
|
||||
const auto &windows = gIPC->windows();
|
||||
const auto &workspaces = gIPC->workspaces();
|
||||
const auto& windows = gIPC->windows();
|
||||
const auto& workspaces = gIPC->workspaces();
|
||||
|
||||
const auto separateOutputs = config_["separate-outputs"].asBool();
|
||||
const auto ws_it = std::find_if(workspaces.cbegin(), workspaces.cend(), [&](const auto &ws) {
|
||||
const auto ws_it = std::find_if(workspaces.cbegin(), workspaces.cend(), [&](const auto& ws) {
|
||||
if (separateOutputs) {
|
||||
return ws["is_active"].asBool() && ws["output"].asString() == bar_.output->name;
|
||||
}
|
||||
@@ -46,32 +47,42 @@ void Window::doUpdate() {
|
||||
} else {
|
||||
const auto id = (*ws_it)["active_window_id"].asUInt64();
|
||||
it = std::find_if(windows.cbegin(), windows.cend(),
|
||||
[id](const auto &win) { return win["id"].asUInt64() == id; });
|
||||
[id](const auto& win) { return win["id"].asUInt64() == id; });
|
||||
}
|
||||
|
||||
setClass("empty", ws_it == workspaces.cend() || (*ws_it)["active_window_id"].isNull());
|
||||
|
||||
if (it != windows.cend()) {
|
||||
const auto &window = *it;
|
||||
const auto& window = *it;
|
||||
|
||||
auto max_col = -1;
|
||||
for (const auto &win : windows) {
|
||||
if (win["workspace_id"].asUInt64() != window["workspace_id"].asUInt64()) {
|
||||
continue;
|
||||
}
|
||||
const auto col = win["layout"]["pos_in_scrolling_layout"][0].asInt64();
|
||||
if (col > max_col) max_col = col;
|
||||
}
|
||||
const auto title = window["title"].asString();
|
||||
const auto appId = window["app_id"].asString();
|
||||
const auto col = window["layout"]["pos_in_scrolling_layout"][0].asInt64();
|
||||
const auto sanitizedTitle = waybar::util::sanitize_string(title);
|
||||
const auto sanitizedAppId = waybar::util::sanitize_string(appId);
|
||||
|
||||
label_.show();
|
||||
label_.set_markup(waybar::util::rewriteString(
|
||||
fmt::format(fmt::runtime(format_), fmt::arg("title", sanitizedTitle),
|
||||
fmt::arg("app_id", sanitizedAppId)),
|
||||
fmt::arg("app_id", sanitizedAppId), fmt::arg("col", col),
|
||||
fmt::arg("max_col", max_col)),
|
||||
config_["rewrite"]));
|
||||
|
||||
updateAppIconName(appId, "");
|
||||
|
||||
if (tooltipEnabled()) label_.set_tooltip_text(title);
|
||||
if (tooltipEnabled()) label_.set_tooltip_markup(title);
|
||||
|
||||
const auto id = window["id"].asUInt64();
|
||||
const auto workspaceId = window["workspace_id"].asUInt64();
|
||||
const auto isSolo = std::none_of(windows.cbegin(), windows.cend(), [&](const auto &win) {
|
||||
const auto isSolo = std::none_of(windows.cbegin(), windows.cend(), [&](const auto& win) {
|
||||
return win["id"].asUInt64() != id && win["workspace_id"].asUInt64() == workspaceId;
|
||||
});
|
||||
setClass("solo", isSolo);
|
||||
@@ -82,7 +93,12 @@ void Window::doUpdate() {
|
||||
oldAppId_ = appId;
|
||||
}
|
||||
} else {
|
||||
label_.hide();
|
||||
label_.show();
|
||||
label_.set_markup(waybar::util::rewriteString(
|
||||
fmt::format(fmt::runtime(format_), fmt::arg("title", ""),
|
||||
fmt::arg("app_id", "")),
|
||||
config_["rewrite"]));
|
||||
|
||||
updateAppIconName("", "");
|
||||
setClass("solo", false);
|
||||
if (!oldAppId_.empty()) setClass(oldAppId_, false);
|
||||
@@ -95,7 +111,7 @@ void Window::update() {
|
||||
AAppIconLabel::update();
|
||||
}
|
||||
|
||||
void Window::setClass(const std::string &className, bool enable) {
|
||||
void Window::setClass(const std::string& className, bool enable) {
|
||||
auto styleContext = bar_.window.get_style_context();
|
||||
if (enable) {
|
||||
if (!styleContext->has_class(className)) {
|
||||
|
||||
+164
-32
@@ -4,10 +4,34 @@
|
||||
#include <gtkmm/label.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
namespace waybar::modules::niri {
|
||||
|
||||
Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value &config)
|
||||
Workspaces::Workspaces(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
: AModule(config, "workspaces", id, false, false), bar_(bar), box_(bar.orientation, 0) {
|
||||
const auto config_sort_by_number = config_["sort-by-number"];
|
||||
if (config_sort_by_number.isBool()) {
|
||||
spdlog::warn("[niri/workspaces]: Prefer sort-by-id instead of sort-by-number");
|
||||
sort_by_id_ = config_sort_by_number.asBool();
|
||||
}
|
||||
|
||||
const auto config_sort_by_id = config_["sort-by-id"];
|
||||
if (config_sort_by_id.isBool()) {
|
||||
sort_by_id_ = config_sort_by_id.asBool();
|
||||
}
|
||||
|
||||
const auto config_sort_by_name = config_["sort-by-name"];
|
||||
if (config_sort_by_name.isBool()) {
|
||||
sort_by_name_ = config_sort_by_name.asBool();
|
||||
}
|
||||
|
||||
const auto config_sort_by_coordinates = config_["sort-by-coordinates"];
|
||||
if (config_sort_by_coordinates.isBool()) {
|
||||
sort_by_coordinates_ = config_sort_by_coordinates.asBool();
|
||||
}
|
||||
|
||||
box_.set_name("workspaces");
|
||||
if (!id.empty()) {
|
||||
box_.get_style_context()->add_class(id);
|
||||
@@ -18,13 +42,13 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
|
||||
// Parse ignore-workspaces rules.
|
||||
auto ignoreWorkspaces = config["ignore-workspaces"];
|
||||
if (ignoreWorkspaces.isArray()) {
|
||||
for (const auto &workspaceRegex : ignoreWorkspaces) {
|
||||
for (const auto& workspaceRegex : ignoreWorkspaces) {
|
||||
if (workspaceRegex.isString()) {
|
||||
std::string ruleString = workspaceRegex.asString();
|
||||
try {
|
||||
const std::regex rule{ruleString, std::regex_constants::icase};
|
||||
ignoreWorkspaces_.emplace_back(rule);
|
||||
} catch (const std::regex_error &e) {
|
||||
} catch (const std::regex_error& e) {
|
||||
spdlog::error("Invalid ignore-workspaces rule {}: {}", ruleString, e.what());
|
||||
}
|
||||
} else {
|
||||
@@ -40,21 +64,27 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
|
||||
gIPC->registerForIPC("WorkspaceActiveWindowChanged", this);
|
||||
gIPC->registerForIPC("WorkspaceUrgencyChanged", this);
|
||||
|
||||
if (config["enable-bar-scroll"].asBool()) {
|
||||
auto& window = const_cast<Bar&>(bar_).window;
|
||||
window.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
|
||||
window.signal_scroll_event().connect(sigc::mem_fun(*this, &Workspaces::handleScroll));
|
||||
}
|
||||
|
||||
dp.emit();
|
||||
}
|
||||
|
||||
Workspaces::~Workspaces() { gIPC->unregisterForIPC(this); }
|
||||
|
||||
void Workspaces::onEvent(const Json::Value &ev) { dp.emit(); }
|
||||
void Workspaces::onEvent(const Json::Value& ev) { dp.emit(); }
|
||||
|
||||
void Workspaces::doUpdate() {
|
||||
auto ipcLock = gIPC->lockData();
|
||||
|
||||
const auto alloutputs = config_["all-outputs"].asBool();
|
||||
std::vector<Json::Value> my_workspaces;
|
||||
const auto &workspaces = gIPC->workspaces();
|
||||
const auto& workspaces = gIPC->workspaces();
|
||||
std::copy_if(workspaces.cbegin(), workspaces.cend(), std::back_inserter(my_workspaces),
|
||||
[&](const auto &ws) {
|
||||
[&](const auto& ws) {
|
||||
std::string name;
|
||||
if (ws["name"]) {
|
||||
name = ws["name"].asString();
|
||||
@@ -69,10 +99,12 @@ void Workspaces::doUpdate() {
|
||||
return ws["output"].asString() == bar_.output->name;
|
||||
});
|
||||
|
||||
sortWorkspaces(my_workspaces);
|
||||
|
||||
// Remove buttons for removed workspaces.
|
||||
for (auto it = buttons_.begin(); it != buttons_.end();) {
|
||||
auto ws = std::find_if(my_workspaces.begin(), my_workspaces.end(),
|
||||
[it](const auto &ws) { return ws["id"].asUInt64() == it->first; });
|
||||
[it](const auto& ws) { return ws["id"].asUInt64() == it->first; });
|
||||
if (ws == my_workspaces.end()) {
|
||||
it = buttons_.erase(it);
|
||||
} else {
|
||||
@@ -81,9 +113,9 @@ void Workspaces::doUpdate() {
|
||||
}
|
||||
|
||||
// Add buttons for new workspaces, update existing ones.
|
||||
for (const auto &ws : my_workspaces) {
|
||||
for (const auto& ws : my_workspaces) {
|
||||
auto bit = buttons_.find(ws["id"].asUInt64());
|
||||
auto &button = bit == buttons_.end() ? addButton(ws) : bit->second;
|
||||
auto& button = bit == buttons_.end() ? addButton(ws) : bit->second;
|
||||
auto style_context = button.get_style_context();
|
||||
|
||||
if (ws["is_focused"].asBool())
|
||||
@@ -125,23 +157,30 @@ void Workspaces::doUpdate() {
|
||||
|
||||
if (config_["format"].isString()) {
|
||||
auto format = config_["format"].asString();
|
||||
name = fmt::format(fmt::runtime(format), fmt::arg("icon", getIcon(name, ws)),
|
||||
fmt::arg("value", name), fmt::arg("name", ws["name"].asString()),
|
||||
fmt::arg("index", ws["idx"].asUInt()),
|
||||
fmt::arg("output", ws["output"].asString()));
|
||||
name = fmt::format(
|
||||
fmt::runtime(format), fmt::arg("icon", getIcon(name, ws)), fmt::arg("value", name),
|
||||
fmt::arg("name", ws["name"].asString()), fmt::arg("index", ws["idx"].asUInt()),
|
||||
fmt::arg("output", ws["output"].asString()), fmt::arg("total", my_workspaces.size()));
|
||||
}
|
||||
if (!config_["disable-markup"].asBool()) {
|
||||
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(name);
|
||||
auto* child = gtk_bin_get_child(GTK_BIN(button.gobj()));
|
||||
if (child != nullptr && GTK_IS_LABEL(child))
|
||||
gtk_label_set_markup(GTK_LABEL(child), name.c_str());
|
||||
} else {
|
||||
button.set_label(name);
|
||||
}
|
||||
|
||||
if (config_["current-only"].asBool()) {
|
||||
const auto *property = alloutputs ? "is_focused" : "is_active";
|
||||
const auto* property = alloutputs ? "is_focused" : "is_active";
|
||||
if (ws[property].asBool())
|
||||
button.show();
|
||||
else
|
||||
button.hide();
|
||||
} else if (config_["hide-empty"].asBool()) {
|
||||
if (ws["active_window_id"].isNull() && !ws["is_focused"].asBool())
|
||||
button.hide();
|
||||
else
|
||||
button.show();
|
||||
} else {
|
||||
button.show();
|
||||
}
|
||||
@@ -149,12 +188,11 @@ void Workspaces::doUpdate() {
|
||||
|
||||
// Refresh the button order.
|
||||
for (auto it = my_workspaces.cbegin(); it != my_workspaces.cend(); ++it) {
|
||||
const auto &ws = *it;
|
||||
const auto& ws = *it;
|
||||
|
||||
auto pos = ws["idx"].asUInt() - 1;
|
||||
if (alloutputs) pos = it - my_workspaces.cbegin();
|
||||
const auto pos = static_cast<int>(std::distance(my_workspaces.cbegin(), it));
|
||||
|
||||
auto &button = buttons_[ws["id"].asUInt64()];
|
||||
auto& button = buttons_[ws["id"].asUInt64()];
|
||||
box_.reorder_child(button, pos);
|
||||
}
|
||||
}
|
||||
@@ -164,7 +202,7 @@ void Workspaces::update() {
|
||||
AModule::update();
|
||||
}
|
||||
|
||||
Gtk::Button &Workspaces::addButton(const Json::Value &ws) {
|
||||
Gtk::Button& Workspaces::addButton(const Json::Value& ws) {
|
||||
std::string name;
|
||||
if (ws["name"]) {
|
||||
name = ws["name"].asString();
|
||||
@@ -173,7 +211,7 @@ Gtk::Button &Workspaces::addButton(const Json::Value &ws) {
|
||||
}
|
||||
|
||||
auto pair = buttons_.emplace(ws["id"].asUInt64(), name);
|
||||
auto &&button = pair.first->second;
|
||||
auto&& button = pair.first->second;
|
||||
box_.pack_start(button, false, false, 0);
|
||||
button.set_relief(Gtk::RELIEF_NONE);
|
||||
if (!config_["disable-click"].asBool()) {
|
||||
@@ -182,13 +220,13 @@ Gtk::Button &Workspaces::addButton(const Json::Value &ws) {
|
||||
try {
|
||||
// {"Action":{"FocusWorkspace":{"reference":{"Id":1}}}}
|
||||
Json::Value request(Json::objectValue);
|
||||
auto &action = (request["Action"] = Json::Value(Json::objectValue));
|
||||
auto &focusWorkspace = (action["FocusWorkspace"] = Json::Value(Json::objectValue));
|
||||
auto &reference = (focusWorkspace["reference"] = Json::Value(Json::objectValue));
|
||||
auto& action = (request["Action"] = Json::Value(Json::objectValue));
|
||||
auto& focusWorkspace = (action["FocusWorkspace"] = Json::Value(Json::objectValue));
|
||||
auto& reference = (focusWorkspace["reference"] = Json::Value(Json::objectValue));
|
||||
reference["Id"] = id;
|
||||
|
||||
IPC::send(request);
|
||||
} catch (const std::exception &e) {
|
||||
} catch (const std::exception& e) {
|
||||
spdlog::error("Error switching workspace: {}", e.what());
|
||||
}
|
||||
});
|
||||
@@ -196,20 +234,20 @@ Gtk::Button &Workspaces::addButton(const Json::Value &ws) {
|
||||
return button;
|
||||
}
|
||||
|
||||
std::string Workspaces::getIcon(const std::string &value, const Json::Value &ws) {
|
||||
const auto &icons = config_["format-icons"];
|
||||
std::string Workspaces::getIcon(const std::string& value, const Json::Value& ws) {
|
||||
const auto& icons = config_["format-icons"];
|
||||
if (!icons) return value;
|
||||
|
||||
if (ws["is_urgent"].asBool() && icons["urgent"]) return icons["urgent"].asString();
|
||||
|
||||
if (ws["active_window_id"].isNull() && icons["empty"]) return icons["empty"].asString();
|
||||
if (ws["is_active"].asBool() && icons["active"]) return icons["active"].asString();
|
||||
|
||||
if (ws["is_focused"].asBool() && icons["focused"]) return icons["focused"].asString();
|
||||
|
||||
if (ws["is_active"].asBool() && icons["active"]) return icons["active"].asString();
|
||||
if (ws["active_window_id"].isNull() && icons["empty"]) return icons["empty"].asString();
|
||||
|
||||
if (ws["name"]) {
|
||||
const auto &name = ws["name"].asString();
|
||||
const auto& name = ws["name"].asString();
|
||||
if (icons[name]) return icons[name].asString();
|
||||
}
|
||||
|
||||
@@ -221,8 +259,8 @@ std::string Workspaces::getIcon(const std::string &value, const Json::Value &ws)
|
||||
return value;
|
||||
}
|
||||
|
||||
bool Workspaces::isWorkspaceIgnored(const std::string &name) {
|
||||
for (auto &rule : ignoreWorkspaces_) {
|
||||
bool Workspaces::isWorkspaceIgnored(const std::string& name) {
|
||||
for (auto& rule : ignoreWorkspaces_) {
|
||||
if (std::regex_match(name, rule)) {
|
||||
return true;
|
||||
}
|
||||
@@ -230,4 +268,98 @@ bool Workspaces::isWorkspaceIgnored(const std::string &name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Workspaces::handleScroll(GdkEventScroll* e) {
|
||||
if (gdk_event_get_pointer_emulated((GdkEvent*)e) != 0) {
|
||||
/**
|
||||
* Ignore emulated scroll events on window
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
auto dir = AModule::getScrollDir(e);
|
||||
if (dir == SCROLL_DIR::NONE) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
Json::Value request(Json::objectValue);
|
||||
auto& action = (request["Action"] = Json::Value(Json::objectValue));
|
||||
|
||||
std::string action_name;
|
||||
|
||||
if (dir == SCROLL_DIR::DOWN || dir == SCROLL_DIR::RIGHT) {
|
||||
action_name = "FocusWorkspaceDown";
|
||||
} else if (dir == SCROLL_DIR::UP || dir == SCROLL_DIR::LEFT) {
|
||||
action_name = "FocusWorkspaceUp";
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
action[action_name] = Json::Value(Json::objectValue);
|
||||
|
||||
IPC::send(request);
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
spdlog::error("Workspaces: {}", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Workspaces::sortWorkspaces(std::vector<Json::Value>& workspaces) const {
|
||||
auto get_name = [](const Json::Value& ws) -> std::string {
|
||||
if (ws["name"]) return ws["name"].asString();
|
||||
return std::to_string(ws["idx"].asUInt());
|
||||
};
|
||||
|
||||
auto is_numeric = [](const std::string& value) {
|
||||
return !value.empty() &&
|
||||
std::all_of(value.begin(), value.end(), [](unsigned char c) { return std::isdigit(c); });
|
||||
};
|
||||
|
||||
const bool names_are_numeric =
|
||||
std::all_of(workspaces.begin(), workspaces.end(),
|
||||
[&](const auto& ws) { return is_numeric(get_name(ws)); });
|
||||
|
||||
auto compare_numeric_strings = [](const std::string& a, const std::string& b) {
|
||||
if (a.size() != b.size()) return a.size() < b.size();
|
||||
return a < b;
|
||||
};
|
||||
|
||||
std::sort(workspaces.begin(), workspaces.end(), [&](const auto& a, const auto& b) {
|
||||
if (sort_by_id_) {
|
||||
return a["id"].asUInt64() < b["id"].asUInt64();
|
||||
}
|
||||
|
||||
if (sort_by_name_) {
|
||||
const auto a_name = get_name(a);
|
||||
const auto b_name = get_name(b);
|
||||
if (a_name == b_name) return a["id"].asUInt64() < b["id"].asUInt64();
|
||||
if (names_are_numeric) return compare_numeric_strings(a_name, b_name);
|
||||
return a_name < b_name;
|
||||
}
|
||||
|
||||
if (sort_by_coordinates_) {
|
||||
const auto& a_output = a["output"].asString();
|
||||
const auto& b_output = b["output"].asString();
|
||||
if (a_output == b_output) {
|
||||
const auto a_idx = a["idx"].asUInt();
|
||||
const auto b_idx = b["idx"].asUInt();
|
||||
if (a_idx == b_idx) return a["id"].asUInt64() < b["id"].asUInt64();
|
||||
return a_idx < b_idx;
|
||||
}
|
||||
return a_output < b_output;
|
||||
}
|
||||
|
||||
// Default to sorting by workspace index on each output.
|
||||
const auto& a_output = a["output"].asString();
|
||||
const auto& b_output = b["output"].asString();
|
||||
const auto a_idx = a["idx"].asUInt();
|
||||
const auto b_idx = b["idx"].asUInt();
|
||||
if (a_output == b_output) return a_idx < b_idx;
|
||||
return a_output < b_output;
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace waybar::modules::niri
|
||||
|
||||
Reference in New Issue
Block a user