1 /*****************************************************************************
2 * embeddedwindow.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2005-2007 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 /* DisableScreenUpdates, SetSystemUIMode, ... */
29 #import <QuickTime/QuickTime.h>
34 #import "embeddedwindow.h"
37 /*****************************************************************************
38 * VLCEmbeddedWindow Implementation
39 *****************************************************************************/
41 @implementation VLCEmbeddedWindow
45 [self setDelegate: self];
47 [o_btn_backward setToolTip: _NS("Rewind")];
48 [o_btn_forward setToolTip: _NS("Fast Forward")];
49 [o_btn_fullscreen setToolTip: _NS("Fullscreen")];
50 [o_btn_play setToolTip: _NS("Play")];
51 [o_slider setToolTip: _NS("Position")];
53 o_img_play = [NSImage imageNamed: @"play_embedded"];
54 o_img_pause = [NSImage imageNamed: @"pause_embedded"];
55 [self controlTintChanged];
56 [[NSNotificationCenter defaultCenter] addObserver: self
57 selector: @selector( controlTintChanged )
58 name: NSControlTintDidChangeNotification
61 /* Useful to save o_view frame in fullscreen mode */
62 o_temp_view = [[NSView alloc] init];
63 [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
65 o_fullscreen_window = nil;
66 o_fullscreen_anim1 = o_fullscreen_anim2 = nil;
68 /* Not fullscreen when we wake up */
69 [o_btn_fullscreen setState: NO];
71 /* Use a recursive lock to be able to trigger enter/leavefullscreen
72 * in middle of an animation, providing that the enter/leave functions
73 * are called from the same thread */
74 o_animation_lock = [[NSRecursiveLock alloc] init];
75 b_animation_lock_alreadylocked = NO;
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 = var_GetBool( p_vout, "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];
208 vlc_object_release( p_vout );
210 screen_rect = [screen frame];
212 [o_btn_fullscreen setState: YES];
214 [NSCursor setHiddenUntilMouseMoves: YES];
216 if (blackout_other_displays)
217 [screen blackoutOtherScreens]; /* We should do something like [screen blackoutOtherScreens]; */
219 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
220 if (!o_fullscreen_window)
222 /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
224 rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
225 rect.origin.x += [self frame].origin.x;
226 rect.origin.y += [self frame].origin.y;
227 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
228 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
229 [o_fullscreen_window setCanBecomeKeyWindow: YES];
231 if (![self isVisible] || [self alphaValue] == 0.0 || MACOS_VERSION < 10.4f)
233 /* We don't animate if we are not visible or if we are running on
234 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
235 * simply fade the display */
236 CGDisplayFadeReservationToken token;
238 [o_fullscreen_window setFrame:screen_rect display:NO];
240 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
241 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
243 if ([screen isMainScreen])
244 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
246 [[self contentView] replaceSubview:o_view with:o_temp_view];
247 [o_temp_view setFrame:[o_view frame]];
248 [o_fullscreen_window setContentView:o_view];
249 [o_fullscreen_window makeKeyAndOrderFront:self];
250 [self orderOut: self];
252 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
253 CGReleaseDisplayFadeReservation( token);
255 /* Will release the lock */
256 [self hasBecomeFullscreen];
261 /* Make sure we don't see the o_view disappearing of the screen during this operation */
262 DisableScreenUpdates();
263 [[self contentView] replaceSubview:o_view with:o_temp_view];
264 [o_temp_view setFrame:[o_view frame]];
265 [o_fullscreen_window setContentView:o_view];
266 [o_fullscreen_window makeKeyAndOrderFront:self];
267 EnableScreenUpdates();
270 if (MACOS_VERSION < 10.4f)
272 /* We were already fullscreen nothing to do when NSAnimation
273 * is not supported */
274 b_animation_lock_alreadylocked = NO;
275 [self unlockFullscreenAnimation];
279 /* We are in fullscreen (and no animation is running) */
282 /* Make sure we are hidden */
283 [super orderOut: self];
284 b_animation_lock_alreadylocked = NO;
285 [self unlockFullscreenAnimation];
289 if (o_fullscreen_anim1)
291 [o_fullscreen_anim1 stopAnimation];
292 [o_fullscreen_anim1 release];
294 if (o_fullscreen_anim2)
296 [o_fullscreen_anim2 stopAnimation];
297 [o_fullscreen_anim2 release];
300 /* This is a recursive lock. If we are already in the middle of an animation we
301 * unlock it. We don't add an extra locking here, because enter/leavefullscreen
302 * are executed always in the same thread */
303 if (b_animation_lock_alreadylocked)
304 [self unlockFullscreenAnimation];
305 b_animation_lock_alreadylocked = YES;
307 if ([screen isMainScreen])
308 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
310 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
311 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
313 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
314 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
316 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
317 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
318 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
320 /* Strategy with NSAnimation allocation:
321 - Keep at most 2 animation at a time
322 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
324 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
325 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
330 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
331 [o_fullscreen_anim1 setDuration: 0.3];
332 [o_fullscreen_anim1 setFrameRate: 30];
333 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
334 [o_fullscreen_anim2 setDuration: 0.2];
335 [o_fullscreen_anim2 setFrameRate: 30];
337 [o_fullscreen_anim2 setDelegate: self];
338 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
340 [o_fullscreen_anim1 startAnimation];
341 /* fullscreenAnimation will be unlocked when animation ends */
344 - (void)hasBecomeFullscreen
346 [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] getControls] getVoutView]];
348 [o_fullscreen_window makeKeyWindow];
349 [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
351 /* tell the fspanel to move itself to front next time it's triggered */
352 [[[[VLCMain sharedInstance] getControls] getFSPanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
354 [super orderOut: self];
356 [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil];
358 [self unlockFullscreenAnimation];
361 - (void)leaveFullscreen
363 [self leaveFullscreenAndFadeOut: NO];
366 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
368 NSMutableDictionary *dict1, *dict2;
371 [self lockFullscreenAnimation];
374 [o_btn_fullscreen setState: NO];
376 /* We always try to do so */
377 [NSScreen unblackoutScreens];
379 /* Don't do anything if o_fullscreen_window is already closed */
380 if (!o_fullscreen_window)
382 b_animation_lock_alreadylocked = NO;
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 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
404 CGReleaseDisplayFadeReservation( token);
408 [self setAlphaValue: 0.0];
409 [self orderFront: self];
411 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
412 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
414 if (o_fullscreen_anim1)
416 [o_fullscreen_anim1 stopAnimation];
417 [o_fullscreen_anim1 release];
419 if (o_fullscreen_anim2)
421 [o_fullscreen_anim2 stopAnimation];
422 [o_fullscreen_anim2 release];
425 /* This is a recursive lock. If we are already in the middle of an animation we
426 * unlock it. We don't add an extra locking here, because enter/leavefullscreen
427 * are executed always in the same thread */
428 if (b_animation_lock_alreadylocked)
429 [self unlockFullscreenAnimation];
430 b_animation_lock_alreadylocked = YES;
432 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
433 frame.origin.x += [self frame].origin.x;
434 frame.origin.y += [self frame].origin.y;
436 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
437 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
438 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
440 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
443 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
444 [o_fullscreen_anim2 setDuration: 0.3];
445 [o_fullscreen_anim2 setFrameRate: 30];
447 [o_fullscreen_anim2 setDelegate: self];
449 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
451 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
452 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
453 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
455 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
458 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
459 [o_fullscreen_anim1 setDuration: 0.2];
460 [o_fullscreen_anim1 setFrameRate: 30];
461 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
463 /* Make sure o_fullscreen_window is the frontmost window */
464 [o_fullscreen_window orderFront: self];
466 [o_fullscreen_anim1 startAnimation];
467 /* fullscreenAnimation will be unlocked when animation ends */
470 - (void)hasEndedFullscreen
472 /* This function is private and should be only triggered at the end of the fullscreen change animation */
473 /* Make sure we don't see the o_view disappearing of the screen during this operation */
474 DisableScreenUpdates();
476 [o_view removeFromSuperviewWithoutNeedingDisplay];
477 [[self contentView] replaceSubview:o_temp_view with:o_view];
479 [o_view setFrame:[o_temp_view frame]];
480 [self makeFirstResponder: o_view];
481 if ([self isVisible])
482 [self makeKeyAndOrderFront:self];
483 [o_fullscreen_window orderOut: self];
484 EnableScreenUpdates();
486 [o_fullscreen_window release];
487 o_fullscreen_window = nil;
488 b_animation_lock_alreadylocked = NO;
489 [self unlockFullscreenAnimation];
492 - (void)animationDidEnd:(NSAnimation*)animation
494 NSArray *viewAnimations;
496 if ([animation currentValue] < 1.0)
499 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
500 viewAnimations = [o_fullscreen_anim2 viewAnimations];
501 if ([viewAnimations count] >=1 &&
502 [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
504 /* Fullscreen ended */
505 [self hasEndedFullscreen];
509 /* Fullscreen started */
510 [self hasBecomeFullscreen];
514 - (void)orderOut: (id)sender
516 [super orderOut: sender];
517 /* Make sure we leave fullscreen */
518 [self leaveFullscreenAndFadeOut: YES];
521 /* Make sure setFrame gets executed on main thread especially if we are animating.
522 * (Thus we won't block the video output thread) */
523 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
525 struct { NSRect frame; BOOL display; BOOL animate;} args;
529 args.display = display;
530 args.animate = animate;
532 packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
534 [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
535 withObject: packedargs waitUntilDone: YES];
538 - (void)setFrameOnMainThread:(NSData*)packedargs
540 struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
542 [super setFrame: args->frame display: args->display animate:args->animate];