aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/managers/EventManager.cpp
blob: 64b4f6bfbe15bd4d1d50fe31e9819218c9deb5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "EventManager.hpp"
#include "../Compositor.hpp"

#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>
#include <sys/ioctl.h>

#include <string>

CEventManager::CEventManager() {}

int fdHandleWrite(int fd, uint32_t mask, void* data) {

    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();) {
            if (it->first == fd) {
                wl_event_source_remove(it->second); // remove this fd listener
                it = ACCEPTEDFDS->erase(it);
            } else {
                it++;
            }
        }

        close(fd);
    };

    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 {} 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 {} sent invalid data (2)", fd);
        removeFD(fd);
        return 0;
    }

    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();

    for (auto& ev : m_dQueuedEvents) {
        std::string eventString = (ev.event + ">>" + ev.data).substr(0, 1022) + "\n";
        for (auto& fd : m_dAcceptedSocketFDs) {
            try {
                write(fd.first, eventString.c_str(), eventString.length());
            } catch (...) {}
        }
    }

    m_dQueuedEvents.clear();

    eventQueueMutex.unlock();
}

void CEventManager::postEvent(const SHyprIPCEvent event) {

    if (g_pCompositor->m_bIsShuttingDown) {
        Debug::log(WARN, "Suppressed (ignoreevents true / shutting down) event of type {}, content: {}", event.event, event.data);
        return;
    }

    std::thread(
        [&](const SHyprIPCEvent ev) {
            eventQueueMutex.lock();
            m_dQueuedEvents.push_back(ev);
            eventQueueMutex.unlock();

            flushEvents();
        },
        event)
        .detach();
}