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>
43 /*****************************************************************************
44 * VLCWindow implementation
45 *****************************************************************************/
46 @implementation VLCWindow
48 - (id)initWithVout:(vout_thread_t *)_p_vout frame:(NSRect *)s_frame
54 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
55 for( i_timeout = 20 ; i_timeout-- ; )
59 msleep( INTF_IDLE_SLEEP );
65 /* No MacOS X intf, unable to communicate with MT */
66 msg_Err( p_vout, "no MacOS X interface present" );
70 /* p_real_vout: the vout we have to use to check for video-on-top
71 and a few other things. If we are the QuickTime output, it's us.
72 It we are the OpenGL provider, it is our parent. */
73 if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
75 p_real_vout = (vout_thread_t *) p_vout->p_parent;
82 p_fullscreen_state = NULL;
83 i_time_mouse_last_moved = mdate();
86 vlc_bool_t b_main_screen;
88 var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
89 var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
90 var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
91 var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
93 /* Setup the menuitem for the multiple displays. Read the vlc preference (macosx-vdev) for the primary display */
94 NSArray * o_screens = [NSScreen screens];
95 if( [o_screens count] > 0 && var_Type( p_real_vout, "video-device" ) == 0 )
98 vlc_value_t val, val2, text;
101 var_Get( p_real_vout, "macosx-vdev", &val );
103 var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER |
105 text.psz_string = _("Video device");
106 var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
108 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
110 while( (o_screen = [o_enumerator nextObject]) != NULL )
113 NSRect s_rect = [o_screen frame];
115 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
116 "%s %d (%dx%d)", _("Screen"), i,
117 (int)s_rect.size.width, (int)s_rect.size.height );
119 text.psz_string = psz_temp;
121 var_Change( p_real_vout, "video-device",
122 VLC_VAR_ADDCHOICE, &val2, &text );
124 if( ( i - 1 ) == val.i_int )
126 var_Set( p_real_vout, "video-device", val2 );
131 var_AddCallback( p_real_vout, "video-device", vout_VarCallback,
134 val2.b_bool = VLC_TRUE;
135 var_Set( p_real_vout, "intf-change", val2 );
138 /* Find out on which screen to open the window */
139 int i_device = var_GetInteger( p_real_vout, "video-device" );
142 /* No preference specified. Use the main screen */
143 o_screen = [NSScreen mainScreen];
148 NSArray *o_screens = [NSScreen screens];
150 if( [o_screens count] < (unsigned) i_device )
152 o_screen = [NSScreen mainScreen];
158 o_screen = [o_screens objectAtIndex: i_device];
159 var_SetInteger( p_real_vout, "macosx-vdev", i_device );
160 b_main_screen = ( i_device == 0 );
164 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
166 if( p_vout->b_fullscreen )
168 NSRect screen_rect = [o_screen frame];
169 screen_rect.origin.x = screen_rect.origin.y = 0;
171 /* Creates a window with size: screen_rect on o_screen */
172 [self initWithContentRect: screen_rect
173 styleMask: NSBorderlessWindowMask
174 backing: NSBackingStoreBuffered
175 defer: YES screen: o_screen];
179 BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
180 NULL, NULL, fullScreenAllowEvents );
185 unsigned int i_stylemask = NSTitledWindowMask |
186 NSMiniaturizableWindowMask |
187 NSClosableWindowMask |
188 NSResizableWindowMask;
193 s_rect.size.width = p_vout->i_window_width;
194 s_rect.size.height = p_vout->i_window_height;
201 [self initWithContentRect: s_rect
202 styleMask: i_stylemask
203 backing: NSBackingStoreBuffered
204 defer: YES screen: o_screen];
206 [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
208 if( var_GetBool( p_real_vout, "video-on-top" ) )
210 [self setLevel: NSStatusWindowLevel];
220 [self makeKeyAndOrderFront: nil];
221 [self setReleasedWhenClosed: YES];
223 /* We'll catch mouse events */
224 [self setAcceptsMouseMovedEvents: YES];
225 [self makeFirstResponder: self];
233 if( p_fullscreen_state )
235 EndFullScreen( p_fullscreen_state, 0 );
240 - (void)setOnTop:(BOOL)b_on_top
244 [self setLevel: NSStatusWindowLevel];
248 [self setLevel: NSNormalWindowLevel];
252 - (void)hideMouse:(BOOL)b_hide
256 NSView *o_contents = [self contentView];
258 ml = [self convertScreenToBase:[NSEvent mouseLocation]];
259 ml = [o_contents convertPoint:ml fromView:nil];
260 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
262 if( b_hide && b_inside )
264 [NSCursor setHiddenUntilMouseMoves: YES];
268 [NSCursor setHiddenUntilMouseMoves: NO];
274 if( p_fullscreen_state )
276 if( mdate() - i_time_mouse_last_moved > 3000000 )
278 [self hideMouse: YES];
283 [self hideMouse: NO];
286 /* Disable screensaver */
287 UpdateSystemActivity( UsrActivity );
290 - (void)scaleWindowWithFactor: (float)factor
293 int i_corrected_height, i_corrected_width;
295 NSPoint topleftscreen;
297 if ( !p_vout->b_fullscreen )
300 topleftbase.y = [self frame].size.height;
301 topleftscreen = [self convertBaseToScreen: topleftbase];
303 if( p_vout->render.i_height * p_vout->render.i_aspect >
304 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
306 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
308 newsize.width = (int) ( i_corrected_width * factor );
309 newsize.height = (int) ( p_vout->render.i_height * factor );
313 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
314 p_vout->render.i_aspect;
315 newsize.width = (int) ( p_vout->render.i_width * factor );
316 newsize.height = (int) ( i_corrected_height * factor );
319 [self setContentSize: newsize];
321 [self setFrameTopLeftPoint: topleftscreen];
322 p_vout->i_changes |= VOUT_SIZE_CHANGE;
326 - (void)toggleFloatOnTop
330 if( var_Get( p_vout, "video-on-top", &val )>=0 && val.b_bool)
332 val.b_bool = VLC_FALSE;
336 val.b_bool = VLC_TRUE;
338 var_Set( p_vout, "video-on-top", val );
341 - (void)toggleFullscreen
344 val.b_bool = !p_real_vout->b_fullscreen;
345 var_Set( p_real_vout, "fullscreen", val );
350 return( p_vout->b_fullscreen );
355 vout_Control( p_vout, VOUT_SNAPSHOT );
358 - (BOOL)canBecomeKeyWindow
363 /* Sometimes crashes VLC....
364 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
366 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
369 - (void)keyDown:(NSEvent *)o_event
373 unsigned int i_pressed_modifiers = 0;
376 i_pressed_modifiers = [o_event modifierFlags];
378 if( i_pressed_modifiers & NSShiftKeyMask )
379 val.i_int |= KEY_MODIFIER_SHIFT;
380 if( i_pressed_modifiers & NSControlKeyMask )
381 val.i_int |= KEY_MODIFIER_CTRL;
382 if( i_pressed_modifiers & NSAlternateKeyMask )
383 val.i_int |= KEY_MODIFIER_ALT;
384 if( i_pressed_modifiers & NSCommandKeyMask )
385 val.i_int |= KEY_MODIFIER_COMMAND;
387 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
391 /* Escape should always get you out of fullscreen */
392 if( key == (unichar) 0x1b )
394 if( [self isFullscreen] )
396 [self toggleFullscreen];
399 else if ( key == ' ' )
402 val.i_int = config_GetInt( p_vout, "key-play-pause" );
403 var_Set( p_vout->p_vlc, "key-pressed", val );
407 val.i_int |= CocoaKeyToVLC( key );
408 var_Set( p_vout->p_vlc, "key-pressed", val );
413 [super keyDown: o_event];
419 NSMutableString * o_title,* o_mrl;
420 vlc_bool_t b_file = VLC_FALSE;
421 input_thread_t * p_input;
428 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
431 if( p_input == NULL )
436 if( ! strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
438 if( p_input->input.p_item->psz_name != NULL )
439 o_title = [NSMutableString stringWithUTF8String:
440 p_input->input.p_item->psz_name];
441 if( p_input->input.p_item->psz_uri != NULL )
442 o_mrl = [NSMutableString stringWithUTF8String:
443 p_input->input.p_item->psz_uri];
447 vlc_object_release( p_input );
450 if( b_file == VLC_TRUE )
452 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
453 if( prefix_range.location != NSNotFound )
454 [o_mrl deleteCharactersInRange: prefix_range];
455 [self setRepresentedFilename: o_mrl];
457 [self setTitle: o_title];
461 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
465 /* This is actually the same as VLCControls::stop. */
466 - (BOOL)windowShouldClose:(id)sender
468 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
470 if( p_playlist == NULL )
475 playlist_Stop( p_playlist );
476 vlc_object_release( p_playlist );
478 /* The window will be closed by the intf later. */
482 - (BOOL)acceptsFirstResponder
487 - (BOOL)becomeFirstResponder
492 - (BOOL)resignFirstResponder
494 /* We need to stay the first responder or we'll miss some
499 - (void)mouseDown:(NSEvent *)o_event
503 switch( [o_event type] )
505 case NSLeftMouseDown:
507 var_Get( p_vout, "mouse-button-down", &val );
509 var_Set( p_vout, "mouse-button-down", val );
514 [super mouseDown: o_event];
519 - (void)otherMouseDown:(NSEvent *)o_event
523 switch( [o_event type] )
525 case NSOtherMouseDown:
527 var_Get( p_vout, "mouse-button-down", &val );
529 var_Set( p_vout, "mouse-button-down", val );
534 [super mouseDown: o_event];
539 - (void)rightMouseDown:(NSEvent *)o_event
543 switch( [o_event type] )
545 case NSRightMouseDown:
547 var_Get( p_vout, "mouse-button-down", &val );
549 var_Set( p_vout, "mouse-button-down", val );
554 [super mouseDown: o_event];
559 - (void)mouseUp:(NSEvent *)o_event
563 switch( [o_event type] )
568 b_val.b_bool = VLC_TRUE;
569 var_Set( p_vout, "mouse-clicked", b_val );
571 var_Get( p_vout, "mouse-button-down", &val );
573 var_Set( p_vout, "mouse-button-down", val );
578 [super mouseUp: o_event];
583 - (void)otherMouseUp:(NSEvent *)o_event
587 switch( [o_event type] )
591 var_Get( p_vout, "mouse-button-down", &val );
593 var_Set( p_vout, "mouse-button-down", val );
598 [super mouseUp: o_event];
603 - (void)rightMouseUp:(NSEvent *)o_event
607 switch( [o_event type] )
611 var_Get( p_vout, "mouse-button-down", &val );
613 var_Set( p_vout, "mouse-button-down", val );
618 [super mouseUp: o_event];
623 - (void)mouseDragged:(NSEvent *)o_event
625 [self mouseMoved: o_event];
628 - (void)otherMouseDragged:(NSEvent *)o_event
630 [self mouseMoved: o_event];
633 - (void)rightMouseDragged:(NSEvent *)o_event
635 [self mouseMoved: o_event];
638 - (void)mouseMoved:(NSEvent *)o_event
645 i_time_mouse_last_moved = mdate();
647 o_view = [self contentView];
648 s_rect = [o_view bounds];
649 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
650 b_inside = [o_view mouse: ml inRect: s_rect];
655 int i_width, i_height, i_x, i_y;
657 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
658 (unsigned int)s_rect.size.height,
659 &i_x, &i_y, &i_width, &i_height );
661 val.i_int = ( ((int)ml.x) - i_x ) *
662 p_vout->render.i_width / i_width;
663 var_Set( p_vout, "mouse-x", val );
665 if( [[o_view className] isEqualToString: @"VLCGLView"] )
667 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
668 p_vout->render.i_height / i_height;
672 val.i_int = ( ((int)ml.y) - i_y ) *
673 p_vout->render.i_height / i_height;
675 var_Set( p_vout, "mouse-y", val );
677 val.b_bool = VLC_TRUE;
678 var_Set( p_vout, "mouse-moved", val );
681 [super mouseMoved: o_event];