--- old/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java 2018-05-18 11:43:37.000000000 -0700 +++ new/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java 2018-05-18 11:43:37.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -61,6 +61,7 @@ import sun.font.FontDesignMetrics; import sun.font.FontUtilities; import sun.java2d.SunGraphicsEnvironment; +import sun.java2d.pipe.Region; import java.util.concurrent.Callable; import java.util.concurrent.Future; @@ -2270,6 +2271,27 @@ } /** + * Converts coordinates from the user's space to the device space using + * appropriate device transformation. + * + * @param x coordinate in the user space + * @param y coordinate in the user space + * @return the point which uses device space(pixels) + */ + public static Point convertToDeviceSpace(double x, double y) { + GraphicsConfiguration gc = + GraphicsEnvironment.getLocalGraphicsEnvironment() + .getDefaultScreenDevice().getDefaultConfiguration(); + gc = getGraphicsConfigurationAtPoint(gc, x, y); + + AffineTransform tx = gc.getDefaultTransform(); + x = Region.clipRound(x * tx.getScaleX()); + y = Region.clipRound(y * tx.getScaleY()); + + return new Point((int) x, (int) y); + } + + /** * Used to listen to "blit" repaints in RepaintManager. */ public interface RepaintListener { --- old/src/java.desktop/windows/classes/sun/awt/windows/WRobotPeer.java 2018-05-18 11:43:39.000000000 -0700 +++ new/src/java.desktop/windows/classes/sun/awt/windows/WRobotPeer.java 2018-05-18 11:43:38.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,9 +25,13 @@ package sun.awt.windows; -import java.awt.*; +import java.awt.GraphicsDevice; +import java.awt.Point; +import java.awt.Rectangle; import java.awt.peer.RobotPeer; +import sun.swing.SwingUtilities2; + final class WRobotPeer extends WObjectPeer implements RobotPeer { WRobotPeer() { @@ -48,7 +52,8 @@ public native void mouseMoveImpl(int x, int y); @Override public void mouseMove(int x, int y) { - mouseMoveImpl(x, y); + Point point = SwingUtilities2.convertToDeviceSpace(x, y); + mouseMoveImpl(point.x, point.y); } @Override public native void mousePress(int buttons); --- old/src/java.desktop/windows/native/libawt/windows/awt_Robot.cpp 2018-05-18 11:43:40.000000000 -0700 +++ new/src/java.desktop/windows/native/libawt/windows/awt_Robot.cpp 2018-05-18 11:43:40.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -43,62 +43,20 @@ { } -#ifndef SPI_GETMOUSESPEED -#define SPI_GETMOUSESPEED 112 -#endif - -#ifndef SPI_SETMOUSESPEED -#define SPI_SETMOUSESPEED 113 -#endif +static int signum(int i) { + // special version of signum which returns 1 when value is 0 + return i >= 0 ? 1 : -1; +} void AwtRobot::MouseMove( jint x, jint y) { - // Fix for Bug 4288230. See Q193003 from MSDN. - int oldAccel[3], newAccel[3]; - INT_PTR oldSpeed, newSpeed; - BOOL bResult; - - // The following values set mouse ballistics to 1 mickey/pixel. - newAccel[0] = 0; - newAccel[1] = 0; - newAccel[2] = 0; - newSpeed = 10; - - // Save the Current Mouse Acceleration Constants - bResult = SystemParametersInfo(SPI_GETMOUSE,0,oldAccel,0); - bResult = SystemParametersInfo(SPI_GETMOUSESPEED, 0, &oldSpeed,0); - // Set the new Mouse Acceleration Constants (Disabled). - bResult = SystemParametersInfo(SPI_SETMOUSE,0,newAccel,SPIF_SENDCHANGE); - bResult = SystemParametersInfo(SPI_SETMOUSESPEED, 0, - // 4504963: Though the third argument to SystemParameterInfo is - // declared as a PVOID, as of Windows 2000 it is apparently - // interpreted as an int. (The MSDN docs for SPI_SETMOUSESPEED - // say that it's an integer between 1 and 20, the default being - // 10). Instead of passing the @ of the desired value, the - // value itself is now passed, cast as a PVOID so as to - // compile. -bchristi 10/02/2001 - (PVOID)newSpeed, - SPIF_SENDCHANGE); - - int primaryIndex = AwtWin32GraphicsDevice::GetDefaultDeviceIndex(); - Devices::InstanceAccess devices; - AwtWin32GraphicsDevice *device = devices->GetDevice(primaryIndex); - - x = (device == NULL) ? x : device->ScaleUpX(x); - y = (device == NULL) ? y : device->ScaleUpY(y); - - POINT curPos; - ::GetCursorPos(&curPos); - x -= curPos.x; - y -= curPos.y; - - mouse_event(MOUSEEVENTF_MOVE,x,y,0,0); - // Move the cursor to the desired coordinates. - - // Restore the old Mouse Acceleration Constants. - bResult = SystemParametersInfo(SPI_SETMOUSE,0, oldAccel, SPIF_SENDCHANGE); - bResult = SystemParametersInfo(SPI_SETMOUSESPEED, 0, (PVOID)oldSpeed, - SPIF_SENDCHANGE); + INPUT mouseInput = {0}; + mouseInput.type = INPUT_MOUSE; + mouseInput.mi.time = 0; + mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; + mouseInput.mi.dx = (x * 65536 /::GetSystemMetrics(SM_CXSCREEN)) + signum(x); + mouseInput.mi.dy = (y * 65536 /::GetSystemMetrics(SM_CYSCREEN)) + signum(y); + ::SendInput(1, &mouseInput, sizeof(mouseInput)); } void AwtRobot::MousePress( jint buttonMask ) --- /dev/null 2018-05-18 11:43:42.000000000 -0700 +++ new/test/jdk/java/awt/Robot/MouseLocationOnScreen/MouseLocationOnScreen.java 2018-05-18 11:43:41.000000000 -0700 @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.AWTException; +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.MouseInfo; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.Robot; + +/** + * @test + * @key headful + * @bug 8196030 + * @summary checks that Robot and MouseInfo use the same coordinates + */ +public final class MouseLocationOnScreen { + + public static void main(final String[] args) throws AWTException { + GraphicsEnvironment lge = + GraphicsEnvironment.getLocalGraphicsEnvironment(); + + for (final GraphicsDevice device : lge.getScreenDevices()) { + GraphicsConfiguration dc = device.getDefaultConfiguration(); + Robot robot = new Robot(device); + + Rectangle bounds = dc.getBounds(); + int x1 = bounds.x; + int x2 = x1 + bounds.width - 1; + int y1 = bounds.y; + int y2 = y1 + bounds.height - 1; + + // We'll check all edge (two pixels in a width) of the each screen + edge(robot, device, x1, y1, x2, y1); // top + edge(robot, device, x1, y1 + 1, x2, y1 + 1); // top + + edge(robot, device, x2, y1, x2, y2); // right + edge(robot, device, x2 - 1, y1, x2 - 1, y2); // right + + edge(robot, device, x1, y1, x1, y2); // left + edge(robot, device, x1 + 1, y1, x1 + 1, y2); // left + + edge(robot, device, x1, y2, x2, y2); // bottom + edge(robot, device, x1, y2 - 1, x2, y2 - 1); // bottom + + // We'll check crossing of diagonals of each screen + cross(robot, device, x1, y1, x2, y2); // cross left-bottom + cross(robot, device, x1, y2, x2, y1); // cross left-top + } + } + + /** + * This method checks the coordinates which were passed to robot and + * returned by MouseInfo. Note that this method will be called for each + * pixel and for performance reasons we try will try to skip waitForIdle() + * a few times. + */ + static void validate(Robot robot, GraphicsDevice device, int x, int y) { + int attempt = 0; + while (true) { + attempt++; + Point actLoc = MouseInfo.getPointerInfo().getLocation(); + GraphicsDevice actDevice = MouseInfo.getPointerInfo().getDevice(); + + if (actLoc.x != x || actLoc.y != y || actDevice != device) { + if (attempt <= 10) { + if (attempt >= 8) { + robot.waitForIdle(); + } + continue; + } + System.err.println("Expected device: " + device); + System.err.println("Actual device: " + actDevice); + System.err.println("Expected X: " + x); + System.err.println("Actual X: " + actLoc.x); + System.err.println("Expected Y: " + y); + System.err.println("Actual Y: " + actLoc.y); + throw new RuntimeException(); + } + return; + } + } + + private static void edge(Robot robot, GraphicsDevice device, + int x1, int y1, int x2, int y2) { + for (int x = x1; x <= x2; x++) { + for (int y = y1; y <= y2; y++) { + robot.mouseMove(x, y); + validate(robot, device, x, y); + } + } + } + + private static void cross(Robot robot, GraphicsDevice device, + int x0, int y0, int x1, int y1) { + float dmax = (float) Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)); + float dx = (x1 - x0) / dmax; + float dy = (y1 - y0) / dmax; + + robot.mouseMove(x0, y0); + validate(robot, device, x0, y0); + for (int i = 1; i <= dmax; i++) { + int x = (int) (x0 + dx * i); + int y = (int) (y0 + dy * i); + robot.mouseMove(x, y); + validate(robot, device, x, y); + } + } +}