1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:17:35 +00:00

WindowServer: Begin refactoring towards a fully asynchronous protocol.

In order to move the WindowServer to userspace, I have to eliminate its
dependence on system call facilities. The communication channel with each
client needs to be message-based in both directions.
This commit is contained in:
Andreas Kling 2019-02-13 17:54:30 +01:00
parent 96352ab735
commit 4f98a35beb
18 changed files with 242 additions and 91 deletions

View file

@ -12,6 +12,8 @@
#include <LibC/time.h>
#include <LibC/sys/select.h>
#include <LibC/gui.h>
#include <errno.h>
#include <string.h>
//#define GEVENTLOOP_DEBUG
@ -26,6 +28,12 @@ GEventLoop::GEventLoop()
{
if (!s_mainGEventLoop)
s_mainGEventLoop = this;
m_event_fd = open("/dev/gui_events", O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (m_event_fd < 0) {
perror("GEventLoop(): open");
exit(1);
}
}
GEventLoop::~GEventLoop()
@ -46,12 +54,6 @@ void GEventLoop::exit(int code)
int GEventLoop::exec()
{
m_event_fd = open("/dev/gui_events", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (m_event_fd < 0) {
perror("GEventLoop::exec(): open");
exit(1);
}
m_running = true;
for (;;) {
if (m_exit_requested)
@ -226,21 +228,17 @@ void GEventLoop::wait_for_event()
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));
bool success = drain_events_from_server();
ASSERT(success);
auto unprocessed_events = move(m_unprocessed_events);
for (auto& event : unprocessed_events) {
switch (event.type) {
case GUI_Event::MenuItemActivated:
handle_menu_event(event);
continue;
default:
break;
}
auto* window = GWindow::from_window_id(event.window_id);
@ -274,6 +272,23 @@ void GEventLoop::wait_for_event()
}
}
bool GEventLoop::drain_events_from_server()
{
for (;;) {
GUI_Event event;
ssize_t nread = read(m_event_fd, &event, sizeof(GUI_Event));
if (nread < 0) {
perror("read");
exit(1);
return false;
}
if (nread == 0)
return true;
assert(nread == sizeof(event));
m_unprocessed_events.append(move(event));
}
}
bool GEventLoop::EventLoopTimer::has_expired() const
{
timeval now;
@ -333,3 +348,36 @@ void GEventLoop::unregister_notifier(Badge<GNotifier>, GNotifier& notifier)
{
m_notifiers.remove(&notifier);
}
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);
}
bool GEventLoop::wait_for_specific_event(GUI_Event::Type type, GUI_Event& event)
{
for (;;) {
bool success = drain_events_from_server();
if (!success)
return false;
for (size_t i = 0; i < m_unprocessed_events.size(); ++i) {
if (m_unprocessed_events[i].type == type) {
event = move(m_unprocessed_events[i]);
m_unprocessed_events.remove(i);
return true;
}
}
}
}
GUI_Event GEventLoop::sync_request(const GUI_ClientMessage& request, GUI_Event::Type response_type)
{
bool success = post_message_to_server(request);
ASSERT(success);
GUI_Event response;
success = GEventLoop::main().wait_for_specific_event(response_type, response);
ASSERT(success);
return response;
}

View file

@ -5,11 +5,11 @@
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
#include <Kernel/GUITypes.h>
class GObject;
class GNotifier;
class GWindow;
struct GUI_Event;
class GEventLoop {
public:
@ -34,15 +34,20 @@ public:
void exit(int);
bool post_message_to_server(const GUI_ClientMessage&);
bool wait_for_specific_event(GUI_Event::Type, GUI_Event&);
GUI_Event sync_request(const GUI_ClientMessage& request, GUI_Event::Type response_type);
private:
void wait_for_event();
bool drain_events_from_server();
void handle_paint_event(const GUI_Event&, GWindow&);
void handle_mouse_event(const GUI_Event&, GWindow&);
void handle_key_event(const GUI_Event&, GWindow&);
void handle_window_activation_event(const GUI_Event&, GWindow&);
void handle_window_close_request_event(const GUI_Event&, GWindow&);
void handle_menu_event(const GUI_Event&);
void get_next_timer_expiration(timeval&);
struct QueuedEvent {
@ -51,6 +56,8 @@ private:
};
Vector<QueuedEvent> m_queued_events;
Vector<GUI_Event> m_unprocessed_events;
int m_event_fd { -1 };
bool m_running { false };
bool m_exit_requested { false };

View file

@ -1,4 +1,5 @@
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GEventLoop.h>
#include <LibC/gui.h>
GMenuBar::GMenuBar()
@ -7,10 +8,7 @@ GMenuBar::GMenuBar()
GMenuBar::~GMenuBar()
{
if (m_menubar_id) {
gui_menubar_destroy(m_menubar_id);
m_menubar_id = 0;
}
unrealize_menubar();
}
void GMenuBar::add_menu(OwnPtr<GMenu>&& menu)
@ -18,10 +16,29 @@ void GMenuBar::add_menu(OwnPtr<GMenu>&& menu)
m_menus.append(move(menu));
}
int GMenuBar::realize_menubar()
{
GUI_ClientMessage request;
request.type = GUI_ClientMessage::Type::CreateMenubar;
GUI_Event response = GEventLoop::main().sync_request(request, GUI_Event::Type::DidCreateMenubar);
return response.menu.menubar_id;
}
void GMenuBar::unrealize_menubar()
{
if (!m_menubar_id)
return;
GUI_ClientMessage request;
request.type = GUI_ClientMessage::Type::DestroyMenubar;
request.menu.menubar_id = m_menubar_id;
GEventLoop::main().sync_request(request, GUI_Event::Type::DidDestroyMenubar);
m_menubar_id = 0;
}
void GMenuBar::notify_added_to_application(Badge<GApplication>)
{
ASSERT(!m_menubar_id);
m_menubar_id = gui_menubar_create();
m_menubar_id = realize_menubar();
ASSERT(m_menubar_id > 0);
for (auto& menu : m_menus) {
ASSERT(menu);
@ -35,7 +52,5 @@ void GMenuBar::notify_added_to_application(Badge<GApplication>)
void GMenuBar::notify_removed_from_application(Badge<GApplication>)
{
ASSERT(m_menubar_id);
gui_menubar_destroy(m_menubar_id);
m_menubar_id = 0;
unrealize_menubar();
}

View file

@ -18,6 +18,9 @@ public:
void notify_removed_from_application(Badge<GApplication>);
private:
int realize_menubar();
void unrealize_menubar();
int m_menubar_id { 0 };
Vector<OwnPtr<GMenu>> m_menus;
};