2012-08-28 22:32:15 +02:00
|
|
|
#include "event_manager.hh"
|
2013-09-17 19:51:14 +02:00
|
|
|
#include "client_manager.hh"
|
2012-08-28 22:32:15 +02:00
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
#include <poll.h>
|
2012-08-28 22:32:15 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
FDWatcher::FDWatcher(int fd, Callback callback)
|
|
|
|
: m_fd{fd}, m_callback{std::move(callback)}
|
2012-12-03 18:49:09 +01:00
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
EventManager::instance().m_fd_watchers.insert(this);
|
2012-12-03 18:49:09 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
FDWatcher::~FDWatcher()
|
2012-08-28 22:32:15 +02:00
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
EventManager::instance().m_fd_watchers.erase(this);
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
EventManager::EventManager()
|
2012-08-28 22:32:15 +02:00
|
|
|
{
|
2013-01-10 18:54:40 +01:00
|
|
|
m_forced_fd.reserve(4);
|
|
|
|
}
|
2012-11-27 13:57:03 +01:00
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
EventManager::~EventManager()
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(m_fd_watchers.empty());
|
|
|
|
kak_assert(m_timers.empty());
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventManager::handle_next_events()
|
|
|
|
{
|
2012-10-27 15:01:13 +02:00
|
|
|
const int timeout_ms = 100;
|
2013-01-10 18:54:40 +01:00
|
|
|
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);
|
2013-01-10 18:54:40 +01:00
|
|
|
poll(events.data(), events.size(), timeout_ms);
|
|
|
|
for (size_t i = 0; i < events.size(); ++i)
|
2012-11-26 14:08:27 +01:00
|
|
|
{
|
2013-01-10 18:54:40 +01:00
|
|
|
auto& event = events[i];
|
2012-11-26 14:08:27 +01:00
|
|
|
const int fd = event.fd;
|
2012-12-03 18:49:09 +01:00
|
|
|
if (event.revents or contains(forced, fd))
|
2013-01-10 18:54:40 +01:00
|
|
|
{
|
2013-01-11 14:28:13 +01:00
|
|
|
auto it = find_if(m_fd_watchers,
|
|
|
|
[fd](FDWatcher* w) { return w->fd() == fd; });
|
2013-01-10 18:54:40 +01:00
|
|
|
if (it != m_fd_watchers.end())
|
|
|
|
(*it)->run();
|
|
|
|
}
|
2012-11-26 14:08:27 +01:00
|
|
|
}
|
2013-01-14 19:07:38 +01:00
|
|
|
|
|
|
|
TimePoint now = Clock::now();
|
|
|
|
for (auto& timer : m_timers)
|
|
|
|
{
|
|
|
|
if (timer->next_date() <= now)
|
|
|
|
timer->run();
|
|
|
|
}
|
2013-09-17 19:51:14 +02:00
|
|
|
|
|
|
|
if (ClientManager::has_instance())
|
|
|
|
ClientManager::instance().redraw_clients();
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
2012-10-27 15:01:13 +02:00
|
|
|
void EventManager::force_signal(int fd)
|
|
|
|
{
|
2013-01-10 18:54:40 +01:00
|
|
|
m_forced_fd.push_back(fd);
|
|
|
|
}
|
2012-10-27 15:01:13 +02:00
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|