< prev index next >

src/java.desktop/macosx/native/libosxapp/NSApplicationAWT.m

Print this page




 332     if (mask == DRAGMASK && [((NSString *)kCFRunLoopDefaultMode) isEqual:mode]) {
 333         postEventDuringEventSynthesis = YES;
 334     }
 335 
 336     NSEvent *event = [super nextEventMatchingMask:mask untilDate:expiration inMode:mode dequeue: deqFlag];
 337     postEventDuringEventSynthesis = NO;
 338 
 339     return event;
 340 }
 341 
 342 // NSTimeInterval has microseconds precision
 343 #define TS_EQUAL(ts1, ts2) (fabs((ts1) - (ts2)) < 1e-6)
 344 
 345 - (void)sendEvent:(NSEvent *)event
 346 {
 347     if ([event type] == NSApplicationDefined
 348             && TS_EQUAL([event timestamp], dummyEventTimestamp)
 349             && [event subtype] == NativeSyncQueueEvent) {
 350         [seenDummyEventLock lockWhenCondition:NO];
 351         [seenDummyEventLock unlockWithCondition:YES];

 352     } else if ([event type] == NSApplicationDefined && [event subtype] == ExecuteBlockEvent) {
 353         void (^block)() = (void (^)()) [event data1];
 354         block();
 355         [block release];
 356     } else if ([event type] == NSKeyUp && ([event modifierFlags] & NSCommandKeyMask)) {
 357         // Cocoa won't send us key up event when releasing a key while Cmd is down,
 358         // so we have to do it ourselves.
 359         [[self keyWindow] sendEvent:event];
 360     } else {
 361         [super sendEvent:event];
 362     }
 363 }
 364 
 365 /*
 366  * Posts the block to the AppKit event queue which will be executed
 367  * on the main AppKit loop.
 368  * While running nested loops this event will be ignored.
 369  */
 370 - (void)postRunnableEvent:(void (^)())block
 371 {
 372     void (^copy)() = [block copy];
 373     NSInteger encode = (NSInteger) copy;
 374     [copy retain];
 375     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 376     NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
 377                                         location: NSMakePoint(0,0)
 378                                    modifierFlags: 0
 379                                        timestamp: 0
 380                                     windowNumber: 0
 381                                          context: nil
 382                                          subtype: ExecuteBlockEvent
 383                                            data1: encode
 384                                            data2: 0];
 385 
 386     [NSApp postEvent: event atStart: NO];
 387     [pool drain];
 388 }
 389 
 390 
 391 
 392 - (void)postDummyEvent {
 393     seenDummyEventLock = [[NSConditionLock alloc] initWithCondition:NO];
 394     dummyEventTimestamp = [NSProcessInfo processInfo].systemUptime;
 395 
 396     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 397     NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
 398                                         location: NSMakePoint(0,0)
 399                                    modifierFlags: 0
 400                                        timestamp: dummyEventTimestamp
 401                                     windowNumber: 0
 402                                          context: nil
 403                                          subtype: NativeSyncQueueEvent
 404                                            data1: 0
 405                                            data2: 0];
 406     [NSApp postEvent: event atStart: NO];






 407     [pool drain];
 408 }
 409 
 410 - (void)waitForDummyEvent:(long long) timeout {

 411     if (timeout >= 0) {
 412         double sec = ((double) timeout)/1000;
 413         [seenDummyEventLock lockWhenCondition:YES
 414                                beforeDate:[NSDate dateWithTimeIntervalSinceNow:sec]];
 415     } else {
 416         [seenDummyEventLock lockWhenCondition:YES];
 417     }

 418     [seenDummyEventLock unlock];

 419     [seenDummyEventLock release];
 420 
 421     seenDummyEventLock = nil;
 422 }
 423 
 424 @end
 425 
 426 
 427 void OSXAPP_SetApplicationDelegate(id <NSApplicationDelegate> delegate)
 428 {
 429 AWT_ASSERT_APPKIT_THREAD;
 430     applicationDelegate = delegate;
 431 
 432     if (NSApp != nil) {
 433         [NSApp setDelegate: applicationDelegate];
 434 
 435         if (applicationDelegate && qad) {
 436             [qad processQueuedEventsWithTargetDelegate: applicationDelegate];
 437             qad = nil;
 438         }


 332     if (mask == DRAGMASK && [((NSString *)kCFRunLoopDefaultMode) isEqual:mode]) {
 333         postEventDuringEventSynthesis = YES;
 334     }
 335 
 336     NSEvent *event = [super nextEventMatchingMask:mask untilDate:expiration inMode:mode dequeue: deqFlag];
 337     postEventDuringEventSynthesis = NO;
 338 
 339     return event;
 340 }
 341 
 342 // NSTimeInterval has microseconds precision
 343 #define TS_EQUAL(ts1, ts2) (fabs((ts1) - (ts2)) < 1e-6)
 344 
 345 - (void)sendEvent:(NSEvent *)event
 346 {
 347     if ([event type] == NSApplicationDefined
 348             && TS_EQUAL([event timestamp], dummyEventTimestamp)
 349             && [event subtype] == NativeSyncQueueEvent) {
 350         [seenDummyEventLock lockWhenCondition:NO];
 351         [seenDummyEventLock unlockWithCondition:YES];
 352 
 353     } else if ([event type] == NSApplicationDefined && [event subtype] == ExecuteBlockEvent) {
 354         void (^block)() = (void (^)()) [event data1];
 355         block();
 356         [block release];
 357     } else if ([event type] == NSKeyUp && ([event modifierFlags] & NSCommandKeyMask)) {
 358         // Cocoa won't send us key up event when releasing a key while Cmd is down,
 359         // so we have to do it ourselves.
 360         [[self keyWindow] sendEvent:event];
 361     } else {
 362         [super sendEvent:event];
 363     }
 364 }
 365 
 366 /*
 367  * Posts the block to the AppKit event queue which will be executed
 368  * on the main AppKit loop.
 369  * While running nested loops this event will be ignored.
 370  */
 371 - (void)postRunnableEvent:(void (^)())block
 372 {
 373     void (^copy)() = [block copy];
 374     NSInteger encode = (NSInteger) copy;
 375     [copy retain];
 376     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 377     NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
 378                                         location: NSMakePoint(0,0)
 379                                    modifierFlags: 0
 380                                        timestamp: 0
 381                                     windowNumber: 0
 382                                          context: nil
 383                                          subtype: ExecuteBlockEvent
 384                                            data1: encode
 385                                            data2: 0];
 386 
 387     [NSApp postEvent: event atStart: NO];
 388     [pool drain];
 389 }
 390 
 391 - (void)postDummyEvent:(bool)useCocoa {


 392     seenDummyEventLock = [[NSConditionLock alloc] initWithCondition:NO];
 393     dummyEventTimestamp = [NSProcessInfo processInfo].systemUptime;
 394 
 395     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 396     NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
 397                                         location: NSMakePoint(0,0)
 398                                    modifierFlags: 0
 399                                        timestamp: dummyEventTimestamp
 400                                     windowNumber: 0
 401                                          context: nil
 402                                          subtype: NativeSyncQueueEvent
 403                                            data1: 0
 404                                            data2: 0];
 405     if (useCocoa) {
 406         [NSApp postEvent:event atStart:NO];
 407     } else {
 408         ProcessSerialNumber psn;
 409         GetCurrentProcess(&psn);
 410         CGEventPostToPSN(&psn, [event CGEvent]);
 411     }
 412     [pool drain];
 413 }
 414 
 415 - (void)waitForDummyEvent:(double)timeout {
 416     bool unlock = true;
 417     if (timeout >= 0) {
 418         double sec = timeout / 1000;
 419         unlock = [seenDummyEventLock lockWhenCondition:YES
 420                                beforeDate:[NSDate dateWithTimeIntervalSinceNow:sec]];
 421     } else {
 422         [seenDummyEventLock lockWhenCondition:YES];
 423     }
 424     if (unlock) {
 425         [seenDummyEventLock unlock];
 426     }
 427     [seenDummyEventLock release];
 428 
 429     seenDummyEventLock = nil;
 430 }
 431 
 432 @end
 433 
 434 
 435 void OSXAPP_SetApplicationDelegate(id <NSApplicationDelegate> delegate)
 436 {
 437 AWT_ASSERT_APPKIT_THREAD;
 438     applicationDelegate = delegate;
 439 
 440     if (NSApp != nil) {
 441         [NSApp setDelegate: applicationDelegate];
 442 
 443         if (applicationDelegate && qad) {
 444             [qad processQueuedEventsWithTargetDelegate: applicationDelegate];
 445             qad = nil;
 446         }
< prev index next >