summaryrefslogtreecommitdiffstats
path: root/video/out/cocoa/window.m
blob: 68e5222a032bad8d55d0d4781e983db049e7a847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * 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/>.
 */

#include <libavutil/common.h>

#include "input/keycodes.h"

#include "osdep/macosx_events.h"
#include "osdep/macosx_compat.h"
#include "video/out/cocoa_common.h"

#include "window.h"

@interface MpvVideoWindow()
@property(nonatomic, retain) NSScreen *targetScreen;
@property(nonatomic, retain) NSScreen *previousScreen;
@property(nonatomic, retain) NSScreen *currentScreen;

- (NSRect)frameRect:(NSRect)frameRect forCenteredContentSize:(NSSize)newSize;
- (void)setCenteredContentSize:(NSSize)newSize;
@end

@implementation MpvVideoWindow {
    NSSize _queued_video_size;
    NSRect _unfs_content_frame;
    NSRect _unfs_screen_frame;
    int _is_animating;
}

@synthesize adapter = _adapter;
@synthesize targetScreen = _target_screen;
@synthesize previousScreen = _previous_screen;
@synthesize currentScreen = _current_screen;
- (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]];
        [self setMinSize:NSMakeSize(50,50)];
        [self setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];

        self.targetScreen = [self screen];
        self.currentScreen = [self screen];
        _is_animating = 0;
        _unfs_content_frame = [self convertRectToScreen:[[self contentView] frame]];
        _unfs_screen_frame = [[self screen] frame];
    }
    return self;
}

- (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]) {
        [self setStyleMask:([self styleMask] | NSWindowStyleMaskFullScreen)];
        NSRect frame = [[self targetScreen] frame];
        [self setFrame:frame display:YES];
    } else {
        [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];
    }
}

- (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;
}

- (void)windowDidFailToExitFullScreen:(NSWindow *)window
{
    _is_animating = 0;
}

- (void)windowDidChangeBackingProperties:(NSNotification *)notification
{
    // XXX: we maybe only need expose for this
    [self.adapter setNeedsResize];
}

- (void)windowDidChangeScreen:(NSNotification *)notification
{
    //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];
    }
    self.currentScreen = [self screen];
    [self.adapter windowDidChangeScreen:notification];
}

- (void)windowDidChangeScreenProfile:(NSNotification *)notification
{
    [self.adapter didChangeWindowedScreenProfile:notification];
}

- (void)windowDidResignKey:(NSNotification *)notification
{
    [self.adapter windowDidResignKey:notification];
}

- (void)windowDidBecomeKey:(NSNotification *)notification
{
    [self.adapter windowDidBecomeKey:notification];
}

- (void)windowDidMiniaturize:(NSNotification *)notification
{
    [self.adapter windowDidMiniaturize:notification];
}

- (void)windowDidDeminiaturize:(NSNotification *)notification
{
    [self.adapter windowDidDeminiaturize:notification];
}

- (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
{
    char cmd[50];
    snprintf(cmd, sizeof(cmd), "set window-scale %f", multiplier);
    [self.adapter putCommand:cmd];
}

- (NSRect)frameRect:(NSRect)f forCenteredContentSize:(NSSize)ns
{
    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);
}

- (void)setCenteredContentSize:(NSSize)ns
{
    [self setFrame:[self frameRect:[self frame] forCenteredContentSize:ns]
           display:NO
           animate:NO];
}

- (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;
}

- (NSRect)constrainFrameRect:(NSRect)nf toScreen:(NSScreen *)screen
{
    if (_is_animating)
        screen = [self targetScreen];

    NSRect of  = [self frame];
    NSRect vf  = [screen ?: self.screen ?: [NSScreen mainScreen] visibleFrame];
    NSRect ncf = [self contentRectForFrameRect:nf];

    // 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))
        nf.origin.y = NSMaxY(vf) - NSHeight(nf);

    // Prevent the window's titlebar from exiting the screen on the bottom edge.
    if (NSMaxY(ncf) < NSMinY(vf))
        nf.origin.y = NSMinY(vf) + NSMinY(ncf) - NSMaxY(ncf);

    // 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);

    if (NSHeight(nf) < NSHeight(vf) && NSHeight(of) > NSHeight(vf))
        // 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.
        nf.origin.y = (NSHeight(vf) - NSHeight(nf)) / 2;

    return nf;
}

- (void)windowWillStartLiveResize:(NSNotification *)notification
{
    [self.adapter windowWillStartLiveResize:notification];
}

- (void)windowDidEndLiveResize:(NSNotification *)notification
{
    [self.adapter windowDidEndLiveResize:notification];
    [self setFrame:[self constrainFrameRect:self.frame toScreen:self.screen]
           display:NO];
}

- (void)updateWindowFrame:(NSSize)newSize
{
    _unfs_content_frame = [self frameRect:_unfs_content_frame forCenteredContentSize:newSize];
}

- (void)tryDequeueSize
{
    if (_queued_video_size.width <= 0.0 || _queued_video_size.height <= 0.0)
        return;

    [self setContentAspectRatio:_queued_video_size];
    [self setCenteredContentSize:_queued_video_size];
    _queued_video_size = NSZeroSize;
}

- (void)queueNewVideoSize:(NSSize)newSize
{
    if ([self.adapter isInFullScreenMode]) {
        [self updateWindowFrame:newSize];
    } else {
        if (NSEqualSizes(_queued_video_size, newSize))
            return;
        _queued_video_size = newSize;
        [self tryDequeueSize];
    }
}

- (void)windowDidBecomeMain:(NSNotification *)notification
{
    [self tryDequeueSize];
}
@end