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