Fix behavior of NSRunLoop runMode:beforeDate: and AppKit nextEvent* calls to properly run once or with a timeout

This commit is contained in:
Christopher Lloyd 2010-11-04 15:08:19 -04:00
parent 4cf8f2e2d7
commit 16c8ddadb7
14 changed files with 150 additions and 84 deletions

View File

@ -527,8 +527,10 @@ id NSApp=nil;
}
-(NSEvent *)nextEventMatchingMask:(unsigned int)mask untilDate:(NSDate *)untilDate inMode:(NSString *)mode dequeue:(BOOL)dequeue {
NSEvent *nextEvent=nil;
do {
NSAutoreleasePool *pool=[NSAutoreleasePool new];
NSEvent *nextEvent;
NS_DURING
[NSClassFromString(@"Win32RunningCopyPipe") performSelector:@selector(createRunningCopyPipe)];
@ -538,17 +540,26 @@ id NSApp=nil;
[self _checkForAppActivation];
[[NSApp windows] makeObjectsPerformSelector:@selector(displayIfNeeded)];
nextEvent=[_display nextEventMatchingMask:mask untilDate:untilDate inMode:mode dequeue:dequeue];
nextEvent=[[_display nextEventMatchingMask:mask untilDate:untilDate inMode:mode dequeue:dequeue] retain];
[_currentEvent release];
_currentEvent=[nextEvent retain];
if([nextEvent type]==NSAppKitSystem){
[nextEvent release];
nextEvent=nil;
}
NS_HANDLER
[self reportException:localException];
NS_ENDHANDLER
[pool release];
}while(nextEvent==nil && [untilDate timeIntervalSinceNow]>0);
return [[_currentEvent retain] autorelease];
if(nextEvent!=nil){
[_currentEvent release];
_currentEvent=[nextEvent retain];
}
return [nextEvent autorelease];
}
-(NSEvent *)currentEvent {
@ -713,10 +724,14 @@ id NSApp=nil;
}
-(int)runModalSession:(NSModalSession)session {
while([session stopCode]==NSRunContinuesResponse) {
NSAutoreleasePool *pool=[NSAutoreleasePool new];
NSDate *future=[NSDate distantFuture];
NSEvent *event=[self nextEventMatchingMask:NSAnyEventMask
untilDate:future inMode:NSModalPanelRunLoopMode dequeue:YES];
NSEvent *event=[self nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate date] inMode:NSModalPanelRunLoopMode dequeue:YES];
if(event==nil)
break;
NSWindow *window=[event window];
// in theory this could get weird, but all we want is the ESC-cancel keybinding, afaik NSApp doesn't respond to any other doCommandBySelectors...
@ -729,6 +744,7 @@ id NSApp=nil;
[[session modalWindow] makeKeyAndOrderFront:self];
[pool release];
}
return [session stopCode];
}
@ -755,7 +771,7 @@ id NSApp=nil;
while((result=[NSApp runModalSession:session])==NSRunContinuesResponse)
;
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
[self endModalSession:session];

View File

@ -93,7 +93,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
}
-(NSEvent *)nextEventMatchingMask:(unsigned)mask untilDate:(NSDate *)untilDate inMode:(NSString *)mode dequeue:(BOOL)dequeue {
NSEvent *result;
NSEvent *result=nil;
_eventMask=mask;
@ -102,15 +102,22 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
[[NSRunLoop currentRunLoop] runMode:mode beforeDate:untilDate];
if([_eventQueue count]==0)
result=[[[NSEvent alloc] initWithType:NSAppKitSystem location:NSMakePoint(0,0) modifierFlags:0 window:nil] autorelease];
while(result==nil && [_eventQueue count]>0){
NSEvent *check=[_eventQueue objectAtIndex:0];
if(!(NSEventMaskFromType([check type])&mask))
[_eventQueue removeObjectAtIndex:0];
else {
result=[[[_eventQueue objectAtIndex:0] retain] autorelease];
result=[[check retain] autorelease];
if(dequeue)
[_eventQueue removeObjectAtIndex:0];
}
}
if(result==nil)
result=[[[NSEvent alloc] initWithType:NSAppKitSystem location:NSMakePoint(0,0) modifierFlags:0 window:nil] autorelease];
return result;
}

View File

