]> git.sesse.net Git - vlc/blob - modules/gui/macosx/VLCVoutWindowController.m
macosx: support multiple VLC windows in fullscreen, try to just display fspanel when...
[vlc] / modules / gui / macosx / VLCVoutWindowController.m
1 /*****************************************************************************
2  * VLCVoutWindowController.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2012 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
57 - (VLCVoutView *)setupVoutForWindow:(vout_window_t *)p_wnd withProposedVideoViewPosition:(NSRect)videoViewPosition
58 {
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;
66
67     if (b_multiple_vout_windows && b_video_wallpaper)
68         b_video_wallpaper = false;
69
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
73
74         msg_Dbg(VLCIntf, "Creating background / blank window");
75         NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
76         if (!screen)
77             screen = [[VLCMainWindow sharedInstance] screen];
78
79         NSRect window_rect;
80         if (b_video_wallpaper)
81             window_rect = [screen frame];
82         else
83             window_rect = [[VLCMainWindow sharedInstance] frame];
84
85         NSUInteger mask = NSBorderlessWindowMask;
86         if (!OSX_SNOW_LEOPARD && !b_video_deco)
87             mask |= NSResizableWindowMask;
88
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];
92
93         if (b_video_wallpaper)
94             [o_new_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
95
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];
102
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];
107
108
109         if (b_video_wallpaper)
110             [o_new_video_window orderBack:nil];
111         else {
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"];
117             }
118             
119             [o_new_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
120         }
121
122         [[VLCMainWindow sharedInstance] setNonembedded:YES];
123         b_nonembedded = YES;
124     } else {
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];
129             b_nonembedded = NO;
130         } else {
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];
136
137             // no frame autosave for additional vout windows
138             if (b_multiple_vout_windows)
139                 [o_new_video_window setFrameAutosaveName:@""];
140
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];
145             b_nonembedded = YES;
146         }
147     }
148
149     if (!b_video_wallpaper) {
150         // set window size
151         NSSize videoViewSize = NSMakeSize(videoViewPosition.size.width, videoViewPosition.size.height);
152
153         if (b_nonembedded) {
154             NSRect window_rect = [o_new_video_window getWindowRectForProposedVideoViewSize:videoViewSize];
155             [o_new_video_window setFrame:window_rect display:YES];
156         }
157
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]];
162
163                 NSPoint topleftbase = NSMakePoint(0, [o_first_window frame].size.height);
164                 top_left_point = [o_first_window convertBaseToScreen: topleftbase];
165             }
166
167             top_left_point = [o_new_video_window cascadeTopLeftFromPoint: top_left_point];
168             [o_new_video_window setFrameTopLeftPoint: top_left_point];
169         }
170         
171         [o_new_video_window setNativeVideoSize:videoViewSize];
172
173         [o_new_video_window makeKeyAndOrderFront: self];
174     }
175
176     [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
177
178     if (!b_multiple_vout_windows)
179         [[VLCMainWindow sharedInstance] setNonembedded:b_nonembedded];
180
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]];
184
185     if (b_nonembedded) {
186         // event occurs before window is created, so call again
187         [[VLCMain sharedInstance] playlistUpdated];
188     }
189
190     return [o_vout_view autorelease];
191 }
192
193 - (void)removeVoutforDisplay:(NSValue *)o_key
194 {
195     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:o_key];
196     if (!o_window) {
197         msg_Err(VLCIntf, "Cannot close nonexisting window");
198         return;
199     }
200
201     if ([[VLCMainWindow sharedInstance] fullscreen] && ![[VLCMainWindow sharedInstance] nativeFullscreenMode])
202         [o_window leaveFullscreen];
203
204     if ([[VLCMainWindow sharedInstance] fullscreen] && [[VLCMainWindow sharedInstance] nativeFullscreenMode])
205         [o_window toggleFullScreen: self];
206
207
208     if (![NSStringFromClass([o_window class]) isEqualToString:@"VLCMainWindow"]) {
209         [o_window orderOut:self];
210     }
211
212     [[o_window videoView] releaseVoutThread];
213     [o_window setHasActiveVideo: NO];
214     [o_vout_dict removeObjectForKey:o_key];
215
216     if ([o_vout_dict count] == 0)
217         [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
218 }
219
220 - (void)updateWindowsControlsBarWithSelector:(SEL)aSel
221 {
222     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
223         if ([obj respondsToSelector:@selector(controlsBar)]) {
224             id o_controlsBar = [obj controlsBar];
225             if (o_controlsBar)
226                 [o_controlsBar performSelector:aSel];
227         }
228     }];
229 }
230
231 - (void)updateWindow:(vout_window_t *)p_wnd withSelector:(SEL)aSel;
232 {
233     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
234     if (!o_window) {
235         msg_Err(VLCIntf, "Cannot call selector for nonexisting window");
236         return;
237     }
238
239     [o_window performSelector:aSel];
240 }
241
242 - (VLCVideoWindowCommon *)getWindow:(vout_window_t *)p_wnd
243 {
244     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
245     assert(o_window);
246     return o_window;
247
248 }
249
250 - (void)updateWindowsUsingBlock:(void (^)(VLCVideoWindowCommon *o_window))windowUpdater
251 {
252     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
253         if ([obj isKindOfClass: [NSWindow class]])
254             windowUpdater(obj);
255     }];
256 }
257
258 - (void)setNativeVideoSize:(NSSize)size forWindow:(vout_window_t *)p_wnd
259 {
260     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
261     if (!o_window) {
262         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
263         return;
264     }
265
266     [o_window setNativeVideoSize:size];
267 }
268
269 - (void)setWindowLevel:(NSInteger)i_level forWindow:(vout_window_t *)p_wnd
270 {
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];
275     } else {
276         i_statusLevelWindowCounter--;
277         if (i_statusLevelWindowCounter == 0) {
278             [self updateWindowLevelForHelperWindows:i_level];
279         }
280     }
281
282     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
283     if (!o_window) {
284         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
285         return;
286     }
287
288     [o_window setWindowLevel:i_level];
289 }
290
291 - (void)updateWindowLevelForHelperWindows:(NSInteger)i_level
292 {
293     if (var_InheritBool(VLCIntf, "video-wallpaper"))
294         return;
295
296     i_currentWindowLevel = i_level;
297
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];
304 }
305
306 @synthesize currentWindowLevel=i_currentWindowLevel;
307
308
309 @end