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

#include "clock.hh"
#include "meta.hh"
#include "utils.hh"
#include "optional.hh"
#include "vector.hh"

#include <functional>

#include <sys/select.h>
#include <csignal>

namespace Kakoune
{

enum class EventMode
{
    Normal,
    Urgent,
};

enum class FdEvents
{
    None = 0,
    Read = 1 << 0,
    Write = 1 << 1,
    Except = 1 << 2,
};

constexpr bool with_bit_ops(Meta::Type<FdEvents>) { return true; }

class FDWatcher
{
public:
    using Callback = std::function<void (FDWatcher& watcher, FdEvents events, EventMode mode)>;
    FDWatcher(int fd, FdEvents events, EventMode mode, Callback callback);
    FDWatcher(const FDWatcher&) = delete;
    FDWatcher& operator=(const FDWatcher&) = delete;
    ~FDWatcher();

    int fd() const { return m_fd; }
    FdEvents events() const { return m_events; }
    FdEvents& events() { return m_events; }
    EventMode mode() const { return m_mode; }

    void run(FdEvents events, EventMode mode);

    void reset_fd(int fd) { m_fd = fd; }
    void close_fd();
    void disable() { m_fd = -1; }

private:
    int      m_fd;
    FdEvents m_events;
    EventMode m_mode;
    Callback m_callback;
};

class Timer
{
public:
    using Callback = std::function<void (Timer& timer)>;

    Timer(TimePoint date, Callback callback,
          EventMode mode = EventMode::Normal);
    Timer(const Timer&) = delete;
    Timer& operator=(const Timer&) = delete;
    ~Timer();

    TimePoint next_date() const { return m_date; }
    void set_next_date(TimePoint date) { m_date = date; }
    void disable() { m_date = TimePoint::max(); }
    void run(EventMode mode);

private:
    TimePoint m_date;
    EventMode m_mode;
    Callback  m_callback;
};

// 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:
    using Nanoseconds = std::chrono::nanoseconds;

    EventManager();
    ~EventManager();

    // blocks until next event if no timeout given
    bool handle_next_events(EventMode mode, sigset_t* sigmask = nullptr,
                            Optional<Nanoseconds> timeout = {});

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

    static void handle_urgent_events();

private:
    friend class FDWatcher;
    friend class Timer;
    Vector<FDWatcher*, MemoryDomain::Events> m_fd_watchers;
    Vector<Timer*, MemoryDomain::Events>     m_timers;
    fd_set m_forced_fd;
    bool   m_has_forced_fd = false;

    TimePoint m_last;
};

using SignalHandler = void(*)(int);

SignalHandler set_signal_handler(int signum, SignalHandler handler);

}

#endif // event_manager_hh_INCLUDED