瀏覽代碼

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.
Andreas Kling 6 年之前
父節點
當前提交
4f98a35beb

+ 3 - 2
Kernel/GUIEventDevice.cpp

@@ -2,6 +2,7 @@
 #include <Kernel/Process.h>
 #include <AK/Lock.h>
 #include <LibC/errno_numbers.h>
+#include <WindowServer/WSMessageLoop.h>
 
 //#define GUIEVENTDEVICE_DEBUG
 
@@ -32,7 +33,7 @@ ssize_t GUIEventDevice::read(Process& process, byte* buffer, size_t size)
     return size;
 }
 
-ssize_t GUIEventDevice::write(Process&, const byte*, size_t)
+ssize_t GUIEventDevice::write(Process& process, const byte* data, size_t size)
 {
-    return -EINVAL;
+    return WSMessageLoop::the().on_receive_from_client(process.gui_client_id(), data, size);
 }

+ 1 - 0
Kernel/GUIEventDevice.h

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <Kernel/CharacterDevice.h>
+#include <Kernel/DoubleBuffer.h>
 
 class GUIEventDevice final : public CharacterDevice {
 public:

+ 20 - 0
Kernel/GUITypes.h

@@ -68,6 +68,8 @@ struct GUI_Event {
         WindowDeactivated,
         WindowCloseRequest,
         MenuItemActivated,
+        DidCreateMenubar,
+        DidDestroyMenubar,
     };
     Type type { Invalid };
     int window_id { -1 };
@@ -90,12 +92,30 @@ struct GUI_Event {
             bool shift : 1;
         } key;
         struct {
+            int menubar_id;
             int menu_id;
             unsigned identifier;
         } menu;
     };
 };
 
+struct GUI_ClientMessage {
+    enum Type : unsigned {
+        Invalid,
+        CreateMenubar,
+        DestroyMenubar,
+    };
+    Type type { Invalid };
+    int window_id { -1 };
+
+    union {
+        struct {
+            int menubar_id;
+            int menu_id;
+        } menu;
+    };
+};
+
 inline Rect::Rect(const GUI_Rect& r) : Rect(r.location, r.size) { }
 inline Point::Point(const GUI_Point& p) : Point(p.x, p.y) { }
 inline Size::Size(const GUI_Size& s) : Size(s.width, s.height) { }

+ 2 - 3
Kernel/Process.h

@@ -230,8 +230,6 @@ public:
     int gui$get_window_rect(int window_id, GUI_Rect*);
     int gui$set_window_rect(int window_id, const GUI_Rect*);
     int gui$set_global_cursor_tracking_enabled(int window_id, bool enabled);
-    int gui$menubar_create();
-    int gui$menubar_destroy(int menubar_id);
     int gui$menubar_add_menu(int menubar_id, int menu_id);
     int gui$menu_create(const char* name);
     int gui$menu_destroy(int menu_id);
@@ -313,6 +311,8 @@ public:
     bool has_used_fpu() const { return m_has_used_fpu; }
     void set_has_used_fpu(bool b) { m_has_used_fpu = b; }
 
+    int gui_client_id() const { return (int)this; }
+
 private:
     friend class MemoryManager;
     friend class Scheduler;
@@ -420,7 +420,6 @@ private:
     Vector<GUI_Event> m_gui_events;
     Lock m_gui_events_lock;
     int m_next_window_id { 1 };
-    bool m_has_created_menus { false };
 
     dword m_wakeup_requested { false };
     bool m_has_used_fpu { false };

+ 1 - 13
Kernel/ProcessGUI.cpp

@@ -259,7 +259,7 @@ int Process::gui$set_global_cursor_tracking_enabled(int window_id, bool enabled)
 
 void Process::destroy_all_menus()
 {
-    if (!m_has_created_menus)
+    if (!WSMessageLoop::the().running())
         return;
     WSWindowManager::the().destroy_all_menus(*this);
 }
@@ -292,17 +292,6 @@ DisplayInfo Process::set_video_resolution(int width, int height)
     return info;
 }
 
