Path: blob/master/src/java.desktop/macosx/classes/sun/lwawt/macosx/CRobot.java
42759 views
/*1* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.lwawt.macosx;2627import java.awt.Point;28import java.awt.Rectangle;29import java.awt.peer.RobotPeer;3031import sun.awt.CGraphicsDevice;3233final class CRobot implements RobotPeer {3435private static final int MOUSE_LOCATION_UNKNOWN = -1;3637private final CGraphicsDevice fDevice;38private int mouseLastX = MOUSE_LOCATION_UNKNOWN;39private int mouseLastY = MOUSE_LOCATION_UNKNOWN;4041// OS X doesn't generate dragged event as a result of button press and42// mouse move events. This means that we have to track buttons state43// in order to generate dragged events ourselves.44private int mouseButtonsState = 0;4546/**47* Uses the given GraphicsDevice as the coordinate system for subsequent48* coordinate calls.49*/50CRobot(CGraphicsDevice d) {51fDevice = d;52initRobot();53}5455/**56* Moves mouse pointer to given screen coordinates.57* @param x X position58* @param y Y position59*/60@Override61public void mouseMove(int x, int y) {62mouseLastX = x;63mouseLastY = y;6465mouseEvent(mouseLastX, mouseLastY, mouseButtonsState, true, true);66}6768/**69* Presses one or more mouse buttons.70*71* @param buttons the button mask (combination of72* {@code InputEvent.BUTTON1/2/3_MASK})73*/74@Override75public void mousePress(int buttons) {76mouseButtonsState |= buttons;77checkMousePos();78mouseEvent(mouseLastX, mouseLastY, buttons, true, false);79}8081/**82* Releases one or more mouse buttons.83*84* @param buttons the button mask (combination of85* {@code InputEvent.BUTTON1/2/3_MASK})86*/87@Override88public void mouseRelease(int buttons) {89mouseButtonsState &= ~buttons;90checkMousePos();91mouseEvent(mouseLastX, mouseLastY, buttons, false, false);92}9394/**95* Set unknown mouse location, if needed.96*/97private void checkMousePos() {98if (mouseLastX == MOUSE_LOCATION_UNKNOWN ||99mouseLastY == MOUSE_LOCATION_UNKNOWN) {100101Rectangle deviceBounds = fDevice.getDefaultConfiguration().getBounds();102Point mousePos = CCursorManager.getInstance().getCursorPosition();103104if (mousePos.x < deviceBounds.x) {105mousePos.x = deviceBounds.x;106}107else if (mousePos.x > deviceBounds.x + deviceBounds.width) {108mousePos.x = deviceBounds.x + deviceBounds.width;109}110111if (mousePos.y < deviceBounds.y) {112mousePos.y = deviceBounds.y;113}114else if (mousePos.y > deviceBounds.y + deviceBounds.height) {115mousePos.y = deviceBounds.y + deviceBounds.height;116}117118mouseLastX = mousePos.x;119mouseLastY = mousePos.y;120}121}122123@Override124public native void mouseWheel(int wheelAmt);125126/**127* Presses a given key.128* <p>129* Key codes that have more than one physical key associated with them130* (e.g. {@code KeyEvent.VK_SHIFT} could mean either the131* left or right shift key) will map to the left key.132* <p>133* Assumes that the134* peer implementations will throw an exception for other bogus135* values e.g. -1, 999999136*137* @param keycode the key to press (e.g. {@code KeyEvent.VK_A})138*/139@Override140public void keyPress(final int keycode) {141keyEvent(keycode, true);142}143144/**145* Releases a given key.146* <p>147* Key codes that have more than one physical key associated with them148* (e.g. {@code KeyEvent.VK_SHIFT} could mean either the149* left or right shift key) will map to the left key.150* <p>151* Assumes that the152* peer implementations will throw an exception for other bogus153* values e.g. -1, 999999154*155* @param keycode the key to release (e.g. {@code KeyEvent.VK_A})156*/157@Override158public void keyRelease(final int keycode) {159keyEvent(keycode, false);160}161162/**163* Returns the color of a pixel at the given screen coordinates.164* @param x X position of pixel165* @param y Y position of pixel166* @return color of the pixel167*/168@Override169public int getRGBPixel(int x, int y) {170int[] c = new int[1];171double scale = fDevice.getScaleFactor();172getScreenPixels(new Rectangle(x, y, (int) scale, (int) scale), c);173return c[0];174}175176/**177* Creates an image containing pixels read from the screen.178* @param bounds the rect to capture in screen coordinates179* @return the array of pixels180*/181@Override182public int [] getRGBPixels(final Rectangle bounds) {183int[] c = new int[bounds.width * bounds.height];184getScreenPixels(bounds, c);185186return c;187}188189private native void initRobot();190private native void mouseEvent(int lastX, int lastY, int buttonsState,191boolean isButtonsDownState,192boolean isMouseMove);193private native void keyEvent(int javaKeyCode, boolean keydown);194private void getScreenPixels(Rectangle r, int[] pixels){195double scale = fDevice.getScaleFactor();196nativeGetScreenPixels(r.x, r.y, r.width, r.height, scale, pixels);197}198private native void nativeGetScreenPixels(int x, int y, int width, int height, double scale, int[] pixels);199}200201202