< prev index next >

src/java.desktop/macosx/native/libawt_lwawt/awt/CRobot.m

Print this page
rev 54096 : 8259651: [macOS] Replace JNF_COCOA_ENTER/EXIT macros
rev 54098 : 8260616: Removing remaining JNF dependencies in the java.desktop module
8259729: Missed JNFInstanceOf -> IsInstanceOf conversion


   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 


 151                 return;
 152             }
 153 
 154             for (i = 0; i < gNumberOfButtons; ++i) {
 155                 gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
 156             }
 157         }];
 158     }
 159 }
 160 
 161 /*
 162  * Class:     sun_lwawt_macosx_CRobot
 163  * Method:    mouseEvent
 164  * Signature: (IIIIZZ)V
 165  */
 166 JNIEXPORT void JNICALL
 167 Java_sun_lwawt_macosx_CRobot_mouseEvent
 168 (JNIEnv *env, jobject peer, jint mouseLastX, jint mouseLastY, jint buttonsState,
 169  jboolean isButtonsDownState, jboolean isMouseMove)
 170 {
 171     JNF_COCOA_ENTER(env);
 172     autoDelay(isMouseMove);
 173 
 174     // This is the native method called when Robot mouse events occur.
 175     // The CRobot tracks the mouse position, and which button was
 176     // pressed. The peer also tracks the mouse button desired state,
 177     // the appropriate key modifier state, and whether the mouse action
 178     // is simply a mouse move with no mouse button state changes.
 179 
 180     // volatile, otherwise it warns that it might be clobbered by 'longjmp'
 181     volatile CGPoint point;
 182 
 183     point.x = mouseLastX;
 184     point.y = mouseLastY;
 185 
 186     __block CGMouseButton button = kCGMouseButtonLeft;
 187     __block CGEventType type = kCGEventMouseMoved;
 188 
 189     void (^HandleRobotButton)(CGMouseButton, CGEventType, CGEventType, CGEventType) =
 190         ^(CGMouseButton cgButton, CGEventType cgButtonUp, CGEventType cgButtonDown,
 191           CGEventType cgButtonDragged) {


 237         }
 238     }
 239 
 240     int clickCount = 0;
 241     int eventNumber = gsEventNumber;
 242 
 243     if (isMouseMove) {
 244         // any mouse movement resets click count
 245         gsLastClickTime = 0;
 246     } else {
 247         clickCount = GetClickCount(isButtonsDownState);
 248 
 249         if (isButtonsDownState) {
 250             gsButtonEventNumber[button] = gsEventNumber++;
 251         }
 252         eventNumber = gsButtonEventNumber[button];
 253     }
 254 
 255     PostMouseEvent(point, button, type, clickCount, eventNumber);
 256 
 257     JNF_COCOA_EXIT(env);
 258 }
 259 
 260 /*
 261  * Class:     sun_lwawt_macosx_CRobot
 262  * Method:    mouseWheel
 263  * Signature: (I)V
 264  */
 265 JNIEXPORT void JNICALL
 266 Java_sun_lwawt_macosx_CRobot_mouseWheel
 267 (JNIEnv *env, jobject peer, jint wheelAmt)
 268 {
 269     autoDelay(NO);
 270     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 271         CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
 272         CGEventRef event = CGEventCreateScrollWheelEvent(source,
 273                                                 kCGScrollEventUnitLine,
 274                                                 k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
 275         if (event != NULL) {
 276             CGEventPost(kCGHIDEventTap, event);
 277             CFRelease(event);


 299         if (event != NULL) {
 300             CGEventPost(kCGHIDEventTap, event);
 301             CFRelease(event);
 302         }
 303         if (source != NULL) {
 304             CFRelease(source);
 305         }
 306     }];
 307 }
 308 
 309 /*
 310  * Class:     sun_lwawt_macosx_CRobot
 311  * Method:    nativeGetScreenPixels
 312  * Signature: (IIIII[I)V
 313  */
 314 JNIEXPORT void JNICALL
 315 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
 316 (JNIEnv *env, jobject peer,
 317  jint x, jint y, jint width, jint height, jdouble scale, jintArray pixels)
 318 {
 319     JNF_COCOA_ENTER(env);
 320 
 321     jint picX = x;
 322     jint picY = y;
 323     jint picWidth = width;
 324     jint picHeight = height;
 325 
 326     CGRect screenRect = CGRectMake(picX / scale, picY / scale,
 327                                 picWidth / scale, picHeight / scale);
 328     CGImageRef screenPixelsImage = CGWindowListCreateImage(screenRect,
 329                                         kCGWindowListOptionOnScreenOnly,
 330                                         kCGNullWindowID, kCGWindowImageBestResolution);
 331 
 332     if (screenPixelsImage == NULL) {
 333         return;
 334     }
 335 
 336     // get a pointer to the Java int array
 337     void *jPixelData = (*env)->GetPrimitiveArrayCritical(env, pixels, 0);
 338     CHECK_NULL(jPixelData);
 339 


 345                                             picWidth, picHeight,
 346                                             8, picWidth * sizeof(jint),
 347                                             picColorSpace,
 348                                             kCGBitmapByteOrder32Host |
 349                                             kCGImageAlphaPremultipliedFirst);
 350 
 351     CGColorSpaceRelease(picColorSpace);
 352 
 353     // flip, scale, and color correct the screen image into the Java pixels
 354     CGRect bounds = { { 0, 0 }, { picWidth, picHeight } };
 355     CGContextDrawImage(jPicContextRef, bounds, screenPixelsImage);
 356     CGContextFlush(jPicContextRef);
 357 
 358     // cleanup
 359     CGContextRelease(jPicContextRef);
 360     CGImageRelease(screenPixelsImage);
 361 
 362     // release the Java int array back up to the JVM
 363     (*env)->ReleasePrimitiveArrayCritical(env, pixels, jPixelData, 0);
 364 
 365     JNF_COCOA_EXIT(env);
 366 }
 367 
 368 /****************************************************
 369  * Helper methods
 370  ****************************************************/
 371 
 372 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
 373                            CGEventType type, int clickCount, int eventNumber)
 374 {
 375     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 376         CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
 377         CGEventRef mouseEvent = CGEventCreateMouseEvent(source, type, point, button);
 378         if (mouseEvent != NULL) {
 379             CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
 380             CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
 381             CGEventPost(kCGHIDEventTap, mouseEvent);
 382             CFRelease(mouseEvent);
 383         }
 384         if (source != NULL) {
 385             CFRelease(source);




   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 "JNIUtilities.h"
  27 

  28 #import <ApplicationServices/ApplicationServices.h>
  29 
  30 #import "CRobotKeyCode.h"
  31 #import "LWCToolkit.h"
  32 #import "sun_lwawt_macosx_CRobot.h"
  33 #import "java_awt_event_InputEvent.h"
  34 #import "java_awt_event_KeyEvent.h"
  35 #import "sizecalc.h"
  36 #import "ThreadUtilities.h"
  37 
  38 // Starting number for event numbers generated by Robot.
  39 // Apple docs don't mention at all what are the requirements
  40 // for these numbers. It seems that they must be higher
  41 // than event numbers from real events, which start at some
  42 // value close to zero. There is no API for obtaining current
  43 // event number, so we have to start from some random number.
  44 // 32000 as starting value works for me, let's hope that it will
  45 // work for others as well.
  46 #define ROBOT_EVENT_NUMBER_START 32000
  47 


 150                 return;
 151             }
 152 
 153             for (i = 0; i < gNumberOfButtons; ++i) {
 154                 gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
 155             }
 156         }];
 157     }
 158 }
 159 
 160 /*
 161  * Class:     sun_lwawt_macosx_CRobot
 162  * Method:    mouseEvent
 163  * Signature: (IIIIZZ)V
 164  */
 165 JNIEXPORT void JNICALL
 166 Java_sun_lwawt_macosx_CRobot_mouseEvent
 167 (JNIEnv *env, jobject peer, jint mouseLastX, jint mouseLastY, jint buttonsState,
 168  jboolean isButtonsDownState, jboolean isMouseMove)
 169 {
 170     JNI_COCOA_ENTER(env);
 171     autoDelay(isMouseMove);
 172 
 173     // This is the native method called when Robot mouse events occur.
 174     // The CRobot tracks the mouse position, and which button was
 175     // pressed. The peer also tracks the mouse button desired state,
 176     // the appropriate key modifier state, and whether the mouse action
 177     // is simply a mouse move with no mouse button state changes.
 178 
 179     // volatile, otherwise it warns that it might be clobbered by 'longjmp'
 180     volatile CGPoint point;
 181 
 182     point.x = mouseLastX;
 183     point.y = mouseLastY;
 184 
 185     __block CGMouseButton button = kCGMouseButtonLeft;
 186     __block CGEventType type = kCGEventMouseMoved;
 187 
 188     void (^HandleRobotButton)(CGMouseButton, CGEventType, CGEventType, CGEventType) =
 189         ^(CGMouseButton cgButton, CGEventType cgButtonUp, CGEventType cgButtonDown,
 190           CGEventType cgButtonDragged) {


 236         }
 237     }
 238 
 239     int clickCount = 0;
 240     int eventNumber = gsEventNumber;
 241 
 242     if (isMouseMove) {
 243         // any mouse movement resets click count
 244         gsLastClickTime = 0;
 245     } else {
 246         clickCount = GetClickCount(isButtonsDownState);
 247 
 248         if (isButtonsDownState) {
 249             gsButtonEventNumber[button] = gsEventNumber++;
 250         }
 251         eventNumber = gsButtonEventNumber[button];
 252     }
 253 
 254     PostMouseEvent(point, button, type, clickCount, eventNumber);
 255 
 256     JNI_COCOA_EXIT(env);
 257 }
 258 
 259 /*
 260  * Class:     sun_lwawt_macosx_CRobot
 261  * Method:    mouseWheel
 262  * Signature: (I)V
 263  */
 264 JNIEXPORT void JNICALL
 265 Java_sun_lwawt_macosx_CRobot_mouseWheel
 266 (JNIEnv *env, jobject peer, jint wheelAmt)
 267 {
 268     autoDelay(NO);
 269     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 270         CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
 271         CGEventRef event = CGEventCreateScrollWheelEvent(source,
 272                                                 kCGScrollEventUnitLine,
 273                                                 k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
 274         if (event != NULL) {
 275             CGEventPost(kCGHIDEventTap, event);
 276             CFRelease(event);


 298         if (event != NULL) {
 299             CGEventPost(kCGHIDEventTap, event);
 300             CFRelease(event);
 301         }
 302         if (source != NULL) {
 303             CFRelease(source);
 304         }
 305     }];
 306 }
 307 
 308 /*
 309  * Class:     sun_lwawt_macosx_CRobot
 310  * Method:    nativeGetScreenPixels
 311  * Signature: (IIIII[I)V
 312  */
 313 JNIEXPORT void JNICALL
 314 Java_sun_lwawt_macosx_CRobot_nativeGetScreenPixels
 315 (JNIEnv *env, jobject peer,
 316  jint x, jint y, jint width, jint height, jdouble scale, jintArray pixels)
 317 {
 318     JNI_COCOA_ENTER(env);
 319 
 320     jint picX = x;
 321     jint picY = y;
 322     jint picWidth = width;
 323     jint picHeight = height;
 324 
 325     CGRect screenRect = CGRectMake(picX / scale, picY / scale,
 326                                 picWidth / scale, picHeight / scale);
 327     CGImageRef screenPixelsImage = CGWindowListCreateImage(screenRect,
 328                                         kCGWindowListOptionOnScreenOnly,
 329                                         kCGNullWindowID, kCGWindowImageBestResolution);
 330 
 331     if (screenPixelsImage == NULL) {
 332         return;
 333     }
 334 
 335     // get a pointer to the Java int array
 336     void *jPixelData = (*env)->GetPrimitiveArrayCritical(env, pixels, 0);
 337     CHECK_NULL(jPixelData);
 338 


 344                                             picWidth, picHeight,
 345                                             8, picWidth * sizeof(jint),
 346                                             picColorSpace,
 347                                             kCGBitmapByteOrder32Host |
 348                                             kCGImageAlphaPremultipliedFirst);
 349 
 350     CGColorSpaceRelease(picColorSpace);
 351 
 352     // flip, scale, and color correct the screen image into the Java pixels
 353     CGRect bounds = { { 0, 0 }, { picWidth, picHeight } };
 354     CGContextDrawImage(jPicContextRef, bounds, screenPixelsImage);
 355     CGContextFlush(jPicContextRef);
 356 
 357     // cleanup
 358     CGContextRelease(jPicContextRef);
 359     CGImageRelease(screenPixelsImage);
 360 
 361     // release the Java int array back up to the JVM
 362     (*env)->ReleasePrimitiveArrayCritical(env, pixels, jPixelData, 0);
 363 
 364     JNI_COCOA_EXIT(env);
 365 }
 366 
 367 /****************************************************
 368  * Helper methods
 369  ****************************************************/
 370 
 371 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
 372                            CGEventType type, int clickCount, int eventNumber)
 373 {
 374     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 375         CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
 376         CGEventRef mouseEvent = CGEventCreateMouseEvent(source, type, point, button);
 377         if (mouseEvent != NULL) {
 378             CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
 379             CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
 380             CGEventPost(kCGHIDEventTap, mouseEvent);
 381             CFRelease(mouseEvent);
 382         }
 383         if (source != NULL) {
 384             CFRelease(source);


< prev index next >