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 frame: (NSRect *) frame
73 if( MACOS_VERSION >= 10.2 )
75 [self performSelectorOnMainThread: @selector(initReal:)
76 withObject: NULL waitUntilDone: YES];
80 [self initReal: NULL];
91 - (id) initReal: (id) sender
93 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
94 NSArray *o_screens = [NSScreen screens];
96 vlc_bool_t b_menubar_screen = VLC_FALSE;
97 int i_timeout, i_device;
98 vlc_value_t value_drawable;
100 b_init_ok = VLC_FALSE;
102 var_Get( p_vout->p_vlc, "drawable", &value_drawable );
104 /* We only wait for NSApp to initialise if we're not embedded (as in the
105 * case of the Mozilla plugin). We can tell whether we're embedded or not
106 * by examining the "drawable" value: if it's zero, we're running in the
107 * main Mac intf; if it's non-zero, we're embedded. */
108 if( value_drawable.i_int == 0 )
110 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
111 for( i_timeout = 20 ; i_timeout-- ; )
115 msleep( INTF_IDLE_SLEEP );
121 /* No MacOS X intf, unable to communicate with MT */
122 msg_Err( p_vout, "no MacOS X interface present" );
127 if( [o_screens count] <= 0 )
129 msg_Err( p_vout, "no OSX screens available" );
133 /* p_real_vout: the vout we have to use to check for video-on-top
134 and a few other things. If we are the QuickTime output, it's us.
135 It we are the OpenGL provider, it is our parent. */
136 if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
138 p_real_vout = (vout_thread_t *) p_vout->p_parent;
142 p_real_vout = p_vout;
145 p_fullscreen_state = NULL;
146 i_time_mouse_last_moved = mdate();
148 var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
149 var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
150 var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
151 var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
153 /* Get the pref value when this is the first time, otherwise retrieve the device from the top level video-device var */
154 if( var_Type( p_real_vout->p_vlc, "video-device" ) == 0 )
156 i_device = var_GetInteger( p_vout, "macosx-vdev" );
160 i_device = var_GetInteger( p_real_vout->p_vlc, "video-device" );
163 /* Setup the menuitem for the multiple displays. */
164 if( var_Type( p_real_vout, "video-device" ) == 0 )
167 vlc_value_t val2, text;
170 var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER |
172 text.psz_string = _("Video Device");
173 var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
175 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
178 text.psz_string = _("Default");
179 var_Change( p_real_vout, "video-device",
180 VLC_VAR_ADDCHOICE, &val2, &text );
181 var_Set( p_real_vout, "video-device", val2 );
183 while( (o_screen = [o_enumerator nextObject]) != NULL )
186 NSRect s_rect = [o_screen frame];
188 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
189 "%s %d (%dx%d)", _("Screen"), i,
190 (int)s_rect.size.width, (int)s_rect.size.height );
192 text.psz_string = psz_temp;
194 var_Change( p_real_vout, "video-device",
195 VLC_VAR_ADDCHOICE, &val2, &text );
198 var_Set( p_real_vout, "video-device", val2 );
203 var_AddCallback( p_real_vout, "video-device", DeviceCallback,
206 val2.b_bool = VLC_TRUE;
207 var_Set( p_real_vout, "intf-change", val2 );
210 /* Find out on which screen to open the window */
211 if( i_device <= 0 || i_device > (int)[o_screens count] )
213 /* No preference specified. Use the main screen */
214 o_screen = [NSScreen mainScreen];
215 if( o_screen == [o_screens objectAtIndex: 0] )
216 b_menubar_screen = VLC_TRUE;
221 o_screen = [o_screens objectAtIndex: i_device];
222 b_menubar_screen = ( i_device == 0 );
225 if( p_vout->b_fullscreen )
227 NSRect screen_rect = [o_screen frame];
228 screen_rect.origin.x = screen_rect.origin.y = 0;
230 /* Creates a window with size: screen_rect on o_screen */
231 [self initWithContentRect: screen_rect
232 styleMask: NSBorderlessWindowMask
233 backing: NSBackingStoreBuffered
234 defer: YES screen: o_screen];
236 if( b_menubar_screen )
238 BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
239 NULL, NULL, fullScreenAllowEvents );
244 unsigned int i_stylemask = NSTitledWindowMask |
245 NSMiniaturizableWindowMask |
246 NSClosableWindowMask |
247 NSResizableWindowMask;
252 s_rect.size.width = p_vout->i_window_width;
253 s_rect.size.height = p_vout->i_window_height;
260 [self initWithContentRect: s_rect
261 styleMask: i_stylemask
262 backing: NSBackingStoreBuffered
263 defer: YES screen: o_screen];
265 [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
267 if( var_GetBool( p_real_vout, "video-on-top" ) )
269 [self setLevel: NSStatusWindowLevel];
279 [self makeKeyAndOrderFront: nil];
280 [self setReleasedWhenClosed: YES];
282 /* We'll catch mouse events */
283 [self setAcceptsMouseMovedEvents: YES];
284 [self makeFirstResponder: self];
288 b_init_ok = VLC_TRUE;
295 if( p_fullscreen_state )
297 EndFullScreen( p_fullscreen_state, 0 );
301 - (void)setOnTop:(BOOL)b_on_top
305 [self setLevel: NSStatusWindowLevel];
309 [self setLevel: NSNormalWindowLevel];
313 - (void)hideMouse:(BOOL)b_hide
317 NSView *o_contents = [self contentView];
319 ml = [self convertScreenToBase:[NSEvent mouseLocation]];
320 ml = [o_contents convertPoint:ml fromView:nil];
321 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
323 if( b_hide && b_inside )
325 [NSCursor setHiddenUntilMouseMoves: YES];
329 [NSCursor setHiddenUntilMouseMoves: NO];
335 if( p_fullscreen_state )
337 if( mdate() - i_time_mouse_last_moved > 3000000 )
339 [self hideMouse: YES];
344 [self hideMouse: NO];
347 /* Disable screensaver */
348 UpdateSystemActivity( UsrActivity );
351 - (void)scaleWindowWithFactor: (float)factor
354 int i_corrected_height, i_corrected_width;
356 NSPoint topleftscreen;
358 if ( !p_vout->b_fullscreen )
361 topleftbase.y = [self frame].size.height;
362 topleftscreen = [self convertBaseToScreen: topleftbase];
364 if( p_vout->render.i_height * p_vout->render.i_aspect >
365 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
367 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
369 newsize.width = (int) ( i_corrected_width * factor );
370 newsize.height = (int) ( p_vout->render.i_height * factor );
374 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
375 p_vout->render.i_aspect;
376 newsize.width = (int) ( p_vout->render.i_width * factor );
377 newsize.height = (int) ( i_corrected_height * factor );
380 [self setContentSize: newsize];
382 [self setFrameTopLeftPoint: topleftscreen];
383 p_vout->i_changes |= VOUT_SIZE_CHANGE;
387 - (void)toggleFloatOnTop
391 if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
393 val.b_bool = VLC_FALSE;
397 val.b_bool = VLC_TRUE;
399 var_Set( p_real_vout, "video-on-top", val );
402 - (void)toggleFullscreen
405 var_Get( p_real_vout, "fullscreen", &val );
406 val.b_bool = !val.b_bool;
407 var_Set( p_real_vout, "fullscreen", val );
413 var_Get( p_real_vout, "fullscreen", &val );
414 return( val.b_bool );
419 vout_Control( p_real_vout, VOUT_SNAPSHOT );
422 - (BOOL)canBecomeKeyWindow
427 /* Sometimes crashes VLC....
428 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
430 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
433 - (void)keyDown:(NSEvent *)o_event
437 unsigned int i_pressed_modifiers = 0;
440 i_pressed_modifiers = [o_event modifierFlags];
442 if( i_pressed_modifiers & NSShiftKeyMask )
443 val.i_int |= KEY_MODIFIER_SHIFT;
444 if( i_pressed_modifiers & NSControlKeyMask )
445 val.i_int |= KEY_MODIFIER_CTRL;
446 if( i_pressed_modifiers & NSAlternateKeyMask )
447 val.i_int |= KEY_MODIFIER_ALT;
448 if( i_pressed_modifiers & NSCommandKeyMask )
449 val.i_int |= KEY_MODIFIER_COMMAND;
451 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
455 /* Escape should always get you out of fullscreen */
456 if( key == (unichar) 0x1b )
458 if( [self isFullscreen] )
460 [self toggleFullscreen];
463 else if ( key == ' ' )
466 val.i_int = config_GetInt( p_vout, "key-play-pause" );
467 var_Set( p_vout->p_vlc, "key-pressed", val );
471 val.i_int |= CocoaKeyToVLC( key );
472 var_Set( p_vout->p_vlc, "key-pressed", val );
477 [super keyDown: o_event];
483 NSMutableString * o_title = NULL, * o_mrl = NULL;
484 input_thread_t * p_input;
491 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
494 if( p_input == NULL )
499 if( p_input->input.p_item->psz_name != NULL )
500 o_title = [NSMutableString stringWithUTF8String:
501 p_input->input.p_item->psz_name];
502 if( p_input->input.p_item->psz_uri != NULL )
503 o_mrl = [NSMutableString stringWithUTF8String:
504 p_input->input.p_item->psz_uri];
508 vlc_object_release( p_input );
511 if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
513 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
514 if( prefix_range.location != NSNotFound )
515 [o_mrl deleteCharactersInRange: prefix_range];
516 [self setRepresentedFilename: o_mrl];
518 [self setTitle: o_title];
522 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
526 /* This is actually the same as VLCControls::stop. */
527 - (BOOL)windowShouldClose:(id)sender
529 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
531 if( p_playlist == NULL )
536 playlist_Stop( p_playlist );
537 vlc_object_release( p_playlist );
539 /* The window will be closed by the intf later. */
543 - (BOOL)acceptsFirstResponder
548 - (BOOL)becomeFirstResponder
553 - (BOOL)resignFirstResponder
555 /* We need to stay the first responder or we'll miss some
560 - (void)mouseDown:(NSEvent *)o_event
564 switch( [o_event type] )
566 case NSLeftMouseDown:
568 var_Get( p_vout, "mouse-button-down", &val );
570 var_Set( p_vout, "mouse-button-down", val );
575 [super mouseDown: o_event];
580 - (void)otherMouseDown:(NSEvent *)o_event
584 switch( [o_event type] )
586 case NSOtherMouseDown:
588 var_Get( p_vout, "mouse-button-down", &val );
590 var_Set( p_vout, "mouse-button-down", val );
595 [super mouseDown: o_event];
600 - (void)rightMouseDown:(NSEvent *)o_event
604 switch( [o_event type] )
606 case NSRightMouseDown:
608 var_Get( p_vout, "mouse-button-down", &val );
610 var_Set( p_vout, "mouse-button-down", val );
615 [super mouseDown: o_event];
620 - (void)mouseUp:(NSEvent *)o_event
624 switch( [o_event type] )
629 b_val.b_bool = VLC_TRUE;
630 var_Set( p_vout, "mouse-clicked", b_val );
632 var_Get( p_vout, "mouse-button-down", &val );
634 var_Set( p_vout, "mouse-button-down", val );
639 [super mouseUp: o_event];
644 - (void)otherMouseUp:(NSEvent *)o_event
648 switch( [o_event type] )
652 var_Get( p_vout, "mouse-button-down", &val );
654 var_Set( p_vout, "mouse-button-down", val );
659 [super mouseUp: o_event];
664 - (void)rightMouseUp:(NSEvent *)o_event
668 switch( [o_event type] )
672 var_Get( p_vout, "mouse-button-down", &val );
674 var_Set( p_vout, "mouse-button-down", val );
679 [super mouseUp: o_event];
684 - (void)mouseDragged:(NSEvent *)o_event
686 [self mouseMoved: o_event];
689 - (void)otherMouseDragged:(NSEvent *)o_event
691 [self mouseMoved: o_event];
694 - (void)rightMouseDragged:(NSEvent *)o_event
696 [self mouseMoved: o_event];
699 - (void)mouseMoved:(NSEvent *)o_event
706 i_time_mouse_last_moved = mdate();
708 o_view = [self contentView];
709 s_rect = [o_view bounds];
710 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
711 b_inside = [o_view mouse: ml inRect: s_rect];
716 unsigned int i_width, i_height, i_x, i_y;
718 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
719 (unsigned int)s_rect.size.height,
720 &i_x, &i_y, &i_width, &i_height );
722 val.i_int = ( ((int)ml.x) - i_x ) *
723 p_vout->render.i_width / i_width;
724 var_Set( p_vout, "mouse-x", val );
726 if( [[o_view className] isEqualToString: @"VLCGLView"] )
728 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
729 p_vout->render.i_height / i_height;
733 val.i_int = ( ((int)ml.y) - i_y ) *
734 p_vout->render.i_height / i_height;
736 var_Set( p_vout, "mouse-y", val );
738 val.b_bool = VLC_TRUE;
739 var_Set( p_vout, "mouse-moved", val );
742 [super mouseMoved: o_event];