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 /* Make sure setVisible: returns NO */
75 b_window_is_invisible = YES;
78 - (void)controlTintChanged
81 if( [o_btn_play alternateImage] == o_img_play_pressed )
84 if( [NSColor currentControlTint] == NSGraphiteControlTint )
86 o_img_play_pressed = [NSImage imageNamed: @"play_embedded_graphite"];
87 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_graphite"];
88 [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_graphite"]];
89 [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_graphite"]];
90 [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_graphite"]];
94 o_img_play_pressed = [NSImage imageNamed: @"play_embedded_blue"];
95 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_blue"];
96 [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_blue"]];
97 [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_blue"]];
98 [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_blue"]];
102 [o_btn_play setAlternateImage: o_img_play_pressed];
104 [o_btn_play setAlternateImage: o_img_pause_pressed];
109 [[NSNotificationCenter defaultCenter] removeObserver: self];
110 [o_img_play release];
111 [o_img_play_pressed release];
112 [o_img_pause release];
113 [o_img_pause_pressed release];
118 - (void)setTime:(NSString *)o_arg_time position:(float)f_position
120 [o_time setStringValue: o_arg_time];
121 [o_slider setFloatValue: f_position];
124 - (void)playStatusUpdated:(int)i_status
126 if( i_status == PLAYING_S )
128 [o_btn_play setImage: o_img_pause];
129 [o_btn_play setAlternateImage: o_img_pause_pressed];
130 [o_btn_play setToolTip: _NS("Pause")];
134 [o_btn_play setImage: o_img_play];
135 [o_btn_play setAlternateImage: o_img_play_pressed];
136 [o_btn_play setToolTip: _NS("Play")];
140 - (void)setSeekable:(BOOL)b_seekable
142 [o_btn_forward setEnabled: b_seekable];
143 [o_btn_backward setEnabled: b_seekable];
144 [o_slider setEnabled: b_seekable];
147 - (BOOL)windowShouldZoom:(NSWindow *)sender toFrame:(NSRect)newFrame
149 [self setFrame: newFrame display: YES animate: YES];
153 - (BOOL)windowShouldClose:(id)sender
155 playlist_t * p_playlist = pl_Yield( VLCIntf );
157 playlist_Stop( p_playlist );
158 vlc_object_release( p_playlist );
164 if (o_fullscreen_window)
170 /*****************************************************************************
179 - (void)lockFullscreenAnimation
181 [o_animation_lock lock];
184 - (void)unlockFullscreenAnimation
186 [o_animation_lock unlock];
189 - (void)enterFullscreen
191 NSMutableDictionary *dict1, *dict2;
195 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
196 BOOL blackout_other_displays = config_GetInt( VLCIntf, "macosx-black" );
198 screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )];
200 [self lockFullscreenAnimation];
204 msg_Dbg( p_vout, "chosen screen isn't present, using current screen for fullscreen mode" );
205 screen = [self screen];
209 msg_Dbg( p_vout, "Using deepest screen" );
210 screen = [NSScreen deepestScreen];
213 vlc_object_release( p_vout );
215 screen_rect = [screen frame];
217 [o_btn_fullscreen setState: YES];
219 [NSCursor setHiddenUntilMouseMoves: YES];
221 if( blackout_other_displays )
222 [screen blackoutOtherScreens];
224 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
225 if (!o_fullscreen_window)
227 /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
229 rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
230 rect.origin.x += [self frame].origin.x;
231 rect.origin.y += [self frame].origin.y;
232 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
233 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
234 [o_fullscreen_window setCanBecomeKeyWindow: YES];
236 if (![self isVisible] || [self alphaValue] == 0.0 || MACOS_VERSION < 10.4f)
238 /* We don't animate if we are not visible or if we are running on
239 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
240 * simply fade the display */
241 CGDisplayFadeReservationToken token;
243 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
244 CGDisplayFade( token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
246 if ([screen isMainScreen])
247 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
249 [[self contentView] replaceSubview:o_view with:o_temp_view];
250 [o_temp_view setFrame:[o_view frame]];
251 [o_fullscreen_window setContentView:o_view];
253 [o_fullscreen_window makeKeyAndOrderFront:self];
255 [o_fullscreen_window makeKeyAndOrderFront:self];
256 [o_fullscreen_window orderFront:self animate:YES];
258 [o_fullscreen_window setFrame:screen_rect display:YES];
260 CGDisplayFade( token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
261 CGReleaseDisplayFadeReservation( token);
263 /* Will release the lock */
264 [self hasBecomeFullscreen];
269 /* Make sure we don't see the o_view disappearing of the screen during this operation */
270 DisableScreenUpdates();
271 [[self contentView] replaceSubview:o_view with:o_temp_view];
272 [o_temp_view setFrame:[o_view frame]];
273 [o_fullscreen_window setContentView:o_view];
274 [o_fullscreen_window makeKeyAndOrderFront:self];
275 EnableScreenUpdates();
278 if (MACOS_VERSION < 10.4f)
280 /* We were already fullscreen nothing to do when NSAnimation
281 * is not supported */
282 [self unlockFullscreenAnimation];
286 /* We are in fullscreen (and no animation is running) */
289 /* Make sure we are hidden */
290 [super orderOut: self];
291 [self unlockFullscreenAnimation];
295 if (o_fullscreen_anim1)
297 [o_fullscreen_anim1 stopAnimation];
298 [o_fullscreen_anim1 release];
300 if (o_fullscreen_anim2)
302 [o_fullscreen_anim2 stopAnimation];
303 [o_fullscreen_anim2 release];
306 if ([screen isMainScreen])
307 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
309 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
310 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
312 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
313 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
315 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
316 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
317 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
319 /* Strategy with NSAnimation allocation:
320 - Keep at most 2 animation at a time
321 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
323 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
324 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
329 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
330 [o_fullscreen_anim1 setDuration: 0.3];
331 [o_fullscreen_anim1 setFrameRate: 30];
332 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
333 [o_fullscreen_anim2 setDuration: 0.2];
334 [o_fullscreen_anim2 setFrameRate: 30];
336 [o_fullscreen_anim2 setDelegate: self];
337 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
339 [o_fullscreen_anim1 startAnimation];
340 /* fullscreenAnimation will be unlocked when animation ends */
343 - (void)hasBecomeFullscreen
345 [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] getControls] getVoutView]];
347 [o_fullscreen_window makeKeyWindow];
348 [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
350 /* tell the fspanel to move itself to front next time it's triggered */
351 [[[[VLCMain sharedInstance] getControls] getFSPanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
354 [super orderOut: self];
356 [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil];
359 [self unlockFullscreenAnimation];
362 - (void)leaveFullscreen
364 [self leaveFullscreenAndFadeOut: NO];
367 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
369 NSMutableDictionary *dict1, *dict2;
372 [self lockFullscreenAnimation];
375 [o_btn_fullscreen setState: NO];
377 /* We always try to do so */
378 [NSScreen unblackoutScreens];
380 /* Don't do anything if o_fullscreen_window is already closed */
381 if (!o_fullscreen_window)
383 [self unlockFullscreenAnimation];
387 if (fadeout || MACOS_VERSION < 10.4f)
389 /* We don't animate if we are not visible or if we are running on
390 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
391 * simply fade the display */
392 CGDisplayFadeReservationToken token;
394 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
395 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
397 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
398 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
400 /* Will release the lock */
401 [self hasEndedFullscreen];
403 /* Our window is hidden, and might be faded. We need to workaround that, so note it
405 b_window_is_invisible = YES;
407 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
408 CGReleaseDisplayFadeReservation( token);
412 [self setAlphaValue: 0.0];
413 [self orderFront: self];
415 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
416 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
418 if (o_fullscreen_anim1)
420 [o_fullscreen_anim1 stopAnimation];
421 [o_fullscreen_anim1 release];
423 if (o_fullscreen_anim2)
425 [o_fullscreen_anim2 stopAnimation];
426 [o_fullscreen_anim2 release];
429 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
430 frame.origin.x += [self frame].origin.x;
431 frame.origin.y += [self frame].origin.y;
433 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
434 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
435 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
437 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
440 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
441 [o_fullscreen_anim2 setDuration: 0.3];
442 [o_fullscreen_anim2 setFrameRate: 30];
444 [o_fullscreen_anim2 setDelegate: self];
446 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
448 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
449 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
450 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
452 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
455 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
456 [o_fullscreen_anim1 setDuration: 0.2];
457 [o_fullscreen_anim1 setFrameRate: 30];
458 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
460 /* Make sure o_fullscreen_window is the frontmost window */
461 [o_fullscreen_window orderFront: self];
463 [o_fullscreen_anim1 startAnimation];
464 /* fullscreenAnimation will be unlocked when animation ends */
467 - (void)hasEndedFullscreen
469 /* This function is private and should be only triggered at the end of the fullscreen change animation */
470 /* Make sure we don't see the o_view disappearing of the screen during this operation */
471 DisableScreenUpdates();
473 [o_view removeFromSuperviewWithoutNeedingDisplay];
474 [[self contentView] replaceSubview:o_temp_view with:o_view];
476 [o_view setFrame:[o_temp_view frame]];
477 [self makeFirstResponder: o_view];
478 if ([self isVisible])
479 [super makeKeyAndOrderFront:self]; /* our version contains a workaround */
480 [o_fullscreen_window orderOut: self];
481 EnableScreenUpdates();
483 [o_fullscreen_window release];
484 o_fullscreen_window = nil;
485 [self unlockFullscreenAnimation];
488 - (void)animationDidEnd:(NSAnimation*)animation
490 NSArray *viewAnimations;
492 if ([animation currentValue] < 1.0)
495 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
496 viewAnimations = [o_fullscreen_anim2 viewAnimations];
497 if ([viewAnimations count] >=1 &&
498 [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
500 /* Fullscreen ended */
501 [self hasEndedFullscreen];
505 /* Fullscreen started */
506 [self hasBecomeFullscreen];
510 - (void)orderOut: (id)sender
512 [super orderOut: sender];
514 /* Make sure we leave fullscreen */
515 [self leaveFullscreenAndFadeOut: YES];
518 - (void)makeKeyAndOrderFront: (id)sender
521 * when we exit fullscreen and fade out, we may endup in
522 * having a window that is faded. We can't have it fade in unless we
525 if(!b_window_is_invisible)
527 /* Make sure we don't do it too much */
528 [super makeKeyAndOrderFront: sender];
532 [super setAlphaValue:0.0f];
533 [super makeKeyAndOrderFront: sender];
535 NSMutableDictionary * dict = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
536 [dict setObject:self forKey:NSViewAnimationTargetKey];
537 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
539 NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
541 [anim setAnimationBlockingMode: NSAnimationNonblocking];
542 [anim setDuration: 0.1];
543 [anim setFrameRate: 30];
545 [anim startAnimation];
546 b_window_is_invisible = NO;
548 /* fullscreenAnimation will be unlocked when animation ends */
553 /* Make sure setFrame gets executed on main thread especially if we are animating.
554 * (Thus we won't block the video output thread) */
555 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
557 struct { NSRect frame; BOOL display; BOOL animate;} args;
561 args.display = display;
562 args.animate = animate;
564 packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
566 [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
567 withObject: packedargs waitUntilDone: YES];
570 - (void)setFrameOnMainThread:(NSData*)packedargs
572 struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
574 [super setFrame: args->frame display: args->display animate:args->animate];