-int Process::gui$menubar_create()
-{
-    m_has_created_menus = true;
-    return WSWindowManager::the().api$menubar_create();
-}
-
-int Process::gui$menubar_destroy(int menubar_id)
-{
-    return WSWindowManager::the().api$menubar_destroy(menubar_id);
-}
-
 int Process::gui$menubar_add_menu(int menubar_id, int menu_id)
 {
     return WSWindowManager::the().api$menubar_add_menu(menubar_id, menu_id);
@@ -312,7 +301,6 @@ int Process::gui$menu_create(const char* name)
 {
     if (!validate_read_str(name))
         return -EFAULT;
-    m_has_created_menus = true;
     return WSWindowManager::the().api$menu_create(String(name));
 }
 

+ 0 - 4
Kernel/Syscall.cpp

@@ -223,10 +223,6 @@ static dword handle(RegisterDump& regs, dword function, dword arg1, dword arg2,
         return current->sys$rmdir((const char*)arg1);
     case Syscall::SC_chmod:
         return current->sys$chmod((const char*)arg1, (mode_t)arg2);
-    case Syscall::SC_gui_menubar_create:
-        return current->gui$menubar_create();
-    case Syscall::SC_gui_menubar_destroy:
-        return current->gui$menubar_destroy((int)arg1);
     case Syscall::SC_gui_menubar_add_menu:
         return current->gui$menubar_add_menu((int)arg1, (int)arg2);
     case Syscall::SC_gui_menu_create:

+ 0 - 2
Kernel/Syscall.h

@@ -85,8 +85,6 @@
     __ENUMERATE_SYSCALL(rmdir) \
     __ENUMERATE_SYSCALL(chmod) \
     __ENUMERATE_SYSCALL(usleep) \
-    __ENUMERATE_SYSCALL(gui_menubar_create) \
-    __ENUMERATE_SYSCALL(gui_menubar_destroy) \
     __ENUMERATE_SYSCALL(gui_menubar_add_menu) \
     __ENUMERATE_SYSCALL(gui_menu_create) \
     __ENUMERATE_SYSCALL(gui_menu_destroy) \

+ 0 - 12
LibC/gui.cpp

@@ -69,18 +69,6 @@ int gui_set_global_cursor_tracking_enabled(int window_id, bool enabled)
     __RETURN_WITH_ERRNO(rc, rc, -1);
 }
 
-int gui_menubar_create()
-{
-    int rc = syscall(SC_gui_menubar_create);
-    __RETURN_WITH_ERRNO(rc, rc, -1);
-}
-
-int gui_menubar_destroy(int menubar_id)
-{
-    int rc = syscall(SC_gui_menubar_destroy, menubar_id);
-    __RETURN_WITH_ERRNO(rc, rc, -1);
-}
-
 int gui_menubar_add_menu(int menubar_id, int menu_id)
 {
     int rc = syscall(SC_gui_menubar_add_menu, menubar_id, menu_id);

+ 0 - 2
LibC/gui.h

@@ -16,8 +16,6 @@ int gui_set_window_title(int window_id, const char*, size_t);
 int gui_get_window_rect(int window_id, GUI_Rect*);
 int gui_set_window_rect(int window_id, const GUI_Rect*);
 int gui_set_global_cursor_tracking_enabled(int window_id, bool);
-int gui_menubar_create();
-int gui_menubar_destroy(int menubar_id);
 int gui_menubar_add_menu(int menubar_id, int menu_id);
 int gui_menu_create(const char* name);
 int gui_menu_destroy(int menu_id);

+ 64 - 16
LibGUI/GEventLoop.cpp

@@ -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;
+}

+ 9 - 2
LibGUI/GEventLoop.h

@@ -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 };

+ 23 - 8
LibGUI/GMenuBar.cpp

@@ -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();
 }

+ 3 - 0
LibGUI/GMenuBar.h

@@ -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;
 };

+ 42 - 0
WindowServer/WSMessage.h

@@ -23,6 +23,11 @@ public:
         WindowActivated,
         WindowDeactivated,
         WindowCloseRequest,
+
+        __Begin_API_Client_Requests,
+        APICreateMenubarRequest,
+        APIDestroyMenubarRequest,
+        __End_API_Client_Requests,
     };
 
     WSMessage() { }
