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