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 *) vout view: (NSView *) view
69 frame: (NSRect *) frame
75 [self performSelectorOnMainThread: @selector(initReal:)
76 withObject: NULL waitUntilDone: YES];
86 - (id) initReal: (id) sender
88 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
89 NSArray *o_screens = [NSScreen screens];
91 vlc_bool_t b_menubar_screen = VLC_FALSE;
92 int i_timeout, i_device;
93 vlc_value_t value_drawable;
95 b_init_ok = VLC_FALSE;
97 var_Get( p_vout->p_vlc, "drawable", &value_drawable );
99 /* We only wait for NSApp to initialise if we're not embedded (as in the
100 * case of the Mozilla plugin). We can tell whether we're embedded or not
101 * by examining the "drawable" value: if it's zero, we're running in the
102 * main Mac intf; if it's non-zero, we're embedded. */
103 if( value_drawable.i_int == 0 )
105 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
106 for( i_timeout = 20 ; i_timeout-- ; )
110 msleep( INTF_IDLE_SLEEP );
116 /* No MacOS X intf, unable to communicate with MT */
117 msg_Err( p_vout, "no MacOS X interface present" );
122 if( [o_screens count] <= 0 )
124 msg_Err( p_vout, "no OSX screens available" );
128 /* p_real_vout: the vout we have to use to check for video-on-top
129 and a few other things. If we are the QuickTime output, it's us.
130 It we are the OpenGL provider, it is our parent. */
131 if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
133 p_real_vout = (vout_thread_t *) p_vout->p_parent;
137 p_real_vout = p_vout;
140 p_fullscreen_state = NULL;
141 i_time_mouse_last_moved = mdate();
143 var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
144 var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
145 var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
146 var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
148 /* Get the pref value when this is the first time, otherwise retrieve the device from the top level video-device var */
149 if( var_Type( p_real_vout->p_vlc, "video-device" ) == 0 )
151 i_device = var_GetInteger( p_vout, "macosx-vdev" );
155 i_device = var_GetInteger( p_real_vout->p_vlc, "video-device" );
158 /* Setup the menuitem for the multiple displays. */
159 if( var_Type( p_real_vout, "video-device" ) == 0 )
162 vlc_value_t val2, text;
165 var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER |
167 text.psz_string = _("Video Device");
168 var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
170 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
173 text.psz_string = _("Default");
174 var_Change( p_real_vout, "video-device",
175 VLC_VAR_ADDCHOICE, &val2, &text );
176 var_Set( p_real_vout, "video-device", val2 );
178 while( (o_screen = [o_enumerator nextObject]) != NULL )
181 NSRect s_rect = [o_screen frame];
183 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
184 "%s %d (%dx%d)", _("Screen"), i,
185 (int)s_rect.size.width, (int)s_rect.size.height );
187 text.psz_string = psz_temp;
189 var_Change( p_real_vout, "video-device",
190 VLC_VAR_ADDCHOICE, &val2, &text );
193 var_Set( p_real_vout, "video-device", val2 );
198 var_AddCallback( p_real_vout, "video-device", DeviceCallback,
201 val2.b_bool = VLC_TRUE;
202 var_Set( p_real_vout, "intf-change", val2 );
205 /* Find out on which screen to open the window */
206 if( i_device <= 0 || i_device > (int)[o_screens count] )
208 /* No preference specified. Use the main screen */
209 o_screen = [NSScreen mainScreen];
210 if( o_screen == [o_screens objectAtIndex: 0] )
211 b_menubar_screen = VLC_TRUE;
216 o_screen = [o_screens objectAtIndex: i_device];
217 b_menubar_screen = ( i_device == 0 );
220 if( p_vout->b_fullscreen )
222 NSRect screen_rect = [o_screen frame];
223 screen_rect.origin.x = screen_rect.origin.y = 0;
225 /* Creates a window with size: screen_rect on o_screen */
226 [self initWithContentRect: screen_rect
227 styleMask: NSBorderlessWindowMask
228 backing: NSBackingStoreBuffered
229 defer: YES screen: o_screen];
231 if( b_menubar_screen )
233 BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
234 NULL, NULL, fullScreenAllowEvents );
239 unsigned int i_stylemask = NSTitledWindowMask |
240 NSMiniaturizableWindowMask |
241 NSClosableWindowMask |
242 NSResizableWindowMask;
247 s_rect.size.width = p_vout->i_window_width;
248 s_rect.size.height = p_vout->i_window_height;
255 [self initWithContentRect: s_rect
256 styleMask: i_stylemask
257 backing: NSBackingStoreBuffered
258 defer: YES screen: o_screen];
260 [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
262 if( var_GetBool( p_real_vout, "video-on-top" ) )
264 [self setLevel: NSStatusWindowLevel];
274 [self makeKeyAndOrderFront: nil];
275 [self setReleasedWhenClosed: YES];
277 /* We'll catch mouse events */
278 [self setAcceptsMouseMovedEvents: YES];
279 [self makeFirstResponder: self];
281 /* Add the view. It's automatically resized to fit the window */
282 [self setContentView: o_view];
286 b_init_ok = VLC_TRUE;
292 /* XXX waitUntilDone = NO to avoid a possible deadlock when hitting
294 [self performSelectorOnMainThread: @selector(closeReal:)
295 withObject: NULL waitUntilDone: NO];
298 - (id) closeReal: (id) sender
301 if( p_fullscreen_state )
303 EndFullScreen( p_fullscreen_state, 0 );
308 - (void)setOnTop:(BOOL)b_on_top
312 [self setLevel: NSStatusWindowLevel];
316 [self setLevel: NSNormalWindowLevel];
320 - (void)hideMouse:(BOOL)b_hide
324 NSView *o_contents = [self contentView];
326 ml = [self convertScreenToBase:[NSEvent mouseLocation]];
327 ml = [o_contents convertPoint:ml fromView:nil];
328 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
330 if( b_hide && b_inside )
332 [NSCursor setHiddenUntilMouseMoves: YES];
336 [NSCursor setHiddenUntilMouseMoves: NO];
342 if( p_fullscreen_state )
344 if( mdate() - i_time_mouse_last_moved > 3000000 )
346 [self hideMouse: YES];
351 [self hideMouse: NO];
354 /* Disable screensaver */
355 UpdateSystemActivity( UsrActivity );
358 - (void)scaleWindowWithFactor: (float)factor
361 int i_corrected_height, i_corrected_width;
363 NSPoint topleftscreen;
365 if ( !p_vout->b_fullscreen )
368 topleftbase.y = [self frame].size.height;
369 topleftscreen = [self convertBaseToScreen: topleftbase];
371 if( p_vout->render.i_height * p_vout->render.i_aspect >
372 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
374 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
376 newsize.width = (int) ( i_corrected_width * factor );
377 newsize.height = (int) ( p_vout->render.i_height * factor );
381 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
382 p_vout->render.i_aspect;
383 newsize.width = (int) ( p_vout->render.i_width * factor );
384 newsize.height = (int) ( i_corrected_height * factor );
387 [self setContentSize: newsize];
389 [self setFrameTopLeftPoint: topleftscreen];
390 p_vout->i_changes |= VOUT_SIZE_CHANGE;
394 - (void)toggleFloatOnTop
398 if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
400 val.b_bool = VLC_FALSE;
404 val.b_bool = VLC_TRUE;
406 var_Set( p_real_vout, "video-on-top", val );
409 - (void)toggleFullscreen
412 var_Get( p_real_vout, "fullscreen", &val );
413 val.b_bool = !val.b_bool;
414 var_Set( p_real_vout, "fullscreen", val );
420 var_Get( p_real_vout, "fullscreen", &val );
421 return( val.b_bool );
426 vout_Control( p_real_vout, VOUT_SNAPSHOT );
429 - (BOOL)canBecomeKeyWindow
434 /* Sometimes crashes VLC....
435 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
437 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
440 - (void)keyDown:(NSEvent *)o_event
444 unsigned int i_pressed_modifiers = 0;
447 i_pressed_modifiers = [o_event modifierFlags];
449 if( i_pressed_modifiers & NSShiftKeyMask )
450 val.i_int |= KEY_MODIFIER_SHIFT;
451 if( i_pressed_modifiers & NSControlKeyMask )
452 val.i_int |= KEY_MODIFIER_CTRL;
453 if( i_pressed_modifiers & NSAlternateKeyMask )
454 val.i_int |= KEY_MODIFIER_ALT;
455 if( i_pressed_modifiers & NSCommandKeyMask )
456 val.i_int |= KEY_MODIFIER_COMMAND;
458 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
462 /* Escape should always get you out of fullscreen */
463 if( key == (unichar) 0x1b )
465 if( [self isFullscreen] )
467 [self toggleFullscreen];
470 else if ( key == ' ' )
473 val.i_int = config_GetInt( p_vout, "key-play-pause" );
474 var_Set( p_vout->p_vlc, "key-pressed", val );
478 val.i_int |= CocoaKeyToVLC( key );
479 var_Set( p_vout->p_vlc, "key-pressed", val );
484 [super keyDown: o_event];
490 NSMutableString * o_title = NULL, * o_mrl = NULL;
491 input_thread_t * p_input;
498 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
501 if( p_input == NULL )
506 if( p_input->input.p_item->psz_name != NULL )
507 o_title = [NSMutableString stringWithUTF8String:
508 p_input->input.p_item->psz_name];
509 if( p_input->input.p_item->psz_uri != NULL )
510 o_mrl = [NSMutableString stringWithUTF8String:
511 p_input->input.p_item->psz_uri];
515 vlc_object_release( p_input );
518 if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
520 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
521 if( prefix_range.location != NSNotFound )
522 [o_mrl deleteCharactersInRange: prefix_range];
523 [self setRepresentedFilename: o_mrl];
525 [self setTitle: o_title];
529 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
533 /* This is actually the same as VLCControls::stop. */
534 - (BOOL)windowShouldClose:(id)sender
536 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
538 if( p_playlist == NULL )
543 playlist_Stop( p_playlist );
544 vlc_object_release( p_playlist );
546 /* The window will be closed by the intf later. */
550 - (BOOL)acceptsFirstResponder
555 - (BOOL)becomeFirstResponder
560 - (BOOL)resignFirstResponder
562 /* We need to stay the first responder or we'll miss some
567 - (void)mouseDown:(NSEvent *)o_event
571 switch( [o_event type] )
573 case NSLeftMouseDown:
575 var_Get( p_vout, "mouse-button-down", &val );
577 var_Set( p_vout, "mouse-button-down", val );
582 [super mouseDown: o_event];
587 - (void)otherMouseDown:(NSEvent *)o_event
591 switch( [o_event type] )
593 case NSOtherMouseDown:
595 var_Get( p_vout, "mouse-button-down", &val );
597 var_Set( p_vout, "mouse-button-down", val );
602 [super mouseDown: o_event];
607 - (void)rightMouseDown:(NSEvent *)o_event
611 switch( [o_event type] )
613 case NSRightMouseDown:
615 var_Get( p_vout, "mouse-button-down", &val );
617 var_Set( p_vout, "mouse-button-down", val );
622 [super mouseDown: o_event];
627 - (void)mouseUp:(NSEvent *)o_event
631 switch( [o_event type] )
636 b_val.b_bool = VLC_TRUE;
637 var_Set( p_vout, "mouse-clicked", b_val );
639 var_Get( p_vout, "mouse-button-down", &val );
641 var_Set( p_vout, "mouse-button-down", val );
646 [super mouseUp: o_event];
651 - (void)otherMouseUp:(NSEvent *)o_event
655 switch( [o_event type] )
659 var_Get( p_vout, "mouse-button-down", &val );
661 var_Set( p_vout, "mouse-button-down", val );
666 [super mouseUp: o_event];
671 - (void)rightMouseUp:(NSEvent *)o_event
675 switch( [o_event type] )
679 var_Get( p_vout, "mouse-button-down", &val );
681 var_Set( p_vout, "mouse-button-down", val );
686 [super mouseUp: o_event];
691 - (void)mouseDragged:(NSEvent *)o_event
693 [self mouseMoved: o_event];
696 - (void)otherMouseDragged:(NSEvent *)o_event
698 [self mouseMoved: o_event];
701 - (void)rightMouseDragged:(NSEvent *)o_event
703 [self mouseMoved: o_event];
706 - (void)mouseMoved:(NSEvent *)o_event
712 i_time_mouse_last_moved = mdate();
714 s_rect = [o_view bounds];
715 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
716 b_inside = [o_view mouse: ml inRect: s_rect];
721 unsigned int i_width, i_height, i_x, i_y;
723 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
724 (unsigned int)s_rect.size.height,
725 &i_x, &i_y, &i_width, &i_height );
727 val.i_int = ( ((int)ml.x) - i_x ) *
728 p_vout->render.i_width / i_width;
729 var_Set( p_vout, "mouse-x", val );
731 if( [[o_view className] isEqualToString: @"VLCGLView"] )
733 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
734 p_vout->render.i_height / i_height;
738 val.i_int = ( ((int)ml.y) - i_y ) *
739 p_vout->render.i_height / i_height;
741 var_Set( p_vout, "mouse-y", val );
743 val.b_bool = VLC_TRUE;
744 var_Set( p_vout, "mouse-moved", val );
747 [super mouseMoved: o_event];