1 /*****************************************************************************
2 * VLCVoutWindowController.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2012 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 "VLCVoutWindowController.h"
27 #import "MainWindow.h"
30 #import "VideoEffects.h"
31 #import "AudioEffects.h"
32 #import "playlistinfo.h"
34 #import "TrackSynchronization.h"
36 @implementation VLCVoutWindowController
41 o_vout_dict = [[NSMutableDictionary alloc] init];
42 i_currentWindowLevel = NSNormalWindowLevel;
48 NSArray *keys = [o_vout_dict allKeys];
49 for (NSValue *key in keys)
50 [self removeVoutforDisplay:key];
52 [o_vout_dict release];
57 - (VLCVoutView *)setupVoutForWindow:(vout_window_t *)p_wnd withProposedVideoViewPosition:(NSRect)videoViewPosition
59 BOOL b_nonembedded = NO;
60 BOOL b_nativeFullscreenMode = [[VLCMain sharedInstance] nativeFullscreenMode];
61 BOOL b_video_deco = var_InheritBool(VLCIntf, "video-deco");
62 BOOL b_video_wallpaper = var_InheritBool(VLCIntf, "video-wallpaper");
63 BOOL b_multiple_vout_windows = [o_vout_dict count] > 0;
64 VLCVoutView *o_vout_view;
65 VLCVideoWindowCommon *o_new_video_window;
67 if (b_multiple_vout_windows && b_video_wallpaper)
68 b_video_wallpaper = false;
70 // TODO: make lion fullscreen compatible with video-wallpaper and !embedded-video
71 if ((b_video_wallpaper || !b_video_deco) && !b_nativeFullscreenMode) {
72 // b_video_wallpaper is priorized over !b_video_deco
74 msg_Dbg(VLCIntf, "Creating background / blank window");
75 NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
77 screen = [[VLCMainWindow sharedInstance] screen];
80 if (b_video_wallpaper)
81 window_rect = [screen frame];
83 window_rect = [[VLCMainWindow sharedInstance] frame];
85 NSUInteger mask = NSBorderlessWindowMask;
86 if (!OSX_SNOW_LEOPARD && !b_video_deco)
87 mask |= NSResizableWindowMask;
89 BOOL b_no_video_deco_only = !b_video_wallpaper;
90 o_new_video_window = [[VLCVideoWindowCommon alloc] initWithContentRect:window_rect styleMask:mask backing:NSBackingStoreBuffered defer:YES];
91 [o_new_video_window setDelegate:o_new_video_window];
93 if (b_video_wallpaper)
94 [o_new_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
96 [o_new_video_window setBackgroundColor: [NSColor blackColor]];
97 [o_new_video_window setCanBecomeKeyWindow: !b_video_wallpaper];
98 [o_new_video_window setCanBecomeMainWindow: !b_video_wallpaper];
99 [o_new_video_window setAcceptsMouseMovedEvents: !b_video_wallpaper];
100 [o_new_video_window setMovableByWindowBackground: !b_video_wallpaper];
101 [o_new_video_window useOptimizedDrawing: YES];
103 o_vout_view = [[VLCVoutView alloc] initWithFrame:[[o_new_video_window contentView] bounds]];
104 [o_vout_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
105 [[o_new_video_window contentView] addSubview:o_vout_view positioned:NSWindowAbove relativeTo:nil];
106 [o_new_video_window setVideoView:o_vout_view];
109 if (b_video_wallpaper)
110 [o_new_video_window orderBack:nil];
112 // no frame autosave for additional vout windows
113 if (!b_multiple_vout_windows) {
114 // initial window position
115 [o_new_video_window center];
116 [o_new_video_window setFrameAutosaveName:@"extra-videowindow"];
119 [o_new_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
122 [[VLCMainWindow sharedInstance] setNonembedded:YES];
125 if ((var_InheritBool(VLCIntf, "embedded-video") && !b_multiple_vout_windows)) {
126 // setup embedded video
127 o_vout_view = [[[VLCMainWindow sharedInstance] videoView] retain];
128 o_new_video_window = [[VLCMainWindow sharedInstance] retain];
131 // setup detached window with controls
132 NSWindowController *o_controller = [[NSWindowController alloc] initWithWindowNibName:@"DetachedVideoWindow"];
133 [o_controller loadWindow];
134 o_new_video_window = [(VLCDetachedVideoWindow *)[o_controller window] retain];
135 [o_controller release];
137 // no frame autosave for additional vout windows
138 if (b_multiple_vout_windows)
139 [o_new_video_window setFrameAutosaveName:@""];
141 [o_new_video_window setDelegate: o_new_video_window];
142 [o_new_video_window setLevel:NSNormalWindowLevel];
143 [o_new_video_window useOptimizedDrawing: YES];
144 o_vout_view = [[o_new_video_window videoView] retain];
149 if (!b_video_wallpaper) {
151 NSSize videoViewSize = NSMakeSize(videoViewPosition.size.width, videoViewPosition.size.height);
154 NSRect window_rect = [o_new_video_window getWindowRectForProposedVideoViewSize:videoViewSize];
155 [o_new_video_window setFrame:window_rect display:YES];
158 // cascade windows if we have more than one vout
159 if (b_multiple_vout_windows) {
160 if ([o_vout_dict count] == 1) {
161 NSWindow * o_first_window = [o_vout_dict objectForKey: [[o_vout_dict allKeys] objectAtIndex: 0]];
163 NSPoint topleftbase = NSMakePoint(0, [o_first_window frame].size.height);
164 top_left_point = [o_first_window convertBaseToScreen: topleftbase];
167 top_left_point = [o_new_video_window cascadeTopLeftFromPoint: top_left_point];
168 [o_new_video_window setFrameTopLeftPoint: top_left_point];
171 [o_new_video_window setNativeVideoSize:videoViewSize];
173 [o_new_video_window makeKeyAndOrderFront: self];
176 [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
178 if (!b_multiple_vout_windows)
179 [[VLCMainWindow sharedInstance] setNonembedded:b_nonembedded];
181 [o_vout_view setVoutThread:(vout_thread_t *)p_wnd->p_parent];
182 [o_new_video_window setHasActiveVideo: YES];
183 [o_vout_dict setObject:[o_new_video_window autorelease] forKey:[NSValue valueWithPointer:p_wnd]];
186 // event occurs before window is created, so call again
187 [[VLCMain sharedInstance] playlistUpdated];
190 return [o_vout_view autorelease];
193 - (void)removeVoutforDisplay:(NSValue *)o_key
195 VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:o_key];
197 msg_Err(VLCIntf, "Cannot close nonexisting window");
201 if ([[VLCMainWindow sharedInstance] fullscreen] && ![[VLCMainWindow sharedInstance] nativeFullscreenMode])
202 [o_window leaveFullscreen];
204 if ([[VLCMainWindow sharedInstance] fullscreen] && [[VLCMainWindow sharedInstance] nativeFullscreenMode])
205 [o_window toggleFullScreen: self];
208 if (![NSStringFromClass([o_window class]) isEqualToString:@"VLCMainWindow"]) {
209 [o_window orderOut:self];
212 [[o_window videoView] releaseVoutThread];
213 [o_window setHasActiveVideo: NO];
214 [o_vout_dict removeObjectForKey:o_key];
216 if ([o_vout_dict count] == 0)
217 [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
220 - (void)updateWindowsControlsBarWithSelector:(SEL)aSel
222 [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
223 if ([obj respondsToSelector:@selector(controlsBar)]) {
224 id o_controlsBar = [obj controlsBar];
226 [o_controlsBar performSelector:aSel];
231 - (void)updateWindow:(vout_window_t *)p_wnd withSelector:(SEL)aSel;
233 VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
235 msg_Err(VLCIntf, "Cannot call selector for nonexisting window");
239 [o_window performSelector:aSel];
242 - (VLCVideoWindowCommon *)getWindow:(vout_window_t *)p_wnd
244 VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
250 - (void)updateWindowsUsingBlock:(void (^)(VLCVideoWindowCommon *o_window))windowUpdater
252 [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
253 if ([obj isKindOfClass: [NSWindow class]])
258 - (void)setNativeVideoSize:(NSSize)size forWindow:(vout_window_t *)p_wnd
260 VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
262 msg_Err(VLCIntf, "Cannot set size for nonexisting window");
266 [o_window setNativeVideoSize:size];
269 - (void)setWindowLevel:(NSInteger)i_level forWindow:(vout_window_t *)p_wnd
271 // only set level for helper windows to normal if no status vout window exist anymore
272 if(i_level == NSStatusWindowLevel) {
273 i_statusLevelWindowCounter++;
274 [self updateWindowLevelForHelperWindows:i_level];
276 i_statusLevelWindowCounter--;
277 if (i_statusLevelWindowCounter == 0) {
278 [self updateWindowLevelForHelperWindows:i_level];
282 VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
284 msg_Err(VLCIntf, "Cannot set size for nonexisting window");
288 [o_window setWindowLevel:i_level];
291 - (void)updateWindowLevelForHelperWindows:(NSInteger)i_level
293 if (var_InheritBool(VLCIntf, "video-wallpaper"))
296 i_currentWindowLevel = i_level;
298 [[VLCMainWindow sharedInstance] setWindowLevel:i_level];
299 [[VLCVideoEffects sharedInstance] updateCocoaWindowLevel:i_level];
300 [[VLCAudioEffects sharedInstance] updateCocoaWindowLevel:i_level];
301 [[[VLCMain sharedInstance] info] updateCocoaWindowLevel:i_level];
302 [[VLCBookmarks sharedInstance] updateCocoaWindowLevel:i_level];
303 [[VLCTrackSynchronization sharedInstance] updateCocoaWindowLevel:i_level];
306 @synthesize currentWindowLevel=i_currentWindowLevel;