1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * Eric Petit <titer@m0k.org>
12 * Benjamin Pracht <bigben at videolan dot org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
32 #include <errno.h> /* ENOMEM */
33 #include <stdlib.h> /* free() */
34 #include <string.h> /* strerror() */
36 /* BeginFullScreen, EndFullScreen */
37 #include <QuickTime/QuickTime.h>
45 /*****************************************************************************
46 * DeviceCallback: Callback triggered when the video-device variable is changed
47 *****************************************************************************/
48 int DeviceCallback( vlc_object_t *p_this, const char *psz_variable,
49 vlc_value_t old_val, vlc_value_t new_val, void *param )
52 vout_thread_t *p_vout = (vout_thread_t *)p_this;
54 msg_Dbg( p_vout, "set %d", new_val.i_int );
55 var_Create( p_vout->p_vlc, "video-device", VLC_VAR_INTEGER );
56 var_Set( p_vout->p_vlc, "video-device", new_val );
58 val.b_bool = VLC_TRUE;
59 var_Set( p_vout, "intf-change", val );
64 /*****************************************************************************
65 * VLCEmbeddedList implementation
66 *****************************************************************************/
67 @implementation VLCEmbeddedList
72 o_embedded_array = [NSMutableArray array];
80 for( i = 0; i < [o_embedded_array count]; i++ )
82 id o_vout_view = [o_embedded_array objectAtIndex: i];
83 if( ![o_vout_view isUsed] )
85 [o_vout_view setUsed: YES];
92 - (void)releaseEmbeddedVout: (id)o_vout_view
94 if( [o_embedded_array containsObject: o_vout_view] )
96 [o_vout_view setUsed: NO];
100 msg_Warn( VLCIntf, "Cannot find Video Output");
104 - (void)addEmbeddedVout: (id)o_vout_view
106 if( ![o_embedded_array containsObject: o_vout_view] )
108 [o_embedded_array addObject: o_vout_view];
112 - (BOOL)windowContainsEmbedded: (id)o_window
114 return ([self getViewForWindow: o_window] == nil ? NO : YES);
117 - (id)getViewForWindow: (id)o_window
119 id o_enumerator = [o_embedded_array objectEnumerator];
120 id o_current_embedded;
122 while( (o_current_embedded = [o_enumerator nextObject]) )
124 if( [o_current_embedded getWindow] == o_window )
126 return o_current_embedded;
134 /*****************************************************************************
135 * VLCVoutView implementation
136 *****************************************************************************/
137 @implementation VLCVoutView
139 - (id)initWithFrame:(NSRect)frameRect
141 [super initWithFrame: frameRect];
144 s_frame = &frameRect;
151 - (BOOL)setVout: (vout_thread_t *) vout subView: (NSView *) view
152 frame: (NSRect *) frame
155 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
156 NSArray *o_screens = [NSScreen screens];
162 if( [o_screens count] <= 0 )
164 msg_Err( p_vout, "no OSX screens available" );
168 p_real_vout = [VLCVoutView getRealVout: p_vout];
170 /* Get the pref value when this is the first time, otherwise retrieve the device from the top level video-device var */
171 if( var_Type( p_real_vout->p_vlc, "video-device" ) == 0 )
173 i_device = var_GetInteger( p_vout, "macosx-vdev" );
177 i_device = var_GetInteger( p_real_vout->p_vlc, "video-device" );
180 /* Setup the menuitem for the multiple displays. */
181 if( var_Type( p_real_vout, "video-device" ) == 0 )
184 vlc_value_t val2, text;
187 var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER |
189 text.psz_string = _("Video Device");
190 var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
192 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
195 text.psz_string = _("Default");
196 var_Change( p_real_vout, "video-device",
197 VLC_VAR_ADDCHOICE, &val2, &text );
198 var_Set( p_real_vout, "video-device", val2 );
200 while( (o_screen = [o_enumerator nextObject]) != NULL )
203 NSRect s_rect = [o_screen frame];
205 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
206 "%s %d (%dx%d)", _("Screen"), i,
207 (int)s_rect.size.width, (int)s_rect.size.height );
209 text.psz_string = psz_temp;
211 var_Change( p_real_vout, "video-device",
212 VLC_VAR_ADDCHOICE, &val2, &text );
215 var_Set( p_real_vout, "video-device", val2 );
220 var_AddCallback( p_real_vout, "video-device", DeviceCallback,
223 val2.b_bool = VLC_TRUE;
224 var_Set( p_real_vout, "intf-change", val2 );
227 /* Add the view. It's automatically resized to fit the window */
228 [self addSubview: o_view];
229 [self setAutoresizesSubviews: YES];
235 - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize
237 [super resizeSubviewsWithOldSize: oldBoundsSize];
238 [o_view setFrameSize: [self frame].size];
243 [o_view removeFromSuperview];
253 NSMutableString * o_title = nil, * o_mrl = nil;
254 input_thread_t * p_input;
261 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
263 if( p_input == NULL )
268 if( p_input->input.p_item->psz_name != NULL )
269 o_title = [NSMutableString stringWithUTF8String:
270 p_input->input.p_item->psz_name];
271 if( p_input->input.p_item->psz_uri != NULL )
272 o_mrl = [NSMutableString stringWithUTF8String:
273 p_input->input.p_item->psz_uri];
279 if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
281 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
282 if( prefix_range.location != NSNotFound )
283 [o_mrl deleteCharactersInRange: prefix_range];
284 [o_window setRepresentedFilename: o_mrl];
286 [o_window setTitle: o_title];
290 [o_window setTitle: [NSString stringWithCString: VOUT_TITLE]];
292 vlc_object_release( p_input );
296 - (void)setOnTop:(BOOL)b_on_top
300 [o_window setLevel: NSStatusWindowLevel];
304 [o_window setLevel: NSNormalWindowLevel];
308 - (void)scaleWindowWithFactor: (float)factor
311 int i_corrected_height, i_corrected_width;
313 NSPoint topleftscreen;
315 if ( !p_vout->b_fullscreen )
319 topleftbase.y = [o_window frame].size.height;
320 topleftscreen = [o_window convertBaseToScreen: topleftbase];
322 if( p_vout->render.i_height * p_vout->render.i_aspect >
323 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
325 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
327 newsize.width = (int) ( i_corrected_width * factor );
328 newsize.height = (int) ( p_vout->render.i_height * factor );
332 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
333 p_vout->render.i_aspect;
334 newsize.width = (int) ( p_vout->render.i_width * factor );
335 newsize.height = (int) ( i_corrected_height * factor );
338 /* Calculate the window's new size */
339 new_frame.size.width = [o_window frame].size.width -
340 [self frame].size.width + newsize.width;
341 new_frame.size.height = [o_window frame].size.height -
342 [self frame].size.height + newsize.height;
344 new_frame.origin.x = topleftscreen.x;
345 new_frame.origin.y = topleftscreen.y - new_frame.size.height;
347 [o_window setFrame: new_frame display: YES];
349 p_vout->i_changes |= VOUT_SIZE_CHANGE;
353 - (void)toggleFloatOnTop
357 if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
359 val.b_bool = VLC_FALSE;
363 val.b_bool = VLC_TRUE;
365 var_Set( p_real_vout, "video-on-top", val );
368 - (void)toggleFullscreen
371 var_Get( p_real_vout, "fullscreen", &val );
372 val.b_bool = !val.b_bool;
373 var_Set( p_real_vout, "fullscreen", val );
379 var_Get( p_real_vout, "fullscreen", &val );
380 return( val.b_bool );
385 vout_Control( p_real_vout, VOUT_SNAPSHOT );
390 /* Disable Screensaver */
391 UpdateSystemActivity( UsrActivity );
399 - (void)keyDown:(NSEvent *)o_event
403 unsigned int i_pressed_modifiers = 0;
406 i_pressed_modifiers = [o_event modifierFlags];
408 if( i_pressed_modifiers & NSShiftKeyMask )
409 val.i_int |= KEY_MODIFIER_SHIFT;
410 if( i_pressed_modifiers & NSControlKeyMask )
411 val.i_int |= KEY_MODIFIER_CTRL;
412 if( i_pressed_modifiers & NSAlternateKeyMask )
413 val.i_int |= KEY_MODIFIER_ALT;
414 if( i_pressed_modifiers & NSCommandKeyMask )
415 val.i_int |= KEY_MODIFIER_COMMAND;
417 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
421 /* Escape should always get you out of fullscreen */
422 if( key == (unichar) 0x1b )
424 if( [self isFullscreen] )
426 [self toggleFullscreen];
429 else if ( key == ' ' )
432 val.i_int = config_GetInt( p_vout, "key-play-pause" );
433 var_Set( p_vout->p_vlc, "key-pressed", val );
437 val.i_int |= CocoaKeyToVLC( key );
438 var_Set( p_vout->p_vlc, "key-pressed", val );
443 [super keyDown: o_event];
447 - (void)mouseDown:(NSEvent *)o_event
453 switch( [o_event type] )
455 case NSLeftMouseDown:
457 var_Get( p_vout, "mouse-button-down", &val );
459 var_Set( p_vout, "mouse-button-down", val );
464 [super mouseDown: o_event];
470 - (void)otherMouseDown:(NSEvent *)o_event
476 switch( [o_event type] )
478 case NSOtherMouseDown:
480 var_Get( p_vout, "mouse-button-down", &val );
482 var_Set( p_vout, "mouse-button-down", val );
487 [super mouseDown: o_event];
493 - (void)rightMouseDown:(NSEvent *)o_event
499 switch( [o_event type] )
501 case NSRightMouseDown:
503 var_Get( p_vout, "mouse-button-down", &val );
505 var_Set( p_vout, "mouse-button-down", val );
510 [super mouseDown: o_event];
516 - (void)mouseUp:(NSEvent *)o_event
522 switch( [o_event type] )
527 b_val.b_bool = VLC_TRUE;
528 var_Set( p_vout, "mouse-clicked", b_val );
530 var_Get( p_vout, "mouse-button-down", &val );
532 var_Set( p_vout, "mouse-button-down", val );
537 [super mouseUp: o_event];
543 - (void)otherMouseUp:(NSEvent *)o_event
549 switch( [o_event type] )
553 var_Get( p_vout, "mouse-button-down", &val );
555 var_Set( p_vout, "mouse-button-down", val );
560 [super mouseUp: o_event];
566 - (void)rightMouseUp:(NSEvent *)o_event
572 switch( [o_event type] )
576 var_Get( p_vout, "mouse-button-down", &val );
578 var_Set( p_vout, "mouse-button-down", val );
583 [super mouseUp: o_event];
589 - (void)mouseDragged:(NSEvent *)o_event
591 [self mouseMoved: o_event];
594 - (void)otherMouseDragged:(NSEvent *)o_event
596 [self mouseMoved: o_event];
599 - (void)rightMouseDragged:(NSEvent *)o_event
601 [self mouseMoved: o_event];
604 - (void)mouseMoved:(NSEvent *)o_event
612 s_rect = [o_view bounds];
613 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
614 b_inside = [o_view mouse: ml inRect: s_rect];
619 unsigned int i_width, i_height, i_x, i_y;
621 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
622 (unsigned int)s_rect.size.height,
623 &i_x, &i_y, &i_width, &i_height );
625 val.i_int = ( ((int)ml.x) - i_x ) *
626 p_vout->render.i_width / i_width;
627 var_Set( p_vout, "mouse-x", val );
629 if( [[o_view className] isEqualToString: @"VLCGLView"] )
631 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
632 p_vout->render.i_height / i_height;
636 val.i_int = ( ((int)ml.y) - i_y ) *
637 p_vout->render.i_height / i_height;
639 var_Set( p_vout, "mouse-y", val );
641 val.b_bool = VLC_TRUE;
642 var_Set( p_vout, "mouse-moved", val );
645 [super mouseMoved: o_event];
648 - (BOOL)acceptsFirstResponder
653 - (BOOL)becomeFirstResponder
658 - (BOOL)resignFirstResponder
660 /* We need to stay the first responder or we'll miss some
665 /* Class methods used by the different vout modules */
667 + (vout_thread_t *)getRealVout: (vout_thread_t *)p_vout
669 /* p_real_vout: the vout we have to use to check for video-on-top
670 and a few other things. If we are the QuickTime output, it's us.
671 It we are the OpenGL provider, it is our parent. */
672 if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
674 return (vout_thread_t *) p_vout->p_parent;
683 + (id)getVoutView: (vout_thread_t *)p_vout subView: (NSView *)view
684 frame: (NSRect *)s_frame
686 vlc_value_t value_drawable;
689 vout_thread_t * p_real_vout = [VLCVoutView getRealVout: p_vout];
691 var_Get( p_vout->p_vlc, "drawable", &value_drawable );
693 var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
694 var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
695 var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
696 var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
697 var_Create( p_vout, "macosx-background", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
698 var_Create( p_vout, "macosx-embedded", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
701 /* We only wait for NSApp to initialise if we're not embedded (as in the
702 * case of the Mozilla plugin). We can tell whether we're embedded or not
703 * by examining the "drawable" value: if it's zero, we're running in the
704 * main Mac intf; if it's non-zero, we're embedded. */
705 if( value_drawable.i_int == 0 )
707 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
708 for( i_timeout = 20 ; i_timeout-- ; )
712 msleep( INTF_IDLE_SLEEP );
718 /* No MacOS X intf, unable to communicate with MT */
719 msg_Err( p_vout, "no MacOS X interface present" );
724 if ( VLCIntf && !(p_vout->b_fullscreen) &&
725 !(var_GetBool( p_real_vout, "macosx-background" )) &&
726 var_GetBool( p_vout, "macosx-embedded") )
728 o_return = [[[VLCMain sharedInstance] getEmbeddedList]
734 /* No embedded vout is available */
735 if( o_return == nil )
738 bzero( &null_rect, sizeof( NSRect ) );
739 o_return = [[VLCDetachedVoutView alloc] initWithFrame: null_rect ];
741 [o_return setVout: p_vout subView: view frame: s_frame];
747 /*****************************************************************************
748 * VLCDetachedVoutView implementation
749 *****************************************************************************/
750 @implementation VLCDetachedVoutView
752 - (id)initWithFrame: (NSRect)frameRect
754 [super initWithFrame: frameRect];
755 i_time_mouse_last_moved = 0;
759 - (bool)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view
760 frame: (NSRect *) s_arg_frame
762 BOOL b_return = [super setVout: p_arg_vout subView: view frame:s_arg_frame];
763 i_time_mouse_last_moved = mdate();
764 o_window = [[VLCWindow alloc] initWithVout: p_arg_vout view: self
767 [view setFrame: [self frame]];
768 [o_window setAcceptsMouseMovedEvents: TRUE];
774 [o_window closeWindow];
775 [o_window setAcceptsMouseMovedEvents: NO];
776 i_time_mouse_last_moved = 0;
780 - (void)mouseMoved:(NSEvent *)o_event
782 i_time_mouse_last_moved = mdate();
783 [super mouseMoved: o_event];
786 - (void)hideMouse:(BOOL)b_hide
790 NSView *o_contents = [o_window contentView];
792 ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
793 ml = [o_contents convertPoint:ml fromView:nil];
794 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
796 if( b_hide && b_inside )
798 [NSCursor setHiddenUntilMouseMoves: YES];
802 [NSCursor setHiddenUntilMouseMoves: NO];
809 if( p_vout->b_fullscreen )
811 if( mdate() - i_time_mouse_last_moved > 3000000 )
813 [self hideMouse: YES];
818 [self hideMouse: NO];
824 /*****************************************************************************
825 * VLCEmbeddedVoutView implementation
826 *****************************************************************************/
828 @implementation VLCEmbeddedVoutView
830 - (id)initWithFrame: (NSRect)frameRect
832 [super initWithFrame: frameRect];
834 [[[VLCMain sharedInstance] getEmbeddedList] addEmbeddedVout: self];
838 - (BOOL)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view
839 frame: (NSRect *) s_arg_frame
843 b_return = [super setVout: p_arg_vout subView: view frame: s_arg_frame];
846 o_window = [self window];
847 [o_window makeKeyAndOrderFront: self];
848 [o_window setAcceptsMouseMovedEvents: TRUE];
849 [view setFrameSize: [self frame].size];
854 - (void)setUsed: (BOOL)b_new_used
867 [o_window setAcceptsMouseMovedEvents: NO];
868 [[[VLCMain sharedInstance] getEmbeddedList] releaseEmbeddedVout: self];
874 @implementation VLCDetachedEmbeddedVoutView
876 - (BOOL)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view
877 frame: (NSRect *) s_arg_frame
879 BOOL b_return = [super setVout: p_arg_vout subView: view frame: s_arg_frame];
883 [o_window setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
885 [self scaleWindowWithFactor: 1.0];
886 [o_window makeKeyAndOrderFront: self];
893 [o_window orderOut: self];
899 /*****************************************************************************
900 * VLCWindow implementation
901 *****************************************************************************/
902 @implementation VLCWindow
904 - (id) initWithVout: (vout_thread_t *) vout view: (VLCVoutView *) view
905 frame: (NSRect *) frame
911 [self performSelectorOnMainThread: @selector(initReal:)
912 withObject: NULL waitUntilDone: YES];
922 - (id)initReal: (id) sender
924 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
925 NSArray *o_screens = [NSScreen screens];
927 vlc_bool_t b_menubar_screen = VLC_FALSE;
930 b_init_ok = VLC_FALSE;
932 p_fullscreen_state = NULL;
934 p_real_vout = [VLCVoutView getRealVout: p_vout];
935 i_device = var_GetInteger( p_real_vout->p_vlc, "video-device" );
937 /* Find out on which screen to open the window */
938 if( i_device <= 0 || i_device > (int)[o_screens count] )
940 /* No preference specified. Use the main screen */
941 o_screen = [NSScreen mainScreen];
942 if( o_screen == [o_screens objectAtIndex: 0] )
943 b_menubar_screen = VLC_TRUE;
948 o_screen = [o_screens objectAtIndex: i_device];
949 b_menubar_screen = ( i_device == 0 );
952 if( p_vout->b_fullscreen )
954 NSRect screen_rect = [o_screen frame];
955 screen_rect.origin.x = screen_rect.origin.y = 0;
957 /* Creates a window with size: screen_rect on o_screen */
958 [self initWithContentRect: screen_rect
959 styleMask: NSBorderlessWindowMask
960 backing: NSBackingStoreBuffered
961 defer: YES screen: o_screen];
963 if( b_menubar_screen )
965 BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
966 NULL, NULL, fullScreenAllowEvents );
969 else if( var_GetBool( p_real_vout, "macosx-background" ) )
971 NSRect screen_rect = [o_screen frame];
972 screen_rect.origin.x = screen_rect.origin.y = 0;
974 /* Creates a window with size: screen_rect on o_screen */
975 [self initWithContentRect: screen_rect
976 styleMask: NSBorderlessWindowMask
977 backing: NSBackingStoreBuffered
978 defer: YES screen: o_screen];
980 [self setLevel: CGWindowLevelForKey(kCGDesktopWindowLevelKey)];
984 unsigned int i_stylemask = NSTitledWindowMask |
985 NSMiniaturizableWindowMask |
986 NSClosableWindowMask |
987 NSResizableWindowMask;
992 s_rect.size.width = p_vout->i_window_width;
993 s_rect.size.height = p_vout->i_window_height;
1000 [self initWithContentRect: s_rect
1001 styleMask: i_stylemask
1002 backing: NSBackingStoreBuffered
1003 defer: YES screen: o_screen];
1005 [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
1007 if( var_GetBool( p_real_vout, "video-on-top" ) )
1009 [self setLevel: NSStatusWindowLevel];
1018 [self makeKeyAndOrderFront: nil];
1019 [self setReleasedWhenClosed: YES];
1021 /* We'll catch mouse events */
1022 [self makeFirstResponder: o_view];
1024 /* Add the view. It's automatically resized to fit the window */
1025 [self setContentView: o_view];
1029 b_init_ok = VLC_TRUE;
1038 - (void) closeWindow
1040 /* XXX waitUntilDone = NO to avoid a possible deadlock when hitting
1042 [self setContentView: NULL];
1043 [self performSelectorOnMainThread: @selector(closeReal:)
1044 withObject: NULL waitUntilDone: NO];
1047 - (id) closeReal: (id) sender
1050 if( p_fullscreen_state )
1052 EndFullScreen( p_fullscreen_state, 0 );
1062 - (BOOL)canBecomeKeyWindow
1067 /* Sometimes crashes VLC....
1068 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
1070 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
1073 /* This is actually the same as VLCControls::stop. */
1075 - (BOOL)windowShouldClose:(id)sender
1077 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1079 if( p_playlist == NULL )
1084 playlist_Stop( p_playlist );
1085 vlc_object_release( p_playlist );
1087 /* The window will be closed by the intf later. */