mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-22 09:45:58 +01:00
socket2: move to the wayland event loop
This commit is contained in:
parent
1742605eb8
commit
f7a3453487
2 changed files with 67 additions and 50 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
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();) {
|
||||
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);
|
||||
|
||||
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();
|
||||
|
||||
|
|
|
@ -21,6 +21,11 @@ class CEventManager {
|
|||
|
||||
std::thread m_tThread;
|
||||
|
||||
int m_iSocketFD = -1;
|
||||
|
||||
int onSocket2Write(int fd, uint32_t mask);
|
||||
int onFDWrite(int fd, uint32_t mask);
|
||||
|
||||
private:
|
||||
void flushEvents();
|
||||
|
||||
|
@ -28,6 +33,8 @@ class CEventManager {
|
|||
std::deque<SHyprIPCEvent> m_dQueuedEvents;
|
||||
|
||||
std::deque<std::pair<int, wl_event_source*>> m_dAcceptedSocketFDs;
|
||||
|
||||
wl_event_source* m_pEventSource = nullptr;
|
||||
};
|
||||
|
||||
inline std::unique_ptr<CEventManager> g_pEventManager;
|
||||
|
|
Loading…
Reference in a new issue