1 /* 2 * Copyright (c) 1999, 2015, 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 java.awt; 27 28 import java.awt.event.InputEvent; 29 import java.awt.event.KeyEvent; 30 import java.awt.image.BufferedImage; 31 import java.awt.image.DataBufferInt; 32 import java.awt.image.DirectColorModel; 33 import java.awt.image.Raster; 34 import java.awt.image.WritableRaster; 35 import java.awt.peer.RobotPeer; 36 37 import sun.awt.AWTPermissions; 38 import sun.awt.ComponentFactory; 39 import sun.awt.SunToolkit; 40 import sun.awt.image.SunWritableRaster; 41 42 /** 43 * This class is used to generate native system input events 44 * for the purposes of test automation, self-running demos, and 45 * other applications where control of the mouse and keyboard 46 * is needed. The primary purpose of Robot is to facilitate 47 * automated testing of Java platform implementations. 48 * <p> 49 * Using the class to generate input events differs from posting 50 * events to the AWT event queue or AWT components in that the 51 * events are generated in the platform's native input 52 * queue. For example, {@code Robot.mouseMove} will actually move 53 * the mouse cursor instead of just generating mouse move events. 54 * <p> 55 * Note that some platforms require special privileges or extensions 56 * to access low-level input control. If the current platform configuration 57 * does not allow input control, an {@code AWTException} will be thrown 58 * when trying to construct Robot objects. For example, X-Window systems 59 * will throw the exception if the XTEST 2.2 standard extension is not supported 60 * (or not enabled) by the X server. 61 * <p> 62 * Applications that use Robot for purposes other than self-testing should 63 * handle these error conditions gracefully. 64 * 65 * @author Robi Khan 66 * @since 1.3 67 */ 68 public class Robot { 69 private static final int MAX_DELAY = 60000; 70 private RobotPeer peer; 71 private boolean isAutoWaitForIdle = false; 72 private int autoDelay = 0; 73 private static int LEGAL_BUTTON_MASK = 0; 74 75 private DirectColorModel screenCapCM = null; 76 77 /** 78 * Constructs a Robot object in the coordinate system of the primary screen. 79 * 80 * @throws AWTException if the platform configuration does not allow 81 * low-level input control. This exception is always thrown when 82 * GraphicsEnvironment.isHeadless() returns true 83 * @throws SecurityException if {@code createRobot} permission is not granted 84 * @see java.awt.GraphicsEnvironment#isHeadless 85 * @see SecurityManager#checkPermission 86 * @see AWTPermission 87 */ 88 public Robot() throws AWTException { 89 if (GraphicsEnvironment.isHeadless()) { 90 throw new AWTException("headless environment"); 91 } 92 init(GraphicsEnvironment.getLocalGraphicsEnvironment() 93 .getDefaultScreenDevice()); 94 } 95 96 /** 97 * Creates a Robot for the given screen device. Coordinates passed 98 * to Robot method calls like mouseMove and createScreenCapture will 99 * be interpreted as being in the same coordinate system as the 100 * specified screen. Note that depending on the platform configuration, 101 * multiple screens may either: 102 * <ul> 103 * <li>share the same coordinate system to form a combined virtual screen</li> 104 * <li>use different coordinate systems to act as independent screens</li> 105 * </ul> 106 * This constructor is meant for the latter case. 107 * <p> 108 * If screen devices are reconfigured such that the coordinate system is 109 * affected, the behavior of existing Robot objects is undefined. 110 * 111 * @param screen A screen GraphicsDevice indicating the coordinate 112 * system the Robot will operate in. 113 * @throws AWTException if the platform configuration does not allow 114 * low-level input control. This exception is always thrown when 115 * GraphicsEnvironment.isHeadless() returns true. 116 * @throws IllegalArgumentException if {@code screen} is not a screen 117 * GraphicsDevice. 118 * @throws SecurityException if {@code createRobot} permission is not granted 119 * @see java.awt.GraphicsEnvironment#isHeadless 120 * @see GraphicsDevice 121 * @see SecurityManager#checkPermission 122 * @see AWTPermission 123 */ 124 public Robot(GraphicsDevice screen) throws AWTException { 125 checkIsScreenDevice(screen); 126 init(screen); 127 } 128 129 private void init(GraphicsDevice screen) throws AWTException { 130 checkRobotAllowed(); 131 Toolkit toolkit = Toolkit.getDefaultToolkit(); 132 if (toolkit instanceof ComponentFactory) { 133 peer = ((ComponentFactory)toolkit).createRobot(this, screen); 134 disposer = new RobotDisposer(peer); 135 sun.java2d.Disposer.addRecord(anchor, disposer); 136 } 137 initLegalButtonMask(); 138 } 139 140 @SuppressWarnings("deprecation") 141 private static synchronized void initLegalButtonMask() { 142 if (LEGAL_BUTTON_MASK != 0) return; 143 144 int tmpMask = 0; 145 if (Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled()){ 146 if (Toolkit.getDefaultToolkit() instanceof SunToolkit) { 147 final int buttonsNumber = ((SunToolkit)(Toolkit.getDefaultToolkit())).getNumberOfButtons(); 148 for (int i = 0; i < buttonsNumber; i++){ 149 tmpMask |= InputEvent.getMaskForButton(i+1); 150 } 151 } 152 } 153 tmpMask |= InputEvent.BUTTON1_MASK| 154 InputEvent.BUTTON2_MASK| 155 InputEvent.BUTTON3_MASK| 156 InputEvent.BUTTON1_DOWN_MASK| 157 InputEvent.BUTTON2_DOWN_MASK| 158 InputEvent.BUTTON3_DOWN_MASK; 159 LEGAL_BUTTON_MASK = tmpMask; 160 } 161 162 /* determine if the security policy allows Robot's to be created */ 163 private void checkRobotAllowed() { 164 SecurityManager security = System.getSecurityManager(); 165 if (security != null) { 166 security.checkPermission(AWTPermissions.CREATE_ROBOT_PERMISSION); 167 } 168 } 169 170 /* check if the given device is a screen device */ 171 private void checkIsScreenDevice(GraphicsDevice device) { 172 if (device == null || device.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) { 173 throw new IllegalArgumentException("not a valid screen device"); 174 } 175 } 176 177 private transient Object anchor = new Object(); 178 179 static class RobotDisposer implements sun.java2d.DisposerRecord { 180 private final RobotPeer peer; 181 public RobotDisposer(RobotPeer peer) { 182 this.peer = peer; 183 } 184 public void dispose() { 185 if (peer != null) { 186 peer.dispose(); 187 } 188 } 189 } 190 191 private transient RobotDisposer disposer; 192 193 /** 194 * Moves mouse pointer to given screen coordinates. 195 * @param x X position 196 * @param y Y position 197 */ 198 public synchronized void mouseMove(int x, int y) { 199 peer.mouseMove(x, y); 200 afterEvent(); 201 } 202 203 /** 204 * Presses one or more mouse buttons. The mouse buttons should 205 * be released using the {@link #mouseRelease(int)} method. 206 * 207 * @param buttons the Button mask; a combination of one or more 208 * mouse button masks. 209 * <p> 210 * It is allowed to use only a combination of valid values as a {@code buttons} parameter. 211 * A valid combination consists of {@code InputEvent.BUTTON1_DOWN_MASK}, 212 * {@code InputEvent.BUTTON2_DOWN_MASK}, {@code InputEvent.BUTTON3_DOWN_MASK} 213 * and values returned by the 214 * {@link InputEvent#getMaskForButton(int) InputEvent.getMaskForButton(button)} method. 215 * 216 * The valid combination also depends on a 217 * {@link Toolkit#areExtraMouseButtonsEnabled() Toolkit.areExtraMouseButtonsEnabled()} value as follows: 218 * <ul> 219 * <li> If support for extended mouse buttons is 220 * {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java 221 * then it is allowed to use only the following standard button masks: 222 * {@code InputEvent.BUTTON1_DOWN_MASK}, {@code InputEvent.BUTTON2_DOWN_MASK}, 223 * {@code InputEvent.BUTTON3_DOWN_MASK}. 224 * <li> If support for extended mouse buttons is 225 * {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java 226 * then it is allowed to use the standard button masks 227 * and masks for existing extended mouse buttons, if the mouse has more then three buttons. 228 * In that way, it is allowed to use the button masks corresponding to the buttons 229 * in the range from 1 to {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()}. 230 * <br> 231 * It is recommended to use the {@link InputEvent#getMaskForButton(int) InputEvent.getMaskForButton(button)} 232 * method to obtain the mask for any mouse button by its number. 233 * </ul> 234 * <p> 235 * The following standard button masks are also accepted: 236 * <ul> 237 * <li>{@code InputEvent.BUTTON1_MASK} 238 * <li>{@code InputEvent.BUTTON2_MASK} 239 * <li>{@code InputEvent.BUTTON3_MASK} 240 * </ul> 241 * However, it is recommended to use {@code InputEvent.BUTTON1_DOWN_MASK}, 242 * {@code InputEvent.BUTTON2_DOWN_MASK}, {@code InputEvent.BUTTON3_DOWN_MASK} instead. 243 * Either extended {@code _DOWN_MASK} or old {@code _MASK} values 244 * should be used, but both those models should not be mixed. 245 * @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button 246 * and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java 247 * @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button 248 * that does not exist on the mouse and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java 249 * @see #mouseRelease(int) 250 * @see InputEvent#getMaskForButton(int) 251 * @see Toolkit#areExtraMouseButtonsEnabled() 252 * @see java.awt.MouseInfo#getNumberOfButtons() 253 * @see java.awt.event.MouseEvent 254 */ 255 public synchronized void mousePress(int buttons) { 256 checkButtonsArgument(buttons); 257 peer.mousePress(buttons); 258 afterEvent(); 259 } 260 261 /** 262 * Releases one or more mouse buttons. 263 * 264 * @param buttons the Button mask; a combination of one or more 265 * mouse button masks. 266 * <p> 267 * It is allowed to use only a combination of valid values as a {@code buttons} parameter. 268 * A valid combination consists of {@code InputEvent.BUTTON1_DOWN_MASK}, 269 * {@code InputEvent.BUTTON2_DOWN_MASK}, {@code InputEvent.BUTTON3_DOWN_MASK} 270 * and values returned by the 271 * {@link InputEvent#getMaskForButton(int) InputEvent.getMaskForButton(button)} method. 272 * 273 * The valid combination also depends on a 274 * {@link Toolkit#areExtraMouseButtonsEnabled() Toolkit.areExtraMouseButtonsEnabled()} value as follows: 275 * <ul> 276 * <li> If the support for extended mouse buttons is 277 * {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java 278 * then it is allowed to use only the following standard button masks: 279 * {@code InputEvent.BUTTON1_DOWN_MASK}, {@code InputEvent.BUTTON2_DOWN_MASK}, 280 * {@code InputEvent.BUTTON3_DOWN_MASK}. 281 * <li> If the support for extended mouse buttons is 282 * {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java 283 * then it is allowed to use the standard button masks 284 * and masks for existing extended mouse buttons, if the mouse has more then three buttons. 285 * In that way, it is allowed to use the button masks corresponding to the buttons 286 * in the range from 1 to {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()}. 287 * <br> 288 * It is recommended to use the {@link InputEvent#getMaskForButton(int) InputEvent.getMaskForButton(button)} 289 * method to obtain the mask for any mouse button by its number. 290 * </ul> 291 * <p> 292 * The following standard button masks are also accepted: 293 * <ul> 294 * <li>{@code InputEvent.BUTTON1_MASK} 295 * <li>{@code InputEvent.BUTTON2_MASK} 296 * <li>{@code InputEvent.BUTTON3_MASK} 297 * </ul> 298 * However, it is recommended to use {@code InputEvent.BUTTON1_DOWN_MASK}, 299 * {@code InputEvent.BUTTON2_DOWN_MASK}, {@code InputEvent.BUTTON3_DOWN_MASK} instead. 300 * Either extended {@code _DOWN_MASK} or old {@code _MASK} values 301 * should be used, but both those models should not be mixed. 302 * @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button 303 * and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java 304 * @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button 305 * that does not exist on the mouse and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java 306 * @see #mousePress(int) 307 * @see InputEvent#getMaskForButton(int) 308 * @see Toolkit#areExtraMouseButtonsEnabled() 309 * @see java.awt.MouseInfo#getNumberOfButtons() 310 * @see java.awt.event.MouseEvent 311 */ 312 public synchronized void mouseRelease(int buttons) { 313 checkButtonsArgument(buttons); 314 peer.mouseRelease(buttons); 315 afterEvent(); 316 } 317 318 private void checkButtonsArgument(int buttons) { 319 if ( (buttons|LEGAL_BUTTON_MASK) != LEGAL_BUTTON_MASK ) { 320 throw new IllegalArgumentException("Invalid combination of button flags"); 321 } 322 } 323 324 /** 325 * Rotates the scroll wheel on wheel-equipped mice. 326 * 327 * @param wheelAmt number of "notches" to move the mouse wheel 328 * Negative values indicate movement up/away from the user, 329 * positive values indicate movement down/towards the user. 330 * 331 * @since 1.4 332 */ 333 public synchronized void mouseWheel(int wheelAmt) { 334 peer.mouseWheel(wheelAmt); 335 afterEvent(); 336 } 337 338 /** 339 * Presses a given key. The key should be released using the 340 * {@code keyRelease} method. 341 * <p> 342 * Key codes that have more than one physical key associated with them 343 * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the 344 * left or right shift key) will map to the left key. 345 * 346 * @param keycode Key to press (e.g. {@code KeyEvent.VK_A}) 347 * @throws IllegalArgumentException if {@code keycode} is not 348 * a valid key 349 * @see #keyRelease(int) 350 * @see java.awt.event.KeyEvent 351 */ 352 public synchronized void keyPress(int keycode) { 353 checkKeycodeArgument(keycode); 354 peer.keyPress(keycode); 355 afterEvent(); 356 } 357 358 /** 359 * Releases a given key. 360 * <p> 361 * Key codes that have more than one physical key associated with them 362 * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the 363 * left or right shift key) will map to the left key. 364 * 365 * @param keycode Key to release (e.g. {@code KeyEvent.VK_A}) 366 * @throws IllegalArgumentException if {@code keycode} is not a 367 * valid key 368 * @see #keyPress(int) 369 * @see java.awt.event.KeyEvent 370 */ 371 public synchronized void keyRelease(int keycode) { 372 checkKeycodeArgument(keycode); 373 peer.keyRelease(keycode); 374 afterEvent(); 375 } 376 377 private void checkKeycodeArgument(int keycode) { 378 // rather than build a big table or switch statement here, we'll 379 // just check that the key isn't VK_UNDEFINED and assume that the 380 // peer implementations will throw an exception for other bogus 381 // values e.g. -1, 999999 382 if (keycode == KeyEvent.VK_UNDEFINED) { 383 throw new IllegalArgumentException("Invalid key code"); 384 } 385 } 386 387 /** 388 * Returns the color of a pixel at the given screen coordinates. 389 * @param x X position of pixel 390 * @param y Y position of pixel 391 * @return Color of the pixel 392 */ 393 public synchronized Color getPixelColor(int x, int y) { 394 Color color = new Color(peer.getRGBPixel(x, y)); 395 return color; 396 } 397 398 /** 399 * Creates an image containing pixels read from the screen. This image does 400 * not include the mouse cursor. 401 * @param screenRect Rect to capture in screen coordinates 402 * @return The captured image 403 * @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero 404 * @throws SecurityException if {@code readDisplayPixels} permission is not granted 405 * @see SecurityManager#checkPermission 406 * @see AWTPermission 407 */ 408 public synchronized BufferedImage createScreenCapture(Rectangle screenRect) { 409 checkScreenCaptureAllowed(); 410 411 checkValidRect(screenRect); 412 413 BufferedImage image; 414 DataBufferInt buffer; 415 WritableRaster raster; 416 417 if (screenCapCM == null) { 418 /* 419 * Fix for 4285201 420 * Create a DirectColorModel equivalent to the default RGB ColorModel, 421 * except with no Alpha component. 422 */ 423 424 screenCapCM = new DirectColorModel(24, 425 /* red mask */ 0x00FF0000, 426 /* green mask */ 0x0000FF00, 427 /* blue mask */ 0x000000FF); 428 } 429 430 // need to sync the toolkit prior to grabbing the pixels since in some 431 // cases rendering to the screen may be delayed 432 Toolkit.getDefaultToolkit().sync(); 433 434 int pixels[]; 435 int[] bandmasks = new int[3]; 436 437 pixels = peer.getRGBPixels(screenRect); 438 buffer = new DataBufferInt(pixels, pixels.length); 439 440 bandmasks[0] = screenCapCM.getRedMask(); 441 bandmasks[1] = screenCapCM.getGreenMask(); 442 bandmasks[2] = screenCapCM.getBlueMask(); 443 444 raster = Raster.createPackedRaster(buffer, screenRect.width, screenRect.height, screenRect.width, bandmasks, null); 445 SunWritableRaster.makeTrackable(buffer); 446 447 image = new BufferedImage(screenCapCM, raster, false, null); 448 449 return image; 450 } 451 452 private static void checkValidRect(Rectangle rect) { 453 if (rect.width <= 0 || rect.height <= 0) { 454 throw new IllegalArgumentException("Rectangle width and height must be > 0"); 455 } 456 } 457 458 private static void checkScreenCaptureAllowed() { 459 SecurityManager security = System.getSecurityManager(); 460 if (security != null) { 461 security.checkPermission(AWTPermissions.READ_DISPLAY_PIXELS_PERMISSION); 462 } 463 } 464 465 /* 466 * Called after an event is generated 467 */ 468 private void afterEvent() { 469 autoWaitForIdle(); 470 autoDelay(); 471 } 472 473 /** 474 * Returns whether this Robot automatically invokes {@code waitForIdle} 475 * after generating an event. 476 * @return Whether {@code waitForIdle} is automatically called 477 */ 478 public synchronized boolean isAutoWaitForIdle() { 479 return isAutoWaitForIdle; 480 } 481 482 /** 483 * Sets whether this Robot automatically invokes {@code waitForIdle} 484 * after generating an event. 485 * @param isOn Whether {@code waitForIdle} is automatically invoked 486 */ 487 public synchronized void setAutoWaitForIdle(boolean isOn) { 488 isAutoWaitForIdle = isOn; 489 } 490 491 /* 492 * Calls waitForIdle after every event if so desired. 493 */ 494 private void autoWaitForIdle() { 495 if (isAutoWaitForIdle) { 496 waitForIdle(); 497 } 498 } 499 500 /** 501 * Returns the number of milliseconds this Robot sleeps after generating an event. 502 * 503 * @return the delay duration in milliseconds 504 */ 505 public synchronized int getAutoDelay() { 506 return autoDelay; 507 } 508 509 /** 510 * Sets the number of milliseconds this Robot sleeps after generating an event. 511 * 512 * @param ms the delay duration in milliseconds 513 * @throws IllegalArgumentException If {@code ms} 514 * is not between 0 and 60,000 milliseconds inclusive 515 */ 516 public synchronized void setAutoDelay(int ms) { 517 checkDelayArgument(ms); 518 autoDelay = ms; 519 } 520 521 /* 522 * Automatically sleeps for the specified interval after event generated. 523 */ 524 private void autoDelay() { 525 delay(autoDelay); 526 } 527 528 /** 529 * Sleeps for the specified time. 530 * To catch any {@code InterruptedException}s that occur, 531 * {@code Thread.sleep()} may be used instead. 532 * 533 * @param ms time to sleep in milliseconds 534 * @throws IllegalArgumentException if {@code ms} 535 * is not between 0 and 60,000 milliseconds inclusive 536 * @see java.lang.Thread#sleep 537 */ 538 public synchronized void delay(int ms) { 539 checkDelayArgument(ms); 540 try { 541 Thread.sleep(ms); 542 } catch(InterruptedException ite) { 543 ite.printStackTrace(); 544 } 545 } 546 547 private void checkDelayArgument(int ms) { 548 if (ms < 0 || ms > MAX_DELAY) { 549 throw new IllegalArgumentException("Delay must be to 0 to 60,000ms"); 550 } 551 } 552 553 /** 554 * Waits until all events currently on the event queue have been processed. 555 * @throws IllegalThreadStateException if called on the AWT event dispatching thread 556 */ 557 public synchronized void waitForIdle() { 558 checkNotDispatchThread(); 559 SunToolkit.flushPendingEvents(); 560 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 561 } 562 563 private void checkNotDispatchThread() { 564 if (EventQueue.isDispatchThread()) { 565 throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread"); 566 } 567 } 568 569 /** 570 * Returns a string representation of this Robot. 571 * 572 * @return the string representation. 573 */ 574 @Override 575 public synchronized String toString() { 576 String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle(); 577 return getClass().getName() + "[ " + params + " ]"; 578 } 579 }