mirror of
https://github.com/mpv-player/mpv
synced 2025-04-07 10:02:50 +00:00
cocoa: use --idle when running inside bundle
Previously when using the bundle we used a custom bizarro thing to wait for events. Just use `--idle` and greatly simplify the code.
This commit is contained in:
parent
429fe85c48
commit
685b8b7a00
TOOLS/osxbundle/mpv.app/Contents/Resources
osdep
video/out
@ -1 +1,3 @@
|
|||||||
screenshot-template=~/Desktop/shot%n
|
screenshot-template=~/Desktop/shot%n
|
||||||
|
quiet
|
||||||
|
idle
|
||||||
|
@ -32,18 +32,7 @@ typedef enum {
|
|||||||
|
|
||||||
// multithreaded wrapper for mpv_main
|
// multithreaded wrapper for mpv_main
|
||||||
int cocoa_main(mpv_main_fn mpv_main, int argc, char *argv[]);
|
int cocoa_main(mpv_main_fn mpv_main, int argc, char *argv[]);
|
||||||
|
|
||||||
void cocoa_register_menu_item_action(MPMenuKey key, void* action);
|
void cocoa_register_menu_item_action(MPMenuKey key, void* action);
|
||||||
|
|
||||||
// initializes Cocoa application
|
|
||||||
void init_cocoa_application(void);
|
|
||||||
void terminate_cocoa_application(void);
|
void terminate_cocoa_application(void);
|
||||||
|
|
||||||
// Runs the Cocoa Main Event Loop
|
|
||||||
void cocoa_run_runloop(void);
|
|
||||||
void cocoa_stop_runloop(void);
|
|
||||||
void cocoa_post_fake_event(void);
|
|
||||||
|
|
||||||
void macosx_finder_args_preinit(int *argc, char ***argv);
|
|
||||||
|
|
||||||
#endif /* MPV_MACOSX_APPLICATION */
|
#endif /* MPV_MACOSX_APPLICATION */
|
||||||
|
@ -48,7 +48,6 @@ static pthread_t playback_thread_id;
|
|||||||
- (NSMenu *)appleMenuWithMainMenu:(NSMenu *)mainMenu;
|
- (NSMenu *)appleMenuWithMainMenu:(NSMenu *)mainMenu;
|
||||||
- (NSMenu *)movieMenu;
|
- (NSMenu *)movieMenu;
|
||||||
- (NSMenu *)windowMenu;
|
- (NSMenu *)windowMenu;
|
||||||
- (void)handleFiles;
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface NSApplication (NiblessAdditions)
|
@interface NSApplication (NiblessAdditions)
|
||||||
@ -61,10 +60,6 @@ Application *mpv_shared_app(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
@implementation Application
|
@implementation Application
|
||||||
@synthesize files = _files;
|
|
||||||
@synthesize argumentsList = _arguments_list;
|
|
||||||
@synthesize willStopOnOpenEvent = _will_stop_on_open_event;
|
|
||||||
|
|
||||||
@synthesize menuItems = _menu_items;
|
@synthesize menuItems = _menu_items;
|
||||||
|
|
||||||
- (void)sendEvent:(NSEvent *)event
|
- (void)sendEvent:(NSEvent *)event
|
||||||
@ -79,10 +74,7 @@ Application *mpv_shared_app(void)
|
|||||||
{
|
{
|
||||||
if (self = [super init]) {
|
if (self = [super init]) {
|
||||||
self.menuItems = [[[NSMutableDictionary alloc] init] autorelease];
|
self.menuItems = [[[NSMutableDictionary alloc] init] autorelease];
|
||||||
self.files = nil;
|
|
||||||
self.argumentsList = [[[NSMutableArray alloc] init] autorelease];
|
|
||||||
_eventsResponder = [EventsResponder sharedInstance];
|
_eventsResponder = [EventsResponder sharedInstance];
|
||||||
self.willStopOnOpenEvent = NO;
|
|
||||||
|
|
||||||
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
|
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
|
||||||
[em setEventHandler:self
|
[em setEventHandler:self
|
||||||
@ -234,47 +226,14 @@ Application *mpv_shared_app(void)
|
|||||||
range:NSMakeRange(0, [MPV_PROTOCOL length])];
|
range:NSMakeRange(0, [MPV_PROTOCOL length])];
|
||||||
|
|
||||||
url = [url stringByRemovingPercentEncoding];
|
url = [url stringByRemovingPercentEncoding];
|
||||||
|
[_eventsResponder handleFilesArray:@[url]];
|
||||||
self.files = @[url];
|
|
||||||
|
|
||||||
if (self.willStopOnOpenEvent) {
|
|
||||||
self.willStopOnOpenEvent = NO;
|
|
||||||
cocoa_stop_runloop();
|
|
||||||
} else {
|
|
||||||
[self handleFiles];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
|
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
|
||||||
{
|
{
|
||||||
Application *app = mpv_shared_app();
|
|
||||||
NSMutableArray *filesToOpen = [[[NSMutableArray alloc] init] autorelease];
|
|
||||||
|
|
||||||
[filenames enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL *_) {
|
|
||||||
NSInteger place = [app.argumentsList indexOfObject:obj];
|
|
||||||
if (place == NSNotFound) {
|
|
||||||
// Proper new event ^_^
|
|
||||||
[filesToOpen addObject:obj];
|
|
||||||
} else {
|
|
||||||
// This file was already opened from the CLI. Cocoa is trying to
|
|
||||||
// open it again using events. Ignore it!
|
|
||||||
[app.argumentsList removeObjectAtIndex:place];
|
|
||||||
}
|
|
||||||
}];
|
|
||||||
|
|
||||||
SEL cmpsel = @selector(localizedStandardCompare:);
|
SEL cmpsel = @selector(localizedStandardCompare:);
|
||||||
self.files = [filesToOpen sortedArrayUsingSelector:cmpsel];
|
NSArray *files = [filenames sortedArrayUsingSelector:cmpsel];
|
||||||
if (self.willStopOnOpenEvent) {
|
[_eventsResponder handleFilesArray:files];
|
||||||
self.willStopOnOpenEvent = NO;
|
|
||||||
cocoa_stop_runloop();
|
|
||||||
} else {
|
|
||||||
[self handleFiles];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)handleFiles
|
|
||||||
{
|
|
||||||
[_eventsResponder handleFilesArray:self.files];
|
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@ -284,6 +243,26 @@ struct playback_thread_ctx {
|
|||||||
char ***argv;
|
char ***argv;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void terminate_cocoa_application(void)
|
||||||
|
{
|
||||||
|
[NSApp hide:NSApp];
|
||||||
|
[NSApp terminate:NSApp];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cocoa_run_runloop(void)
|
||||||
|
{
|
||||||
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||||
|
[NSApp run];
|
||||||
|
[pool drain];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cocoa_stop_runloop(void)
|
||||||
|
{
|
||||||
|
[NSApp performSelectorOnMainThread:@selector(stop:)
|
||||||
|
withObject:nil
|
||||||
|
waitUntilDone:true];
|
||||||
|
}
|
||||||
|
|
||||||
static void *playback_thread(void *ctx_obj)
|
static void *playback_thread(void *ctx_obj)
|
||||||
{
|
{
|
||||||
mpthread_set_name("playback core (OSX)");
|
mpthread_set_name("playback core (OSX)");
|
||||||
@ -295,38 +274,12 @@ static void *playback_thread(void *ctx_obj)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int cocoa_main(mpv_main_fn mpv_main, int argc, char *argv[])
|
|
||||||
{
|
|
||||||
@autoreleasepool {
|
|
||||||
struct playback_thread_ctx ctx = {0};
|
|
||||||
ctx.mpv_main = mpv_main;
|
|
||||||
ctx.argc = &argc;
|
|
||||||
ctx.argv = &argv;
|
|
||||||
|
|
||||||
init_cocoa_application();
|
|
||||||
macosx_finder_args_preinit(&argc, &argv);
|
|
||||||
pthread_create(&playback_thread_id, NULL, playback_thread, &ctx);
|
|
||||||
|
|
||||||
[[EventsResponder sharedInstance] waitForInputContext];
|
|
||||||
|
|
||||||
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");
|
|
||||||
pthread_join(playback_thread_id, NULL);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void cocoa_register_menu_item_action(MPMenuKey key, void* action)
|
void cocoa_register_menu_item_action(MPMenuKey key, void* action)
|
||||||
{
|
{
|
||||||
[NSApp registerSelector:(SEL)action forKey:key];
|
[NSApp registerSelector:(SEL)action forKey:key];
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_cocoa_application(void)
|
static void init_cocoa_application(bool regular)
|
||||||
{
|
{
|
||||||
NSApp = mpv_shared_app();
|
NSApp = mpv_shared_app();
|
||||||
[NSApp setDelegate:NSApp];
|
[NSApp setDelegate:NSApp];
|
||||||
@ -334,7 +287,9 @@ void init_cocoa_application(void)
|
|||||||
|
|
||||||
// Will be set to Regular from cocoa_common during UI creation so that we
|
// Will be set to Regular from cocoa_common during UI creation so that we
|
||||||
// don't create an icon when playing audio only files.
|
// don't create an icon when playing audio only files.
|
||||||
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
|
[NSApp setActivationPolicy: regular ?
|
||||||
|
NSApplicationActivationPolicyRegular :
|
||||||
|
NSApplicationActivationPolicyAccessory];
|
||||||
|
|
||||||
atexit_b(^{
|
atexit_b(^{
|
||||||
// Because activation policy has just been set to behave like a real
|
// Because activation policy has just been set to behave like a real
|
||||||
@ -344,47 +299,6 @@ void init_cocoa_application(void)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void terminate_cocoa_application(void)
|
|
||||||
{
|
|
||||||
[NSApp hide:NSApp];
|
|
||||||
[NSApp terminate:NSApp];
|
|
||||||
}
|
|
||||||
|
|
||||||
void cocoa_run_runloop()
|
|
||||||
{
|
|
||||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
||||||
[NSApp run];
|
|
||||||
[pool drain];
|
|
||||||
}
|
|
||||||
|
|
||||||
void cocoa_stop_runloop(void)
|
|
||||||
{
|
|
||||||
[NSApp performSelectorOnMainThread:@selector(stop:)
|
|
||||||
withObject:nil
|
|
||||||
waitUntilDone:true];
|
|
||||||
cocoa_post_fake_event();
|
|
||||||
}
|
|
||||||
|
|
||||||
void cocoa_post_fake_event(void)
|
|
||||||
{
|
|
||||||
NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined
|
|
||||||
location:NSMakePoint(0,0)
|
|
||||||
modifierFlags:0
|
|
||||||
timestamp:0.0
|
|
||||||
windowNumber:0
|
|
||||||
context:nil
|
|
||||||
subtype:0
|
|
||||||
data1:0
|
|
||||||
data2:0];
|
|
||||||
[NSApp postEvent:event atStart:NO];
|
|
||||||
}
|
|
||||||
|
|
||||||
static void macosx_wait_fileopen_events()
|
|
||||||
{
|
|
||||||
mpv_shared_app().willStopOnOpenEvent = YES;
|
|
||||||
cocoa_run_runloop(); // block until done
|
|
||||||
}
|
|
||||||
|
|
||||||
static void macosx_redirect_output_to_logfile(const char *filename)
|
static void macosx_redirect_output_to_logfile(const char *filename)
|
||||||
{
|
{
|
||||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||||
@ -444,30 +358,33 @@ static bool bundle_started_from_finder(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void macosx_finder_args_preinit(int *argc, char ***argv)
|
int cocoa_main(mpv_main_fn mpv_main, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Application *app = mpv_shared_app();
|
@autoreleasepool {
|
||||||
|
struct playback_thread_ctx ctx = {0};
|
||||||
|
ctx.mpv_main = mpv_main;
|
||||||
|
ctx.argc = &argc;
|
||||||
|
ctx.argv = &argv;
|
||||||
|
|
||||||
if (bundle_started_from_finder(*argc, *argv)) {
|
if (bundle_started_from_finder(argc, argv)) {
|
||||||
macosx_redirect_output_to_logfile("mpv");
|
argc = 1; // clears out -psn argument is present
|
||||||
macosx_wait_fileopen_events();
|
macosx_redirect_output_to_logfile("mpv");
|
||||||
|
init_cocoa_application(true);
|
||||||
char **cocoa_argv = talloc_zero_array(NULL, char*, [app.files count] + 2);
|
} else {
|
||||||
cocoa_argv[0] = "mpv";
|
init_cocoa_application(false);
|
||||||
cocoa_argv[1] = "--quiet";
|
|
||||||
int cocoa_argc = 2;
|
|
||||||
|
|
||||||
for (NSString *filename in app.files) {
|
|
||||||
cocoa_argv[cocoa_argc] = (char*)[filename UTF8String];
|
|
||||||
cocoa_argc++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*argc = cocoa_argc;
|
pthread_create(&playback_thread_id, NULL, playback_thread, &ctx);
|
||||||
*argv = cocoa_argv;
|
[[EventsResponder sharedInstance] waitForInputContext];
|
||||||
} else {
|
cocoa_run_runloop();
|
||||||
for (int i = 0; i < *argc; i++ ) {
|
|
||||||
NSString *arg = [NSString stringWithUTF8String:(*argv)[i]];
|
// This should never be reached: cocoa_run_runloop blocks until the
|
||||||
[app.argumentsList addObject:arg];
|
// 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");
|
||||||
|
pthread_join(playback_thread_id, NULL);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,8 +26,6 @@
|
|||||||
|
|
||||||
@property(nonatomic, retain) NSMutableDictionary *menuItems;
|
@property(nonatomic, retain) NSMutableDictionary *menuItems;
|
||||||
@property(nonatomic, retain) NSArray *files;
|
@property(nonatomic, retain) NSArray *files;
|
||||||
@property(nonatomic, retain) NSMutableArray *argumentsList;
|
|
||||||
@property(nonatomic, assign) BOOL willStopOnOpenEvent;
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
Application *mpv_shared_app(void);
|
Application *mpv_shared_app(void);
|
||||||
|
@ -439,11 +439,11 @@ void cocoa_set_input_context(struct input_ctx *input_context)
|
|||||||
{
|
{
|
||||||
size_t num_files = [files count];
|
size_t num_files = [files count];
|
||||||
char **files_utf8 = talloc_array(NULL, char*, num_files);
|
char **files_utf8 = talloc_array(NULL, char*, num_files);
|
||||||
[files enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL *_){
|
[files enumerateObjectsUsingBlock:^(NSString *p, NSUInteger i, BOOL *_){
|
||||||
NSURL *url = [NSURL URLWithString:obj];
|
if ([p hasPrefix:@"file:///.file/id="])
|
||||||
NSString *path = [url path];
|
p = [[NSURL URLWithString:p] path];
|
||||||
char *filename = (char *)[path UTF8String];
|
char *filename = (char *)[p UTF8String];
|
||||||
size_t bytes = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
|
size_t bytes = [p lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
|
||||||
files_utf8[i] = talloc_memdup(files_utf8, filename, bytes + 1);
|
files_utf8[i] = talloc_memdup(files_utf8, filename, bytes + 1);
|
||||||
}];
|
}];
|
||||||
mp_event_drop_files(_inputContext, num_files, files_utf8);
|
mp_event_drop_files(_inputContext, num_files, files_utf8);
|
||||||
|
@ -211,10 +211,7 @@ void vo_cocoa_uninit(struct vo *vo)
|
|||||||
[s->gl_ctx release];
|
[s->gl_ctx release];
|
||||||
[s->view removeFromSuperview];
|
[s->view removeFromSuperview];
|
||||||
[s->view release];
|
[s->view release];
|
||||||
if (s->window) {
|
if (s->window) [s->window release];
|
||||||
[s->window release];
|
|
||||||
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user