Skip to content

Commit

Permalink
fix clang-tidy errors in hyprland module
Browse files Browse the repository at this point in the history
  • Loading branch information
zjeffer committed Feb 18, 2024
1 parent 3cd3118 commit e2fe0cc
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 146 deletions.
6 changes: 4 additions & 2 deletions include/modules/hyprland/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class EventHandler {

class IPC {
public:
IPC() { startIPC(); }
IPC();
~IPC() = default;

void registerForIPC(const std::string&, EventHandler*);
void unregisterForIPC(EventHandler*);
Expand All @@ -28,8 +29,9 @@ class IPC {

private:
void startIPC();
void parseIPC(const std::string&);
void parseIPC(const std::string& ev);

bool m_running = false;
std::mutex m_callbackMutex;
util::JsonParser m_parser;
std::list<std::pair<std::string, EventHandler*>> m_callbacks;
Expand Down
14 changes: 7 additions & 7 deletions include/modules/hyprland/language.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace waybar::modules::hyprland {
class Language : public waybar::ALabel, public EventHandler {
public:
Language(const std::string&, const waybar::Bar&, const Json::Value&);
virtual ~Language();
~Language() override;

auto update() -> void override;

private:
void onEvent(const std::string&) override;
void onEvent(const std::string& ev) override;

void initLanguage();

Expand All @@ -30,13 +30,13 @@ class Language : public waybar::ALabel, public EventHandler {
std::string short_description;
};

auto getLayout(const std::string&) -> Layout;
auto getLayout(const std::string& fullName) -> Layout;

std::mutex mutex_;
const Bar& bar_;
util::JsonParser parser_;
std::mutex m_mutex;
const Bar& m_bar;
util::JsonParser m_parser;

Layout layout_;
Layout m_layout;
};

} // namespace waybar::modules::hyprland
12 changes: 6 additions & 6 deletions include/modules/hyprland/submap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ namespace waybar::modules::hyprland {
class Submap : public waybar::ALabel, public EventHandler {
public:
Submap(const std::string&, const waybar::Bar&, const Json::Value&);
virtual ~Submap();
~Submap() override;

auto update() -> void override;

private:
void onEvent(const std::string&) override;
void onEvent(const std::string& ev) override;

std::mutex mutex_;
const Bar& bar_;
util::JsonParser parser_;
std::string submap_;
std::mutex m_mutex;
const Bar& m_bar;
util::JsonParser m_parser;
std::string m_submap;
};

} // namespace waybar::modules::hyprland
24 changes: 12 additions & 12 deletions include/modules/hyprland/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ class Window : public waybar::AAppIconLabel, public EventHandler {
void queryActiveWorkspace();
void setClass(const std::string&, bool enable);

bool separate_outputs;
std::mutex mutex_;
const Bar& bar_;
util::JsonParser parser_;
WindowData window_data_;
Workspace workspace_;
std::string solo_class_;
std::string last_solo_class_;
bool solo_;
bool all_floating_;
bool swallowing_;
bool fullscreen_;
bool m_separateOutputs;
std::mutex m_mutex;
const Bar& m_bar;
util::JsonParser m_parser;
WindowData m_windowData;
Workspace m_workspace;
std::string m_soloClass;
std::string m_lastSoloClass;
bool m_solo;
bool m_allFloating;
bool m_swallowing;
bool m_fullscreen;
};

} // namespace waybar::modules::hyprland
26 changes: 14 additions & 12 deletions src/modules/hyprland/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace waybar::modules::hyprland {

IPC::IPC() { startIPC(); }

void IPC::startIPC() {
// will start IPC and relay events to parseIPC

Expand Down Expand Up @@ -54,19 +56,19 @@ void IPC::startIPC() {
return;
}

auto file = fdopen(socketfd, "r");
auto* file = fdopen(socketfd, "r");

while (true) {
char buffer[1024]; // Hyprland socket2 events are max 1024 bytes
std::array<char, 1024> buffer; // Hyprland socket2 events are max 1024 bytes

auto recievedCharPtr = fgets(buffer, 1024, file);
auto* recievedCharPtr = fgets(buffer.data(), buffer.size(), file);

if (!recievedCharPtr) {
if (recievedCharPtr == nullptr) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}

std::string messageRecieved(buffer);
std::string messageRecieved(buffer.data());
messageRecieved = messageRecieved.substr(0, messageRecieved.find_first_of('\n'));
spdlog::debug("hyprland IPC received {}", messageRecieved);
parseIPC(messageRecieved);
Expand All @@ -88,7 +90,7 @@ void IPC::parseIPC(const std::string& ev) {
}

void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
if (!ev_handler) {
if (ev_handler == nullptr) {
return;
}

Expand All @@ -97,7 +99,7 @@ void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
}

void IPC::unregisterForIPC(EventHandler* ev_handler) {
if (!ev_handler) {
if (ev_handler == nullptr) {
return;
}

Expand Down Expand Up @@ -135,9 +137,9 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
}

// get the instance signature
auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
auto* instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");

if (!instanceSig) {
if (instanceSig == nullptr) {
spdlog::error("Hyprland IPC: HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
return "";
}
Expand Down Expand Up @@ -169,18 +171,18 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
return "";
}

char buffer[8192] = {0};
std::array<char, 8192> buffer = {0};
std::string response;

do {
sizeWritten = read(serverSocket, buffer, 8192);
sizeWritten = read(serverSocket, buffer.data(), 8192);

if (sizeWritten < 0) {
spdlog::error("Hyprland IPC: Couldn't read (5)");
close(serverSocket);
return "";
}
response.append(buffer, sizeWritten);
response.append(buffer.data(), sizeWritten);
} while (sizeWritten > 0);

close(serverSocket);
Expand Down
55 changes: 27 additions & 28 deletions src/modules/hyprland/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
namespace waybar::modules::hyprland {

Language::Language(const std::string& id, const Bar& bar, const Json::Value& config)
: ALabel(config, "language", id, "{}", 0, true), bar_(bar) {
: ALabel(config, "language", id, "{}", 0, true), m_bar(bar) {
modulesReady = true;

if (!gIPC.get()) {
if (!gIPC) {
gIPC = std::make_unique<IPC>();
}

Expand All @@ -30,24 +30,24 @@ Language::Language(const std::string& id, const Bar& bar, const Json::Value& con
Language::~Language() {
gIPC->unregisterForIPC(this);
// wait for possible event handler to finish
std::lock_guard<std::mutex> lg(mutex_);
std::lock_guard<std::mutex> lg(m_mutex);
}

auto Language::update() -> void {
std::lock_guard<std::mutex> lg(mutex_);
std::lock_guard<std::mutex> lg(m_mutex);

std::string layoutName = std::string{};
if (config_.isMember("format-" + layout_.short_description + "-" + layout_.variant)) {
const auto propName = "format-" + layout_.short_description + "-" + layout_.variant;
if (config_.isMember("format-" + m_layout.short_description + "-" + m_layout.variant)) {
const auto propName = "format-" + m_layout.short_description + "-" + m_layout.variant;
layoutName = fmt::format(fmt::runtime(format_), config_[propName].asString());
} else if (config_.isMember("format-" + layout_.short_description)) {
const auto propName = "format-" + layout_.short_description;
} else if (config_.isMember("format-" + m_layout.short_description)) {
const auto propName = "format-" + m_layout.short_description;
layoutName = fmt::format(fmt::runtime(format_), config_[propName].asString());
} else {
layoutName = trim(fmt::format(fmt::runtime(format_), fmt::arg("long", layout_.full_name),
fmt::arg("short", layout_.short_name),
fmt::arg("shortDescription", layout_.short_description),
fmt::arg("variant", layout_.variant)));
layoutName = trim(fmt::format(fmt::runtime(format_), fmt::arg("long", m_layout.full_name),
fmt::arg("short", m_layout.short_name),
fmt::arg("shortDescription", m_layout.short_description),
fmt::arg("variant", m_layout.variant)));
}

if (!format_.empty()) {
Expand All @@ -61,7 +61,7 @@ auto Language::update() -> void {
}

void Language::onEvent(const std::string& ev) {
std::lock_guard<std::mutex> lg(mutex_);
std::lock_guard<std::mutex> lg(m_mutex);
std::string kbName(begin(ev) + ev.find_last_of('>') + 1, begin(ev) + ev.find_first_of(','));
auto layoutName = ev.substr(ev.find_first_of(',') + 1);

Expand All @@ -70,7 +70,7 @@ void Language::onEvent(const std::string& ev) {

layoutName = waybar::util::sanitize_string(layoutName);

layout_ = getLayout(layoutName);
m_layout = getLayout(layoutName);

spdlog::debug("hyprland language onevent with {}", layoutName);

Expand All @@ -91,9 +91,9 @@ void Language::initLanguage() {

searcher = waybar::util::sanitize_string(searcher);

layout_ = getLayout(searcher);
m_layout = getLayout(searcher);

spdlog::debug("hyprland language initLanguage found {}", layout_.full_name);
spdlog::debug("hyprland language initLanguage found {}", m_layout.full_name);

dp.emit();
} catch (std::exception& e) {
Expand All @@ -102,11 +102,11 @@ void Language::initLanguage() {
}

auto Language::getLayout(const std::string& fullName) -> Layout {
const auto CONTEXT = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
rxkb_context_parse_default_ruleset(CONTEXT);
auto* const context = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
rxkb_context_parse_default_ruleset(context);

rxkb_layout* layout = rxkb_layout_first(CONTEXT);
while (layout) {
rxkb_layout* layout = rxkb_layout_first(context);
while (layout != nullptr) {
std::string nameOfLayout = rxkb_layout_get_description(layout);

if (nameOfLayout != fullName) {
Expand All @@ -115,21 +115,20 @@ auto Language::getLayout(const std::string& fullName) -> Layout {
}

auto name = std::string(rxkb_layout_get_name(layout));
auto variant_ = rxkb_layout_get_variant(layout);
std::string variant = variant_ == nullptr ? "" : std::string(variant_);
const auto* variantPtr = rxkb_layout_get_variant(layout);
std::string variant = variantPtr == nullptr ? "" : std::string(variantPtr);

auto short_description_ = rxkb_layout_get_brief(layout);
std::string short_description =
short_description_ == nullptr ? "" : std::string(short_description_);
const auto* descriptionPtr = rxkb_layout_get_brief(layout);
std::string description = descriptionPtr == nullptr ? "" : std::string(descriptionPtr);

Layout info = Layout{nameOfLayout, name, variant, short_description};
Layout info = Layout{nameOfLayout, name, variant, description};

rxkb_context_unref(CONTEXT);
rxkb_context_unref(context);

return info;
}

rxkb_context_unref(CONTEXT);
rxkb_context_unref(context);

spdlog::debug("hyprland language didn't find matching layout");

Expand Down
28 changes: 13 additions & 15 deletions src/modules/hyprland/submap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace waybar::modules::hyprland {

Submap::Submap(const std::string& id, const Bar& bar, const Json::Value& config)
: ALabel(config, "submap", id, "{}", 0, true), bar_(bar) {
: ALabel(config, "submap", id, "{}", 0, true), m_bar(bar) {
modulesReady = true;

if (!gIPC.get()) {
if (!gIPC) {
gIPC = std::make_unique<IPC>();
}

Expand All @@ -25,18 +25,18 @@ Submap::Submap(const std::string& id, const Bar& bar, const Json::Value& config)
Submap::~Submap() {
gIPC->unregisterForIPC(this);
// wait for possible event handler to finish
std::lock_guard<std::mutex> lg(mutex_);
std::lock_guard<std::mutex> lg(m_mutex);
}

auto Submap::update() -> void {
std::lock_guard<std::mutex> lg(mutex_);
std::lock_guard<std::mutex> lg(m_mutex);

if (submap_.empty()) {
if (m_submap.empty()) {
event_box_.hide();
} else {
label_.set_markup(fmt::format(fmt::runtime(format_), submap_));
label_.set_markup(fmt::format(fmt::runtime(format_), m_submap));
if (tooltipEnabled()) {
label_.set_tooltip_text(submap_);
label_.set_tooltip_text(m_submap);
}
event_box_.show();
}
Expand All @@ -45,7 +45,7 @@ auto Submap::update() -> void {
}

void Submap::onEvent(const std::string& ev) {
std::lock_guard<std::mutex> lg(mutex_);
std::lock_guard<std::mutex> lg(m_mutex);

if (ev.find("submap") == std::string::npos) {
return;
Expand All @@ -54,17 +54,15 @@ void Submap::onEvent(const std::string& ev) {
auto submapName = ev.substr(ev.find_last_of('>') + 1);
submapName = waybar::util::sanitize_string(submapName);

if (!submap_.empty()){
label_.get_style_context()->remove_class(submap_);
if (!m_submap.empty()) {
label_.get_style_context()->remove_class(m_submap);
}

submap_ = submapName;
m_submap = submapName;

label_.get_style_context()->add_class(submap_);
label_.get_style_context()->add_class(m_submap);



spdlog::debug("hyprland submap onevent with {}", submap_);
spdlog::debug("hyprland submap onevent with {}", m_submap);

dp.emit();
}
Expand Down
Loading

0 comments on commit e2fe0cc

Please sign in to comment.