1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-20 01:41:40 +03:00
kakoune/src/event_manager.hh

99 lines
2.0 KiB
C++
Raw Normal View History

#ifndef event_manager_hh_INCLUDED
#define event_manager_hh_INCLUDED
2016-07-20 10:49:04 +03:00
#include "clock.hh"
#include "utils.hh"
#include "flags.hh"
2015-01-12 16:58:41 +03:00
#include "vector.hh"
2014-12-10 23:54:47 +03:00
#include <functional>
2013-01-14 22:07:38 +04:00
#include <sys/select.h>
#include <signal.h>
namespace Kakoune
{
enum class EventMode
{
Normal,
Urgent,
Pending
};
class FDWatcher
{
public:
using Callback = std::function<void (FDWatcher& watcher, EventMode mode)>;
FDWatcher(int fd, Callback callback);
FDWatcher(const FDWatcher&) = delete;
FDWatcher& operator=(const FDWatcher&) = delete;
~FDWatcher();
int fd() const { return m_fd; }
void run(EventMode mode);
void close_fd();
2015-10-01 21:36:37 +03:00
void disable() { m_fd = -1; }
private:
int m_fd;
Callback m_callback;
};
2013-01-14 22:07:38 +04:00
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;
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; }
void run(EventMode mode);
2013-01-14 22:07:38 +04:00
private:
TimePoint m_date;
EventMode m_mode;
2013-01-14 22:07:38 +04:00
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:
EventManager();
~EventManager();
void handle_next_events(EventMode mode, sigset_t* sigmask = nullptr);
// force the watchers associated with fd to be executed
// on next handle_next_events call.
void force_signal(int fd);
private:
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;
fd_set m_forced_fd;
2013-01-14 22:07:38 +04:00
TimePoint m_last;
};
using SignalHandler = void(*)(int);
SignalHandler set_signal_handler(int signum, SignalHandler handler);
}
#endif // event_manager_hh_INCLUDED