summaryrefslogtreecommitdiff
path: root/src/managers/EventManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/managers/EventManager.cpp')
-rw-r--r--src/managers/EventManager.cpp44
1 files changed, 19 insertions, 25 deletions
diff --git a/src/managers/EventManager.cpp b/src/managers/EventManager.cpp
index fc55b472..6231f0bc 100644
--- a/src/managers/EventManager.cpp
+++ b/src/managers/EventManager.cpp
@@ -9,9 +9,10 @@
#include <sys/un.h>
#include <unistd.h>
#include <cstring>
+using namespace Hyprutils::OS;
CEventManager::CEventManager() : m_iSocketFD(socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) {
- if (m_iSocketFD < 0) {
+ if (!m_iSocketFD.isValid()) {
Debug::log(ERR, "Couldn't start the Hyprland Socket 2. (1) IPC will not work.");
return;
}
@@ -25,31 +26,27 @@ CEventManager::CEventManager() : m_iSocketFD(socket(AF_UNIX, SOCK_STREAM | SOCK_
strncpy(SERVERADDRESS.sun_path, PATH.c_str(), sizeof(SERVERADDRESS.sun_path) - 1);
- if (bind(m_iSocketFD, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS)) < 0) {
+ if (bind(m_iSocketFD.get(), (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS)) < 0) {
Debug::log(ERR, "Couldn't bind the Hyprland Socket 2. (3) IPC will not work.");
return;
}
// 10 max queued.
- if (listen(m_iSocketFD, 10) < 0) {
+ if (listen(m_iSocketFD.get(), 10) < 0) {
Debug::log(ERR, "Couldn't listen on the Hyprland Socket 2. (4) IPC will not work.");
return;
}
- m_pEventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, m_iSocketFD, WL_EVENT_READABLE, onClientEvent, nullptr);
+ m_pEventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, m_iSocketFD.get(), WL_EVENT_READABLE, onClientEvent, nullptr);
}
CEventManager::~CEventManager() {
for (const auto& client : m_vClients) {
wl_event_source_remove(client.eventSource);
- close(client.fd);
}
if (m_pEventSource != nullptr)
wl_event_source_remove(m_pEventSource);
-
- if (m_iSocketFD >= 0)
- close(m_iSocketFD);
}
int CEventManager::onServerEvent(int fd, uint32_t mask, void* data) {
@@ -66,33 +63,31 @@ int CEventManager::onServerEvent(int fd, uint32_t mask) {
wl_event_source_remove(m_pEventSource);
m_pEventSource = nullptr;
- close(fd);
- m_iSocketFD = -1;
+ m_iSocketFD.reset();
return 0;
}
- sockaddr_in clientAddress;
- socklen_t clientSize = sizeof(clientAddress);
- const auto ACCEPTEDCONNECTION = accept4(m_iSocketFD, (sockaddr*)&clientAddress, &clientSize, SOCK_CLOEXEC | SOCK_NONBLOCK);
- if (ACCEPTEDCONNECTION < 0) {
+ sockaddr_in clientAddress;
+ socklen_t clientSize = sizeof(clientAddress);
+ CFileDescriptor ACCEPTEDCONNECTION{accept4(m_iSocketFD.get(), (sockaddr*)&clientAddress, &clientSize, SOCK_CLOEXEC | SOCK_NONBLOCK)};
+ if (!ACCEPTEDCONNECTION.isValid()) {
if (errno != EAGAIN) {
Debug::log(ERR, "Socket2 failed receiving connection, errno: {}", errno);
wl_event_source_remove(m_pEventSource);
m_pEventSource = nullptr;
- close(fd);
- m_iSocketFD = -1;
+ m_iSocketFD.reset();
}
return 0;
}
- Debug::log(LOG, "Socket2 accepted a new client at FD {}", ACCEPTEDCONNECTION);
+ Debug::log(LOG, "Socket2 accepted a new client at FD {}", ACCEPTEDCONNECTION.get());
// add to event loop so we can close it when we need to
- auto* eventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, ACCEPTEDCONNECTION, 0, onServerEvent, nullptr);
+ auto* eventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, ACCEPTEDCONNECTION.get(), 0, onServerEvent, nullptr);
m_vClients.emplace_back(SClient{
- ACCEPTEDCONNECTION,
+ std::move(ACCEPTEDCONNECTION),
{},
eventSource,
});
@@ -113,7 +108,7 @@ int CEventManager::onClientEvent(int fd, uint32_t mask) {
// send all queued events
while (!CLIENTIT->events.empty()) {
const auto& event = CLIENTIT->events.front();
- if (write(CLIENTIT->fd, event->c_str(), event->length()) < 0)
+ if (write(CLIENTIT->fd.get(), event->c_str(), event->length()) < 0)
break;
CLIENTIT->events.erase(CLIENTIT->events.begin());
@@ -128,13 +123,12 @@ int CEventManager::onClientEvent(int fd, uint32_t mask) {
}
std::vector<CEventManager::SClient>::iterator CEventManager::findClientByFD(int fd) {
- return std::find_if(m_vClients.begin(), m_vClients.end(), [fd](const auto& client) { return client.fd == fd; });
+ return std::find_if(m_vClients.begin(), m_vClients.end(), [fd](const auto& client) { return client.fd.get() == fd; });
}
std::vector<CEventManager::SClient>::iterator CEventManager::removeClientByFD(int fd) {
const auto CLIENTIT = findClientByFD(fd);
wl_event_source_remove(CLIENTIT->eventSource);
- close(fd);
return m_vClients.erase(CLIENTIT);
}
@@ -157,11 +151,11 @@ void CEventManager::postEvent(const SHyprIPCEvent& event) {
for (auto it = m_vClients.begin(); it != m_vClients.end();) {
// try to send the event immediately if the queue is empty
const auto QUEUESIZE = it->events.size();
- if (QUEUESIZE > 0 || write(it->fd, sharedEvent->c_str(), sharedEvent->length()) < 0) {
+ if (QUEUESIZE > 0 || write(it->fd.get(), sharedEvent->c_str(), sharedEvent->length()) < 0) {
if (QUEUESIZE >= MAX_QUEUED_EVENTS) {
// too many events queued, remove the client
- Debug::log(ERR, "Socket2 fd {} overflowed event queue, removing", it->fd);
- it = removeClientByFD(it->fd);
+ Debug::log(ERR, "Socket2 fd {} overflowed event queue, removing", it->fd.get());
+ it = removeClientByFD(it->fd.get());
continue;
}