Skip to content

Commit

Permalink
Fix getWindowPointerPosition on Wayland #177
Browse files Browse the repository at this point in the history
Depending on the window currently focused the
coordinates returned may be wrong (sometimes even
negative). This happens mainly with popup windows.

Fixes #177
  • Loading branch information
simon-spinner authored and akurtakov committed May 7, 2024
1 parent a69e03d commit bd900fb
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6203,7 +6203,27 @@ long getWindowPointerPosition(long window, int[] x, int[] y, int[] mask) {
}

long pointer = GDK.gdk_get_pointer(display);
return GDK.gdk_window_get_device_position(window, pointer, x, y, mask);
if (OS.isWayland()) {
// Read the current mask value, but not the position. The position is not reliable on Wayland because
// the origin of the coordinates returned may change depending on the currently focused window.
// For instance, if a popup window is currently focused and this method is called with a window outside
// of the popup window, the returned coordinates are in relation to the root coordinate system of the
// popup (possibly negative values). See also issue GH-177.
GDK.gdk_window_get_device_position(window, pointer, null, null, mask);
var windowAtPosition = GDK.gdk_device_get_window_at_position(pointer, x, y);
if (windowAtPosition != 0 && windowAtPosition != window) {
int[] origin_x = new int[1], origin_y = new int[1];
GDK.gdk_window_get_origin(windowAtPosition, origin_x, origin_y);
x[0] += origin_x[0];
y[0] += origin_y[0];
GDK.gdk_window_get_origin(window, origin_x, origin_y);
x[0] -= origin_x[0];
y[0] -= origin_y[0];
}
return windowAtPosition;
} else {
return GDK.gdk_window_get_device_position(window, pointer, x, y, mask);
}
}

/**
Expand Down

0 comments on commit bd900fb

Please sign in to comment.