]> git.sesse.net Git - vlc/blob - modules/gui/macosx/Windows.m
macosx: fix recursive inclusion of the compatibility header
[vlc] / modules / gui / macosx / Windows.m
1 /*****************************************************************************
2  * Windows.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 "Windows.h"
26 #import "intf.h"
27 #import "CoreInteraction.h"
28 #import "ControlsBar.h"
29 #import "VideoView.h"
30 #import "CompatibilityFixes.h"
31
32 /*****************************************************************************
33  * VLCWindow
34  *
35  *  Missing extension to NSWindow
36  *****************************************************************************/
37
38 @implementation VLCWindow
39
40 @synthesize hasActiveVideo=b_has_active_video;
41 @synthesize fullscreen=b_fullscreen;
42
43 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
44                   backing:(NSBackingStoreType)backingType defer:(BOOL)flag
45 {
46     self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag];
47     if (self) {
48         /* we don't want this window to be restored on relaunch */
49         if (!OSX_SNOW_LEOPARD)
50             [self setRestorable:NO];
51     }
52     return self;
53 }
54
55 - (void)setCanBecomeKeyWindow: (BOOL)canBecomeKey
56 {
57     b_isset_canBecomeKeyWindow = YES;
58     b_canBecomeKeyWindow = canBecomeKey;
59 }
60
61 - (BOOL)canBecomeKeyWindow
62 {
63     if (b_isset_canBecomeKeyWindow)
64         return b_canBecomeKeyWindow;
65
66     return [super canBecomeKeyWindow];
67 }
68
69 - (void)setCanBecomeMainWindow: (BOOL)canBecomeMain
70 {
71     b_isset_canBecomeMainWindow = YES;
72     b_canBecomeMainWindow = canBecomeMain;
73 }
74
75 - (BOOL)canBecomeMainWindow
76 {
77     if (b_isset_canBecomeMainWindow)
78         return b_canBecomeMainWindow;
79
80     return [super canBecomeMainWindow];
81 }
82
83 - (void)closeAndAnimate: (BOOL)animate
84 {
85     NSInvocation *invoc;
86
87     if (!animate) {
88         [super close];
89         return;
90     }
91
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];
96
97     if (![self isVisible] || [self alphaValue] == 0.0) {
98         [super close];
99         return;
100     }
101
102     [self orderOut: self animate: YES callback: invoc];
103 }
104
105 - (void)orderOut: (id)sender animate: (BOOL)animate
106 {
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];
112 }
113
114 - (void)orderOut: (id)sender animate: (BOOL)animate callback:(NSInvocation *)callback
115 {
116     NSViewAnimation *anim;
117     NSViewAnimation *current_anim;
118     NSMutableDictionary *dict;
119
120     if (!animate) {
121         [self orderOut: sender];
122         return;
123     }
124
125     dict = [[NSMutableDictionary alloc] initWithCapacity:2];
126
127     [dict setObject:self forKey:NSViewAnimationTargetKey];
128
129     [dict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
130     anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
131     [dict release];
132
133     [anim setAnimationBlockingMode:NSAnimationNonblocking];
134     [anim setDuration:0.9];
135     [anim setFrameRate:30];
136     [anim setUserInfo:callback];
137     [anim setDelegate:self];
138
139     @synchronized(self) {
140         current_anim = self->o_current_animation;
141
142         if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeOutEffect && [current_anim isAnimating]) {
143             [anim release];
144         } else {
145             if (current_anim) {
146                 [current_anim stopAnimation];
147                 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
148                 [current_anim release];
149             }
150             else
151                 [anim setCurrentProgress:1.0 - [self alphaValue]];
152             self->o_current_animation = anim;
153             [anim startAnimation];
154         }
155     }
156 }
157
158 - (void)orderFront: (id)sender animate: (BOOL)animate
159 {
160     NSViewAnimation *anim;
161     NSViewAnimation *current_anim;
162     NSMutableDictionary *dict;
163
164     if (!animate) {
165         [super orderFront: sender];
166         [self setAlphaValue: 1.0];
167         return;
168     }
169
170     if (![self isVisible]) {
171         [self setAlphaValue: 0.0];
172         [super orderFront: sender];
173     }
174     else if ([self alphaValue] == 1.0) {
175         [super orderFront: self];
176         return;
177     }
178
179     dict = [[NSMutableDictionary alloc] initWithCapacity:2];
180
181     [dict setObject:self forKey:NSViewAnimationTargetKey];
182
183     [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
184     anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
185     [dict release];
186
187     [anim setAnimationBlockingMode:NSAnimationNonblocking];
188     [anim setDuration:0.5];
189     [anim setFrameRate:30];
190     [anim setDelegate:self];
191
192     @synchronized(self) {
193         current_anim = self->o_current_animation;
194
195         if ([[[current_anim viewAnimations] objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] == NSViewAnimationFadeInEffect && [current_anim isAnimating]) {
196             [anim release];
197         } else {
198             if (current_anim) {
199                 [current_anim stopAnimation];
200                 [anim setCurrentProgress:1.0 - [current_anim currentProgress]];
201                 [current_anim release];
202             }
203             else
204                 [anim setCurrentProgress:[self alphaValue]];
205             self->o_current_animation = anim;
206             [self orderFront: sender];
207             [anim startAnimation];
208         }
209     }
210 }
211
212 - (void)animationDidEnd:(NSAnimation*)anim
213 {
214     if ([self alphaValue] <= 0.0) {
215         NSInvocation * invoc;
216         [super orderOut: nil];
217         [self setAlphaValue: 1.0];
218         if ((invoc = [anim userInfo])) {
219             [invoc invoke];
220         }
221     }
222 }
223
224 - (VLCVoutView *)videoView
225 {
226     if ([[self contentView] class] == [VLCVoutView class])
227         return (VLCVoutView *)[self contentView];
228
229     return nil;
230 }
231
232
233 @end
234
235
236 /*****************************************************************************
237  * VLCVideoWindowCommon
238  *
239  *  Common code for main window, detached window and extra video window
240  *****************************************************************************/
241
242 @interface VLCVideoWindowCommon (Internal)
243 - (void)customZoom:(id)sender;
244 - (void)hasBecomeFullscreen;
245 - (void)leaveFullscreenAndFadeOut:(BOOL)fadeout;
246 - (void)hasEndedFullscreen;
247 @end
248
249 @implementation VLCVideoWindowCommon
250
251 @synthesize videoView=o_video_view;
252 @synthesize controlsBar=o_controls_bar;
253 @synthesize inFullscreenTransition=b_in_fullscreen_transition;
254
255 #pragma mark -
256 #pragma mark Init
257
258 - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask
259                   backing:(NSBackingStoreType)backingType defer:(BOOL)flag
260 {
261     b_dark_interface = config_GetInt(VLCIntf, "macosx-interfacestyle");
262
263     if (b_dark_interface) {
264         styleMask = NSBorderlessWindowMask;
265 #ifdef MAC_OS_X_VERSION_10_7
266         if (!OSX_SNOW_LEOPARD)
267             styleMask |= NSResizableWindowMask;
268 #endif
269     }
270
271     self = [super initWithContentRect:contentRect styleMask:styleMask
272                               backing:backingType defer:flag];
273
274     /* we want to be moveable regardless of our style */
275     [self setMovableByWindowBackground: YES];
276     [self setCanBecomeKeyWindow:YES];
277
278     o_temp_view = [[NSView alloc] init];
279     [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
280
281     return self;
282 }
283
284 - (void)dealloc
285 {
286     [o_temp_view release];
287     [super dealloc];
288 }
289
290 - (void)awakeFromNib
291 {
292     BOOL b_nativeFullscreenMode = NO;
293 #ifdef MAC_OS_X_VERSION_10_7
294     if (!OSX_SNOW_LEOPARD)
295         b_nativeFullscreenMode = var_InheritBool(VLCIntf, "macosx-nativefullscreenmode");
296 #endif
297
298     if (b_nativeFullscreenMode) {
299         [self setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
300     } else {
301         [o_titlebar_view setFullscreenButtonHidden: YES];
302     }
303
304     [super awakeFromNib];
305 }
306
307 - (void)setTitle:(NSString *)title
308 {
309     if (!title || [title length] < 1)
310         return;
311
312     if (b_dark_interface && o_titlebar_view)
313         [o_titlebar_view setWindowTitle: title];
314
315     [super setTitle: title];
316 }
317
318 #pragma mark -
319 #pragma mark zoom / minimize / close
320
321 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
322 {
323     SEL s_menuAction = [menuItem action];
324
325     if ((s_menuAction == @selector(performClose:)) || (s_menuAction == @selector(performMiniaturize:)) || (s_menuAction == @selector(performZoom:)))
326         return YES;
327
328     return [super validateMenuItem:menuItem];
329 }
330
331 - (BOOL)windowShouldClose:(id)sender
332 {
333     return YES;
334 }
335
336 - (void)performClose:(id)sender
337 {
338     if (!([self styleMask] & NSTitledWindowMask)) {
339         [[NSNotificationCenter defaultCenter] postNotificationName:NSWindowWillCloseNotification object:self];
340
341         [self close];
342     } else
343         [super performClose: sender];
344 }
345
346 - (void)performMiniaturize:(id)sender
347 {
348     if (!([self styleMask] & NSTitledWindowMask))
349         [self miniaturize: sender];
350     else
351         [super performMiniaturize: sender];
352 }
353
354 - (void)performZoom:(id)sender
355 {
356     if (!([self styleMask] & NSTitledWindowMask))
357         [self customZoom: sender];
358     else
359         [super performZoom: sender];
360 }
361
362 - (void)zoom:(id)sender
363 {
364     if (!([self styleMask] & NSTitledWindowMask))
365         [self customZoom: sender];
366     else
367         [super zoom: sender];
368 }
369
370 /**
371  * Given a proposed frame rectangle, return a modified version
372  * which will fit inside the screen.
373  *
374  * This method is based upon NSWindow.m, part of the GNUstep GUI Library, licensed under LGPLv2+.
375  *    Authors:  Scott Christley <scottc@net-community.com>, Venkat Ajjanagadde <venkat@ocbi.com>,
376  *              Felipe A. Rodriguez <far@ix.netcom.com>, Richard Frith-Macdonald <richard@brainstorm.co.uk>
377  *    Copyright (C) 1996 Free Software Foundation, Inc.
378  */
379 - (NSRect) customConstrainFrameRect: (NSRect)frameRect toScreen: (NSScreen*)screen
380 {
381     NSRect screenRect = [screen visibleFrame];
382     float difference;
383
384     /* Move top edge of the window inside the screen */
385     difference = NSMaxY (frameRect) - NSMaxY (screenRect);
386     if (difference > 0) {
387         frameRect.origin.y -= difference;
388     }
389
390     /* If the window is resizable, resize it (if needed) so that the
391      bottom edge is on the screen or can be on the screen when the user moves
392      the window */
393     difference = NSMaxY (screenRect) - NSMaxY (frameRect);
394     if (_styleMask & NSResizableWindowMask) {
395         float difference2;
396
397         difference2 = screenRect.origin.y - frameRect.origin.y;
398         difference2 -= difference;
399         // Take in account the space between the top of window and the top of the
400         // screen which can be used to move the bottom of the window on the screen
401         if (difference2 > 0) {
402             frameRect.size.height -= difference2;
403             frameRect.origin.y += difference2;
404         }
405
406         /* Ensure that resizing doesn't makewindow smaller than minimum */
407         difference2 = [self minSize].height - frameRect.size.height;
408         if (difference2 > 0) {
409             frameRect.size.height += difference2;
410             frameRect.origin.y -= difference2;
411         }
412     }
413
414     return frameRect;
415 }
416
417 #define DIST 3
418
419 /**
420  Zooms the receiver.   This method calls the delegate method
421  windowShouldZoom:toFrame: to determine if the window should
422  be allowed to zoom to full screen.
423  *
424  * This method is based upon NSWindow.m, part of the GNUstep GUI Library, licensed under LGPLv2+.
425  *    Authors:  Scott Christley <scottc@net-community.com>, Venkat Ajjanagadde <venkat@ocbi.com>,
426  *              Felipe A. Rodriguez <far@ix.netcom.com>, Richard Frith-Macdonald <richard@brainstorm.co.uk>
427  *    Copyright (C) 1996 Free Software Foundation, Inc.
428  */
429 - (void) customZoom: (id)sender
430 {
431     NSRect maxRect = [[self screen] visibleFrame];
432     NSRect currentFrame = [self frame];
433
434     if ([[self delegate] respondsToSelector: @selector(windowWillUseStandardFrame:defaultFrame:)]) {
435         maxRect = [[self delegate] windowWillUseStandardFrame: self defaultFrame: maxRect];
436     }
437
438     maxRect = [self customConstrainFrameRect: maxRect toScreen: [self screen]];
439
440     // Compare the new frame with the current one
441     if ((abs(NSMaxX(maxRect) - NSMaxX(currentFrame)) < DIST)
442         && (abs(NSMaxY(maxRect) - NSMaxY(currentFrame)) < DIST)
443         && (abs(NSMinX(maxRect) - NSMinX(currentFrame)) < DIST)
444         && (abs(NSMinY(maxRect) - NSMinY(currentFrame)) < DIST)) {
445         // Already in zoomed mode, reset user frame, if stored
446         if ([self frameAutosaveName] != nil) {
447             [self setFrame: previousSavedFrame display: YES animate: YES];
448             [self saveFrameUsingName: [self frameAutosaveName]];
449         }
450         return;
451     }
452
453     if ([self frameAutosaveName] != nil) {
454         [self saveFrameUsingName: [self frameAutosaveName]];
455         previousSavedFrame = [self frame];
456     }
457
458     [self setFrame: maxRect display: YES animate: YES];
459 }
460
461 #pragma mark -
462 #pragma mark Video window resizing logic
463
464 - (void)setWindowLevel:(NSInteger)i_state
465 {
466     if (var_InheritBool(VLCIntf, "video-wallpaper") || [self level] < NSNormalWindowLevel)
467         return;
468
469     if (!b_fullscreen && !b_in_fullscreen_transition)
470         [self setLevel: i_state];
471
472     // save it for restore if window is currently minimized or in fullscreen
473     i_originalLevel = i_state;
474 }
475
476 - (NSRect)getWindowRectForProposedVideoViewSize:(NSSize)size
477 {
478     NSSize windowMinSize = [self minSize];
479     NSRect screenFrame = [[self screen] visibleFrame];
480
481     NSPoint topleftbase = NSMakePoint(0, [self frame].size.height);
482     NSPoint topleftscreen = [self convertBaseToScreen: topleftbase];
483
484     unsigned int i_width = size.width;
485     unsigned int i_height = size.height;
486     if (i_width < windowMinSize.width)
487         i_width = windowMinSize.width;
488     if (i_height < f_min_video_height)
489         i_height = f_min_video_height;
490
491     /* Calculate the window's new size */
492     NSRect new_frame;
493     new_frame.size.width = [self frame].size.width - [o_video_view frame].size.width + i_width;
494     new_frame.size.height = [self frame].size.height - [o_video_view frame].size.height + i_height;
495     new_frame.origin.x = topleftscreen.x;
496     new_frame.origin.y = topleftscreen.y - new_frame.size.height;
497
498     /* make sure the window doesn't exceed the screen size the window is on */
499     if (new_frame.size.width > screenFrame.size.width) {
500         new_frame.size.width = screenFrame.size.width;
501         new_frame.origin.x = screenFrame.origin.x;
502     }
503     if (new_frame.size.height > screenFrame.size.height) {
504         new_frame.size.height = screenFrame.size.height;
505         new_frame.origin.y = screenFrame.origin.y;
506     }
507     if (new_frame.origin.y < screenFrame.origin.y)
508         new_frame.origin.y = screenFrame.origin.y;
509
510     CGFloat right_screen_point = screenFrame.origin.x + screenFrame.size.width;
511     CGFloat right_window_point = new_frame.origin.x + new_frame.size.width;
512     if (right_window_point > right_screen_point)
513         new_frame.origin.x -= (right_window_point - right_screen_point);
514
515     return new_frame;
516 }
517
518 - (void)resizeWindow
519 {
520     // VOUT_WINDOW_SET_SIZE is triggered when exiting fullscreen. This event is ignored here
521     // to avoid interference with the animation.
522     if ([self fullscreen] || b_in_fullscreen_transition)
523         return;
524
525     NSRect window_rect = [self getWindowRectForProposedVideoViewSize:nativeVideoSize];
526     [[self animator] setFrame:window_rect display:YES];
527 }
528
529 - (void)setNativeVideoSize:(NSSize)size
530 {
531     nativeVideoSize = size;
532
533     if (var_InheritBool(VLCIntf, "macosx-video-autoresize") && !var_InheritBool(VLCIntf, "video-wallpaper"))
534         [self resizeWindow];
535 }
536
537 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
538 {
539     if (![[VLCMain sharedInstance] activeVideoPlayback] || nativeVideoSize.width == 0. || nativeVideoSize.height == 0. || window != self)
540         return proposedFrameSize;
541
542     // needed when entering lion fullscreen mode
543     if (b_in_fullscreen_transition || [self fullscreen])
544         return proposedFrameSize;
545
546     if ([[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]) {
547         NSRect videoWindowFrame = [self frame];
548         NSRect viewRect = [o_video_view convertRect:[o_video_view bounds] toView: nil];
549         NSRect contentRect = [self contentRectForFrameRect:videoWindowFrame];
550         float marginy = viewRect.origin.y + videoWindowFrame.size.height - contentRect.size.height;
551         float marginx = contentRect.size.width - viewRect.size.width;
552         if (o_titlebar_view && b_dark_interface)
553             marginy += [o_titlebar_view frame].size.height;
554
555         proposedFrameSize.height = (proposedFrameSize.width - marginx) * nativeVideoSize.height / nativeVideoSize.width + marginy;
556     }
557
558     return proposedFrameSize;
559 }
560
561 - (void)windowWillMiniaturize:(NSNotification *)notification
562 {
563     // Set level to normal as a workaround for Mavericks bug causing window
564     // to vanish from screen, see radar://15473716
565     i_originalLevel = [self level];
566     [self setLevel: NSNormalWindowLevel];
567 }
568
569 - (void)windowDidDeminiaturize:(NSNotification *)notification
570 {
571     [self setLevel: i_originalLevel];
572 }
573
574 #pragma mark -
575 #pragma mark Mouse cursor handling
576
577 //  NSTimer selectors require this function signature as per Apple's docs
578 - (void)hideMouseCursor:(NSTimer *)timer
579 {
580     [NSCursor setHiddenUntilMouseMoves: YES];
581 }
582
583 - (void)recreateHideMouseTimer
584 {
585     if (t_hide_mouse_timer != nil) {
586         [t_hide_mouse_timer invalidate];
587         [t_hide_mouse_timer release];
588     }
589
590     t_hide_mouse_timer = [NSTimer scheduledTimerWithTimeInterval:2
591                                                           target:self
592                                                         selector:@selector(hideMouseCursor:)
593                                                         userInfo:nil
594                                                          repeats:NO];
595     [t_hide_mouse_timer retain];
596 }
597
598 //  Called automatically if window's acceptsMouseMovedEvents property is true
599 - (void)mouseMoved:(NSEvent *)theEvent
600 {
601     if (b_fullscreen)
602         [self recreateHideMouseTimer];
603
604     [super mouseMoved: theEvent];
605 }
606
607 #pragma mark -
608 #pragma mark Lion native fullscreen handling
609
610 - (void)becomeKeyWindow
611 {
612     [super becomeKeyWindow];
613
614     // change fspanel state for the case when multiple windows are in fullscreen
615     if ([self hasActiveVideo] && [self fullscreen])
616         [[[VLCMainWindow sharedInstance] fsPanel] setActive:nil];
617     else
618         [[[VLCMainWindow sharedInstance] fsPanel] setNonActive:nil];
619 }
620
621 - (void)resignKeyWindow
622 {
623     [super resignKeyWindow];
624
625     [[[VLCMainWindow sharedInstance] fsPanel] setNonActive:nil];
626 }
627
628 - (void)windowWillEnterFullScreen:(NSNotification *)notification
629 {
630     // workaround, see #6668
631     [NSApp setPresentationOptions:(NSApplicationPresentationFullScreen | NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
632
633     i_originalLevel = [self level];
634     // b_fullscreen and b_in_fullscreen_transition must not be true yet
635     [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
636     [self setLevel:NSNormalWindowLevel];
637
638     b_in_fullscreen_transition = YES;
639
640     var_SetBool(pl_Get(VLCIntf), "fullscreen", true);
641
642     if ([self hasActiveVideo]) {
643         vout_thread_t *p_vout = getVoutForActiveWindow();
644         if (p_vout) {
645             var_SetBool(p_vout, "fullscreen", true);
646             vlc_object_release(p_vout);
647         }
648     }
649
650     if ([self hasActiveVideo])
651         [[VLCMainWindow sharedInstance] recreateHideMouseTimer];
652
653     if (b_dark_interface) {
654         [o_titlebar_view removeFromSuperviewWithoutNeedingDisplay];
655
656         NSRect winrect;
657         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
658         winrect = [self frame];
659
660         winrect.size.height = winrect.size.height - f_titleBarHeight;
661         [self setFrame: winrect display:NO animate:NO];
662     }
663
664     [o_video_view setFrame: [[self contentView] frame]];
665     if (![o_video_view isHidden]) {
666         [[o_controls_bar bottomBarView] setHidden: YES];
667     }
668     
669
670     [self setMovableByWindowBackground: NO];
671 }
672
673 - (void)windowDidEnterFullScreen:(NSNotification *)notification
674 {
675     // Indeed, we somehow can have an "inactive" fullscreen (but a visible window!).
676     // But this creates some problems when leaving fs over remote intfs, so activate app here.
677     [NSApp activateIgnoringOtherApps:YES];
678
679     [self setFullscreen: YES];
680     b_in_fullscreen_transition = NO;
681
682     if ([self hasActiveVideo]) {
683         [[[VLCMainWindow sharedInstance] fsPanel] setVoutWasUpdated: self];
684         if (![o_video_view isHidden])
685             [[[VLCMainWindow sharedInstance] fsPanel] setActive: nil];
686     }
687
688     NSArray *subviews = [[self videoView] subviews];
689     NSUInteger count = [subviews count];
690
691     for (NSUInteger x = 0; x < count; x++) {
692         if ([[subviews objectAtIndex:x] respondsToSelector:@selector(reshape)])
693             [[subviews objectAtIndex:x] reshape];
694     }
695
696 }
697
698 - (void)windowWillExitFullScreen:(NSNotification *)notification
699 {
700     b_in_fullscreen_transition = YES;
701     [self setFullscreen: NO];
702
703     var_SetBool(pl_Get(VLCIntf), "fullscreen", false);
704
705     if ([self hasActiveVideo]) {
706         vout_thread_t *p_vout = getVoutForActiveWindow();
707         if (p_vout) {
708             var_SetBool(p_vout, "fullscreen", false);
709             vlc_object_release(p_vout);
710         }
711     }
712
713     [NSCursor setHiddenUntilMouseMoves: NO];
714     [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
715
716
717     if (b_dark_interface) {
718         NSRect winrect;
719         CGFloat f_titleBarHeight = [o_titlebar_view frame].size.height;
720
721         winrect = [o_video_view frame];
722         winrect.size.height -= f_titleBarHeight;
723         [o_video_view setFrame: winrect];
724
725         winrect = [self frame];
726         [o_titlebar_view setFrame: NSMakeRect(0, winrect.size.height - f_titleBarHeight,
727                                               winrect.size.width, f_titleBarHeight)];
728         [[self contentView] addSubview: o_titlebar_view];
729
730         winrect.size.height = winrect.size.height + f_titleBarHeight;
731         [self setFrame: winrect display:NO animate:NO];
732     }
733
734     NSRect videoViewFrame = [o_video_view frame];
735     videoViewFrame.origin.y += [o_controls_bar height];
736     videoViewFrame.size.height -= [o_controls_bar height];
737     [o_video_view setFrame: videoViewFrame];
738
739     if (![o_video_view isHidden]) {
740         [[o_controls_bar bottomBarView] setHidden: NO];
741     }
742
743     [self setMovableByWindowBackground: YES];
744 }
745
746 - (void)windowDidExitFullScreen:(NSNotification *)notification
747 {
748     b_in_fullscreen_transition = NO;
749
750     [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: i_originalLevel];
751     [self setLevel:i_originalLevel];
752 }
753
754 #pragma mark -
755 #pragma mark Fullscreen Logic
756
757 - (void)enterFullscreen
758 {
759     NSMutableDictionary *dict1, *dict2;
760     NSScreen *screen;
761     NSRect screen_rect;
762     NSRect rect;
763     BOOL blackout_other_displays = var_InheritBool(VLCIntf, "macosx-black");
764
765     screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_InheritInteger(VLCIntf, "macosx-vdev")];
766
767     if (!screen) {
768         msg_Dbg(VLCIntf, "chosen screen isn't present, using current screen for fullscreen mode");
769         screen = [self screen];
770     }
771     if (!screen) {
772         msg_Dbg(VLCIntf, "Using deepest screen");
773         screen = [NSScreen deepestScreen];
774     }
775
776     screen_rect = [screen frame];
777
778     if (o_controls_bar)
779         [o_controls_bar setFullscreenState:YES];
780     [[[VLCMainWindow sharedInstance] controlsBar] setFullscreenState:YES];
781
782     [[VLCMainWindow sharedInstance] recreateHideMouseTimer];
783
784     if (blackout_other_displays)
785         [screen blackoutOtherScreens];
786
787     /* Make sure we don't see the window flashes in float-on-top mode */
788     i_originalLevel = [self level];
789     // b_fullscreen must not be true yet
790     [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: NSNormalWindowLevel];
791     [self setLevel:NSNormalWindowLevel];
792
793     /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
794     if (!o_fullscreen_window) {
795         /* We can't change the styleMask of an already created NSWindow, so we create another window, and do eye catching stuff */
796
797         rect = [[o_video_view superview] convertRect: [o_video_view frame] toView: nil]; /* Convert to Window base coord */
798         rect.origin.x += [self frame].origin.x;
799         rect.origin.y += [self frame].origin.y;
800         o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
801         [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
802         [o_fullscreen_window setCanBecomeKeyWindow: YES];
803         [o_fullscreen_window setCanBecomeMainWindow: YES];
804         [o_fullscreen_window setHasActiveVideo: YES];
805         [o_fullscreen_window setFullscreen: YES];
806
807         if (![self isVisible] || [self alphaValue] == 0.0) {
808             /* We don't animate if we are not visible, instead we
809              * simply fade the display */
810             CGDisplayFadeReservationToken token;
811
812             if (blackout_other_displays) {
813                 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
814                 CGDisplayFade(token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES);
815             }
816
817             [screen setFullscreenPresentationOptions];
818
819             [o_video_view retain];
820             [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
821             [o_temp_view setFrame:[o_video_view frame]];
822             [o_fullscreen_window setContentView:o_video_view];
823             [o_video_view release];
824
825             [o_fullscreen_window makeKeyAndOrderFront:self];
826             [o_fullscreen_window orderFront:self animate:YES];
827
828             [o_fullscreen_window setFrame:screen_rect display:YES animate:YES];
829             [o_fullscreen_window setLevel:NSNormalWindowLevel];
830
831             if (blackout_other_displays) {
832                 CGDisplayFade(token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO);
833                 CGReleaseDisplayFadeReservation(token);
834             }
835
836             /* Will release the lock */
837             [self hasBecomeFullscreen];
838
839             return;
840         }
841
842         /* Make sure video view gets visible in case the playlist was visible before */
843         b_video_view_was_hidden = [o_video_view isHidden];
844         [o_video_view setHidden: NO];
845
846         /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
847         NSDisableScreenUpdates();
848         [o_video_view retain];
849         [[o_video_view superview] replaceSubview:o_video_view with:o_temp_view];
850         [o_temp_view setFrame:[o_video_view frame]];
851         [o_fullscreen_window setContentView:o_video_view];
852         [o_video_view release];
853         [o_fullscreen_window makeKeyAndOrderFront:self];
854         NSEnableScreenUpdates();
855     }
856
857     /* We are in fullscreen (and no animation is running) */
858     if ([self fullscreen]) {
859         /* Make sure we are hidden */
860         [self orderOut: self];
861
862         return;
863     }
864
865     if (o_fullscreen_anim1) {
866         [o_fullscreen_anim1 stopAnimation];
867         [o_fullscreen_anim1 release];
868     }
869     if (o_fullscreen_anim2) {
870         [o_fullscreen_anim2 stopAnimation];
871         [o_fullscreen_anim2 release];
872     }
873
874     [screen setFullscreenPresentationOptions];
875
876     dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
877     dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
878
879     [dict1 setObject:self forKey:NSViewAnimationTargetKey];
880     [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
881
882     [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
883     [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
884     [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
885
886     /* Strategy with NSAnimation allocation:
887      - Keep at most 2 animation at a time
888      - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
889      */
890     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
891     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
892
893     [dict1 release];
894     [dict2 release];
895
896     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
897     [o_fullscreen_anim1 setDuration: 0.3];
898     [o_fullscreen_anim1 setFrameRate: 30];
899     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
900     [o_fullscreen_anim2 setDuration: 0.2];
901     [o_fullscreen_anim2 setFrameRate: 30];
902
903     [o_fullscreen_anim2 setDelegate: self];
904     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
905
906     [o_fullscreen_anim1 startAnimation];
907     /* fullscreenAnimation will be unlocked when animation ends */
908
909     b_in_fullscreen_transition = YES;
910 }
911
912 - (void)hasBecomeFullscreen
913 {
914     if ([[o_video_view subviews] count] > 0)
915         [o_fullscreen_window makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
916
917     [o_fullscreen_window makeKeyWindow];
918     [o_fullscreen_window setAcceptsMouseMovedEvents: YES];
919
920     /* tell the fspanel to move itself to front next time it's triggered */
921     [[[VLCMainWindow sharedInstance] fsPanel] setVoutWasUpdated: o_fullscreen_window];
922     [[[VLCMainWindow sharedInstance] fsPanel] setActive: nil];
923
924     if ([self isVisible])
925         [self orderOut: self];
926
927     b_in_fullscreen_transition = NO;
928     [self setFullscreen:YES];
929 }
930
931 - (void)leaveFullscreen
932 {
933     [self leaveFullscreenAndFadeOut: NO];
934 }
935
936 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
937 {
938     NSMutableDictionary *dict1, *dict2;
939     NSRect frame;
940     BOOL blackout_other_displays = var_InheritBool(VLCIntf, "macosx-black");
941
942     if (o_controls_bar)
943         [o_controls_bar setFullscreenState:NO];
944     [[[VLCMainWindow sharedInstance] controlsBar] setFullscreenState:NO];
945
946     /* We always try to do so */
947     [NSScreen unblackoutScreens];
948
949     [[o_video_view window] makeKeyAndOrderFront: nil];
950
951     /* Don't do anything if o_fullscreen_window is already closed */
952     if (!o_fullscreen_window) {
953         return;
954     }
955
956     if (fadeout) {
957         /* We don't animate if we are not visible, instead we
958          * simply fade the display */
959         CGDisplayFadeReservationToken token;
960
961         if (blackout_other_displays) {
962             CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
963             CGDisplayFade(token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES);
964         }
965
966         [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
967         [[o_fullscreen_window screen] setNonFullscreenPresentationOptions];
968
969         /* Will release the lock */
970         [self hasEndedFullscreen];
971
972         /* Our window is hidden, and might be faded. We need to workaround that, so note it
973          * here */
974         b_window_is_invisible = YES;
975
976         if (blackout_other_displays) {
977             CGDisplayFade(token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO);
978             CGReleaseDisplayFadeReservation(token);
979         }
980
981         return;
982     }
983
984     b_in_fullscreen_transition = YES;
985
986     [self setAlphaValue: 0.0];
987     [self orderFront: self];
988     [[o_video_view window] orderFront: self];
989
990     [[[VLCMainWindow sharedInstance] fsPanel] setNonActive: nil];
991     [[o_fullscreen_window screen] setNonFullscreenPresentationOptions];
992
993     if (o_fullscreen_anim1) {
994         [o_fullscreen_anim1 stopAnimation];
995         [o_fullscreen_anim1 release];
996     }
997     if (o_fullscreen_anim2) {
998         [o_fullscreen_anim2 stopAnimation];
999         [o_fullscreen_anim2 release];
1000     }
1001
1002     frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
1003     frame.origin.x += [self frame].origin.x;
1004     frame.origin.y += [self frame].origin.y;
1005
1006     dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
1007     [dict2 setObject:self forKey:NSViewAnimationTargetKey];
1008     [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1009
1010     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict2]];
1011     [dict2 release];
1012
1013     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
1014     [o_fullscreen_anim2 setDuration: 0.3];
1015     [o_fullscreen_anim2 setFrameRate: 30];
1016
1017     [o_fullscreen_anim2 setDelegate: self];
1018
1019     dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
1020
1021     [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
1022     [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
1023     [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
1024
1025     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict1]];
1026     [dict1 release];
1027
1028     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
1029     [o_fullscreen_anim1 setDuration: 0.2];
1030     [o_fullscreen_anim1 setFrameRate: 30];
1031     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
1032
1033     /* Make sure o_fullscreen_window is the frontmost window */
1034     [o_fullscreen_window orderFront: self];
1035
1036     [o_fullscreen_anim1 startAnimation];
1037     /* fullscreenAnimation will be unlocked when animation ends */
1038 }
1039
1040 - (void)hasEndedFullscreen
1041 {
1042     [self setFullscreen:NO];
1043     b_in_fullscreen_transition = NO;
1044
1045     /* This function is private and should be only triggered at the end of the fullscreen change animation */
1046     /* Make sure we don't see the o_video_view disappearing of the screen during this operation */
1047     NSDisableScreenUpdates();
1048     [o_video_view retain];
1049     [o_video_view removeFromSuperviewWithoutNeedingDisplay];
1050     [[o_temp_view superview] replaceSubview:o_temp_view with:o_video_view];
1051     [o_video_view release];
1052     [o_video_view setFrame:[o_temp_view frame]];
1053     if ([[o_video_view subviews] count] > 0)
1054         [self makeFirstResponder: [[o_video_view subviews] objectAtIndex:0]];
1055
1056     [o_video_view setHidden: b_video_view_was_hidden];
1057
1058     [super makeKeyAndOrderFront:self]; /* our version (in main window) contains a workaround */
1059
1060     [o_fullscreen_window orderOut: self];
1061     NSEnableScreenUpdates();
1062
1063     [o_fullscreen_window release];
1064     o_fullscreen_window = nil;
1065
1066     [[[VLCMain sharedInstance] voutController] updateWindowLevelForHelperWindows: i_originalLevel];
1067     [self setLevel:i_originalLevel];
1068
1069     [self setAlphaValue: config_GetFloat(VLCIntf, "macosx-opaqueness")];
1070
1071     // if we quit fullscreen because there is no video anymore, make sure non-embedded window is not visible
1072     if (![[VLCMain sharedInstance] activeVideoPlayback] && [self class] != [VLCMainWindow class])
1073         [self orderOut: self];
1074 }
1075
1076 - (void)animationDidEnd:(NSAnimation*)animation
1077 {
1078     NSArray *viewAnimations;
1079     if (o_makekey_anim == animation) {
1080         [o_makekey_anim release];
1081         return;
1082     }
1083     if ([animation currentValue] < 1.0)
1084         return;
1085
1086     /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
1087     viewAnimations = [o_fullscreen_anim2 viewAnimations];
1088     if ([viewAnimations count] >=1 &&
1089         [[[viewAnimations objectAtIndex:0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect]) {
1090         /* Fullscreen ended */
1091         [self hasEndedFullscreen];
1092     } else
1093     /* Fullscreen started */
1094         [self hasBecomeFullscreen];
1095 }
1096
1097 - (void)orderOut:(id)sender
1098 {
1099     [super orderOut:sender];
1100
1101     /*
1102      * TODO reimplement leaveFullscreenAndFadeOut:YES, or remove code
1103      * and the hack below
1104     
1105     if (![NSStringFromClass([self class]) isEqualToString:@"VLCMainWindow"]) {
1106         [self leaveFullscreenAndFadeOut:YES];
1107     }
1108      */
1109 }
1110
1111 - (void)makeKeyAndOrderFront: (id)sender
1112 {
1113     /* Hack
1114      * when we exit fullscreen and fade out, we may endup in
1115      * having a window that is faded. We can't have it fade in unless we
1116      * animate again. */
1117
1118     if (!b_window_is_invisible) {
1119         /* Make sure we don't do it too much */
1120         [super makeKeyAndOrderFront: sender];
1121         return;
1122     }
1123
1124     [super setAlphaValue:0.0f];
1125     [super makeKeyAndOrderFront: sender];
1126
1127     NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
1128     [dict setObject:self forKey:NSViewAnimationTargetKey];
1129     [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
1130
1131     o_makekey_anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
1132     [dict release];
1133
1134     [o_makekey_anim setAnimationBlockingMode: NSAnimationNonblocking];
1135     [o_makekey_anim setDuration: 0.1];
1136     [o_makekey_anim setFrameRate: 30];
1137     [o_makekey_anim setDelegate: self];
1138
1139     [o_makekey_anim startAnimation];
1140     b_window_is_invisible = NO;
1141
1142     /* fullscreenAnimation will be unlocked when animation ends */
1143 }
1144
1145
1146 #pragma mark -
1147 #pragma mark Accessibility stuff
1148
1149 - (NSArray *)accessibilityAttributeNames
1150 {
1151     if (!b_dark_interface || !o_titlebar_view)
1152         return [super accessibilityAttributeNames];
1153
1154     static NSMutableArray *attributes = nil;
1155     if (attributes == nil) {
1156         attributes = [[super accessibilityAttributeNames] mutableCopy];
1157         NSArray *appendAttributes = [NSArray arrayWithObjects:NSAccessibilitySubroleAttribute,
1158                                      NSAccessibilityCloseButtonAttribute,
1159                                      NSAccessibilityMinimizeButtonAttribute,
1160                                      NSAccessibilityZoomButtonAttribute, nil];
1161
1162         for(NSString *attribute in appendAttributes) {
1163             if (![attributes containsObject:attribute])
1164                 [attributes addObject:attribute];
1165         }
1166     }
1167     return attributes;
1168 }
1169
1170 - (id)accessibilityAttributeValue: (NSString*)o_attribute_name
1171 {
1172     if (b_dark_interface && o_titlebar_view) {
1173         VLCMainWindowTitleView *o_tbv = o_titlebar_view;
1174
1175         if ([o_attribute_name isEqualTo: NSAccessibilitySubroleAttribute])
1176             return NSAccessibilityStandardWindowSubrole;
1177
1178         if ([o_attribute_name isEqualTo: NSAccessibilityCloseButtonAttribute])
1179             return [[o_tbv closeButton] cell];
1180
1181         if ([o_attribute_name isEqualTo: NSAccessibilityMinimizeButtonAttribute])
1182             return [[o_tbv minimizeButton] cell];
1183
1184         if ([o_attribute_name isEqualTo: NSAccessibilityZoomButtonAttribute])
1185             return [[o_tbv zoomButton] cell];
1186     }
1187
1188     return [super accessibilityAttributeValue: o_attribute_name];
1189 }
1190
1191 @end