1 /*****************************************************************************
2 * VLCVoutWindowController.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2012-2014 VLC authors and VideoLAN
7 * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
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.
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.
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 *****************************************************************************/
25 #import "CompatibilityFixes.h"
26 #import "VLCVoutWindowController.h"
28 #import "MainWindow.h"
31 #import "VideoEffects.h"
32 #import "AudioEffects.h"
33 #import "playlistinfo.h"
35 #import "TrackSynchronization.h"
37 @implementation VLCVoutWindowController
42 o_vout_dict = [[NSMutableDictionary alloc] init];
43 i_currentWindowLevel = NSNormalWindowLevel;
44 i_currentFloatingWindowLevel = NSFloatingWindowLevel;
50 NSArray *keys = [o_vout_dict allKeys];
51 for (NSValue *key in keys)
52 [self removeVoutforDisplay:key];
54 [o_vout_dict release];
59 #pragma mark Methods for vout provider
61 - (VLCVoutView *)setupVoutForWindow:(vout_window_t *)p_wnd withProposedVideoViewPosition:(NSRect)videoViewPosition
63 BOOL b_nonembedded = NO;
64 BOOL b_nativeFullscreenMode = [[VLCMain sharedInstance] nativeFullscreenMode];
65 BOOL b_video_deco = var_InheritBool(VLCIntf, "video-deco");
66 BOOL b_video_wallpaper = var_InheritBool(VLCIntf, "video-wallpaper");
67 BOOL b_multiple_vout_windows = [o_vout_dict count] > 0;
68 VLCVoutView *o_vout_view;
69 VLCVideoWindowCommon *o_new_video_window;
71 // should be called before any window resizing occurs
72 if (!b_multiple_vout_windows)
73 [[VLCMainWindow sharedInstance] videoplayWillBeStarted];
75 if (b_multiple_vout_windows && b_video_wallpaper)
76 b_video_wallpaper = false;
78 // TODO: make lion fullscreen compatible with video-wallpaper
79 if ((b_video_wallpaper || !b_video_deco) && !b_nativeFullscreenMode) {
80 // b_video_wallpaper is priorized over !b_video_deco
82 msg_Dbg(VLCIntf, "Creating background / blank window");
83 NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
85 screen = [[VLCMainWindow sharedInstance] screen];
88 if (b_video_wallpaper)
89 window_rect = [screen frame];
91 window_rect = [[VLCMainWindow sharedInstance] frame];
93 NSUInteger mask = NSBorderlessWindowMask;
94 if (!OSX_SNOW_LEOPARD && !b_video_deco)
95 mask |= NSResizableWindowMask;
97 BOOL b_no_video_deco_only = !b_video_wallpaper;
98 o_new_video_window = [[VLCVideoWindowCommon alloc] initWithContentRect:window_rect styleMask:mask backing:NSBackingStoreBuffered defer:YES];
99 [o_new_video_window setDelegate:o_new_video_window];
100 [o_new_video_window setReleasedWhenClosed: NO];
102 if (b_video_wallpaper)
103 [o_new_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
105 [o_new_video_window setBackgroundColor: [NSColor blackColor]];
106 [o_new_video_window setCanBecomeKeyWindow: !b_video_wallpaper];
107 [o_new_video_window setCanBecomeMainWindow: !b_video_wallpaper];
108 [o_new_video_window setAcceptsMouseMovedEvents: !b_video_wallpaper];
109 [o_new_video_window setMovableByWindowBackground: !b_video_wallpaper];
110 [o_new_video_window useOptimizedDrawing: YES];
112 o_vout_view = [[VLCVoutView alloc] initWithFrame:[[o_new_video_window contentView] bounds]];
113 [o_vout_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
114 [[o_new_video_window contentView] addSubview:o_vout_view positioned:NSWindowAbove relativeTo:nil];
115 [o_new_video_window setVideoView:o_vout_view];
118 if (b_video_wallpaper)
119 [o_new_video_window orderBack:nil];
121 // no frame autosave for additional vout windows
122 if (!b_multiple_vout_windows) {
123 // initial window position
124 [o_new_video_window center];
125 [o_new_video_window setFrameAutosaveName:@"extra-videowindow"];
128 [o_new_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
133 if ((var_InheritBool(VLCIntf, "embedded-video") && !b_mainwindow_has_video)) {
134 // setup embedded video
135 o_vout_view = [[[VLCMainWindow sharedInstance] videoView] retain];
136 o_new_video_window = [[VLCMainWindow sharedInstance] retain];
137 b_mainwindow_has_video = YES;
140 // setup detached window with controls
141 NSWindowController *o_controller = [[NSWindowController alloc] initWithWindowNibName:@"DetachedVideoWindow"];
142 [o_controller loadWindow];
143 o_new_video_window = [(VLCDetachedVideoWindow *)[o_controller window] retain];
144 [o_controller release];
146 // no frame autosave for additional vout windows
147 if (b_multiple_vout_windows)
148 [o_new_video_window setFrameAutosaveName:@""];
150 [o_new_video_window setDelegate: o_new_video_window];
151 [o_new_video_window setLevel:NSNormalWindowLevel];
152 [o_new_video_window useOptimizedDrawing: YES];
153 o_vout_view = [[o_new_video_window videoView] retain];
158 NSSize videoViewSize = NSMakeSize(videoViewPosition.size.width, videoViewPosition.size.height);
160 // Avoid flashes if video will directly start in fullscreen
161 NSDisableScreenUpdates();
163 if (!b_video_wallpaper) {
164 // set (only!) window origin if specified
166 NSRect window_rect = [o_new_video_window frame];
167 if (videoViewPosition.origin.x > 0.)
168 window_rect.origin.x = videoViewPosition.origin.x;
169 if (videoViewPosition.origin.y > 0.)
170 window_rect.origin.y = videoViewPosition.origin.y;
172 [o_new_video_window setFrame:window_rect display:YES];
175 // cascade windows if we have more than one vout
176 if (b_multiple_vout_windows) {
177 if ([o_vout_dict count] == 1) {
178 NSWindow * o_first_window = [o_vout_dict objectForKey: [[o_vout_dict allKeys] objectAtIndex:0]];
180 NSPoint topleftbase = NSMakePoint(0, [o_first_window frame].size.height);
181 top_left_point = [o_first_window convertBaseToScreen: topleftbase];
184 top_left_point = [o_new_video_window cascadeTopLeftFromPoint: top_left_point];
185 [o_new_video_window setFrameTopLeftPoint: top_left_point];
189 [o_new_video_window setNativeVideoSize:videoViewSize];
191 [o_new_video_window makeKeyAndOrderFront: self];
194 [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
196 [o_vout_view setVoutThread:(vout_thread_t *)p_wnd->p_parent];
197 [o_new_video_window setHasActiveVideo: YES];
198 [o_vout_dict setObject:[o_new_video_window autorelease] forKey:[NSValue valueWithPointer:p_wnd]];
200 [[VLCMain sharedInstance] setActiveVideoPlayback: YES];
201 [[VLCMainWindow sharedInstance] setNonembedded:!b_mainwindow_has_video];
203 // beware of order, setActiveVideoPlayback:, setHasActiveVideo: and setNonembedded: must be called before
204 if ([o_new_video_window class] == [VLCMainWindow class])
205 [[VLCMainWindow sharedInstance] changePlaylistState: psVideoStartedOrStoppedEvent];
208 // event occurs before window is created, so call again
209 [[VLCMain sharedInstance] playlistUpdated];
212 // TODO: find a cleaner way for "start in fullscreen"
213 // Start in fs, because either prefs settings, or fullscreen button was pressed before
214 char *psz_splitter = var_GetString(pl_Get(VLCIntf), "video-splitter");
215 BOOL b_have_splitter = psz_splitter != NULL && *psz_splitter != '\0';
218 if (!b_have_splitter && (var_InheritBool(VLCIntf, "fullscreen") || var_GetBool(pl_Get(VLCIntf), "fullscreen"))) {
220 // this is not set when we start in fullscreen because of
221 // fullscreen settings in video prefs the second time
222 var_SetBool(p_wnd->p_parent, "fullscreen", 1);
224 [self setFullscreen:1 forWindow:p_wnd withAnimation:NO];
227 NSEnableScreenUpdates();
229 return [o_vout_view autorelease];
232 - (void)removeVoutforDisplay:(NSValue *)o_key
234 VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:o_key];
236 msg_Err(VLCIntf, "Cannot close nonexisting window");
240 [[o_window videoView] releaseVoutThread];
242 // set active video to no BEFORE closing the window and exiting fullscreen
243 // (avoid stopping playback due to NSWindowWillCloseNotification, preserving fullscreen state)
244 [o_window setHasActiveVideo: NO];
246 // prevent visible extra window if in fullscreen
247 NSDisableScreenUpdates();
248 BOOL b_native = [[VLCMainWindow sharedInstance] nativeFullscreenMode];
250 // close fullscreen, without changing fullscreen vars
251 if (!b_native && ([o_window fullscreen] || [o_window inFullscreenTransition]))
252 [o_window leaveFullscreenWithAnimation:NO];
254 // native fullscreen window will not be closed if
255 // fullscreen was triggered without video
256 if ((b_native && [o_window class] == [VLCMainWindow class] && [o_window fullscreen] && [o_window windowShouldExitFullscreenWhenFinished])) {
257 [o_window toggleFullScreen:self];
260 if ([o_window class] != [VLCMainWindow class]) {
263 NSEnableScreenUpdates();
266 [o_vout_dict removeObjectForKey:o_key];
267 if ([o_vout_dict count] == 0) {
268 [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
269 i_statusLevelWindowCounter = 0;
272 if ([o_window class] == [VLCMainWindow class]) {
273 b_mainwindow_has_video = NO;
275 // video in main window might get stopped while another vout is open
276 if ([o_vout_dict count] > 0)
277 [[VLCMainWindow sharedInstance] setNonembedded:YES];
279 // beware of order, setActiveVideoPlayback:, setHasActiveVideo: and setNonembedded: must be called before
280 [[VLCMainWindow sharedInstance] changePlaylistState: psVideoStartedOrStoppedEvent];
287 - (void)setNativeVideoSize:(NSSize)size forWindow:(vout_window_t *)p_wnd
289 VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
291 msg_Err(VLCIntf, "Cannot set size for nonexisting window");
295 [o_window setNativeVideoSize:size];
298 - (void)setWindowLevel:(NSInteger)i_level forWindow:(vout_window_t *)p_wnd
300 VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
302 msg_Err(VLCIntf, "Cannot set level for nonexisting window");
306 // only set level for helper windows to normal if no status vout window exist anymore
307 if(i_level == NSStatusWindowLevel) {
308 i_statusLevelWindowCounter++;
309 // window level need to stay on normal in fullscreen mode
310 if (![o_window fullscreen] && ![o_window inFullscreenTransition])
311 [self updateWindowLevelForHelperWindows:i_level];
313 if (i_statusLevelWindowCounter > 0)
314 i_statusLevelWindowCounter--;
316 if (i_statusLevelWindowCounter == 0) {
317 [self updateWindowLevelForHelperWindows:i_level];
321 [o_window setWindowLevel:i_level];
324 - (void)setFullscreen:(int)i_full forWindow:(vout_window_t *)p_wnd withAnimation:(BOOL)b_animation
326 intf_thread_t *p_intf = VLCIntf;
327 BOOL b_nativeFullscreenMode = [[VLCMain sharedInstance] nativeFullscreenMode];
329 if (!p_intf || (!b_nativeFullscreenMode && !p_wnd))
331 playlist_t *p_playlist = pl_Get(p_intf);
332 BOOL b_fullscreen = i_full != 0;
334 if (!var_GetBool(p_playlist, "fullscreen") != !b_fullscreen)
335 var_SetBool(p_playlist, "fullscreen", b_fullscreen);
337 VLCVideoWindowCommon *o_current_window = nil;
339 o_current_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
341 if (b_nativeFullscreenMode) {
342 if(!o_current_window)
343 o_current_window = [VLCMainWindow sharedInstance];
344 assert(o_current_window);
346 // fullscreen might be triggered twice (vout event)
347 // so ignore duplicate events here
348 if((b_fullscreen && !([o_current_window fullscreen] || [o_current_window inFullscreenTransition])) ||
349 (!b_fullscreen && [o_current_window fullscreen])) {
351 [o_current_window toggleFullScreen:self];
354 assert(o_current_window);
357 input_thread_t * p_input = pl_CurrentInput(p_intf);
358 if (p_input != NULL && [[VLCMain sharedInstance] activeVideoPlayback]) {
359 // activate app, as method can also be triggered from outside the app (prevents nasty window layout)
360 [NSApp activateIgnoringOtherApps:YES];
361 [o_current_window enterFullscreenWithAnimation:b_animation];
365 vlc_object_release(p_input);
367 // leaving fullscreen is always allowed
368 [o_current_window leaveFullscreenWithAnimation:YES];
374 #pragma mark Misc methods
376 - (void)updateWindowsControlsBarWithSelector:(SEL)aSel
378 [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
379 if ([obj respondsToSelector:@selector(controlsBar)]) {
380 id o_controlsBar = [obj controlsBar];
382 [o_controlsBar performSelector:aSel];
387 - (void)updateWindowsUsingBlock:(void (^)(VLCVideoWindowCommon *o_window))windowUpdater
389 [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
390 if ([obj isKindOfClass: [NSWindow class]])
395 - (void)updateWindowLevelForHelperWindows:(NSInteger)i_level
397 if (var_InheritBool(VLCIntf, "video-wallpaper"))
400 i_currentWindowLevel = i_level;
401 if (i_level == NSNormalWindowLevel) {
402 i_currentFloatingWindowLevel = NSFloatingWindowLevel;
404 i_currentFloatingWindowLevel = i_level + 1;
407 [[VLCMainWindow sharedInstance] setWindowLevel:i_level];
409 [[VLCVideoEffects sharedInstance] updateCocoaWindowLevel:i_currentFloatingWindowLevel];
410 [[VLCAudioEffects sharedInstance] updateCocoaWindowLevel:i_currentFloatingWindowLevel];
411 [[[VLCMain sharedInstance] info] updateCocoaWindowLevel:i_currentFloatingWindowLevel];
412 [[VLCBookmarks sharedInstance] updateCocoaWindowLevel:i_currentFloatingWindowLevel];
413 [[VLCTrackSynchronization sharedInstance] updateCocoaWindowLevel:i_currentFloatingWindowLevel];
416 @synthesize currentStatusWindowLevel=i_currentFloatingWindowLevel;