Added hyprevents listener.
This commit is contained in:
64
src/main.cpp
64
src/main.cpp
@ -1,11 +1,16 @@
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <hyprutils/signal/Listener.hpp>
|
||||
#include <cstring>
|
||||
#include <print>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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<char, BUFFER_SIZE> 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user