2019-01-20 06:49:48 +03:00
|
|
|
#include "GEventLoop.h"
|
|
|
|
#include "GEvent.h"
|
|
|
|
#include "GObject.h"
|
2019-01-20 07:48:43 +03:00
|
|
|
#include "GWindow.h"
|
|
|
|
#include <LibC/unistd.h>
|
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/fcntl.h>
|
|
|
|
#include <LibC/string.h>
|
|
|
|
#include <LibC/sys/select.h>
|
|
|
|
#include <LibC/gui.h>
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-01-20 09:03:38 +03:00
|
|
|
//#define GEVENTLOOP_DEBUG
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
static GEventLoop* s_mainGEventLoop;
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
void GEventLoop::initialize()
|
2019-01-11 01:19:29 +03:00
|
|
|
{
|
2019-01-20 06:49:48 +03:00
|
|
|
s_mainGEventLoop = nullptr;
|
2019-01-11 01:19:29 +03:00
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GEventLoop::GEventLoop()
|
2018-10-10 16:12:38 +03:00
|
|
|
{
|
2019-01-20 06:49:48 +03:00
|
|
|
if (!s_mainGEventLoop)
|
|
|
|
s_mainGEventLoop = this;
|
2018-10-10 16:12:38 +03:00
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GEventLoop::~GEventLoop()
|
2018-10-10 16:12:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
GEventLoop& GEventLoop::main()
|
2018-10-10 16:12:38 +03:00
|
|
|
{
|
2019-01-20 06:49:48 +03:00
|
|
|
ASSERT(s_mainGEventLoop);
|
|
|
|
return *s_mainGEventLoop;
|
2018-10-10 16:12:38 +03:00
|
|
|
}
|
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
int GEventLoop::exec()
|
2018-10-10 16:12:38 +03:00
|
|
|
{
|
2019-01-21 03:33:16 +03:00
|
|
|
m_event_fd = open("/dev/gui_events", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
2019-01-20 07:48:43 +03:00
|
|
|
if (m_event_fd < 0) {
|
|
|
|
perror("GEventLoop::exec(): open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-01-13 03:59:38 +03:00
|
|
|
m_running = true;
|
2018-10-10 16:12:38 +03:00
|
|
|
for (;;) {
|
2019-01-20 07:48:43 +03:00
|
|
|
if (m_queued_events.is_empty())
|
|
|
|
wait_for_event();
|
2019-01-20 09:03:38 +03:00
|
|
|
Vector<QueuedEvent> events = move(m_queued_events);
|
|
|
|
for (auto& queued_event : events) {
|
|
|
|
auto* receiver = queued_event.receiver;
|
|
|
|
auto& event = *queued_event.event;
|
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("GEventLoop: GObject{%p} event %u (%s)\n", receiver, (unsigned)event.type(), event.name());
|
|
|
|
#endif
|
2018-10-10 16:12:38 +03:00
|
|
|
if (!receiver) {
|
|
|
|
switch (event.type()) {
|
2019-01-20 06:49:48 +03:00
|
|
|
case GEvent::Quit:
|
2019-01-13 03:59:38 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-10 16:12:38 +03:00
|
|
|
return 0;
|
|
|
|
default:
|
2019-01-20 01:49:56 +03:00
|
|
|
dbgprintf("event type %u with no receiver :(\n", event.type());
|
2019-01-13 03:59:38 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-10 16:12:38 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
receiver->event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 07:48:43 +03:00
|
|
|
void GEventLoop::post_event(GObject* receiver, OwnPtr<GEvent>&& event)
|
2018-10-10 16:12:38 +03:00
|
|
|
{
|
2019-01-20 09:03:38 +03:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("GEventLoop::post_event: {%u} << receiver=%p, event=%p\n", m_queued_events.size(), receiver, event.ptr());
|
|
|
|
#endif
|
2019-01-20 07:48:43 +03:00
|
|
|
m_queued_events.append({ receiver, move(event) });
|
|
|
|
}
|
|
|
|
|
|
|
|
void GEventLoop::handle_paint_event(const GUI_Event& event, GWindow& window)
|
|
|
|
{
|
2019-01-26 23:44:13 +03:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WID=%x Paint [%d,%d %dx%d]\n", event.window_id, event.paint.rect.location.x, event.paint.rect.location.y, event.paint.rect.size.width, event.paint.rect.size.height);
|
|
|
|
#endif
|
2019-01-20 07:48:43 +03:00
|
|
|
post_event(&window, make<GPaintEvent>(event.paint.rect));
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:58:43 +03:00
|
|
|
void GEventLoop::handle_window_activation_event(const GUI_Event& event, GWindow& window)
|
|
|
|
{
|
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WID=%x WindowActivation\n", event.window_id);
|
|
|
|
#endif
|
|
|
|
post_event(&window, make<GEvent>(event.type == GUI_Event::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
|
|
|
|
}
|
|
|
|
|
2019-01-26 08:39:13 +03:00
|
|
|
void GEventLoop::handle_key_event(const GUI_Event& event, GWindow& window)
|
|
|
|
{
|
2019-01-26 23:44:13 +03:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WID=%x KeyEvent character=0x%b\n", event.window_id, event.key.character);
|
|
|
|
#endif
|
2019-01-26 08:39:13 +03:00
|
|
|
auto key_event = make<GKeyEvent>(event.type == GUI_Event::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key);
|
|
|
|
key_event->m_alt = event.key.alt;
|
|
|
|
key_event->m_ctrl = event.key.ctrl;
|
|
|
|
key_event->m_shift = event.key.shift;
|
|
|
|
if (event.key.character != '\0')
|
|
|
|
key_event->m_text = String(&event.key.character, 1);
|
|
|
|
post_event(&window, move(key_event));
|
|
|
|
}
|
|
|
|
|
2019-01-20 07:48:43 +03:00
|
|
|
void GEventLoop::handle_mouse_event(const GUI_Event& event, GWindow& window)
|
|
|
|
{
|
2019-01-26 23:44:13 +03:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WID=%x MouseEvent %d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y);
|
|
|
|
#endif
|
2019-01-20 07:48:43 +03:00
|
|
|
GMouseEvent::Type type;
|
|
|
|
switch (event.type) {
|
|
|
|
case GUI_Event::Type::MouseMove: type = GEvent::MouseMove; break;
|
|
|
|
case GUI_Event::Type::MouseUp: type = GEvent::MouseUp; break;
|
|
|
|
case GUI_Event::Type::MouseDown: type = GEvent::MouseDown; break;
|
|
|
|
default: ASSERT_NOT_REACHED(); break;
|
|
|
|
}
|
|
|
|
GMouseButton button { GMouseButton::None };
|
|
|
|
switch (event.mouse.button) {
|
|
|
|
case GUI_MouseButton::NoButton: button = GMouseButton::None; break;
|
|
|
|
case GUI_MouseButton::Left: button = GMouseButton::Left; break;
|
|
|
|
case GUI_MouseButton::Right: button = GMouseButton::Right; break;
|
|
|
|
case GUI_MouseButton::Middle: button = GMouseButton::Middle; break;
|
|
|
|
default: ASSERT_NOT_REACHED(); break;
|
|
|
|
}
|
2019-01-20 09:03:38 +03:00
|
|
|
post_event(&window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button));
|
2019-01-11 01:19:29 +03:00
|
|
|
}
|
|
|
|
|
2019-01-20 07:48:43 +03:00
|
|
|
void GEventLoop::wait_for_event()
|
2019-01-11 01:19:29 +03:00
|
|
|
{
|
2019-01-20 07:48:43 +03:00
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(m_event_fd, &rfds);
|
2019-01-20 09:03:38 +03:00
|
|
|
struct timeval timeout = { 0, 0 };
|
|
|
|
int rc = select(m_event_fd + 1, &rfds, nullptr, nullptr, m_queued_events.is_empty() ? nullptr : &timeout);
|
2019-01-20 07:48:43 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FD_ISSET(m_event_fd, &rfds))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
GUI_Event event;
|
|
|
|
ssize_t nread = read(m_event_fd, &event, sizeof(GUI_Event));
|
|
|
|
if (nread < 0) {
|
|
|
|
perror("read");
|
|
|
|
exit(1); // FIXME: This should cause EventLoop::exec() to return 1.
|
|
|
|
}
|
|
|
|
if (nread == 0)
|
|
|
|
break;
|
|
|
|
assert(nread == sizeof(event));
|
|
|
|
auto* window = GWindow::from_window_id(event.window_id);
|
|
|
|
if (!window) {
|
|
|
|
dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
|
2019-01-20 09:03:38 +03:00
|
|
|
continue;
|
2019-01-20 07:48:43 +03:00
|
|
|
}
|
|
|
|
switch (event.type) {
|
|
|
|
case GUI_Event::Type::Paint:
|
|
|
|
handle_paint_event(event, *window);
|
|
|
|
break;
|
|
|
|
case GUI_Event::Type::MouseDown:
|
|
|
|
case GUI_Event::Type::MouseUp:
|
|
|
|
case GUI_Event::Type::MouseMove:
|
|
|
|
handle_mouse_event(event, *window);
|
|
|
|
break;
|
|
|
|
case GUI_Event::Type::WindowActivated:
|
|
|
|
case GUI_Event::Type::WindowDeactivated:
|
2019-01-26 23:58:43 +03:00
|
|
|
handle_window_activation_event(event, *window);
|
2019-01-20 07:48:43 +03:00
|
|
|
break;
|
2019-01-26 08:39:13 +03:00
|
|
|
case GUI_Event::Type::KeyDown:
|
|
|
|
case GUI_Event::Type::KeyUp:
|
|
|
|
handle_key_event(event, *window);
|
|
|
|
break;
|
2019-01-20 07:48:43 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 16:12:38 +03:00
|
|
|
}
|