2013-06-02 15:39:05 +00:00
|
|
|
/*
|
|
|
|
* Cocoa Application Event Handling
|
|
|
|
*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2017-06-24 14:08:17 +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-06-02 15:39:05 +00:00
|
|
|
*
|
2013-06-02 22:52:40 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-06-02 15:39:05 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-06-24 14:08:17 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-06-02 15:39:05 +00:00
|
|
|
*
|
2017-06-24 14:08:17 +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-06-02 15:39:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Carbon header is included but Carbon is NOT linked to mpv's binary. This
|
|
|
|
// file only needs this include to use the keycode definitions in keymap.
|
|
|
|
#import <Carbon/Carbon.h>
|
2013-06-02 16:23:18 +00:00
|
|
|
|
|
|
|
// Media keys definitions
|
|
|
|
#import <IOKit/hidsystem/ev_keymap.h>
|
2013-06-02 15:39:05 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2014-04-16 02:38:19 +00:00
|
|
|
#include "input/event.h"
|
2013-12-17 02:35:43 +00:00
|
|
|
#include "input/input.h"
|
2017-02-25 20:56:59 +00:00
|
|
|
#include "player/client.h"
|
2014-04-15 23:51:40 +00:00
|
|
|
#include "input/keycodes.h"
|
2013-06-02 15:39:05 +00:00
|
|
|
// doesn't make much sense, but needed to access keymap functionality
|
|
|
|
#include "video/out/vo.h"
|
2013-06-19 17:25:19 +00:00
|
|
|
|
|
|
|
#include "osdep/macosx_compat.h"
|
2014-04-15 23:51:40 +00:00
|
|
|
#import "osdep/macosx_events_objc.h"
|
2017-02-25 20:56:59 +00:00
|
|
|
#import "osdep/macosx_application_objc.h"
|
2014-04-15 23:51:40 +00:00
|
|
|
|
2014-10-17 15:07:01 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
cocoa-cb: initial implementation via opengl-cb API
this is meant to replace the old and not properly working vo_gpu/opengl
cocoa backend in the future. the problems are various shortcomings of
Apple's opengl implementation and buggy behaviour in certain
circumstances that couldn't be properly worked around. there are also
certain regressions on newer macOS versions from 10.11 onwards.
- awful opengl performance with a none layer backed context
- huge amount of dropped frames with an early context flush
- flickering of system elements like the dock or volume indicator
- double buffering not properly working with a none layer backed context
- bad performance in fullscreen because of system optimisations
all the problems were caused by using a normal opengl context, that
seems somewhat abandoned by apple, and are fixed by using a layer backed
opengl context instead. problems that couldn't be fixed could be
properly worked around.
this has all features our old backend has sans the wid embedding,
the possibility to disable the automatic GPU switching and taking
screenshots of the window content. the first was deemed unnecessary by
me for now, since i just use the libmpv API that others can use anyway.
second is technically not possible atm because we have to pre-allocate
our opengl context at a time the config isn't read yet, so we can't get
the needed property. third one is a bit tricky because of deadlocking
and it needed to be in sync, hopefully i can work around that in the
future.
this also has at least one additional feature or eye-candy. a properly
working fullscreen animation with the native fs. also since this is a
direct port of the old backend of the parts that could be used, though
with adaptions and improvements, this looks a lot cleaner and easier to
understand.
some credit goes to @pigoz for the initial swift build support which
i could improve upon.
Fixes: #5478, #5393, #5152, #5151, #4615, #4476, #3978, #3746, #3739,
#2392, #2217
2018-02-12 11:28:19 +00:00
|
|
|
#if HAVE_MACOS_COCOA_CB
|
|
|
|
#include "osdep/macOS_swift.h"
|
|
|
|
#endif
|
|
|
|
|
2014-04-15 23:51:40 +00:00
|
|
|
@interface EventsResponder ()
|
|
|
|
{
|
2014-04-16 00:09:47 +00:00
|
|
|
struct input_ctx *_inputContext;
|
2017-02-25 20:56:59 +00:00
|
|
|
struct mpv_handle *_ctx;
|
|
|
|
BOOL _is_application;
|
2015-05-26 20:47:11 +00:00
|
|
|
NSCondition *_input_lock;
|
2014-04-15 23:51:40 +00:00
|
|
|
CFMachPortRef _mk_tap_port;
|
2014-10-17 15:07:01 +00:00
|
|
|
#if HAVE_APPLE_REMOTE
|
2014-04-15 23:51:40 +00:00
|
|
|
HIDRemote *_remote;
|
2014-10-17 15:07:01 +00:00
|
|
|
#endif
|
2014-04-15 23:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)handleMediaKey:(NSEvent *)event;
|
|
|
|
- (NSEvent *)handleKey:(NSEvent *)event;
|
2017-04-19 10:35:42 +00:00
|
|
|
- (BOOL)setMpvHandle:(struct mpv_handle *)ctx;
|
2017-02-25 20:56:59 +00:00
|
|
|
- (void)readEvents;
|
2014-04-15 23:51:40 +00:00
|
|
|
- (void)startAppleRemote;
|
|
|
|
- (void)stopAppleRemote;
|
|
|
|
- (void)startMediaKeys;
|
|
|
|
- (void)restartMediaKeys;
|
|
|
|
- (void)stopMediaKeys;
|
|
|
|
- (int)mapKeyModifiers:(int)cocoaModifiers;
|
|
|
|
- (int)keyModifierMask:(NSEvent *)event;
|
|
|
|
@end
|
|
|
|
|
2013-06-02 15:39:05 +00:00
|
|
|
|
2016-09-20 21:30:54 +00:00
|
|
|
#define NSLeftAlternateKeyMask (0x000020 | NSEventModifierFlagOption)
|
|
|
|
#define NSRightAlternateKeyMask (0x000040 | NSEventModifierFlagOption)
|
2013-06-02 15:39:05 +00:00
|
|
|
|
2013-07-21 08:33:18 +00:00
|
|
|
static bool LeftAltPressed(int mask)
|
2013-06-02 15:39:05 +00:00
|
|
|
{
|
2013-07-21 08:33:18 +00:00
|
|
|
return (mask & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask;
|
2013-06-02 15:39:05 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 08:33:18 +00:00
|
|
|
static bool RightAltPressed(int mask)
|
2013-06-02 15:39:05 +00:00
|
|
|
{
|
2013-07-21 08:33:18 +00:00
|
|
|
return (mask & NSRightAlternateKeyMask) == NSRightAlternateKeyMask;
|
2013-06-02 15:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct mp_keymap keymap[] = {
|
|
|
|
// special keys
|
|
|
|
{kVK_Return, MP_KEY_ENTER}, {kVK_Escape, MP_KEY_ESC},
|
|
|
|
{kVK_Delete, MP_KEY_BACKSPACE}, {kVK_Option, MP_KEY_BACKSPACE},
|
|
|
|
{kVK_Control, MP_KEY_BACKSPACE}, {kVK_Shift, MP_KEY_BACKSPACE},
|
|
|
|
{kVK_Tab, MP_KEY_TAB},
|
|
|
|
|
|
|
|
// cursor keys
|
|
|
|
{kVK_UpArrow, MP_KEY_UP}, {kVK_DownArrow, MP_KEY_DOWN},
|
|
|
|
{kVK_LeftArrow, MP_KEY_LEFT}, {kVK_RightArrow, MP_KEY_RIGHT},
|
|
|
|
|
|
|
|
// navigation block
|
|
|
|
{kVK_Help, MP_KEY_INSERT}, {kVK_ForwardDelete, MP_KEY_DELETE},
|
|
|
|
{kVK_Home, MP_KEY_HOME}, {kVK_End, MP_KEY_END},
|
|
|
|
{kVK_PageUp, MP_KEY_PAGE_UP}, {kVK_PageDown, MP_KEY_PAGE_DOWN},
|
|
|
|
|
|
|
|
// F-keys
|
|
|
|
{kVK_F1, MP_KEY_F + 1}, {kVK_F2, MP_KEY_F + 2}, {kVK_F3, MP_KEY_F + 3},
|
|
|
|
{kVK_F4, MP_KEY_F + 4}, {kVK_F5, MP_KEY_F + 5}, {kVK_F6, MP_KEY_F + 6},
|
|
|
|
{kVK_F7, MP_KEY_F + 7}, {kVK_F8, MP_KEY_F + 8}, {kVK_F9, MP_KEY_F + 9},
|
|
|
|
{kVK_F10, MP_KEY_F + 10}, {kVK_F11, MP_KEY_F + 11}, {kVK_F12, MP_KEY_F + 12},
|
|
|
|
|
|
|
|
// numpad
|
|
|
|
{kVK_ANSI_KeypadPlus, '+'}, {kVK_ANSI_KeypadMinus, '-'},
|
|
|
|
{kVK_ANSI_KeypadMultiply, '*'}, {kVK_ANSI_KeypadDivide, '/'},
|
|
|
|
{kVK_ANSI_KeypadEnter, MP_KEY_KPENTER},
|
|
|
|
{kVK_ANSI_KeypadDecimal, MP_KEY_KPDEC},
|
|
|
|
{kVK_ANSI_Keypad0, MP_KEY_KP0}, {kVK_ANSI_Keypad1, MP_KEY_KP1},
|
|
|
|
{kVK_ANSI_Keypad2, MP_KEY_KP2}, {kVK_ANSI_Keypad3, MP_KEY_KP3},
|
|
|
|
{kVK_ANSI_Keypad4, MP_KEY_KP4}, {kVK_ANSI_Keypad5, MP_KEY_KP5},
|
|
|
|
{kVK_ANSI_Keypad6, MP_KEY_KP6}, {kVK_ANSI_Keypad7, MP_KEY_KP7},
|
|
|
|
{kVK_ANSI_Keypad8, MP_KEY_KP8}, {kVK_ANSI_Keypad9, MP_KEY_KP9},
|
|
|
|
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int convert_key(unsigned key, unsigned charcode)
|
|
|
|
{
|
|
|
|
int mpkey = lookup_keymap_table(keymap, key);
|
|
|
|
if (mpkey)
|
|
|
|
return mpkey;
|
|
|
|
return charcode;
|
|
|
|
}
|
|
|
|
|
2013-06-04 20:12:23 +00:00
|
|
|
void cocoa_init_apple_remote(void)
|
2013-06-02 22:52:40 +00:00
|
|
|
{
|
2014-04-15 23:51:40 +00:00
|
|
|
[[EventsResponder sharedInstance] startAppleRemote];
|
2013-06-02 22:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 20:12:23 +00:00
|
|
|
void cocoa_uninit_apple_remote(void)
|
2013-06-02 22:52:40 +00:00
|
|
|
{
|
2014-04-15 23:51:40 +00:00
|
|
|
[[EventsResponder sharedInstance] stopAppleRemote];
|
2013-06-02 22:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 19:31:20 +00:00
|
|
|
static int mk_code(NSEvent *event)
|
|
|
|
{
|
|
|
|
return (([event data1] & 0xFFFF0000) >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mk_flags(NSEvent *event)
|
|
|
|
{
|
|
|
|
return ([event data1] & 0x0000FFFF);
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:46:28 +00:00
|
|
|
static int mk_down(NSEvent *event)
|
2016-12-15 22:06:04 +00:00
|
|
|
{
|
2013-06-06 19:31:20 +00:00
|
|
|
return (((mk_flags(event) & 0xFF00) >> 8)) == 0xA;
|
|
|
|
}
|
|
|
|
|
2013-06-04 20:12:23 +00:00
|
|
|
static CGEventRef tap_event_callback(CGEventTapProxy proxy, CGEventType type,
|
|
|
|
CGEventRef event, void *ctx)
|
|
|
|
{
|
|
|
|
EventsResponder *responder = ctx;
|
|
|
|
|
|
|
|
if (type == kCGEventTapDisabledByTimeout) {
|
|
|
|
// The Mach Port receiving the taps became unresponsive for some
|
|
|
|
// reason, restart listening on it.
|
|
|
|
[responder restartMediaKeys];
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == kCGEventTapDisabledByUserInput)
|
|
|
|
return event;
|
|
|
|
|
|
|
|
NSEvent *nse = [NSEvent eventWithCGEvent:event];
|
|
|
|
|
2016-09-20 21:30:54 +00:00
|
|
|
if ([nse type] != NSEventTypeSystemDefined || [nse subtype] != 8)
|
2013-06-04 20:12:23 +00:00
|
|
|
// This is not a media key
|
|
|
|
return event;
|
|
|
|
|
2013-06-06 19:31:20 +00:00
|
|
|
if (mk_down(nse) && [responder handleMediaKey:nse]) {
|
2013-06-04 20:12:23 +00:00
|
|
|
// Handled this event, return nil so that it is removed from the
|
|
|
|
// global queue.
|
|
|
|
return nil;
|
|
|
|
} else {
|
|
|
|
// Was a media key but we were not interested in it. Leave it in the
|
|
|
|
// global queue by returning the original event.
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 22:06:04 +00:00
|
|
|
void cocoa_init_media_keys(void)
|
|
|
|
{
|
2014-04-15 23:51:40 +00:00
|
|
|
[[EventsResponder sharedInstance] startMediaKeys];
|
2013-06-04 20:12:23 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 22:06:04 +00:00
|
|
|
void cocoa_uninit_media_keys(void)
|
|
|
|
{
|
2014-04-15 23:51:40 +00:00
|
|
|
[[EventsResponder sharedInstance] stopMediaKeys];
|
2013-06-02 15:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cocoa_put_key(int keycode)
|
|
|
|
{
|
2015-05-26 20:39:04 +00:00
|
|
|
[[EventsResponder sharedInstance] putKey:keycode];
|
2013-06-02 15:39:05 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 08:33:18 +00:00
|
|
|
void cocoa_put_key_with_modifiers(int keycode, int modifiers)
|
|
|
|
{
|
2014-04-15 23:51:40 +00:00
|
|
|
keycode |= [[EventsResponder sharedInstance] mapKeyModifiers:modifiers];
|
2013-07-21 08:33:18 +00:00
|
|
|
cocoa_put_key(keycode);
|
|
|
|
}
|
|
|
|
|
2014-04-16 02:33:58 +00:00
|
|
|
void cocoa_set_input_context(struct input_ctx *input_context)
|
|
|
|
{
|
2015-05-26 20:39:04 +00:00
|
|
|
[[EventsResponder sharedInstance] setInputContext:input_context];
|
2014-04-16 02:33:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 20:56:59 +00:00
|
|
|
static void wakeup(void *context)
|
|
|
|
{
|
|
|
|
[[EventsResponder sharedInstance] readEvents];
|
|
|
|
}
|
|
|
|
|
|
|
|
void cocoa_set_mpv_handle(struct mpv_handle *ctx)
|
|
|
|
{
|
2017-04-19 10:35:42 +00:00
|
|
|
if ([[EventsResponder sharedInstance] setMpvHandle:ctx]) {
|
|
|
|
mpv_observe_property(ctx, 0, "duration", MPV_FORMAT_DOUBLE);
|
|
|
|
mpv_observe_property(ctx, 0, "time-pos", MPV_FORMAT_DOUBLE);
|
|
|
|
mpv_observe_property(ctx, 0, "pause", MPV_FORMAT_FLAG);
|
|
|
|
mpv_set_wakeup_callback(ctx, wakeup, NULL);
|
|
|
|
}
|
2017-02-25 20:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 23:51:40 +00:00
|
|
|
@implementation EventsResponder
|
|
|
|
|
|
|
|
+ (EventsResponder *)sharedInstance
|
|
|
|
{
|
|
|
|
static EventsResponder *responder = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
responder = [EventsResponder new];
|
|
|
|
});
|
|
|
|
return responder;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
2015-05-26 20:47:11 +00:00
|
|
|
_input_lock = [NSCondition new];
|
2014-04-15 23:51:40 +00:00
|
|
|
}
|
|
|
|
return self;
|
2013-06-04 20:12:23 +00:00
|
|
|
}
|
2013-07-03 20:19:26 +00:00
|
|
|
|
2014-04-16 00:09:47 +00:00
|
|
|
- (void)waitForInputContext
|
|
|
|
{
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock lock];
|
2015-05-26 20:39:04 +00:00
|
|
|
while (!_inputContext)
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock wait];
|
|
|
|
[_input_lock unlock];
|
2014-04-16 00:09:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 19:46:28 +00:00
|
|
|
- (void)setInputContext:(struct input_ctx *)ctx
|
2014-04-16 00:09:47 +00:00
|
|
|
{
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock lock];
|
2014-04-16 00:09:47 +00:00
|
|
|
_inputContext = ctx;
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock signal];
|
|
|
|
[_input_lock unlock];
|
2014-04-16 00:09:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-26 20:39:04 +00:00
|
|
|
- (void)wakeup
|
2014-04-16 00:09:47 +00:00
|
|
|
{
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock lock];
|
2015-06-01 17:36:20 +00:00
|
|
|
if (_inputContext)
|
|
|
|
mp_input_wakeup(_inputContext);
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock unlock];
|
2015-05-26 20:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (bool)queueCommand:(char *)cmd
|
|
|
|
{
|
2015-05-26 20:47:11 +00:00
|
|
|
bool r = false;
|
|
|
|
[_input_lock lock];
|
|
|
|
if (_inputContext) {
|
|
|
|
mp_cmd_t *cmdt = mp_input_parse_cmd(_inputContext, bstr0(cmd), "");
|
|
|
|
mp_input_queue_cmd(_inputContext, cmdt);
|
|
|
|
r = true;
|
|
|
|
}
|
|
|
|
[_input_lock unlock];
|
|
|
|
return r;
|
2015-05-26 20:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)putKey:(int)keycode
|
|
|
|
{
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock lock];
|
2015-05-26 20:39:04 +00:00
|
|
|
if (_inputContext)
|
|
|
|
mp_input_put_key(_inputContext, keycode);
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock unlock];
|
2014-04-16 00:09:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-01 05:23:39 +00:00
|
|
|
- (BOOL)useAltGr
|
|
|
|
{
|
2015-05-26 20:47:11 +00:00
|
|
|
BOOL r = YES;
|
|
|
|
[_input_lock lock];
|
2015-05-26 20:39:04 +00:00
|
|
|
if (_inputContext)
|
2015-05-26 20:47:11 +00:00
|
|
|
r = mp_input_use_alt_gr(_inputContext);
|
|
|
|
[_input_lock unlock];
|
|
|
|
return r;
|
2013-12-01 05:23:39 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 20:56:59 +00:00
|
|
|
- (void)setIsApplication:(BOOL)isApplication
|
|
|
|
{
|
|
|
|
_is_application = isApplication;
|
|
|
|
}
|
|
|
|
|
2017-04-19 10:35:42 +00:00
|
|
|
- (BOOL)setMpvHandle:(struct mpv_handle *)ctx
|
2017-02-25 20:56:59 +00:00
|
|
|
{
|
|
|
|
if (_is_application) {
|
cocoa-cb: initial implementation via opengl-cb API
this is meant to replace the old and not properly working vo_gpu/opengl
cocoa backend in the future. the problems are various shortcomings of
Apple's opengl implementation and buggy behaviour in certain
circumstances that couldn't be properly worked around. there are also
certain regressions on newer macOS versions from 10.11 onwards.
- awful opengl performance with a none layer backed context
- huge amount of dropped frames with an early context flush
- flickering of system elements like the dock or volume indicator
- double buffering not properly working with a none layer backed context
- bad performance in fullscreen because of system optimisations
all the problems were caused by using a normal opengl context, that
seems somewhat abandoned by apple, and are fixed by using a layer backed
opengl context instead. problems that couldn't be fixed could be
properly worked around.
this has all features our old backend has sans the wid embedding,
the possibility to disable the automatic GPU switching and taking
screenshots of the window content. the first was deemed unnecessary by
me for now, since i just use the libmpv API that others can use anyway.
second is technically not possible atm because we have to pre-allocate
our opengl context at a time the config isn't read yet, so we can't get
the needed property. third one is a bit tricky because of deadlocking
and it needed to be in sync, hopefully i can work around that in the
future.
this also has at least one additional feature or eye-candy. a properly
working fullscreen animation with the native fs. also since this is a
direct port of the old backend of the parts that could be used, though
with adaptions and improvements, this looks a lot cleaner and easier to
understand.
some credit goes to @pigoz for the initial swift build support which
i could improve upon.
Fixes: #5478, #5393, #5152, #5151, #4615, #4476, #3978, #3746, #3739,
#2392, #2217
2018-02-12 11:28:19 +00:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
_ctx = ctx;
|
|
|
|
[NSApp setMpvHandle:ctx];
|
|
|
|
});
|
2017-04-19 10:35:42 +00:00
|
|
|
return YES;
|
2017-02-25 20:56:59 +00:00
|
|
|
} else {
|
2017-04-19 10:35:42 +00:00
|
|
|
mpv_detach_destroy(ctx);
|
|
|
|
return NO;
|
2017-02-25 20:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)readEvents
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
while (_ctx) {
|
|
|
|
mpv_event *event = mpv_wait_event(_ctx, 0);
|
|
|
|
if (event->event_id == MPV_EVENT_NONE)
|
|
|
|
break;
|
|
|
|
[self processEvent:event];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)processEvent:(struct mpv_event *)event
|
|
|
|
{
|
cocoa-cb: initial implementation via opengl-cb API
this is meant to replace the old and not properly working vo_gpu/opengl
cocoa backend in the future. the problems are various shortcomings of
Apple's opengl implementation and buggy behaviour in certain
circumstances that couldn't be properly worked around. there are also
certain regressions on newer macOS versions from 10.11 onwards.
- awful opengl performance with a none layer backed context
- huge amount of dropped frames with an early context flush
- flickering of system elements like the dock or volume indicator
- double buffering not properly working with a none layer backed context
- bad performance in fullscreen because of system optimisations
all the problems were caused by using a normal opengl context, that
seems somewhat abandoned by apple, and are fixed by using a layer backed
opengl context instead. problems that couldn't be fixed could be
properly worked around.
this has all features our old backend has sans the wid embedding,
the possibility to disable the automatic GPU switching and taking
screenshots of the window content. the first was deemed unnecessary by
me for now, since i just use the libmpv API that others can use anyway.
second is technically not possible atm because we have to pre-allocate
our opengl context at a time the config isn't read yet, so we can't get
the needed property. third one is a bit tricky because of deadlocking
and it needed to be in sync, hopefully i can work around that in the
future.
this also has at least one additional feature or eye-candy. a properly
working fullscreen animation with the native fs. also since this is a
direct port of the old backend of the parts that could be used, though
with adaptions and improvements, this looks a lot cleaner and easier to
understand.
some credit goes to @pigoz for the initial swift build support which
i could improve upon.
Fixes: #5478, #5393, #5152, #5151, #4615, #4476, #3978, #3746, #3739,
#2392, #2217
2018-02-12 11:28:19 +00:00
|
|
|
if(_is_application) {
|
|
|
|
[NSApp processEvent:event];
|
|
|
|
}
|
|
|
|
|
2017-02-25 20:56:59 +00:00
|
|
|
switch (event->event_id) {
|
|
|
|
case MPV_EVENT_SHUTDOWN: {
|
cocoa-cb: initial implementation via opengl-cb API
this is meant to replace the old and not properly working vo_gpu/opengl
cocoa backend in the future. the problems are various shortcomings of
Apple's opengl implementation and buggy behaviour in certain
circumstances that couldn't be properly worked around. there are also
certain regressions on newer macOS versions from 10.11 onwards.
- awful opengl performance with a none layer backed context
- huge amount of dropped frames with an early context flush
- flickering of system elements like the dock or volume indicator
- double buffering not properly working with a none layer backed context
- bad performance in fullscreen because of system optimisations
all the problems were caused by using a normal opengl context, that
seems somewhat abandoned by apple, and are fixed by using a layer backed
opengl context instead. problems that couldn't be fixed could be
properly worked around.
this has all features our old backend has sans the wid embedding,
the possibility to disable the automatic GPU switching and taking
screenshots of the window content. the first was deemed unnecessary by
me for now, since i just use the libmpv API that others can use anyway.
second is technically not possible atm because we have to pre-allocate
our opengl context at a time the config isn't read yet, so we can't get
the needed property. third one is a bit tricky because of deadlocking
and it needed to be in sync, hopefully i can work around that in the
future.
this also has at least one additional feature or eye-candy. a properly
working fullscreen animation with the native fs. also since this is a
direct port of the old backend of the parts that could be used, though
with adaptions and improvements, this looks a lot cleaner and easier to
understand.
some credit goes to @pigoz for the initial swift build support which
i could improve upon.
Fixes: #5478, #5393, #5152, #5151, #4615, #4476, #3978, #3746, #3739,
#2392, #2217
2018-02-12 11:28:19 +00:00
|
|
|
#if HAVE_MACOS_COCOA_CB
|
|
|
|
if ([(Application *)NSApp cocoaCB].isShuttingDown)
|
|
|
|
return;
|
|
|
|
#endif
|
2017-02-25 20:56:59 +00:00
|
|
|
mpv_detach_destroy(_ctx);
|
|
|
|
_ctx = nil;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-02 22:52:40 +00:00
|
|
|
- (void)startAppleRemote
|
|
|
|
{
|
2014-10-17 15:07:01 +00:00
|
|
|
#if HAVE_APPLE_REMOTE
|
2013-06-02 22:52:40 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2013-06-04 21:48:36 +00:00
|
|
|
self->_remote = [[HIDRemote alloc] init];
|
|
|
|
if (self->_remote) {
|
|
|
|
[self->_remote setDelegate:self];
|
|
|
|
[self->_remote startRemoteControl:kHIDRemoteModeExclusiveAuto];
|
2013-06-02 22:52:40 +00:00
|
|
|
}
|
|
|
|
});
|
2014-10-17 15:07:01 +00:00
|
|
|
#endif
|
2013-06-02 22:52:40 +00:00
|
|
|
}
|
2017-03-26 19:46:28 +00:00
|
|
|
|
2013-06-02 22:52:40 +00:00
|
|
|
- (void)stopAppleRemote
|
|
|
|
{
|
2014-10-17 15:07:01 +00:00
|
|
|
#if HAVE_APPLE_REMOTE
|
2013-06-02 22:52:40 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2013-06-04 21:48:36 +00:00
|
|
|
[self->_remote stopRemoteControl];
|
|
|
|
[self->_remote release];
|
2013-06-02 22:52:40 +00:00
|
|
|
});
|
2014-10-17 15:07:01 +00:00
|
|
|
#endif
|
2013-06-02 22:52:40 +00:00
|
|
|
}
|
2017-03-26 19:46:28 +00:00
|
|
|
|
2013-06-04 20:12:23 +00:00
|
|
|
- (void)restartMediaKeys
|
|
|
|
{
|
|
|
|
CGEventTapEnable(self->_mk_tap_port, true);
|
|
|
|
}
|
2017-03-26 19:46:28 +00:00
|
|
|
|
2017-09-05 21:29:06 +00:00
|
|
|
- (void)setHighestPriotityMediaKeysTap
|
|
|
|
{
|
|
|
|
if (self->_mk_tap_port == nil)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CGEventTapInformation *taps = ta_alloc_size(nil, sizeof(CGEventTapInformation));
|
|
|
|
uint32_t numTaps = 0;
|
|
|
|
CGError err = CGGetEventTapList(1, taps, &numTaps);
|
|
|
|
|
|
|
|
if (err == kCGErrorSuccess && numTaps > 0) {
|
|
|
|
pid_t processID = [NSProcessInfo processInfo].processIdentifier;
|
|
|
|
if (taps[0].tappingProcess != processID) {
|
|
|
|
[self stopMediaKeys];
|
|
|
|
[self startMediaKeys];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
talloc_free(taps);
|
|
|
|
}
|
|
|
|
|
2013-06-04 20:12:23 +00:00
|
|
|
- (void)startMediaKeys
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
// Install a Quartz Event Tap. This will notify mpv through the
|
|
|
|
// returned Mach Port and cause mpv to execute the `tap_event_callback`
|
|
|
|
// function.
|
|
|
|
self->_mk_tap_port = CGEventTapCreate(kCGSessionEventTap,
|
|
|
|
kCGHeadInsertEventTap,
|
|
|
|
kCGEventTapOptionDefault,
|
|
|
|
CGEventMaskBit(NX_SYSDEFINED),
|
|
|
|
tap_event_callback,
|
|
|
|
self);
|
|
|
|
|
|
|
|
assert(self->_mk_tap_port != nil);
|
|
|
|
|
|
|
|
NSMachPort *port = (NSMachPort *)self->_mk_tap_port;
|
|
|
|
[[NSRunLoop mainRunLoop] addPort:port forMode:NSRunLoopCommonModes];
|
|
|
|
});
|
|
|
|
}
|
2017-03-26 19:46:28 +00:00
|
|
|
|
2013-06-04 20:12:23 +00:00
|
|
|
- (void)stopMediaKeys
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
NSMachPort *port = (NSMachPort *)self->_mk_tap_port;
|
2017-09-05 21:29:06 +00:00
|
|
|
CGEventTapEnable(self->_mk_tap_port, false);
|
2013-06-04 20:12:23 +00:00
|
|
|
[[NSRunLoop mainRunLoop] removePort:port forMode:NSRunLoopCommonModes];
|
|
|
|
CFRelease(self->_mk_tap_port);
|
|
|
|
self->_mk_tap_port = nil;
|
|
|
|
});
|
|
|
|
}
|
2013-06-06 19:31:20 +00:00
|
|
|
|
|
|
|
- (BOOL)handleMediaKey:(NSEvent *)event
|
2013-06-02 16:23:18 +00:00
|
|
|
{
|
2013-11-04 07:11:32 +00:00
|
|
|
NSDictionary *keymapd = @{
|
2017-03-04 21:52:48 +00:00
|
|
|
@(NX_KEYTYPE_PLAY): @(MP_KEY_PLAY),
|
|
|
|
@(NX_KEYTYPE_REWIND): @(MP_KEY_PREV),
|
|
|
|
@(NX_KEYTYPE_FAST): @(MP_KEY_NEXT),
|
|
|
|
@(NX_KEYTYPE_PREVIOUS): @(MP_KEY_REWIND),
|
|
|
|
@(NX_KEYTYPE_NEXT): @(MP_KEY_FORWARD),
|
2013-06-04 20:12:23 +00:00
|
|
|
};
|
|
|
|
|
2013-06-06 19:31:20 +00:00
|
|
|
return [self handleKey:mk_code(event)
|
|
|
|
withMask:[self keyModifierMask:event]
|
2013-11-04 07:11:32 +00:00
|
|
|
andMapping:keymapd];
|
2013-06-02 16:23:18 +00:00
|
|
|
}
|
2013-06-06 19:31:20 +00:00
|
|
|
|
2013-06-02 22:52:40 +00:00
|
|
|
- (void)hidRemote:(HIDRemote *)remote
|
|
|
|
eventWithButton:(HIDRemoteButtonCode)buttonCode
|
|
|
|
isPressed:(BOOL)isPressed
|
|
|
|
fromHardwareWithAttributes:(NSMutableDictionary *)attributes
|
|
|
|
{
|
|
|
|
if (!isPressed) return;
|
|
|
|
|
2013-11-04 07:11:32 +00:00
|
|
|
NSDictionary *keymapd = @{
|
2013-06-02 22:52:40 +00:00
|
|
|
@(kHIDRemoteButtonCodePlay): @(MP_AR_PLAY),
|
|
|
|
@(kHIDRemoteButtonCodePlayHold): @(MP_AR_PLAY_HOLD),
|
|
|
|
@(kHIDRemoteButtonCodeCenter): @(MP_AR_CENTER),
|
|
|
|
@(kHIDRemoteButtonCodeCenterHold): @(MP_AR_CENTER_HOLD),
|
|
|
|
@(kHIDRemoteButtonCodeLeft): @(MP_AR_PREV),
|
|
|
|
@(kHIDRemoteButtonCodeLeftHold): @(MP_AR_PREV_HOLD),
|
|
|
|
@(kHIDRemoteButtonCodeRight): @(MP_AR_NEXT),
|
|
|
|
@(kHIDRemoteButtonCodeRightHold): @(MP_AR_NEXT_HOLD),
|
|
|
|
@(kHIDRemoteButtonCodeMenu): @(MP_AR_MENU),
|
|
|
|
@(kHIDRemoteButtonCodeMenuHold): @(MP_AR_MENU_HOLD),
|
|
|
|
@(kHIDRemoteButtonCodeUp): @(MP_AR_VUP),
|
|
|
|
@(kHIDRemoteButtonCodeUpHold): @(MP_AR_VUP_HOLD),
|
|
|
|
@(kHIDRemoteButtonCodeDown): @(MP_AR_VDOWN),
|
|
|
|
@(kHIDRemoteButtonCodeDownHold): @(MP_AR_VDOWN_HOLD),
|
|
|
|
};
|
|
|
|
|
2013-11-04 07:11:32 +00:00
|
|
|
[self handleKey:buttonCode withMask:0 andMapping:keymapd];
|
2013-06-05 19:39:59 +00:00
|
|
|
}
|
2013-06-06 19:31:20 +00:00
|
|
|
|
2013-07-21 08:33:18 +00:00
|
|
|
- (int)mapKeyModifiers:(int)cocoaModifiers
|
2013-06-06 19:31:20 +00:00
|
|
|
{
|
|
|
|
int mask = 0;
|
2016-09-20 21:30:54 +00:00
|
|
|
if (cocoaModifiers & NSEventModifierFlagShift)
|
2013-06-06 19:31:20 +00:00
|
|
|
mask |= MP_KEY_MODIFIER_SHIFT;
|
2016-09-20 21:30:54 +00:00
|
|
|
if (cocoaModifiers & NSEventModifierFlagControl)
|
2013-06-06 19:31:20 +00:00
|
|
|
mask |= MP_KEY_MODIFIER_CTRL;
|
2013-12-01 05:23:39 +00:00
|
|
|
if (LeftAltPressed(cocoaModifiers) ||
|
2015-03-04 09:21:36 +00:00
|
|
|
(RightAltPressed(cocoaModifiers) && ![self useAltGr]))
|
2013-06-06 19:31:20 +00:00
|
|
|
mask |= MP_KEY_MODIFIER_ALT;
|
2016-09-20 21:30:54 +00:00
|
|
|
if (cocoaModifiers & NSEventModifierFlagCommand)
|
2013-06-06 19:31:20 +00:00
|
|
|
mask |= MP_KEY_MODIFIER_META;
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2013-08-30 17:46:48 +00:00
|
|
|
- (int)mapTypeModifiers:(NSEventType)type
|
|
|
|
{
|
|
|
|
NSDictionary *map = @{
|
2016-09-20 21:30:54 +00:00
|
|
|
@(NSEventTypeKeyDown) : @(MP_KEY_STATE_DOWN),
|
|
|
|
@(NSEventTypeKeyUp) : @(MP_KEY_STATE_UP),
|
2013-08-30 17:46:48 +00:00
|
|
|
};
|
|
|
|
return [map[@(type)] intValue];
|
|
|
|
}
|
|
|
|
|
2013-07-21 08:33:18 +00:00
|
|
|
- (int)keyModifierMask:(NSEvent *)event
|
|
|
|
{
|
2013-08-30 17:46:48 +00:00
|
|
|
return [self mapKeyModifiers:[event modifierFlags]] |
|
|
|
|
[self mapTypeModifiers:[event type]];
|
2013-07-21 08:33:18 +00:00
|
|
|
}
|
|
|
|
|
2013-09-02 19:11:05 +00:00
|
|
|
-(BOOL)handleMPKey:(int)key withMask:(int)mask
|
2013-06-05 19:39:59 +00:00
|
|
|
{
|
2013-09-02 19:11:05 +00:00
|
|
|
if (key > 0) {
|
|
|
|
cocoa_put_key(key | mask);
|
2013-09-02 19:13:56 +00:00
|
|
|
if (mask & MP_KEY_STATE_UP)
|
|
|
|
cocoa_put_key(MP_INPUT_RELEASE_ALL);
|
2013-06-05 19:39:59 +00:00
|
|
|
return YES;
|
|
|
|
} else {
|
|
|
|
return NO;
|
|
|
|
}
|
2013-06-02 22:52:40 +00:00
|
|
|
}
|
2013-09-02 19:11:05 +00:00
|
|
|
|
|
|
|
-(BOOL)handleKey:(int)key withMask:(int)mask andMapping:(NSDictionary *)mapping
|
|
|
|
{
|
|
|
|
int mpkey = [mapping[@(key)] intValue];
|
2013-09-05 05:36:35 +00:00
|
|
|
return [self handleMPKey:mpkey withMask:mask];
|
2013-09-02 19:11:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSEvent*)handleKey:(NSEvent *)event
|
|
|
|
{
|
|
|
|
if ([event isARepeat]) return nil;
|
|
|
|
|
|
|
|
NSString *chars;
|
|
|
|
|
2016-12-15 22:06:04 +00:00
|
|
|
if ([self useAltGr] && RightAltPressed([event modifierFlags])) {
|
2013-09-02 19:11:05 +00:00
|
|
|
chars = [event characters];
|
2016-12-15 22:06:04 +00:00
|
|
|
} else {
|
2013-09-02 19:11:05 +00:00
|
|
|
chars = [event charactersIgnoringModifiers];
|
2016-12-15 22:06:04 +00:00
|
|
|
}
|
2013-09-02 19:11:05 +00:00
|
|
|
|
2016-02-13 11:19:45 +00:00
|
|
|
struct bstr t = bstr0([chars UTF8String]);
|
|
|
|
int key = convert_key([event keyCode], bstr_decode_utf8(t, &t));
|
2013-09-02 19:11:05 +00:00
|
|
|
|
2014-07-30 09:06:58 +00:00
|
|
|
if (key > -1)
|
2013-09-02 19:11:05 +00:00
|
|
|
[self handleMPKey:key withMask:[self keyModifierMask:event]];
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
2014-04-16 02:38:19 +00:00
|
|
|
|
2017-03-26 18:32:04 +00:00
|
|
|
- (bool)processKeyEvent:(NSEvent *)event
|
|
|
|
{
|
2017-03-26 23:29:45 +00:00
|
|
|
if (event.type == NSEventTypeKeyDown || event.type == NSEventTypeKeyUp){
|
|
|
|
if (![[NSApp mainMenu] performKeyEquivalent:event])
|
|
|
|
[self handleKey:event];
|
2017-03-26 18:32:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-16 02:38:19 +00:00
|
|
|
- (void)handleFilesArray:(NSArray *)files
|
|
|
|
{
|
2016-12-13 17:44:57 +00:00
|
|
|
enum mp_dnd_action action = [NSEvent modifierFlags] &
|
|
|
|
NSEventModifierFlagShift ? DND_APPEND : DND_REPLACE;
|
|
|
|
|
2014-04-16 02:38:19 +00:00
|
|
|
size_t num_files = [files count];
|
|
|
|
char **files_utf8 = talloc_array(NULL, char*, num_files);
|
2014-12-06 11:48:18 +00:00
|
|
|
[files enumerateObjectsUsingBlock:^(NSString *p, NSUInteger i, BOOL *_){
|
|
|
|
if ([p hasPrefix:@"file:///.file/id="])
|
|
|
|
p = [[NSURL URLWithString:p] path];
|
|
|
|
char *filename = (char *)[p UTF8String];
|
|
|
|
size_t bytes = [p lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
|
2014-04-16 02:38:19 +00:00
|
|
|
files_utf8[i] = talloc_memdup(files_utf8, filename, bytes + 1);
|
|
|
|
}];
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock lock];
|
|
|
|
if (_inputContext)
|
2016-12-13 17:44:57 +00:00
|
|
|
mp_event_drop_files(_inputContext, num_files, files_utf8, action);
|
2015-05-26 20:47:11 +00:00
|
|
|
[_input_lock unlock];
|
2014-04-16 02:38:19 +00:00
|
|
|
talloc_free(files_utf8);
|
|
|
|
}
|
|
|
|
|
2013-06-02 15:39:05 +00:00
|
|
|
@end
|