1 /*
   2  * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 import java.awt.Point;
  29 import java.awt.Rectangle;
  30 import java.awt.peer.RobotPeer;
  31 
  32 import sun.awt.CGraphicsDevice;
  33 
  34 final class CRobot implements RobotPeer {
  35 
  36     private static final int MOUSE_LOCATION_UNKNOWN      = -1;
  37 
  38     private final CGraphicsDevice fDevice;
  39     private int mouseLastX = MOUSE_LOCATION_UNKNOWN;
  40     private int mouseLastY = MOUSE_LOCATION_UNKNOWN;
  41 
  42     // OS X doesn't generate dragged event as a result of button press and
  43     // mouse move events. This means that we have to track buttons state
  44     // in order to generate dragged events ourselves.
  45     private int mouseButtonsState = 0;
  46 
  47     /**
  48      * Uses the given GraphicsDevice as the coordinate system for subsequent
  49      * coordinate calls.
  50      */
  51     CRobot(CGraphicsDevice d) {
  52         fDevice = d;
  53         initRobot();
  54     }
  55 
  56     /**
  57      * Moves mouse pointer to given screen coordinates.
  58      * @param x X position
  59      * @param y Y position
  60      */
  61     @Override
  62     public void mouseMove(int x, int y) {
  63         mouseLastX = x;
  64         mouseLastY = y;
  65 
  66         mouseEvent(mouseLastX, mouseLastY, mouseButtonsState, true, true);
  67     }
  68 
  69     /**
  70      * Presses one or more mouse buttons.
  71      *
  72      * @param buttons the button mask (combination of
  73      * {@code InputEvent.BUTTON1/2/3_MASK})
  74      */
  75     @Override
  76     public void mousePress(int buttons) {
  77         mouseButtonsState |= buttons;
  78         checkMousePos();
  79         mouseEvent(mouseLastX, mouseLastY, buttons, true, false);
  80     }
  81 
  82     /**
  83      * Releases one or more mouse buttons.
  84      *
  85      * @param buttons the button mask (combination of
  86      * {@code InputEvent.BUTTON1/2/3_MASK})
  87      */
  88     @Override
  89     public void mouseRelease(int buttons) {
  90         mouseButtonsState &= ~buttons;
  91         checkMousePos();
  92         mouseEvent(mouseLastX, mouseLastY, buttons, false, false);
  93     }
  94 
  95     /**
  96      * Set unknown mouse location, if needed.
  97      */
  98     private void checkMousePos() {
  99         if (mouseLastX == MOUSE_LOCATION_UNKNOWN ||
 100                 mouseLastY == MOUSE_LOCATION_UNKNOWN) {
 101 
 102             Rectangle deviceBounds = fDevice.getDefaultConfiguration().getBounds();
 103             Point mousePos = CCursorManager.getInstance().getCursorPosition();
 104 
 105             if (mousePos.x < deviceBounds.x) {
 106                 mousePos.x = deviceBounds.x;
 107             }
 108             else if (mousePos.x > deviceBounds.x + deviceBounds.width) {
 109                 mousePos.x = deviceBounds.x + deviceBounds.width;
 110             }
 111 
 112             if (mousePos.y < deviceBounds.y) {
 113                 mousePos.y = deviceBounds.y;
 114             }
 115             else if (mousePos.y > deviceBounds.y + deviceBounds.height) {
 116                 mousePos.y = deviceBounds.y + deviceBounds.height;
 117             }
 118 
 119             mouseLastX = mousePos.x;
 120             mouseLastY = mousePos.y;
 121         }
 122     }
 123 
 124     @Override
 125     public native void mouseWheel(int wheelAmt);
 126 
 127     /**
 128      * Presses a given key.
 129      * <p>
 130      * Key codes that have more than one physical key associated with them
 131      * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the
 132      * left or right shift key) will map to the left key.
 133      * <p>
 134      * Assumes that the
 135      * peer implementations will throw an exception for other bogus
 136      * values e.g. -1, 999999
 137      *
 138      * @param keycode the key to press (e.g. {@code KeyEvent.VK_A})
 139      */
 140     @Override
 141     public void keyPress(final int keycode) {
 142         keyEvent(keycode, true);
 143     }
 144 
 145     /**
 146      * Releases a given key.
 147      * <p>
 148      * Key codes that have more than one physical key associated with them
 149      * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the
 150      * left or right shift key) will map to the left key.
 151      * <p>
 152      * Assumes that the
 153      * peer implementations will throw an exception for other bogus
 154      * values e.g. -1, 999999
 155      *
 156      * @param keycode the key to release (e.g. {@code KeyEvent.VK_A})
 157      */
 158     @Override
 159     public void keyRelease(final int keycode) {
 160         keyEvent(keycode, false);
 161     }
 162 
 163     /**
 164      * Returns the color of a pixel at the given screen coordinates.
 165      * @param x X position of pixel
 166      * @param y Y position of pixel
 167      * @return color of the pixel
 168      */
 169     @Override
 170     public int getRGBPixel(int x, int y) {
 171         int[] c = new int[1];
 172         double scale = fDevice.getScaleFactor();
 173         getScreenPixels(new Rectangle(x, y, (int) scale, (int) scale), c);
 174         return c[0];
 175     }
 176 
 177     /**
 178      * Creates an image containing pixels read from the screen.
 179      * @param bounds the rect to capture in screen coordinates
 180      * @return the array of pixels
 181      */
 182     @Override
 183     public int [] getRGBPixels(final Rectangle bounds) {
 184         int[] c = new int[bounds.width * bounds.height];
 185         getScreenPixels(bounds, c);
 186 
 187         return c;
 188     }
 189 
 190     private native void initRobot();
 191     private native void mouseEvent(int lastX, int lastY, int buttonsState,
 192                                    boolean isButtonsDownState,
 193                                    boolean isMouseMove);
 194     private native void keyEvent(int javaKeyCode, boolean keydown);
 195     private void getScreenPixels(Rectangle r, int[] pixels){
 196         double scale = fDevice.getScaleFactor();
 197         nativeGetScreenPixels(r.x, r.y, r.width, r.height, scale, pixels);
 198     }
 199     private native void nativeGetScreenPixels(int x, int y, int width, int height, double scale, int[] pixels);
 200 }