EventManager: add force_signal method to force event execution

This commit is contained in:
Maxime Coste 2012-10-27 15:01:13 +02:00
parent 24b43f91c8
commit 70cdb7a811
2 changed files with 17 additions and 3 deletions

View File

@ -5,6 +5,8 @@
namespace Kakoune
{
EventManager::EventManager() { m_forced.reserve(4); }
void EventManager::watch(int fd, EventHandler handler)
{
auto event = std::find_if(m_events.begin(), m_events.end(),
@ -31,15 +33,22 @@ void EventManager::unwatch(int fd)
void EventManager::handle_next_events()
{
int res = poll(m_events.data(), m_events.size(), -1);
if (res > 0)
const int timeout_ms = 100;
int res = poll(m_events.data(), m_events.size(), timeout_ms);
if (res >= 0)
{
for (size_t i = 0; i < m_events.size(); ++i)
{
if (m_events[i].revents)
if (m_events[i].revents or contains(m_forced, m_events[i].fd))
m_handlers[i](m_events[i].fd);
}
m_forced.clear();
}
}
void EventManager::force_signal(int fd)
{
m_forced.push_back(fd);
}
}

View File

@ -13,14 +13,19 @@ using EventHandler = std::function<void (int fd)>;
class EventManager : public Singleton<EventManager>
{
public:
EventManager();
void watch(int fd, EventHandler handler);
void unwatch(int fd);
void handle_next_events();
void force_signal(int fd);
private:
std::vector<pollfd> m_events;
std::vector<EventHandler> m_handlers;
std::vector<int> m_forced;
};
}