2013-09-03 19:18:28 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2013-12-17 02:35:43 +00:00
|
|
|
#include "input/input.h"
|
|
|
|
#include "input/keycodes.h"
|
2013-09-03 19:18:28 +00:00
|
|
|
|
|
|
|
#include "osdep/macosx_compat.h"
|
|
|
|
#include "video/out/cocoa_common.h"
|
2014-10-05 20:31:33 +00:00
|
|
|
#include "events_view.h"
|
2013-09-03 19:18:28 +00:00
|
|
|
|
2014-10-05 20:31:33 +00:00
|
|
|
@interface MpvEventsView()
|
2015-01-01 13:38:08 +00:00
|
|
|
@property(nonatomic, assign) BOOL clearing;
|
2014-10-05 20:31:33 +00:00
|
|
|
@property(nonatomic, assign) BOOL hasMouseDown;
|
|
|
|
@property(nonatomic, retain) NSTrackingArea *tracker;
|
2014-10-11 22:14:21 +00:00
|
|
|
- (BOOL)hasDock:(NSScreen*)screen;
|
|
|
|
- (BOOL)hasMenubar:(NSScreen*)screen;
|
|
|
|
- (int)mpvButtonNumber:(NSEvent*)event;
|
2014-10-17 17:15:17 +00:00
|
|
|
- (void)mouseDownEvent:(NSEvent *)event;
|
|
|
|
- (void)mouseUpEvent:(NSEvent *)event;
|
2014-10-05 20:31:33 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MpvEventsView
|
2015-01-01 13:38:08 +00:00
|
|
|
@synthesize clearing = _clearing;
|
2013-09-03 19:18:28 +00:00
|
|
|
@synthesize adapter = _adapter;
|
|
|
|
@synthesize tracker = _tracker;
|
2014-10-05 20:31:33 +00:00
|
|
|
@synthesize hasMouseDown = _mouse_down;
|
2013-09-03 19:18:28 +00:00
|
|
|
|
2014-01-04 16:17:33 +00:00
|
|
|
- (id)initWithFrame:(NSRect)frame {
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
if (self) {
|
2014-01-09 13:42:43 +00:00
|
|
|
[self registerForDraggedTypes:@[NSFilenamesPboardType,
|
|
|
|
NSURLPboardType]];
|
2014-10-11 22:17:48 +00:00
|
|
|
[self setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
|
2014-01-04 16:17:33 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)setFullScreen:(BOOL)willBeFullscreen
|
|
|
|
{
|
|
|
|
if (willBeFullscreen && ![self isInFullScreenMode]) {
|
|
|
|
NSApplicationPresentationOptions popts =
|
|
|
|
NSApplicationPresentationDefault;
|
|
|
|
|
2014-10-11 22:14:21 +00:00
|
|
|
if ([self hasMenubar:[self.adapter fsScreen]])
|
2013-09-03 19:18:28 +00:00
|
|
|
// Cocoa raises an exception when autohiding the menubar but
|
|
|
|
// not the dock. They probably got bored while programming the
|
|
|
|
// multi screen support and took some shortcuts (tested on 10.8).
|
|
|
|
popts |= NSApplicationPresentationAutoHideMenuBar |
|
|
|
|
NSApplicationPresentationAutoHideDock;
|
|
|
|
|
2014-10-11 22:14:21 +00:00
|
|
|
if ([self hasDock:[self.adapter fsScreen]])
|
2013-09-03 19:18:28 +00:00
|
|
|
popts |= NSApplicationPresentationAutoHideDock;
|
|
|
|
|
|
|
|
NSDictionary *fsopts = @{
|
2014-12-01 17:05:30 +00:00
|
|
|
NSFullScreenModeAllScreens : @([self.adapter fsModeAllScreens]),
|
2013-09-03 19:18:28 +00:00
|
|
|
NSFullScreenModeApplicationPresentationOptions : @(popts)
|
|
|
|
};
|
|
|
|
|
|
|
|
// The original "windowed" window will stay around since sending a
|
|
|
|
// view fullscreen wraps it in another window. This is noticeable when
|
|
|
|
// sending the View fullscreen to another screen. Make it go away
|
|
|
|
// manually.
|
|
|
|
[self.window orderOut:self];
|
|
|
|
|
|
|
|
[self enterFullScreenMode:[self.adapter fsScreen]
|
|
|
|
withOptions:fsopts];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!willBeFullscreen && [self isInFullScreenMode]) {
|
|
|
|
[self exitFullScreenModeWithOptions:nil];
|
|
|
|
|
|
|
|
// Show the "windowed" window again.
|
|
|
|
[self.window makeKeyAndOrderFront:self];
|
|
|
|
[self.window makeFirstResponder:self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 13:38:08 +00:00
|
|
|
- (void)clear
|
|
|
|
{
|
2015-01-02 19:52:46 +00:00
|
|
|
if ([self isInFullScreenMode]) {
|
2015-01-02 18:17:43 +00:00
|
|
|
self.clearing = YES;
|
|
|
|
[self exitFullScreenModeWithOptions:nil];
|
|
|
|
}
|
2015-01-01 13:38:08 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
// mpv uses flipped coordinates, because X11 uses those. So let's just use them
|
|
|
|
// as well without having to do any coordinate conversion of mouse positions.
|
|
|
|
- (BOOL)isFlipped { return YES; }
|
|
|
|
|
|
|
|
- (void)updateTrackingAreas
|
|
|
|
{
|
2014-10-17 17:15:17 +00:00
|
|
|
if (self.tracker)
|
|
|
|
[self removeTrackingArea:self.tracker];
|
|
|
|
|
|
|
|
if (![self.adapter mouseEnabled])
|
|
|
|
return;
|
2013-09-03 19:18:28 +00:00
|
|
|
|
|
|
|
NSTrackingAreaOptions trackingOptions =
|
|
|
|
NSTrackingEnabledDuringMouseDrag |
|
|
|
|
NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved |
|
|
|
|
NSTrackingActiveAlways;
|
|
|
|
|
|
|
|
self.tracker =
|
|
|
|
[[[NSTrackingArea alloc] initWithRect:[self bounds]
|
|
|
|
options:trackingOptions
|
|
|
|
owner:self
|
|
|
|
userInfo:nil] autorelease];
|
|
|
|
|
|
|
|
[self addTrackingArea:self.tracker];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSPoint)mouseLocation
|
|
|
|
{
|
2013-09-28 08:09:57 +00:00
|
|
|
return [self.window mouseLocationOutsideOfEventStream];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)containsMouseLocation
|
|
|
|
{
|
|
|
|
NSRect vF = [[self.window screen] visibleFrame];
|
|
|
|
NSRect vFW = [self.window convertRectFromScreen:vF];
|
|
|
|
NSRect vFV = [self convertRect:vFW fromView:nil];
|
2013-09-28 08:09:57 +00:00
|
|
|
NSPoint pt = [self convertPoint:[self mouseLocation] fromView:nil];
|
2013-09-03 19:18:28 +00:00
|
|
|
|
|
|
|
// clip bounds to current visibleFrame
|
|
|
|
NSRect clippedBounds = CGRectIntersection([self bounds], vFV);
|
2013-09-28 08:09:57 +00:00
|
|
|
return CGRectContainsPoint(clippedBounds, pt);
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 17:15:17 +00:00
|
|
|
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
return [self.adapter mouseEnabled];
|
|
|
|
}
|
|
|
|
- (BOOL)acceptsFirstResponder {
|
|
|
|
return [self.adapter keyboardEnabled] || [self.adapter mouseEnabled];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)becomeFirstResponder {
|
|
|
|
return [self.adapter keyboardEnabled] || [self.adapter mouseEnabled];
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (BOOL)resignFirstResponder { return YES; }
|
|
|
|
|
2014-10-09 20:11:12 +00:00
|
|
|
- (void)keyDown:(NSEvent *)event {
|
|
|
|
[self.adapter putKeyEvent:event];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)keyUp:(NSEvent *)event {
|
|
|
|
[self.adapter putKeyEvent:event];
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (BOOL)canHideCursor
|
|
|
|
{
|
2014-10-05 21:49:09 +00:00
|
|
|
return !self.hasMouseDown && [self containsMouseLocation];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseEntered:(NSEvent *)event
|
|
|
|
{
|
2014-10-17 17:15:17 +00:00
|
|
|
[super mouseEntered:event];
|
2015-02-17 05:50:57 +00:00
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self.adapter putKey:MP_KEY_MOUSE_ENTER withModifiers:0];
|
|
|
|
}
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseExited:(NSEvent *)event
|
|
|
|
{
|
2014-10-17 17:15:17 +00:00
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self.adapter putKey:MP_KEY_MOUSE_LEAVE withModifiers:0];
|
|
|
|
} else {
|
|
|
|
[super mouseExited:event];
|
|
|
|
}
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setFrameSize:(NSSize)size
|
|
|
|
{
|
|
|
|
[super setFrameSize:size];
|
2015-01-01 13:38:08 +00:00
|
|
|
|
|
|
|
if (self.clearing)
|
|
|
|
return;
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
[self signalMousePosition];
|
|
|
|
}
|
|
|
|
|
2013-09-28 08:09:57 +00:00
|
|
|
- (NSPoint)convertPointToPixels:(NSPoint)point
|
|
|
|
{
|
|
|
|
point = [self convertPoint:point fromView:nil];
|
|
|
|
point = [self convertPointToBacking:point];
|
|
|
|
// flip y since isFlipped returning YES doesn't affect the backing
|
|
|
|
// coordinate system
|
|
|
|
point.y = -point.y;
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)signalMousePosition
|
|
|
|
{
|
2013-09-28 08:09:57 +00:00
|
|
|
NSPoint p = [self convertPointToPixels:[self mouseLocation]];
|
2013-09-03 19:18:28 +00:00
|
|
|
[self.adapter signalMouseMovement:p];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)signalMouseMovement:(NSEvent *)event
|
|
|
|
{
|
2013-09-28 08:09:57 +00:00
|
|
|
NSPoint p = [self convertPointToPixels:[event locationInWindow]];
|
2013-09-03 19:18:28 +00:00
|
|
|
[self.adapter signalMouseMovement:p];
|
|
|
|
}
|
|
|
|
|
2014-10-17 17:15:17 +00:00
|
|
|
- (void)mouseMoved:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self signalMouseMovement:event];
|
|
|
|
} else {
|
|
|
|
[super mouseMoved:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDragged:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self signalMouseMovement:event];
|
|
|
|
} else {
|
|
|
|
[super mouseDragged:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDown:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self mouseDownEvent:event];
|
|
|
|
} else {
|
|
|
|
[super mouseDown:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- (void)mouseUp:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self mouseUpEvent:event];
|
|
|
|
} else {
|
|
|
|
[super mouseUp:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- (void)rightMouseDown:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self mouseDownEvent:event];
|
|
|
|
} else {
|
|
|
|
[super rightMouseUp:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)rightMouseUp:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self mouseUpEvent:event];
|
|
|
|
} else {
|
|
|
|
[super rightMouseUp:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherMouseDown:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self mouseDownEvent:event];
|
|
|
|
} else {
|
|
|
|
[super otherMouseDown:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)otherMouseUp:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self mouseUpEvent:event];
|
|
|
|
} else {
|
|
|
|
[super otherMouseUp:event];
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 19:18:28 +00:00
|
|
|
|
2013-10-12 14:03:41 +00:00
|
|
|
- (void)preciseScroll:(NSEvent *)event
|
2013-09-03 19:18:28 +00:00
|
|
|
{
|
|
|
|
CGFloat delta;
|
|
|
|
int cmd;
|
|
|
|
|
|
|
|
if (FFABS([event deltaY]) >= FFABS([event deltaX])) {
|
|
|
|
delta = [event deltaY] * 0.1;
|
|
|
|
cmd = delta > 0 ? MP_AXIS_UP : MP_AXIS_DOWN;
|
|
|
|
} else {
|
|
|
|
delta = [event deltaX] * 0.1;
|
|
|
|
cmd = delta > 0 ? MP_AXIS_RIGHT : MP_AXIS_LEFT;
|
|
|
|
}
|
|
|
|
|
2013-10-12 14:03:41 +00:00
|
|
|
[self.adapter putAxis:cmd delta:FFABS(delta)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scrollWheel:(NSEvent *)event
|
|
|
|
{
|
2014-10-17 17:15:17 +00:00
|
|
|
if (![self.adapter mouseEnabled]) {
|
|
|
|
[super scrollWheel:event];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
if ([event hasPreciseScrollingDeltas]) {
|
2013-10-12 14:03:41 +00:00
|
|
|
[self preciseScroll:event];
|
2013-09-03 19:18:28 +00:00
|
|
|
} else {
|
|
|
|
const int modifiers = [event modifierFlags];
|
2013-10-12 13:24:26 +00:00
|
|
|
const int mpkey = [event deltaY] > 0 ? MP_MOUSE_BTN3 : MP_MOUSE_BTN4;
|
2013-09-03 19:18:28 +00:00
|
|
|
[self.adapter putKey:mpkey withModifiers:modifiers];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDownEvent:(NSEvent *)event
|
|
|
|
{
|
|
|
|
[self putMouseEvent:event withState:MP_KEY_STATE_DOWN];
|
|
|
|
|
|
|
|
if ([event clickCount] > 1)
|
|
|
|
[self putMouseEvent:event withState:MP_KEY_STATE_UP];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseUpEvent:(NSEvent *)event
|
|
|
|
{
|
|
|
|
[self putMouseEvent:event withState:MP_KEY_STATE_UP];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)putMouseEvent:(NSEvent *)event withState:(int)state
|
|
|
|
{
|
2014-10-05 20:31:33 +00:00
|
|
|
self.hasMouseDown = (state == MP_KEY_STATE_DOWN);
|
2014-10-11 22:14:21 +00:00
|
|
|
int mpkey = (MP_MOUSE_BTN0 + [self mpvButtonNumber:event]);
|
2013-09-03 19:18:28 +00:00
|
|
|
[self.adapter putKey:(mpkey | state) withModifiers:[event modifierFlags]];
|
|
|
|
}
|
|
|
|
|
2014-01-04 16:17:33 +00:00
|
|
|
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
|
|
|
|
{
|
2014-01-09 13:42:43 +00:00
|
|
|
NSPasteboard *pboard = [sender draggingPasteboard];
|
|
|
|
NSArray *types = [pboard types];
|
|
|
|
if ([types containsObject:NSFilenamesPboardType] ||
|
|
|
|
[types containsObject:NSURLPboardType])
|
|
|
|
return NSDragOperationCopy;
|
|
|
|
else
|
|
|
|
return NSDragOperationNone;
|
2014-01-04 16:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
|
|
|
|
{
|
|
|
|
NSPasteboard *pboard = [sender draggingPasteboard];
|
2014-01-09 13:42:43 +00:00
|
|
|
if ([[pboard types] containsObject:NSURLPboardType]) {
|
|
|
|
NSURL *file_url = [NSURL URLFromPasteboard:pboard];
|
|
|
|
[self.adapter handleFilesArray:@[[file_url absoluteString]]];
|
|
|
|
return YES;
|
|
|
|
} else if ([[pboard types] containsObject:NSFilenamesPboardType]) {
|
|
|
|
NSArray *pbitems = [pboard propertyListForType:NSFilenamesPboardType];
|
|
|
|
[self.adapter handleFilesArray:pbitems];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
2014-01-04 16:17:33 +00:00
|
|
|
}
|
2014-10-11 22:14:21 +00:00
|
|
|
|
|
|
|
- (BOOL)hasDock:(NSScreen*)screen
|
|
|
|
{
|
|
|
|
NSRect vF = [screen visibleFrame];
|
|
|
|
NSRect f = [screen frame];
|
|
|
|
return
|
|
|
|
// The visible frame's width is smaller: dock is on left or right end
|
|
|
|
// of this method's receiver.
|
|
|
|
vF.size.width < f.size.width ||
|
|
|
|
// The visible frame's veritical origin is bigger: dock is
|
|
|
|
// on the bottom of this method's receiver.
|
|
|
|
vF.origin.y > f.origin.y;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)hasMenubar:(NSScreen*)screen
|
|
|
|
{
|
|
|
|
NSRect vF = [screen visibleFrame];
|
|
|
|
NSRect f = [screen frame];
|
|
|
|
return f.size.height + f.origin.y > vF.size.height + vF.origin.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)mpvButtonNumber:(NSEvent*)event
|
|
|
|
{
|
|
|
|
int buttonNumber = [event buttonNumber];
|
|
|
|
switch (buttonNumber) {
|
|
|
|
case 1: return 2;
|
|
|
|
case 2: return 1;
|
|
|
|
default: return buttonNumber;
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 19:18:28 +00:00
|
|
|
@end
|