Skip to content

Commit

Permalink
Added mouse throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
thepoet committed Mar 25, 2007
1 parent 5cd1ab8 commit 8687f78
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 24 deletions.
9 changes: 9 additions & 0 deletions Source/EventFilter.h
Expand Up @@ -80,6 +80,9 @@
* _emulationButton: The button number that mouse button #1 is currently being emulated as. A mouse up * _emulationButton: The button number that mouse button #1 is currently being emulated as. A mouse up
* event on button #1 will be mapped to a mouse up event on this button. * event on button #1 will be mapped to a mouse up event on this button.
* *
* _lastMousePoint: Maintains the last unpublished mouse move. Mouse moves are cached and only sent to the
* server on a periodic basis so as not to flood the server with mouse moves.
*
* When an event is received from the NSResponder, it is added to _pendingEvents. Then, _pendingEvents * When an event is received from the NSResponder, it is added to _pendingEvents. Then, _pendingEvents
* is scanned to determine whether any action can be taken. Things that might occur at this point are: * is scanned to determine whether any action can be taken. Things that might occur at this point are:
* *
Expand Down Expand Up @@ -127,6 +130,10 @@ typedef enum {
NSTimeInterval _tapAndClickButtonSpeed[2]; NSTimeInterval _tapAndClickButtonSpeed[2];
NSTimeInterval _tapAndClickTimeout[2]; NSTimeInterval _tapAndClickTimeout[2];
NSTimer *_tapAndClickTimer; NSTimer *_tapAndClickTimer;

NSTimer *_mouseTimer;
NSPoint _lastMousePoint;
bool _unsentMouseMoveExists;
} }


// Talking to the server // Talking to the server
Expand All @@ -147,6 +154,8 @@ typedef enum {
- (void)mouseDragged:(NSEvent *)theEvent; - (void)mouseDragged:(NSEvent *)theEvent;
- (void)rightMouseDragged:(NSEvent *)theEvent; - (void)rightMouseDragged:(NSEvent *)theEvent;
- (void)otherMouseDragged:(NSEvent *)theEvent; - (void)otherMouseDragged:(NSEvent *)theEvent;
- (void)sendUnpublishedMouseMove;
- (void)clearUnpublishedMouseMove;


// Local Keyboard Events // Local Keyboard Events
- (void)keyDown: (NSEvent *)theEvent; - (void)keyDown: (NSEvent *)theEvent;
Expand Down
112 changes: 88 additions & 24 deletions Source/EventFilter.m
Expand Up @@ -75,6 +75,10 @@ - (id)init
_pendingEvents = [[NSMutableArray alloc] init]; _pendingEvents = [[NSMutableArray alloc] init];
_pressedKeys = [[NSMutableSet alloc] init]; _pressedKeys = [[NSMutableSet alloc] init];
_emulationButton = 1; _emulationButton = 1;
_mouseTimer = nil;
_unsentMouseMoveExists = NO;
_lastMousePoint.x = -1;
_lastMousePoint.y = -1;


[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationDidBecomeActive:) name: NSApplicationDidBecomeActiveNotification object: nil]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationDidBecomeActive:) name: NSApplicationDidBecomeActiveNotification object: nil];
} }
Expand Down Expand Up @@ -172,6 +176,7 @@ - (void)scrollWheel: (NSEvent *)theEvent
addMask = rfbButton4Mask; addMask = rfbButton4Mask;
else else
addMask = rfbButton5Mask; addMask = rfbButton5Mask;
[self clearUnpublishedMouseMove];
[_connection mouseAt: p buttons: _pressedButtons | addMask]; // 'Mouse button down' [_connection mouseAt: p buttons: _pressedButtons | addMask]; // 'Mouse button down'
[_connection mouseAt: p buttons: _pressedButtons]; // 'Mouse button up' [_connection mouseAt: p buttons: _pressedButtons]; // 'Mouse button up'
} }
Expand All @@ -180,43 +185,94 @@ - (void)mouseMoved:(NSEvent *)theEvent
{ {
if ( _viewOnly ) if ( _viewOnly )
return; return;

if( nil != _mouseTimer )
{
[_mouseTimer invalidate];
[_mouseTimer release];
_mouseTimer = nil;
}


// send this out of order, in front of anything we've got pending NSPoint currentPoint = [_view convertPoint: [theEvent locationInWindow] fromView: nil];
NSPoint p = [_view convertPoint: [theEvent locationInWindow] fromView: nil];
[_connection mouseAt: p buttons: _pressedButtons]; //#define CHANGE_DIFF 5
#define IGNORE_COUNT 10

static int ct = IGNORE_COUNT;
bool bSendEventImmediately = NO;

if( IGNORE_COUNT == ct )
{
// if( (_lastMousePoint.x - currentPoint.x <= CHANGE_DIFF && _lastMousePoint.x - currentPoint.x >= -CHANGE_DIFF) &&
// (_lastMousePoint.y - currentPoint.y <= CHANGE_DIFF && _lastMousePoint.y - currentPoint.y >= -CHANGE_DIFF) )
// {
bSendEventImmediately = YES;
// }

ct = 0;
}
else
{
++ct;
}

_unsentMouseMoveExists = YES;
_lastMousePoint = currentPoint;

if( YES == bSendEventImmediately )
{
NSLog( @"Forced Mouse Move." );
[self sendUnpublishedMouseMove];
}
else
{
NSLog( @"Ignored Mouse Move." );
_mouseTimer = [NSTimer scheduledTimerWithTimeInterval: 0.05
target: self
selector: @selector(handleMouseTimer:)
userInfo: nil
repeats: NO];
[_mouseTimer retain];
}
} }


