8196030: AWT Robot mouseMove fails on Windows 10 1709 with HiDPI

8190326: Robot.mouseMove uses scaling factor of main display on unscaled second display

Reviewed-by: prr, kcr
This commit is contained in:
Sergey Bylokhov 2018-06-04 20:32:19 -07:00
parent 4ac10c32b9
commit 014033a098
7 changed files with 211 additions and 114 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 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
@ -29,10 +29,10 @@ import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.MultiResolutionImage;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.DirectColorModel;
import java.awt.image.MultiResolutionImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.awt.peer.RobotPeer;
@ -41,7 +41,7 @@ import sun.awt.AWTPermissions;
import sun.awt.ComponentFactory;
import sun.awt.SunToolkit;
import sun.awt.image.SunWritableRaster;
import sun.swing.SwingUtilities2;
import sun.java2d.SunGraphicsEnvironment;
/**
* This class is used to generate native system input events
@ -505,7 +505,7 @@ public class Robot {
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().
getDefaultConfiguration();
gc = SwingUtilities2.getGraphicsConfigurationAtPoint(
gc = SunGraphicsEnvironment.getGraphicsConfigurationAtPoint(
gc, screenRect.getCenterX(), screenRect.getCenterY());
AffineTransform tx = gc.getDefaultTransform();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 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
@ -33,40 +33,26 @@ import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.peer.ComponentPeer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.security.AccessController;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import sun.awt.AppContext;
import sun.awt.DisplayChangedListener;
import sun.awt.FontConfiguration;
import sun.awt.SunDisplayChanger;
import sun.font.CompositeFontDescriptor;
import sun.font.Font2D;
import sun.font.FontManager;
import sun.font.FontManagerFactory;
import sun.font.FontManagerForSGE;
import sun.font.NativeFont;
import java.security.AccessController;
import sun.java2d.pipe.Region;
import sun.security.action.GetPropertyAction;
/**
@ -389,4 +375,48 @@ public abstract class SunGraphicsEnvironment extends GraphicsEnvironment
return -1;
}
}
/**
* Returns the graphics configuration which bounds contain the given point.
*
* @param current the default configuration which is checked in the first
* place
* @param x the x coordinate of the given point
* @param y the y coordinate of the given point
* @return the graphics configuration
*/
public static GraphicsConfiguration getGraphicsConfigurationAtPoint(
GraphicsConfiguration current, double x, double y) {
if (current.getBounds().contains(x, y)) {
return current;
}
GraphicsEnvironment env = getLocalGraphicsEnvironment();
for (GraphicsDevice device : env.getScreenDevices()) {
GraphicsConfiguration config = device.getDefaultConfiguration();
if (config.getBounds().contains(x, y)) {
return config;
}
}
return current;
}
/**
* 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 = 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);
}
}

View File

@ -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
@ -52,6 +52,7 @@ import javax.swing.table.TableColumnModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import sun.java2d.pipe.Region;
import sun.print.ProxyPrintGraphics;
import sun.awt.*;
import java.io.*;
@ -2240,35 +2241,6 @@ public class SwingUtilities2 {
return UIManager.getBoolean(key);
}
/**
*
* Returns the graphics configuration which bounds contain the given
* point
*
* @param current the default configuration which is checked in the first place
* @param x the x coordinate of the given point
* @param y the y coordinate of the given point
* @return the graphics configuration
*/
public static GraphicsConfiguration getGraphicsConfigurationAtPoint(GraphicsConfiguration current, double x, double y) {
if (current.getBounds().contains(x, y)) {
return current;
}
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = env.getScreenDevices();
for (GraphicsDevice device : devices) {
GraphicsConfiguration config = device.getDefaultConfiguration();
if (config.getBounds().contains(x, y)) {
return config;
}
}
return current;
}
/**
* Used to listen to "blit" repaints in RepaintManager.
*/

View File

@ -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.java2d.SunGraphicsEnvironment;
final class WRobotPeer extends WObjectPeer implements RobotPeer
{
WRobotPeer() {
@ -48,7 +52,8 @@ final class WRobotPeer extends WObjectPeer implements RobotPeer
public native void mouseMoveImpl(int x, int y);
@Override
public void mouseMove(int x, int y) {
mouseMoveImpl(x, y);
Point point = SunGraphicsEnvironment.convertToDeviceSpace(x, y);
mouseMoveImpl(point.x, point.y);
}
@Override
public native void mousePress(int buttons);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 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,8 +61,8 @@ import sun.awt.SunToolkit;
import sun.awt.Win32GraphicsConfig;
import sun.awt.Win32GraphicsDevice;
import sun.awt.Win32GraphicsEnvironment;
import sun.java2d.SunGraphicsEnvironment;
import sun.java2d.pipe.Region;
import sun.swing.SwingUtilities2;
import sun.util.logging.PlatformLogger;
public class WWindowPeer extends WPanelPeer implements WindowPeer,
@ -659,7 +659,8 @@ public class WWindowPeer extends WPanelPeer implements WindowPeer,
int cx = x + width / 2;
int cy = y + height / 2;
GraphicsConfiguration current = getGraphicsConfiguration();
GraphicsConfiguration other = SwingUtilities2.getGraphicsConfigurationAtPoint(current, cx, cy);
GraphicsConfiguration other = SunGraphicsEnvironment
.getGraphicsConfigurationAtPoint(current, cx, cy);
if (!current.equals(other)) {
AffineTransform tx = other.getDefaultTransform();
double otherScaleX = tx.getScaleX();

View File

@ -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 @@ AwtRobot::~AwtRobot()
{
}
#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 )

View File

@ -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);
}
}
}