2019-01-16 18:03:50 +03:00
|
|
|
#include "WSEventLoop.h"
|
|
|
|
#include "WSEvent.h"
|
|
|
|
#include "WSEventReceiver.h"
|
|
|
|
#include "WSWindowManager.h"
|
|
|
|
#include "WSScreen.h"
|
|
|
|
#include "PS2MouseDevice.h"
|
2019-01-16 19:20:58 +03:00
|
|
|
#include <Kernel/Keyboard.h>
|
|
|
|
#include <AK/Bitmap.h>
|
|
|
|
#include "Process.h"
|
2019-01-16 18:03:50 +03:00
|
|
|
|
|
|
|
//#define WSEVENTLOOP_DEBUG
|
|
|
|
|
|
|
|
static WSEventLoop* s_the;
|
|
|
|
|
|
|
|
void WSEventLoop::initialize()
|
|
|
|
{
|
|
|
|
s_the = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
WSEventLoop::WSEventLoop()
|
|
|
|
{
|
|
|
|
if (!s_the)
|
|
|
|
s_the = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WSEventLoop::~WSEventLoop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WSEventLoop& WSEventLoop::the()
|
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WSEventLoop::exec()
|
|
|
|
{
|
|
|
|
m_server_process = current;
|
2019-01-16 19:20:58 +03:00
|
|
|
|
|
|
|
m_keyboard_fd = m_server_process->sys$open("/dev/keyboard", O_RDONLY);
|
|
|
|
m_mouse_fd = m_server_process->sys$open("/dev/psaux", O_RDONLY);
|
|
|
|
|
|
|
|
ASSERT(m_keyboard_fd >= 0);
|
|
|
|
ASSERT(m_mouse_fd >= 0);
|
|
|
|
|
2019-01-16 18:03:50 +03:00
|
|
|
m_running = true;
|
|
|
|
for (;;) {
|
2019-01-16 19:20:58 +03:00
|
|
|
|
2019-01-16 18:03:50 +03:00
|
|
|
if (m_queued_events.is_empty())
|
2019-01-16 19:20:58 +03:00
|
|
|
wait_for_event();
|
|
|
|
|
2019-01-16 18:03:50 +03:00
|
|
|
Vector<QueuedEvent> events;
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
events = move(m_queued_events);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& queued_event : events) {
|
|
|
|
auto* receiver = queued_event.receiver;
|
|
|
|
auto& event = *queued_event.event;
|
|
|
|
#ifdef WSEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WSEventLoop: receiver{%p} event %u (%s)\n", receiver, (unsigned)event.type(), event.name());
|
|
|
|
#endif
|
|
|
|
if (!receiver) {
|
|
|
|
dbgprintf("WSEvent type %u with no receiver :(\n", event.type());
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
receiver->event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSEventLoop::post_event(WSEventReceiver* receiver, OwnPtr<WSEvent>&& event)
|
|
|
|
{
|
2019-01-17 04:42:52 +03:00
|
|
|
ASSERT_INTERRUPTS_ENABLED();
|
2019-01-16 18:03:50 +03:00
|
|
|
LOCKER(m_lock);
|
|
|
|
#ifdef WSEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WSEventLoop::post_event: {%u} << receiver=%p, event=%p\n", m_queued_events.size(), receiver, event.ptr());
|
|
|
|
#endif
|
2019-01-17 04:42:52 +03:00
|
|
|
|
|
|
|
if (event->type() == WSEvent::WM_Invalidate) {
|
|
|
|
for (auto& queued_event : m_queued_events) {
|
|
|
|
if (receiver == queued_event.receiver && queued_event.event->type() == WSEvent::WM_Invalidate) {
|
|
|
|
#ifdef WSEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("Swallow WM_Invalidate\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 18:03:50 +03:00
|
|
|
m_queued_events.append({ receiver, move(event) });
|
2019-01-16 19:20:58 +03:00
|
|
|
|
|
|
|
if (current != m_server_process)
|
|
|
|
m_server_process->request_wakeup();
|
2019-01-16 18:03:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 19:20:58 +03:00
|
|
|
void WSEventLoop::wait_for_event()
|
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
memset(&rfds, 0, sizeof(rfds));
|
|
|
|
auto bitmap = Bitmap::wrap((byte*)&rfds, FD_SETSIZE);
|
|
|
|
bitmap.set(m_keyboard_fd, true);
|
|
|
|
bitmap.set(m_mouse_fd, true);
|
|
|
|
Syscall::SC_select_params params;
|
|
|
|
params.nfds = max(m_keyboard_fd, m_mouse_fd) + 1;
|
|
|
|
params.readfds = &rfds;
|
|
|
|
params.writefds = nullptr;
|
|
|
|
params.exceptfds = nullptr;
|
|
|
|
params.timeout = nullptr;
|
|
|
|
int rc = m_server_process->sys$select(¶ms);
|
|
|
|
memory_barrier();
|
|
|
|
if (rc < 0) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-01-17 04:34:08 +03:00
|
|
|
if (bitmap.get(m_keyboard_fd))
|
2019-01-16 19:20:58 +03:00
|
|
|
drain_keyboard();
|
2019-01-17 04:34:08 +03:00
|
|
|
if (bitmap.get(m_mouse_fd))
|
2019-01-16 19:20:58 +03:00
|
|
|
drain_mouse();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSEventLoop::drain_mouse()
|
2019-01-16 18:03:50 +03:00
|
|
|
{
|
|
|
|
auto& screen = WSScreen::the();
|
2019-01-16 19:20:58 +03:00
|
|
|
auto& mouse = PS2MouseDevice::the();
|
2019-01-16 18:03:50 +03:00
|
|
|
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)) {
|
2019-01-17 04:32:40 +03:00
|
|
|
byte data[3];
|
2019-01-16 19:20:58 +03:00
|
|
|
ssize_t nread = mouse.read(*m_server_process, (byte*)data, sizeof(data));
|
|
|
|
ASSERT(nread == sizeof(data));
|
2019-01-16 18:03:50 +03:00
|
|
|
bool left_button = data[0] & 1;
|
|
|
|
bool right_button = data[0] & 2;
|
2019-01-17 04:32:40 +03:00
|
|
|
dx += data[1] ? (int)data[1] - (int)((data[0] << 4) & 0x100) : 0;
|
|
|
|
dy += data[2] ? (int)((data[0] << 3) & 0x100) - (int)data[2] : 0;
|
2019-01-16 18:03:50 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-16 19:20:58 +03:00
|
|
|
|
|
|
|
void WSEventLoop::drain_keyboard()
|
|
|
|
{
|
|
|
|
auto& screen = WSScreen::the();
|
|
|
|
auto& keyboard = Keyboard::the();
|
|
|
|
while (keyboard.can_read(*m_server_process)) {
|
|
|
|
byte data[2];
|
|
|
|
ssize_t nread = keyboard.read(*m_server_process, (byte*)data, sizeof(data));
|
|
|
|
ASSERT(nread == sizeof(data));
|
|
|
|
Keyboard::Key key;
|
|
|
|
key.character = data[0];
|
|
|
|
key.modifiers = data[1];
|
|
|
|
screen.on_receive_keyboard_data(key);
|
|
|
|
}
|
|
|
|
}
|