Merge pull request #2930 from zjeffer/fix/zjeffer/hyprland-clang-tidy

fix clang-tidy errors in hyprland module
pull/2917/head^2
Alexis Rouillard 2024-02-25 17:11:13 +01:00 committed by GitHub
commit 3a33c0b290
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 99 additions and 96 deletions

View File

@ -22,7 +22,7 @@ CheckOptions:
- { key: readability-identifier-naming.FunctionCase, value: camelBack } - { key: readability-identifier-naming.FunctionCase, value: camelBack }
- { key: readability-identifier-naming.VariableCase, value: camelBack } - { key: readability-identifier-naming.VariableCase, value: camelBack }
- { key: readability-identifier-naming.PrivateMemberCase, value: camelBack } - { key: readability-identifier-naming.PrivateMemberCase, value: camelBack }
- { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ } - { key: readability-identifier-naming.PrivateMemberSuffix, value: _ }
- { key: readability-identifier-naming.EnumCase, value: CamelCase } - { key: readability-identifier-naming.EnumCase, value: CamelCase }
- { key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE } - { key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE }
- { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE } - { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE }

View File

@ -20,8 +20,8 @@ class IPC {
public: public:
IPC() { startIPC(); } IPC() { startIPC(); }
void registerForIPC(const std::string&, EventHandler*); void registerForIPC(const std::string& ev, EventHandler* ev_handler);
void unregisterForIPC(EventHandler*); void unregisterForIPC(EventHandler* handler);
static std::string getSocket1Reply(const std::string& rq); static std::string getSocket1Reply(const std::string& rq);
Json::Value getSocket1JsonReply(const std::string& rq); Json::Value getSocket1JsonReply(const std::string& rq);
@ -30,9 +30,9 @@ class IPC {
void startIPC(); void startIPC();
void parseIPC(const std::string&); void parseIPC(const std::string&);
std::mutex m_callbackMutex; std::mutex callbackMutex_;
util::JsonParser m_parser; util::JsonParser parser_;
std::list<std::pair<std::string, EventHandler*>> m_callbacks; std::list<std::pair<std::string, EventHandler*>> callbacks_;
}; };
inline std::unique_ptr<IPC> gIPC; inline std::unique_ptr<IPC> gIPC;

View File

@ -30,7 +30,7 @@ class Language : public waybar::ALabel, public EventHandler {
std::string short_description; std::string short_description;
}; };
auto getLayout(const std::string&) -> Layout; static auto getLayout(const std::string&) -> Layout;
std::mutex mutex_; std::mutex mutex_;
const Bar& bar_; const Bar& bar_;

View File

