2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-06-07 12:47:19 +03:00
|
|
|
#include "WSScreen.h"
|
2019-05-24 20:32:46 +03:00
|
|
|
#include "WSCompositor.h"
|
2019-04-14 06:23:37 +03:00
|
|
|
#include "WSEvent.h"
|
2019-05-24 20:32:46 +03:00
|
|
|
#include "WSEventLoop.h"
|
2019-01-16 18:03:50 +03:00
|
|
|
#include "WSWindowManager.h"
|
2019-08-18 07:32:14 +03:00
|
|
|
#include <Kernel/FB.h>
|
2020-02-03 19:30:35 +03:00
|
|
|
#include <Kernel/MousePacket.h>
|
2019-02-17 03:43:01 +03:00
|
|
|
#include <fcntl.h>
|
2020-01-19 01:01:46 +03:00
|
|
|
#include <stdio.h>
|
2019-02-17 03:43:01 +03:00
|
|
|
#include <sys/mman.h>
|
2019-05-24 20:32:46 +03:00
|
|
|
#include <unistd.h>
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-01-16 18:03:50 +03:00
|
|
|
static WSScreen* s_the;
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-01-16 18:03:50 +03:00
|
|
|
WSScreen& WSScreen::the()
|
2018-10-10 16:12:38 +03:00
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2019-08-18 07:32:14 +03:00
|
|
|
WSScreen::WSScreen(unsigned desired_width, unsigned desired_height)
|
2018-10-10 16:12:38 +03:00
|
|
|
{
|
|
|
|
ASSERT(!s_the);
|
|
|
|
s_the = this;
|
2019-11-13 16:00:59 +03:00
|
|
|
m_framebuffer_fd = open("/dev/fb0", O_RDWR | O_CLOEXEC);
|
2020-01-19 01:01:46 +03:00
|
|
|
if (m_framebuffer_fd < 0) {
|
|
|
|
perror("failed to open /dev/fb0");
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2019-02-17 03:43:01 +03:00
|
|
|
|
2019-08-18 07:32:14 +03:00
|
|
|
if (fb_set_buffer(m_framebuffer_fd, 0) == 0) {
|
|
|
|
m_can_set_buffer = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_resolution(desired_width, desired_height);
|
2019-10-26 12:39:15 +03:00
|
|
|
m_cursor_location = rect().center();
|
2019-02-17 15:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WSScreen::~WSScreen()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSScreen::set_resolution(int width, int height)
|
|
|
|
{
|
2019-08-18 07:32:14 +03:00
|
|
|
FBResolution resolution { 0, (int)width, (int)height };
|
|
|
|
int rc = fb_set_resolution(m_framebuffer_fd, &resolution);
|
2019-02-17 03:43:01 +03:00
|
|
|
ASSERT(rc == 0);
|
2019-08-18 07:32:14 +03:00
|
|
|
on_change_resolution(resolution.pitch, resolution.width, resolution.height);
|
|
|
|
}
|
2019-02-17 03:43:01 +03:00
|
|
|
|
2019-08-18 07:32:14 +03:00
|
|
|
void WSScreen::on_change_resolution(int pitch, int width, int height)
|
|
|
|
{
|
2019-02-17 15:12:59 +03:00
|
|
|
if (m_framebuffer) {
|
2019-08-18 07:32:14 +03:00
|
|
|
size_t previous_size_in_bytes = m_size_in_bytes;
|
2019-02-17 15:12:59 +03:00
|
|
|
int rc = munmap(m_framebuffer, previous_size_in_bytes);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
2019-08-18 07:32:14 +03:00
|
|
|
int rc = fb_get_size_in_bytes(m_framebuffer_fd, &m_size_in_bytes);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
|
|
|
|
m_framebuffer = (RGBA32*)mmap(nullptr, m_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_framebuffer_fd, 0);
|
2019-02-17 03:43:01 +03:00
|
|
|
ASSERT(m_framebuffer && m_framebuffer != (void*)-1);
|
2018-10-10 16:12:38 +03:00
|
|
|
|
2019-08-18 07:32:14 +03:00
|
|
|
m_pitch = pitch;
|
2019-02-17 15:12:59 +03:00
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
|
|
|
|
m_cursor_location.constrain(rect());
|
2018-10-10 16:12:38 +03:00
|
|
|
}
|
|
|
|
|
2019-08-18 07:32:14 +03:00
|
|
|
void WSScreen::set_buffer(int index)
|
|
|
|
{
|
|
|
|
ASSERT(m_can_set_buffer);
|
|
|
|
int rc = fb_set_buffer(m_framebuffer_fd, index);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
2020-02-03 19:30:35 +03:00
|
|
|
void WSScreen::on_receive_mouse_data(const MousePacket& packet)
|
2019-01-11 05:52:09 +03:00
|
|
|
{
|
|
|
|
auto prev_location = m_cursor_location;
|
2020-02-03 19:30:35 +03:00
|
|
|
if (packet.is_relative)
|
|
|
|
m_cursor_location.move_by(packet.x, packet.y);
|
|
|
|
else
|
|
|
|
m_cursor_location = { packet.x * m_width / 0xffff, packet.y * m_height / 0xffff };
|
2019-01-12 03:00:24 +03:00
|
|
|
m_cursor_location.constrain(rect());
|
2020-02-03 19:30:35 +03:00
|
|
|
|
|
|
|
unsigned buttons = packet.buttons;
|
2019-03-05 16: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 12:47:19 +03:00
|
|
|
auto post_mousedown_or_mouseup_if_needed = [&](MouseButton button) {
|
2019-03-05 16:11:46 +03:00
|
|
|
if (!(changed_buttons & (unsigned)button))
|
|
|
|
return;
|
2019-04-14 06:23:37 +03:00
|
|
|
auto message = make<WSMouseEvent>(buttons & (unsigned)button ? WSEvent::MouseDown : WSEvent::MouseUp, m_cursor_location, buttons, button, m_modifiers);
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::EventLoop::current().post_event(WSWindowManager::the(), move(message));
|
2019-03-05 16: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);
|
2019-03-05 15:59:44 +03:00
|
|
|
if (m_cursor_location != prev_location) {
|
2019-04-14 06:23:37 +03:00
|
|
|
auto message = make<WSMouseEvent>(WSEvent::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::EventLoop::current().post_event(WSWindowManager::the(), move(message));
|
2019-03-05 15:59:44 +03:00
|
|
|
}
|
2019-05-07 04:56:54 +03:00
|
|
|
|
2020-02-03 19:30:35 +03:00
|
|
|
if (packet.z) {
|
|
|
|
auto message = make<WSMouseEvent>(WSEvent::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, packet.z);
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::EventLoop::current().post_event(WSWindowManager::the(), move(message));
|
2019-05-13 20:52:57 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 04:56:54 +03:00
|
|
|
if (m_cursor_location != prev_location)
|
2019-05-24 20:32:46 +03:00
|
|
|
WSCompositor::the().invalidate_cursor();
|
2019-01-11 05:52:09 +03:00
|
|
|
}
|
2019-01-11 06:40:05 +03:00
|
|
|
|
2019-02-17 02:13:47 +03:00
|
|
|
void WSScreen::on_receive_keyboard_data(KeyEvent kernel_event)
|
2019-01-11 06:40:05 +03:00
|
|
|
{
|
2019-03-08 19:53:02 +03:00
|
|
|
m_modifiers = kernel_event.modifiers();
|
2019-04-14 06:23:37 +03:00
|
|
|
auto message = make<WSKeyEvent>(kernel_event.is_press() ? WSEvent::KeyDown : WSEvent::KeyUp, kernel_event.key, kernel_event.character, kernel_event.modifiers());
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::EventLoop::current().post_event(WSWindowManager::the(), move(message));
|
2019-01-11 06:40:05 +03:00
|
|
|
}
|