LibGUI: Add 10 second timeout for connecting to WindowServer

This commit is contained in:
Bananymous 2024-06-02 17:25:17 +03:00
parent cfdce9be61
commit 64be3f05a3
2 changed files with 45 additions and 8 deletions

View File

@ -1,6 +1,7 @@
#include "LibGUI/Window.h" #include "LibGUI/Window.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h>
#include <sys/banan-os.h> #include <sys/banan-os.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/select.h> #include <sys/select.h>
@ -17,24 +18,46 @@ namespace LibGUI
close(m_server_fd); close(m_server_fd);
} }
BAN::ErrorOr<BAN::UniqPtr<Window>> Window::create(uint32_t width, uint32_t height) BAN::ErrorOr<BAN::UniqPtr<Window>> Window::create(uint32_t width, uint32_t height, BAN::StringView title)
{ {
if (title.size() >= sizeof(WindowCreatePacket::title))
return BAN::Error::from_errno(EINVAL);
int server_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0); 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);
sockaddr_un server_address; timespec start_time;
server_address.sun_family = AF_UNIX; clock_gettime(CLOCK_MONOTONIC, &start_time);
strcpy(server_address.sun_path, s_window_server_socket.data());
if (connect(server_fd, (sockaddr*)&server_address, sizeof(server_address)) == -1) for (;;)
{ {
close(server_fd); sockaddr_un server_address;
return BAN::Error::from_errno(errno); 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)) == 0)
break;
timespec current_time;
clock_gettime(CLOCK_MONOTONIC, &current_time);
time_t duration_s = (current_time.tv_sec - start_time.tv_sec) + (current_time.tv_nsec >= start_time.tv_nsec);
if (duration_s > 10)
{
close(server_fd);
return BAN::Error::from_errno(ETIMEDOUT);
}
timespec sleep_time;
sleep_time.tv_sec = 0;
sleep_time.tv_nsec = 1'000'000;
nanosleep(&sleep_time, nullptr);
} }
WindowCreatePacket packet; WindowCreatePacket packet;
packet.width = width; packet.width = width;
packet.height = height; packet.height = height;
strncpy(packet.title, title.data(), title.size());
packet.title[title.size()] = '\0';
if (send(server_fd, &packet, sizeof(packet), 0) != sizeof(packet)) if (send(server_fd, &packet, sizeof(packet), 0) != sizeof(packet))
{ {
close(server_fd); close(server_fd);
@ -92,6 +115,14 @@ namespace LibGUI
switch (packet.type) switch (packet.type)
{ {
case EventPacket::Type::DestroyWindow:
exit(1);
case EventPacket::Type::CloseWindow:
if (m_close_window_event_callback)
m_close_window_event_callback();
else
exit(0);
break;
case EventPacket::Type::KeyEvent: case EventPacket::Type::KeyEvent:
if (m_key_event_callback) if (m_key_event_callback)
m_key_event_callback(packet.key_event); m_key_event_callback(packet.key_event);

View File

@ -20,6 +20,7 @@ namespace LibGUI
INVALID, INVALID,
CreateWindow, CreateWindow,
Invalidate, Invalidate,
COUNT
}; };
struct WindowCreatePacket struct WindowCreatePacket
@ -27,6 +28,7 @@ namespace LibGUI
WindowPacketType type = WindowPacketType::CreateWindow; WindowPacketType type = WindowPacketType::CreateWindow;
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
char title[52];
}; };
struct WindowInvalidatePacket struct WindowInvalidatePacket
@ -61,6 +63,8 @@ namespace LibGUI
{ {
enum class Type : uint8_t enum class Type : uint8_t
{ {
DestroyWindow,
CloseWindow,
KeyEvent, KeyEvent,
MouseButtonEvent, MouseButtonEvent,
MouseMoveEvent, MouseMoveEvent,
@ -97,7 +101,7 @@ namespace LibGUI
public: public:
~Window(); ~Window();
static BAN::ErrorOr<BAN::UniqPtr<Window>> create(uint32_t width, uint32_t height); static BAN::ErrorOr<BAN::UniqPtr<Window>> create(uint32_t width, uint32_t height, BAN::StringView title);
void set_pixel(uint32_t x, uint32_t y, uint32_t color) void set_pixel(uint32_t x, uint32_t y, uint32_t color)
{ {
@ -112,6 +116,7 @@ namespace LibGUI
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_key_event_callback(BAN::Function<void(EventPacket::KeyEvent)> callback) { m_key_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_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_move_event_callback(BAN::Function<void(EventPacket::MouseMoveEvent)> callback) { m_mouse_move_event_callback = callback; }
@ -131,6 +136,7 @@ namespace LibGUI
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(EventPacket::KeyEvent)> m_key_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::MouseButtonEvent)> m_mouse_button_event_callback;
BAN::Function<void(EventPacket::MouseMoveEvent)> m_mouse_move_event_callback; BAN::Function<void(EventPacket::MouseMoveEvent)> m_mouse_move_event_callback;