1 /*****************************************************************************
2 * embeddedwindow.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2005-2006 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_play_pressed = [NSImage imageNamed: @"play_embedded_blue"];
55 o_img_pause = [NSImage imageNamed: @"pause_embedded"];
56 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_blue"];
58 o_saved_frame = NSMakeRect( 0.0f, 0.0f, 0.0f, 0.0f );
60 /* Useful to save o_view frame in fullscreen mode */
61 o_temp_view = [[NSView alloc] init];
62 [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
64 o_fullscreen_window = nil;
65 o_fullscreen_anim1 = o_fullscreen_anim2 = nil;
67 /* Not fullscreen when we wake up */
68 [o_btn_fullscreen setState: NO];
70 o_animation_lock = [[NSLock alloc] init];
73 - (void)setTime:(NSString *)o_arg_time position:(float)f_position
75 [o_time setStringValue: o_arg_time];
76 [o_slider setFloatValue: f_position];
79 - (void)playStatusUpdated:(int)i_status
81 if( i_status == PLAYING_S )
83 [o_btn_play setImage: o_img_pause];
84 [o_btn_play setAlternateImage: o_img_pause_pressed];
85 [o_btn_play setToolTip: _NS("Pause")];
89 [o_btn_play setImage: o_img_play];
90 [o_btn_play setAlternateImage: o_img_play_pressed];
91 [o_btn_play setToolTip: _NS("Play")];
95 - (void)setSeekable:(BOOL)b_seekable
97 [o_btn_forward setEnabled: b_seekable];
98 [o_btn_backward setEnabled: b_seekable];
99 [o_slider setEnabled: b_seekable];
102 - (void)zoom:(id)sender
104 if( ![self isZoomed] )
106 NSRect zoomRect = [[self screen] frame];
107 o_saved_frame = [self frame];
108 /* we don't have to take care of the eventual menu bar and dock
109 as zoomRect will be cropped automatically by setFrame:display:
110 to the right rectangle */
111 [self setFrame: zoomRect display: YES animate: YES];
115 /* unzoom to the saved_frame if the o_saved_frame coords look sound
117 if( o_saved_frame.size.width > 0 && o_saved_frame.size.height > 0 )
118 [self setFrame: o_saved_frame display: YES animate: YES];
122 - (BOOL)windowShouldClose:(id)sender
124 playlist_t * p_playlist = pl_Yield( VLCIntf );
126 playlist_Stop( p_playlist );
127 vlc_object_release( p_playlist );
133 if (o_fullscreen_window)
139 /*****************************************************************************
148 - (void)lockFullscreenAnimation
150 [o_animation_lock lock];
153 - (void)unlockFullscreenAnimation
155 [o_animation_lock unlock];
158 - (void)enterFullscreen
160 NSMutableDictionary *dict1, *dict2;
164 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
165 BOOL blackout_other_displays = var_GetBool( p_vout, "macosx-black" );
167 screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )];
169 vlc_object_release( p_vout );
171 [self lockFullscreenAnimation];
174 screen = [self screen];
176 screen_rect = [screen frame];
178 [o_btn_fullscreen setState: YES];
180 [NSCursor setHiddenUntilMouseMoves: YES];
182 if (blackout_other_displays)
183 [screen blackoutOtherScreens]; /* We should do something like [screen blackoutOtherScreens]; */
185 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
186 if (!o_fullscreen_window)
188 /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
190 rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
191 rect.origin.x += [self frame].origin.x;
192 rect.origin.y += [self frame].origin.y;
193 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
194 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
195 [o_fullscreen_window setCanBecomeKeyWindow: YES];
197 if (![self isVisible] || [self alphaValue] == 0.0 || MACOS_VERSION < 10.4f)
199 /* We don't animate if we are not visible or if we are running on
200 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
201 * simply fade the display */
202 CGDisplayFadeReservationToken token;
204 [o_fullscreen_window setFrame:screen_rect display:NO];
206 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
207 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
209 if ([screen isMainScreen])
210 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
212 [[self contentView] replaceSubview:o_view with:o_temp_view];
213 [o_temp_view setFrame:[o_view frame]];
214 [o_fullscreen_window setContentView:o_view];
215 [o_fullscreen_window makeKeyAndOrderFront:self];
216 [self orderOut: self];
218 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
219 CGReleaseDisplayFadeReservation( token);
221 /* Will release the lock */
222 [self hasBecomeFullscreen];
227 /* Make sure we don't see the o_view disappearing of the screen during this operation */
228 DisableScreenUpdates();
229 [[self contentView] replaceSubview:o_view with:o_temp_view];
230 [o_temp_view setFrame:[o_view frame]];
231 [o_fullscreen_window setContentView:o_view];
232 [o_fullscreen_window makeKeyAndOrderFront:self];
233 EnableScreenUpdates();
236 if (MACOS_VERSION < 10.4f)
238 /* We were already fullscreen nothing to do when NSAnimation
239 * is not supported */
240 [self unlockFullscreenAnimation];
244 /* We are in fullscreen (and no animation is running) */
247 [self unlockFullscreenAnimation];
251 if (o_fullscreen_anim1)
253 [o_fullscreen_anim1 stopAnimation];
254 [o_fullscreen_anim1 release];
256 if (o_fullscreen_anim2)
258 [o_fullscreen_anim2 stopAnimation];
259 [o_fullscreen_anim2 release];
262 if ([screen isMainScreen])
263 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
265 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
266 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
268 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
269 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
271 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
272 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
273 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
275 /* Strategy with NSAnimation allocation:
276 - Keep at most 2 animation at a time
277 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
279 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
280 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
285 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
286 [o_fullscreen_anim1 setDuration: 0.3];
287 [o_fullscreen_anim1 setFrameRate: 30];
288 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
289 [o_fullscreen_anim2 setDuration: 0.2];
290 [o_fullscreen_anim2 setFrameRate: 30];
292 [o_fullscreen_anim2 setDelegate: self];
293 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
295 [o_fullscreen_anim1 startAnimation];
296 /* fullscreenAnimation will be unlocked when animation ends */
299 - (void)hasBecomeFullscreen
301 [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] getControls] getVoutView]];
303 [o_fullscreen_window makeKeyWindow];
304 [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
306 /* tell the fspanel to move itself to front next time it's triggered */
307 [[[[VLCMain sharedInstance] getControls] getFSPanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
309 [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil];
311 [self unlockFullscreenAnimation];
314 - (void)leaveFullscreen
316 NSMutableDictionary *dict1, *dict2;
319 [self lockFullscreenAnimation];
322 [o_btn_fullscreen setState: NO];
324 /* We always try to do so */
325 [NSScreen unblackoutScreens];
327 /* Don't do anything if o_fullscreen_window is already closed */
328 if (!o_fullscreen_window)
330 [self lockFullscreenAnimation];
334 if (![self isVisible] || MACOS_VERSION < 10.4f)
336 /* We don't animate if we are not visible or if we are running on
337 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
338 * simply fade the display */
339 CGDisplayFadeReservationToken token;
341 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
342 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
344 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
345 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
347 /* Will release the lock */
348 [self hasEndedFullscreen];
350 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
351 CGReleaseDisplayFadeReservation( token);
355 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
356 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
358 if (o_fullscreen_anim1)
360 [o_fullscreen_anim1 stopAnimation];
361 [o_fullscreen_anim1 release];
363 if (o_fullscreen_anim2)
365 [o_fullscreen_anim2 stopAnimation];
366 [o_fullscreen_anim2 release];
369 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
370 frame.origin.x += [self frame].origin.x;
371 frame.origin.y += [self frame].origin.y;
373 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
374 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
375 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
377 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
380 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
381 [o_fullscreen_anim2 setDuration: 0.3];
382 [o_fullscreen_anim2 setFrameRate: 30];
384 [o_fullscreen_anim2 setDelegate: self];
386 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
388 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
389 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
390 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
392 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
395 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
396 [o_fullscreen_anim1 setDuration: 0.2];
397 [o_fullscreen_anim1 setFrameRate: 30];
398 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
399 [o_fullscreen_anim1 startAnimation];
400 /* fullscreenAnimation will be unlocked when animation ends */
403 - (void)hasEndedFullscreen
405 /* This function is private and should be only triggered at the end of the fullscreen change animation */
406 /* Make sure we don't see the o_view disappearing of the screen during this operation */
407 DisableScreenUpdates();
409 [o_view removeFromSuperviewWithoutNeedingDisplay];
410 [[self contentView] replaceSubview:o_temp_view with:o_view];
412 [o_view setFrame:[o_temp_view frame]];
413 if ([self isVisible])
414 [self makeKeyAndOrderFront:self];
415 [o_fullscreen_window orderOut: self];
416 EnableScreenUpdates();
418 [o_fullscreen_window release];
419 o_fullscreen_window = nil;
420 [self unlockFullscreenAnimation];
423 - (void)animationDidEnd:(NSAnimation*)animation
425 NSArray *viewAnimations;
427 if ([animation currentValue] < 1.0)
430 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
431 viewAnimations = [o_fullscreen_anim2 viewAnimations];
432 if ([viewAnimations count] >=1 &&
433 [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
435 /* Fullscreen ended */
436 [self hasEndedFullscreen];
440 /* Fullscreen started */
441 [self hasBecomeFullscreen];
445 - (void)orderOut: (id)sender
447 [super orderOut: sender];
448 /* Make sure we leave fullscreen */
449 [self leaveFullscreen];
452 /* Make sure setFrame gets executed on main thread especially if we are animating.
453 * (Thus we won't block the video output thread) */
454 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
456 struct { NSRect frame; BOOL display; BOOL animate;} args;
460 args.display = display;
461 args.animate = animate;
463 packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
465 [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
466 withObject: packedargs waitUntilDone: YES];
469 - (void)setFrameOnMainThread:(NSData*)packedargs
471 struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
473 [super setFrame: args->frame display: args->display animate:args->animate];