summaryrefslogtreecommitdiff
path: root/Widgets/EventLoop.cpp
blob: c396c14903887a1749c559ed4b38032942ea1d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "EventLoop.h"
#include "Event.h"
#include "Object.h"

static EventLoop* s_mainEventLoop;

EventLoop::EventLoop()
{
    if (!s_mainEventLoop)
        s_mainEventLoop = this;
}

EventLoop::~EventLoop()
{
}

EventLoop& EventLoop::main()
{
    ASSERT(s_mainEventLoop);
    return *s_mainEventLoop;
}

int EventLoop::exec()
{
    for (;;) {
        if (m_queuedEvents.isEmpty())
            waitForEvent();
        auto events = std::move(m_queuedEvents);
        for (auto& queuedEvent : events) {
            auto* receiver = queuedEvent.receiver;
            auto& event = *queuedEvent.event;
            //printf("EventLoop: Object{%p} event %u (%s)\n", receiver, (unsigned)event.type(), event.name());
            if (!receiver) {
                switch (event.type()) {
                case Event::Quit:
                    return 0;
                default:
                    printf("event type %u with no receiver :(\n", event.type());
                    return 1;
                }
            } else {
                receiver->event(event);
            }
        }
    }
}

void EventLoop::postEvent(Object* receiver, OwnPtr<Event>&& event)
{
    m_queuedEvents.append({ receiver, std::move(event) });
}