Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexays committed Sep 13, 2024
1 parent 8a89706 commit 1142979
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 137 deletions.
6 changes: 3 additions & 3 deletions include/modules/niri/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class IPC {

// The data members are only safe to access while dataMutex_ is locked.
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> &windows() const { return windows_; }
const std::vector<std::string> &keyboardLayoutNames() const { return keyboardLayoutNames_; }
const std::vector<Json::Value>& workspaces() const { return workspaces_; }
const std::vector<Json::Value>& windows() const { return windows_; }
const std::vector<std::string>& keyboardLayoutNames() const { return keyboardLayoutNames_; }
unsigned keyboardLayoutCurrent() const { return keyboardLayoutCurrent_; }

private:
Expand Down
2 changes: 1 addition & 1 deletion include/modules/niri/language.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace waybar::modules::niri {

class Language : public ALabel, public EventHandler {
public:
Language(const std::string&, const Bar&, const Json::Value&);
Language(const std::string &, const Bar &, const Json::Value &);
~Language() override;
void update() override;

Expand Down
113 changes: 53 additions & 60 deletions src/modules/niri/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,47 @@
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "giomm/datainputstream.h"
#include "giomm/dataoutputstream.h"
#include "giomm/unixinputstream.h"
#include "giomm/unixoutputstream.h"

#include <iostream>
#include <string>
#include <thread>

#include "giomm/datainputstream.h"
#include "giomm/dataoutputstream.h"
#include "giomm/unixinputstream.h"
#include "giomm/unixoutputstream.h"

namespace waybar::modules::niri {

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;
}
if (socket_path == nullptr) {
spdlog::warn("Niri is not running, niri IPC will not be available.");
return -1;
}

struct sockaddr_un addr;
int socketfd = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un addr;
int socketfd = socket(AF_UNIX, SOCK_STREAM, 0);

if (socketfd == -1) {
throw std::runtime_error("socketfd failed");
}
if (socketfd == -1) {
throw std::runtime_error("socketfd failed");
}

addr.sun_family = AF_UNIX;
addr.sun_family = AF_UNIX;

strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);

addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;

int l = sizeof(struct sockaddr_un);
int l = sizeof(struct sockaddr_un);

if (connect(socketfd, (struct sockaddr*)&addr, l) == -1) {
close(socketfd);
throw std::runtime_error("unable to connect");
}
if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
close(socketfd);
throw std::runtime_error("unable to connect");
}

return socketfd;
return socketfd;
}

void IPC::startIPC() {
Expand All @@ -61,8 +62,7 @@ void IPC::startIPC() {
spdlog::error("Niri IPC: failed to start, reason: {}", e.what());
return;
}
if (socketfd == -1)
return;
if (socketfd == -1) return;

spdlog::info("Niri IPC starting");

Expand All @@ -87,7 +87,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;
Expand All @@ -98,11 +98,10 @@ 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");
if (members.size() != 1) throw std::runtime_error("Event must have a single member");

{
auto lock = lockData();
Expand All @@ -112,17 +111,15 @@ void IPC::parseIPC(const std::string& line) {
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();
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"]) {
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"]) {
const auto id = payload["id"].asUInt64();
const auto focused = payload["focused"].asBool();
auto it = std::find_if(workspaces_.begin(), workspaces_.end(),
Expand All @@ -132,19 +129,18 @@ void IPC::parseIPC(const std::string& line) {
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;
if (ws["output"] == output) ws["is_active"] = got_activated;

if (focused)
ws["is_focused"] = got_activated;
if (focused) ws["is_focused"] = got_activated;
}
} 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) { return ws["id"].asUInt64() == workspaceId; });
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;
ws["active_window_id"] = payload["active_window_id"];
Expand All @@ -157,9 +153,8 @@ void IPC::parseIPC(const std::string& line) {
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"]) {
windows_.clear();
Expand Down Expand Up @@ -201,14 +196,14 @@ void IPC::parseIPC(const std::string& line) {

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;
}
Expand All @@ -217,15 +212,15 @@ 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;
}

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 {
Expand All @@ -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();
if (socketfd == -1)
throw std::runtime_error("Niri is not running");
if (socketfd == -1) throw std::runtime_error("Niri is not running");

auto unix_istream = Gio::UnixInputStream::create(socketfd, true);
auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false);
Expand All @@ -256,13 +250,12 @@ Json::Value IPC::send(const Json::Value& request) {
throw std::runtime_error("error writing to niri socket");

std::string line;
if (!istream->read_line(line))
throw std::runtime_error("error reading from niri socket");
if (!istream->read_line(line)) throw std::runtime_error("error reading from niri socket");

std::istringstream iss(std::move(line));
Json::Value response;
iss >> response;
return response;
}

} // namespace waybar::modules::hyprland
} // namespace waybar::modules::niri
16 changes: 7 additions & 9 deletions src/modules/niri/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
label_.hide();

if (!gIPC)
gIPC = std::make_unique<IPC>();
if (!gIPC) gIPC = std::make_unique<IPC>();

gIPC->registerForIPC("KeyboardLayoutsChanged", this);
gIPC->registerForIPC("KeyboardLayoutSwitched", this);
Expand All @@ -33,8 +32,7 @@ void Language::updateFromIPC() {
auto ipcLock = gIPC->lockData();

layouts_.clear();
for (const auto &fullName : gIPC->keyboardLayoutNames())
layouts_.push_back(getLayout(fullName));
for (const auto &fullName : gIPC->keyboardLayoutNames()) layouts_.push_back(getLayout(fullName));

current_idx_ = gIPC->keyboardLayoutCurrent();
}
Expand Down Expand Up @@ -89,7 +87,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"]) {
Expand All @@ -102,10 +100,10 @@ void Language::onEvent(const Json::Value& ev) {
}

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_layout* layout = rxkb_layout_first(context);
rxkb_layout *layout = rxkb_layout_first(context);
while (layout != nullptr) {
std::string nameOfLayout = rxkb_layout_get_description(layout);

Expand All @@ -115,10 +113,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};
Expand Down
Loading

0 comments on commit 1142979

Please sign in to comment.