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 input_thread_t * p_input;
427 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
430 if( p_input == NULL )
435 if( p_input->input.p_item->psz_name != NULL )
436 o_title = [NSMutableString stringWithUTF8String:
437 p_input->input.p_item->psz_name];
438 if( p_input->input.p_item->psz_uri != NULL )
439 o_mrl = [NSMutableString stringWithUTF8String:
440 p_input->input.p_item->psz_uri];
444 vlc_object_release( p_input );
447 if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
449 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
450 if( prefix_range.location != NSNotFound )
451 [o_mrl deleteCharactersInRange: prefix_range];
452 [self setRepresentedFilename: o_mrl];
454 [self setTitle: o_title];
458 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
462 /* This is actually the same as VLCControls::stop. */
463 - (BOOL)windowShouldClose:(id)sender
465 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
467 if( p_playlist == NULL )
472 playlist_Stop( p_playlist );
473 vlc_object_release( p_playlist );
475 /* The window will be closed by the intf later. */
479 - (BOOL)acceptsFirstResponder
484 - (BOOL)becomeFirstResponder
489 - (BOOL)resignFirstResponder
491 /* We need to stay the first responder or we'll miss some
496 - (void)mouseDown:(NSEvent *)o_event
500 switch( [o_event type] )
502 case NSLeftMouseDown:
504 var_Get( p_vout, "mouse-button-down", &val );
506 var_Set( p_vout, "mouse-button-down", val );
511 [super mouseDown: o_event];
516 - (void)otherMouseDown:(NSEvent *)o_event
520 switch( [o_event type] )
522 case NSOtherMouseDown:
524 var_Get( p_vout, "mouse-button-down", &val );
526 var_Set( p_vout, "mouse-button-down", val );
531 [super mouseDown: o_event];
536 - (void)rightMouseDown:(NSEvent *)o_event
540 switch( [o_event type] )
542 case NSRightMouseDown:
544 var_Get( p_vout, "mouse-button-down", &val );
546 var_Set( p_vout, "mouse-button-down", val );
551 [super mouseDown: o_event];
556 - (void)mouseUp:(NSEvent *)o_event
560 switch( [o_event type] )
565 b_val.b_bool = VLC_TRUE;
566 var_Set( p_vout, "mouse-clicked", b_val );
568 var_Get( p_vout, "mouse-button-down", &val );
570 var_Set( p_vout, "mouse-button-down", val );
575 [super mouseUp: o_event];
580 - (void)otherMouseUp:(NSEvent *)o_event
584 switch( [o_event type] )
588 var_Get( p_vout, "mouse-button-down", &val );
590 var_Set( p_vout, "mouse-button-down", val );
595 [super mouseUp: o_event];
600 - (void)rightMouseUp:(NSEvent *)o_event
604 switch( [o_event type] )
608 var_Get( p_vout, "mouse-button-down", &val );
610 var_Set( p_vout, "mouse-button-down", val );
615 [super mouseUp: o_event];
620 - (void)mouseDragged:(NSEvent *)o_event
622 [self mouseMoved: o_event];
625 - (void)otherMouseDragged:(NSEvent *)o_event
627 [self mouseMoved: o_event];
630 - (void)rightMouseDragged:(NSEvent *)o_event
632 [self mouseMoved: o_event];
635 - (void)mouseMoved:(NSEvent *)o_event
642 i_time_mouse_last_moved = mdate();
644 o_view = [self contentView];
645 s_rect = [o_view bounds];
646 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
647 b_inside = [o_view mouse: ml inRect: s_rect];
652 int i_width, i_height, i_x, i_y;
654 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
655 (unsigned int)s_rect.size.height,
656 &i_x, &i_y, &i_width, &i_height );
658 val.i_int = ( ((int)ml.x) - i_x ) *
659 p_vout->render.i_width / i_width;
660 var_Set( p_vout, "mouse-x", val );
662 if( [[o_view className] isEqualToString: @"VLCGLView"] )
664 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
665 p_vout->render.i_height / i_height;
669 val.i_int = ( ((int)ml.y) - i_y ) *
670 p_vout->render.i_height / i_height;
672 var_Set( p_vout, "mouse-y", val );
674 val.b_bool = VLC_TRUE;
675 var_Set( p_vout, "mouse-moved", val );
678 [super mouseMoved: o_event];