1 /*****************************************************************************
2 * Windows.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2012-2014 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 *****************************************************************************/
27 #import "CoreInteraction.h"
28 #import "ControlsBar.h"
30 #import "CompatibilityFixes.h"
32 /*****************************************************************************
35 * Missing extension to NSWindow
36 *****************************************************************************/
38 @implementation VLCWindow
40 @synthesize hasActiveVideo=b_has_active_video;
41 @synthesize fullscreen=b_fullscreen;
43 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
44 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
46 self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
48 /* we don't want this window to be restored on relaunch */
49 if (!OSX_SNOW_LEOPARD)
50 [self setRestorable:NO];
55 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
57 b_isset_canBecomeKeyWindow = YES;
58 b_canBecomeKeyWindow = canBecomeKey;
61 - (BOOL)canBecomeKeyWindow
63 if (b_isset_canBecomeKeyWindow)
64 return b_canBecomeKeyWindow;
66 return [super canBecomeKeyWindow];
69 - (void)setCanBecomeMainWindow: (BOOL)canBecomeMain
71 b_isset_canBecomeMainWindow = YES;
72 b_canBecomeMainWindow = canBecomeMain;
75 - (BOOL)canBecomeMainWindow
77 if (b_isset_canBecomeMainWindow)
78 return b_canBecomeMainWindow;
80 return [super canBecomeMainWindow];
83 - (void)closeAndAnimate: (BOOL)animate
92 // TODO this callback stuff does not work and is not needed
93 invoc = [[[NSInvocation alloc] init] autorelease];
94 [invoc setSelector:@selector(close)];
95 [invoc setTarget: self];
97 if (![self isVisible] || [self alphaValue] == 0.0) {
102 [self orderOut: self animate: YES callback: invoc];
105 - (void)orderOut: (id)sender animate: (BOOL)animate
107 NSInvocation *invoc = [[[NSInvocation alloc] init] autorelease];
108 [invoc setSelector:@selector(orderOut:)];
109 [invoc setTarget: self];
110 [invoc setArgument: sender atIndex: 2];
111 [self orderOut: sender animate: animate callback: invoc];
114 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
116 NSViewAnimation *anim;
117 NSViewAnimation *current_anim;
118 NSMutableDictionary *dict;
121 [self orderOut: sender];
125 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
127 [dict setObject:self forKey:NSViewAnimationTargetKey];
129 [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
130 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
133 [anim setAnimationBlockingMode:NSAnimationNonblocking];
134 [anim setDuration:0.9];
135 [anim setFrameRate:30];
136 [anim setUserInfo:callback];
137 [anim setDelegate:self];
139 @synchronized(self) {
140 current_anim = self->o_current_animation;
142 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating]) {
146 [current_anim stopAnimation];
147 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
148 [current_anim release];
151 [anim setCurrentProgress:1.0 - [self alphaValue]];
152 self->o_current_animation = anim;
153 [anim startAnimation];
158 - (void)orderFront: (id)sender animate: (BOOL)animate
160 NSViewAnimation *anim;
161 NSViewAnimation *current_anim;
162 NSMutableDictionary *dict;
165 [super orderFront: sender];
166 [self setAlphaValue: 1.0];
170 if (![self isVisible]) {
171 [self setAlphaValue: 0.0];
172 [super orderFront: sender];
174 else if ([self alphaValue] == 1.0) {
175 [super orderFront: self];
179 dict = [[NSMutableDictionary alloc] initWithCapacity:2];
181 [dict setObject:self forKey:NSViewAnimationTargetKey];
183 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
184 anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
187 [anim setAnimationBlockingMode:NSAnimationNonblocking];
188 [anim setDuration:0.5];
189 [anim setFrameRate:30];
190 [anim setDelegate:self];
192 @synchronized(self) {
193 current_anim = self->o_current_animation;
195 if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating]) {
199 [current_anim stopAnimation];
200 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
201 [current_anim release];
204 [anim setCurrentProgress:[self alphaValue]];
205 self->o_current_animation = anim;
206 [self orderFront: sender];
207 [anim startAnimation];
212 - (void)animationDidEnd:(NSAnimation*)anim
214 if ([self alphaValue] <= 0.0) {
215 NSInvocation * invoc;
216 [super orderOut: nil];
217 [self setAlphaValue: 1.0];
218 if ((invoc = [anim userInfo])) {
224 - (VLCVoutView *)videoView
226 if ([[self contentView] class] == [VLCVoutView class])
227 return (VLCVoutView *)[self contentView];
232 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
235 screen = [self screen];
236 NSRect screenRect = [screen frame];
237 NSRect constrainedRect = [super constrainFrameRect:frameRect toScreen:screen];
241 * With Mavericks, there is a nasty bug resulting in grey bars on top in fullscreen mode.
242 * It looks like this is enforced by the os because the window is in the way for the menu bar.
244 * According to the documentation, this constraining can be changed by overwriting this
245 * method. But in this situation, even the received frameRect is already contrained with the
246 * menu bars height substracted. This case is detected here, and the full height is
249 * See #9469 and radar://15583566
252 BOOL b_inFullscreen = [self fullscreen] || ([self respondsToSelector:@selector(inFullscreenTransition)] && [(VLCVideoWindowCommon *)self inFullscreenTransition]);
254 if(OSX_MAVERICKS && b_inFullscreen && constrainedRect.size.width == screenRect.size.width
255 && constrainedRect.size.height != screenRect.size.height
256 && abs(screenRect.size.height - constrainedRect.size.height) <= 25.) {
258 msg_Dbg(VLCIntf, "Contrain window height %.1f to screen height %.1f",
259 constrainedRect.size.height, screenRect.size.height);
260 constrainedRect.size.height = screenRect.size.height;
263 return constrainedRect;
269 /*****************************************************************************
270 * VLCVideoWindowCommon
272 * Common code for main window, detached window and extra video window
273 *****************************************************************************/
275 @interface VLCVideoWindowCommon (Internal)
276 - (void)customZoom:(id)sender;
277 - (void)hasBecomeFullscreen;
278 - (void)leaveFullscreenAndFadeOut:(BOOL)fadeout;
279 - (void)hasEndedFullscreen;
282 @implementation VLCVideoWindowCommon
284 @synthesize videoView=o_video_view;
285 @synthesize controlsBar=o_controls_bar;
286 @synthesize inFullscreenTransition=b_in_fullscreen_transition;
291 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
292 backing:(NSBackingStoreType)backingType defer:(BOOL)flag
294 b_dark_interface = config_GetInt(VLCIntf, "macosx-interfacestyle");
296 if (b_dark_interface) {
297 styleMask = NSBorderlessWindowMask;
298 #ifdef MAC_OS_X_VERSION_10_7
299 if (!OSX_SNOW_LEOPARD)
300 styleMask |= NSResizableWindowMask;
304 self = [super initWithContentRect:contentRect styleMask:styleMask
305 backing:backingType defer:flag];
307 /* we want to be moveable regardless of our style */
308 [self setMovableByWindowBackground: YES];
309 [self setCanBecomeKeyWindow:YES];
311 o_temp_view = [[NSView alloc] init];
312 [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
319 [o_temp_view release];
325 BOOL b_nativeFullscreenMode = NO;
326 #ifdef MAC_OS_X_VERSION_10_7
327 if (!OSX_SNOW_LEOPARD)
328 b_nativeFullscreenMode = var_InheritBool(VLCIntf, "macosx-nativefullscreenmode");
331 if (b_nativeFullscreenMode) {
332 [self setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
334 [o_titlebar_view setFullscreenButtonHidden: YES];
337 [super awakeFromNib];
340 - (void)setTitle:(NSString *)title
342 if (!title || [title length] < 1)
345 if (b_dark_interface && o_titlebar_view)
346 [o_titlebar_view setWindowTitle: title];
348 [super setTitle: title];
352 #pragma mark zoom / minimize / close
354 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
356 SEL s_menuAction = [menuItem action];
358 if ((s_menuAction == @selector(performClose:)) || (s_menuAction == @selector(performMiniaturize:)) || (s_menuAction == @selector(performZoom:)))
361 return [super validateMenuItem:menuItem];
364 - (BOOL)windowShouldClose:(id)sender
369 - (void)performClose:(id)sender
371 if (!([self styleMask] & NSTitledWindowMask)) {
372 [[NSNotificationCenter defaultCenter] postNotificationName:NSWindowWillCloseNotification object:self];
376 [super performClose: sender];
379 - (void)performMiniaturize:(id)sender
381 if (!([self styleMask] & NSTitledWindowMask))
382 [self miniaturize: sender];
384 [super performMiniaturize: sender];
387 - (void)performZoom:(id)sender
389 if (!([self styleMask] & NSTitledWindowMask))
390 [self customZoom: sender];
392 [super performZoom: sender];
395 - (void)zoom:(id)sender
397 if (!([self styleMask] & NSTitledWindowMask))
398 [self customZoom: sender];
400 [super zoom: sender];
404 * Given a proposed frame rectangle, return a modified version
405 * which will fit inside the screen.
407 * This method is based upon NSWindow.m, part of the GNUstep GUI Library, licensed under LGPLv2+.
408 * Authors: Scott Christley <scottc@net-community.com>, Venkat Ajjanagadde <venkat@ocbi.com>,
409 * Felipe A. Rodriguez <far@ix.netcom.com>, Richard Frith-Macdonald <richard@brainstorm.co.uk>
410 * Copyright (C) 1996 Free Software Foundation, Inc.
412 - (NSRect) customConstrainFrameRect: (NSRect)frameRect toScreen: (NSScreen*)screen
414 NSRect screenRect = [screen visibleFrame];
417 /* Move top edge of the window inside the screen */
418 difference = NSMaxY (frameRect) - NSMaxY (screenRect);
419 if (difference > 0) {
420 frameRect.origin.y -= difference;
423 /* If the window is resizable, resize it (if needed) so that the
424 bottom edge is on the screen or can be on the screen when the user moves
426 difference = NSMaxY (screenRect) - NSMaxY (frameRect);
427 if (_styleMask & NSResizableWindowMask) {
430 difference2 = screenRect.origin.y - frameRect.origin.y;
431 difference2 -= difference;
432 // Take in account the space between the top of window and the top of the
433 // screen which can be used to move the bottom of the window on the screen
434 if (difference2 > 0) {
435 frameRect.size.height -= difference2;
436 frameRect.origin.y += difference2;
439 /* Ensure that resizing doesn't makewindow smaller than minimum */
440 difference2 = [self minSize].height - frameRect.size.height;
441 if (difference2 > 0) {
442 frameRect.size.height += difference2;
443 frameRect.origin.y -= difference2;
453 Zooms the receiver. This method calls the delegate method
454 windowShouldZoom:toFrame: to determine if the window should
455 be allowed to zoom to full screen.
457 * This method is based upon NSWindow.m, part of the GNUstep GUI Library, licensed under LGPLv2+.
458 * Authors: Scott Christley <scottc@net-community.com>, Venkat Ajjanagadde <venkat@ocbi.com>,
459 * Felipe A. Rodriguez <far@ix.netcom.com>, Richard Frith-Macdonald <richard@brainstorm.co.uk>
460 * Copyright (C) 1996 Free Software Foundation, Inc.
462 - (void) customZoom: (id)sender
464 NSRect maxRect = [[self screen] visibleFrame];
465 NSRect currentFrame = [self frame];
467 if ([[self delegate] respondsToSelector: @selector(windowWillUseStandardFrame:defaultFrame:)]) {
468 maxRect = [[self delegate] windowWillUseStandardFrame: self defaultFrame: maxRect];
471 maxRect = [self customConstrainFrameRect: maxRect toScreen: [self screen]];
473 // Compare the new frame with the current one
474 if ((abs(NSMaxX(maxRect) - NSMaxX(currentFrame)) < DIST)
475 && (abs(NSMaxY(maxRect) - NSMaxY(currentFrame)) < DIST)
476 && (abs(NSMinX(maxRect) - NSMinX(currentFrame)) < DIST)
477 && (abs(NSMinY(maxRect) - NSMinY(currentFrame)) < DIST)) {
478 // Already in zoomed mode, reset user frame, if stored
479 if ([self frameAutosaveName] != nil) {
480 [self setFrame: previousSavedFrame display: YES animate: YES];
481 [self saveFrameUsingName: [self frameAutosaveName]];
486 if ([self frameAutosaveName] != nil) {
487 [self saveFrameUsingName: [self frameAutosaveName]];
488 previousSavedFrame = [self frame];
491 [self setFrame: maxRect display: YES animate: YES];
495 #pragma mark Video window resizing logic
497 - (void)setWindowLevel:(NSInteger)i_state
499 if (var_InheritBool(VLCIntf, "video-wallpaper") || [self level] < NSNormalWindowLevel)
502 if (!b_fullscreen && !b_in_fullscreen_transition)
503 [self setLevel: i_state];
505 // save it for restore if window is currently minimized or in fullscreen
506 i_originalLevel = i_state;
509 - (NSRect)getWindowRectForProposedVideoViewSize:(NSSize)size
511 NSSize windowMinSize = [self minSize];
512 NSRect screenFrame = [[self screen] visibleFrame];
514 NSPoint topleftbase = NSMakePoint(0, [self frame].size.height);
515 NSPoint topleftscreen = [self convertBaseToScreen: topleftbase];
517 unsigned int i_width = size.width;
518 unsigned int i_height = size.height;
519 if (i_width < windowMinSize.width)
520 i_width = windowMinSize.width;
521 if (i_height < f_min_video_height)
522 i_height = f_min_video_height;
524 /* Calculate the window's new size */
526 new_frame.size.width = [self frame].size.width - [o_video_view frame].size.width + i_width;
527 new_frame.size.height = [self frame].size.height - [o_video_view frame].size.height + i_height;
528 new_frame.origin.x = topleftscreen.x;
529 new_frame.origin.y = topleftscreen.y - new_frame.size.height;
531 /* make sure the window doesn't exceed the screen size the window is on */
532 if (new_frame.size.width > screenFrame.size.width) {
533 new_frame.size.width = screenFrame.size.width;
534 new_frame.origin.x = screenFrame.origin.x;
536 if (new_frame.size.height > screenFrame.size.height) {
537 new_frame.size.height = screenFrame.size.height;
538 new_frame.origin.y = screenFrame.origin.y;
540 if (new_frame.origin.y < screenFrame.origin.y)
541 new_frame.origin.y = screenFrame.origin.y;
543 CGFloat right_screen_point = screenFrame.origin.x + screenFrame.size.width;
544 CGFloat right_window_point = new_frame.origin.x + new_frame.size.width;
545 if (right_window_point > right_screen_point)
546 new_frame.origin.x -= (right_window_point - right_screen_point);
553 // VOUT_WINDOW_SET_SIZE is triggered when exiting fullscreen. This event is ignored here
554 // to avoid interference with the animation.
555 if ([self fullscreen] || b_in_fullscreen_transition)
558 NSRect window_rect = [self getWindowRectForProposedVideoViewSize:nativeVideoSize];
559 [[self animator] setFrame:window_rect display:YES];
562 - (void)setNativeVideoSize:(NSSize)size
564 nativeVideoSize = size;
566 if (var_InheritBool(VLCIntf, "macosx-video-autoresize") && !var_InheritBool(VLCIntf, "video-wallpaper"))
570 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
572 if (![[VLCMain sharedInstance] activeVideoPlayback] || nativeVideoSize.width == 0. || nativeVideoSize.height == 0. || window != self)
573 return proposedFrameSize;
575 // needed when entering lion fullscreen mode
576 if (b_in_fullscreen_transition || [self fullscreen])
577 return proposedFrameSize;
579 if ([o_video_view isHidden])
580 return proposedFrameSize;
582 if ([[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]) {
583 NSRect videoWindowFrame = [self frame];
584 NSRect viewRect = [o_video_view convertRect:[o_video_view bounds] toView: nil];
585 NSRect contentRect = [self contentRectForFrameRect:videoWindowFrame];
586 float marginy = viewRect.origin.y + videoWindowFrame.size.height - contentRect.size.height;
587 float marginx = contentRect.size.width - viewRect.size.width;
588 if (o_titlebar_view && b_dark_interface)
589 marginy += [o_titlebar_view frame].size.height;
591 proposedFrameSize.height = (proposedFrameSize.width - marginx) * nativeVideoSize.height / nativeVideoSize.width + marginy;
594 return proposedFrameSize;
597 - (void)windowWillMiniaturize:(NSNotification *)notification
599 // Set level to normal as a workaround for Mavericks bug causing window
600 // to vanish from screen, see radar://15473716
601 i_originalLevel = [self level];
602 [self setLevel: NSNormalWindowLevel];
605 - (void)windowDidDeminiaturize:(NSNotification *)notification
607 [self setLevel: i_originalLevel];
611 #pragma mark Mouse cursor handling
613 // NSTimer selectors require this function signature as per Apple's docs
614 - (void)hideMouseCursor:(NSTimer *)timer
616 [NSCursor setHiddenUntilMouseMoves: YES];
619 - (void)recreateHideMouseTimer
621 if (t_hide_mouse_timer != nil) {
622 [t_hide_mouse_timer invalidate];
623 [t_hide_mouse_timer release];
626 t_hide_mouse_timer = [NSTimer scheduledTimerWithTimeInterval:2
628 selector:@selector(hideMouseCursor:)
631 [t_hide_mouse_timer retain];
634 // Called automatically if window's acceptsMouseMovedEvents property is true
635 - (void)mouseMoved:(NSEvent *)theEvent
638 [self recreateHideMouseTimer];
640 [super mouseMoved: theEvent];
644 #pragma mark Lion native fullscreen handling
646 - (void)becomeKeyWindow
648 [super becomeKeyWindow];
650 // change fspanel state for the case when multiple windows are in fullscreen
651 if ([self hasActiveVideo] && [self fullscreen])
652 [[[VLCMainWindow sharedInstance] fsPanel] setActive:nil];
654 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive:nil];
657 - (void)resignKeyWindow
659 [super resignKeyWindow];
661 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive:nil];
664 -(NSArray*)customWindowsToEnterFullScreenForWindow:(NSWindow *)window
666 if (window == self) {
667 return [NSArray arrayWithObject:window];
673 - (NSArray*)customWindowsToExitFullScreenForWindow:(NSWindow*)window
675 if (window == self) {
676 return [NSArray arrayWithObject:window];
682 - (void)window:window startCustomAnimationToEnterFullScreenWithDuration:(NSTimeInterval)duration
684 [window setStyleMask:([window styleMask] | NSFullScreenWindowMask)];
686 NSScreen *screen = [window screen];
687 NSRect screenFrame = [screen frame];
689 [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
690 [context setDuration:0.5 * duration];
691 [[window animator] setFrame:screenFrame display:YES];
692 } completionHandler:nil];
695 - (void)window:window startCustomAnimationToExitFullScreenWithDuration:(NSTimeInterval)duration
697 [window setStyleMask:([window styleMask] & ~NSFullScreenWindowMask)];
698 [[window animator] setFrame:frameBeforeLionFullscreen display:YES animate:YES];
700 [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
701 [context setDuration:0.5 * duration];
702 [[window animator] setFrame:frameBeforeLionFullscreen display:YES animate:YES];
703 } completionHandler:nil];
706 - (void)windowWillEnterFullScreen:(NSNotification *)notification
708 // workaround, see #6668
709 [NSApp setPresentationOptions:(NSApplicationPresentationFullScreen | NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
711 i_originalLevel = [self level];
712 // b_fullscreen and b_in_fullscreen_transition must not be true yet
713 [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
714 [self setLevel:NSNormalWindowLevel];
716 b_in_fullscreen_transition = YES;
718 var_SetBool(pl_Get(VLCIntf), "fullscreen", true);
720 frameBeforeLionFullscreen = [self frame];
722 if ([self hasActiveVideo]) {
723 vout_thread_t *p_vout = getVoutForActiveWindow();
725 var_SetBool(p_vout, "fullscreen", true);
726 vlc_object_release(p_vout);
730 if ([self hasActiveVideo])
731 [[VLCMainWindow sharedInstance] recreateHideMouseTimer];
733 if (b_dark_interface) {
734 [o_titlebar_view removeFromSuperviewWithoutNeedingDisplay];
737 CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
738 winrect = [self frame];
740 winrect.size.height = winrect.size.height - f_titleBarHeight;
741 [self setFrame: winrect display:NO animate:NO];
744 [o_video_view setFrame: [[self contentView] frame]];
745 if (![o_video_view isHidden]) {
746 [[o_controls_bar bottomBarView] setHidden: YES];
749 [self setMovableByWindowBackground: NO];
752 - (void)windowDidEnterFullScreen:(NSNotification *)notification
754 // Indeed, we somehow can have an "inactive" fullscreen (but a visible window!).
755 // But this creates some problems when leaving fs over remote intfs, so activate app here.
756 [NSApp activateIgnoringOtherApps:YES];
758 [self setFullscreen: YES];
759 b_in_fullscreen_transition = NO;
761 if ([self hasActiveVideo]) {
762 [[[VLCMainWindow sharedInstance] fsPanel] setVoutWasUpdated: self];
763 if (![o_video_view isHidden])
764 [[[VLCMainWindow sharedInstance] fsPanel] setActive: nil];
767 NSArray *subviews = [[self videoView] subviews];
768 NSUInteger count = [subviews count];
770 for (NSUInteger x = 0; x < count; x++) {
771 if ([[subviews objectAtIndex:x] respondsToSelector:@selector(reshape)])
772 [[subviews objectAtIndex:x] reshape];
777 - (void)windowWillExitFullScreen:(NSNotification *)notification
779 b_in_fullscreen_transition = YES;
780 [self setFullscreen: NO];
782 var_SetBool(pl_Get(VLCIntf), "fullscreen", false);
784 if ([self hasActiveVideo]) {
785 vout_thread_t *p_vout = getVoutForActiveWindow();
787 var_SetBool(p_vout, "fullscreen", false);
788 vlc_object_release(p_vout);
792 [NSCursor setHiddenUntilMouseMoves: NO];
793 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
796 if (b_dark_interface) {
798 CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
800 winrect = [o_video_view frame];
801 winrect.size.height -= f_titleBarHeight;
802 [o_video_view setFrame: winrect];
804 winrect = [self frame];
805 [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
806 winrect.size.width, f_titleBarHeight)];
807 [[self contentView] addSubview: o_titlebar_view];
809 winrect.size.height = winrect.size.height + f_titleBarHeight;
810 [self setFrame: winrect display:NO animate:NO];
813 NSRect videoViewFrame = [o_video_view frame];
814 videoViewFrame.origin.y += [o_controls_bar height];
815 videoViewFrame.size.height -= [o_controls_bar height];
816 [o_video_view setFrame: videoViewFrame];
818 if (![o_video_view isHidden]) {
819 [[o_controls_bar bottomBarView] setHidden: NO];
822 [self setMovableByWindowBackground: YES];
825 - (void)windowDidExitFullScreen:(NSNotification *)notification
827 b_in_fullscreen_transition = NO;
829 [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: i_originalLevel];
830 [self setLevel:i_originalLevel];
834 #pragma mark Fullscreen Logic
836 - (void)enterFullscreen
838 NSMutableDictionary *dict1, *dict2;
842 BOOL blackout_other_displays = var_InheritBool(VLCIntf, "macosx-black");
844 screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
847 msg_Dbg(VLCIntf, "chosen screen isn't present, using current screen for fullscreen mode");
848 screen = [self screen];
851 msg_Dbg(VLCIntf, "Using deepest screen");
852 screen = [NSScreen deepestScreen];
855 screen_rect = [screen frame];
858 [o_controls_bar setFullscreenState:YES];
859 [[[VLCMainWindow sharedInstance] controlsBar] setFullscreenState:YES];
861 [[VLCMainWindow sharedInstance] recreateHideMouseTimer];
863 if (blackout_other_displays)
864 [screen blackoutOtherScreens];
866 /* Make sure we don't see the window flashes in float-on-top mode */
867 i_originalLevel = [self level];
868 // b_fullscreen must not be true yet
869 [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
870 [self setLevel:NSNormalWindowLevel];
872 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
873 if (!o_fullscreen_window) {
874 /* We can't change the styleMask of an already created NSWindow, so we create another window, and do eye catching stuff */
876 rect = [[o_video_view superview] convertRect: [o_video_view frame] toView: nil]; /* Convert to Window base coord */
877 rect.origin.x += [self frame].origin.x;
878 rect.origin.y += [self frame].origin.y;
879 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
880 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
881 [o_fullscreen_window setCanBecomeKeyWindow: YES];
882 [o_fullscreen_window setCanBecomeMainWindow: YES];
883 [o_fullscreen_window setHasActiveVideo: YES];
884 [o_fullscreen_window setFullscreen: YES];
886 if (![self isVisible] || [self alphaValue] == 0.0) {
887 /* We don't animate if we are not visible, instead we
888 * simply fade the display */
889 CGDisplayFadeReservationToken token;
891 if (blackout_other_displays) {
892 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
893 CGDisplayFade(token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES);
896 [screen setFullscreenPresentationOptions];
898 [o_video_view retain];
899 [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
900 [o_temp_view setFrame:[o_video_view frame]];
901 [o_fullscreen_window setContentView:o_video_view];
902 [o_video_view release];
904 [o_fullscreen_window makeKeyAndOrderFront:self];
905 [o_fullscreen_window orderFront:self animate:YES];
907 [o_fullscreen_window setFrame:screen_rect display:YES animate:YES];
908 [o_fullscreen_window setLevel:NSNormalWindowLevel];
910 if (blackout_other_displays) {
911 CGDisplayFade(token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO);
912 CGReleaseDisplayFadeReservation(token);
915 /* Will release the lock */
916 [self hasBecomeFullscreen];
921 /* Make sure video view gets visible in case the playlist was visible before */
922 b_video_view_was_hidden = [o_video_view isHidden];
923 [o_video_view setHidden: NO];
925 /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
926 NSDisableScreenUpdates();
927 [o_video_view retain];
928 [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
929 [o_temp_view setFrame:[o_video_view frame]];
930 [o_fullscreen_window setContentView:o_video_view];
931 [o_video_view release];
932 [o_fullscreen_window makeKeyAndOrderFront:self];
933 NSEnableScreenUpdates();
936 /* We are in fullscreen (and no animation is running) */
937 if ([self fullscreen]) {
938 /* Make sure we are hidden */
939 [self orderOut: self];
944 if (o_fullscreen_anim1) {
945 [o_fullscreen_anim1 stopAnimation];
946 [o_fullscreen_anim1 release];
948 if (o_fullscreen_anim2) {
949 [o_fullscreen_anim2 stopAnimation];
950 [o_fullscreen_anim2 release];
953 [screen setFullscreenPresentationOptions];
955 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
956 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
958 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
959 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
961 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
962 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
963 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
965 /* Strategy with NSAnimation allocation:
966 - Keep at most 2 animation at a time
967 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
969 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
970 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
975 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
976 [o_fullscreen_anim1 setDuration: 0.3];
977 [o_fullscreen_anim1 setFrameRate: 30];
978 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
979 [o_fullscreen_anim2 setDuration: 0.2];
980 [o_fullscreen_anim2 setFrameRate: 30];
982 [o_fullscreen_anim2 setDelegate: self];
983 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
985 [o_fullscreen_anim1 startAnimation];
986 /* fullscreenAnimation will be unlocked when animation ends */
988 b_in_fullscreen_transition = YES;
991 - (void)hasBecomeFullscreen
993 if ([[o_video_view subviews] count] > 0)
994 [o_fullscreen_window makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
996 [o_fullscreen_window makeKeyWindow];
997 [o_fullscreen_window setAcceptsMouseMovedEvents: YES];
999 /* tell the fspanel to move itself to front next time it's triggered */
1000 [[[VLCMainWindow sharedInstance] fsPanel] setVoutWasUpdated: o_fullscreen_window];
1001 [[[VLCMainWindow sharedInstance] fsPanel] setActive: nil];
1003 if ([self isVisible])
1004 [self orderOut: self];
1006 b_in_fullscreen_transition = NO;
1007 [self setFullscreen:YES];
1010 - (void)leaveFullscreen
1012 [self leaveFullscreenAndFadeOut: NO];
1015 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
1017 NSMutableDictionary *dict1, *dict2;
1019 BOOL blackout_other_displays = var_InheritBool(VLCIntf, "macosx-black");
1022 [o_controls_bar setFullscreenState:NO];
1023 [[[VLCMainWindow sharedInstance] controlsBar] setFullscreenState:NO];
1025 /* We always try to do so */
1026 [NSScreen unblackoutScreens];
1028 [[o_video_view window] makeKeyAndOrderFront: nil];
1030 /* Don't do anything if o_fullscreen_window is already closed */
1031 if (!o_fullscreen_window) {
1036 /* We don't animate if we are not visible, instead we
1037 * simply fade the display */
1038 CGDisplayFadeReservationToken token;
1040 if (blackout_other_displays) {
1041 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
1042 CGDisplayFade(token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES);
1045 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
1046 [[o_fullscreen_window screen] setNonFullscreenPresentationOptions];
1048 /* Will release the lock */
1049 [self hasEndedFullscreen];
1051 /* Our window is hidden, and might be faded. We need to workaround that, so note it
1053 b_window_is_invisible = YES;
1055 if (blackout_other_displays) {
1056 CGDisplayFade(token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO);
1057 CGReleaseDisplayFadeReservation(token);
1063 b_in_fullscreen_transition = YES;
1065 [self setAlphaValue: 0.0];
1066 [self orderFront: self];
1067 [[o_video_view window] orderFront: self];
1069 [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
1070 [[o_fullscreen_window screen] setNonFullscreenPresentationOptions];
1072 if (o_fullscreen_anim1) {
1073 [o_fullscreen_anim1 stopAnimation];
1074 [o_fullscreen_anim1 release];
1076 if (o_fullscreen_anim2) {
1077 [o_fullscreen_anim2 stopAnimation];
1078 [o_fullscreen_anim2 release];
1081 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
1082 frame.origin.x += [self frame].origin.x;
1083 frame.origin.y += [self frame].origin.y;
1085 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
1086 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
1087 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1089 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
1092 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
1093 [o_fullscreen_anim2 setDuration: 0.3];
1094 [o_fullscreen_anim2 setFrameRate: 30];
1096 [o_fullscreen_anim2 setDelegate: self];
1098 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
1100 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
1101 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
1102 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
1104 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
1107 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
1108 [o_fullscreen_anim1 setDuration: 0.2];
1109 [o_fullscreen_anim1 setFrameRate: 30];
1110 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
1112 /* Make sure o_fullscreen_window is the frontmost window */
1113 [o_fullscreen_window orderFront: self];
1115 [o_fullscreen_anim1 startAnimation];
1116 /* fullscreenAnimation will be unlocked when animation ends */
1119 - (void)hasEndedFullscreen
1121 [self setFullscreen:NO];
1122 b_in_fullscreen_transition = NO;
1124 /* This function is private and should be only triggered at the end of the fullscreen change animation */
1125 /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
1126 NSDisableScreenUpdates();
1127 [o_video_view retain];
1128 [o_video_view removeFromSuperviewWithoutNeedingDisplay];
1129 [[o_temp_view superview] replaceSubview:o_temp_view with:o_video_view];
1130 [o_video_view release];
1131 [o_video_view setFrame:[o_temp_view frame]];
1132 if ([[o_video_view subviews] count] > 0)
1133 [self makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
1135 [o_video_view setHidden: b_video_view_was_hidden];
1137 [super makeKeyAndOrderFront:self]; /* our version (in main window) contains a workaround */
1139 [o_fullscreen_window orderOut: self];
1140 NSEnableScreenUpdates();
1142 [o_fullscreen_window release];
1143 o_fullscreen_window = nil;
1145 [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: i_originalLevel];
1146 [self setLevel:i_originalLevel];
1148 [self setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
1150 // if we quit fullscreen because there is no video anymore, make sure non-embedded window is not visible
1151 if (![[VLCMain sharedInstance] activeVideoPlayback] && [self class] != [VLCMainWindow class])
1152 [self orderOut: self];
1155 - (void)animationDidEnd:(NSAnimation*)animation
1157 NSArray *viewAnimations;
1158 if (o_makekey_anim == animation) {
1159 [o_makekey_anim release];
1162 if ([animation currentValue] < 1.0)
1165 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
1166 viewAnimations = [o_fullscreen_anim2 viewAnimations];
1167 if ([viewAnimations count] >=1 &&
1168 [[[viewAnimations objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect]) {
1169 /* Fullscreen ended */
1170 [self hasEndedFullscreen];
1172 /* Fullscreen started */
1173 [self hasBecomeFullscreen];
1176 - (void)orderOut:(id)sender
1178 [super orderOut:sender];
1181 * TODO reimplement leaveFullscreenAndFadeOut:YES, or remove code
1182 * and the hack below
1184 if (![NSStringFromClass([self class]) isEqualToString:@"VLCMainWindow"]) {
1185 [self leaveFullscreenAndFadeOut:YES];
1190 - (void)makeKeyAndOrderFront: (id)sender
1193 * when we exit fullscreen and fade out, we may endup in
1194 * having a window that is faded. We can't have it fade in unless we
1197 if (!b_window_is_invisible) {
1198 /* Make sure we don't do it too much */
1199 [super makeKeyAndOrderFront: sender];
1203 [super setAlphaValue:0.0f];
1204 [super makeKeyAndOrderFront: sender];
1206 NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
1207 [dict setObject:self forKey:NSViewAnimationTargetKey];
1208 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1210 o_makekey_anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
1213 [o_makekey_anim setAnimationBlockingMode: NSAnimationNonblocking];
1214 [o_makekey_anim setDuration: 0.1];
1215 [o_makekey_anim setFrameRate: 30];
1216 [o_makekey_anim setDelegate: self];
1218 [o_makekey_anim startAnimation];
1219 b_window_is_invisible = NO;
1221 /* fullscreenAnimation will be unlocked when animation ends */
1226 #pragma mark Accessibility stuff
1228 - (NSArray *)accessibilityAttributeNames
1230 if (!b_dark_interface || !o_titlebar_view)
1231 return [super accessibilityAttributeNames];
1233 static NSMutableArray *attributes = nil;
1234 if (attributes == nil) {
1235 attributes = [[super accessibilityAttributeNames] mutableCopy];
1236 NSArray *appendAttributes = [NSArray arrayWithObjects:NSAccessibilitySubroleAttribute,
1237 NSAccessibilityCloseButtonAttribute,
1238 NSAccessibilityMinimizeButtonAttribute,
1239 NSAccessibilityZoomButtonAttribute, nil];
1241 for(NSString *attribute in appendAttributes) {
1242 if (![attributes containsObject:attribute])
1243 [attributes addObject:attribute];
1249 - (id)accessibilityAttributeValue: (NSString*)o_attribute_name
1251 if (b_dark_interface && o_titlebar_view) {
1252 VLCMainWindowTitleView *o_tbv = o_titlebar_view;
1254 if ([o_attribute_name isEqualTo: NSAccessibilitySubroleAttribute])
1255 return NSAccessibilityStandardWindowSubrole;
1257 if ([o_attribute_name isEqualTo: NSAccessibilityCloseButtonAttribute])
1258 return [[o_tbv closeButton] cell];
1260 if ([o_attribute_name isEqualTo: NSAccessibilityMinimizeButtonAttribute])
1261 return [[o_tbv minimizeButton] cell];
1263 if ([o_attribute_name isEqualTo: NSAccessibilityZoomButtonAttribute])
1264 return [[o_tbv zoomButton] cell];
1267 return [super accessibilityAttributeValue: o_attribute_name];