summaryrefslogtreecommitdiff
path: root/Servers
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-04-22 01:15:47 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-04-22 01:15:47 +0200
commit9f122bff5a2082df40b8e7c839df86f07fbebda6 (patch)
tree00b10c2b6f29660108a5afa76a1f3c0409ce2323 /Servers
parentf9d3abf5d041f574304d2e585c5cf51448a77063 (diff)
downloadserenity-9f122bff5a2082df40b8e7c839df86f07fbebda6.zip
WindowServer+LibGUI: Allow arbitrary number of rects in messages.
To get truly atomic updates, add a mechanism for passing arbitrary amounts of extra data along with WindowServer messages. This allows us to pass all the rects in a single message.
Diffstat (limited to 'Servers')
-rw-r--r--Servers/WindowServer/WSAPITypes.h7
-rw-r--r--Servers/WindowServer/WSClientConnection.cpp47
-rw-r--r--Servers/WindowServer/WSClientConnection.h4
-rw-r--r--Servers/WindowServer/WSEventLoop.cpp54
-rw-r--r--Servers/WindowServer/WSEventLoop.h3
-rw-r--r--Servers/WindowServer/WSMenu.cpp2
-rw-r--r--Servers/WindowServer/WSWindowFrame.cpp4
-rw-r--r--Servers/WindowServer/WSWindowManager.cpp4
8 files changed, 99 insertions, 26 deletions
diff --git a/Servers/WindowServer/WSAPITypes.h b/Servers/WindowServer/WSAPITypes.h
index 8d866a5846..e874ab4474 100644
--- a/Servers/WindowServer/WSAPITypes.h
+++ b/Servers/WindowServer/WSAPITypes.h
@@ -109,12 +109,14 @@ struct WSAPI_ServerMessage {
};
Type type { Invalid };
int window_id { -1 };
+ unsigned extra_size { 0 };
union {
int text_length { 0 };
int rect_count;
};
+ static const int max_inline_rect_count = 1;
union {
char text[512];
WSAPI_Rect rects[32];
@@ -214,13 +216,16 @@ struct WSAPI_ClientMessage {
};
Type type { Invalid };
int window_id { -1 };
+ unsigned extra_size { 0 };
union {
int text_length { 0 };
int rect_count;
};
+
+ static const int max_inline_rect_count = 1;
union {
char text[512];
- WSAPI_Rect rects[32];
+ WSAPI_Rect rects[max_inline_rect_count];
};
int value { 0 };
diff --git a/Servers/WindowServer/WSClientConnection.cpp b/Servers/WindowServer/WSClientConnection.cpp
index 37ced67017..22cb858356 100644
--- a/Servers/WindowServer/WSClientConnection.cpp
+++ b/Servers/WindowServer/WSClientConnection.cpp
@@ -70,8 +70,11 @@ void WSClientConnection::post_error(const String& error_message)
post_message(message);
}
-void WSClientConnection::post_message(const WSAPI_ServerMessage& message)
+void WSClientConnection::post_message(const WSAPI_ServerMessage& message, const ByteBuffer& extra_data)
{
+ if (!extra_data.is_empty())
+ const_cast<WSAPI_ServerMessage&>(message).extra_size = extra_data.size();
+
int nwritten = write(m_fd, &message, sizeof(message));
if (nwritten < 0) {
if (errno == EPIPE) {
@@ -83,6 +86,20 @@ void WSClientConnection::post_message(const WSAPI_ServerMessage& message)
}
ASSERT(nwritten == sizeof(message));
+
+ if (!extra_data.is_empty()) {
+ nwritten = write(m_fd, extra_data.data(), extra_data.size());
+ if (nwritten < 0) {
+ if (errno == EPIPE) {
+ dbgprintf("WSClientConnection::post_message: Disconnected from peer during extra_data write.\n");
+ return;
+ }
+ perror("WSClientConnection::post_message write (extra_data)");
+ ASSERT_NOT_REACHED();
+ }
+
+ ASSERT(nwritten == extra_data.size());
+ }
}
void WSClientConnection::notify_about_new_screen_rect(const Rect& rect)
@@ -93,19 +110,28 @@ void WSClientConnection::notify_about_new_screen_rect(const Rect& rect)
post_message(message);
}
-void WSClientConnection::event(CEvent& message)
+void WSClientConnection::event(CEvent& event)
{
- if (static_cast<WSEvent&>(message).is_client_request()) {
- on_request(static_cast<const WSAPIClientRequest&>(message));
+ if (static_cast<WSEvent&>(event).is_client_request()) {
+ on_request(static_cast<const WSAPIClientRequest&>(event));
return;
}
- if (message.type() == WSEvent::WM_ClientDisconnected) {
- int client_id = static_cast<const WSClientDisconnectedNotification&>(message).client_id();
+ if (event.type() == WSEvent::WM_ClientDisconnected) {
+ int client_id = static_cast<const WSClientDisconnectedNotification&>(event).client_id();
dbgprintf("WSClientConnection: Client disconnected: %d\n", client_id);
delete this;
return;
}
+
+ CObject::event(event);
+}
+
+void WSClientConnection::did_misbehave()
+{
+ dbgprintf("WSClientConnection{%p} (id=%d, pid=%d) misbehaved, disconnecting.\n", this, m_client_id, m_pid);
+ // FIXME: We should make sure we avoid processing any further messages from this client.
+ delete_later();
}
void WSClientConnection::handle_request(const WSAPICreateMenubarRequest&)
@@ -484,13 +510,14 @@ void WSClientConnection::post_paint_message(WSWindow& window)
message.window_id = window.window_id();
auto rect_set = window.take_pending_paint_rects();
auto& rects = rect_set.rects();
- // FIXME: Break it into multiple batches if needed.
- ASSERT(rects.size() <= 32);
message.rect_count = rects.size();
- for (int i = 0; i < rects.size(); ++i)
+ for (int i = 0; i < min(WSAPI_ServerMessage::max_inline_rect_count, rects.size()); ++i)
message.rects[i] = rects[i];
message.paint.window_size = window.size();
- post_message(message);
+ ByteBuffer extra_data;
+ if (rects.size() > WSAPI_ServerMessage::max_inline_rect_count)
+ extra_data = ByteBuffer::wrap((void*)&rects[WSAPI_ServerMessage::max_inline_rect_count], (rects.size() - WSAPI_ServerMessage::max_inline_rect_count) * sizeof(WSAPI_Rect));
+ post_message(message, extra_data);
}
void WSClientConnection::handle_request(const WSAPIInvalidateRectRequest& request)
diff --git a/Servers/WindowServer/WSClientConnection.h b/Servers/WindowServer/WSClientConnection.h
index 5410f7d083..eea8ac43c4 100644
--- a/Servers/WindowServer/WSClientConnection.h
+++ b/Servers/WindowServer/WSClientConnection.h
@@ -21,7 +21,7 @@ public:
static WSClientConnection* from_client_id(int client_id);
static void for_each_client(Function<void(WSClientConnection&)>);
- void post_message(const WSAPI_ServerMessage&);
+ void post_message(const WSAPI_ServerMessage&, const ByteBuffer& = { });
int client_id() const { return m_client_id; }
WSMenuBar* app_menubar() { return m_app_menubar.ptr(); }
@@ -39,6 +39,8 @@ public:
void notify_about_new_screen_rect(const Rect&);
void post_paint_message(WSWindow&);
+ void did_misbehave();
+
private:
virtual void event(CEvent&) override;
diff --git a/Servers/WindowServer/WSEventLoop.cpp b/Servers/WindowServer/WSEventLoop.cpp
index 4a5340f1c4..3e80ef08df 100644
--- a/Servers/WindowServer/WSEventLoop.cpp
+++ b/Servers/WindowServer/WSEventLoop.cpp
@@ -115,7 +115,23 @@ static WSWindowType from_api(WSAPI_WindowType api_type)
}
}
-void WSEventLoop::on_receive_from_client(int client_id, const WSAPI_ClientMessage& message)
+static Vector<Rect, 32> get_rects(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data)
+{
+ Vector<Rect, 32> rects;
+ if (message.rect_count > (WSAPI_ClientMessage::max_inline_rect_count + extra_data.size() / sizeof(WSAPI_Rect))) {
+ return { };
+ }
+ for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, message.rect_count); ++i)
+ rects.append(message.rects[i]);
+ if (!extra_data.is_empty()) {
+ auto* extra_rects = reinterpret_cast<const WSAPI_Rect*>(extra_data.data());
+ for (int i = 0; i < (extra_data.size() / sizeof(WSAPI_Rect)); ++i)
+ rects.append(extra_rects[i]);
+ }
+ return rects;
+}
+
+bool WSEventLoop::on_receive_from_client(int client_id, const WSAPI_ClientMessage& message, ByteBuffer&& extra_data)
{
WSClientConnection& client = *WSClientConnection::from_client_id(client_id);
switch (message.type) {
@@ -192,18 +208,20 @@ void WSEventLoop::on_receive_from_client(int client_id, const WSAPI_ClientMessag
post_event(client, make<WSAPIGetClipboardContentsRequest>(client_id));
break;
case WSAPI_ClientMessage::Type::InvalidateRect: {
- Vector<Rect, 32> rects;
- ASSERT(message.rect_count <= 32);
- for (int i = 0; i < message.rect_count; ++i)
- rects.append(message.rects[i]);
+ auto rects = get_rects(message, extra_data);
+ if (rects.is_empty()) {
+ client.did_misbehave();
+ return false;
+ }
post_event(client, make<WSAPIInvalidateRectRequest>(client_id, message.window_id, rects));
break;
}
case WSAPI_ClientMessage::Type::DidFinishPainting: {
- Vector<Rect, 32> rects;
- ASSERT(message.rect_count <= 32);
- for (int i = 0; i < message.rect_count; ++i)
- rects.append(message.rects[i]);
+ auto rects = get_rects(message, extra_data);
+ if (rects.is_empty()) {
+ client.did_misbehave();
+ return false;
+ }
post_event(client, make<WSAPIDidFinishPaintingNotification>(client_id, message.window_id, rects));
break;
}
@@ -232,6 +250,7 @@ void WSEventLoop::on_receive_from_client(int client_id, const WSAPI_ClientMessag
default:
break;
}
+ return true;
}
void WSEventLoop::add_file_descriptors_for_select(fd_set& fds, int& max_fd_added)
@@ -279,7 +298,22 @@ void WSEventLoop::drain_client(WSClientConnection& client)
perror("read");
ASSERT_NOT_REACHED();
}
- on_receive_from_client(client.client_id(), message);
+ ByteBuffer extra_data;
+ if (message.extra_size) {
+ if (message.extra_size >= 32768) {
+ dbgprintf("message.extra_size is way too large\n");
+ return client.did_misbehave();
+ }
+
+ extra_data = ByteBuffer::create_uninitialized(message.extra_size);
+ int extra_nread = read(client.fd(), extra_data.data(), extra_data.size());
+ if (extra_nread != message.extra_size) {
+ dbgprintf("extra_nread(%d) != extra_size(%d)\n", extra_nread, extra_data.size());
+ return client.did_misbehave();
+ }
+ }
+ if (!on_receive_from_client(client.client_id(), message, move(extra_data)))
+ return;
++messages_received;
}
}
diff --git a/Servers/WindowServer/WSEventLoop.h b/Servers/WindowServer/WSEventLoop.h
index 68f28c0f38..a45a9eb9d4 100644
--- a/Servers/WindowServer/WSEventLoop.h
+++ b/Servers/WindowServer/WSEventLoop.h
@@ -1,6 +1,7 @@
#pragma once
#include <LibCore/CEventLoop.h>
+#include <AK/ByteBuffer.h>
class WSClientConnection;
struct WSAPI_ClientMessage;
@@ -20,7 +21,7 @@ private:
void drain_mouse();
void drain_keyboard();
void drain_client(WSClientConnection&);
- void on_receive_from_client(int client_id, const WSAPI_ClientMessage&);
+ bool on_receive_from_client(int client_id, const WSAPI_ClientMessage&, ByteBuffer&& extra_data);
int m_keyboard_fd { -1 };
int m_mouse_fd { -1 };
diff --git a/Servers/WindowServer/WSMenu.cpp b/Servers/WindowServer/WSMenu.cpp
index 017d259bed..9c02de1b17 100644
--- a/Servers/WindowServer/WSMenu.cpp
+++ b/Servers/WindowServer/WSMenu.cpp
@@ -134,6 +134,8 @@ void WSMenu::event(CEvent& event)
clear_hovered_item();
return;
}
+
+ CObject::event(event);
}
void WSMenu::clear_hovered_item()
diff --git a/Servers/WindowServer/WSWindowFrame.cpp b/Servers/WindowServer/WSWindowFrame.cpp
index 98d97fdde1..3b1d304cdb 100644
--- a/Servers/WindowServer/WSWindowFrame.cpp
+++ b/Servers/WindowServer/WSWindowFrame.cpp
@@ -239,7 +239,7 @@ void WSWindowFrame::on_mouse_event(const WSMouseEvent& event)
return;
}
- if (event.type() == WSEvent::MouseMove && event.buttons() == 0) {
+ if (m_window.is_resizable() && event.type() == WSEvent::MouseMove && event.buttons() == 0) {
constexpr ResizeDirection direction_for_hot_area[3][3] = {
{ ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
{ ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
@@ -256,6 +256,6 @@ void WSWindowFrame::on_mouse_event(const WSMouseEvent& event)
return;
}
- if (event.button() == MouseButton::Left)
+ if (m_window.is_resizable() && event.button() == MouseButton::Left)
wm.start_window_resize(m_window, event.translated(rect().location()));
}
diff --git a/Servers/WindowServer/WSWindowManager.cpp b/Servers/WindowServer/WSWindowManager.cpp
index 6bbe3d8cb2..716ad2d2b7 100644
--- a/Servers/WindowServer/WSWindowManager.cpp
+++ b/Servers/WindowServer/WSWindowManager.cpp
@@ -681,7 +681,7 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
start_window_drag(window, event);
return IterationDecision::Abort;
}
- if (m_keyboard_modifiers == Mod_Logo && event.type() == WSEvent::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
+ if (window.is_resizable() && m_keyboard_modifiers == Mod_Logo && event.type() == WSEvent::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
start_window_resize(window, event);
return IterationDecision::Abort;
}
@@ -983,6 +983,8 @@ void WSWindowManager::event(CEvent& event)
compose();
return;
}
+
+ CObject::event(event);
}
void WSWindowManager::set_highlight_window(WSWindow* window)