1 /*
   2  * Copyright (c) 2007, 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.jemmy.input;
  24 
  25 
  26 import org.jemmy.Point;
  27 import org.jemmy.Rectangle;
  28 import org.jemmy.env.Environment;
  29 import org.jemmy.env.Timeout;
  30 import org.jemmy.image.Image;
  31 import org.jemmy.interfaces.Keyboard.KeyboardButton;
  32 import org.jemmy.interfaces.Keyboard.KeyboardButtons;
  33 import org.jemmy.interfaces.Keyboard.KeyboardModifiers;
  34 import org.jemmy.interfaces.Modifier;
  35 import org.jemmy.interfaces.Mouse.MouseButton;
  36 
  37 import java.awt.event.InputEvent;
  38 
  39 import static org.jemmy.input.AWTRobotInputFactory.ROBOT_DELAY_TIMEOUT_NAME;
  40 
  41 
  42 /**
  43  * @author Alexandre Iline(alexandre.iline@sun.com), KAM <mrkam@mail.ru>
  44  */
  45 public class RobotDriver {
  46 
  47     private static boolean haveOldPos = false;
  48     private static int smoothness;
  49     private static double oldX;
  50     private static double oldY;
  51 
  52     static {
  53         Environment.getEnvironment().setTimeout(
  54                 new Timeout(ROBOT_DELAY_TIMEOUT_NAME, 10));
  55         Environment.getEnvironment().setPropertyIfNotSet(
  56                 AWTRobotInputFactory.ROBOT_MOUSE_SMOOTHNESS_PROPERTY,
  57                 new Integer(Integer.MAX_VALUE).toString());
  58         smoothness =  Integer.parseInt(
  59                 (String)Environment.getEnvironment().getProperty(
  60                 AWTRobotInputFactory.ROBOT_MOUSE_SMOOTHNESS_PROPERTY));
  61     }
  62 
  63     /**
  64      * Sets mouse smoothness
  65      * @param mouseSmoothness the maximum distance in pixels between
  66      * mouse positions during movement
  67      * @see #moveMouse(Point)
  68      */
  69     public static void setMouseSmoothness(int mouseSmoothness) {
  70         smoothness = mouseSmoothness;
  71     }
  72 
  73     /**
  74      * Gets mouse smoothness
  75      * @return the maximum distance in pixels between
  76      * mouse positions during movement
  77      * @see #setMouseSmoothness(int)
  78      * @see #moveMouse(Point)
  79      */
  80     public static int getMouseSmoothness() {
  81         return smoothness;
  82     }
  83 
  84     /**
  85      * Constructs a RobotDriver object.
  86      * @param autoDelay Time for <code>Robot.setAutoDelay(long)</code> method.
  87      */
  88     public RobotDriver(Timeout autoDelay) {
  89         RobotExecutor.get().setAutoDelay(autoDelay);
  90     }
  91 
  92     /**
  93      * Constructs a RobotDriver object.
  94      * @param env Environment with ROBOT_DELAY_TIMEOUT_NAME timeout
  95      * @see AWTRobotInputFactory#ROBOT_DELAY_TIMEOUT_NAME
  96      */
  97     public RobotDriver(Environment env) {
  98         this(env.getTimeout(ROBOT_DELAY_TIMEOUT_NAME));
  99     }
 100 
 101     /**
 102      * Capture an image of specified rectangular area of screen
 103      * @param screenRect area on screen that will be captured
 104      * @return image of specified rectangular area of screen
 105      */
 106     public static Image createScreenCapture(Rectangle screenRect) {
 107         return RobotExecutor.get().createScreenCapture(screenRect);
 108     }
 109 
 110     /**
 111      * Presses mouse button specified by mouseButton preceding pressing of
 112      * modifier keys or buttons specified by modifiers
 113      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 114      * @param modifiers Combination of InputEvent.*_DOWN_MASK
 115      * @see java.awt.event.InputEvent
 116      * @see java.awt.event.MouseEvent
 117      */
 118     public void pressMouse(MouseButton mouseButton, Modifier... modifiers) {
 119         pressModifiers(modifiers);
 120         makeAnOperation("mousePress",
 121                 new Object[]{mouseButton},
 122                 new Class[]{MouseButton.class});
 123     }
 124 
 125     /**
 126      * Releases mouse button specified by mouseButton then releasing
 127      * modifier keys or buttons specified by modifiers
 128      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 129      * @param modifiers Combination of InputEvent.*_DOWN_MASK
 130      * @see java.awt.event.InputEvent
 131      * @see java.awt.event.MouseEvent
 132      */
 133     public void releaseMouse(MouseButton mouseButton, Modifier... modifiers) {
 134         makeAnOperation("mouseRelease",
 135                 new Object[]{mouseButton},
 136                 new Class[]{MouseButton.class});
 137         releaseModifiers(modifiers);
 138     }
 139 
 140     /**
 141      * Moves mouse to the specified mouse. When previous mouse location is
 142      * remembered mouse moved smoothly between the points according to
 143      * mouse smoothness parameter. Otherwise it jumps to the specified point
 144      * @param point Position on the screen where to move mouse
 145      * @see #setMouseSmoothness(int)
 146      * @see #getMouseSmoothness()
 147      */
 148     public void moveMouse(Point point) {
 149         double targetX = point.x;
 150         double targetY = point.y;
 151         if (haveOldPos && (oldX != targetX || oldY != targetY)) {
 152             double currX = oldX;
 153             double currY = oldY;
 154             double hyp = Math.sqrt((targetX - currX) * (targetX - currX) +
 155                     (targetY - currY) * (targetY - currY));
 156             double steps = Math.ceil(hyp / Math.min(hyp, smoothness));
 157             double vx = (targetX - currX) / steps;
 158             double vy = (targetY - currY) / steps;
 159             assert (long)vx * vx + (long)vy * vy <= (long)smoothness * smoothness;
 160             while (Math.round(currX) != Math.round(targetX) ||
 161                     Math.round(currY) != Math.round(targetY)) {
 162                 currX += vx;
 163                 currY += vy;
 164                 makeAnOperation("mouseMove", new Object[]{
 165                             new Integer((int) Math.round(currX)),
 166                             new Integer((int) Math.round(currY))},
 167                         new Class[]{Integer.TYPE, Integer.TYPE});
 168             }
 169         } else {
 170             makeAnOperation("mouseMove",
 171                     new Object[]{new Integer(point.x), new Integer(point.y)},
 172                     new Class[]{Integer.TYPE, Integer.TYPE});
 173         }
 174         haveOldPos = true;
 175         oldX = targetX;
 176         oldY = targetY;
 177     }
 178 
 179     /**
 180      * Clicks the mouse button specified by mouseButton at the specified point
 181      * specified number of times preceding it by pressing the modifiers key or
 182      * buttons and ending by releasing them. The last click is as long as
 183      * mouseClick timeout
 184      * @param point Screen location where to click mouse
 185      * @param clickCount Number of clicks
 186      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 187      * @param modifiers Combination of InputEvent.*_DOWN_MASK
 188      * @param mouseClick Timeout of the last click
 189      * @see java.awt.event.InputEvent
 190      * @see java.awt.event.MouseEvent
 191      */
 192     public void clickMouse(Point point, int clickCount, MouseButton mouseButton, Timeout mouseClick, Modifier... modifiers) {
 193         pressModifiers(modifiers);
 194         moveMouse(point);
 195         makeAnOperation("mousePress", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 196         for (int i = 1; i < clickCount; i++) {
 197             makeAnOperation("mouseRelease", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 198             makeAnOperation("mousePress", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 199         }
 200         mouseClick.sleep();
 201         makeAnOperation("mouseRelease", new Object[]{mouseButton}, new Class[]{MouseButton.class});
 202         releaseModifiers(modifiers);
 203     }
 204 
 205     /**
 206      * @deprecated Implementation doesn't seem to be correct as it ignores mouseButton and modifiers
 207      * @param point
 208      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 209      * @param modifiers
 210      */
 211     public void dragMouse(Point point, int mouseButton, int modifiers) {
 212         moveMouse(point);
 213     }
 214 
 215     /**
 216      * Performs drag and drop from startPoint to endPoint using specified
 217      * mouseButton and modifiers to perform it.
 218      * @param startPoint Screen coordinates of drag start point
 219      * @param endPoint Screen coordinates of drag end point
 220      * @param mouseButton One of MouseEvent.BUTTON*_MASK
 221      * @param modifiers Combination of InputEvent.*_DOWN_MASK
 222      * @param before Timeout between pressing mouse at the startPoint and
 223      * mouse move
 224      * @param after Timeout between mouse move to the endPoint and mouse
 225      * release
 226      */
 227     public void dragNDrop(Point startPoint, Point endPoint, MouseButton mouseButton, Modifier modifiers[], Timeout before, Timeout after) {
 228         moveMouse(startPoint);
 229         pressMouse(mouseButton, modifiers);
 230         before.sleep();
 231         moveMouse(endPoint);
 232         after.sleep();
 233         releaseMouse(mouseButton, modifiers);
 234     }
 235 
 236     /**
 237      * Presses a key.
 238      * @param kbdButton Key code (<code>KeyEventVK_*</code> field.
 239      * @param modifiers a combination of <code>InputEvent.*_MASK</code> fields.
 240      */
 241     public void pressKey(KeyboardButton kbdButton, Modifier... modifiers) {
 242         pressModifiers(modifiers);
 243         makeAnOperation("keyPress",
 244                 new Object[]{kbdButton},
 245                 new Class[]{KeyboardButton.class});
 246     }
 247 
 248     /**
 249      * Releases a key.
 250      * @param kbdButton Key code (<code>KeyEventVK_*</code> field.
 251      * @param modifiers a combination of <code>InputEvent.*_MASK</code> fields.
 252      */
 253     public void releaseKey(KeyboardButton kbdButton, Modifier... modifiers) {
 254         makeAnOperation("keyRelease",
 255                 new Object[]{kbdButton},
 256                 new Class[]{KeyboardButton.class});
 257         releaseModifiers(modifiers);
 258     }
 259 
 260     /**
 261      * Turns the wheel.
 262      * @param p
 263      * @param amount Either positive or negative
 264      * @param modifiers
 265      */
 266     public void turnWheel(Point p, int amount, Modifier... modifiers) {
 267         pressModifiers(modifiers);
 268         moveMouse(p);
 269         java.awt.Robot r = null;
 270         makeAnOperation("mouseWheel",
 271                 new Object[]{amount},
 272                 new Class[]{Integer.TYPE});
 273         releaseModifiers(modifiers);
 274     }
 275 
 276     /**
 277      * Performs a single operation.
 278      * @param method a name of <code>java.awt.Robot</code> method.
 279      * @param params method parameters
 280      * @param paramClasses method parameters classes
 281      */
 282     public void makeAnOperation(final String method, final Object[] params, final Class[] paramClasses) {
 283         RobotExecutor.get().makeAnOperation(method, params, paramClasses);
 284     }
 285 
 286     final static int SHIFT_MASK = InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK;
 287     final static int ALT_GRAPH_MASK = InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.ALT_GRAPH_MASK;
 288     final static int ALT_MASK = InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK;
 289     final static int META_MASK = InputEvent.META_DOWN_MASK | InputEvent.META_MASK;
 290     final static int CTRL_MASK = InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK;
 291 
 292     /**
 293      * Presses modifiers keys by robot.
 294      * @param modifiers a combination of <code>InputEvent.*_MASK</code> fields.
 295      */
 296     protected void pressModifiers(Modifier... modifiers) {
 297         for (Modifier modifier : modifiers) { // TODO: ALT_GRAPH_MASK?
 298             if (modifier == KeyboardModifiers.ALT_DOWN_MASK) {
 299                 pressKey(KeyboardButtons.ALT);
 300             } else if (modifier == KeyboardModifiers.CTRL_DOWN_MASK) {
 301                 pressKey(KeyboardButtons.CONTROL);
 302             } else if (modifier == KeyboardModifiers.META_DOWN_MASK) {
 303                 pressKey(KeyboardButtons.META);
 304             } else if (modifier == KeyboardModifiers.SHIFT_DOWN_MASK) {
 305                 pressKey(KeyboardButtons.SHIFT);
 306             }
 307         }
 308     }
 309 
 310     /**
 311      * Releases modifiers keys by robot.
 312      * @param modifiers a combination of <code>InputEvent.*_MASK</code> fields.
 313      */
 314     protected void releaseModifiers(Modifier... modifiers) {
 315         for (Modifier modifier : modifiers) { // TODO: ALT_GRAPH_MASK?
 316             if (modifier == KeyboardModifiers.ALT_DOWN_MASK) {
 317                 releaseKey(KeyboardButtons.ALT);
 318             } else if (modifier == KeyboardModifiers.CTRL_DOWN_MASK) {
 319                 releaseKey(KeyboardButtons.CONTROL);
 320             } else if (modifier == KeyboardModifiers.META_DOWN_MASK) {
 321                 releaseKey(KeyboardButtons.META);
 322             } else if (modifier == KeyboardModifiers.SHIFT_DOWN_MASK) {
 323                 releaseKey(KeyboardButtons.SHIFT);
 324             }
 325         }
 326     }
 327     /**
 328      * If java.awt.Robot is running in other JVM, it shutdowns that JVM
 329      * @see AWTRobotInputFactory#runInOtherJVM(boolean)
 330      */
 331     public static void exit() {
 332         RobotExecutor.get().exit();
 333     }
 334 }