chore: lint

This commit is contained in:
Alex 2024-09-13 09:43:19 +02:00
parent 8a89706d03
commit 1142979581
6 changed files with 98 additions and 137 deletions

View File

@ -26,9 +26,9 @@ class IPC {
// The data members are only safe to access while dataMutex_ is locked. // The data members are only safe to access while dataMutex_ is locked.
std::lock_guard<std::mutex> lockData() { return std::lock_guard(dataMutex_); } std::lock_guard<std::mutex> lockData() { return std::lock_guard(dataMutex_); }
const std::vector<Json::Value> &workspaces() const { return workspaces_; } const std::vector<Json::Value>& workspaces() const { return workspaces_; }
const std::vector<Json::Value> &windows() const { return windows_; } const std::vector<Json::Value>& windows() const { return windows_; }
const std::vector<std::string> &keyboardLayoutNames() const { return keyboardLayoutNames_; } const std::vector<std::string>& keyboardLayoutNames() const { return keyboardLayoutNames_; }
unsigned keyboardLayoutCurrent() const { return keyboardLayoutCurrent_; } unsigned keyboardLayoutCurrent() const { return keyboardLayoutCurrent_; }
private: private:

View File

@ -10,7 +10,7 @@ namespace waybar::modules::niri {
class Language : public ALabel, public EventHandler { class Language : public ALabel, public EventHandler {
public: public:
Language(const std::string&, const Bar&, const Json::Value&); Language(const std::string &, const Bar &, const Json::Value &);
~Language() override; ~Language() override;
void update() override; void update() override;

View File

@ -8,19 +8,20 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include "giomm/datainputstream.h"
#include "giomm/dataoutputstream.h"
#include "giomm/unixinputstream.h"
#include "giomm/unixoutputstream.h"
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <thread> #include <thread>
#include "giomm/datainputstream.h"
#include "giomm/dataoutputstream.h"
#include "giomm/unixinputstream.h"
#include "giomm/unixoutputstream.h"
namespace waybar::modules::niri { namespace waybar::modules::niri {
int IPC::connectToSocket() { int IPC::connectToSocket() {
const char* socket_path = getenv("NIRI_SOCKET"); const char *socket_path = getenv("NIRI_SOCKET");
if (socket_path == nullptr) { if (socket_path == nullptr) {
spdlog::warn("Niri is not running, niri IPC will not be available."); spdlog::warn("Niri is not running, niri IPC will not be available.");
@ -42,7 +43,7 @@ int IPC::connectToSocket() {
int l = sizeof(struct sockaddr_un); int l = sizeof(struct sockaddr_un);
if (connect(socketfd, (struct sockaddr*)&addr, l) == -1) { if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
close(socketfd); close(socketfd);
throw std::runtime_error("unable to connect"); throw std::runtime_error("unable to connect");
} }
@ -61,8 +62,7 @@ void IPC::startIPC() {
spdlog::error("Niri IPC: failed to start, reason: {}", e.what()); spdlog::error("Niri IPC: failed to start, reason: {}", e.what());
return; return;
} }
if (socketfd == -1) if (socketfd == -1) return;
return;
spdlog::info("Niri IPC starting"); spdlog::info("Niri IPC starting");
@ -87,7 +87,7 @@ void IPC::startIPC() {
try { try {
parseIPC(line); parseIPC(line);
} catch (std::exception& e) { } catch (std::exception &e) {
spdlog::warn("Failed to parse IPC message: {}, reason: {}", line, e.what()); spdlog::warn("Failed to parse IPC message: {}, reason: {}", line, e.what());
} catch (...) { } catch (...) {
throw; throw;
@ -98,11 +98,10 @@ void IPC::startIPC() {
}).detach(); }).detach();
} }
void IPC::parseIPC(const std::string& line) { void IPC::parseIPC(const std::string &line) {
const auto ev = parser_.parse(line); const auto ev = parser_.parse(line);
const auto members = ev.getMemberNames(); const auto members = ev.getMemberNames();
if (members.size() != 1) if (members.size() != 1) throw std::runtime_error("Event must have a single member");
throw std::runtime_error("Event must have a single member");
{ {
auto lock = lockData(); auto lock = lockData();
@ -112,17 +111,15 @@ void IPC::parseIPC(const std::string& line) {
const auto &values = payload["workspaces"]; const auto &values = payload["workspaces"];
std::copy(values.begin(), values.end(), std::back_inserter(workspaces_)); std::copy(values.begin(), values.end(), std::back_inserter(workspaces_));
std::sort(workspaces_.begin(), workspaces_.end(), std::sort(workspaces_.begin(), workspaces_.end(), [](const auto &a, const auto &b) {
[](const auto &a, const auto &b) {
const auto &aOutput = a["output"].asString(); const auto &aOutput = a["output"].asString();
const auto &bOutput = b["output"].asString(); const auto &bOutput = b["output"].asString();
const auto aIdx = a["idx"].asUInt(); const auto aIdx = a["idx"].asUInt();
const auto bIdx = b["idx"].asUInt(); const auto bIdx = b["idx"].asUInt();
if (aOutput == bOutput) if (aOutput == bOutput) return aIdx < bIdx;
return aIdx < bIdx;
return aOutput < bOutput; 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 id = payload["id"].asUInt64();
const auto focused = payload["focused"].asBool(); const auto focused = payload["focused"].asBool();
auto it = std::find_if(workspaces_.begin(), workspaces_.end(), auto it = std::find_if(workspaces_.begin(), workspaces_.end(),
@ -132,19 +129,18 @@ void IPC::parseIPC(const std::string& line) {
const auto &output = ws["output"].asString(); const auto &output = ws["output"].asString();
for (auto &ws : workspaces_) { for (auto &ws : workspaces_) {
const auto got_activated = (ws["id"].asUInt64() == id); const auto got_activated = (ws["id"].asUInt64() == id);
if (ws["output"] == output) if (ws["output"] == output) ws["is_active"] = got_activated;
ws["is_active"] = got_activated;
if (focused) if (focused) ws["is_focused"] = got_activated;
ws["is_focused"] = got_activated;
} }
} else { } else {
spdlog::error("Activated unknown workspace"); 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(); const auto workspaceId = payload["workspace_id"].asUInt64();
auto it = std::find_if(workspaces_.begin(), workspaces_.end(), auto it = std::find_if(workspaces_.begin(), workspaces_.end(), [workspaceId](const auto &ws) {
[workspaceId](const auto &ws) { return ws["id"].asUInt64() == workspaceId; }); return ws["id"].asUInt64() == workspaceId;
});
if (it != workspaces_.end()) { if (it != workspaces_.end()) {
auto &ws = *it; auto &ws = *it;
ws["active_window_id"] = payload["active_window_id"]; ws["active_window_id"] = payload["active_window_id"];
@ -157,9 +153,8 @@ void IPC::parseIPC(const std::string& line) {
keyboardLayoutCurrent_ = layouts["current_idx"].asUInt(); keyboardLayoutCurrent_ = layouts["current_idx"].asUInt();
keyboardLayoutNames_.clear(); keyboardLayoutNames_.clear();
for (const auto &fullName : names) for (const auto &fullName : names) keyboardLayoutNames_.push_back(fullName.asString());
keyboardLayoutNames_.push_back(fullName.asString()); } else if (const auto &payload = ev["KeyboardLayoutSwitched"]) {
} else if (const auto& payload = ev["KeyboardLayoutSwitched"]) {
keyboardLayoutCurrent_ = payload["idx"].asUInt(); keyboardLayoutCurrent_ = payload["idx"].asUInt();
} else if (const auto &payload = ev["WindowsChanged"]) { } else if (const auto &payload = ev["WindowsChanged"]) {
windows_.clear(); windows_.clear();
@ -201,14 +196,14 @@ void IPC::parseIPC(const std::string& line) {
std::unique_lock lock(callbackMutex_); std::unique_lock lock(callbackMutex_);
for (auto& [eventname, handler] : callbacks_) { for (auto &[eventname, handler] : callbacks_) {
if (eventname == members[0]) { if (eventname == members[0]) {
handler->onEvent(ev); 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) { if (ev_handler == nullptr) {
return; return;
} }
@ -217,7 +212,7 @@ void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
callbacks_.emplace_back(ev, ev_handler); callbacks_.emplace_back(ev, ev_handler);
} }
void IPC::unregisterForIPC(EventHandler* ev_handler) { void IPC::unregisterForIPC(EventHandler *ev_handler) {
if (ev_handler == nullptr) { if (ev_handler == nullptr) {
return; return;
} }
@ -225,7 +220,7 @@ void IPC::unregisterForIPC(EventHandler* ev_handler) {
std::unique_lock lock(callbackMutex_); std::unique_lock lock(callbackMutex_);
for (auto it = callbacks_.begin(); it != callbacks_.end();) { for (auto it = callbacks_.begin(); it != callbacks_.end();) {
auto& [eventname, handler] = *it; auto &[eventname, handler] = *it;
if (handler == ev_handler) { if (handler == ev_handler) {
it = callbacks_.erase(it); it = callbacks_.erase(it);
} else { } else {
@ -234,10 +229,9 @@ void IPC::unregisterForIPC(EventHandler* ev_handler) {
} }
} }
Json::Value IPC::send(const Json::Value& request) { Json::Value IPC::send(const Json::Value &request) {
int socketfd = connectToSocket(); int socketfd = connectToSocket();
if (socketfd == -1) if (socketfd == -1) throw std::runtime_error("Niri is not running");
throw std::runtime_error("Niri is not running");
auto unix_istream = Gio::UnixInputStream::create(socketfd, true); auto unix_istream = Gio::UnixInputStream::create(socketfd, true);
auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false); auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false);
@ -256,8 +250,7 @@ Json::Value IPC::send(const Json::Value& request) {
throw std::runtime_error("error writing to niri socket"); throw std::runtime_error("error writing to niri socket");
std::string line; std::string line;
if (!istream->read_line(line)) if (!istream->read_line(line)) throw std::runtime_error("error reading from niri socket");
throw std::runtime_error("error reading from niri socket");
std::istringstream iss(std::move(line)); std::istringstream iss(std::move(line));
Json::Value response; Json::Value response;
@ -265,4 +258,4 @@ Json::Value IPC::send(const Json::Value& request) {
return response; return response;
} }
} // namespace waybar::modules::hyprland } // namespace waybar::modules::niri

View File

@ -12,8 +12,7 @@ Language::Language(const std::string &id, const Bar &bar, const Json::Value &con
: ALabel(config, "language", id, "{}", 0, true), bar_(bar) { : ALabel(config, "language", id, "{}", 0, true), bar_(bar) {
label_.hide(); label_.hide();
if (!gIPC) if (!gIPC) gIPC = std::make_unique<IPC>();
gIPC = std::make_unique<IPC>();
gIPC->registerForIPC("KeyboardLayoutsChanged", this); gIPC->registerForIPC("KeyboardLayoutsChanged", this);
gIPC->registerForIPC("KeyboardLayoutSwitched", this); gIPC->registerForIPC("KeyboardLayoutSwitched", this);
@ -33,8 +32,7 @@ void Language::updateFromIPC() {
auto ipcLock = gIPC->lockData(); auto ipcLock = gIPC->lockData();
layouts_.clear(); layouts_.clear();
for (const auto &fullName : gIPC->keyboardLayoutNames()) for (const auto &fullName : gIPC->keyboardLayoutNames()) layouts_.push_back(getLayout(fullName));
layouts_.push_back(getLayout(fullName));
current_idx_ = gIPC->keyboardLayoutCurrent(); current_idx_ = gIPC->keyboardLayoutCurrent();
} }
@ -89,7 +87,7 @@ void Language::update() {
ALabel::update(); ALabel::update();
} }
void Language::onEvent(const Json::Value& ev) { void Language::onEvent(const Json::Value &ev) {
if (ev["KeyboardLayoutsChanged"]) { if (ev["KeyboardLayoutsChanged"]) {
updateFromIPC(); updateFromIPC();
} else if (ev["KeyboardLayoutSwitched"]) { } else if (ev["KeyboardLayoutSwitched"]) {
@ -102,10 +100,10 @@ void Language::onEvent(const Json::Value& ev) {
} }
Language::Layout Language::getLayout(const std::string &fullName) { Language::Layout Language::getLayout(const std::string &fullName) {
auto* const context = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES); auto *const context = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
rxkb_context_parse_default_ruleset(context); rxkb_context_parse_default_ruleset(context);
rxkb_layout* layout = rxkb_layout_first(context); rxkb_layout *layout = rxkb_layout_first(context);
while (layout != nullptr) { while (layout != nullptr) {
std::string nameOfLayout = rxkb_layout_get_description(layout); std::string nameOfLayout = rxkb_layout_get_description(layout);
@ -115,10 +113,10 @@ Language::Layout Language::getLayout(const std::string &fullName) {
} }
auto name = std::string(rxkb_layout_get_name(layout)); 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); 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); std::string description = descriptionPtr == nullptr ? "" : std::string(descriptionPtr);
Layout info = Layout{nameOfLayout, name, variant, description}; Layout info = Layout{nameOfLayout, name, variant, description};

View File

@ -11,8 +11,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) { : AAppIconLabel(config, "window", id, "{title}", 0, true), bar_(bar) {
if (!gIPC) if (!gIPC) gIPC = std::make_unique<IPC>();
gIPC = std::make_unique<IPC>();
gIPC->registerForIPC("WindowsChanged", this); gIPC->registerForIPC("WindowsChanged", this);
gIPC->registerForIPC("WindowOpenedOrChanged", this); gIPC->registerForIPC("WindowOpenedOrChanged", this);
@ -22,13 +21,9 @@ Window::Window(const std::string &id, const Bar &bar, const Json::Value &config)
dp.emit(); dp.emit();
} }
Window::~Window() { Window::~Window() { gIPC->unregisterForIPC(this); }
gIPC->unregisterForIPC(this);
}
void Window::onEvent(const Json::Value &ev) { void Window::onEvent(const Json::Value &ev) { dp.emit(); }
dp.emit();
}
void Window::doUpdate() { void Window::doUpdate() {
auto ipcLock = gIPC->lockData(); auto ipcLock = gIPC->lockData();
@ -37,8 +32,7 @@ void Window::doUpdate() {
const auto &workspaces = gIPC->workspaces(); const auto &workspaces = gIPC->workspaces();
const auto separateOutputs = config_["separate-outputs"].asBool(); const auto separateOutputs = config_["separate-outputs"].asBool();
const auto ws_it = std::find_if(workspaces.cbegin(), workspaces.cend(), const auto ws_it = std::find_if(workspaces.cbegin(), workspaces.cend(), [&](const auto &ws) {
[&](const auto &ws) {
if (separateOutputs) { if (separateOutputs) {
return ws["is_active"].asBool() && ws["output"].asString() == bar_.output->name; return ws["is_active"].asBool() && ws["output"].asString() == bar_.output->name;
} }
@ -67,37 +61,31 @@ void Window::doUpdate() {
label_.show(); label_.show();
label_.set_markup(waybar::util::rewriteString( label_.set_markup(waybar::util::rewriteString(
fmt::format(fmt::runtime(format_), fmt::format(fmt::runtime(format_), fmt::arg("title", sanitizedTitle),
fmt::arg("title", sanitizedTitle),
fmt::arg("app_id", sanitizedAppId)), fmt::arg("app_id", sanitizedAppId)),
config_["rewrite"])); config_["rewrite"]));
updateAppIconName(appId, ""); updateAppIconName(appId, "");
if (tooltipEnabled()) if (tooltipEnabled()) label_.set_tooltip_text(title);
label_.set_tooltip_text(title);
const auto id = window["id"].asUInt64(); const auto id = window["id"].asUInt64();
const auto workspaceId = window["workspace_id"].asUInt64(); const auto workspaceId = window["workspace_id"].asUInt64();
const auto isSolo = std::none_of(windows.cbegin(), windows.cend(), const auto isSolo = std::none_of(windows.cbegin(), windows.cend(), [&](const auto &win) {
[&](const auto &win) {
return win["id"].asUInt64() != id && win["workspace_id"].asUInt64() == workspaceId; return win["id"].asUInt64() != id && win["workspace_id"].asUInt64() == workspaceId;
}); });
setClass("solo", isSolo); setClass("solo", isSolo);
if (!appId.empty()) if (!appId.empty()) setClass(appId, isSolo);
setClass(appId, isSolo);
if (oldAppId_ != appId) { if (oldAppId_ != appId) {
if (!oldAppId_.empty()) if (!oldAppId_.empty()) setClass(oldAppId_, false);
setClass(oldAppId_, false);
oldAppId_ = appId; oldAppId_ = appId;
} }
} else { } else {
label_.hide(); label_.hide();
updateAppIconName("", ""); updateAppIconName("", "");
setClass("solo", false); setClass("solo", false);
if (!oldAppId_.empty()) if (!oldAppId_.empty()) setClass(oldAppId_, false);
setClass(oldAppId_, false);
oldAppId_.clear(); oldAppId_.clear();
} }
} }

View File

@ -7,9 +7,7 @@
namespace waybar::modules::niri { 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), : AModule(config, "workspaces", id, false, false), bar_(bar), box_(bar.orientation, 0) {
bar_(bar),
box_(bar.orientation, 0) {
box_.set_name("workspaces"); box_.set_name("workspaces");
if (!id.empty()) { if (!id.empty()) {
box_.get_style_context()->add_class(id); box_.get_style_context()->add_class(id);
@ -17,8 +15,7 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
box_.get_style_context()->add_class(MODULE_CLASS); box_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(box_); event_box_.add(box_);
if (!gIPC) if (!gIPC) gIPC = std::make_unique<IPC>();
gIPC = std::make_unique<IPC>();
gIPC->registerForIPC("WorkspacesChanged", this); gIPC->registerForIPC("WorkspacesChanged", this);
gIPC->registerForIPC("WorkspaceActivated", this); gIPC->registerForIPC("WorkspaceActivated", this);
@ -27,13 +24,9 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
dp.emit(); dp.emit();
} }
Workspaces::~Workspaces() { Workspaces::~Workspaces() { gIPC->unregisterForIPC(this); }
gIPC->unregisterForIPC(this);
}
void Workspaces::onEvent(const Json::Value &ev) { void Workspaces::onEvent(const Json::Value &ev) { dp.emit(); }
dp.emit();
}
void Workspaces::doUpdate() { void Workspaces::doUpdate() {
auto ipcLock = gIPC->lockData(); auto ipcLock = gIPC->lockData();
@ -43,13 +36,12 @@ void Workspaces::doUpdate() {
const auto &workspaces = gIPC->workspaces(); const auto &workspaces = gIPC->workspaces();
std::copy_if(workspaces.cbegin(), workspaces.cend(), std::back_inserter(my_workspaces), std::copy_if(workspaces.cbegin(), workspaces.cend(), std::back_inserter(my_workspaces),
[&](const auto &ws) { [&](const auto &ws) {
if (alloutputs) if (alloutputs) return true;
return true;
return ws["output"].asString() == bar_.output->name; return ws["output"].asString() == bar_.output->name;
}); });
// Remove buttons for removed workspaces. // Remove buttons for removed workspaces.
for (auto it = buttons_.begin(); it != buttons_.end(); ) { for (auto it = buttons_.begin(); it != buttons_.end();) {
auto ws = std::find_if(my_workspaces.begin(), my_workspaces.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()) { if (ws == my_workspaces.end()) {
@ -99,11 +91,8 @@ void Workspaces::doUpdate() {
if (config_["format"].isString()) { if (config_["format"].isString()) {
auto format = config_["format"].asString(); auto format = config_["format"].asString();
name = fmt::format( name = fmt::format(fmt::runtime(format), fmt::arg("icon", getIcon(name, ws)),
fmt::runtime(format), fmt::arg("value", name), fmt::arg("name", ws["name"].asString()),
fmt::arg("icon", getIcon(name, ws)),
fmt::arg("value", name),
fmt::arg("name", ws["name"].asString()),
fmt::arg("index", ws["idx"].asUInt()), fmt::arg("index", ws["idx"].asUInt()),
fmt::arg("output", ws["output"].asString())); fmt::arg("output", ws["output"].asString()));
} }
@ -129,8 +118,7 @@ void Workspaces::doUpdate() {
const auto &ws = *it; const auto &ws = *it;
auto pos = ws["idx"].asUInt() - 1; auto pos = ws["idx"].asUInt() - 1;
if (alloutputs) if (alloutputs) pos = it - my_workspaces.cbegin();
pos = it - my_workspaces.cbegin();
auto &button = buttons_[ws["id"].asUInt64()]; auto &button = buttons_[ws["id"].asUInt64()];
box_.reorder_child(button, pos); box_.reorder_child(button, pos);
@ -176,27 +164,21 @@ Gtk::Button &Workspaces::addButton(const Json::Value &ws) {
std::string Workspaces::getIcon(const std::string &value, const Json::Value &ws) { std::string Workspaces::getIcon(const std::string &value, const Json::Value &ws) {
const auto &icons = config_["format-icons"]; const auto &icons = config_["format-icons"];
if (!icons) if (!icons) return value;
return value;
if (ws["is_focused"].asBool() && icons["focused"]) if (ws["is_focused"].asBool() && icons["focused"]) return icons["focused"].asString();
return icons["focused"].asString();
if (ws["is_active"].asBool() && icons["active"]) if (ws["is_active"].asBool() && icons["active"]) return icons["active"].asString();
return icons["active"].asString();
if (ws["name"]) { if (ws["name"]) {
const auto &name = ws["name"].asString(); const auto &name = ws["name"].asString();
if (icons[name]) if (icons[name]) return icons[name].asString();
return icons[name].asString();
} }
const auto idx = ws["idx"].asString(); const auto idx = ws["idx"].asString();
if (icons[idx]) if (icons[idx]) return icons[idx].asString();
return icons[idx].asString();
if (icons["default"]) if (icons["default"]) return icons["default"].asString();
return icons["default"].asString();
return value; return value;
} }