X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fgui%2Fmacosx%2Fvout.m;h=99712961a7908f5fe1ca458614935c5ad134dc34;hb=8907852c4f4354ac3d7a672521915c78749f7d84;hp=5a6c0fea9b53f22a8275cf3605b2d9d026b16fda;hpb=fe087a38282e93addb25fa9598393e40ea233b09;p=vlc diff --git a/modules/gui/macosx/vout.m b/modules/gui/macosx/vout.m index 5a6c0fea9b..99712961a7 100644 --- a/modules/gui/macosx/vout.m +++ b/modules/gui/macosx/vout.m @@ -1,7 +1,7 @@ /***************************************************************************** * vout.m: MacOS X video output module ***************************************************************************** - * Copyright (C) 2001-2005 the VideoLAN team + * Copyright (C) 2001-2008 the VideoLAN team * $Id$ * * Authors: Colin Delacroix @@ -9,12 +9,14 @@ * Jon Lech Johansen * Derk-Jan Hartman * Eric Petit + * Benjamin Pracht + * Felix Paul Kühne * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -22,7 +24,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** @@ -30,7 +32,7 @@ *****************************************************************************/ #include /* ENOMEM */ #include /* free() */ -#include /* strerror() */ +#include /* BeginFullScreen, EndFullScreen */ #include @@ -38,8 +40,10 @@ #include #include "intf.h" +#include "fspanel.h" #include "vout.h" - +#import "controls.h" +#import "embeddedwindow.h" /***************************************************************************** * DeviceCallback: Callback triggered when the video-device variable is changed @@ -49,110 +53,136 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, { vlc_value_t val; vout_thread_t *p_vout = (vout_thread_t *)p_this; - + msg_Dbg( p_vout, "set %d", new_val.i_int ); - var_Create( p_vout->p_vlc, "video-device", VLC_VAR_INTEGER ); - var_Set( p_vout->p_vlc, "video-device", new_val ); - - val.b_bool = VLC_TRUE; + var_Create( p_vout->p_libvlc, "video-device", VLC_VAR_INTEGER ); + var_Set( p_vout->p_libvlc, "video-device", new_val ); + + val.b_bool = true; var_Set( p_vout, "intf-change", val ); return VLC_SUCCESS; } /***************************************************************************** - * VLCWindow implementation + * VLCEmbeddedList implementation *****************************************************************************/ -@implementation VLCWindow +@implementation VLCEmbeddedList -- (id) initWithVout: (vout_thread_t *) vout view: (NSView *) view - frame: (NSRect *) frame +- (id)init { - p_vout = vout; - o_view = view; - s_frame = frame; - - [self performSelectorOnMainThread: @selector(initReal:) - withObject: NULL waitUntilDone: YES]; - - if( !b_init_ok ) - { - return NULL; - } - + [super init]; + o_embedded_array = [NSMutableArray array]; return self; } -- (id) initReal: (id) sender +- (id)getEmbeddedVout { - NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init]; - NSArray *o_screens = [NSScreen screens]; - NSScreen *o_screen; - vlc_bool_t b_menubar_screen = VLC_FALSE; - int i_timeout, i_device; - vlc_value_t value_drawable; - - b_init_ok = VLC_FALSE; + unsigned int i; - var_Get( p_vout->p_vlc, "drawable", &value_drawable ); - - /* We only wait for NSApp to initialise if we're not embedded (as in the - * case of the Mozilla plugin). We can tell whether we're embedded or not - * by examining the "drawable" value: if it's zero, we're running in the - * main Mac intf; if it's non-zero, we're embedded. */ - if( value_drawable.i_int == 0 ) + for( i = 0; i < [o_embedded_array count]; i++ ) { - /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */ - for( i_timeout = 20 ; i_timeout-- ; ) + id o_vout_view = [o_embedded_array objectAtIndex: i]; + if( ![o_vout_view isUsed] ) { - if( NSApp == NULL ) - { - msleep( INTF_IDLE_SLEEP ); - } + [o_vout_view setUsed: YES]; + return o_vout_view; } + } + return nil; +} - if( NSApp == NULL ) - { - /* No MacOS X intf, unable to communicate with MT */ - msg_Err( p_vout, "no MacOS X interface present" ); - return NULL; - } +- (void)releaseEmbeddedVout: (id)o_vout_view +{ + if( [o_embedded_array containsObject: o_vout_view] ) + { + [o_vout_view setUsed: NO]; } - - if( [o_screens count] <= 0 ) + else { - msg_Err( p_vout, "no OSX screens available" ); - return NULL; + msg_Warn( VLCIntf, "cannot find Video Output"); } +} - /* p_real_vout: the vout we have to use to check for video-on-top - and a few other things. If we are the QuickTime output, it's us. - It we are the OpenGL provider, it is our parent. */ - if( p_vout->i_object_type == VLC_OBJECT_OPENGL ) +- (void)addEmbeddedVout: (id)o_vout_view +{ + if( ![o_embedded_array containsObject: o_vout_view] ) { - p_real_vout = (vout_thread_t *) p_vout->p_parent; + [o_embedded_array addObject: o_vout_view]; } - else +} + +- (BOOL)windowContainsEmbedded: (id)o_window +{ +/* if( ![[o_window className] isEqualToString: @"VLCVoutWindow"] ) + { + NSLog( @"We were not given a VLCVoutWindow" ); + }*/ + return ([self getViewForWindow: o_window] == nil ? NO : YES ); +} + +- (id)getViewForWindow: (id)o_window +{ + id o_enumerator = [o_embedded_array objectEnumerator]; + id o_current_embedded; + + while( (o_current_embedded = [o_enumerator nextObject]) ) { - p_real_vout = p_vout; + if( [o_current_embedded getWindow] == o_window ) + { + return o_current_embedded; + } } + return nil; +} - p_fullscreen_state = NULL; - i_time_mouse_last_moved = mdate(); +@end - var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); - var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); - var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); - var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT ); +/***************************************************************************** + * VLCVoutView implementation + *****************************************************************************/ +@implementation VLCVoutView + +- (id)initWithFrame: (NSRect)frameRect +{ + [super initWithFrame: frameRect]; + p_vout = NULL; + o_view = nil; + s_frame = &frameRect; + + p_real_vout = NULL; + o_window = nil; + return self; +} + +- (BOOL)setVout: (vout_thread_t *) vout + subView: (NSView *) view + frame: (NSRect *) frame +{ + int i_device; + NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init]; + NSArray *o_screens = [NSScreen screens]; + + p_vout = vout; + o_view = view; + s_frame = frame; + + if( [o_screens count] <= 0 ) + { + msg_Err( p_vout, "no OSX screens available" ); + return NO; + } + + p_real_vout = [VLCVoutView getRealVout: p_vout]; /* Get the pref value when this is the first time, otherwise retrieve the device from the top level video-device var */ - if( var_Type( p_real_vout->p_vlc, "video-device" ) == 0 ) + if( var_Type( p_real_vout->p_libvlc, "video-device" ) == 0 ) { i_device = var_GetInteger( p_vout, "macosx-vdev" ); } else { - i_device = var_GetInteger( p_real_vout->p_vlc, "video-device" ); + i_device = var_GetInteger( p_real_vout->p_libvlc, "video-device" ); } /* Setup the menuitem for the multiple displays. */ @@ -164,7 +194,7 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE ); - text.psz_string = _("Video Device"); + text.psz_string = _("Fullscreen Video Device"); var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL ); NSEnumerator * o_enumerator = [o_screens objectEnumerator]; @@ -185,10 +215,10 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, (int)s_rect.size.width, (int)s_rect.size.height ); text.psz_string = psz_temp; - val2.i_int = i; + val2.i_int = (int)[o_screen displayID]; var_Change( p_real_vout, "video-device", VLC_VAR_ADDCHOICE, &val2, &text ); - if( i == i_device ) + if( (int)[o_screen displayID] == i_device ) { var_Set( p_real_vout, "video-device", val2 ); } @@ -198,197 +228,177 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, var_AddCallback( p_real_vout, "video-device", DeviceCallback, NULL ); - val2.b_bool = VLC_TRUE; + val2.b_bool = true; var_Set( p_real_vout, "intf-change", val2 ); } - /* Find out on which screen to open the window */ - if( i_device <= 0 || i_device > (int)[o_screens count] ) - { - /* No preference specified. Use the main screen */ - o_screen = [NSScreen mainScreen]; - if( o_screen == [o_screens objectAtIndex: 0] ) - b_menubar_screen = VLC_TRUE; - } - else - { - i_device--; - o_screen = [o_screens objectAtIndex: i_device]; - b_menubar_screen = ( i_device == 0 ); - } + /* Add the view. It's automatically resized to fit the window */ + [self addSubview: o_view]; + [self setAutoresizesSubviews: YES]; + [o_pool release]; - if( p_vout->b_fullscreen ) - { - NSRect screen_rect = [o_screen frame]; - screen_rect.origin.x = screen_rect.origin.y = 0; + return YES; +} - /* Creates a window with size: screen_rect on o_screen */ - [self initWithContentRect: screen_rect - styleMask: NSBorderlessWindowMask - backing: NSBackingStoreBuffered - defer: YES screen: o_screen]; +- (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize +{ + [super resizeSubviewsWithOldSize: oldBoundsSize]; + [o_view setFrameSize: [self frame].size]; +} - if( b_menubar_screen ) - { - BeginFullScreen( &p_fullscreen_state, NULL, 0, 0, - NULL, NULL, fullScreenAllowEvents ); - } - } - else - { - unsigned int i_stylemask = NSTitledWindowMask | - NSMiniaturizableWindowMask | - NSClosableWindowMask | - NSResizableWindowMask; +- (void)drawRect:(NSRect)rect +{ + /* When there is no subview we draw a black background */ + [self lockFocus]; + [[NSColor blackColor] set]; + NSRectFill(rect); + [self unlockFocus]; +} - NSRect s_rect; - if( !s_frame ) - { - s_rect.size.width = p_vout->i_window_width; - s_rect.size.height = p_vout->i_window_height; - } - else - { - s_rect = *s_frame; - } - - [self initWithContentRect: s_rect - styleMask: i_stylemask - backing: NSBackingStoreBuffered - defer: YES screen: o_screen]; +- (void)closeVout +{ + [[[[VLCMain sharedInstance] getControls] getFSPanel] fadeOut]; + + /* Make sure we don't see a white flash */ + [[self window] disableScreenUpdatesUntilFlush]; + [o_view removeFromSuperview]; + o_view = nil; + p_vout = NULL; + s_frame = nil; + o_window = nil; + p_real_vout = NULL; +} - [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )]; +- (void)updateTitle +{ + NSString * o_title = nil; + NSMutableString * o_mrl = nil; + input_thread_t * p_input; + char * psz_title; - if( var_GetBool( p_real_vout, "video-on-top" ) ) - { - [self setLevel: NSStatusWindowLevel]; - } + if( !p_vout ) return; - if( !s_frame ) - { - [self center]; - } - } + p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT ); - [self updateTitle]; - [self makeKeyAndOrderFront: nil]; - [self setReleasedWhenClosed: YES]; + if( !p_input ) return; - /* We'll catch mouse events */ - [self setAcceptsMouseMovedEvents: YES]; - [self makeFirstResponder: self]; + input_item_t * p_item = input_GetItem( p_input ); - /* Add the view. It's automatically resized to fit the window */ - [self setContentView: o_view]; - - [o_pool release]; + psz_title = input_item_GetNowPlaying ( p_item ); + if( !psz_title ) + psz_title = input_item_GetName( p_item ); - b_init_ok = VLC_TRUE; - return self; -} + if( psz_title ) + o_title = [NSString stringWithUTF8String: psz_title]; -- (void) close -{ - /* XXX waitUntilDone = NO to avoid a possible deadlock when hitting - Command-Q */ - [self setAcceptsMouseMovedEvents: NO]; - [self setContentView: NULL]; - [self performSelectorOnMainThread: @selector(closeReal:) - withObject: NULL waitUntilDone: NO]; -} + char *psz_uri = input_item_GetURI( p_item ); + if( psz_uri ) + o_mrl = [NSMutableString stringWithUTF8String: psz_uri]; -- (id) closeReal: (id) sender -{ - [super close]; - if( p_fullscreen_state ) - { - EndFullScreen( p_fullscreen_state, 0 ); - } - return NULL; -} + free( psz_title ); + free( psz_uri ); -- (void)setOnTop:(BOOL)b_on_top -{ - if( b_on_top ) + if( !o_title ) + o_title = o_mrl; + + if( o_mrl != nil ) { - [self setLevel: NSStatusWindowLevel]; + /* FIXME once psz_access is exported, we could check if we are + * reading from a file in a smarter way. */ + + NSRange prefix_range = [o_mrl rangeOfString: @"file:"]; + if( prefix_range.location != NSNotFound ) + [o_mrl deleteCharactersInRange: prefix_range]; + + if( [o_mrl characterAtIndex:0] == '/' ) + { + /* it's a local file */ + [o_window setRepresentedFilename: o_mrl]; + } + else + { + /* it's from the network or somewhere else, + * we clear the previous path */ + [o_window setRepresentedFilename: @""]; + } + [o_window setTitle: o_title]; } else { - [self setLevel: NSNormalWindowLevel]; + [o_window setTitle: [NSString stringWithUTF8String: VOUT_TITLE]]; } + vlc_object_release( p_input ); } -- (void)hideMouse:(BOOL)b_hide + +- (void)setOnTop:(BOOL)b_on_top { - BOOL b_inside; - NSPoint ml; - NSView *o_contents = [self contentView]; - - ml = [self convertScreenToBase:[NSEvent mouseLocation]]; - ml = [o_contents convertPoint:ml fromView:nil]; - b_inside = [o_contents mouse: ml inRect: [o_contents bounds]]; - - if( b_hide && b_inside ) + if( b_on_top ) { - [NSCursor setHiddenUntilMouseMoves: YES]; + [o_window setLevel: NSStatusWindowLevel]; } - else if( !b_hide ) + else { - [NSCursor setHiddenUntilMouseMoves: NO]; + [o_window setLevel: NSNormalWindowLevel]; } } -- (void)manage +- (NSSize)voutSizeForFactor: (float)factor { - if( p_fullscreen_state ) + int i_corrected_height, i_corrected_width; + NSSize newsize; + + if( p_vout->render.i_height * p_vout->render.i_aspect > + p_vout->render.i_width * VOUT_ASPECT_FACTOR ) { - if( mdate() - i_time_mouse_last_moved > 3000000 ) - { - [self hideMouse: YES]; - } + i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect / + VOUT_ASPECT_FACTOR; + newsize.width = (int) ( i_corrected_width * factor ); + newsize.height = (int) ( p_vout->render.i_height * factor ); } else { - [self hideMouse: NO]; + i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR / + p_vout->render.i_aspect; + newsize.width = (int) ( p_vout->render.i_width * factor ); + newsize.height = (int) ( i_corrected_height * factor ); } - /* Disable screensaver */ - UpdateSystemActivity( UsrActivity ); + return newsize; } -- (void)scaleWindowWithFactor: (float)factor +- (void)scaleWindowWithFactor: (float)factor animate: (BOOL)animate { - NSSize newsize; - int i_corrected_height, i_corrected_width; - NSPoint topleftbase; - NSPoint topleftscreen; - if ( !p_vout->b_fullscreen ) { + NSSize newsize; + NSPoint topleftbase; + NSPoint topleftscreen; + NSView *mainView; + NSRect new_frame; topleftbase.x = 0; - topleftbase.y = [self frame].size.height; - topleftscreen = [self convertBaseToScreen: topleftbase]; - - if( p_vout->render.i_height * p_vout->render.i_aspect > - p_vout->render.i_width * VOUT_ASPECT_FACTOR ) - { - i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect / - VOUT_ASPECT_FACTOR; - newsize.width = (int) ( i_corrected_width * factor ); - newsize.height = (int) ( p_vout->render.i_height * factor ); - } + topleftbase.y = [o_window frame].size.height; + topleftscreen = [o_window convertBaseToScreen: topleftbase]; + + newsize = [self voutSizeForFactor:factor]; + + /* In fullscreen mode we need to use a view that is different from + * ourselves, with the VLCEmbeddedWindow */ + if([o_window isKindOfClass:[VLCEmbeddedWindow class]]) + mainView = [o_window mainView]; else - { - i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR / - p_vout->render.i_aspect; - newsize.width = (int) ( p_vout->render.i_width * factor ); - newsize.height = (int) ( i_corrected_height * factor ); - } - - [self setContentSize: newsize]; - - [self setFrameTopLeftPoint: topleftscreen]; + mainView = self; + + /* Calculate the window's new size */ + new_frame.size.width = [o_window frame].size.width - + [mainView frame].size.width + newsize.width; + new_frame.size.height = [o_window frame].size.height - + [mainView frame].size.height + newsize.height; + + new_frame.origin.x = topleftscreen.x; + new_frame.origin.y = topleftscreen.y - new_frame.size.height; + + [o_window setFrame:new_frame display:animate animate:animate]; p_vout->i_changes |= VOUT_SIZE_CHANGE; } } @@ -397,13 +407,14 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, { vlc_value_t val; + if( !p_real_vout ) return; if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool) { - val.b_bool = VLC_FALSE; + val.b_bool = false; } else { - val.b_bool = VLC_TRUE; + val.b_bool = true; } var_Set( p_real_vout, "video-on-top", val ); } @@ -411,6 +422,7 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, - (void)toggleFullscreen { vlc_value_t val; + if( !p_real_vout ) return; var_Get( p_real_vout, "fullscreen", &val ); val.b_bool = !val.b_bool; var_Set( p_real_vout, "fullscreen", val ); @@ -419,6 +431,7 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, - (BOOL)isFullscreen { vlc_value_t val; + if( !p_real_vout ) return NO; var_Get( p_real_vout, "fullscreen", &val ); return( val.b_bool ); } @@ -428,16 +441,26 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, vout_Control( p_real_vout, VOUT_SNAPSHOT ); } -- (BOOL)canBecomeKeyWindow +- (void)manage +{ + /* Disable Screensaver, when we're playing something, but allow it on pause */ + if( !VLCIntf || !VLCIntf->p_sys || !VLCIntf->p_sys->i_play_status ) + return; + + if( VLCIntf->p_sys->i_play_status == PLAYING_S ) + UpdateSystemActivity( UsrActivity ); +} + +- (id)getWindow { - return YES; + return o_window; } -/* Sometimes crashes VLC.... -- (BOOL)performKeyEquivalent:(NSEvent *)o_event +- (void)scrollWheel:(NSEvent *)theEvent { - return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event]; -}*/ + VLCControls * o_controls = (VLCControls *)[[NSApp delegate] getControls]; + [o_controls scrollWheel: theEvent]; +} - (void)keyDown:(NSEvent *)o_event { @@ -445,7 +468,7 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, vlc_value_t val; unsigned int i_pressed_modifiers = 0; val.i_int = 0; - + i_pressed_modifiers = [o_event modifierFlags]; if( i_pressed_modifiers & NSShiftKeyMask ) @@ -464,7 +487,7 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, /* Escape should always get you out of fullscreen */ if( key == (unichar) 0x1b ) { - if( [self isFullscreen] ) + if( p_real_vout && [self isFullscreen] ) { [self toggleFullscreen]; } @@ -473,12 +496,12 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, { vlc_value_t val; val.i_int = config_GetInt( p_vout, "key-play-pause" ); - var_Set( p_vout->p_vlc, "key-pressed", val ); + var_Set( p_vout->p_libvlc, "key-pressed", val ); } else { val.i_int |= CocoaKeyToVLC( key ); - var_Set( p_vout->p_vlc, "key-pressed", val ); + var_Set( p_vout->p_libvlc, "key-pressed", val ); } } else @@ -487,266 +510,636 @@ int DeviceCallback( vlc_object_t *p_this, const char *psz_variable, } } -- (void)updateTitle +- (void)mouseDown:(NSEvent *)o_event { - NSMutableString * o_title = NULL, * o_mrl = NULL; - input_thread_t * p_input; - - if( p_vout == NULL ) - { - return; - } - - p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, - FIND_PARENT ); - - if( p_input == NULL ) - { - return; - } - - if( p_input->input.p_item->psz_name != NULL ) - o_title = [NSMutableString stringWithUTF8String: - p_input->input.p_item->psz_name]; - if( p_input->input.p_item->psz_uri != NULL ) - o_mrl = [NSMutableString stringWithUTF8String: - p_input->input.p_item->psz_uri]; - if( o_title == nil ) - o_title = o_mrl; - - vlc_object_release( p_input ); - if( o_mrl != nil ) + vlc_value_t val; + if( p_vout ) { - if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) ) + if( ( [o_event type] == NSLeftMouseDown ) && + ( ! ( [o_event modifierFlags] & NSControlKeyMask ) ) ) { - NSRange prefix_range = [o_mrl rangeOfString: @"file:"]; - if( prefix_range.location != NSNotFound ) - [o_mrl deleteCharactersInRange: prefix_range]; - [self setRepresentedFilename: o_mrl]; + if( [o_event clickCount] <= 1 ) + { + /* single clicking */ + var_Get( p_vout, "mouse-button-down", &val ); + val.i_int |= 1; + var_Set( p_vout, "mouse-button-down", val ); + } + else + { + /* multiple clicking */ + [self toggleFullscreen]; + } + } + else if( ( [o_event type] == NSRightMouseDown ) || + ( ( [o_event type] == NSLeftMouseDown ) && + ( [o_event modifierFlags] & NSControlKeyMask ) ) ) + { + msg_Dbg( p_vout, "received NSRightMouseDown (generic method) or Ctrl clic" ); + [NSMenu popUpContextMenu: [[VLCMain sharedInstance] getVoutMenu] withEvent: o_event forView: [[[VLCMain sharedInstance] getControls] getVoutView]]; } - [self setTitle: o_title]; - } - else - { - [self setTitle: [NSString stringWithCString: VOUT_TITLE]]; } + + [super mouseDown: o_event]; } -/* This is actually the same as VLCControls::stop. */ -- (BOOL)windowShouldClose:(id)sender +- (void)otherMouseDown:(NSEvent *)o_event { - playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST, - FIND_ANYWHERE ); - if( p_playlist == NULL ) + vlc_value_t val; + + if( p_vout && [o_event type] == NSOtherMouseDown ) { - return NO; + var_Get( p_vout, "mouse-button-down", &val ); + val.i_int |= 2; + var_Set( p_vout, "mouse-button-down", val ); } - playlist_Stop( p_playlist ); - vlc_object_release( p_playlist ); - - /* The window will be closed by the intf later. */ - return NO; -} - -- (BOOL)acceptsFirstResponder -{ - return YES; + [super mouseDown: o_event]; } -- (BOOL)becomeFirstResponder +- (void)rightMouseDown:(NSEvent *)o_event { - return YES; -} + if( p_vout && [o_event type] == NSRightMouseDown ) + { + msg_Dbg( p_vout, "received NSRightMouseDown (specific method)" ); + [NSMenu popUpContextMenu: [[VLCMain sharedInstance] getVoutMenu] withEvent: o_event forView: [[[VLCMain sharedInstance] getControls] getVoutView]]; + } -- (BOOL)resignFirstResponder -{ - /* We need to stay the first responder or we'll miss some - events */ - return NO; + [super mouseDown: o_event]; } -- (void)mouseDown:(NSEvent *)o_event +- (void)mouseUp:(NSEvent *)o_event { vlc_value_t val; - switch( [o_event type] ) + if( p_vout && [o_event type] == NSLeftMouseUp ) { - case NSLeftMouseDown: - { - var_Get( p_vout, "mouse-button-down", &val ); - val.i_int |= 1; - var_Set( p_vout, "mouse-button-down", val ); - } - break; + vlc_value_t b_val; + b_val.b_bool = true; + var_Set( p_vout, "mouse-clicked", b_val ); - default: - [super mouseDown: o_event]; - break; + var_Get( p_vout, "mouse-button-down", &val ); + val.i_int &= ~1; + var_Set( p_vout, "mouse-button-down", val ); } + + [super mouseUp: o_event]; } -- (void)otherMouseDown:(NSEvent *)o_event +- (void)otherMouseUp:(NSEvent *)o_event { vlc_value_t val; - switch( [o_event type] ) + if( p_vout && [o_event type] == NSOtherMouseUp ) { - case NSOtherMouseDown: - { - var_Get( p_vout, "mouse-button-down", &val ); - val.i_int |= 2; - var_Set( p_vout, "mouse-button-down", val ); - } - break; + var_Get( p_vout, "mouse-button-down", &val ); + val.i_int &= ~2; + var_Set( p_vout, "mouse-button-down", val ); + } - default: - [super mouseDown: o_event]; - break; + [super mouseUp: o_event]; +} + +- (void)rightMouseUp:(NSEvent *)o_event +{ + if( p_vout && [o_event type] == NSRightMouseUp ) + { + /* FIXME: this isn't the appropriate place, but we can't receive + * NSRightMouseDown some how */ + msg_Dbg( p_vout, "received NSRightMouseUp" ); + [NSMenu popUpContextMenu: [[VLCMain sharedInstance] getVoutMenu] withEvent: o_event forView: [[[VLCMain sharedInstance] getControls] getVoutView]]; } + + [super mouseUp: o_event]; } -- (void)rightMouseDown:(NSEvent *)o_event +- (void)mouseDragged:(NSEvent *)o_event { - vlc_value_t val; + [self mouseMoved: o_event]; +} - switch( [o_event type] ) +- (void)otherMouseDragged:(NSEvent *)o_event +{ + [self mouseMoved: o_event]; +} + +- (void)rightMouseDragged:(NSEvent *)o_event +{ + [self mouseMoved: o_event]; +} + +- (void)mouseMoved:(NSEvent *)o_event +{ + NSPoint ml; + NSRect s_rect; + BOOL b_inside; + + if( p_vout ) { - case NSRightMouseDown: + s_rect = [o_view bounds]; + ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil]; + b_inside = [o_view mouse: ml inRect: s_rect]; + + if( b_inside ) { - var_Get( p_vout, "mouse-button-down", &val ); - val.i_int |= 4; - var_Set( p_vout, "mouse-button-down", val ); + vlc_value_t val; + unsigned int i_width, i_height, i_x, i_y; + + vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width, + (unsigned int)s_rect.size.height, + &i_x, &i_y, &i_width, &i_height ); + + val.i_int = ( ((int)ml.x) - i_x ) * + p_vout->render.i_width / i_width; + var_Set( p_vout, "mouse-x", val ); + + if( [[o_view className] isEqualToString: @"VLCGLView"] ) + { + val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) * + p_vout->render.i_height / i_height; + } + else + { + val.i_int = ( ((int)ml.y) - i_y ) * + p_vout->render.i_height / i_height; + } + var_Set( p_vout, "mouse-y", val ); + + val.b_bool = true; + var_Set( p_vout, "mouse-moved", val ); } - break; + if( [self isFullscreen] ) + [[[[VLCMain sharedInstance] getControls] getFSPanel] fadeIn]; + } + + [super mouseMoved: o_event]; +} - default: - [super mouseDown: o_event]; - break; +- (BOOL)acceptsFirstResponder +{ + return YES; +} + +- (BOOL)becomeFirstResponder +{ + return YES; +} + +- (BOOL)resignFirstResponder +{ + /* We need to stay the first responder or we'll miss some + events */ + return NO; +} + +/* Class methods used by the different vout modules */ + ++ (vout_thread_t *)getRealVout: (vout_thread_t *)p_vout +{ + /* p_real_vout: the vout we have to use to check for video-on-top + and a few other things. If we are the QuickTime output, it's us. + It we are the OpenGL provider, it is our parent. */ + if( p_vout->i_object_type == VLC_OBJECT_OPENGL ) + { + return (vout_thread_t *) p_vout->p_parent; } + else + { + return p_vout; + } + } -- (void)mouseUp:(NSEvent *)o_event ++ (id)getVoutView: (vout_thread_t *)p_vout subView: (NSView *)view + frame: (NSRect *)s_frame { - vlc_value_t val; + vlc_value_t value_drawable; + int i_timeout; + id o_return = nil; + + var_Get( p_vout->p_libvlc, "drawable", &value_drawable ); + + var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); + var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); + var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT ); + var_Create( p_vout, "macosx-background", VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); + var_Create( p_vout, "macosx-black", VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); + var_Create( p_vout, "embedded-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); - switch( [o_event type] ) + /* We only wait for NSApp to initialise if we're not embedded (as in the + * case of the Mozilla plugin). We can tell whether we're embedded or not + * by examining the "drawable" value: if it's zero, we're running in the + * main Mac intf; if it's non-zero, we're embedded. */ + if( value_drawable.i_int == 0 ) { - case NSLeftMouseUp: + /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */ + for( i_timeout = 20 ; i_timeout-- ; ) { - vlc_value_t b_val; - b_val.b_bool = VLC_TRUE; - var_Set( p_vout, "mouse-clicked", b_val ); + if( NSApp == NULL ) + { + msleep( INTF_IDLE_SLEEP ); + } + } - var_Get( p_vout, "mouse-button-down", &val ); - val.i_int &= ~1; - var_Set( p_vout, "mouse-button-down", val ); + if( NSApp == NULL ) + { + /* No MacOS X intf, unable to communicate with MT */ + msg_Err( p_vout, "no MacOS X interface present" ); + return nil; + } + else + { + if ( VLCIntf && !(p_vout->b_fullscreen) && + !(var_GetBool( p_vout, "macosx-background" )) && + var_GetBool( p_vout, "embedded-video") ) + { + o_return = [[[VLCMain sharedInstance] getEmbeddedList] + getEmbeddedVout]; + } } - break; + } - default: - [super mouseUp: o_event]; - break; + /* No embedded vout is available */ + if( o_return == nil ) + { + NSRect null_rect; + bzero( &null_rect, sizeof( NSRect ) ); + o_return = [[VLCDetachedVoutView alloc] initWithFrame: null_rect ]; } + [o_return setVout: p_vout subView: view frame: s_frame]; + return o_return; } -- (void)otherMouseUp:(NSEvent *)o_event +- (void)enterFullscreen { - vlc_value_t val; + /* Save the settings for next playing item */ + playlist_t * p_playlist = pl_Yield( p_real_vout ); + var_SetBool( p_playlist, "fullscreen", true ); + pl_Release( p_real_vout ); +} + +- (void)leaveFullscreen +{ + /* Save the settings for next playing item */ + playlist_t * p_playlist = pl_Yield( p_real_vout ); + var_SetBool( p_playlist, "fullscreen", false ); + pl_Release( p_real_vout ); +} + +@end + +/***************************************************************************** + * VLCDetachedVoutView implementation + *****************************************************************************/ +@implementation VLCDetachedVoutView - switch( [o_event type] ) +- (id)initWithFrame: (NSRect)frameRect +{ + [super initWithFrame: frameRect]; + i_time_mouse_last_moved = 0; + return self; +} + +- (BOOL)mouseDownCanMoveWindow +{ + return YES; +} + +- (bool)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view + frame: (NSRect *) s_arg_frame +{ + BOOL b_return = [super setVout: p_arg_vout subView: view frame:s_arg_frame]; + i_time_mouse_last_moved = mdate(); + o_window = [[VLCVoutWindow alloc] initWithVout: p_arg_vout view: self + frame: s_arg_frame]; + + [self updateTitle]; + if([self isFullscreen]) + [o_window performSelectorOnMainThread: @selector(enterFullscreen) withObject: NULL waitUntilDone: YES]; + else + [view setFrame: [self frame]]; + + return b_return; +} + +- (void)closeVout +{ + [o_window performSelectorOnMainThread: @selector(close) withObject: NULL waitUntilDone: YES]; + i_time_mouse_last_moved = 0; + [super closeVout]; +} + +- (void)mouseMoved:(NSEvent *)o_event +{ + i_time_mouse_last_moved = mdate(); + [super mouseMoved: o_event]; +} + +- (void)hideMouse:(BOOL)b_hide +{ + BOOL b_inside; + NSPoint ml; + NSView *o_contents = [o_window contentView]; + + ml = [o_window convertScreenToBase:[NSEvent mouseLocation]]; + ml = [o_contents convertPoint:ml fromView:nil]; + b_inside = [o_contents mouse: ml inRect: [o_contents bounds]]; + + if( b_hide && b_inside ) + { + [NSCursor setHiddenUntilMouseMoves: YES]; + } + else if( !b_hide ) { - case NSOtherMouseUp: + [NSCursor setHiddenUntilMouseMoves: NO]; + } +} + +- (void)manage +{ + [super manage]; + unsigned int i_mouse_hide_timeout = + var_GetInteger(p_vout, "mouse-hide-timeout") * 1000; + if( p_vout->b_fullscreen ) + { + if( mdate() - i_time_mouse_last_moved > i_mouse_hide_timeout ) { - var_Get( p_vout, "mouse-button-down", &val ); - val.i_int &= ~2; - var_Set( p_vout, "mouse-button-down", val ); + [self hideMouse: YES]; } - break; + } + else + { + [self hideMouse: NO]; + } +} + + +- (void)enterFullscreen +{ + [super enterFullscreen]; + [o_window performSelectorOnMainThread: @selector(enterFullscreen) withObject: NULL waitUntilDone: NO]; + +} + +- (void)leaveFullscreen +{ + [super leaveFullscreen]; + [o_window performSelectorOnMainThread: @selector(leaveFullscreen) withObject: NULL waitUntilDone: NO]; +} + +@end + +/***************************************************************************** + * VLCEmbeddedVoutView implementation + *****************************************************************************/ + +@implementation VLCEmbeddedVoutView + +- (void)awakeFromNib +{ + o_embeddedwindow = [self window]; +} - default: - [super mouseUp: o_event]; - break; +- (BOOL)mouseDownCanMoveWindow +{ + return YES; +} + +- (id)initWithFrame: (NSRect)frameRect +{ + if(self = [super initWithFrame: frameRect]) + { + b_used = NO; + [[[VLCMain sharedInstance] getEmbeddedList] addEmbeddedVout: self]; + o_embeddedwindow = nil; /* Filled later on in -awakeFromNib */ } + return self; } -- (void)rightMouseUp:(NSEvent *)o_event +- (BOOL)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view + frame: (NSRect *)s_arg_frame { - vlc_value_t val; + BOOL b_return; + + [NSObject cancelPreviousPerformRequestsWithTarget:o_window]; - switch( [o_event type] ) + b_return = [super setVout: p_arg_vout subView: view frame: s_arg_frame]; + if( b_return ) { - case NSRightMouseUp: + o_window = [self window]; + + [o_window setAcceptsMouseMovedEvents: TRUE]; + + if( var_GetBool( p_real_vout, "video-on-top" ) ) { - var_Get( p_vout, "mouse-button-down", &val ); - val.i_int &= ~4; - var_Set( p_vout, "mouse-button-down", val ); + [o_window setLevel: NSStatusWindowLevel]; } - break; - default: - [super mouseUp: o_event]; - break; + [view setFrameSize: [self frame].size]; + } + + /* o_window needs to point to our o_embeddedwindow, super might have set it + * to the fullscreen window that o_embeddedwindow setups during fullscreen */ + o_window = o_embeddedwindow; + + if( b_return ) + { + [o_window lockFullscreenAnimation]; + + [o_window setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )]; + + [self updateTitle]; + + [NSObject cancelPreviousPerformRequestsWithTarget:o_window]; + + /* Make the window the front and key window before animating */ + if ([o_window isVisible] && (![o_window isFullscreen])) + [o_window makeKeyAndOrderFront: self]; + + [self scaleWindowWithFactor: 1.0 animate: [o_window isVisible] && (![o_window isFullscreen])]; + + [o_embeddedwindow setVideoRatio:[self voutSizeForFactor:1.0]]; + + /* Make sure our window is visible, if we are not in fullscreen */ + if (![o_window isFullscreen]) + [o_window makeKeyAndOrderFront: self]; + [o_window unlockFullscreenAnimation]; + } + + return b_return; } -- (void)mouseDragged:(NSEvent *)o_event +- (void)setUsed: (BOOL)b_new_used { - [self mouseMoved: o_event]; + b_used = b_new_used; } -- (void)otherMouseDragged:(NSEvent *)o_event +- (BOOL)isUsed { - [self mouseMoved: o_event]; + return b_used; } -- (void)rightMouseDragged:(NSEvent *)o_event +- (void)closeVout { - [self mouseMoved: o_event]; + [super closeVout]; + + /* Don't close the window yet, wait a bit to see if a new input is poping up */ + /* FIXME: Probably fade the window In and Out */ + /* FIXME: fix core */ + [o_embeddedwindow performSelector:@selector(orderOut:) withObject:nil afterDelay:3.]; + + [o_window setAcceptsMouseMovedEvents: NO]; + [[[VLCMain sharedInstance] getEmbeddedList] releaseEmbeddedVout: self]; } -- (void)mouseMoved:(NSEvent *)o_event -{ - NSPoint ml; - NSRect s_rect; - BOOL b_inside; +- (void)enterFullscreen +{ + /* Save settings */ + [super enterFullscreen]; - i_time_mouse_last_moved = mdate(); + /* We are in a VLCEmbeddedWindow */ + [o_embeddedwindow performSelectorOnMainThread: @selector(enterFullscreen) withObject: NULL waitUntilDone: YES]; +} - s_rect = [o_view bounds]; - ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil]; - b_inside = [o_view mouse: ml inRect: s_rect]; +- (void)leaveFullscreen +{ + /* Save settings */ + [super leaveFullscreen]; - if( b_inside ) - { - vlc_value_t val; - unsigned int i_width, i_height, i_x, i_y; + /* We are in a VLCEmbeddedWindow */ + [o_embeddedwindow performSelectorOnMainThread: @selector(leaveFullscreen) withObject: NULL waitUntilDone: YES]; +} +@end - vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width, - (unsigned int)s_rect.size.height, - &i_x, &i_y, &i_width, &i_height ); +/***************************************************************************** + * VLCVoutWindow implementation + *****************************************************************************/ +@implementation VLCVoutWindow - val.i_int = ( ((int)ml.x) - i_x ) * - p_vout->render.i_width / i_width; - var_Set( p_vout, "mouse-x", val ); +- (id) initWithVout: (vout_thread_t *) vout view: (VLCVoutView *) view + frame: (NSRect *) frame +{ + p_vout = vout; + o_view = view; + s_frame = frame; + b_init_ok = NO; + [self performSelectorOnMainThread: @selector(initMainThread:) + withObject: NULL waitUntilDone: YES]; + + return b_init_ok ? self : nil; +} + +- (id)initMainThread: (id) sender +{ + NSRect rect; + rect.size.height = p_vout->i_window_height; + rect.size.width = p_vout->i_window_width; + rect.origin.x = rect.origin.y = 70.; - if( [[o_view className] isEqualToString: @"VLCGLView"] ) + if( self = [super initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]) + { + [self setBackgroundColor:[NSColor blackColor]]; + [self setHasShadow:YES]; + [self setMovableByWindowBackground: YES]; + [self center]; + [self makeKeyAndOrderFront: self]; + [self setReleasedWhenClosed: YES]; + + /* We'll catch mouse events */ + [self makeFirstResponder: o_view]; + [self setCanBecomeKeyWindow: YES]; + + if( var_GetBool( p_vout, "macosx-background" ) ) { - val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) * - p_vout->render.i_height / i_height; + int i_device = var_GetInteger( p_vout->p_libvlc, "video-device" ); + + /* Find out on which screen to open the window */ + NSScreen * screen = [NSScreen screenWithDisplayID: (CGDirectDisplayID)i_device]; + if( !screen ) screen = [NSScreen mainScreen]; + + NSRect screen_rect = [screen frame]; + screen_rect.origin.x = screen_rect.origin.y = 0; + + /* Creates a window with size: screen_rect on o_screen */ + [self setFrame: screen_rect display: NO]; + + [self setLevel: CGWindowLevelForKey(kCGDesktopWindowLevelKey)]; + [self setMovableByWindowBackground: NO]; } - else + if( var_GetBool( p_vout, "video-on-top" ) ) { - val.i_int = ( ((int)ml.y) - i_y ) * - p_vout->render.i_height / i_height; + [self setLevel: NSStatusWindowLevel]; } - var_Set( p_vout, "mouse-y", val ); - - val.b_bool = VLC_TRUE; - var_Set( p_vout, "mouse-moved", val ); + + [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )]; + + /* Add the view. It's automatically resized to fit the window */ + [self setContentView: o_view]; + + b_init_ok = YES; } + return self; +} - [super mouseMoved: o_event]; +- (void)enterFullscreen +{ + if( fullscreen ) return; + + NSScreen *screen; + int i_device; + BOOL b_black = NO; + + i_device = var_GetInteger( p_vout->p_libvlc, "video-device" ); + b_black = var_GetBool( p_vout, "macosx-black" ); + + /* Find out on which screen to open the window */ + screen = [NSScreen screenWithDisplayID: (CGDirectDisplayID)i_device]; + if( !screen ) screen = [self screen]; + + if( b_black && [[NSScreen screens] count] > 1) + { + CGDisplayFadeReservationToken token; + CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token); + CGDisplayFade( token, 0.2 , kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES ); + + [screen blackoutOtherScreens]; + + CGDisplayFade( token, 0.1 , kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO ); + CGReleaseDisplayFadeReservation( token); + } + + [self setMovableByWindowBackground: NO]; + + /* tell the fspanel to move itself to front next time it's triggered */ + [[[[VLCMain sharedInstance] getControls] getFSPanel] setVoutWasUpdated: i_device]; + [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil]; + + if( [screen isMainScreen] ) + SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar); + + initialFrame = [self frame]; + [self setFrame:[screen frame] display:YES animate:YES]; + fullscreen = YES; +} + +- (void)leaveFullscreen +{ + if( !fullscreen ) return; + fullscreen = NO; + + [NSScreen unblackoutScreens]; + + [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil]; + SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar); + [self setFrame:initialFrame display:YES animate:YES]; + [self setMovableByWindowBackground: YES]; +} + +- (id)getVoutView // FIXME Naming scheme! +{ + return o_view; } @end +