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()
|
|
|
|
- (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize adapter = _adapter;
|
|
|
|
- (id)initWithContentRect:(NSRect)content_rect
|
|
|
|
styleMask:(NSUInteger)style_mask
|
|
|
|
backing:(NSBackingStoreType)buffering_type
|
|
|
|
defer:(BOOL)flag
|
|
|
|
{
|
|
|
|
if (self = [super initWithContentRect:content_rect
|
|
|
|
styleMask:style_mask
|
|
|
|
backing:buffering_type
|
|
|
|
defer:flag]) {
|
|
|
|
[self setBackgroundColor:[NSColor blackColor]];
|
2014-03-13 03:34:56 +00:00
|
|
|
[self setMinSize:NSMakeSize(50,50)];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-24 23:04:30 +00:00
|
|
|
- (void)windowDidChangeScreenProfile:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self.adapter didChangeWindowedScreenProfile:[self screen]];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRect)constrainFrameRect:(NSRect)nf toScreen:(NSScreen *)screen
|
|
|
|
{
|
2014-04-01 17:14:15 +00:00
|
|
|
NSRect of = [self frame];
|
2014-04-01 17:18:35 +00:00
|
|
|
NSRect vf = [screen ?: self.screen ?: [NSScreen mainScreen] 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
|
|
|
|
2014-04-01 17:14:15 +00:00
|
|
|
// Prevent the window's titlebar from exiting the screen on the top edge.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-13 03:34:56 +00:00
|
|
|
- (void)windowDidEndLiveResize:(NSNotification *)notification
|
2013-10-28 07:28:01 +00:00
|
|
|
{
|
2014-03-13 03:34:56 +00:00
|
|
|
[self setFrame:[self constrainFrameRect:self.frame toScreen:self.screen]
|
|
|
|
display:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tryDequeueSize {
|
|
|
|
if (_queued_video_size.width <= 0.0 || _queued_video_size.height <= 0.0)
|
|
|
|
return;
|
|
|
|
|
2014-10-05 16:07:27 +00:00
|
|
|
// XXX find a way to kill this state
|
2014-03-13 03:34:56 +00:00
|
|
|
if (![self.adapter isInFullScreenMode]) {
|
|
|
|
[self setContentAspectRatio:_queued_video_size];
|
|
|
|
[self setCenteredContentSize:_queued_video_size];
|
|
|
|
_queued_video_size = NSZeroSize;
|
|
|
|
}
|
2013-10-28 07:28:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 19:18:28 +00:00
|
|
|
- (void)queueNewVideoSize:(NSSize)new_size
|
|
|
|
{
|
2014-03-13 03:34:56 +00:00
|
|
|
if (NSEqualSizes(_queued_video_size, new_size))
|
|
|
|
return;
|
|
|
|
_queued_video_size = new_size;
|
|
|
|
[self tryDequeueSize];
|
|
|
|
}
|
2013-09-03 19:18:28 +00:00
|
|
|
|
2014-03-13 03:34:56 +00:00
|
|
|
- (void)windowDidBecomeMain:(NSNotification *)notification {
|
|
|
|
[self tryDequeueSize];
|
2013-09-03 19:18:28 +00:00
|
|
|
}
|
|
|
|
@end
|