1 /*
   2  * Copyright (c) 2011, 2018, 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 package com.sun.glass.ui.mac;
  26 
  27 import javafx.scene.image.WritableImage;
  28 import javafx.scene.input.KeyCode;
  29 import javafx.scene.input.MouseButton;
  30 import javafx.scene.paint.Color;
  31 
  32 import com.sun.glass.ui.Application;
  33 import com.sun.glass.ui.GlassRobot;
  34 import com.sun.glass.ui.Pixels;
  35 
  36 /**
  37  * MacOSX platform implementation class for Robot.
  38  */
  39 final class MacRobot extends GlassRobot {
  40 
  41     // TODO: get rid of native Robot object
  42     private long ptr;
  43 
  44     private native long _init();
  45     @Override
  46     public void create() {
  47         Application.checkEventThread();
  48         ptr = _init();
  49     }
  50 
  51     native private void _destroy(long ptr);
  52     @Override
  53     public void destroy() {
  54         Application.checkEventThread();
  55         if (ptr == 0) {
  56             return;
  57         }
  58         _destroy(ptr);
  59     }
  60 
  61     native protected void _keyPress(int code);
  62     @Override
  63     public void keyPress(KeyCode code) {
  64         Application.checkEventThread();
  65         _keyPress(code.getCode());
  66     }
  67 
  68     native protected void _keyRelease(int code);
  69     @Override
  70     public void keyRelease(KeyCode code) {
  71         Application.checkEventThread();
  72         _keyRelease(code.getCode());
  73     }
  74 
  75     native private void _mouseMove(long ptr, float x, float y);
  76     @Override
  77     public void mouseMove(double x, double y) {
  78         Application.checkEventThread();
  79         if (ptr == 0) {
  80             return;
  81         }
  82         _mouseMove(ptr, (float) x, (float) y);
  83     }
  84 
  85     native private void _mousePress(long ptr, int buttons);
  86     @Override
  87     public void mousePress(MouseButton... buttons) {
  88         Application.checkEventThread();
  89         if (ptr == 0) {
  90             return;
  91         }
  92         _mousePress(ptr, GlassRobot.convertToRobotMouseButton(buttons));
  93     }
  94 
  95     native private void _mouseRelease(long ptr, int buttons);
  96     @Override
  97     public void mouseRelease(MouseButton... buttons) {
  98         Application.checkEventThread();
  99         if (ptr == 0) {
 100             return;
 101         }
 102         _mouseRelease(ptr, GlassRobot.convertToRobotMouseButton(buttons));
 103     }
 104 
 105     native protected void _mouseWheel(int wheelAmt);
 106     @Override
 107     public void mouseWheel(int wheelAmt) {
 108         Application.checkEventThread();
 109         _mouseWheel(wheelAmt);
 110     }
 111 
 112     native private float _getMouseX(long ptr);
 113     @Override
 114     public double getMouseX() {
 115         Application.checkEventThread();
 116         if (ptr == 0) {
 117             return 0;
 118         }
 119         return _getMouseX(ptr);
 120     }
 121 
 122     native private float _getMouseY(long ptr);
 123     @Override
 124     public double getMouseY() {
 125         Application.checkEventThread();
 126         if (ptr == 0) {
 127             return 0;
 128         }
 129         return _getMouseY(ptr);
 130     }
 131 
 132     native protected int _getPixelColor(double x, double y);
 133     @Override
 134     public Color getPixelColor(double x, double y) {
 135         Application.checkEventThread();
 136         return GlassRobot.convertFromIntArgb(_getPixelColor(x, y));
 137     }
 138 
 139     native protected Pixels _getScreenCapture(int x, int y, int width, int height, boolean scaleToFit);
 140     @Override
 141     public WritableImage getScreenCapture(WritableImage image, double x, double y, double width,
 142                                           double height, boolean scaleToFit) {
 143         Application.checkEventThread();
 144         if (width <= 0) {
 145             throw new IllegalArgumentException("width must be > 0");
 146         }
 147         if (height <= 0) {
 148             throw new IllegalArgumentException("height must be > 0");
 149         }
 150 
 151         Pixels pixels;
 152         pixels = _getScreenCapture((int) x, (int) y, (int) width, (int) height, scaleToFit);
 153         return convertFromPixels(image, pixels);
 154     }
 155 }
 156