From 4cb9c9894dad8f976682605e7e3f5bc84d8f7493 Mon Sep 17 00:00:00 2001 From: Akemi Date: Sun, 10 Sep 2017 21:34:40 +0200 Subject: [PATCH] properly wait for animations to end before quit --- osdep/macosx_events.h | 3 ++- osdep/macosx_events.m | 12 ++++++++++- player/main.c | 6 +++++- video/out/cocoa-cb/window.swift | 36 +++++++++++++-------------------- video/out/cocoa-cb_common.swift | 10 +++++++-- 5 files changed, 40 insertions(+), 27 deletions(-) diff --git a/osdep/macosx_events.h b/osdep/macosx_events.h index 8eb606bf170dc..33172001b7300 100644 --- a/osdep/macosx_events.h +++ b/osdep/macosx_events.h @@ -37,6 +37,7 @@ void cocoa_uninit_media_keys(void); void cocoa_set_input_context(struct input_ctx *input_context); void cocoa_set_mpv_handle(struct mpv_handle *ctx); -void cocoa_cocoa_cb_dealloc(void); +void cocoa_cb_dealloc(void); +void cocoa_cb_animation_wait(void); #endif diff --git a/osdep/macosx_events.m b/osdep/macosx_events.m index 57f0beea4af17..d74110eaa79ef 100644 --- a/osdep/macosx_events.m +++ b/osdep/macosx_events.m @@ -39,6 +39,10 @@ #include "config.h" +#if HAVE_MACOS_COCOA_CB +#include "osdep/macOS_swift.h" +#endif + @interface EventsResponder () { struct input_ctx *_inputContext; @@ -227,13 +231,19 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx) printf("cocoa_set_mpv_handle end\n"); } -void cocoa_cocoa_cb_dealloc() { +void cocoa_cb_dealloc() { if ([(Application *)NSApp cocoaCB]) { [[(Application *)NSApp cocoaCB] dealloc]; [(Application *)NSApp setCocoaCB:nil]; } } +void cocoa_cb_animation_wait() { + if ([(Application *)NSApp cocoaCB]) { + [[(Application *)NSApp cocoaCB] waitForAnimation]; + } +} + @implementation EventsResponder + (EventsResponder *)sharedInstance diff --git a/player/main.c b/player/main.c index 95b80423be90a..d209de8f37930 100644 --- a/player/main.c +++ b/player/main.c @@ -173,6 +173,10 @@ void mp_destroy(struct MPContext *mpctx) mp_uninit_ipc(mpctx->ipc_ctx); mpctx->ipc_ctx = NULL; +#if HAVE_COCOA + cocoa_cb_animation_wait(); +#endif + uninit_audio_out(mpctx); uninit_video_out(mpctx); @@ -468,7 +472,7 @@ int mp_initialize(struct MPContext *mpctx, char **options) if (opts->vo->video_driver_list == NULL || strcmp(opts->vo->video_driver_list->name, "opengl-cb") != 0) { - cocoa_cocoa_cb_dealloc(); + cocoa_cb_dealloc(); } mpv_handle *ctx = mp_new_client(mpctx->clients, "osx"); cocoa_set_mpv_handle(ctx); diff --git a/video/out/cocoa-cb/window.swift b/video/out/cocoa-cb/window.swift index 06461aac15b5b..7fd7d8fa11998 100644 --- a/video/out/cocoa-cb/window.swift +++ b/video/out/cocoa-cb/window.swift @@ -16,7 +16,7 @@ class VideoWindow: NSWindow, NSWindowDelegate { var isInFullscreen: Bool = false var isAnimating: Bool = false var isMoving: Bool = false - //let animationLock = NSCondition() + let animationLock = NSCondition() override var canBecomeKey: Bool { return true } override var canBecomeMain: Bool { return true } @@ -143,8 +143,7 @@ class VideoWindow: NSWindow, NSWindowDelegate { self.cocoaCB.layer.isAsynchronous = false }*/ - isAnimating = false - //unlockAnimation() + unlockAnimation() } func windowDidExitFullScreen(_ notification: Notification) { @@ -156,49 +155,42 @@ class VideoWindow: NSWindow, NSWindowDelegate { }*/ contentAspectRatio = unfsContentFrame!.size - isAnimating = false - //unlockAnimation() + unlockAnimation() } func windowDidFailToEnterFullScreen(_ window: NSWindow) { let newFrame = calculateWindowPosition(for: targetScreen!, withoutBounds: targetScreen == screen) setFrame(newFrame, display: true) contentAspectRatio = unfsContentFrame!.size - isAnimating = false - //unlockAnimation() + unlockAnimation() } func windowDidFailToExitFullScreen(_ window: NSWindow) { let newFrame = targetScreen!.frame setFrame(newFrame, display: true) - isAnimating = false - //unlockAnimation() + unlockAnimation() } - /*func unlockAnimation() { - /*Swift.print("unlockAnimation start") - animationLock.lock()*/ + func unlockAnimation() { + animationLock.lock() isAnimating = false - /*animationLock.signal() + animationLock.signal() animationLock.unlock() - Swift.print("unlockAnimation end")*/ - }*/ + } - /*func waitForAnimation() { - Swift.print("waitForAnimation start") + func waitForAnimation() { animationLock.lock() while isAnimating { - Swift.print("waitForAnimation wait") animationLock.wait() } + close() animationLock.unlock() - Swift.print("waitForAnimation end") - }*/ + } func setToFullScreen() { NSApp.presentationOptions = [.autoHideMenuBar, .autoHideDock] setFrame(targetScreen!.frame, display: true) - isAnimating = false + unlockAnimation() isInFullscreen = true cocoaCB.flagEvents(VO_EVENT_FULLSCREEN_STATE) } @@ -208,7 +200,7 @@ class VideoWindow: NSWindow, NSWindowDelegate { NSApp.presentationOptions = [] setFrame(newFrame, display: true) contentAspectRatio = unfsContentFrame!.size - isAnimating = false + unlockAnimation() isInFullscreen = false cocoaCB.flagEvents(VO_EVENT_FULLSCREEN_STATE) } diff --git a/video/out/cocoa-cb_common.swift b/video/out/cocoa-cb_common.swift index 99542637b06cc..64b8e61e5861b 100644 --- a/video/out/cocoa-cb_common.swift +++ b/video/out/cocoa-cb_common.swift @@ -37,6 +37,10 @@ class CocoaCB: NSObject { view.wantsLayer = true } + func waitForAnimation() { + window.waitForAnimation() + } + func setMpvHandle(_ ctx: OpaquePointer) { mpv = MPVHelper(ctx) layer.setUpGLCB() @@ -401,8 +405,10 @@ https://github.com/mpv-player/mpv/commit/c028d782c1eec46e2416da483881f1d0b27c2be func processEvent(_ event: UnsafePointer) { switch event.pointee.event_id { case MPV_EVENT_SHUTDOWN: - // TODO: need to stop shutdown for animations, not on main thread - //window.waitForAnimation() + if window.isInFullscreen { + window.toggleFullScreen(nil) + } + setCursorVisiblility(true) stopDisplaylink() uninitLightSensor()