2013-09-03 19:18:28 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2017-06-24 14:50:52 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2013-09-03 19:18:28 +00:00
|
|
|
*
|
|
|
|
* 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
|
2017-06-24 14:50:52 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-03 19:18:28 +00:00
|
|
|
*
|
2017-06-24 14:50:52 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-03 19:18:28 +00:00
|
|
|
*/
|
|
|
|
|
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()
|
|
|
|
@property(nonatomic, assign) BOOL hasMouseDown;
|
|
|
|
@property(nonatomic, retain) NSTrackingArea *tracker;
|
2014-10-11 22:14:21 +00:00
|
|
|
- (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
|
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
|
|
|
|
2016-12-15 22:06:04 +00:00
|
|
|
- (id)initWithFrame:(NSRect)frame
|
|
|
|
{
|
2014-01-04 16:17:33 +00:00
|
|
|
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
|
|
|
// 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];
|
2017-02-10 14:33:01 +00:00
|
|
|
|
|
|
|
if (![self containsMouseLocation])
|
|
|
|
[self.adapter putKey:MP_KEY_MOUSE_LEAVE withModifiers:0];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSPoint)mouseLocation
|
|
|
|
{
|
2013-09-28 08:09:57 +00:00
|
|
|
return [self.window mouseLocationOutsideOfEventStream];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)containsMouseLocation
|
|
|
|
{
|
2017-02-19 02:25:32 +00:00
|
|
|
CGFloat topMargin = 0.0;
|
|
|
|
CGFloat menuBarHeight = [[NSApp mainMenu] menuBarHeight];
|
|
|
|
|
|
|
|
// menuBarHeight is 0 when menu bar is hidden in fullscreen
|
|
|
|
// 1pt to compensate of the black line beneath the menu bar
|
|
|
|
if ([self.adapter isInFullScreenMode] && menuBarHeight > 0) {
|
|
|
|
NSRect tr = [NSWindow frameRectForContentRect:CGRectZero
|
|
|
|
styleMask:NSWindowStyleMaskTitled];
|
|
|
|
topMargin = tr.size.height + 1 + menuBarHeight;
|
|
|
|
}
|
|
|
|
|
2017-02-19 02:05:34 +00:00
|
|
|
NSRect vF = [[self.window screen] frame];
|
2017-02-19 02:25:32 +00:00
|
|
|
vF.size.height -= topMargin;
|
2013-09-03 19:18:28 +00:00
|
|
|
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];
|
|
|
|
}
|
2017-03-26 19:46:28 +00:00
|
|
|
|
2016-12-15 22:06:04 +00:00
|
|
|
- (BOOL)acceptsFirstResponder
|
|
|
|
{
|
2014-10-17 17:15:17 +00:00
|
|
|
return [self.adapter keyboardEnabled] || [self.adapter mouseEnabled];
|
|
|
|
}
|
|
|
|
|
2016-12-15 22:06:04 +00:00
|
|
|
- (BOOL)becomeFirstResponder
|
|
|
|
{
|
2014-10-17 17:15:17 +00:00
|
|
|
return [self.adapter keyboardEnabled] || [self.adapter mouseEnabled];
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (BOOL)resignFirstResponder { return YES; }
|
|
|
|
|
|
|
|
- (BOOL)canHideCursor
|
|
|
|
{
|
2015-03-08 14:19:17 +00:00
|
|
|
return !self.hasMouseDown && [self containsMouseLocation]
|
|
|
|
&& [[self window] isKeyWindow];
|
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
|
|
|
}
|
|
|
|
|
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)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];
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 19:46:28 +00:00
|
|
|
|
2014-10-17 17:15:17 +00:00
|
|
|
- (void)mouseUp:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([self.adapter mouseEnabled]) {
|
|
|
|
[self mouseUpEvent:event];
|
|
|
|
} else {
|
|
|
|
[super mouseUp:event];
|
|
|
|
}
|
|
|
|
}
|
2017-03-26 19:46:28 +00:00
|
|
|
|
2014-10-17 17:15:17 +00:00
|
|
|
- (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;
|
|
|
|
|
2017-07-26 19:41:31 +00:00
|
|
|
if (fabs([event deltaY]) >= fabs([event deltaX])) {
|
2013-09-03 19:18:28 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-26 19:41:31 +00:00
|
|
|
[self.adapter putAxis:cmd delta:fabs(delta)];
|
2013-10-12 14:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (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];
|
2017-07-26 19:08:14 +00:00
|
|
|
const float deltaX = (modifiers & NSEventModifierFlagShift) ?
|
|
|
|
[event scrollingDeltaY] : [event scrollingDeltaX];
|
|
|
|
const float deltaY = (modifiers & NSEventModifierFlagShift) ?
|
|
|
|
[event scrollingDeltaX] : [event scrollingDeltaY];
|
|
|
|
int mpkey;
|
|
|
|
|
|
|
|
if (fabs(deltaY) >= fabs(deltaX)) {
|
input: use mnemonic names for mouse buttons
mpv's mouse button numbering is based on X11 button numbering, which
allows for an arbitrary number of buttons and includes mouse wheel input
as buttons 3-6. This button numbering was used throughout the codebase
and exposed in input.conf, and it was difficult to remember which
physical button each number actually referred to and which referred to
the scroll wheel.
In practice, PC mice only have between two and five buttons and one or
two scroll wheel axes, which are more or less in the same location and
have more or less the same function. This allows us to use names to
refer to the buttons instead of numbers, which makes input.conf syntax a
lot easier to remember. It also makes the syntax robust to changes in
mpv's underlying numbering. The old MOUSE_BTNx names are still
understood as deprecated aliases of the named buttons.
This changes both the input.conf syntax and the MP_MOUSE_BTNx symbols in
the codebase, since I think both would benefit from using names over
numbers, especially since some platforms don't use X11 button numbering
and handle different mouse buttons in different windowing system events.
This also makes the names shorter, since otherwise they would be pretty
long, and it removes the high-numbered MOUSE_BTNx_DBL names, since they
weren't used.
Names are the same as used in Qt:
https://doc.qt.io/qt-5/qt.html#MouseButton-enum
2017-08-08 11:34:38 +00:00
|
|
|
mpkey = deltaY > 0 ? MP_WHEEL_UP : MP_WHEEL_DOWN;
|
2017-07-26 19:08:14 +00:00
|
|
|
} else {
|
input: use mnemonic names for mouse buttons
mpv's mouse button numbering is based on X11 button numbering, which
allows for an arbitrary number of buttons and includes mouse wheel input
as buttons 3-6. This button numbering was used throughout the codebase
and exposed in input.conf, and it was difficult to remember which
physical button each number actually referred to and which referred to
the scroll wheel.
In practice, PC mice only have between two and five buttons and one or
two scroll wheel axes, which are more or less in the same location and
have more or less the same function. This allows us to use names to
refer to the buttons instead of numbers, which makes input.conf syntax a
lot easier to remember. It also makes the syntax robust to changes in
mpv's underlying numbering. The old MOUSE_BTNx names are still
understood as deprecated aliases of the named buttons.
This changes both the input.conf syntax and the MP_MOUSE_BTNx symbols in
the codebase, since I think both would benefit from using names over
numbers, especially since some platforms don't use X11 button numbering
and handle different mouse buttons in different windowing system events.
This also makes the names shorter, since otherwise they would be pretty
long, and it removes the high-numbered MOUSE_BTNx_DBL names, since they
weren't used.
Names are the same as used in Qt:
https://doc.qt.io/qt-5/qt.html#MouseButton-enum
2017-08-08 11:34:38 +00:00
|
|
|
mpkey = deltaX > 0 ? MP_WHEEL_LEFT : MP_WHEEL_RIGHT;
|
2017-07-26 19:08:14 +00:00
|
|
|
}
|
|
|
|
|
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);
|
input: use mnemonic names for mouse buttons
mpv's mouse button numbering is based on X11 button numbering, which
allows for an arbitrary number of buttons and includes mouse wheel input
as buttons 3-6. This button numbering was used throughout the codebase
and exposed in input.conf, and it was difficult to remember which
physical button each number actually referred to and which referred to
the scroll wheel.
In practice, PC mice only have between two and five buttons and one or
two scroll wheel axes, which are more or less in the same location and
have more or less the same function. This allows us to use names to
refer to the buttons instead of numbers, which makes input.conf syntax a
lot easier to remember. It also makes the syntax robust to changes in
mpv's underlying numbering. The old MOUSE_BTNx names are still
understood as deprecated aliases of the named buttons.
This changes both the input.conf syntax and the MP_MOUSE_BTNx symbols in
the codebase, since I think both would benefit from using names over
numbers, especially since some platforms don't use X11 button numbering
and handle different mouse buttons in different windowing system events.
This also makes the names shorter, since otherwise they would be pretty
long, and it removes the high-numbered MOUSE_BTNx_DBL names, since they
weren't used.
Names are the same as used in Qt:
https://doc.qt.io/qt-5/qt.html#MouseButton-enum
2017-08-08 11:34:38 +00:00
|
|
|
int mpkey = (MP_MOUSE_BASE + [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] ||
|
2016-12-15 22:06:04 +00:00
|
|
|
[types containsObject:NSURLPboardType]) {
|
2014-01-09 13:42:43 +00:00
|
|
|
return NSDragOperationCopy;
|
2016-12-15 22:06:04 +00:00
|
|
|
} else {
|
2014-01-09 13:42:43 +00:00
|
|
|
return NSDragOperationNone;
|
2016-12-15 22:06:04 +00:00
|
|
|
}
|
2014-01-04 16:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
|
|
|
|
{
|
|
|
|
NSPasteboard *pboard = [sender draggingPasteboard];
|
2017-01-29 17:54:59 +00:00
|
|
|
if ([[pboard types] containsObject:NSFilenamesPboardType]) {
|
2014-01-09 13:42:43 +00:00
|
|
|
NSArray *pbitems = [pboard propertyListForType:NSFilenamesPboardType];
|
|
|
|
[self.adapter handleFilesArray:pbitems];
|
|
|
|
return YES;
|
2017-01-29 17:54:59 +00:00
|
|
|
} else if ([[pboard types] containsObject:NSURLPboardType]) {
|
|
|
|
NSURL *url = [NSURL URLFromPasteboard:pboard];
|
|
|
|
[self.adapter handleFilesArray:@[[url absoluteString]]];
|
|
|
|
return YES;
|
2014-01-09 13:42:43 +00:00
|
|
|
}
|
|
|
|
return NO;
|
2014-01-04 16:17:33 +00:00
|
|
|
}
|
2014-10-11 22:14:21 +00:00
|
|
|
|
|
|
|
- (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
|