2019-01-20 03:49:48 +00:00
|
|
|
#include "GEventLoop.h"
|
|
|
|
#include "GEvent.h"
|
|
|
|
#include "GObject.h"
|
2019-01-20 04:48:43 +00:00
|
|
|
#include "GWindow.h"
|
2019-02-12 13:09:48 +00:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-02-10 13:28:39 +00:00
|
|
|
#include <LibGUI/GNotifier.h>
|
2019-02-12 09:08:35 +00:00
|
|
|
#include <LibGUI/GMenu.h>
|
2019-01-20 04:48:43 +00:00
|
|
|
#include <LibC/unistd.h>
|
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/fcntl.h>
|
|
|
|
#include <LibC/string.h>
|
2019-02-01 02:50:06 +00:00
|
|
|
#include <LibC/time.h>
|
2019-01-20 04:48:43 +00:00
|
|
|
#include <LibC/sys/select.h>
|
2019-02-13 16:54:30 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2018-10-10 13:12:38 +00:00
|
|
|
|
2019-01-20 06:03:38 +00:00
|
|
|
//#define GEVENTLOOP_DEBUG
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
static GEventLoop* s_mainGEventLoop;
|
2018-10-10 13:12:38 +00:00
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
void GEventLoop::initialize()
|
2019-01-10 22:19:29 +00:00
|
|
|
{
|
2019-01-20 03:49:48 +00:00
|
|
|
s_mainGEventLoop = nullptr;
|
2019-01-10 22:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
GEventLoop::GEventLoop()
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
2019-01-20 03:49:48 +00:00
|
|
|
if (!s_mainGEventLoop)
|
|
|
|
s_mainGEventLoop = this;
|
2019-02-13 16:54:30 +00:00
|
|
|
|
|
|
|
m_event_fd = open("/dev/gui_events", O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
|
|
|
if (m_event_fd < 0) {
|
|
|
|
perror("GEventLoop(): open");
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-10-10 13:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
GEventLoop::~GEventLoop()
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
GEventLoop& GEventLoop::main()
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
2019-01-20 03:49:48 +00:00
|
|
|
ASSERT(s_mainGEventLoop);
|
|
|
|
return *s_mainGEventLoop;
|
2018-10-10 13:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 09:31:37 +00:00
|
|
|
void GEventLoop::exit(int code)
|
|
|
|
{
|
|
|
|
m_exit_requested = true;
|
|
|
|
m_exit_code = code;
|
|
|
|
}
|
|
|
|
|
2019-01-20 03:49:48 +00:00
|
|
|
int GEventLoop::exec()
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
2019-01-13 00:59:38 +00:00
|
|
|
m_running = true;
|
2018-10-10 13:12:38 +00:00
|
|
|
for (;;) {
|
2019-02-05 09:31:37 +00:00
|
|
|
if (m_exit_requested)
|
|
|
|
return m_exit_code;
|
2019-01-20 04:48:43 +00:00
|
|
|
if (m_queued_events.is_empty())
|
|
|
|
wait_for_event();
|
2019-01-20 06:03:38 +00: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
|
2019-02-10 13:28:39 +00:00
|
|
|
dbgprintf("GEventLoop: %s{%p} event %u\n", receiver->class_name(), receiver, (unsigned)event.type());
|
2019-01-20 06:03:38 +00:00
|
|
|
#endif
|
2018-10-10 13:12:38 +00:00
|
|
|
if (!receiver) {
|
|
|
|
switch (event.type()) {
|
2019-01-20 03:49:48 +00:00
|
|
|
case GEvent::Quit:
|
2019-01-13 00:59:38 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-10 13:12:38 +00:00
|
|
|
return 0;
|
|
|
|
default:
|
2019-01-19 22:49:56 +00:00
|
|
|
dbgprintf("event type %u with no receiver :(\n", event.type());
|
2019-01-13 00:59:38 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-10 13:12:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
receiver->event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 09:31:37 +00:00
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-10 13:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:48:43 +00:00
|
|
|
void GEventLoop::post_event(GObject* receiver, OwnPtr<GEvent>&& event)
|
2018-10-10 13:12:38 +00:00
|
|
|
{
|
2019-01-20 06:03:38 +00: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 04:48:43 +00:00
|
|
|
m_queued_events.append({ receiver, move(event) });
|
|
|
|
}
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
void GEventLoop::handle_paint_event(const GUI_ServerMessage& event, GWindow& window)
|
2019-01-20 04:48:43 +00:00
|
|
|
{
|
2019-01-26 20:44:13 +00: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 04:48:43 +00:00
|
|
|
post_event(&window, make<GPaintEvent>(event.paint.rect));
|
|
|
|
}
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
void GEventLoop::handle_window_activation_event(const GUI_ServerMessage& event, GWindow& window)
|
2019-01-26 20:58:43 +00:00
|
|
|
{
|
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WID=%x WindowActivation\n", event.window_id);
|
|
|
|
#endif
|
2019-02-13 16:59:38 +00:00
|
|
|
post_event(&window, make<GEvent>(event.type == GUI_ServerMessage::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
|
2019-01-26 20:58:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
void GEventLoop::handle_window_close_request_event(const GUI_ServerMessage&, GWindow& window)
|
2019-02-05 09:31:37 +00:00
|
|
|
{
|
|
|
|
post_event(&window, make<GEvent>(GEvent::WindowCloseRequest));
|
|
|
|
}
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
void GEventLoop::handle_key_event(const GUI_ServerMessage& event, GWindow& window)
|
2019-01-26 05:39:13 +00:00
|
|
|
{
|
2019-01-26 20:44:13 +00:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WID=%x KeyEvent character=0x%b\n", event.window_id, event.key.character);
|
|
|
|
#endif
|
2019-02-13 16:59:38 +00:00
|
|
|
auto key_event = make<GKeyEvent>(event.type == GUI_ServerMessage::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key);
|
2019-01-26 05:39:13 +00:00
|
|
|
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-02-13 16:59:38 +00:00
|
|
|
void GEventLoop::handle_mouse_event(const GUI_ServerMessage& event, GWindow& window)
|
2019-01-20 04:48:43 +00:00
|
|
|
{
|
2019-01-26 20:44:13 +00: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 04:48:43 +00:00
|
|
|
GMouseEvent::Type type;
|
|
|
|
switch (event.type) {
|
2019-02-13 16:59:38 +00:00
|
|
|
case GUI_ServerMessage::Type::MouseMove: type = GEvent::MouseMove; break;
|
|
|
|
case GUI_ServerMessage::Type::MouseUp: type = GEvent::MouseUp; break;
|
|
|
|
case GUI_ServerMessage::Type::MouseDown: type = GEvent::MouseDown; break;
|
2019-01-20 04:48:43 +00:00
|
|
|
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 06:03:38 +00:00
|
|
|
post_event(&window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button));
|
2019-01-10 22:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
void GEventLoop::handle_menu_event(const GUI_ServerMessage& event)
|
2019-02-12 09:08:35 +00:00
|
|
|
{
|
2019-02-13 16:59:38 +00:00
|
|
|
if (event.type == GUI_ServerMessage::Type::MenuItemActivated) {
|
2019-02-12 09:08:35 +00:00
|
|
|
auto* menu = GMenu::from_menu_id(event.menu.menu_id);
|
|
|
|
if (!menu) {
|
|
|
|
dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-12 13:09:48 +00:00
|
|
|
if (auto* action = menu->action_at(event.menu.identifier))
|
|
|
|
action->activate();
|
2019-02-12 09:08:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-01-20 04:48:43 +00:00
|
|
|
void GEventLoop::wait_for_event()
|
2019-01-10 22:19:29 +00:00
|
|
|
{
|
2019-01-20 04:48:43 +00:00
|
|
|
fd_set rfds;
|
2019-02-10 13:28:39 +00:00
|
|
|
fd_set wfds;
|
2019-01-20 04:48:43 +00:00
|
|
|
FD_ZERO(&rfds);
|
2019-02-10 13:28:39 +00:00
|
|
|
FD_ZERO(&wfds);
|
|
|
|
|
|
|
|
int max_fd = 0;
|
|
|
|
auto add_fd_to_set = [&max_fd] (int fd, fd_set& set){
|
|
|
|
FD_SET(fd, &set);
|
|
|
|
if (fd > max_fd)
|
|
|
|
max_fd = fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
add_fd_to_set(m_event_fd, rfds);
|
|
|
|
for (auto& notifier : m_notifiers) {
|
|
|
|
if (notifier->event_mask() & GNotifier::Read)
|
|
|
|
add_fd_to_set(notifier->fd(), rfds);
|
|
|
|
if (notifier->event_mask() & GNotifier::Write)
|
|
|
|
add_fd_to_set(notifier->fd(), wfds);
|
|
|
|
if (notifier->event_mask() & GNotifier::Exceptional)
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-01-20 06:03:38 +00:00
|
|
|
struct timeval timeout = { 0, 0 };
|
2019-02-01 02:50:06 +00:00
|
|
|
if (!m_timers.is_empty())
|
|
|
|
get_next_timer_expiration(timeout);
|
2019-02-13 16:07:10 +00:00
|
|
|
int rc = select(max_fd + 1, &rfds, &wfds, nullptr, (m_queued_events.is_empty() && m_timers.is_empty()) ? nullptr : &timeout);
|
2019-01-20 04:48:43 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:50:06 +00:00
|
|
|
for (auto& it : m_timers) {
|
|
|
|
auto& timer = *it.value;
|
|
|
|
if (!timer.has_expired())
|
|
|
|
continue;
|
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("GEventLoop: Timer %d has expired, sending GTimerEvent to %p\n", timer.timer_id, timer.owner);
|
|
|
|
#endif
|
|
|
|
post_event(timer.owner, make<GTimerEvent>(timer.timer_id));
|
|
|
|
if (timer.should_reload) {
|
|
|
|
timer.reload();
|
|
|
|
} else {
|
|
|
|
// FIXME: Support removing expired timers that don't want to reload.
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:28:39 +00:00
|
|
|
for (auto& notifier : m_notifiers) {
|
|
|
|
if (FD_ISSET(notifier->fd(), &rfds)) {
|
|
|
|
if (notifier->on_ready_to_read)
|
|
|
|
notifier->on_ready_to_read(*notifier);
|
|
|
|
}
|
|
|
|
if (FD_ISSET(notifier->fd(), &wfds)) {
|
|
|
|
if (notifier->on_ready_to_write)
|
|
|
|
notifier->on_ready_to_write(*notifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 04:48:43 +00:00
|
|
|
if (!FD_ISSET(m_event_fd, &rfds))
|
|
|
|
return;
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
bool success = drain_messages_from_server();
|
2019-02-13 16:54:30 +00:00
|
|
|
ASSERT(success);
|
2019-02-12 09:08:35 +00:00
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
auto unprocessed_events = move(m_unprocessed_messages);
|
2019-02-13 16:54:30 +00:00
|
|
|
for (auto& event : unprocessed_events) {
|
2019-02-14 08:32:34 +00:00
|
|
|
|
|
|
|
if (event.type == GUI_ServerMessage::Error) {
|
|
|
|
dbgprintf("GEventLoop got error message from server\n");
|
|
|
|
dbgprintf(" - error message: %s\n", String(event.text, event.text_length).characters());
|
|
|
|
exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-12 09:08:35 +00:00
|
|
|
switch (event.type) {
|
2019-02-13 16:59:38 +00:00
|
|
|
case GUI_ServerMessage::MenuItemActivated:
|
2019-02-12 09:08:35 +00:00
|
|
|
handle_menu_event(event);
|
|
|
|
continue;
|
2019-02-13 16:54:30 +00:00
|
|
|
default:
|
|
|
|
break;
|
2019-02-12 09:08:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 04:48:43 +00:00
|
|
|
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 06:03:38 +00:00
|
|
|
continue;
|
2019-01-20 04:48:43 +00:00
|
|
|
}
|
|
|
|
switch (event.type) {
|
2019-02-13 16:59:38 +00:00
|
|
|
case GUI_ServerMessage::Type::Paint:
|
2019-01-20 04:48:43 +00:00
|
|
|
handle_paint_event(event, *window);
|
|
|
|
break;
|
2019-02-13 16:59:38 +00:00
|
|
|
case GUI_ServerMessage::Type::MouseDown:
|
|
|
|
case GUI_ServerMessage::Type::MouseUp:
|
|
|
|
case GUI_ServerMessage::Type::MouseMove:
|
2019-01-20 04:48:43 +00:00
|
|
|
handle_mouse_event(event, *window);
|
|
|
|
break;
|
2019-02-13 16:59:38 +00:00
|
|
|
case GUI_ServerMessage::Type::WindowActivated:
|
|
|
|
case GUI_ServerMessage::Type::WindowDeactivated:
|
2019-01-26 20:58:43 +00:00
|
|
|
handle_window_activation_event(event, *window);
|
2019-01-20 04:48:43 +00:00
|
|
|
break;
|
2019-02-13 16:59:38 +00:00
|
|
|
case GUI_ServerMessage::Type::WindowCloseRequest:
|
2019-02-05 09:31:37 +00:00
|
|
|
handle_window_close_request_event(event, *window);
|
|
|
|
break;
|
2019-02-13 16:59:38 +00:00
|
|
|
case GUI_ServerMessage::Type::KeyDown:
|
|
|
|
case GUI_ServerMessage::Type::KeyUp:
|
2019-01-26 05:39:13 +00:00
|
|
|
handle_key_event(event, *window);
|
|
|
|
break;
|
2019-02-12 09:08:35 +00:00
|
|
|
default:
|
|
|
|
break;
|
2019-01-20 04:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 13:12:38 +00:00
|
|
|
}
|
2019-02-01 02:50:06 +00:00
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
bool GEventLoop::drain_messages_from_server()
|
2019-02-13 16:54:30 +00:00
|
|
|
{
|
|
|
|
for (;;) {
|
2019-02-13 16:59:38 +00:00
|
|
|
GUI_ServerMessage message;
|
|
|
|
ssize_t nread = read(m_event_fd, &message, sizeof(GUI_ServerMessage));
|
2019-02-13 16:54:30 +00:00
|
|
|
if (nread < 0) {
|
|
|
|
perror("read");
|
|
|
|
exit(1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nread == 0)
|
|
|
|
return true;
|
2019-02-13 16:59:38 +00:00
|
|
|
assert(nread == sizeof(message));
|
|
|
|
m_unprocessed_messages.append(move(message));
|
2019-02-13 16:54:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:50:06 +00:00
|
|
|
bool GEventLoop::EventLoopTimer::has_expired() const
|
|
|
|
{
|
|
|
|
timeval now;
|
|
|
|
gettimeofday(&now, nullptr);
|
|
|
|
return now.tv_sec > fire_time.tv_sec || (now.tv_sec == fire_time.tv_sec && now.tv_usec >= fire_time.tv_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GEventLoop::EventLoopTimer::reload()
|
|
|
|
{
|
|
|
|
gettimeofday(&fire_time, nullptr);
|
|
|
|
fire_time.tv_sec += interval / 1000;
|
|
|
|
fire_time.tv_usec += interval % 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GEventLoop::get_next_timer_expiration(timeval& soonest)
|
|
|
|
{
|
|
|
|
ASSERT(!m_timers.is_empty());
|
|
|
|
bool has_checked_any = false;
|
|
|
|
for (auto& it : m_timers) {
|
|
|
|
auto& fire_time = it.value->fire_time;
|
|
|
|
if (!has_checked_any || fire_time.tv_sec < soonest.tv_sec || (fire_time.tv_sec == soonest.tv_sec && fire_time.tv_usec < soonest.tv_usec))
|
|
|
|
soonest = fire_time;
|
|
|
|
has_checked_any = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int GEventLoop::register_timer(GObject& object, int milliseconds, bool should_reload)
|
|
|
|
{
|
|
|
|
ASSERT(milliseconds >= 0);
|
|
|
|
auto timer = make<EventLoopTimer>();
|
|
|
|
timer->owner = &object;
|
|
|
|
timer->interval = milliseconds;
|
|
|
|
timer->reload();
|
|
|
|
timer->should_reload = should_reload;
|
|
|
|
int timer_id = ++m_next_timer_id; // FIXME: This will eventually wrap around.
|
|
|
|
ASSERT(timer_id); // FIXME: Aforementioned wraparound.
|
|
|
|
timer->timer_id = timer_id;
|
|
|
|
m_timers.set(timer->timer_id, move(timer));
|
|
|
|
return timer_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GEventLoop::unregister_timer(int timer_id)
|
|
|
|
{
|
|
|
|
auto it = m_timers.find(timer_id);
|
|
|
|
if (it == m_timers.end())
|
|
|
|
return false;
|
|
|
|
m_timers.remove(it);
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-10 13:28:39 +00:00
|
|
|
|
|
|
|
void GEventLoop::register_notifier(Badge<GNotifier>, GNotifier& notifier)
|
|
|
|
{
|
|
|
|
m_notifiers.set(¬ifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GEventLoop::unregister_notifier(Badge<GNotifier>, GNotifier& notifier)
|
|
|
|
{
|
|
|
|
m_notifiers.remove(¬ifier);
|
|
|
|
}
|
2019-02-13 16:54:30 +00:00
|
|
|
|
|
|
|
bool GEventLoop::post_message_to_server(const GUI_ClientMessage& message)
|
|
|
|
{
|
|
|
|
int nwritten = write(m_event_fd, &message, sizeof(GUI_ClientMessage));
|
|
|
|
return nwritten == sizeof(GUI_ClientMessage);
|
|
|
|
}
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
bool GEventLoop::wait_for_specific_event(GUI_ServerMessage::Type type, GUI_ServerMessage& event)
|
2019-02-13 16:54:30 +00:00
|
|
|
{
|
|
|
|
for (;;) {
|
2019-02-13 16:59:38 +00:00
|
|
|
bool success = drain_messages_from_server();
|
2019-02-13 16:54:30 +00:00
|
|
|
if (!success)
|
|
|
|
return false;
|
2019-02-13 16:59:38 +00:00
|
|
|
for (size_t i = 0; i < m_unprocessed_messages.size(); ++i) {
|
|
|
|
if (m_unprocessed_messages[i].type == type) {
|
|
|
|
event = move(m_unprocessed_messages[i]);
|
|
|
|
m_unprocessed_messages.remove(i);
|
2019-02-13 16:54:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
GUI_ServerMessage GEventLoop::sync_request(const GUI_ClientMessage& request, GUI_ServerMessage::Type response_type)
|
2019-02-13 16:54:30 +00:00
|
|
|
{
|
|
|
|
bool success = post_message_to_server(request);
|
|
|
|
ASSERT(success);
|
|
|
|
|
2019-02-13 16:59:38 +00:00
|
|
|
GUI_ServerMessage response;
|
2019-02-13 16:54:30 +00:00
|
|
|
success = GEventLoop::main().wait_for_specific_event(response_type, response);
|
|
|
|
ASSERT(success);
|
|
|
|
return response;
|
|
|
|
}
|