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 setContentView: NULL];
295 [self performSelectorOnMainThread: @selector(closeReal:)
296 withObject: NULL waitUntilDone: NO];
299 - (id) closeReal: (id) sender
302 if( p_fullscreen_state )
304 EndFullScreen( p_fullscreen_state, 0 );
309 - (void)setOnTop:(BOOL)b_on_top
313 [self setLevel: NSStatusWindowLevel];
317 [self setLevel: NSNormalWindowLevel];
321 - (void)hideMouse:(BOOL)b_hide
325 NSView *o_contents = [self contentView];
327 ml = [self convertScreenToBase:[NSEvent mouseLocation]];
328 ml = [o_contents convertPoint:ml fromView:nil];
329 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
331 if( b_hide && b_inside )
333 [NSCursor setHiddenUntilMouseMoves: YES];
337 [NSCursor setHiddenUntilMouseMoves: NO];
343 if( p_fullscreen_state )
345 if( mdate() - i_time_mouse_last_moved > 3000000 )
347 [self hideMouse: YES];
352 [self hideMouse: NO];
355 /* Disable screensaver */
356 UpdateSystemActivity( UsrActivity );
359 - (void)scaleWindowWithFactor: (float)factor
362 int i_corrected_height, i_corrected_width;
364 NSPoint topleftscreen;
366 if ( !p_vout->b_fullscreen )
369 topleftbase.y = [self frame].size.height;
370 topleftscreen = [self convertBaseToScreen: topleftbase];
372 if( p_vout->render.i_height * p_vout->render.i_aspect >
373 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
375 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
377 newsize.width = (int) ( i_corrected_width * factor );
378 newsize.height = (int) ( p_vout->render.i_height * factor );
382 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
383 p_vout->render.i_aspect;
384 newsize.width = (int) ( p_vout->render.i_width * factor );
385 newsize.height = (int) ( i_corrected_height * factor );
388 [self setContentSize: newsize];
390 [self setFrameTopLeftPoint: topleftscreen];
391 p_vout->i_changes |= VOUT_SIZE_CHANGE;
395 - (void)toggleFloatOnTop
399 if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
401 val.b_bool = VLC_FALSE;
405 val.b_bool = VLC_TRUE;
407 var_Set( p_real_vout, "video-on-top", val );
410 - (void)toggleFullscreen
413 var_Get( p_real_vout, "fullscreen", &val );
414 val.b_bool = !val.b_bool;
415 var_Set( p_real_vout, "fullscreen", val );
421 var_Get( p_real_vout, "fullscreen", &val );
422 return( val.b_bool );
427 vout_Control( p_real_vout, VOUT_SNAPSHOT );
430 - (BOOL)canBecomeKeyWindow
435 /* Sometimes crashes VLC....
436 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
438 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
441 - (void)keyDown:(NSEvent *)o_event
445 unsigned int i_pressed_modifiers = 0;
448 i_pressed_modifiers = [o_event modifierFlags];
450 if( i_pressed_modifiers & NSShiftKeyMask )
451 val.i_int |= KEY_MODIFIER_SHIFT;
452 if( i_pressed_modifiers & NSControlKeyMask )
453 val.i_int |= KEY_MODIFIER_CTRL;
454 if( i_pressed_modifiers & NSAlternateKeyMask )
455 val.i_int |= KEY_MODIFIER_ALT;
456 if( i_pressed_modifiers & NSCommandKeyMask )
457 val.i_int |= KEY_MODIFIER_COMMAND;
459 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
463 /* Escape should always get you out of fullscreen */
464 if( key == (unichar) 0x1b )
466 if( [self isFullscreen] )
468 [self toggleFullscreen];
471 else if ( key == ' ' )
474 val.i_int = config_GetInt( p_vout, "key-play-pause" );
475 var_Set( p_vout->p_vlc, "key-pressed", val );
479 val.i_int |= CocoaKeyToVLC( key );
480 var_Set( p_vout->p_vlc, "key-pressed", val );
485 [super keyDown: o_event];
491 NSMutableString * o_title = NULL, * o_mrl = NULL;
492 input_thread_t * p_input;
499 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
502 if( p_input == NULL )
507 if( p_input->input.p_item->psz_name != NULL )
508 o_title = [NSMutableString stringWithUTF8String:
509 p_input->input.p_item->psz_name];
510 if( p_input->input.p_item->psz_uri != NULL )
511 o_mrl = [NSMutableString stringWithUTF8String:
512 p_input->input.p_item->psz_uri];
516 vlc_object_release( p_input );
519 if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
521 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
522 if( prefix_range.location != NSNotFound )
523 [o_mrl deleteCharactersInRange: prefix_range];
524 [self setRepresentedFilename: o_mrl];
526 [self setTitle: o_title];
530 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
534 /* This is actually the same as VLCControls::stop. */
535 - (BOOL)windowShouldClose:(id)sender
537 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
539 if( p_playlist == NULL )
544 playlist_Stop( p_playlist );
545 vlc_object_release( p_playlist );
547 /* The window will be closed by the intf later. */
551 - (BOOL)acceptsFirstResponder
556 - (BOOL)becomeFirstResponder
561 - (BOOL)resignFirstResponder
563 /* We need to stay the first responder or we'll miss some
568 - (void)mouseDown:(NSEvent *)o_event
572 switch( [o_event type] )
574 case NSLeftMouseDown:
576 var_Get( p_vout, "mouse-button-down", &val );
578 var_Set( p_vout, "mouse-button-down", val );
583 [super mouseDown: o_event];
588 - (void)otherMouseDown:(NSEvent *)o_event
592 switch( [o_event type] )
594 case NSOtherMouseDown:
596 var_Get( p_vout, "mouse-button-down", &val );
598 var_Set( p_vout, "mouse-button-down", val );
603 [super mouseDown: o_event];
608 - (void)rightMouseDown:(NSEvent *)o_event
612 switch( [o_event type] )
614 case NSRightMouseDown:
616 var_Get( p_vout, "mouse-button-down", &val );
618 var_Set( p_vout, "mouse-button-down", val );
623 [super mouseDown: o_event];
628 - (void)mouseUp:(NSEvent *)o_event
632 switch( [o_event type] )
637 b_val.b_bool = VLC_TRUE;
638 var_Set( p_vout, "mouse-clicked", b_val );
640 var_Get( p_vout, "mouse-button-down", &val );
642 var_Set( p_vout, "mouse-button-down", val );
647 [super mouseUp: o_event];
652 - (void)otherMouseUp:(NSEvent *)o_event
656 switch( [o_event type] )
660 var_Get( p_vout, "mouse-button-down", &val );
662 var_Set( p_vout, "mouse-button-down", val );
667 [super mouseUp: o_event];
672 - (void)rightMouseUp:(NSEvent *)o_event
676 switch( [o_event type] )
680 var_Get( p_vout, "mouse-button-down", &val );
682 var_Set( p_vout, "mouse-button-down", val );
687 [super mouseUp: o_event];
692 - (void)mouseDragged:(NSEvent *)o_event
694 [self mouseMoved: o_event];
697 - (void)otherMouseDragged:(NSEvent *)o_event
699 [self mouseMoved: o_event];
702 - (void)rightMouseDragged:(NSEvent *)o_event
704 [self mouseMoved: o_event];
707 - (void)mouseMoved:(NSEvent *)o_event
713 i_time_mouse_last_moved = mdate();
715 s_rect = [o_view bounds];
716 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
717 b_inside = [o_view mouse: ml inRect: s_rect];
722 unsigned int i_width, i_height, i_x, i_y;
724 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
725 (unsigned int)s_rect.size.height,
726 &i_x, &i_y, &i_width, &i_height );
728 val.i_int = ( ((int)ml.x) - i_x ) *
729 p_vout->render.i_width / i_width;
730 var_Set( p_vout, "mouse-x", val );
732 if( [[o_view className] isEqualToString: @"VLCGLView"] )
734 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
735 p_vout->render.i_height / i_height;
739 val.i_int = ( ((int)ml.y) - i_y ) *
740 p_vout->render.i_height / i_height;
742 var_Set( p_vout, "mouse-y", val );
744 val.b_bool = VLC_TRUE;
745 var_Set( p_vout, "mouse-moved", val );
748 [super mouseMoved: o_event];