@@ -31,6 +36,7 @@ public:
 
     Type type() const { return m_type; }
 
+    bool is_client_request() const { return m_type > __Begin_API_Client_Requests && m_type < __End_API_Client_Requests; }
     bool is_mouse_event() const { return m_type == MouseMove || m_type == MouseDown || m_type == MouseUp; }
     bool is_key_event() const { return m_type == KeyUp || m_type == KeyDown; }
 
@@ -38,6 +44,42 @@ private:
     Type m_type { Invalid };
 };
 
+class WSAPIClientRequest : public WSMessage {
+public:
+    WSAPIClientRequest(Type type, int client_id)
+        : WSMessage(type)
+        , m_client_id(client_id)
+    {
+    }
+
+    int client_id() const { return m_client_id; }
+
+private:
+    int m_client_id { 0 };
+};
+
+class WSAPICreateMenubarRequest : public WSAPIClientRequest {
+public:
+    WSAPICreateMenubarRequest(int client_id)
+        : WSAPIClientRequest(WSMessage::APICreateMenubarRequest, client_id)
+    {
+    }
+};
+
+class WSAPIDestroyMenubarRequest : public WSAPIClientRequest {
+public:
+    WSAPIDestroyMenubarRequest(int client_id, int menubar_id)
+        : WSAPIClientRequest(WSMessage::APIDestroyMenubarRequest, client_id)
+        , m_menubar_id(menubar_id)
+    {
+    }
+
+    int menubar_id() const { return m_menubar_id; }
+
+private:
+    int m_menubar_id { 0 };
+};
+
 class WSClientFinishedPaintMessage final : public WSMessage {
 public:
     explicit WSClientFinishedPaintMessage(const Rect& rect = Rect())

+ 31 - 0
WindowServer/WSMessageLoop.cpp

@@ -67,6 +67,21 @@ int WSMessageLoop::exec()
     }
 }
 
+Process* WSMessageLoop::process_from_client_id(int client_id)
+{
+    // FIXME: This shouldn't work this way lol.
+    return (Process*)client_id;
+}
+
+void WSMessageLoop::post_message_to_client(int client_id, const GUI_Event& message)
+{
+    auto* process = process_from_client_id(client_id);
+    if (!process)
+        return;
+    LOCKER(process->gui_events_lock());
+    process->gui_events().append(move(message));
+}
+
 void WSMessageLoop::post_message(WSMessageReceiver* receiver, OwnPtr<WSMessage>&& message)
 {
     LOCKER(m_lock);
@@ -246,3 +261,19 @@ void WSMessageLoop::drain_keyboard()
         screen.on_receive_keyboard_data(event);
     }
 }
+
+ssize_t WSMessageLoop::on_receive_from_client(int client_id, const byte* data, size_t size)
+{
+    LOCKER(m_lock);
+    ASSERT(size == sizeof(GUI_ClientMessage));
+    auto& message = *reinterpret_cast<const GUI_ClientMessage*>(data);
+    switch (message.type) {
+    case GUI_ClientMessage::Type::CreateMenubar:
+        post_message(&WSWindowManager::the(), make<WSAPICreateMenubarRequest>(client_id));
+        break;
+    case GUI_ClientMessage::Type::DestroyMenubar:
+        post_message(&WSWindowManager::the(), make<WSAPIDestroyMenubarRequest>(client_id, message.menu.menubar_id));
+        break;
+    }
+    return size;
+}

+ 6 - 0
WindowServer/WSMessageLoop.h

@@ -9,6 +9,7 @@
 
 class WSMessageReceiver;
 class Process;
+struct GUI_Event;
 
 class WSMessageLoop {
 public:
@@ -29,6 +30,11 @@ public:
     int start_timer(int ms, Function<void()>&&);
     int stop_timer(int timer_id);
 
+    void post_message_to_client(int client_id, const GUI_Event&);
+    ssize_t on_receive_from_client(int client_id, const byte*, size_t);
+
+    static Process* process_from_client_id(int client_id);
+
 private:
     void wait_for_message();
     void drain_mouse();

+ 37 - 25
WindowServer/WSWindowManager.cpp

@@ -733,12 +733,46 @@ void WSWindowManager::on_message(WSMessage& message)
         compose();
         return;
     }
