From 63191dd5dd5d8c25d78bfa69c601f4ddec158778 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Mon, 8 Sep 2025 22:36:54 +0200 Subject: [PATCH] Added hyprevents listener. --- src/main.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 343799a..41bbf44 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,16 @@ +#include +#include #include -#include +#include #include #include #include +#include #include std::string instanceSignature; +int hypr_socket; +bool sigintReceived; std::string getRuntimeDir() { const auto XDG = getenv("XDG_RUNTIME_DIR"); @@ -18,6 +23,39 @@ std::string getRuntimeDir() { return std::string(XDG) + "/hypr"; } +int readLoop(int hypr_socket) { + constexpr size_t BUFFER_SIZE = 8192; + std::array buffer = {0}; + long sizeWritten = 0; + + while (!sigintReceived) { + sizeWritten = read(hypr_socket, buffer.data(), BUFFER_SIZE); + if (sizeWritten < 0 && errno != EAGAIN) { + if (errno != EINTR) + std::println("Couldn't read (5): {}: {}", strerror(errno), errno); + close(hypr_socket); + return 5; + } + + if (sizeWritten == 0) + break; + + if (sizeWritten > 0) { + std::println("{}", std::string(buffer.data(), sizeWritten)); + buffer.fill('\0'); + } + + usleep(100000); + } + close(hypr_socket); + return 0; +} + +void signalHandler(int signal) { + std::println("Received {}", signal); + sigintReceived = true; +} + int main() { auto runtimeDir = getRuntimeDir(); instanceSignature = std::string(std::getenv("HYPRLAND_INSTANCE_SIGNATURE")); @@ -26,17 +64,23 @@ int main() { return 1; } - auto socket_fpath = runtimeDir + "/" + instanceSignature + "/.socket.sock"; + auto socket_fpath = runtimeDir + "/" + instanceSignature + "/.socket2.sock"; std::println("Found socket file path: {}", socket_fpath); - sockaddr *socket_address = new sockaddr(); - socket_address->sa_family = AF_UNIX; + struct sockaddr_un socket_address; + socket_address.sun_family = AF_UNIX; + strncpy(socket_address.sun_path, socket_fpath.c_str(), + sizeof(socket_address.sun_path) - 1); - auto hypr_socket = socket(AF_INET, SOCK_STREAM, 0); + auto hypr_socket = socket(AF_UNIX, SOCK_STREAM, 0); if (hypr_socket < 1) { std::println("Cannot create socket (1)."); return 1; } + + std::signal(SIGINT, signalHandler); + std::signal(SIGTERM, signalHandler); + auto timeout = timeval{.tv_sec = 5, .tv_usec = 0}; if (setsockopt(hypr_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0) { @@ -44,5 +88,15 @@ int main() { return 2; } + if (connect(hypr_socket, (struct sockaddr *)&socket_address, + SUN_LEN(&socket_address)) < 0) { + perror("connect"); + close(hypr_socket); + std::println("Cannot connect to {} (3).", socket_fpath); + return 3; + } + + readLoop(hypr_socket); + return 0; }