@ -14,12 +14,12 @@ namespace waybar::modules::hyprland {
class Submap : public waybar::ALabel, public EventHandler { class Submap : public waybar::ALabel, public EventHandler {
public: public:
Submap(const std::string&, const waybar::Bar&, const Json::Value&); Submap(const std::string&, const waybar::Bar&, const Json::Value&);
virtual ~Submap(); ~Submap() override;
auto update() -> void override; auto update() -> void override;
private: private:
void onEvent(const std::string&) override; void onEvent(const std::string& ev) override;
std::mutex mutex_; std::mutex mutex_;
const Bar& bar_; const Bar& bar_;

View File

@ -25,7 +25,7 @@ class Window : public waybar::AAppIconLabel, public EventHandler {
std::string last_window; std::string last_window;
std::string last_window_title; std::string last_window_title;
static auto parse(const Json::Value&) -> Workspace; static auto parse(const Json::Value& value) -> Workspace;
}; };
struct WindowData { struct WindowData {
@ -41,22 +41,22 @@ class Window : public waybar::AAppIconLabel, public EventHandler {
static auto parse(const Json::Value&) -> WindowData; static auto parse(const Json::Value&) -> WindowData;
}; };
auto getActiveWorkspace(const std::string&) -> Workspace; static auto getActiveWorkspace(const std::string&) -> Workspace;
auto getActiveWorkspace() -> Workspace; static auto getActiveWorkspace() -> Workspace;
void onEvent(const std::string&) override; void onEvent(const std::string& ev) override;
void queryActiveWorkspace(); void queryActiveWorkspace();
void setClass(const std::string&, bool enable); void setClass(const std::string&, bool enable);
bool separate_outputs; bool separateOutputs_;
std::mutex mutex_; std::mutex mutex_;
const Bar& bar_; const Bar& bar_;
util::JsonParser parser_; util::JsonParser parser_;
WindowData window_data_; WindowData windowData_;
Workspace workspace_; Workspace workspace_;
std::string solo_class_; std::string soloClass_;
std::string last_solo_class_; std::string lastSoloClass_;
bool solo_; bool solo_;
bool all_floating_; bool allFloating_;
bool swallowing_; bool swallowing_;
bool fullscreen_; bool fullscreen_;
}; };

View File

@ -54,22 +54,22 @@ void IPC::startIPC() {
return; return;
} }
auto file = fdopen(socketfd, "r"); auto* file = fdopen(socketfd, "r");
while (true) { 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* receivedCharPtr = fgets(buffer.data(), buffer.size(), file);
if (!recievedCharPtr) { if (receivedCharPtr == nullptr) {
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue; continue;
} }
std::string messageRecieved(buffer); std::string messageReceived(buffer.data());
messageRecieved = messageRecieved.substr(0, messageRecieved.find_first_of('\n')); messageReceived = messageReceived.substr(0, messageReceived.find_first_of('\n'));
spdlog::debug("hyprland IPC received {}", messageRecieved); spdlog::debug("hyprland IPC received {}", messageReceived);
parseIPC(messageRecieved); parseIPC(messageReceived);
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
@ -78,9 +78,9 @@ void IPC::startIPC() {
void IPC::parseIPC(const std::string& ev) { void IPC::parseIPC(const std::string& ev) {
std::string request = ev.substr(0, ev.find_first_of('>')); std::string request = ev.substr(0, ev.find_first_of('>'));
std::unique_lock lock(m_callbackMutex); std::unique_lock lock(callbackMutex_);
for (auto& [eventname, handler] : m_callbacks) { for (auto& [eventname, handler] : callbacks_) {
if (eventname == request) { if (eventname == request) {
handler->onEvent(ev); handler->onEvent(ev);
} }
@ -88,25 +88,25 @@ void IPC::parseIPC(const std::string& ev) {
} }
void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) { void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
if (!ev_handler) { if (ev_handler == nullptr) {
return; return;
} }
std::unique_lock lock(m_callbackMutex); std::unique_lock lock(callbackMutex_);
m_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) { if (ev_handler == nullptr) {
return; return;
} }
std::unique_lock lock(m_callbackMutex); std::unique_lock lock(callbackMutex_);
for (auto it = m_callbacks.begin(); it != m_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) {
m_callbacks.erase(it++); callbacks_.erase(it++);
} else { } else {
++it; ++it;
} }
@ -135,9 +135,9 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
} }
// get the instance signature // 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?)"); spdlog::error("Hyprland IPC: HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
return ""; return "";
} }
@ -169,18 +169,18 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
return ""; return "";
} }
char buffer[8192] = {0}; std::array<char, 8192> buffer = {0};
std::string response; std::string response;
do { do {
sizeWritten = read(serverSocket, buffer, 8192); sizeWritten = read(serverSocket, buffer.data(), 8192);
if (sizeWritten < 0) { if (sizeWritten < 0) {
spdlog::error("Hyprland IPC: Couldn't read (5)"); spdlog::error("Hyprland IPC: Couldn't read (5)");
close(serverSocket); close(serverSocket);
return ""; return "";
} }
response.append(buffer, sizeWritten); response.append(buffer.data(), sizeWritten);
} while (sizeWritten > 0); } while (sizeWritten > 0);
close(serverSocket); close(serverSocket);
@ -188,7 +188,7 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
} }
Json::Value IPC::getSocket1JsonReply(const std::string& rq) { Json::Value IPC::getSocket1JsonReply(const std::string& rq) {
return m_parser.parse(getSocket1Reply("j/" + rq)); return parser_.parse(getSocket1Reply("j/" + rq));
} }
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland

View File

