Compare commits
6 Commits
91381546d5
...
988f7b0561
Author | SHA1 | Date |
---|---|---|
Bananymous | 988f7b0561 | |
Bananymous | e99a271465 | |
Bananymous | d266c7f93b | |
Bananymous | d7e5c56e94 | |
Bananymous | ddd3b4c093 | |
Bananymous | 3a6fc4c197 |
|
@ -12,9 +12,9 @@
|
||||||
#define MUST(expr) ({ auto&& e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); e.release_value(); })
|
#define MUST(expr) ({ auto&& e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); e.release_value(); })
|
||||||
#define MUST_REF(expr) *({ auto&& e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); &e.release_value(); })
|
#define MUST_REF(expr) *({ auto&& e = expr; if (e.is_error()) Kernel::panic("{}", e.error()); &e.release_value(); })
|
||||||
#else
|
#else
|
||||||
#include <assert.h>
|
#include <BAN/Debug.h>
|
||||||
#define MUST(expr) ({ auto&& e = expr; assert(!e.is_error()); e.release_value(); })
|
#define MUST(expr) ({ auto&& e = expr; if (e.is_error()) { derrorln("MUST(" #expr "): {}", e.error()); __builtin_trap(); } e.release_value(); })
|
||||||
#define MUST_REF(expr) *({ auto&& e = expr; assert(!e.is_error()); &e.release_value(); })
|
#define MUST_REF(expr) *({ auto&& e = expr; if (e.is_error()) { derrorln("MUST(" #expr "): {}", e.error()); __builtin_trap(); } &e.release_value(); })
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define TRY(expr) ({ auto&& e = expr; if (e.is_error()) return e.release_error(); e.release_value(); })
|
#define TRY(expr) ({ auto&& e = expr; if (e.is_error()) return e.release_error(); e.release_value(); })
|
||||||
|
|
|
@ -90,6 +90,9 @@ namespace BAN
|
||||||
template<typename Base, typename Derived> struct is_base_of { static constexpr bool value = __is_base_of(Base, Derived); };
|
template<typename Base, typename Derived> struct is_base_of { static constexpr bool value = __is_base_of(Base, Derived); };
|
||||||
template<typename Base, typename Derived> inline constexpr bool is_base_of_v = is_base_of<Base, Derived>::value;
|
template<typename Base, typename Derived> inline constexpr bool is_base_of_v = is_base_of<Base, Derived>::value;
|
||||||
|
|
||||||
|
template<typename T> struct is_pod { static constexpr bool value = __is_pod(T); };
|
||||||
|
template<typename T> inline constexpr bool is_pod_v = is_pod<T>::value;
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
template<typename T, bool = is_arithmetic_v<T>> struct is_signed { static constexpr bool value = T(-1) < T(0); };
|
template<typename T, bool = is_arithmetic_v<T>> struct is_signed { static constexpr bool value = T(-1) < T(0); };
|
||||||
|
|
Binary file not shown.
|
@ -1,6 +1,6 @@
|
||||||
From 0f37d9f2df042eb8ba021dd91b898c1f07d86b58 Mon Sep 17 00:00:00 2001
|
From 0f37d9f2df042eb8ba021dd91b898c1f07d86b58 Mon Sep 17 00:00:00 2001
|
||||||
From: Bananymous <bananymousosq@gmail.com>
|
From: Bananymous <bananymousosq@gmail.com>
|
||||||
Date: Mon, 3 Jun 2024 20:01:40 +0300
|
Date: Fri, 18 Oct 2024 03:44:10 +0300
|
||||||
Subject: [PATCH] Add support for banan-os
|
Subject: [PATCH] Add support for banan-os
|
||||||
|
|
||||||
---
|
---
|
||||||
|
@ -114,7 +114,7 @@ index 0000000..9161771
|
||||||
+{
|
+{
|
||||||
+ s_window = MUST(LibGUI::Window::create(DOOMGENERIC_RESX, DOOMGENERIC_RESY, "DOOM"_sv));
|
+ s_window = MUST(LibGUI::Window::create(DOOMGENERIC_RESX, DOOMGENERIC_RESY, "DOOM"_sv));
|
||||||
+ s_window->set_key_event_callback(
|
+ s_window->set_key_event_callback(
|
||||||
+ [](LibGUI::EventPacket::KeyEvent event)
|
+ [](LibGUI::EventPacket::KeyEvent::event_t event)
|
||||||
+ {
|
+ {
|
||||||
+ unsigned short doom_key = 0;
|
+ unsigned short doom_key = 0;
|
||||||
+ switch (event.key)
|
+ switch (event.key)
|
||||||
|
|
|
@ -16,6 +16,47 @@
|
||||||
namespace LibGUI
|
namespace LibGUI
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct ReceivePacket
|
||||||
|
{
|
||||||
|
PacketType type;
|
||||||
|
BAN::Vector<uint8_t> data_with_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
static BAN::ErrorOr<ReceivePacket> recv_packet(int socket)
|
||||||
|
{
|
||||||
|
uint32_t packet_size;
|
||||||
|
|
||||||
|
{
|
||||||
|
const ssize_t nrecv = recv(socket, &packet_size, sizeof(uint32_t), 0);
|
||||||
|
if (nrecv < 0)
|
||||||
|
return BAN::Error::from_errno(errno);
|
||||||
|
if (nrecv == 0)
|
||||||
|
return BAN::Error::from_errno(ECONNRESET);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packet_size < sizeof(uint32_t))
|
||||||
|
return BAN::Error::from_literal("invalid packet, does not fit packet id");
|
||||||
|
|
||||||
|
BAN::Vector<uint8_t> packet_data;
|
||||||
|
TRY(packet_data.resize(packet_size));
|
||||||
|
|
||||||
|
size_t total_recv = 0;
|
||||||
|
while (total_recv < packet_size)
|
||||||
|
{
|
||||||
|
const ssize_t nrecv = recv(socket, packet_data.data() + total_recv, packet_size - total_recv, 0);
|
||||||
|
if (nrecv < 0)
|
||||||
|
return BAN::Error::from_errno(errno);
|
||||||
|
if (nrecv == 0)
|
||||||
|
return BAN::Error::from_errno(ECONNRESET);
|
||||||
|
total_recv += nrecv;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ReceivePacket {
|
||||||
|
*reinterpret_cast<PacketType*>(packet_data.data()),
|
||||||
|
packet_data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Window::~Window()
|
Window::~Window()
|
||||||
{
|
{
|
||||||
munmap(m_framebuffer_smo, m_width * m_height * 4);
|
munmap(m_framebuffer_smo, m_width * m_height * 4);
|
||||||
|
@ -24,13 +65,7 @@ namespace LibGUI
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::UniqPtr<Window>> Window::create(uint32_t width, uint32_t height, BAN::StringView title)
|
BAN::ErrorOr<BAN::UniqPtr<Window>> Window::create(uint32_t width, uint32_t height, BAN::StringView title)
|
||||||
{
|
{
|
||||||
if (title.size() >= sizeof(WindowCreatePacket::title))
|
int server_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
return BAN::Error::from_errno(EINVAL);
|
|
||||||
|
|
||||||
BAN::Vector<uint32_t> framebuffer;
|
|
||||||
TRY(framebuffer.resize(width * height, 0xFF000000));
|
|
||||||
|
|
||||||
int server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
|
||||||
if (server_fd == -1)
|
if (server_fd == -1)
|
||||||
return BAN::Error::from_errno(errno);
|
return BAN::Error::from_errno(errno);
|
||||||
BAN::ScopeGuard server_closer([server_fd] { close(server_fd); });
|
BAN::ScopeGuard server_closer([server_fd] { close(server_fd); });
|
||||||
|
@ -61,31 +96,37 @@ namespace LibGUI
|
||||||
nanosleep(&sleep_time, nullptr);
|
nanosleep(&sleep_time, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowCreatePacket packet;
|
WindowPacket::WindowCreate create_packet;
|
||||||
packet.width = width;
|
create_packet.width = width;
|
||||||
packet.height = height;
|
create_packet.height = height;
|
||||||
strncpy(packet.title, title.data(), title.size());
|
TRY(create_packet.title.append(title));
|
||||||
packet.title[title.size()] = '\0';
|
TRY(create_packet.send_serialized(server_fd));
|
||||||
if (send(server_fd, &packet, sizeof(packet), 0) != sizeof(packet))
|
|
||||||
return BAN::Error::from_errno(errno);
|
|
||||||
|
|
||||||
WindowCreateResponse response;
|
const auto [response_type, response_data ] = TRY(recv_packet(server_fd));
|
||||||
if (recv(server_fd, &response, sizeof(response), 0) != sizeof(response))
|
if (response_type != PacketType::WindowCreateResponse)
|
||||||
return BAN::Error::from_errno(errno);
|
return BAN::Error::from_literal("Server responded with invalid packet");
|
||||||
|
|
||||||
void* framebuffer_addr = smo_map(response.framebuffer_smo_key);
|
const auto create_response = TRY(WindowPacket::WindowCreateResponse::deserialize(response_data.span()));
|
||||||
|
void* framebuffer_addr = smo_map(create_response.smo_key);
|
||||||
if (framebuffer_addr == nullptr)
|
if (framebuffer_addr == nullptr)
|
||||||
return BAN::Error::from_errno(errno);
|
return BAN::Error::from_errno(errno);
|
||||||
|
width = create_response.width;
|
||||||
|
height = create_response.height;
|
||||||
|
|
||||||
server_closer.disable();
|
BAN::Vector<uint32_t> framebuffer;
|
||||||
|
TRY(framebuffer.resize(width * height, 0xFFFFFFFF));
|
||||||
|
|
||||||
return TRY(BAN::UniqPtr<Window>::create(
|
auto window = TRY(BAN::UniqPtr<Window>::create(
|
||||||
server_fd,
|
server_fd,
|
||||||
static_cast<uint32_t*>(framebuffer_addr),
|
static_cast<uint32_t*>(framebuffer_addr),
|
||||||
BAN::move(framebuffer),
|
BAN::move(framebuffer),
|
||||||
width,
|
width,
|
||||||
height
|
height
|
||||||
));
|
));
|
||||||
|
|
||||||
|
server_closer.disable();
|
||||||
|
|
||||||
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::fill_rect(int32_t x, int32_t y, uint32_t width, uint32_t height, uint32_t color)
|
void Window::fill_rect(int32_t x, int32_t y, uint32_t width, uint32_t height, uint32_t color)
|
||||||
|
@ -211,14 +252,56 @@ namespace LibGUI
|
||||||
for (uint32_t i = 0; i < height; i++)
|
for (uint32_t i = 0; i < height; i++)
|
||||||
memcpy(&m_framebuffer_smo[(y + i) * m_width + x], &m_framebuffer[(y + i) * m_width + x], width * sizeof(uint32_t));
|
memcpy(&m_framebuffer_smo[(y + i) * m_width + x], &m_framebuffer[(y + i) * m_width + x], width * sizeof(uint32_t));
|
||||||
|
|
||||||
WindowInvalidatePacket packet;
|
WindowPacket::WindowInvalidate packet;
|
||||||
packet.x = x;
|
packet.x = x;
|
||||||
packet.y = y;
|
packet.y = y;
|
||||||
packet.width = width;
|
packet.width = width;
|
||||||
packet.height = height;
|
packet.height = height;
|
||||||
return send(m_server_fd, &packet, sizeof(packet), 0) == sizeof(packet);
|
|
||||||
|
if (auto ret = packet.send_serialized(m_server_fd); ret.is_error())
|
||||||
|
{
|
||||||
|
dprintln("failed to invalidate window: {}", ret.error());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Window::set_position(int32_t x, int32_t y)
|
||||||
|
{
|
||||||
|
WindowPacket::WindowSetPosition packet;
|
||||||
|
packet.x = x;
|
||||||
|
packet.y = y;
|
||||||
|
|
||||||
|
if (auto ret = packet.send_serialized(m_server_fd); ret.is_error())
|
||||||
|
{
|
||||||
|
dprintln("failed to set window position: {}", ret.error());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Window::set_attributes(Attributes attributes)
|
||||||
|
{
|
||||||
|
WindowPacket::WindowSetAttributes packet;
|
||||||
|
packet.title_bar = attributes.title_bar;
|
||||||
|
packet.movable = attributes.movable;
|
||||||
|
packet.rounded_corners = attributes.rounded_corners;
|
||||||
|
packet.alpha_channel = attributes.alpha_channel;
|
||||||
|
|
||||||
|
if (auto ret = packet.send_serialized(m_server_fd); ret.is_error())
|
||||||
|
{
|
||||||
|
dprintln("failed to set window attributes: {}", ret.error());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_attributes = attributes;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TRY_OR_BREAK(...) ({ auto&& e = (__VA_ARGS__); if (e.is_error()) break; e.release_value(); })
|
||||||
|
|
||||||
void Window::poll_events()
|
void Window::poll_events()
|
||||||
{
|
{
|
||||||
for (;;)
|
for (;;)
|
||||||
|
@ -232,35 +315,38 @@ namespace LibGUI
|
||||||
if (!FD_ISSET(m_server_fd, &fds))
|
if (!FD_ISSET(m_server_fd, &fds))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
EventPacket packet;
|
auto packet_or_error = recv_packet(m_server_fd);
|
||||||
if (recv(m_server_fd, &packet, sizeof(packet), 0) <= 0)
|
if (packet_or_error.is_error())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
switch (packet.type)
|
const auto [packet_type, packet_data] = packet_or_error.release_value();
|
||||||
|
switch (packet_type)
|
||||||
{
|
{
|
||||||
case EventPacket::Type::DestroyWindow:
|
case PacketType::DestroyWindowEvent:
|
||||||
exit(1);
|
exit(1);
|
||||||
case EventPacket::Type::CloseWindow:
|
case PacketType::CloseWindowEvent:
|
||||||
if (m_close_window_event_callback)
|
if (m_close_window_event_callback)
|
||||||
m_close_window_event_callback();
|
m_close_window_event_callback();
|
||||||
else
|
else
|
||||||
exit(0);
|
exit(0);
|
||||||
break;
|
break;
|
||||||
case EventPacket::Type::KeyEvent:
|
case PacketType::KeyEvent:
|
||||||
if (m_key_event_callback)
|
if (m_key_event_callback)
|
||||||
m_key_event_callback(packet.key_event);
|
m_key_event_callback(TRY_OR_BREAK(EventPacket::KeyEvent::deserialize(packet_data.span())).event);
|
||||||
break;
|
break;
|
||||||
case EventPacket::Type::MouseButtonEvent:
|
case PacketType::MouseButtonEvent:
|
||||||
if (m_mouse_button_event_callback)
|
if (m_mouse_button_event_callback)
|
||||||
m_mouse_button_event_callback(packet.mouse_button_event);
|
m_mouse_button_event_callback(TRY_OR_BREAK(EventPacket::MouseButtonEvent::deserialize(packet_data.span())).event);
|
||||||
break;
|
break;
|
||||||
case EventPacket::Type::MouseMoveEvent:
|
case PacketType::MouseMoveEvent:
|
||||||
if (m_mouse_move_event_callback)
|
if (m_mouse_move_event_callback)
|
||||||
m_mouse_move_event_callback(packet.mouse_move_event);
|
m_mouse_move_event_callback(TRY_OR_BREAK(EventPacket::MouseMoveEvent::deserialize(packet_data.span())).event);
|
||||||
break;
|
break;
|
||||||
case EventPacket::Type::MouseScrollEvent:
|
case PacketType::MouseScrollEvent:
|
||||||
if (m_mouse_scroll_event_callback)
|
if (m_mouse_scroll_event_callback)
|
||||||
m_mouse_scroll_event_callback(packet.mouse_scroll_event);
|
m_mouse_scroll_event_callback(TRY_OR_BREAK(EventPacket::MouseScrollEvent::deserialize(packet_data.span())).event);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,257 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <BAN/String.h>
|
||||||
|
#include <BAN/StringView.h>
|
||||||
|
#include <BAN/ByteSpan.h>
|
||||||
|
|
||||||
|
#include <LibInput/KeyEvent.h>
|
||||||
|
#include <LibInput/MouseEvent.h>
|
||||||
|
|
||||||
|
#include <sys/banan-os.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#define FOR_EACH_0(macro)
|
||||||
|
#define FOR_EACH_2(macro, type, name) macro(type, name)
|
||||||
|
#define FOR_EACH_4(macro, type, name, ...) macro(type, name) FOR_EACH_2(macro, __VA_ARGS__)
|
||||||
|
#define FOR_EACH_6(macro, type, name, ...) macro(type, name) FOR_EACH_4(macro, __VA_ARGS__)
|
||||||
|
#define FOR_EACH_8(macro, type, name, ...) macro(type, name) FOR_EACH_6(macro, __VA_ARGS__)
|
||||||
|
|
||||||
|
#define CONCATENATE_2(arg1, arg2) arg1 ## arg2
|
||||||
|
#define CONCATENATE_1(arg1, arg2) CONCATENATE_2(arg1, arg2)
|
||||||
|
#define CONCATENATE(arg1, arg2) CONCATENATE_1(arg1, arg2)
|
||||||
|
|
||||||
|
#define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__ __VA_OPT__(,) FOR_EACH_RSEQ_N())
|
||||||
|
#define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__)
|
||||||
|
#define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
|
||||||
|
#define FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
|
||||||
|
|
||||||
|
#define FOR_EACH_(N, what, ...) CONCATENATE(FOR_EACH_, N)(what __VA_OPT__(,) __VA_ARGS__)
|
||||||
|
#define FOR_EACH(what, ...) FOR_EACH_(FOR_EACH_NARG(__VA_ARGS__), what __VA_OPT__(,) __VA_ARGS__)
|
||||||
|
|
||||||
|
#define FIELD_DECL(type, name) type name;
|
||||||
|
#define ADD_SERIALIZED_SIZE(type, name) serialized_size += Serialize::serialized_size_impl<type>(this->name);
|
||||||
|
#define SEND_SERIALIZED(type, name) TRY(Serialize::send_serialized_impl<type>(socket, this->name));
|
||||||
|
#define DESERIALIZE(type, name) value.name = TRY(Serialize::deserialize_impl<type>(buffer));
|
||||||
|
|
||||||
|
#define DEFINE_PACKET_EXTRA(name, extra, ...) \
|
||||||
|
struct name \
|
||||||
|
{ \
|
||||||
|
static constexpr PacketType type = PacketType::name; \
|
||||||
|
static constexpr uint32_t type_u32 = static_cast<uint32_t>(type); \
|
||||||
|
\
|
||||||
|
extra; \
|
||||||
|
\
|
||||||
|
FOR_EACH(FIELD_DECL, __VA_ARGS__) \
|
||||||
|
\
|
||||||
|
size_t serialized_size() \
|
||||||
|
{ \
|
||||||
|
size_t serialized_size = Serialize::serialized_size_impl<uint32_t>(type_u32); \
|
||||||
|
FOR_EACH(ADD_SERIALIZED_SIZE, __VA_ARGS__) \
|
||||||
|
return serialized_size; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
BAN::ErrorOr<void> send_serialized(int socket) \
|
||||||
|
{ \
|
||||||
|
const uint32_t serialized_size = this->serialized_size(); \
|
||||||
|
TRY(Serialize::send_serialized_impl<uint32_t>(socket, serialized_size)); \
|
||||||
|
TRY(Serialize::send_serialized_impl<uint32_t>(socket, type_u32)); \
|
||||||
|
FOR_EACH(SEND_SERIALIZED, __VA_ARGS__) \
|
||||||
|
return {}; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
static BAN::ErrorOr<name> deserialize(BAN::ConstByteSpan buffer) \
|
||||||
|
{ \
|
||||||
|
const uint32_t type_u32 = TRY(Serialize::deserialize_impl<uint32_t>(buffer)); \
|
||||||
|
if (type_u32 != name::type_u32) \
|
||||||
|
return BAN::Error::from_errno(EINVAL); \
|
||||||
|
name value; \
|
||||||
|
FOR_EACH(DESERIALIZE, __VA_ARGS__) \
|
||||||
|
return value; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DEFINE_PACKET(name, ...) DEFINE_PACKET_EXTRA(name, , __VA_ARGS__)
|
||||||
|
|
||||||
|
namespace LibGUI
|
||||||
|
{
|
||||||
|
|
||||||
|
static constexpr BAN::StringView s_window_server_socket = "/tmp/window-server.socket"_sv;
|
||||||
|
|
||||||
|
namespace Serialize
|
||||||
|
{
|
||||||
|
|
||||||
|
inline BAN::ErrorOr<void> send_raw_data(int socket, BAN::ConstByteSpan data)
|
||||||
|
{
|
||||||
|
size_t send_done = 0;
|
||||||
|
while (send_done < data.size())
|
||||||
|
{
|
||||||
|
const ssize_t nsend = ::send(socket, data.data() + send_done, data.size() - send_done, 0);
|
||||||
|
if (nsend < 0)
|
||||||
|
return BAN::Error::from_errno(errno);
|
||||||
|
if (nsend == 0)
|
||||||
|
return BAN::Error::from_errno(ECONNRESET);
|
||||||
|
send_done += nsend;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> requires BAN::is_pod_v<T>
|
||||||
|
inline size_t serialized_size_impl(const T&)
|
||||||
|
{
|
||||||
|
return sizeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> requires BAN::is_pod_v<T>
|
||||||
|
inline BAN::ErrorOr<void> send_serialized_impl(int socket, const T& value)
|
||||||
|
{
|
||||||
|
TRY(send_raw_data(socket, BAN::ConstByteSpan::from(value)));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> requires BAN::is_pod_v<T>
|
||||||
|
inline BAN::ErrorOr<T> deserialize_impl(BAN::ConstByteSpan& buffer)
|
||||||
|
{
|
||||||
|
if (buffer.size() < sizeof(T))
|
||||||
|
return BAN::Error::from_errno(ENOBUFS);
|
||||||
|
const T value = buffer.as<const T>();
|
||||||
|
buffer = buffer.slice(sizeof(T));
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> requires BAN::is_same_v<T, BAN::String>
|
||||||
|
inline size_t serialized_size_impl(const T& value)
|
||||||
|
{
|
||||||
|
return sizeof(uint32_t) + value.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> requires BAN::is_same_v<T, BAN::String>
|
||||||
|
inline BAN::ErrorOr<void> send_serialized_impl(int socket, const T& value)
|
||||||
|
{
|
||||||
|
const uint32_t value_size = value.size();
|
||||||
|
TRY(send_raw_data(socket, BAN::ConstByteSpan::from(value_size)));
|
||||||
|
auto* u8_data = reinterpret_cast<const uint8_t*>(value.data());
|
||||||
|
TRY(send_raw_data(socket, BAN::ConstByteSpan(u8_data, value.size())));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> requires BAN::is_same_v<T, BAN::String>
|
||||||
|
inline BAN::ErrorOr<T> deserialize_impl(BAN::ConstByteSpan& buffer)
|
||||||
|
{
|
||||||
|
if (buffer.size() < sizeof(uint32_t))
|
||||||
|
return BAN::Error::from_errno(ENOBUFS);
|
||||||
|
const uint32_t string_len = buffer.as<const uint32_t>();
|
||||||
|
buffer = buffer.slice(sizeof(uint32_t));
|
||||||
|
|
||||||
|
if (buffer.size() < string_len)
|
||||||
|
return BAN::Error::from_errno(ENOBUFS);
|
||||||
|
|
||||||
|
BAN::String string;
|
||||||
|
TRY(string.resize(string_len));
|
||||||
|
memcpy(string.data(), buffer.data(), string_len);
|
||||||
|
buffer = buffer.slice(string_len);
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class PacketType : uint32_t
|
||||||
|
{
|
||||||
|
WindowCreate,
|
||||||
|
WindowCreateResponse,
|
||||||
|
WindowInvalidate,
|
||||||
|
WindowSetPosition,
|
||||||
|
WindowSetAttributes,
|
||||||
|
|
||||||
|
DestroyWindowEvent,
|
||||||
|
CloseWindowEvent,
|
||||||
|
KeyEvent,
|
||||||
|
MouseButtonEvent,
|
||||||
|
MouseMoveEvent,
|
||||||
|
MouseScrollEvent,
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace WindowPacket
|
||||||
|
{
|
||||||
|
|
||||||
|
DEFINE_PACKET(WindowCreate,
|
||||||
|
uint32_t, width,
|
||||||
|
uint32_t, height,
|
||||||
|
BAN::String, title
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET(WindowCreateResponse,
|
||||||
|
uint32_t, width,
|
||||||
|
uint32_t, height,
|
||||||
|
long, smo_key
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET(WindowInvalidate,
|
||||||
|
uint32_t, x,
|
||||||
|
uint32_t, y,
|
||||||
|
uint32_t, width,
|
||||||
|
uint32_t, height
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET(WindowSetPosition,
|
||||||
|
int32_t, x,
|
||||||
|
int32_t, y
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET(WindowSetAttributes,
|
||||||
|
bool, title_bar,
|
||||||
|
bool, rounded_corners,
|
||||||
|
bool, movable,
|
||||||
|
bool, alpha_channel
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace EventPacket
|
||||||
|
{
|
||||||
|
|
||||||
|
DEFINE_PACKET(
|
||||||
|
DestroyWindowEvent
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET(
|
||||||
|
CloseWindowEvent
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET_EXTRA(
|
||||||
|
KeyEvent,
|
||||||
|
using event_t = LibInput::KeyEvent,
|
||||||
|
event_t, event
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET_EXTRA(
|
||||||
|
MouseButtonEvent,
|
||||||
|
struct event_t {
|
||||||
|
LibInput::MouseButton button;
|
||||||
|
bool pressed;
|
||||||
|
int32_t x;
|
||||||
|
int32_t y;
|
||||||
|
},
|
||||||
|
event_t, event
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET_EXTRA(
|
||||||
|
MouseMoveEvent,
|
||||||
|
struct event_t {
|
||||||
|
int32_t x;
|
||||||
|
int32_t y;
|
||||||
|
},
|
||||||
|
event_t, event
|
||||||
|
);
|
||||||
|
|
||||||
|
DEFINE_PACKET_EXTRA(
|
||||||
|
MouseScrollEvent,
|
||||||
|
struct event_t {
|
||||||
|
int32_t scroll;
|
||||||
|
},
|
||||||
|
event_t, event
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,102 +4,24 @@
|
||||||
#include <BAN/StringView.h>
|
#include <BAN/StringView.h>
|
||||||
#include <BAN/UniqPtr.h>
|
#include <BAN/UniqPtr.h>
|
||||||
|
|
||||||
#include <LibInput/KeyEvent.h>
|
#include <LibGUI/Packet.h>
|
||||||
#include <LibInput/MouseEvent.h>
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
namespace LibFont { class Font; }
|
namespace LibFont { class Font; }
|
||||||
|
|
||||||
namespace LibGUI
|
namespace LibGUI
|
||||||
{
|
{
|
||||||
|
|
||||||
static constexpr BAN::StringView s_window_server_socket = "/tmp/window-server.socket"_sv;
|
|
||||||
|
|
||||||
enum WindowPacketType : uint8_t
|
|
||||||
{
|
|
||||||
INVALID,
|
|
||||||
CreateWindow,
|
|
||||||
Invalidate,
|
|
||||||
COUNT
|
|
||||||
};
|
|
||||||
|
|
||||||
struct WindowCreatePacket
|
|
||||||
{
|
|
||||||
WindowPacketType type = WindowPacketType::CreateWindow;
|
|
||||||
uint32_t width;
|
|
||||||
uint32_t height;
|
|
||||||
char title[52];
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
DestroyWindow,
|
|
||||||
CloseWindow,
|
|
||||||
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
|
class Window
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
struct Attributes
|
||||||
|
{
|
||||||
|
bool title_bar { true };
|
||||||
|
bool movable { true };
|
||||||
|
bool rounded_corners { true };
|
||||||
|
bool alpha_channel { false };
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~Window();
|
~Window();
|
||||||
|
|
||||||
|
@ -136,15 +58,20 @@ namespace LibGUI
|
||||||
bool invalidate(int32_t x, int32_t y, uint32_t width, uint32_t height);
|
bool invalidate(int32_t x, int32_t y, uint32_t width, uint32_t height);
|
||||||
bool invalidate() { return invalidate(0, 0, width(), height()); }
|
bool invalidate() { return invalidate(0, 0, width(), height()); }
|
||||||
|
|
||||||
|
bool set_position(int32_t x, int32_t y);
|
||||||
|
|
||||||
|
Attributes get_attributes() const { return m_attributes; }
|
||||||
|
bool set_attributes(Attributes attributes);
|
||||||
|
|
||||||
uint32_t width() const { return m_width; }
|
uint32_t width() const { return m_width; }
|
||||||
uint32_t height() const { return m_height; }
|
uint32_t height() const { return m_height; }
|
||||||
|
|
||||||
void poll_events();
|
void poll_events();
|
||||||
void set_close_window_event_callback(BAN::Function<void()> callback) { m_close_window_event_callback = callback; }
|
void set_close_window_event_callback(BAN::Function<void()> callback) { m_close_window_event_callback = callback; }
|
||||||
void set_key_event_callback(BAN::Function<void(EventPacket::KeyEvent)> callback) { m_key_event_callback = callback; }
|
void set_key_event_callback(BAN::Function<void(EventPacket::KeyEvent::event_t)> 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_button_event_callback(BAN::Function<void(EventPacket::MouseButtonEvent::event_t)> 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_move_event_callback(BAN::Function<void(EventPacket::MouseMoveEvent::event_t)> 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; }
|
void set_mouse_scroll_event_callback(BAN::Function<void(EventPacket::MouseScrollEvent::event_t)> callback) { m_mouse_scroll_event_callback = callback; }
|
||||||
|
|
||||||
int server_fd() const { return m_server_fd; }
|
int server_fd() const { return m_server_fd; }
|
||||||
|
|
||||||
|
@ -162,16 +89,18 @@ namespace LibGUI
|
||||||
private:
|
private:
|
||||||
int m_server_fd;
|
int m_server_fd;
|
||||||
|
|
||||||
|
Attributes m_attributes;
|
||||||
|
|
||||||
BAN::Vector<uint32_t> m_framebuffer;
|
BAN::Vector<uint32_t> m_framebuffer;
|
||||||
uint32_t* m_framebuffer_smo;
|
uint32_t* m_framebuffer_smo;
|
||||||
uint32_t m_width;
|
uint32_t m_width;
|
||||||
uint32_t m_height;
|
uint32_t m_height;
|
||||||
|
|
||||||
BAN::Function<void()> m_close_window_event_callback;
|
BAN::Function<void()> m_close_window_event_callback;
|
||||||
BAN::Function<void(EventPacket::KeyEvent)> m_key_event_callback;
|
BAN::Function<void(EventPacket::KeyEvent::event_t)> m_key_event_callback;
|
||||||
BAN::Function<void(EventPacket::MouseButtonEvent)> m_mouse_button_event_callback;
|
BAN::Function<void(EventPacket::MouseButtonEvent::event_t)> m_mouse_button_event_callback;
|
||||||
BAN::Function<void(EventPacket::MouseMoveEvent)> m_mouse_move_event_callback;
|
BAN::Function<void(EventPacket::MouseMoveEvent::event_t)> m_mouse_move_event_callback;
|
||||||
BAN::Function<void(EventPacket::MouseScrollEvent)> m_mouse_scroll_event_callback;
|
BAN::Function<void(EventPacket::MouseScrollEvent::event_t)> m_mouse_scroll_event_callback;
|
||||||
|
|
||||||
friend class BAN::UniqPtr<Window>;
|
friend class BAN::UniqPtr<Window>;
|
||||||
};
|
};
|
||||||
|
|
|
@ -30,6 +30,7 @@ set(USERSPACE_PROGRAMS
|
||||||
sudo
|
sudo
|
||||||
sync
|
sync
|
||||||
tee
|
tee
|
||||||
|
TaskBar
|
||||||
Terminal
|
Terminal
|
||||||
touch
|
touch
|
||||||
u8sum
|
u8sum
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
set(SOURCES
|
||||||
|
main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
add_executable(TaskBar ${SOURCES})
|
||||||
|
banan_link_library(TaskBar ban)
|
||||||
|
banan_link_library(TaskBar libc)
|
||||||
|
banan_link_library(TaskBar libfont)
|
||||||
|
banan_link_library(TaskBar libgui)
|
||||||
|
banan_include_headers(TaskBar libinput)
|
||||||
|
|
||||||
|
install(TARGETS TaskBar OPTIONAL)
|
|
@ -0,0 +1,71 @@
|
||||||
|
#include <LibFont/Font.h>
|
||||||
|
#include <LibGUI/Window.h>
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
constexpr uint32_t padding = 3;
|
||||||
|
constexpr uint32_t bg_color = 0xFF202020;
|
||||||
|
constexpr uint32_t fg_color = 0xFFFFFFFF;
|
||||||
|
|
||||||
|
auto font = MUST(LibFont::Font::load("/usr/share/fonts/lat0-16.psfu"_sv));
|
||||||
|
|
||||||
|
auto window = MUST(LibGUI::Window::create(0, font.height() + 2 * padding, "TaskBar"));
|
||||||
|
|
||||||
|
auto attributes = window->get_attributes();
|
||||||
|
attributes.title_bar = false;
|
||||||
|
attributes.movable = false;
|
||||||
|
attributes.alpha_channel = false;
|
||||||
|
attributes.rounded_corners = false;
|
||||||
|
window->set_attributes(attributes);
|
||||||
|
|
||||||
|
window->set_position(0, 0);
|
||||||
|
|
||||||
|
window->fill(bg_color);
|
||||||
|
window->invalidate();
|
||||||
|
|
||||||
|
time_t last_update;
|
||||||
|
const auto update_time_string =
|
||||||
|
[&]()
|
||||||
|
{
|
||||||
|
last_update = time(nullptr);
|
||||||
|
BAN::StringView time_sv = ctime(&last_update);
|
||||||
|
time_sv = time_sv.substring(0, time_sv.size() - 1); // new line
|
||||||
|
|
||||||
|
const uint32_t text_w = time_sv.size() * font.width();
|
||||||
|
const uint32_t text_h = font.height();
|
||||||
|
const uint32_t text_x = window->width() - text_w - padding;
|
||||||
|
const uint32_t text_y = padding;
|
||||||
|
|
||||||
|
window->fill_rect(text_x, text_y, text_w, text_h, bg_color);
|
||||||
|
window->draw_text(time_sv, font, text_x, text_y, fg_color);
|
||||||
|
window->invalidate(text_x, text_y, text_w, text_h);
|
||||||
|
};
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
update_time_string();
|
||||||
|
|
||||||
|
constexpr uint64_t ns_per_s = 1'000'000'000;
|
||||||
|
|
||||||
|
timespec current_ts;
|
||||||
|
clock_gettime(CLOCK_REALTIME, ¤t_ts);
|
||||||
|
|
||||||
|
uint64_t current_ns = 0;
|
||||||
|
current_ns += current_ts.tv_sec * ns_per_s;
|
||||||
|
current_ns += current_ts.tv_nsec;
|
||||||
|
|
||||||
|
uint64_t target_ns = current_ns;
|
||||||
|
if (auto rem = target_ns % ns_per_s)
|
||||||
|
target_ns += ns_per_s - rem;
|
||||||
|
|
||||||
|
uint64_t sleep_ns = target_ns - current_ns;
|
||||||
|
|
||||||
|
timespec sleep_ts;
|
||||||
|
sleep_ts.tv_sec = sleep_ns / ns_per_s;
|
||||||
|
sleep_ts.tv_nsec = sleep_ns % ns_per_s;
|
||||||
|
|
||||||
|
nanosleep(&sleep_ts, nullptr);
|
||||||
|
}
|
||||||
|
}
|
|
@ -111,6 +111,11 @@ void Terminal::run()
|
||||||
m_fg_color = s_colors_bright[7];
|
m_fg_color = s_colors_bright[7];
|
||||||
|
|
||||||
m_window = MUST(LibGUI::Window::create(600, 400, "Terminal"_sv));
|
m_window = MUST(LibGUI::Window::create(600, 400, "Terminal"_sv));
|
||||||
|
|
||||||
|
auto attributes = m_window->get_attributes();
|
||||||
|
attributes.alpha_channel = true;
|
||||||
|
m_window->set_attributes(attributes);
|
||||||
|
|
||||||
m_window->fill(m_bg_color);
|
m_window->fill(m_bg_color);
|
||||||
m_window->invalidate();
|
m_window->invalidate();
|
||||||
|
|
||||||
|
@ -126,7 +131,7 @@ void Terminal::run()
|
||||||
MUST(m_cursor_buffer.resize(m_font.width() * m_font.height(), m_bg_color));
|
MUST(m_cursor_buffer.resize(m_font.width() * m_font.height(), m_bg_color));
|
||||||
show_cursor();
|
show_cursor();
|
||||||
|
|
||||||
m_window->set_key_event_callback([&](LibGUI::EventPacket::KeyEvent event) { on_key_event(event); });
|
m_window->set_key_event_callback([&](LibGUI::EventPacket::KeyEvent::event_t event) { on_key_event(event); });
|
||||||
|
|
||||||
const int max_fd = BAN::Math::max(m_shell_info.pts_master, m_window->server_fd());
|
const int max_fd = BAN::Math::max(m_shell_info.pts_master, m_window->server_fd());
|
||||||
while (!s_shell_exited)
|
while (!s_shell_exited)
|
||||||
|
@ -576,7 +581,7 @@ Rectangle Terminal::putchar(uint8_t ch)
|
||||||
return should_invalidate;
|
return should_invalidate;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Terminal::on_key_event(LibGUI::EventPacket::KeyEvent event)
|
void Terminal::on_key_event(LibGUI::EventPacket::KeyEvent::event_t event)
|
||||||
{
|
{
|
||||||
if (event.released())
|
if (event.released())
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -43,7 +43,7 @@ private:
|
||||||
void hide_cursor();
|
void hide_cursor();
|
||||||
void show_cursor();
|
void show_cursor();
|
||||||
|
|
||||||
void on_key_event(LibGUI::EventPacket::KeyEvent);
|
void on_key_event(LibGUI::EventPacket::KeyEvent::event_t);
|
||||||
|
|
||||||
void start_shell();
|
void start_shell();
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,8 @@ struct Rectangle
|
||||||
|
|
||||||
BAN::Optional<Rectangle> get_overlap(Rectangle other) const
|
BAN::Optional<Rectangle> get_overlap(Rectangle other) const
|
||||||
{
|
{
|
||||||
|
if (height == 0 || width == 0 || other.width == 0 || other.height == 0)
|
||||||
|
return {};
|
||||||
const auto min_x = BAN::Math::max(x, other.x);
|
const auto min_x = BAN::Math::max(x, other.x);
|
||||||
const auto min_y = BAN::Math::max(y, other.y);
|
const auto min_y = BAN::Math::max(y, other.y);
|
||||||
const auto max_x = BAN::Math::min(x + width, other.x + other.width);
|
const auto max_x = BAN::Math::min(x + width, other.x + other.width);
|
||||||
|
|
|
@ -19,7 +19,8 @@ Window::Window(int fd, Rectangle area, long smo_key, BAN::StringView title, cons
|
||||||
|
|
||||||
m_fb_addr = static_cast<uint32_t*>(smo_map(smo_key));
|
m_fb_addr = static_cast<uint32_t*>(smo_map(smo_key));
|
||||||
ASSERT(m_fb_addr);
|
ASSERT(m_fb_addr);
|
||||||
memset(m_fb_addr, 0, client_width() * client_height() * 4);
|
|
||||||
|
memset(m_fb_addr, 0xFF, client_width() * client_height() * 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
Window::~Window()
|
Window::~Window()
|
||||||
|
@ -27,9 +28,8 @@ Window::~Window()
|
||||||
munmap(m_fb_addr, client_width() * client_height() * 4);
|
munmap(m_fb_addr, client_width() * client_height() * 4);
|
||||||
smo_delete(m_smo_key);
|
smo_delete(m_smo_key);
|
||||||
|
|
||||||
LibGUI::EventPacket event;
|
LibGUI::EventPacket::DestroyWindowEvent packet;
|
||||||
event.type = LibGUI::EventPacket::Type::DestroyWindow;
|
(void)packet.send_serialized(m_client_fd);
|
||||||
send(m_client_fd, &event, sizeof(event), 0);
|
|
||||||
close(m_client_fd);
|
close(m_client_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <BAN/String.h>
|
#include <BAN/String.h>
|
||||||
|
|
||||||
#include <LibFont/Font.h>
|
#include <LibFont/Font.h>
|
||||||
|
#include <LibGUI/Window.h>
|
||||||
|
|
||||||
class Window : public BAN::RefCounted<Window>
|
class Window : public BAN::RefCounted<Window>
|
||||||
{
|
{
|
||||||
|
@ -29,9 +30,9 @@ public:
|
||||||
Rectangle client_area() const { return m_client_area; }
|
Rectangle client_area() const { return m_client_area; }
|
||||||
|
|
||||||
int32_t title_bar_x() const { return client_x(); }
|
int32_t title_bar_x() const { return client_x(); }
|
||||||
int32_t title_bar_y() const { return client_y() - title_bar_height(); }
|
int32_t title_bar_y() const { return m_attributes.title_bar ? client_y() - title_bar_height() : client_y(); }
|
||||||
int32_t title_bar_width() const { return client_width(); }
|
int32_t title_bar_width() const { return client_width(); }
|
||||||
int32_t title_bar_height() const { return m_title_bar_height; }
|
int32_t title_bar_height() const { return m_attributes.title_bar ? m_title_bar_height : 0; }
|
||||||
Rectangle title_bar_size() const { return { 0, 0, title_bar_width(), title_bar_height() }; }
|
Rectangle title_bar_size() const { return { 0, 0, title_bar_width(), title_bar_height() }; }
|
||||||
Rectangle title_bar_area() const { return { title_bar_x(), title_bar_y(), title_bar_width(), title_bar_height() }; }
|
Rectangle title_bar_area() const { return { title_bar_x(), title_bar_y(), title_bar_width(), title_bar_height() }; }
|
||||||
|
|
||||||
|
@ -42,6 +43,9 @@ public:
|
||||||
Rectangle full_size() const { return { 0, 0, full_width(), full_height() }; }
|
Rectangle full_size() const { return { 0, 0, full_width(), full_height() }; }
|
||||||
Rectangle full_area() const { return { full_x(), full_y(), full_width(), full_height() }; }
|
Rectangle full_area() const { return { full_x(), full_y(), full_width(), full_height() }; }
|
||||||
|
|
||||||
|
LibGUI::Window::Attributes get_attributes() const { return m_attributes; };
|
||||||
|
void set_attributes(LibGUI::Window::Attributes attributes) { m_attributes = attributes; };
|
||||||
|
|
||||||
const uint32_t* framebuffer() const { return m_fb_addr; }
|
const uint32_t* framebuffer() const { return m_fb_addr; }
|
||||||
|
|
||||||
uint32_t title_bar_pixel(int32_t abs_x, int32_t abs_y, Position cursor) const
|
uint32_t title_bar_pixel(int32_t abs_x, int32_t abs_y, Position cursor) const
|
||||||
|
@ -72,5 +76,7 @@ private:
|
||||||
uint32_t* m_title_bar_data { nullptr };
|
uint32_t* m_title_bar_data { nullptr };
|
||||||
BAN::String m_title;
|
BAN::String m_title;
|
||||||
|
|
||||||
|
LibGUI::Window::Attributes m_attributes;
|
||||||
|
|
||||||
friend class BAN::RefPtr<Window>;
|
friend class BAN::RefPtr<Window>;
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include "WindowServer.h"
|
#include "WindowServer.h"
|
||||||
|
|
||||||
#include <BAN/Debug.h>
|
#include <BAN/Debug.h>
|
||||||
|
#include <BAN/ScopeGuard.h>
|
||||||
|
|
||||||
#include <LibGUI/Window.h>
|
#include <LibGUI/Window.h>
|
||||||
#include <LibInput/KeyboardLayout.h>
|
#include <LibInput/KeyboardLayout.h>
|
||||||
|
@ -32,101 +33,162 @@ BAN::ErrorOr<void> WindowServer::set_background_image(BAN::UniqPtr<LibImage::Ima
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowServer::on_window_packet(int fd, LibGUI::WindowPacket packet)
|
void WindowServer::on_window_create(int fd, const LibGUI::WindowPacket::WindowCreate& packet)
|
||||||
{
|
{
|
||||||
switch (packet.type)
|
|
||||||
{
|
|
||||||
case LibGUI::WindowPacketType::CreateWindow:
|
|
||||||
{
|
|
||||||
// FIXME: This should be probably allowed
|
|
||||||
for (auto& window : m_client_windows)
|
for (auto& window : m_client_windows)
|
||||||
{
|
{
|
||||||
if (window->client_fd() == fd)
|
if (window->client_fd() != fd)
|
||||||
{
|
continue;
|
||||||
dwarnln("client {} tried to create window while already owning a window", fd);
|
dwarnln("client with window tried to create another one");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const size_t window_fb_bytes = packet.create.width * packet.create.height * 4;
|
const uint32_t width = packet.width ? packet.width : m_framebuffer.width;
|
||||||
|
const uint32_t height = packet.height ? packet.height : m_framebuffer.height;
|
||||||
|
|
||||||
|
const size_t window_fb_bytes = width * height * 4;
|
||||||
|
|
||||||
long smo_key = smo_create(window_fb_bytes, PROT_READ | PROT_WRITE);
|
long smo_key = smo_create(window_fb_bytes, PROT_READ | PROT_WRITE);
|
||||||
if (smo_key == -1)
|
if (smo_key == -1)
|
||||||
{
|
{
|
||||||
dwarnln("smo_create: {}", strerror(errno));
|
dwarnln("smo_create: {}", strerror(errno));
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
BAN::ScopeGuard smo_deleter([smo_key] { smo_delete(smo_key); });
|
||||||
|
|
||||||
Rectangle window_area {
|
Rectangle window_area {
|
||||||
static_cast<int32_t>((m_framebuffer.width - packet.create.width) / 2),
|
static_cast<int32_t>((m_framebuffer.width - width) / 2),
|
||||||
static_cast<int32_t>((m_framebuffer.height - packet.create.height) / 2),
|
static_cast<int32_t>((m_framebuffer.height - height) / 2),
|
||||||
static_cast<int32_t>(packet.create.width),
|
static_cast<int32_t>(width),
|
||||||
static_cast<int32_t>(packet.create.height)
|
static_cast<int32_t>(height)
|
||||||
};
|
};
|
||||||
|
|
||||||
packet.create.title[sizeof(packet.create.title) - 1] = '\0';
|
|
||||||
|
|
||||||
// Window::Window(int fd, Rectangle area, long smo_key, BAN::StringView title, const LibFont::Font& font)
|
// Window::Window(int fd, Rectangle area, long smo_key, BAN::StringView title, const LibFont::Font& font)
|
||||||
auto window = MUST(BAN::RefPtr<Window>::create(
|
auto window_or_error = (BAN::RefPtr<Window>::create(
|
||||||
fd,
|
fd,
|
||||||
window_area,
|
window_area,
|
||||||
smo_key,
|
smo_key,
|
||||||
packet.create.title,
|
packet.title,
|
||||||
m_font
|
m_font
|
||||||
));
|
));
|
||||||
MUST(m_client_windows.push_back(window));
|
if (window_or_error.is_error())
|
||||||
|
{
|
||||||
|
dwarnln("could not create window for client: {}", window_or_error.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto window = window_or_error.release_value();
|
||||||
|
|
||||||
|
if (auto ret = m_client_windows.push_back(window); ret.is_error())
|
||||||
|
{
|
||||||
|
dwarnln("could not create window for client: {}", ret.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
BAN::ScopeGuard window_popper([&] { m_client_windows.pop_back(); });
|
||||||
|
|
||||||
|
LibGUI::WindowPacket::WindowCreateResponse response;
|
||||||
|
response.width = width;
|
||||||
|
response.height = height;
|
||||||
|
response.smo_key = smo_key;
|
||||||
|
if (auto ret = response.send_serialized(fd); ret.is_error())
|
||||||
|
{
|
||||||
|
dwarnln("could not respond to window create request: {}", ret.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
smo_deleter.disable();
|
||||||
|
window_popper.disable();
|
||||||
|
|
||||||
set_focused_window(window);
|
set_focused_window(window);
|
||||||
|
|
||||||
LibGUI::WindowCreateResponse response;
|
|
||||||
response.framebuffer_smo_key = smo_key;
|
|
||||||
if (send(window->client_fd(), &response, sizeof(response), 0) != sizeof(response))
|
|
||||||
{
|
|
||||||
dwarnln("send: {}", strerror(errno));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
void WindowServer::on_window_invalidate(int fd, const LibGUI::WindowPacket::WindowInvalidate& packet)
|
||||||
}
|
|
||||||
case LibGUI::WindowPacketType::Invalidate:
|
|
||||||
{
|
{
|
||||||
if (packet.invalidate.width == 0 || packet.invalidate.height == 0)
|
if (packet.width == 0 || packet.height == 0)
|
||||||
break;
|
return;
|
||||||
|
|
||||||
BAN::RefPtr<Window> target_window;
|
BAN::RefPtr<Window> target_window;
|
||||||
for (auto& window : m_client_windows)
|
for (auto& window : m_client_windows)
|
||||||
{
|
{
|
||||||
if (window->client_fd() == fd)
|
if (window->client_fd() != fd)
|
||||||
{
|
continue;
|
||||||
target_window = window;
|
target_window = window;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (!target_window)
|
if (!target_window)
|
||||||
{
|
{
|
||||||
dwarnln("client {} tried to invalidate window while not owning a window", fd);
|
dwarnln("client tried to invalidate window while not owning a window");
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int32_t br_x = packet.invalidate.x + packet.invalidate.width - 1;
|
const int32_t br_x = packet.x + packet.width - 1;
|
||||||
const int32_t br_y = packet.invalidate.y + packet.invalidate.height - 1;
|
const int32_t br_y = packet.y + packet.height - 1;
|
||||||
if (!target_window->client_size().contains({ br_x, br_y }))
|
if (!target_window->client_size().contains({ br_x, br_y }))
|
||||||
{
|
{
|
||||||
dwarnln("Invalid Invalidate packet parameters");
|
dwarnln("invalid Invalidate packet parameters");
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
invalidate({
|
invalidate({
|
||||||
target_window->client_x() + static_cast<int32_t>(packet.invalidate.x),
|
target_window->client_x() + static_cast<int32_t>(packet.x),
|
||||||
target_window->client_y() + static_cast<int32_t>(packet.invalidate.y),
|
target_window->client_y() + static_cast<int32_t>(packet.y),
|
||||||
static_cast<int32_t>(packet.invalidate.width),
|
static_cast<int32_t>(packet.width),
|
||||||
static_cast<int32_t>(packet.invalidate.height),
|
static_cast<int32_t>(packet.height),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowServer::on_window_set_position(int fd, const LibGUI::WindowPacket::WindowSetPosition& packet)
|
||||||
|
{
|
||||||
|
BAN::RefPtr<Window> target_window;
|
||||||
|
for (auto& window : m_client_windows)
|
||||||
|
{
|
||||||
|
if (window->client_fd() != fd)
|
||||||
|
continue;
|
||||||
|
target_window = window;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
ASSERT_NOT_REACHED();
|
if (!target_window)
|
||||||
|
{
|
||||||
|
dwarnln("client tried to set window position while not owning a window");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto old_client_area = target_window->full_area();
|
||||||
|
target_window->set_position({
|
||||||
|
.x = packet.x,
|
||||||
|
.y = packet.y,
|
||||||
|
});
|
||||||
|
const auto new_client_area = target_window->full_area();
|
||||||
|
invalidate(new_client_area.get_bounding_box(old_client_area));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WindowServer::on_window_set_attributes(int fd, const LibGUI::WindowPacket::WindowSetAttributes& packet)
|
||||||
|
{
|
||||||
|
BAN::RefPtr<Window> target_window;
|
||||||
|
for (auto& window : m_client_windows)
|
||||||
|
{
|
||||||
|
if (window->client_fd() != fd)
|
||||||
|
continue;
|
||||||
|
target_window = window;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!target_window)
|
||||||
|
{
|
||||||
|
dwarnln("client tried to set window attributes while not owning a window");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto old_client_area = target_window->full_area();
|
||||||
|
target_window->set_attributes({
|
||||||
|
.title_bar = packet.title_bar,
|
||||||
|
.movable = packet.movable,
|
||||||
|
.rounded_corners = packet.rounded_corners,
|
||||||
|
.alpha_channel = packet.alpha_channel,
|
||||||
|
});
|
||||||
|
const auto new_client_area = target_window->full_area();
|
||||||
|
invalidate(new_client_area.get_bounding_box(old_client_area));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowServer::on_key_event(LibInput::KeyEvent event)
|
void WindowServer::on_key_event(LibInput::KeyEvent event)
|
||||||
|
@ -173,10 +235,10 @@ void WindowServer::on_key_event(LibInput::KeyEvent event)
|
||||||
|
|
||||||
if (m_focused_window)
|
if (m_focused_window)
|
||||||
{
|
{
|
||||||
LibGUI::EventPacket packet;
|
LibGUI::EventPacket::KeyEvent packet;
|
||||||
packet.type = LibGUI::EventPacket::Type::KeyEvent;
|
packet.event = event;
|
||||||
packet.key_event = event;
|
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
|
||||||
send(m_focused_window->client_fd(), &packet, sizeof(packet), 0);
|
dwarnln("could not send key event: {}", ret.error());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,26 +263,32 @@ void WindowServer::on_mouse_button(LibInput::MouseButtonEvent event)
|
||||||
// Handle window moving when mod key is held or mouse press on title bar
|
// Handle window moving when mod key is held or mouse press on title bar
|
||||||
const bool can_start_move = m_is_mod_key_held || target_window->title_text_area().contains(m_cursor);
|
const bool can_start_move = m_is_mod_key_held || target_window->title_text_area().contains(m_cursor);
|
||||||
if (event.pressed && event.button == LibInput::MouseButton::Left && !m_is_moving_window && can_start_move)
|
if (event.pressed && event.button == LibInput::MouseButton::Left && !m_is_moving_window && can_start_move)
|
||||||
m_is_moving_window = true;
|
m_is_moving_window = target_window->get_attributes().movable;
|
||||||
else if (m_is_moving_window && !event.pressed)
|
else if (m_is_moving_window && !event.pressed)
|
||||||
m_is_moving_window = false;
|
m_is_moving_window = false;
|
||||||
else if (!event.pressed && event.button == LibInput::MouseButton::Left && target_window->close_button_area().contains(m_cursor))
|
else if (!event.pressed && event.button == LibInput::MouseButton::Left && target_window->close_button_area().contains(m_cursor))
|
||||||
{
|
{
|
||||||
// NOTE: we always have target window if code reaches here
|
// NOTE: we always have target window if code reaches here
|
||||||
LibGUI::EventPacket packet;
|
LibGUI::EventPacket::CloseWindowEvent packet;
|
||||||
packet.type = LibGUI::EventPacket::Type::CloseWindow;
|
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
|
||||||
send(m_focused_window->client_fd(), &packet, sizeof(packet), 0);
|
{
|
||||||
|
dwarnln("could not send close window event: {}", ret.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (target_window->client_area().contains(m_cursor))
|
else if (target_window->client_area().contains(m_cursor))
|
||||||
{
|
{
|
||||||
// NOTE: we always have target window if code reaches here
|
// NOTE: we always have target window if code reaches here
|
||||||
LibGUI::EventPacket packet;
|
LibGUI::EventPacket::MouseButtonEvent packet;
|
||||||
packet.type = LibGUI::EventPacket::Type::MouseButtonEvent;
|
packet.event.button = event.button;
|
||||||
packet.mouse_button_event.button = event.button;
|
packet.event.pressed = event.pressed;
|
||||||
packet.mouse_button_event.pressed = event.pressed;
|
packet.event.x = m_cursor.x - m_focused_window->client_x();
|
||||||
packet.mouse_button_event.x = m_cursor.x - m_focused_window->client_x();
|
packet.event.y = m_cursor.y - m_focused_window->client_y();
|
||||||
packet.mouse_button_event.y = m_cursor.y - m_focused_window->client_y();
|
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
|
||||||
send(m_focused_window->client_fd(), &packet, sizeof(packet), 0);
|
{
|
||||||
|
dwarnln("could not send mouse button event event: {}", ret.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,11 +333,14 @@ void WindowServer::on_mouse_move(LibInput::MouseMoveEvent event)
|
||||||
|
|
||||||
if (m_focused_window)
|
if (m_focused_window)
|
||||||
{
|
{
|
||||||
LibGUI::EventPacket packet;
|
LibGUI::EventPacket::MouseMoveEvent packet;
|
||||||
packet.type = LibGUI::EventPacket::Type::MouseMoveEvent;
|
packet.event.x = m_cursor.x - m_focused_window->client_x();
|
||||||
packet.mouse_move_event.x = m_cursor.x - m_focused_window->client_x();
|
packet.event.y = m_cursor.y - m_focused_window->client_y();
|
||||||
packet.mouse_move_event.y = m_cursor.y - m_focused_window->client_y();
|
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
|
||||||
send(m_focused_window->client_fd(), &packet, sizeof(packet), 0);
|
{
|
||||||
|
dwarnln("could not send mouse move event event: {}", ret.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,10 +348,13 @@ void WindowServer::on_mouse_scroll(LibInput::MouseScrollEvent event)
|
||||||
{
|
{
|
||||||
if (m_focused_window)
|
if (m_focused_window)
|
||||||
{
|
{
|
||||||
LibGUI::EventPacket packet;
|
LibGUI::EventPacket::MouseScrollEvent packet;
|
||||||
packet.type = LibGUI::EventPacket::Type::MouseScrollEvent;
|
packet.event.scroll = event.scroll;
|
||||||
packet.mouse_scroll_event = event;
|
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
|
||||||
send(m_focused_window->client_fd(), &packet, sizeof(packet), 0);
|
{
|
||||||
|
dwarnln("could not send mouse scroll event event: {}", ret.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,6 +413,8 @@ void WindowServer::invalidate(Rectangle area)
|
||||||
m_framebuffer.mmap[y * m_framebuffer.width + x] = 0xFF101010;
|
m_framebuffer.mmap[y * m_framebuffer.width + x] = 0xFF101010;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: this loop should be inverse order and terminate
|
||||||
|
// after window without alpha channel is found
|
||||||
for (auto& pwindow : m_client_windows)
|
for (auto& pwindow : m_client_windows)
|
||||||
{
|
{
|
||||||
auto& window = *pwindow;
|
auto& window = *pwindow;
|
||||||
|
@ -411,8 +487,10 @@ void WindowServer::invalidate(Rectangle area)
|
||||||
};
|
};
|
||||||
|
|
||||||
const auto is_rounded_off =
|
const auto is_rounded_off =
|
||||||
[&](Position pos) -> bool
|
[&](const Window& window, Position pos) -> bool
|
||||||
{
|
{
|
||||||
|
if (!window.get_attributes().rounded_corners)
|
||||||
|
return false;
|
||||||
for (int32_t i = 0; i < 4; i++)
|
for (int32_t i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
if (!corner_areas[i].contains(pos))
|
if (!corner_areas[i].contains(pos))
|
||||||
|
@ -436,7 +514,7 @@ void WindowServer::invalidate(Rectangle area)
|
||||||
{
|
{
|
||||||
const int32_t abs_x = title_overlap->x + x_off;
|
const int32_t abs_x = title_overlap->x + x_off;
|
||||||
const int32_t abs_y = title_overlap->y + y_off;
|
const int32_t abs_y = title_overlap->y + y_off;
|
||||||
if (is_rounded_off({ abs_x, abs_y }))
|
if (is_rounded_off(window, { abs_x, abs_y }))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const uint32_t color = window.title_bar_pixel(abs_x, abs_y, m_cursor);
|
const uint32_t color = window.title_bar_pixel(abs_x, abs_y, m_cursor);
|
||||||
|
@ -464,12 +542,14 @@ void WindowServer::invalidate(Rectangle area)
|
||||||
auto* window_row = &window.framebuffer()[src_row_y * window.client_width() + src_row_x];
|
auto* window_row = &window.framebuffer()[src_row_y * window.client_width() + src_row_x];
|
||||||
auto* frameb_row = &m_framebuffer.mmap[ abs_row_y * m_framebuffer.width + abs_row_x];
|
auto* frameb_row = &m_framebuffer.mmap[ abs_row_y * m_framebuffer.width + abs_row_x];
|
||||||
|
|
||||||
|
const bool should_alpha_blend = window.get_attributes().alpha_channel;
|
||||||
for (int32_t i = 0; i < fast_overlap->width; i++)
|
for (int32_t i = 0; i < fast_overlap->width; i++)
|
||||||
{
|
{
|
||||||
const uint32_t color_a = *window_row;
|
const uint32_t color_a = *window_row;
|
||||||
const uint32_t color_b = *frameb_row;
|
const uint32_t color_b = *frameb_row;
|
||||||
*frameb_row = alpha_blend(color_a, color_b);
|
*frameb_row = should_alpha_blend
|
||||||
|
? alpha_blend(color_a, color_b)
|
||||||
|
: color_a;
|
||||||
window_row++;
|
window_row++;
|
||||||
frameb_row++;
|
frameb_row++;
|
||||||
}
|
}
|
||||||
|
@ -487,7 +567,7 @@ void WindowServer::invalidate(Rectangle area)
|
||||||
{
|
{
|
||||||
const int32_t abs_x = corner_overlap->x + x_off;
|
const int32_t abs_x = corner_overlap->x + x_off;
|
||||||
const int32_t abs_y = corner_overlap->y + y_off;
|
const int32_t abs_y = corner_overlap->y + y_off;
|
||||||
if (is_rounded_off({ abs_x, abs_y }))
|
if (is_rounded_off(window, { abs_x, abs_y }))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const int32_t src_x = abs_x - window.client_x();
|
const int32_t src_x = abs_x - window.client_x();
|
||||||
|
@ -496,7 +576,10 @@ void WindowServer::invalidate(Rectangle area)
|
||||||
const uint32_t color_a = window.framebuffer()[src_y * window.client_width() + src_x];
|
const uint32_t color_a = window.framebuffer()[src_y * window.client_width() + src_x];
|
||||||
const uint32_t color_b = m_framebuffer.mmap[abs_y * m_framebuffer.width + abs_x];
|
const uint32_t color_b = m_framebuffer.mmap[abs_y * m_framebuffer.width + abs_x];
|
||||||
|
|
||||||
m_framebuffer.mmap[abs_y * m_framebuffer.width + abs_x] = alpha_blend(color_a, color_b);
|
const bool should_alpha_blend = window.get_attributes().alpha_channel;
|
||||||
|
m_framebuffer.mmap[abs_y * m_framebuffer.width + abs_x] = should_alpha_blend
|
||||||
|
? alpha_blend(color_a, color_b)
|
||||||
|
: color_a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -595,19 +678,19 @@ Rectangle WindowServer::cursor_area() const
|
||||||
|
|
||||||
void WindowServer::add_client_fd(int fd)
|
void WindowServer::add_client_fd(int fd)
|
||||||
{
|
{
|
||||||
MUST(m_client_fds.push_back(fd));
|
if (auto ret = m_client_data.emplace(fd); ret.is_error())
|
||||||
|
{
|
||||||
|
dwarnln("could not add client: {}", ret.error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowServer::remove_client_fd(int fd)
|
void WindowServer::remove_client_fd(int fd)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < m_client_fds.size(); i++)
|
auto it = m_client_data.find(fd);
|
||||||
{
|
if (it == m_client_data.end())
|
||||||
if (m_client_fds[i] == fd)
|
return;
|
||||||
{
|
m_client_data.remove(it);
|
||||||
m_client_fds.remove(i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < m_client_windows.size(); i++)
|
for (size_t i = 0; i < m_client_windows.size(); i++)
|
||||||
{
|
{
|
||||||
|
@ -635,7 +718,7 @@ void WindowServer::remove_client_fd(int fd)
|
||||||
int WindowServer::get_client_fds(fd_set& fds) const
|
int WindowServer::get_client_fds(fd_set& fds) const
|
||||||
{
|
{
|
||||||
int max_fd = 0;
|
int max_fd = 0;
|
||||||
for (int fd : m_client_fds)
|
for (const auto& [fd, _] : m_client_data)
|
||||||
{
|
{
|
||||||
FD_SET(fd, &fds);
|
FD_SET(fd, &fds);
|
||||||
max_fd = BAN::Math::max(max_fd, fd);
|
max_fd = BAN::Math::max(max_fd, fd);
|
||||||
|
@ -643,13 +726,13 @@ int WindowServer::get_client_fds(fd_set& fds) const
|
||||||
return max_fd;
|
return max_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowServer::for_each_client_fd(const BAN::Function<BAN::Iteration(int)>& callback)
|
void WindowServer::for_each_client_fd(const BAN::Function<BAN::Iteration(int, ClientData&)>& callback)
|
||||||
{
|
{
|
||||||
m_deleted_window = false;
|
m_deleted_window = false;
|
||||||
for (int fd : m_client_fds)
|
for (auto& [fd, cliend_data] : m_client_data)
|
||||||
{
|
{
|
||||||
if (m_deleted_window)
|
if (m_deleted_window)
|
||||||
break;
|
break;
|
||||||
callback(fd);
|
callback(fd, cliend_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,22 @@
|
||||||
|
|
||||||
class WindowServer
|
class WindowServer
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
struct ClientData
|
||||||
|
{
|
||||||
|
size_t packet_buffer_nread = 0;
|
||||||
|
BAN::Vector<uint8_t> packet_buffer;
|
||||||
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WindowServer(Framebuffer& framebuffer, int32_t corner_radius);
|
WindowServer(Framebuffer& framebuffer, int32_t corner_radius);
|
||||||
|
|
||||||
BAN::ErrorOr<void> set_background_image(BAN::UniqPtr<LibImage::Image>);
|
BAN::ErrorOr<void> set_background_image(BAN::UniqPtr<LibImage::Image>);
|
||||||
|
|
||||||
void on_window_packet(int fd, LibGUI::WindowPacket);
|
void on_window_create(int fd, const LibGUI::WindowPacket::WindowCreate&);
|
||||||
|
void on_window_invalidate(int fd, const LibGUI::WindowPacket::WindowInvalidate&);
|
||||||
|
void on_window_set_position(int fd, const LibGUI::WindowPacket::WindowSetPosition&);
|
||||||
|
void on_window_set_attributes(int fd, const LibGUI::WindowPacket::WindowSetAttributes&);
|
||||||
|
|
||||||
void on_key_event(LibInput::KeyEvent event);
|
void on_key_event(LibInput::KeyEvent event);
|
||||||
void on_mouse_button(LibInput::MouseButtonEvent event);
|
void on_mouse_button(LibInput::MouseButtonEvent event);
|
||||||
|
@ -39,14 +49,15 @@ public:
|
||||||
void add_client_fd(int fd);
|
void add_client_fd(int fd);
|
||||||
void remove_client_fd(int fd);
|
void remove_client_fd(int fd);
|
||||||
int get_client_fds(fd_set& fds) const;
|
int get_client_fds(fd_set& fds) const;
|
||||||
void for_each_client_fd(const BAN::Function<BAN::Iteration(int)>& callback);
|
void for_each_client_fd(const BAN::Function<BAN::Iteration(int, ClientData&)>& callback);
|
||||||
|
|
||||||
bool is_stopped() const { return m_is_stopped; }
|
bool is_stopped() const { return m_is_stopped; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Framebuffer& m_framebuffer;
|
Framebuffer& m_framebuffer;
|
||||||
BAN::Vector<BAN::RefPtr<Window>> m_client_windows;
|
BAN::Vector<BAN::RefPtr<Window>> m_client_windows;
|
||||||
BAN::Vector<int> m_client_fds;
|
|
||||||
|
BAN::HashMap<int, ClientData> m_client_data;
|
||||||
|
|
||||||
const int32_t m_corner_radius;
|
const int32_t m_corner_radius;
|
||||||
|
|
||||||
|
|
|
@ -114,7 +114,7 @@ int open_server_fd()
|
||||||
if (stat(LibGUI::s_window_server_socket.data(), &st) != -1)
|
if (stat(LibGUI::s_window_server_socket.data(), &st) != -1)
|
||||||
unlink(LibGUI::s_window_server_socket.data());
|
unlink(LibGUI::s_window_server_socket.data());
|
||||||
|
|
||||||
int server_fd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
|
int server_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||||
if (server_fd == -1)
|
if (server_fd == -1)
|
||||||
{
|
{
|
||||||
perror("socket");
|
perror("socket");
|
||||||
|
@ -178,12 +178,6 @@ int main()
|
||||||
|
|
||||||
dprintln("Window server started");
|
dprintln("Window server started");
|
||||||
|
|
||||||
size_t window_packet_sizes[LibGUI::WindowPacketType::COUNT] {};
|
|
||||||
window_packet_sizes[LibGUI::WindowPacketType::INVALID] = 0;
|
|
||||||
window_packet_sizes[LibGUI::WindowPacketType::CreateWindow] = sizeof(LibGUI::WindowCreatePacket);
|
|
||||||
window_packet_sizes[LibGUI::WindowPacketType::Invalidate] = sizeof(LibGUI::WindowInvalidatePacket);
|
|
||||||
static_assert(LibGUI::WindowPacketType::COUNT == 3);
|
|
||||||
|
|
||||||
auto config = parse_config();
|
auto config = parse_config();
|
||||||
|
|
||||||
WindowServer window_server(framebuffer, config.corner_radius);
|
WindowServer window_server(framebuffer, config.corner_radius);
|
||||||
|
@ -281,13 +275,49 @@ int main()
|
||||||
}
|
}
|
||||||
|
|
||||||
window_server.for_each_client_fd(
|
window_server.for_each_client_fd(
|
||||||
[&](int fd) -> BAN::Iteration
|
[&](int fd, WindowServer::ClientData& client_data) -> BAN::Iteration
|
||||||
{
|
{
|
||||||
if (!FD_ISSET(fd, &fds))
|
if (!FD_ISSET(fd, &fds))
|
||||||
return BAN::Iteration::Continue;
|
return BAN::Iteration::Continue;
|
||||||
|
|
||||||
LibGUI::WindowPacket packet;
|
if (client_data.packet_buffer.empty())
|
||||||
ssize_t nrecv = recv(fd, &packet, sizeof(packet), 0);
|
{
|
||||||
|
uint32_t packet_size;
|
||||||
|
const ssize_t nrecv = recv(fd, &packet_size, sizeof(uint32_t), 0);
|
||||||
|
if (nrecv < 0)
|
||||||
|
dwarnln("recv: {}", strerror(errno));
|
||||||
|
if (nrecv > 0 && nrecv != sizeof(uint32_t))
|
||||||
|
dwarnln("could not read packet size with a single recv call, closing connection...");
|
||||||
|
if (nrecv != sizeof(uint32_t))
|
||||||
|
{
|
||||||
|
window_server.remove_client_fd(fd);
|
||||||
|
return BAN::Iteration::Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (packet_size < 4)
|
||||||
|
{
|
||||||
|
dwarnln("client sent invalid packet, closing connection...");
|
||||||
|
return BAN::Iteration::Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is a bit harsh, but i don't want to work on skipping streaming packets
|
||||||
|
if (client_data.packet_buffer.resize(packet_size).is_error())
|
||||||
|
{
|
||||||
|
dwarnln("could not allocate memory for client packet, closing connection...");
|
||||||
|
window_server.remove_client_fd(fd);
|
||||||
|
return BAN::Iteration::Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
client_data.packet_buffer_nread = 0;
|
||||||
|
return BAN::Iteration::Continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ssize_t nrecv = recv(
|
||||||
|
fd,
|
||||||
|
client_data.packet_buffer.data() + client_data.packet_buffer_nread,
|
||||||
|
client_data.packet_buffer.size() - client_data.packet_buffer_nread,
|
||||||
|
0
|
||||||
|
);
|
||||||
if (nrecv < 0)
|
if (nrecv < 0)
|
||||||
dwarnln("recv: {}", strerror(errno));
|
dwarnln("recv: {}", strerror(errno));
|
||||||
if (nrecv <= 0)
|
if (nrecv <= 0)
|
||||||
|
@ -296,12 +326,36 @@ int main()
|
||||||
return BAN::Iteration::Continue;
|
return BAN::Iteration::Continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packet.type == LibGUI::WindowPacketType::INVALID || packet.type >= LibGUI::WindowPacketType::COUNT)
|
client_data.packet_buffer_nread += nrecv;
|
||||||
dwarnln("Invalid WindowPacket (type {})", (int)packet.type);
|
if (client_data.packet_buffer_nread < client_data.packet_buffer.size())
|
||||||
if (static_cast<size_t>(nrecv) != window_packet_sizes[packet.type])
|
return BAN::Iteration::Continue;
|
||||||
dwarnln("Invalid WindowPacket size (type {}, size {})", (int)packet.type, nrecv);
|
|
||||||
else
|
ASSERT(client_data.packet_buffer.size() >= sizeof(uint32_t));
|
||||||
window_server.on_window_packet(fd, packet);
|
|
||||||
|
switch (*reinterpret_cast<LibGUI::PacketType*>(client_data.packet_buffer.data()))
|
||||||
|
{
|
||||||
|
case LibGUI::PacketType::WindowCreate:
|
||||||
|
if (auto ret = LibGUI::WindowPacket::WindowCreate::deserialize(client_data.packet_buffer.span()); !ret.is_error())
|
||||||
|
window_server.on_window_create(fd, ret.release_value());
|
||||||
|
break;
|
||||||
|
case LibGUI::PacketType::WindowInvalidate:
|
||||||
|
if (auto ret = LibGUI::WindowPacket::WindowInvalidate::deserialize(client_data.packet_buffer.span()); !ret.is_error())
|
||||||
|
window_server.on_window_invalidate(fd, ret.release_value());
|
||||||
|
break;
|
||||||
|
case LibGUI::PacketType::WindowSetPosition:
|
||||||
|
if (auto ret = LibGUI::WindowPacket::WindowSetPosition::deserialize(client_data.packet_buffer.span()); !ret.is_error())
|
||||||
|
window_server.on_window_set_position(fd, ret.release_value());
|
||||||
|
break;
|
||||||
|
case LibGUI::PacketType::WindowSetAttributes:
|
||||||
|
if (auto ret = LibGUI::WindowPacket::WindowSetAttributes::deserialize(client_data.packet_buffer.span()); !ret.is_error())
|
||||||
|
window_server.on_window_set_attributes(fd, ret.release_value());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
dprintln("unhandled packet type: {}", *reinterpret_cast<uint32_t*>(client_data.packet_buffer.data()));
|
||||||
|
}
|
||||||
|
|
||||||
|
client_data.packet_buffer.clear();
|
||||||
|
client_data.packet_buffer_nread = 0;
|
||||||
return BAN::Iteration::Continue;
|
return BAN::Iteration::Continue;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -32,19 +32,19 @@ int main()
|
||||||
auto window = window_or_error.release_value();
|
auto window = window_or_error.release_value();
|
||||||
window->set_close_window_event_callback([&] { running = false; });
|
window->set_close_window_event_callback([&] { running = false; });
|
||||||
window->set_mouse_button_event_callback(
|
window->set_mouse_button_event_callback(
|
||||||
[&](LibGUI::EventPacket::MouseButtonEvent event)
|
[&](LibGUI::EventPacket::MouseButtonEvent::event_t event)
|
||||||
{
|
{
|
||||||
if (event.pressed && event.button == LibGUI::EventPacket::MouseButton::Left)
|
if (event.pressed && event.button == LibInput::MouseButton::Left)
|
||||||
randomize_color(window);
|
randomize_color(window);
|
||||||
|
|
||||||
const char* button;
|
const char* button;
|
||||||
switch (event.button)
|
switch (event.button)
|
||||||
{
|
{
|
||||||
case LibGUI::EventPacket::MouseButton::Left: button = "left"; break;
|
case LibInput::MouseButton::Left: button = "left"; break;
|
||||||
case LibGUI::EventPacket::MouseButton::Right: button = "right"; break;
|
case LibInput::MouseButton::Right: button = "right"; break;
|
||||||
case LibGUI::EventPacket::MouseButton::Middle: button = "middle"; break;
|
case LibInput::MouseButton::Middle: button = "middle"; break;
|
||||||
case LibGUI::EventPacket::MouseButton::Extra1: button = "extra1"; break;
|
case LibInput::MouseButton::Extra1: button = "extra1"; break;
|
||||||
case LibGUI::EventPacket::MouseButton::Extra2: button = "extra2"; break;
|
case LibInput::MouseButton::Extra2: button = "extra2"; break;
|
||||||
}
|
}
|
||||||
dprintln("mouse button '{}' {} at {}, {}", button, event.pressed ? "pressed" : "released", event.x, event.y);
|
dprintln("mouse button '{}' {} at {}, {}", button, event.pressed ? "pressed" : "released", event.x, event.y);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue