kakoune/src/event_manager.cc

88 lines
1.9 KiB
C++
Raw Normal View History

#include "event_manager.hh"
#include "client_manager.hh"
#include <poll.h>
namespace Kakoune
{
FDWatcher::FDWatcher(int fd, Callback callback)
: m_fd{fd}, m_callback{std::move(callback)}
{
2013-01-31 18:58:25 +01:00
EventManager::instance().m_fd_watchers.insert(this);
}
FDWatcher::~FDWatcher()
{
2013-01-31 18:58:25 +01:00
EventManager::instance().m_fd_watchers.erase(this);
}
2013-01-14 19:07:38 +01:00
Timer::Timer(TimePoint date, Callback callback)
: m_date{date}, m_callback{std::move(callback)}
{
2013-01-31 18:58:25 +01:00
EventManager::instance().m_timers.insert(this);
2013-01-14 19:07:38 +01:00
}
Timer::~Timer()
{
2013-01-31 18:58:25 +01:00
EventManager::instance().m_timers.erase(this);
2013-01-14 19:07:38 +01:00
}
void Timer::run()
{
m_date = TimePoint::max();
m_callback(*this);
}
EventManager::EventManager()
{
m_forced_fd.reserve(4);
}
2012-11-27 13:57:03 +01:00
EventManager::~EventManager()
{
kak_assert(m_fd_watchers.empty());
kak_assert(m_timers.empty());
}
void EventManager::handle_next_events()
{
const int timeout_ms = 100;
std::vector<pollfd> events;
events.reserve(m_fd_watchers.size());
for (auto& watcher : m_fd_watchers)
events.emplace_back(pollfd{ watcher->fd(), POLLIN | POLLPRI, 0 });
2013-09-23 20:32:31 +02:00
std::vector<int> forced;
std::swap(forced, m_forced_fd);
poll(events.data(), events.size(), timeout_ms);
for (size_t i = 0; i < events.size(); ++i)
{
auto& event = events[i];
const int fd = event.fd;
if (event.revents or contains(forced, fd))
{
auto it = find_if(m_fd_watchers,
[fd](FDWatcher* w) { return w->fd() == fd; });
if (it != m_fd_watchers.end())
(*it)->run();
}
}
2013-01-14 19:07:38 +01:00
TimePoint now = Clock::now();
for (auto& timer : m_timers)
{
if (timer->next_date() <= now)
timer->run();
}
if (ClientManager::has_instance())
ClientManager::instance().redraw_clients();
}
void EventManager::force_signal(int fd)
{
m_forced_fd.push_back(fd);
}
}