1 /*****************************************************************************
2 * VLCVideoView.h: VLC.framework VLCVideoView implementation
3 *****************************************************************************
4 * Copyright (C) 2007 Pierre d'Herbemont
5 * Copyright (C) 2007 the VideoLAN team
8 * Authors: Pierre d'Herbemont <pdherbemont # videolan.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 #import "VLCVideoView.h"
26 #import "VLCLibrary.h"
27 #import "VLCEventManager.h"
31 #include <vlc/libvlc.h>
33 #import <QuartzCore/QuartzCore.h>
36 NSString *VLCVideoViewEnteredFullScreen = @"VLCVideoViewEnteredFullScreen";
37 NSString *VLCVideoViewLeftFullScreen = @"VLCVideoViewLeftFullScreen";
39 /******************************************************************************
40 * Soon deprecated stuff
43 /* This is a forward reference to VLCOpenGLVoutView specified in minimal_macosx
44 library. We could get rid of this, but it prevents warnings from the
45 compiler. (Scheduled to deletion) */
46 @interface VLCOpenGLVoutView : NSView
47 - (void)detachFromVout;
50 /* Depreacted methods */
51 @interface VLCVideoView (Deprecated)
52 - (void)setStretchesVideo:(BOOL)value;
53 - (BOOL)stretchesVideo;
55 - (void)addVoutSubview:(NSView *)aView; /* (Scheduled to deletion) */
56 - (void)removeVoutSubview:(NSView *)aView; /* (Scheduled to deletion) */
59 /******************************************************************************
60 * VLCVideoView (Private)
63 @interface VLCVideoView (Private)
65 - (void)addVoutLayer:(CALayer *)aLayer;
70 /******************************************************************************
71 * Interface & Implementation VLCConstraintLayoutManager
73 * Manage the size of the video layer
75 @interface VLCConstraintLayoutManager : CAConstraintLayoutManager
77 CGSize originalVideoSize;
78 BOOL fillScreenEntirely;
80 @property BOOL fillScreenEntirely;
81 @property CGSize originalVideoSize;
84 @implementation VLCConstraintLayoutManager
85 @synthesize fillScreenEntirely;
86 @synthesize originalVideoSize;
89 if( self = [super init] )
91 self.originalVideoSize = CGSizeMake(0., 0.);
92 self.fillScreenEntirely = NO;
99 return [[[VLCConstraintLayoutManager alloc] init] autorelease];
101 - (void)layoutSublayersOfLayer:(CALayer *)layer
103 /* Called by CA, when our rootLayer needs layout */
104 [super layoutSublayersOfLayer:layer];
106 /* After having done everything normally resize the vlcopengllayer */
107 if( [[layer sublayers] count] > 0 && [[[[layer sublayers] objectAtIndex:0] name] isEqualToString:@"vlcopengllayer"])
109 CALayer * videolayer = [[layer sublayers] objectAtIndex:0];
110 CGRect bounds = layer.bounds;
111 float new_height = (bounds.size.width * originalVideoSize.height) / originalVideoSize.width;
113 if( fillScreenEntirely )
115 if( bounds.size.height > new_height )
116 bounds.size.height = new_height;
118 bounds.size.width = (bounds.size.height * originalVideoSize.width) / originalVideoSize.height;
122 if( bounds.size.height > new_height )
123 bounds.size.width = (bounds.size.height * originalVideoSize.width) / originalVideoSize.height;
125 bounds.size.height = new_height;
128 bounds.origin = CGPointMake( 0.0, 0.0 );
129 videolayer.bounds = bounds;
130 videolayer.position = CGPointMake((layer.bounds.size.width - layer.bounds.origin.x)/2, (layer.bounds.size.height - layer.bounds.origin.y)/2);
135 /******************************************************************************
136 * Implementation VLCVideoView
139 @implementation VLCVideoView
143 return [layoutManager fillScreenEntirely];
145 - (void)setFillScreen:(BOOL)fillScreen
147 [layoutManager setFillScreenEntirely:fillScreen];
148 [[self layer] setNeedsLayout];
156 - (void)setFullScreen:(BOOL)newFullScreen
161 [self enterFullscreen];
166 [self leaveFullscreen];
171 - (id)initWithFrame:(NSRect)rect
173 if (self = [super initWithFrame:rect])
176 [self setBackColor:[NSColor blackColor]];
177 [self setStretchesVideo:NO];
178 [self setAutoresizesSubviews:YES];
179 [self setFillScreen: NO];
180 layoutManager = [VLCConstraintLayoutManager layoutManager];
187 [layoutManager release];
193 - (void)setDelegate:(id)value
203 - (void)setBackColor:(NSColor *)value
205 if (backColor != value)
208 backColor = [value retain];
212 - (NSColor *)backColor
217 /* This is a LibVLC notification that we're about to enter into full screen,
218 there is no other place where I can see where we can trap this event */
219 - (void)enterFullscreen
221 // Go ahead and send a notification to the world we're going into full screen
222 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
223 withDelegateMethod:nil
224 withNotificationName:VLCVideoViewEnteredFullScreen];
226 [super enterFullScreenMode:[[self window] screen] withOptions:nil];
227 if( !self.fullScreen ) self.fullScreen = YES;
230 /* This is a LibVLC notification that we're about to enter leaving full screen,
231 there is no other place where I can see where we can trap this event */
232 - (void)leaveFullscreen
234 // Go ahead and send a notification to the world we're leaving full screen
235 [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
236 withDelegateMethod:nil
237 withNotificationName:VLCVideoViewLeftFullScreen];
239 // There is nothing else to do, as this object strictly displays the video feed
240 [super exitFullScreenModeWithOptions:nil];
241 if( self.fullScreen ) self.fullScreen = NO;
244 - (void)drawRect:(NSRect)aRect
257 - (void)mouseDown:(NSEvent *)theEvent
259 if([theEvent clickCount] != 2)
262 [self leaveFullscreen];
264 [self enterFullscreen];
268 /******************************************************************************
269 * Implementation VLCVideoView (Private)
272 @implementation VLCVideoView (Private)
274 /* This is called by the libvlc module 'opengllayer' as soon as there is one
277 - (void)addVoutLayer:(CALayer *)aLayer
279 [CATransaction begin];
280 [self setWantsLayer: YES];
281 CALayer * rootLayer = [self layer];
283 aLayer.name = @"vlcopengllayer";
285 [layoutManager setOriginalVideoSize:aLayer.bounds.size];
286 [rootLayer setLayoutManager:layoutManager];
287 [rootLayer insertSublayer:aLayer atIndex:0];
289 [aLayer setNeedsLayout];
290 [aLayer setNeedsDisplay];
291 [rootLayer setNeedsDisplay];
292 [rootLayer layoutIfNeeded];
293 [CATransaction commit];
298 /******************************************************************************
299 * Implementation VLCVideoView (Deprecated)
302 @implementation VLCVideoView (Deprecated)
304 - (void)setStretchesVideo:(BOOL)value
306 stretchesVideo = value;
309 - (BOOL)stretchesVideo
311 return stretchesVideo;
314 /* This is called by the libvlc module 'minimal_macosx' as soon as there is one
317 - (void)addVoutSubview:(NSView *)aView /* (Scheduled to deletion) */
319 /* This is where the real video comes from */
320 if( [[self subviews] count] )
322 /* XXX: This is a hack until core gets fixed */
324 for( i = 0; i < [[self subviews] count]; i++ )
326 [[[self subviews] objectAtIndex:i] detachFromVout];
327 [[[self subviews] objectAtIndex:i] retain];
328 [[[self subviews] objectAtIndex:i] removeFromSuperview];
332 [aView setFrame:[self bounds]];
334 [self addSubview:aView];
336 // TODO: Should we let the media player specify these values?
337 [aView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
340 - (void)removeVoutSubview:(NSView *)view /* (Scheduled to deletion) */
342 // Should we do something? I don't know, however the protocol requires
343 // this to be implemented