2012-08-28 22:32:15 +02:00
|
|
|
#include "event_manager.hh"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-10-27 15:01:13 +02:00
|
|
|
EventManager::EventManager() { m_forced.reserve(4); }
|
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
void EventManager::watch(int fd, EventHandler handler)
|
|
|
|
{
|
|
|
|
auto event = std::find_if(m_events.begin(), m_events.end(),
|
|
|
|
[&](const pollfd& pfd) { return pfd.fd == fd; });
|
|
|
|
if (event != m_events.end())
|
|
|
|
throw runtime_error("fd already watched");
|
|
|
|
|
|
|
|
m_events.push_back(pollfd{ fd, POLLIN | POLLPRI, 0 });
|
|
|
|
m_handlers.push_back(std::move(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventManager::unwatch(int fd)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_events.size(); ++i)
|
|
|
|
{
|
|
|
|
if (m_events[i].fd == fd)
|
|
|
|
{
|
|
|
|
m_events.erase(m_events.begin() + i);
|
|
|
|
m_handlers.erase(m_handlers.begin() + i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventManager::handle_next_events()
|
|
|
|
{
|
2012-10-27 15:01:13 +02:00
|
|
|
const int timeout_ms = 100;
|
|
|
|
int res = poll(m_events.data(), m_events.size(), timeout_ms);
|
2012-10-27 15:15:34 +02:00
|
|
|
for (size_t i = 0; i < m_events.size(); ++i)
|
2012-08-28 22:32:15 +02:00
|
|
|
{
|
2012-10-27 15:15:34 +02:00
|
|
|
if ((res > 0 and m_events[i].revents) or
|
|
|
|
contains(m_forced, m_events[i].fd))
|
|
|
|
m_handlers[i](m_events[i].fd);
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
2012-10-27 15:15:34 +02:00
|
|
|
m_forced.clear();
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
2012-10-27 15:01:13 +02:00
|
|
|
void EventManager::force_signal(int fd)
|
|
|
|
{
|
|
|
|
m_forced.push_back(fd);
|
|
|
|
}
|
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|