1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 VideoLAN
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>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <string.h> /* strerror() */
35 /* BeginFullScreen, EndFullScreen */
36 #include <QuickTime/QuickTime.h>
44 /*****************************************************************************
45 * DeviceCallback: Callback triggered when the video-device variable is changed
46 *****************************************************************************/
47 int DeviceCallback( vlc_object_t *p_this, const char *psz_variable,
48 vlc_value_t old_val, vlc_value_t new_val, void *param )
51 vout_thread_t *p_vout = (vout_thread_t *)p_this;
53 msg_Dbg( p_vout, "set %d", new_val.i_int );
54 var_Create( p_vout->p_vlc, "video-device", VLC_VAR_INTEGER );
55 var_Set( p_vout->p_vlc, "video-device", new_val );
57 val.b_bool = VLC_TRUE;
58 var_Set( p_vout, "intf-change", val );
63 /*****************************************************************************
64 * VLCWindow implementation
65 *****************************************************************************/
66 @implementation VLCWindow
68 - (id)initWithVout:(vout_thread_t *)_p_vout frame:(NSRect *)s_frame
70 NSAutoreleasePool *o_pool;
71 NSArray *o_screens = [NSScreen screens];
73 vlc_bool_t b_menubar_screen = VLC_FALSE;
74 int i_timeout, i_device;
75 vlc_value_t value_drawable;
79 var_Get( p_vout->p_vlc, "drawable", &value_drawable );
81 /* We only wait for NSApp to initialise if we're not embedded (as in the
82 * case of the Mozilla plugin). We can tell whether we're embedded or not
83 * by examining the "drawable" value: if it's zero, we're running in the
84 * main Mac intf; if it's non-zero, we're embedded. */
85 if( value_drawable.i_int == 0 )
87 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
88 for( i_timeout = 20 ; i_timeout-- ; )
92 msleep( INTF_IDLE_SLEEP );
98 /* No MacOS X intf, unable to communicate with MT */
99 msg_Err( p_vout, "no MacOS X interface present" );
104 if( [o_screens count] <= 0 )
106 msg_Err( p_vout, "no OSX screens available" );
110 /* p_real_vout: the vout we have to use to check for video-on-top
111 and a few other things. If we are the QuickTime output, it's us.
112 It we are the OpenGL provider, it is our parent. */
113 if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
115 p_real_vout = (vout_thread_t *) p_vout->p_parent;
119 p_real_vout = p_vout;
122 p_fullscreen_state = NULL;
123 i_time_mouse_last_moved = mdate();
125 var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
126 var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
127 var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
128 var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
130 /* Get the pref value when this is the first time, otherwise retrieve the device from the top level video-device var */
131 if( var_Type( p_real_vout->p_vlc, "video-device" ) == 0 )
133 i_device = var_GetInteger( p_vout, "macosx-vdev" );
137 i_device = var_GetInteger( p_real_vout->p_vlc, "video-device" );
140 /* Setup the menuitem for the multiple displays. */
141 if( var_Type( p_real_vout, "video-device" ) == 0 )
144 vlc_value_t val2, text;
147 var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER |
149 text.psz_string = _("Video Device");
150 var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
152 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
155 text.psz_string = _("Default");
156 var_Change( p_real_vout, "video-device",
157 VLC_VAR_ADDCHOICE, &val2, &text );
158 var_Set( p_real_vout, "video-device", val2 );
160 while( (o_screen = [o_enumerator nextObject]) != NULL )
163 NSRect s_rect = [o_screen frame];
165 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
166 "%s %d (%dx%d)", _("Screen"), i,
167 (int)s_rect.size.width, (int)s_rect.size.height );
169 text.psz_string = psz_temp;
171 var_Change( p_real_vout, "video-device",
172 VLC_VAR_ADDCHOICE, &val2, &text );
175 var_Set( p_real_vout, "video-device", val2 );
180 var_AddCallback( p_real_vout, "video-device", DeviceCallback,
183 val2.b_bool = VLC_TRUE;
184 var_Set( p_real_vout, "intf-change", val2 );
187 /* Find out on which screen to open the window */
188 if( i_device <= 0 || i_device > (int)[o_screens count] )
190 /* No preference specified. Use the main screen */
191 o_screen = [NSScreen mainScreen];
192 if( o_screen == [o_screens objectAtIndex: 0] )
193 b_menubar_screen = VLC_TRUE;
198 o_screen = [o_screens objectAtIndex: i_device];
199 b_menubar_screen = ( i_device == 0 );
202 o_pool = [[NSAutoreleasePool alloc] init];
204 if( p_vout->b_fullscreen )
206 NSRect screen_rect = [o_screen frame];
207 screen_rect.origin.x = screen_rect.origin.y = 0;
209 /* Creates a window with size: screen_rect on o_screen */
210 [self initWithContentRect: screen_rect
211 styleMask: NSBorderlessWindowMask
212 backing: NSBackingStoreBuffered
213 defer: YES screen: o_screen];
215 if( b_menubar_screen )
217 BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
218 NULL, NULL, fullScreenAllowEvents );
223 unsigned int i_stylemask = NSTitledWindowMask |
224 NSMiniaturizableWindowMask |
225 NSClosableWindowMask |
226 NSResizableWindowMask;
231 s_rect.size.width = p_vout->i_window_width;
232 s_rect.size.height = p_vout->i_window_height;
239 [self initWithContentRect: s_rect
240 styleMask: i_stylemask
241 backing: NSBackingStoreBuffered
242 defer: YES screen: o_screen];
244 [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
246 if( var_GetBool( p_real_vout, "video-on-top" ) )
248 [self setLevel: NSStatusWindowLevel];
258 [self makeKeyAndOrderFront: nil];
259 [self setReleasedWhenClosed: YES];
261 /* We'll catch mouse events */
262 [self setAcceptsMouseMovedEvents: YES];
263 [self makeFirstResponder: self];
271 if( p_fullscreen_state )
273 EndFullScreen( p_fullscreen_state, 0 );
278 - (void)setOnTop:(BOOL)b_on_top
282 [self setLevel: NSStatusWindowLevel];
286 [self setLevel: NSNormalWindowLevel];
290 - (void)hideMouse:(BOOL)b_hide
294 NSView *o_contents = [self contentView];
296 ml = [self convertScreenToBase:[NSEvent mouseLocation]];
297 ml = [o_contents convertPoint:ml fromView:nil];
298 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
300 if( b_hide && b_inside )
302 [NSCursor setHiddenUntilMouseMoves: YES];
306 [NSCursor setHiddenUntilMouseMoves: NO];
312 if( p_fullscreen_state )
314 if( mdate() - i_time_mouse_last_moved > 3000000 )
316 [self hideMouse: YES];
321 [self hideMouse: NO];
324 /* Disable screensaver */
325 UpdateSystemActivity( UsrActivity );
328 - (void)scaleWindowWithFactor: (float)factor
331 int i_corrected_height, i_corrected_width;
333 NSPoint topleftscreen;
335 if ( !p_vout->b_fullscreen )
338 topleftbase.y = [self frame].size.height;
339 topleftscreen = [self convertBaseToScreen: topleftbase];
341 if( p_vout->render.i_height * p_vout->render.i_aspect >
342 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
344 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
346 newsize.width = (int) ( i_corrected_width * factor );
347 newsize.height = (int) ( p_vout->render.i_height * factor );
351 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
352 p_vout->render.i_aspect;
353 newsize.width = (int) ( p_vout->render.i_width * factor );
354 newsize.height = (int) ( i_corrected_height * factor );
357 [self setContentSize: newsize];
359 [self setFrameTopLeftPoint: topleftscreen];
360 p_vout->i_changes |= VOUT_SIZE_CHANGE;
364 - (void)toggleFloatOnTop
368 if( var_Get( p_vout, "video-on-top", &val )>=0 && val.b_bool)
370 val.b_bool = VLC_FALSE;
374 val.b_bool = VLC_TRUE;
376 var_Set( p_vout, "video-on-top", val );
379 - (void)toggleFullscreen
382 val.b_bool = !p_real_vout->b_fullscreen;
383 var_Set( p_real_vout, "fullscreen", val );
388 return( p_vout->b_fullscreen );
393 vout_Control( p_vout, VOUT_SNAPSHOT );
396 - (BOOL)canBecomeKeyWindow
401 /* Sometimes crashes VLC....
402 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
404 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
407 - (void)keyDown:(NSEvent *)o_event
411 unsigned int i_pressed_modifiers = 0;
414 i_pressed_modifiers = [o_event modifierFlags];
416 if( i_pressed_modifiers & NSShiftKeyMask )
417 val.i_int |= KEY_MODIFIER_SHIFT;
418 if( i_pressed_modifiers & NSControlKeyMask )
419 val.i_int |= KEY_MODIFIER_CTRL;
420 if( i_pressed_modifiers & NSAlternateKeyMask )
421 val.i_int |= KEY_MODIFIER_ALT;
422 if( i_pressed_modifiers & NSCommandKeyMask )
423 val.i_int |= KEY_MODIFIER_COMMAND;
425 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
429 /* Escape should always get you out of fullscreen */
430 if( key == (unichar) 0x1b )
432 if( [self isFullscreen] )
434 [self toggleFullscreen];
437 else if ( key == ' ' )
440 val.i_int = config_GetInt( p_vout, "key-play-pause" );
441 var_Set( p_vout->p_vlc, "key-pressed", val );
445 val.i_int |= CocoaKeyToVLC( key );
446 var_Set( p_vout->p_vlc, "key-pressed", val );
451 [super keyDown: o_event];
457 NSMutableString * o_title,* o_mrl;
458 input_thread_t * p_input;
465 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
468 if( p_input == NULL )
473 if( p_input->input.p_item->psz_name != NULL )
474 o_title = [NSMutableString stringWithUTF8String:
475 p_input->input.p_item->psz_name];
476 if( p_input->input.p_item->psz_uri != NULL )
477 o_mrl = [NSMutableString stringWithUTF8String:
478 p_input->input.p_item->psz_uri];
482 vlc_object_release( p_input );
485 if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
487 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
488 if( prefix_range.location != NSNotFound )
489 [o_mrl deleteCharactersInRange: prefix_range];
490 [self setRepresentedFilename: o_mrl];
492 [self setTitle: o_title];
496 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
500 /* This is actually the same as VLCControls::stop. */
501 - (BOOL)windowShouldClose:(id)sender
503 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
505 if( p_playlist == NULL )
510 playlist_Stop( p_playlist );
511 vlc_object_release( p_playlist );
513 /* The window will be closed by the intf later. */
517 - (BOOL)acceptsFirstResponder
522 - (BOOL)becomeFirstResponder
527 - (BOOL)resignFirstResponder
529 /* We need to stay the first responder or we'll miss some
534 - (void)mouseDown:(NSEvent *)o_event
538 switch( [o_event type] )
540 case NSLeftMouseDown:
542 var_Get( p_vout, "mouse-button-down", &val );
544 var_Set( p_vout, "mouse-button-down", val );
549 [super mouseDown: o_event];
554 - (void)otherMouseDown:(NSEvent *)o_event
558 switch( [o_event type] )
560 case NSOtherMouseDown:
562 var_Get( p_vout, "mouse-button-down", &val );
564 var_Set( p_vout, "mouse-button-down", val );
569 [super mouseDown: o_event];
574 - (void)rightMouseDown:(NSEvent *)o_event
578 switch( [o_event type] )
580 case NSRightMouseDown:
582 var_Get( p_vout, "mouse-button-down", &val );
584 var_Set( p_vout, "mouse-button-down", val );
589 [super mouseDown: o_event];
594 - (void)mouseUp:(NSEvent *)o_event
598 switch( [o_event type] )
603 b_val.b_bool = VLC_TRUE;
604 var_Set( p_vout, "mouse-clicked", b_val );
606 var_Get( p_vout, "mouse-button-down", &val );
608 var_Set( p_vout, "mouse-button-down", val );
613 [super mouseUp: o_event];
618 - (void)otherMouseUp:(NSEvent *)o_event
622 switch( [o_event type] )
626 var_Get( p_vout, "mouse-button-down", &val );
628 var_Set( p_vout, "mouse-button-down", val );
633 [super mouseUp: o_event];
638 - (void)rightMouseUp:(NSEvent *)o_event
642 switch( [o_event type] )
646 var_Get( p_vout, "mouse-button-down", &val );
648 var_Set( p_vout, "mouse-button-down", val );
653 [super mouseUp: o_event];
658 - (void)mouseDragged:(NSEvent *)o_event
660 [self mouseMoved: o_event];
663 - (void)otherMouseDragged:(NSEvent *)o_event
665 [self mouseMoved: o_event];
668 - (void)rightMouseDragged:(NSEvent *)o_event
670 [self mouseMoved: o_event];
673 - (void)mouseMoved:(NSEvent *)o_event
680 i_time_mouse_last_moved = mdate();
682 o_view = [self contentView];
683 s_rect = [o_view bounds];
684 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
685 b_inside = [o_view mouse: ml inRect: s_rect];
690 int i_width, i_height, i_x, i_y;
692 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
693 (unsigned int)s_rect.size.height,
694 &i_x, &i_y, &i_width, &i_height );
696 val.i_int = ( ((int)ml.x) - i_x ) *
697 p_vout->render.i_width / i_width;
698 var_Set( p_vout, "mouse-x", val );
700 if( [[o_view className] isEqualToString: @"VLCGLView"] )
702 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
703 p_vout->render.i_height / i_height;
707 val.i_int = ( ((int)ml.y) - i_y ) *
708 p_vout->render.i_height / i_height;
710 var_Set( p_vout, "mouse-y", val );
712 val.b_bool = VLC_TRUE;
713 var_Set( p_vout, "mouse-moved", val );
716 [super mouseMoved: o_event];