2019-01-16 16:03:50 +01:00
|
|
|
#include "WSScreen.h"
|
2019-01-26 05:28:02 +01:00
|
|
|
#include "WSMessageLoop.h"
|
|
|
|
#include "WSMessage.h"
|
2019-01-16 16:03:50 +01:00
|
|
|
#include "WSWindowManager.h"
|
2019-02-17 01:43:01 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
2018-10-10 15:12:38 +02:00
|
|
|
|
2019-01-16 16:03:50 +01:00
|
|
|
static WSScreen* s_the;
|
2018-10-10 15:12:38 +02:00
|
|
|
|
2019-01-16 16:03:50 +01:00
|
|
|
WSScreen& WSScreen::the()
|
2018-10-10 15:12:38 +02:00
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2019-02-17 01:43:01 +01:00
|
|
|
WSScreen::WSScreen(unsigned width, unsigned height)
|
|
|
|
: m_width(width)
|
2018-10-10 15:12:38 +02:00
|
|
|
, m_height(height)
|
|
|
|
{
|
|
|
|
ASSERT(!s_the);
|
|
|
|
s_the = this;
|
2019-01-11 03:52:09 +01:00
|
|
|
m_cursor_location = rect().center();
|
2019-02-17 01:43:01 +01:00
|
|
|
m_framebuffer_fd = open("/dev/bxvga", O_RDWR);
|
|
|
|
ASSERT(m_framebuffer_fd >= 0);
|
|
|
|
|
2019-02-17 13:12:59 +01:00
|
|
|
set_resolution(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
WSScreen::~WSScreen()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSScreen::set_resolution(int width, int height)
|
|
|
|
{
|
2019-02-17 01:43:01 +01:00
|
|
|
struct BXVGAResolution {
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
};
|
|
|
|
BXVGAResolution resolution { (int)width, (int)height};
|
|
|
|
int rc = ioctl(m_framebuffer_fd, 1985, (int)&resolution);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
|
2019-02-17 13:12:59 +01:00
|
|
|
if (m_framebuffer) {
|
|
|
|
size_t previous_size_in_bytes = m_width * m_height * sizeof(RGBA32) * 2;
|
|
|
|
int rc = munmap(m_framebuffer, previous_size_in_bytes);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t framebuffer_size_in_bytes = width * height * sizeof(RGBA32) * 2;
|
2019-02-17 01:43:01 +01:00
|
|
|
m_framebuffer = (RGBA32*)mmap(nullptr, framebuffer_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_framebuffer_fd, 0);
|
|
|
|
ASSERT(m_framebuffer && m_framebuffer != (void*)-1);
|
2018-10-10 15:12:38 +02:00
|
|
|
|
2019-02-17 13:12:59 +01:00
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
|
|
|
|
m_cursor_location.constrain(rect());
|
2018-10-10 15:12:38 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 14:11:46 +01:00
|
|
|
void WSScreen::on_receive_mouse_data(int dx, int dy, unsigned buttons)
|
2019-01-11 03:52:09 +01:00
|
|
|
{
|
|
|
|
auto prev_location = m_cursor_location;
|
2019-01-16 17:54:06 +01:00
|
|
|
m_cursor_location.move_by(dx, dy);
|
2019-01-12 01:00:24 +01:00
|
|
|
m_cursor_location.constrain(rect());
|
2019-03-05 14:11:46 +01:00
|
|
|
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;
|
2019-03-08 17:53:02 +01:00
|
|
|
auto message = make<WSMouseEvent>(buttons & (unsigned)button ? WSMessage::MouseDown : WSMessage::MouseUp, m_cursor_location, buttons, button, m_modifiers);
|
2019-04-14 05:15:22 +02:00
|
|
|
WSMessageLoop::the().post_event(WSWindowManager::the(), move(message));
|
2019-03-05 14:11:46 +01: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 13:59:44 +01:00
|
|
|
if (m_cursor_location != prev_location) {
|
2019-03-08 17:53:02 +01:00
|
|
|
auto message = make<WSMouseEvent>(WSMessage::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
|
2019-04-14 05:15:22 +02:00
|
|
|
WSMessageLoop::the().post_event(WSWindowManager::the(), move(message));
|
2019-03-05 13:59:44 +01:00
|
|
|
}
|
|
|
|
// NOTE: Invalidate the cursor if it moved, or if the left button changed state (for the cursor color inversion.)
|
2019-03-05 14:11:46 +01:00
|
|
|
if (m_cursor_location != prev_location || changed_buttons & (unsigned)MouseButton::Left)
|
2019-02-07 08:53:57 +01:00
|
|
|
WSWindowManager::the().invalidate_cursor();
|
2019-01-11 03:52:09 +01:00
|
|
|
}
|
2019-01-11 04:40:05 +01:00
|
|
|
|
2019-02-17 00:13:47 +01:00
|
|
|
void WSScreen::on_receive_keyboard_data(KeyEvent kernel_event)
|
2019-01-11 04:40:05 +01:00
|
|
|
{
|
2019-03-08 17:53:02 +01:00
|
|
|
m_modifiers = kernel_event.modifiers();
|
2019-03-03 12:32:15 +01:00
|
|
|
auto message = make<WSKeyEvent>(kernel_event.is_press() ? WSMessage::KeyDown : WSMessage::KeyUp, kernel_event.key, kernel_event.character, kernel_event.modifiers());
|
2019-04-14 05:15:22 +02:00
|
|
|
WSMessageLoop::the().post_event(WSWindowManager::the(), move(message));
|
2019-01-11 04:40:05 +01:00
|
|
|
}
|
2019-02-17 01:43:01 +01:00
|
|
|
|
|
|
|
void WSScreen::set_y_offset(int offset)
|
|
|
|
{
|
|
|
|
int rc = ioctl(m_framebuffer_fd, 1982, offset);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|