+
+    if (message.is_client_request()) {
+        auto& request = static_cast<WSAPIClientRequest&>(message);
+        switch (request.type()) {
+        case WSMessage::APICreateMenubarRequest: {
+            int menubar_id = m_next_menubar_id++;
+            auto menubar = make<WSMenuBar>(menubar_id, *WSMessageLoop::process_from_client_id(request.client_id()));
+            m_menubars.set(menubar_id, move(menubar));
+            GUI_Event event;
+            event.type = GUI_Event::Type::DidCreateMenubar;
+            event.menu.menubar_id = menubar_id;
+            WSMessageLoop::the().post_message_to_client(request.client_id(), event);
+            break;
+        }
+        case WSMessage::APIDestroyMenubarRequest: {
+            int menubar_id = static_cast<WSAPIDestroyMenubarRequest&>(request).menubar_id();
+            auto it = m_menubars.find(menubar_id);
+            if (it == m_menubars.end()) {
+                ASSERT_NOT_REACHED();
+                // FIXME: Send an error.
+                return;
+            }
+            auto& menubar = *(*it).value;
+            if (&menubar == m_current_menubar)
+                set_current_menubar(nullptr);
+            m_menubars.remove(it);
+            GUI_Event event;
+            event.type = GUI_Event::Type::DidDestroyMenubar;
+            event.menu.menubar_id = menubar_id;
+            WSMessageLoop::the().post_message_to_client(request.client_id(), event);
+        }
+        default:
+            break;
+        }
+    }
 }
 
 void WSWindowManager::set_active_window(WSWindow* window)
 {
     LOCKER(m_lock);
-    dbgprintf("set_active_window %p\n", window);
     if (window == m_active_window.ptr())
         return;
 
@@ -751,6 +785,7 @@ void WSWindowManager::set_active_window(WSWindow* window)
         WSMessageLoop::the().post_message(m_active_window.ptr(), make<WSMessage>(WSMessage::WindowActivated));
         invalidate(*m_active_window);
 
+        ASSERT(window->process());
         auto it = m_app_menubars.find(window->process());
         if (it != m_app_menubars.end()) {
             auto* menubar = (*it).value;
@@ -855,29 +890,6 @@ WSMenu& WSWindowManager::create_menu(String&& name)
     return *menu_ptr;
 }
 
-
-int WSWindowManager::api$menubar_create()
-{
-    LOCKER(m_lock);
-    int menubar_id = m_next_menubar_id++;
-    auto menubar = make<WSMenuBar>(menubar_id, *current);
-    m_menubars.set(menubar_id, move(menubar));
-    return menubar_id;
-}
-
-int WSWindowManager::api$menubar_destroy(int menubar_id)
-{
-    LOCKER(m_lock);
-    auto it = m_menubars.find(menubar_id);
-    if (it == m_menubars.end())
-        return -EBADMENUBAR;
-    auto& menubar = *(*it).value;
-    if (&menubar == m_current_menubar)
-        set_current_menubar(nullptr);
-    m_menubars.remove(it);
-    return 0;
-}
-
 int WSWindowManager::api$menubar_add_menu(int menubar_id, int menu_id)
 {
     LOCKER(m_lock);
@@ -984,5 +996,5 @@ void WSWindowManager::destroy_all_menus(Process& process)
         set_current_menubar(nullptr);
     for (int menubar_id : menubar_ids)
         m_menubars.remove(menubar_id);
-    m_app_menubars.remove(current);
+    m_app_menubars.remove(&process);
 }

+ 0 - 2
WindowServer/WSWindowManager.h

@@ -59,8 +59,6 @@ public:
     Color menu_selection_color() const { return m_menu_selection_color; }
     int menubar_menu_margin() const;
 
-    int api$menubar_create();
-    int api$menubar_destroy(int menubar_id);
     int api$menubar_add_menu(int menubar_id, int menu_id);
     int api$menu_create(String&&);
     int api$menu_destroy(int menu_id);