Userspace: Start work on GUI and WindowServer
Current implementation can create custom windows and each window has its own framebuffer. When window wants to write its framebuffer to the screen it will send a message to the WindowServer using unix sockets.
This commit is contained in:
24
LibGUI/CMakeLists.txt
Normal file
24
LibGUI/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
cmake_minimum_required(VERSION 3.26)
|
||||
|
||||
project(libgui CXX)
|
||||
|
||||
set(LIBGUI_SOURCES
|
||||
Window.cpp
|
||||
)
|
||||
|
||||
add_custom_target(libgui-headers
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different ${CMAKE_CURRENT_SOURCE_DIR}/include/ ${BANAN_INCLUDE}/
|
||||
DEPENDS sysroot
|
||||
)
|
||||
|
||||
add_library(libgui ${LIBGUI_SOURCES})
|
||||
add_dependencies(libgui headers libc-install)
|
||||
target_link_libraries(libgui PUBLIC libc)
|
||||
|
||||
add_custom_target(libgui-install
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/libgui.a ${BANAN_LIB}/
|
||||
DEPENDS libgui
|
||||
BYPRODUCTS ${BANAN_LIB}/libgui.a
|
||||
)
|
||||
|
||||
set(CMAKE_STATIC_LIBRARY_PREFIX "")
|
||||
115
LibGUI/Window.cpp
Normal file
115
LibGUI/Window.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
#include "LibGUI/Window.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/banan-os.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace LibGUI
|
||||
{
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
munmap(m_framebuffer, m_width * m_height * 4);
|
||||
close(m_server_fd);
|
||||
}
|
||||
|
||||
BAN::ErrorOr<BAN::UniqPtr<Window>> Window::create(uint32_t width, uint32_t height)
|
||||
{
|
||||
int server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
||||
if (server_fd == -1)
|
||||
return BAN::Error::from_errno(errno);
|
||||
|
||||
sockaddr_un server_address;
|
||||
server_address.sun_family = AF_UNIX;
|
||||
strcpy(server_address.sun_path, s_window_server_socket.data());
|
||||
if (connect(server_fd, (sockaddr*)&server_address, sizeof(server_address)) == -1)
|
||||
{
|
||||
close(server_fd);
|
||||
return BAN::Error::from_errno(errno);
|
||||
}
|
||||
|
||||
WindowCreatePacket packet;
|
||||
packet.width = width;
|
||||
packet.height = height;
|
||||
if (send(server_fd, &packet, sizeof(packet), 0) != sizeof(packet))
|
||||
{
|
||||
close(server_fd);
|
||||
return BAN::Error::from_errno(errno);
|
||||
}
|
||||
|
||||
WindowCreateResponse response;
|
||||
if (recv(server_fd, &response, sizeof(response), 0) != sizeof(response))
|
||||
{
|
||||
close(server_fd);
|
||||
return BAN::Error::from_errno(errno);
|
||||
}
|
||||
|
||||
void* framebuffer_addr = smo_map(response.framebuffer_smo_key);
|
||||
if (framebuffer_addr == nullptr)
|
||||
{
|
||||
close(server_fd);
|
||||
return BAN::Error::from_errno(errno);
|
||||
}
|
||||
|
||||
return TRY(BAN::UniqPtr<Window>::create(
|
||||
server_fd,
|
||||
static_cast<uint32_t*>(framebuffer_addr),
|
||||
width,
|
||||
height
|
||||
));
|
||||
}
|
||||
|
||||
bool Window::invalidate()
|
||||
{
|
||||
WindowInvalidatePacket packet;
|
||||
packet.x = 0;
|
||||
packet.y = 0;
|
||||
packet.width = m_width;
|
||||
packet.height = m_height;
|
||||
return send(m_server_fd, &packet, sizeof(packet), 0) == sizeof(packet);
|
||||
}
|
||||
|
||||
void Window::poll_events()
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(m_server_fd, &fds);
|
||||
timeval timeout { .tv_sec = 0, .tv_usec = 0 };
|
||||
select(m_server_fd + 1, &fds, nullptr, nullptr, &timeout);
|
||||
|
||||
if (!FD_ISSET(m_server_fd, &fds))
|
||||
break;
|
||||
|
||||
EventPacket packet;
|
||||
if (recv(m_server_fd, &packet, sizeof(packet), 0) <= 0)
|
||||
break;
|
||||
|
||||
switch (packet.type)
|
||||
{
|
||||
case EventPacket::Type::KeyEvent:
|
||||
if (m_key_event_callback)
|
||||
m_key_event_callback(packet.key_event);
|
||||
break;
|
||||
case EventPacket::Type::MouseButtonEvent:
|
||||
if (m_mouse_button_event_callback)
|
||||
m_mouse_button_event_callback(packet.mouse_button_event);
|
||||
break;
|
||||
case EventPacket::Type::MouseMoveEvent:
|
||||
if (m_mouse_move_event_callback)
|
||||
m_mouse_move_event_callback(packet.mouse_move_event);
|
||||
break;
|
||||
case EventPacket::Type::MouseScrollEvent:
|
||||
if (m_mouse_scroll_event_callback)
|
||||
m_mouse_scroll_event_callback(packet.mouse_scroll_event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
142
LibGUI/include/LibGUI/Window.h
Normal file
142
LibGUI/include/LibGUI/Window.h
Normal file
@@ -0,0 +1,142 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Function.h>
|
||||
#include <BAN/StringView.h>
|
||||
#include <BAN/UniqPtr.h>
|
||||
|
||||
#include <LibInput/KeyEvent.h>
|
||||
#include <LibInput/MouseEvent.h>
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LibGUI
|
||||
{
|
||||
|
||||
static constexpr BAN::StringView s_window_server_socket = "/tmp/window-server.socket"sv;
|
||||
|
||||
enum WindowPacketType : uint8_t
|
||||
{
|
||||
INVALID,
|
||||
CreateWindow,
|
||||
Invalidate,
|
||||
};
|
||||
|
||||
struct WindowCreatePacket
|
||||
{
|
||||
WindowPacketType type = WindowPacketType::CreateWindow;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
};
|
||||
|
||||
struct WindowInvalidatePacket
|
||||
{
|
||||
WindowPacketType type = WindowPacketType::Invalidate;
|
||||
uint32_t x;
|
||||
uint32_t y;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
};
|
||||
|
||||
struct WindowCreateResponse
|
||||
{
|
||||
long framebuffer_smo_key;
|
||||
};
|
||||
|
||||
struct WindowPacket
|
||||
{
|
||||
WindowPacket()
|
||||
: type(WindowPacketType::INVALID)
|
||||
{ }
|
||||
|
||||
union
|
||||
{
|
||||
WindowPacketType type;
|
||||
WindowCreatePacket create;
|
||||
WindowInvalidatePacket invalidate;
|
||||
};
|
||||
};
|
||||
|
||||
struct EventPacket
|
||||
{
|
||||
enum class Type : uint8_t
|
||||
{
|
||||
KeyEvent,
|
||||
MouseButtonEvent,
|
||||
MouseMoveEvent,
|
||||
MouseScrollEvent,
|
||||
};
|
||||
using KeyEvent = LibInput::KeyEvent;
|
||||
using MouseButton = LibInput::MouseButton;
|
||||
struct MouseButtonEvent
|
||||
{
|
||||
MouseButton button;
|
||||
bool pressed;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
};
|
||||
struct MouseMoveEvent
|
||||
{
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
};
|
||||
using MouseScrollEvent = LibInput::MouseScrollEvent;
|
||||
|
||||
Type type;
|
||||
union
|
||||
{
|
||||
KeyEvent key_event;
|
||||
MouseButtonEvent mouse_button_event;
|
||||
MouseMoveEvent mouse_move_event;
|
||||
MouseScrollEvent mouse_scroll_event;
|
||||
};
|
||||
};
|
||||
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
~Window();
|
||||
|
||||
static BAN::ErrorOr<BAN::UniqPtr<Window>> create(uint32_t width, uint32_t height);
|
||||
|
||||
void set_pixel(uint32_t x, uint32_t y, uint32_t color)
|
||||
{
|
||||
ASSERT(x < m_width);
|
||||
ASSERT(y < m_height);
|
||||
m_framebuffer[y * m_width + x] = color;
|
||||
}
|
||||
|
||||
bool invalidate();
|
||||
|
||||
uint32_t width() const { return m_width; }
|
||||
uint32_t height() const { return m_height; }
|
||||
|
||||
void poll_events();
|
||||
void set_key_event_callback(BAN::Function<void(EventPacket::KeyEvent)> callback) { m_key_event_callback = callback; }
|
||||
void set_mouse_button_event_callback(BAN::Function<void(EventPacket::MouseButtonEvent)> callback) { m_mouse_button_event_callback = callback; }
|
||||
void set_mouse_move_event_callback(BAN::Function<void(EventPacket::MouseMoveEvent)> callback) { m_mouse_move_event_callback = callback; }
|
||||
void set_mouse_scroll_event_callback(BAN::Function<void(EventPacket::MouseScrollEvent)> callback) { m_mouse_scroll_event_callback = callback; }
|
||||
|
||||
private:
|
||||
Window(int server_fd, uint32_t* framebuffer, uint32_t width, uint32_t height)
|
||||
: m_server_fd(server_fd)
|
||||
, m_framebuffer(framebuffer)
|
||||
, m_width(width)
|
||||
, m_height(height)
|
||||
{ }
|
||||
|
||||
private:
|
||||
int m_server_fd;
|
||||
uint32_t* m_framebuffer;
|
||||
uint32_t m_width;
|
||||
uint32_t m_height;
|
||||
|
||||
BAN::Function<void(EventPacket::KeyEvent)> m_key_event_callback;
|
||||
BAN::Function<void(EventPacket::MouseButtonEvent)> m_mouse_button_event_callback;
|
||||
BAN::Function<void(EventPacket::MouseMoveEvent)> m_mouse_move_event_callback;
|
||||
BAN::Function<void(EventPacket::MouseScrollEvent)> m_mouse_scroll_event_callback;
|
||||
|
||||
friend class BAN::UniqPtr<Window>;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user