]> git.sesse.net Git - vlc/blob - modules/gui/macosx/VLCVoutWindowController.m
macosx: fix wrong size of playlist when using podcast and minimal view
[vlc] / modules / gui / macosx / VLCVoutWindowController.m
1 /*****************************************************************************
2  * VLCVoutWindowController.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2012-2014 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 "CompatibilityFixes.h"
26 #import "VLCVoutWindowController.h"
27 #import "intf.h"
28 #import "MainWindow.h"
29 #import "VideoView.h"
30
31 #import "VideoEffects.h"
32 #import "AudioEffects.h"
33 #import "playlistinfo.h"
34 #import "bookmarks.h"
35 #import "TrackSynchronization.h"
36
37 @implementation VLCVoutWindowController
38
39 - (id)init
40 {
41     self = [super init];
42     o_vout_dict = [[NSMutableDictionary alloc] init];
43     i_currentWindowLevel = NSNormalWindowLevel;
44     i_currentFloatingWindowLevel = NSFloatingWindowLevel;
45     return self;
46 }
47
48 - (void)dealloc
49 {
50     NSArray *keys = [o_vout_dict allKeys];
51     for (NSValue *key in keys)
52         [self removeVoutforDisplay:key];
53
54     [o_vout_dict release];
55     [super dealloc];
56 }
57
58 #pragma mark -
59 #pragma mark Methods for vout provider
60
61 - (VLCVoutView *)setupVoutForWindow:(vout_window_t *)p_wnd withProposedVideoViewPosition:(NSRect)videoViewPosition
62 {
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;
70
71     // should be called before any window resizing occurs
72     if (!b_multiple_vout_windows)
73         [[VLCMainWindow sharedInstance] videoplayWillBeStarted];
74
75     if (b_multiple_vout_windows && b_video_wallpaper)
76         b_video_wallpaper = false;
77
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
81
82         msg_Dbg(VLCIntf, "Creating background / blank window");
83         NSScreen *screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
84         if (!screen)
85             screen = [[VLCMainWindow sharedInstance] screen];
86
87         NSRect window_rect;
88         if (b_video_wallpaper)
89             window_rect = [screen frame];
90         else
91             window_rect = [[VLCMainWindow sharedInstance] frame];
92
93         NSUInteger mask = NSBorderlessWindowMask;
94         if (!OSX_SNOW_LEOPARD && !b_video_deco)
95             mask |= NSResizableWindowMask;
96
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];
101
102         if (b_video_wallpaper)
103             [o_new_video_window setLevel:CGWindowLevelForKey(kCGDesktopWindowLevelKey) + 1];
104
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];
111
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];
116
117
118         if (b_video_wallpaper)
119             [o_new_video_window orderBack:nil];
120         else {
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"];
126             }
127
128             [o_new_video_window setContentMinSize: NSMakeSize(f_min_video_height, f_min_video_height)];
129         }
130
131         b_nonembedded = YES;
132     } else {
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;
138             b_nonembedded = NO;
139         } else {
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];
145
146             // no frame autosave for additional vout windows
147             if (b_multiple_vout_windows)
148                 [o_new_video_window setFrameAutosaveName:@""];
149
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];
154             b_nonembedded = YES;
155         }
156     }
157
158     NSSize videoViewSize = NSMakeSize(videoViewPosition.size.width, videoViewPosition.size.height);
159
160     // Avoid flashes if video will directly start in fullscreen
161     NSDisableScreenUpdates();
162
163     if (!b_video_wallpaper) {
164         // set (only!) window origin if specified
165         if (b_nonembedded) {
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;
171
172             [o_new_video_window setFrame:window_rect display:YES];
173         }
174
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]];
179
180                 NSPoint topleftbase = NSMakePoint(0, [o_first_window frame].size.height);
181                 top_left_point = [o_first_window convertBaseToScreen: topleftbase];
182             }
183
184             top_left_point = [o_new_video_window cascadeTopLeftFromPoint: top_left_point];
185             [o_new_video_window setFrameTopLeftPoint: top_left_point];
186         }
187
188         // resize window
189         [o_new_video_window setNativeVideoSize:videoViewSize];
190
191         [o_new_video_window makeKeyAndOrderFront: self];
192     }
193
194     [o_new_video_window setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
195
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]];
199
200     [[VLCMain sharedInstance] setActiveVideoPlayback: YES];
201     [[VLCMainWindow sharedInstance] setNonembedded:!b_mainwindow_has_video];
202
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];
206
207     if (b_nonembedded) {
208         // event occurs before window is created, so call again
209         [[VLCMain sharedInstance] playlistUpdated];
210     }
211
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';
216     free(psz_splitter);
217
218     if (!b_have_splitter && (var_InheritBool(VLCIntf, "fullscreen") || var_GetBool(pl_Get(VLCIntf), "fullscreen"))) {
219
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);
223
224         [self setFullscreen:1 forWindow:p_wnd withAnimation:NO];
225     }
226
227     NSEnableScreenUpdates();
228
229     return [o_vout_view autorelease];
230 }
231
232 - (void)removeVoutforDisplay:(NSValue *)o_key
233 {
234     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:o_key];
235     if (!o_window) {
236         msg_Err(VLCIntf, "Cannot close nonexisting window");
237         return;
238     }
239
240     [[o_window videoView] releaseVoutThread];
241
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];
245
246     // prevent visible extra window if in fullscreen
247     NSDisableScreenUpdates();
248     BOOL b_native = [[VLCMainWindow sharedInstance] nativeFullscreenMode];
249
250     // close fullscreen, without changing fullscreen vars
251     if (!b_native && ([o_window fullscreen] || [o_window inFullscreenTransition]))
252         [o_window leaveFullscreenWithAnimation:NO];
253
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];
258     }
259
260     if ([o_window class] != [VLCMainWindow class]) {
261         [o_window close];
262     }
263     NSEnableScreenUpdates();
264
265     [o_window retain];
266     [o_vout_dict removeObjectForKey:o_key];
267     if ([o_vout_dict count] == 0) {
268         [[VLCMain sharedInstance] setActiveVideoPlayback:NO];
269         i_statusLevelWindowCounter = 0;
270     }
271
272     if ([o_window class] == [VLCMainWindow class]) {
273         b_mainwindow_has_video = NO;
274
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];
278
279         // beware of order, setActiveVideoPlayback:, setHasActiveVideo: and setNonembedded: must be called before
280         [[VLCMainWindow sharedInstance] changePlaylistState: psVideoStartedOrStoppedEvent];
281     }
282
283     [o_window release];
284 }
285
286
287 - (void)setNativeVideoSize:(NSSize)size forWindow:(vout_window_t *)p_wnd
288 {
289     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
290     if (!o_window) {
291         msg_Err(VLCIntf, "Cannot set size for nonexisting window");
292         return;
293     }
294
295     [o_window setNativeVideoSize:size];
296 }
297
298 - (void)setWindowLevel:(NSInteger)i_level forWindow:(vout_window_t *)p_wnd
299 {
300     VLCVideoWindowCommon *o_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
301     if (!o_window) {
302         msg_Err(VLCIntf, "Cannot set level for nonexisting window");
303         return;
304     }
305
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];
312     } else {
313         if (i_statusLevelWindowCounter > 0)
314             i_statusLevelWindowCounter--;
315
316         if (i_statusLevelWindowCounter == 0) {
317             [self updateWindowLevelForHelperWindows:i_level];
318         }
319     }
320
321     [o_window setWindowLevel:i_level];
322 }
323
324 - (void)setFullscreen:(int)i_full forWindow:(vout_window_t *)p_wnd withAnimation:(BOOL)b_animation
325 {
326     intf_thread_t *p_intf = VLCIntf;
327     BOOL b_nativeFullscreenMode = [[VLCMain sharedInstance] nativeFullscreenMode];
328
329     if (!p_intf || (!b_nativeFullscreenMode && !p_wnd))
330         return;
331     playlist_t *p_playlist = pl_Get(p_intf);
332     BOOL b_fullscreen = i_full != 0;
333
334     if (!var_GetBool(p_playlist, "fullscreen") != !b_fullscreen)
335         var_SetBool(p_playlist, "fullscreen", b_fullscreen);
336
337     VLCVideoWindowCommon *o_current_window = nil;
338     if(p_wnd)
339         o_current_window = [o_vout_dict objectForKey:[NSValue valueWithPointer:p_wnd]];
340
341     if (b_nativeFullscreenMode) {
342         if(!o_current_window)
343             o_current_window = [VLCMainWindow sharedInstance];
344         assert(o_current_window);
345
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])) {
350
351             [o_current_window toggleFullScreen:self];
352         }
353     } else {
354         assert(o_current_window);
355
356         if (b_fullscreen) {
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];
362
363             }
364             if (p_input)
365                 vlc_object_release(p_input);
366         } else {
367             // leaving fullscreen is always allowed
368             [o_current_window leaveFullscreenWithAnimation:YES];
369         }
370     }
371 }
372
373 #pragma mark -
374 #pragma mark Misc methods
375
376 - (void)updateWindowsControlsBarWithSelector:(SEL)aSel
377 {
378     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
379         if ([obj respondsToSelector:@selector(controlsBar)]) {
380             id o_controlsBar = [obj controlsBar];
381             if (o_controlsBar)
382                 [o_controlsBar performSelector:aSel];
383         }
384     }];
385 }
386
387 - (void)updateWindowsUsingBlock:(void (^)(VLCVideoWindowCommon *o_window))windowUpdater
388 {
389     [o_vout_dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
390         if ([obj isKindOfClass: [NSWindow class]])
391             windowUpdater(obj);
392     }];
393 }
394
395 - (void)updateWindowLevelForHelperWindows:(NSInteger)i_level
396 {
397     if (var_InheritBool(VLCIntf, "video-wallpaper"))
398         return;
399
400     i_currentWindowLevel = i_level;
401     if (i_level == NSNormalWindowLevel) {
402         i_currentFloatingWindowLevel = NSFloatingWindowLevel;
403     } else {
404         i_currentFloatingWindowLevel = i_level + 1;
405     }
406
407     [[VLCMainWindow sharedInstance] setWindowLevel:i_level];
408
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];
414 }
415
416 @synthesize currentStatusWindowLevel=i_currentFloatingWindowLevel;
417
418 @end