commit bb180080b175e6c860872ff6865176bee12ff00f Author: Pavel Kirilin Date: Mon Sep 8 21:42:47 2025 +0200 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b876454 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +bin/ +.cache/ + +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +CMakeUserPresets.json + +# Binary itself +hyprevents + +# CLion +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#cmake-build-* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..207abee --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.2) + +project( + hyprevents + DESCRIPTION "React on hyprland events" + LANGUAGES CXX +) + +set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}) +set(CMAKE_EXPORT_COMPILE_COMMANDS, 1) + +set(CMAKE_CXX_FLAGS "-Wall -Wextra -W") +set(CMAKE_CXX_FLAGS_DEBUG "-g") +set(CMAKE_CXX_FLAGS_RELEASE "-O3") +set(CMAKE_CXX_STANDARD 23) + +add_subdirectory(src) + +add_custom_target(run + COMMAND hyprevents + DEPENDS hyprevents + WORKING_DIRECTORY src +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..a21a638 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Hyprland custom event listeneres diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..ea635e6 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(hyprevents main.cpp) +install(TARGETS hyprevents) diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..343799a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include + +std::string instanceSignature; + +std::string getRuntimeDir() { + const auto XDG = getenv("XDG_RUNTIME_DIR"); + + if (!XDG) { + const std::string USERID = std::to_string(getuid()); + return "/run/user/" + USERID + "/hypr"; + } + + return std::string(XDG) + "/hypr"; +} + +int main() { + auto runtimeDir = getRuntimeDir(); + instanceSignature = std::string(std::getenv("HYPRLAND_INSTANCE_SIGNATURE")); + if (instanceSignature.empty()) { + std::println("Hyprland instance isn't running."); + return 1; + } + + auto socket_fpath = runtimeDir + "/" + instanceSignature + "/.socket.sock"; + std::println("Found socket file path: {}", socket_fpath); + + sockaddr *socket_address = new sockaddr(); + socket_address->sa_family = AF_UNIX; + + auto hypr_socket = socket(AF_INET, SOCK_STREAM, 0); + if (hypr_socket < 1) { + std::println("Cannot create socket (1)."); + return 1; + } + auto timeout = timeval{.tv_sec = 5, .tv_usec = 0}; + if (setsockopt(hypr_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, + sizeof(struct timeval)) < 0) { + std::println("Cannot set timeout for a socket (2)."); + return 2; + } + + return 0; +}