From c9396c0aabb6c1b710e1cdaa3fb123182dc91279 Mon Sep 17 00:00:00 2001 From: Mad Fish Date: Sun, 20 Jan 2013 17:15:37 +0100 Subject: cocoa_common: improved trackpad scrolling --- video/out/cocoa_common.m | 50 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'video') diff --git a/video/out/cocoa_common.m b/video/out/cocoa_common.m index d23a439629..217321d593 100644 --- a/video/out/cocoa_common.m +++ b/video/out/cocoa_common.m @@ -21,6 +21,7 @@ #import // for CGDisplayHideCursor #import #include +#include #include "cocoa_common.h" @@ -59,6 +60,9 @@ - (NSRect)convertRectToBacking:(NSRect)aRect; - (void)setWantsBestResolutionOpenGLSurface:(BOOL)aBool; @end +@interface NSEvent (IntroducedInLion) +- (BOOL)hasPreciseScrollingDeltas; +@end #endif // add power management assertion not available on OSX versions prior to 10.7 @@ -113,6 +117,8 @@ struct vo_cocoa_state { bool out_fs_resize; IOPMAssertionID power_mgmt_assertion; + + CGFloat accumulated_scroll; }; static int _instances = 0; @@ -135,15 +141,26 @@ static struct vo_cocoa_state *vo_cocoa_init_state(struct vo *vo) .display_cursor = 1, .cursor_autohide_delay = vo->opts->cursor_autohide_delay, .power_mgmt_assertion = kIOPMNullAssertionID, + .accumulated_scroll = 0, }; if (!vo_border) s->windowed_mask = NSBorderlessWindowMask; return s; } +static bool is_lion_or_above() +{ + static bool result = false, checked = false; + if (!checked) { + result = is_osx_version_at_least(10, 7, 0); + checked = true; + } + return result; +} + static bool supports_hidpi(NSView *view) { SEL hdpi_selector = @selector(setWantsBestResolutionOpenGLSurface:); - return is_osx_version_at_least(10, 7, 0) && view && + return is_lion_or_above() && view && [view respondsToSelector:hdpi_selector]; } @@ -738,10 +755,33 @@ void create_menu() - (void)scrollWheel:(NSEvent *)theEvent { - if ([theEvent deltaY] > 0) - mplayer_put_key(_vo->key_fifo, MOUSE_BTN3); - else - mplayer_put_key(_vo->key_fifo, MOUSE_BTN4); + struct vo_cocoa_state *s = _vo->cocoa; + + CGFloat delta; + // Use the dimention with the most delta as the scrolling one + if (FFABS([theEvent deltaY]) > FFABS([theEvent deltaX])) { + delta = [theEvent deltaY]; + } else { + delta = - [theEvent deltaX]; + } + + if (is_lion_or_above() && [theEvent hasPreciseScrollingDeltas]) { + s->accumulated_scroll += delta; + static const CGFloat threshold = 10; + while (s->accumulated_scroll >= threshold) { + s->accumulated_scroll -= threshold; + mplayer_put_key(_vo->key_fifo, MOUSE_BTN3); + } + while (s->accumulated_scroll <= -threshold) { + s->accumulated_scroll += threshold; + mplayer_put_key(_vo->key_fifo, MOUSE_BTN4); + } + } else { + if (delta > 0) + mplayer_put_key(_vo->key_fifo, MOUSE_BTN3); + else + mplayer_put_key(_vo->key_fifo, MOUSE_BTN4); + } } - (void)mouseEvent:(NSEvent *)theEvent -- cgit v1.2.3