- (void)mouseDragged:(NSEvent *)theEvent - (void)handleMouseTimer: (NSTimer *) timer
{ {
if ( _viewOnly ) [_mouseTimer release];
return; _mouseTimer = nil;

[self sendUnpublishedMouseMove];

NSLog( @"Sent Mouse Move." );
}


// getting this implies that we've gotten a mouse down, so we can just send it directly - (void)clearUnpublishedMouseMove
NSPoint p = [_view convertPoint: [theEvent locationInWindow] fromView: nil]; {
[_connection mouseAt: p buttons: _pressedButtons]; _unsentMouseMoveExists = NO;
} }


- (void)rightMouseDragged:(NSEvent *)theEvent - (void)sendUnpublishedMouseMove
{ {
if ( _viewOnly ) if( YES == _unsentMouseMoveExists )
return; {
[self clearUnpublishedMouseMove];
[_connection mouseAt: _lastMousePoint buttons: _pressedButtons];
}
}


// getting this implies that we've gotten a mouse down, so we can just send it directly - (void)mouseDragged:(NSEvent *)theEvent
NSPoint p = [_view convertPoint: [theEvent locationInWindow] fromView: nil]; {
[_connection mouseAt: p buttons: _pressedButtons]; [self mouseMoved:theEvent];
} }


- (void)otherMouseDragged:(NSEvent *)theEvent - (void)rightMouseDragged:(NSEvent *)theEvent
{ {
if ( _viewOnly ) [self mouseMoved:theEvent];
return; }


// getting this implies that we've gotten a mouse down, so we can just send it directly - (void)otherMouseDragged:(NSEvent *)theEvent
if ( 2 == [theEvent buttonNumber] ) {
{ [self mouseMoved:theEvent];
NSPoint p = [_view convertPoint: [theEvent locationInWindow] fromView: nil];
[_connection mouseAt: p buttons: _pressedButtons];
}
} }




Expand Down Expand Up @@ -570,7 +626,10 @@ - (void)_sendMouseEvent: (QueuedEvent *)event
} }


if ( _pressedButtons != oldPressedButtons ) if ( _pressedButtons != oldPressedButtons )
{
[self clearUnpublishedMouseMove];
[_connection mouseAt: [event locationInWindow] buttons: _pressedButtons]; [_connection mouseAt: [event locationInWindow] buttons: _pressedButtons];
}
} }




Expand All @@ -596,11 +655,13 @@ - (void)_sendKeyEvent: (QueuedEvent *)event


if ( kQueuedKeyDownEvent == [event type] ) if ( kQueuedKeyDownEvent == [event type] )
{ {
[self sendUnpublishedMouseMove];
[_pressedKeys addObject: encodedChar]; [_pressedKeys addObject: encodedChar];
[_connection sendKey: sendKey pressed: YES]; [_connection sendKey: sendKey pressed: YES];
} }
else if ( [_pressedKeys containsObject: encodedChar] ) else if ( [_pressedKeys containsObject: encodedChar] )
{ {
[self sendUnpublishedMouseMove];
[_pressedKeys removeObject: encodedChar]; [_pressedKeys removeObject: encodedChar];
[_connection sendKey: sendKey pressed: NO]; [_connection sendKey: sendKey pressed: NO];
} }
Expand All @@ -613,11 +674,13 @@ - (void)_sendModifierEvent: (QueuedEvent *)event


if ( kQueuedModifierDownEvent == [event type] ) if ( kQueuedModifierDownEvent == [event type] )
{ {
[self sendUnpublishedMouseMove];
_pressedModifiers |= modifier; _pressedModifiers |= modifier;
[_connection sendModifier: modifier pressed: YES]; [_connection sendModifier: modifier pressed: YES];
} }
else if ( _pressedModifiers & modifier ) else if ( _pressedModifiers & modifier )
{ {
[self sendUnpublishedMouseMove];
_pressedModifiers &= ~modifier; _pressedModifiers &= ~modifier;
[_connection sendModifier: modifier pressed: NO]; [_connection sendModifier: modifier pressed: NO];
} }
Expand Down Expand Up @@ -852,6 +915,7 @@ - (unsigned int)handleMultiTapForButton: (unsigned int)button
NSPoint p = [_view convertPoint: [[_view window] convertScreenToBase: [NSEvent mouseLocation]] NSPoint p = [_view convertPoint: [[_view window] convertScreenToBase: [NSEvent mouseLocation]]
fromView: nil]; fromView: nil];
unsigned int rfbButton = ButtonNumberToRFBButtomMask( button ); unsigned int rfbButton = ButtonNumberToRFBButtomMask( button );
[self clearUnpublishedMouseMove];
[_connection mouseAt: p buttons: _pressedButtons | rfbButton]; // 'Mouse button down' [_connection mouseAt: p buttons: _pressedButtons | rfbButton]; // 'Mouse button down'
[_connection mouseAt: p buttons: _pressedButtons]; // 'Mouse button up' [_connection mouseAt: p buttons: _pressedButtons]; // 'Mouse button up'
return 0; return 0;
Expand Down

0 comments on commit 8687f78

Please sign in to comment.