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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#include "EventLoop.h"
#include "Event.h"
#include "Object.h"
#include "WindowManager.h"
#include "AbstractScreen.h"
#include "PS2MouseDevice.h"
#include "Scheduler.h"
static EventLoop* s_mainEventLoop;
void EventLoop::initialize()
{
s_mainEventLoop = nullptr;
}
EventLoop::EventLoop()
{
if (!s_mainEventLoop)
s_mainEventLoop = this;
}
EventLoop::~EventLoop()
{
}
EventLoop& EventLoop::main()
{
ASSERT(s_mainEventLoop);
return *s_mainEventLoop;
}
int EventLoop::exec()
{
m_server_process = current;
m_running = true;
for (;;) {
if (m_queuedEvents.is_empty())
waitForEvent();
Vector<QueuedEvent> events;
{
InterruptDisabler disabler;
events = 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:
ASSERT_NOT_REACHED();
return 0;
default:
printf("event type %u with no receiver :(\n", event.type());
ASSERT_NOT_REACHED();
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()
{
auto& mouse = PS2MouseDevice::the();
auto& screen = AbstractScreen::the();
bool prev_left_button = screen.left_mouse_button_pressed();
bool prev_right_button = screen.right_mouse_button_pressed();
int dx = 0;
int dy = 0;
while (mouse.can_read(*m_server_process)) {
signed_byte data[3];
ssize_t nread = mouse.read(*m_server_process, (byte*)data, 3);
ASSERT(nread == 3);
bool left_button = data[0] & 1;
bool right_button = data[0] & 2;
dx += data[1];
dy += -data[2];
if (left_button != prev_left_button || right_button != prev_right_button || !mouse.can_read(*m_server_process)) {
prev_left_button = left_button;
prev_right_button = right_button;
screen.on_receive_mouse_data(dx, dy, left_button, right_button);
dx = 0;
dy = 0;
}
}
}
|