Repository: godotengine/godot
File: platform/linuxbsd/wayland/wayland_thread.cpp (C++)

Code before:
	p_rect.size = scale_vector2i(p_rect.size, 1.0 / parent_scale);

	ws.id = p_window_id;
	ws.parent_id = p_parent_id;
	ws.registry = &registry;
	ws.wayland_thread = this;

	ws.rect = p_rect;

	ws.wl_surface = wl_compositor_create_surface(registry.wl_compositor);
	wl_proxy_tag_godot((struct wl_proxy *)ws.wl_surface);
	wl_surface_add_listener(ws.wl_surface, &wl_surface_listener, &ws);

	if (registry.wp_viewporter) {
		ws.wp_viewport = wp_viewporter_get_viewport(registry.wp_viewporter, ws.wl_surface);

		if (registry.wp_fractional_scale_manager) {
			ws.wp_fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(registry.wp_fractional_scale_manager, ws.wl_surface);
			wp_fractional_scale_v1_add_listener(ws.wp_fractional_scale, &wp_fractional_scale_listener, &ws);
		}
	}

	ws.xdg_surface = xdg_wm_base_get_xdg_surface(registry.xdg_wm_base, ws.wl_surface);
	xdg_surface_add_listener(ws.xdg_surface, &xdg_surface_listener, &ws);

	Rect2i positioner_rect;
	positioner_rect.size = parent.rect.size;
	struct xdg_surface *parent_xdg_surface = parent.xdg_surface;

	Point2i offset = ws.rect.position - parent.rect.position;

#ifdef LIBDECOR_ENABLED
	if (!parent_xdg_surface && parent.libdecor_frame) {
		parent_xdg_surface = libdecor_frame_get_xdg_surface(parent.libdecor_frame);

		int corner_x = 0;
		int corner_y = 0;
		libdecor_frame_translate_coordinate(parent.libdecor_frame, 0, 0, &corner_x, &corner_y);

		positioner_rect.position.x = corner_x;
		positioner_rect.position.y = corner_y;

		positioner_rect.size.width -= corner_x;
		positioner_rect.size.height -= corner_y;
	}
#endif

	ERR_FAIL_NULL(parent_xdg_surface);

	struct xdg_positioner *xdg_positioner = xdg_wm_base_create_positioner(registry.xdg_wm_base);
	xdg_positioner_set_size(xdg_positioner, ws.rect.size.width, ws.rect.size.height);

Review comment (bug):
Hi, heads up: this can return a `nullptr` if the window does not exist.

I'd do an early return for `p_ss->pointer_data.pointed_id == DisplayServer::INVALID_WINDOW_ID` and add an `ERR_FAIL_NULL` on the returned `*surface* variable:


```suggestion
	if(p_ss->pointer_data.pointed_id == DisplayServer::INVALID_ID) {
		return;
	}
	struct wl_surface *surface = window_get_wl_surface(p_ss->pointer_data.pointed_id);
	ERR_FAIL_NULL(surface);
```

I've not tested this, hopefully I didn't miss anything 😅 

Code after:
			locked_surface = window_get_wl_surface(DisplayServer::MAIN_WINDOW_ID);
		}
		ERR_FAIL_NULL(locked_surface);

		p_ss->wp_locked_pointer = zwp_pointer_constraints_v1_lock_pointer(registry.wp_pointer_constraints, locked_surface, p_ss->wl_pointer, nullptr, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
	}
}

void WaylandThread::seat_state_set_hint(SeatState *p_ss, int p_x, int p_y) {
	if (p_ss->wp_locked_pointer == nullptr) {
		return;
	}

	zwp_locked_pointer_v1_set_cursor_position_hint(p_ss->wp_locked_pointer, wl_fixed_from_int(p_x), wl_fixed_from_int(p_y));
}

void WaylandThread::seat_state_warp_pointer(SeatState *p_ss, int p_x, int p_y) {
	if (registry.wp_pointer_warp == nullptr) {
		return;
	}

	if (p_ss->pointer_data.pointed_id == DisplayServer::INVALID_WINDOW_ID) {
		return;
	}

	struct wl_surface *surface = window_get_wl_surface(p_ss->pointer_data.pointed_id);
	ERR_FAIL_NULL(surface);

	wp_pointer_warp_v1_warp_pointer(registry.wp_pointer_warp, surface, p_ss->wl_pointer, wl_fixed_from_int(p_x), wl_fixed_from_int(p_y), p_ss->pointer_enter_serial);
}

void WaylandThread::seat_state_confine_pointer(SeatState *p_ss) {
	ERR_FAIL_NULL(p_ss);

	if (p_ss->wl_pointer == nullptr) {
		return;
	}

	if (registry.wp_pointer_constraints == nullptr) {
		return;
	}

	if (p_ss->wp_confined_pointer == nullptr) {
		struct wl_surface *confined_surface = window_get_wl_surface(p_ss->pointer_data.last_pointed_id);
		ERR_FAIL_NULL(confined_surface);

		p_ss->wp_confined_pointer = zwp_pointer_constraints_v1_confine_pointer(registry.wp_pointer_constraints, confined_surface, p_ss->wl_pointer, nullptr, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
	}
}

void WaylandThread::seat_state_update_cursor(SeatState *p_ss) {