2019-04-10 18:01:54 +03:00
|
|
|
#include <LibCore/CObject.h>
|
2019-01-20 06:49:48 +03:00
|
|
|
#include "GEventLoop.h"
|
|
|
|
#include "GEvent.h"
|
2019-01-20 07:48:43 +03:00
|
|
|
#include "GWindow.h"
|
2019-03-03 14:32:15 +03:00
|
|
|
#include <LibGUI/GApplication.h>
|
2019-02-12 16:09:48 +03:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-04-10 18:35:43 +03:00
|
|
|
#include <LibCore/CNotifier.h>
|
2019-02-12 12:08:35 +03:00
|
|
|
#include <LibGUI/GMenu.h>
|
2019-04-03 18:22:14 +03:00
|
|
|
#include <LibGUI/GDesktop.h>
|
2019-04-20 22:56:56 +03:00
|
|
|
#include <LibGUI/GWidget.h>
|
2019-01-20 07:48:43 +03:00
|
|
|
#include <LibC/unistd.h>
|
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/fcntl.h>
|
|
|
|
#include <LibC/string.h>
|
2019-02-01 05:50:06 +03:00
|
|
|
#include <LibC/time.h>
|
2019-01-20 07:48:43 +03:00
|
|
|
#include <LibC/sys/select.h>
|
2019-02-14 19:18:35 +03:00
|
|
|
#include <LibC/sys/socket.h>
|
2019-03-27 03:31:53 +03:00
|
|
|
#include <LibC/sys/time.h>
|
2019-02-14 19:18:35 +03:00
|
|
|
#include <LibC/errno.h>
|
|
|
|
#include <LibC/string.h>
|
|
|
|
#include <LibC/stdlib.h>
|
2019-05-10 04:19:25 +03:00
|
|
|
#include <sys/uio.h>
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-01-20 09:03:38 +03:00
|
|
|
//#define GEVENTLOOP_DEBUG
|
2019-04-10 16:39:28 +03:00
|
|
|
//#define COALESCING_DEBUG
|
2019-01-20 09:03:38 +03:00
|
|
|
|
2019-05-14 18:12:09 +03:00
|
|
|
int GEventLoop::s_windowserver_fd = -1;
|
2019-05-03 02:38:24 +03:00
|
|
|
int GEventLoop::s_my_client_id = -1;
|
2019-03-09 19:34:09 +03:00
|
|
|
pid_t GEventLoop::s_server_pid = -1;
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-03-09 19:34:09 +03:00
|
|
|
void GEventLoop::connect_to_server()
|
2018-10-10 16:12:38 +03:00
|
|
|
{
|
2019-05-14 18:12:09 +03:00
|
|
|
ASSERT(s_windowserver_fd == -1);
|
|
|
|
s_windowserver_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
|
|
|
if (s_windowserver_fd < 0) {
|
2019-02-14 19:18:35 +03:00
|
|
|
perror("socket");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_un address;
|
|
|
|
address.sun_family = AF_LOCAL;
|
2019-02-21 17:45:31 +03:00
|
|
|
strcpy(address.sun_path, "/tmp/wsportal");
|
2019-02-14 19:43:20 +03:00
|
|
|
|
2019-02-17 02:13:47 +03:00
|
|
|
int retries = 1000;
|
2019-02-14 19:43:20 +03:00
|
|
|
int rc = 0;
|
|
|
|
while (retries) {
|
2019-05-14 18:12:09 +03:00
|
|
|
rc = connect(s_windowserver_fd, (const sockaddr*)&address, sizeof(address));
|
2019-02-14 19:43:20 +03:00
|
|
|
if (rc == 0)
|
|
|
|
break;
|
2019-03-01 12:51:58 +03:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
2019-02-14 19:18:35 +03:00
|
|
|
dbgprintf("connect failed: %d, %s\n", errno, strerror(errno));
|
2019-03-01 12:51:58 +03:00
|
|
|
#endif
|
2019-02-14 19:43:20 +03:00
|
|
|
sleep(1);
|
|
|
|
--retries;
|
|
|
|
}
|
|
|
|
if (rc < 0) {
|
2019-02-14 19:18:35 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-02-13 19:54:30 +03:00
|
|
|
}
|
2019-03-19 02:01:02 +03:00
|
|
|
|
|
|
|
WSAPI_ClientMessage request;
|
|
|
|
request.type = WSAPI_ClientMessage::Type::Greeting;
|
|
|
|
request.greeting.client_pid = getpid();
|
|
|
|
auto response = sync_request(request, WSAPI_ServerMessage::Type::Greeting);
|
2019-05-03 02:38:24 +03:00
|
|
|
handle_greeting(response);
|
2019-03-09 19:34:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GEventLoop::GEventLoop()
|
|
|
|
{
|
2019-04-10 18:30:34 +03:00
|
|
|
static bool connected = false;
|
|
|
|
if (!connected) {
|
2019-03-09 19:34:09 +03:00
|
|
|
connect_to_server();
|
2019-04-10 18:30:34 +03:00
|
|
|
connected = true;
|
2019-03-09 19:34:09 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 12:51:58 +03:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
2019-02-17 02:13:47 +03:00
|
|
|
dbgprintf("(%u) GEventLoop constructed :)\n", getpid());
|
2019-03-01 12:51:58 +03:00
|
|
|
#endif
|
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-04-22 02:15:47 +03:00
|
|
|
void GEventLoop::handle_paint_event(const WSAPI_ServerMessage& event, GWindow& window, const ByteBuffer& extra_data)
|
2019-01-20 07:48:43 +03:00
|
|
|
{
|
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-04-20 18:37:28 +03:00
|
|
|
Vector<Rect, 32> rects;
|
2019-04-22 02:15:47 +03:00
|
|
|
for (int i = 0; i < min(WSAPI_ServerMessage::max_inline_rect_count, event.rect_count); ++i)
|
2019-04-20 18:37:28 +03:00
|
|
|
rects.append(event.rects[i]);
|
2019-04-22 02:15:47 +03:00
|
|
|
if (event.extra_size) {
|
|
|
|
auto* extra_rects = reinterpret_cast<const WSAPI_Rect*>(extra_data.data());
|
|
|
|
for (int i = 0; i < event.rect_count - WSAPI_ServerMessage::max_inline_rect_count; ++i)
|
|
|
|
rects.append(extra_rects[i]);
|
|
|
|
}
|
2019-04-20 18:37:28 +03:00
|
|
|
post_event(window, make<GMultiPaintEvent>(rects, event.paint.window_size));
|
2019-01-20 07:48:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-20 17:34:55 +03:00
|
|
|
void GEventLoop::handle_resize_event(const WSAPI_ServerMessage& event, GWindow& window)
|
|
|
|
{
|
2019-02-26 02:51:49 +03:00
|
|
|
post_event(window, make<GResizeEvent>(event.window.old_rect.size, event.window.rect.size));
|
2019-02-20 17:34:55 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
void GEventLoop::handle_window_activation_event(const WSAPI_ServerMessage& event, GWindow& window)
|
2019-01-26 23:58:43 +03:00
|
|
|
{
|
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("WID=%x WindowActivation\n", event.window_id);
|
|
|
|
#endif
|
2019-02-26 02:51:49 +03:00
|
|
|
post_event(window, make<GEvent>(event.type == WSAPI_ServerMessage::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive));
|
2019-01-26 23:58:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
void GEventLoop::handle_window_close_request_event(const WSAPI_ServerMessage&, GWindow& window)
|
2019-02-05 12:31:37 +03:00
|
|
|
{
|
2019-02-26 02:51:49 +03:00
|
|
|
post_event(window, make<GEvent>(GEvent::WindowCloseRequest));
|
2019-02-05 12:31:37 +03:00
|
|
|
}
|
|
|
|
|
2019-02-20 12:12:19 +03:00
|
|
|
void GEventLoop::handle_window_entered_or_left_event(const WSAPI_ServerMessage& message, GWindow& window)
|
|
|
|
{
|
2019-02-26 02:51:49 +03:00
|
|
|
post_event(window, make<GEvent>(message.type == WSAPI_ServerMessage::Type::WindowEntered ? GEvent::WindowEntered : GEvent::WindowLeft));
|
2019-02-20 12:12:19 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
void GEventLoop::handle_key_event(const WSAPI_ServerMessage& event, GWindow& window)
|
2019-01-26 08:39:13 +03:00
|
|
|
{
|
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-03-03 14:32:15 +03:00
|
|
|
auto key_event = make<GKeyEvent>(event.type == WSAPI_ServerMessage::Type::KeyDown ? GEvent::KeyDown : GEvent::KeyUp, event.key.key, event.key.modifiers);
|
|
|
|
if (event.key.character != '\0')
|
|
|
|
key_event->m_text = String(&event.key.character, 1);
|
2019-03-02 12:04:49 +03:00
|
|
|
|
2019-03-08 03:59:07 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::Type::KeyDown) {
|
2019-04-20 22:56:56 +03:00
|
|
|
if (auto* focused_widget = window.focused_widget()) {
|
|
|
|
if (auto* action = focused_widget->action_for_key_event(*key_event)) {
|
|
|
|
if (action->is_enabled()) {
|
|
|
|
action->activate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:59:07 +03:00
|
|
|
if (auto* action = GApplication::the().action_for_key_event(*key_event)) {
|
2019-04-12 03:53:27 +03:00
|
|
|
if (action->is_enabled()) {
|
|
|
|
action->activate();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 03:59:07 +03:00
|
|
|
}
|
2019-03-02 12:04:49 +03:00
|
|
|
}
|
2019-02-26 02:51:49 +03:00
|
|
|
post_event(window, move(key_event));
|
2019-01-26 08:39:13 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
void GEventLoop::handle_mouse_event(const WSAPI_ServerMessage& event, GWindow& window)
|
2019-01-20 07:48:43 +03:00
|
|
|
{
|
2019-01-26 23:44:13 +03:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
2019-05-13 20:52:57 +03:00
|
|
|
dbgprintf("WID=%x MouseEvent %d,%d,%d\n", event.window_id, event.mouse.position.x, event.mouse.position.y, event.mouse.wheel_delta);
|
2019-01-26 23:44:13 +03:00
|
|
|
#endif
|
2019-01-20 07:48:43 +03:00
|
|
|
GMouseEvent::Type type;
|
|
|
|
switch (event.type) {
|
2019-02-15 11:17:18 +03:00
|
|
|
case WSAPI_ServerMessage::Type::MouseMove: type = GEvent::MouseMove; break;
|
|
|
|
case WSAPI_ServerMessage::Type::MouseUp: type = GEvent::MouseUp; break;
|
|
|
|
case WSAPI_ServerMessage::Type::MouseDown: type = GEvent::MouseDown; break;
|
2019-05-13 20:52:57 +03:00
|
|
|
case WSAPI_ServerMessage::Type::MouseWheel: type = GEvent::MouseWheel; break;
|
2019-01-20 07:48:43 +03:00
|
|
|
default: ASSERT_NOT_REACHED(); break;
|
|
|
|
}
|
|
|
|
GMouseButton button { GMouseButton::None };
|
|
|
|
switch (event.mouse.button) {
|
2019-02-15 11:17:18 +03:00
|
|
|
case WSAPI_MouseButton::NoButton: button = GMouseButton::None; break;
|
|
|
|
case WSAPI_MouseButton::Left: button = GMouseButton::Left; break;
|
|
|
|
case WSAPI_MouseButton::Right: button = GMouseButton::Right; break;
|
|
|
|
case WSAPI_MouseButton::Middle: button = GMouseButton::Middle; break;
|
2019-01-20 07:48:43 +03:00
|
|
|
default: ASSERT_NOT_REACHED(); break;
|
|
|
|
}
|
2019-05-13 20:52:57 +03:00
|
|
|
post_event(window, make<GMouseEvent>(type, event.mouse.position, event.mouse.buttons, button, event.mouse.modifiers, event.mouse.wheel_delta));
|
2019-01-11 01:19:29 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
void GEventLoop::handle_menu_event(const WSAPI_ServerMessage& event)
|
2019-02-12 12:08:35 +03:00
|
|
|
{
|
2019-02-15 11:17:18 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::Type::MenuItemActivated) {
|
2019-02-12 12:08:35 +03: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 16:09:48 +03:00
|
|
|
if (auto* action = menu->action_at(event.menu.identifier))
|
|
|
|
action->activate();
|
2019-02-12 12:08:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-04-04 02:44:35 +03:00
|
|
|
void GEventLoop::handle_wm_event(const WSAPI_ServerMessage& event, GWindow& window)
|
|
|
|
{
|
2019-04-04 17:23:23 +03:00
|
|
|
#ifdef GEVENTLOOP_DEBUG
|
|
|
|
dbgprintf("GEventLoop: handle_wm_event: %d\n", (int)event.type);
|
|
|
|
#endif
|
2019-04-04 02:44:35 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::WM_WindowStateChanged)
|
2019-04-18 01:39:11 +03:00
|
|
|
return post_event(window, make<GWMWindowStateChangedEvent>(event.wm.client_id, event.wm.window_id, String(event.text, event.text_length), event.wm.rect, event.wm.is_active, (GWindowType)event.wm.window_type, event.wm.is_minimized));
|
2019-04-20 15:40:38 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::WM_WindowRectChanged)
|
|
|
|
return post_event(window, make<GWMWindowRectChangedEvent>(event.wm.client_id, event.wm.window_id, event.wm.rect));
|
2019-04-18 01:39:11 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::WM_WindowIconChanged)
|
|
|
|
return post_event(window, make<GWMWindowIconChangedEvent>(event.wm.client_id, event.wm.window_id, String(event.text, event.text_length)));
|
2019-04-04 02:44:35 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::WM_WindowRemoved)
|
|
|
|
return post_event(window, make<GWMWindowRemovedEvent>(event.wm.client_id, event.wm.window_id));
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-04-22 02:15:47 +03:00
|
|
|
void GEventLoop::process_unprocessed_bundles()
|
2019-02-20 23:59:13 +03:00
|
|
|
{
|
2019-04-10 16:39:28 +03:00
|
|
|
int coalesced_paints = 0;
|
|
|
|
int coalesced_resizes = 0;
|
2019-04-22 02:15:47 +03:00
|
|
|
auto unprocessed_bundles = move(m_unprocessed_bundles);
|
2019-04-10 16:39:28 +03:00
|
|
|
|
|
|
|
HashMap<int, Size> latest_size_for_window_id;
|
2019-04-22 02:15:47 +03:00
|
|
|
for (auto& bundle : unprocessed_bundles) {
|
|
|
|
auto& event = bundle.message;
|
2019-04-10 16:39:28 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::Type::WindowResized) {
|
|
|
|
latest_size_for_window_id.set(event.window_id, event.window.rect.size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int paint_count = 0;
|
|
|
|
HashMap<int, Size> latest_paint_size_for_window_id;
|
2019-04-22 02:15:47 +03:00
|
|
|
for (auto& bundle : unprocessed_bundles) {
|
|
|
|
auto& event = bundle.message;
|
2019-04-10 16:39:28 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::Type::Paint) {
|
|
|
|
++paint_count;
|
|
|
|
#ifdef COALESCING_DEBUG
|
|
|
|
dbgprintf(" %s (window: %s)\n", Rect(event.paint.rect).to_string().characters(), Size(event.paint.window_size).to_string().characters());
|
|
|
|
#endif
|
|
|
|
latest_paint_size_for_window_id.set(event.window_id, event.paint.window_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef COALESCING_DEBUG
|
|
|
|
dbgprintf("paint_count: %d\n", paint_count);
|
|
|
|
#endif
|
|
|
|
|
2019-04-22 02:15:47 +03:00
|
|
|
for (auto& bundle : unprocessed_bundles) {
|
|
|
|
auto& event = bundle.message;
|
2019-02-20 23:59:13 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::Type::Greeting) {
|
2019-05-03 02:38:24 +03:00
|
|
|
handle_greeting(event);
|
2019-04-03 18:22:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.type == WSAPI_ServerMessage::Type::ScreenRectChanged) {
|
|
|
|
GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), event.screen.rect);
|
2019-02-20 23:59:13 +03:00
|
|
|
continue;
|
|
|
|
}
|
2019-02-14 11:32:34 +03:00
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
if (event.type == WSAPI_ServerMessage::Error) {
|
2019-02-14 11:32:34 +03:00
|
|
|
dbgprintf("GEventLoop got error message from server\n");
|
|
|
|
dbgprintf(" - error message: %s\n", String(event.text, event.text_length).characters());
|
2019-02-17 11:58:35 +03:00
|
|
|
quit(1);
|
2019-02-14 11:32:34 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-12 12:08:35 +03:00
|
|
|
switch (event.type) {
|
2019-02-15 11:17:18 +03:00
|
|
|
case WSAPI_ServerMessage::MenuItemActivated:
|
2019-02-12 12:08:35 +03:00
|
|
|
handle_menu_event(event);
|
|
|
|
continue;
|
2019-02-13 19:54:30 +03:00
|
|
|
default:
|
|
|
|
break;
|
2019-02-12 12:08:35 +03:00
|
|
|
}
|
|
|
|
|
2019-01-20 07:48:43 +03: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 09:03:38 +03:00
|
|
|
continue;
|
2019-01-20 07:48:43 +03:00
|
|
|
}
|
|
|
|
switch (event.type) {
|
2019-02-15 11:17:18 +03:00
|
|
|
case WSAPI_ServerMessage::Type::Paint:
|
2019-04-10 16:39:28 +03:00
|
|
|
if (Size(event.paint.window_size) != latest_paint_size_for_window_id.get(event.window_id)) {
|
|
|
|
++coalesced_paints;
|
|
|
|
break;
|
|
|
|
}
|
2019-04-22 02:15:47 +03:00
|
|
|
handle_paint_event(event, *window, bundle.extra_data);
|
2019-01-20 07:48:43 +03:00
|
|
|
break;
|
2019-02-15 11:17:18 +03:00
|
|
|
case WSAPI_ServerMessage::Type::MouseDown:
|
|
|
|
case WSAPI_ServerMessage::Type::MouseUp:
|
|
|
|
case WSAPI_ServerMessage::Type::MouseMove:
|
2019-05-13 20:52:57 +03:00
|
|
|
case WSAPI_ServerMessage::Type::MouseWheel:
|
2019-01-20 07:48:43 +03:00
|
|
|
handle_mouse_event(event, *window);
|
|
|
|
break;
|
2019-02-15 11:17:18 +03:00
|
|
|
case WSAPI_ServerMessage::Type::WindowActivated:
|
|
|
|
case WSAPI_ServerMessage::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-02-15 11:17:18 +03:00
|
|
|
case WSAPI_ServerMessage::Type::WindowCloseRequest:
|
2019-02-05 12:31:37 +03:00
|
|
|
handle_window_close_request_event(event, *window);
|
|
|
|
break;
|
2019-02-15 11:17:18 +03:00
|
|
|
case WSAPI_ServerMessage::Type::KeyDown:
|
|
|
|
case WSAPI_ServerMessage::Type::KeyUp:
|
2019-01-26 08:39:13 +03:00
|
|
|
handle_key_event(event, *window);
|
|
|
|
break;
|
2019-02-20 12:12:19 +03:00
|
|
|
case WSAPI_ServerMessage::Type::WindowEntered:
|
|
|
|
case WSAPI_ServerMessage::Type::WindowLeft:
|
|
|
|
handle_window_entered_or_left_event(event, *window);
|
|
|
|
break;
|
2019-02-20 17:34:55 +03:00
|
|
|
case WSAPI_ServerMessage::Type::WindowResized:
|
2019-04-10 16:39:28 +03:00
|
|
|
if (Size(event.window.rect.size) != latest_size_for_window_id.get(event.window_id)) {
|
|
|
|
++coalesced_resizes;
|
|
|
|
break;
|
|
|
|
}
|
2019-02-20 17:34:55 +03:00
|
|
|
handle_resize_event(event, *window);
|
|
|
|
break;
|
2019-04-04 02:44:35 +03:00
|
|
|
case WSAPI_ServerMessage::Type::WM_WindowRemoved:
|
|
|
|
case WSAPI_ServerMessage::Type::WM_WindowStateChanged:
|
2019-04-18 01:39:11 +03:00
|
|
|
case WSAPI_ServerMessage::Type::WM_WindowIconChanged:
|
2019-04-04 02:44:35 +03:00
|
|
|
handle_wm_event(event, *window);
|
|
|
|
break;
|
2019-02-12 12:08:35 +03:00
|
|
|
default:
|
|
|
|
break;
|
2019-01-20 07:48:43 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 23:59:13 +03:00
|
|
|
|
2019-04-10 16:39:28 +03:00
|
|
|
#ifdef COALESCING_DEBUG
|
|
|
|
if (coalesced_paints)
|
|
|
|
dbgprintf("Coalesced %d paints\n", coalesced_paints);
|
|
|
|
if (coalesced_resizes)
|
|
|
|
dbgprintf("Coalesced %d resizes\n", coalesced_resizes);
|
|
|
|
#endif
|
2018-10-10 16:12:38 +03:00
|
|
|
}
|
2019-02-01 05:50:06 +03:00
|
|
|
|
2019-02-13 19:59:38 +03:00
|
|
|
bool GEventLoop::drain_messages_from_server()
|
2019-02-13 19:54:30 +03:00
|
|
|
{
|
2019-03-20 03:09:35 +03:00
|
|
|
bool is_first_pass = true;
|
2019-02-13 19:54:30 +03:00
|
|
|
for (;;) {
|
2019-02-15 11:17:18 +03:00
|
|
|
WSAPI_ServerMessage message;
|
2019-05-14 18:12:09 +03:00
|
|
|
ssize_t nread = read(s_windowserver_fd, &message, sizeof(WSAPI_ServerMessage));
|
2019-02-13 19:54:30 +03:00
|
|
|
if (nread < 0) {
|
|
|
|
perror("read");
|
2019-02-17 11:58:35 +03:00
|
|
|
quit(1);
|
2019-02-13 19:54:30 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-20 03:09:35 +03:00
|
|
|
if (nread == 0) {
|
|
|
|
if (is_first_pass) {
|
|
|
|
fprintf(stderr, "EOF on WindowServer fd\n");
|
|
|
|
quit(1);
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-13 19:54:30 +03:00
|
|
|
return true;
|
2019-03-20 03:09:35 +03:00
|
|
|
}
|
2019-02-13 19:59:38 +03:00
|
|
|
assert(nread == sizeof(message));
|
2019-04-22 02:15:47 +03:00
|
|
|
ByteBuffer extra_data;
|
|
|
|
if (message.extra_size) {
|
|
|
|
extra_data = ByteBuffer::create_uninitialized(message.extra_size);
|
2019-05-01 19:25:24 +03:00
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
2019-05-14 18:12:09 +03:00
|
|
|
FD_SET(s_windowserver_fd, &rfds);
|
2019-05-01 19:25:24 +03:00
|
|
|
struct timeval timeout { 1, 0 };
|
2019-05-14 18:12:09 +03:00
|
|
|
int rc = select(s_windowserver_fd + 1, &rfds, nullptr, nullptr, &timeout);
|
2019-05-01 19:25:24 +03:00
|
|
|
ASSERT(rc == 1);
|
2019-05-14 18:12:09 +03:00
|
|
|
int extra_nread = read(s_windowserver_fd, extra_data.data(), extra_data.size());
|
2019-04-22 02:15:47 +03:00
|
|
|
ASSERT(extra_nread == message.extra_size);
|
|
|
|
}
|
|
|
|
m_unprocessed_bundles.append({ move(message), move(extra_data) });
|
2019-03-20 03:09:35 +03:00
|
|
|
is_first_pass = false;
|
2019-02-13 19:54:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 02:15:47 +03:00
|
|
|
bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data)
|
2019-02-13 19:54:30 +03:00
|
|
|
{
|
2019-04-22 02:15:47 +03:00
|
|
|
if (!extra_data.is_empty())
|
|
|
|
const_cast<WSAPI_ClientMessage&>(message).extra_size = extra_data.size();
|
|
|
|
|
2019-05-10 04:19:25 +03:00
|
|
|
struct iovec iov[2];
|
|
|
|
int iov_count = 1;
|
|
|
|
iov[0].iov_base = (void*)&message;
|
|
|
|
iov[0].iov_len = sizeof(message);
|
|
|
|
|
2019-04-22 02:15:47 +03:00
|
|
|
if (!extra_data.is_empty()) {
|
2019-05-10 04:19:25 +03:00
|
|
|
iov[1].iov_base = (void*)extra_data.data();
|
|
|
|
iov[1].iov_len = extra_data.size();
|
|
|
|
++iov_count;
|
2019-04-22 02:15:47 +03:00
|
|
|
}
|
|
|
|
|
2019-05-14 18:12:09 +03:00
|
|
|
int nwritten = writev(s_windowserver_fd, iov, iov_count);
|
2019-05-10 04:19:25 +03:00
|
|
|
ASSERT(nwritten == sizeof(message) + extra_data.size());
|
|
|
|
|
2019-04-22 02:15:47 +03:00
|
|
|
return true;
|
2019-02-13 19:54:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_ServerMessage& event)
|
2019-02-13 19:54:30 +03:00
|
|
|
{
|
|
|
|
for (;;) {
|
2019-02-14 12:06:41 +03:00
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
2019-05-14 18:12:09 +03:00
|
|
|
FD_SET(s_windowserver_fd, &rfds);
|
|
|
|
int rc = select(s_windowserver_fd + 1, &rfds, nullptr, nullptr, nullptr);
|
2019-05-03 22:07:16 +03:00
|
|
|
if (rc < 0) {
|
|
|
|
perror("select");
|
|
|
|
}
|
2019-02-14 12:06:41 +03:00
|
|
|
ASSERT(rc > 0);
|
2019-05-14 18:12:09 +03:00
|
|
|
ASSERT(FD_ISSET(s_windowserver_fd, &rfds));
|
2019-02-13 19:59:38 +03:00
|
|
|
bool success = drain_messages_from_server();
|
2019-02-13 19:54:30 +03:00
|
|
|
if (!success)
|
|
|
|
return false;
|
2019-04-22 02:15:47 +03:00
|
|
|
for (ssize_t i = 0; i < m_unprocessed_bundles.size(); ++i) {
|
|
|
|
if (m_unprocessed_bundles[i].message.type == type) {
|
|
|
|
event = move(m_unprocessed_bundles[i].message);
|
|
|
|
m_unprocessed_bundles.remove(i);
|
2019-02-13 19:54:30 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
WSAPI_ServerMessage GEventLoop::sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type)
|
2019-02-13 19:54:30 +03:00
|
|
|
{
|
|
|
|
bool success = post_message_to_server(request);
|
|
|
|
ASSERT(success);
|
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
WSAPI_ServerMessage response;
|
2019-03-19 02:01:02 +03:00
|
|
|
success = wait_for_specific_event(response_type, response);
|
2019-02-13 19:54:30 +03:00
|
|
|
ASSERT(success);
|
|
|
|
return response;
|
|
|
|
}
|
2019-05-03 02:38:24 +03:00
|
|
|
|
|
|
|
void GEventLoop::handle_greeting(WSAPI_ServerMessage& message)
|
|
|
|
{
|
|
|
|
s_server_pid = message.greeting.server_pid;
|
|
|
|
s_my_client_id = message.greeting.your_client_id;
|
|
|
|
GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), message.greeting.screen_rect);
|
|
|
|
}
|