2012-08-28 22:32:15 +02:00
|
|
|
#include "event_manager.hh"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
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 });
|
2012-11-06 13:34:58 +01:00
|
|
|
m_handlers.emplace(fd, std::move(handler));
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventManager::unwatch(int fd)
|
|
|
|
{
|
2012-11-26 14:08:27 +01:00
|
|
|
// do not unwatch now, do that at the end of handle_next_events,
|
|
|
|
// so that if unwatch(fd) is called from fd event handler,
|
|
|
|
// it is not deleted now.
|
|
|
|
m_unwatched.push_back(fd);
|
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;
|
2012-11-26 14:08:27 +01:00
|
|
|
poll(m_events.data(), m_events.size(), timeout_ms);
|
|
|
|
for (auto& event : m_events)
|
|
|
|
{
|
|
|
|
const int fd = event.fd;
|
|
|
|
if ((event.revents or contains(m_forced, fd)) and not contains(m_unwatched, fd))
|
|
|
|
m_handlers[fd](fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove unwatched.
|
|
|
|
for (auto fd : m_unwatched)
|
2012-08-28 22:32:15 +02:00
|
|
|
{
|
2012-11-26 14:08:27 +01:00
|
|
|
auto it = std::find_if(m_events.begin(), m_events.end(),
|
|
|
|
[fd](pollfd& p) { return p.fd == fd; });
|
|
|
|
m_events.erase(it);
|
|
|
|
m_handlers.erase(fd);
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
2012-11-26 14:08:27 +01:00
|
|
|
m_unwatched.clear();
|
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
|
|
|
}
|