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 
  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         int i;
 101         jint* tmp;
 102         jboolean copy = JNI_FALSE;
 103 
 104         setupDone = 1;
 105         // Don't block local events after posting ours
 106         CGSetLocalEventsSuppressionInterval(0.0);
 107 
 108         // Let our event's modifier key state blend with local hardware events
 109         CGEnableEventStateCombining(TRUE);
 110 
 111         // Don't let our events block local hardware events
 112         CGSetLocalEventsFilterDuringSupressionState(
 113                                     kCGEventFilterMaskPermitAllEvents,
 114                                     kCGEventSupressionStateSupressionInterval);
 115         CGSetLocalEventsFilterDuringSupressionState(
 116                                     kCGEventFilterMaskPermitAllEvents,
 117                                     kCGEventSupressionStateRemoteMouseDrag);
 118 
 119         gsClickCount = 0;
 120         gsLastClickTime = 0;
 121         gsEventNumber = ROBOT_EVENT_NUMBER_START;
 122 
 123         gsButtonEventNumber = (int*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(int), gNumberOfButtons);
 124         if (gsButtonEventNumber == NULL) {
 125             JNU_ThrowOutOfMemoryError(env, NULL);
 126             return;
 127         }
 128 
 129         for (i = 0; i < gNumberOfButtons; ++i) {
 130             gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
 131         }
 132     }
 133 }
 134 
 135 /*
 136  * Class:     sun_lwawt_macosx_CRobot
 137  * Method:    mouseEvent
 138  * Signature: (IIIIZZ)V
 139  */
 140 JNIEXPORT void JNICALL
 141 Java_sun_lwawt_macosx_CRobot_mouseEvent
 142 (JNIEnv *env, jobject peer,
 143  jint displayID, jint mouseLastX, jint mouseLastY, jint buttonsState,
 144  jboolean isButtonsDownState, jboolean isMouseMove)
 145 {
 146     JNF_COCOA_ENTER(env);
 147 
 148     // This is the native method called when Robot mouse events occur.
 149     // The CRobot tracks the mouse position, and which button was
 150     // pressed. The peer also tracks the mouse button desired state,
 151     // the appropriate key modifier state, and whether the mouse action
 152     // is simply a mouse move with no mouse button state changes.
 153 
 154     // volatile, otherwise it warns that it might be clobbered by 'longjmp'
 155     volatile CGPoint point;
 156 
 157     point.x = mouseLastX;
 158     point.y = mouseLastY;
 159 
 160     __block CGMouseButton button = kCGMouseButtonLeft;
 161     __block CGEventType type = kCGEventMouseMoved;
 162 
 163     void (^HandleRobotButton)(CGMouseButton, CGEventType, CGEventType, CGEventType) =
 164         ^(CGMouseButton cgButton, CGEventType cgButtonUp, CGEventType cgButtonDown,
 165           CGEventType cgButtonDragged) {
 166 
 167             button = cgButton;
 168             type = cgButtonUp;
 169 
 170             if (isButtonsDownState) {
 171                 if (isMouseMove) {
 172                     type = cgButtonDragged;
 173                 } else {
 174                     type = cgButtonDown;
 175                 }
 176             }
 177         };
 178 
 179     // Left
 180     if (buttonsState & java_awt_event_InputEvent_BUTTON1_MASK ||
 181         buttonsState & java_awt_event_InputEvent_BUTTON1_DOWN_MASK ) {
 182 
 183         HandleRobotButton(kCGMouseButtonLeft, kCGEventLeftMouseUp,
 184                           kCGEventLeftMouseDown, kCGEventLeftMouseDragged);
 185     }
 186 
 187     // Other
 188     if (buttonsState & java_awt_event_InputEvent_BUTTON2_MASK ||
 189         buttonsState & java_awt_event_InputEvent_BUTTON2_DOWN_MASK ) {
 190 
 191         HandleRobotButton(kCGMouseButtonCenter, kCGEventOtherMouseUp,
 192                           kCGEventOtherMouseDown, kCGEventOtherMouseDragged);
 193     }
 194 
 195     // Right
 196     if (buttonsState & java_awt_event_InputEvent_BUTTON3_MASK ||
 197         buttonsState & java_awt_event_InputEvent_BUTTON3_DOWN_MASK ) {
 198 
 199         HandleRobotButton(kCGMouseButtonRight, kCGEventRightMouseUp,
 200                           kCGEventRightMouseDown, kCGEventRightMouseDragged);
 201     }
 202 
 203     // Extra
 204     if (gNumberOfButtons > 3) {
 205         int extraButton;
 206         for (extraButton = 3; extraButton < gNumberOfButtons; ++extraButton) {
 207             if ((buttonsState & gButtonDownMasks[extraButton])) {
 208                 HandleRobotButton(extraButton, kCGEventOtherMouseUp,
 209                             kCGEventOtherMouseDown, kCGEventOtherMouseDragged);
 210             }
 211         }
 212     }
 213 
 214     int clickCount = 0;
 215     int eventNumber = gsEventNumber;
 216 
 217     if (isMouseMove) {
 218         // any mouse movement resets click count
 219         gsLastClickTime = 0;
 220     } else {
 221         clickCount = GetClickCount(isButtonsDownState);
 222 
 223         if (isButtonsDownState) {
 224             gsButtonEventNumber[button] = gsEventNumber++;
 225         }
 226         eventNumber = gsButtonEventNumber[button];
 227     }
 228 
 229     PostMouseEvent(point, button, type, clickCount, eventNumber);
 230 
 231     JNF_COCOA_EXIT(env);
 232 }
 233 
 234 /*
 235  * Class:     sun_lwawt_macosx_CRobot
 236  * Method:    mouseWheel
 237  * Signature: (I)V
 238  */
 239 JNIEXPORT void JNICALL
 240 Java_sun_lwawt_macosx_CRobot_mouseWheel
 241 (JNIEnv *env, jobject peer, jint wheelAmt)
 242 {
 243     CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
 244                                             kCGScrollEventUnitLine,
 245                                             k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
 246 
 247     if (event != NULL) {
 248         CGEventPost(kCGSessionEventTap, event);
 249         CFRelease(event);
 250     }
 251 }
 252 
 253 /*
 254  * Class:     sun_lwawt_macosx_CRobot
 255  * Method:    keyEvent
 256  * Signature: (IZ)V
 257  */
 258 JNIEXPORT void JNICALL
 259 Java_sun_lwawt_macosx_CRobot_keyEvent
 260 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 261 {
 262     /*
 263      * Well, using CGEventCreateKeyboardEvent/CGEventPost would have been
 264      * a better solution, however, it gives me all kinds of trouble and I have
 265      * no idea how to solve them without inserting delays between simulated
 266      * events. So, I've ended up disabling it and opted for another approach
 267      * that uses Accessibility API instead.
 268      */
 269 
 270     CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
 271 
 272     /*
 273      * JDK-8155740: AXUIElementPostKeyboardEvent posts 0 key code for all
 274      * the modifier keys with key codes (16, 17,18, 20, 157) and also for
 275      * newly added modifier key VK_ALT_GRAPH. But it posts correct key code
 276      * for all the other keys. On the other hand CGEventCreateKeyboardEvent
 277      * posts correct key code for all the keys except numeric keys (0 to 9).
 278      * CGEventCreateKeyboardEvent posts wrong key codes for the number keys
 279      * 0 to 9. Instead of posting number key codes it posts Numpad key codes
 280      * for the corresponding number key. For example Numpad0 key is posted for
 281      * number 0 and simillarly for remaining num keys. Based on this behaviour
 282      * AXUIElementPostKeyboardEvent is used to post only numeric key events
 283      * and CGEventCreateKeyboardEvent used for posting all other key events.
 284      */
 285 
 286     if ((javaKeyCode >= java_awt_event_KeyEvent_VK_0) &&
 287         (javaKeyCode <= java_awt_event_KeyEvent_VK_9))
 288     {
 289         AXUIElementRef elem = AXUIElementCreateSystemWide();
 290         AXUIElementPostKeyboardEvent(elem, (CGCharCode)0, keyCode, keyPressed);
 291         CFRelease(elem);
 292     } else {


 293         CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
 294         if (event != NULL) {
 295             CGEventPost(kCGSessionEventTap, event);
 296             CFRelease(event);
 297         }
 298     }
 299 }
 300 
 301 /*
 302  * Class:     sun_lwawt_macosx_CRobot
 303  * Method:    nativeGetScreenPixels
 304  * Signature: (IIIII[I)V
 305  */
 306 JNIEXPORT void JNICALL
 307 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
 308 (JNIEnv *env, jobject peer,
 309  jint x, jint y, jint width, jint height, jintArray pixels)
 310 {
 311     JNF_COCOA_ENTER(env);
 312 
 313     jint picX = x;
 314     jint picY = y;
 315     jint picWidth = width;
 316     jint picHeight = height;
 317 
 318     CGRect screenRect = CGRectMake(picX, picY, picWidth, picHeight);
 319     CGImageRef screenPixelsImage = CGWindowListCreateImage(screenRect,
 320                                         kCGWindowListOptionOnScreenOnly,
 321                                         kCGNullWindowID, kCGWindowImageDefault);
 322 
 323     if (screenPixelsImage == NULL) {
 324         return;
 325     }
 326 
 327     // get a pointer to the Java int array
 328     void *jPixelData = (*env)->GetPrimitiveArrayCritical(env, pixels, 0);
 329     CHECK_NULL(jPixelData);
 330 
 331     // create a graphics context around the Java int array
 332     CGColorSpaceRef picColorSpace = CGColorSpaceCreateWithName(
 333                                             kCGColorSpaceGenericRGB);
 334     CGContextRef jPicContextRef = CGBitmapContextCreate(
 335                                             jPixelData,
 336                                             picWidth, picHeight,
 337                                             8, picWidth * sizeof(jint),
 338                                             picColorSpace,
 339                                             kCGBitmapByteOrder32Host |
 340                                             kCGImageAlphaPremultipliedFirst);
 341 
 342     CGColorSpaceRelease(picColorSpace);
 343 
 344     // flip, scale, and color correct the screen image into the Java pixels
 345     CGRect bounds = { { 0, 0 }, { picWidth, picHeight } };
 346     CGContextDrawImage(jPicContextRef, bounds, screenPixelsImage);
 347     CGContextFlush(jPicContextRef);
 348 
 349     // cleanup
 350     CGContextRelease(jPicContextRef);
 351     CGImageRelease(screenPixelsImage);
 352 
 353     // release the Java int array back up to the JVM
 354     (*env)->ReleasePrimitiveArrayCritical(env, pixels, jPixelData, 0);
 355 
 356     JNF_COCOA_EXIT(env);
 357 }
 358 
 359 /****************************************************
 360  * Helper methods
 361  ****************************************************/
 362 
 363 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
 364                            CGEventType type, int clickCount, int eventNumber)
 365 {
 366     CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, type, point, button);
 367     if (mouseEvent != NULL) {
 368         CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
 369         CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
 370         CGEventPost(kCGSessionEventTap, mouseEvent);
 371         CFRelease(mouseEvent);
 372     }
 373 }
 374 
 375 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode)
 376 {
 377     CRobotKeyCodeMapping *keyCodeMapping = [CRobotKeyCodeMapping sharedInstance];
 378     return [keyCodeMapping getOSXKeyCodeForJavaKey:javaKeyCode];
 379 }
 380 
 381 static int GetClickCount(BOOL isDown) {
 382     NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate];
 383     NSTimeInterval clickInterval = now - gsLastClickTime;
 384     BOOL isWithinTreshold = clickInterval < [NSEvent doubleClickInterval];
 385 
 386     if (isDown) {
 387         if (isWithinTreshold) {
 388             gsClickCount++;
 389         } else {
 390             gsClickCount = 1;
 391         }
 392 
 393         gsLastClickTime = now;
 394     } else {
 395         // In OS X, a mouse up has the click count of the last mouse down
 396         // if an interval between up and down is within the double click
 397         // threshold, and 0 otherwise.
 398         if (!isWithinTreshold) {
 399             gsClickCount = 0;
 400         }
 401     }
 402 
 403     return gsClickCount;
 404 }
--- EOF ---