2012-08-28 22:32:15 +02:00
|
|
|
#ifndef event_manager_hh_INCLUDED
|
|
|
|
#define event_manager_hh_INCLUDED
|
|
|
|
|
2016-07-20 09:49:04 +02:00
|
|
|
#include "clock.hh"
|
2017-03-15 18:55:34 +01:00
|
|
|
#include "meta.hh"
|
2012-08-28 22:32:15 +02:00
|
|
|
#include "utils.hh"
|
2015-01-12 14:58:41 +01:00
|
|
|
#include "vector.hh"
|
2012-08-28 22:32:15 +02:00
|
|
|
|
2014-12-10 21:54:47 +01:00
|
|
|
#include <functional>
|
2013-01-14 19:07:38 +01:00
|
|
|
|
2014-12-03 14:56:02 +01:00
|
|
|
#include <sys/select.h>
|
2017-01-08 23:30:15 +01:00
|
|
|
#include <csignal>
|
2014-12-03 14:56:02 +01:00
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
enum class EventMode
|
|
|
|
{
|
2014-11-29 21:14:52 +01:00
|
|
|
Normal,
|
|
|
|
Urgent,
|
2014-11-25 02:00:18 +01:00
|
|
|
};
|
|
|
|
|
2016-11-30 14:59:08 +01:00
|
|
|
enum class FdEvents
|
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
Read = 1 << 0,
|
|
|
|
Write = 1 << 1,
|
|
|
|
Except = 1 << 2,
|
|
|
|
};
|
|
|
|
|
2017-03-15 18:55:34 +01:00
|
|
|
constexpr bool with_bit_ops(Meta::Type<FdEvents>) { return true; }
|
2016-11-30 14:59:08 +01:00
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
class FDWatcher
|
|
|
|
{
|
|
|
|
public:
|
2016-11-30 14:59:08 +01:00
|
|
|
using Callback = std::function<void (FDWatcher& watcher, FdEvents events, EventMode mode)>;
|
|
|
|
FDWatcher(int fd, FdEvents events, Callback callback);
|
2014-11-29 21:14:52 +01:00
|
|
|
FDWatcher(const FDWatcher&) = delete;
|
|
|
|
FDWatcher& operator=(const FDWatcher&) = delete;
|
2013-01-10 18:54:40 +01:00
|
|
|
~FDWatcher();
|
|
|
|
|
|
|
|
int fd() const { return m_fd; }
|
2016-11-30 14:59:08 +01:00
|
|
|
FdEvents events() const { return m_events; }
|
|
|
|
FdEvents& events() { return m_events; }
|
|
|
|
|
|
|
|
void run(FdEvents events, EventMode mode);
|
2014-12-03 14:56:02 +01:00
|
|
|
|
|
|
|
void close_fd();
|
2015-10-01 20:36:37 +02:00
|
|
|
void disable() { m_fd = -1; }
|
2014-03-25 10:15:56 +01:00
|
|
|
|
2017-07-07 06:59:53 +02:00
|
|
|
private:
|
2016-11-30 14:59:08 +01:00
|
|
|
int m_fd;
|
|
|
|
FdEvents m_events;
|
|
|
|
Callback m_callback;
|
2013-01-10 18:54:40 +01:00
|
|
|
};
|
2012-08-28 22:32:15 +02:00
|
|
|
|
2013-01-14 19:07:38 +01:00
|
|
|
class Timer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Callback = std::function<void (Timer& timer)>;
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
Timer(TimePoint date, Callback callback,
|
|
|
|
EventMode mode = EventMode::Normal);
|
2014-11-29 21:14:52 +01:00
|
|
|
Timer(const Timer&) = delete;
|
|
|
|
Timer& operator=(const Timer&) = delete;
|
2013-01-14 19:07:38 +01:00
|
|
|
~Timer();
|
|
|
|
|
|
|
|
TimePoint next_date() const { return m_date; }
|
|
|
|
void set_next_date(TimePoint date) { m_date = date; }
|
2014-11-25 02:00:18 +01:00
|
|
|
void run(EventMode mode);
|
2013-01-14 19:07:38 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
TimePoint m_date;
|
2014-11-25 02:00:18 +01:00
|
|
|
EventMode m_mode;
|
2013-01-14 19:07:38 +01:00
|
|
|
Callback m_callback;
|
|
|
|
};
|
|
|
|
|
2012-11-26 14:08:27 +01:00
|
|
|
// 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.
|
2012-08-28 22:32:15 +02:00
|
|
|
class EventManager : public Singleton<EventManager>
|
|
|
|
{
|
|
|
|
public:
|
2012-12-03 18:49:09 +01:00
|
|
|
EventManager();
|
2013-01-10 18:54:40 +01:00
|
|
|
~EventManager();
|
2012-08-28 22:32:15 +02:00
|
|
|
|
2018-05-15 13:37:41 +02:00
|
|
|
bool handle_next_events(EventMode mode, sigset_t* sigmask = nullptr, bool block = true);
|
2012-08-28 22:32:15 +02:00
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
// force the watchers associated with fd to be executed
|
2012-11-26 14:08:27 +01:00
|
|
|
// on next handle_next_events call.
|
2012-10-27 15:01:13 +02:00
|
|
|
void force_signal(int fd);
|
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
private:
|
2013-01-10 18:54:40 +01:00
|
|
|
friend class FDWatcher;
|
2013-01-14 19:07:38 +01:00
|
|
|
friend class Timer;
|
2016-11-28 14:59:55 +01:00
|
|
|
Vector<FDWatcher*, MemoryDomain::Events> m_fd_watchers;
|
|
|
|
Vector<Timer*, MemoryDomain::Events> m_timers;
|
2014-12-03 14:56:02 +01:00
|
|
|
fd_set m_forced_fd;
|
2018-06-03 04:04:55 +02:00
|
|
|
bool m_has_forced_fd = false;
|
2013-01-14 19:07:38 +01:00
|
|
|
|
2016-11-28 14:59:55 +01:00
|
|
|
TimePoint m_last;
|
2012-08-28 22:32:15 +02:00
|
|
|
};
|
|
|
|
|
2016-01-10 21:46:15 +01:00
|
|
|
using SignalHandler = void(*)(int);
|
|
|
|
|
|
|
|
SignalHandler set_signal_handler(int signum, SignalHandler handler);
|
2015-12-20 21:27:34 +01:00
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // event_manager_hh_INCLUDED
|