ladybird/LibGUI/GEventLoop.cpp

68 lines
1.6 KiB
C++
Raw Normal View History

#include "GEventLoop.h"
#include "GEvent.h"
#include "GObject.h"
2018-10-10 15:12:38 +02:00
static GEventLoop* s_mainGEventLoop;
2018-10-10 15:12:38 +02:00
void GEventLoop::initialize()
{
s_mainGEventLoop = nullptr;
}
GEventLoop::GEventLoop()
2018-10-10 15:12:38 +02:00
{
if (!s_mainGEventLoop)
s_mainGEventLoop = this;
2018-10-10 15:12:38 +02:00
}
GEventLoop::~GEventLoop()
2018-10-10 15:12:38 +02:00
{
}
GEventLoop& GEventLoop::main()
2018-10-10 15:12:38 +02:00
{
ASSERT(s_mainGEventLoop);
return *s_mainGEventLoop;
2018-10-10 15:12:38 +02:00
}
int GEventLoop::exec()
2018-10-10 15:12:38 +02:00
{
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;
//printf("GEventLoop: GObject{%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 GEvent::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 GEventLoop::postEvent(GObject* receiver, OwnPtr<GEvent>&& event)
2018-10-10 15:12:38 +02:00
{
//printf("GEventLoop::postGEvent: {%u} << receiver=%p, event=%p\n", m_queuedEvents.size(), receiver, event.ptr());
m_queuedEvents.append({ receiver, move(event) });
}
void GEventLoop::waitForEvent()
{
2018-10-10 15:12:38 +02:00
}