summaryrefslogtreecommitdiff
path: root/src/managers/EventManager.cpp
diff options
context:
space:
mode:
authorVaxry <vaxry@vaxry.net>2024-02-26 17:20:46 +0000
committerVaxry <vaxry@vaxry.net>2024-02-26 17:20:51 +0000
commitf7a34534873bf499fc1a04a5fa25ada03c4ee415 (patch)
tree4c911b8b551d6be18940da692177258ddb5d7c12 /src/managers/EventManager.cpp
parent1742605eb89b17d04d807dfb1a21ac0deb3a945c (diff)
socket2: move to the wayland event loop
Diffstat (limited to 'src/managers/EventManager.cpp')
-rw-r--r--src/managers/EventManager.cpp110
1 files changed, 60 insertions, 50 deletions
diff --git a/src/managers/EventManager.cpp b/src/managers/EventManager.cpp
index 64b4f6bf..8365c8c8 100644
--- a/src/managers/EventManager.cpp
+++ b/src/managers/EventManager.cpp
@@ -19,13 +19,69 @@
CEventManager::CEventManager() {}
int fdHandleWrite(int fd, uint32_t mask, void* data) {
+ const auto PEVMGR = (CEventManager*)data;
+ return PEVMGR->onFDWrite(fd, mask);
+}
+
+int socket2HandleWrite(int fd, uint32_t mask, void* data) {
+ const auto PEVMGR = (CEventManager*)data;
+ return PEVMGR->onSocket2Write(fd, mask);
+}
+
+void CEventManager::startThread() {
+
+ m_iSocketFD = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+
+ if (m_iSocketFD < 0) {
+ Debug::log(ERR, "Couldn't start the Hyprland Socket 2. (1) IPC will not work.");
+ return;
+ }
+
+ sockaddr_un SERVERADDRESS = {.sun_family = AF_UNIX};
+ std::string socketPath = "/tmp/hypr/" + g_pCompositor->m_szInstanceSignature + "/.socket2.sock";
+ strncpy(SERVERADDRESS.sun_path, socketPath.c_str(), sizeof(SERVERADDRESS.sun_path) - 1);
+
+ bind(m_iSocketFD, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS));
+
+ // 10 max queued.
+ listen(m_iSocketFD, 10);
- auto removeFD = [&](int fd) -> void {
- const auto ACCEPTEDFDS = (std::deque<std::pair<int, wl_event_source*>>*)data;
- for (auto it = ACCEPTEDFDS->begin(); it != ACCEPTEDFDS->end();) {
+ m_pEventSource = wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, m_iSocketFD, WL_EVENT_READABLE, socket2HandleWrite, this);
+}
+
+int CEventManager::onSocket2Write(int fd, uint32_t mask) {
+
+ if (mask & WL_EVENT_ERROR || mask & WL_EVENT_HANGUP) {
+ Debug::log(ERR, "Socket2 hangup?? IPC broke");
+ wl_event_source_remove(m_pEventSource);
+ close(fd);
+ 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) {
+ Debug::log(LOG, "Socket2 accepted a new client at FD {}", ACCEPTEDCONNECTION);
+
+ // add to event loop so we can close it when we need to
+ m_dAcceptedSocketFDs.push_back(
+ std::make_pair<>(ACCEPTEDCONNECTION, wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, ACCEPTEDCONNECTION, WL_EVENT_READABLE, fdHandleWrite, this)));
+ } else {
+ Debug::log(ERR, "Socket2 failed receiving connection, errno: {}", errno);
+ close(fd);
+ }
+
+ return 0;
+}
+
+int CEventManager::onFDWrite(int fd, uint32_t mask) {
+ auto removeFD = [this](int fd) -> void {
+ for (auto it = m_dAcceptedSocketFDs.begin(); it != m_dAcceptedSocketFDs.end();) {
if (it->first == fd) {
wl_event_source_remove(it->second); // remove this fd listener
- it = ACCEPTEDFDS->erase(it);
+ it = m_dAcceptedSocketFDs.erase(it);
} else {
it++;
}
@@ -58,52 +114,6 @@ int fdHandleWrite(int fd, uint32_t mask, void* data) {
return 0;
}
-void CEventManager::startThread() {
- m_tThread = std::thread([&]() {
- const auto SOCKET = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
-
- if (SOCKET < 0) {
- Debug::log(ERR, "Couldn't start the Hyprland Socket 2. (1) IPC will not work.");
- return;
- }
-
- sockaddr_un SERVERADDRESS = {.sun_family = AF_UNIX};
- std::string socketPath = "/tmp/hypr/" + g_pCompositor->m_szInstanceSignature + "/.socket2.sock";
- strncpy(SERVERADDRESS.sun_path, socketPath.c_str(), sizeof(SERVERADDRESS.sun_path) - 1);
-
- bind(SOCKET, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS));
-
- // 10 max queued.
- listen(SOCKET, 10);
-
- sockaddr_in clientAddress;
- socklen_t clientSize = sizeof(clientAddress);
-
- Debug::log(LOG, "Hypr socket 2 started at {}", socketPath);
-
- while (1) {
- const auto ACCEPTEDCONNECTION = accept4(SOCKET, (sockaddr*)&clientAddress, &clientSize, SOCK_CLOEXEC);
-
- if (ACCEPTEDCONNECTION > 0) {
- // new connection!
-
- int flagsNew = fcntl(ACCEPTEDCONNECTION, F_GETFL, 0);
- fcntl(ACCEPTEDCONNECTION, F_SETFL, flagsNew | O_NONBLOCK);
-
- Debug::log(LOG, "Socket 2 accepted a new client at FD {}", ACCEPTEDCONNECTION);
-
- // add to event loop so we can close it when we need to
- m_dAcceptedSocketFDs.push_back(
- {ACCEPTEDCONNECTION, wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, ACCEPTEDCONNECTION, WL_EVENT_READABLE, fdHandleWrite, &m_dAcceptedSocketFDs)});
- }
- }
-
- close(SOCKET);
- });
-
- m_tThread.detach();
-}
-
void CEventManager::flushEvents() {
eventQueueMutex.lock();