]> git.sesse.net Git - vlc/blob - modules/gui/macosx/VLCVoutWindowController.m
macosx: save idle main window frame as early as possible (refs #8826)
[vlc] / modules / gui / macosx / VLCVoutWindowController.m
1 /*****************************************************************************
2  * VLCVoutWindowController.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2012-2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *          David Fuhrmann <david dot fuhrmann at googlemail dot com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "VLCVoutWindowController.h"
26 #import "intf.h"
27 #import "MainWindow.h"
28 #import "VideoView.h"
29
30 #import "VideoEffects.h"
31 #import "AudioEffects.h"
32 #import "playlistinfo.h"
33 #import "bookmarks.h"
34 #import "TrackSynchronization.h"
35
36 @implementation VLCVoutWindowController
37
38 - (id)init
39 {
40     self = [super init];
41     o_vout_dict = [[NSMutableDictionary alloc] init];
42     i_currentWindowLevel = NSNormalWindowLevel;
43     return self;
44 }
45
46 - (void)dealloc
47 {
48     NSArray *keys = [o_vout_dict allKeys];
49     for (NSValue *key in keys)
50         [self removeVoutforDisplay:key];
51
52     [o_vout_dict release];
53     [super dealloc];
54 }
55
56 #pragma mark -
57 #pragma mark Methods for vout provider
58
59 - (VLCVoutView *)setupVoutForWindow:(vout_window_t *)p_wnd withProposedVideoViewPosition:(NSRect)videoViewPosition
60 {
61     BOOL b_nonembedded = NO;
62     BOOL b_nativeFullscreenMode = [[VLCMain sharedInstance] nativeFullscreenMode];
63     BOOL b_video_deco = var_InheritBool(VLCIntf, "video-deco");
64     BOOL b_video_wallpaper = var_InheritBool(VLCIntf, "video-wallpaper");
65     BOOL b_multiple_vout_windows = [o_vout_dict count] > 0;
66     VLCVoutView *o_vout_view;
67     VLCVideoWindowCommon *o_new_video_window;
68
69     // should be called before any window resizing occurs
70     [[VLCMainWindow sharedInstance] videoplayWillBeStarted];
71
72     if (b_multiple_vout_windows && b_video_wallpaper)
73         b_video_wallpaper = false;
74
75     // TODO: make lion fullscreen compatible with video-wallpaper
76     if ((b_video_wallpaper || !b_video_deco) && !b_nativeFullscreenMode) {
77         // b_video_wallpaper is priorized over !b_video_deco
78
79         msg_Dbg(VLCIntf, "Creating background / blank window");
80         NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
81         if (!screen)
82             screen = [[VLCMainWindow sharedInstance] screen];
83
84         NSRect window_rect;
85         if (b_video_wallpaper)
86             window_rect = [screen frame];
87         else
88             window_rect = [[VLCMainWindow sharedInstance] frame];
89
90         NSUInteger mask = NSBorderlessWindowMask;
91         if (!OSX_SNOW_LEOPARD && !b_video_deco)
92             mask |= NSResizableWindowMask;
93
94         BOOL b_no_video_deco_only = !b_video_wallpaper;
95         o_new_video_window = [[VLCVideoWindowCommon alloc] initWithContentRect:window_rect styleMask:mask backing:NSBackingStoreBuffered defer:YES];
96         [o_new_video_window setDelegate:o_new_video_window];
97
98         if (b_video_wallpaper)
99             [o_new_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
100
101         [o_new_video_window setBackgroundColor: [NSColor blackColor]];
102         [o_new_video_window setCanBecomeKeyWindow: !b_video_wallpaper];
103         [o_new_video_window setCanBecomeMainWindow: !b_video_wallpaper];
104         [o_new_video_window setAcceptsMouseMovedEvents: !b_video_wallpaper];
105         [o_new_video_window setMovableByWindowBackground: !b_video_wallpaper];
106         [o_new_video_window useOptimizedDrawing: YES];
107
108         o_vout_view = [[VLCVoutView alloc] initWithFrame:[[o_new_video_window contentView] bounds]];
109         [o_vout_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
110         [[o_new_video_window contentView] addSubview:o_vout_view positioned:NSWindowAbove relativeTo:nil];
111         [o_new_video_window setVideoView:o_vout_view];
112
113
114         if (b_video_wallpaper)
115             [o_new_video_window orderBack:nil];
116         else {
117             // no frame autosave for additional vout windows
118             if (!b_multiple_vout_windows) {
119                 // initial window position
120                 [o_new_video_window center];
121                 [o_new_video_window setFrameAutosaveName:@"extra-videowindow"];
122             }
123
124             [o_new_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
125         }
126
127         [[VLCMainWindow sharedInstance] setNonembedded:YES];
128         b_nonembedded = YES;
129     } else {
130         if ((var_InheritBool(VLCIntf, "embedded-video") && !b_multiple_vout_windows)) {
131             // setup embedded video
132             o_vout_view = [[[VLCMainWindow sharedInstance] videoView] retain];
133             o_new_video_window = [[VLCMainWindow sharedInstance] retain];
134             b_nonembedded = NO;
135         } else {
136             // setup detached window with controls
137             NSWindowController *o_controller = [[NSWindowController alloc] initWithWindowNibName:@"DetachedVideoWindow"];
138             [o_controller loadWindow];
139             o_new_video_window = [(VLCDetachedVideoWindow *)[o_controller window] retain];
140             [o_controller release];
141
142             // no frame autosave for additional vout windows
143             if (b_multiple_vout_windows)
144                 [o_new_video_window setFrameAutosaveName:@""];
145
146             [o_new_video_window setDelegate: o_new_video_window];
147             [o_new_video_window setLevel:NSNormalWindowLevel];
148             [o_new_video_window useOptimizedDrawing: YES];
149             o_vout_view = [[o_new_video_window videoView] retain];
150             b_nonembedded = YES;
151         }
152     }
153
154     NSSize videoViewSize = NSMakeSize(videoViewPosition.size.width, videoViewPosition.size.height);
155
156     // TODO: find a cleaner way for "start in fullscreen"
157     // Start in fs, because either prefs settings, or fullscreen button was pressed before
158     if (var_InheritBool(VLCIntf, "fullscreen") || var_GetBool(pl_Get(VLCIntf), "fullscreen")) {
159
160         // this is not set when we start in fullscreen because of
161         // fullscreen settings in video prefs the second time
162         var_SetBool(p_wnd->p_parent, "fullscreen", 1);
163
164         int i_full = 1;
165
166         SEL sel = @selector(setFullscreen:forWindow:);
167         NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:sel]];
168         [inv setTarget:self];
169         [inv setSelector:sel];
170         [inv setArgument:&i_full atIndex:2];
171         [inv setArgument:&p_wnd atIndex:3];
172
173         NSTimeInterval resizeTime = 0.;
174         if(!b_nonembedded && !b_video_wallpaper) {
175             NSRect window_rect = [o_new_video_window getWindowRectForProposedVideoViewSize:videoViewSize];
176             resizeTime = [o_new_video_window animationResizeTime:window_rect];
177             resizeTime += 0.1;
178         }
179         
180         [NSTimer scheduledTimerWithTimeInterval:resizeTime invocation:inv repeats:NO];
181     }
182
183     if (!b_video_wallpaper) {
184         // set window size
185
186         if (b_nonembedded) {
187             NSRect window_rect = [o_new_video_window getWindowRectForProposedVideoViewSize:videoViewSize];
188             if (videoViewPosition.origin.x > 0.)
189                 window_rect.origin.x = videoViewPosition.origin.x;
190             if (videoViewPosition.origin.y > 0.)
191                 window_rect.origin.y = videoViewPosition.origin.y;
192
193             [o_new_video_window setFrame:window_rect display:YES];
194         }
195
196         // cascade windows if we have more than one vout
197         if (b_multiple_vout_windows) {
198             if ([o_vout_dict count] == 1) {
199                 NSWindow * o_first_window = [o_vout_dict objectForKey: [[o_vout_dict allKeys] objectAtIndex:0]];
200
201                 NSPoint topleftbase = NSMakePoint(0, [o_first_window frame].size.height);
202                 top_left_point = [o_first_window convertBaseToScreen: topleftbase];
203             }
204
205             top_left_point = [o_new_video_window cascadeTopLeftFromPoint: top_left_point];
206             [o_new_video_window setFrameTopLeftPoint: top_left_point];
207         }
208
209         [o_new_video_window setNativeVideoSize:videoViewSize];
210
211         [o_new_video_window makeKeyAndOrderFront: self];
212     }
213
214     [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
215
216     if (!b_multiple_vout_windows)
217         [[VLCMainWindow sharedInstance] setNonembedded:b_nonembedded];
218
219     [o_vout_view setVoutThread:(vout_thread_t *)p_wnd->p_parent];
220     [o_new_video_window setHasActiveVideo: YES];
221     [o_vout_dict setObject:[o_new_video_window autorelease] forKey:[NSValue valueWithPointer:p_wnd]];
222
223     if (b_nonembedded) {
224         // event occurs before window is created, so call again
225         [[VLCMain sharedInstance] playlistUpdated];
226     }
227
228     return [o_vout_view autorelease];
229 }
230
231 - (void)removeVoutforDisplay:(NSValue *)o_key
232 {
233     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:o_key];
234     if (!o_window) {
235         msg_Err(VLCIntf, "Cannot close nonexisting window");
236         return;
237     }
238
239     if ([o_window fullscreen] && ![[VLCMainWindow sharedInstance] nativeFullscreenMode])
240         [o_window leaveFullscreen];
241
242     [[o_window videoView] releaseVoutThread];
243
244     // set active video to no BEFORE closing the window to avoid stopping playback
245     // due to NSWindowWillCloseNotification
246     [o_window setHasActiveVideo: NO];
247     if (![NSStringFromClass([o_window class]) isEqualToString:@"VLCMainWindow"]) {
248         [o_window close];
249         [o_window orderOut:self]; // for dark interface
250     }
251
252     [o_vout_dict removeObjectForKey:o_key];
253
254     if ([o_vout_dict count] == 0)
255         [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
256 }
257
258
259 - (void)setNativeVideoSize:(NSSize)size forWindow:(vout_window_t *)p_wnd
260 {
261     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
262     if (!o_window) {
263         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
264         return;
265     }
266
267     [o_window setNativeVideoSize:size];
268 }
269
270 - (void)setWindowLevel:(NSInteger)i_level forWindow:(vout_window_t *)p_wnd
271 {
272     // only set level for helper windows to normal if no status vout window exist anymore
273     if(i_level == NSStatusWindowLevel) {
274         i_statusLevelWindowCounter++;
275         [self updateWindowLevelForHelperWindows:i_level];
276     } else {
277         i_statusLevelWindowCounter--;
278         if (i_statusLevelWindowCounter == 0) {
279             [self updateWindowLevelForHelperWindows:i_level];
280         }
281     }
282
283     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
284     if (!o_window) {
285         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
286         return;
287     }
288
289     [o_window setWindowLevel:i_level];
290 }
291
292
293 - (void)setFullscreen:(int)i_full forWindow:(vout_window_t *)p_wnd
294 {
295     intf_thread_t *p_intf = VLCIntf;
296     BOOL b_nativeFullscreenMode = [[VLCMain sharedInstance] nativeFullscreenMode];
297
298     if (!p_intf || (!b_nativeFullscreenMode && !p_wnd))
299         return;
300     playlist_t *p_playlist = pl_Get(p_intf);
301     BOOL b_fullscreen = i_full;
302
303     if (!var_GetBool(p_playlist, "fullscreen") != !b_fullscreen)
304         var_SetBool(p_playlist, "fullscreen", b_fullscreen);
305
306     VLCVideoWindowCommon *o_current_window = nil;
307     if(p_wnd)
308         o_current_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
309
310     if (b_nativeFullscreenMode) {
311         if(!o_current_window)
312             o_current_window = [VLCMainWindow sharedInstance];
313         assert(o_current_window);
314
315         // fullscreen might be triggered twice (vout event)
316         // so ignore duplicate events here
317         if((b_fullscreen && !([o_current_window fullscreen] || [o_current_window enteringFullscreenTransition])) ||
318            (!b_fullscreen && [o_current_window fullscreen])) {
319
320             [o_current_window toggleFullScreen:self];
321         }
322
323         if (b_fullscreen)
324             [NSApp setPresentationOptions:(NSApplicationPresentationFullScreen | NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
325         else
326             [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
327     } else {
328         assert(o_current_window);
329
330         if (b_fullscreen) {
331             input_thread_t * p_input = pl_CurrentInput(p_intf);
332             if (p_input != NULL && [[VLCMain sharedInstance] activeVideoPlayback]) {
333                 // activate app, as method can also be triggered from outside the app (prevents nasty window layout)
334                 [NSApp activateIgnoringOtherApps:YES];
335                 [o_current_window enterFullscreen];
336
337             }
338             if (p_input)
339                 vlc_object_release(p_input);
340         } else {
341             // leaving fullscreen is always allowed
342             [o_current_window leaveFullscreen];
343         }
344     }
345 }
346
347 #pragma mark -
348 #pragma mark Misc methods
349
350 - (void)updateWindowsControlsBarWithSelector:(SEL)aSel
351 {
352     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
353         if ([obj respondsToSelector:@selector(controlsBar)]) {
354             id o_controlsBar = [obj controlsBar];
355             if (o_controlsBar)
356                 [o_controlsBar performSelector:aSel];
357         }
358     }];
359 }
360
361 - (void)updateWindowsUsingBlock:(void (^)(VLCVideoWindowCommon *o_window))windowUpdater
362 {
363     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
364         if ([obj isKindOfClass: [NSWindow class]])
365             windowUpdater(obj);
366     }];
367 }
368
369 - (void)updateWindowLevelForHelperWindows:(NSInteger)i_level
370 {
371     if (var_InheritBool(VLCIntf, "video-wallpaper"))
372         return;
373
374     i_currentWindowLevel = i_level;
375
376     [[VLCMainWindow sharedInstance] setWindowLevel:i_level];
377     [[VLCVideoEffects sharedInstance] updateCocoaWindowLevel:i_level];
378     [[VLCAudioEffects sharedInstance] updateCocoaWindowLevel:i_level];
379     [[[VLCMain sharedInstance] info] updateCocoaWindowLevel:i_level];
380     [[VLCBookmarks sharedInstance] updateCocoaWindowLevel:i_level];
381     [[VLCTrackSynchronization sharedInstance] updateCocoaWindowLevel:i_level];
382 }
383
384 @synthesize currentWindowLevel=i_currentWindowLevel;
385
386 @end