Improvements for Hyprland backend

1. Fix warnings reported by clang tidy
2. Use unique lock instead of manully lock/unlock on mutex.
   The RAII style locking makes sure mutex is unlocked when exceptions are thrown
pull/2813/head
yangyingchao 2024-01-06 12:57:27 +08:00
parent a34e3ccc86
commit bdd7271da9
2 changed files with 35 additions and 54 deletions

View File

@ -1,11 +1,9 @@
#pragma once #pragma once
#include <functional>
#include <list> #include <list>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread>
#include <utility> #include <utility>
#include "util/json.hpp" #include "util/json.hpp"
@ -25,16 +23,16 @@ class IPC {
void registerForIPC(const std::string&, EventHandler*); void registerForIPC(const std::string&, EventHandler*);
void unregisterForIPC(EventHandler*); void unregisterForIPC(EventHandler*);
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);
private: private:
void startIPC(); void startIPC();
void parseIPC(const std::string&); void parseIPC(const std::string&);
std::mutex callbackMutex; std::mutex m_callbackMutex;
util::JsonParser parser_; util::JsonParser m_parser;
std::list<std::pair<std::string, EventHandler*>> callbacks; std::list<std::pair<std::string, EventHandler*>> m_callbacks;
}; };
inline std::unique_ptr<IPC> gIPC; inline std::unique_ptr<IPC> gIPC;

View File

