2013-02-23 17:28:22 +00:00
|
|
|
/*
|
|
|
|
* 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-02-23 17:28:22 +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:08:17 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-02-23 17:28:22 +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-02-23 17:28:22 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-21 19:46:20 +00:00
|
|
|
#include <stdio.h>
|
2017-02-25 20:56:59 +00:00
|
|
|
#include "config.h"
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2013-02-23 17:28:22 +00:00
|
|
|
|
2013-12-17 02:35:43 +00:00
|
|
|
#include "common/msg.h"
|
|
|
|
#include "input/input.h"
|
2017-02-25 20:56:59 +00:00
|
|
|
#include "player/client.h"
|
2018-02-16 12:07:15 +00:00
|
|
|
#include "options/m_config.h"
|
2019-12-22 23:02:23 +00:00
|
|
|
#include "options/options.h"
|
2013-02-23 17:28:22 +00:00
|
|
|
|
2014-04-15 23:51:40 +00:00
|
|
|
#import "osdep/macosx_application_objc.h"
|
|
|
|
#import "osdep/macosx_events_objc.h"
|
2014-10-19 21:32:34 +00:00
|
|
|
#include "osdep/threads.h"
|
2015-05-02 16:47:57 +00:00
|
|
|
#include "osdep/main-fn.h"
|
2013-02-23 17:28:22 +00:00
|
|
|
|
2017-02-25 20:56:59 +00:00
|
|
|
#if HAVE_MACOS_TOUCHBAR
|
|
|
|
#import "osdep/macosx_touchbar.h"
|
|
|
|
#endif
|
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
|
2017-02-25 20:56:59 +00:00
|
|
|
|
2013-09-07 13:37:02 +00:00
|
|
|
#define MPV_PROTOCOL @"mpv://"
|
|
|
|
|
2018-02-16 12:07:15 +00:00
|
|
|
#define OPT_BASE_STRUCT struct macos_opts
|
|
|
|
const struct m_sub_options macos_conf = {
|
|
|
|
.opts = (const struct m_option[]) {
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"macos-title-bar-appearance", OPT_CHOICE(macos_title_bar_appearance,
|
|
|
|
{"auto", 0}, {"aqua", 1}, {"darkAqua", 2},
|
|
|
|
{"vibrantLight", 3}, {"vibrantDark", 4},
|
|
|
|
{"aquaHighContrast", 5}, {"darkAquaHighContrast", 6},
|
|
|
|
{"vibrantLightHighContrast", 7},
|
|
|
|
{"vibrantDarkHighContrast", 8})},
|
|
|
|
{"macos-title-bar-material", OPT_CHOICE(macos_title_bar_material,
|
|
|
|
{"titlebar", 0}, {"selection", 1}, {"menu", 2},
|
|
|
|
{"popover", 3}, {"sidebar", 4}, {"headerView", 5},
|
|
|
|
{"sheet", 6}, {"windowBackground", 7}, {"hudWindow", 8},
|
|
|
|
{"fullScreen", 9}, {"toolTip", 10}, {"contentBackground", 11},
|
|
|
|
{"underWindowBackground", 12}, {"underPageBackground", 13},
|
|
|
|
{"dark", 14}, {"light", 15}, {"mediumLight", 16},
|
|
|
|
{"ultraDark", 17})},
|
|
|
|
{"macos-title-bar-color", OPT_COLOR(macos_title_bar_color)},
|
|
|
|
{"macos-fs-animation-duration",
|
|
|
|
OPT_CHOICE(macos_fs_animation_duration, {"default", -1}),
|
|
|
|
M_RANGE(0, 1000)},
|
2023-02-20 03:32:50 +00:00
|
|
|
{"macos-force-dedicated-gpu", OPT_BOOL(macos_force_dedicated_gpu)},
|
2020-09-15 14:44:12 +00:00
|
|
|
{"macos-app-activation-policy", OPT_CHOICE(macos_app_activation_policy,
|
|
|
|
{"regular", 0}, {"accessory", 1}, {"prohibited", 2})},
|
2020-12-13 16:13:18 +00:00
|
|
|
{"macos-geometry-calculation", OPT_CHOICE(macos_geometry_calculation,
|
|
|
|
{"visible", FRAME_VISIBLE}, {"whole", FRAME_WHOLE})},
|
2023-09-30 14:01:04 +00:00
|
|
|
{"macos-render-timer", OPT_CHOICE(macos_render_timer,
|
|
|
|
{"callback", RENDER_TIMER_CALLBACK}, {"precise", RENDER_TIMER_PRECISE},
|
|
|
|
{"system", RENDER_TIMER_SYSTEM})},
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"cocoa-cb-sw-renderer", OPT_CHOICE(cocoa_cb_sw_renderer,
|
|
|
|
{"auto", -1}, {"no", 0}, {"yes", 1})},
|
2023-02-20 03:32:50 +00:00
|
|
|
{"cocoa-cb-10bit-context", OPT_BOOL(cocoa_cb_10bit_context)},
|
2018-02-16 12:07:15 +00:00
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct macos_opts),
|
2018-02-27 23:46:16 +00:00
|
|
|
.defaults = &(const struct macos_opts){
|
2019-03-29 19:39:42 +00:00
|
|
|
.macos_title_bar_color = {0, 0, 0, 0},
|
2018-02-27 23:46:16 +00:00
|
|
|
.macos_fs_animation_duration = -1,
|
2018-07-22 21:07:32 +00:00
|
|
|
.cocoa_cb_sw_renderer = -1,
|
2023-02-20 03:32:50 +00:00
|
|
|
.cocoa_cb_10bit_context = true
|
2018-02-27 23:46:16 +00:00
|
|
|
},
|
2018-02-16 12:07:15 +00:00
|
|
|
};
|
|
|
|
|
2015-05-02 16:09:01 +00:00
|
|
|
// Whether the NSApplication singleton was created. If this is false, we are
|
|
|
|
// running in libmpv mode, and cocoa_main() was never called.
|
|
|
|
static bool application_instantiated;
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
static mp_thread playback_thread_id;
|
2013-02-23 17:28:22 +00:00
|
|
|
|
2014-04-15 23:51:40 +00:00
|
|
|
@interface Application ()
|
|
|
|
{
|
|
|
|
EventsResponder *_eventsResponder;
|
|
|
|
}
|
|
|
|
|
2013-02-23 17:28:22 +00:00
|
|
|
@end
|
|
|
|
|
2015-05-02 16:09:01 +00:00
|
|
|
static Application *mpv_shared_app(void)
|
2013-06-02 15:39:05 +00:00
|
|
|
{
|
|
|
|
return (Application *)[Application sharedApplication];
|
|
|
|
}
|
2013-02-23 17:28:22 +00:00
|
|
|
|
2015-05-02 16:09:01 +00:00
|
|
|
static void terminate_cocoa_application(void)
|
|
|
|
{
|
2020-02-22 11:22:16 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[NSApp hide:NSApp];
|
|
|
|
[NSApp terminate:NSApp];
|
|
|
|
});
|
2015-05-02 16:09:01 +00:00
|
|
|
}
|
|
|
|
|
2013-06-02 15:39:05 +00:00
|
|
|
@implementation Application
|
2018-01-20 14:54:27 +00:00
|
|
|
@synthesize menuBar = _menu_bar;
|
2014-12-07 20:39:26 +00:00
|
|
|
@synthesize openCount = _open_count;
|
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
|
|
|
@synthesize cocoaCB = _cocoa_cb;
|
2013-06-02 15:39:05 +00:00
|
|
|
|
2013-06-02 15:58:48 +00:00
|
|
|
- (void)sendEvent:(NSEvent *)event
|
|
|
|
{
|
2017-07-29 20:19:34 +00:00
|
|
|
if ([self modalWindow] || ![_eventsResponder processKeyEvent:event])
|
2017-03-26 18:32:04 +00:00
|
|
|
[super sendEvent:event];
|
2015-05-26 20:39:04 +00:00
|
|
|
[_eventsResponder wakeup];
|
2013-06-02 15:58:48 +00:00
|
|
|
}
|
|
|
|
|
2013-02-23 17:28:22 +00:00
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
2014-04-15 23:51:40 +00:00
|
|
|
_eventsResponder = [EventsResponder sharedInstance];
|
2013-09-07 09:49:04 +00:00
|
|
|
|
|
|
|
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
|
|
|
|
[em setEventHandler:self
|
|
|
|
andSelector:@selector(getUrl:withReplyEvent:)
|
|
|
|
forEventClass:kInternetEventClass
|
|
|
|
andEventID:kAEGetURL];
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-09-07 09:49:04 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
|
|
|
|
[em removeEventHandlerForEventClass:kInternetEventClass
|
|
|
|
andEventID:kAEGetURL];
|
2017-02-15 19:41:16 +00:00
|
|
|
[em removeEventHandlerForEventClass:kCoreEventClass
|
|
|
|
andEventID:kAEQuitApplication];
|
2013-09-07 09:49:04 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static const char macosx_icon[] =
|
2023-07-23 22:54:35 +00:00
|
|
|
#include "TOOLS/osxbundle/icon.icns.inc"
|
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
|
|
|
;
|
|
|
|
|
|
|
|
- (NSImage *)getMPVIcon
|
|
|
|
{
|
2018-07-01 09:56:08 +00:00
|
|
|
// The C string contains a trailing null, so we strip it away
|
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
|
|
|
NSData *icon_data = [NSData dataWithBytesNoCopy:(void *)macosx_icon
|
2018-07-01 09:56:08 +00:00
|
|
|
length:sizeof(macosx_icon) - 1
|
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
|
|
|
freeWhenDone:NO];
|
|
|
|
return [[NSImage alloc] initWithData:icon_data];
|
|
|
|
}
|
|
|
|
|
2017-02-25 20:56:59 +00:00
|
|
|
#if HAVE_MACOS_TOUCHBAR
|
|
|
|
- (NSTouchBar *)makeTouchBar
|
|
|
|
{
|
|
|
|
TouchBar *tBar = [[TouchBar alloc] init];
|
|
|
|
[tBar setApp:self];
|
|
|
|
tBar.delegate = tBar;
|
|
|
|
tBar.customizationIdentifier = customID;
|
|
|
|
tBar.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar];
|
|
|
|
tBar.customizationAllowedItemIdentifiers = @[play, seekBar, previousItem,
|
|
|
|
nextItem, previousChapter, nextChapter, cycleAudio, cycleSubtitle,
|
|
|
|
currentPosition, timeLeft];
|
|
|
|
return tBar;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
- (void)processEvent:(struct mpv_event *)event
|
|
|
|
{
|
|
|
|
#if HAVE_MACOS_TOUCHBAR
|
2023-11-09 17:57:41 +00:00
|
|
|
[(TouchBar *)self.touchBar processEvent:event];
|
2017-02-25 20:56:59 +00:00
|
|
|
#endif
|
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 (_cocoa_cb) {
|
|
|
|
[_cocoa_cb processEvent:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMpvHandle:(struct mpv_handle *)ctx
|
|
|
|
{
|
2018-06-06 15:04:40 +00:00
|
|
|
#if HAVE_MACOS_COCOA_CB
|
|
|
|
[NSApp setCocoaCB:[[CocoaCB alloc] init:ctx]];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-11-23 16:44:02 +00:00
|
|
|
+ (const struct m_sub_options *)getMacOSConf
|
2018-06-06 15:04:40 +00:00
|
|
|
{
|
|
|
|
return &macos_conf;
|
2017-02-25 20:56:59 +00:00
|
|
|
}
|
|
|
|
|
2023-11-23 16:44:02 +00:00
|
|
|
+ (const struct m_sub_options *)getVoSubConf
|
2019-11-30 22:47:52 +00:00
|
|
|
{
|
|
|
|
return &vo_sub_opts;
|
|
|
|
}
|
|
|
|
|
2017-02-25 20:56:59 +00:00
|
|
|
- (void)queueCommand:(char *)cmd
|
|
|
|
{
|
|
|
|
[_eventsResponder queueCommand:cmd];
|
|
|
|
}
|
|
|
|
|
2013-06-02 15:39:05 +00:00
|
|
|
- (void)stopMPV:(char *)cmd
|
2013-05-23 07:44:56 +00:00
|
|
|
{
|
2015-05-26 20:39:04 +00:00
|
|
|
if (![_eventsResponder queueCommand:cmd])
|
2013-05-12 18:29:23 +00:00
|
|
|
terminate_cocoa_application();
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 19:41:16 +00:00
|
|
|
- (void)applicationWillFinishLaunching:(NSNotification *)notification
|
2016-12-15 22:06:04 +00:00
|
|
|
{
|
2017-02-15 19:41:16 +00:00
|
|
|
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
|
|
|
|
[em setEventHandler:self
|
|
|
|
andSelector:@selector(handleQuitEvent:withReplyEvent:)
|
|
|
|
forEventClass:kCoreEventClass
|
|
|
|
andEventID:kAEQuitApplication];
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 19:41:16 +00:00
|
|
|
- (void)handleQuitEvent:(NSAppleEventDescriptor *)event
|
|
|
|
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
|
2013-02-23 17:28:22 +00:00
|
|
|
{
|
2017-07-29 13:26:58 +00:00
|
|
|
[self stopMPV:"quit"];
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 09:49:04 +00:00
|
|
|
- (void)getUrl:(NSAppleEventDescriptor *)event
|
|
|
|
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
|
|
|
|
{
|
|
|
|
NSString *url =
|
|
|
|
[[event paramDescriptorForKeyword:keyDirectObject] stringValue];
|
|
|
|
|
2013-09-07 13:37:02 +00:00
|
|
|
url = [url stringByReplacingOccurrencesOfString:MPV_PROTOCOL
|
|
|
|
withString:@""
|
|
|
|
options:NSAnchoredSearch
|
|
|
|
range:NSMakeRange(0, [MPV_PROTOCOL length])];
|
|
|
|
|
2014-12-02 21:27:33 +00:00
|
|
|
url = [url stringByRemovingPercentEncoding];
|
2014-12-06 11:48:18 +00:00
|
|
|
[_eventsResponder handleFilesArray:@[url]];
|
2013-09-07 09:49:04 +00:00
|
|
|
}
|
|
|
|
|
2013-02-23 17:28:22 +00:00
|
|
|
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
|
|
|
|
{
|
2014-12-07 20:39:26 +00:00
|
|
|
if (mpv_shared_app().openCount > 0) {
|
|
|
|
mpv_shared_app().openCount--;
|
|
|
|
return;
|
|
|
|
}
|
2017-07-29 20:19:34 +00:00
|
|
|
[self openFiles:filenames];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)openFiles:(NSArray *)filenames
|
|
|
|
{
|
2014-01-19 23:06:07 +00:00
|
|
|
SEL cmpsel = @selector(localizedStandardCompare:);
|
2014-12-06 11:48:18 +00:00
|
|
|
NSArray *files = [filenames sortedArrayUsingSelector:cmpsel];
|
|
|
|
[_eventsResponder handleFilesArray:files];
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2018-06-06 15:04:40 +00:00
|
|
|
struct playback_thread_ctx {
|
|
|
|
int *argc;
|
|
|
|
char ***argv;
|
|
|
|
};
|
|
|
|
|
2014-12-06 11:48:18 +00:00
|
|
|
static void cocoa_run_runloop(void)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
[NSApp run];
|
|
|
|
[pool drain];
|
|
|
|
}
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
static MP_THREAD_VOID playback_thread(void *ctx_obj)
|
2018-06-06 15:04:40 +00:00
|
|
|
{
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_thread_set_name("core/playback");
|
2018-06-06 15:04:40 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
struct playback_thread_ctx *ctx = (struct playback_thread_ctx*) ctx_obj;
|
|
|
|
int r = mpv_main(*ctx->argc, *ctx->argv);
|
|
|
|
terminate_cocoa_application();
|
|
|
|
// normally never reached - unless the cocoa mainloop hasn't started yet
|
|
|
|
exit(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-23 17:28:22 +00:00
|
|
|
void cocoa_register_menu_item_action(MPMenuKey key, void* action)
|
|
|
|
{
|
2015-05-02 16:09:01 +00:00
|
|
|
if (application_instantiated)
|
2017-07-29 13:26:58 +00:00
|
|
|
[[NSApp menuBar] registerSelector:(SEL)action forKey:key];
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2014-12-06 11:48:18 +00:00
|
|
|
static void init_cocoa_application(bool regular)
|
2013-02-23 17:28:22 +00:00
|
|
|
{
|
2013-06-02 15:39:05 +00:00
|
|
|
NSApp = mpv_shared_app();
|
|
|
|
[NSApp setDelegate:NSApp];
|
2017-07-29 13:26:58 +00:00
|
|
|
[NSApp setMenuBar:[[MenuBar alloc] init]];
|
2014-12-05 21:58:02 +00:00
|
|
|
|
|
|
|
// Will be set to Regular from cocoa_common during UI creation so that we
|
|
|
|
// don't create an icon when playing audio only files.
|
2014-12-06 11:48:18 +00:00
|
|
|
[NSApp setActivationPolicy: regular ?
|
|
|
|
NSApplicationActivationPolicyRegular :
|
|
|
|
NSApplicationActivationPolicyAccessory];
|
2013-05-26 20:03:16 +00:00
|
|
|
|
|
|
|
atexit_b(^{
|
|
|
|
// Because activation policy has just been set to behave like a real
|
|
|
|
// application, that policy must be reset on exit to prevent, among
|
|
|
|
// other things, the menubar created here from remaining on screen.
|
2020-02-22 11:22:16 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];
|
|
|
|
});
|
2013-05-26 20:03:16 +00:00
|
|
|
});
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 20:09:37 +00:00
|
|
|
static bool bundle_started_from_finder()
|
2013-09-16 17:35:01 +00:00
|
|
|
{
|
2023-08-10 20:09:37 +00:00
|
|
|
NSString* bundle = [[[NSProcessInfo processInfo] environment] objectForKey:@"MPVBUNDLE"];
|
|
|
|
return [bundle isEqual:@"true"];
|
2017-09-29 16:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_psn_argument(char *arg_to_check)
|
|
|
|
{
|
|
|
|
NSString *arg = [NSString stringWithUTF8String:arg_to_check];
|
|
|
|
return [arg hasPrefix:@"-psn_"];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_bundle(int *argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (*argc > 1 && is_psn_argument(argv[1])) {
|
|
|
|
*argc = 1;
|
|
|
|
argv[1] = NULL;
|
|
|
|
}
|
2013-02-23 17:28:22 +00:00
|
|
|
|
2017-09-29 16:25:54 +00:00
|
|
|
NSDictionary *env = [[NSProcessInfo processInfo] environment];
|
|
|
|
NSString *path_bundle = [env objectForKey:@"PATH"];
|
2017-11-11 18:21:21 +00:00
|
|
|
NSString *path_new = [NSString stringWithFormat:@"%@:%@:%@:%@:%@",
|
2017-09-29 16:25:54 +00:00
|
|
|
path_bundle,
|
|
|
|
@"/usr/local/bin",
|
2017-10-07 20:18:22 +00:00
|
|
|
@"/usr/local/sbin",
|
|
|
|
@"/opt/local/bin",
|
|
|
|
@"/opt/local/sbin"];
|
2017-09-29 16:25:54 +00:00
|
|
|
setenv("PATH", [path_new UTF8String], 1);
|
2013-09-12 06:26:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 16:47:57 +00:00
|
|
|
int cocoa_main(int argc, char *argv[])
|
2013-02-23 17:28:22 +00:00
|
|
|
{
|
2014-12-06 11:48:18 +00:00
|
|
|
@autoreleasepool {
|
2015-05-02 16:09:01 +00:00
|
|
|
application_instantiated = true;
|
2017-02-25 20:56:59 +00:00
|
|
|
[[EventsResponder sharedInstance] setIsApplication:YES];
|
2015-05-02 16:09:01 +00:00
|
|
|
|
2018-06-06 15:04:40 +00:00
|
|
|
struct playback_thread_ctx ctx = {0};
|
|
|
|
ctx.argc = &argc;
|
|
|
|
ctx.argv = &argv;
|
2013-02-23 17:28:22 +00:00
|
|
|
|
2023-08-10 20:09:37 +00:00
|
|
|
if (bundle_started_from_finder()) {
|
2017-09-29 16:17:32 +00:00
|
|
|
setup_bundle(&argc, argv);
|
2014-12-06 11:48:18 +00:00
|
|
|
init_cocoa_application(true);
|
|
|
|
} else {
|
2014-12-07 20:39:26 +00:00
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
if (argv[i][0] != '-')
|
|
|
|
mpv_shared_app().openCount++;
|
2014-12-06 11:48:18 +00:00
|
|
|
init_cocoa_application(false);
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_thread_create(&playback_thread_id, playback_thread, &ctx);
|
2018-06-06 15:04:40 +00:00
|
|
|
[[EventsResponder sharedInstance] waitForInputContext];
|
2014-12-06 11:48:18 +00:00
|
|
|
cocoa_run_runloop();
|
|
|
|
|
|
|
|
// This should never be reached: cocoa_run_runloop blocks until the
|
|
|
|
// process is quit
|
|
|
|
fprintf(stderr, "There was either a problem "
|
|
|
|
"initializing Cocoa or the Runloop was stopped unexpectedly. "
|
|
|
|
"Please report this issues to a developer.\n");
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_thread_join(playback_thread_id);
|
2014-12-06 11:48:18 +00:00
|
|
|
return 1;
|
2013-02-23 17:28:22 +00:00
|
|
|
}
|
|
|
|
}
|