2013-09-03 19:18:28 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-10-25 17:08:06 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2013-12-17 02:35:43 +00:00
|
|
|
#include "input/keycodes.h"
|
2013-09-03 19:18:28 +00:00
|
|
|
|
|
|
|
#include "osdep/macosx_events.h"
|
|
|
|
#include "osdep/macosx_compat.h"
|
|
|
|
#include "video/out/cocoa_common.h"
|
|
|
|
|
|
|
|
#include "window.h"
|
|
|
|
|
2014-10-05 12:28:33 +00:00
|
|
|
@interface MpvVideoWindow()
|
2016-12-04 21:52:14 +00:00
|
|
|
@property(nonatomic, retain) NSScreen *targetScreen;
|
|
|
|
@property(nonatomic, retain) NSScreen *previousScreen;
|
|
|
|
@property(nonatomic, retain) NSScreen *currentScreen;
|
|
|
|
|
2014-10-05 12:28:33 +00:00
|
|
|
- (NSRect)frameRect:(NSRect)frameRect forCenteredContentSize:(NSSize)newSize;
|
|
|
|
- (void)setCenteredContentSize:(NSSize)newSize;
|
|
|
|
@end
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
@implementation MpvVideoWindow {
|
|
|
|
NSSize _queued_video_size;
|
2016-12-04 21:52:14 +00:00
|
|
|
NSRect _unfs_content_frame;
|
|
|
|
NSRect _unfs_screen_frame;
|
|
|
|
int _is_animating;
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize adapter = _adapter;
|
2016-12-04 21:52:14 +00:00
|
|
|
@synthesize targetScreen = _target_screen;
|
|
|
|
@synthesize previousScreen = _previous_screen;
|
|
|
|
@synthesize currentScreen = _current_screen;
|
2013-09-03 19:18:28 +00:00
|
|
|
- (id)initWithContentRect:(NSRect)content_rect
|
|
|
|
styleMask:(NSUInteger)style_mask
|
|
|
|
backing:(NSBackingStoreType)buffering_type
|
|
|
|
defer:(BOOL)flag
|
2017-01-28 15:29:22 +00:00
|
|
|
screen:(NSScreen *)screen
|
2013-09-03 19:18:28 +00:00
|
|
|
{
|
|
|
|
if (self = [super initWithContentRect:content_rect
|
|
|
|
styleMask:style_mask
|
|
|
|
backing:buffering_type
|
2017-01-28 15:29:22 +00:00
|
|
|
defer:flag
|
|
|
|
screen:screen]) {
|
2013-09-03 19:18:28 +00:00
|
|
|
[self setBackgroundColor:[NSColor blackColor]];
|
2014-03-13 03:34:56 +00:00
|
|
|
[self setMinSize:NSMakeSize(50,50)];
|
2016-12-04 21:52:14 +00:00
|
|
|
[self setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
|
|
|
|
|
2017-01-28 15:29:22 +00:00
|
|
|
self.targetScreen = screen;
|
|
|
|
self.currentScreen = screen;
|
2016-12-04 21:52:14 +00:00
|
|
|
_is_animating = 0;
|
|
|
|
_unfs_content_frame = [self convertRectToScreen:[[self contentView] frame]];
|
2017-01-28 15:29:22 +00:00
|
|
|
_unfs_screen_frame = [screen frame];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-12-04 21:52:14 +00:00
|
|
|
- (void)toggleFullScreen:(id)sender
|
|
|
|
{
|
|
|
|
if (_is_animating)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_is_animating = 1;
|
|
|
|
|
|
|
|
self.targetScreen = [self.adapter getTargetScreen];
|
|
|
|
if(![self targetScreen] && ![self previousScreen]) {
|
|
|
|
self.targetScreen = [self screen];
|
|
|
|
} else if (![self targetScreen]) {
|
|
|
|
self.targetScreen = self.previousScreen;
|
|
|
|
self.previousScreen = nil;
|
|
|
|
} else {
|
|
|
|
self.previousScreen = [self screen];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![self.adapter isInFullScreenMode]) {
|
|
|
|
_unfs_content_frame = [self convertRectToScreen:[[self contentView] frame]];
|
|
|
|
_unfs_screen_frame = [[self screen] frame];
|
|
|
|
}
|
|
|
|
|
|
|
|
//move window to target screen when going to fullscreen
|
|
|
|
if (![self.adapter isInFullScreenMode] && ![[self targetScreen] isEqual:[self screen]]) {
|
|
|
|
[self setFrame:[self calculateWindowPositionForScreen:[self targetScreen]] display:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
[super toggleFullScreen:sender];
|
|
|
|
|
|
|
|
if (![self.adapter isInFullScreenMode]) {
|
2017-01-14 20:12:16 +00:00
|
|
|
[self setToFullScreen];
|
2016-12-04 21:52:14 +00:00
|
|
|
} else {
|
2017-01-14 20:12:16 +00:00
|
|
|
[self setToWindow];
|
2016-12-04 21:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 20:12:16 +00:00
|
|
|
- (void)setToFullScreen
|
|
|
|
{
|
|
|
|
[self setStyleMask:([self styleMask] | NSWindowStyleMaskFullScreen)];
|
|
|
|
NSRect frame = [[self targetScreen] frame];
|
|
|
|
[self setFrame:frame display:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setToWindow
|
|
|
|
{
|
|
|
|
[self setStyleMask:([self styleMask] & ~NSWindowStyleMaskFullScreen)];
|
|
|
|
NSRect frame = [self calculateWindowPositionForScreen:[self targetScreen]];
|
|
|
|
[self setFrame:frame display:YES];
|
|
|
|
[self setContentAspectRatio:_unfs_content_frame.size];
|
|
|
|
[self setCenteredContentSize:_unfs_content_frame.size];
|
|
|
|
}
|
|
|
|
|
2016-12-04 21:52:14 +00:00
|
|
|
- (NSArray *)customWindowsToEnterFullScreenForWindow:(NSWindow *)window
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObject:window];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray*)customWindowsToExitFullScreenForWindow:(NSWindow*)window
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObject:window];
|
|
|
|
}
|
|
|
|
|
|
|
|
// we still need to keep those around or it will use the standard animation
|
|
|
|
- (void)window:(NSWindow *)window startCustomAnimationToEnterFullScreenWithDuration:(NSTimeInterval)duration {}
|
|
|
|
|
|
|
|
- (void)window:(NSWindow *)window startCustomAnimationToExitFullScreenWithDuration:(NSTimeInterval)duration {}
|
|
|
|
|
|
|
|
- (void)windowDidEnterFullScreen:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
_is_animating = 0;
|
|
|
|
[self.adapter windowDidEnterFullScreen:notification];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidExitFullScreen:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
_is_animating = 0;
|
|
|
|
[self.adapter windowDidExitFullScreen:notification];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidFailToEnterFullScreen:(NSWindow *)window
|
|
|
|
{
|
|
|
|
_is_animating = 0;
|
2017-01-14 20:12:16 +00:00
|
|
|
[self setToWindow];
|
2016-12-04 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidFailToExitFullScreen:(NSWindow *)window
|
|
|
|
{
|
|
|
|
_is_animating = 0;
|
2017-01-14 20:12:16 +00:00
|
|
|
[self setToFullScreen];
|
2016-12-04 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
2014-02-24 23:04:30 +00:00
|
|
|
- (void)windowDidChangeBackingProperties:(NSNotification *)notification
|
|
|
|
{
|
2014-10-04 15:39:43 +00:00
|
|
|
// XXX: we maybe only need expose for this
|
2014-10-05 21:41:39 +00:00
|
|
|
[self.adapter setNeedsResize];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 02:02:51 +00:00
|
|
|
- (void)windowDidChangeScreen:(NSNotification *)notification
|
|
|
|
{
|
2016-12-04 21:52:14 +00:00
|
|
|
//this event doesn't exclusively trigger on screen change
|
|
|
|
//examples: screen reconfigure, toggling fullscreen
|
|
|
|
if (!_is_animating && ![[self currentScreen] isEqual:[self screen]]) {
|
|
|
|
self.previousScreen = [self screen];
|
|
|
|
}
|
2017-01-28 15:29:22 +00:00
|
|
|
if (![[self currentScreen] isEqual:[self screen]]) {
|
|
|
|
[self.adapter windowDidChangeScreen:notification];
|
|
|
|
}
|
2016-12-04 21:52:14 +00:00
|
|
|
self.currentScreen = [self screen];
|
2016-06-17 02:02:51 +00:00
|
|
|
}
|
|
|
|
|
2014-02-24 23:04:30 +00:00
|
|
|
- (void)windowDidChangeScreenProfile:(NSNotification *)notification
|
|
|
|
{
|
2016-12-04 21:52:14 +00:00
|
|
|
[self.adapter didChangeWindowedScreenProfile:notification];
|
2014-02-24 23:04:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 10:00:29 +00:00
|
|
|
- (void)windowDidResignKey:(NSNotification *)notification
|
|
|
|
{
|
2015-03-08 14:19:17 +00:00
|
|
|
[self.adapter windowDidResignKey:notification];
|
2015-03-08 10:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidBecomeKey:(NSNotification *)notification
|
|
|
|
{
|
2015-03-08 14:19:17 +00:00
|
|
|
[self.adapter windowDidBecomeKey:notification];
|
2015-03-08 10:00:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 16:40:01 +00:00
|
|
|
- (void)windowDidMiniaturize:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self.adapter windowDidMiniaturize:notification];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidDeminiaturize:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self.adapter windowDidDeminiaturize:notification];
|
|
|
|
}
|
|
|
|
|
2017-02-20 16:25:21 +00:00
|
|
|
- (void)windowWillMove:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self.adapter windowWillMove:notification];
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (BOOL)canBecomeMainWindow { return YES; }
|
|
|
|
- (BOOL)canBecomeKeyWindow { return YES; }
|
|
|
|
- (BOOL)windowShouldClose:(id)sender
|
|
|
|
{
|
|
|
|
cocoa_put_key(MP_KEY_CLOSE_WIN);
|
|
|
|
// We have to wait for MPlayer to handle this,
|
|
|
|
// otherwise we are in trouble if the
|
|
|
|
// MP_KEY_CLOSE_WIN handler is disabled
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)normalSize { [self mulSize:1.0f]; }
|
|
|
|
|
|
|
|
- (void)halfSize { [self mulSize:0.5f];}
|
|
|
|
|
|
|
|
- (void)doubleSize { [self mulSize:2.0f];}
|
|
|
|
|
|
|
|
- (void)mulSize:(float)multiplier
|
|
|
|
{
|
2014-03-13 03:34:56 +00:00
|
|
|
char cmd[50];
|
|
|
|
snprintf(cmd, sizeof(cmd), "set window-scale %f", multiplier);
|
2013-11-22 07:43:02 +00:00
|
|
|
[self.adapter putCommand:cmd];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 21:12:46 +00:00
|
|
|
- (void)updateBorder:(int)border
|
|
|
|
{
|
|
|
|
int borderStyle = NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|
|
|
|
|
NSWindowStyleMaskMiniaturizable;
|
|
|
|
if (border) {
|
|
|
|
int window_mask = [self styleMask] & ~NSWindowStyleMaskBorderless;
|
|
|
|
window_mask |= borderStyle;
|
|
|
|
[self setStyleMask:window_mask];
|
|
|
|
} else {
|
|
|
|
int window_mask = [self styleMask] & ~borderStyle;
|
|
|
|
window_mask |= NSWindowStyleMaskBorderless;
|
|
|
|
[self setStyleMask:window_mask];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![self.adapter isInFullScreenMode]) {
|
|
|
|
// XXX: workaround to force redrawing of window decoration
|
|
|
|
if (border) {
|
|
|
|
NSRect frame = [self frame];
|
|
|
|
frame.size.width += 1;
|
|
|
|
[self setFrame:frame display:YES];
|
|
|
|
frame.size.width -= 1;
|
|
|
|
[self setFrame:frame display:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self setContentAspectRatio:_unfs_content_frame.size];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 03:34:56 +00:00
|
|
|
- (NSRect)frameRect:(NSRect)f forCenteredContentSize:(NSSize)ns
|
2013-09-03 19:18:28 +00:00
|
|
|
{
|
2014-03-13 03:34:56 +00:00
|
|
|
NSRect cr = [self contentRectForFrameRect:f];
|
|
|
|
CGFloat dx = (cr.size.width - ns.width) / 2;
|
|
|
|
CGFloat dy = (cr.size.height - ns.height) / 2;
|
|
|
|
return NSInsetRect(f, dx, dy);
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCenteredContentSize:(NSSize)ns
|
|
|
|
{
|
2014-03-13 03:34:56 +00:00
|
|
|
[self setFrame:[self frameRect:[self frame] forCenteredContentSize:ns]
|
|
|
|
display:NO
|
|
|
|
animate:NO];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 21:52:14 +00:00
|
|
|
- (NSRect)calculateWindowPositionForScreen:(NSScreen *)screen
|
|
|
|
{
|
|
|
|
NSRect frame = [self frameRectForContentRect:_unfs_content_frame];
|
|
|
|
NSRect targetFrame = [screen frame];
|
|
|
|
|
|
|
|
CGFloat x_per = (_unfs_screen_frame.size.width - frame.size.width);
|
|
|
|
CGFloat y_per = (_unfs_screen_frame.size.height - frame.size.height);
|
|
|
|
if (x_per > 0) x_per = (frame.origin.x - _unfs_screen_frame.origin.x)/x_per;
|
|
|
|
if (y_per > 0) y_per = (frame.origin.y - _unfs_screen_frame.origin.y)/y_per;
|
|
|
|
|
|
|
|
frame.origin.x = targetFrame.origin.x +
|
|
|
|
(targetFrame.size.width - frame.size.width)*x_per;
|
|
|
|
frame.origin.y = targetFrame.origin.y +
|
|
|
|
(targetFrame.size.height - frame.size.height)*y_per;
|
|
|
|
|
|
|
|
//screen bounds right and left
|
|
|
|
if (frame.origin.x + frame.size.width > targetFrame.origin.x + targetFrame.size.width)
|
|
|
|
frame.origin.x = targetFrame.origin.x + targetFrame.size.width - frame.size.width;
|
|
|
|
if (frame.origin.x < targetFrame.origin.x)
|
|
|
|
frame.origin.x = targetFrame.origin.x;
|
|
|
|
|
|
|
|
//screen bounds top and bottom
|
|
|
|
if (frame.origin.y + frame.size.height > targetFrame.origin.y + targetFrame.size.height)
|
|
|
|
frame.origin.y = targetFrame.origin.y + targetFrame.size.height - frame.size.height;
|
|
|
|
if (frame.origin.y < targetFrame.origin.y)
|
|
|
|
frame.origin.y = targetFrame.origin.y;
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (NSRect)constrainFrameRect:(NSRect)nf toScreen:(NSScreen *)screen
|
|
|
|
{
|
2017-01-16 16:51:16 +00:00
|
|
|
if (_is_animating && ![self.adapter isInFullScreenMode])
|
|
|
|
return nf;
|
2016-12-04 21:52:14 +00:00
|
|
|
|
2017-01-16 16:51:16 +00:00
|
|
|
screen = screen ?: self.screen ?: [NSScreen mainScreen];
|
2014-04-01 17:14:15 +00:00
|
|
|
NSRect of = [self frame];
|
2017-01-16 16:51:16 +00:00
|
|
|
NSRect vf = [_is_animating ? [self targetScreen] : screen visibleFrame];
|
2014-04-01 17:14:15 +00:00
|
|
|
NSRect ncf = [self contentRectForFrameRect:nf];
|
2014-03-13 03:34:56 +00:00
|
|
|
|
2014-03-25 20:27:20 +00:00
|
|
|
// Prevent the window's titlebar from exiting the screen on the top edge.
|
|
|
|
// This introduces a 'snap to top' behaviour.
|
|
|
|
if (NSMaxY(nf) > NSMaxY(vf))
|
2014-03-13 03:34:56 +00:00
|
|
|
nf.origin.y = NSMaxY(vf) - NSHeight(nf);
|
2014-03-25 20:27:20 +00:00
|
|
|
|
2016-12-04 21:52:14 +00:00
|
|
|
// Prevent the window's titlebar from exiting the screen on the bottom edge.
|
2014-04-01 17:14:15 +00:00
|
|
|
if (NSMaxY(ncf) < NSMinY(vf))
|
|
|
|
nf.origin.y = NSMinY(vf) + NSMinY(ncf) - NSMaxY(ncf);
|
2014-03-25 20:27:20 +00:00
|
|
|
|
|
|
|
// Prevent window from exiting the screen on the right edge
|
|
|
|
if (NSMinX(nf) > NSMaxX(vf))
|
|
|
|
nf.origin.x = NSMaxX(vf) - NSWidth(nf);
|
|
|
|
|
|
|
|
// Prevent window from exiting the screen on the left
|
|
|
|
if (NSMaxX(nf) < NSMinX(vf))
|
|
|
|
nf.origin.x = NSMinX(vf);
|
|
|
|
|
2014-04-01 17:14:15 +00:00
|
|
|
if (NSHeight(nf) < NSHeight(vf) && NSHeight(of) > NSHeight(vf))
|
2014-03-25 20:27:20 +00:00
|
|
|
// If the window height is smaller than the visible frame, but it was
|
|
|
|
// bigger previously recenter the smaller window vertically. This is
|
|
|
|
// needed to counter the 'snap to top' behaviour.
|
2014-03-13 03:34:56 +00:00
|
|
|
nf.origin.y = (NSHeight(vf) - NSHeight(nf)) / 2;
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
return nf;
|
|
|
|
}
|
|
|
|
|
2016-12-04 21:52:14 +00:00
|
|
|
- (void)windowWillStartLiveResize:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self.adapter windowWillStartLiveResize:notification];
|
|
|
|
}
|
|
|
|
|
2014-03-13 03:34:56 +00:00
|
|
|
- (void)windowDidEndLiveResize:(NSNotification *)notification
|
2013-10-28 07:28:01 +00:00
|
|
|
{
|
2016-12-04 21:52:14 +00:00
|
|
|
[self.adapter windowDidEndLiveResize:notification];
|
2014-03-13 03:34:56 +00:00
|
|
|
[self setFrame:[self constrainFrameRect:self.frame toScreen:self.screen]
|
|
|
|
display:NO];
|
|
|
|
}
|
|
|
|
|
2016-12-15 22:06:04 +00:00
|
|
|
- (void)tryDequeueSize
|
|
|
|
{
|
2014-03-13 03:34:56 +00:00
|
|
|
if (_queued_video_size.width <= 0.0 || _queued_video_size.height <= 0.0)
|
|
|
|
return;
|
|
|
|
|
2016-12-04 21:52:14 +00:00
|
|
|
[self setContentAspectRatio:_queued_video_size];
|
|
|
|
[self setCenteredContentSize:_queued_video_size];
|
|
|
|
_queued_video_size = NSZeroSize;
|
2013-10-28 07:28:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 21:52:14 +00:00
|
|
|
- (void)queueNewVideoSize:(NSSize)newSize
|
2013-09-03 19:18:28 +00:00
|
|
|
{
|
2017-01-14 14:41:23 +00:00
|
|
|
_unfs_content_frame = [self frameRect:_unfs_content_frame forCenteredContentSize:newSize];
|
|
|
|
if (![self.adapter isInFullScreenMode]) {
|
2016-12-04 21:52:14 +00:00
|
|
|
if (NSEqualSizes(_queued_video_size, newSize))
|
|
|
|
return;
|
|
|
|
_queued_video_size = newSize;
|
|
|
|
[self tryDequeueSize];
|
|
|
|
}
|
2014-03-13 03:34:56 +00:00
|
|
|
}
|
2013-09-03 19:18:28 +00:00
|
|
|
|
2016-12-15 22:06:04 +00:00
|
|
|
- (void)windowDidBecomeMain:(NSNotification *)notification
|
|
|
|
{
|
2014-03-13 03:34:56 +00:00
|
|
|
[self tryDequeueSize];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
@end
|