@ -379,13 +379,28 @@ static BOOL CALLBACK monitorEnumerator(HMONITOR hMonitor,HDC hdcMonitor,LPRECT r
}
-(NSEvent *)nextEventMatchingMask:(unsigned)mask untilDate:(NSDate *)untilDate inMode:(NSString *)mode dequeue:(BOOL)dequeue {
NSEvent *result;
NSEvent *result=nil;
[[NSRunLoop currentRunLoop] addInputSource:_eventInputSource forMode:mode];
[self stopWaitCursor];
result=[super nextEventMatchingMask:mask untilDate:untilDate inMode:mode dequeue:dequeue];
while([untilDate timeIntervalSinceNow]>0){
result=[super nextEventMatchingMask:mask|NSPlatformSpecificDisplayMask untilDate:untilDate inMode:mode dequeue:dequeue];
if([result type]==NSPlatformSpecificDisplayEvent){
Win32Event *win32Event=[(NSEvent_CoreGraphics *)result coreGraphicsEvent];
MSG msg=[win32Event msg];
DispatchMessage(&msg);
result=nil;
}
if(result!=nil)
break;
}
[self startWaitCursor];
[[NSRunLoop currentRunLoop] removeInputSource:_eventInputSource forMode:mode];
// [[NSRunLoop currentRunLoop] removeInputSource:_eventInputSource forMode:mode];
return result;
}

View File

@ -9,7 +9,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#import <AppKit/Win32EventInputSource.h>
#import <AppKit/Win32Display.h>
#import <AppKit/Win32Event.h>
#import <AppKit/NSEvent_periodic.h>
#import <AppKit/NSEvent_CoreGraphics.h>
@implementation Win32EventInputSource
@ -23,8 +25,12 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
NSAutoreleasePool *pool=[NSAutoreleasePool new];
if(![(Win32Display *)[Win32Display currentDisplay] postMSG:msg])
DispatchMessage(&msg);
if(![(Win32Display *)[Win32Display currentDisplay] postMSG:msg]){
Win32Event *cgEvent=[Win32Event eventWithMSG:msg];
NSEvent *event=[[[NSEvent_CoreGraphics alloc] initWithDisplayEvent:cgEvent] autorelease];
[[Win32Display currentDisplay] postEvent:event atStart:NO];
}
[pool release];
return YES;

View File

