1 /*****************************************************************************
2 * embeddedwindow.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2005-2008 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 *****************************************************************************/
29 /* DisableScreenUpdates, SetSystemUIMode, ... */
30 #import <QuickTime/QuickTime.h>
35 #import "embeddedwindow.h"
38 /*****************************************************************************
39 * VLCEmbeddedWindow Implementation
40 *****************************************************************************/
42 @implementation VLCEmbeddedWindow
46 [self setDelegate: self];
48 [o_btn_backward setToolTip: _NS("Rewind")];
49 [o_btn_forward setToolTip: _NS("Fast Forward")];
50 [o_btn_fullscreen setToolTip: _NS("Fullscreen")];
51 [o_btn_play setToolTip: _NS("Play")];
52 [o_slider setToolTip: _NS("Position")];
54 o_img_play = [NSImage imageNamed: @"play_embedded"];
55 o_img_pause = [NSImage imageNamed: @"pause_embedded"];
56 [self controlTintChanged];
57 [[NSNotificationCenter defaultCenter] addObserver: self
58 selector: @selector( controlTintChanged )
59 name: NSControlTintDidChangeNotification
62 /* Useful to save o_view frame in fullscreen mode */
63 o_temp_view = [[NSView alloc] init];
64 [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
66 o_fullscreen_window = nil;
67 o_fullscreen_anim1 = o_fullscreen_anim2 = nil;
69 /* Not fullscreen when we wake up */
70 [o_btn_fullscreen setState: NO];
73 [self setMovableByWindowBackground:YES];
75 [self setDelegate:self];
77 /* Make sure setVisible: returns NO */
79 b_window_is_invisible = YES;
80 videoRatio = NSMakeSize( 0., 0. );
83 - (void)controlTintChanged
86 if( [o_btn_play alternateImage] == o_img_play_pressed )
89 if( [NSColor currentControlTint] == NSGraphiteControlTint )
91 o_img_play_pressed = [NSImage imageNamed: @"play_embedded_graphite"];
92 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_graphite"];
93 [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_graphite"]];
94 [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_graphite"]];
95 [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_graphite"]];
99 o_img_play_pressed = [NSImage imageNamed: @"play_embedded_blue"];
100 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_blue"];
101 [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_blue"]];
102 [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_blue"]];
103 [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_blue"]];
107 [o_btn_play setAlternateImage: o_img_play_pressed];
109 [o_btn_play setAlternateImage: o_img_pause_pressed];
114 [[NSNotificationCenter defaultCenter] removeObserver: self];
115 [o_img_play release];
116 [o_img_play_pressed release];
117 [o_img_pause release];
118 [o_img_pause_pressed release];
123 - (void)setTime:(NSString *)o_arg_time position:(float)f_position
125 [o_time setStringValue: o_arg_time];
126 [o_slider setFloatValue: f_position];
129 - (void)playStatusUpdated:(int)i_status
131 if( i_status == PLAYING_S )
133 [o_btn_play setImage: o_img_pause];
134 [o_btn_play setAlternateImage: o_img_pause_pressed];
135 [o_btn_play setToolTip: _NS("Pause")];
139 [o_btn_play setImage: o_img_play];
140 [o_btn_play setAlternateImage: o_img_play_pressed];
141 [o_btn_play setToolTip: _NS("Play")];
145 - (void)setSeekable:(BOOL)b_seekable
147 [o_btn_forward setEnabled: b_seekable];
148 [o_btn_backward setEnabled: b_seekable];
149 [o_slider setEnabled: b_seekable];
152 - (BOOL)windowShouldZoom:(NSWindow *)sender toFrame:(NSRect)newFrame
154 [self setFrame: newFrame display: YES animate: YES];
158 - (BOOL)windowShouldClose:(id)sender
160 playlist_t * p_playlist = pl_Hold( VLCIntf );
162 playlist_Stop( p_playlist );
163 vlc_object_release( p_playlist );
169 if (o_fullscreen_window)
175 - (void)setVideoRatio:(NSSize)ratio
180 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
182 if( videoRatio.height == 0. || videoRatio.width == 0. )
183 return proposedFrameSize;
185 NSRect viewRect = [o_view convertRect:[o_view bounds] toView: nil];
186 NSRect contentRect = [self contentRectForFrameRect:[self frame]];
187 float marginy = viewRect.origin.y + [self frame].size.height - contentRect.size.height;
188 float marginx = contentRect.size.width - viewRect.size.width;
189 proposedFrameSize.height = (proposedFrameSize.width - marginx) * videoRatio.height / videoRatio.width + marginy;
191 return proposedFrameSize;
194 /*****************************************************************************
203 - (void)lockFullscreenAnimation
205 [o_animation_lock lock];
208 - (void)unlockFullscreenAnimation
210 [o_animation_lock unlock];
213 - (void)enterFullscreen
215 NSMutableDictionary *dict1, *dict2;
219 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
220 BOOL blackout_other_displays = config_GetInt( VLCIntf, "macosx-black" );
222 screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )];
224 [self lockFullscreenAnimation];
228 msg_Dbg( p_vout, "chosen screen isn't present, using current screen for fullscreen mode" );
229 screen = [self screen];
233 msg_Dbg( p_vout, "Using deepest screen" );
234 screen = [NSScreen deepestScreen];
237 vlc_object_release( p_vout );
239 screen_rect = [screen frame];
241 [o_btn_fullscreen setState: YES];
243 [NSCursor setHiddenUntilMouseMoves: YES];
245 if( blackout_other_displays )
246 [screen blackoutOtherScreens];
248 /* Make sure we don't see the window flashes in float-on-top mode */
249 originalLevel = [self level];
250 [self setLevel:NSNormalWindowLevel];
252 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
253 if (!o_fullscreen_window)
255 /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
257 rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
258 rect.origin.x += [self frame].origin.x;
259 rect.origin.y += [self frame].origin.y;
260 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
261 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
262 [o_fullscreen_window setCanBecomeKeyWindow: YES];
264 if (![self isVisible] || [self alphaValue] == 0.0 || MACOS_VERSION < 10.4f)
266 /* We don't animate if we are not visible or if we are running on
267 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
268 * simply fade the display */
269 CGDisplayFadeReservationToken token;
271 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
272 CGDisplayFade( token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
274 if ([screen isMainScreen])
275 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
277 [[self contentView] replaceSubview:o_view with:o_temp_view];
278 [o_temp_view setFrame:[o_view frame]];
279 [o_fullscreen_window setContentView:o_view];
281 [o_fullscreen_window makeKeyAndOrderFront:self];
282 [o_fullscreen_window orderFront:self animate:YES];
284 [o_fullscreen_window setFrame:screen_rect display:YES];
286 CGDisplayFade( token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
287 CGReleaseDisplayFadeReservation( token);
289 /* Will release the lock */
290 [self hasBecomeFullscreen];
295 /* Make sure we don't see the o_view disappearing of the screen during this operation */
296 DisableScreenUpdates();
297 [[self contentView] replaceSubview:o_view with:o_temp_view];
298 [o_temp_view setFrame:[o_view frame]];
299 [o_fullscreen_window setContentView:o_view];
300 [o_fullscreen_window makeKeyAndOrderFront:self];
301 EnableScreenUpdates();
304 if (MACOS_VERSION < 10.4f)
306 /* We were already fullscreen nothing to do when NSAnimation
307 * is not supported */
308 [self unlockFullscreenAnimation];
312 /* We are in fullscreen (and no animation is running) */
315 /* Make sure we are hidden */
316 [super orderOut: self];
317 [self unlockFullscreenAnimation];
321 if (o_fullscreen_anim1)
323 [o_fullscreen_anim1 stopAnimation];
324 [o_fullscreen_anim1 release];
326 if (o_fullscreen_anim2)
328 [o_fullscreen_anim2 stopAnimation];
329 [o_fullscreen_anim2 release];
332 if ([screen isMainScreen])
333 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
335 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
336 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
338 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
339 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
341 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
342 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
343 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
345 /* Strategy with NSAnimation allocation:
346 - Keep at most 2 animation at a time
347 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
349 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
350 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
355 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
356 [o_fullscreen_anim1 setDuration: 0.3];
357 [o_fullscreen_anim1 setFrameRate: 30];
358 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
359 [o_fullscreen_anim2 setDuration: 0.2];
360 [o_fullscreen_anim2 setFrameRate: 30];
362 [o_fullscreen_anim2 setDelegate: self];
363 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
365 [o_fullscreen_anim1 startAnimation];
366 /* fullscreenAnimation will be unlocked when animation ends */
369 - (void)hasBecomeFullscreen
371 [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] getControls] voutView]];
373 [o_fullscreen_window makeKeyWindow];
374 [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
376 /* tell the fspanel to move itself to front next time it's triggered */
377 [[[[VLCMain sharedInstance] getControls] getFSPanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
380 [super orderOut: self];
382 [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil];
385 [self unlockFullscreenAnimation];
388 - (void)leaveFullscreen
390 [self leaveFullscreenAndFadeOut: NO];
393 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
395 NSMutableDictionary *dict1, *dict2;
398 [self lockFullscreenAnimation];
401 [o_btn_fullscreen setState: NO];
403 /* We always try to do so */
404 [NSScreen unblackoutScreens];
406 /* Don't do anything if o_fullscreen_window is already closed */
407 if (!o_fullscreen_window)
409 [self unlockFullscreenAnimation];
413 if (fadeout || MACOS_VERSION < 10.4f)
415 /* We don't animate if we are not visible or if we are running on
416 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
417 * simply fade the display */
418 CGDisplayFadeReservationToken token;
420 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
421 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
423 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
424 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
426 /* Will release the lock */
427 [self hasEndedFullscreen];
429 /* Our window is hidden, and might be faded. We need to workaround that, so note it
431 b_window_is_invisible = YES;
433 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
434 CGReleaseDisplayFadeReservation( token);
438 [self setAlphaValue: 0.0];
439 [self orderFront: self];
441 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
442 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
444 if (o_fullscreen_anim1)
446 [o_fullscreen_anim1 stopAnimation];
447 [o_fullscreen_anim1 release];
449 if (o_fullscreen_anim2)
451 [o_fullscreen_anim2 stopAnimation];
452 [o_fullscreen_anim2 release];
455 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
456 frame.origin.x += [self frame].origin.x;
457 frame.origin.y += [self frame].origin.y;
459 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
460 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
461 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
463 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
466 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
467 [o_fullscreen_anim2 setDuration: 0.3];
468 [o_fullscreen_anim2 setFrameRate: 30];
470 [o_fullscreen_anim2 setDelegate: self];
472 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
474 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
475 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
476 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
478 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
481 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
482 [o_fullscreen_anim1 setDuration: 0.2];
483 [o_fullscreen_anim1 setFrameRate: 30];
484 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
486 /* Make sure o_fullscreen_window is the frontmost window */
487 [o_fullscreen_window orderFront: self];
489 [o_fullscreen_anim1 startAnimation];
490 /* fullscreenAnimation will be unlocked when animation ends */
493 - (void)hasEndedFullscreen
495 /* This function is private and should be only triggered at the end of the fullscreen change animation */
496 /* Make sure we don't see the o_view disappearing of the screen during this operation */
497 DisableScreenUpdates();
499 [o_view removeFromSuperviewWithoutNeedingDisplay];
500 [[self contentView] replaceSubview:o_temp_view with:o_view];
502 [o_view setFrame:[o_temp_view frame]];
503 [self makeFirstResponder: o_view];
504 if ([self isVisible])
505 [super makeKeyAndOrderFront:self]; /* our version contains a workaround */
506 [o_fullscreen_window orderOut: self];
507 EnableScreenUpdates();
509 [o_fullscreen_window release];
510 o_fullscreen_window = nil;
511 [self setLevel:originalLevel];
513 [self unlockFullscreenAnimation];
516 - (void)animationDidEnd:(NSAnimation*)animation
518 NSArray *viewAnimations;
520 if ([animation currentValue] < 1.0)
523 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
524 viewAnimations = [o_fullscreen_anim2 viewAnimations];
525 if ([viewAnimations count] >=1 &&
526 [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
528 /* Fullscreen ended */
529 [self hasEndedFullscreen];
533 /* Fullscreen started */
534 [self hasBecomeFullscreen];
538 - (void)orderOut: (id)sender
540 [super orderOut: sender];
542 /* Make sure we leave fullscreen */
543 [self leaveFullscreenAndFadeOut: YES];
546 - (void)makeKeyAndOrderFront: (id)sender
549 * when we exit fullscreen and fade out, we may endup in
550 * having a window that is faded. We can't have it fade in unless we
553 if(!b_window_is_invisible)
555 /* Make sure we don't do it too much */
556 [super makeKeyAndOrderFront: sender];
560 [super setAlphaValue:0.0f];
561 [super makeKeyAndOrderFront: sender];
563 NSMutableDictionary * dict = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
564 [dict setObject:self forKey:NSViewAnimationTargetKey];
565 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
567 NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
569 [anim setAnimationBlockingMode: NSAnimationNonblocking];
570 [anim setDuration: 0.1];
571 [anim setFrameRate: 30];
573 [anim startAnimation];
574 b_window_is_invisible = NO;
576 /* fullscreenAnimation will be unlocked when animation ends */
581 /* Make sure setFrame gets executed on main thread especially if we are animating.
582 * (Thus we won't block the video output thread) */
583 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
585 struct { NSRect frame; BOOL display; BOOL animate;} args;
589 args.display = display;
590 args.animate = animate;
592 packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
594 [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
595 withObject: packedargs waitUntilDone: YES];
598 - (void)setFrameOnMainThread:(NSData*)packedargs
600 struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
604 /* Make sure we don't block too long and set up a non blocking animation */
605 NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
606 self, NSViewAnimationTargetKey,
607 [NSValue valueWithRect:[self frame]], NSViewAnimationStartFrameKey,
608 [NSValue valueWithRect:args->frame], NSViewAnimationEndFrameKey, nil];
610 NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
612 [anim setAnimationBlockingMode: NSAnimationNonblocking];
613 [anim setDuration: 0.4];
614 [anim setFrameRate: 30];
615 [anim startAnimation];
618 [super setFrame:args->frame display:args->display animate:args->animate];