1 /*
2 * Copyright (c) 2011, 2019, 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
53 int gNumberOfButtons;
54 jint* gButtonDownMasks;
55
56 // Indicates that the app has been started with -XstartOnFirstThread
57 // (directly or via WebStart settings), and AWT should not run its
58 // own event loop in this mode. Even if a loop isn't running yet,
59 // we expect an embedder (e.g. SWT) to start it some time later.
60 static BOOL forceEmbeddedMode = NO;
61
62 // Indicates if awt toolkit is embedded into another UI toolkit
63 static BOOL isEmbedded = NO;
64
65 // This is the data necessary to have JNI_OnLoad wait for AppKit to start.
66 static BOOL sAppKitStarted = NO;
67 static pthread_mutex_t sAppKitStarted_mutex = PTHREAD_MUTEX_INITIALIZER;
68 static pthread_cond_t sAppKitStarted_cv = PTHREAD_COND_INITIALIZER;
69
70 @implementation AWTToolkit
71
72 static long eventCount;
73
74 + (long) getEventCount{
75 return eventCount;
76 }
77
78 + (void) eventCountPlusPlus{
79 eventCount++;
80 }
81
82 + (jint) scrollStateWithEvent: (NSEvent*) event {
83
84 if ([event type] != NSScrollWheel) {
85 return 0;
86 }
87
88 if ([event phase]) {
89 // process a phase of manual scrolling
90 switch ([event phase]) {
91 case NSEventPhaseBegan: return SCROLL_PHASE_BEGAN;
92 case NSEventPhaseCancelled: return SCROLL_PHASE_ENDED;
93 case NSEventPhaseEnded: return SCROLL_PHASE_ENDED;
94 default: return SCROLL_PHASE_CONTINUED;
95 }
96 }
97
98 if ([event momentumPhase]) {
99 // process a phase of automatic scrolling
403 /*
404 * Class: sun_lwawt_macosx_LWCToolkit
405 * Method: nativeSyncQueue
406 * Signature: (J)Z
407 */
408 JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_LWCToolkit_nativeSyncQueue
409 (JNIEnv *env, jobject self, jlong timeout)
410 {
411 long currentEventNum = [AWTToolkit getEventCount];
412
413 NSApplication* sharedApp = [NSApplication sharedApplication];
414 if ([sharedApp isKindOfClass:[NSApplicationAWT class]]) {
415 NSApplicationAWT* theApp = (NSApplicationAWT*)sharedApp;
416 // We use two different API to post events to the application,
417 // - [NSApplication postEvent]
418 // - CGEventPost(), see CRobot.m
419 // It was found that if we post an event via CGEventPost in robot and
420 // immediately after this we will post the second event via
421 // [NSApp postEvent] then sometimes the second event will be handled
422 // first. The opposite isn't proved, but we use both here to be safer.
423 [theApp postDummyEvent:false];
424 [theApp waitForDummyEvent:timeout / 2.0];
425 [theApp postDummyEvent:true];
426 [theApp waitForDummyEvent:timeout / 2.0];
427
428 } else {
429 // could happen if we are embedded inside SWT application,
430 // in this case just spin a single empty block through
431 // the event loop to give it a chance to process pending events
432 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){}];
433 }
434
435 if (([AWTToolkit getEventCount] - currentEventNum) != 0) {
436 return JNI_TRUE;
437 }
438
439 return JNI_FALSE;
440 }
441
442 /*
443 * Class: sun_lwawt_macosx_LWCToolkit
444 * Method: flushNativeSelectors
445 * Signature: ()J
446 */
|
1 /*
2 * Copyright (c) 2011, 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
53 int gNumberOfButtons;
54 jint* gButtonDownMasks;
55
56 // Indicates that the app has been started with -XstartOnFirstThread
57 // (directly or via WebStart settings), and AWT should not run its
58 // own event loop in this mode. Even if a loop isn't running yet,
59 // we expect an embedder (e.g. SWT) to start it some time later.
60 static BOOL forceEmbeddedMode = NO;
61
62 // Indicates if awt toolkit is embedded into another UI toolkit
63 static BOOL isEmbedded = NO;
64
65 // This is the data necessary to have JNI_OnLoad wait for AppKit to start.
66 static BOOL sAppKitStarted = NO;
67 static pthread_mutex_t sAppKitStarted_mutex = PTHREAD_MUTEX_INITIALIZER;
68 static pthread_cond_t sAppKitStarted_cv = PTHREAD_COND_INITIALIZER;
69
70 @implementation AWTToolkit
71
72 static long eventCount;
73 static BOOL inDoDragDropLoop;
74
75 + (BOOL) inDoDragDropLoop {
76 @synchronized(self) {
77 return inDoDragDropLoop;
78 }
79 }
80
81 + (void) setInDoDragDropLoop:(BOOL)val {
82 @synchronized(self) {
83 inDoDragDropLoop = val;
84 }
85 }
86
87 + (long) getEventCount{
88 @synchronized(self) {
89 return eventCount;
90 }
91 }
92
93 + (void) eventCountPlusPlus{
94 @synchronized(self) {
95 eventCount++;
96 }
97 }
98
99 + (jint) scrollStateWithEvent: (NSEvent*) event {
100
101 if ([event type] != NSScrollWheel) {
102 return 0;
103 }
104
105 if ([event phase]) {
106 // process a phase of manual scrolling
107 switch ([event phase]) {
108 case NSEventPhaseBegan: return SCROLL_PHASE_BEGAN;
109 case NSEventPhaseCancelled: return SCROLL_PHASE_ENDED;
110 case NSEventPhaseEnded: return SCROLL_PHASE_ENDED;
111 default: return SCROLL_PHASE_CONTINUED;
112 }
113 }
114
115 if ([event momentumPhase]) {
116 // process a phase of automatic scrolling
420 /*
421 * Class: sun_lwawt_macosx_LWCToolkit
422 * Method: nativeSyncQueue
423 * Signature: (J)Z
424 */
425 JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_LWCToolkit_nativeSyncQueue
426 (JNIEnv *env, jobject self, jlong timeout)
427 {
428 long currentEventNum = [AWTToolkit getEventCount];
429
430 NSApplication* sharedApp = [NSApplication sharedApplication];
431 if ([sharedApp isKindOfClass:[NSApplicationAWT class]]) {
432 NSApplicationAWT* theApp = (NSApplicationAWT*)sharedApp;
433 // We use two different API to post events to the application,
434 // - [NSApplication postEvent]
435 // - CGEventPost(), see CRobot.m
436 // It was found that if we post an event via CGEventPost in robot and
437 // immediately after this we will post the second event via
438 // [NSApp postEvent] then sometimes the second event will be handled
439 // first. The opposite isn't proved, but we use both here to be safer.
440
441 // If the native drag is in progress, skip native sync.
442 if (!AWTToolkit.inDoDragDropLoop) {
443 [theApp postDummyEvent:false];
444 [theApp waitForDummyEvent:timeout / 2.0];
445 }
446 if (!AWTToolkit.inDoDragDropLoop) {
447 [theApp postDummyEvent:true];
448 [theApp waitForDummyEvent:timeout / 2.0];
449 }
450
451 } else {
452 // could happen if we are embedded inside SWT application,
453 // in this case just spin a single empty block through
454 // the event loop to give it a chance to process pending events
455 [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){}];
456 }
457
458 if (([AWTToolkit getEventCount] - currentEventNum) != 0) {
459 return JNI_TRUE;
460 }
461
462 return JNI_FALSE;
463 }
464
465 /*
466 * Class: sun_lwawt_macosx_LWCToolkit
467 * Method: flushNativeSelectors
468 * Signature: ()J
469 */
|