userspace: Use SOCK_STREAM instead of SOCK_SEQPACKET for WindowServer

This makes more sense if we have longer packages
This commit is contained in:
2024-10-17 01:36:59 +03:00
parent ddd3b4c093
commit d7e5c56e94
10 changed files with 568 additions and 292 deletions

View File

@@ -0,0 +1,241 @@
#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,
DestroyWindowEvent,
CloseWindowEvent,
KeyEvent,
MouseButtonEvent,
MouseMoveEvent,
MouseScrollEvent,
};
namespace WindowPacket
{
DEFINE_PACKET(WindowCreate,
uint32_t, width,
uint32_t, height,
BAN::String, title
);
DEFINE_PACKET(WindowCreateResponse,
long, smo_key
);
DEFINE_PACKET(WindowInvalidate,
uint32_t, x,
uint32_t, y,
uint32_t, width,
uint32_t, height
);
}
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
);
}
}

View File

@@ -4,100 +4,13 @@
#include <BAN/StringView.h>
#include <BAN/UniqPtr.h>
#include <LibInput/KeyEvent.h>
#include <LibInput/MouseEvent.h>
#include <limits.h>
#include <stdint.h>
#include <LibGUI/Packet.h>
namespace LibFont { class Font; }
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
{
public:
@@ -140,11 +53,11 @@ namespace LibGUI
uint32_t height() const { return m_height; }
void poll_events();
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_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; }
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::event_t)> callback) { m_key_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::event_t)> callback) { m_mouse_move_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; }
@@ -167,11 +80,11 @@ namespace LibGUI
uint32_t m_width;
uint32_t m_height;
BAN::Function<void()> m_close_window_event_callback;
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;
BAN::Function<void()> m_close_window_event_callback;
BAN::Function<void(EventPacket::KeyEvent::event_t)> m_key_event_callback;
BAN::Function<void(EventPacket::MouseButtonEvent::event_t)> m_mouse_button_event_callback;
BAN::Function<void(EventPacket::MouseMoveEvent::event_t)> m_mouse_move_event_callback;
BAN::Function<void(EventPacket::MouseScrollEvent::event_t)> m_mouse_scroll_event_callback;
friend class BAN::UniqPtr<Window>;
};