2012-08-28 22:32:15 +02:00
|
|
|
#ifndef event_manager_hh_INCLUDED
|
|
|
|
#define event_manager_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "utils.hh"
|
|
|
|
|
2013-01-14 19:07:38 +01:00
|
|
|
#include <chrono>
|
2013-01-31 18:58:25 +01:00
|
|
|
#include <unordered_set>
|
2013-01-14 19:07:38 +01:00
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
class FDWatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Callback = std::function<void (FDWatcher& watcher)>;
|
|
|
|
FDWatcher(int fd, Callback callback);
|
|
|
|
~FDWatcher();
|
|
|
|
|
|
|
|
int fd() const { return m_fd; }
|
|
|
|
void run() { m_callback(*this); }
|
|
|
|
private:
|
2014-03-25 10:15:56 +01:00
|
|
|
FDWatcher(const FDWatcher&) = delete;
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
int m_fd;
|
|
|
|
Callback m_callback;
|
|
|
|
};
|
2012-08-28 22:32:15 +02:00
|
|
|
|
2013-01-14 19:07:38 +01:00
|
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
using TimePoint = Clock::time_point;
|
|
|
|
|
|
|
|
class Timer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Callback = std::function<void (Timer& timer)>;
|
|
|
|
|
|
|
|
Timer(TimePoint date, Callback callback);
|
|
|
|
~Timer();
|
|
|
|
|
|
|
|
TimePoint next_date() const { return m_date; }
|
|
|
|
void set_next_date(TimePoint date) { m_date = date; }
|
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
TimePoint m_date;
|
|
|
|
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
|
|
|
|
|
|
|
void handle_next_events();
|
|
|
|
|
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;
|
2013-01-31 18:58:25 +01:00
|
|
|
std::unordered_set<FDWatcher*> m_fd_watchers;
|
|
|
|
std::unordered_set<Timer*> m_timers;
|
2013-01-11 14:28:13 +01:00
|
|
|
std::vector<int> m_forced_fd;
|
2013-01-14 19:07:38 +01:00
|
|
|
|
|
|
|
TimePoint m_last;
|
2012-08-28 22:32:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // event_manager_hh_INCLUDED
|
|
|
|
|