Wayland: Simplify cursor code and fix custom cursors

Initially the WaylandThread cursor code was supposed to be as stateless
as possible but, as time went on, this wasn't possible.

This expectation made the resulting API quite convoluted, so this patch
aims to simplify it substantially bot in terms of API surface and, most
importantly, in terms of actual implementation complexity.

This patch also fixes custom cursors since I accidentally changed the
mmap flags to MAP_PRIVATE some time ago. This took me hours to notice.
This commit is contained in:
Riteo 2024-09-13 18:59:38 +02:00
parent 74de05a01c
commit c15cd3acc4
3 changed files with 66 additions and 100 deletions

View file

@ -327,15 +327,7 @@ void DisplayServerWayland::mouse_set_mode(MouseMode p_mode) {
bool show_cursor = (p_mode == MOUSE_MODE_VISIBLE || p_mode == MOUSE_MODE_CONFINED);
if (show_cursor) {
if (custom_cursors.has(cursor_shape)) {
wayland_thread.cursor_set_custom_shape(cursor_shape);
} else {
wayland_thread.cursor_set_shape(cursor_shape);
}
} else {
wayland_thread.cursor_hide();
}
wayland_thread.cursor_set_visible(show_cursor);
WaylandThread::PointerConstraint constraint = WaylandThread::PointerConstraint::NONE;
@ -993,11 +985,7 @@ void DisplayServerWayland::cursor_set_shape(CursorShape p_shape) {
return;
}
if (custom_cursors.has(p_shape)) {
wayland_thread.cursor_set_custom_shape(p_shape);
} else {
wayland_thread.cursor_set_shape(p_shape);
}
wayland_thread.cursor_set_shape(p_shape);
}
DisplayServerWayland::CursorShape DisplayServerWayland::cursor_get_shape() const {
@ -1009,18 +997,13 @@ DisplayServerWayland::CursorShape DisplayServerWayland::cursor_get_shape() const
void DisplayServerWayland::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
MutexLock mutex_lock(wayland_thread.mutex);
bool visible = (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED);
if (p_cursor.is_valid()) {
HashMap<CursorShape, CustomCursor>::Iterator cursor_c = custom_cursors.find(p_shape);
if (cursor_c) {
if (cursor_c->value.rid == p_cursor->get_rid() && cursor_c->value.hotspot == p_hotspot) {
// We have a cached cursor. Nice.
if (visible) {
wayland_thread.cursor_set_custom_shape(p_shape);
}
wayland_thread.cursor_set_shape(p_shape);
return;
}
@ -1039,20 +1022,18 @@ void DisplayServerWayland::cursor_set_custom_image(const Ref<Resource> &p_cursor
wayland_thread.cursor_shape_set_custom_image(p_shape, image, p_hotspot);
if (visible) {
wayland_thread.cursor_set_custom_shape(p_shape);
}
wayland_thread.cursor_set_shape(p_shape);
} else {
// Clear cache and reset to default system cursor.
if (cursor_shape == p_shape && visible) {
wayland_thread.cursor_shape_clear_custom_image(p_shape);
if (cursor_shape == p_shape) {
wayland_thread.cursor_set_shape(p_shape);
}
if (custom_cursors.has(p_shape)) {
custom_cursors.erase(p_shape);
}
wayland_thread.cursor_shape_clear_custom_image(p_shape);
}
}