1 /*
   2  * Copyright (c) 2016, 2020, 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 #import "jni_util.h"
  27 
  28 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  29 #import <ApplicationServices/ApplicationServices.h>
  30 
  31 #import "CRobotKeyCode.h"
  32 #import "LWCToolkit.h"
  33 #import "sun_lwawt_macosx_CRobot.h"
  34 #import "java_awt_event_InputEvent.h"
  35 #import "java_awt_event_KeyEvent.h"
  36 #import "sizecalc.h"
  37 #import "ThreadUtilities.h"
  38 
  39 // Starting number for event numbers generated by Robot.
  40 // Apple docs don't mention at all what are the requirements
  41 // for these numbers. It seems that they must be higher
  42 // than event numbers from real events, which start at some
  43 // value close to zero. There is no API for obtaining current
  44 // event number, so we have to start from some random number.
  45 // 32000 as starting value works for me, let's hope that it will
  46 // work for others as well.
  47 #define ROBOT_EVENT_NUMBER_START 32000
  48 
  49 #define k_JAVA_ROBOT_WHEEL_COUNT 1
  50 
  51 #if !defined(kCGBitmapByteOrder32Host)
  52 #define kCGBitmapByteOrder32Host 0
  53 #endif
  54 
  55 // In OS X, left and right mouse button share the same click count.
  56 // That is, if one starts clicking the left button rapidly and then
  57 // switches to the right button, then the click count will continue
  58 // increasing, without dropping to 1 in between. The middle button,
  59 // however, has its own click count.
  60 // For robot, we aren't going to emulate all that complexity. All our
  61 // synhtetic clicks share the same click count.
  62 static int gsClickCount;
  63 static NSTimeInterval gsLastClickTime;
  64 
  65 // Apparently, for mouse up/down events we have to set an event number
  66 // that is incremented on each button press. Otherwise, strange things
  67 // happen with z-order.
  68 static int gsEventNumber;
  69 static int* gsButtonEventNumber;
  70 
  71 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode);
  72 
  73 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
  74                            CGEventType type, int clickCount, int eventNumber);
  75 
  76 static int GetClickCount(BOOL isDown);
  77 
  78 static void
  79 CreateJavaException(JNIEnv* env, CGError err)
  80 {
  81     // Throw a java exception indicating what is wrong.
  82     NSString* s = [NSString stringWithFormat:@"Robot: CGError: %d", err];
  83     (*env)->ThrowNew(env, (*env)->FindClass(env, "java/awt/AWTException"),
  84                      [s UTF8String]);
  85 }
  86 
  87 /*
  88  * Class:     sun_lwawt_macosx_CRobot
  89  * Method:    initRobot
  90  * Signature: (V)V
  91  */
  92 JNIEXPORT void JNICALL
  93 Java_sun_lwawt_macosx_CRobot_initRobot
  94 (JNIEnv *env, jobject peer)
  95 {
  96     // Set things up to let our app act like a synthetic keyboard and mouse.
  97     // Always set all states, in case Apple ever changes default behaviors.
  98     static int setupDone = 0;
  99     if (!setupDone) {
 100         [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 101             int i;
 102             jint* tmp;
 103             jboolean copy = JNI_FALSE;
 104 
 105             setupDone = 1;
 106             // Don't block local events after posting ours
 107             CGSetLocalEventsSuppressionInterval(0.0);
 108 
 109             // Let our event's modifier key state blend with local hardware events
 110             CGEnableEventStateCombining(TRUE);
 111 
 112             // Don't let our events block local hardware events
 113             CGSetLocalEventsFilterDuringSupressionState(
 114                                         kCGEventFilterMaskPermitAllEvents,
 115                                         kCGEventSupressionStateSupressionInterval);
 116             CGSetLocalEventsFilterDuringSupressionState(
 117                                         kCGEventFilterMaskPermitAllEvents,
 118                                         kCGEventSupressionStateRemoteMouseDrag);
 119 
 120             gsClickCount = 0;
 121             gsLastClickTime = 0;
 122             gsEventNumber = ROBOT_EVENT_NUMBER_START;
 123 
 124             gsButtonEventNumber = (int*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(int), gNumberOfButtons);
 125             if (gsButtonEventNumber == NULL) {
 126                 JNU_ThrowOutOfMemoryError(env, NULL);
 127                 return;
 128             }
 129 
 130             for (i = 0; i < gNumberOfButtons; ++i) {
 131                 gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
 132             }
 133         }];
 134     }
 135 }
 136 
 137 /*
 138  * Class:     sun_lwawt_macosx_CRobot
 139  * Method:    mouseEvent
 140  * Signature: (IIIIZZ)V
 141  */
 142 JNIEXPORT void JNICALL
 143 Java_sun_lwawt_macosx_CRobot_mouseEvent
 144 (JNIEnv *env, jobject peer, jint mouseLastX, jint mouseLastY, jint buttonsState,
 145  jboolean isButtonsDownState, jboolean isMouseMove)
 146 {
 147     JNF_COCOA_ENTER(env);
 148 
 149     // This is the native method called when Robot mouse events occur.
 150     // The CRobot tracks the mouse position, and which button was
 151     // pressed. The peer also tracks the mouse button desired state,
 152     // the appropriate key modifier state, and whether the mouse action
 153     // is simply a mouse move with no mouse button state changes.
 154 
 155     // volatile, otherwise it warns that it might be clobbered by 'longjmp'
 156     volatile CGPoint point;
 157 
 158     point.x = mouseLastX;
 159     point.y = mouseLastY;
 160 
 161     __block CGMouseButton button = kCGMouseButtonLeft;
 162     __block CGEventType type = kCGEventMouseMoved;
 163 
 164     void (^HandleRobotButton)(CGMouseButton, CGEventType, CGEventType, CGEventType) =
 165         ^(CGMouseButton cgButton, CGEventType cgButtonUp, CGEventType cgButtonDown,
 166           CGEventType cgButtonDragged) {
 167 
 168             button = cgButton;
 169             type = cgButtonUp;
 170 
 171             if (isButtonsDownState) {
 172                 if (isMouseMove) {
 173                     type = cgButtonDragged;
 174                 } else {
 175                     type = cgButtonDown;
 176                 }
 177             }
 178         };
 179 
 180     // Left
 181     if (buttonsState & java_awt_event_InputEvent_BUTTON1_MASK ||
 182         buttonsState & java_awt_event_InputEvent_BUTTON1_DOWN_MASK ) {
 183 
 184         HandleRobotButton(kCGMouseButtonLeft, kCGEventLeftMouseUp,
 185                           kCGEventLeftMouseDown, kCGEventLeftMouseDragged);
 186     }
 187 
 188     // Other
 189     if (buttonsState & java_awt_event_InputEvent_BUTTON2_MASK ||
 190         buttonsState & java_awt_event_InputEvent_BUTTON2_DOWN_MASK ) {
 191 
 192         HandleRobotButton(kCGMouseButtonCenter, kCGEventOtherMouseUp,
 193                           kCGEventOtherMouseDown, kCGEventOtherMouseDragged);
 194     }
 195 
 196     // Right
 197     if (buttonsState & java_awt_event_InputEvent_BUTTON3_MASK ||
 198         buttonsState & java_awt_event_InputEvent_BUTTON3_DOWN_MASK ) {
 199 
 200         HandleRobotButton(kCGMouseButtonRight, kCGEventRightMouseUp,
 201                           kCGEventRightMouseDown, kCGEventRightMouseDragged);
 202     }
 203 
 204     // Extra
 205     if (gNumberOfButtons > 3) {
 206         int extraButton;
 207         for (extraButton = 3; extraButton < gNumberOfButtons; ++extraButton) {
 208             if ((buttonsState & gButtonDownMasks[extraButton])) {
 209                 HandleRobotButton(extraButton, kCGEventOtherMouseUp,
 210                             kCGEventOtherMouseDown, kCGEventOtherMouseDragged);
 211             }
 212         }
 213     }
 214 
 215     int clickCount = 0;
 216     int eventNumber = gsEventNumber;
 217 
 218     if (isMouseMove) {
 219         // any mouse movement resets click count
 220         gsLastClickTime = 0;
 221     } else {
 222         clickCount = GetClickCount(isButtonsDownState);
 223 
 224         if (isButtonsDownState) {
 225             gsButtonEventNumber[button] = gsEventNumber++;
 226         }
 227         eventNumber = gsButtonEventNumber[button];
 228     }
 229 
 230     PostMouseEvent(point, button, type, clickCount, eventNumber);
 231 
 232     JNF_COCOA_EXIT(env);
 233 }
 234 
 235 /*
 236  * Class:     sun_lwawt_macosx_CRobot
 237  * Method:    mouseWheel
 238  * Signature: (I)V
 239  */
 240 JNIEXPORT void JNICALL
 241 Java_sun_lwawt_macosx_CRobot_mouseWheel
 242 (JNIEnv *env, jobject peer, jint wheelAmt)
 243 {
 244     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 245         CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
 246                                                 kCGScrollEventUnitLine,
 247                                                 k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
 248         if (event != NULL) {
 249             CGEventPost(kCGSessionEventTap, event);
 250             CFRelease(event);
 251         }
 252     }];
 253 }
 254 
 255 /*
 256  * Class:     sun_lwawt_macosx_CRobot
 257  * Method:    keyEvent
 258  * Signature: (IZ)V
 259  */
 260 JNIEXPORT void JNICALL
 261 Java_sun_lwawt_macosx_CRobot_keyEvent
 262 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 263 {
 264     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 265         CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
 266         CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
 267         if (event != NULL) {
 268             CGEventPost(kCGSessionEventTap, event);
 269             CFRelease(event);
 270         }
 271     }];
 272 }
 273 
 274 /*
 275  * Class:     sun_lwawt_macosx_CRobot
 276  * Method:    nativeGetScreenPixels
 277  * Signature: (IIIII[I)V
 278  */
 279 JNIEXPORT void JNICALL
 280 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
 281 (JNIEnv *env, jobject peer,
 282  jint x, jint y, jint width, jint height, jdouble scale, jintArray pixels)
 283 {
 284     JNF_COCOA_ENTER(env);
 285 
 286     jint picX = x;
 287     jint picY = y;
 288     jint picWidth = width;
 289     jint picHeight = height;
 290 
 291     CGRect screenRect = CGRectMake(picX / scale, picY / scale,
 292                                 picWidth / scale, picHeight / scale);
 293     CGImageRef screenPixelsImage = CGWindowListCreateImage(screenRect,
 294                                         kCGWindowListOptionOnScreenOnly,
 295                                         kCGNullWindowID, kCGWindowImageBestResolution);
 296 
 297     if (screenPixelsImage == NULL) {
 298         return;
 299     }
 300 
 301     // get a pointer to the Java int array
 302     void *jPixelData = (*env)->GetPrimitiveArrayCritical(env, pixels, 0);
 303     CHECK_NULL(jPixelData);
 304 
 305     // create a graphics context around the Java int array
 306     CGColorSpaceRef picColorSpace = CGColorSpaceCreateWithName(
 307                                             kCGColorSpaceSRGB);
 308     CGContextRef jPicContextRef = CGBitmapContextCreate(
 309                                             jPixelData,
 310                                             picWidth, picHeight,
 311                                             8, picWidth * sizeof(jint),
 312                                             picColorSpace,
 313                                             kCGBitmapByteOrder32Host |
 314                                             kCGImageAlphaPremultipliedFirst);
 315 
 316     CGColorSpaceRelease(picColorSpace);
 317 
 318     // flip, scale, and color correct the screen image into the Java pixels
 319     CGRect bounds = { { 0, 0 }, { picWidth, picHeight } };
 320     CGContextDrawImage(jPicContextRef, bounds, screenPixelsImage);
 321     CGContextFlush(jPicContextRef);
 322 
 323     // cleanup
 324     CGContextRelease(jPicContextRef);
 325     CGImageRelease(screenPixelsImage);
 326 
 327     // release the Java int array back up to the JVM
 328     (*env)->ReleasePrimitiveArrayCritical(env, pixels, jPixelData, 0);
 329 
 330     JNF_COCOA_EXIT(env);
 331 }
 332 
 333 /****************************************************
 334  * Helper methods
 335  ****************************************************/
 336 
 337 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
 338                            CGEventType type, int clickCount, int eventNumber)
 339 {
 340     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 341         CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, type, point, button);
 342         if (mouseEvent != NULL) {
 343             CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
 344             CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
 345             CGEventPost(kCGSessionEventTap, mouseEvent);
 346             CFRelease(mouseEvent);
 347         }
 348     }];
 349 }
 350 
 351 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode)
 352 {
 353     CRobotKeyCodeMapping *keyCodeMapping = [CRobotKeyCodeMapping sharedInstance];
 354     return [keyCodeMapping getOSXKeyCodeForJavaKey:javaKeyCode];
 355 }
 356 
 357 static int GetClickCount(BOOL isDown) {
 358     NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate];
 359     NSTimeInterval clickInterval = now - gsLastClickTime;
 360     BOOL isWithinTreshold = clickInterval < [NSEvent doubleClickInterval];
 361 
 362     if (isDown) {
 363         if (isWithinTreshold) {
 364             gsClickCount++;
 365         } else {
 366             gsClickCount = 1;
 367         }
 368 
 369         gsLastClickTime = now;
 370     } else {
 371         // In OS X, a mouse up has the click count of the last mouse down
 372         // if an interval between up and down is within the double click
 373         // threshold, and 0 otherwise.
 374         if (!isWithinTreshold) {
 375             gsClickCount = 0;
 376         }
 377     }
 378 
 379     return gsClickCount;
 380 }