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