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];
72 - (void)setTime:(NSString *)o_arg_time position:(float)f_position
74 [o_time setStringValue: o_arg_time];
75 [o_slider setFloatValue: f_position];
78 - (void)playStatusUpdated:(int)i_status
80 if( i_status == PLAYING_S )
82 [o_btn_play setImage: o_img_pause];
83 [o_btn_play setAlternateImage: o_img_pause_pressed];
84 [o_btn_play setToolTip: _NS("Pause")];
88 [o_btn_play setImage: o_img_play];
89 [o_btn_play setAlternateImage: o_img_play_pressed];
90 [o_btn_play setToolTip: _NS("Play")];
94 - (void)setSeekable:(BOOL)b_seekable
96 [o_btn_forward setEnabled: b_seekable];
97 [o_btn_backward setEnabled: b_seekable];
98 [o_slider setEnabled: b_seekable];
101 - (void)zoom:(id)sender
103 if( ![self isZoomed] )
105 NSRect zoomRect = [[self screen] frame];
106 o_saved_frame = [self frame];
107 /* we don't have to take care of the eventual menu bar and dock
108 as zoomRect will be cropped automatically by setFrame:display:
109 to the right rectangle */
110 [self setFrame: zoomRect display: YES animate: YES];
114 /* unzoom to the saved_frame if the o_saved_frame coords look sound
116 if( o_saved_frame.size.width > 0 && o_saved_frame.size.height > 0 )
117 [self setFrame: o_saved_frame display: YES animate: YES];
121 - (BOOL)windowShouldClose:(id)sender
123 playlist_t * p_playlist = pl_Yield( VLCIntf );
125 playlist_Stop( p_playlist );
126 vlc_object_release( p_playlist );
132 if (o_fullscreen_window)
138 /*****************************************************************************
147 - (void)enterFullscreen
149 NSMutableDictionary *dict1, *dict2;
153 vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
154 BOOL blackout_other_displays = var_GetBool( p_vout, "macosx-black" );
156 screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )];
158 vlc_object_release( p_vout );
161 screen = [self screen];
163 screen_rect = [screen frame];
165 [o_btn_fullscreen setState: YES];
167 [NSCursor setHiddenUntilMouseMoves: YES];
169 if (blackout_other_displays)
170 [screen blackoutOtherScreens]; /* We should do something like [screen blackoutOtherScreens]; */
172 /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
173 if (!o_fullscreen_window)
175 /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
177 rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
178 rect.origin.x += [self frame].origin.x;
179 rect.origin.y += [self frame].origin.y;
180 o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
181 [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
182 [o_fullscreen_window setCanBecomeKeyWindow: YES];
184 if (![self isVisible] || [self alphaValue] == 0.0 || MACOS_VERSION < 10.4f)
186 /* We don't animate if we are not visible or if we are running on
187 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
188 * simply fade the display */
189 CGDisplayFadeReservationToken token;
191 [o_fullscreen_window setFrame:screen_rect display:NO];
193 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
194 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
196 if (screen == [NSScreen mainScreen])
197 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
199 [[self contentView] replaceSubview:o_view with:o_temp_view];
200 [o_temp_view setFrame:[o_view frame]];
201 [o_fullscreen_window setContentView:o_view];
202 [o_fullscreen_window makeKeyAndOrderFront:self];
203 [self orderOut: self];
205 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
206 CGReleaseDisplayFadeReservation( token);
207 [self hasBecomeFullscreen];
211 /* Make sure we don't see the o_view disappearing of the screen during this operation */
212 DisableScreenUpdates();
213 [[self contentView] replaceSubview:o_view with:o_temp_view];
214 [o_temp_view setFrame:[o_view frame]];
215 [o_fullscreen_window setContentView:o_view];
216 [o_fullscreen_window makeKeyAndOrderFront:self];
217 EnableScreenUpdates();
220 if (MACOS_VERSION < 10.4f)
222 /* We were already fullscreen nothing to do when NSAnimation
223 * is not supported */
227 /* We are in fullscreen (and no animation is running) */
231 if (o_fullscreen_anim1)
233 [o_fullscreen_anim1 stopAnimation];
234 [o_fullscreen_anim1 release];
236 if (o_fullscreen_anim2)
238 [o_fullscreen_anim2 stopAnimation];
239 [o_fullscreen_anim2 release];
242 if (screen == [NSScreen mainScreen])
243 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
245 dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
246 dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
248 [dict1 setObject:self forKey:NSViewAnimationTargetKey];
249 [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
251 [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
252 [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
253 [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
255 /* Strategy with NSAnimation allocation:
256 - Keep at most 2 animation at a time
257 - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
259 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
260 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
265 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
266 [o_fullscreen_anim1 setDuration: 0.3];
267 [o_fullscreen_anim1 setFrameRate: 30];
268 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
269 [o_fullscreen_anim2 setDuration: 0.2];
270 [o_fullscreen_anim2 setFrameRate: 30];
272 [o_fullscreen_anim2 setDelegate: self];
273 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
275 [o_fullscreen_anim1 startAnimation];
278 - (void)hasBecomeFullscreen
280 [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] getControls] getVoutView]];
282 [o_fullscreen_window makeKeyWindow];
283 [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
285 /* tell the fspanel to move itself to front next time it's triggered */
286 [[[[VLCMain sharedInstance] getControls] getFSPanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
288 [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil];
292 - (void)leaveFullscreen
294 NSMutableDictionary *dict1, *dict2;
298 [o_btn_fullscreen setState: NO];
300 /* We always try to do so */
301 [NSScreen unblackoutScreens];
303 /* Don't do anything if o_fullscreen_window is already closed */
304 if (!o_fullscreen_window)
307 if (![self isVisible] || MACOS_VERSION < 10.4f)
309 /* We don't animate if we are not visible or if we are running on
310 * Mac OS X <10.4 which doesn't support NSAnimation, instead we
311 * simply fade the display */
312 CGDisplayFadeReservationToken token;
314 CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
315 CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
317 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
318 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
320 [self hasEndedFullscreen];
322 CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
323 CGReleaseDisplayFadeReservation( token);
327 [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
328 SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
330 if (o_fullscreen_anim1)
332 [o_fullscreen_anim1 stopAnimation];
333 [o_fullscreen_anim1 release];
335 if (o_fullscreen_anim2)
337 [o_fullscreen_anim2 stopAnimation];
338 [o_fullscreen_anim2 release];
341 frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
342 frame.origin.x += [self frame].origin.x;
343 frame.origin.y += [self frame].origin.y;
345 dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
346 [dict2 setObject:self forKey:NSViewAnimationTargetKey];
347 [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
349 o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
352 [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
353 [o_fullscreen_anim2 setDuration: 0.3];
354 [o_fullscreen_anim2 setFrameRate: 30];
356 [o_fullscreen_anim2 setDelegate: self];
358 dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
360 [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
361 [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
362 [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
364 o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
367 [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
368 [o_fullscreen_anim1 setDuration: 0.2];
369 [o_fullscreen_anim1 setFrameRate: 30];
370 [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
371 [o_fullscreen_anim1 startAnimation];
374 - (void)hasEndedFullscreen
376 /* This function is private and should be only triggered at the end of the fullscreen change animation */
377 /* Make sure we don't see the o_view disappearing of the screen during this operation */
378 DisableScreenUpdates();
380 [o_view removeFromSuperviewWithoutNeedingDisplay];
381 [[self contentView] replaceSubview:o_temp_view with:o_view];
383 [o_view setFrame:[o_temp_view frame]];
384 if ([self isVisible])
385 [self makeKeyAndOrderFront:self];
386 [o_fullscreen_window orderOut: self];
387 EnableScreenUpdates();
389 [o_fullscreen_window release];
390 o_fullscreen_window = nil;
393 - (void)animationDidEnd:(NSAnimation*)animation
395 NSArray *viewAnimations;
397 if ([animation currentValue] < 1.0)
400 /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
401 viewAnimations = [o_fullscreen_anim2 viewAnimations];
402 if ([viewAnimations count] >=1 &&
403 [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
405 /* Fullscreen ended */
406 [self hasEndedFullscreen];
410 /* Fullscreen started */
411 [self hasBecomeFullscreen];
415 - (void)orderOut: (id)sender
417 [super orderOut: sender];
418 /* Make sure we leave fullscreen */
419 [self leaveFullscreen];
422 /* Make sure setFrame gets executed on main thread especially if we are animating.
423 * (Thus we won't block the video output thread) */
424 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
426 struct { NSRect frame; BOOL display; BOOL animate;} args;
430 args.display = display;
431 args.animate = animate;
433 packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
435 [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
436 withObject: packedargs waitUntilDone: YES];
439 - (void)setFrameOnMainThread:(NSData*)packedargs
441 struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
443 [super setFrame: args->frame display: args->display animate:args->animate];