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 /* SetSystemUIMode, ... */
36 #import <Carbon/Carbon.h>
38 /*****************************************************************************
39 * VLCEmbeddedWindow Implementation
40 *****************************************************************************/
42 @implementation VLCEmbeddedWindow
44 - (id)initWithContentRect:(NSRect)contentRect styleMask: (NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
46 BOOL b_useTextured = YES;
47 if( [[NSWindow class] instancesRespondToSelector:@selector(setContentBorderThickness:forEdge:)] )
50 windowStyle ^= NSTexturedBackgroundWindowMask;
52 self = [super initWithContentRect:contentRect styleMask:windowStyle backing:bufferingType defer:deferCreation];
55 [self setContentBorderThickness:28.0 forEdge:NSMinYEdge];
62 [self setDelegate: self];
64 [o_btn_backward setToolTip: _NS("Rewind")];
65 [o_btn_forward setToolTip: _NS("Fast Forward")];
66 [o_btn_fullscreen setToolTip: _NS("Fullscreen")];
67 [o_btn_play setToolTip: _NS("Play")];
68 [o_slider setToolTip: _NS("Position")];
70 o_img_play = [NSImage imageNamed: @"play_embedded"];
71 o_img_pause = [NSImage imageNamed: @"pause_embedded"];
72 [self controlTintChanged];
73 [[NSNotificationCenter defaultCenter] addObserver: self
74 selector: @selector( controlTintChanged )
75 name: NSControlTintDidChangeNotification
78 /* Useful to save o_view frame in fullscreen mode */
79 o_temp_view = [[NSView alloc] init];
80 [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
82 o_fullscreen_window = nil;
83 o_fullscreen_anim1 = o_fullscreen_anim2 = nil;
85 /* Not fullscreen when we wake up */
86 [o_btn_fullscreen setState: NO];
89 [self setMovableByWindowBackground:YES];
91 [self setDelegate:self];
93 /* Make sure setVisible: returns NO */
95 b_window_is_invisible = YES;
96 videoRatio = NSMakeSize( 0., 0. );
99 - (void)controlTintChanged
102 if( [o_btn_play alternateImage] == o_img_play_pressed )
105 if( [NSColor currentControlTint] == NSGraphiteControlTint )
107 o_img_play_pressed = [NSImage imageNamed: @"play_embedded_graphite"];
108 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_graphite"];
109 [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_graphite"]];
110 [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_graphite"]];
111 [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_graphite"]];
115 o_img_play_pressed = [NSImage imageNamed: @"play_embedded_blue"];
116 o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_blue"];
117 [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_blue"]];
118 [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_blue"]];
119 [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_blue"]];
123 [o_btn_play setAlternateImage: o_img_play_pressed];
125 [o_btn_play setAlternateImage: o_img_pause_pressed];
130 [[NSNotificationCenter defaultCenter] removeObserver: self];
131 [o_img_play release];
132 [o_img_play_pressed release];
133 [o_img_pause release];
134 [o_img_pause_pressed release];
139 - (void)setTime:(NSString *)o_arg_time position:(float)f_position
141 [o_time setStringValue: o_arg_time];
142 [o_slider setFloatValue: f_position];
145 - (void)playStatusUpdated:(int)i_status
147 if( i_status == PLAYING_S )
149 [o_btn_play setImage: o_img_pause];
150 [o_btn_play setAlternateImage: o_img_pause_pressed];
151 [o_btn_play setToolTip: _NS("Pause")];
155 [o_btn_play setImage: o_img_play];
156 [o_btn_play setAlternateImage: o_img_play_pressed];
157 [o_btn_play setToolTip: _NS("Play")];
161 - (void)setSeekable:(BOOL)b_seekable
163 [o_btn_forward setEnabled: b_seekable];
164 [o_btn_backward setEnabled: b_seekable];
165 [o_slider setEnabled: b_seekable];
168 - (BOOL)windowShouldZoom:(NSWindow *)sender toFrame:(NSRect)newFrame
170 [self setFrame: newFrame display: YES animate: YES];
174 - (BOOL)windowShouldClose:(id)sender
176 playlist_t * p_playlist = pl_Hold( VLCIntf );
178 playlist_Stop( p_playlist );
179 pl_Release( VLCIntf );
185 if (o_fullscreen_window)
191 - (void)setVideoRatio:(NSSize)ratio
196 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
198 if( videoRatio.height == 0. || videoRatio.width == 0. )
199 return proposedFrameSize;
201 NSRect viewRect = [o_view convertRect:[o_view bounds] toView: nil];
202 NSRect contentRect = [self contentRectForFrameRect:[self frame]];
203 float marginy = viewRect.origin.y + [self frame].size.height - contentRect.size.height;
204 float marginx = contentRect.size.width - viewRect.size.width;
205 proposedFrameSize.height = (proposedFrameSize.width - marginx) * videoRatio.height / videoRatio.width + marginy;
207 return proposedFrameSize;
210 /*****************************************************************************
219 - (void)lockFullscreenAnimation
221 [o_animation_lock lock];
224 - (void)unlockFullscreenAnimation
226 [o_animation_lock unlock];
229 - (void)enterFullscreen
231 NSMutableDictionary *dict1, *dict2;
235 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
236 BOOL blackout_other_displays = config_GetInt( VLCIntf, "macosx-black" );
238 screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )];
240 [self lockFullscreenAnimation];
244 msg_Dbg( p_vout, "chosen screen isn't present, using current screen for fullscreen mode" );
245 screen = [self screen];
249 msg_Dbg( p_vout, "Using deepest screen" );
250 screen = [NSScreen deepestScreen];
253 vlc_object_release( p_vout );
255 screen_rect = [screen frame];
257 [o_btn_fullscreen setState: YES];
259 [NSCursor setHiddenUntilMouseMoves: YES];
261 if( blackout_other_displays )
262 [screen blackoutOtherScreens];
264 /* Make sure we don't see the window flashes in float-on-top mode */
265 originalLevel = [self level];
266 [self setLevel:NSNormalWindowLevel];
268 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
269 if (!o_fullscreen_window)
271 /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
273 rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
274 rect.origin.x += [self frame].origin.x;
275 rect.origin.y += [self frame].origin.y;
276 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
277 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
278 [o_fullscreen_window setCanBecomeKeyWindow: YES];
280 if (![self isVisible] || [self alphaValue] == 0.0)
282 /* We don't animate if we are not visible, instead we
283 * simply fade the display */
284 CGDisplayFadeReservationToken token;
286 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
287 CGDisplayFade( token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
289 if ([screen isMainScreen])
290 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
292 [[self contentView] replaceSubview:o_view with:o_temp_view];
293 [o_temp_view setFrame:[o_view frame]];
294 [o_fullscreen_window setContentView:o_view];
296 [o_fullscreen_window makeKeyAndOrderFront:self];
297 [o_fullscreen_window orderFront:self animate:YES];
299 [o_fullscreen_window setFrame:screen_rect display:YES];
301 CGDisplayFade( token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
302 CGReleaseDisplayFadeReservation( token);
304 /* Will release the lock */
305 [self hasBecomeFullscreen];
310 /* Make sure we don't see the o_view disappearing of the screen during this operation */
311 NSDisableScreenUpdates();
312 [[self contentView] replaceSubview:o_view with:o_temp_view];
313 [o_temp_view setFrame:[o_view frame]];
314 [o_fullscreen_window setContentView:o_view];
315 [o_fullscreen_window makeKeyAndOrderFront:self];
316 NSEnableScreenUpdates();
319 /* We are in fullscreen (and no animation is running) */
322 /* Make sure we are hidden */
323 [super orderOut: self];
324 [self unlockFullscreenAnimation];
328 if (o_fullscreen_anim1)
330 [o_fullscreen_anim1 stopAnimation];
331 [o_fullscreen_anim1 release];
333 if (o_fullscreen_anim2)
335 [o_fullscreen_anim2 stopAnimation];
336 [o_fullscreen_anim2 release];
339 if ([screen isMainScreen])
340 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
342 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
343 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
345 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
346 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
348 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
349 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
350 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
352 /* Strategy with NSAnimation allocation:
353 - Keep at most 2 animation at a time
354 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
356 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
357 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
362 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
363 [o_fullscreen_anim1 setDuration: 0.3];
364 [o_fullscreen_anim1 setFrameRate: 30];
365 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
366 [o_fullscreen_anim2 setDuration: 0.2];
367 [o_fullscreen_anim2 setFrameRate: 30];
369 [o_fullscreen_anim2 setDelegate: self];
370 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
372 [o_fullscreen_anim1 startAnimation];
373 /* fullscreenAnimation will be unlocked when animation ends */
376 - (void)hasBecomeFullscreen
378 [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] controls] voutView]];
380 [o_fullscreen_window makeKeyWindow];
381 [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
383 /* tell the fspanel to move itself to front next time it's triggered */
384 [[[[VLCMain sharedInstance] controls] fspanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
387 [super orderOut: self];
389 [[[[VLCMain sharedInstance] controls] fspanel] setActive: nil];
392 [self unlockFullscreenAnimation];
395 - (void)leaveFullscreen
397 [self leaveFullscreenAndFadeOut: NO];
400 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
402 NSMutableDictionary *dict1, *dict2;
405 [self lockFullscreenAnimation];
408 [o_btn_fullscreen setState: NO];
410 /* We always try to do so */
411 [NSScreen unblackoutScreens];
413 /* Don't do anything if o_fullscreen_window is already closed */
414 if (!o_fullscreen_window)
416 [self unlockFullscreenAnimation];
422 /* We don't animate if we are not visible, instead we
423 * simply fade the display */
424 CGDisplayFadeReservationToken token;
426 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
427 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
429 [[[[VLCMain sharedInstance] controls] fspanel] setNonActive: nil];
430 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
432 /* Will release the lock */
433 [self hasEndedFullscreen];
435 /* Our window is hidden, and might be faded. We need to workaround that, so note it
437 b_window_is_invisible = YES;
439 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
440 CGReleaseDisplayFadeReservation( token);
444 [self setAlphaValue: 0.0];
445 [self orderFront: self];
447 [[[[VLCMain sharedInstance] controls] fspanel] setNonActive: nil];
448 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
450 if (o_fullscreen_anim1)
452 [o_fullscreen_anim1 stopAnimation];
453 [o_fullscreen_anim1 release];
455 if (o_fullscreen_anim2)
457 [o_fullscreen_anim2 stopAnimation];
458 [o_fullscreen_anim2 release];
461 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
462 frame.origin.x += [self frame].origin.x;
463 frame.origin.y += [self frame].origin.y;
465 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
466 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
467 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
469 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
472 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
473 [o_fullscreen_anim2 setDuration: 0.3];
474 [o_fullscreen_anim2 setFrameRate: 30];
476 [o_fullscreen_anim2 setDelegate: self];
478 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
480 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
481 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
482 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
484 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
487 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
488 [o_fullscreen_anim1 setDuration: 0.2];
489 [o_fullscreen_anim1 setFrameRate: 30];
490 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
492 /* Make sure o_fullscreen_window is the frontmost window */
493 [o_fullscreen_window orderFront: self];
495 [o_fullscreen_anim1 startAnimation];
496 /* fullscreenAnimation will be unlocked when animation ends */
499 - (void)hasEndedFullscreen
501 /* This function is private and should be only triggered at the end of the fullscreen change animation */
502 /* Make sure we don't see the o_view disappearing of the screen during this operation */
503 NSDisableScreenUpdates();
505 [o_view removeFromSuperviewWithoutNeedingDisplay];
506 [[self contentView] replaceSubview:o_temp_view with:o_view];
508 [o_view setFrame:[o_temp_view frame]];
509 [self makeFirstResponder: o_view];
510 if ([self isVisible])
511 [super makeKeyAndOrderFront:self]; /* our version contains a workaround */
512 [o_fullscreen_window orderOut: self];
513 NSEnableScreenUpdates();
515 [o_fullscreen_window release];
516 o_fullscreen_window = nil;
517 [self setLevel:originalLevel];
519 [self unlockFullscreenAnimation];
522 - (void)animationDidEnd:(NSAnimation*)animation
524 NSArray *viewAnimations;
526 if ([animation currentValue] < 1.0)
529 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
530 viewAnimations = [o_fullscreen_anim2 viewAnimations];
531 if ([viewAnimations count] >=1 &&
532 [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
534 /* Fullscreen ended */
535 [self hasEndedFullscreen];
539 /* Fullscreen started */
540 [self hasBecomeFullscreen];
544 - (void)orderOut: (id)sender
546 [super orderOut: sender];
548 /* Make sure we leave fullscreen */
549 [self leaveFullscreenAndFadeOut: YES];
552 - (void)makeKeyAndOrderFront: (id)sender
555 * when we exit fullscreen and fade out, we may endup in
556 * having a window that is faded. We can't have it fade in unless we
559 if(!b_window_is_invisible)
561 /* Make sure we don't do it too much */
562 [super makeKeyAndOrderFront: sender];
566 [super setAlphaValue:0.0f];
567 [super makeKeyAndOrderFront: sender];
569 NSMutableDictionary * dict = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
570 [dict setObject:self forKey:NSViewAnimationTargetKey];
571 [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
573 NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
575 [anim setAnimationBlockingMode: NSAnimationNonblocking];
576 [anim setDuration: 0.1];
577 [anim setFrameRate: 30];
579 [anim startAnimation];
580 b_window_is_invisible = NO;
582 /* fullscreenAnimation will be unlocked when animation ends */
587 /* Make sure setFrame gets executed on main thread especially if we are animating.
588 * (Thus we won't block the video output thread) */
589 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
591 struct { NSRect frame; BOOL display; BOOL animate;} args;
595 args.display = display;
596 args.animate = animate;
598 packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
600 [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
601 withObject: packedargs waitUntilDone: YES];
604 - (void)setFrameOnMainThread:(NSData*)packedargs
606 struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
610 /* Make sure we don't block too long and set up a non blocking animation */
611 NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
612 self, NSViewAnimationTargetKey,
613 [NSValue valueWithRect:[self frame]], NSViewAnimationStartFrameKey,
614 [NSValue valueWithRect:args->frame], NSViewAnimationEndFrameKey, nil];
616 NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
618 [anim setAnimationBlockingMode: NSAnimationNonblocking];
619 [anim setDuration: 0.4];
620 [anim setFrameRate: 30];
621 [anim startAnimation];
624 [super setFrame:args->frame display:args->display animate:args->animate];