LibGUI/WindowServer: Allow querying global cursor position

This commit is contained in:
2026-06-23 23:31:17 +03:00
parent 1287c8e335
commit 689494db63
6 changed files with 46 additions and 0 deletions

View File

@@ -274,6 +274,12 @@ namespace LibGUI
send_packet(packet, __FUNCTION__);
}
void Window::query_cursor_position()
{
const WindowPacket::QueryPointer packet {};
send_packet(packet, __FUNCTION__);
}
void Window::on_socket_error(BAN::StringView function)
{
if (m_handling_socket_error)
@@ -419,6 +425,10 @@ namespace LibGUI
if (m_mouse_scroll_event_callback)
m_mouse_scroll_event_callback(TRY_OR_BREAK(EventPacket::MouseScrollEvent::deserialize(packet_span)).event);
break;
case PacketType::QueryPointerEvent:
if (m_query_pointer_event_callback)
m_query_pointer_event_callback(TRY_OR_BREAK(EventPacket::QueryPointerEvent::deserialize(packet_span)).event);
break;
#undef TRY_OR_BREAK
default:
dprintln("unhandled packet type: {}", static_cast<uint32_t>(header.type));

View File

@@ -211,6 +211,9 @@ namespace LibGUI
MouseButtonEvent,
MouseMoveEvent,
MouseScrollEvent,
QueryPointer,
QueryPointerEvent,
};
struct PacketHeader
@@ -303,6 +306,10 @@ namespace LibGUI
BAN::Vector<uint32_t>, pixels
);
DEFINE_PACKET(
QueryPointer
);
}
namespace EventPacket
@@ -390,6 +397,15 @@ namespace LibGUI
event_t, event
);
DEFINE_PACKET_EXTRA(
QueryPointerEvent,
struct event_t {
int32_t x;
int32_t y;
},
event_t, event
);
}
}

View File

@@ -63,6 +63,8 @@ namespace LibGUI
// actual resize is only done after resize callback is called
void request_resize(uint32_t width, uint32_t height);
void query_cursor_position();
uint32_t width() const { return m_width; }
uint32_t height() const { return m_height; }
@@ -80,6 +82,7 @@ namespace LibGUI
void set_window_focus_event_callback(BAN::Function<void(EventPacket::WindowFocusEvent::event_t)> callback) { m_window_focus_event_callback = callback; }
void set_window_fullscreen_event_callback(BAN::Function<void(EventPacket::WindowFullscreenEvent::event_t)> callback) { m_window_fullscreen_event_callback = callback; }
void set_window_move_event_callback(BAN::Function<void(EventPacket::WindowMoveEvent::event_t)> callback) { m_window_move_event_callback = callback; }
void set_query_pointer_event_callback(BAN::Function<void(EventPacket::QueryPointerEvent::event_t)> callback) { m_query_pointer_event_callback = callback; }
int server_fd() const { return m_server_fd; }
@@ -124,6 +127,7 @@ namespace LibGUI
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;
BAN::Function<void(EventPacket::QueryPointerEvent::event_t)> m_query_pointer_event_callback;
size_t m_in_buffer_size { 0 };
BAN::Array<uint8_t, 64 * 1024> m_in_buffer;

View File

@@ -40,6 +40,19 @@ BAN::ErrorOr<void> WindowServer::set_background_image(BAN::UniqPtr<LibImage::Ima
return {};
}
void WindowServer::on_query_pointer(int fd, const LibGUI::WindowPacket::QueryPointer&)
{
const LibGUI::EventPacket::QueryPointerEvent event_packet { .event = {
.x = m_cursor.x,
.y = m_cursor.y,
}};
if (auto ret = append_serialized_packet(event_packet, fd); ret.is_error())
{
dwarnln("could not respond to query pointer request: {}", ret.error());
return;
}
}
void WindowServer::on_window_create(int fd, const LibGUI::WindowPacket::WindowCreate& packet)
{
for (auto& window : m_client_windows)

View File

@@ -31,6 +31,8 @@ public:
BAN::ErrorOr<void> set_background_image(BAN::UniqPtr<LibImage::Image>);
void on_query_pointer(int fd, const LibGUI::WindowPacket::QueryPointer&);
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&);

View File

@@ -472,6 +472,7 @@ int main()
WINDOW_PACKET_CASE(WindowSetFullscreen, on_window_set_fullscreen);
WINDOW_PACKET_CASE(WindowSetTitle, on_window_set_title);
WINDOW_PACKET_CASE(WindowSetCursor, on_window_set_cursor);
WINDOW_PACKET_CASE(QueryPointer, on_query_pointer);
#undef WINDOW_PACKET_CASE
default:
dprintln("unhandled packet type: {}", static_cast<uint32_t>(header.type));