1 /*****************************************************************************
2 * embeddedwindow.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2005-2009 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #import "embeddedwindow.h"
35 /*****************************************************************************
36 * VLCEmbeddedWindow Implementation
37 *****************************************************************************/
39 @implementation VLCEmbeddedWindow
41 - (id)initWithContentRect:(NSRect)contentRect styleMask: (NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
43 BOOL b_useTextured = YES;
44 if( [[NSWindow class] instancesRespondToSelector:@selector(setContentBorderThickness:forEdge:)] )
47 windowStyle ^= NSTexturedBackgroundWindowMask;
49 self = [super initWithContentRect:contentRect styleMask:windowStyle backing:bufferingType defer:deferCreation];
52 [self setContentBorderThickness:28.0 forEdge:NSMinYEdge];
59 [self setDelegate: self];
61 [o_btn_backward setToolTip: _NS("Rewind")];
62 [o_btn_forward setToolTip: _NS("Fast Forward")];
63 [o_btn_fullscreen setToolTip: _NS("Fullscreen")];
64 [o_btn_play setToolTip: _NS("Play")];
65 [o_slider setToolTip: _NS("Position")];
67 o_img_play = [NSImage imageNamed: @"play_embedded"];
68 o_img_pause = [NSImage imageNamed: @"pause_embedded"];
69 [self controlTintChanged];
70 [[NSNotificationCenter defaultCenter] addObserver: self
71 selector: @selector( controlTintChanged )
72 name: NSControlTintDidChangeNotification
75 /* Useful to save o_view frame in fullscreen mode */
76 o_temp_view = [[NSView alloc] init];
77 [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
79 o_fullscreen_window = nil;
80 o_fullscreen_anim1 = o_fullscreen_anim2 = nil;
82 /* Not fullscreen when we wake up */
83 [o_btn_fullscreen setState: NO];
86 [self setMovableByWindowBackground:YES];
88 [self setDelegate:self];
90 /* Make sure setVisible: returns NO */
92 b_window_is_invisible = YES;
93 videoRatio = NSMakeSize( 0., 0. );
96 - (void)controlTintChanged
99 if( [o_btn_play alternateImage] == o_img_play_pressed )
102 if( [NSColor currentControlTint] == NSGraphiteControlTint )
104 o_img_play_pressed = [NSImage imageNamed: @"play_embedded_graphite"];
105 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_graphite"];
106 [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_graphite"]];
107 [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_graphite"]];
108 [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_graphite"]];
112 o_img_play_pressed = [NSImage imageNamed: @"play_embedded_blue"];
113 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_blue"];
114 [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_blue"]];
115 [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_blue"]];
116 [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_blue"]];
120 [o_btn_play setAlternateImage: o_img_play_pressed];
122 [o_btn_play setAlternateImage: o_img_pause_pressed];
127 [[NSNotificationCenter defaultCenter] removeObserver: self];
128 [o_img_play release];
129 [o_img_play_pressed release];
130 [o_img_pause release];
131 [o_img_pause_pressed release];
136 - (void)setTime:(NSString *)o_arg_time position:(float)f_position
138 [o_time setStringValue: o_arg_time];
139 [o_slider setFloatValue: f_position];
142 - (void)playStatusUpdated:(int)i_status
144 if( i_status == PLAYING_S )
146 [o_btn_play setImage: o_img_pause];
147 [o_btn_play setAlternateImage: o_img_pause_pressed];
148 [o_btn_play setToolTip: _NS("Pause")];
152 [o_btn_play setImage: o_img_play];
153 [o_btn_play setAlternateImage: o_img_play_pressed];
154 [o_btn_play setToolTip: _NS("Play")];
158 - (void)setSeekable:(BOOL)b_seekable
160 [o_btn_forward setEnabled: b_seekable];
161 [o_btn_backward setEnabled: b_seekable];
162 [o_slider setEnabled: b_seekable];
165 - (BOOL)windowShouldZoom:(NSWindow *)sender toFrame:(NSRect)newFrame
167 [self setFrame: newFrame display: YES animate: YES];
171 - (BOOL)windowShouldClose:(id)sender
173 playlist_t * p_playlist = pl_Hold( VLCIntf );
175 playlist_Stop( p_playlist );
176 pl_Release( VLCIntf );
182 if (o_fullscreen_window)
188 - (void)setVideoRatio:(NSSize)ratio
193 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
195 if( videoRatio.height == 0. || videoRatio.width == 0. )
196 return proposedFrameSize;
198 NSRect viewRect = [o_view convertRect:[o_view bounds] toView: nil];
199 NSRect contentRect = [self contentRectForFrameRect:[self frame]];
200 float marginy = viewRect.origin.y + [self frame].size.height - contentRect.size.height;
201 float marginx = contentRect.size.width - viewRect.size.width;
202 proposedFrameSize.height = (proposedFrameSize.width - marginx) * videoRatio.height / videoRatio.width + marginy;
204 return proposedFrameSize;
207 /*****************************************************************************
216 - (void)lockFullscreenAnimation
218 [o_animation_lock lock];
221 - (void)unlockFullscreenAnimation
223 [o_animation_lock unlock];
226 - (void)enterFullscreen
228 NSMutableDictionary *dict1, *dict2;
232 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
233 BOOL blackout_other_displays = config_GetInt( VLCIntf, "macosx-black" );
235 screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )];
237 [self lockFullscreenAnimation];
241 msg_Dbg( p_vout, "chosen screen isn't present, using current screen for fullscreen mode" );
242 screen = [self screen];
246 msg_Dbg( p_vout, "Using deepest screen" );
247 screen = [NSScreen deepestScreen];
250 vlc_object_release( p_vout );
252 screen_rect = [screen frame];
254 [o_btn_fullscreen setState: YES];
256 [NSCursor setHiddenUntilMouseMoves: YES];
258 if( blackout_other_displays )
259 [screen blackoutOtherScreens];
261 /* Make sure we don't see the window flashes in float-on-top mode */
262 originalLevel = [self level];
263 [self setLevel:NSNormalWindowLevel];
265 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
266 if (!o_fullscreen_window)
268 /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
270 rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
271 rect.origin.x += [self frame].origin.x;
272 rect.origin.y += [self frame].origin.y;
273 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
274 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
275 [o_fullscreen_window setCanBecomeKeyWindow: YES];
277 if (![self isVisible] || [self alphaValue] == 0.0)
279 /* We don't animate if we are not visible, instead we
280 * simply fade the display */
281 CGDisplayFadeReservationToken token;
283 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
284 CGDisplayFade( token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
286 if ([screen isMainScreen])
287 [NSMenu setMenuBarVisible:NO];
289 [[self contentView] replaceSubview:o_view with:o_temp_view];
290 [o_temp_view setFrame:[o_view frame]];
291 [o_fullscreen_window setContentView:o_view];
293 [o_fullscreen_window makeKeyAndOrderFront:self];
294 [o_fullscreen_window orderFront:self animate:YES];
296 [o_fullscreen_window setFrame:screen_rect display:YES];
298 CGDisplayFade( token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
299 CGReleaseDisplayFadeReservation( token);
301 /* Will release the lock */
302 [self hasBecomeFullscreen];
307 /* Make sure we don't see the o_view disappearing of the screen during this operation */
308 NSEnableScreenUpdates();
309 [[self contentView] replaceSubview:o_view with:o_temp_view];
310 [o_temp_view setFrame:[o_view frame]];
311 [o_fullscreen_window setContentView:o_view];
312 [o_fullscreen_window makeKeyAndOrderFront:self];
313 NSDisableScreenUpdates();
316 /* We are in fullscreen (and no animation is running) */
319 /* Make sure we are hidden */
320 [super orderOut: self];
321 [self unlockFullscreenAnimation];
325 if (o_fullscreen_anim1)
327 [o_fullscreen_anim1 stopAnimation];
328 [o_fullscreen_anim1 release];
330 if (o_fullscreen_anim2)
332 [o_fullscreen_anim2 stopAnimation];
333 [o_fullscreen_anim2 release];
336 if ([screen isMainScreen])
337 [NSMenu setMenuBarVisible:NO];
339 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
340 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
342 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
343 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
345 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
346 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
347 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
349 /* Strategy with NSAnimation allocation:
350 - Keep at most 2 animation at a time
351 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
353 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
354 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
359 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
360 [o_fullscreen_anim1 setDuration: 0.3];
361 [o_fullscreen_anim1 setFrameRate: 30];
362 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
363 [o_fullscreen_anim2 setDuration: 0.2];
364 [o_fullscreen_anim2 setFrameRate: 30];
366 [o_fullscreen_anim2 setDelegate: self];
367 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
369 [o_fullscreen_anim1 startAnimation];
370 /* fullscreenAnimation will be unlocked when animation ends */
373 - (void)hasBecomeFullscreen
375 [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] controls] voutView]];
377 [o_fullscreen_window makeKeyWindow];
378 [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
380 /* tell the fspanel to move itself to front next time it's triggered */
381 [[[[VLCMain sharedInstance] controls] fspanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
384 [super orderOut: self];
386 [[[[VLCMain sharedInstance] controls] fspanel] setActive: nil];
389 [self unlockFullscreenAnimation];
392 - (void)leaveFullscreen
394 [self leaveFullscreenAndFadeOut: NO];
397 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
399 NSMutableDictionary *dict1, *dict2;
402 [self lockFullscreenAnimation];
405 [o_btn_fullscreen setState: NO];
407 /* We always try to do so */
408 [NSScreen unblackoutScreens];
410 /* Don't do anything if o_fullscreen_window is already closed */
411 if (!o_fullscreen_window)
413 [self unlockFullscreenAnimation];
419 /* We don't animate if we are not visible, instead we
420 * simply fade the display */
421 CGDisplayFadeReservationToken token;
423 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
424 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
426 [[[[VLCMain sharedInstance] controls] fspanel] setNonActive: nil];
427 [NSMenu setMenuBarVisible:YES];
429 /* Will release the lock */
430 [self hasEndedFullscreen];
432 /* Our window is hidden, and might be faded. We need to workaround that, so note it
434 b_window_is_invisible = YES;
436 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
437 CGReleaseDisplayFadeReservation( token);
441 [self setAlphaValue: 0.0];
442 [self orderFront: self];
444 [[[[VLCMain sharedInstance] controls] fspanel] setNonActive: nil];
445 [NSMenu setMenuBarVisible:YES];
447 if (o_fullscreen_anim1)
449 [o_fullscreen_anim1 stopAnimation];
450 [o_fullscreen_anim1 release];
452 if (o_fullscreen_anim2)
454 [o_fullscreen_anim2 stopAnimation];
455 [o_fullscreen_anim2 release];
458 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
459 frame.origin.x += [self frame].origin.x;
460 frame.origin.y += [self frame].origin.y;
462 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
463 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
464 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
466 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
469 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
470 [o_fullscreen_anim2 setDuration: 0.3];
471 [o_fullscreen_anim2 setFrameRate: 30];
473 [o_fullscreen_anim2 setDelegate: self];
475 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
477 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
478 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
479 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
481 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
484 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
485 [o_fullscreen_anim1 setDuration: 0.2];
486 [o_fullscreen_anim1 setFrameRate: 30];
487 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
489 /* Make sure o_fullscreen_window is the frontmost window */
490 [o_fullscreen_window orderFront: self];
492 [o_fullscreen_anim1 startAnimation];
493 /* fullscreenAnimation will be unlocked when animation ends */
496 - (void)hasEndedFullscreen
498 /* This function is private and should be only triggered at the end of the fullscreen change animation */
499 /* Make sure we don't see the o_view disappearing of the screen during this operation */
500 NSDisableScreenUpdates();
502 [o_view removeFromSuperviewWithoutNeedingDisplay];
503 [[self contentView] replaceSubview:o_temp_view with:o_view];
505 [o_view setFrame:[o_temp_view frame]];
506 [self makeFirstResponder: o_view];
507 if ([self isVisible])
508 [super makeKeyAndOrderFront:self]; /* our version contains a workaround */
509 [o_fullscreen_window orderOut: self];
510 NSEnableScreenUpdates();
512 [o_fullscreen_window release];
513 o_fullscreen_window = nil;
514 [self setLevel:originalLevel];
516 [self unlockFullscreenAnimation];
519 - (void)animationDidEnd:(NSAnimation*)animation
521 NSArray *viewAnimations;
523 if ([animation currentValue] < 1.0)
526 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
527 viewAnimations = [o_fullscreen_anim2 viewAnimations];
528 if ([viewAnimations count] >=1 &&
529 [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
531 /* Fullscreen ended */
532 [self hasEndedFullscreen];
536 /* Fullscreen started */
537 [self hasBecomeFullscreen];
541 - (void)orderOut: (id)sender
543 [super orderOut: sender];
545 /* Make sure we leave fullscreen */
546 [self leaveFullscreenAndFadeOut: YES];
549 - (void)makeKeyAndOrderFront: (id)sender
552 * when we exit fullscreen and fade out, we may endup in
553 * having a window that is faded. We can't have it fade in unless we
556 if(!b_window_is_invisible)
558 /* Make sure we don't do it too much */
559 [super makeKeyAndOrderFront: sender];
563 [super setAlphaValue:0.0f];
564 [super makeKeyAndOrderFront: sender];
566 NSMutableDictionary * dict = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
567 [dict setObject:self forKey:NSViewAnimationTargetKey];
568 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
570 NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
572 [anim setAnimationBlockingMode: NSAnimationNonblocking];
573 [anim setDuration: 0.1];
574 [anim setFrameRate: 30];
576 [anim startAnimation];
577 b_window_is_invisible = NO;
579 /* fullscreenAnimation will be unlocked when animation ends */
584 /* Make sure setFrame gets executed on main thread especially if we are animating.
585 * (Thus we won't block the video output thread) */
586 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
588 struct { NSRect frame; BOOL display; BOOL animate;} args;
592 args.display = display;
593 args.animate = animate;
595 packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
597 [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
598 withObject: packedargs waitUntilDone: YES];
601 - (void)setFrameOnMainThread:(NSData*)packedargs
603 struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
607 /* Make sure we don't block too long and set up a non blocking animation */
608 NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
609 self, NSViewAnimationTargetKey,
610 [NSValue valueWithRect:[self frame]], NSViewAnimationStartFrameKey,
611 [NSValue valueWithRect:args->frame], NSViewAnimationEndFrameKey, nil];
613 NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
615 [anim setAnimationBlockingMode: NSAnimationNonblocking];
616 [anim setDuration: 0.4];
617 [anim setFrameRate: 30];
618 [anim startAnimation];
621 [super setFrame:args->frame display:args->display animate:args->animate];