1 /*
   2  * Copyright (c) 2012, 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 #include "LensCommon.h"
  27 #include "com_sun_glass_ui_lens_LensRobot.h"
  28 
  29 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensRobot_postScrollEvent
  30 (JNIEnv *env, jobject _this, jint wheelAmt) {
  31 
  32     jboolean isSuccessful =
  33         glass_robot_postScrollEvent(env, wheelAmt);
  34 
  35     if (!isSuccessful) {
  36         glass_throw_exception_by_name(env, glass_RuntimeException,
  37                                       "Failed to post scroll event");
  38     }
  39 }
  40 
  41 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensRobot_postKeyEvent
  42 (JNIEnv *env, jobject _this, jint keyEventType, jint jfxKeyCode) {
  43 
  44     jboolean isSuccessful = glass_robot_postKeyEvent(env, keyEventType, jfxKeyCode);
  45 
  46     if (!isSuccessful) {
  47         glass_throw_exception_by_name(env, glass_RuntimeException,
  48                                       "Failed to post key event");
  49     }
  50 }
  51 
  52 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensRobot_postMouseEvent
  53 (JNIEnv *env, jobject _this, jint mouseEventType, jint x, jint y, jint buttons) {
  54 
  55     jboolean isSuccessful =
  56         glass_robot_postMouseEvent(env, mouseEventType,
  57                                    x, y, buttons);
  58 
  59     if (!isSuccessful) {
  60         glass_throw_exception_by_name(env, glass_RuntimeException,
  61                                       "Failed to post mouse event");
  62     }
  63 }
  64 
  65 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_lens_LensRobot_getMouseLocation
  66 (JNIEnv *env, jobject _this, jint axsis) {
  67 
  68     jint x;
  69     jint y;
  70     jint result;
  71     jboolean isSuccessful = glass_robot_getMouseLocation(&x, &y);
  72 
  73     if (isSuccessful) {
  74         result = (axsis == com_sun_glass_ui_lens_LensRobot_GET_X) ? x : y;
  75     } else {
  76         glass_throw_exception_by_name(env, glass_RuntimeException,
  77                                       "Failed to get mouse location");
  78         result = -1;
  79     }
  80 
  81     return result;
  82 }
  83 
  84 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_lens_LensRobot__1getPixelColor
  85 (JNIEnv *env, jobject _this, jint x, jint y) {
  86 
  87     jint pixelColor = 0;
  88     GLASS_LOG_FINEST("Getting pixel at %i,%i", x, y);
  89     jboolean isSuccessful = glass_screen_capture(x, y, 1, 1, &pixelColor);
  90     GLASS_LOG_FINEST("PixelColor = 0x08%x", pixelColor);
  91 
  92     if (!isSuccessful) {
  93         glass_throw_exception_by_name(env, glass_RuntimeException,
  94                                       "Failed to get pixel color");
  95     }
  96 
  97     return pixelColor;
  98 }
  99 
 100 JNIEXPORT void JNICALL Java_com_sun_glass_ui_lens_LensRobot__1getScreenCapture
 101 (JNIEnv *env, jobject _this, jint x, jint y,
 102  jint width, jint height, jintArray data) {
 103 
 104     GLASS_LOG_FINEST("Capturing screen region %i,%i+%ix%i",
 105                      x, y, width, height);
 106 
 107     jint pixelsBufferLength = width * height;
 108     jint *pixels = (jint *)malloc(sizeof(jint) * pixelsBufferLength);
 109     GLASS_LOG_FINEST("Allocated pixel offer at %p, size=%i bytes",
 110                      pixels, sizeof(jint) * pixelsBufferLength);
 111     jboolean isSuccessful;
 112 
 113     if (pixels != NULL) {
 114 
 115 
 116         isSuccessful = glass_screen_capture(x, y, width, height, pixels);
 117         if (isSuccessful) {
 118             GLASS_LOG_FINEST("JNI SetIntArrayRegion");
 119             (*env)->SetIntArrayRegion(env, data, 0, pixelsBufferLength, pixels);
 120         } else {
 121             glass_throw_exception_by_name(env, glass_RuntimeException,
 122                                           "Failed to capture screen");
 123         }
 124 
 125         GLASS_LOG_FINEST("free(%p)", pixels);
 126         free(pixels);
 127     } else {
 128         glass_throw_exception_by_name(env, "java/lang/OutOfMemoryError",
 129                                       "Failed to allocate a buffer for"
 130                                       " screen capture");
 131     }
 132 }