src/macosx/native/sun/awt/LWCToolkit.m

Print this page




   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 <dlfcn.h>




  27 #import <JavaNativeFoundation/JavaNativeFoundation.h>

  28 
  29 #include "jni_util.h"
  30 #import "CMenuBar.h"
  31 #import "InitIDs.h"
  32 #import "LWCToolkit.h"
  33 #import "ThreadUtilities.h"
  34 #import "AWT_debug.h"
  35 #import "CSystemColors.h"
  36 #import  "NSApplicationAWT.h"


  37 
  38 #import "sun_lwawt_macosx_LWCToolkit.h"
  39 
  40 #import "sizecalc.h"
  41 
  42 int gNumberOfButtons;
  43 jint* gButtonDownMasks;
  44 











  45 @implementation AWTToolkit
  46 
  47 static long eventCount;
  48 
  49 + (long) getEventCount{
  50     return eventCount;
  51 }
  52 
  53 + (void) eventCountPlusPlus{    
  54     eventCount++;
  55 }
  56 
  57 @end
  58 
  59 
  60 @interface AWTRunLoopObject : NSObject {
  61     BOOL _shouldEndRunLoop;
  62 }
  63 @end
  64 


  98     return self;
  99 }
 100 
 101 - (void)dealloc {
 102     JNIEnv *env = [ThreadUtilities getJNIEnv];
 103     if (self.runnable) {
 104         (*env)->DeleteGlobalRef(env, self.runnable);
 105     }
 106     [super dealloc];
 107 }
 108 
 109 - (void)perform {
 110     JNIEnv* env = [ThreadUtilities getJNIEnv];
 111     static JNF_CLASS_CACHE(sjc_Runnable, "java/lang/Runnable");
 112     static JNF_MEMBER_CACHE(jm_Runnable_run, sjc_Runnable, "run", "()V");
 113     JNFCallVoidMethod(env, self.runnable, jm_Runnable_run);
 114     [self release];
 115 }
 116 @end
 117 



































































































































































































































 118 /*
 119  * Class:     sun_lwawt_macosx_LWCToolkit
 120  * Method:    nativeSyncQueue
 121  * Signature: (J)Z
 122  */
 123 JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_LWCToolkit_nativeSyncQueue
 124 (JNIEnv *env, jobject self, jlong timeout)
 125 {
 126     int currentEventNum = [AWTToolkit getEventCount];
 127 
 128     NSApplication* sharedApp = [NSApplication sharedApplication];
 129     if ([sharedApp isKindOfClass:[NSApplicationAWT class]]) {
 130         NSApplicationAWT* theApp = (NSApplicationAWT*)sharedApp;
 131         [theApp postDummyEvent];
 132         [theApp waitForDummyEvent];
 133     } else {
 134         // could happen if we are embedded inside SWT application,
 135         // in this case just spin a single empty block through 
 136         // the event loop to give it a chance to process pending events
 137         [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){}];


 152 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_LWCToolkit_flushNativeSelectors
 153 (JNIEnv *env, jclass clz)
 154 {
 155 JNF_COCOA_ENTER(env);
 156         [ThreadUtilities performOnMainThreadWaiting:YES block:^(){}];
 157 JNF_COCOA_EXIT(env);
 158 }
 159 
 160 /*
 161  * Class:     sun_lwawt_macosx_LWCToolkit
 162  * Method:    beep
 163  * Signature: ()V
 164  */
 165 JNIEXPORT void JNICALL
 166 Java_sun_lwawt_macosx_LWCToolkit_beep
 167 (JNIEnv *env, jobject self)
 168 {
 169     NSBeep(); // produces both sound and visual flash, if configured in System Preferences
 170 }
 171 
 172 /*
 173  * Class:     sun_lwawt_macosx_LWCToolkit
 174  * Method:    initIDs
 175  * Signature: ()V
 176  */
 177 JNIEXPORT void JNICALL
 178 Java_sun_lwawt_macosx_LWCToolkit_initIDs
 179 (JNIEnv *env, jclass klass) {
 180     // set thread names
 181     if (![ThreadUtilities isAWTEmbedded]) {
 182         dispatch_async(dispatch_get_main_queue(), ^(void){
 183             [[NSThread currentThread] setName:@"AppKit Thread"];
 184             JNIEnv *env = [ThreadUtilities getJNIEnv];
 185             static JNF_CLASS_CACHE(jc_LWCToolkit, "sun/lwawt/macosx/LWCToolkit");
 186             static JNF_STATIC_MEMBER_CACHE(jsm_installToolkitThreadInJava, jc_LWCToolkit, "installToolkitThreadInJava", "()V");
 187             JNFCallStaticVoidMethod(env, jsm_installToolkitThreadInJava);
 188         });
 189     }
 190     
 191     gNumberOfButtons = sun_lwawt_macosx_LWCToolkit_BUTTONS;
 192 
 193     jclass inputEventClazz = (*env)->FindClass(env, "java/awt/event/InputEvent");
 194     CHECK_NULL(inputEventClazz);
 195     jmethodID getButtonDownMasksID = (*env)->GetStaticMethodID(env, inputEventClazz, "getButtonDownMasks", "()[I");
 196     CHECK_NULL(getButtonDownMasksID);
 197     jintArray obj = (jintArray)(*env)->CallStaticObjectMethod(env, inputEventClazz, getButtonDownMasksID);
 198     jint * tmp = (*env)->GetIntArrayElements(env, obj, JNI_FALSE);
 199     CHECK_NULL(tmp);
 200 
 201     gButtonDownMasks = (jint*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(jint), gNumberOfButtons);
 202     if (gButtonDownMasks == NULL) {
 203         gNumberOfButtons = 0;
 204         (*env)->ReleaseIntArrayElements(env, obj, tmp, JNI_ABORT);
 205         JNU_ThrowOutOfMemoryError(env, NULL);
 206         return;
 207     }
 208 
 209     int i;
 210     for (i = 0; i < gNumberOfButtons; i++) {
 211         gButtonDownMasks[i] = tmp[i];
 212     }
 213 
 214     (*env)->ReleaseIntArrayElements(env, obj, tmp, 0);
 215     (*env)->DeleteLocalRef(env, obj);
 216 }
 217 
 218 static UInt32 RGB(NSColor *c) {
 219     c = [c colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
 220     if (c == nil)
 221     {
 222         return -1; // opaque white
 223     }
 224 
 225     CGFloat r, g, b, a;
 226     [c getRed:&r green:&g blue:&b alpha:&a];
 227 
 228     UInt32 ir = (UInt32) (r*255+0.5),
 229     ig = (UInt32) (g*255+0.5),
 230     ib = (UInt32) (b*255+0.5),
 231     ia = (UInt32) (a*255+0.5);
 232 
 233     //    NSLog(@"%@ %d, %d, %d", c, ir, ig, ib);
 234 
 235     return ((ia & 0xFF) << 24) | ((ir & 0xFF) << 16) | ((ig & 0xFF) << 8) | ((ib & 0xFF) << 0);
 236 }
 237 


 426 
 427 
 428 // TODO: definitely doesn't belong here (copied from fontpath.c in the
 429 // solaris tree)...
 430 
 431 JNIEXPORT jstring JNICALL
 432 Java_sun_font_FontManager_getFontPath
 433 (JNIEnv *env, jclass obj, jboolean noType1)
 434 {
 435     return JNFNSToJavaString(env, @"/Library/Fonts");
 436 }
 437 
 438 // This isn't yet used on unix, the implementation is added since shared
 439 // code calls this method in preparation for future use.
 440 JNIEXPORT void JNICALL
 441 Java_sun_font_FontManager_populateFontFileNameMap
 442 (JNIEnv *env, jclass obj, jobject fontToFileMap, jobject fontToFamilyMap, jobject familyToFontListMap, jobject locale)
 443 {
 444 
 445 }



















































































   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 <dlfcn.h>
  27 #import <pthread.h>
  28 #import <objc/runtime.h>
  29 #import <Cocoa/Cocoa.h>
  30 #import <Security/AuthSession.h>
  31 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  32 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
  33 
  34 #include "jni_util.h"
  35 #import "CMenuBar.h"
  36 #import "InitIDs.h"
  37 #import "LWCToolkit.h"
  38 #import "ThreadUtilities.h"
  39 #import "AWT_debug.h"
  40 #import "CSystemColors.h"
  41 #import  "NSApplicationAWT.h"
  42 #import "PropertiesUtilities.h"
  43 #import "ApplicationDelegate.h"
  44 
  45 #import "sun_lwawt_macosx_LWCToolkit.h"
  46 
  47 #import "sizecalc.h"
  48 
  49 int gNumberOfButtons;
  50 jint* gButtonDownMasks;
  51 
  52 // Indicates that the app has been started with -XstartOnFirstThread
  53 // (directly or via WebStart settings), and AWT should not run its
  54 // own event loop in this mode. Even if a loop isn't running yet,
  55 // we expect an embedder (e.g. SWT) to start it some time later.
  56 static BOOL forceEmbeddedMode = NO;
  57 
  58 // This is the data necessary to have JNI_OnLoad wait for AppKit to start.
  59 static BOOL sAppKitStarted = NO;
  60 static pthread_mutex_t sAppKitStarted_mutex = PTHREAD_MUTEX_INITIALIZER;
  61 static pthread_cond_t sAppKitStarted_cv = PTHREAD_COND_INITIALIZER;
  62 
  63 @implementation AWTToolkit
  64 
  65 static long eventCount;
  66 
  67 + (long) getEventCount{
  68     return eventCount;
  69 }
  70 
  71 + (void) eventCountPlusPlus{    
  72     eventCount++;
  73 }
  74 
  75 @end
  76 
  77 
  78 @interface AWTRunLoopObject : NSObject {
  79     BOOL _shouldEndRunLoop;
  80 }
  81 @end
  82 


 116     return self;
 117 }
 118 
 119 - (void)dealloc {
 120     JNIEnv *env = [ThreadUtilities getJNIEnv];
 121     if (self.runnable) {
 122         (*env)->DeleteGlobalRef(env, self.runnable);
 123     }
 124     [super dealloc];
 125 }
 126 
 127 - (void)perform {
 128     JNIEnv* env = [ThreadUtilities getJNIEnv];
 129     static JNF_CLASS_CACHE(sjc_Runnable, "java/lang/Runnable");
 130     static JNF_MEMBER_CACHE(jm_Runnable_run, sjc_Runnable, "run", "()V");
 131     JNFCallVoidMethod(env, self.runnable, jm_Runnable_run);
 132     [self release];
 133 }
 134 @end
 135 
 136 void setBusy(BOOL busy) {
 137     AWT_ASSERT_APPKIT_THREAD;
 138 
 139     JNIEnv *env = [ThreadUtilities getJNIEnv];
 140     static JNF_CLASS_CACHE(jc_AWTAutoShutdown, "sun/awt/AWTAutoShutdown");
 141 
 142     if (busy) {
 143         static JNF_STATIC_MEMBER_CACHE(jm_notifyBusyMethod, jc_AWTAutoShutdown, "notifyToolkitThreadBusy", "()V");
 144         JNFCallStaticVoidMethod(env, jm_notifyBusyMethod);
 145     } else {
 146         static JNF_STATIC_MEMBER_CACHE(jm_notifyFreeMethod, jc_AWTAutoShutdown, "notifyToolkitThreadFree", "()V");
 147         JNFCallStaticVoidMethod(env, jm_notifyFreeMethod);
 148     }
 149 }
 150 
 151 static void setUpAWTAppKit(BOOL installObservers)
 152 {
 153     if (installObservers) {
 154         AWT_STARTUP_LOG(@"Setting up busy observers");
 155 
 156         // Add CFRunLoopObservers to call into AWT so that AWT knows that the
 157         //  AWT thread (which is the AppKit main thread) is alive. This way AWT
 158         //  will not automatically shutdown.
 159         CFRunLoopObserverRef busyObserver = CFRunLoopObserverCreateWithHandler(
 160                                                NULL,                        // CFAllocator
 161                                                kCFRunLoopAfterWaiting,      // CFOptionFlags
 162                                                true,                        // repeats
 163                                                NSIntegerMax,                // order
 164                                                ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
 165                                                    setBusy(YES);
 166                                                });
 167         
 168         CFRunLoopObserverRef notBusyObserver = CFRunLoopObserverCreateWithHandler(
 169                                                 NULL,                        // CFAllocator
 170                                                 kCFRunLoopBeforeWaiting,     // CFOptionFlags
 171                                                 true,                        // repeats
 172                                                 NSIntegerMin,                // order
 173                                                 ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
 174                                                     setBusy(NO);
 175                                                 });
 176         
 177         CFRunLoopRef runLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
 178         CFRunLoopAddObserver(runLoop, busyObserver, kCFRunLoopDefaultMode);
 179         CFRunLoopAddObserver(runLoop, notBusyObserver, kCFRunLoopDefaultMode);
 180         
 181         CFRelease(busyObserver);
 182         CFRelease(notBusyObserver);
 183         
 184         setBusy(YES);
 185     }
 186 
 187     JNIEnv* env = [ThreadUtilities getJNIEnv];
 188     static JNF_CLASS_CACHE(jc_LWCToolkit, "sun/lwawt/macosx/LWCToolkit");
 189     static JNF_STATIC_MEMBER_CACHE(jsm_installToolkitThreadInJava, jc_LWCToolkit, "installToolkitThreadInJava", "()V");
 190     JNFCallStaticVoidMethod(env, jsm_installToolkitThreadInJava);
 191 }
 192 
 193 BOOL isSWTInWebStart(JNIEnv* env) {
 194     NSString *swtWebStart = [PropertiesUtilities javaSystemPropertyForKey:@"com.apple.javaws.usingSWT" withEnv:env];
 195     return [@"true" isCaseInsensitiveLike:swtWebStart];
 196 }
 197 
 198 static void AWT_NSUncaughtExceptionHandler(NSException *exception) {
 199     NSLog(@"Apple AWT Internal Exception: %@", [exception description]);
 200 }
 201 
 202 @interface AWTStarter : NSObject
 203 + (void)start:(BOOL)headless;
 204 + (void)starter:(BOOL)onMainThread headless:(BOOL)headless;
 205 + (void)appKitIsRunning:(id)arg;
 206 @end
 207 
 208 @implementation AWTStarter
 209 
 210 + (BOOL) isConnectedToWindowServer {
 211     SecuritySessionId session_id;
 212     SessionAttributeBits session_info;
 213     OSStatus status = SessionGetInfo(callerSecuritySession, &session_id, &session_info);
 214     if (status != noErr) return NO;
 215     if (!(session_info & sessionHasGraphicAccess)) return NO;
 216     return YES;
 217 }
 218 
 219 + (BOOL) markAppAsDaemon {
 220     id jrsAppKitAWTClass = objc_getClass("JRSAppKitAWT");
 221     SEL markAppSel = @selector(markAppIsDaemon);
 222     if (![jrsAppKitAWTClass respondsToSelector:markAppSel]) return NO;
 223     return [jrsAppKitAWTClass performSelector:markAppSel] ? YES : NO;
 224 }
 225 
 226 + (void)appKitIsRunning:(id)arg {
 227     AWT_ASSERT_APPKIT_THREAD;
 228     AWT_STARTUP_LOG(@"About to message AppKit started");
 229 
 230     // Signal that AppKit has started (or is already running).
 231     pthread_mutex_lock(&sAppKitStarted_mutex);
 232     sAppKitStarted = YES;
 233     pthread_cond_signal(&sAppKitStarted_cv);
 234     pthread_mutex_unlock(&sAppKitStarted_mutex);
 235 
 236     AWT_STARTUP_LOG(@"Finished messaging AppKit started");
 237 }
 238 
 239 + (void)start:(BOOL)headless
 240 {
 241     // onMainThread is NOT the same at SWT mode!
 242     // If the JVM was started on the first thread for SWT, but the SWT loads the AWT on a secondary thread,
 243     // onMainThread here will be false but SWT mode will be true.  If we are currently on the main thread, we don't
 244     // need to throw AWT startup over to another thread.
 245     BOOL onMainThread = [NSThread isMainThread];
 246 
 247     NSString* msg = [NSString stringWithFormat:@"+[AWTStarter start headless:%d] { onMainThread:%d }", headless, onMainThread];
 248     AWT_STARTUP_LOG(msg);
 249 
 250     if (!headless)
 251     {
 252         // Listen for the NSApp to start. This indicates that JNI_OnLoad can proceed.
 253         //  It must wait because there is a chance that another java thread will grab
 254         //  the AppKit lock before the +[NSApplication sharedApplication] returns.
 255         //  See <rdar://problem/3492666> for an example.
 256         [[NSNotificationCenter defaultCenter] addObserver:[AWTStarter class]
 257                                                  selector:@selector(appKitIsRunning:)
 258                                                      name:NSApplicationDidFinishLaunchingNotification
 259                                                    object:nil];
 260 
 261         AWT_STARTUP_LOG(@"+[AWTStarter start:::]: registered NSApplicationDidFinishLaunchingNotification");
 262     }
 263 
 264     [ThreadUtilities performOnMainThreadWaiting:NO block:^() {
 265         [AWTStarter starter:onMainThread headless:headless];
 266     }];
 267 
 268 
 269     if (!headless && !onMainThread) {
 270 
 271         AWT_STARTUP_LOG(@"about to wait on AppKit startup mutex");
 272 
 273         // Wait here for AppKit to have started (or for AWT to have been loaded into
 274         //  an already running NSApplication).
 275         pthread_mutex_lock(&sAppKitStarted_mutex);
 276         while (sAppKitStarted == NO) {
 277             pthread_cond_wait(&sAppKitStarted_cv, &sAppKitStarted_mutex);
 278         }
 279         pthread_mutex_unlock(&sAppKitStarted_mutex);
 280 
 281         // AWT gets here AFTER +[AWTStarter appKitIsRunning:] is called.
 282         AWT_STARTUP_LOG(@"got out of the AppKit startup mutex");
 283     }
 284 
 285     if (!headless) {
 286         // Don't set the delegate until the NSApplication has been created and
 287         // its finishLaunching has initialized it.
 288         //  ApplicationDelegate is the support code for com.apple.eawt.
 289         [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 290             id<NSApplicationDelegate> delegate = [ApplicationDelegate sharedDelegate];
 291             if (delegate != nil) {
 292                 OSXAPP_SetApplicationDelegate(delegate);
 293             }
 294         }];
 295     }
 296 }
 297 
 298 + (void)starter:(BOOL)wasOnMainThread headless:(BOOL)headless {
 299     NSAutoreleasePool *pool = [NSAutoreleasePool new];
 300     // Add the exception handler of last resort
 301     NSSetUncaughtExceptionHandler(AWT_NSUncaughtExceptionHandler);
 302 
 303     // Headless mode trumps either ordinary AWT or SWT-in-AWT mode.  Declare us a daemon and return.
 304     if (headless) {
 305         // Note that we don't install run loop observers in headless mode
 306         // because we don't need them (see 7174704)
 307         if (!forceEmbeddedMode) {
 308             setUpAWTAppKit(false);
 309         }
 310         [AWTStarter markAppAsDaemon];
 311         return;
 312     }
 313 
 314     if (forceEmbeddedMode) {
 315         AWT_STARTUP_LOG(@"in SWT or SWT/WebStart mode");
 316 
 317         // Init a default NSApplication instance instead of the NSApplicationAWT.
 318         // Note that [NSApp isRunning] will return YES after that, though
 319         // this behavior isn't specified anywhere. We rely on that.
 320         NSApplicationLoad();
 321     }
 322 
 323     // This will create a NSApplicationAWT for standalone AWT programs, unless there is
 324     //  already a NSApplication instance. If there is already a NSApplication instance,
 325     //  and -[NSApplication isRunning] returns YES, AWT is embedded inside another
 326     //  AppKit Application.
 327     NSApplication *app = [NSApplicationAWT sharedApplication];
 328     BOOL isEmbedded = ![NSApp isKindOfClass:[NSApplicationAWT class]];
 329     [ThreadUtilities setAWTEmbedded:isEmbedded];
 330 
 331     if (!isEmbedded) {
 332         // Install run loop observers and set the AppKit Java thread name
 333         setUpAWTAppKit(true);
 334     }
 335 
 336     // AWT gets to this point BEFORE NSApplicationDidFinishLaunchingNotification is sent.
 337     if (![app isRunning]) {
 338         AWT_STARTUP_LOG(@"+[AWTStarter startAWT]: ![app isRunning]");
 339         // This is where the AWT AppKit thread parks itself to process events.
 340         [NSApplicationAWT runAWTLoopWithApp: app];
 341     } else {
 342         // We're either embedded, or showing a splash screen
 343         if (isEmbedded) {
 344             AWT_STARTUP_LOG(@"running embedded");
 345             
 346             // We don't track if the runloop is busy, so set it free to let AWT finish when it needs
 347             setBusy(NO);
 348         } else {
 349             AWT_STARTUP_LOG(@"running after showing a splash screen");
 350         }
 351         
 352         // Signal so that JNI_OnLoad can proceed.
 353         if (!wasOnMainThread) [AWTStarter appKitIsRunning:nil];
 354         
 355         // Proceed to exit this call as there is no reason to run the NSApplication event loop.
 356     }
 357     
 358     [pool drain];
 359 }
 360 
 361 @end
 362 
 363 /*
 364  * Class:     sun_lwawt_macosx_LWCToolkit
 365  * Method:    nativeSyncQueue
 366  * Signature: (J)Z
 367  */
 368 JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_LWCToolkit_nativeSyncQueue
 369 (JNIEnv *env, jobject self, jlong timeout)
 370 {
 371     int currentEventNum = [AWTToolkit getEventCount];
 372 
 373     NSApplication* sharedApp = [NSApplication sharedApplication];
 374     if ([sharedApp isKindOfClass:[NSApplicationAWT class]]) {
 375         NSApplicationAWT* theApp = (NSApplicationAWT*)sharedApp;
 376         [theApp postDummyEvent];
 377         [theApp waitForDummyEvent];
 378     } else {
 379         // could happen if we are embedded inside SWT application,
 380         // in this case just spin a single empty block through 
 381         // the event loop to give it a chance to process pending events
 382         [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){}];


 397 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_LWCToolkit_flushNativeSelectors
 398 (JNIEnv *env, jclass clz)
 399 {
 400 JNF_COCOA_ENTER(env);
 401         [ThreadUtilities performOnMainThreadWaiting:YES block:^(){}];
 402 JNF_COCOA_EXIT(env);
 403 }
 404 
 405 /*
 406  * Class:     sun_lwawt_macosx_LWCToolkit
 407  * Method:    beep
 408  * Signature: ()V
 409  */
 410 JNIEXPORT void JNICALL
 411 Java_sun_lwawt_macosx_LWCToolkit_beep
 412 (JNIEnv *env, jobject self)
 413 {
 414     NSBeep(); // produces both sound and visual flash, if configured in System Preferences
 415 }
 416 














































 417 static UInt32 RGB(NSColor *c) {
 418     c = [c colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
 419     if (c == nil)
 420     {
 421         return -1; // opaque white
 422     }
 423 
 424     CGFloat r, g, b, a;
 425     [c getRed:&r green:&g blue:&b alpha:&a];
 426 
 427     UInt32 ir = (UInt32) (r*255+0.5),
 428     ig = (UInt32) (g*255+0.5),
 429     ib = (UInt32) (b*255+0.5),
 430     ia = (UInt32) (a*255+0.5);
 431 
 432     //    NSLog(@"%@ %d, %d, %d", c, ir, ig, ib);
 433 
 434     return ((ia & 0xFF) << 24) | ((ir & 0xFF) << 16) | ((ig & 0xFF) << 8) | ((ib & 0xFF) << 0);
 435 }
 436 


 625 
 626 
 627 // TODO: definitely doesn't belong here (copied from fontpath.c in the
 628 // solaris tree)...
 629 
 630 JNIEXPORT jstring JNICALL
 631 Java_sun_font_FontManager_getFontPath
 632 (JNIEnv *env, jclass obj, jboolean noType1)
 633 {
 634     return JNFNSToJavaString(env, @"/Library/Fonts");
 635 }
 636 
 637 // This isn't yet used on unix, the implementation is added since shared
 638 // code calls this method in preparation for future use.
 639 JNIEXPORT void JNICALL
 640 Java_sun_font_FontManager_populateFontFileNameMap
 641 (JNIEnv *env, jclass obj, jobject fontToFileMap, jobject fontToFamilyMap, jobject familyToFontListMap, jobject locale)
 642 {
 643 
 644 }
 645 
 646 /*
 647  * Class:     sun_lwawt_macosx_LWCToolkit
 648  * Method:    initIDs
 649  * Signature: ()V
 650  */
 651 JNIEXPORT void JNICALL
 652 Java_sun_lwawt_macosx_LWCToolkit_initIDs
 653 (JNIEnv *env, jclass klass) {
 654 
 655     JNF_COCOA_ENTER(env)
 656 
 657     gNumberOfButtons = sun_lwawt_macosx_LWCToolkit_BUTTONS;
 658 
 659     jclass inputEventClazz = (*env)->FindClass(env, "java/awt/event/InputEvent");
 660     CHECK_NULL(inputEventClazz);
 661     jmethodID getButtonDownMasksID = (*env)->GetStaticMethodID(env, inputEventClazz, "getButtonDownMasks", "()[I");
 662     CHECK_NULL(getButtonDownMasksID);
 663     jintArray obj = (jintArray)(*env)->CallStaticObjectMethod(env, inputEventClazz, getButtonDownMasksID);
 664     jint * tmp = (*env)->GetIntArrayElements(env, obj, JNI_FALSE);
 665     CHECK_NULL(tmp);
 666 
 667     gButtonDownMasks = (jint*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(jint), gNumberOfButtons);
 668     if (gButtonDownMasks == NULL) {
 669         gNumberOfButtons = 0;
 670         (*env)->ReleaseIntArrayElements(env, obj, tmp, JNI_ABORT);
 671         JNU_ThrowOutOfMemoryError(env, NULL);
 672         return;
 673     }
 674 
 675     int i;
 676     for (i = 0; i < gNumberOfButtons; i++) {
 677         gButtonDownMasks[i] = tmp[i];
 678     }
 679 
 680     (*env)->ReleaseIntArrayElements(env, obj, tmp, 0);
 681     (*env)->DeleteLocalRef(env, obj);
 682 
 683     JNF_COCOA_EXIT(env)
 684 }
 685 
 686 /*
 687  * Class:     sun_lwawt_macosx_LWCToolkit
 688  * Method:    initAppkit
 689  * Signature: (Ljava/lang/ThreadGroup;)V
 690  */
 691 JNIEXPORT void JNICALL
 692 Java_sun_lwawt_macosx_LWCToolkit_initAppkit
 693 (JNIEnv *env, jclass klass, jobject appkitThreadGroup, jboolean headless) {
 694     JNF_COCOA_ENTER(env)
 695 
 696     [ThreadUtilities setAppkitThreadGroup:(*env)->NewGlobalRef(env, appkitThreadGroup)];
 697 
 698     // Launcher sets this env variable if -XstartOnFirstThread is specified
 699     char envVar[80];
 700     snprintf(envVar, sizeof(envVar), "JAVA_STARTED_ON_FIRST_THREAD_%d", getpid());
 701     if (getenv(envVar) != NULL) {
 702         forceEmbeddedMode = YES;
 703         unsetenv(envVar);
 704     }
 705 
 706     if (isSWTInWebStart(env)) {
 707         forceEmbeddedMode = YES;
 708     }
 709 
 710     [AWTStarter start:headless ? YES : NO];
 711 
 712     JNF_COCOA_EXIT(env)
 713 }
 714 
 715 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
 716     OSXAPP_SetJavaVM(vm);
 717 
 718     // We need to let Foundation know that this is a multithreaded application, if it isn't already.
 719     if (![NSThread isMultiThreaded]) {
 720         [NSThread detachNewThreadSelector:nil toTarget:nil withObject:nil];
 721     }
 722 
 723     return JNI_VERSION_1_4;
 724 }
 725