< prev index next >

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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -21,11 +21,10 @@
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
-
 #import "jni_util.h"
 
 #import <JavaNativeFoundation/JavaNativeFoundation.h>
 #import <ApplicationServices/ApplicationServices.h>
 

@@ -33,10 +32,11 @@
 #import "LWCToolkit.h"
 #import "sun_lwawt_macosx_CRobot.h"
 #import "java_awt_event_InputEvent.h"
 #import "java_awt_event_KeyEvent.h"
 #import "sizecalc.h"
+#import "ThreadUtilities.h"
 
 // Starting number for event numbers generated by Robot.
 // Apple docs don't mention at all what are the requirements
 // for these numbers. It seems that they must be higher
 // than event numbers from real events, which start at some

@@ -95,10 +95,11 @@
 {
     // Set things up to let our app act like a synthetic keyboard and mouse.
     // Always set all states, in case Apple ever changes default behaviors.
     static int setupDone = 0;
     if (!setupDone) {
+        [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
         int i;
         jint* tmp;
         jboolean copy = JNI_FALSE;
 
         setupDone = 1;

@@ -127,10 +128,11 @@
         }
 
         for (i = 0; i < gNumberOfButtons; ++i) {
             gsButtonEventNumber[i] = ROBOT_EVENT_NUMBER_START;
         }
+        }];
     }
 }
 
 /*
  * Class:     sun_lwawt_macosx_CRobot

@@ -237,18 +239,19 @@
  */
 JNIEXPORT void JNICALL
 Java_sun_lwawt_macosx_CRobot_mouseWheel
 (JNIEnv *env, jobject peer, jint wheelAmt)
 {
+    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
     CGEventRef event = CGEventCreateScrollWheelEvent(NULL,
                                             kCGScrollEventUnitLine,
                                             k_JAVA_ROBOT_WHEEL_COUNT, wheelAmt);
-
     if (event != NULL) {
         CGEventPost(kCGSessionEventTap, event);
         CFRelease(event);
     }
+    }];
 }
 
 /*
  * Class:     sun_lwawt_macosx_CRobot
  * Method:    keyEvent

@@ -256,17 +259,18 @@
  */
 JNIEXPORT void JNICALL
 Java_sun_lwawt_macosx_CRobot_keyEvent
 (JNIEnv *env, jobject peer, jint javaKeyCode, jboolean keyPressed)
 {
+    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
     CGKeyCode keyCode = GetCGKeyCode(javaKeyCode);
-
     CGEventRef event = CGEventCreateKeyboardEvent(NULL, keyCode, keyPressed);
     if (event != NULL) {
         CGEventPost(kCGSessionEventTap, event);
         CFRelease(event);
     }
+    }];
 }
 
 /*
  * Class:     sun_lwawt_macosx_CRobot
  * Method:    nativeGetScreenPixels

@@ -331,17 +335,19 @@
  ****************************************************/
 
 static void PostMouseEvent(const CGPoint point, CGMouseButton button,
                            CGEventType type, int clickCount, int eventNumber)
 {
+    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
     CGEventRef mouseEvent = CGEventCreateMouseEvent(NULL, type, point, button);
     if (mouseEvent != NULL) {
         CGEventSetIntegerValueField(mouseEvent, kCGMouseEventClickState, clickCount);
         CGEventSetIntegerValueField(mouseEvent, kCGMouseEventNumber, eventNumber);
         CGEventPost(kCGSessionEventTap, mouseEvent);
         CFRelease(mouseEvent);
     }
+    }];
 }
 
 static inline CGKeyCode GetCGKeyCode(jint javaKeyCode)
 {
     CRobotKeyCodeMapping *keyCodeMapping = [CRobotKeyCodeMapping sharedInstance];
< prev index next >