@ -1,12 +1,10 @@
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd <cjwl@objc.net>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
// Original - Christopher Lloyd <cjwl@objc.net>
#import <Foundation/NSDelayedPerform.h>
#import <Foundation/NSString.h>
@ -19,8 +17,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return self;
}
-(void)dealloc
{
-(void)dealloc {
[_object release];
[_argument release];
[super dealloc];

View File

@ -23,9 +23,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
-(NSSet *)validInputSources;
-(BOOL)immediateInputInMode:(NSString *)mode;
-(BOOL)fireSingleImmediateInputInMode:(NSString *)mode;
-(void)changingIntoMode:(NSString *)mode;
-(void)startingInMode:(NSString *)mode;
-(void)waitInBackgroundInMode:(NSString *)mode;

View File

@ -41,7 +41,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
[_inputSources removeObject:source];
}
-(void)changingIntoMode:(NSString *)mode {
-(void)startingInMode:(NSString *)mode {
// do nothing
}
@ -67,7 +67,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return _inputSources;
}
-(BOOL)immediateInputInMode:(NSString *)mode {
-(BOOL)fireSingleImmediateInputInMode:(NSString *)mode {
NSEnumerator *state=[[self validInputSources] objectEnumerator];
NSInputSource *check;

View File

@ -144,16 +144,16 @@ NSString * const NSRunLoopCommonModes=@"kCFRunLoopCommonModes";
-(NSDate *)limitDateForMode:(NSString *)mode {
NSRunLoopState *state=[self stateForMode:mode];
if(![mode isEqualToString:_currentMode]){
mode=[mode retain];
[_currentMode release];
_currentMode=[mode retain];
[state changingIntoMode:mode];
}
_currentMode=mode;
[state startingInMode:mode];
if([self _orderedPerforms])
[self _wakeUp];
;//[self _wakeUp];
if([state fireFirstTimer])
[self _wakeUp];
;//[self _wakeUp];
[[NSNotificationQueue defaultQueue] asapProcessMode:mode];
return [state limitDateForMode:mode];
@ -167,7 +167,7 @@ NSString * const NSRunLoopCommonModes=@"kCFRunLoopCommonModes";
[[NSNotificationQueue defaultQueue] idleProcessMode:mode];
}
else {
[state acceptInputForMode:mode beforeDate:date];
[state waitForSingleInputForMode:mode beforeDate:date];
}
}
@ -183,17 +183,39 @@ NSString * const NSRunLoopCommonModes=@"kCFRunLoopCommonModes";
@class NSSelectInputSource, NSSocket;
-(BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)date {
BOOL didProcessAnything=NO;
do {
NSAutoreleasePool *pool=[NSAutoreleasePool new];
NSDate *limitDate=[self limitDateForMode:mode];
if(limitDate!=nil){
if(limitDate==nil){
[pool release];
return didProcessAnything;
}
limitDate=[limitDate earlierDate:date];
[self acceptInputForMode:mode beforeDate:limitDate];
didProcessAnything=YES;
NSRunLoopState *state=[self stateForMode:mode];
if([[NSNotificationQueue defaultQueue] hasIdleNotificationsInMode:mode]){
if(![state pollInputForMode:mode])
[[NSNotificationQueue defaultQueue] idleProcessMode:mode];
}
else {
[state waitForSingleInputForMode:mode beforeDate:limitDate];
[pool release];
return YES;
}
[pool release];
return (limitDate!=nil);
}while([date timeIntervalSinceNow]>0);
return YES;
}
-(void)runUntilDate:(NSDate *)date {

View File

@ -20,7 +20,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
-(void)addTimer:(NSTimer *)timer;
-(void)changingIntoMode:(NSString *)mode;
-(void)startingInMode:(NSString *)mode;
-(BOOL)fireFirstTimer;
-(NSDate *)limitDateForMode:(NSString *)mode;
@ -30,7 +30,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
-(void)invalidateTimerWithDelayedPerform:(NSDelayedPerform *)delayed;
-(void)acceptInputForMode:(NSString *)mode beforeDate:(NSDate *)date;
-(BOOL)waitForSingleInputForMode:(NSString *)mode beforeDate:(NSDate *)date;
-(BOOL)pollInputForMode:(NSString *)mode;

View File

@ -47,13 +47,13 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
[_timers addObject:timer];
}
-(void)changingIntoMode:(NSString *)mode {
-(void)startingInMode:(NSString *)mode {
NSInteger i,count=[_asyncInputSourceSets count];
[_inputSourceSet changingIntoMode:mode];
[_inputSourceSet startingInMode:mode];
for(i=0;i<count;i++)
[[_asyncInputSourceSets objectAtIndex:i] changingIntoMode:mode];
[[_asyncInputSourceSets objectAtIndex:i] startingInMode:mode];
}
-(BOOL)fireFirstTimer {
@ -114,6 +114,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
}
-(NSInputSourceSet *)inputSourceSetForInputSource:(NSInputSource *)source {
if([_inputSourceSet recognizesInputSource:source])
return _inputSourceSet;
else {
@ -126,6 +127,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return check;
}
}
return nil;
}
@ -151,33 +153,31 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
}
}
-(BOOL)immediateInputInMode:(NSString *)mode {
if([_inputSourceSet immediateInputInMode:mode])
-(BOOL)fireSingleImmediateInputInMode:(NSString *)mode {
NSInteger i,count=[_asyncInputSourceSets count];
for(i=0;i<count;i++)
if([[_asyncInputSourceSets objectAtIndex:i] fireSingleImmediateInputInMode:mode])
return YES;
return [_inputSourceSet fireSingleImmediateInputInMode:mode];
}
-(BOOL)waitForSingleInputForMode:(NSString *)mode beforeDate:(NSDate *)date {
if([self fireSingleImmediateInputInMode:mode])
return YES;
else {
NSInteger i,count=[_asyncInputSourceSets count];
for(i=0;i<count;i++)
if([[_asyncInputSourceSets objectAtIndex:i] immediateInputInMode:mode])
return YES;
return NO;
}
}
-(void)acceptInputForMode:(NSString *)mode beforeDate:(NSDate *)date {
if(![self immediateInputInMode:mode]){
NSInteger i,count=[_asyncInputSourceSets count];
for(i=0;i<count;i++)
[[_asyncInputSourceSets objectAtIndex:i] waitInBackgroundInMode:mode];
[_inputSourceSet waitForInputInMode:mode beforeDate:date];
return [_inputSourceSet waitForInputInMode:mode beforeDate:date];
}
}
-(BOOL)pollInputForMode:(NSString *)mode {
if([self immediateInputInMode:mode])
if([self fireSingleImmediateInputInMode:mode])
return YES;
return [_inputSourceSet waitForInputInMode:mode beforeDate:[NSDate date]];

View File

@ -35,7 +35,7 @@ enum {
-(NSUInteger)selectEventMask;
-(void)setSelectEventMask:(NSUInteger)mask;
-(BOOL)processImmediateEvents:(NSUInteger)selectEvent;
-(NSUInteger)processImmediateEvents:(NSUInteger)selectEvent;
@end

View File

@ -58,12 +58,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
_eventMask=eventMask;
}
-(BOOL)processImmediateEvents:(NSUInteger)selectEvent {
-(NSUInteger)processImmediateEvents:(NSUInteger)selectEvent {
if((selectEvent&=_eventMask)==0)
return NO;
return 0;
[_delegate selectInputSource:self selectEvent:selectEvent];
return YES;
return selectEvent;
}
@end

View File

@ -11,7 +11,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
@class NSSelectSet;
@interface NSSelectInputSourceSet : NSInputSourceSet {
NSMutableSet *_outputSources;
NSSelectSet *_outputSet;
}

View File

@ -20,7 +20,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
-init {
[super init];
_outputSources=[NSMutableSet new];
_outputSet=nil;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectSetOutputNotification:) name:NSSelectSetOutputNotification object:nil];
return self;
@ -28,7 +27,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_outputSources release];
[_outputSet release];
[super dealloc];
}
@ -37,25 +35,29 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
return [source isKindOfClass:[NSSelectInputSource class]];
}
-(void)changingIntoMode:(NSString *)mode {
[_outputSources removeAllObjects];
[_outputSet autorelease];
_outputSet=nil;
/* The old logic was to remove all output when starting a different mode to prevent
stale triggers. However, when switching back and forth between modes the output
from the previous run was cleared when switching modes, causing the sockets to
never fire since they are a two times through event (one to fire the handle, one to fire the socket).
At this point, sockets always check their status with a select before firing, so staleness
shouldnt be a problem anymore.
Perfect solution is to probably make sockets a one time fire event.
*/
-(void)startingInMode:(NSString *)mode {
}
-(BOOL)immediateInputInMode:(NSString *)mode {
NSArray *sources=[_outputSources allObjects];
-(BOOL)fireSingleImmediateInputInMode:(NSString *)mode {
NSSet *validInputSources=[self validInputSources];
NSArray *sources=[validInputSources allObjects];
NSInteger i,count=[sources count];
for(i=0;i<count;i++){
NSSelectInputSource *check=[sources objectAtIndex:i];
[_outputSources removeObject:check];
if([validInputSources containsObject:check]){
NSSocket *socket=[check socket];
unsigned event=0;
NSUInteger event=0,remove;
if([_outputSet containsObjectForRead:socket])
event|=NSSelectReadEvent;
@ -64,15 +66,17 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
if([_outputSet containsObjectForException:socket])
event|=NSSelectExceptEvent;
if([check processImmediateEvents:event])
if((remove=[check processImmediateEvents:event])){
if(remove&NSSelectReadEvent)
[_outputSet removeObjectForRead:socket];
if(remove&NSSelectWriteEvent)
[_outputSet removeObjectForWrite:socket];
if(remove&NSSelectExceptEvent)
[_outputSet removeObjectForException:socket];
return YES;
}
}
[_outputSources removeAllObjects];
[_outputSet autorelease];
_outputSet=nil;
return NO;
}
@ -106,7 +110,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
-(void)waitInBackgroundInMode:(NSString *)mode {
NSSelectSet *selectSet=[self inputSelectSet];
[_outputSources setSet:[self validInputSources]];
[_outputSet autorelease];
_outputSet=nil;
@ -117,7 +120,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
NSSelectSet *selectSet=[self inputSelectSet];
NSError *error;
[_outputSources setSet:[self validInputSources]];
[_outputSet autorelease];
_outputSet=nil;
@ -130,7 +132,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
else {
[_outputSet retain];
return [self immediateInputInMode:mode];
return [self fireSingleImmediateInputInMode:mode];
}
}