Skip to content

Commit

Permalink
properly wait for animations to end before quit
Browse files Browse the repository at this point in the history
  • Loading branch information
Akemi committed Sep 11, 2017
1 parent 42caaf0 commit 4cb9c98
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 27 deletions.
3 changes: 2 additions & 1 deletion osdep/macosx_events.h
Expand Up @@ -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
12 changes: 11 additions & 1 deletion osdep/macosx_events.m
Expand Up @@ -39,6 +39,10 @@

#include "config.h"

#if HAVE_MACOS_COCOA_CB
#include "osdep/macOS_swift.h"
#endif

@interface EventsResponder ()
{
struct input_ctx *_inputContext;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion player/main.c
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
36 changes: 14 additions & 22 deletions video/out/cocoa-cb/window.swift
Expand Up @@ -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 }
Expand Down Expand Up @@ -143,8 +143,7 @@ class VideoWindow: NSWindow, NSWindowDelegate {
self.cocoaCB.layer.isAsynchronous = false
}*/

isAnimating = false
//unlockAnimation()
unlockAnimation()
}

func windowDidExitFullScreen(_ notification: Notification) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
10 changes: 8 additions & 2 deletions video/out/cocoa-cb_common.swift
Expand Up @@ -37,6 +37,10 @@ class CocoaCB: NSObject {
view.wantsLayer = true
}

func waitForAnimation() {
window.waitForAnimation()
}

func setMpvHandle(_ ctx: OpaquePointer) {
mpv = MPVHelper(ctx)
layer.setUpGLCB()
Expand Down Expand Up @@ -401,8 +405,10 @@ https://github.com/mpv-player/mpv/commit/c028d782c1eec46e2416da483881f1d0b27c2be
func processEvent(_ event: UnsafePointer<mpv_event>) {
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()
Expand Down

0 comments on commit 4cb9c98

Please sign in to comment.