@ -13,7 +13,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) {
modulesReady = true; modulesReady = true;
if (!gIPC.get()) { if (!gIPC) {
gIPC = std::make_unique<IPC>(); gIPC = std::make_unique<IPC>();
} }
@ -102,11 +102,11 @@ void Language::initLanguage() {
} }
auto Language::getLayout(const std::string& fullName) -> Layout { auto Language::getLayout(const std::string& fullName) -> Layout {
const auto 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) { while (layout != nullptr) {
std::string nameOfLayout = rxkb_layout_get_description(layout); std::string nameOfLayout = rxkb_layout_get_description(layout);
if (nameOfLayout != fullName) { if (nameOfLayout != fullName) {
@ -115,21 +115,20 @@ auto Language::getLayout(const std::string& fullName) -> Layout {
} }
auto name = std::string(rxkb_layout_get_name(layout)); auto name = std::string(rxkb_layout_get_name(layout));
auto variant_ = rxkb_layout_get_variant(layout); const auto* variantPtr = rxkb_layout_get_variant(layout);
std::string variant = variant_ == nullptr ? "" : std::string(variant_); std::string variant = variantPtr == nullptr ? "" : std::string(variantPtr);
auto short_description_ = rxkb_layout_get_brief(layout); const auto* descriptionPtr = rxkb_layout_get_brief(layout);
std::string short_description = std::string description = descriptionPtr == nullptr ? "" : std::string(descriptionPtr);
short_description_ == nullptr ? "" : std::string(short_description_);
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; return info;
} }
rxkb_context_unref(CONTEXT); rxkb_context_unref(context);
spdlog::debug("hyprland language didn't find matching layout"); spdlog::debug("hyprland language didn't find matching layout");

View File

@ -10,7 +10,7 @@ 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), bar_(bar) {
modulesReady = true; modulesReady = true;
if (!gIPC.get()) { if (!gIPC) {
gIPC = std::make_unique<IPC>(); gIPC = std::make_unique<IPC>();
} }

View File

@ -17,9 +17,9 @@ namespace waybar::modules::hyprland {
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) {
modulesReady = true; modulesReady = true;
separate_outputs = config["separate-outputs"].asBool(); separateOutputs_ = config["separate-outputs"].asBool();
if (!gIPC.get()) { if (!gIPC) {
gIPC = std::make_unique<IPC>(); gIPC = std::make_unique<IPC>();
} }
@ -45,18 +45,18 @@ auto Window::update() -> void {
// fix ampersands // fix ampersands
std::lock_guard<std::mutex> lg(mutex_); std::lock_guard<std::mutex> lg(mutex_);
std::string window_name = waybar::util::sanitize_string(workspace_.last_window_title); std::string windowName = waybar::util::sanitize_string(workspace_.last_window_title);
std::string window_address = workspace_.last_window; std::string windowAddress = workspace_.last_window;
window_data_.title = window_name; windowData_.title = windowName;
if (!format_.empty()) { if (!format_.empty()) {
label_.show(); label_.show();
label_.set_markup(waybar::util::rewriteString( label_.set_markup(waybar::util::rewriteString(
fmt::format(fmt::runtime(format_), fmt::arg("title", window_name), fmt::format(fmt::runtime(format_), fmt::arg("title", windowName),
fmt::arg("initialTitle", window_data_.initial_title), fmt::arg("initialTitle", windowData_.initial_title),
fmt::arg("class", window_data_.class_name), fmt::arg("class", windowData_.class_name),
fmt::arg("initialClass", window_data_.initial_class_name)), fmt::arg("initialClass", windowData_.initial_class_name)),
config_["rewrite"])); config_["rewrite"]));
} else { } else {
label_.hide(); label_.hide();
@ -64,22 +64,22 @@ auto Window::update() -> void {
setClass("empty", workspace_.windows == 0); setClass("empty", workspace_.windows == 0);
setClass("solo", solo_); setClass("solo", solo_);
setClass("floating", all_floating_); setClass("floating", allFloating_);
setClass("swallowing", swallowing_); setClass("swallowing", swallowing_);
setClass("fullscreen", fullscreen_); setClass("fullscreen", fullscreen_);
if (!last_solo_class_.empty() && solo_class_ != last_solo_class_) { if (!lastSoloClass_.empty() && soloClass_ != lastSoloClass_) {
if (bar_.window.get_style_context()->has_class(last_solo_class_)) { if (bar_.window.get_style_context()->has_class(lastSoloClass_)) {
bar_.window.get_style_context()->remove_class(last_solo_class_); bar_.window.get_style_context()->remove_class(lastSoloClass_);
spdlog::trace("Removing solo class: {}", last_solo_class_); spdlog::trace("Removing solo class: {}", lastSoloClass_);
} }
} }
if (!solo_class_.empty() && solo_class_ != last_solo_class_) { if (!soloClass_.empty() && soloClass_ != lastSoloClass_) {
bar_.window.get_style_context()->add_class(solo_class_); bar_.window.get_style_context()->add_class(soloClass_);
spdlog::trace("Adding solo class: {}", solo_class_); spdlog::trace("Adding solo class: {}", soloClass_);
} }
last_solo_class_ = solo_class_; lastSoloClass_ = soloClass_;
AAppIconLabel::update(); AAppIconLabel::update();
} }
@ -113,8 +113,12 @@ auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
} }
auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace { auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace {
return Workspace{value["id"].asInt(), value["windows"].asInt(), value["lastwindow"].asString(), return Workspace{
value["lastwindowtitle"].asString()}; value["id"].asInt(),
value["windows"].asInt(),
value["lastwindow"].asString(),
value["lastwindowtitle"].asString(),
};
} }
auto Window::WindowData::parse(const Json::Value& value) -> Window::WindowData { auto Window::WindowData::parse(const Json::Value& value) -> Window::WindowData {
@ -127,7 +131,7 @@ auto Window::WindowData::parse(const Json::Value& value) -> Window::WindowData {
void Window::queryActiveWorkspace() { void Window::queryActiveWorkspace() {
std::lock_guard<std::mutex> lg(mutex_); std::lock_guard<std::mutex> lg(mutex_);
if (separate_outputs) { if (separateOutputs_) {
workspace_ = getActiveWorkspace(this->bar_.output->name); workspace_ = getActiveWorkspace(this->bar_.output->name);
} else { } else {
workspace_ = getActiveWorkspace(); workspace_ = getActiveWorkspace();
@ -136,33 +140,33 @@ void Window::queryActiveWorkspace() {
if (workspace_.windows > 0) { if (workspace_.windows > 0) {
const auto clients = gIPC->getSocket1JsonReply("clients"); const auto clients = gIPC->getSocket1JsonReply("clients");
assert(clients.isArray()); assert(clients.isArray());
auto active_window = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) { auto activeWindow = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) {
return window["address"] == workspace_.last_window; return window["address"] == workspace_.last_window;
}); });
if (active_window == std::end(clients)) { if (activeWindow == std::end(clients)) {
return; return;
} }
window_data_ = WindowData::parse(*active_window); windowData_ = WindowData::parse(*activeWindow);
updateAppIconName(window_data_.class_name, window_data_.initial_class_name); updateAppIconName(windowData_.class_name, windowData_.initial_class_name);
std::vector<Json::Value> workspace_windows; std::vector<Json::Value> workspaceWindows;
std::copy_if(clients.begin(), clients.end(), std::back_inserter(workspace_windows), std::copy_if(clients.begin(), clients.end(), std::back_inserter(workspaceWindows),
[&](Json::Value window) { [&](Json::Value window) {
return window["workspace"]["id"] == workspace_.id && window["mapped"].asBool(); return window["workspace"]["id"] == workspace_.id && window["mapped"].asBool();
}); });
swallowing_ = swallowing_ =
std::any_of(workspace_windows.begin(), workspace_windows.end(), [&](Json::Value window) { std::any_of(workspaceWindows.begin(), workspaceWindows.end(), [&](Json::Value window) {
return !window["swallowing"].isNull() && window["swallowing"].asString() != "0x0"; return !window["swallowing"].isNull() && window["swallowing"].asString() != "0x0";
}); });
std::vector<Json::Value> visible_windows; std::vector<Json::Value> visibleWindows;
std::copy_if(workspace_windows.begin(), workspace_windows.end(), std::copy_if(workspaceWindows.begin(), workspaceWindows.end(),
std::back_inserter(visible_windows), std::back_inserter(visibleWindows),
[&](Json::Value window) { return !window["hidden"].asBool(); }); [&](Json::Value window) { return !window["hidden"].asBool(); });
solo_ = 1 == std::count_if(visible_windows.begin(), visible_windows.end(), solo_ = 1 == std::count_if(visibleWindows.begin(), visibleWindows.end(),
[&](Json::Value window) { return !window["floating"].asBool(); }); [&](Json::Value window) { return !window["floating"].asBool(); });
all_floating_ = std::all_of(visible_windows.begin(), visible_windows.end(), allFloating_ = std::all_of(visibleWindows.begin(), visibleWindows.end(),
[&](Json::Value window) { return window["floating"].asBool(); }); [&](Json::Value window) { return window["floating"].asBool(); });
fullscreen_ = window_data_.fullscreen; fullscreen_ = windowData_.fullscreen;
// Fullscreen windows look like they are solo // Fullscreen windows look like they are solo
if (fullscreen_) { if (fullscreen_) {
@ -170,23 +174,23 @@ void Window::queryActiveWorkspace() {
} }
// Grouped windows have a tab bar and therefore don't look fullscreen or solo // Grouped windows have a tab bar and therefore don't look fullscreen or solo
if (window_data_.grouped) { if (windowData_.grouped) {
fullscreen_ = false; fullscreen_ = false;
solo_ = false; solo_ = false;
} }
if (solo_) { if (solo_) {
solo_class_ = window_data_.class_name; soloClass_ = windowData_.class_name;
} else { } else {
solo_class_ = ""; soloClass_ = "";
} }
} else { } else {
window_data_ = WindowData{}; windowData_ = WindowData{};
all_floating_ = false; allFloating_ = false;
swallowing_ = false; swallowing_ = false;
fullscreen_ = false; fullscreen_ = false;
solo_ = false; solo_ = false;
solo_class_ = ""; soloClass_ = "";
} }
} }