@ -1,21 +1,16 @@
#include "modules/hyprland/backend.hpp" #include "modules/hyprland/backend.hpp"
#include <ctype.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <fstream>
#include <iostream>
#include <string> #include <string>
#include <thread>
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
@ -24,9 +19,9 @@ void IPC::startIPC() {
std::thread([&]() { std::thread([&]() {
// check for hyprland // check for hyprland
const char* HIS = getenv("HYPRLAND_INSTANCE_SIGNATURE"); const char* his = getenv("HYPRLAND_INSTANCE_SIGNATURE");
if (!HIS) { if (his == nullptr) {
spdlog::warn("Hyprland is not running, Hyprland IPC will not be available."); spdlog::warn("Hyprland is not running, Hyprland IPC will not be available.");
return; return;
} }
@ -45,8 +40,8 @@ void IPC::startIPC() {
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
// socket path // socket path, specified by EventManager of Hyprland
std::string socketPath = "/tmp/hypr/" + std::string(HIS) + "/.socket2.sock"; std::string socketPath = "/tmp/hypr/" + std::string(his) + "/.socket2.sock";
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1); strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
@ -61,10 +56,9 @@ void IPC::startIPC() {
auto file = fdopen(socketfd, "r"); auto file = fdopen(socketfd, "r");
while (1) { while (true) {
// read
char buffer[1024]; // Hyprland socket2 events are max 1024 bytes char buffer[1024]; // Hyprland socket2 events are max 1024 bytes
auto recievedCharPtr = fgets(buffer, 1024, file); auto recievedCharPtr = fgets(buffer, 1024, file);
if (!recievedCharPtr) { if (!recievedCharPtr) {
@ -72,28 +66,21 @@ void IPC::startIPC() {
continue; continue;
} }
callbackMutex.lock();
std::string messageRecieved(buffer); std::string messageRecieved(buffer);
messageRecieved = messageRecieved.substr(0, messageRecieved.find_first_of('\n')); messageRecieved = messageRecieved.substr(0, messageRecieved.find_first_of('\n'));
spdlog::debug("hyprland IPC received {}", messageRecieved); spdlog::debug("hyprland IPC received {}", messageRecieved);
parseIPC(messageRecieved); parseIPC(messageRecieved);
callbackMutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
}).detach(); }).detach();
} }
void IPC::parseIPC(const std::string& ev) { void IPC::parseIPC(const std::string& ev) {
// todo
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);
for (auto& [eventname, handler] : callbacks) { for (auto& [eventname, handler] : m_callbacks) {
if (eventname == request) { if (eventname == request) {
handler->onEvent(ev); handler->onEvent(ev);
} }
@ -104,11 +91,9 @@ void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
if (!ev_handler) { if (!ev_handler) {
return; return;
} }
callbackMutex.lock();
callbacks.emplace_back(std::make_pair(ev, ev_handler)); std::unique_lock lock(m_callbackMutex);
m_callbacks.emplace_back(ev, ev_handler);
callbackMutex.unlock();
} }
void IPC::unregisterForIPC(EventHandler* ev_handler) { void IPC::unregisterForIPC(EventHandler* ev_handler) {
@ -116,37 +101,35 @@ void IPC::unregisterForIPC(EventHandler* ev_handler) {
return; return;
} }
callbackMutex.lock(); std::unique_lock lock(m_callbackMutex);
for (auto it = callbacks.begin(); it != callbacks.end();) { for (auto it = m_callbacks.begin(); it != m_callbacks.end();) {
auto it_current = it; auto& [eventname, handler] = *it;
it++;
auto& [eventname, handler] = *it_current;
if (handler == ev_handler) { if (handler == ev_handler) {
callbacks.erase(it_current); m_callbacks.erase(it++);
} else {
++it;
} }
} }
callbackMutex.unlock();
} }
std::string IPC::getSocket1Reply(const std::string& rq) { std::string IPC::getSocket1Reply(const std::string& rq) {
// basically hyprctl // basically hyprctl
struct addrinfo ai_hints; struct addrinfo aiHints;
struct addrinfo* ai_res = NULL; struct addrinfo* aiRes = nullptr;
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0); const auto serverSocket = socket(AF_UNIX, SOCK_STREAM, 0);
if (SERVERSOCKET < 0) { if (serverSocket < 0) {
spdlog::error("Hyprland IPC: Couldn't open a socket (1)"); spdlog::error("Hyprland IPC: Couldn't open a socket (1)");
return ""; return "";
} }
memset(&ai_hints, 0, sizeof(struct addrinfo)); memset(&aiHints, 0, sizeof(struct addrinfo));
ai_hints.ai_family = AF_UNSPEC; aiHints.ai_family = AF_UNSPEC;
ai_hints.ai_socktype = SOCK_STREAM; aiHints.ai_socktype = SOCK_STREAM;
if (getaddrinfo("localhost", NULL, &ai_hints, &ai_res) != 0) { if (getaddrinfo("localhost", nullptr, &aiHints, &aiRes) != 0) {
spdlog::error("Hyprland IPC: Couldn't get host (2)"); spdlog::error("Hyprland IPC: Couldn't get host (2)");
return ""; return "";
} }
@ -173,13 +156,13 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
return ""; return "";
} }
if (connect(SERVERSOCKET, reinterpret_cast<sockaddr*>(&serverAddress), sizeof(serverAddress)) < if (connect(serverSocket, reinterpret_cast<sockaddr*>(&serverAddress), sizeof(serverAddress)) <
0) { 0) {
spdlog::error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)"); spdlog::error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)");
return ""; return "";
} }
auto sizeWritten = write(SERVERSOCKET, rq.c_str(), rq.length()); auto sizeWritten = write(serverSocket, rq.c_str(), rq.length());
if (sizeWritten < 0) { if (sizeWritten < 0) {
spdlog::error("Hyprland IPC: Couldn't write (4)"); spdlog::error("Hyprland IPC: Couldn't write (4)");
@ -190,22 +173,22 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
std::string response; std::string response;
do { do {
sizeWritten = read(SERVERSOCKET, buffer, 8192); sizeWritten = read(serverSocket, buffer, 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, sizeWritten);
} while (sizeWritten > 0); } while (sizeWritten > 0);
close(SERVERSOCKET); close(serverSocket);
return response; return response;
} }
Json::Value IPC::getSocket1JsonReply(const std::string& rq) { Json::Value IPC::getSocket1JsonReply(const std::string& rq) {
return parser_.parse(getSocket1Reply("j/" + rq)); return m_parser.parse(getSocket1Reply("j/" + rq));
} }
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland