summaryrefslogtreecommitdiff
path: root/Services/WindowServer
diff options
context:
space:
mode:
Diffstat (limited to 'Services/WindowServer')
-rw-r--r--Services/WindowServer/AppletManager.cpp141
-rw-r--r--Services/WindowServer/AppletManager.h56
-rw-r--r--Services/WindowServer/Button.cpp116
-rw-r--r--Services/WindowServer/Button.h69
-rw-r--r--Services/WindowServer/CMakeLists.txt26
-rw-r--r--Services/WindowServer/ClientConnection.cpp941
-rw-r--r--Services/WindowServer/ClientConnection.h170
-rw-r--r--Services/WindowServer/Compositor.cpp1040
-rw-r--r--Services/WindowServer/Compositor.h135
-rw-r--r--Services/WindowServer/Cursor.cpp185
-rw-r--r--Services/WindowServer/Cursor.h81
-rw-r--r--Services/WindowServer/Event.h166
-rw-r--r--Services/WindowServer/EventLoop.cpp149
-rw-r--r--Services/WindowServer/EventLoop.h57
-rw-r--r--Services/WindowServer/Menu.cpp568
-rw-r--r--Services/WindowServer/Menu.h169
-rw-r--r--Services/WindowServer/MenuBar.cpp55
-rw-r--r--Services/WindowServer/MenuBar.h61
-rw-r--r--Services/WindowServer/MenuItem.cpp111
-rw-r--r--Services/WindowServer/MenuItem.h104
-rw-r--r--Services/WindowServer/MenuManager.cpp497
-rw-r--r--Services/WindowServer/MenuManager.h123
-rw-r--r--Services/WindowServer/Screen.cpp186
-rw-r--r--Services/WindowServer/Screen.h98
-rw-r--r--Services/WindowServer/Window.cpp776
-rw-r--r--Services/WindowServer/Window.h363
-rw-r--r--Services/WindowServer/WindowClient.ipc42
-rw-r--r--Services/WindowServer/WindowFrame.cpp382
-rw-r--r--Services/WindowServer/WindowFrame.h81
-rw-r--r--Services/WindowServer/WindowManager.cpp1509
-rw-r--r--Services/WindowServer/WindowManager.h479
-rw-r--r--Services/WindowServer/WindowServer.ipc117
-rw-r--r--Services/WindowServer/WindowSwitcher.cpp257
-rw-r--r--Services/WindowServer/WindowSwitcher.h84
-rw-r--r--Services/WindowServer/WindowType.h41
-rw-r--r--Services/WindowServer/main.cpp119
36 files changed, 0 insertions, 9554 deletions
diff --git a/Services/WindowServer/AppletManager.cpp b/Services/WindowServer/AppletManager.cpp
deleted file mode 100644
index 509d5de3d0..0000000000
--- a/Services/WindowServer/AppletManager.cpp
+++ /dev/null
@@ -1,141 +0,0 @@
-/*
- * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "AppletManager.h"
-#include <AK/QuickSort.h>
-#include <LibGfx/Painter.h>
-#include <WindowServer/MenuManager.h>
-
-namespace WindowServer {
-
-static AppletManager* s_the;
-Vector<String> order_vector;
-
-AppletManager::AppletManager()
-{
- s_the = this;
-
- auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
- auto order = wm_config->read_entry("Applet", "Order");
- order_vector = order.split(',');
-}
-
-AppletManager::~AppletManager()
-{
-}
-
-AppletManager& AppletManager::the()
-{
- ASSERT(s_the);
- return *s_the;
-}
-
-void AppletManager::event(Core::Event& event)
-{
- auto& mouse_event = static_cast<MouseEvent&>(event);
-
- for (auto& applet : m_applets) {
- if (!applet)
- continue;
- if (!applet->rect_in_menubar().contains(mouse_event.position()))
- continue;
- auto local_event = mouse_event.translated(-applet->rect_in_menubar().location());
- applet->event(local_event);
- }
-}
-
-void AppletManager::add_applet(Window& applet)
-{
- m_applets.append(applet);
-
- // Prune any dead weak pointers from the applet list.
- m_applets.remove_all_matching([](auto& entry) {
- return entry.is_null();
- });
-
- quick_sort(m_applets, [](auto& a, auto& b) {
- auto index_a = order_vector.find_first_index(a->title());
- auto index_b = order_vector.find_first_index(b->title());
- return index_a.value_or(0) > index_b.value_or(0);
- });
-
- calculate_applet_rects(MenuManager::the().window());
-
- MenuManager::the().refresh();
-}
-
-void AppletManager::calculate_applet_rects(Window& window)
-{
- auto menubar_rect = window.rect();
- int right_edge_x = menubar_rect.width() - 4;
- for (auto& existing_applet : m_applets) {
-
- Gfx::IntRect new_applet_rect(right_edge_x - existing_applet->size().width(), 0, existing_applet->size().width(), existing_applet->size().height());
- Gfx::IntRect dummy_menubar_rect(0, 0, 0, 18);
- new_applet_rect.center_vertically_within(dummy_menubar_rect);
-
- existing_applet->set_rect_in_menubar(new_applet_rect);
- right_edge_x = existing_applet->rect_in_menubar().x() - 4;
- }
-}
-
-void AppletManager::remove_applet(Window& applet)
-{
- m_applets.remove_first_matching([&](auto& entry) {
- return &applet == entry.ptr();
- });
-
- MenuManager::the().refresh();
-}
-
-void AppletManager::draw()
-{
- for (auto& applet : m_applets) {
- if (!applet)
- continue;
- draw_applet(*applet);
- }
-}
-
-void AppletManager::draw_applet(const Window& applet)
-{
- if (!applet.backing_store())
- return;
-
- Gfx::Painter painter(*MenuManager::the().window().backing_store());
- Gfx::PainterStateSaver saver(painter);
- painter.add_clip_rect(applet.rect_in_menubar());
- painter.fill_rect(applet.rect_in_menubar(), WindowManager::the().palette().window());
- painter.blit(applet.rect_in_menubar().location(), *applet.backing_store(), applet.backing_store()->rect());
-}
-
-void AppletManager::invalidate_applet(const Window& applet, const Gfx::IntRect& rect)
-{
- draw_applet(applet);
- MenuManager::the().window().invalidate(rect.translated(applet.rect_in_menubar().location()));
-}
-
-}
diff --git a/Services/WindowServer/AppletManager.h b/Services/WindowServer/AppletManager.h
deleted file mode 100644
index 92f39fbe6d..0000000000
--- a/Services/WindowServer/AppletManager.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <WindowServer/Window.h>
-#include <WindowServer/WindowManager.h>
-
-namespace WindowServer {
-
-class AppletManager : public Core::Object {
- C_OBJECT(AppletManager)
-public:
- AppletManager();
- ~AppletManager();
-
- static AppletManager& the();
-
- virtual void event(Core::Event&) override;
-
- void add_applet(Window& applet);
- void remove_applet(Window& applet);
- void draw();
- void invalidate_applet(const Window& applet, const Gfx::IntRect& rect);
- void calculate_applet_rects(Window& window);
-
-private:
- void draw_applet(const Window& applet);
-
- Vector<WeakPtr<Window>> m_applets;
-};
-
-}
diff --git a/Services/WindowServer/Button.cpp b/Services/WindowServer/Button.cpp
deleted file mode 100644
index 7265494b31..0000000000
--- a/Services/WindowServer/Button.cpp
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <LibGfx/CharacterBitmap.h>
-#include <LibGfx/Painter.h>
-#include <LibGfx/StylePainter.h>
-#include <WindowServer/Button.h>
-#include <WindowServer/Event.h>
-#include <WindowServer/WindowManager.h>
-
-namespace WindowServer {
-
-Button::Button(WindowFrame& frame, Function<void(Button&)>&& on_click_handler)
- : on_click(move(on_click_handler))
- , m_frame(frame)
-{
-}
-
-Button::~Button()
-{
-}
-
-void Button::paint(Gfx::Painter& painter)
-{
- auto palette = WindowManager::the().palette();
- Gfx::PainterStateSaver saver(painter);
- painter.translate(relative_rect().location());
- Gfx::StylePainter::paint_button(painter, rect(), palette, Gfx::ButtonStyle::Normal, m_pressed, m_hovered);
-
- if (m_icon) {
- auto icon_location = rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
- if (m_pressed)
- painter.translate(1, 1);
- painter.blit(icon_location, *m_icon, m_icon->rect());
- }
-}
-
-void Button::on_mouse_event(const MouseEvent& event)
-{
- auto& wm = WindowManager::the();
-
- if (event.type() == Event::MouseDown && event.button() == MouseButton::Left) {
- m_pressed = true;
- wm.set_cursor_tracking_button(this);
- m_frame.invalidate(m_relative_rect);
- return;
- }
-
- if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
- if (wm.cursor_tracking_button() != this)
- return;
- wm.set_cursor_tracking_button(nullptr);
- bool old_pressed = m_pressed;
- m_pressed = false;
- if (rect().contains(event.position())) {
- if (on_click)
- on_click(*this);
- }
- if (old_pressed != m_pressed) {
- // Would like to compute:
- // m_hovered = rect_after_action().contains(event.position());
- // However, we don't know that rect yet. We can make an educated
- // guess which also looks okay even when wrong:
- m_hovered = false;
- m_frame.invalidate(m_relative_rect);
- }
- return;
- }
-
- if (event.type() == Event::MouseMove) {
- bool old_hovered = m_hovered;
- m_hovered = rect().contains(event.position());
- wm.set_hovered_button(m_hovered ? this : nullptr);
- if (old_hovered != m_hovered)
- m_frame.invalidate(m_relative_rect);
- }
-
- if (event.type() == Event::MouseMove && event.buttons() & (unsigned)MouseButton::Left) {
- if (wm.cursor_tracking_button() != this)
- return;
- bool old_pressed = m_pressed;
- m_pressed = m_hovered;
- if (old_pressed != m_pressed)
- m_frame.invalidate(m_relative_rect);
- }
-}
-
-Gfx::IntRect Button::screen_rect() const
-{
- return m_relative_rect.translated(m_frame.rect().location());
-}
-
-}
diff --git a/Services/WindowServer/Button.h b/Services/WindowServer/Button.h
deleted file mode 100644
index 7a2d6b82e9..0000000000
--- a/Services/WindowServer/Button.h
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/Function.h>
-#include <AK/Weakable.h>
-#include <LibGfx/Forward.h>
-#include <LibGfx/Rect.h>
-
-namespace WindowServer {
-
-class MouseEvent;
-class WindowFrame;
-
-class Button : public Weakable<Button> {
-public:
- Button(WindowFrame&, Function<void(Button&)>&& on_click_handler);
- ~Button();
-
- Gfx::IntRect relative_rect() const { return m_relative_rect; }
- void set_relative_rect(const Gfx::IntRect& rect) { m_relative_rect = rect; }
-
- Gfx::IntRect rect() const { return { {}, m_relative_rect.size() }; }
- Gfx::IntRect screen_rect() const;
-
- void paint(Gfx::Painter&);
-
- void on_mouse_event(const MouseEvent&);
-
- Function<void(Button&)> on_click;
-
- bool is_visible() const { return m_visible; }
-
- void set_icon(const Gfx::Bitmap& icon) { m_icon = icon; }
-
-private:
- WindowFrame& m_frame;
- Gfx::IntRect m_relative_rect;
- RefPtr<Gfx::Bitmap> m_icon;
- bool m_pressed { false };
- bool m_visible { true };
- bool m_hovered { false };
-};
-
-}
diff --git a/Services/WindowServer/CMakeLists.txt b/Services/WindowServer/CMakeLists.txt
deleted file mode 100644
index b822fec6b5..0000000000
--- a/Services/WindowServer/CMakeLists.txt
+++ /dev/null
@@ -1,26 +0,0 @@
-compile_ipc(WindowServer.ipc WindowServerEndpoint.h)
-compile_ipc(WindowClient.ipc WindowClientEndpoint.h)
-
-set(SOURCES
- AppletManager.cpp
- Button.cpp
- ClientConnection.cpp
- Compositor.cpp
- Cursor.cpp
- EventLoop.cpp
- main.cpp
- MenuBar.cpp
- Menu.cpp
- MenuItem.cpp
- MenuManager.cpp
- Screen.cpp
- Window.cpp
- WindowFrame.cpp
- WindowManager.cpp
- WindowSwitcher.cpp
- WindowServerEndpoint.h
- WindowClientEndpoint.h
-)
-
-serenity_bin(WindowServer)
-target_link_libraries(WindowServer LibCore LibGfx LibThread LibPthread LibIPC)
diff --git a/Services/WindowServer/ClientConnection.cpp b/Services/WindowServer/ClientConnection.cpp
deleted file mode 100644
index f0e4402d81..0000000000
--- a/Services/WindowServer/ClientConnection.cpp
+++ /dev/null
@@ -1,941 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <AK/Badge.h>
-#include <AK/SharedBuffer.h>
-#include <LibGfx/Bitmap.h>
-#include <LibGfx/StandardCursor.h>
-#include <LibGfx/SystemTheme.h>
-#include <WindowServer/AppletManager.h>
-#include <WindowServer/ClientConnection.h>
-#include <WindowServer/Compositor.h>
-#include <WindowServer/Menu.h>
-#include <WindowServer/MenuBar.h>
-#include <WindowServer/MenuItem.h>
-#include <WindowServer/Screen.h>
-#include <WindowServer/Window.h>
-#include <WindowServer/WindowClientEndpoint.h>
-#include <WindowServer/WindowManager.h>
-#include <WindowServer/WindowSwitcher.h>
-#include <errno.h>
-#include <serenity.h>
-#include <stdio.h>
-#include <unistd.h>
-
-namespace WindowServer {
-
-HashMap<int, NonnullRefPtr<ClientConnection>>* s_connections;
-
-static Gfx::IntRect normalize_window_rect(Gfx::IntRect rect, WindowType window_type)
-{
- auto min_size = 1;
- if (window_type == WindowType::Normal)
- min_size = 50;
- Gfx::IntRect normalized_rect = { rect.x(), rect.y(), max(rect.width(), min_size), max(rect.height(), min_size) };
- return normalized_rect;
-}
-
-void ClientConnection::for_each_client(Function<void(ClientConnection&)> callback)
-{
- if (!s_connections)
- return;
- for (auto& it : *s_connections) {
- callback(*it.value);
- }
-}
-
-ClientConnection* ClientConnection::from_client_id(int client_id)
-{
- if (!s_connections)
- return nullptr;
- auto it = s_connections->find(client_id);
- if (it == s_connections->end())
- return nullptr;
- return (*it).value.ptr();
-}
-
-ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
- : IPC::ClientConnection<WindowClientEndpoint, WindowServerEndpoint>(*this, move(client_socket), client_id)
-{
- if (!s_connections)
- s_connections = new HashMap<int, NonnullRefPtr<ClientConnection>>;
- s_connections->set(client_id, *this);
-}
-
-ClientConnection::~ClientConnection()
-{
- if (m_has_display_link)
- Compositor::the().decrement_display_link_count({});
-
- MenuManager::the().close_all_menus_from_client({}, *this);
- auto windows = move(m_windows);
- for (auto& window : windows) {
- window.value->detach_client({});
- if (window.value->type() == WindowType::MenuApplet)
- AppletManager::the().remove_applet(window.value);
- }
-}
-
-void ClientConnection::die()
-{
- deferred_invoke([this](auto&) {
- s_connections->remove(client_id());
- });
-}
-
-void ClientConnection::notify_about_new_screen_rect(const Gfx::IntRect& rect)
-{
- post_message(Messages::WindowClient::ScreenRectChanged(rect));
-}
-
-OwnPtr<Messages::WindowServer::CreateMenubarResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenubar&)
-{
- int menubar_id = m_next_menubar_id++;
- auto menubar = make<MenuBar>(*this, menubar_id);
- m_menubars.set(menubar_id, move(menubar));
- return make<Messages::WindowServer::CreateMenubarResponse>(menubar_id);
-}
-
-OwnPtr<Messages::WindowServer::DestroyMenubarResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenubar& message)
-{
- int menubar_id = message.menubar_id();
- auto it = m_menubars.find(menubar_id);
- if (it == m_menubars.end()) {
- did_misbehave("DestroyMenubar: Bad menubar ID");
- return {};
- }
- auto& menubar = *(*it).value;
- MenuManager::the().close_menubar(menubar);
- m_menubars.remove(it);
- return make<Messages::WindowServer::DestroyMenubarResponse>();
-}
-
-OwnPtr<Messages::WindowServer::CreateMenuResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenu& message)
-{
- int menu_id = m_next_menu_id++;
- auto menu = Menu::construct(this, menu_id, message.menu_title());
- m_menus.set(menu_id, move(menu));
- return make<Messages::WindowServer::CreateMenuResponse>(menu_id);
-}
-
-OwnPtr<Messages::WindowServer::DestroyMenuResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenu& message)
-{
- int menu_id = message.menu_id();
- auto it = m_menus.find(menu_id);
- if (it == m_menus.end()) {
- did_misbehave("DestroyMenu: Bad menu ID");
- return {};
- }
- auto& menu = *(*it).value;
- menu.close();
- m_menus.remove(it);
- remove_child(menu);
- return make<Messages::WindowServer::DestroyMenuResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetApplicationMenubarResponse> ClientConnection::handle(const Messages::WindowServer::SetApplicationMenubar& message)
-{
- int menubar_id = message.menubar_id();
- auto it = m_menubars.find(menubar_id);
- if (it == m_menubars.end()) {
- did_misbehave("SetApplicationMenubar: Bad menubar ID");
- return {};
- }
- auto& menubar = *(*it).value;
- m_app_menubar = menubar.make_weak_ptr();
- WindowManager::the().notify_client_changed_app_menubar(*this);
- return make<Messages::WindowServer::SetApplicationMenubarResponse>();
-}
-
-OwnPtr<Messages::WindowServer::AddMenuToMenubarResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuToMenubar& message)
-{
- int menubar_id = message.menubar_id();
- int menu_id = message.menu_id();
- auto it = m_menubars.find(menubar_id);
- auto jt = m_menus.find(menu_id);
- if (it == m_menubars.end()) {
- did_misbehave("AddMenuToMenubar: Bad menubar ID");
- return {};
- }
- if (jt == m_menus.end()) {
- did_misbehave("AddMenuToMenubar: Bad menu ID");
- return {};
- }
- auto& menubar = *(*it).value;
- auto& menu = *(*jt).value;
- menubar.add_menu(menu);
- return make<Messages::WindowServer::AddMenuToMenubarResponse>();
-}
-
-OwnPtr<Messages::WindowServer::AddMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuItem& message)
-{
- int menu_id = message.menu_id();
- unsigned identifier = message.identifier();
- auto it = m_menus.find(menu_id);
- if (it == m_menus.end()) {
- dbg() << "AddMenuItem: Bad menu ID: " << menu_id;
- return {};
- }
- auto& menu = *(*it).value;
- auto menu_item = make<MenuItem>(menu, identifier, message.text(), message.shortcut(), message.enabled(), message.checkable(), message.checked());
- if (message.is_default())
- menu_item->set_default(true);
- if (message.icon_buffer_id() != -1) {
- auto icon_buffer = SharedBuffer::create_from_shbuf_id(message.icon_buffer_id());
- if (!icon_buffer)
- return {};
- // FIXME: Verify that the icon buffer can accommodate a 16x16 bitmap view.
- auto shared_icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, icon_buffer.release_nonnull(), { 16, 16 });
- menu_item->set_icon(shared_icon);
- }
- menu_item->set_submenu_id(message.submenu_id());
- menu_item->set_exclusive(message.exclusive());
- menu.add_item(move(menu_item));
- return make<Messages::WindowServer::AddMenuItemResponse>();
-}
-
-OwnPtr<Messages::WindowServer::PopupMenuResponse> ClientConnection::handle(const Messages::WindowServer::PopupMenu& message)
-{
- int menu_id = message.menu_id();
- auto position = message.screen_position();
- auto it = m_menus.find(menu_id);
- if (it == m_menus.end()) {
- did_misbehave("PopupMenu: Bad menu ID");
- return {};
- }
- auto& menu = *(*it).value;
- menu.popup(position);
- return make<Messages::WindowServer::PopupMenuResponse>();
-}
-
-OwnPtr<Messages::WindowServer::DismissMenuResponse> ClientConnection::handle(const Messages::WindowServer::DismissMenu& message)
-{
- int menu_id = message.menu_id();
- auto it = m_menus.find(menu_id);
- if (it == m_menus.end()) {
- did_misbehave("DismissMenu: Bad menu ID");
- return {};
- }
- auto& menu = *(*it).value;
- menu.close();
- return make<Messages::WindowServer::DismissMenuResponse>();
-}
-
-OwnPtr<Messages::WindowServer::UpdateMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::UpdateMenuItem& message)
-{
- int menu_id = message.menu_id();
- auto it = m_menus.find(menu_id);
- if (it == m_menus.end()) {
- did_misbehave("UpdateMenuItem: Bad menu ID");
- return {};
- }
- auto& menu = *(*it).value;
- auto* menu_item = menu.item_with_identifier(message.identifier());
- if (!menu_item) {
- did_misbehave("UpdateMenuItem: Bad menu item identifier");
- return {};
- }
- menu_item->set_text(message.text());
- menu_item->set_shortcut_text(message.shortcut());
- menu_item->set_enabled(message.enabled());
- menu_item->set_checkable(message.checkable());
- menu_item->set_default(message.is_default());
- if (message.checkable())
- menu_item->set_checked(message.checked());
- return make<Messages::WindowServer::UpdateMenuItemResponse>();
-}
-
-OwnPtr<Messages::WindowServer::AddMenuSeparatorResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuSeparator& message)
-{
- int menu_id = message.menu_id();
- auto it = m_menus.find(menu_id);
- if (it == m_menus.end()) {
- did_misbehave("AddMenuSeparator: Bad menu ID");
- return {};
- }
- auto& menu = *(*it).value;
- menu.add_item(make<MenuItem>(menu, MenuItem::Separator));
- return make<Messages::WindowServer::AddMenuSeparatorResponse>();
-}
-
-OwnPtr<Messages::WindowServer::MoveWindowToFrontResponse> ClientConnection::handle(const Messages::WindowServer::MoveWindowToFront& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("MoveWindowToFront: Bad window ID");
- return {};
- }
- WindowManager::the().move_to_front_and_make_active(*(*it).value);
- return make<Messages::WindowServer::MoveWindowToFrontResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetFullscreenResponse> ClientConnection::handle(const Messages::WindowServer::SetFullscreen& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetFullscreen: Bad window ID");
- return {};
- }
- it->value->set_fullscreen(message.fullscreen());
- return make<Messages::WindowServer::SetFullscreenResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetWindowOpacityResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowOpacity& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowOpacity: Bad window ID");
- return {};
- }
- it->value->set_opacity(message.opacity());
- return make<Messages::WindowServer::SetWindowOpacityResponse>();
-}
-
-void ClientConnection::handle(const Messages::WindowServer::AsyncSetWallpaper& message)
-{
- Compositor::the().set_wallpaper(message.path(), [&](bool success) {
- post_message(Messages::WindowClient::AsyncSetWallpaperFinished(success));
- });
-}
-
-OwnPtr<Messages::WindowServer::SetBackgroundColorResponse> ClientConnection::handle(const Messages::WindowServer::SetBackgroundColor& message)
-{
- Compositor::the().set_background_color(message.background_color());
- return make<Messages::WindowServer::SetBackgroundColorResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetWallpaperModeResponse> ClientConnection::handle(const Messages::WindowServer::SetWallpaperMode& message)
-{
- Compositor::the().set_wallpaper_mode(message.mode());
- return make<Messages::WindowServer::SetWallpaperModeResponse>();
-}
-
-OwnPtr<Messages::WindowServer::GetWallpaperResponse> ClientConnection::handle(const Messages::WindowServer::GetWallpaper&)
-{
- return make<Messages::WindowServer::GetWallpaperResponse>(Compositor::the().wallpaper_path());
-}
-
-OwnPtr<Messages::WindowServer::SetResolutionResponse> ClientConnection::handle(const Messages::WindowServer::SetResolution& message)
-{
- return make<Messages::WindowServer::SetResolutionResponse>(WindowManager::the().set_resolution(message.resolution().width(), message.resolution().height()), WindowManager::the().resolution());
-}
-
-OwnPtr<Messages::WindowServer::SetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowTitle& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowTitle: Bad window ID");
- return {};
- }
- it->value->set_title(message.title());
- return make<Messages::WindowServer::SetWindowTitleResponse>();
-}
-
-OwnPtr<Messages::WindowServer::GetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowTitle& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("GetWindowTitle: Bad window ID");
- return {};
- }
- return make<Messages::WindowServer::GetWindowTitleResponse>(it->value->title());
-}
-
-OwnPtr<Messages::WindowServer::IsMaximizedResponse> ClientConnection::handle(const Messages::WindowServer::IsMaximized& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("IsMaximized: Bad window ID");
- return {};
- }
- return make<Messages::WindowServer::IsMaximizedResponse>(it->value->is_maximized());
-}
-
-OwnPtr<Messages::WindowServer::SetWindowIconBitmapResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowIconBitmap& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowIconBitmap: Bad window ID");
- return {};
- }
- auto& window = *(*it).value;
-
- if (message.icon().is_valid()) {
- window.set_icon(*message.icon().bitmap());
- } else {
- window.set_default_icon();
- }
-
- window.frame().invalidate_title_bar();
- WindowManager::the().tell_wm_listeners_window_icon_changed(window);
- return make<Messages::WindowServer::SetWindowIconBitmapResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowRect& message)
-{
- int window_id = message.window_id();
- auto it = m_windows.find(window_id);
- if (it == m_windows.end()) {
- did_misbehave("SetWindowRect: Bad window ID");
- return {};
- }
- auto& window = *(*it).value;
- if (window.is_fullscreen()) {
- dbgln("ClientConnection: Ignoring SetWindowRect request for fullscreen window");
- return {};
- }
-
- if (message.rect().location() != window.rect().location()) {
- window.set_default_positioned(false);
- }
- auto normalized_rect = normalize_window_rect(message.rect(), window.type());
- window.set_rect(normalized_rect);
- window.request_update(normalized_rect);
- return make<Messages::WindowServer::SetWindowRectResponse>(normalized_rect);
-}
-
-OwnPtr<Messages::WindowServer::GetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRect& message)
-{
- int window_id = message.window_id();
- auto it = m_windows.find(window_id);
- if (it == m_windows.end()) {
- did_misbehave("GetWindowRect: Bad window ID");
- return {};
- }
- return make<Messages::WindowServer::GetWindowRectResponse>(it->value->rect());
-}
-
-OwnPtr<Messages::WindowServer::GetWindowRectInMenubarResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRectInMenubar& message)
-{
- int window_id = message.window_id();
- auto it = m_windows.find(window_id);
- if (it == m_windows.end()) {
- did_misbehave("GetWindowRectInMenubar: Bad window ID");
- return {};
- }
- return make<Messages::WindowServer::GetWindowRectInMenubarResponse>(it->value->rect_in_menubar());
-}
-
-Window* ClientConnection::window_from_id(i32 window_id)
-{
- auto it = m_windows.find(window_id);
- if (it == m_windows.end())
- return nullptr;
- return it->value.ptr();
-}
-
-OwnPtr<Messages::WindowServer::CreateWindowResponse> ClientConnection::handle(const Messages::WindowServer::CreateWindow& message)
-{
- Window* parent_window = nullptr;
- if (message.parent_window_id()) {
- parent_window = window_from_id(message.parent_window_id());
- if (!parent_window) {
- did_misbehave("CreateWindow with bad parent_window_id");
- return {};
- }
- }
-
- int window_id = m_next_window_id++;
- auto window = Window::construct(*this, (WindowType)message.type(), window_id, message.modal(), message.minimizable(), message.frameless(), message.resizable(), message.fullscreen(), message.accessory(), parent_window);
-
- window->set_has_alpha_channel(message.has_alpha_channel());
- window->set_title(message.title());
- if (!message.fullscreen()) {
- auto rect = message.rect();
- if (message.auto_position() && window->type() == WindowType::Normal) {
- rect = { WindowManager::the().get_recommended_window_position({ 100, 100 }), message.rect().size() };
- window->set_default_positioned(true);
- }
- auto normalized_rect = normalize_window_rect(rect, window->type());
- window->set_rect(normalized_rect);
- }
- if (window->type() == WindowType::Desktop) {
- window->set_rect(WindowManager::the().desktop_rect());
- window->recalculate_rect();
- }
- window->set_opacity(message.opacity());
- window->set_size_increment(message.size_increment());
- window->set_base_size(message.base_size());
- window->set_resize_aspect_ratio(message.resize_aspect_ratio());
- window->invalidate();
- if (window->type() == WindowType::MenuApplet)
- AppletManager::the().add_applet(*window);
- m_windows.set(window_id, move(window));
- return make<Messages::WindowServer::CreateWindowResponse>(window_id);
-}
-
-void ClientConnection::destroy_window(Window& window, Vector<i32>& destroyed_window_ids)
-{
- for (auto& child_window : window.child_windows()) {
- if (!child_window)
- continue;
- ASSERT(child_window->window_id() != window.window_id());
- destroy_window(*child_window, destroyed_window_ids);
- }
-
- for (auto& accessory_window : window.accessory_windows()) {
- if (!accessory_window)
- continue;
- ASSERT(accessory_window->window_id() != window.window_id());
- destroy_window(*accessory_window, destroyed_window_ids);
- }
-
- destroyed_window_ids.append(window.window_id());
-
- if (window.type() == WindowType::MenuApplet)
- AppletManager::the().remove_applet(window);
-
- window.destroy();
- remove_child(window);
- m_windows.remove(window.window_id());
-}
-
-OwnPtr<Messages::WindowServer::DestroyWindowResponse> ClientConnection::handle(const Messages::WindowServer::DestroyWindow& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("DestroyWindow: Bad window ID");
- return {};
- }
- auto& window = *(*it).value;
- Vector<i32> destroyed_window_ids;
- destroy_window(window, destroyed_window_ids);
- return make<Messages::WindowServer::DestroyWindowResponse>(destroyed_window_ids);
-}
-
-void ClientConnection::post_paint_message(Window& window, bool ignore_occlusion)
-{
- auto rect_set = window.take_pending_paint_rects();
- if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
- return;
-
- post_message(Messages::WindowClient::Paint(window.window_id(), window.size(), rect_set.rects()));
-}
-
-void ClientConnection::handle(const Messages::WindowServer::InvalidateRect& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("InvalidateRect: Bad window ID");
- return;
- }
- auto& window = *(*it).value;
- for (size_t i = 0; i < message.rects().size(); ++i)
- window.request_update(message.rects()[i].intersected({ {}, window.size() }), message.ignore_occlusion());
-}
-
-void ClientConnection::handle(const Messages::WindowServer::DidFinishPainting& message)
-{
- int window_id = message.window_id();
- auto it = m_windows.find(window_id);
- if (it == m_windows.end()) {
- did_misbehave("DidFinishPainting: Bad window ID");
- return;
- }
- auto& window = *(*it).value;
- for (auto& rect : message.rects())
- window.invalidate(rect);
-
- WindowSwitcher::the().refresh_if_needed();
-}
-
-OwnPtr<Messages::WindowServer::SetWindowBackingStoreResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBackingStore& message)
-{
- int window_id = message.window_id();
- auto it = m_windows.find(window_id);
- if (it == m_windows.end()) {
- did_misbehave("SetWindowBackingStore: Bad window ID");
- return {};
- }
- auto& window = *(*it).value;
- if (window.last_backing_store() && window.last_backing_store()->shbuf_id() == message.shbuf_id()) {
- window.swap_backing_stores();
- } else {
- auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
- if (!shared_buffer)
- return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
- auto backing_store = Gfx::Bitmap::create_with_shared_buffer(
- message.has_alpha_channel() ? Gfx::BitmapFormat::RGBA32 : Gfx::BitmapFormat::RGB32,
- *shared_buffer,
- message.size());
- window.set_backing_store(move(backing_store));
- }
-
- if (message.flush_immediately())
- window.invalidate(false);
-
- return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> ClientConnection::handle(const Messages::WindowServer::SetGlobalCursorTracking& message)
-{
- int window_id = message.window_id();
- auto it = m_windows.find(window_id);
- if (it == m_windows.end()) {
- did_misbehave("SetGlobalCursorTracking: Bad window ID");
- return {};
- }
- it->value->set_global_cursor_tracking_enabled(message.enabled());
- return make<Messages::WindowServer::SetGlobalCursorTrackingResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetWindowCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCursor& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowCursor: Bad window ID");
- return {};
- }
- auto& window = *(*it).value;
- if (message.cursor_type() < 0 || message.cursor_type() >= (i32)Gfx::StandardCursor::__Count) {
- did_misbehave("SetWindowCursor: Bad cursor type");
- return {};
- }
- window.set_cursor(Cursor::create((Gfx::StandardCursor)message.cursor_type()));
- Compositor::the().invalidate_cursor();
- return make<Messages::WindowServer::SetWindowCursorResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetWindowCustomCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCustomCursor& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowCustomCursor: Bad window ID");
- return {};
- }
-
- auto& window = *(*it).value;
- if (!message.cursor().is_valid()) {
- did_misbehave("SetWindowCustomCursor: Bad cursor");
- return {};
- }
-
- window.set_cursor(Cursor::create(*message.cursor().bitmap()));
- Compositor::the().invalidate_cursor();
- return make<Messages::WindowServer::SetWindowCustomCursorResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
- return {};
- }
- it->value->set_has_alpha_channel(message.has_alpha_channel());
- return make<Messages::WindowServer::SetWindowHasAlphaChannelResponse>();
-}
-
-void ClientConnection::handle(const Messages::WindowServer::WM_SetActiveWindow& message)
-{
- auto* client = ClientConnection::from_client_id(message.client_id());
- if (!client) {
- did_misbehave("WM_SetActiveWindow: Bad client ID");
- return;
- }
- auto it = client->m_windows.find(message.window_id());
- if (it == client->m_windows.end()) {
- did_misbehave("WM_SetActiveWindow: Bad window ID");
- return;
- }
- auto& window = *(*it).value;
- WindowManager::the().minimize_windows(window, false);
- WindowManager::the().move_to_front_and_make_active(window);
-}
-
-void ClientConnection::handle(const Messages::WindowServer::WM_PopupWindowMenu& message)
-{
- auto* client = ClientConnection::from_client_id(message.client_id());
- if (!client) {
- did_misbehave("WM_PopupWindowMenu: Bad client ID");
- return;
- }
- auto it = client->m_windows.find(message.window_id());
- if (it == client->m_windows.end()) {
- did_misbehave("WM_PopupWindowMenu: Bad window ID");
- return;
- }
- auto& window = *(*it).value;
- if (auto* modal_window = window.blocking_modal_window()) {
- modal_window->popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
- } else {
- window.popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
- }
-}
-
-void ClientConnection::handle(const Messages::WindowServer::WM_StartWindowResize& request)
-{
- auto* client = ClientConnection::from_client_id(request.client_id());
- if (!client) {
- did_misbehave("WM_StartWindowResize: Bad client ID");
- return;
- }
- auto it = client->m_windows.find(request.window_id());
- if (it == client->m_windows.end()) {
- did_misbehave("WM_StartWindowResize: Bad window ID");
- return;
- }
- auto& window = *(*it).value;
- // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
- // Maybe the client should be allowed to specify what initiated this request?
- WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
-}
-
-void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowMinimized& message)
-{
- auto* client = ClientConnection::from_client_id(message.client_id());
- if (!client) {
- did_misbehave("WM_SetWindowMinimized: Bad client ID");
- return;
- }
- auto it = client->m_windows.find(message.window_id());
- if (it == client->m_windows.end()) {
- did_misbehave("WM_SetWindowMinimized: Bad window ID");
- return;
- }
- auto& window = *(*it).value;
- WindowManager::the().minimize_windows(window, message.minimized());
-}
-
-OwnPtr<Messages::WindowServer::GreetResponse> ClientConnection::handle(const Messages::WindowServer::Greet&)
-{
- return make<Messages::WindowServer::GreetResponse>(client_id(), Screen::the().rect(), Gfx::current_system_theme_buffer_id());
-}
-
-void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowTaskbarRect& message)
-{
- // Because the Taskbar (which should be the only user of this API) does not own the
- // window or the client id, there is a possibility that it may send this message for
- // a window or client that may have been destroyed already. This is not an error,
- // and we should not call did_misbehave() for either.
- auto* client = ClientConnection::from_client_id(message.client_id());
- if (!client)
- return;
-
- auto it = client->m_windows.find(message.window_id());
- if (it == client->m_windows.end())
- return;
-
- auto& window = *(*it).value;
- window.set_taskbar_rect(message.rect());
-}
-
-OwnPtr<Messages::WindowServer::StartDragResponse> ClientConnection::handle(const Messages::WindowServer::StartDrag& message)
-{
- auto& wm = WindowManager::the();
- if (wm.dnd_client())
- return make<Messages::WindowServer::StartDragResponse>(false);
-
- RefPtr<Gfx::Bitmap> bitmap;
- if (message.bitmap_id() != -1) {
- auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.bitmap_id());
- ssize_t size_in_bytes = message.bitmap_size().area() * sizeof(Gfx::RGBA32);
- if (size_in_bytes > shared_buffer->size()) {
- did_misbehave("SetAppletBackingStore: Shared buffer is too small for applet size");
- return {};
- }
- bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *shared_buffer, message.bitmap_size());
- }
-
- wm.start_dnd_drag(*this, message.text(), bitmap, Core::MimeData::construct(message.mime_data()));
- return make<Messages::WindowServer::StartDragResponse>(true);
-}
-
-OwnPtr<Messages::WindowServer::SetSystemMenuResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemMenu& message)
-{
- auto it = m_menus.find(message.menu_id());
- if (it == m_menus.end()) {
- did_misbehave("SetSystemMenu called with invalid menu ID");
- return {};
- }
-
- auto& menu = it->value;
- MenuManager::the().set_system_menu(menu);
- return make<Messages::WindowServer::SetSystemMenuResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemTheme& message)
-{
- bool success = WindowManager::the().update_theme(message.theme_path(), message.theme_name());
- return make<Messages::WindowServer::SetSystemThemeResponse>(success);
-}
-
-OwnPtr<Messages::WindowServer::GetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::GetSystemTheme&)
-{
- auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
- auto name = wm_config->read_entry("Theme", "Name");
- return make<Messages::WindowServer::GetSystemThemeResponse>(name);
-}
-
-void ClientConnection::boost()
-{
- // FIXME: Re-enable this when we have a solution for boosting.
-#if 0
- if (set_process_boost(client_pid(), 10) < 0)
- perror("boost: set_process_boost");
-#endif
-}
-
-void ClientConnection::deboost()
-{
- // FIXME: Re-enable this when we have a solution for boosting.
-#if 0
- if (set_process_boost(client_pid(), 0) < 0)
- perror("deboost: set_process_boost");
-#endif
-}
-
-OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
- return {};
- }
-
- auto& window = *it->value;
- window.set_base_size(message.base_size());
- window.set_size_increment(message.size_increment());
-
- return make<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse>();
-}
-
-OwnPtr<Messages::WindowServer::SetWindowResizeAspectRatioResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowResizeAspectRatio& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowResizeAspectRatioResponse: Bad window ID");
- return {};
- }
-
- auto& window = *it->value;
- window.set_resize_aspect_ratio(message.resize_aspect_ratio());
-
- return make<Messages::WindowServer::SetWindowResizeAspectRatioResponse>();
-}
-
-void ClientConnection::handle(const Messages::WindowServer::EnableDisplayLink&)
-{
- if (m_has_display_link)
- return;
- m_has_display_link = true;
- Compositor::the().increment_display_link_count({});
-}
-
-void ClientConnection::handle(const Messages::WindowServer::DisableDisplayLink&)
-{
- if (!m_has_display_link)
- return;
- m_has_display_link = false;
- Compositor::the().decrement_display_link_count({});
-}
-
-void ClientConnection::notify_display_link(Badge<Compositor>)
-{
- if (!m_has_display_link)
- return;
-
- post_message(Messages::WindowClient::DisplayLinkNotification());
-}
-
-void ClientConnection::handle(const Messages::WindowServer::SetWindowProgress& message)
-{
- auto it = m_windows.find(message.window_id());
- if (it == m_windows.end()) {
- did_misbehave("SetWindowProgress with bad window ID");
- return;
- }
- it->value->set_progress(message.progress());
-}
-
-void ClientConnection::handle(const Messages::WindowServer::Pong&)
-{
- m_ping_timer = nullptr;
- set_unresponsive(false);
-}
-
-OwnPtr<Messages::WindowServer::GetGlobalCursorPositionResponse> ClientConnection::handle(const Messages::WindowServer::GetGlobalCursorPosition&)
-{
- return make<Messages::WindowServer::GetGlobalCursorPositionResponse>(Screen::the().cursor_location());
-}
-
-OwnPtr<Messages::WindowServer::SetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::SetMouseAcceleration& message)
-{
- if (message.factor() < mouse_accel_min || message.factor() > mouse_accel_max) {
- did_misbehave("SetMouseAcceleration with bad acceleration factor");
- return {};
- }
- WindowManager::the().set_acceleration_factor(message.factor());
- return make<Messages::WindowServer::SetMouseAccelerationResponse>();
-}
-
-OwnPtr<Messages::WindowServer::GetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::GetMouseAcceleration&)
-{
- return make<Messages::WindowServer::GetMouseAccelerationResponse>(Screen::the().acceleration_factor());
-}
-
-OwnPtr<Messages::WindowServer::SetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::SetScrollStepSize& message)
-{
- if (message.step_size() < scroll_step_size_min) {
- did_misbehave("SetScrollStepSize with bad scroll step size");
- return {};
- }
- WindowManager::the().set_scroll_step_size(message.step_size());
- return make<Messages::WindowServer::SetScrollStepSizeResponse>();
-}
-OwnPtr<Messages::WindowServer::GetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::GetScrollStepSize&)
-{
- return make<Messages::WindowServer::GetScrollStepSizeResponse>(Screen::the().scroll_step_size());
-}
-
-void ClientConnection::set_unresponsive(bool unresponsive)
-{
- if (m_unresponsive == unresponsive)
- return;
- m_unresponsive = unresponsive;
- for (auto& it : m_windows) {
- auto& window = *it.value;
- window.invalidate();
- if (unresponsive)
- window.set_cursor(WindowManager::the().wait_cursor());
- }
- Compositor::the().invalidate_cursor();
-}
-
-void ClientConnection::may_have_become_unresponsive()
-{
- post_message(Messages::WindowClient::Ping());
- m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
- set_unresponsive(true);
- });
-}
-
-void ClientConnection::did_become_responsive()
-{
- set_unresponsive(false);
-}
-
-}
diff --git a/Services/WindowServer/ClientConnection.h b/Services/WindowServer/ClientConnection.h
deleted file mode 100644
index 12174e9189..0000000000
--- a/Services/WindowServer/ClientConnection.h
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/Badge.h>
-#include <AK/Function.h>
-#include <AK/HashMap.h>
-#include <AK/OwnPtr.h>
-#include <AK/WeakPtr.h>
-#include <LibCore/Object.h>
-#include <LibGfx/Bitmap.h>
-#include <LibIPC/ClientConnection.h>
-#include <WindowServer/Event.h>
-#include <WindowServer/WindowClientEndpoint.h>
-#include <WindowServer/WindowServerEndpoint.h>
-
-namespace WindowServer {
-
-class Compositor;
-class Window;
-class Menu;
-class MenuBar;
-
-class ClientConnection final
- : public IPC::ClientConnection<WindowClientEndpoint, WindowServerEndpoint>
- , public WindowServerEndpoint {
- C_OBJECT(ClientConnection)
-public:
- ~ClientConnection() override;
-
- bool is_unresponsive() const { return m_unresponsive; }
-
- void boost();
- void deboost();
-
- static ClientConnection* from_client_id(int client_id);
- static void for_each_client(Function<void(ClientConnection&)>);
-
- MenuBar* app_menubar() { return m_app_menubar.ptr(); }
-
- void notify_about_new_screen_rect(const Gfx::IntRect&);
- void post_paint_message(Window&, bool ignore_occlusion = false);
-
- Menu* find_menu_by_id(int menu_id)
- {
- auto menu = m_menus.get(menu_id);
- if (!menu.has_value())
- return nullptr;
- return const_cast<Menu*>(menu.value().ptr());
- }
- const Menu* find_menu_by_id(int menu_id) const
- {
- auto menu = m_menus.get(menu_id);
- if (!menu.has_value())
- return nullptr;
- return menu.value().ptr();
- }
-
- void notify_display_link(Badge<Compositor>);
-
-private:
- explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
-
- // ^ClientConnection
- virtual void die() override;
- virtual void may_have_become_unresponsive() override;
- virtual void did_become_responsive() override;
-
- void set_unresponsive(bool);
- void destroy_window(Window&, Vector<i32>& destroyed_window_ids);
-
- virtual OwnPtr<Messages::WindowServer::GreetResponse> handle(const Messages::WindowServer::Greet&) override;
- virtual OwnPtr<Messages::WindowServer::CreateMenubarResponse> handle(const Messages::WindowServer::CreateMenubar&) override;
- virtual OwnPtr<Messages::WindowServer::DestroyMenubarResponse> handle(const Messages::WindowServer::DestroyMenubar&) override;
- virtual OwnPtr<Messages::WindowServer::CreateMenuResponse> handle(const Messages::WindowServer::CreateMenu&) override;
- virtual OwnPtr<Messages::WindowServer::DestroyMenuResponse> handle(const Messages::WindowServer::DestroyMenu&) override;
- virtual OwnPtr<Messages::WindowServer::AddMenuToMenubarResponse> handle(const Messages::WindowServer::AddMenuToMenubar&) override;
- virtual OwnPtr<Messages::WindowServer::SetApplicationMenubarResponse> handle(const Messages::WindowServer::SetApplicationMenubar&) override;
- virtual OwnPtr<Messages::WindowServer::AddMenuItemResponse> handle(const Messages::WindowServer::AddMenuItem&) override;
- virtual OwnPtr<Messages::WindowServer::AddMenuSeparatorResponse> handle(const Messages::WindowServer::AddMenuSeparator&) override;
- virtual OwnPtr<Messages::WindowServer::UpdateMenuItemResponse> handle(const Messages::WindowServer::UpdateMenuItem&) override;
- virtual OwnPtr<Messages::WindowServer::CreateWindowResponse> handle(const Messages::WindowServer::CreateWindow&) override;
- virtual OwnPtr<Messages::WindowServer::DestroyWindowResponse> handle(const Messages::WindowServer::DestroyWindow&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowTitleResponse> handle(const Messages::WindowServer::SetWindowTitle&) override;
- virtual OwnPtr<Messages::WindowServer::GetWindowTitleResponse> handle(const Messages::WindowServer::GetWindowTitle&) override;
- virtual OwnPtr<Messages::WindowServer::IsMaximizedResponse> handle(const Messages::WindowServer::IsMaximized&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowRectResponse> handle(const Messages::WindowServer::SetWindowRect&) override;
- virtual OwnPtr<Messages::WindowServer::GetWindowRectResponse> handle(const Messages::WindowServer::GetWindowRect&) override;
- virtual OwnPtr<Messages::WindowServer::GetWindowRectInMenubarResponse> handle(const Messages::WindowServer::GetWindowRectInMenubar&) override;
- virtual void handle(const Messages::WindowServer::InvalidateRect&) override;
- virtual void handle(const Messages::WindowServer::DidFinishPainting&) override;
- virtual OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> handle(const Messages::WindowServer::SetGlobalCursorTracking&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowOpacityResponse> handle(const Messages::WindowServer::SetWindowOpacity&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowBackingStoreResponse> handle(const Messages::WindowServer::SetWindowBackingStore&) override;
- virtual void handle(const Messages::WindowServer::WM_SetActiveWindow&) override;
- virtual void handle(const Messages::WindowServer::WM_SetWindowMinimized&) override;
- virtual void handle(const Messages::WindowServer::WM_StartWindowResize&) override;
- virtual void handle(const Messages::WindowServer::WM_PopupWindowMenu&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> handle(const Messages::WindowServer::SetWindowHasAlphaChannel&) override;
- virtual OwnPtr<Messages::WindowServer::MoveWindowToFrontResponse> handle(const Messages::WindowServer::MoveWindowToFront&) override;
- virtual OwnPtr<Messages::WindowServer::SetFullscreenResponse> handle(const Messages::WindowServer::SetFullscreen&) override;
- virtual void handle(const Messages::WindowServer::AsyncSetWallpaper&) override;
- virtual OwnPtr<Messages::WindowServer::SetBackgroundColorResponse> handle(const Messages::WindowServer::SetBackgroundColor&) override;
- virtual OwnPtr<Messages::WindowServer::SetWallpaperModeResponse> handle(const Messages::WindowServer::SetWallpaperMode&) override;
- virtual OwnPtr<Messages::WindowServer::GetWallpaperResponse> handle(const Messages::WindowServer::GetWallpaper&) override;
- virtual OwnPtr<Messages::WindowServer::SetResolutionResponse> handle(const Messages::WindowServer::SetResolution&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowCursorResponse> handle(const Messages::WindowServer::SetWindowCursor&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowCustomCursorResponse> handle(const Messages::WindowServer::SetWindowCustomCursor&) override;
- virtual OwnPtr<Messages::WindowServer::PopupMenuResponse> handle(const Messages::WindowServer::PopupMenu&) override;
- virtual OwnPtr<Messages::WindowServer::DismissMenuResponse> handle(const Messages::WindowServer::DismissMenu&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowIconBitmapResponse> handle(const Messages::WindowServer::SetWindowIconBitmap&) override;
- virtual void handle(const Messages::WindowServer::WM_SetWindowTaskbarRect&) override;
- virtual OwnPtr<Messages::WindowServer::StartDragResponse> handle(const Messages::WindowServer::StartDrag&) override;
- virtual OwnPtr<Messages::WindowServer::SetSystemMenuResponse> handle(const Messages::WindowServer::SetSystemMenu&) override;
- virtual OwnPtr<Messages::WindowServer::SetSystemThemeResponse> handle(const Messages::WindowServer::SetSystemTheme&) override;
- virtual OwnPtr<Messages::WindowServer::GetSystemThemeResponse> handle(const Messages::WindowServer::GetSystemTheme&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement&) override;
- virtual OwnPtr<Messages::WindowServer::SetWindowResizeAspectRatioResponse> handle(const Messages::WindowServer::SetWindowResizeAspectRatio&) override;
- virtual void handle(const Messages::WindowServer::EnableDisplayLink&) override;
- virtual void handle(const Messages::WindowServer::DisableDisplayLink&) override;
- virtual void handle(const Messages::WindowServer::SetWindowProgress&) override;
- virtual void handle(const Messages::WindowServer::Pong&) override;
- virtual OwnPtr<Messages::WindowServer::GetGlobalCursorPositionResponse> handle(const Messages::WindowServer::GetGlobalCursorPosition&) override;
- virtual OwnPtr<Messages::WindowServer::SetMouseAccelerationResponse> handle(const Messages::WindowServer::SetMouseAcceleration&) override;
- virtual OwnPtr<Messages::WindowServer::GetMouseAccelerationResponse> handle(const Messages::WindowServer::GetMouseAcceleration&) override;
- virtual OwnPtr<Messages::WindowServer::SetScrollStepSizeResponse> handle(const Messages::WindowServer::SetScrollStepSize&) override;
- virtual OwnPtr<Messages::WindowServer::GetScrollStepSizeResponse> handle(const Messages::WindowServer::GetScrollStepSize&) override;
-
- Window* window_from_id(i32 window_id);
-
- HashMap<int, NonnullRefPtr<Window>> m_windows;
- HashMap<int, NonnullOwnPtr<MenuBar>> m_menubars;
- HashMap<int, NonnullRefPtr<Menu>> m_menus;
- WeakPtr<MenuBar> m_app_menubar;
-
- RefPtr<Core::Timer> m_ping_timer;
-
- int m_next_menubar_id { 10000 };
- int m_next_menu_id { 20000 };
- int m_next_window_id { 1982 };
-
- bool m_has_display_link { false };
- bool m_unresponsive { false };
-};
-
-}
diff --git a/Services/WindowServer/Compositor.cpp b/Services/WindowServer/Compositor.cpp
deleted file mode 100644
index 470a8e53a7..0000000000
--- a/Services/WindowServer/Compositor.cpp
+++ /dev/null
@@ -1,1040 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "Compositor.h"
-#include "ClientConnection.h"
-#include "Event.h"
-#include "EventLoop.h"
-#include "Screen.h"
-#include "Window.h"
-#include "WindowManager.h"
-#include <AK/Memory.h>
-#include <AK/ScopeGuard.h>
-#include <LibCore/Timer.h>
-#include <LibGfx/Font.h>
-#include <LibGfx/Painter.h>
-#include <LibGfx/StylePainter.h>
-#include <LibThread/BackgroundAction.h>
-
-//#define COMPOSE_DEBUG
-//#define OCCLUSIONS_DEBUG
-
-namespace WindowServer {
-
-Compositor& Compositor::the()
-{
- static Compositor s_the;
- return s_the;
-}
-
-static WallpaperMode mode_to_enum(const String& name)
-{
- if (name == "simple")
- return WallpaperMode::Simple;
- if (name == "tile")
- return WallpaperMode::Tile;
- if (name == "center")
- return WallpaperMode::Center;
- if (name == "scaled")
- return WallpaperMode::Scaled;
- return WallpaperMode::Simple;
-}
-
-Compositor::Compositor()
-{
- m_display_link_notify_timer = add<Core::Timer>(
- 1000 / 60, [this] {
- notify_display_links();
- });
- m_display_link_notify_timer->stop();
-
- m_compose_timer = Core::Timer::create_single_shot(
- 1000 / 60,
- [this] {
- compose();
- },
- this);
-
- m_immediate_compose_timer = Core::Timer::create_single_shot(
- 0,
- [this] {
- compose();
- },
- this);
-
- m_screen_can_set_buffer = Screen::the().can_set_buffer();
- init_bitmaps();
-}
-
-void Compositor::init_bitmaps()
-{
- auto& screen = Screen::the();
- auto size = screen.size();
-
- m_front_bitmap = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGB32, size, screen.pitch(), screen.scanline(0));
-
- if (m_screen_can_set_buffer)
- m_back_bitmap = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::RGB32, size, screen.pitch(), screen.scanline(size.height()));
- else
- m_back_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, size);
-
- m_temp_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, size);
-
- m_front_painter = make<Gfx::Painter>(*m_front_bitmap);
- m_back_painter = make<Gfx::Painter>(*m_back_bitmap);
- m_temp_painter = make<Gfx::Painter>(*m_temp_bitmap);
-
- m_buffers_are_flipped = false;
-
- invalidate_screen();
-}
-
-void Compositor::did_construct_window_manager(Badge<WindowManager>)
-{
- auto& wm = WindowManager::the();
- m_wallpaper_mode = mode_to_enum(wm.config()->read_entry("Background", "Mode", "simple"));
- m_custom_background_color = Color::from_string(wm.config()->read_entry("Background", "Color", ""));
-
- invalidate_screen();
- invalidate_occlusions();
- compose();
-}
-
-void Compositor::compose()
-{
- auto& wm = WindowManager::the();
- auto& ws = Screen::the();
-
- {
- auto& current_cursor = wm.active_cursor();
- if (m_current_cursor != &current_cursor)
- change_cursor(&current_cursor);
- }
-
- if (!m_invalidated_any) {
- // nothing dirtied since the last compose pass.
- return;
- }
-
- if (m_occlusions_dirty) {
- m_occlusions_dirty = false;
- recompute_occlusions();
- }
-
- auto dirty_screen_rects = move(m_dirty_screen_rects);
- dirty_screen_rects.add(m_last_geometry_label_damage_rect.intersected(ws.rect()));
- dirty_screen_rects.add(m_last_dnd_rect.intersected(ws.rect()));
- if (m_invalidated_cursor) {
- if (wm.dnd_client())
- dirty_screen_rects.add(wm.dnd_rect().intersected(ws.rect()));
- }
-
- // Mark window regions as dirty that need to be re-rendered
- wm.for_each_visible_window_from_back_to_front([&](Window& window) {
- auto frame_rect = window.frame().rect();
- for (auto& dirty_rect : dirty_screen_rects.rects()) {
- auto invalidate_rect = dirty_rect.intersected(frame_rect);
- if (!invalidate_rect.is_empty()) {
- auto inner_rect_offset = window.rect().location() - frame_rect.location();
- invalidate_rect.move_by(-(frame_rect.location() + inner_rect_offset));
- window.invalidate_no_notify(invalidate_rect);
- m_invalidated_window = true;
- }
- }
- window.prepare_dirty_rects();
- return IterationDecision::Continue;
- });
-
- // Any windows above or below a given window that need to be re-rendered
- // also require us to re-render that window's intersecting area, regardless
- // of whether that window has any dirty rectangles
- wm.for_each_visible_window_from_back_to_front([&](Window& window) {
- auto& transparency_rects = window.transparency_rects();
- if (transparency_rects.is_empty())
- return IterationDecision::Continue;
-
- auto frame_rect = window.frame().rect();
- auto& dirty_rects = window.dirty_rects();
- wm.for_each_visible_window_from_back_to_front([&](Window& w) {
- if (&w == &window)
- return IterationDecision::Continue;
- auto frame_rect2 = w.frame().rect();
- if (!frame_rect2.intersects(frame_rect))
- return IterationDecision::Continue;
- transparency_rects.for_each_intersected(w.dirty_rects(), [&](const Gfx::IntRect& intersected_dirty) {
- dirty_rects.add(intersected_dirty);
- return IterationDecision::Continue;
- });
- return IterationDecision::Continue;
- });
- return IterationDecision::Continue;
- });
-
- Color background_color = wm.palette().desktop_background();
- if (m_custom_background_color.has_value())
- background_color = m_custom_background_color.value();
-
-#ifdef COMPOSE_DEBUG
- dbg() << "COMPOSE: invalidated: window:" << m_invalidated_window << " cursor:" << m_invalidated_cursor << " any: " << m_invalidated_any;
- for (auto& r : dirty_screen_rects.rects())
- dbg() << "dirty screen: " << r;
-#endif
- Gfx::DisjointRectSet flush_rects;
- Gfx::DisjointRectSet flush_transparent_rects;
- Gfx::DisjointRectSet flush_special_rects;
- auto cursor_rect = current_cursor_rect();
- bool need_to_draw_cursor = false;
-
- auto check_restore_cursor_back = [&](const Gfx::IntRect& rect) {
- if (!need_to_draw_cursor && rect.intersects(cursor_rect)) {
- // Restore what's behind the cursor if anything touches the area of the cursor
- need_to_draw_cursor = true;
- restore_cursor_back();
- }
- };
-
- auto prepare_rect = [&](const Gfx::IntRect& rect) {
-#ifdef COMPOSE_DEBUG
- dbg() << " -> flush opaque: " << rect;
-#endif
- ASSERT(!flush_rects.intersects(rect));
- ASSERT(!flush_transparent_rects.intersects(rect));
- flush_rects.add(rect);
- check_restore_cursor_back(rect);
- };
-
- auto prepare_transparency_rect = [&](const Gfx::IntRect& rect) {
-#ifdef COMPOSE_DEBUG
- dbg() << " -> flush transparent: " << rect;
-#endif
- ASSERT(!flush_rects.intersects(rect));
- bool have_rect = false;
- for (auto& r : flush_transparent_rects.rects()) {
- if (r == rect) {
- have_rect = true;
- break;
- }
- }
-
- if (!have_rect) {
- flush_transparent_rects.add(rect);
- check_restore_cursor_back(rect);
- }
- };
-
- if (!m_cursor_back_bitmap || m_invalidated_cursor)
- check_restore_cursor_back(cursor_rect);
-
- auto back_painter = *m_back_painter;
- auto temp_painter = *m_temp_painter;
-
- auto paint_wallpaper = [&](Gfx::Painter& painter, const Gfx::IntRect& rect) {
- // FIXME: If the wallpaper is opaque, no need to fill with color!
- painter.fill_rect(rect, background_color);
- if (m_wallpaper) {
- if (m_wallpaper_mode == WallpaperMode::Simple) {
- painter.blit(rect.location(), *m_wallpaper, rect);
- } else if (m_wallpaper_mode == WallpaperMode::Center) {
- Gfx::IntPoint offset { ws.size().width() / 2 - m_wallpaper->size().width() / 2,
- ws.size().height() / 2 - m_wallpaper->size().height() / 2 };
- painter.blit_offset(rect.location(), *m_wallpaper,
- rect, offset);
- } else if (m_wallpaper_mode == WallpaperMode::Tile) {
- painter.draw_tiled_bitmap(rect, *m_wallpaper);
- } else if (m_wallpaper_mode == WallpaperMode::Scaled) {
- float hscale = (float)m_wallpaper->size().width() / (float)ws.size().width();
- float vscale = (float)m_wallpaper->size().height() / (float)ws.size().height();
-
- // TODO: this may look ugly, we should scale to a backing bitmap and then blit
- painter.blit_scaled(rect, *m_wallpaper, rect, hscale, vscale);
- } else {
- ASSERT_NOT_REACHED();
- }
- }
- };
-
- m_opaque_wallpaper_rects.for_each_intersected(dirty_screen_rects, [&](const Gfx::IntRect& render_rect) {
-#ifdef COMPOSE_DEBUG
- dbg() << " render wallpaper opaque: " << render_rect;
-#endif
- prepare_rect(render_rect);
- paint_wallpaper(back_painter, render_rect);
- return IterationDecision::Continue;
- });
-
- auto compose_window = [&](Window& window) -> IterationDecision {
- auto frame_rect = window.frame().rect();
- if (!frame_rect.intersects(ws.rect()))
- return IterationDecision::Continue;
- auto frame_rects = frame_rect.shatter(window.rect());
-
-#ifdef COMPOSE_DEBUG
- dbg() << " window " << window.title() << " frame rect: " << frame_rect;
-#endif
-
- RefPtr<Gfx::Bitmap> backing_store = window.backing_store();
- auto compose_window_rect = [&](Gfx::Painter& painter, const Gfx::IntRect& rect) {
- if (!window.is_fullscreen()) {
- rect.for_each_intersected(frame_rects, [&](const Gfx::IntRect& intersected_rect) {
- // TODO: Should optimize this to use a backing buffer
- Gfx::PainterStateSaver saver(painter);
- painter.add_clip_rect(intersected_rect);
-#ifdef COMPOSE_DEBUG
- dbg() << " render frame: " << intersected_rect;
-#endif
- window.frame().paint(painter);
- return IterationDecision::Continue;
- });
- }
-
- if (!backing_store) {
- if (window.is_opaque())
- painter.fill_rect(window.rect().intersected(rect), wm.palette().window());
- return;
- }
-
- // Decide where we would paint this window's backing store.
- // This is subtly different from widow.rect(), because window
- // size may be different from its backing store size. This
- // happens when the window has been resized and the client
- // has not yet attached a new backing store. In this case,
- // we want to try to blit the backing store at the same place
- // it was previously, and fill the rest of the window with its
- // background color.
- Gfx::IntRect backing_rect;
- backing_rect.set_size(backing_store->size());
- switch (WindowManager::the().resize_direction_of_window(window)) {
- case ResizeDirection::None:
- case ResizeDirection::Right:
- case ResizeDirection::Down:
- case ResizeDirection::DownRight:
- backing_rect.set_location(window.rect().location());
- break;
- case ResizeDirection::Left:
- case ResizeDirection::Up:
- case ResizeDirection::UpLeft:
- backing_rect.set_right_without_resize(window.rect().right());
- backing_rect.set_bottom_without_resize(window.rect().bottom());
- break;
- case ResizeDirection::UpRight:
- backing_rect.set_left(window.rect().left());
- backing_rect.set_bottom_without_resize(window.rect().bottom());
- break;
- case ResizeDirection::DownLeft:
- backing_rect.set_right_without_resize(window.rect().right());
- backing_rect.set_top(window.rect().top());
- break;
- }
-
- Gfx::IntRect dirty_rect_in_backing_coordinates = rect.intersected(window.rect())
- .intersected(backing_rect)
- .translated(-backing_rect.location());
-
- if (dirty_rect_in_backing_coordinates.is_empty())
- return;
- auto dst = backing_rect.location().translated(dirty_rect_in_backing_coordinates.location());
-
- if (window.client() && window.client()->is_unresponsive()) {
- painter.blit_filtered(dst, *backing_store, dirty_rect_in_backing_coordinates, [](Color src) {
- return src.to_grayscale().darkened(0.75f);
- });
- } else {
- painter.blit(dst, *backing_store, dirty_rect_in_backing_coordinates, window.opacity());
- }
-
- if (window.is_opaque()) {
- for (auto background_rect : window.rect().shatter(backing_rect))
- painter.fill_rect(background_rect, wm.palette().window());
- }
- };
-
- auto& dirty_rects = window.dirty_rects();
-#ifdef COMPOSE_DEBUG
- for (auto& dirty_rect : dirty_rects.rects())
- dbg() << " dirty: " << dirty_rect;
- for (auto& r : window.opaque_rects().rects())
- dbg() << " opaque: " << r;
- for (auto& r : window.transparency_rects().rects())
- dbg() << " transparent: " << r;
-#endif
-
- // Render opaque portions directly to the back buffer
- auto& opaque_rects = window.opaque_rects();
- if (!opaque_rects.is_empty()) {
- opaque_rects.for_each_intersected(dirty_rects, [&](const Gfx::IntRect& render_rect) {
-#ifdef COMPOSE_DEBUG
- dbg() << " render opaque: " << render_rect;
-#endif
- prepare_rect(render_rect);
- Gfx::PainterStateSaver saver(back_painter);
- back_painter.add_clip_rect(render_rect);
- compose_window_rect(back_painter, render_rect);
- return IterationDecision::Continue;
- });
- }
-
- // Render the wallpaper for any transparency directly covering
- // the wallpaper
- auto& transparency_wallpaper_rects = window.transparency_wallpaper_rects();
- if (!transparency_wallpaper_rects.is_empty()) {
- transparency_wallpaper_rects.for_each_intersected(dirty_rects, [&](const Gfx::IntRect& render_rect) {
-#ifdef COMPOSE_DEBUG
- dbg() << " render wallpaper: " << render_rect;
-#endif
- prepare_transparency_rect(render_rect);
- paint_wallpaper(temp_painter, render_rect);
- return IterationDecision::Continue;
- });
- }
- auto& transparency_rects = window.transparency_rects();
- if (!transparency_rects.is_empty()) {
- transparency_rects.for_each_intersected(dirty_rects, [&](const Gfx::IntRect& render_rect) {
-#ifdef COMPOSE_DEBUG
- dbg() << " render transparent: " << render_rect;
-#endif
- prepare_transparency_rect(render_rect);
- Gfx::PainterStateSaver saver(temp_painter);
- temp_painter.add_clip_rect(render_rect);
- compose_window_rect(temp_painter, render_rect);
- return IterationDecision::Continue;
- });
- }
- return IterationDecision::Continue;
- };
-
- // Paint the window stack.
- if (m_invalidated_window) {
- if (auto* fullscreen_window = wm.active_fullscreen_window()) {
- compose_window(*fullscreen_window);
- } else {
- wm.for_each_visible_window_from_back_to_front([&](Window& window) {
- compose_window(window);
- window.clear_dirty_rects();
- return IterationDecision::Continue;
- });
- }
-
- // Check that there are no overlapping transparent and opaque flush rectangles
- ASSERT(![&]() {
- for (auto& rect_transparent : flush_transparent_rects.rects()) {
- for (auto& rect_opaque : flush_rects.rects()) {
- if (rect_opaque.intersects(rect_transparent)) {
- dbg() << "Transparent rect " << rect_transparent << " overlaps opaque rect: " << rect_opaque << ": " << rect_opaque.intersected(rect_transparent);
- return true;
- }
- }
- }
- return false;
- }());
-
- // Copy anything rendered to the temporary buffer to the back buffer
- for (auto& rect : flush_transparent_rects.rects())
- back_painter.blit(rect.location(), *m_temp_bitmap, rect);
-
- Gfx::IntRect geometry_label_damage_rect;
- if (draw_geometry_label(geometry_label_damage_rect))
- flush_special_rects.add(geometry_label_damage_rect);
- }
-
- m_invalidated_any = false;
- m_invalidated_window = false;
- m_invalidated_cursor = false;
-
- if (wm.dnd_client()) {
- auto dnd_rect = wm.dnd_rect();
-
- // TODO: render once into a backing bitmap, then just blit...
- auto render_dnd = [&]() {
- back_painter.fill_rect(dnd_rect, wm.palette().selection().with_alpha(200));
- back_painter.draw_rect(dnd_rect, wm.palette().selection());
- if (!wm.dnd_text().is_empty()) {
- auto text_rect = dnd_rect;
- if (wm.dnd_bitmap())
- text_rect.move_by(wm.dnd_bitmap()->width() + 8, 0);
- back_painter.draw_text(text_rect, wm.dnd_text(), Gfx::TextAlignment::CenterLeft, wm.palette().selection_text());
- }
- if (wm.dnd_bitmap()) {
- back_painter.blit(dnd_rect.top_left().translated(4, 4), *wm.dnd_bitmap(), wm.dnd_bitmap()->rect());
- }
- };
-
- dirty_screen_rects.for_each_intersected(dnd_rect, [&](const Gfx::IntRect& render_rect) {
- Gfx::PainterStateSaver saver(back_painter);
- back_painter.add_clip_rect(render_rect);
- render_dnd();
- return IterationDecision::Continue;
- });
- flush_transparent_rects.for_each_intersected(dnd_rect, [&](const Gfx::IntRect& render_rect) {
- Gfx::PainterStateSaver saver(back_painter);
- back_painter.add_clip_rect(render_rect);
- render_dnd();
- return IterationDecision::Continue;
- });
- m_last_dnd_rect = dnd_rect;
- } else {
- if (!m_last_dnd_rect.is_empty()) {
- invalidate_screen(m_last_dnd_rect);
- m_last_dnd_rect = {};
- }
- }
-
- run_animations(flush_special_rects);
-
- if (need_to_draw_cursor) {
- flush_rects.add(cursor_rect);
- if (cursor_rect != m_last_cursor_rect)
- flush_rects.add(m_last_cursor_rect);
- draw_cursor(cursor_rect);
- }
-
- if (m_flash_flush) {
- for (auto& rect : flush_rects.rects())
- m_front_painter->fill_rect(rect, Color::Yellow);
- }
-
- if (m_screen_can_set_buffer)
- flip_buffers();
-
- for (auto& rect : flush_rects.rects())
- flush(rect);
- for (auto& rect : flush_transparent_rects.rects())
- flush(rect);
- for (auto& rect : flush_special_rects.rects())
- flush(rect);
-}
-
-void Compositor::flush(const Gfx::IntRect& a_rect)
-{
- auto rect = Gfx::IntRect::intersection(a_rect, Screen::the().rect());
-
- Gfx::RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
- Gfx::RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
- size_t pitch = m_back_bitmap->pitch();
-
- // NOTE: The meaning of a flush depends on whether we can flip buffers or not.
- //
- // If flipping is supported, flushing means that we've flipped, and now we
- // copy the changed bits from the front buffer to the back buffer, to keep
- // them in sync.
- //
- // If flipping is not supported, flushing means that we copy the changed
- // rects from the backing bitmap to the display framebuffer.
-
- Gfx::RGBA32* to_ptr;
- const Gfx::RGBA32* from_ptr;
-
- if (m_screen_can_set_buffer) {
- to_ptr = back_ptr;
- from_ptr = front_ptr;
- } else {
- to_ptr = front_ptr;
- from_ptr = back_ptr;
- }
-
- for (int y = 0; y < rect.height(); ++y) {
- fast_u32_copy(to_ptr, from_ptr, rect.width());
- from_ptr = (const Gfx::RGBA32*)((const u8*)from_ptr + pitch);
- to_ptr = (Gfx::RGBA32*)((u8*)to_ptr + pitch);
- }
-}
-
-void Compositor::invalidate_screen()
-{
- invalidate_screen(Screen::the().rect());
-}
-
-void Compositor::invalidate_screen(const Gfx::IntRect& screen_rect)
-{
- m_dirty_screen_rects.add(screen_rect.intersected(Screen::the().rect()));
-
- if (m_invalidated_any)
- return;
-
- m_invalidated_any = true;
- m_invalidated_window = true;
- start_compose_async_timer();
-}
-
-void Compositor::invalidate_window()
-{
- if (m_invalidated_window)
- return;
- m_invalidated_window = true;
- m_invalidated_any = true;
-
- start_compose_async_timer();
-}
-
-void Compositor::start_compose_async_timer()
-{
- // We delay composition by a timer interval, but to not affect latency too
- // much, if a pending compose is not already scheduled, we also schedule an
- // immediate compose the next spin of the event loop.
- if (!m_compose_timer->is_active()) {
- m_compose_timer->start();
- m_immediate_compose_timer->start();
- }
-}
-
-bool Compositor::set_background_color(const String& background_color)
-{
- auto color = Color::from_string(background_color);
- if (!color.has_value())
- return false;
-
- m_custom_background_color = color;
-
- auto& wm = WindowManager::the();
- wm.config()->write_entry("Background", "Color", background_color);
- bool ret_val = wm.config()->sync();
-
- if (ret_val)
- Compositor::invalidate_screen();
-
- return ret_val;
-}
-
-bool Compositor::set_wallpaper_mode(const String& mode)
-{
- auto& wm = WindowManager::the();
- wm.config()->write_entry("Background", "Mode", mode);
- bool ret_val = wm.config()->sync();
-
- if (ret_val) {
- m_wallpaper_mode = mode_to_enum(mode);
- Compositor::invalidate_screen();
- }
-
- return ret_val;
-}
-
-bool Compositor::set_wallpaper(const String& path, Function<void(bool)>&& callback)
-{
- LibThread::BackgroundAction<RefPtr<Gfx::Bitmap>>::create(
- [path] {
- return Gfx::Bitmap::load_from_file(path);
- },
-
- [this, path, callback = move(callback)](RefPtr<Gfx::Bitmap> bitmap) {
- m_wallpaper_path = path;
- m_wallpaper = move(bitmap);
- invalidate_screen();
- callback(true);
- });
- return true;
-}
-
-void Compositor::flip_buffers()
-{
- ASSERT(m_screen_can_set_buffer);
- swap(m_front_bitmap, m_back_bitmap);
- swap(m_front_painter, m_back_painter);
- Screen::the().set_buffer(m_buffers_are_flipped ? 0 : 1);
- m_buffers_are_flipped = !m_buffers_are_flipped;
-}
-
-void Compositor::run_animations(Gfx::DisjointRectSet& flush_rects)
-{
- static const int minimize_animation_steps = 10;
- auto& painter = *m_back_painter;
- Gfx::PainterStateSaver saver(painter);
- painter.set_draw_op(Gfx::Painter::DrawOp::Invert);
-
- WindowManager::the().for_each_window([&](Window& window) {
- if (window.in_minimize_animation()) {
- int animation_index = window.minimize_animation_index();
-
- auto from_rect = window.is_minimized() ? window.frame().rect() : window.taskbar_rect();
- auto to_rect = window.is_minimized() ? window.taskbar_rect() : window.frame().rect();
-
- float x_delta_per_step = (float)(from_rect.x() - to_rect.x()) / minimize_animation_steps;
- float y_delta_per_step = (float)(from_rect.y() - to_rect.y()) / minimize_animation_steps;
- float width_delta_per_step = (float)(from_rect.width() - to_rect.width()) / minimize_animation_steps;
- float height_delta_per_step = (float)(from_rect.height() - to_rect.height()) / minimize_animation_steps;
-
- Gfx::IntRect rect {
- from_rect.x() - (int)(x_delta_per_step * animation_index),
- from_rect.y() - (int)(y_delta_per_step * animation_index),
- from_rect.width() - (int)(width_delta_per_step * animation_index),
- from_rect.height() - (int)(height_delta_per_step * animation_index)
- };
-
-#ifdef MINIMIZE_ANIMATION_DEBUG
- dbg() << "Minimize animation from " << from_rect << " to " << to_rect << " frame# " << animation_index << " " << rect;
-#endif
-
- painter.draw_rect(rect, Color::Transparent); // Color doesn't matter, we draw inverted
- flush_rects.add(rect);
- invalidate_screen(rect);
-
- window.step_minimize_animation();
- if (window.minimize_animation_index() >= minimize_animation_steps)
- window.end_minimize_animation();
- }
- return IterationDecision::Continue;
- });
-}
-
-bool Compositor::set_resolution(int desired_width, int desired_height)
-{
- auto screen_rect = Screen::the().rect();
- if (screen_rect.width() == desired_width && screen_rect.height() == desired_height)
- return true;
-
- // Make sure it's impossible to set an invalid resolution
- if (!(desired_width >= 640 && desired_height >= 480)) {
- dbg() << "Compositor: Tried to set invalid resolution: " << desired_width << "x" << desired_height;
- return false;
- }
- bool success = Screen::the().set_resolution(desired_width, desired_height);
- init_bitmaps();
- invalidate_occlusions();
- compose();
- return success;
-}
-
-Gfx::IntRect Compositor::current_cursor_rect() const
-{
- auto& wm = WindowManager::the();
- auto& current_cursor = m_current_cursor ? *m_current_cursor : wm.active_cursor();
- return { Screen::the().cursor_location().translated(-current_cursor.params().hotspot()), current_cursor.size() };
-}
-
-void Compositor::invalidate_cursor(bool compose_immediately)
-{
- if (m_invalidated_cursor)
- return;
- m_invalidated_cursor = true;
- m_invalidated_any = true;
-
- if (compose_immediately)
- compose();
- else
- start_compose_async_timer();
-}
-
-bool Compositor::draw_geometry_label(Gfx::IntRect& geometry_label_damage_rect)
-{
- auto& wm = WindowManager::the();
- auto* window_being_moved_or_resized = wm.m_move_window ? wm.m_move_window.ptr() : (wm.m_resize_window ? wm.m_resize_window.ptr() : nullptr);
- if (!window_being_moved_or_resized) {
- m_last_geometry_label_damage_rect = {};
- return false;
- }
- auto geometry_string = window_being_moved_or_resized->rect().to_string();
- if (!window_being_moved_or_resized->size_increment().is_null()) {
- int width_steps = (window_being_moved_or_resized->width() - window_being_moved_or_resized->base_size().width()) / window_being_moved_or_resized->size_increment().width();
- int height_steps = (window_being_moved_or_resized->height() - window_being_moved_or_resized->base_size().height()) / window_being_moved_or_resized->size_increment().height();
- geometry_string = String::format("%s (%dx%d)", geometry_string.characters(), width_steps, height_steps);
- }
- auto geometry_label_rect = Gfx::IntRect { 0, 0, wm.font().width(geometry_string) + 16, wm.font().glyph_height() + 10 };
- geometry_label_rect.center_within(window_being_moved_or_resized->rect());
- auto& back_painter = *m_back_painter;
- back_painter.fill_rect(geometry_label_rect.translated(1, 1), Color(Color::Black).with_alpha(80));
- Gfx::StylePainter::paint_button(back_painter, geometry_label_rect.translated(-1, -1), wm.palette(), Gfx::ButtonStyle::Normal, false);
- back_painter.draw_text(geometry_label_rect.translated(-1, -1), geometry_string, Gfx::TextAlignment::Center, wm.palette().window_text());
-
- geometry_label_damage_rect = geometry_label_rect.inflated(2, 2);
- m_last_geometry_label_damage_rect = geometry_label_damage_rect;
- return true;
-}
-
-void Compositor::change_cursor(const Cursor* cursor)
-{
- if (m_current_cursor == cursor)
- return;
- m_current_cursor = cursor;
- m_current_cursor_frame = 0;
- if (m_cursor_timer) {
- m_cursor_timer->stop();
- m_cursor_timer = nullptr;
- }
- if (cursor && cursor->params().frames() > 1 && cursor->params().frame_ms() != 0) {
- m_cursor_timer = add<Core::Timer>(
- cursor->params().frame_ms(), [this, cursor] {
- if (m_current_cursor != cursor)
- return;
- auto frames = cursor->params().frames();
- if (++m_current_cursor_frame >= frames)
- m_current_cursor_frame = 0;
- invalidate_cursor(true);
- });
- }
-}
-
-void Compositor::draw_cursor(const Gfx::IntRect& cursor_rect)
-{
- auto& wm = WindowManager::the();
-
- if (!m_cursor_back_bitmap || m_cursor_back_bitmap->size() != cursor_rect.size()) {
- m_cursor_back_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, cursor_rect.size());
- m_cursor_back_painter = make<Gfx::Painter>(*m_cursor_back_bitmap);
- }
-
- auto& current_cursor = m_current_cursor ? *m_current_cursor : wm.active_cursor();
- m_cursor_back_painter->blit({ 0, 0 }, *m_back_bitmap, current_cursor.rect().translated(cursor_rect.location()).intersected(Screen::the().rect()));
- auto& back_painter = *m_back_painter;
- back_painter.blit(cursor_rect.location(), current_cursor.bitmap(), current_cursor.source_rect(m_current_cursor_frame));
-
- m_last_cursor_rect = cursor_rect;
-}
-
-void Compositor::restore_cursor_back()
-{
- if (!m_cursor_back_bitmap)
- return;
-
- m_back_painter->blit(m_last_cursor_rect.location().constrained(Screen::the().rect()), *m_cursor_back_bitmap, { { 0, 0 }, m_last_cursor_rect.intersected(Screen::the().rect()).size() });
-}
-
-void Compositor::notify_display_links()
-{
- ClientConnection::for_each_client([](auto& client) {
- client.notify_display_link({});
- });
-}
-
-void Compositor::increment_display_link_count(Badge<ClientConnection>)
-{
- ++m_display_link_count;
- if (m_display_link_count == 1)
- m_display_link_notify_timer->start();
-}
-
-void Compositor::decrement_display_link_count(Badge<ClientConnection>)
-{
- ASSERT(m_display_link_count);
- --m_display_link_count;
- if (!m_display_link_count)
- m_display_link_notify_timer->stop();
-}
-
-bool Compositor::any_opaque_window_above_this_one_contains_rect(const Window& a_window, const Gfx::IntRect& rect)
-{
- bool found_containing_window = false;
- bool checking = false;
- WindowManager::the().for_each_visible_window_from_back_to_front([&](Window& window) {
- if (&window == &a_window) {
- checking = true;
- return IterationDecision::Continue;
- }
- if (!checking)
- return IterationDecision::Continue;
- if (!window.is_visible())
- return IterationDecision::Continue;
- if (window.is_minimized())
- return IterationDecision::Continue;
- if (!window.is_opaque())
- return IterationDecision::Continue;
- if (window.frame().rect().contains(rect)) {
- found_containing_window = true;
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
- });
- return found_containing_window;
-};
-
-void Compositor::recompute_occlusions()
-{
- auto& wm = WindowManager::the();
- wm.for_each_visible_window_from_back_to_front([&](Window& window) {
- if (wm.m_switcher.is_visible()) {
- window.set_occluded(false);
- } else {
- if (any_opaque_window_above_this_one_contains_rect(window, window.frame().rect()))
- window.set_occluded(true);
- else
- window.set_occluded(false);
- }
- return IterationDecision::Continue;
- });
-
-#ifdef OCCLUSIONS_DEBUG
- dbgln("OCCLUSIONS:");
-#endif
-
- auto screen_rect = Screen::the().rect();
-
- if (auto* fullscreen_window = wm.active_fullscreen_window()) {
- WindowManager::the().for_each_visible_window_from_front_to_back([&](Window& w) {
- auto& visible_opaque = w.opaque_rects();
- auto& transparency_rects = w.transparency_rects();
- auto& transparency_wallpaper_rects = w.transparency_wallpaper_rects();
- if (&w == fullscreen_window) {
- if (w.is_opaque()) {
- visible_opaque = screen_rect;
- transparency_rects.clear();
- transparency_wallpaper_rects.clear();
- } else {
- visible_opaque.clear();
- transparency_rects = screen_rect;
- transparency_wallpaper_rects = screen_rect;
- }
- } else {
- visible_opaque.clear();
- transparency_rects.clear();
- transparency_wallpaper_rects.clear();
- }
- return IterationDecision::Continue;
- });
-
- m_opaque_wallpaper_rects.clear();
- } else {
- Gfx::DisjointRectSet visible_rects(screen_rect);
- bool have_transparent = false;
- WindowManager::the().for_each_visible_window_from_front_to_back([&](Window& w) {
- auto window_frame_rect = w.frame().rect().intersected(screen_rect);
- w.transparency_wallpaper_rects().clear();
- auto& visible_opaque = w.opaque_rects();
- auto& transparency_rects = w.transparency_rects();
- if (w.is_minimized() || window_frame_rect.is_empty()) {
- visible_opaque.clear();
- transparency_rects.clear();
- return IterationDecision::Continue;
- }
-
- Gfx::DisjointRectSet opaque_covering;
- if (w.is_opaque()) {
- visible_opaque = visible_rects.intersected(window_frame_rect);
- transparency_rects.clear();
- } else {
- visible_opaque.clear();
- transparency_rects = visible_rects.intersected(window_frame_rect);
- }
-
- bool found_this_window = false;
- WindowManager::the().for_each_visible_window_from_back_to_front([&](Window& w2) {
- if (!found_this_window) {
- if (&w == &w2)
- found_this_window = true;
- return IterationDecision::Continue;
- }
-
- if (w2.is_minimized())
- return IterationDecision::Continue;
- auto window_frame_rect2 = w2.frame().rect().intersected(screen_rect);
- auto covering_rect = window_frame_rect2.intersected(window_frame_rect);
- if (covering_rect.is_empty())
- return IterationDecision::Continue;
-
- if (w2.is_opaque()) {
- opaque_covering.add(covering_rect);
- if (opaque_covering.contains(window_frame_rect)) {
- // This window is entirely covered by another opaque window
- visible_opaque.clear();
- transparency_rects.clear();
- return IterationDecision::Break;
- }
-
- if (!visible_opaque.is_empty()) {
- auto uncovered_opaque = visible_opaque.shatter(covering_rect);
- visible_opaque = move(uncovered_opaque);
- }
-
- if (!transparency_rects.is_empty()) {
- auto uncovered_transparency = transparency_rects.shatter(covering_rect);
- transparency_rects = move(uncovered_transparency);
- }
- } else {
- visible_rects.for_each_intersected(covering_rect, [&](const Gfx::IntRect& intersected) {
- transparency_rects.add(intersected);
- if (!visible_opaque.is_empty()) {
- auto uncovered_opaque = visible_opaque.shatter(intersected);
- visible_opaque = move(uncovered_opaque);
- }
- return IterationDecision::Continue;
- });
- }
-
- return IterationDecision::Continue;
- });
-
- if (!transparency_rects.is_empty())
- have_transparent = true;
-
- ASSERT(!visible_opaque.intersects(transparency_rects));
-
- if (w.is_opaque()) {
- // Determine visible area for the window below
- auto visible_rects_below_window = visible_rects.shatter(window_frame_rect);
- visible_rects = move(visible_rects_below_window);
- }
- return IterationDecision::Continue;
- });
-
- if (have_transparent) {
- // Determine what transparent window areas need to render the wallpaper first
- WindowManager::the().for_each_visible_window_from_back_to_front([&](Window& w) {
- auto& transparency_wallpaper_rects = w.transparency_wallpaper_rects();
- if (w.is_opaque() || w.is_minimized()) {
- transparency_wallpaper_rects.clear();
- return IterationDecision::Continue;
- }
- Gfx::DisjointRectSet& transparency_rects = w.transparency_rects();
- if (transparency_rects.is_empty()) {
- transparency_wallpaper_rects.clear();
- return IterationDecision::Continue;
- }
-
- transparency_wallpaper_rects = visible_rects.intersected(transparency_rects);
-
- auto remaining_visible = visible_rects.shatter(transparency_wallpaper_rects);
- visible_rects = move(remaining_visible);
- return IterationDecision::Continue;
- });
- }
-
- m_opaque_wallpaper_rects = move(visible_rects);
- }
-
-#ifdef OCCLUSIONS_DEBUG
- for (auto& r : m_opaque_wallpaper_rects.rects())
- dbg() << " wallpaper opaque: " << r;
-#endif
-
- wm.for_each_visible_window_from_back_to_front([&](Window& w) {
- auto window_frame_rect = w.frame().rect().intersected(screen_rect);
- if (w.is_minimized() || window_frame_rect.is_empty())
- return IterationDecision::Continue;
-
-#ifdef OCCLUSIONS_DEBUG
- dbg() << " Window " << w.title() << " frame rect: " << window_frame_rect;
- for (auto& r : w.opaque_rects().rects())
- dbg() << " opaque: " << r;
- for (auto& r : w.transparency_wallpaper_rects().rects())
- dbg() << " transparent wallpaper: " << r;
- for (auto& r : w.transparency_rects().rects())
- dbg() << " transparent: " << r;
-#endif
- ASSERT(!w.opaque_rects().intersects(m_opaque_wallpaper_rects));
- ASSERT(!w.transparency_rects().intersects(m_opaque_wallpaper_rects));
- ASSERT(!w.transparency_wallpaper_rects().intersects(m_opaque_wallpaper_rects));
- return IterationDecision::Continue;
- });
-}
-
-}
diff --git a/Services/WindowServer/Compositor.h b/Services/WindowServer/Compositor.h
deleted file mode 100644
index 1e6a3e2014..0000000000
--- a/Services/WindowServer/Compositor.h
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/OwnPtr.h>
-#include <AK/RefPtr.h>
-#include <LibCore/Object.h>
-#include <LibGfx/Color.h>
-#include <LibGfx/DisjointRectSet.h>
-
-namespace WindowServer {
-
-class ClientConnection;
-class Cursor;
-class Window;
-class WindowManager;
-
-enum class WallpaperMode {
- Simple,
- Tile,
- Center,
- Scaled,
- Unchecked
-};
-
-class Compositor final : public Core::Object {
- C_OBJECT(Compositor)
-public:
- static Compositor& the();
-
- void compose();
- void invalidate_window();
- void invalidate_screen();
- void invalidate_screen(const Gfx::IntRect&);
-
- bool set_resolution(int desired_width, int desired_height);
-
- bool set_background_color(const String& background_color);
-
- bool set_wallpaper_mode(const String& mode);
-
- bool set_wallpaper(const String& path, Function<void(bool)>&& callback);
- String wallpaper_path() const { return m_wallpaper_path; }
-
- void invalidate_cursor(bool = false);
- Gfx::IntRect current_cursor_rect() const;
-
- void increment_display_link_count(Badge<ClientConnection>);
- void decrement_display_link_count(Badge<ClientConnection>);
-
- void invalidate_occlusions() { m_occlusions_dirty = true; }
-
- void did_construct_window_manager(Badge<WindowManager>);
-
-private:
- Compositor();
- void init_bitmaps();
- void flip_buffers();
- void flush(const Gfx::IntRect&);
- void draw_menubar();
- void run_animations(Gfx::DisjointRectSet&);
- void notify_display_links();
- void start_compose_async_timer();
- void recompute_occlusions();
- bool any_opaque_window_above_this_one_contains_rect(const Window&, const Gfx::IntRect&);
- void change_cursor(const Cursor*);
- void draw_cursor(const Gfx::IntRect&);
- void restore_cursor_back();
- bool draw_geometry_label(Gfx::IntRect&);
-
- RefPtr<Core::Timer> m_compose_timer;
- RefPtr<Core::Timer> m_immediate_compose_timer;
- bool m_flash_flush { false };
- bool m_buffers_are_flipped { false };
- bool m_screen_can_set_buffer { false };
- bool m_occlusions_dirty { true };
- bool m_invalidated_any { true };
- bool m_invalidated_window { false };
- bool m_invalidated_cursor { false };
-
- RefPtr<Gfx::Bitmap> m_front_bitmap;
- RefPtr<Gfx::Bitmap> m_back_bitmap;
- RefPtr<Gfx::Bitmap> m_temp_bitmap;
- OwnPtr<Gfx::Painter> m_back_painter;
- OwnPtr<Gfx::Painter> m_front_painter;
- OwnPtr<Gfx::Painter> m_temp_painter;
-
- Gfx::DisjointRectSet m_dirty_screen_rects;
- Gfx::DisjointRectSet m_opaque_wallpaper_rects;
-
- RefPtr<Gfx::Bitmap> m_cursor_back_bitmap;
- OwnPtr<Gfx::Painter> m_cursor_back_painter;
- Gfx::IntRect m_last_cursor_rect;
- Gfx::IntRect m_last_dnd_rect;
- Gfx::IntRect m_last_geometry_label_damage_rect;
-
- String m_wallpaper_path { "" };
- WallpaperMode m_wallpaper_mode { WallpaperMode::Unchecked };
- RefPtr<Gfx::Bitmap> m_wallpaper;
-
- const Cursor* m_current_cursor { nullptr };
- unsigned m_current_cursor_frame { 0 };
- RefPtr<Core::Timer> m_cursor_timer;
-
- RefPtr<Core::Timer> m_display_link_notify_timer;
- size_t m_display_link_count { 0 };
-
- Optional<Gfx::Color> m_custom_background_color;
-};
-
-}
diff --git a/Services/WindowServer/Cursor.cpp b/Services/WindowServer/Cursor.cpp
deleted file mode 100644
index db18aa4281..0000000000
--- a/Services/WindowServer/Cursor.cpp
+++ /dev/null
@@ -1,185 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <AK/LexicalPath.h>
-#include <WindowServer/Cursor.h>
-#include <WindowServer/WindowManager.h>
-
-namespace WindowServer {
-
-CursorParams CursorParams::parse_from_file_name(const StringView& cursor_path, const Gfx::IntPoint& default_hotspot)
-{
- LexicalPath path(cursor_path);
- if (!path.is_valid()) {
- dbgln("Cannot parse invalid cursor path, use default cursor params");
- return { default_hotspot };
- }
- auto file_title = path.title();
- auto last_dot_in_title = StringView(file_title).find_last_of('.');
- if (!last_dot_in_title.has_value() || last_dot_in_title.value() == 0) {
- // No encoded params in filename. Not an error, we'll just use defaults
- return { default_hotspot };
- }
- auto params_str = file_title.substring_view(last_dot_in_title.value() + 1);
-
- CursorParams params(default_hotspot);
- for (size_t i = 0; i + 1 < params_str.length();) {
- auto property = params_str[i++];
-
- auto value = [&]() -> Optional<size_t> {
- size_t k = i;
- while (k < params_str.length()) {
- auto ch = params_str[k];
- if (ch < '0' || ch > '9')
- break;
- k++;
- }
- if (k == i)
- return {};
- auto parsed_number = params_str.substring_view(i, k - i).to_uint();
- if (!parsed_number.has_value())
- return {};
- i = k;
- return parsed_number.value();
- }();
- if (!value.has_value()) {
- dbg() << "Failed to parse value for property '" << property << "' from parsed cursor path: " << cursor_path;
- return { default_hotspot };
- }
- switch (property) {
- case 'x':
- params.m_hotspot.set_x(value.value());
- params.m_have_hotspot = true;
- break;
- case 'y':
- params.m_hotspot.set_y(value.value());
- params.m_have_hotspot = true;
- break;
- case 'f':
- if (value.value() > 1)
- params.m_frames = value.value();
- break;
- case 't':
- if (value.value() >= 100 && value.value() <= 1000)
- params.m_frame_ms = value.value();
- else
- dbgln("Cursor frame rate outside of valid range (100-1000ms)");
- break;
- default:
- dbg() << "Ignore unknown property '" << property << "' with value " << value.value() << " parsed from cursor path: " << cursor_path;
- return { default_hotspot };
- }
- }
- return params;
-}
-
-CursorParams CursorParams::constrained(const Gfx::Bitmap& bitmap) const
-{
- CursorParams params(*this);
- auto rect = bitmap.rect();
- if (params.m_frames > 1) {
- if (rect.width() % params.m_frames == 0) {
- rect.set_width(rect.width() / (int)params.m_frames);
- } else {
- dbg() << "Cannot divide cursor dimensions " << rect << " into " << params.m_frames << " frames";
- params.m_frames = 1;
- }
- }
- if (params.m_have_hotspot)
- params.m_hotspot = params.m_hotspot.constrained(rect);
- else
- params.m_hotspot = rect.center();
- return params;
-}
-
-Cursor::Cursor(NonnullRefPtr<Gfx::Bitmap>&& bitmap, const CursorParams& cursor_params)
- : m_bitmap(move(bitmap))
- , m_params(cursor_params.constrained(*m_bitmap))
- , m_rect(m_bitmap->rect())
-{
- if (m_params.frames() > 1) {
- ASSERT(m_rect.width() % m_params.frames() == 0);
- m_rect.set_width(m_rect.width() / m_params.frames());
- }
-}
-
-Cursor::~Cursor()
-{
-}
-
-NonnullRefPtr<Cursor> Cursor::create(NonnullRefPtr<Gfx::Bitmap>&& bitmap)
-{
- auto hotspot = bitmap->rect().center();
- return adopt(*new Cursor(move(bitmap), CursorParams(hotspot)));
-}
-
-NonnullRefPtr<Cursor> Cursor::create(NonnullRefPtr<Gfx::Bitmap>&& bitmap, const StringView& filename)
-{
- auto default_hotspot = bitmap->rect().center();
- return adopt(*new Cursor(move(bitmap), CursorParams::parse_from_file_name(filename, default_hotspot)));
-}
-
-RefPtr<Cursor> Cursor::create(Gfx::StandardCursor standard_cursor)
-{
- switch (standard_cursor) {
- case Gfx::StandardCursor::None:
- return nullptr;
- case Gfx::StandardCursor::Hidden:
- return WindowManager::the().hidden_cursor();
- case Gfx::StandardCursor::Arrow:
- return WindowManager::the().arrow_cursor();
- case Gfx::StandardCursor::Crosshair:
- return WindowManager::the().crosshair_cursor();
- case Gfx::StandardCursor::IBeam:
- return WindowManager::the().i_beam_cursor();
- case Gfx::StandardCursor::ResizeHorizontal:
- return WindowManager::the().resize_horizontally_cursor();
- case Gfx::StandardCursor::ResizeVertical:
- return WindowManager::the().resize_vertically_cursor();
- case Gfx::StandardCursor::ResizeDiagonalTLBR:
- return WindowManager::the().resize_diagonally_tlbr_cursor();
- case Gfx::StandardCursor::ResizeDiagonalBLTR:
- return WindowManager::the().resize_diagonally_bltr_cursor();
- case Gfx::StandardCursor::ResizeColumn:
- return WindowManager::the().resize_column_cursor();
- case Gfx::StandardCursor::ResizeRow:
- return WindowManager::the().resize_row_cursor();
- case Gfx::StandardCursor::Hand:
- return WindowManager::the().hand_cursor();
- case Gfx::StandardCursor::Help:
- return WindowManager::the().help_cursor();
- case Gfx::StandardCursor::Drag:
- return WindowManager::the().drag_cursor();
- case Gfx::StandardCursor::Move:
- return WindowManager::the().move_cursor();
- case Gfx::StandardCursor::Wait:
- return WindowManager::the().wait_cursor();
- default:
- ASSERT_NOT_REACHED();
- }
-}
-
-}
diff --git a/Services/WindowServer/Cursor.h b/Services/WindowServer/Cursor.h
deleted file mode 100644
index efe014a735..0000000000
--- a/Services/WindowServer/Cursor.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <LibGfx/Bitmap.h>
-#include <LibGfx/StandardCursor.h>
-
-namespace WindowServer {
-
-class CursorParams {
-public:
- static CursorParams parse_from_file_name(const StringView&, const Gfx::IntPoint&);
- CursorParams(const Gfx::IntPoint& hotspot)
- : m_hotspot(hotspot)
- {
- }
- CursorParams constrained(const Gfx::Bitmap&) const;
-
- const Gfx::IntPoint& hotspot() const { return m_hotspot; }
- unsigned frames() const { return m_frames; }
- unsigned frame_ms() const { return m_frame_ms; }
-
-private:
- CursorParams() = default;
- Gfx::IntPoint m_hotspot;
- unsigned m_frames { 1 };
- unsigned m_frame_ms { 0 };
- bool m_have_hotspot { false };
-};
-
-class Cursor : public RefCounted<Cursor> {
-public:
- static NonnullRefPtr<Cursor> create(NonnullRefPtr<Gfx::Bitmap>&&, const StringView&);
- static NonnullRefPtr<Cursor> create(NonnullRefPtr<Gfx::Bitmap>&&);
- static RefPtr<Cursor> create(Gfx::StandardCursor);
- ~Cursor();
-
- const CursorParams& params() const { return m_params; }
- const Gfx::Bitmap& bitmap() const { return *m_bitmap; }
-
- Gfx::IntRect source_rect(unsigned frame) const
- {
- return m_rect.translated(frame * m_rect.width(), 0);
- }
-
- Gfx::IntRect rect() const { return m_rect; }
- Gfx::IntSize size() const { return m_rect.size(); }
-
-private:
- Cursor(NonnullRefPtr<Gfx::Bitmap>&&, const CursorParams&);
-
- RefPtr<Gfx::Bitmap> m_bitmap;
- CursorParams m_params;
- Gfx::IntRect m_rect;
-};
-
-}
diff --git a/Services/WindowServer/Event.h b/Services/WindowServer/Event.h
deleted file mode 100644
index e82df2d4c9..0000000000
--- a/Services/WindowServer/Event.h
+++ /dev/null
@@ -1,166 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/String.h>
-#include <Kernel/API/KeyCode.h>
-#include <LibCore/Event.h>
-#include <LibCore/MimeData.h>
-#include <LibGfx/Rect.h>
-#include <WindowServer/Cursor.h>
-#include <WindowServer/WindowType.h>
-
-namespace WindowServer {
-
-class Event : public Core::Event {
-public:
- enum Type {
- Invalid = 3000,
- MouseMove,
- MouseDown,
- MouseDoubleClick,
- MouseUp,
- MouseWheel,
- WindowEntered,
- WindowLeft,
- KeyDown,
- KeyUp,
- WindowActivated,
- WindowDeactivated,
- WindowInputEntered,
- WindowInputLeft,
- WindowCloseRequest,
- WindowResized,
- };
-
- Event() { }
- explicit Event(Type type)
- : Core::Event(type)
- {
- }
- virtual ~Event() { }
-
- bool is_mouse_event() const { return type() == MouseMove || type() == MouseDown || type() == MouseDoubleClick || type() == MouseUp || type() == MouseWheel; }
- bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
-};
-
-enum class MouseButton : u8 {
- None = 0,
- Left = 1,
- Right = 2,
- Middle = 4,
- Back = 8,
- Forward = 16,
-};
-
-class KeyEvent final : public Event {
-public:
- KeyEvent(Type type, int key, u32 code_point, u8 modifiers, u32 scancode)
- : Event(type)
- , m_key(key)
- , m_code_point(code_point)
- , m_modifiers(modifiers)
- , m_scancode(scancode)
- {
- }
-
- int key() const { return m_key; }
- bool ctrl() const { return m_modifiers & Mod_Ctrl; }
- bool alt() const { return m_modifiers & Mod_Alt; }
- bool shift() const { return m_modifiers & Mod_Shift; }
- bool logo() const { return m_modifiers & Mod_Logo; }
- u8 modifiers() const { return m_modifiers; }
- u32 code_point() const { return m_code_point; }
- u32 scancode() const { return m_scancode; }
-
-private:
- friend class EventLoop;
- friend class Screen;
- int m_key { 0 };
- u32 m_code_point { 0 };
- u8 m_modifiers { 0 };
- u32 m_scancode { 0 };
-};
-
-class MouseEvent final : public Event {
-public:
- MouseEvent(Type type, const Gfx::IntPoint& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta = 0)
- : Event(type)
- , m_position(position)
- , m_buttons(buttons)
- , m_button(button)
- , m_modifiers(modifiers)
- , m_wheel_delta(wheel_delta)
- {
- }
-
- const Gfx::IntPoint& position() const { return m_position; }
- int x() const { return m_position.x(); }
- int y() const { return m_position.y(); }
- MouseButton button() const { return m_button; }
- unsigned buttons() const { return m_buttons; }
- unsigned modifiers() const { return m_modifiers; }
- int wheel_delta() const { return m_wheel_delta; }
- bool is_drag() const { return m_drag; }
-
- Vector<String> mime_types() const
- {
- if (!m_mime_data)
- return {};
- return m_mime_data->formats();
- }
-
- void set_drag(bool b) { m_drag = b; }
- void set_mime_data(const Core::MimeData& mime_data) { m_mime_data = mime_data; }
-
- MouseEvent translated(const Gfx::IntPoint& delta) const { return MouseEvent((Type)type(), m_position.translated(delta), m_buttons, m_button, m_modifiers, m_wheel_delta); }
-
-private:
- Gfx::IntPoint m_position;
- unsigned m_buttons { 0 };
- MouseButton m_button { MouseButton::None };
- unsigned m_modifiers { 0 };
- int m_wheel_delta { 0 };
- bool m_drag { false };
- RefPtr<const Core::MimeData> m_mime_data;
-};
-
-class ResizeEvent final : public Event {
-public:
- ResizeEvent(const Gfx::IntRect& rect)
- : Event(Event::WindowResized)
- , m_rect(rect)
- {
- }
-
- const Gfx::IntRect& rect() const { return m_rect; }
-
-private:
- Gfx::IntRect m_rect;
-};
-
-}
diff --git a/Services/WindowServer/EventLoop.cpp b/Services/WindowServer/EventLoop.cpp
deleted file mode 100644
index 21dae58823..0000000000
--- a/Services/WindowServer/EventLoop.cpp
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <Kernel/API/MousePacket.h>
-#include <LibCore/LocalSocket.h>
-#include <LibCore/Object.h>
-#include <WindowServer/ClientConnection.h>
-#include <WindowServer/Cursor.h>
-#include <WindowServer/Event.h>
-#include <WindowServer/EventLoop.h>
-#include <WindowServer/Screen.h>
-#include <WindowServer/WindowManager.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <sys/select.h>
-#include <sys/socket.h>
-#include <sys/time.h>
-#include <time.h>
-#include <unistd.h>
-
-//#define WSMESSAGELOOP_DEBUG
-
-namespace WindowServer {
-
-EventLoop::EventLoop()
- : m_server(Core::LocalServer::construct())
-{
- m_keyboard_fd = open("/dev/keyboard", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
- m_mouse_fd = open("/dev/mouse", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
-
- bool ok = m_server->take_over_from_system_server();
- ASSERT(ok);
-
- m_server->on_ready_to_accept = [this] {
- auto client_socket = m_server->accept();
- if (!client_socket) {
- dbgln("WindowServer: accept failed.");
- return;
- }
- static int s_next_client_id = 0;
- int client_id = ++s_next_client_id;
- IPC::new_client_connection<ClientConnection>(client_socket.release_nonnull(), client_id);
- };
-
- ASSERT(m_keyboard_fd >= 0);
- ASSERT(m_mouse_fd >= 0);
-
- m_keyboard_notifier = Core::Notifier::construct(m_keyboard_fd, Core::Notifier::Read);
- m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); };
-
- m_mouse_notifier = Core::Notifier::construct(m_mouse_fd, Core::Notifier::Read);
- m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); };
-}
-
-EventLoop::~EventLoop()
-{
-}
-
-void EventLoop::drain_mouse()
-{
- auto& screen = Screen::the();
- MousePacket state;
- state.buttons = screen.mouse_button_state();
- unsigned buttons = state.buttons;
- MousePacket packets[32];
-
- ssize_t nread = read(m_mouse_fd, &packets, sizeof(packets));
- if (nread < 0) {
- perror("EventLoop::drain_mouse read");
- return;
- }
- size_t npackets = nread / sizeof(MousePacket);
- if (!npackets)
- return;
- for (size_t i = 0; i < npackets; ++i) {
- auto& packet = packets[i];
-#ifdef WSMESSAGELOOP_DEBUG
- dbgln("EventLoop: Mouse X {}, Y {}, Z {}, relative={}", packet.x, packet.y, packet.z, packet.is_relative);
-#endif
- buttons = packet.buttons;
-
- state.is_relative = packet.is_relative;
- if (packet.is_relative) {
- state.x += packet.x;
- state.y -= packet.y;
- state.z += packet.z;
- } else {
- state.x = packet.x;
- state.y = packet.y;
- state.z += packet.z;
- }
-
- if (buttons != state.buttons) {
- state.buttons = buttons;
-#ifdef WSMESSAGELOOP_DEBUG
- dbgln("EventLoop: Mouse Button Event");
-#endif
- screen.on_receive_mouse_data(state);
- if (state.is_relative) {
- state.x = 0;
- state.y = 0;
- state.z = 0;
- }
- }
- }
- if (state.is_relative && (state.x || state.y || state.z))
- screen.on_receive_mouse_data(state);
- if (!state.is_relative)
- screen.on_receive_mouse_data(state);
-}
-
-void EventLoop::drain_keyboard()
-{
- auto& screen = Screen::the();
- for (;;) {
- ::KeyEvent event;
- ssize_t nread = read(m_keyboard_fd, (u8*)&event, sizeof(::KeyEvent));
- if (nread == 0)
- break;
- ASSERT(nread == sizeof(::KeyEvent));
- screen.on_receive_keyboard_data(event);
- }
-}
-
-}
diff --git a/Services/WindowServer/EventLoop.h b/Services/WindowServer/EventLoop.h
deleted file mode 100644
index b476b5e639..0000000000
--- a/Services/WindowServer/EventLoop.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/ByteBuffer.h>
-#include <LibCore/EventLoop.h>
-#include <LibCore/LocalServer.h>
-#include <LibCore/Notifier.h>
-
-namespace WindowServer {
-
-class ClientConnection;
-
-class EventLoop {
-public:
- EventLoop();
- virtual ~EventLoop();
-
- int exec() { return m_event_loop.exec(); }
-
-private:
- void drain_mouse();
- void drain_keyboard();
-
- Core::EventLoop m_event_loop;
- int m_keyboard_fd { -1 };
- RefPtr<Core::Notifier> m_keyboard_notifier;
- int m_mouse_fd { -1 };
- RefPtr<Core::Notifier> m_mouse_notifier;
- RefPtr<Core::LocalServer> m_server;
-};
-
-}
diff --git a/Services/WindowServer/Menu.cpp b/Services/WindowServer/Menu.cpp
deleted file mode 100644
index 4d204406c2..0000000000
--- a/Services/WindowServer/Menu.cpp
+++ /dev/null
@@ -1,568 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "Menu.h"
-#include "Event.h"
-#include "MenuItem.h"
-#include "MenuManager.h"
-#include "Screen.h"
-#include "Window.h"
-#include "WindowManager.h"
-#include <LibGfx/Bitmap.h>
-#include <LibGfx/CharacterBitmap.h>
-#include <LibGfx/Font.h>
-#include <LibGfx/Painter.h>
-#include <LibGfx/StylePainter.h>
-#include <LibGfx/Triangle.h>
-#include <WindowServer/ClientConnection.h>
-#include <WindowServer/WindowClientEndpoint.h>
-
-namespace WindowServer {
-
-Menu::Menu(ClientConnection* client, int menu_id, const String& name)
- : Core::Object(client)
- , m_client(client)
- , m_menu_id(menu_id)
- , m_name(move(name))
-{
-}
-
-Menu::~Menu()
-{
-}
-
-void Menu::set_title_font(const Gfx::Font& font)
-{
- m_title_font = &font;
-}
-
-const Gfx::Font& Menu::title_font() const
-{
- return *m_title_font;
-}
-
-const Gfx::Font& Menu::font() const
-{
- return Gfx::FontDatabase::default_font();
-}
-
-static const char* s_checked_bitmap_data = {
- " "
- " # "
- " ## "
- " ### "
- " ## ### "
- " ##### "
- " ### "
- " # "
- " "
-};
-
-static const char* s_submenu_arrow_bitmap_data = {
- " "
- " # "
- " ## "
- " ### "
- " #### "
- " ### "
- " ## "
- " # "
- " "
-};
-
-static Gfx::CharacterBitmap* s_checked_bitmap;
-static const int s_checked_bitmap_width = 9;
-static const int s_checked_bitmap_height = 9;
-static const int s_submenu_arrow_bitmap_width = 9;
-static const int s_submenu_arrow_bitmap_height = 9;
-static const int s_item_icon_width = 16;
-static const int s_stripe_width = 23;
-
-int Menu::content_width() const
-{
- int widest_text = 0;
- int widest_shortcut = 0;
- for (auto& item : m_items) {
- if (item.type() != MenuItem::Text)
- continue;
- auto& use_font = item.is_default() ? Gfx::FontDatabase::default_bold_font() : font();
- int text_width = use_font.width(item.text());
- if (!item.shortcut_text().is_empty()) {
- int shortcut_width = use_font.width(item.shortcut_text());
- widest_shortcut = max(shortcut_width, widest_shortcut);
- }
- widest_text = max(widest_text, text_width);
- }
-
- int widest_item = widest_text + s_stripe_width;
- if (widest_shortcut)
- widest_item += padding_between_text_and_shortcut() + widest_shortcut;
-
- return max(widest_item, rect_in_menubar().width()) + horizontal_padding() + frame_thickness() * 2;
-}
-
-void Menu::redraw()
-{
- if (!menu_window())
- return;
- draw();
- menu_window()->invalidate();
-}
-
-Window& Menu::ensure_menu_window()
-{
- if (m_menu_window)
- return *m_menu_window;
-
- int width = this->content_width();
-
- Gfx::IntPoint next_item_location(frame_thickness(), frame_thickness());
- for (auto& item : m_items) {
- int height = 0;
- if (item.type() == MenuItem::Text)
- height = item_height();
- else if (item.type() == MenuItem::Separator)
- height = 8;
- item.set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
- next_item_location.move_by(0, height);
- }
-
- int window_height_available = Screen::the().height() - MenuManager::the().menubar_rect().height() - frame_thickness() * 2;
- int max_window_height = (window_height_available / item_height()) * item_height() + frame_thickness() * 2;
- int content_height = m_items.is_empty() ? 0 : (m_items.last().rect().bottom() + 1) + frame_thickness();
- int window_height = min(max_window_height, content_height);
- if (window_height < content_height) {
- m_scrollable = true;
- m_max_scroll_offset = item_count() - window_height / item_height() + 2;
- }
-
- auto window = Window::construct(*this, WindowType::Menu);
- window->set_rect(0, 0, width, window_height);
- m_menu_window = move(window);
- draw();
-
- return *m_menu_window;
-}
-
-int Menu::visible_item_count() const
-{
- if (!is_scrollable())
- return m_items.size();
- ASSERT(m_menu_window);
- // Make space for up/down arrow indicators
- return m_menu_window->height() / item_height() - 2;
-}
-
-void Menu::draw()
-{
- auto palette = WindowManager::the().palette();
- m_theme_index_at_last_paint = MenuManager::the().theme_index();
-
- ASSERT(menu_window());
- ASSERT(menu_window()->backing_store());
- Gfx::Painter painter(*menu_window()->backing_store());
-
- Gfx::IntRect rect { {}, menu_window()->size() };
- Gfx::StylePainter::paint_window_frame(painter, rect, palette);
- painter.fill_rect(rect.shrunken(6, 6), palette.menu_base());
- int width = this->content_width();
-
- if (!s_checked_bitmap)
- s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
-
- bool has_checkable_items = false;
- bool has_items_with_icon = false;
- for (auto& item : m_items) {
- has_checkable_items = has_checkable_items | item.is_checkable();
- has_items_with_icon = has_items_with_icon | !!item.icon();
- }
-
- Gfx::IntRect stripe_rect { frame_thickness(), frame_thickness(), s_stripe_width, menu_window()->height() - frame_thickness() * 2 };
- painter.fill_rect(stripe_rect, palette.menu_stripe());
- painter.draw_line(stripe_rect.top_right(), stripe_rect.bottom_right(), palette.menu_stripe().darkened());
-
- int visible_item_count = this->visible_item_count();
-
- if (is_scrollable()) {
- bool can_go_up = m_scroll_offset > 0;
- bool can_go_down = m_scroll_offset < m_max_scroll_offset;
- Gfx::IntRect up_indicator_rect { frame_thickness(), frame_thickness(), content_width(), item_height() };
- painter.draw_text(up_indicator_rect, "\xE2\xAC\x86", Gfx::TextAlignment::Center, can_go_up ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
- Gfx::IntRect down_indicator_rect { frame_thickness(), menu_window()->height() - item_height() - frame_thickness(), content_width(), item_height() };
- painter.draw_text(down_indicator_rect, "\xE2\xAC\x87", Gfx::TextAlignment::Center, can_go_down ? palette.menu_base_text() : palette.color(ColorRole::DisabledText));
- }
-
- for (int i = 0; i < visible_item_count; ++i) {
- auto& item = m_items.at(m_scroll_offset + i);
- if (item.type() == MenuItem::Text) {
- Color text_color = palette.menu_base_text();
- if (&item == hovered_item() && item.is_enabled()) {
- painter.fill_rect(item.rect(), palette.menu_selection());
- painter.draw_rect(item.rect(), palette.menu_selection().darkened());
- text_color = palette.menu_selection_text();
- } else if (!item.is_enabled()) {
- text_color = Color::MidGray;
- }
- Gfx::IntRect text_rect = item.rect().translated(stripe_rect.width() + 6, 0);
- if (item.is_checkable()) {
- if (item.is_exclusive()) {
- Gfx::IntRect radio_rect { item.rect().x() + 5, 0, 12, 12 };
- radio_rect.center_vertically_within(text_rect);
- Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, item.is_checked(), false);
- } else {
- Gfx::IntRect checkmark_rect { item.rect().x() + 7, 0, s_checked_bitmap_width, s_checked_bitmap_height };
- checkmark_rect.center_vertically_within(text_rect);
- Gfx::IntRect checkbox_rect = checkmark_rect.inflated(4, 4);
- painter.fill_rect(checkbox_rect, palette.base());
- Gfx::StylePainter::paint_frame(painter, checkbox_rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
- if (item.is_checked()) {
- painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, palette.button_text());
- }
- }
- } else if (item.icon()) {
- Gfx::IntRect icon_rect { item.rect().x() + 3, 0, s_item_icon_width, s_item_icon_width };
- icon_rect.center_vertically_within(text_rect);
-
- if (&item == hovered_item() && item.is_enabled()) {
- auto shadow_color = palette.menu_selection().darkened(0.7f);
- painter.blit_filtered(icon_rect.location().translated(1, 1), *item.icon(), item.icon()->rect(), [&shadow_color](auto) {
- return shadow_color;
- });
- icon_rect.move_by(-1, -1);
- }
- if (item.is_enabled())
- painter.blit(icon_rect.location(), *item.icon(), item.icon()->rect());
- else
- painter.blit_disabled(icon_rect.location(), *item.icon(), item.icon()->rect(), palette);
- }
- auto& previous_font = painter.font();
- if (item.is_default())
- painter.set_font(Gfx::FontDatabase::default_bold_font());
- painter.draw_text(text_rect, item.text(), Gfx::TextAlignment::CenterLeft, text_color);
- if (!item.shortcut_text().is_empty()) {
- painter.draw_text(item.rect().translated(-right_padding(), 0), item.shortcut_text(), Gfx::TextAlignment::CenterRight, text_color);
- }
- painter.set_font(previous_font);
- if (item.is_submenu()) {
- static auto& submenu_arrow_bitmap = Gfx::CharacterBitmap::create_from_ascii(s_submenu_arrow_bitmap_data, s_submenu_arrow_bitmap_width, s_submenu_arrow_bitmap_height).leak_ref();
- Gfx::IntRect submenu_arrow_rect {
- item.rect().right() - s_submenu_arrow_bitmap_width - 2,
- 0,
- s_submenu_arrow_bitmap_width,
- s_submenu_arrow_bitmap_height
- };
- submenu_arrow_rect.center_vertically_within(item.rect());
- painter.draw_bitmap(submenu_arrow_rect.location(), submenu_arrow_bitmap, text_color);
- }
- } else if (item.type() == MenuItem::Separator) {
- Gfx::IntPoint p1(item.rect().translated(stripe_rect.width() + 4, 0).x(), item.rect().center().y() - 1);
- Gfx::IntPoint p2(width - 7, item.rect().center().y() - 1);
- painter.draw_line(p1, p2, palette.threed_shadow1());
- painter.draw_line(p1.translated(0, 1), p2.translated(0, 1), palette.threed_highlight());
- }
- }
-}
-
-MenuItem* Menu::hovered_item() const
-{
- if (m_hovered_item_index == -1)
- return nullptr;
- return const_cast<MenuItem*>(&item(m_hovered_item_index));
-}
-
-void Menu::update_for_new_hovered_item(bool make_input)
-{
- if (hovered_item() && hovered_item()->is_submenu()) {
- ASSERT(menu_window());
- MenuManager::the().close_everyone_not_in_lineage(*hovered_item()->submenu());
- hovered_item()->submenu()->do_popup(hovered_item()->rect().top_right().translated(menu_window()->rect().location()), make_input);
- } else {
- MenuManager::the().close_everyone_not_in_lineage(*this);
- ensure_menu_window().set_visible(true);
- }
- redraw();
-}
-
-void Menu::open_hovered_item()
-{
- ASSERT(menu_window());
- ASSERT(menu_window()->is_visible());
- if (!hovered_item())
- return;
- if (hovered_item()->is_enabled())
- did_activate(*hovered_item());
- clear_hovered_item();
-}
-
-void Menu::descend_into_submenu_at_hovered_item()
-{
- ASSERT(hovered_item());
- auto submenu = hovered_item()->submenu();
- ASSERT(submenu);
- MenuManager::the().open_menu(*submenu, false);
- submenu->set_hovered_item(0);
- ASSERT(submenu->hovered_item()->type() != MenuItem::Separator);
-}
-
-void Menu::handle_mouse_move_event(const MouseEvent& mouse_event)
-{
- ASSERT(menu_window());
- MenuManager::the().set_current_menu(this);
- if (hovered_item() && hovered_item()->is_submenu()) {
-
- auto item = *hovered_item();
- auto submenu_top_left = item.rect().location() + Gfx::IntPoint { item.rect().width(), 0 };
- auto submenu_bottom_left = submenu_top_left + Gfx::IntPoint { 0, item.submenu()->menu_window()->height() };
-
- auto safe_hover_triangle = Gfx::Triangle { m_last_position_in_hover, submenu_top_left, submenu_bottom_left };
- m_last_position_in_hover = mouse_event.position();
-
- // Don't update the hovered item if mouse is moving towards a submenu
- if (safe_hover_triangle.contains(mouse_event.position()))
- return;
- }
-
- int index = item_index_at(mouse_event.position());
- if (m_hovered_item_index == index)
- return;
- m_hovered_item_index = index;
-
- update_for_new_hovered_item();
- return;
-}
-
-void Menu::event(Core::Event& event)
-{
- if (event.type() == Event::MouseMove) {
- handle_mouse_move_event(static_cast<const MouseEvent&>(event));
- return;
- }
-
- if (event.type() == Event::MouseUp) {
- open_hovered_item();
- return;
- }
-
- if (event.type() == Event::MouseWheel && is_scrollable()) {
- ASSERT(menu_window());
- auto& mouse_event = static_cast<const MouseEvent&>(event);
- m_scroll_offset += mouse_event.wheel_delta();
- m_scroll_offset = clamp(m_scroll_offset, 0, m_max_scroll_offset);
-
- int index = item_index_at(mouse_event.position());
- if (m_hovered_item_index == index)
- return;
-
- m_hovered_item_index = index;
- update_for_new_hovered_item();
- return;
- }
-
- if (event.type() == Event::KeyDown) {
- auto key = static_cast<KeyEvent&>(event).key();
-
- if (!(key == Key_Up || key == Key_Down || key == Key_Left || key == Key_Right || key == Key_Return))
- return;
-
- ASSERT(menu_window());
- ASSERT(menu_window()->is_visible());
-
- // Default to the first item on key press if one has not been selected yet
- if (!hovered_item()) {
- m_hovered_item_index = 0;
- update_for_new_hovered_item(key == Key_Right);
- return;
- }
-
- if (key == Key_Up) {
- ASSERT(m_items.at(0).type() != MenuItem::Separator);
-
- if (is_scrollable() && m_hovered_item_index == 0)
- return;
-
- auto original_index = m_hovered_item_index;
- do {
- if (m_hovered_item_index == 0)
- m_hovered_item_index = m_items.size() - 1;
- else
- --m_hovered_item_index;
- if (m_hovered_item_index == original_index)
- return;
- } while (hovered_item()->type() == MenuItem::Separator || !hovered_item()->is_enabled());
-
- ASSERT(m_hovered_item_index >= 0 && m_hovered_item_index <= static_cast<int>(m_items.size()) - 1);
-
- if (is_scrollable() && m_hovered_item_index < m_scroll_offset)
- --m_scroll_offset;
-
- update_for_new_hovered_item();
- return;
- }
-
- if (key == Key_Down) {
- ASSERT(m_items.at(0).type() != MenuItem::Separator);
-
- if (is_scrollable() && m_hovered_item_index == static_cast<int>(m_items.size()) - 1)
- return;
-
- auto original_index = m_hovered_item_index;
- do {
- if (m_hovered_item_index == static_cast<int>(m_items.size()) - 1)
- m_hovered_item_index = 0;
- else
- ++m_hovered_item_index;
- if (m_hovered_item_index == original_index)
- return;
- } while (hovered_item()->type() == MenuItem::Separator || !hovered_item()->is_enabled());
-
- ASSERT(m_hovered_item_index >= 0 && m_hovered_item_index <= static_cast<int>(m_items.size()) - 1);
-
- if (is_scrollable() && m_hovered_item_index >= (m_scroll_offset + visible_item_count()))
- ++m_scroll_offset;
-
- update_for_new_hovered_item();
- return;
- }
- }
- Core::Object::event(event);
-}
-
-void Menu::clear_hovered_item()
-{
- if (!hovered_item())
- return;
- m_hovered_item_index = -1;
- redraw();
-}
-
-void Menu::did_activate(MenuItem& item)
-{
- if (item.type() == MenuItem::Type::Separator)
- return;
-
- if (on_item_activation)
- on_item_activation(item);
-
- MenuManager::the().close_bar();
-
- if (m_client)
- m_client->post_message(Messages::WindowClient::MenuItemActivated(m_menu_id, item.identifier()));
-}
-
-bool Menu::activate_default()
-{
- for (auto& item : m_items) {
- if (item.type() == MenuItem::Type::Separator)
- continue;
- if (item.is_enabled() && item.is_default()) {
- did_activate(item);
- return true;
- }
- }
- return false;
-}
-
-MenuItem* Menu::item_with_identifier(unsigned identifier)
-{
- for (auto& item : m_items) {
- if (item.identifier() == identifier)
- return &item;
- }
- return nullptr;
-}
-
-int Menu::item_index_at(const Gfx::IntPoint& position)
-{
- int i = 0;
- for (auto& item : m_items) {
- if (item.rect().contains(position))
- return i;
- ++i;
- }
- return -1;
-}
-
-void Menu::close()
-{
- MenuManager::the().close_menu_and_descendants(*this);
-}
-
-void Menu::redraw_if_theme_changed()
-{
- if (m_theme_index_at_last_paint != MenuManager::the().theme_index())
- redraw();
-}
-
-void Menu::popup(const Gfx::IntPoint& position)
-{
- do_popup(position, true);
-}
-
-void Menu::do_popup(const Gfx::IntPoint& position, bool make_input)
-{
- if (is_empty()) {
- dbgln("Menu: Empty menu popup");
- return;
- }
-
- auto& window = ensure_menu_window();
- redraw_if_theme_changed();
-
- const int margin = 30;
- Gfx::IntPoint adjusted_pos = position;
-
- if (adjusted_pos.x() + window.width() >= Screen::the().width() - margin) {
- adjusted_pos = adjusted_pos.translated(-window.width(), 0);
- }
- if (adjusted_pos.y() + window.height() >= Screen::the().height() - margin) {
- adjusted_pos = adjusted_pos.translated(0, -window.height());
- }
-
- if (adjusted_pos.y() < MenuManager::the().menubar_rect().height())
- adjusted_pos.set_y(MenuManager::the().menubar_rect().height());
-
- window.move_to(adjusted_pos);
- window.set_visible(true);
- MenuManager::the().open_menu(*this, make_input);
- WindowManager::the().did_popup_a_menu({});
-}
-
-bool Menu::is_menu_ancestor_of(const Menu& other) const
-{
- for (auto& item : m_items) {
- if (!item.is_submenu())
- continue;
- auto& submenu = *item.submenu();
- if (&submenu == &other)
- return true;
- if (submenu.is_menu_ancestor_of(other))
- return true;
- }
- return false;
-}
-
-}
diff --git a/Services/WindowServer/Menu.h b/Services/WindowServer/Menu.h
deleted file mode 100644
index 20fdb201cc..0000000000
--- a/Services/WindowServer/Menu.h
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/NonnullOwnPtrVector.h>
-#include <AK/String.h>
-#include <AK/WeakPtr.h>
-#include <LibCore/Object.h>
-#include <LibGfx/Font.h>
-#include <LibGfx/FontDatabase.h>
-#include <LibGfx/Forward.h>
-#include <LibGfx/Rect.h>
-#include <WindowServer/Cursor.h>
-#include <WindowServer/MenuItem.h>
-#include <WindowServer/Window.h>
-
-namespace WindowServer {
-
-class ClientConnection;
-class MenuBar;
-class Event;
-
-class Menu final : public Core::Object {
- C_OBJECT(Menu)
-public:
- Menu(ClientConnection*, int menu_id, const String& name);
- virtual ~Menu() override;
-
- ClientConnection* client() { return m_client; }
- const ClientConnection* client() const { return m_client; }
- int menu_id() const { return m_menu_id; }
-
- MenuBar* menubar() { return m_menubar; }
- const MenuBar* menubar() const { return m_menubar; }
- void set_menubar(MenuBar* menubar) { m_menubar = menubar; }
-
- bool is_empty() const { return m_items.is_empty(); }
- int item_count() const { return m_items.size(); }
- const MenuItem& item(int index) const { return m_items.at(index); }
- MenuItem& item(int index) { return m_items.at(index); }
-
- void add_item(NonnullOwnPtr<MenuItem>&& item) { m_items.append(move(item)); }
-
- String name() const { return m_name; }
-
- template<typename Callback>
- void for_each_item(Callback callback) const
- {
- for (auto& item : m_items)
- callback(item);
- }
-
- Gfx::IntRect text_rect_in_menubar() const { return m_text_rect_in_menubar; }
- void set_text_rect_in_menubar(const Gfx::IntRect& rect) { m_text_rect_in_menubar = rect; }
-
- Gfx::IntRect rect_in_menubar() const { return m_rect_in_menubar; }
- void set_rect_in_menubar(const Gfx::IntRect& rect) { m_rect_in_menubar = rect; }
-
- Window* menu_window() { return m_menu_window.ptr(); }
- Window& ensure_menu_window();
-
- Window* window_menu_of() { return m_window_menu_of; }
- void set_window_menu_of(Window& window) { m_window_menu_of = window; }
- bool is_window_menu_open() { return m_is_window_menu_open; }
- void set_window_menu_open(bool is_open) { m_is_window_menu_open = is_open; }
-
- bool activate_default();
-
- int content_width() const;
-
- int item_height() const { return 20; }
- int frame_thickness() const { return 3; }
- int horizontal_padding() const { return left_padding() + right_padding(); }
- int left_padding() const { return 14; }
- int right_padding() const { return 14; }
-
- void draw();
- const Gfx::Font& font() const;
- const Gfx::Font& title_font() const;
- void set_title_font(const Gfx::Font& font);
-
- MenuItem* item_with_identifier(unsigned);
- void redraw();
-
- MenuItem* hovered_item() const;
-
- void set_hovered_item(int index)
- {
- m_hovered_item_index = index;
- update_for_new_hovered_item();
- }
-
- void clear_hovered_item();
-
- Function<void(MenuItem&)> on_item_activation;
-
- void close();
-
- void popup(const Gfx::IntPoint&);
- void do_popup(const Gfx::IntPoint&, bool);
-
- bool is_menu_ancestor_of(const Menu&) const;
-
- void redraw_if_theme_changed();
-
- bool is_scrollable() const { return m_scrollable; }
- int scroll_offset() const { return m_scroll_offset; }
-
- void descend_into_submenu_at_hovered_item();
- void open_hovered_item();
-
-private:
- virtual void event(Core::Event&) override;
-
- RefPtr<Gfx::Font> m_title_font { &Gfx::FontDatabase::default_font() };
-
- void handle_mouse_move_event(const MouseEvent&);
- int visible_item_count() const;
-
- int item_index_at(const Gfx::IntPoint&);
- int padding_between_text_and_shortcut() const { return 50; }
- void did_activate(MenuItem&);
- void update_for_new_hovered_item(bool make_input = false);
-
- ClientConnection* m_client { nullptr };
- int m_menu_id { 0 };
- String m_name;
- Gfx::IntRect m_rect_in_menubar;
- Gfx::IntRect m_text_rect_in_menubar;
- MenuBar* m_menubar { nullptr };
- NonnullOwnPtrVector<MenuItem> m_items;
- RefPtr<Window> m_menu_window;
-
- WeakPtr<Window> m_window_menu_of;
- bool m_is_window_menu_open = { false };
- Gfx::IntPoint m_last_position_in_hover;
- int m_theme_index_at_last_paint { -1 };
- int m_hovered_item_index { -1 };
-
- bool m_scrollable { false };
- int m_scroll_offset { 0 };
- int m_max_scroll_offset { 0 };
-};
-
-}
diff --git a/Services/WindowServer/MenuBar.cpp b/Services/WindowServer/MenuBar.cpp
deleted file mode 100644
index 30d739aaf7..0000000000
--- a/Services/WindowServer/MenuBar.cpp
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "MenuBar.h"
-#include "Menu.h"
-#include "MenuItem.h"
-#include <LibGfx/Bitmap.h>
-
-namespace WindowServer {
-
-MenuBar::MenuBar(ClientConnection& client, int menubar_id)
- : m_client(client)
- , m_menubar_id(menubar_id)
-{
-}
-
-MenuBar::~MenuBar()
-{
-}
-
-void MenuBar::add_menu(Menu& menu)
-{
- menu.set_menubar(this);
-
- // NOTE: We assume that the first menu is the App menu, which has a bold font.
- if (m_menus.is_empty())
- menu.set_title_font(Gfx::FontDatabase::default_bold_font());
-
- m_menus.append(&menu);
-}
-
-}
diff --git a/Services/WindowServer/MenuBar.h b/Services/WindowServer/MenuBar.h
deleted file mode 100644
index 31dff9ac0f..0000000000
--- a/Services/WindowServer/MenuBar.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include "Menu.h"
-#include <AK/Vector.h>
-#include <AK/WeakPtr.h>
-#include <AK/Weakable.h>
-
-namespace WindowServer {
-
-class MenuBar : public Weakable<MenuBar> {
-public:
- MenuBar(ClientConnection& client, int menubar_id);
- ~MenuBar();
-
- ClientConnection& client() { return m_client; }
- const ClientConnection& client() const { return m_client; }
- int menubar_id() const { return m_menubar_id; }
- void add_menu(Menu&);
-
- template<typename Callback>
- void for_each_menu(Callback callback)
- {
- for (auto& menu : m_menus) {
- if (callback(*menu) == IterationDecision::Break)
- return;
- }
- }
-
-private:
- ClientConnection& m_client;
- int m_menubar_id { 0 };
- Vector<Menu*> m_menus;
-};
-
-}
diff --git a/Services/WindowServer/MenuItem.cpp b/Services/WindowServer/MenuItem.cpp
deleted file mode 100644
index 324724a893..0000000000
--- a/Services/WindowServer/MenuItem.cpp
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "MenuItem.h"
-#include "ClientConnection.h"
-#include "Menu.h"
-#include "WindowManager.h"
-#include <LibGfx/Bitmap.h>
-
-namespace WindowServer {
-
-MenuItem::MenuItem(Menu& menu, unsigned identifier, const String& text, const String& shortcut_text, bool enabled, bool checkable, bool checked, const Gfx::Bitmap* icon)
- : m_menu(menu)
- , m_type(Text)
- , m_enabled(enabled)
- , m_checkable(checkable)
- , m_checked(checked)
- , m_identifier(identifier)
- , m_text(text)
- , m_shortcut_text(shortcut_text)
- , m_icon(icon)
-{
-}
-
-MenuItem::MenuItem(Menu& menu, Type type)
- : m_menu(menu)
- , m_type(type)
-{
-}
-
-MenuItem::~MenuItem()
-{
-}
-
-void MenuItem::set_enabled(bool enabled)
-{
- if (m_enabled == enabled)
- return;
- m_enabled = enabled;
- m_menu.redraw();
-}
-
-void MenuItem::set_checked(bool checked)
-{
- if (m_checked == checked)
- return;
- m_checked = checked;
- m_menu.redraw();
-}
-
-void MenuItem::set_default(bool is_default)
-{
- if (m_default == is_default)
- return;
- m_default = is_default;
- m_menu.redraw();
-}
-
-Menu* MenuItem::submenu()
-{
- ASSERT(is_submenu());
- ASSERT(m_menu.client());
- return m_menu.client()->find_menu_by_id(m_submenu_id);
-}
-
-const Menu* MenuItem::submenu() const
-{
- ASSERT(is_submenu());
- ASSERT(m_menu.client());
- return m_menu.client()->find_menu_by_id(m_submenu_id);
-}
-
-Gfx::IntRect MenuItem::rect() const
-{
- if (!m_menu.is_scrollable())
- return m_rect;
- return m_rect.translated(0, m_menu.item_height() - (m_menu.scroll_offset() * m_menu.item_height()));
-}
-
-void MenuItem::set_icon(const Gfx::Bitmap* icon)
-{
- if (m_icon == icon)
- return;
- m_icon = icon;
- m_menu.redraw();
-}
-
-}
diff --git a/Services/WindowServer/MenuItem.h b/Services/WindowServer/MenuItem.h
deleted file mode 100644
index 6bb1c203f1..0000000000
--- a/Services/WindowServer/MenuItem.h
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/Function.h>
-#include <AK/String.h>
-#include <LibGfx/Forward.h>
-#include <LibGfx/Rect.h>
-
-namespace WindowServer {
-
-class Menu;
-
-class MenuItem {
-public:
- enum Type {
- None,
- Text,
- Separator,
- };
-
- MenuItem(Menu&, unsigned identifier, const String& text, const String& shortcut_text = {}, bool enabled = true, bool checkable = false, bool checked = false, const Gfx::Bitmap* icon = nullptr);
- MenuItem(Menu&, Type);
- ~MenuItem();
-
- Type type() const { return m_type; }
-
- bool is_enabled() const { return m_enabled; }
- void set_enabled(bool);
-
- bool is_checkable() const { return m_checkable; }
- void set_checkable(bool checkable) { m_checkable = checkable; }
-
- bool is_checked() const { return m_checked; }
- void set_checked(bool);
-
- bool is_default() const { return m_default; }
- void set_default(bool);
-
- String text() const { return m_text; }
- void set_text(const String& text) { m_text = text; }
-
- String shortcut_text() const { return m_shortcut_text; }
- void set_shortcut_text(const String& text) { m_shortcut_text = text; }
-
- void set_rect(const Gfx::IntRect& rect) { m_rect = rect; }
- Gfx::IntRect rect() const;
-
- unsigned identifier() const { return m_identifier; }
-
- const Gfx::Bitmap* icon() const { return m_icon; }
- void set_icon(const Gfx::Bitmap*);
-
- bool is_submenu() const { return m_submenu_id != -1; }
- int submenu_id() const { return m_submenu_id; }
- void set_submenu_id(int submenu_id) { m_submenu_id = submenu_id; }
-
- Menu* submenu();
- const Menu* submenu() const;
-
- bool is_exclusive() const { return m_exclusive; }
- void set_exclusive(bool exclusive) { m_exclusive = exclusive; }
-
-private:
- Menu& m_menu;
- Type m_type { None };
- bool m_enabled { true };
- bool m_checkable { false };
- bool m_checked { false };
- bool m_default { false };
- unsigned m_identifier { 0 };
- String m_text;
- String m_shortcut_text;
- Gfx::IntRect m_rect;
- RefPtr<Gfx::Bitmap> m_icon;
- int m_submenu_id { -1 };
- bool m_exclusive { false };
-};
-
-}
diff --git a/Services/WindowServer/MenuManager.cpp b/Services/WindowServer/MenuManager.cpp
deleted file mode 100644
index ba2ba789f9..0000000000
--- a/Services/WindowServer/MenuManager.cpp
+++ /dev/null
@@ -1,497 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <AK/Badge.h>
-#include <AK/QuickSort.h>
-#include <LibCore/DirIterator.h>
-#include <LibGfx/Font.h>
-#include <LibGfx/Painter.h>
-#include <WindowServer/AppletManager.h>
-#include <WindowServer/MenuManager.h>
-#include <WindowServer/Screen.h>
-#include <WindowServer/WindowManager.h>
-#include <unistd.h>
-
-//#define DEBUG_MENUS
-
-namespace WindowServer {
-
-static MenuManager* s_the;
-static constexpr int s_search_timeout = 3000;
-
-MenuManager& MenuManager::the()
-{
- ASSERT(s_the);
- return *s_the;
-}
-
-MenuManager::MenuManager()
-{
- s_the = this;
- m_needs_window_resize = true;
-
- // NOTE: This ensures that the system menu has the correct dimensions.
- set_current_menubar(nullptr);
-
- m_window = Window::construct(*this, WindowType::Menubar);
- m_window->set_rect(menubar_rect());
-
- m_search_timer = Core::Timer::create_single_shot(0, [this] {
- m_current_search.clear();
- });
-}
-
-MenuManager::~MenuManager()
-{
-}
-
-bool MenuManager::is_open(const Menu& menu) const
-{
- for (size_t i = 0; i < m_open_menu_stack.size(); ++i) {
- if (&menu == m_open_menu_stack[i].ptr())
- return true;
- }
- return false;
-}
-
-void MenuManager::draw()
-{
- auto& wm = WindowManager::the();
- auto palette = wm.palette();
- auto menubar_rect = this->menubar_rect();
-
- if (m_needs_window_resize) {
- m_window->set_rect(menubar_rect);
- AppletManager::the().calculate_applet_rects(window());
- m_needs_window_resize = false;
- }
-
- Gfx::Painter painter(*window().backing_store());
-
- painter.fill_rect(menubar_rect, palette.window());
- painter.draw_line({ 0, menubar_rect.bottom() - 1 }, { menubar_rect.right(), menubar_rect.bottom() - 1 }, palette.threed_shadow1());
- painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow2());
-
- for_each_active_menubar_menu([&](Menu& menu) {
- Color text_color = palette.window_text();
- if (is_open(menu)) {
- painter.fill_rect(menu.rect_in_menubar(), palette.menu_selection());
- painter.draw_rect(menu.rect_in_menubar(), palette.menu_selection().darkened());
- text_color = palette.menu_selection_text();
- }
- painter.draw_text(
- menu.text_rect_in_menubar(),
- menu.name(),
- menu.title_font(),
- Gfx::TextAlignment::CenterLeft,
- text_color);
- //painter.draw_rect(menu.text_rect_in_menubar(), Color::Magenta);
- return IterationDecision::Continue;
- });
-
- AppletManager::the().draw();
-}
-
-void MenuManager::refresh()
-{
- if (!m_window)
- return;
- draw();
- window().invalidate();
-}
-
-void MenuManager::event(Core::Event& event)
-{
- if (static_cast<Event&>(event).is_mouse_event()) {
- handle_mouse_event(static_cast<MouseEvent&>(event));
- return;
- }
-
- if (static_cast<Event&>(event).is_key_event()) {
- auto& key_event = static_cast<const KeyEvent&>(event);
-
- if (key_event.type() == Event::KeyUp && key_event.key() == Key_Escape) {
- close_everyone();
- return;
- }
-
- if (key_event.key() == Key_Backspace) {
- m_current_search.clear();
- return;
- }
-
- if (m_current_menu && event.type() == Event::KeyDown
- && ((key_event.key() >= Key_A && key_event.key() <= Key_Z)
- || (key_event.key() >= Key_0 && key_event.key() <= Key_9))) {
- m_current_search.append_code_point(key_event.code_point());
- m_search_timer->restart(s_search_timeout);
- for (int i = 0; i < m_current_menu->item_count(); ++i) {
- auto text = m_current_menu->item(i).text();
- if (text.to_lowercase().starts_with(m_current_search.to_string().to_lowercase())) {
- m_current_menu->set_hovered_item(i);
- return;
- }
- }
- return;
- }
-
- if (event.type() == Event::KeyDown) {
-
- if (key_event.key() == Key_Left) {
- auto it = m_open_menu_stack.find_if([&](const auto& other) { return m_current_menu == other.ptr(); });
- ASSERT(!it.is_end());
-
- // Going "back" a menu should be the previous menu in the stack
- if (it.index() > 0)
- set_current_menu(m_open_menu_stack.at(it.index() - 1));
- close_everyone_not_in_lineage(*m_current_menu);
- return;
- }
-
- if (key_event.key() == Key_Right) {
- auto hovered_item = m_current_menu->hovered_item();
- if (hovered_item && hovered_item->is_submenu())
- m_current_menu->descend_into_submenu_at_hovered_item();
- return;
- }
-
- if (key_event.key() == Key_Return) {
- auto hovered_item = m_current_menu->hovered_item();
- if (!hovered_item || !hovered_item->is_enabled())
- return;
- if (hovered_item->is_submenu())
- m_current_menu->descend_into_submenu_at_hovered_item();
- else
- m_current_menu->open_hovered_item();
- return;
- }
- m_current_menu->dispatch_event(event);
- }
- }
-
- return Core::Object::event(event);
-}
-
-void MenuManager::handle_mouse_event(MouseEvent& mouse_event)
-{
- auto* active_window = WindowManager::the().active_window();
- bool handled_menubar_event = false;
- for_each_active_menubar_menu([&](Menu& menu) {
- if (menu.rect_in_menubar().contains(mouse_event.position())) {
- handled_menubar_event = &menu == m_system_menu || !active_window || !active_window->is_modal();
- if (handled_menubar_event)
- handle_menu_mouse_event(menu, mouse_event);
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
- });
- if (handled_menubar_event)
- return;
-
- if (has_open_menu()) {
- auto* topmost_menu = m_open_menu_stack.last().ptr();
- ASSERT(topmost_menu);
- auto* window = topmost_menu->menu_window();
- if (!window) {
- dbgln("MenuManager::handle_mouse_event: No menu window");
- return;
- }
- ASSERT(window->is_visible());
-
- bool event_is_inside_current_menu = window->rect().contains(mouse_event.position());
- if (event_is_inside_current_menu) {
- WindowManager::the().set_hovered_window(window);
- auto translated_event = mouse_event.translated(-window->position());
- WindowManager::the().deliver_mouse_event(*window, translated_event);
- return;
- }
-
- if (topmost_menu->hovered_item())
- topmost_menu->clear_hovered_item();
- if (mouse_event.type() == Event::MouseDown || mouse_event.type() == Event::MouseUp) {
- auto* window_menu_of = topmost_menu->window_menu_of();
- if (window_menu_of) {
- bool event_is_inside_taskbar_button = window_menu_of->taskbar_rect().contains(mouse_event.position());
- if (event_is_inside_taskbar_button && !topmost_menu->is_window_menu_open()) {
- topmost_menu->set_window_menu_open(true);
- return;
- }
- }
-
- if (mouse_event.type() == Event::MouseDown) {
- close_bar();
- topmost_menu->set_window_menu_open(false);
- }
- }
-
- if (mouse_event.type() == Event::MouseMove) {
- for (auto& menu : m_open_menu_stack) {
- if (!menu)
- continue;
- if (!menu->menu_window()->rect().contains(mouse_event.position()))
- continue;
- WindowManager::the().set_hovered_window(menu->menu_window());
- auto translated_event = mouse_event.translated(-menu->menu_window()->position());
- WindowManager::the().deliver_mouse_event(*menu->menu_window(), translated_event);
- break;
- }
- }
- return;
- }
-
- AppletManager::the().dispatch_event(static_cast<Event&>(mouse_event));
-}
-
-void MenuManager::handle_menu_mouse_event(Menu& menu, const MouseEvent& event)
-{
- bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove
- && has_open_menu()
- && (m_open_menu_stack.first()->menubar() || m_open_menu_stack.first() == m_system_menu.ptr());
- bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Left;
- bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
-
- if (is_mousedown_with_left_button)
- m_bar_open = !m_bar_open;
-
- if (should_open_menu && m_bar_open) {
- close_everyone();
- open_menu(menu);
- return;
- }
-
- if (!m_bar_open)
- close_everyone();
-}
-
-void MenuManager::set_needs_window_resize()
-{
- m_needs_window_resize = true;
-}
-
-void MenuManager::close_all_menus_from_client(Badge<ClientConnection>, ClientConnection& client)
-{
- if (!has_open_menu())
- return;
- if (m_open_menu_stack.first()->client() != &client)
- return;
- close_everyone();
-}
-
-void MenuManager::close_everyone()
-{
- for (auto& menu : m_open_menu_stack) {
- ASSERT(menu);
- if (menu->menu_window())
- menu->menu_window()->set_visible(false);
- menu->clear_hovered_item();
- }
- m_open_menu_stack.clear();
- m_current_search.clear();
- clear_current_menu();
- refresh();
-}
-
-void MenuManager::close_everyone_not_in_lineage(Menu& menu)
-{
- Vector<Menu*> menus_to_close;
- for (auto& open_menu : m_open_menu_stack) {
- if (!open_menu)
- continue;
- if (&menu == open_menu.ptr() || open_menu->is_menu_ancestor_of(menu))
- continue;
- menus_to_close.append(open_menu);
- }
- close_menus(menus_to_close);
-}
-
-void MenuManager::close_menus(const Vector<Menu*>& menus)
-{
- for (auto& menu : menus) {
- if (menu == m_current_menu)
- clear_current_menu();
- if (menu->menu_window())
- menu->menu_window()->set_visible(false);
- menu->clear_hovered_item();
- m_open_menu_stack.remove_first_matching([&](auto& entry) {
- return entry == menu;
- });
- }
- refresh();
-}
-
-static void collect_menu_subtree(Menu& menu, Vector<Menu*>& menus)
-{
- menus.append(&menu);
- for (int i = 0; i < menu.item_count(); ++i) {
- auto& item = menu.item(i);
- if (!item.is_submenu())
- continue;
- collect_menu_subtree(*item.submenu(), menus);
- }
-}
-
-void MenuManager::close_menu_and_descendants(Menu& menu)
-{
- Vector<Menu*> menus_to_close;
- collect_menu_subtree(menu, menus_to_close);
- close_menus(menus_to_close);
-}
-
-void MenuManager::toggle_menu(Menu& menu)
-{
- if (is_open(menu)) {
- close_menu_and_descendants(menu);
- return;
- }
- open_menu(menu);
-}
-
-void MenuManager::open_menu(Menu& menu, bool as_current_menu)
-{
- if (is_open(menu)) {
- if (as_current_menu || current_menu() != &menu) {
- // This menu is already open. If requested, or if the current
- // window doesn't match this one, then set it to this
- set_current_menu(&menu);
- }
- return;
- }
-
- if (!menu.is_empty()) {
- menu.redraw_if_theme_changed();
- if (!menu.menu_window()) {
- auto& menu_window = menu.ensure_menu_window();
- menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
- }
- menu.menu_window()->set_visible(true);
- }
-
- if (m_open_menu_stack.find_if([&menu](auto& other) { return &menu == other.ptr(); }).is_end())
- m_open_menu_stack.append(menu);
-
- if (as_current_menu || !current_menu()) {
- // Only make this menu the current menu if requested, or if no
- // other menu is current
- set_current_menu(&menu);
- }
-
- refresh();
-}
-
-void MenuManager::clear_current_menu()
-{
- Menu* previous_current_menu = m_current_menu;
- m_current_menu = nullptr;
- if (previous_current_menu) {
- // When closing the last menu, restore the previous active input window
- auto& wm = WindowManager::the();
- wm.restore_active_input_window(m_previous_input_window);
- }
-}
-
-void MenuManager::set_current_menu(Menu* menu)
-{
- if (!menu) {
- clear_current_menu();
- return;
- }
-
- ASSERT(is_open(*menu));
- if (menu == m_current_menu) {
- return;
- }
-
- m_current_search.clear();
-
- Menu* previous_current_menu = m_current_menu;
- m_current_menu = menu;
-
- auto& wm = WindowManager::the();
- if (!previous_current_menu) {
- // When opening the first menu, store the current active input window
- if (auto* active_input = wm.active_input_window())
- m_previous_input_window = *active_input;
- else
- m_previous_input_window = nullptr;
- }
-
- wm.set_active_input_window(m_current_menu->menu_window());
-}
-
-void MenuManager::close_bar()
-{
- close_everyone();
- m_bar_open = false;
-}
-
-Gfx::IntRect MenuManager::menubar_rect() const
-{
- return { 0, 0, Screen::the().rect().width(), 19 };
-}
-
-void MenuManager::set_current_menubar(MenuBar* menubar)
-{
- if (menubar)
- m_current_menubar = *menubar;
- else
- m_current_menubar = nullptr;
-#ifdef DEBUG_MENUS
- dbg() << "[WM] Current menubar is now " << menubar;
-#endif
- Gfx::IntPoint next_menu_location { MenuManager::menubar_menu_margin() / 2, 0 };
- for_each_active_menubar_menu([&](Menu& menu) {
- int text_width = menu.title_font().width(menu.name());
- menu.set_rect_in_menubar({ next_menu_location.x() - MenuManager::menubar_menu_margin() / 2, 0, text_width + MenuManager::menubar_menu_margin(), menubar_rect().height() - 1 });
-
- Gfx::IntRect text_rect { next_menu_location.translated(0, 1), { text_width, menubar_rect().height() - 3 } };
-
- menu.set_text_rect_in_menubar(text_rect);
- next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
- return IterationDecision::Continue;
- });
- refresh();
-}
-
-void MenuManager::close_menubar(MenuBar& menubar)
-{
- if (current_menubar() == &menubar)
- set_current_menubar(nullptr);
-}
-
-void MenuManager::set_system_menu(Menu& menu)
-{
- m_system_menu = menu;
- set_current_menubar(m_current_menubar);
-}
-
-void MenuManager::did_change_theme()
-{
- ++m_theme_index;
- refresh();
-}
-
-}
diff --git a/Services/WindowServer/MenuManager.h b/Services/WindowServer/MenuManager.h
deleted file mode 100644
index 0d56f6a8b7..0000000000
--- a/Services/WindowServer/MenuManager.h
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include "Menu.h"
-#include "MenuBar.h"
-#include "Window.h"
-#include <AK/HashMap.h>
-#include <AK/StringBuilder.h>
-#include <LibCore/Object.h>
-#include <LibCore/Timer.h>
-
-namespace WindowServer {
-
-class MenuManager final : public Core::Object {
- C_OBJECT(MenuManager)
-public:
- static MenuManager& the();
-
- MenuManager();
- virtual ~MenuManager() override;
-
- void refresh();
-
- bool is_open(const Menu&) const;
- bool has_open_menu() const { return !m_open_menu_stack.is_empty(); }
-
- Gfx::IntRect menubar_rect() const;
- static int menubar_menu_margin() { return 16; }
-
- void set_needs_window_resize();
-
- Menu* current_menu() { return m_current_menu.ptr(); }
- void set_current_menu(Menu*);
- void clear_current_menu();
- void open_menu(Menu&, bool as_current_menu = true);
- void toggle_menu(Menu&);
-
- MenuBar* current_menubar() { return m_current_menubar.ptr(); }
- void set_current_menubar(MenuBar*);
- void close_menubar(MenuBar&);
-
- void close_bar();
- void close_everyone();
- void close_everyone_not_in_lineage(Menu&);
- void close_menu_and_descendants(Menu&);
-
- void close_all_menus_from_client(Badge<ClientConnection>, ClientConnection&);
-
- Menu* system_menu() { return m_system_menu; }
- void set_system_menu(Menu&);
-
- int theme_index() const { return m_theme_index; }
-
- Window& window() { return *m_window; }
-
- template<typename Callback>
- void for_each_active_menubar_menu(Callback callback)
- {
- if (system_menu()) {
- if (callback(*system_menu()) == IterationDecision::Break)
- return;
- }
- if (m_current_menubar)
- m_current_menubar->for_each_menu(callback);
- }
-
- void did_change_theme();
-
-private:
- void close_menus(const Vector<Menu*>&);
-
- const Window& window() const { return *m_window; }
-
- virtual void event(Core::Event&) override;
- void handle_mouse_event(MouseEvent&);
- void handle_menu_mouse_event(Menu&, const MouseEvent&);
-
- void draw();
-
- RefPtr<Window> m_window;
-
- WeakPtr<Menu> m_current_menu;
- WeakPtr<Window> m_previous_input_window;
- Vector<WeakPtr<Menu>> m_open_menu_stack;
-
- RefPtr<Core::Timer> m_search_timer;
- StringBuilder m_current_search;
- WeakPtr<Menu> m_system_menu;
-
- bool m_needs_window_resize { false };
- bool m_bar_open { false };
-
- int m_theme_index { 0 };
-
- WeakPtr<MenuBar> m_current_menubar;
-};
-
-}
diff --git a/Services/WindowServer/Screen.cpp b/Services/WindowServer/Screen.cpp
deleted file mode 100644
index 4cc9ae809b..0000000000
--- a/Services/WindowServer/Screen.cpp
+++ /dev/null
@@ -1,186 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "Screen.h"
-#include "Compositor.h"
-#include "Event.h"
-#include "EventLoop.h"
-#include "WindowManager.h"
-#include <Kernel/API/FB.h>
-#include <Kernel/API/MousePacket.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <sys/mman.h>
-#include <unistd.h>
-
-namespace WindowServer {
-
-static Screen* s_the;
-
-Screen& Screen::the()
-{
- ASSERT(s_the);
- return *s_the;
-}
-
-Screen::Screen(unsigned desired_width, unsigned desired_height)
-{
- ASSERT(!s_the);
- s_the = this;
- m_framebuffer_fd = open("/dev/fb0", O_RDWR | O_CLOEXEC);
- if (m_framebuffer_fd < 0) {
- perror("failed to open /dev/fb0");
- ASSERT_NOT_REACHED();
- }
-
- if (fb_set_buffer(m_framebuffer_fd, 0) == 0) {
- m_can_set_buffer = true;
- }
-
- set_resolution(desired_width, desired_height);
- m_cursor_location = rect().center();
-}
-
-Screen::~Screen()
-{
- close(m_framebuffer_fd);
-}
-
-bool Screen::set_resolution(int width, int height)
-{
- FBResolution resolution { 0, (unsigned)width, (unsigned)height };
- int rc = fb_set_resolution(m_framebuffer_fd, &resolution);
-#ifdef WSSCREEN_DEBUG
- dbg() << "fb_set_resolution() - return code " << rc;
-#endif
- if (rc == 0) {
- on_change_resolution(resolution.pitch, resolution.width, resolution.height);
- return true;
- }
- if (rc == -1) {
- dbg() << "Invalid resolution " << width << "x" << height;
- on_change_resolution(resolution.pitch, resolution.width, resolution.height);
- return false;
- }
- ASSERT_NOT_REACHED();
-}
-
-void Screen::on_change_resolution(int pitch, int width, int height)
-{
- if (m_framebuffer) {
- size_t previous_size_in_bytes = m_size_in_bytes;
- int rc = munmap(m_framebuffer, previous_size_in_bytes);
- ASSERT(rc == 0);
- }
-
- int rc = fb_get_size_in_bytes(m_framebuffer_fd, &m_size_in_bytes);
- ASSERT(rc == 0);
-
- m_framebuffer = (Gfx::RGBA32*)mmap(nullptr, m_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_framebuffer_fd, 0);
- ASSERT(m_framebuffer && m_framebuffer != (void*)-1);
-
- m_pitch = pitch;
- m_width = width;
- m_height = height;
-
- m_cursor_location.constrain(rect());
-}
-
-void Screen::set_buffer(int index)
-{
- ASSERT(m_can_set_buffer);
- int rc = fb_set_buffer(m_framebuffer_fd, index);
- ASSERT(rc == 0);
-}
-
-void Screen::set_acceleration_factor(double factor)
-{
- ASSERT(factor >= mouse_accel_min && factor <= mouse_accel_max);
- m_acceleration_factor = factor;
-}
-
-void Screen::set_scroll_step_size(unsigned step_size)
-{
- ASSERT(step_size >= scroll_step_size_min);
- m_scroll_step_size = step_size;
-}
-
-void Screen::on_receive_mouse_data(const MousePacket& packet)
-{
- auto prev_location = m_cursor_location;
- if (packet.is_relative) {
- m_cursor_location.move_by(packet.x * m_acceleration_factor, packet.y * m_acceleration_factor);
-#ifdef WSSCREEN_DEBUG
- dbgln("Screen: New Relative mouse point @ {}", m_cursor_location);
-#endif
- } else {
- m_cursor_location = { packet.x * m_width / 0xffff, packet.y * m_height / 0xffff };
-#ifdef WSSCREEN_DEBUG
- dbgln("Screen: New Absolute mouse point @ {}", m_cursor_location);
-#endif
- }
-
- m_cursor_location.constrain(rect());
-
- unsigned buttons = packet.buttons;
- unsigned prev_buttons = m_mouse_button_state;
- m_mouse_button_state = buttons;
- unsigned changed_buttons = prev_buttons ^ buttons;
- auto post_mousedown_or_mouseup_if_needed = [&](MouseButton button) {
- if (!(changed_buttons & (unsigned)button))
- return;
- auto message = make<MouseEvent>(buttons & (unsigned)button ? Event::MouseDown : Event::MouseUp, m_cursor_location, buttons, button, m_modifiers);
- Core::EventLoop::current().post_event(WindowManager::the(), move(message));
- };
- post_mousedown_or_mouseup_if_needed(MouseButton::Left);
- post_mousedown_or_mouseup_if_needed(MouseButton::Right);
- post_mousedown_or_mouseup_if_needed(MouseButton::Middle);
- post_mousedown_or_mouseup_if_needed(MouseButton::Back);
- post_mousedown_or_mouseup_if_needed(MouseButton::Forward);
- if (m_cursor_location != prev_location) {
- auto message = make<MouseEvent>(Event::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
- if (WindowManager::the().dnd_client())
- message->set_mime_data(WindowManager::the().dnd_mime_data());
- Core::EventLoop::current().post_event(WindowManager::the(), move(message));
- }
-
- if (packet.z) {
- auto message = make<MouseEvent>(Event::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, packet.z * m_scroll_step_size);
- Core::EventLoop::current().post_event(WindowManager::the(), move(message));
- }
-
- if (m_cursor_location != prev_location)
- Compositor::the().invalidate_cursor();
-}
-
-void Screen::on_receive_keyboard_data(::KeyEvent kernel_event)
-{
- m_modifiers = kernel_event.modifiers();
- auto message = make<KeyEvent>(kernel_event.is_press() ? Event::KeyDown : Event::KeyUp, kernel_event.key, kernel_event.code_point, kernel_event.modifiers(), kernel_event.scancode);
- Core::EventLoop::current().post_event(WindowManager::the(), move(message));
-}
-
-}
diff --git a/Services/WindowServer/Screen.h b/Services/WindowServer/Screen.h
deleted file mode 100644
index c435bc62c9..0000000000
--- a/Services/WindowServer/Screen.h
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <Kernel/API/KeyCode.h>
-#include <LibGfx/Color.h>
-#include <LibGfx/Rect.h>
-#include <LibGfx/Size.h>
-
-struct MousePacket;
-
-namespace WindowServer {
-
-const double mouse_accel_max = 3.5;
-const double mouse_accel_min = 0.5;
-const unsigned scroll_step_size_min = 1;
-
-class Screen {
-public:
- Screen(unsigned width, unsigned height);
- ~Screen();
-
- bool set_resolution(int width, int height);
- bool can_set_buffer() { return m_can_set_buffer; }
- void set_buffer(int index);
-
- int width() const { return m_width; }
- int height() const { return m_height; }
- size_t pitch() const { return m_pitch; }
- Gfx::RGBA32* scanline(int y);
-
- static Screen& the();
-
- Gfx::IntSize size() const { return { width(), height() }; }
- Gfx::IntRect rect() const { return { 0, 0, width(), height() }; }
-
- Gfx::IntPoint cursor_location() const { return m_cursor_location; }
- unsigned mouse_button_state() const { return m_mouse_button_state; }
-
- double acceleration_factor() const { return m_acceleration_factor; }
- void set_acceleration_factor(double);
-
- unsigned scroll_step_size() const { return m_scroll_step_size; }
- void set_scroll_step_size(unsigned);
-
- void on_receive_mouse_data(const MousePacket&);
- void on_receive_keyboard_data(::KeyEvent);
-
-private:
- void on_change_resolution(int pitch, int width, int height);
-
- size_t m_size_in_bytes;
-
- Gfx::RGBA32* m_framebuffer { nullptr };
- bool m_can_set_buffer { false };
-
- int m_pitch { 0 };
- int m_width { 0 };
- int m_height { 0 };
- int m_framebuffer_fd { -1 };
-
- Gfx::IntPoint m_cursor_location;
- unsigned m_mouse_button_state { 0 };
- unsigned m_modifiers { 0 };
- double m_acceleration_factor { 1.0 };
- unsigned m_scroll_step_size { 1 };
-};
-
-inline Gfx::RGBA32* Screen::scanline(int y)
-{
- return reinterpret_cast<Gfx::RGBA32*>(((u8*)m_framebuffer) + (y * m_pitch));
-}
-
-}
diff --git a/Services/WindowServer/Window.cpp b/Services/WindowServer/Window.cpp
deleted file mode 100644
index 1d0497c1c6..0000000000
--- a/Services/WindowServer/Window.cpp
+++ /dev/null
@@ -1,776 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "Window.h"
-#include "AppletManager.h"
-#include "ClientConnection.h"
-#include "Compositor.h"
-#include "Event.h"
-#include "EventLoop.h"
-#include "Screen.h"
-#include "WindowManager.h"
-#include <AK/Badge.h>
-#include <WindowServer/WindowClientEndpoint.h>
-
-namespace WindowServer {
-
-static String default_window_icon_path()
-{
- return "/res/icons/16x16/window.png";
-}
-
-static Gfx::Bitmap& default_window_icon()
-{
- static Gfx::Bitmap* s_icon;
- if (!s_icon)
- s_icon = Gfx::Bitmap::load_from_file(default_window_icon_path()).leak_ref();
- return *s_icon;
-}
-
-static Gfx::Bitmap& minimize_icon()
-{
- static Gfx::Bitmap* s_icon;
- if (!s_icon)
- s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png").leak_ref();
- return *s_icon;
-}
-
-static Gfx::Bitmap& maximize_icon()
-{
- static Gfx::Bitmap* s_icon;
- if (!s_icon)
- s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png").leak_ref();
- return *s_icon;
-}
-
-static Gfx::Bitmap& restore_icon()
-{
- static Gfx::Bitmap* s_icon;
- if (!s_icon)
- s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-restore.png").leak_ref();
- return *s_icon;
-}
-
-static Gfx::Bitmap& close_icon()
-{
- static Gfx::Bitmap* s_icon;
- if (!s_icon)
- s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png").leak_ref();
- return *s_icon;
-}
-
-Window::Window(Core::Object& parent, WindowType type)
- : Core::Object(&parent)
- , m_type(type)
- , m_icon(default_window_icon())
- , m_frame(*this)
-{
- WindowManager::the().add_window(*this);
-}
-
-Window::Window(ClientConnection& client, WindowType window_type, int window_id, bool modal, bool minimizable, bool frameless, bool resizable, bool fullscreen, bool accessory, Window* parent_window)
- : Core::Object(&client)
- , m_client(&client)
- , m_type(window_type)
- , m_modal(modal)
- , m_minimizable(minimizable)
- , m_frameless(frameless)
- , m_resizable(resizable)
- , m_fullscreen(fullscreen)
- , m_accessory(accessory)
- , m_window_id(window_id)
- , m_client_id(client.client_id())
- , m_icon(default_window_icon())
- , m_frame(*this)
-{
- // FIXME: This should not be hard-coded here.
- if (m_type == WindowType::Taskbar) {
- m_wm_event_mask = WMEventMask::WindowStateChanges | WMEventMask::WindowRemovals | WMEventMask::WindowIconChanges;
- m_listens_to_wm_events = true;
- }
-
- if (parent_window)
- set_parent_window(*parent_window);
- WindowManager::the().add_window(*this);
-}
-
-Window::~Window()
-{
- // Detach from client at the start of teardown since we don't want
- // to confuse things by trying to send messages to it.
- m_client = nullptr;
-
- WindowManager::the().remove_window(*this);
-}
-
-void Window::destroy()
-{
- m_destroyed = true;
- set_visible(false);
-}
-
-void Window::set_title(const String& title)
-{
- if (m_title == title)
- return;
- m_title = title;
- frame().invalidate_title_bar();
- WindowManager::the().notify_title_changed(*this);
-}
-
-void Window::set_rect(const Gfx::IntRect& rect)
-{
- ASSERT(!rect.is_empty());
- if (m_rect == rect)
- return;
- auto old_rect = m_rect;
- m_rect = rect;
- if (!m_client && (!m_backing_store || old_rect.size() != rect.size())) {
- m_backing_store = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, m_rect.size());
- }
-
- invalidate(true);
- m_frame.notify_window_rect_changed(old_rect, rect); // recomputes occlusions
-}
-
-void Window::set_rect_without_repaint(const Gfx::IntRect& rect)
-{
- ASSERT(!rect.is_empty());
- if (m_rect == rect)
- return;
- auto old_rect = m_rect;
- m_rect = rect;
-
- if (old_rect.size() == m_rect.size()) {
- auto delta = m_rect.location() - old_rect.location();
- for (auto& child_window : m_child_windows) {
- if (child_window)
- child_window->move_by(delta);
- }
- }
-
- invalidate(true);
- m_frame.notify_window_rect_changed(old_rect, rect); // recomputes occlusions
-}
-
-void Window::handle_mouse_event(const MouseEvent& event)
-{
- set_automatic_cursor_tracking_enabled(event.buttons() != 0);
-
- switch (event.type()) {
- case Event::MouseMove:
- m_client->post_message(Messages::WindowClient::MouseMove(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta(), event.is_drag(), event.mime_types()));
- break;
- case Event::MouseDown:
- m_client->post_message(Messages::WindowClient::MouseDown(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
- break;
- case Event::MouseDoubleClick:
- m_client->post_message(Messages::WindowClient::MouseDoubleClick(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
- break;
- case Event::MouseUp:
- m_client->post_message(Messages::WindowClient::MouseUp(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
- break;
- case Event::MouseWheel:
- m_client->post_message(Messages::WindowClient::MouseWheel(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
- break;
- default:
- ASSERT_NOT_REACHED();
- }
-}
-
-void Window::update_menu_item_text(PopupMenuItem item)
-{
- if (m_window_menu) {
- m_window_menu->item((int)item).set_text(item == PopupMenuItem::Minimize ? (m_minimized ? "Unminimize" : "Minimize") : (m_maximized ? "Restore" : "Maximize"));
- m_window_menu->redraw();
- }
-}
-
-void Window::update_menu_item_enabled(PopupMenuItem item)
-{
- if (m_window_menu) {
- m_window_menu->item((int)item).set_enabled(item == PopupMenuItem::Minimize ? m_minimizable : m_resizable);
- m_window_menu->redraw();
- }
-}
-
-void Window::set_minimized(bool minimized)
-{
- if (m_minimized == minimized)
- return;
- if (minimized && !m_minimizable)
- return;
- m_minimized = minimized;
- update_menu_item_text(PopupMenuItem::Minimize);
- Compositor::the().invalidate_occlusions();
- Compositor::the().invalidate_screen(frame().rect());
- if (!blocking_modal_window())
- start_minimize_animation();
- if (!minimized)
- request_update({ {}, size() });
- WindowManager::the().notify_minimization_state_changed(*this);
-}
-
-void Window::set_minimizable(bool minimizable)
-{
- if (m_minimizable == minimizable)
- return;
- m_minimizable = minimizable;
- update_menu_item_enabled(PopupMenuItem::Minimize);
- // TODO: Hide/show (or alternatively change enabled state of) window minimize button dynamically depending on value of m_minimizable
-}
-
-void Window::set_taskbar_rect(const Gfx::IntRect& rect)
-{
- m_taskbar_rect = rect;
- m_have_taskbar_rect = !m_taskbar_rect.is_empty();
-}
-
-void Window::start_minimize_animation()
-{
- if (!m_have_taskbar_rect) {
- // If this is a modal window, it may not have its own taskbar
- // button, so there is no rectangle. In that case, walk the
- // modal stack until we find a window that may have one
- WindowManager::the().for_each_window_in_modal_stack(*this, [&](Window& w, bool) {
- if (w.has_taskbar_rect()) {
- // We purposely do NOT set m_have_taskbar_rect to true here
- // because we want to only copy the rectangle from the
- // window that has it, but since this window wouldn't receive
- // any updates down the road we want to query it again
- // next time we want to start the animation
- m_taskbar_rect = w.taskbar_rect();
-
- ASSERT(!m_have_taskbar_rect); // should remain unset!
- return IterationDecision::Break;
- };
- return IterationDecision::Continue;
- });
- }
- m_minimize_animation_step = 0;
-}
-
-void Window::set_opacity(float opacity)
-{
- if (m_opacity == opacity)
- return;
- bool was_opaque = is_opaque();
- m_opacity = opacity;
- if (was_opaque != is_opaque())
- Compositor::the().invalidate_occlusions();
- Compositor::the().invalidate_screen(frame().rect());
- WindowManager::the().notify_opacity_changed(*this);
-}
-
-void Window::set_occluded(bool occluded)
-{
- if (m_occluded == occluded)
- return;
- m_occluded = occluded;
- WindowManager::the().notify_occlusion_state_changed(*this);
-}
-
-void Window::set_maximized(bool maximized)
-{
- if (m_maximized == maximized)
- return;
- if (maximized && (!is_resizable() || resize_aspect_ratio().has_value()))
- return;
- set_tiled(WindowTileType::None);
- m_maximized = maximized;
- update_menu_item_text(PopupMenuItem::Maximize);
- if (maximized) {
- m_unmaximized_rect = m_rect;
- set_rect(WindowManager::the().maximized_window_rect(*this));
- } else {
- set_rect(m_unmaximized_rect);
- }
- m_frame.did_set_maximized({}, maximized);
- Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect));
- set_default_positioned(false);
-}
-
-void Window::set_resizable(bool resizable)
-{
- if (m_resizable == resizable)
- return;
- m_resizable = resizable;
- update_menu_item_enabled(PopupMenuItem::Maximize);
- // TODO: Hide/show (or alternatively change enabled state of) window maximize button dynamically depending on value of is_resizable()
-}
-
-void Window::event(Core::Event& event)
-{
- if (!m_client) {
- ASSERT(parent());
- event.ignore();
- return;
- }
-
- if (blocking_modal_window()) {
- // We still want to handle the WindowDeactivated event below when a new modal is
- // created to notify its parent window, despite it being "blocked by modal window".
- if (event.type() != Event::WindowDeactivated)
- return;
- }
-
- if (static_cast<Event&>(event).is_mouse_event())
- return handle_mouse_event(static_cast<const MouseEvent&>(event));
-
- switch (event.type()) {
- case Event::WindowEntered:
- m_client->post_message(Messages::WindowClient::WindowEntered(m_window_id));
- break;
- case Event::WindowLeft:
- m_client->post_message(Messages::WindowClient::WindowLeft(m_window_id));
- break;
- case Event::KeyDown:
- m_client->post_message(
- Messages::WindowClient::KeyDown(m_window_id,
- (u32) static_cast<const KeyEvent&>(event).code_point(),
- (u32) static_cast<const KeyEvent&>(event).key(),
- static_cast<const KeyEvent&>(event).modifiers(),
- (u32) static_cast<const KeyEvent&>(event).scancode()));
- break;
- case Event::KeyUp:
- m_client->post_message(
- Messages::WindowClient::KeyUp(m_window_id,
- (u32) static_cast<const KeyEvent&>(event).code_point(),
- (u32) static_cast<const KeyEvent&>(event).key(),
- static_cast<const KeyEvent&>(event).modifiers(),
- (u32) static_cast<const KeyEvent&>(event).scancode()));
- break;
- case Event::WindowActivated:
- m_client->post_message(Messages::WindowClient::WindowActivated(m_window_id));
- break;
- case Event::WindowDeactivated:
- m_client->post_message(Messages::WindowClient::WindowDeactivated(m_window_id));
- break;
- case Event::WindowInputEntered:
- m_client->post_message(Messages::WindowClient::WindowInputEntered(m_window_id));
- break;
- case Event::WindowInputLeft:
- m_client->post_message(Messages::WindowClient::WindowInputLeft(m_window_id));
- break;
- case Event::WindowCloseRequest:
- m_client->post_message(Messages::WindowClient::WindowCloseRequest(m_window_id));
- break;
- case Event::WindowResized:
- m_client->post_message(Messages::WindowClient::WindowResized(m_window_id, static_cast<const ResizeEvent&>(event).rect()));
- break;
- default:
- break;
- }
-}
-
-void Window::set_global_cursor_tracking_enabled(bool enabled)
-{
- m_global_cursor_tracking_enabled = enabled;
-}
-
-void Window::set_visible(bool b)
-{
- if (m_visible == b)
- return;
- m_visible = b;
-
- Compositor::the().invalidate_occlusions();
- if (m_visible)
- invalidate(true);
- else
- Compositor::the().invalidate_screen(frame().rect());
-}
-
-void Window::invalidate(bool invalidate_frame)
-{
- m_invalidated = true;
- m_invalidated_all = true;
- m_invalidated_frame |= invalidate_frame;
- m_dirty_rects.clear();
- Compositor::the().invalidate_window();
-}
-
-void Window::invalidate(const Gfx::IntRect& rect, bool with_frame)
-{
- if (type() == WindowType::MenuApplet) {
- AppletManager::the().invalidate_applet(*this, rect);
- return;
- }
-
- if (invalidate_no_notify(rect, with_frame))
- Compositor::the().invalidate_window();
-}
-
-bool Window::invalidate_no_notify(const Gfx::IntRect& rect, bool with_frame)
-{
- if (rect.is_empty())
- return false;
- if (m_invalidated_all) {
- m_invalidated_frame |= with_frame;
- return false;
- }
-
- auto outer_rect = frame().rect();
- auto inner_rect = rect;
- inner_rect.move_by(position());
- // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
- inner_rect.intersect(outer_rect);
- if (inner_rect.is_empty())
- return false;
-
- m_invalidated = true;
- m_invalidated_frame |= with_frame;
- m_dirty_rects.add(inner_rect.translated(-outer_rect.location()));
- return true;
-}
-
-void Window::prepare_dirty_rects()
-{
- if (m_invalidated_all) {
- if (m_invalidated_frame)
- m_dirty_rects = frame().rect();
- else
- m_dirty_rects = rect();
- } else {
- m_dirty_rects.move_by(frame().rect().location());
- }
-}
-
-void Window::clear_dirty_rects()
-{
- m_invalidated_all = false;
- m_invalidated_frame = false;
- m_invalidated = false;
- m_dirty_rects.clear_with_capacity();
-}
-
-bool Window::is_active() const
-{
- return WindowManager::the().active_window() == this;
-}
-
-Window* Window::blocking_modal_window()
-{
- // A window is blocked if any immediate child, or any child further
- // down the chain is modal
- for (auto& window : m_child_windows) {
- if (window && !window->is_destroyed()) {
- if (window->is_modal())
- return window;
-
- if (auto* blocking_window = window->blocking_modal_window())
- return blocking_window;
- }
- }
- return nullptr;
-}
-
-void Window::set_default_icon()
-{
- m_icon = default_window_icon();
-}
-
-void Window::request_update(const Gfx::IntRect& rect, bool ignore_occlusion)
-{
- if (rect.is_empty())
- return;
- if (m_pending_paint_rects.is_empty()) {
- deferred_invoke([this, ignore_occlusion](auto&) {
- client()->post_paint_message(*this, ignore_occlusion);
- });
- }
- m_pending_paint_rects.add(rect);
-}
-
-void Window::ensure_window_menu()
-{
- if (!m_window_menu) {
- m_window_menu = Menu::construct(nullptr, -1, "(Window Menu)");
- m_window_menu->set_window_menu_of(*this);
-
- auto minimize_item = make<MenuItem>(*m_window_menu, 1, m_minimized ? "Unminimize" : "Minimize");
- m_window_menu_minimize_item = minimize_item.ptr();
- m_window_menu->add_item(move(minimize_item));
-
- auto maximize_item = make<MenuItem>(*m_window_menu, 2, m_maximized ? "Restore" : "Maximize");
- m_window_menu_maximize_item = maximize_item.ptr();
- m_window_menu->add_item(move(maximize_item));
-
- m_window_menu->add_item(make<MenuItem>(*m_window_menu, MenuItem::Type::Separator));
-
- auto close_item = make<MenuItem>(*m_window_menu, 3, "Close");
- m_window_menu_close_item = close_item.ptr();
- m_window_menu_close_item->set_icon(&close_icon());
- m_window_menu_close_item->set_default(true);
- m_window_menu->add_item(move(close_item));
-
- m_window_menu->item((int)PopupMenuItem::Minimize).set_enabled(m_minimizable);
- m_window_menu->item((int)PopupMenuItem::Maximize).set_enabled(m_resizable);
-
- m_window_menu->on_item_activation = [&](auto& item) {
- switch (item.identifier()) {
- case 1:
- WindowManager::the().minimize_windows(*this, !m_minimized);
- if (!m_minimized)
- WindowManager::the().move_to_front_and_make_active(*this);
- break;
- case 2:
- WindowManager::the().maximize_windows(*this, !m_maximized);
- WindowManager::the().move_to_front_and_make_active(*this);
- break;
- case 3:
- request_close();
- break;
- }
- };
- }
-}
-
-void Window::popup_window_menu(const Gfx::IntPoint& position, WindowMenuDefaultAction default_action)
-{
- ensure_window_menu();
- if (default_action == WindowMenuDefaultAction::BasedOnWindowState) {
- // When clicked on the task bar, determine the default action
- if (!is_active() && !is_minimized())
- default_action = WindowMenuDefaultAction::None;
- else if (is_minimized())
- default_action = WindowMenuDefaultAction::Unminimize;
- else
- default_action = WindowMenuDefaultAction::Minimize;
- }
- m_window_menu_minimize_item->set_default(default_action == WindowMenuDefaultAction::Minimize || default_action == WindowMenuDefaultAction::Unminimize);
- m_window_menu_minimize_item->set_icon(m_minimized ? nullptr : &minimize_icon());
- m_window_menu_maximize_item->set_default(default_action == WindowMenuDefaultAction::Maximize || default_action == WindowMenuDefaultAction::Restore);
- m_window_menu_maximize_item->set_icon(m_maximized ? &restore_icon() : &maximize_icon());
- m_window_menu_close_item->set_default(default_action == WindowMenuDefaultAction::Close);
-
- m_window_menu->popup(position);
-}
-
-void Window::window_menu_activate_default()
-{
- ensure_window_menu();
- m_window_menu->activate_default();
-}
-
-void Window::request_close()
-{
- Event close_request(Event::WindowCloseRequest);
- event(close_request);
-}
-
-void Window::set_fullscreen(bool fullscreen)
-{
- if (m_fullscreen == fullscreen)
- return;
- m_fullscreen = fullscreen;
- Gfx::IntRect new_window_rect = m_rect;
- if (m_fullscreen) {
- m_saved_nonfullscreen_rect = m_rect;
- new_window_rect = Screen::the().rect();
- } else if (!m_saved_nonfullscreen_rect.is_empty()) {
- new_window_rect = m_saved_nonfullscreen_rect;
- }
-
- Core::EventLoop::current().post_event(*this, make<ResizeEvent>(new_window_rect));
- set_rect(new_window_rect);
-}
-
-Gfx::IntRect Window::tiled_rect(WindowTileType tiled) const
-{
- int frame_width = (m_frame.rect().width() - m_rect.width()) / 2;
- int title_bar_height = m_frame.title_bar_rect().height();
- int menu_height = WindowManager::the().maximized_window_rect(*this).y();
- int max_height = WindowManager::the().maximized_window_rect(*this).height();
-
- switch (tiled) {
- case WindowTileType::None:
- return m_untiled_rect;
- case WindowTileType::Left:
- return Gfx::IntRect(0,
- menu_height,
- Screen::the().width() / 2 - frame_width,
- max_height);
- case WindowTileType::Right:
- return Gfx::IntRect(Screen::the().width() / 2 + frame_width,
- menu_height,
- Screen::the().width() / 2 - frame_width,
- max_height);
- case WindowTileType::Top:
- return Gfx::IntRect(0,
- menu_height,
- Screen::the().width() - frame_width,
- (max_height - title_bar_height) / 2 - frame_width);
- case WindowTileType::Bottom:
- return Gfx::IntRect(0,
- menu_height + (title_bar_height + max_height) / 2 + frame_width,
- Screen::the().width() - frame_width,
- (max_height - title_bar_height) / 2 - frame_width);
- case WindowTileType::TopLeft:
- return Gfx::IntRect(0,
- menu_height,
- Screen::the().width() / 2 - frame_width,
- (max_height - title_bar_height) / 2 - frame_width);
- case WindowTileType::TopRight:
- return Gfx::IntRect(Screen::the().width() / 2 + frame_width,
- menu_height,
- Screen::the().width() / 2 - frame_width,
- (max_height - title_bar_height) / 2 - frame_width);
- case WindowTileType::BottomLeft:
- return Gfx::IntRect(0,
- menu_height + (title_bar_height + max_height) / 2 + frame_width,
- Screen::the().width() / 2 - frame_width,
- (max_height - title_bar_height) / 2);
- case WindowTileType::BottomRight:
- return Gfx::IntRect(Screen::the().width() / 2 + frame_width,
- menu_height + (title_bar_height + max_height) / 2 + frame_width,
- Screen::the().width() / 2 - frame_width,
- (max_height - title_bar_height) / 2);
- default:
- ASSERT_NOT_REACHED();
- }
-}
-
-void Window::set_tiled(WindowTileType tiled)
-{
- if (m_tiled == tiled)
- return;
-
- if (resize_aspect_ratio().has_value())
- return;
-
- m_tiled = tiled;
- if (tiled != WindowTileType::None)
- m_untiled_rect = m_rect;
- set_rect(tiled_rect(tiled));
- Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect));
-}
-
-void Window::detach_client(Badge<ClientConnection>)
-{
- m_client = nullptr;
-}
-
-void Window::recalculate_rect()
-{
- if (!is_resizable())
- return;
-
- bool send_event = true;
- if (m_tiled != WindowTileType::None) {
- set_rect(tiled_rect(m_tiled));
- } else if (is_maximized()) {
- set_rect(WindowManager::the().maximized_window_rect(*this));
- } else if (type() == WindowType::Desktop) {
- set_rect(WindowManager::the().desktop_rect());
- } else {
- send_event = false;
- }
-
- if (send_event) {
- Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect));
- }
-}
-
-void Window::add_child_window(Window& child_window)
-{
- m_child_windows.append(child_window);
-}
-
-void Window::add_accessory_window(Window& accessory_window)
-{
- m_accessory_windows.append(accessory_window);
-}
-
-void Window::set_parent_window(Window& parent_window)
-{
- ASSERT(!m_parent_window);
- m_parent_window = parent_window;
- if (m_accessory)
- parent_window.add_accessory_window(*this);
- else
- parent_window.add_child_window(*this);
-}
-
-bool Window::is_accessory() const
-{
- if (!m_accessory)
- return false;
- if (parent_window() != nullptr)
- return true;
-
- // If accessory window was unparented, convert to a regular window
- const_cast<Window*>(this)->set_accessory(false);
- return false;
-}
-
-bool Window::is_accessory_of(Window& window) const
-{
- if (!is_accessory())
- return false;
- return parent_window() == &window;
-}
-
-void Window::modal_unparented()
-{
- m_modal = false;
- WindowManager::the().notify_modal_unparented(*this);
-}
-
-bool Window::is_modal() const
-{
- if (!m_modal)
- return false;
- if (!m_parent_window) {
- const_cast<Window*>(this)->modal_unparented();
- return false;
- }
- return true;
-}
-
-void Window::set_progress(int progress)
-{
- if (m_progress == progress)
- return;
-
- m_progress = progress;
- WindowManager::the().notify_progress_changed(*this);
-}
-
-bool Window::is_descendant_of(Window& window) const
-{
- for (auto* parent = parent_window(); parent; parent = parent->parent_window()) {
- if (parent == &window)
- return true;
- for (auto& accessory : parent->accessory_windows()) {
- if (accessory == &window)
- return true;
- }
- }
- return false;
-}
-
-}
diff --git a/Services/WindowServer/Window.h b/Services/WindowServer/Window.h
deleted file mode 100644
index 286d543cc4..0000000000
--- a/Services/WindowServer/Window.h
+++ /dev/null
@@ -1,363 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/InlineLinkedList.h>
-#include <AK/String.h>
-#include <AK/WeakPtr.h>
-#include <LibCore/Object.h>
-#include <LibGfx/Bitmap.h>
-#include <LibGfx/DisjointRectSet.h>
-#include <LibGfx/Rect.h>
-#include <WindowServer/WindowFrame.h>
-#include <WindowServer/WindowType.h>
-
-namespace WindowServer {
-
-class ClientConnection;
-class Cursor;
-class Menu;
-class MenuItem;
-class MouseEvent;
-
-enum WMEventMask {
- WindowRectChanges = 1 << 0,
- WindowStateChanges = 1 << 1,
- WindowIconChanges = 1 << 2,
- WindowRemovals = 1 << 3,
-};
-
-enum class WindowTileType {
- None = 0,
- Left,
- Right,
- Top,
- Bottom,
- TopLeft,
- TopRight,
- BottomLeft,
- BottomRight
-};
-
-enum class PopupMenuItem {
- Minimize = 0,
- Maximize,
-};
-
-enum class WindowMenuDefaultAction {
- None = 0,
- BasedOnWindowState,
- Close,
- Minimize,
- Unminimize,
- Maximize,
- Restore
-};
-
-class Window final : public Core::Object
- , public InlineLinkedListNode<Window> {
- C_OBJECT(Window)
-public:
- Window(ClientConnection&, WindowType, int window_id, bool modal, bool minimizable, bool frameless, bool resizable, bool fullscreen, bool accessory, Window* parent_window = nullptr);
- Window(Core::Object&, WindowType);
- virtual ~Window() override;
-
- void popup_window_menu(const Gfx::IntPoint&, WindowMenuDefaultAction);
- void window_menu_activate_default();
- void request_close();
-
- unsigned wm_event_mask() const { return m_wm_event_mask; }
- void set_wm_event_mask(unsigned mask) { m_wm_event_mask = mask; }
-
- bool is_minimized() const { return m_minimized; }
- void set_minimized(bool);
-
- bool is_minimizable() const { return m_minimizable; }
- void set_minimizable(bool);
-
- bool is_resizable() const { return m_resizable && !m_fullscreen; }
- void set_resizable(bool);
-
- bool is_maximized() const { return m_maximized; }
- void set_maximized(bool);
-
- bool is_fullscreen() const { return m_fullscreen; }
- void set_fullscreen(bool);
-
- WindowTileType tiled() const { return m_tiled; }
- void set_tiled(WindowTileType);
-
- bool is_occluded() const { return m_occluded; }
- void set_occluded(bool);
-
- bool is_movable() const
- {
- return m_type == WindowType::Normal;
- }
-
- WindowFrame& frame() { return m_frame; }
- const WindowFrame& frame() const { return m_frame; }
-
- Window* blocking_modal_window();
-
- bool listens_to_wm_events() const { return m_listens_to_wm_events; }
-
- ClientConnection* client() { return m_client; }
- const ClientConnection* client() const { return m_client; }
-
- WindowType type() const { return m_type; }
- int window_id() const { return m_window_id; }
-
- bool is_internal() const { return m_client_id == -1; }
- i32 client_id() const { return m_client_id; }
-
- String title() const { return m_title; }
- void set_title(const String&);
-
- float opacity() const { return m_opacity; }
- void set_opacity(float);
-
- int x() const { return m_rect.x(); }
- int y() const { return m_rect.y(); }
- int width() const { return m_rect.width(); }
- int height() const { return m_rect.height(); }
-
- bool is_active() const;
-
- bool is_visible() const { return m_visible; }
- void set_visible(bool);
-
- bool is_modal() const;
- bool is_modal_dont_unparent() const { return m_modal && m_parent_window; }
-
- Gfx::IntRect rect() const { return m_rect; }
- void set_rect(const Gfx::IntRect&);
- void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
- void set_rect_without_repaint(const Gfx::IntRect&);
-
- void set_taskbar_rect(const Gfx::IntRect&);
- const Gfx::IntRect& taskbar_rect() const { return m_taskbar_rect; }
-
- void move_to(const Gfx::IntPoint& position) { set_rect({ position, size() }); }
- void move_to(int x, int y) { move_to({ x, y }); }
-
- void move_by(const Gfx::IntPoint& delta) { set_position_without_repaint(position().translated(delta)); }
-
- Gfx::IntPoint position() const { return m_rect.location(); }
- void set_position(const Gfx::IntPoint& position) { set_rect({ position.x(), position.y(), width(), height() }); }
- void set_position_without_repaint(const Gfx::IntPoint& position) { set_rect_without_repaint({ position.x(), position.y(), width(), height() }); }
-
- Gfx::IntSize size() const { return m_rect.size(); }
-
- void invalidate(bool with_frame = true);
- void invalidate(const Gfx::IntRect&, bool with_frame = false);
- bool invalidate_no_notify(const Gfx::IntRect& rect, bool with_frame = false);
-
- void prepare_dirty_rects();
- void clear_dirty_rects();
- Gfx::DisjointRectSet& dirty_rects() { return m_dirty_rects; }
-
- virtual void event(Core::Event&) override;
-
- // Only used by WindowType::MenuApplet. Perhaps it could be a Window subclass? I don't know.
- void set_rect_in_menubar(const Gfx::IntRect& rect) { m_rect_in_menubar = rect; }
- const Gfx::IntRect& rect_in_menubar() const { return m_rect_in_menubar; }
-
- const Gfx::Bitmap* backing_store() const { return m_backing_store.ptr(); }
- Gfx::Bitmap* backing_store() { return m_backing_store.ptr(); }
-
- void set_backing_store(RefPtr<Gfx::Bitmap>&& backing_store)
- {
- m_last_backing_store = move(m_backing_store);
- m_backing_store = move(backing_store);
- }
-
- void swap_backing_stores()
- {
- swap(m_backing_store, m_last_backing_store);
- }
-
- Gfx::Bitmap* last_backing_store() { return m_last_backing_store.ptr(); }
-
- void set_global_cursor_tracking_enabled(bool);
- void set_automatic_cursor_tracking_enabled(bool enabled) { m_automatic_cursor_tracking_enabled = enabled; }
- bool global_cursor_tracking() const { return m_global_cursor_tracking_enabled || m_automatic_cursor_tracking_enabled; }
-
- bool has_alpha_channel() const { return m_has_alpha_channel; }
- void set_has_alpha_channel(bool value) { m_has_alpha_channel = value; }
-
- Gfx::IntSize size_increment() const { return m_size_increment; }
- void set_size_increment(const Gfx::IntSize& increment) { m_size_increment = increment; }
-
- const Optional<Gfx::IntSize>& resize_aspect_ratio() const { return m_resize_aspect_ratio; }
- void set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio) { m_resize_aspect_ratio = ratio; }
-
- Gfx::IntSize base_size() const { return m_base_size; }
- void set_base_size(const Gfx::IntSize& size) { m_base_size = size; }
-
- const Gfx::Bitmap& icon() const { return *m_icon; }
- void set_icon(NonnullRefPtr<Gfx::Bitmap>&& icon) { m_icon = move(icon); }
-
- void set_default_icon();
-
- const Cursor* cursor() const { return m_cursor.ptr(); }
- void set_cursor(RefPtr<Cursor> cursor) { m_cursor = move(cursor); }
-
- void request_update(const Gfx::IntRect&, bool ignore_occlusion = false);
- Gfx::DisjointRectSet take_pending_paint_rects() { return move(m_pending_paint_rects); }
-
- bool has_taskbar_rect() const { return m_have_taskbar_rect; };
- bool in_minimize_animation() const { return m_minimize_animation_step != -1; }
- int minimize_animation_index() const { return m_minimize_animation_step; }
- void step_minimize_animation() { m_minimize_animation_step += 1; }
- void start_minimize_animation();
- void end_minimize_animation() { m_minimize_animation_step = -1; }
-
- Gfx::IntRect tiled_rect(WindowTileType) const;
- void recalculate_rect();
-
- // For InlineLinkedList.
- // FIXME: Maybe make a ListHashSet and then WindowManager can just use that.
- Window* m_next { nullptr };
- Window* m_prev { nullptr };
-
- void detach_client(Badge<ClientConnection>);
-
- Window* parent_window() { return m_parent_window; }
- const Window* parent_window() const { return m_parent_window; }
-
- void set_parent_window(Window&);
-
- Vector<WeakPtr<Window>>& child_windows() { return m_child_windows; }
- const Vector<WeakPtr<Window>>& child_windows() const { return m_child_windows; }
-
- Vector<WeakPtr<Window>>& accessory_windows() { return m_accessory_windows; }
- const Vector<WeakPtr<Window>>& accessory_windows() const { return m_accessory_windows; }
-
- bool is_descendant_of(Window&) const;
-
- void set_accessory(bool accessory) { m_accessory = accessory; }
- bool is_accessory() const;
- bool is_accessory_of(Window&) const;
-
- void set_frameless(bool frameless) { m_frameless = frameless; }
- bool is_frameless() const { return m_frameless; }
-
- int progress() const { return m_progress; }
- void set_progress(int);
-
- bool is_destroyed() const { return m_destroyed; }
- void destroy();
-
- bool default_positioned() const { return m_default_positioned; }
- void set_default_positioned(bool p) { m_default_positioned = p; }
-
- bool is_invalidated() const { return m_invalidated; }
-
- bool is_opaque() const
- {
- if (opacity() < 1.0f)
- return false;
- if (has_alpha_channel())
- return false;
- return true;
- }
-
- Gfx::DisjointRectSet& opaque_rects() { return m_opaque_rects; }
- Gfx::DisjointRectSet& transparency_rects() { return m_transparency_rects; }
- Gfx::DisjointRectSet& transparency_wallpaper_rects() { return m_transparency_wallpaper_rects; }
-
-private:
- void handle_mouse_event(const MouseEvent&);
- void update_menu_item_text(PopupMenuItem item);
- void update_menu_item_enabled(PopupMenuItem item);
- void add_child_window(Window&);
- void add_accessory_window(Window&);
- void ensure_window_menu();
- void modal_unparented();
-
- ClientConnection* m_client { nullptr };
-
- WeakPtr<Window> m_parent_window;
- Vector<WeakPtr<Window>> m_child_windows;
- Vector<WeakPtr<Window>> m_accessory_windows;
-
- String m_title;
- Gfx::IntRect m_rect;
- Gfx::IntRect m_saved_nonfullscreen_rect;
- Gfx::IntRect m_taskbar_rect;
- Gfx::DisjointRectSet m_dirty_rects;
- Gfx::DisjointRectSet m_opaque_rects;
- Gfx::DisjointRectSet m_transparency_rects;
- Gfx::DisjointRectSet m_transparency_wallpaper_rects;
- WindowType m_type { WindowType::Normal };
- bool m_global_cursor_tracking_enabled { false };
- bool m_automatic_cursor_tracking_enabled { false };
- bool m_visible { true };
- bool m_has_alpha_channel { false };
- bool m_modal { false };
- bool m_minimizable { false };
- bool m_frameless { false };
- bool m_resizable { false };
- Optional<Gfx::IntSize> m_resize_aspect_ratio {};
- bool m_listens_to_wm_events { false };
- bool m_minimized { false };
- bool m_maximized { false };
- bool m_fullscreen { false };
- bool m_accessory { false };
- bool m_destroyed { false };
- bool m_default_positioned { false };
- bool m_have_taskbar_rect { false };
- bool m_invalidated { true };
- bool m_invalidated_all { true };
- bool m_invalidated_frame { true };
- WindowTileType m_tiled { WindowTileType::None };
- Gfx::IntRect m_untiled_rect;
- bool m_occluded { false };
- RefPtr<Gfx::Bitmap> m_backing_store;
- RefPtr<Gfx::Bitmap> m_last_backing_store;
- int m_window_id { -1 };
- i32 m_client_id { -1 };
- float m_opacity { 1 };
- Gfx::IntSize m_size_increment;
- Gfx::IntSize m_base_size;
- NonnullRefPtr<Gfx::Bitmap> m_icon;
- RefPtr<Cursor> m_cursor;
- WindowFrame m_frame;
- unsigned m_wm_event_mask { 0 };
- Gfx::DisjointRectSet m_pending_paint_rects;
- Gfx::IntRect m_unmaximized_rect;
- Gfx::IntRect m_rect_in_menubar;
- RefPtr<Menu> m_window_menu;
- MenuItem* m_window_menu_minimize_item { nullptr };
- MenuItem* m_window_menu_maximize_item { nullptr };
- MenuItem* m_window_menu_close_item { nullptr };
- int m_minimize_animation_step { -1 };
- int m_progress { -1 };
-};
-
-}
diff --git a/Services/WindowServer/WindowClient.ipc b/Services/WindowServer/WindowClient.ipc
deleted file mode 100644
index c6ac17b022..0000000000
--- a/Services/WindowServer/WindowClient.ipc
+++ /dev/null
@@ -1,42 +0,0 @@
-endpoint WindowClient = 4
-{
- Paint(i32 window_id, Gfx::IntSize window_size, Vector<Gfx::IntRect> rects) =|
- MouseMove(i32 window_id, Gfx::IntPoint mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta, bool is_drag, Vector<String> mime_types) =|
- MouseDown(i32 window_id, Gfx::IntPoint mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta) =|
- MouseDoubleClick(i32 window_id, Gfx::IntPoint mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta) =|
- MouseUp(i32 window_id, Gfx::IntPoint mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta) =|
- MouseWheel(i32 window_id, Gfx::IntPoint mouse_position, u32 button, u32 buttons, u32 modifiers, i32 wheel_delta) =|
- WindowEntered(i32 window_id) =|
- WindowLeft(i32 window_id) =|
- WindowInputEntered(i32 window_id) =|
- WindowInputLeft(i32 window_id) =|
- KeyDown(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode) =|
- KeyUp(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode) =|
- WindowActivated(i32 window_id) =|
- WindowDeactivated(i32 window_id) =|
- WindowStateChanged(i32 window_id, bool minimized, bool occluded) =|
- WindowCloseRequest(i32 window_id) =|
- WindowResized(i32 window_id, Gfx::IntRect new_rect) =|
-
- MenuItemActivated(i32 menu_id, i32 identifier) =|
-
- ScreenRectChanged(Gfx::IntRect rect) =|
-
- WM_WindowRemoved(i32 wm_id, i32 client_id, i32 window_id) =|
- WM_WindowStateChanged(i32 wm_id, i32 client_id, i32 window_id, i32 parent_client_id, i32 parent_window_id, bool is_active, bool is_minimized, bool is_modal, bool is_frameless, i32 window_type, [UTF8] String title, Gfx::IntRect rect, i32 progress) =|
- WM_WindowIconBitmapChanged(i32 wm_id, i32 client_id, i32 window_id, i32 icon_buffer_id, Gfx::IntSize icon_size) =|
- WM_WindowRectChanged(i32 wm_id, i32 client_id, i32 window_id, Gfx::IntRect rect) =|
-
- AsyncSetWallpaperFinished(bool success) =|
-
- DragAccepted() =|
- DragCancelled() =|
-
- DragDropped(i32 window_id, Gfx::IntPoint mouse_position, [UTF8] String text, HashMap<String,ByteBuffer> mime_data) =|
-
- UpdateSystemTheme(i32 system_theme_buffer_id) =|
-
- DisplayLinkNotification() =|
-
- Ping() =|
-}
diff --git a/Services/WindowServer/WindowFrame.cpp b/Services/WindowServer/WindowFrame.cpp
deleted file mode 100644
index fee0e2a939..0000000000
--- a/Services/WindowServer/WindowFrame.cpp
+++ /dev/null
@@ -1,382 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "ClientConnection.h"
-#include <AK/Badge.h>
-#include <LibGfx/Font.h>
-#include <LibGfx/Painter.h>
-#include <LibGfx/StylePainter.h>
-#include <LibGfx/WindowTheme.h>
-#include <WindowServer/Button.h>
-#include <WindowServer/Compositor.h>
-#include <WindowServer/Event.h>
-#include <WindowServer/Window.h>
-#include <WindowServer/WindowFrame.h>
-#include <WindowServer/WindowManager.h>
-
-namespace WindowServer {
-
-static Gfx::WindowTheme::WindowType to_theme_window_type(WindowType type)
-{
- switch (type) {
- case WindowType::Normal:
- return Gfx::WindowTheme::WindowType::Normal;
- case WindowType::Notification:
- return Gfx::WindowTheme::WindowType::Notification;
- default:
- return Gfx::WindowTheme::WindowType::Other;
- }
-}
-
-static Gfx::Bitmap* s_minimize_icon;
-static Gfx::Bitmap* s_maximize_icon;
-static Gfx::Bitmap* s_restore_icon;
-static Gfx::Bitmap* s_close_icon;
-
-static String s_last_title_button_icons_path;
-
-WindowFrame::WindowFrame(Window& window)
- : m_window(window)
-{
- auto button = make<Button>(*this, [this](auto&) {
- m_window.request_close();
- });
- m_close_button = button.ptr();
- m_buttons.append(move(button));
-
- if (window.is_resizable()) {
- auto button = make<Button>(*this, [this](auto&) {
- WindowManager::the().maximize_windows(m_window, !m_window.is_maximized());
- });
- m_maximize_button = button.ptr();
- m_buttons.append(move(button));
- }
-
- if (window.is_minimizable()) {
- auto button = make<Button>(*this, [this](auto&) {
- WindowManager::the().minimize_windows(m_window, true);
- });
- m_minimize_button = button.ptr();
- m_buttons.append(move(button));
- }
-
- set_button_icons();
-}
-
-WindowFrame::~WindowFrame()
-{
-}
-
-void WindowFrame::set_button_icons()
-{
- if (m_window.is_frameless())
- return;
-
- String icons_path = WindowManager::the().palette().title_button_icons_path();
-
- StringBuilder full_path;
- if (!s_minimize_icon || s_last_title_button_icons_path != icons_path) {
- full_path.append(icons_path);
- full_path.append("window-minimize.png");
- if (!(s_minimize_icon = Gfx::Bitmap::load_from_file(full_path.to_string()).leak_ref()))
- s_minimize_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png").leak_ref();
- full_path.clear();
- }
- if (!s_maximize_icon || s_last_title_button_icons_path != icons_path) {
- full_path.append(icons_path);
- full_path.append("window-maximize.png");
- if (!(s_maximize_icon = Gfx::Bitmap::load_from_file(full_path.to_string()).leak_ref()))
- s_maximize_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png").leak_ref();
- full_path.clear();
- }
- if (!s_restore_icon || s_last_title_button_icons_path != icons_path) {
- full_path.append(icons_path);
- full_path.append("window-restore.png");
- if (!(s_restore_icon = Gfx::Bitmap::load_from_file(full_path.to_string()).leak_ref()))
- s_restore_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-restore.png").leak_ref();
- full_path.clear();
- }
- if (!s_close_icon || s_last_title_button_icons_path != icons_path) {
- full_path.append(icons_path);
- full_path.append("window-close.png");
- if (!(s_close_icon = Gfx::Bitmap::load_from_file(full_path.to_string()).leak_ref()))
- s_close_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png").leak_ref();
- full_path.clear();
- }
-
- m_close_button->set_icon(*s_close_icon);
- if (m_window.is_minimizable())
- m_minimize_button->set_icon(*s_minimize_icon);
- if (m_window.is_resizable())
- m_maximize_button->set_icon(m_window.is_maximized() ? *s_restore_icon : *s_maximize_icon);
-
- s_last_title_button_icons_path = icons_path;
-}
-
-void WindowFrame::did_set_maximized(Badge<Window>, bool maximized)
-{
- ASSERT(m_maximize_button);
- m_maximize_button->set_icon(maximized ? *s_restore_icon : *s_maximize_icon);
-}
-
-Gfx::IntRect WindowFrame::title_bar_rect() const
-{
- return Gfx::WindowTheme::current().title_bar_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
-}
-
-Gfx::IntRect WindowFrame::title_bar_icon_rect() const
-{
- return Gfx::WindowTheme::current().title_bar_icon_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
-}
-
-Gfx::IntRect WindowFrame::title_bar_text_rect() const
-{
- return Gfx::WindowTheme::current().title_bar_text_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
-}
-
-Gfx::WindowTheme::WindowState WindowFrame::window_state_for_theme() const
-{
- auto& wm = WindowManager::the();
-
- if (m_flash_timer && m_flash_timer->is_active())
- return m_flash_counter & 1 ? Gfx::WindowTheme::WindowState::Active : Gfx::WindowTheme::WindowState::Inactive;
-
- if (&m_window == wm.m_highlight_window)
- return Gfx::WindowTheme::WindowState::Highlighted;
- if (&m_window == wm.m_move_window)
- return Gfx::WindowTheme::WindowState::Moving;
- if (wm.is_active_window_or_accessory(m_window))
- return Gfx::WindowTheme::WindowState::Active;
- return Gfx::WindowTheme::WindowState::Inactive;
-}
-
-void WindowFrame::paint_notification_frame(Gfx::Painter& painter)
-{
- auto palette = WindowManager::the().palette();
- Gfx::WindowTheme::current().paint_notification_frame(painter, m_window.rect(), palette, m_buttons.last().relative_rect());
-}
-
-void WindowFrame::paint_normal_frame(Gfx::Painter& painter)
-{
- auto palette = WindowManager::the().palette();
- auto& window = m_window;
- String title_text;
- if (window.client() && window.client()->is_unresponsive()) {
- StringBuilder builder;
- builder.append(window.title());
- builder.append(" (Not responding)");
- title_text = builder.to_string();
- } else {
- title_text = window.title();
- }
-
- auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::IntRect() : m_buttons.last().relative_rect();
- Gfx::WindowTheme::current().paint_normal_frame(painter, window_state_for_theme(), m_window.rect(), title_text, m_window.icon(), palette, leftmost_button_rect);
-}
-
-void WindowFrame::paint(Gfx::Painter& painter)
-{
- if (m_window.is_frameless())
- return;
-
- Gfx::PainterStateSaver saver(painter);
- painter.translate(rect().location());
-
- if (m_window.type() == WindowType::Notification)
- paint_notification_frame(painter);
- else if (m_window.type() == WindowType::Normal)
- paint_normal_frame(painter);
- else
- return;
-
- for (auto& button : m_buttons) {
- button.paint(painter);
- }
-}
-
-static Gfx::IntRect frame_rect_for_window(Window& window, const Gfx::IntRect& rect)
-{
- if (window.is_frameless())
- return rect;
- return Gfx::WindowTheme::current().frame_rect_for_window(to_theme_window_type(window.type()), rect, WindowManager::the().palette());
-}
-
-static Gfx::IntRect frame_rect_for_window(Window& window)
-{
- return frame_rect_for_window(window, window.rect());
-}
-
-Gfx::IntRect WindowFrame::rect() const
-{
- return frame_rect_for_window(m_window);
-}
-
-void WindowFrame::invalidate_title_bar()
-{
- invalidate(title_bar_rect());
-}
-
-void WindowFrame::invalidate(Gfx::IntRect relative_rect)
-{
- auto frame_rect = rect();
- auto window_rect = m_window.rect();
- relative_rect.move_by(frame_rect.x() - window_rect.x(), frame_rect.y() - window_rect.y());
- m_window.invalidate(relative_rect, true);
-}
-
-void WindowFrame::notify_window_rect_changed(const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect)
-{
- layout_buttons();
-
- auto old_frame_rect = frame_rect_for_window(m_window, old_rect);
- auto& compositor = Compositor::the();
- for (auto& dirty : old_frame_rect.shatter(rect()))
- compositor.invalidate_screen(dirty);
- if (!m_window.is_opaque())
- compositor.invalidate_screen(rect());
-
- compositor.invalidate_occlusions();
-
- WindowManager::the().notify_rect_changed(m_window, old_rect, new_rect);
-}
-
-void WindowFrame::layout_buttons()
-{
- auto button_rects = Gfx::WindowTheme::current().layout_buttons(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette(), m_buttons.size());
- for (size_t i = 0; i < m_buttons.size(); i++)
- m_buttons[i].set_relative_rect(button_rects[i]);
-}
-
-void WindowFrame::on_mouse_event(const MouseEvent& event)
-{
- ASSERT(!m_window.is_fullscreen());
-
- auto& wm = WindowManager::the();
- if (m_window.type() != WindowType::Normal && m_window.type() != WindowType::Notification)
- return;
-
- if (m_window.type() == WindowType::Normal) {
- if (event.type() == Event::MouseDown)
- wm.move_to_front_and_make_active(m_window);
-
- if (m_window.blocking_modal_window())
- return;
-
- if (title_bar_icon_rect().contains(event.position())) {
- if (event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right)) {
- // Manually start a potential double click. Since we're opening
- // a menu, we will only receive the MouseDown event, so we
- // need to record that fact. If the user subsequently clicks
- // on the same area, the menu will get closed, and we will
- // receive a MouseUp event, but because windows have changed
- // we don't get a MouseDoubleClick event. We can however record
- // this click, and when we receive the MouseUp event check if
- // it would have been considered a double click, if it weren't
- // for the fact that we opened and closed a window in the meanwhile
- auto& wm = WindowManager::the();
- wm.start_menu_doubleclick(m_window, event);
-
- m_window.popup_window_menu(title_bar_rect().bottom_left().translated(rect().location()), WindowMenuDefaultAction::Close);
- return;
- } else if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
- // Since the MouseDown event opened a menu, another MouseUp
- // from the second click outside the menu wouldn't be considered
- // a double click, so let's manually check if it would otherwise
- // have been be considered to be one
- auto& wm = WindowManager::the();
- if (wm.is_menu_doubleclick(m_window, event)) {
- // It is a double click, so perform activate the default item
- m_window.window_menu_activate_default();
- }
- return;
- }
- }
- }
-
- // This is slightly hackish, but expand the title bar rect by two pixels downwards,
- // so that mouse events between the title bar and window contents don't act like
- // mouse events on the border.
- auto adjusted_title_bar_rect = title_bar_rect();
- adjusted_title_bar_rect.set_height(adjusted_title_bar_rect.height() + 2);
-
- if (adjusted_title_bar_rect.contains(event.position())) {
- wm.clear_resize_candidate();
-
- if (event.type() == Event::MouseDown)
- wm.move_to_front_and_make_active(m_window);
-
- for (auto& button : m_buttons) {
- if (button.relative_rect().contains(event.position()))
- return button.on_mouse_event(event.translated(-button.relative_rect().location()));
- }
- if (event.type() == Event::MouseDown) {
- if (m_window.type() == WindowType::Normal && event.button() == MouseButton::Right) {
- auto default_action = m_window.is_maximized() ? WindowMenuDefaultAction::Restore : WindowMenuDefaultAction::Maximize;
- m_window.popup_window_menu(event.position().translated(rect().location()), default_action);
- return;
- }
- if (m_window.is_movable() && event.button() == MouseButton::Left)
- wm.start_window_move(m_window, event.translated(rect().location()));
- }
- return;
- }
-
- if (m_window.is_resizable() && event.type() == Event::MouseMove && event.buttons() == 0) {
- constexpr ResizeDirection direction_for_hot_area[3][3] = {
- { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
- { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
- { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
- };
- Gfx::IntRect outer_rect = { {}, rect().size() };
- ASSERT(outer_rect.contains(event.position()));
- int window_relative_x = event.x() - outer_rect.x();
- int window_relative_y = event.y() - outer_rect.y();
- int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
- int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
- wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]);
- Compositor::the().invalidate_cursor();
- return;
- }
-
- if (m_window.is_resizable() && event.type() == Event::MouseDown && event.button() == MouseButton::Left)
- wm.start_window_resize(m_window, event.translated(rect().location()));
-}
-
-void WindowFrame::start_flash_animation()
-{
- if (!m_flash_timer) {
- m_flash_timer = Core::Timer::construct(100, [this] {
- ASSERT(m_flash_counter);
- invalidate_title_bar();
- if (!--m_flash_counter)
- m_flash_timer->stop();
- });
- }
- m_flash_counter = 8;
- m_flash_timer->start();
-}
-
-}
diff --git a/Services/WindowServer/WindowFrame.h b/Services/WindowServer/WindowFrame.h
deleted file mode 100644
index ace32b8c7c..0000000000
--- a/Services/WindowServer/WindowFrame.h
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/Forward.h>
-#include <AK/NonnullOwnPtrVector.h>
-#include <AK/RefPtr.h>
-#include <LibCore/Forward.h>
-#include <LibGfx/Forward.h>
-#include <LibGfx/WindowTheme.h>
-
-namespace WindowServer {
-
-class Button;
-class MouseEvent;
-class Window;
-
-class WindowFrame {
-public:
- WindowFrame(Window&);
- ~WindowFrame();
-
- Gfx::IntRect rect() const;
- void paint(Gfx::Painter&);
- void on_mouse_event(const MouseEvent&);
- void notify_window_rect_changed(const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect);
- void invalidate_title_bar();
- void invalidate(Gfx::IntRect relative_rect);
-
- Gfx::IntRect title_bar_rect() const;
- Gfx::IntRect title_bar_icon_rect() const;
- Gfx::IntRect title_bar_text_rect() const;
-
- void did_set_maximized(Badge<Window>, bool);
-
- void layout_buttons();
- void set_button_icons();
-
- void start_flash_animation();
-
-private:
- void paint_notification_frame(Gfx::Painter&);
- void paint_normal_frame(Gfx::Painter&);
-
- Gfx::WindowTheme::WindowState window_state_for_theme() const;
-
- Window& m_window;
- NonnullOwnPtrVector<Button> m_buttons;
- Button* m_close_button { nullptr };
- Button* m_maximize_button { nullptr };
- Button* m_minimize_button { nullptr };
-
- RefPtr<Core::Timer> m_flash_timer;
- size_t m_flash_counter { 0 };
-};
-
-}
diff --git a/Services/WindowServer/WindowManager.cpp b/Services/WindowServer/WindowManager.cpp
deleted file mode 100644
index aa072fb0b6..0000000000
--- a/Services/WindowServer/WindowManager.cpp
+++ /dev/null
@@ -1,1509 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "WindowManager.h"
-#include "Compositor.h"
-#include "EventLoop.h"
-#include "Menu.h"
-#include "MenuBar.h"
-#include "MenuItem.h"
-#include "Screen.h"
-#include "Window.h"
-#include <AK/LogStream.h>
-#include <AK/SharedBuffer.h>
-#include <AK/StdLibExtras.h>
-#include <AK/Vector.h>
-#include <LibGfx/CharacterBitmap.h>
-#include <LibGfx/Font.h>
-#include <LibGfx/Painter.h>
-#include <LibGfx/StylePainter.h>
-#include <LibGfx/SystemTheme.h>
-#include <WindowServer/AppletManager.h>
-#include <WindowServer/Button.h>
-#include <WindowServer/ClientConnection.h>
-#include <WindowServer/Cursor.h>
-#include <WindowServer/WindowClientEndpoint.h>
-#include <errno.h>
-#include <serenity.h>
-#include <stdio.h>
-#include <time.h>
-#include <unistd.h>
-
-//#define WINDOWMANAGER_DEBUG
-//#define RESIZE_DEBUG
-//#define MOVE_DEBUG
-//#define DOUBLECLICK_DEBUG
-
-namespace WindowServer {
-
-static WindowManager* s_the;
-
-WindowManager& WindowManager::the()
-{
- ASSERT(s_the);
- return *s_the;
-}
-
-WindowManager::WindowManager(const Gfx::PaletteImpl& palette)
- : m_palette(palette)
-{
- s_the = this;
-
- reload_config(false);
-
- Compositor::the().did_construct_window_manager({});
-}
-
-WindowManager::~WindowManager()
-{
-}
-
-NonnullRefPtr<Cursor> WindowManager::get_cursor(const String& name)
-{
- static const auto s_default_cursor_path = "/res/cursors/arrow.x2y2.png";
- auto path = m_config->read_entry("Cursor", name, s_default_cursor_path);
- auto gb = Gfx::Bitmap::load_from_file(path);
- if (gb)
- return Cursor::create(*gb, path);
- return Cursor::create(*Gfx::Bitmap::load_from_file(s_default_cursor_path), s_default_cursor_path);
-}
-
-void WindowManager::reload_config(bool set_screen)
-{
- m_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
-
- m_double_click_speed = m_config->read_num_entry("Input", "DoubleClickSpeed", 250);
-
- if (set_screen) {
- set_resolution(m_config->read_num_entry("Screen", "Width", 1920), m_config->read_num_entry("Screen", "Height", 1080));
- }
-
- m_hidden_cursor = get_cursor("Hidden");
- m_arrow_cursor = get_cursor("Arrow");
- m_hand_cursor = get_cursor("Hand");
- m_help_cursor = get_cursor("Help");
- m_resize_horizontally_cursor = get_cursor("ResizeH");
- m_resize_vertically_cursor = get_cursor("ResizeV");
- m_resize_diagonally_tlbr_cursor = get_cursor("ResizeDTLBR");
- m_resize_diagonally_bltr_cursor = get_cursor("ResizeDBLTR");
- m_resize_column_cursor = get_cursor("ResizeColumn");
- m_resize_row_cursor = get_cursor("ResizeRow");
- m_i_beam_cursor = get_cursor("IBeam");
- m_disallowed_cursor = get_cursor("Disallowed");
- m_move_cursor = get_cursor("Move");
- m_drag_cursor = get_cursor("Drag");
- m_wait_cursor = get_cursor("Wait");
- m_crosshair_cursor = get_cursor("Crosshair");
-}
-
-const Gfx::Font& WindowManager::font() const
-{
- return Gfx::FontDatabase::default_font();
-}
-
-const Gfx::Font& WindowManager::window_title_font() const
-{
- return Gfx::FontDatabase::default_bold_font();
-}
-
-bool WindowManager::set_resolution(int width, int height)
-{
- bool success = Compositor::the().set_resolution(width, height);
- MenuManager::the().set_needs_window_resize();
- ClientConnection::for_each_client([&](ClientConnection& client) {
- client.notify_about_new_screen_rect(Screen::the().rect());
- });
- if (success) {
- for_each_window([](Window& window) {
- window.recalculate_rect();
- return IterationDecision::Continue;
- });
- }
- if (m_config) {
- if (success) {
- dbg() << "Saving resolution: " << Gfx::IntSize(width, height) << " to config file at " << m_config->file_name();
- m_config->write_num_entry("Screen", "Width", width);
- m_config->write_num_entry("Screen", "Height", height);
- m_config->sync();
- } else {
- dbg() << "Saving fallback resolution: " << resolution() << " to config file at " << m_config->file_name();
- m_config->write_num_entry("Screen", "Width", resolution().width());
- m_config->write_num_entry("Screen", "Height", resolution().height());
- m_config->sync();
- }
- }
- return success;
-}
-
-Gfx::IntSize WindowManager::resolution() const
-{
- return Screen::the().size();
-}
-
-void WindowManager::set_acceleration_factor(double factor)
-{
- Screen::the().set_acceleration_factor(factor);
- dbgln("Saving acceleration factor {} to config file at {}", factor, m_config->file_name());
- m_config->write_entry("Mouse", "AccelerationFactor", String::formatted("{}", factor));
- m_config->sync();
-}
-
-void WindowManager::set_scroll_step_size(unsigned step_size)
-{
- Screen::the().set_scroll_step_size(step_size);
- dbgln("Saving scroll step size {} to config file at {}", step_size, m_config->file_name());
- m_config->write_entry("Mouse", "ScrollStepSize", String::number(step_size));
- m_config->sync();
-}
-
-void WindowManager::add_window(Window& window)
-{
- bool is_first_window = m_windows_in_order.is_empty();
-
- m_windows_in_order.append(&window);
-
- if (window.is_fullscreen()) {
- Core::EventLoop::current().post_event(window, make<ResizeEvent>(Screen::the().rect()));
- window.set_rect(Screen::the().rect());
- }
-
- if (window.type() != WindowType::Desktop || is_first_window)
- set_active_window(&window);
-
- if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
- m_switcher.refresh();
-
- Compositor::the().invalidate_occlusions();
-
- if (window.listens_to_wm_events()) {
- for_each_window([&](Window& other_window) {
- if (&window != &other_window) {
- tell_wm_listener_about_window(window, other_window);
- tell_wm_listener_about_window_icon(window, other_window);
- }
- return IterationDecision::Continue;
- });
- }
-
- tell_wm_listeners_window_state_changed(window);
-}
-
-void WindowManager::move_to_front_and_make_active(Window& window)
-{
- auto move_window_to_front = [&](Window& wnd, bool make_active, bool make_input) {
- if (wnd.is_accessory()) {
- auto* parent = wnd.parent_window();
- do_move_to_front(*parent, true, false);
- make_active = false;
-
- for (auto& accessory_window : parent->accessory_windows()) {
- if (accessory_window && accessory_window.ptr() != &wnd)
- do_move_to_front(*accessory_window, false, false);
- }
- }
-
- do_move_to_front(wnd, make_active, make_input);
- };
-
- // If a window that is currently blocked by a modal child is being
- // brought to the front, bring the entire stack of modal windows
- // to the front and activate the modal window. Also set the
- // active input window to that same window (which would pull
- // active input from any accessory window)
- for_each_window_in_modal_stack(window, [&](auto& w, bool is_stack_top) {
- move_window_to_front(w, is_stack_top, is_stack_top);
- return IterationDecision::Continue;
- });
-
- Compositor::the().invalidate_occlusions();
-}
-
-void WindowManager::do_move_to_front(Window& window, bool make_active, bool make_input)
-{
- if (m_windows_in_order.tail() != &window)
- window.invalidate();
- m_windows_in_order.remove(&window);
- m_windows_in_order.append(&window);
-
- if (make_active)
- set_active_window(&window, make_input);
-
- if (m_switcher.is_visible()) {
- m_switcher.refresh();
- if (!window.is_accessory()) {
- m_switcher.select_window(window);
- set_highlight_window(&window);
- }
- }
-
- for (auto& child_window : window.child_windows()) {
- if (child_window)
- do_move_to_front(*child_window, make_active, make_input);
- }
-}
-
-void WindowManager::remove_window(Window& window)
-{
- m_windows_in_order.remove(&window);
- auto* active = active_window();
- auto* active_input = active_input_window();
- if (active == &window || active_input == &window || (active && window.is_descendant_of(*active)) || (active_input && active_input != active && window.is_descendant_of(*active_input)))
- pick_new_active_window(&window);
-
- Compositor::the().invalidate_screen(window.frame().rect());
-
- if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
- m_switcher.refresh();
-
- Compositor::the().invalidate_occlusions();
-
- for_each_window_listening_to_wm_events([&window](Window& listener) {
- if (!(listener.wm_event_mask() & WMEventMask::WindowRemovals))
- return IterationDecision::Continue;
- if (!window.is_internal() && !window.is_modal())
- listener.client()->post_message(Messages::WindowClient::WM_WindowRemoved(listener.window_id(), window.client_id(), window.window_id()));
- return IterationDecision::Continue;
- });
-}
-
-void WindowManager::tell_wm_listener_about_window(Window& listener, Window& window)
-{
- if (!(listener.wm_event_mask() & WMEventMask::WindowStateChanges))
- return;
- if (window.is_internal())
- return;
- auto* parent = window.parent_window();
- listener.client()->post_message(Messages::WindowClient::WM_WindowStateChanged(listener.window_id(), window.client_id(), window.window_id(), parent ? parent->client_id() : -1, parent ? parent->window_id() : -1, window.is_active(), window.is_minimized(), window.is_modal_dont_unparent(), window.is_frameless(), (i32)window.type(), window.title(), window.rect(), window.progress()));
-}
-
-void WindowManager::tell_wm_listener_about_window_rect(Window& listener, Window& window)
-{
- if (!(listener.wm_event_mask() & WMEventMask::WindowRectChanges))
- return;
- if (window.is_internal())
- return;
- listener.client()->post_message(Messages::WindowClient::WM_WindowRectChanged(listener.window_id(), window.client_id(), window.window_id(), window.rect()));
-}
-
-void WindowManager::tell_wm_listener_about_window_icon(Window& listener, Window& window)
-{
- if (!(listener.wm_event_mask() & WMEventMask::WindowIconChanges))
- return;
- if (window.is_internal())
- return;
- if (window.icon().shbuf_id() == -1)
- return;
-#ifdef WINDOWMANAGER_DEBUG
- dbg() << "WindowServer: Sharing icon buffer " << window.icon().shbuf_id() << " with PID " << listener.client()->client_pid();
-#endif
- if (shbuf_allow_pid(window.icon().shbuf_id(), listener.client()->client_pid()) < 0) {
- ASSERT_NOT_REACHED();
- }
- listener.client()->post_message(Messages::WindowClient::WM_WindowIconBitmapChanged(listener.window_id(), window.client_id(), window.window_id(), window.icon().shbuf_id(), window.icon().size()));
-}
-
-void WindowManager::tell_wm_listeners_window_state_changed(Window& window)
-{
- for_each_window_listening_to_wm_events([&](Window& listener) {
- tell_wm_listener_about_window(listener, window);
- return IterationDecision::Continue;
- });
-}
-
-void WindowManager::tell_wm_listeners_window_icon_changed(Window& window)
-{
- for_each_window_listening_to_wm_events([&](Window& listener) {
- tell_wm_listener_about_window_icon(listener, window);
- return IterationDecision::Continue;
- });
-}
-
-void WindowManager::tell_wm_listeners_window_rect_changed(Window& window)
-{
- for_each_window_listening_to_wm_events([&](Window& listener) {
- tell_wm_listener_about_window_rect(listener, window);
- return IterationDecision::Continue;
- });
-}
-
-void WindowManager::notify_title_changed(Window& window)
-{
- if (window.type() != WindowType::Normal)
- return;
-#ifdef WINDOWMANAGER_DEBUG
- dbg() << "[WM] Window{" << &window << "} title set to \"" << window.title() << '"';
-#endif
- if (m_switcher.is_visible())
- m_switcher.refresh();
-
- tell_wm_listeners_window_state_changed(window);
-}
-
-void WindowManager::notify_modal_unparented(Window& window)
-{
- if (window.type() != WindowType::Normal)
- return;
-#ifdef WINDOWMANAGER_DEBUG
- dbg() << "[WM] Modal Window{" << &window << "} was unparented";
-#endif
- if (m_switcher.is_visible())
- m_switcher.refresh();
-
- tell_wm_listeners_window_state_changed(window);
-}
-
-void WindowManager::notify_rect_changed(Window& window, [[maybe_unused]] const Gfx::IntRect& old_rect, [[maybe_unused]] const Gfx::IntRect& new_rect)
-{
-#ifdef RESIZE_DEBUG
- dbg() << "[WM] Window " << &window << " rect changed " << old_rect << " -> " << new_rect;
-#endif
- if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
- m_switcher.refresh();
-
- tell_wm_listeners_window_rect_changed(window);
-
- if (window.type() == WindowType::MenuApplet)
- AppletManager::the().calculate_applet_rects(MenuManager::the().window());
-
- MenuManager::the().refresh();
-}
-
-void WindowManager::notify_opacity_changed(Window&)
-{
- Compositor::the().invalidate_occlusions();
-}
-
-void WindowManager::notify_minimization_state_changed(Window& window)
-{
- tell_wm_listeners_window_state_changed(window);
-
- if (window.client())
- window.client()->post_message(Messages::WindowClient::WindowStateChanged(window.window_id(), window.is_minimized(), window.is_occluded()));
-
- if (window.is_active() && window.is_minimized())
- pick_new_active_window(&window);
-}
-
-void WindowManager::notify_occlusion_state_changed(Window& window)
-{
- if (window.client())
- window.client()->post_message(Messages::WindowClient::WindowStateChanged(window.window_id(), window.is_minimized(), window.is_occluded()));
-}
-
-void WindowManager::notify_progress_changed(Window& window)
-{
- tell_wm_listeners_window_state_changed(window);
-}
-
-bool WindowManager::pick_new_active_window(Window* previous_active)
-{
- bool new_window_picked = false;
- Window* first_candidate = nullptr;
- for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& candidate) {
- if (candidate.is_destroyed())
- return IterationDecision::Continue;
- if (previous_active != first_candidate)
- first_candidate = &candidate;
- if ((!previous_active && !candidate.is_accessory()) || (previous_active && !candidate.is_accessory_of(*previous_active))) {
- set_active_window(&candidate);
- new_window_picked = true;
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
- });
- if (!new_window_picked) {
- set_active_window(first_candidate);
- new_window_picked = first_candidate != nullptr;
- }
- return new_window_picked;
-}
-
-void WindowManager::start_window_move(Window& window, const MouseEvent& event)
-{
-#ifdef MOVE_DEBUG
- dbg() << "[WM] Begin moving Window{" << &window << "}";
-#endif
- move_to_front_and_make_active(window);
- m_move_window = window;
- m_move_window->set_default_positioned(false);
- m_move_origin = event.position();
- m_move_window_origin = window.position();
- window.invalidate();
-}
-
-void WindowManager::start_window_resize(Window& window, const Gfx::IntPoint& position, MouseButton button)
-{
- move_to_front_and_make_active(window);
- constexpr ResizeDirection direction_for_hot_area[3][3] = {
- { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
- { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
- { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
- };
- Gfx::IntRect outer_rect = window.frame().rect();
- if (!outer_rect.contains(position)) {
- // FIXME: This used to be an ASSERT but crashing WindowServer over this seems silly.
- dbg() << "FIXME: !outer_rect.contains(position): outer_rect=" << outer_rect << ", position=" << position;
- }
- int window_relative_x = position.x() - outer_rect.x();
- int window_relative_y = position.y() - outer_rect.y();
- int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
- int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
- m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
- if (m_resize_direction == ResizeDirection::None) {
- ASSERT(!m_resize_window);
- return;
- }
-
-#ifdef RESIZE_DEBUG
- dbg() << "[WM] Begin resizing Window{" << &window << "}";
-#endif
- m_resizing_mouse_button = button;
- m_resize_window = window;
- m_resize_origin = position;
- m_resize_window_original_rect = window.rect();
-
- m_active_input_tracking_window = nullptr;
-
- window.invalidate();
-
- if (hot_area_row == 0 || hot_area_column == 0) {
- m_resize_window->set_default_positioned(false);
- }
-}
-
-void WindowManager::start_window_resize(Window& window, const MouseEvent& event)
-{
- start_window_resize(window, event.position(), event.button());
-}
-
-bool WindowManager::process_ongoing_window_move(MouseEvent& event, Window*& hovered_window)
-{
- if (!m_move_window)
- return false;
- if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
-#ifdef MOVE_DEBUG
- dbg() << "[WM] Finish moving Window{" << m_move_window << "}";
-#endif
-
- m_move_window->invalidate();
- if (m_move_window->rect().contains(event.position()))
- hovered_window = m_move_window;
- if (m_move_window->is_resizable()) {
- process_event_for_doubleclick(*m_move_window, event);
- if (event.type() == Event::MouseDoubleClick) {
-#if defined(DOUBLECLICK_DEBUG)
- dbgln("[WM] Click up became doubleclick!");
-#endif
- m_move_window->set_maximized(!m_move_window->is_maximized());
- }
- }
- m_move_window = nullptr;
- return true;
- }
- if (event.type() == Event::MouseMove) {
-
-#ifdef MOVE_DEBUG
- dbg() << "[WM] Moving, origin: " << m_move_origin << ", now: " << event.position();
- if (m_move_window->is_maximized()) {
- dbgln(" [!] The window is still maximized. Not moving yet.");
- }
-
-#endif
-
- const int tiling_deadzone = 10;
- const int secondary_deadzone = 2;
-
- if (m_move_window->is_maximized()) {
- auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
- // dbg() << "[WM] " << pixels_moved_from_start << " moved since start of window move";
- if (pixels_moved_from_start > 5) {
- // dbgln("[WM] de-maximizing window");
- m_move_origin = event.position();
- if (m_move_origin.y() <= secondary_deadzone)
- return true;
- auto width_before_resize = m_move_window->width();
- m_move_window->set_maximized(false);
- m_move_window->move_to(m_move_origin.x() - (m_move_window->width() * ((float)m_move_origin.x() / width_before_resize)), m_move_origin.y());
- m_move_window_origin = m_move_window->position();
- }
- } else {
- bool is_resizable = m_move_window->is_resizable();
- auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
- auto desktop = desktop_rect();
-
- if (is_resizable && event.x() <= tiling_deadzone) {
- if (event.y() <= tiling_deadzone + desktop.top())
- m_move_window->set_tiled(WindowTileType::TopLeft);
- else if (event.y() >= desktop.height() - tiling_deadzone)
- m_move_window->set_tiled(WindowTileType::BottomLeft);
- else
- m_move_window->set_tiled(WindowTileType::Left);
- } else if (is_resizable && event.x() >= Screen::the().width() - tiling_deadzone) {
- if (event.y() <= tiling_deadzone + desktop.top())
- m_move_window->set_tiled(WindowTileType::TopRight);
- else if (event.y() >= desktop.height() - tiling_deadzone)
- m_move_window->set_tiled(WindowTileType::BottomRight);
- else
- m_move_window->set_tiled(WindowTileType::Right);
- } else if (is_resizable && event.y() <= secondary_deadzone + desktop.top()) {
- m_move_window->set_tiled(WindowTileType::Top);
- } else if (is_resizable && event.y() >= desktop.bottom() - secondary_deadzone) {
- m_move_window->set_tiled(WindowTileType::Bottom);
- } else if (pixels_moved_from_start > 5 || m_move_window->tiled() == WindowTileType::None) {
- m_move_window->set_tiled(WindowTileType::None);
- Gfx::IntPoint pos = m_move_window_origin.translated(event.position() - m_move_origin);
- m_move_window->set_position_without_repaint(pos);
- if (m_move_window->rect().contains(event.position()))
- hovered_window = m_move_window;
- }
- return true;
- }
- }
- return false;
-}
-
-bool WindowManager::process_ongoing_window_resize(const MouseEvent& event, Window*& hovered_window)
-{
- if (!m_resize_window)
- return false;
-
- if (event.type() == Event::MouseUp && event.button() == m_resizing_mouse_button) {
-#ifdef RESIZE_DEBUG
- dbg() << "[WM] Finish resizing Window{" << m_resize_window << "}";
-#endif
- Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(m_resize_window->rect()));
- m_resize_window->invalidate();
- if (m_resize_window->rect().contains(event.position()))
- hovered_window = m_resize_window;
- m_resize_window = nullptr;
- m_resizing_mouse_button = MouseButton::None;
- return true;
- }
-
- if (event.type() != Event::MouseMove)
- return false;
-
- int diff_x = event.x() - m_resize_origin.x();
- int diff_y = event.y() - m_resize_origin.y();
-
- int change_w = 0;
- int change_h = 0;
-
- switch (m_resize_direction) {
- case ResizeDirection::DownRight:
- change_w = diff_x;
- change_h = diff_y;
- break;
- case ResizeDirection::Right:
- change_w = diff_x;
- break;
- case ResizeDirection::UpRight:
- change_w = diff_x;
- change_h = -diff_y;
- break;
- case ResizeDirection::Up:
- change_h = -diff_y;
- break;
- case ResizeDirection::UpLeft:
- change_w = -diff_x;
- change_h = -diff_y;
- break;
- case ResizeDirection::Left:
- change_w = -diff_x;
- break;
- case ResizeDirection::DownLeft:
- change_w = -diff_x;
- change_h = diff_y;
- break;
- case ResizeDirection::Down:
- change_h = diff_y;
- break;
- default:
- ASSERT_NOT_REACHED();
- }
-
- auto new_rect = m_resize_window_original_rect;
-
- // First, size the new rect.
- Gfx::IntSize minimum_size { 50, 50 };
-
- new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
- new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
-
- if (!m_resize_window->size_increment().is_null()) {
- int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
- new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
- int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
- new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
- }
-
- if (m_resize_window->resize_aspect_ratio().has_value()) {
- auto& ratio = m_resize_window->resize_aspect_ratio().value();
- if (abs(change_w) > abs(change_h)) {
- new_rect.set_height(new_rect.width() * ratio.height() / ratio.width());
- } else {
- new_rect.set_width(new_rect.height() * ratio.width() / ratio.height());
- }
- }
-
- // Second, set its position so that the sides of the window
- // that end up moving are the same ones as the user is dragging,
- // no matter which part of the logic above caused us to decide
- // to resize by this much.
- switch (m_resize_direction) {
- case ResizeDirection::DownRight:
- case ResizeDirection::Right:
- case ResizeDirection::Down:
- break;
- case ResizeDirection::Left:
- case ResizeDirection::Up:
- case ResizeDirection::UpLeft:
- new_rect.set_right_without_resize(m_resize_window_original_rect.right());
- new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
- break;
- case ResizeDirection::UpRight:
- new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
- break;
- case ResizeDirection::DownLeft:
- new_rect.set_right_without_resize(m_resize_window_original_rect.right());
- break;
- default:
- ASSERT_NOT_REACHED();
- }
-
- if (new_rect.contains(event.position()))
- hovered_window = m_resize_window;
-
- if (m_resize_window->rect() == new_rect)
- return true;
-#ifdef RESIZE_DEBUG
- dbg() << "[WM] Resizing, original: " << m_resize_window_original_rect << ", now: " << new_rect;
-#endif
- m_resize_window->set_rect(new_rect);
- Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(new_rect));
- return true;
-}
-
-bool WindowManager::process_ongoing_drag(MouseEvent& event, Window*& hovered_window)
-{
- if (!m_dnd_client)
- return false;
-
- if (event.type() == Event::MouseMove) {
- // We didn't let go of the drag yet, see if we should send some drag move events..
- for_each_visible_window_from_front_to_back([&](Window& window) {
- if (!window.rect().contains(event.position()))
- return IterationDecision::Continue;
- hovered_window = &window;
- auto translated_event = event.translated(-window.position());
- translated_event.set_drag(true);
- translated_event.set_mime_data(*m_dnd_mime_data);
- deliver_mouse_event(window, translated_event);
- return IterationDecision::Break;
- });
- }
-
- if (!(event.type() == Event::MouseUp && event.button() == MouseButton::Left))
- return true;
-
- hovered_window = nullptr;
- for_each_visible_window_from_front_to_back([&](auto& window) {
- if (window.frame().rect().contains(event.position())) {
- hovered_window = &window;
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
- });
-
- if (hovered_window) {
- m_dnd_client->post_message(Messages::WindowClient::DragAccepted());
- if (hovered_window->client()) {
- auto translated_event = event.translated(-hovered_window->position());
- hovered_window->client()->post_message(Messages::WindowClient::DragDropped(hovered_window->window_id(), translated_event.position(), m_dnd_text, m_dnd_mime_data->all_data()));
- }
- } else {
- m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
- }
-
- end_dnd_drag();
- return true;
-}
-
-void WindowManager::set_cursor_tracking_button(Button* button)
-{
- m_cursor_tracking_button = button;
-}
-
-auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) const -> const ClickMetadata&
-{
- switch (button) {
- case MouseButton::Left:
- return m_left;
- case MouseButton::Right:
- return m_right;
- case MouseButton::Middle:
- return m_middle;
- case MouseButton::Back:
- return m_back;
- case MouseButton::Forward:
- return m_forward;
- default:
- ASSERT_NOT_REACHED();
- }
-}
-
-auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) -> ClickMetadata&
-{
- switch (button) {
- case MouseButton::Left:
- return m_left;
- case MouseButton::Right:
- return m_right;
- case MouseButton::Middle:
- return m_middle;
- case MouseButton::Back:
- return m_back;
- case MouseButton::Forward:
- return m_forward;
- default:
- ASSERT_NOT_REACHED();
- }
-}
-
-// #define DOUBLECLICK_DEBUG
-
-bool WindowManager::is_considered_doubleclick(const MouseEvent& event, const DoubleClickInfo::ClickMetadata& metadata) const
-{
- int elapsed_since_last_click = metadata.clock.elapsed();
- if (elapsed_since_last_click < m_double_click_speed) {
- auto diff = event.position() - metadata.last_position;
- auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
- if (distance_travelled_squared <= (m_max_distance_for_double_click * m_max_distance_for_double_click))
- return true;
- }
- return false;
-}
-
-void WindowManager::start_menu_doubleclick(Window& window, const MouseEvent& event)
-{
- // This is a special case. Basically, we're trying to determine whether
- // double clicking on the window menu icon happened. In this case, the
- // WindowFrame only receives a MouseDown event, and since the window
- // menu popus up, it does not see the MouseUp event. But, if they subsequently
- // click there again, the menu is closed and we receive a MouseUp event.
- // So, in order to be able to detect a double click when a menu is being
- // opened by the MouseDown event, we need to consider the MouseDown event
- // as a potential double-click trigger
- ASSERT(event.type() == Event::MouseDown);
-
- auto& metadata = m_double_click_info.metadata_for_button(event.button());
- if (&window != m_double_click_info.m_clicked_window) {
- // we either haven't clicked anywhere, or we haven't clicked on this
- // window. set the current click window, and reset the timers.
-#if defined(DOUBLECLICK_DEBUG)
- dbg() << "Initial mousedown on window " << &window << " for menu (previous was " << m_double_click_info.m_clicked_window << ')';
-#endif
- m_double_click_info.m_clicked_window = window;
- m_double_click_info.reset();
- }
-
- metadata.last_position = event.position();
- metadata.clock.start();
-}
-
-bool WindowManager::is_menu_doubleclick(Window& window, const MouseEvent& event) const
-{
- ASSERT(event.type() == Event::MouseUp);
-
- if (&window != m_double_click_info.m_clicked_window)
- return false;
-
- auto& metadata = m_double_click_info.metadata_for_button(event.button());
- if (!metadata.clock.is_valid())
- return false;
-
- return is_considered_doubleclick(event, metadata);
-}
-
-void WindowManager::process_event_for_doubleclick(Window& window, MouseEvent& event)
-{
- // We only care about button presses (because otherwise it's not a doubleclick, duh!)
- ASSERT(event.type() == Event::MouseUp);
-
- if (&window != m_double_click_info.m_clicked_window) {
- // we either haven't clicked anywhere, or we haven't clicked on this
- // window. set the current click window, and reset the timers.
-#if defined(DOUBLECLICK_DEBUG)
- dbg() << "Initial mouseup on window " << &window << " (previous was " << m_double_click_info.m_clicked_window << ')';
-#endif
- m_double_click_info.m_clicked_window = window;
- m_double_click_info.reset();
- }
-
- auto& metadata = m_double_click_info.metadata_for_button(event.button());
-
- if (!metadata.clock.is_valid() || !is_considered_doubleclick(event, metadata)) {
- // either the clock is invalid because we haven't clicked on this
- // button on this window yet, so there's nothing to do, or this
- // isn't considered to be a double click. either way, restart the
- // clock
- metadata.clock.start();
- } else {
-#if defined(DOUBLECLICK_DEBUG)
- dbg() << "Transforming MouseUp to MouseDoubleClick (" << metadata.clock.elapsed() << " < " << m_double_click_speed << ")!";
-#endif
- event = MouseEvent(Event::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
- // invalidate this now we've delivered a doubleclick, otherwise
- // tripleclick will deliver two doubleclick events (incorrectly).
- metadata.clock = {};
- }
-
- metadata.last_position = event.position();
-}
-
-void WindowManager::deliver_mouse_event(Window& window, MouseEvent& event)
-{
- window.dispatch_event(event);
- if (event.type() == Event::MouseUp) {
- process_event_for_doubleclick(window, event);
- if (event.type() == Event::MouseDoubleClick)
- window.dispatch_event(event);
- }
-}
-
-void WindowManager::process_mouse_event(MouseEvent& event, Window*& hovered_window)
-{
- HashTable<Window*> windows_who_received_mouse_event_due_to_cursor_tracking;
-
- // We need to process ongoing drag events first. Otherwise, global tracking
- // and dnd collides, leading to duplicate GUI::DragOperation instances
- if (process_ongoing_drag(event, hovered_window))
- return;
-
- for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
- if (!window->global_cursor_tracking() || !window->is_visible() || window->is_minimized() || window->blocking_modal_window())
- continue;
- windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
- auto translated_event = event.translated(-window->position());
- deliver_mouse_event(*window, translated_event);
- }
-
- hovered_window = nullptr;
-
- if (process_ongoing_window_move(event, hovered_window))
- return;
-
- if (process_ongoing_window_resize(event, hovered_window))
- return;
-
- if (m_cursor_tracking_button)
- return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
-
- // This is quite hackish, but it's how the Button hover effect is implemented.
- if (m_hovered_button && event.type() == Event::MouseMove)
- m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
-
- // FIXME: Now that the menubar has a dedicated window, is this special-casing really necessary?
- if (MenuManager::the().has_open_menu() || menubar_rect().contains(event.position())) {
- for_each_visible_window_of_type_from_front_to_back(WindowType::MenuApplet, [&](auto& window) {
- if (!window.rect_in_menubar().contains(event.position()))
- return IterationDecision::Continue;
- hovered_window = &window;
- return IterationDecision::Break;
- });
- clear_resize_candidate();
- MenuManager::the().dispatch_event(event);
- return;
- }
-
- Window* event_window_with_frame = nullptr;
-
- if (m_active_input_tracking_window) {
- // At this point, we have delivered the start of an input sequence to a
- // client application. We must keep delivering to that client
- // application until the input sequence is done.
- //
- // This prevents e.g. moving on one window out of the bounds starting
- // a move in that other unrelated window, and other silly shenanigans.
- if (!windows_who_received_mouse_event_due_to_cursor_tracking.contains(m_active_input_tracking_window)) {
- auto translated_event = event.translated(-m_active_input_tracking_window->position());
- deliver_mouse_event(*m_active_input_tracking_window, translated_event);
- windows_who_received_mouse_event_due_to_cursor_tracking.set(m_active_input_tracking_window.ptr());
- }
- if (event.type() == Event::MouseUp && event.buttons() == 0) {
- m_active_input_tracking_window = nullptr;
- }
-
- for_each_visible_window_from_front_to_back([&](auto& window) {
- if (window.frame().rect().contains(event.position())) {
- hovered_window = &window;
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
- });
- } else {
- auto process_mouse_event_for_window = [&](Window& window) {
- if (&window != m_resize_candidate.ptr())
- clear_resize_candidate();
-
- // First check if we should initiate a move or resize (Logo+LMB or Logo+RMB).
- // In those cases, the event is swallowed by the window manager.
- if (window.is_movable()) {
- if (!window.is_fullscreen() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Left) {
- hovered_window = &window;
- start_window_move(window, event);
- return;
- }
- if (window.is_resizable() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Right && !window.blocking_modal_window()) {
- hovered_window = &window;
- start_window_resize(window, event);
- return;
- }
- }
-
- if (m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseWheel) {
- float opacity_change = -event.wheel_delta() * 0.05f;
- float new_opacity = window.opacity() + opacity_change;
- if (new_opacity < 0.05f)
- new_opacity = 0.05f;
- if (new_opacity > 1.0f)
- new_opacity = 1.0f;
- window.set_opacity(new_opacity);
- return;
- }
-
- ASSERT(window.frame().rect().contains(event.position()));
- if (event.type() == Event::MouseDown) {
- // We're clicking on something that's blocked by a modal window.
- // Flash the modal window to let the user know about it.
- if (auto* blocking_modal_window = window.blocking_modal_window())
- blocking_modal_window->frame().start_flash_animation();
-
- if (window.type() == WindowType::Normal)
- move_to_front_and_make_active(window);
- else if (window.type() == WindowType::Desktop)
- set_active_window(&window);
- }
-
- // Well okay, let's see if we're hitting the frame or the window inside the frame.
- if (window.rect().contains(event.position())) {
- hovered_window = &window;
- if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window) && !window.blocking_modal_window()) {
- auto translated_event = event.translated(-window.position());
- deliver_mouse_event(window, translated_event);
- if (event.type() == Event::MouseDown) {
- m_active_input_tracking_window = window;
- }
- }
- return;
- }
-
- // We are hitting the frame, pass the event along to WindowFrame.
- window.frame().on_mouse_event(event.translated(-window.frame().rect().location()));
- event_window_with_frame = &window;
- };
-
- if (auto* fullscreen_window = active_fullscreen_window()) {
- process_mouse_event_for_window(*fullscreen_window);
- } else {
- for_each_visible_window_from_front_to_back([&](Window& window) {
- auto window_frame_rect = window.frame().rect();
- if (!window_frame_rect.contains(event.position()))
- return IterationDecision::Continue;
- process_mouse_event_for_window(window);
- return IterationDecision::Break;
- });
- }
-
- // Clicked outside of any window
- if (!hovered_window && !event_window_with_frame && event.type() == Event::MouseDown)
- set_active_window(nullptr);
- }
-
- if (event_window_with_frame != m_resize_candidate.ptr())
- clear_resize_candidate();
-}
-
-void WindowManager::clear_resize_candidate()
-{
- if (m_resize_candidate)
- Compositor::the().invalidate_cursor();
- m_resize_candidate = nullptr;
-}
-
-Gfx::IntRect WindowManager::menubar_rect() const
-{
- if (active_fullscreen_window())
- return {};
- return MenuManager::the().menubar_rect();
-}
-
-Gfx::IntRect WindowManager::desktop_rect() const
-{
- if (active_fullscreen_window())
- return {};
- return {
- 0,
- menubar_rect().bottom() + 1,
- Screen::the().width(),
- Screen::the().height() - menubar_rect().height() - 28
- };
-}
-
-void WindowManager::event(Core::Event& event)
-{
- if (static_cast<Event&>(event).is_mouse_event()) {
- Window* hovered_window = nullptr;
- process_mouse_event(static_cast<MouseEvent&>(event), hovered_window);
- set_hovered_window(hovered_window);
- return;
- }
-
- if (static_cast<Event&>(event).is_key_event()) {
- auto& key_event = static_cast<const KeyEvent&>(event);
- m_keyboard_modifiers = key_event.modifiers();
-
- // Escape key cancels an ongoing drag.
- if (key_event.type() == Event::KeyDown && key_event.key() == Key_Escape && m_dnd_client) {
- // Notify the drag-n-drop client that the drag was cancelled.
- m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
-
- // Also notify the currently hovered window (if any) that the ongoing drag was cancelled.
- if (m_hovered_window && m_hovered_window->client() && m_hovered_window->client() != m_dnd_client)
- m_hovered_window->client()->post_message(Messages::WindowClient::DragCancelled());
-
- end_dnd_drag();
- return;
- }
-
- if (MenuManager::the().current_menu()) {
- MenuManager::the().dispatch_event(event);
- return;
- }
-
- if (key_event.type() == Event::KeyDown && ((key_event.modifiers() == Mod_Logo && key_event.key() == Key_Tab) || (key_event.modifiers() == (Mod_Logo | Mod_Shift) && key_event.key() == Key_Tab)))
- m_switcher.show();
- if (m_switcher.is_visible()) {
- m_switcher.on_key_event(key_event);
- return;
- }
-
- if (m_active_input_window) {
- if (key_event.type() == Event::KeyDown && key_event.modifiers() == Mod_Logo) {
- if (key_event.key() == Key_Down) {
- if (m_active_input_window->is_resizable() && m_active_input_window->is_maximized()) {
- maximize_windows(*m_active_input_window, false);
- return;
- }
- if (m_active_input_window->is_minimizable())
- minimize_windows(*m_active_input_window, true);
- return;
- }
- if (m_active_input_window->is_resizable()) {
- if (key_event.key() == Key_Up) {
- maximize_windows(*m_active_input_window, !m_active_input_window->is_maximized());
- return;
- }
- if (key_event.key() == Key_Left) {
- if (m_active_input_window->tiled() != WindowTileType::None) {
- m_active_input_window->set_tiled(WindowTileType::None);
- return;
- }
- if (m_active_input_window->is_maximized())
- maximize_windows(*m_active_input_window, false);
- m_active_input_window->set_tiled(WindowTileType::Left);
- return;
- }
- if (key_event.key() == Key_Right) {
- if (m_active_input_window->tiled() != WindowTileType::None) {
- m_active_input_window->set_tiled(WindowTileType::None);
- return;
- }
- if (m_active_input_window->is_maximized())
- maximize_windows(*m_active_input_window, false);
- m_active_input_window->set_tiled(WindowTileType::Right);
- return;
- }
- }
- }
- m_active_input_window->dispatch_event(event);
- return;
- }
- }
-
- Core::Object::event(event);
-}
-
-void WindowManager::set_highlight_window(Window* window)
-{
- if (window == m_highlight_window)
- return;
- if (auto* previous_highlight_window = m_highlight_window.ptr())
- previous_highlight_window->invalidate();
- m_highlight_window = window;
- if (m_highlight_window)
- m_highlight_window->invalidate();
-}
-
-bool WindowManager::is_active_window_or_accessory(Window& window) const
-{
- if (m_active_window == &window)
- return true;
-
- if (!window.is_accessory())
- return false;
-
- return m_active_window == window.parent_window();
-}
-
-static bool window_type_can_become_active(WindowType type)
-{
- return type == WindowType::Normal || type == WindowType::Desktop;
-}
-
-void WindowManager::restore_active_input_window(Window* window)
-{
- // If the previous active input window is gone, fall back to the
- // current active window
- if (!window)
- window = active_window();
- // If the current active window is also gone, pick some other window
- if (!window && pick_new_active_window(nullptr))
- return;
-
- set_active_input_window(window);
-}
-
-Window* WindowManager::set_active_input_window(Window* window)
-{
- if (window == m_active_input_window)
- return window;
-
- Window* previous_input_window = m_active_input_window;
- if (previous_input_window)
- Core::EventLoop::current().post_event(*previous_input_window, make<Event>(Event::WindowInputLeft));
-
- if (window) {
- m_active_input_window = *window;
- Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
- } else {
- m_active_input_window = nullptr;
- }
-
- return previous_input_window;
-}
-
-void WindowManager::set_active_window(Window* window, bool make_input)
-{
- if (window) {
- if (auto* modal_window = window->blocking_modal_window()) {
- ASSERT(modal_window->is_modal());
- ASSERT(modal_window != window);
- window = modal_window;
- make_input = true;
- }
-
- if (!window_type_can_become_active(window->type()))
- return;
- }
-
- auto* new_active_input_window = window;
- if (window && window->is_accessory()) {
- // The parent of an accessory window is always the active
- // window, but input is routed to the accessory window
- window = window->parent_window();
- }
-
- if (make_input)
- set_active_input_window(new_active_input_window);
-
- if (window == m_active_window)
- return;
-
- auto* previously_active_window = m_active_window.ptr();
-
- ClientConnection* previously_active_client = nullptr;
- ClientConnection* active_client = nullptr;
-
- if (previously_active_window) {
- previously_active_client = previously_active_window->client();
- Core::EventLoop::current().post_event(*previously_active_window, make<Event>(Event::WindowDeactivated));
- previously_active_window->invalidate();
- m_active_window = nullptr;
- m_active_input_tracking_window = nullptr;
- tell_wm_listeners_window_state_changed(*previously_active_window);
- }
-
- if (window) {
- m_active_window = *window;
- active_client = m_active_window->client();
- Core::EventLoop::current().post_event(*m_active_window, make<Event>(Event::WindowActivated));
- m_active_window->invalidate();
- if (auto* client = window->client()) {
- MenuManager::the().set_current_menubar(client->app_menubar());
- } else {
- MenuManager::the().set_current_menubar(nullptr);
- }
- tell_wm_listeners_window_state_changed(*m_active_window);
- } else {
- MenuManager::the().set_current_menubar(nullptr);
- }
-
- if (active_client != previously_active_client) {
- if (previously_active_client)
- previously_active_client->deboost();
- if (active_client)
- active_client->boost();
- }
-}
-
-void WindowManager::set_hovered_window(Window* window)
-{
- if (m_hovered_window == window)
- return;
-
- if (m_hovered_window)
- Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowLeft));
-
- m_hovered_window = window;
-
- if (m_hovered_window)
- Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowEntered));
-}
-
-const ClientConnection* WindowManager::active_client() const
-{
- if (m_active_window)
- return m_active_window->client();
- return nullptr;
-}
-
-void WindowManager::notify_client_changed_app_menubar(ClientConnection& client)
-{
- if (active_client() == &client)
- MenuManager::the().set_current_menubar(client.app_menubar());
-}
-
-const Cursor& WindowManager::active_cursor() const
-{
- if (m_dnd_client)
- return *m_drag_cursor;
-
- if (m_move_window)
- return *m_move_cursor;
-
- if (m_resize_window || m_resize_candidate) {
- switch (m_resize_direction) {
- case ResizeDirection::Up:
- case ResizeDirection::Down:
- return *m_resize_vertically_cursor;
- case ResizeDirection::Left:
- case ResizeDirection::Right:
- return *m_resize_horizontally_cursor;
- case ResizeDirection::UpLeft:
- case ResizeDirection::DownRight:
- return *m_resize_diagonally_tlbr_cursor;
- case ResizeDirection::UpRight:
- case ResizeDirection::DownLeft:
- return *m_resize_diagonally_bltr_cursor;
- case ResizeDirection::None:
- break;
- }
- }
-
- if (m_hovered_window) {
- if (auto* modal_window = const_cast<Window&>(*m_hovered_window).blocking_modal_window()) {
- if (modal_window->cursor())
- return *modal_window->cursor();
- } else if (m_hovered_window->cursor()) {
- return *m_hovered_window->cursor();
- }
- }
-
- return *m_arrow_cursor;
-}
-
-void WindowManager::set_hovered_button(Button* button)
-{
- m_hovered_button = button;
-}
-
-void WindowManager::set_resize_candidate(Window& window, ResizeDirection direction)
-{
- m_resize_candidate = window;
- m_resize_direction = direction;
-}
-
-ResizeDirection WindowManager::resize_direction_of_window(const Window& window)
-{
- if (&window != m_resize_window)
- return ResizeDirection::None;
- return m_resize_direction;
-}
-
-Gfx::IntRect WindowManager::maximized_window_rect(const Window& window) const
-{
- Gfx::IntRect rect = Screen::the().rect();
-
- // Subtract window title bar (leaving the border)
- rect.set_y(rect.y() + window.frame().title_bar_rect().height());
- rect.set_height(rect.height() - window.frame().title_bar_rect().height());
-
- // Subtract menu bar
- rect.set_y(rect.y() + menubar_rect().height());
- rect.set_height(rect.height() - menubar_rect().height());
-
- // Subtract taskbar window height if present
- const_cast<WindowManager*>(this)->for_each_visible_window_of_type_from_back_to_front(WindowType::Taskbar, [&rect](Window& taskbar_window) {
- rect.set_height(rect.height() - taskbar_window.height());
- return IterationDecision::Break;
- });
-
- constexpr int tasteful_space_above_maximized_window = 2;
- rect.set_y(rect.y() + tasteful_space_above_maximized_window);
- rect.set_height(rect.height() - tasteful_space_above_maximized_window);
-
- return rect;
-}
-
-void WindowManager::start_dnd_drag(ClientConnection& client, const String& text, Gfx::Bitmap* bitmap, const Core::MimeData& mime_data)
-{
- ASSERT(!m_dnd_client);
- m_dnd_client = client;
- m_dnd_text = text;
- m_dnd_bitmap = bitmap;
- m_dnd_mime_data = mime_data;
- Compositor::the().invalidate_cursor();
- m_active_input_tracking_window = nullptr;
-}
-
-void WindowManager::end_dnd_drag()
-{
- ASSERT(m_dnd_client);
- Compositor::the().invalidate_cursor();
- m_dnd_client = nullptr;
- m_dnd_text = {};
- m_dnd_bitmap = nullptr;
-}
-
-Gfx::IntRect WindowManager::dnd_rect() const
-{
- int bitmap_width = m_dnd_bitmap ? m_dnd_bitmap->width() : 0;
- int bitmap_height = m_dnd_bitmap ? m_dnd_bitmap->height() : 0;
- int width = font().width(m_dnd_text) + bitmap_width;
- int height = max((int)font().glyph_height(), bitmap_height);
- auto location = Compositor::the().current_cursor_rect().center().translated(8, 8);
- return Gfx::IntRect(location, { width, height }).inflated(16, 8);
-}
-
-bool WindowManager::update_theme(String theme_path, String theme_name)
-{
- auto new_theme = Gfx::load_system_theme(theme_path);
- if (!new_theme)
- return false;
- ASSERT(new_theme);
- Gfx::set_system_theme(*new_theme);
- m_palette = Gfx::PaletteImpl::create_with_shared_buffer(*new_theme);
- Compositor::the().set_background_color(palette().desktop_background().to_string());
- HashTable<ClientConnection*> notified_clients;
- for_each_window([&](Window& window) {
- if (window.client()) {
- if (!notified_clients.contains(window.client())) {
- window.client()->post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer_id()));
- notified_clients.set(window.client());
- }
- }
- window.frame().layout_buttons();
- window.frame().set_button_icons();
- return IterationDecision::Continue;
- });
- MenuManager::the().did_change_theme();
- auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
- wm_config->write_entry("Theme", "Name", theme_name);
- wm_config->sync();
- Compositor::the().invalidate_screen();
- return true;
-}
-
-void WindowManager::did_popup_a_menu(Badge<Menu>)
-{
- // Clear any ongoing input gesture
- if (!m_active_input_tracking_window)
- return;
- m_active_input_tracking_window->set_automatic_cursor_tracking_enabled(false);
- m_active_input_tracking_window = nullptr;
-}
-
-void WindowManager::minimize_windows(Window& window, bool minimized)
-{
- for_each_window_in_modal_stack(window, [&](auto& w, bool) {
- w.set_minimized(minimized);
- return IterationDecision::Continue;
- });
-}
-
-void WindowManager::maximize_windows(Window& window, bool maximized)
-{
- for_each_window_in_modal_stack(window, [&](auto& w, bool stack_top) {
- if (stack_top)
- w.set_maximized(maximized);
- if (w.is_minimized())
- w.set_minimized(false);
- return IterationDecision::Continue;
- });
-}
-
-Gfx::IntPoint WindowManager::get_recommended_window_position(const Gfx::IntPoint& desired)
-{
- // FIXME: Find a better source for the width and height to shift by.
- Gfx::IntPoint shift(8, Gfx::WindowTheme::current().title_bar_height(palette()) + 10);
-
- // FIXME: Find a better source for this.
- int taskbar_height = 28;
- int menubar_height = MenuManager::the().menubar_rect().height();
-
- const Window* overlap_window = nullptr;
- for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& window) {
- if (window.default_positioned() && (!overlap_window || overlap_window->window_id() < window.window_id())) {
- overlap_window = &window;
- }
- return IterationDecision::Continue;
- });
-
- Gfx::IntPoint point;
- if (overlap_window) {
- point = overlap_window->position() + shift;
- point = { point.x() % Screen::the().width(),
- (point.y() >= (Screen::the().height() - taskbar_height))
- ? menubar_height + Gfx::WindowTheme::current().title_bar_height(palette())
- : point.y() };
- } else {
- point = desired;
- }
-
- return point;
-}
-}
diff --git a/Services/WindowServer/WindowManager.h b/Services/WindowServer/WindowManager.h
deleted file mode 100644
index 94e1f212d7..0000000000
--- a/Services/WindowServer/WindowManager.h
+++ /dev/null
@@ -1,479 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/HashMap.h>
-#include <AK/HashTable.h>
-#include <AK/InlineLinkedList.h>
-#include <AK/WeakPtr.h>
-#include <LibCore/ConfigFile.h>
-#include <LibCore/ElapsedTimer.h>
-#include <LibGfx/Color.h>
-#include <LibGfx/DisjointRectSet.h>
-#include <LibGfx/Painter.h>
-#include <LibGfx/Palette.h>
-#include <LibGfx/Rect.h>
-#include <WindowServer/Cursor.h>
-#include <WindowServer/Event.h>
-#include <WindowServer/MenuBar.h>
-#include <WindowServer/MenuManager.h>
-#include <WindowServer/Window.h>
-#include <WindowServer/WindowSwitcher.h>
-#include <WindowServer/WindowType.h>
-
-namespace WindowServer {
-
-class Screen;
-class MouseEvent;
-class Window;
-class ClientConnection;
-class WindowSwitcher;
-class Button;
-
-enum class ResizeDirection {
- None,
- Left,
- UpLeft,
- Up,
- UpRight,
- Right,
- DownRight,
- Down,
- DownLeft
-};
-
-class WindowManager : public Core::Object {
- C_OBJECT(WindowManager)
-
- friend class Compositor;
- friend class WindowFrame;
- friend class WindowSwitcher;
-
-public:
- static WindowManager& the();
-
- explicit WindowManager(const Gfx::PaletteImpl&);
- virtual ~WindowManager() override;
-
- Palette palette() const { return Palette(*m_palette); }
-
- RefPtr<Core::ConfigFile> config() const { return m_config; }
- void reload_config(bool);
-
- void add_window(Window&);
- void remove_window(Window&);
-
- void notify_title_changed(Window&);
- void notify_modal_unparented(Window&);
- void notify_rect_changed(Window&, const Gfx::IntRect& oldRect, const Gfx::IntRect& newRect);
- void notify_minimization_state_changed(Window&);
- void notify_opacity_changed(Window&);
- void notify_occlusion_state_changed(Window&);
- void notify_progress_changed(Window&);
- void notify_client_changed_app_menubar(ClientConnection&);
-
- Gfx::IntRect maximized_window_rect(const Window&) const;
-
- const ClientConnection* dnd_client() const { return m_dnd_client.ptr(); }
- const String& dnd_text() const { return m_dnd_text; }
- const Core::MimeData& dnd_mime_data() const { return *m_dnd_mime_data; }
- const Gfx::Bitmap* dnd_bitmap() const { return m_dnd_bitmap; }
- Gfx::IntRect dnd_rect() const;
-
- void start_dnd_drag(ClientConnection&, const String& text, Gfx::Bitmap*, const Core::MimeData&);
- void end_dnd_drag();
-
- Window* active_window() { return m_active_window.ptr(); }
- const Window* active_window() const { return m_active_window.ptr(); }
- Window* active_input_window() { return m_active_input_window.ptr(); }
- const Window* active_input_window() const { return m_active_input_window.ptr(); }
- const ClientConnection* active_client() const;
-
- const Window* highlight_window() const { return m_highlight_window.ptr(); }
- void set_highlight_window(Window*);
-
- void move_to_front_and_make_active(Window&);
-
- Gfx::IntRect menubar_rect() const;
- Gfx::IntRect desktop_rect() const;
-
- const Cursor& active_cursor() const;
- const Cursor& hidden_cursor() const { return *m_hidden_cursor; }
- const Cursor& arrow_cursor() const { return *m_arrow_cursor; }
- const Cursor& crosshair_cursor() const { return *m_crosshair_cursor; }
- const Cursor& hand_cursor() const { return *m_hand_cursor; }
- const Cursor& help_cursor() const { return *m_help_cursor; }
- const Cursor& resize_horizontally_cursor() const { return *m_resize_horizontally_cursor; }
- const Cursor& resize_vertically_cursor() const { return *m_resize_vertically_cursor; }
- const Cursor& resize_diagonally_tlbr_cursor() const { return *m_resize_diagonally_tlbr_cursor; }
- const Cursor& resize_diagonally_bltr_cursor() const { return *m_resize_diagonally_bltr_cursor; }
- const Cursor& resize_column_cursor() const { return *m_resize_column_cursor; }
- const Cursor& resize_row_cursor() const { return *m_resize_row_cursor; }
- const Cursor& i_beam_cursor() const { return *m_i_beam_cursor; }
- const Cursor& disallowed_cursor() const { return *m_disallowed_cursor; }
- const Cursor& move_cursor() const { return *m_move_cursor; }
- const Cursor& drag_cursor() const { return *m_drag_cursor; }
- const Cursor& wait_cursor() const { return *m_wait_cursor; }
-
- const Gfx::Font& font() const;
- const Gfx::Font& window_title_font() const;
-
- bool set_resolution(int width, int height);
- Gfx::IntSize resolution() const;
-
- void set_acceleration_factor(double);
- void set_scroll_step_size(unsigned);
-
- Window* set_active_input_window(Window*);
- void restore_active_input_window(Window*);
- void set_active_window(Window*, bool make_input = true);
- void set_hovered_button(Button*);
-
- const Button* cursor_tracking_button() const { return m_cursor_tracking_button.ptr(); }
- void set_cursor_tracking_button(Button*);
-
- void set_resize_candidate(Window&, ResizeDirection);
- void clear_resize_candidate();
- ResizeDirection resize_direction_of_window(const Window&);
-
- void tell_wm_listeners_window_state_changed(Window&);
- void tell_wm_listeners_window_icon_changed(Window&);
- void tell_wm_listeners_window_rect_changed(Window&);
-
- bool is_active_window_or_accessory(Window&) const;
-
- void start_window_resize(Window&, const Gfx::IntPoint&, MouseButton);
- void start_window_resize(Window&, const MouseEvent&);
-
- const Window* active_fullscreen_window() const
- {
- if (m_active_window && m_active_window->is_fullscreen()) {
- return m_active_window;
- }
- return nullptr;
- };
-
- Window* active_fullscreen_window()
- {
- if (m_active_window && m_active_window->is_fullscreen()) {
- return m_active_window;
- }
- return nullptr;
- }
-
- bool update_theme(String theme_path, String theme_name);
-
- void set_hovered_window(Window*);
- void deliver_mouse_event(Window& window, MouseEvent& event);
-
- void did_popup_a_menu(Badge<Menu>);
-
- void start_menu_doubleclick(Window& window, const MouseEvent& event);
- bool is_menu_doubleclick(Window& window, const MouseEvent& event) const;
-
- void minimize_windows(Window&, bool);
- void maximize_windows(Window&, bool);
-
- template<typename Function>
- IterationDecision for_each_window_in_modal_stack(Window& window, Function f)
- {
- auto* blocking_modal_window = window.blocking_modal_window();
- if (blocking_modal_window || window.is_modal()) {
- Vector<Window*> modal_stack;
- auto* modal_stack_top = blocking_modal_window ? blocking_modal_window : &window;
- for (auto* parent = modal_stack_top->parent_window(); parent; parent = parent->parent_window()) {
- auto* blocked_by = parent->blocking_modal_window();
- if (!blocked_by || (blocked_by != modal_stack_top && !modal_stack_top->is_descendant_of(*blocked_by)))
- break;
- modal_stack.append(parent);
- if (!parent->is_modal())
- break;
- }
- if (!modal_stack.is_empty()) {
- for (size_t i = modal_stack.size(); i > 0; i--) {
- IterationDecision decision = f(*modal_stack[i - 1], false);
- if (decision != IterationDecision::Continue)
- return decision;
- }
- }
- return f(*modal_stack_top, true);
- } else {
- // Not a modal window stack, just "iterate" over this window
- return f(window, true);
- }
- }
-
- Gfx::IntPoint get_recommended_window_position(const Gfx::IntPoint& desired);
-
-private:
- NonnullRefPtr<Cursor> get_cursor(const String& name);
-
- void process_mouse_event(MouseEvent&, Window*& hovered_window);
- void process_event_for_doubleclick(Window& window, MouseEvent& event);
- bool process_ongoing_window_resize(const MouseEvent&, Window*& hovered_window);
- bool process_ongoing_window_move(MouseEvent&, Window*& hovered_window);
- bool process_ongoing_drag(MouseEvent&, Window*& hovered_window);
- void start_window_move(Window&, const MouseEvent&);
- template<typename Callback>
- IterationDecision for_each_visible_window_of_type_from_back_to_front(WindowType, Callback, bool ignore_highlight = false);
- template<typename Callback>
- IterationDecision for_each_visible_window_of_type_from_front_to_back(WindowType, Callback, bool ignore_highlight = false);
- template<typename Callback>
- IterationDecision for_each_visible_window_from_front_to_back(Callback);
- template<typename Callback>
- IterationDecision for_each_visible_window_from_back_to_front(Callback);
- template<typename Callback>
- void for_each_window_listening_to_wm_events(Callback);
- template<typename Callback>
- void for_each_window(Callback);
- template<typename Callback>
- IterationDecision for_each_window_of_type_from_front_to_back(WindowType, Callback, bool ignore_highlight = false);
-
- virtual void event(Core::Event&) override;
- void paint_window_frame(const Window&);
- void tell_wm_listener_about_window(Window& listener, Window&);
- void tell_wm_listener_about_window_icon(Window& listener, Window&);
- void tell_wm_listener_about_window_rect(Window& listener, Window&);
- bool pick_new_active_window(Window*);
-
- void do_move_to_front(Window&, bool, bool);
-
- RefPtr<Cursor> m_hidden_cursor;
- RefPtr<Cursor> m_arrow_cursor;
- RefPtr<Cursor> m_hand_cursor;
- RefPtr<Cursor> m_help_cursor;
- RefPtr<Cursor> m_resize_horizontally_cursor;
- RefPtr<Cursor> m_resize_vertically_cursor;
- RefPtr<Cursor> m_resize_diagonally_tlbr_cursor;
- RefPtr<Cursor> m_resize_diagonally_bltr_cursor;
- RefPtr<Cursor> m_resize_column_cursor;
- RefPtr<Cursor> m_resize_row_cursor;
- RefPtr<Cursor> m_i_beam_cursor;
- RefPtr<Cursor> m_disallowed_cursor;
- RefPtr<Cursor> m_move_cursor;
- RefPtr<Cursor> m_drag_cursor;
- RefPtr<Cursor> m_wait_cursor;
- RefPtr<Cursor> m_crosshair_cursor;
-
- InlineLinkedList<Window> m_windows_in_order;
-
- struct DoubleClickInfo {
- struct ClickMetadata {
- Core::ElapsedTimer clock;
- Gfx::IntPoint last_position;
- };
-
- const ClickMetadata& metadata_for_button(MouseButton) const;
- ClickMetadata& metadata_for_button(MouseButton);
-
- void reset()
- {
- m_left = {};
- m_right = {};
- m_middle = {};
- m_back = {};
- m_forward = {};
- }
-
- WeakPtr<Window> m_clicked_window;
-
- private:
- ClickMetadata m_left;
- ClickMetadata m_right;
- ClickMetadata m_middle;
- ClickMetadata m_back;
- ClickMetadata m_forward;
- };
-
- bool is_considered_doubleclick(const MouseEvent& event, const DoubleClickInfo::ClickMetadata& metadata) const;
-
- DoubleClickInfo m_double_click_info;
- int m_double_click_speed { 0 };
- int m_max_distance_for_double_click { 4 };
-
- WeakPtr<Window> m_active_window;
- WeakPtr<Window> m_hovered_window;
- WeakPtr<Window> m_highlight_window;
- WeakPtr<Window> m_active_input_window;
- WeakPtr<Window> m_active_input_tracking_window;
-
- WeakPtr<Window> m_move_window;
- Gfx::IntPoint m_move_origin;
- Gfx::IntPoint m_move_window_origin;
-
- WeakPtr<Window> m_resize_window;
- WeakPtr<Window> m_resize_candidate;
- MouseButton m_resizing_mouse_button { MouseButton::None };
- Gfx::IntRect m_resize_window_original_rect;
- Gfx::IntPoint m_resize_origin;
- ResizeDirection m_resize_direction { ResizeDirection::None };
-
- u8 m_keyboard_modifiers { 0 };
-
- WindowSwitcher m_switcher;
-
- WeakPtr<Button> m_cursor_tracking_button;
- WeakPtr<Button> m_hovered_button;
-
- NonnullRefPtr<Gfx::PaletteImpl> m_palette;
-
- RefPtr<Core::ConfigFile> m_config;
-
- WeakPtr<ClientConnection> m_dnd_client;
- String m_dnd_text;
- RefPtr<Core::MimeData> m_dnd_mime_data;
- RefPtr<Gfx::Bitmap> m_dnd_bitmap;
-};
-
-template<typename Callback>
-IterationDecision WindowManager::for_each_visible_window_of_type_from_back_to_front(WindowType type, Callback callback, bool ignore_highlight)
-{
- bool do_highlight_window_at_end = false;
- for (auto& window : m_windows_in_order) {
- if (!window.is_visible())
- continue;
- if (window.is_minimized())
- continue;
- if (window.type() != type)
- continue;
- if (!ignore_highlight && m_highlight_window == &window) {
- do_highlight_window_at_end = true;
- continue;
- }
- if (callback(window) == IterationDecision::Break)
- return IterationDecision::Break;
- }
- if (do_highlight_window_at_end) {
- if (callback(*m_highlight_window) == IterationDecision::Break)
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
-}
-
-template<typename Callback>
-IterationDecision WindowManager::for_each_visible_window_from_back_to_front(Callback callback)
-{
- if (for_each_visible_window_of_type_from_back_to_front(WindowType::Desktop, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_back_to_front(WindowType::Normal, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_back_to_front(WindowType::Taskbar, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_back_to_front(WindowType::Notification, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_back_to_front(WindowType::Tooltip, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_back_to_front(WindowType::Menubar, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_back_to_front(WindowType::Menu, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- return for_each_visible_window_of_type_from_back_to_front(WindowType::WindowSwitcher, callback);
-}
-
-template<typename Callback>
-IterationDecision WindowManager::for_each_visible_window_of_type_from_front_to_back(WindowType type, Callback callback, bool ignore_highlight)
-{
- if (!ignore_highlight && m_highlight_window && m_highlight_window->type() == type && m_highlight_window->is_visible()) {
- if (callback(*m_highlight_window) == IterationDecision::Break)
- return IterationDecision::Break;
- }
-
- for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
- if (!window->is_visible())
- continue;
- if (window->is_minimized())
- continue;
- if (window->type() != type)
- continue;
- if (!ignore_highlight && window == m_highlight_window)
- continue;
- if (callback(*window) == IterationDecision::Break)
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
-}
-
-template<typename Callback>
-IterationDecision WindowManager::for_each_visible_window_from_front_to_back(Callback callback)
-{
- if (for_each_visible_window_of_type_from_front_to_back(WindowType::WindowSwitcher, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_front_to_back(WindowType::Menu, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_front_to_back(WindowType::Menubar, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_front_to_back(WindowType::Tooltip, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_front_to_back(WindowType::Notification, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_front_to_back(WindowType::Taskbar, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- if (for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, callback) == IterationDecision::Break)
- return IterationDecision::Break;
- return for_each_visible_window_of_type_from_front_to_back(WindowType::Desktop, callback);
-}
-
-template<typename Callback>
-void WindowManager::for_each_window_listening_to_wm_events(Callback callback)
-{
- for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
- if (!window->listens_to_wm_events())
- continue;
- if (callback(*window) == IterationDecision::Break)
- return;
- }
-}
-
-template<typename Callback>
-void WindowManager::for_each_window(Callback callback)
-{
- for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
- if (callback(*window) == IterationDecision::Break)
- return;
- }
-}
-
-template<typename Callback>
-IterationDecision WindowManager::for_each_window_of_type_from_front_to_back(WindowType type, Callback callback, bool ignore_highlight)
-{
- if (!ignore_highlight && m_highlight_window && m_highlight_window->type() == type && m_highlight_window->is_visible()) {
- if (callback(*m_highlight_window) == IterationDecision::Break)
- return IterationDecision::Break;
- }
-
- for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
- if (window->type() != type)
- continue;
- if (!ignore_highlight && window == m_highlight_window)
- continue;
- if (callback(*window) == IterationDecision::Break)
- return IterationDecision::Break;
- }
- return IterationDecision::Continue;
-}
-
-}
diff --git a/Services/WindowServer/WindowServer.ipc b/Services/WindowServer/WindowServer.ipc
deleted file mode 100644
index 72399de1db..0000000000
--- a/Services/WindowServer/WindowServer.ipc
+++ /dev/null
@@ -1,117 +0,0 @@
-endpoint WindowServer = 2
-{
- Greet() => (i32 client_id, Gfx::IntRect screen_rect, i32 system_theme_buffer_id)
-
- CreateMenubar() => (i32 menubar_id)
- DestroyMenubar(i32 menubar_id) => ()
-
- CreateMenu([UTF8] String menu_title) => (i32 menu_id)
- DestroyMenu(i32 menu_id) => ()
-
- AddMenuToMenubar(i32 menubar_id, i32 menu_id) => ()
- SetApplicationMenubar(i32 menubar_id) => ()
-
- SetSystemMenu(i32 menu_id) => ()
-
- AddMenuItem(
- i32 menu_id,
- i32 identifier,
- i32 submenu_id,
- [UTF8] String text,
- bool enabled,
- bool checkable,
- bool checked,
- bool is_default,
- [UTF8] String shortcut,
- i32 icon_buffer_id,
- bool exclusive) => ()
-
- AddMenuSeparator(i32 menu_id) => ()
-
- UpdateMenuItem(i32 menu_id, i32 identifier, i32 submenu_id, [UTF8] String text, bool enabled, bool checkable, bool checked, bool is_default, [UTF8] String shortcut) => ()
-
- CreateWindow(
- Gfx::IntRect rect,
- bool auto_position,
- bool has_alpha_channel,
- bool modal,
- bool minimizable,
- bool resizable,
- bool fullscreen,
- bool frameless,
- bool accessory,
- float opacity,
- Gfx::IntSize base_size,
- Gfx::IntSize size_increment,
- Optional<Gfx::IntSize> resize_aspect_ratio,
- i32 type,
- [UTF8] String title,
- i32 parent_window_id) => (i32 window_id)
-
- DestroyWindow(i32 window_id) => (Vector<i32> destroyed_window_ids)
-
- SetWindowTitle(i32 window_id, [UTF8] String title) => ()
- GetWindowTitle(i32 window_id) => ([UTF8] String title)
-
- SetWindowProgress(i32 window_id, i32 progress) =|
-
- SetWindowRect(i32 window_id, Gfx::IntRect rect) => (Gfx::IntRect rect)
- GetWindowRect(i32 window_id) => (Gfx::IntRect rect)
-
- GetWindowRectInMenubar(i32 window_id) => (Gfx::IntRect rect)
-
- IsMaximized(i32 window_id) => (bool maximized)
-
- InvalidateRect(i32 window_id, Vector<Gfx::IntRect> rects, bool ignore_occlusion) =|
- DidFinishPainting(i32 window_id, Vector<Gfx::IntRect> rects) =|
-
- SetGlobalCursorTracking(i32 window_id, bool enabled) => ()
- SetWindowOpacity(i32 window_id, float opacity) => ()
-
- SetWindowBackingStore(i32 window_id, i32 bpp, i32 pitch, i32 shbuf_id, bool has_alpha_channel, Gfx::IntSize size, bool flush_immediately) => ()
-
- WM_SetActiveWindow(i32 client_id, i32 window_id) =|
- WM_SetWindowMinimized(i32 client_id, i32 window_id, bool minimized) =|
- WM_StartWindowResize(i32 client_id, i32 window_id) =|
- WM_PopupWindowMenu(i32 client_id, i32 window_id, Gfx::IntPoint screen_position) =|
- WM_SetWindowTaskbarRect(i32 client_id, i32 window_id, Gfx::IntRect rect) =|
-
- SetWindowHasAlphaChannel(i32 window_id, bool has_alpha_channel) => ()
- MoveWindowToFront(i32 window_id) => ()
- SetFullscreen(i32 window_id, bool fullscreen) => ()
- PopupMenu(i32 menu_id, Gfx::IntPoint screen_position) => ()
- DismissMenu(i32 menu_id) => ()
-
- AsyncSetWallpaper(String path) =|
-
- SetBackgroundColor(String background_color) => ()
- SetWallpaperMode(String mode) => ()
-
- SetResolution(Gfx::IntSize resolution) => (bool success, Gfx::IntSize resolution)
- SetWindowIconBitmap(i32 window_id, Gfx::ShareableBitmap icon) => ()
-
- GetWallpaper() => (String path)
- SetWindowCursor(i32 window_id, i32 cursor_type) => ()
- SetWindowCustomCursor(i32 window_id, Gfx::ShareableBitmap cursor) => ()
-
- StartDrag([UTF8] String text, HashMap<String,ByteBuffer> mime_data, i32 bitmap_id, Gfx::IntSize bitmap_size) => (bool started)
-
- SetSystemTheme(String theme_path, [UTF8] String theme_name) => (bool success)
- GetSystemTheme() => ([UTF8] String theme_name)
-
- SetWindowBaseSizeAndSizeIncrement(i32 window_id, Gfx::IntSize base_size, Gfx::IntSize size_increment) => ()
- SetWindowResizeAspectRatio(i32 window_id, Optional<Gfx::IntSize> resize_aspect_ratio) => ()
-
- EnableDisplayLink() =|
- DisableDisplayLink() =|
-
- GetGlobalCursorPosition() => (Gfx::IntPoint position)
-
- SetMouseAcceleration(float factor) => ()
- GetMouseAcceleration() => (float factor)
-
- SetScrollStepSize(u32 step_size) => ()
- GetScrollStepSize() => (u32 step_size)
-
- Pong() =|
-}
diff --git a/Services/WindowServer/WindowSwitcher.cpp b/Services/WindowServer/WindowSwitcher.cpp
deleted file mode 100644
index 803887c657..0000000000
--- a/Services/WindowServer/WindowSwitcher.cpp
+++ /dev/null
@@ -1,257 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <LibGfx/Bitmap.h>
-#include <LibGfx/Font.h>
-#include <LibGfx/StylePainter.h>
-#include <WindowServer/Compositor.h>
-#include <WindowServer/Event.h>
-#include <WindowServer/Screen.h>
-#include <WindowServer/WindowManager.h>
-#include <WindowServer/WindowSwitcher.h>
-
-namespace WindowServer {
-
-static WindowSwitcher* s_the;
-
-WindowSwitcher& WindowSwitcher::the()
-{
- ASSERT(s_the);
- return *s_the;
-}
-
-WindowSwitcher::WindowSwitcher()
-{
- s_the = this;
-}
-
-WindowSwitcher::~WindowSwitcher()
-{
-}
-
-void WindowSwitcher::set_visible(bool visible)
-{
- if (m_visible == visible)
- return;
- m_visible = visible;
- Compositor::the().invalidate_occlusions();
- if (m_switcher_window)
- m_switcher_window->set_visible(visible);
- if (!m_visible)
- return;
- refresh();
-}
-
-Window* WindowSwitcher::selected_window()
-{
- if (m_selected_index < 0 || m_selected_index >= static_cast<int>(m_windows.size()))
- return nullptr;
- return m_windows[m_selected_index].ptr();
-}
-
-void WindowSwitcher::event(Core::Event& event)
-{
- if (!static_cast<Event&>(event).is_mouse_event())
- return;
-
- auto& mouse_event = static_cast<MouseEvent&>(event);
- int new_hovered_index = -1;
- for (size_t i = 0; i < m_windows.size(); ++i) {
- auto item_rect = this->item_rect(i);
- if (item_rect.contains(mouse_event.position())) {
- new_hovered_index = i;
- break;
- }
- }
-
- if (mouse_event.type() == Event::MouseMove) {
- if (m_hovered_index != new_hovered_index) {
- m_hovered_index = new_hovered_index;
- redraw();
- }
- }
-
- if (new_hovered_index == -1)
- return;
-
- if (mouse_event.type() == Event::MouseDown)
- select_window_at_index(new_hovered_index);
-
- event.accept();
-}
-
-void WindowSwitcher::on_key_event(const KeyEvent& event)
-{
- if (event.type() == Event::KeyUp) {
- if (event.key() == Key_Logo) {
- if (auto* window = selected_window()) {
- window->set_minimized(false);
- WindowManager::the().move_to_front_and_make_active(*window);
- }
- WindowManager::the().set_highlight_window(nullptr);
- hide();
- }
- return;
- }
-
- if (event.key() == Key_LeftShift || event.key() == Key_RightShift)
- return;
- if (event.key() != Key_Tab) {
- WindowManager::the().set_highlight_window(nullptr);
- hide();
- return;
- }
- ASSERT(!m_windows.is_empty());
-
- int new_selected_index;
-
- if (!event.shift()) {
- new_selected_index = (m_selected_index + 1) % static_cast<int>(m_windows.size());
- } else {
- new_selected_index = (m_selected_index - 1) % static_cast<int>(m_windows.size());
- if (new_selected_index < 0)
- new_selected_index = static_cast<int>(m_windows.size()) - 1;
- }
- ASSERT(new_selected_index < static_cast<int>(m_windows.size()));
-
- select_window_at_index(new_selected_index);
-}
-
-void WindowSwitcher::select_window(Window& window)
-{
- for (size_t i = 0; i < m_windows.size(); ++i) {
- if (m_windows.at(i) == &window) {
- select_window_at_index(i);
- return;
- }
- }
-}
-
-void WindowSwitcher::select_window_at_index(int index)
-{
- m_selected_index = index;
- auto* highlight_window = m_windows.at(index).ptr();
- ASSERT(highlight_window);
- WindowManager::the().set_highlight_window(highlight_window);
- redraw();
-}
-
-void WindowSwitcher::redraw()
-{
- draw();
- Compositor::the().invalidate_screen(m_rect);
-}
-
-Gfx::IntRect WindowSwitcher::item_rect(int index) const
-{
- return {
- padding(),
- padding() + index * item_height(),
- m_rect.width() - padding() * 2,
- item_height()
- };
-}
-
-void WindowSwitcher::draw()
-{
- auto palette = WindowManager::the().palette();
- Gfx::Painter painter(*m_switcher_window->backing_store());
- painter.fill_rect({ {}, m_rect.size() }, palette.window());
- painter.draw_rect({ {}, m_rect.size() }, palette.threed_shadow2());
- for (size_t index = 0; index < m_windows.size(); ++index) {
- auto& window = *m_windows.at(index);
- auto item_rect = this->item_rect(index);
- Color text_color;
- Color rect_text_color;
- if (static_cast<int>(index) == m_selected_index) {
- painter.fill_rect(item_rect, palette.selection());
- text_color = palette.selection_text();
- rect_text_color = palette.threed_shadow1();
- } else {
- if (static_cast<int>(index) == m_hovered_index)
- Gfx::StylePainter::paint_button(painter, item_rect, palette, Gfx::ButtonStyle::CoolBar, false, true);
- text_color = palette.window_text();
- rect_text_color = palette.threed_shadow2();
- }
- item_rect.shrink(item_padding(), 0);
- Gfx::IntRect thumbnail_rect = { item_rect.location().translated(0, 5), { thumbnail_width(), thumbnail_height() } };
- if (window.backing_store()) {
- painter.draw_scaled_bitmap(thumbnail_rect, *window.backing_store(), window.backing_store()->rect());
- Gfx::StylePainter::paint_frame(painter, thumbnail_rect.inflated(4, 4), palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
- }
- Gfx::IntRect icon_rect = { thumbnail_rect.bottom_right().translated(-window.icon().width(), -window.icon().height()), { window.icon().width(), window.icon().height() } };
- painter.fill_rect(icon_rect, palette.window());
- painter.blit(icon_rect.location(), window.icon(), window.icon().rect());
- painter.draw_text(item_rect.translated(thumbnail_width() + 12, 0), window.title(), WindowManager::the().window_title_font(), Gfx::TextAlignment::CenterLeft, text_color);
- painter.draw_text(item_rect, window.rect().to_string(), Gfx::TextAlignment::CenterRight, rect_text_color);
- }
-}
-
-void WindowSwitcher::refresh()
-{
- auto& wm = WindowManager::the();
- const Window* selected_window = nullptr;
- if (m_selected_index > 0 && m_windows[m_selected_index])
- selected_window = m_windows[m_selected_index].ptr();
- if (!selected_window)
- selected_window = wm.highlight_window() ? wm.highlight_window() : wm.active_window();
- m_windows.clear();
- m_selected_index = 0;
- int window_count = 0;
- int longest_title_width = 0;
- wm.for_each_window_of_type_from_front_to_back(
- WindowType::Normal, [&](Window& window) {
- if (window.is_frameless())
- return IterationDecision::Continue;
- ++window_count;
- longest_title_width = max(longest_title_width, wm.font().width(window.title()));
- if (selected_window == &window)
- m_selected_index = m_windows.size();
- m_windows.append(window);
- return IterationDecision::Continue;
- },
- true);
- if (m_windows.is_empty()) {
- hide();
- return;
- }
- int space_for_window_rect = 180;
- m_rect.set_width(thumbnail_width() + longest_title_width + space_for_window_rect + padding() * 2 + item_padding() * 2);
- m_rect.set_height(window_count * item_height() + padding() * 2);
- m_rect.center_within(Screen::the().rect());
- if (!m_switcher_window)
- m_switcher_window = Window::construct(*this, WindowType::WindowSwitcher);
- m_switcher_window->set_rect(m_rect);
- redraw();
-}
-
-void WindowSwitcher::refresh_if_needed()
-{
- if (m_visible)
- refresh();
-}
-
-}
diff --git a/Services/WindowServer/WindowSwitcher.h b/Services/WindowServer/WindowSwitcher.h
deleted file mode 100644
index 8b52eea372..0000000000
--- a/Services/WindowServer/WindowSwitcher.h
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-#include <AK/Vector.h>
-#include <AK/WeakPtr.h>
-#include <LibCore/Object.h>
-#include <LibGfx/Forward.h>
-#include <LibGfx/Rect.h>
-
-namespace WindowServer {
-
-class KeyEvent;
-class Window;
-
-class WindowSwitcher final : public Core::Object {
- C_OBJECT(WindowSwitcher)
-public:
- static WindowSwitcher& the();
-
- WindowSwitcher();
- virtual ~WindowSwitcher() override;
-
- bool is_visible() const { return m_visible; }
- void set_visible(bool);
-
- void show() { set_visible(true); }
- void hide() { set_visible(false); }
-
- void on_key_event(const KeyEvent&);
-
- void refresh();
- void refresh_if_needed();
-
- void select_window(Window&);
-
-private:
- int thumbnail_width() const { return 40; }
- int thumbnail_height() const { return 40; }
- int item_height() const { return 10 + thumbnail_height(); }
- int padding() const { return 8; }
- int item_padding() const { return 8; }
-
- void draw();
- void redraw();
- void select_window_at_index(int index);
- Gfx::IntRect item_rect(int index) const;
- Window* selected_window();
-
- virtual void event(Core::Event&) override;
-
- RefPtr<Window> m_switcher_window;
- Gfx::IntRect m_rect;
- bool m_visible { false };
- Vector<WeakPtr<Window>> m_windows;
- int m_selected_index { 0 };
- int m_hovered_index { -1 };
-};
-
-}
diff --git a/Services/WindowServer/WindowType.h b/Services/WindowServer/WindowType.h
deleted file mode 100644
index a3c53007a1..0000000000
--- a/Services/WindowServer/WindowType.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#pragma once
-
-// Keep this in sync with GUI::WindowType.
-enum class WindowType {
- Invalid = 0,
- Normal,
- Menu,
- WindowSwitcher,
- Taskbar,
- Tooltip,
- Menubar,
- MenuApplet,
- Notification,
- Desktop,
-};
diff --git a/Services/WindowServer/main.cpp b/Services/WindowServer/main.cpp
deleted file mode 100644
index ca8d997f0d..0000000000
--- a/Services/WindowServer/main.cpp
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "AppletManager.h"
-#include "Compositor.h"
-#include "EventLoop.h"
-#include "Screen.h"
-#include "WindowManager.h"
-#include <AK/SharedBuffer.h>
-#include <LibCore/ConfigFile.h>
-#include <LibGfx/Palette.h>
-#include <LibGfx/SystemTheme.h>
-#include <signal.h>
-#include <stdio.h>
-#include <string.h>
-
-int main(int, char**)
-{
- if (pledge("stdio video thread shared_buffer accept rpath wpath cpath unix proc fattr sigaction", nullptr) < 0) {
- perror("pledge");
- return 1;
- }
-
- if (unveil("/res", "r") < 0) {
- perror("unveil /res");
- return 1;
- }
-
- if (unveil("/tmp", "cw") < 0) {
- perror("unveil /tmp cw");
- return 1;
- }
-
- if (unveil("/etc/WindowServer/WindowServer.ini", "rwc") < 0) {
- perror("unveil /etc/WindowServer/WindowServer.ini");
- return 1;
- }
-
- if (unveil("/dev", "rw") < 0) {
- perror("unveil /dev rw");
- return 1;
- }
-
- struct sigaction act;
- memset(&act, 0, sizeof(act));
- act.sa_flags = SA_NOCLDWAIT;
- act.sa_handler = SIG_IGN;
- int rc = sigaction(SIGCHLD, &act, nullptr);
- if (rc < 0) {
- perror("sigaction");
- return 1;
- }
-
- auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
- auto theme_name = wm_config->read_entry("Theme", "Name", "Default");
-
- auto theme = Gfx::load_system_theme(String::format("/res/themes/%s.ini", theme_name.characters()));
- ASSERT(theme);
- Gfx::set_system_theme(*theme);
- auto palette = Gfx::PaletteImpl::create_with_shared_buffer(*theme);
-
- WindowServer::EventLoop loop;
-
- if (pledge("stdio video thread shared_buffer accept rpath wpath cpath proc", nullptr) < 0) {
- perror("pledge");
- return 1;
- }
-
- WindowServer::Screen screen(wm_config->read_num_entry("Screen", "Width", 1024),
- wm_config->read_num_entry("Screen", "Height", 768));
- screen.set_acceleration_factor(atof(wm_config->read_entry("Mouse", "AccelerationFactor", "1.0").characters()));
- screen.set_scroll_step_size(wm_config->read_num_entry("Mouse", "ScrollStepSize", 4));
- WindowServer::Compositor::the();
- auto wm = WindowServer::WindowManager::construct(*palette);
- auto am = WindowServer::AppletManager::construct();
- auto mm = WindowServer::MenuManager::construct();
-
- if (unveil("/tmp", "") < 0) {
- perror("unveil /tmp");
- return 1;
- }
-
- if (unveil("/dev", "") < 0) {
- perror("unveil /dev");
- return 1;
- }
-
- if (unveil(nullptr, nullptr) < 0) {
- perror("unveil");
- return 1;
- }
-
- dbgln("Entering WindowServer main loop");
- loop.exec();
- ASSERT_NOT_REACHED();
-}