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 = [[NSAutoreleasePool alloc] init];
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 if( p_vout->b_fullscreen )
204 NSRect screen_rect = [o_screen frame];
205 screen_rect.origin.x = screen_rect.origin.y = 0;
207 /* Creates a window with size: screen_rect on o_screen */
208 [self initWithContentRect: screen_rect
209 styleMask: NSBorderlessWindowMask
210 backing: NSBackingStoreBuffered
211 defer: YES screen: o_screen];
213 if( b_menubar_screen )
215 BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
216 NULL, NULL, fullScreenAllowEvents );
221 unsigned int i_stylemask = NSTitledWindowMask |
222 NSMiniaturizableWindowMask |
223 NSClosableWindowMask |
224 NSResizableWindowMask;
229 s_rect.size.width = p_vout->i_window_width;
230 s_rect.size.height = p_vout->i_window_height;
237 [self initWithContentRect: s_rect
238 styleMask: i_stylemask
239 backing: NSBackingStoreBuffered
240 defer: YES screen: o_screen];
242 [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
244 if( var_GetBool( p_real_vout, "video-on-top" ) )
246 [self setLevel: NSStatusWindowLevel];
256 [self makeKeyAndOrderFront: nil];
257 [self setReleasedWhenClosed: YES];
259 /* We'll catch mouse events */
260 [self setAcceptsMouseMovedEvents: YES];
261 [self makeFirstResponder: self];
270 if( p_fullscreen_state )
272 EndFullScreen( p_fullscreen_state, 0 );
276 - (void)setOnTop:(BOOL)b_on_top
280 [self setLevel: NSStatusWindowLevel];
284 [self setLevel: NSNormalWindowLevel];
288 - (void)hideMouse:(BOOL)b_hide
292 NSView *o_contents = [self contentView];
294 ml = [self convertScreenToBase:[NSEvent mouseLocation]];
295 ml = [o_contents convertPoint:ml fromView:nil];
296 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
298 if( b_hide && b_inside )
300 [NSCursor setHiddenUntilMouseMoves: YES];
304 [NSCursor setHiddenUntilMouseMoves: NO];
310 if( p_fullscreen_state )
312 if( mdate() - i_time_mouse_last_moved > 3000000 )
314 [self hideMouse: YES];
319 [self hideMouse: NO];
322 /* Disable screensaver */
323 UpdateSystemActivity( UsrActivity );
326 - (void)scaleWindowWithFactor: (float)factor
329 int i_corrected_height, i_corrected_width;
331 NSPoint topleftscreen;
333 if ( !p_vout->b_fullscreen )
336 topleftbase.y = [self frame].size.height;
337 topleftscreen = [self convertBaseToScreen: topleftbase];
339 if( p_vout->render.i_height * p_vout->render.i_aspect >
340 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
342 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
344 newsize.width = (int) ( i_corrected_width * factor );
345 newsize.height = (int) ( p_vout->render.i_height * factor );
349 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
350 p_vout->render.i_aspect;
351 newsize.width = (int) ( p_vout->render.i_width * factor );
352 newsize.height = (int) ( i_corrected_height * factor );
355 [self setContentSize: newsize];
357 [self setFrameTopLeftPoint: topleftscreen];
358 p_vout->i_changes |= VOUT_SIZE_CHANGE;
362 - (void)toggleFloatOnTop
366 if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
368 val.b_bool = VLC_FALSE;
372 val.b_bool = VLC_TRUE;
374 var_Set( p_real_vout, "video-on-top", val );
377 - (void)toggleFullscreen
380 var_Get( p_real_vout, "fullscreen", &val );
381 val.b_bool = !val.b_bool;
382 var_Set( p_real_vout, "fullscreen", val );
388 var_Get( p_real_vout, "fullscreen", &val );
389 return( val.b_bool );
394 vout_Control( p_real_vout, VOUT_SNAPSHOT );
397 - (BOOL)canBecomeKeyWindow
402 /* Sometimes crashes VLC....
403 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
405 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
408 - (void)keyDown:(NSEvent *)o_event
412 unsigned int i_pressed_modifiers = 0;
415 i_pressed_modifiers = [o_event modifierFlags];
417 if( i_pressed_modifiers & NSShiftKeyMask )
418 val.i_int |= KEY_MODIFIER_SHIFT;
419 if( i_pressed_modifiers & NSControlKeyMask )
420 val.i_int |= KEY_MODIFIER_CTRL;
421 if( i_pressed_modifiers & NSAlternateKeyMask )
422 val.i_int |= KEY_MODIFIER_ALT;
423 if( i_pressed_modifiers & NSCommandKeyMask )
424 val.i_int |= KEY_MODIFIER_COMMAND;
426 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
430 /* Escape should always get you out of fullscreen */
431 if( key == (unichar) 0x1b )
433 if( [self isFullscreen] )
435 [self toggleFullscreen];
438 else if ( key == ' ' )
441 val.i_int = config_GetInt( p_vout, "key-play-pause" );
442 var_Set( p_vout->p_vlc, "key-pressed", val );
446 val.i_int |= CocoaKeyToVLC( key );
447 var_Set( p_vout->p_vlc, "key-pressed", val );
452 [super keyDown: o_event];
458 NSMutableString * o_title,* o_mrl;
459 input_thread_t * p_input;
466 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
469 if( p_input == NULL )
474 if( p_input->input.p_item->psz_name != NULL )
475 o_title = [NSMutableString stringWithUTF8String:
476 p_input->input.p_item->psz_name];
477 if( p_input->input.p_item->psz_uri != NULL )
478 o_mrl = [NSMutableString stringWithUTF8String:
479 p_input->input.p_item->psz_uri];
483 vlc_object_release( p_input );
486 if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
488 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
489 if( prefix_range.location != NSNotFound )
490 [o_mrl deleteCharactersInRange: prefix_range];
491 [self setRepresentedFilename: o_mrl];
493 [self setTitle: o_title];
497 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
501 /* This is actually the same as VLCControls::stop. */
502 - (BOOL)windowShouldClose:(id)sender
504 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
506 if( p_playlist == NULL )
511 playlist_Stop( p_playlist );
512 vlc_object_release( p_playlist );
514 /* The window will be closed by the intf later. */
518 - (BOOL)acceptsFirstResponder
523 - (BOOL)becomeFirstResponder
528 - (BOOL)resignFirstResponder
530 /* We need to stay the first responder or we'll miss some
535 - (void)mouseDown:(NSEvent *)o_event
539 switch( [o_event type] )
541 case NSLeftMouseDown:
543 var_Get( p_vout, "mouse-button-down", &val );
545 var_Set( p_vout, "mouse-button-down", val );
550 [super mouseDown: o_event];
555 - (void)otherMouseDown:(NSEvent *)o_event
559 switch( [o_event type] )
561 case NSOtherMouseDown:
563 var_Get( p_vout, "mouse-button-down", &val );
565 var_Set( p_vout, "mouse-button-down", val );
570 [super mouseDown: o_event];
575 - (void)rightMouseDown:(NSEvent *)o_event
579 switch( [o_event type] )
581 case NSRightMouseDown:
583 var_Get( p_vout, "mouse-button-down", &val );
585 var_Set( p_vout, "mouse-button-down", val );
590 [super mouseDown: o_event];
595 - (void)mouseUp:(NSEvent *)o_event
599 switch( [o_event type] )
604 b_val.b_bool = VLC_TRUE;
605 var_Set( p_vout, "mouse-clicked", b_val );
607 var_Get( p_vout, "mouse-button-down", &val );
609 var_Set( p_vout, "mouse-button-down", val );
614 [super mouseUp: o_event];
619 - (void)otherMouseUp:(NSEvent *)o_event
623 switch( [o_event type] )
627 var_Get( p_vout, "mouse-button-down", &val );
629 var_Set( p_vout, "mouse-button-down", val );
634 [super mouseUp: o_event];
639 - (void)rightMouseUp:(NSEvent *)o_event
643 switch( [o_event type] )
647 var_Get( p_vout, "mouse-button-down", &val );
649 var_Set( p_vout, "mouse-button-down", val );
654 [super mouseUp: o_event];
659 - (void)mouseDragged:(NSEvent *)o_event
661 [self mouseMoved: o_event];
664 - (void)otherMouseDragged:(NSEvent *)o_event
666 [self mouseMoved: o_event];
669 - (void)rightMouseDragged:(NSEvent *)o_event
671 [self mouseMoved: o_event];
674 - (void)mouseMoved:(NSEvent *)o_event
681 i_time_mouse_last_moved = mdate();
683 o_view = [self contentView];
684 s_rect = [o_view bounds];
685 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
686 b_inside = [o_view mouse: ml inRect: s_rect];
691 unsigned int i_width, i_height, i_x, i_y;
693 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
694 (unsigned int)s_rect.size.height,
695 &i_x, &i_y, &i_width, &i_height );
697 val.i_int = ( ((int)ml.x) - i_x ) *
698 p_vout->render.i_width / i_width;
699 var_Set( p_vout, "mouse-x", val );
701 if( [[o_view className] isEqualToString: @"VLCGLView"] )
703 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
704 p_vout->render.i_height / i_height;
708 val.i_int = ( ((int)ml.y) - i_y ) *
709 p_vout->render.i_height / i_height;
711 var_Set( p_vout, "mouse-y", val );
713 val.b_bool = VLC_TRUE;
714 var_Set( p_vout, "mouse-moved", val );
717 [super mouseMoved: o_event];