2022-05-24 19:42:43 +02:00
|
|
|
#include "EventManager.hpp"
|
2022-06-03 17:41:57 +02:00
|
|
|
#include "../Compositor.hpp"
|
2022-05-24 19:42:43 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
2023-04-09 00:14:12 +02:00
|
|
|
#include <sys/ioctl.h>
|
2022-05-24 19:42:43 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
CEventManager::CEventManager() {}
|
2022-05-24 19:42:43 +02:00
|
|
|
|
2022-10-22 17:15:48 +02:00
|
|
|
int fdHandleWrite(int fd, uint32_t mask, void* data) {
|
2023-04-09 00:14:12 +02:00
|
|
|
|
|
|
|
auto removeFD = [&](int fd) -> void {
|
2022-10-22 17:15:48 +02:00
|
|
|
const auto ACCEPTEDFDS = (std::deque<std::pair<int, wl_event_source*>>*)data;
|
2022-12-16 18:17:31 +01:00
|
|
|
for (auto it = ACCEPTEDFDS->begin(); it != ACCEPTEDFDS->end();) {
|
2022-10-22 17:15:48 +02:00
|
|
|
if (it->first == fd) {
|
|
|
|
wl_event_source_remove(it->second); // remove this fd listener
|
|
|
|
it = ACCEPTEDFDS->erase(it);
|
|
|
|
} else {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2023-07-06 00:18:44 +02:00
|
|
|
|
|
|
|
close(fd);
|
2023-04-09 00:14:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (mask & WL_EVENT_ERROR || mask & WL_EVENT_HANGUP) {
|
|
|
|
// remove, hanged up
|
|
|
|
removeFD(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int availableBytes;
|
|
|
|
if (ioctl(fd, FIONREAD, &availableBytes) == -1) {
|
|
|
|
Debug::log(ERR, "fd %d sent invalid data (1)", fd);
|
|
|
|
removeFD(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[availableBytes];
|
|
|
|
const auto RECEIVED = recv(fd, buf, availableBytes, 0);
|
|
|
|
if (RECEIVED == -1) {
|
|
|
|
Debug::log(ERR, "fd %d sent invalid data (2)", fd);
|
|
|
|
removeFD(fd);
|
|
|
|
return 0;
|
2022-10-22 17:15:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-24 19:42:43 +02:00
|
|
|
void CEventManager::startThread() {
|
2022-08-28 11:19:08 +02:00
|
|
|
m_tThread = std::thread([&]() {
|
2022-05-24 19:42:43 +02:00
|
|
|
const auto SOCKET = socket(AF_UNIX, SOCK_STREAM, 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};
|
2022-12-16 18:17:31 +01:00
|
|
|
std::string socketPath = "/tmp/hypr/" + g_pCompositor->m_szInstanceSignature + "/.socket2.sock";
|
2023-04-23 22:20:29 +02:00
|
|
|
strncpy(SERVERADDRESS.sun_path, socketPath.c_str(), sizeof(SERVERADDRESS.sun_path) - 1);
|
2022-05-24 19:42:43 +02:00
|
|
|
|
|
|
|
bind(SOCKET, (sockaddr*)&SERVERADDRESS, SUN_LEN(&SERVERADDRESS));
|
|
|
|
|
|
|
|
// 10 max queued.
|
|
|
|
listen(SOCKET, 10);
|
|
|
|
|
|
|
|
sockaddr_in clientAddress;
|
2022-12-16 18:17:31 +01:00
|
|
|
socklen_t clientSize = sizeof(clientAddress);
|
2022-05-24 19:42:43 +02:00
|
|
|
|
2022-06-03 17:41:57 +02:00
|
|
|
Debug::log(LOG, "Hypr socket 2 started at %s", socketPath.c_str());
|
2022-05-24 19:42:43 +02:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize);
|
|
|
|
|
|
|
|
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 %d", ACCEPTEDCONNECTION);
|
2022-10-22 17:15:48 +02:00
|
|
|
|
|
|
|
// add to event loop so we can close it when we need to
|
2022-12-16 18:17:31 +01:00
|
|
|
m_dAcceptedSocketFDs.push_back(
|
|
|
|
{ACCEPTEDCONNECTION, wl_event_loop_add_fd(g_pCompositor->m_sWLEventLoop, ACCEPTEDCONNECTION, WL_EVENT_READABLE, fdHandleWrite, &m_dAcceptedSocketFDs)});
|
2022-05-24 19:42:43 +02:00
|
|
|
}
|
2022-10-03 21:47:15 +02:00
|
|
|
}
|
2022-05-24 19:42:43 +02:00
|
|
|
|
2022-10-03 21:47:15 +02:00
|
|
|
close(SOCKET);
|
|
|
|
});
|
2022-05-24 19:42:43 +02:00
|
|
|
|
2022-10-03 21:47:15 +02:00
|
|
|
m_tThread.detach();
|
|
|
|
}
|
2022-05-24 19:42:43 +02:00
|
|
|
|
2022-10-03 21:47:15 +02:00
|
|
|
void CEventManager::flushEvents() {
|
|
|
|
eventQueueMutex.lock();
|
2022-05-24 19:42:43 +02:00
|
|
|
|
2022-10-03 21:47:15 +02:00
|
|
|
for (auto& ev : m_dQueuedEvents) {
|
|
|
|
std::string eventString = (ev.event + ">>" + ev.data).substr(0, 1022) + "\n";
|
|
|
|
for (auto& fd : m_dAcceptedSocketFDs) {
|
2023-05-21 14:38:18 +02:00
|
|
|
try {
|
|
|
|
write(fd.first, eventString.c_str(), eventString.length());
|
|
|
|
} catch(...) {}
|
2022-05-24 19:42:43 +02:00
|
|
|
}
|
2022-10-03 21:47:15 +02:00
|
|
|
}
|
2022-05-24 19:42:43 +02:00
|
|
|
|
2022-10-03 21:47:15 +02:00
|
|
|
m_dQueuedEvents.clear();
|
2022-08-28 11:19:08 +02:00
|
|
|
|
2022-10-03 21:47:15 +02:00
|
|
|
eventQueueMutex.unlock();
|
2022-05-24 19:42:43 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 18:19:06 +02:00
|
|
|
void CEventManager::postEvent(const SHyprIPCEvent event, bool force) {
|
2022-06-21 22:17:30 +02:00
|
|
|
|
2022-08-28 11:19:08 +02:00
|
|
|
if ((m_bIgnoreEvents && !force) || g_pCompositor->m_bIsShuttingDown) {
|
2022-12-16 18:17:31 +01:00
|
|
|
Debug::log(WARN, "Suppressed (ignoreevents true / shutting down) event of type %s, content: %s", event.event.c_str(), event.data.c_str());
|
2022-06-21 22:17:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
std::thread(
|
|
|
|
[&](const SHyprIPCEvent ev) {
|
|
|
|
eventQueueMutex.lock();
|
|
|
|
m_dQueuedEvents.push_back(ev);
|
|
|
|
eventQueueMutex.unlock();
|
2022-10-03 21:47:15 +02:00
|
|
|
|
2022-12-16 18:17:31 +01:00
|
|
|
flushEvents();
|
|
|
|
},
|
|
|
|
event)
|
|
|
|
.detach();
|
2022-06-22 01:13:05 +02:00
|
|
|
}
|