Gtk3: Mouse position relative to widget

I can get the absolute position. Moreover, Gdk::Window::get_device_position should return relative coordinates, but it seems it returns the absolute position.

Get current mouse position in GTK3 with Vala language:

int x = 0, y = 0, origin_x = 0, origin_y = 0;
this.get_window().get_origin(out origin_x, out origin_y);
Gdk.Device? pointer = this.get_display().get_default_seat().get_pointer();
this.get_window().get_device_position(pointer, out x, out y, null);

But I fell into trouble in GTK4 via same solution, the app will crashed:

double x = 0, y = 0;
var pointer_device = Gdk.Display.get_default()?.get_default_seat()?.get_pointer();
(this.root as Gtk.Window).get_surface().get_device_position(pointer_device, out x, out y, null);
GLib.warning("Mouse pointer: %f / %f", x, y);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.