ladybird/LibGUI/EventLoop.cpp

68 lines
1.6 KiB
C++
Raw Normal View History

2018-10-10 15:12:38 +02:00
#include "EventLoop.h"
#include "Event.h"
#include "Object.h"
static EventLoop* s_mainEventLoop;
void EventLoop::initialize()
{
s_mainEventLoop = nullptr;
}
2018-10-10 15:12:38 +02:00
EventLoop::EventLoop()
{
if (!s_mainEventLoop)
s_mainEventLoop = this;
}
EventLoop::~EventLoop()
{
}
EventLoop& EventLoop::main()
{
ASSERT(s_mainEventLoop);
return *s_mainEventLoop;
}
int EventLoop::exec()
{
2019-01-13 01:59:38 +01:00
m_running = true;
2018-10-10 15:12:38 +02:00
for (;;) {
2018-12-21 02:10:45 +01:00
if (m_queuedEvents.is_empty())
2018-10-10 15:12:38 +02:00
waitForEvent();
Vector<QueuedEvent> events;
{
events = move(m_queuedEvents);
}
2018-10-10 15:12:38 +02:00
for (auto& queuedEvent : events) {
auto* receiver = queuedEvent.receiver;
auto& event = *queuedEvent.event;
2018-10-11 23:54:34 +02:00
//printf("EventLoop: Object{%p} event %u (%s)\n", receiver, (unsigned)event.type(), event.name());
2018-10-10 15:12:38 +02:00
if (!receiver) {
switch (event.type()) {
case Event::Quit:
2019-01-13 01:59:38 +01:00
ASSERT_NOT_REACHED();
2018-10-10 15:12:38 +02:00
return 0;
default:
2019-01-19 23:49:56 +01:00
dbgprintf("event type %u with no receiver :(\n", event.type());
2019-01-13 01:59:38 +01:00
ASSERT_NOT_REACHED();
2018-10-10 15:12:38 +02:00
return 1;
}
} else {
receiver->event(event);
}
}
}
}
void EventLoop::postEvent(Object* receiver, OwnPtr<Event>&& event)
{
//printf("EventLoop::postEvent: {%u} << receiver=%p, event=%p\n", m_queuedEvents.size(), receiver, event.ptr());
m_queuedEvents.append({ receiver, move(event) });
}
void EventLoop::waitForEvent()
{
2018-10-10 15:12:38 +02:00
}