1 /* 2 * Copyright (c) 2011, 2013, 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.*; 29 import java.awt.peer.*; 30 31 import sun.awt.CGraphicsDevice; 32 33 class CRobot implements RobotPeer { 34 private static final int MOUSE_LOCATION_UNKNOWN = -1; 35 36 private final CGraphicsDevice fDevice; 37 private int mouseLastX = MOUSE_LOCATION_UNKNOWN; 38 private int mouseLastY = MOUSE_LOCATION_UNKNOWN; 39 40 // OS X doesn't generate dragged event as a result of button press and 41 // mouse move events. This means that we have to track buttons state 42 // in order to generate dragged events ourselves. 43 private int mouseButtonsState = 0; 44 45 /** 46 * Uses the given GraphicsDevice as the coordinate system for subsequent 47 * coordinate calls. 48 */ 49 public CRobot(Robot r, CGraphicsDevice d) { 50 fDevice = d; 51 initRobot(); 52 } 53 54 @Override 55 public void dispose() { 56 } 57 58 /** 59 * Moves mouse pointer to given screen coordinates. 60 * @param x X position 61 * @param y Y position 62 */ 63 @Override 64 public void mouseMove(int x, int y) { 65 mouseLastX = x; 66 mouseLastY = y; 67 68 mouseEvent(fDevice.getCGDisplayID(), mouseLastX, mouseLastY, 69 mouseButtonsState, true, true); 70 } 71 72 /** 73 * Presses one or more mouse buttons. 74 * 75 * @param buttons the button mask (combination of 76 * <code>InputEvent.BUTTON1/2/3_MASK</code>) 77 */ 78 @Override 79 public void mousePress(int buttons) { 80 mouseButtonsState |= buttons; 81 82 mouseEvent(fDevice.getCGDisplayID(), mouseLastX, mouseLastY, 83 buttons, true, false); 84 } 85 86 /** 87 * Releases one or more mouse buttons. 88 * 89 * @param buttons the button mask (combination of 90 * <code>InputEvent.BUTTON1/2/3_MASK</code>) 91 */ 92 @Override 93 public void mouseRelease(int buttons) { 94 mouseButtonsState &= ~buttons; 95 96 mouseEvent(fDevice.getCGDisplayID(), mouseLastX, mouseLastY, 97 buttons, false, false); 98 } 99 100 @Override 101 public native void mouseWheel(int wheelAmt); 102 103 /** 104 * Presses a given key. 105 * <p> 106 * Key codes that have more than one physical key associated with them 107 * (e.g. <code>KeyEvent.VK_SHIFT</code> could mean either the 108 * left or right shift key) will map to the left key. 109 * <p> 110 * Assumes that the 111 * peer implementations will throw an exception for other bogus 112 * values e.g. -1, 999999 113 * 114 * @param keycode the key to press (e.g. <code>KeyEvent.VK_A</code>) 115 */ 116 @Override 117 public void keyPress(final int keycode) { 118 keyEvent(keycode, true); 119 } 120 121 /** 122 * Releases a given key. 123 * <p> 124 * Key codes that have more than one physical key associated with them 125 * (e.g. <code>KeyEvent.VK_SHIFT</code> could mean either the 126 * left or right shift key) will map to the left key. 127 * <p> 128 * Assumes that the 129 * peer implementations will throw an exception for other bogus 130 * values e.g. -1, 999999 131 * 132 * @param keycode the key to release (e.g. <code>KeyEvent.VK_A</code>) 133 */ 134 @Override 135 public void keyRelease(final int keycode) { 136 keyEvent(keycode, false); 137 } 138 139 /** 140 * Returns the color of a pixel at the given screen coordinates. 141 * @param x X position of pixel 142 * @param y Y position of pixel 143 * @return color of the pixel 144 */ 145 @Override 146 public int getRGBPixel(int x, int y) { 147 int c[] = new int[1]; 148 getScreenPixels(new Rectangle(x, y, 1, 1), c); 149 return c[0]; 150 } 151 152 /** 153 * Creates an image containing pixels read from the screen. 154 * @param bounds the rect to capture in screen coordinates 155 * @return the array of pixels 156 */ 157 @Override 158 public int [] getRGBPixels(final Rectangle bounds) { 159 int c[] = new int[bounds.width * bounds.height]; 160 getScreenPixels(bounds, c); 161 162 return c; 163 } 164 165 private native void initRobot(); 166 private native void mouseEvent(int displayID, int lastX, int lastY, 167 int buttonsState, 168 boolean isButtonsDownState, 169 boolean isMouseMove); 170 private native void keyEvent(int javaKeyCode, boolean keydown); 171 private void getScreenPixels(Rectangle r, int[] pixels){ 172 nativeGetScreenPixels(r.x, r.y, r.width, r.height, pixels); 173 } 174 private native void nativeGetScreenPixels(int x, int y, int width, int height, int[] pixels); 175 }