2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
#include "Screen.h"
|
|
|
|
|
#include "Compositor.h"
|
|
|
|
|
#include "Event.h"
|
|
|
|
|
#include "EventLoop.h"
|
|
|
|
|
#include "WindowManager.h"
|
2021-01-17 16:28:43 -03:00
|
|
|
#include <AK/Debug.h>
|
2020-07-04 12:22:23 -03:00
|
|
|
#include <Kernel/API/FB.h>
|
|
|
|
|
#include <Kernel/API/MousePacket.h>
|
2019-02-16 22:43:01 -02:00
|
|
|
#include <fcntl.h>
|
2020-01-18 19:01:46 -03:00
|
|
|
#include <stdio.h>
|
2019-02-16 22:43:01 -02:00
|
|
|
#include <sys/mman.h>
|
2019-05-24 14:32:46 -03:00
|
|
|
#include <unistd.h>
|
2018-10-10 10:12:38 -03:00
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
namespace WindowServer {
|
2018-10-10 10:12:38 -03:00
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
NonnullRefPtrVector<Screen, default_screen_count> Screen::s_screens;
|
2021-06-13 09:16:06 -03:00
|
|
|
Screen* Screen::s_main_screen { nullptr };
|
|
|
|
|
Gfx::IntRect Screen::s_bounding_screens_rect {};
|
2021-06-15 00:19:56 -03:00
|
|
|
ScreenLayout Screen::s_layout;
|
2021-06-18 22:21:30 -03:00
|
|
|
Vector<int, default_scale_factors_in_use_count> Screen::s_scale_factors_in_use;
|
2020-02-06 16:03:37 -03:00
|
|
|
|
2021-06-26 14:04:01 -03:00
|
|
|
struct ScreenFBData {
|
|
|
|
|
Vector<FBRect, 32> pending_flush_rects;
|
|
|
|
|
bool too_many_pending_flush_rects { false };
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
ScreenInput& ScreenInput::the()
|
2018-10-10 10:12:38 -03:00
|
|
|
{
|
2021-06-13 09:16:06 -03:00
|
|
|
static ScreenInput s_the;
|
|
|
|
|
return s_the;
|
2018-10-10 10:12:38 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
Screen& ScreenInput::cursor_location_screen()
|
2018-10-10 10:12:38 -03:00
|
|
|
{
|
2021-06-13 09:16:06 -03:00
|
|
|
auto* screen = Screen::find_by_location(m_cursor_location);
|
|
|
|
|
VERIFY(screen);
|
|
|
|
|
return *screen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Screen& ScreenInput::cursor_location_screen() const
|
|
|
|
|
{
|
|
|
|
|
auto* screen = Screen::find_by_location(m_cursor_location);
|
|
|
|
|
VERIFY(screen);
|
|
|
|
|
return *screen;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 00:19:56 -03:00
|
|
|
bool Screen::apply_layout(ScreenLayout&& screen_layout, String& error_msg)
|
2021-06-13 09:16:06 -03:00
|
|
|
{
|
2021-06-15 00:19:56 -03:00
|
|
|
if (!screen_layout.is_valid(&error_msg))
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
if (screen_layout == s_layout)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
bool place_cursor_on_main_screen = find_by_location(ScreenInput::the().cursor_location()) == nullptr;
|
|
|
|
|
|
|
|
|
|
HashMap<size_t, size_t> current_to_new_indices_map;
|
|
|
|
|
HashMap<size_t, size_t> new_to_current_indices_map;
|
|
|
|
|
HashMap<size_t, NonnullRefPtr<Screen>> devices_no_longer_used;
|
|
|
|
|
for (size_t i = 0; i < s_layout.screens.size(); i++) {
|
|
|
|
|
auto& screen = s_layout.screens[i];
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (size_t j = 0; j < screen_layout.screens.size(); j++) {
|
|
|
|
|
auto& new_screen = screen_layout.screens[j];
|
|
|
|
|
if (new_screen.device == screen.device) {
|
|
|
|
|
current_to_new_indices_map.set(i, j);
|
|
|
|
|
new_to_current_indices_map.set(j, i);
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
|
devices_no_longer_used.set(i, s_screens[i]);
|
|
|
|
|
}
|
|
|
|
|
HashMap<Screen*, size_t> screens_with_resolution_change;
|
|
|
|
|
HashMap<Screen*, size_t> screens_with_scale_change;
|
|
|
|
|
for (auto& it : current_to_new_indices_map) {
|
|
|
|
|
auto& screen = s_layout.screens[it.key];
|
|
|
|
|
auto& new_screen = screen_layout.screens[it.value];
|
|
|
|
|
if (screen.resolution != new_screen.resolution)
|
|
|
|
|
screens_with_resolution_change.set(&s_screens[it.key], it.value);
|
|
|
|
|
if (screen.scale_factor != new_screen.scale_factor)
|
|
|
|
|
screens_with_scale_change.set(&s_screens[it.key], it.value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 00:19:56 -03:00
|
|
|
auto screens_backup = move(s_screens);
|
|
|
|
|
auto layout_backup = move(s_layout);
|
2021-07-18 13:24:41 -03:00
|
|
|
|
|
|
|
|
for (auto& it : screens_with_resolution_change) {
|
|
|
|
|
auto& existing_screen = *it.key;
|
|
|
|
|
dbgln("Closing device {} in preparation for resolution change", layout_backup.screens[existing_screen.index()].device);
|
|
|
|
|
existing_screen.close_device();
|
|
|
|
|
}
|
2021-06-15 00:19:56 -03:00
|
|
|
|
|
|
|
|
AK::ArmedScopeGuard rollback([&] {
|
|
|
|
|
for (auto& screen : s_screens)
|
|
|
|
|
screen.close_device();
|
|
|
|
|
s_screens = move(screens_backup);
|
|
|
|
|
s_layout = move(layout_backup);
|
2021-07-18 13:24:41 -03:00
|
|
|
for (size_t i = 0; i < s_screens.size(); i++) {
|
|
|
|
|
auto& old_screen = s_screens[i];
|
|
|
|
|
// Restore the original screen index in case it changed
|
|
|
|
|
old_screen.set_index(i);
|
|
|
|
|
if (i == s_layout.main_screen_index)
|
|
|
|
|
old_screen.make_main_screen();
|
|
|
|
|
bool changed_scale = screens_with_scale_change.contains(&old_screen);
|
|
|
|
|
if (screens_with_resolution_change.contains(&old_screen)) {
|
|
|
|
|
if (old_screen.open_device()) {
|
|
|
|
|
// The resolution was changed, so we also implicitly applied the new scale factor
|
|
|
|
|
changed_scale = false;
|
|
|
|
|
} else {
|
|
|
|
|
// Don't set error_msg here, it should already be set
|
|
|
|
|
dbgln("Rolling back screen layout failed: could not open device");
|
|
|
|
|
}
|
2021-06-15 00:19:56 -03:00
|
|
|
}
|
2021-07-18 13:24:41 -03:00
|
|
|
|
|
|
|
|
old_screen.update_virtual_rect();
|
|
|
|
|
if (changed_scale)
|
|
|
|
|
old_screen.scale_factor_changed();
|
2021-06-15 00:19:56 -03:00
|
|
|
}
|
|
|
|
|
update_bounding_rect();
|
|
|
|
|
});
|
|
|
|
|
s_layout = move(screen_layout);
|
|
|
|
|
for (size_t index = 0; index < s_layout.screens.size(); index++) {
|
2021-07-18 13:24:41 -03:00
|
|
|
Screen* screen;
|
|
|
|
|
bool need_to_open_device;
|
|
|
|
|
if (auto it = new_to_current_indices_map.find(index); it != new_to_current_indices_map.end()) {
|
|
|
|
|
// Re-use the existing screen instance
|
|
|
|
|
screen = &screens_backup[it->value];
|
|
|
|
|
s_screens.append(*screen);
|
|
|
|
|
screen->set_index(index);
|
|
|
|
|
|
|
|
|
|
need_to_open_device = screens_with_resolution_change.contains(screen);
|
|
|
|
|
} else {
|
|
|
|
|
screen = WindowServer::Screen::create(index);
|
|
|
|
|
if (!screen) {
|
|
|
|
|
error_msg = String::formatted("Error creating screen #{}", index);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
need_to_open_device = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (need_to_open_device && !screen->open_device()) {
|
|
|
|
|
error_msg = String::formatted("Error opening device for screen #{}", index);
|
2021-06-15 00:19:56 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
screen->update_virtual_rect();
|
|
|
|
|
if (!need_to_open_device && screens_with_scale_change.contains(screen))
|
|
|
|
|
screen->scale_factor_changed();
|
|
|
|
|
|
|
|
|
|
VERIFY(screen);
|
|
|
|
|
VERIFY(index == screen->index());
|
|
|
|
|
|
2021-06-15 00:19:56 -03:00
|
|
|
if (s_layout.main_screen_index == index)
|
|
|
|
|
screen->make_main_screen();
|
2019-08-18 01:32:14 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-15 00:19:56 -03:00
|
|
|
rollback.disarm();
|
2021-07-18 13:24:41 -03:00
|
|
|
|
|
|
|
|
if (place_cursor_on_main_screen) {
|
|
|
|
|
ScreenInput::the().set_cursor_location(Screen::main().rect().center());
|
|
|
|
|
} else {
|
|
|
|
|
auto cursor_location = ScreenInput::the().cursor_location();
|
|
|
|
|
if (!find_by_location(cursor_location)) {
|
|
|
|
|
// Cursor is off screen, try to find the closest location on another screen
|
|
|
|
|
float closest_distance = 0;
|
|
|
|
|
Optional<Gfx::IntPoint> closest_point;
|
|
|
|
|
for (auto& screen : s_screens) {
|
|
|
|
|
auto closest_point_on_screen_rect = screen.rect().closest_to(cursor_location);
|
|
|
|
|
auto distance = closest_point_on_screen_rect.distance_from(cursor_location);
|
|
|
|
|
if (!closest_point.has_value() || distance < closest_distance) {
|
|
|
|
|
closest_distance = distance;
|
|
|
|
|
closest_point = closest_point_on_screen_rect;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ScreenInput::the().set_cursor_location(closest_point.value()); // We should always have one
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 00:19:56 -03:00
|
|
|
update_bounding_rect();
|
2021-06-18 22:21:30 -03:00
|
|
|
update_scale_factors_in_use();
|
2021-06-15 00:19:56 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 22:21:30 -03:00
|
|
|
void Screen::update_scale_factors_in_use()
|
|
|
|
|
{
|
|
|
|
|
s_scale_factors_in_use.clear();
|
|
|
|
|
for_each([&](auto& screen) {
|
|
|
|
|
auto scale_factor = screen.scale_factor();
|
|
|
|
|
// The This doesn't have to be extremely efficient as this
|
|
|
|
|
// code is only run when we start up or the screen configuration
|
|
|
|
|
// changes. But using a vector allows for efficient iteration,
|
|
|
|
|
// which is the most common use case.
|
|
|
|
|
if (!s_scale_factors_in_use.contains_slow(scale_factor))
|
|
|
|
|
s_scale_factors_in_use.append(scale_factor);
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
Screen::Screen(size_t screen_index)
|
|
|
|
|
: m_index(screen_index)
|
2021-06-26 14:04:01 -03:00
|
|
|
, m_framebuffer_data(adopt_own(*new ScreenFBData()))
|
2021-07-18 13:24:41 -03:00
|
|
|
, m_compositor_screen_data(Compositor::create_screen_data({}))
|
2021-06-15 00:19:56 -03:00
|
|
|
{
|
2021-07-18 13:24:41 -03:00
|
|
|
update_virtual_rect();
|
2021-06-15 00:19:56 -03:00
|
|
|
open_device();
|
2019-02-17 09:12:59 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
Screen::~Screen()
|
2019-02-17 09:12:59 -03:00
|
|
|
{
|
2021-06-15 00:19:56 -03:00
|
|
|
close_device();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Screen::open_device()
|
|
|
|
|
{
|
|
|
|
|
close_device();
|
2021-07-18 13:24:41 -03:00
|
|
|
auto& info = screen_layout_info();
|
|
|
|
|
m_framebuffer_fd = open(info.device.characters(), O_RDWR | O_CLOEXEC);
|
2021-06-15 00:19:56 -03:00
|
|
|
if (m_framebuffer_fd < 0) {
|
2021-07-18 13:24:41 -03:00
|
|
|
perror(String::formatted("failed to open {}", info.device).characters());
|
2021-06-15 00:19:56 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_can_set_buffer = (fb_set_buffer(m_framebuffer_fd, 0) == 0);
|
2021-06-26 14:04:01 -03:00
|
|
|
m_can_device_flush_buffers = true; // If the device can't do it we revert to false
|
2021-06-15 00:19:56 -03:00
|
|
|
set_resolution(true);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Screen::close_device()
|
|
|
|
|
{
|
|
|
|
|
if (m_framebuffer_fd >= 0) {
|
|
|
|
|
close(m_framebuffer_fd);
|
|
|
|
|
m_framebuffer_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
if (m_framebuffer) {
|
|
|
|
|
int rc = munmap(m_framebuffer, m_size_in_bytes);
|
|
|
|
|
VERIFY(rc == 0);
|
|
|
|
|
|
|
|
|
|
m_framebuffer = nullptr;
|
|
|
|
|
m_size_in_bytes = 0;
|
|
|
|
|
}
|
2019-02-17 09:12:59 -03:00
|
|
|
}
|
|
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
void Screen::update_virtual_rect()
|
|
|
|
|
{
|
|
|
|
|
auto& screen_info = screen_layout_info();
|
|
|
|
|
m_virtual_rect = { screen_info.location, { screen_info.resolution.width() / screen_info.scale_factor, screen_info.resolution.height() / screen_info.scale_factor } };
|
|
|
|
|
dbgln("update_virtual_rect for screen #{}: {}", index(), m_virtual_rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Screen::scale_factor_changed()
|
|
|
|
|
{
|
|
|
|
|
// Flush rects are affected by the screen factor
|
|
|
|
|
constrain_pending_flush_rects();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
void Screen::init()
|
|
|
|
|
{
|
2021-06-15 00:19:56 -03:00
|
|
|
set_resolution(true);
|
2021-06-13 09:16:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Screen& Screen::closest_to_rect(const Gfx::IntRect& rect)
|
2019-02-17 09:12:59 -03:00
|
|
|
{
|
2021-06-13 09:16:06 -03:00
|
|
|
Screen* best_screen = nullptr;
|
|
|
|
|
int best_area = 0;
|
|
|
|
|
for (auto& screen : s_screens) {
|
|
|
|
|
auto r = screen.rect().intersected(rect);
|
|
|
|
|
int area = r.width() * r.height();
|
|
|
|
|
if (!best_screen || area > best_area) {
|
|
|
|
|
best_screen = &screen;
|
|
|
|
|
best_area = area;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!best_screen) {
|
|
|
|
|
// TODO: try to find the best screen in close proximity
|
|
|
|
|
best_screen = &Screen::main();
|
|
|
|
|
}
|
|
|
|
|
return *best_screen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Screen& Screen::closest_to_location(const Gfx::IntPoint& point)
|
|
|
|
|
{
|
|
|
|
|
for (auto& screen : s_screens) {
|
|
|
|
|
if (screen.rect().contains(point))
|
|
|
|
|
return screen;
|
|
|
|
|
}
|
|
|
|
|
// TODO: guess based on how close the point is to the next screen rectangle
|
|
|
|
|
return Screen::main();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Screen::update_bounding_rect()
|
|
|
|
|
{
|
|
|
|
|
if (!s_screens.is_empty()) {
|
|
|
|
|
s_bounding_screens_rect = s_screens[0].rect();
|
|
|
|
|
for (size_t i = 1; i < s_screens.size(); i++)
|
|
|
|
|
s_bounding_screens_rect = s_bounding_screens_rect.united(s_screens[i].rect());
|
|
|
|
|
} else {
|
|
|
|
|
s_bounding_screens_rect = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 00:19:56 -03:00
|
|
|
bool Screen::set_resolution(bool initial)
|
2021-06-13 09:16:06 -03:00
|
|
|
{
|
|
|
|
|
// Remember the screen that the cursor is on. Make sure it stays on the same screen if we change its resolution...
|
2021-06-15 00:19:56 -03:00
|
|
|
Screen* screen_with_cursor = nullptr;
|
|
|
|
|
if (!initial)
|
|
|
|
|
screen_with_cursor = &ScreenInput::the().cursor_location_screen();
|
2021-01-15 16:53:53 -03:00
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
auto& info = screen_layout_info();
|
|
|
|
|
FBResolution physical_resolution { 0, (unsigned)info.resolution.width(), (unsigned)info.resolution.height() };
|
WindowServer: Make HighDPI aware
Almost all logic stays in "logical" (unscaled coordinates), which
means the patch is small and things like DnD, window moving and
resizing, menu handling, menuapplets, etc all work without changes.
Screen knows about phyiscal coordinates and mouse handling internally is
in physical coordinates (so that two 1 pixel movements in succession can
translate to one 1 logical coordinate mouse movement -- only a single
event is sent in this case, on the 2nd moved pixel).
Compositor also knows about physical pixels for its backbuffers. This is
a temporary state -- in a follow-up, I'll try to let Bitmaps know about
their intrinsic scale, then Compositor won't have to know about pixels
any longer. Most of Compositor's logic stays in view units, just
blitting to and from back buffers and the cursor save buffer has to be
done in pixels. The back buffer Painter gets a scale applied which
transparently handles all drawing. (But since the backbuffer and cursor
save buffer are also HighDPI, they currently need to be drawn using a
hack temporary unscaled Painter object. This will also go away once
Bitmaps know about their intrinsic scale.)
With this, editing WindowServer.ini to say
Width=800
Height=600
ScaleFactor=2
and booting brings up a fully-functional HighDPI UI.
(Except for minimizing windows, which will crash the window server
until #4932 is merged. And I didn't test the window switcher since the
win-tab shortcut doesn't work on my system.) It's all pixel-scaled,
but it looks pretty decent :^)
2021-01-15 12:58:55 -03:00
|
|
|
int rc = fb_set_resolution(m_framebuffer_fd, &physical_resolution);
|
2021-06-13 09:16:06 -03:00
|
|
|
dbgln_if(WSSCREEN_DEBUG, "Screen #{}: fb_set_resolution() - return code {}", index(), rc);
|
2021-01-17 16:28:43 -03:00
|
|
|
|
2021-06-15 00:19:56 -03:00
|
|
|
auto on_change_resolution = [&]() {
|
2021-07-18 13:24:41 -03:00
|
|
|
if (initial || physical_resolution.width != (unsigned)info.resolution.width() || physical_resolution.height != (unsigned)info.resolution.height()) {
|
2021-06-15 00:19:56 -03:00
|
|
|
if (m_framebuffer) {
|
|
|
|
|
size_t previous_size_in_bytes = m_size_in_bytes;
|
|
|
|
|
int rc = munmap(m_framebuffer, previous_size_in_bytes);
|
|
|
|
|
VERIFY(rc == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int rc = fb_get_size_in_bytes(m_framebuffer_fd, &m_size_in_bytes);
|
|
|
|
|
VERIFY(rc == 0);
|
|
|
|
|
|
|
|
|
|
m_framebuffer = (Gfx::RGBA32*)mmap(nullptr, m_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_framebuffer_fd, 0);
|
|
|
|
|
VERIFY(m_framebuffer && m_framebuffer != (void*)-1);
|
2021-07-03 23:36:16 -03:00
|
|
|
|
|
|
|
|
if (m_can_set_buffer) {
|
|
|
|
|
unsigned buffer_offset = 0;
|
|
|
|
|
rc = fb_get_buffer_offset(m_framebuffer_fd, 1, &buffer_offset);
|
|
|
|
|
if (rc == 0) {
|
|
|
|
|
m_back_buffer_offset = buffer_offset;
|
|
|
|
|
} else {
|
|
|
|
|
// fall back to assuming the second buffer starts right after the last line of the first
|
|
|
|
|
m_back_buffer_offset = physical_resolution.pitch * physical_resolution.height;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_back_buffer_offset = 0;
|
|
|
|
|
}
|
2021-06-15 00:19:56 -03:00
|
|
|
}
|
|
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
info.resolution = { physical_resolution.width, physical_resolution.height };
|
2021-06-15 00:19:56 -03:00
|
|
|
m_pitch = physical_resolution.pitch;
|
2021-07-18 13:24:41 -03:00
|
|
|
|
|
|
|
|
update_virtual_rect();
|
|
|
|
|
|
|
|
|
|
// Since pending flush rects are affected by the scale factor
|
|
|
|
|
// update even if only the scale factor changed
|
|
|
|
|
constrain_pending_flush_rects();
|
|
|
|
|
|
2021-06-15 00:19:56 -03:00
|
|
|
if (this == screen_with_cursor) {
|
|
|
|
|
auto& screen_input = ScreenInput::the();
|
|
|
|
|
screen_input.set_cursor_location(screen_input.cursor_location().constrained(rect()));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-27 12:09:41 -03:00
|
|
|
if (rc == 0) {
|
2021-06-15 00:19:56 -03:00
|
|
|
on_change_resolution();
|
2020-02-27 12:09:41 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (rc == -1) {
|
2021-06-13 09:16:06 -03:00
|
|
|
int err = errno;
|
2021-07-18 13:24:41 -03:00
|
|
|
dbgln("Screen #{}: Failed to set resolution {}: {}", index(), info.resolution, strerror(err));
|
2021-06-15 00:19:56 -03:00
|
|
|
on_change_resolution();
|
2020-02-27 12:09:41 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-18 01:32:14 -03:00
|
|
|
}
|
2019-02-16 22:43:01 -02:00
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
void Screen::set_buffer(int index)
|
2019-08-18 01:32:14 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(m_can_set_buffer);
|
2019-08-18 01:32:14 -03:00
|
|
|
int rc = fb_set_buffer(m_framebuffer_fd, index);
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(rc == 0);
|
2019-08-18 01:32:14 -03:00
|
|
|
}
|
|
|
|
|
|
2021-07-03 23:36:16 -03:00
|
|
|
size_t Screen::buffer_offset(int index) const
|
|
|
|
|
{
|
|
|
|
|
if (index == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
if (index == 1)
|
|
|
|
|
return m_back_buffer_offset;
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
void ScreenInput::set_acceleration_factor(double factor)
|
2020-12-29 18:32:54 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(factor >= mouse_accel_min && factor <= mouse_accel_max);
|
2020-12-29 18:32:54 -03:00
|
|
|
m_acceleration_factor = factor;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
void ScreenInput::set_scroll_step_size(unsigned step_size)
|
2020-12-29 18:32:54 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(step_size >= scroll_step_size_min);
|
2020-12-29 18:32:54 -03:00
|
|
|
m_scroll_step_size = step_size;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
void ScreenInput::on_receive_mouse_data(const MousePacket& packet)
|
2019-01-11 00:52:09 -02:00
|
|
|
{
|
2021-06-13 09:16:06 -03:00
|
|
|
auto& current_screen = cursor_location_screen();
|
|
|
|
|
auto prev_location = m_cursor_location;
|
2020-02-03 21:38:50 -03:00
|
|
|
if (packet.is_relative) {
|
2021-06-13 09:16:06 -03:00
|
|
|
m_cursor_location.translate_by(packet.x * m_acceleration_factor, packet.y * m_acceleration_factor);
|
|
|
|
|
dbgln_if(WSSCREEN_DEBUG, "Screen: New Relative mouse point @ {}", m_cursor_location);
|
2020-02-03 21:38:50 -03:00
|
|
|
} else {
|
2021-06-23 15:35:33 -03:00
|
|
|
m_cursor_location = { packet.x * current_screen.width() / 0xffff, packet.y * current_screen.height() / 0xffff };
|
2021-06-13 09:16:06 -03:00
|
|
|
dbgln_if(WSSCREEN_DEBUG, "Screen: New Absolute mouse point @ {}", m_cursor_location);
|
2020-02-03 21:38:50 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
auto* moved_to_screen = Screen::find_by_location(m_cursor_location);
|
|
|
|
|
if (!moved_to_screen) {
|
|
|
|
|
m_cursor_location = m_cursor_location.constrained(current_screen.rect());
|
|
|
|
|
moved_to_screen = ¤t_screen;
|
|
|
|
|
}
|
2020-02-03 13:30:35 -03:00
|
|
|
|
|
|
|
|
unsigned buttons = packet.buttons;
|
2019-03-05 10:11:46 -03:00
|
|
|
unsigned prev_buttons = m_mouse_button_state;
|
|
|
|
|
m_mouse_button_state = buttons;
|
|
|
|
|
unsigned changed_buttons = prev_buttons ^ buttons;
|
2019-06-07 06:47:19 -03:00
|
|
|
auto post_mousedown_or_mouseup_if_needed = [&](MouseButton button) {
|
2019-03-05 10:11:46 -03:00
|
|
|
if (!(changed_buttons & (unsigned)button))
|
|
|
|
|
return;
|
2021-06-13 09:16:06 -03:00
|
|
|
auto message = make<MouseEvent>(buttons & (unsigned)button ? Event::MouseDown : Event::MouseUp, m_cursor_location, buttons, button, m_modifiers);
|
2020-02-06 16:03:37 -03:00
|
|
|
Core::EventLoop::current().post_event(WindowManager::the(), move(message));
|
2019-03-05 10:11:46 -03:00
|
|
|
};
|
|
|
|
|
post_mousedown_or_mouseup_if_needed(MouseButton::Left);
|
|
|
|
|
post_mousedown_or_mouseup_if_needed(MouseButton::Right);
|
|
|
|
|
post_mousedown_or_mouseup_if_needed(MouseButton::Middle);
|
2020-05-02 17:07:43 -03:00
|
|
|
post_mousedown_or_mouseup_if_needed(MouseButton::Back);
|
|
|
|
|
post_mousedown_or_mouseup_if_needed(MouseButton::Forward);
|
2021-06-13 09:16:06 -03:00
|
|
|
if (m_cursor_location != prev_location) {
|
|
|
|
|
auto message = make<MouseEvent>(Event::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
|
2021-01-09 07:01:41 -03:00
|
|
|
if (WindowManager::the().dnd_client())
|
|
|
|
|
message->set_mime_data(WindowManager::the().dnd_mime_data());
|
2020-02-06 16:03:37 -03:00
|
|
|
Core::EventLoop::current().post_event(WindowManager::the(), move(message));
|
2019-03-05 09:59:44 -03:00
|
|
|
}
|
2019-05-06 22:56:54 -03:00
|
|
|
|
2020-02-03 13:30:35 -03:00
|
|
|
if (packet.z) {
|
2021-06-13 09:16:06 -03:00
|
|
|
auto message = make<MouseEvent>(Event::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, packet.z * m_scroll_step_size);
|
2020-02-06 16:03:37 -03:00
|
|
|
Core::EventLoop::current().post_event(WindowManager::the(), move(message));
|
2019-05-13 14:52:57 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
if (m_cursor_location != prev_location)
|
2020-02-06 16:03:37 -03:00
|
|
|
Compositor::the().invalidate_cursor();
|
2019-01-11 00:52:09 -02:00
|
|
|
}
|
2019-01-11 01:40:05 -02:00
|
|
|
|
2021-06-13 09:16:06 -03:00
|
|
|
void ScreenInput::on_receive_keyboard_data(::KeyEvent kernel_event)
|
2019-01-11 01:40:05 -02:00
|
|
|
{
|
2019-03-08 13:53:02 -03:00
|
|
|
m_modifiers = kernel_event.modifiers();
|
2020-06-13 08:02:00 -03:00
|
|
|
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);
|
2020-02-06 16:03:37 -03:00
|
|
|
Core::EventLoop::current().post_event(WindowManager::the(), move(message));
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
void Screen::constrain_pending_flush_rects()
|
|
|
|
|
{
|
|
|
|
|
auto& fb_data = *m_framebuffer_data;
|
|
|
|
|
if (fb_data.pending_flush_rects.is_empty())
|
|
|
|
|
return;
|
|
|
|
|
Gfx::IntRect screen_rect({}, rect().size());
|
|
|
|
|
Gfx::DisjointRectSet rects;
|
|
|
|
|
for (auto& fb_rect : fb_data.pending_flush_rects) {
|
|
|
|
|
Gfx::IntRect rect { (int)fb_rect.x, (int)fb_rect.y, (int)fb_rect.width, (int)fb_rect.height };
|
|
|
|
|
auto intersected_rect = rect.intersected(screen_rect);
|
|
|
|
|
if (!intersected_rect.is_empty())
|
|
|
|
|
rects.add(intersected_rect);
|
|
|
|
|
}
|
|
|
|
|
fb_data.pending_flush_rects.clear_with_capacity();
|
2021-07-22 03:33:16 -03:00
|
|
|
for (auto const& rect : rects.rects()) {
|
2021-07-18 13:24:41 -03:00
|
|
|
fb_data.pending_flush_rects.append({
|
2021-07-22 03:33:16 -03:00
|
|
|
.x = (unsigned)rect.x(),
|
|
|
|
|
.y = (unsigned)rect.y(),
|
|
|
|
|
.width = (unsigned)rect.width(),
|
|
|
|
|
.height = (unsigned)rect.height(),
|
2021-07-18 13:24:41 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 14:04:01 -03:00
|
|
|
void Screen::queue_flush_display_rect(Gfx::IntRect const& flush_region)
|
2021-06-12 09:46:54 -03:00
|
|
|
{
|
2021-06-26 14:04:01 -03:00
|
|
|
// NOTE: we don't scale until in Screen::flush_display so that when
|
|
|
|
|
// there are too many rectangles that we end up throwing away, we didn't
|
|
|
|
|
// waste accounting for scale factor!
|
|
|
|
|
auto& fb_data = *m_framebuffer_data;
|
|
|
|
|
if (fb_data.too_many_pending_flush_rects) {
|
|
|
|
|
// We already have too many, just make sure we extend it if needed
|
|
|
|
|
VERIFY(!fb_data.pending_flush_rects.is_empty());
|
|
|
|
|
if (fb_data.pending_flush_rects.size() == 1) {
|
|
|
|
|
auto& union_rect = fb_data.pending_flush_rects[0];
|
|
|
|
|
auto new_union = flush_region.united(Gfx::IntRect((int)union_rect.x, (int)union_rect.y, (int)union_rect.width, (int)union_rect.height));
|
|
|
|
|
union_rect.x = new_union.left();
|
|
|
|
|
union_rect.y = new_union.top();
|
|
|
|
|
union_rect.width = new_union.width();
|
|
|
|
|
union_rect.height = new_union.height();
|
|
|
|
|
} else {
|
|
|
|
|
// Convert all the rectangles into one union
|
|
|
|
|
auto new_union = flush_region;
|
|
|
|
|
for (auto& flush_rect : fb_data.pending_flush_rects)
|
|
|
|
|
new_union = new_union.united(Gfx::IntRect((int)flush_rect.x, (int)flush_rect.y, (int)flush_rect.width, (int)flush_rect.height));
|
|
|
|
|
fb_data.pending_flush_rects.resize(1, true);
|
|
|
|
|
auto& union_rect = fb_data.pending_flush_rects[0];
|
|
|
|
|
union_rect.x = new_union.left();
|
|
|
|
|
union_rect.y = new_union.top();
|
|
|
|
|
union_rect.width = new_union.width();
|
|
|
|
|
union_rect.height = new_union.height();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
VERIFY(fb_data.pending_flush_rects.size() < fb_data.pending_flush_rects.capacity());
|
|
|
|
|
fb_data.pending_flush_rects.append({ (unsigned)flush_region.left(),
|
|
|
|
|
(unsigned)flush_region.top(),
|
|
|
|
|
(unsigned)flush_region.width(),
|
|
|
|
|
(unsigned)flush_region.height() });
|
|
|
|
|
if (fb_data.pending_flush_rects.size() == fb_data.pending_flush_rects.capacity()) {
|
|
|
|
|
// If we get one more rectangle then we need to convert it to a single union rectangle
|
|
|
|
|
fb_data.too_many_pending_flush_rects = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-03 15:43:35 -03:00
|
|
|
void Screen::flush_display(int buffer_index)
|
2021-06-26 14:04:01 -03:00
|
|
|
{
|
|
|
|
|
VERIFY(m_can_device_flush_buffers);
|
|
|
|
|
auto& fb_data = *m_framebuffer_data;
|
|
|
|
|
if (fb_data.pending_flush_rects.is_empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Now that we have a final set of rects, apply the scale factor
|
|
|
|
|
auto scale_factor = this->scale_factor();
|
|
|
|
|
for (auto& flush_rect : fb_data.pending_flush_rects) {
|
2021-07-18 13:24:41 -03:00
|
|
|
VERIFY(Gfx::IntRect({}, m_virtual_rect.size()).contains({ (int)flush_rect.x, (int)flush_rect.y, (int)flush_rect.width, (int)flush_rect.height }));
|
2021-06-26 14:04:01 -03:00
|
|
|
flush_rect.x *= scale_factor;
|
|
|
|
|
flush_rect.y *= scale_factor;
|
|
|
|
|
flush_rect.width *= scale_factor;
|
|
|
|
|
flush_rect.height *= scale_factor;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-03 15:43:35 -03:00
|
|
|
if (fb_flush_buffers(m_framebuffer_fd, buffer_index, fb_data.pending_flush_rects.data(), (unsigned)fb_data.pending_flush_rects.size()) < 0) {
|
2021-06-26 14:04:01 -03:00
|
|
|
int err = errno;
|
|
|
|
|
if (err == ENOTSUP)
|
|
|
|
|
m_can_device_flush_buffers = false;
|
|
|
|
|
else
|
|
|
|
|
dbgln("Screen #{}: Error ({}) flushing display: {}", index(), err, strerror(err));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fb_data.too_many_pending_flush_rects = false;
|
|
|
|
|
fb_data.pending_flush_rects.clear_with_capacity();
|
2021-06-12 09:46:54 -03:00
|
|
|
}
|
2021-07-10 14:28:26 -03:00
|
|
|
|
|
|
|
|
void Screen::flush_display_front_buffer(int front_buffer_index, Gfx::IntRect& rect)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(m_can_device_flush_buffers);
|
|
|
|
|
auto scale_factor = this->scale_factor();
|
|
|
|
|
FBRect flush_rect {
|
2021-07-12 14:14:47 -03:00
|
|
|
.x = (unsigned)(rect.x() * scale_factor),
|
|
|
|
|
.y = (unsigned)(rect.y() * scale_factor),
|
|
|
|
|
.width = (unsigned)(rect.width() * scale_factor),
|
|
|
|
|
.height = (unsigned)(rect.height() * scale_factor)
|
2021-07-10 14:28:26 -03:00
|
|
|
};
|
|
|
|
|
|
2021-07-18 13:24:41 -03:00
|
|
|
VERIFY(Gfx::IntRect({}, m_virtual_rect.size()).contains(rect));
|
2021-07-10 14:28:26 -03:00
|
|
|
if (fb_flush_buffers(m_framebuffer_fd, front_buffer_index, &flush_rect, 1) < 0) {
|
|
|
|
|
int err = errno;
|
|
|
|
|
if (err == ENOTSUP)
|
|
|
|
|
m_can_device_flush_buffers = false;
|
|
|
|
|
else
|
|
|
|
|
dbgln("Screen #{}: Error ({}) flushing display front buffer: {}", index(), err, strerror(err));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 01:40:05 -02:00
|
|
|
}
|