2012-08-29 00:32:15 +04:00
|
|
|
#ifndef event_manager_hh_INCLUDED
|
|
|
|
#define event_manager_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "utils.hh"
|
2014-11-25 04:00:18 +03:00
|
|
|
#include "flags.hh"
|
2015-01-12 16:58:41 +03:00
|
|
|
#include "vector.hh"
|
2012-08-29 00:32:15 +04:00
|
|
|
|
2013-01-14 22:07:38 +04:00
|
|
|
#include <chrono>
|
2014-12-10 23:54:47 +03:00
|
|
|
#include <functional>
|
2013-01-14 22:07:38 +04:00
|
|
|
|
2014-12-03 16:56:02 +03:00
|
|
|
#include <sys/select.h>
|
|
|
|
|
2012-08-29 00:32:15 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-11-25 04:00:18 +03:00
|
|
|
enum class EventMode
|
|
|
|
{
|
2014-11-29 23:14:52 +03:00
|
|
|
Normal,
|
|
|
|
Urgent,
|
|
|
|
Pending
|
2014-11-25 04:00:18 +03:00
|
|
|
};
|
|
|
|
|
2013-01-10 21:54:40 +04:00
|
|
|
class FDWatcher
|
|
|
|
{
|
|
|
|
public:
|
2014-11-25 04:00:18 +03:00
|
|
|
using Callback = std::function<void (FDWatcher& watcher, EventMode mode)>;
|
2013-01-10 21:54:40 +04:00
|
|
|
FDWatcher(int fd, Callback callback);
|
2014-11-29 23:14:52 +03:00
|
|
|
FDWatcher(const FDWatcher&) = delete;
|
|
|
|
FDWatcher& operator=(const FDWatcher&) = delete;
|
2013-01-10 21:54:40 +04:00
|
|
|
~FDWatcher();
|
|
|
|
|
|
|
|
int fd() const { return m_fd; }
|
2014-11-25 04:00:18 +03:00
|
|
|
void run(EventMode mode);
|
2014-12-03 16:56:02 +03:00
|
|
|
|
|
|
|
void close_fd();
|
|
|
|
bool closed() const { return m_fd == -1; }
|
2013-01-10 21:54:40 +04:00
|
|
|
private:
|
2014-03-25 13:15:56 +04:00
|
|
|
|
2014-11-25 04:00:18 +03:00
|
|
|
int m_fd;
|
|
|
|
Callback m_callback;
|
2013-01-10 21:54:40 +04:00
|
|
|
};
|
2012-08-29 00:32:15 +04:00
|
|
|
|
2013-01-14 22:07:38 +04:00
|
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
using TimePoint = Clock::time_point;
|
|
|
|
|
|
|
|
class Timer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Callback = std::function<void (Timer& timer)>;
|
|
|
|
|
2014-11-25 04:00:18 +03:00
|
|
|
Timer(TimePoint date, Callback callback,
|
|
|
|
EventMode mode = EventMode::Normal);
|
2014-11-29 23:14:52 +03:00
|
|
|
Timer(const Timer&) = delete;
|
|
|
|
Timer& operator=(const Timer&) = delete;
|
2013-01-14 22:07:38 +04:00
|
|
|
~Timer();
|
|
|
|
|
|
|
|
TimePoint next_date() const { return m_date; }
|
|
|
|
void set_next_date(TimePoint date) { m_date = date; }
|
2014-11-25 04:00:18 +03:00
|
|
|
void run(EventMode mode);
|
2013-01-14 22:07:38 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
TimePoint m_date;
|
2014-11-25 04:00:18 +03:00
|
|
|
EventMode m_mode;
|
2013-01-14 22:07:38 +04:00
|
|
|
Callback m_callback;
|
|
|
|
};
|
|
|
|
|
2012-11-26 17:08:27 +04: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-29 00:32:15 +04:00
|
|
|
class EventManager : public Singleton<EventManager>
|
|
|
|
{
|
|
|
|
public:
|
2012-12-03 21:49:09 +04:00
|
|
|
EventManager();
|
2013-01-10 21:54:40 +04:00
|
|
|
~EventManager();
|
2012-08-29 00:32:15 +04:00
|
|
|
|
2014-11-25 04:00:18 +03:00
|
|
|
void handle_next_events(EventMode mode);
|
2012-08-29 00:32:15 +04:00
|
|
|
|
2013-01-10 21:54:40 +04:00
|
|
|
// force the watchers associated with fd to be executed
|
2012-11-26 17:08:27 +04:00
|
|
|
// on next handle_next_events call.
|
2012-10-27 17:01:13 +04:00
|
|
|
void force_signal(int fd);
|
|
|
|
|
2012-08-29 00:32:15 +04:00
|
|
|
private:
|
2013-01-10 21:54:40 +04:00
|
|
|
friend class FDWatcher;
|
2013-01-14 22:07:38 +04:00
|
|
|
friend class Timer;
|
2015-01-12 16:58:41 +03:00
|
|
|
Vector<FDWatcher*> m_fd_watchers;
|
|
|
|
Vector<Timer*> m_timers;
|
2014-12-03 16:56:02 +03:00
|
|
|
fd_set m_forced_fd;
|
2013-01-14 22:07:38 +04:00
|
|
|
|
|
|
|
TimePoint m_last;
|
2012-08-29 00:32:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // event_manager_hh_INCLUDED
|