summaryrefslogtreecommitdiff
path: root/src/event_manager.hh
blob: fc1e94da28d3afd83b6b9578b9b9bcc9ecce0958 (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
#ifndef event_manager_hh_INCLUDED
#define event_manager_hh_INCLUDED

#include <poll.h>
#include <unordered_map>

#include "utils.hh"

namespace Kakoune
{

using EventHandler = std::function<void (int fd)>;

// The EventManager provides an interface to file descriptor
// based event handling.
//
// The program main loop should call handle_next_events()
// until it's time to quit.
class EventManager : public Singleton<EventManager>
{
public:
    EventManager();
    // Watch the given file descriptor, when data becomes
    // ready, handler will be called with fd as parameter.
    // It is an error to register multiple handlers on the
    // same file descriptor.
    void watch(int fd, EventHandler handler);

    // stop watching fd
    void unwatch(int fd);

    void handle_next_events();

    // force the handler associated with fd to be executed
    // on next handle_next_events call.
    void force_signal(int fd);

private:
    std::vector<pollfd>                        m_events;
    std::vector<std::unique_ptr<EventHandler>> m_handlers;
    std::vector<std::unique_ptr<EventHandler>> m_handlers_trash;
    std::vector<int>                           m_forced;
};

}

#endif // event_manager_hh_INCLUDED