1 /*
   2  * Copyright (c) 2016, 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 
  27 #import "jni_util.h"
  28 
  29 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  30 #import <ApplicationServices/ApplicationServices.h>
  31 
  32 #import "CRobotKeyCode.h"
  33 #import "LWCToolkit.h"
  34 #import "sun_lwawt_macosx_CRobot.h"
  35 #import "java_awt_event_InputEvent.h"
  36 #import "java_awt_event_KeyEvent.h"
  37 #import "sizecalc.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 static CRobotKeyCodeMapping *keyCodeMapping;
  71 
  72 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode);
  73 
  74 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
  75                            CGEventType type, int clickCount, int eventNumber);
  76 
  77 static int GetClickCount(BOOL isDown);
  78 
  79 static void
  80 CreateJavaException(JNIEnv* env, CGError err)
  81 {
  82     // Throw a java exception indicating what is wrong.
  83     NSString* s = [NSString stringWithFormat:@"Robot: CGError: %d", err];
  84     (*env)->ThrowNew(env, (*env)->FindClass(env, "java/awt/AWTException"),
  85                      [s UTF8String]);
  86 }
  87 
  88 /*
  89  * Class:     sun_lwawt_macosx_CRobot
  90  * Method:    initRobot
  91  * Signature: (V)V
  92  */
  93 JNIEXPORT void JNICALL
  94 Java_sun_lwawt_macosx_CRobot_initRobot
  95 (JNIEnv *env, jobject peer)
  96 {
  97     // Set things up to let our app act like a synthetic keyboard and mouse.
  98     // Always set all states, in case Apple ever changes default behaviors.
  99     static int setupDone = 0;
 100     if (!setupDone) {
 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     keyCodeMapping = [CRobotKeyCodeMapping sharedInstance];
 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,
 145  jint displayID, jint mouseLastX, jint mouseLastY, jint buttonsState,
 146  jboolean isButtonsDownState, jboolean isMouseMove)
 147 {
 148     JNF_COCOA_ENTER(env);
 149 
 150     // This is the native method called when Robot mouse events occur.
 151     // The CRobot tracks the mouse position, and which button was
 152     // pressed. The peer also tracks the mouse button desired state,
 153     // the appropriate key modifier state, and whether the mouse action
 154     // is simply a mouse move with no mouse button state changes.
 155 
 156     // volatile, otherwise it warns that it might be clobbered by 'longjmp'
 157     volatile CGPoint point;
 158 
 159     point.x = mouseLastX;
 160     point.y = mouseLastY;
 161 
 162     __block CGMouseButton button = kCGMouseButtonLeft;
 163     __block CGEventType type = kCGEventMouseMoved;
 164 
 165     void (^HandleRobotButton)(CGMouseButton, CGEventType, CGEventType, CGEventType) =
 166         ^(CGMouseButton cgButton, CGEventType cgButtonUp, CGEventType cgButtonDown,
 167           CGEventType cgButtonDragged) {
 168 
 169             button = cgButton;
 170             type = cgButtonUp;
 171 
 172             if (isButtonsDownState) {
 173                 if (isMouseMove) {
 174                     type = cgButtonDragged;
 175                 } else {
 176                     type = cgButtonDown;
 177                 }
 178             }
 179         };
 180 
 181     // Left
 182     if (buttonsState & java_awt_event_InputEvent_BUTTON1_MASK ||
 183         buttonsState & java_awt_event_InputEvent_BUTTON1_DOWN_MASK ) {
 184 
 185         HandleRobotButton(kCGMouseButtonLeft, kCGEventLeftMouseUp,
 186                           kCGEventLeftMouseDown, kCGEventLeftMouseDragged);
 187     }
 188 
 189     // Other
 190     if (buttonsState & java_awt_event_InputEvent_BUTTON2_MASK ||
 191         buttonsState & java_awt_event_InputEvent_BUTTON2_DOWN_MASK ) {
 192 
 193         HandleRobotButton(kCGMouseButtonCenter, kCGEventOtherMouseUp,
 194                           kCGEventOtherMouseDown, kCGEventOtherMouseDragged);
 195     }
 196 
 197     // Right
 198     if (buttonsState & java_awt_event_InputEvent_BUTTON3_MASK ||
 199         buttonsState & java_awt_event_InputEvent_BUTTON3_DOWN_MASK ) {
 200 
 201         HandleRobotButton(kCGMouseButtonRight, kCGEventRightMouseUp,
 202                           kCGEventRightMouseDown, kCGEventRightMouseDragged);
 203     }
 204 
 205     // Extra
 206     if (gNumberOfButtons > 3) {
 207         int extraButton;
 208         for (extraButton = 3; extraButton < gNumberOfButtons; ++extraButton) {
 209             if ((buttonsState & gButtonDownMasks[extraButton])) {
 210                 HandleRobotButton(extraButton, kCGEventOtherMouseUp,
 211                             kCGEventOtherMouseDown, kCGEventOtherMouseDragged);
 212             }
 213         }
 214     }
 215 
 216     int clickCount = 0;
 217     int eventNumber = gsEventNumber;
 218 
 219     if (isMouseMove) {
 220         // any mouse movement resets click count
 221         gsLastClickTime = 0;
 222     } else {
 223         clickCount = GetClickCount(isButtonsDownState);
 224 
 225         if (isButtonsDownState) {
 226             gsButtonEventNumber[button] = gsEventNumber++;
 227         }
 228         eventNumber = gsButtonEventNumber[button];
 229     }
 230 
 231     PostMouseEvent(point, button, type, clickCount, eventNumber);
 232 
 233     JNF_COCOA_EXIT(env);
 234 }
 235 
 236 /*
 237  * Class:     sun_lwawt_macosx_CRobot
 238  * Method:    mouseWheel
 239  * Signature: (I)V
 240  */
 241 JNIEXPORT void JNICALL
 242 Java_sun_lwawt_macosx_CRobot_mouseWheel
 243 (JNIEnv *env, jobject peer, jint wheelAmt)
 244 {
 245     CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
 246                                             kCGScrollEventUnitLine,
 247                                             k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
 248 
 249     if (event != NULL) {
 250         CGEventPost(kCGSessionEventTap, event);
 251         CFRelease(event);
 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     CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
 265 
 266     CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
 267     if (event != NULL) {
 268         CGEventPost(kCGSessionEventTap, event);
 269         CFRelease(event);
 270     }
 271 }
 272 
 273 /*
 274  * Class:     sun_lwawt_macosx_CRobot
 275  * Method:    nativeGetScreenPixels
 276  * Signature: (IIIII[I)V
 277  */
 278 JNIEXPORT void JNICALL
 279 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
 280 (JNIEnv *env, jobject peer,
 281  jint x, jint y, jint width, jint height, jintArray pixels)
 282 {
 283     JNF_COCOA_ENTER(env);
 284 
 285     jint picX = x;
 286     jint picY = y;
 287     jint picWidth = width;
 288     jint picHeight = height;
 289 
 290     CGRect screenRect = CGRectMake(picX, picY, picWidth, picHeight);
 291     CGImageRef screenPixelsImage = CGWindowListCreateImage(screenRect,
 292                                         kCGWindowListOptionOnScreenOnly,
 293                                         kCGNullWindowID, kCGWindowImageDefault);
 294 
 295     if (screenPixelsImage == NULL) {
 296         return;
 297     }
 298 
 299     // get a pointer to the Java int array
 300     void *jPixelData = (*env)->GetPrimitiveArrayCritical(env, pixels, 0);
 301     CHECK_NULL(jPixelData);
 302 
 303     // create a graphics context around the Java int array
 304     CGColorSpaceRef picColorSpace = CGColorSpaceCreateWithName(
 305                                             kCGColorSpaceGenericRGB);
 306     CGContextRef jPicContextRef = CGBitmapContextCreate(
 307                                             jPixelData,
 308                                             picWidth, picHeight,
 309                                             8, picWidth * sizeof(jint),
 310                                             picColorSpace,
 311                                             kCGBitmapByteOrder32Host |
 312                                             kCGImageAlphaPremultipliedFirst);
 313 
 314     CGColorSpaceRelease(picColorSpace);
 315 
 316     // flip, scale, and color correct the screen image into the Java pixels
 317     CGRect bounds = { { 0, 0 }, { picWidth, picHeight } };
 318     CGContextDrawImage(jPicContextRef, bounds, screenPixelsImage);
 319     CGContextFlush(jPicContextRef);
 320 
 321     // cleanup
 322     CGContextRelease(jPicContextRef);
 323     CGImageRelease(screenPixelsImage);
 324 
 325     // release the Java int array back up to the JVM
 326     (*env)->ReleasePrimitiveArrayCritical(env, pixels, jPixelData, 0);
 327 
 328     JNF_COCOA_EXIT(env);
 329 }
 330 
 331 /****************************************************
 332  * Helper methods
 333  ****************************************************/
 334 
 335 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
 336                            CGEventType type, int clickCount, int eventNumber)
 337 {
 338     CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, type, point, button);
 339     if (mouseEvent != NULL) {
 340         CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
 341         CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
 342         CGEventPost(kCGSessionEventTap, mouseEvent);
 343         CFRelease(mouseEvent);
 344     }
 345 }
 346 
 347 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode)
 348 {
 349     if (keyCodeMapping) {
 350         return [keyCodeMapping getOSXKeyCodeForJavaKey:javaKeyCode];
 351     }
 352     return OSX_Undefined;
 353 }
 354 
 355 static int GetClickCount(BOOL isDown) {
 356     NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate];
 357     NSTimeInterval clickInterval = now - gsLastClickTime;
 358     BOOL isWithinTreshold = clickInterval < [NSEvent doubleClickInterval];
 359 
 360     if (isDown) {
 361         if (isWithinTreshold) {
 362             gsClickCount++;
 363         } else {
 364             gsClickCount = 1;
 365         }
 366 
 367         gsLastClickTime = now;
 368     } else {
 369         // In OS X, a mouse up has the click count of the last mouse down
 370         // if an interval between up and down is within the double click
 371         // threshold, and 0 otherwise.
 372         if (!isWithinTreshold) {
 373             gsClickCount = 0;
 374         }
 375     }
 376 
 377     return gsClickCount;
 378 }