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
51 vlc_value_t value_drawable;
55 var_Get( p_vout->p_vlc, "drawable", &value_drawable );
57 /* We only wait for NSApp to initialise if we're not embedded (as in the
58 * case of the Mozilla plugin). We can tell whether we're embedded or not
59 * by examining the "drawable" value: if it's zero, we're running in the
60 * main Mac intf; if it's non-zero, we're embedded. */
61 if( value_drawable.i_int == 0 )
63 /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
64 for( i_timeout = 20 ; i_timeout-- ; )
68 msleep( INTF_IDLE_SLEEP );
74 /* No MacOS X intf, unable to communicate with MT */
75 msg_Err( p_vout, "no MacOS X interface present" );
80 /* p_real_vout: the vout we have to use to check for video-on-top
81 and a few other things. If we are the QuickTime output, it's us.
82 It we are the OpenGL provider, it is our parent. */
83 if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
85 p_real_vout = (vout_thread_t *) p_vout->p_parent;
92 p_fullscreen_state = NULL;
93 i_time_mouse_last_moved = mdate();
96 vlc_bool_t b_main_screen;
98 var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
99 var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
100 var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
101 var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
103 /* Setup the menuitem for the multiple displays. Read the vlc preference (macosx-vdev) for the primary display */
104 NSArray * o_screens = [NSScreen screens];
105 if( [o_screens count] > 0 && var_Type( p_real_vout, "video-device" ) == 0 )
108 vlc_value_t val, val2, text;
111 var_Get( p_real_vout, "macosx-vdev", &val );
113 var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER |
115 text.psz_string = _("Video device");
116 var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
118 NSEnumerator * o_enumerator = [o_screens objectEnumerator];
120 while( (o_screen = [o_enumerator nextObject]) != NULL )
123 NSRect s_rect = [o_screen frame];
125 snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
126 "%s %d (%dx%d)", _("Screen"), i,
127 (int)s_rect.size.width, (int)s_rect.size.height );
129 text.psz_string = psz_temp;
131 var_Change( p_real_vout, "video-device",
132 VLC_VAR_ADDCHOICE, &val2, &text );
134 if( ( i - 1 ) == val.i_int )
136 var_Set( p_real_vout, "video-device", val2 );
141 var_AddCallback( p_real_vout, "video-device", vout_VarCallback,
144 val2.b_bool = VLC_TRUE;
145 var_Set( p_real_vout, "intf-change", val2 );
148 /* Find out on which screen to open the window */
149 int i_device = var_GetInteger( p_real_vout, "video-device" );
152 /* No preference specified. Use the main screen */
153 o_screen = [NSScreen mainScreen];
158 NSArray *o_screens = [NSScreen screens];
160 if( [o_screens count] < (unsigned) i_device )
162 o_screen = [NSScreen mainScreen];
168 o_screen = [o_screens objectAtIndex: i_device];
169 var_SetInteger( p_real_vout, "macosx-vdev", i_device );
170 b_main_screen = ( i_device == 0 );
174 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
176 if( p_vout->b_fullscreen )
178 NSRect screen_rect = [o_screen frame];
179 screen_rect.origin.x = screen_rect.origin.y = 0;
181 /* Creates a window with size: screen_rect on o_screen */
182 [self initWithContentRect: screen_rect
183 styleMask: NSBorderlessWindowMask
184 backing: NSBackingStoreBuffered
185 defer: YES screen: o_screen];
189 BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
190 NULL, NULL, fullScreenAllowEvents );
195 unsigned int i_stylemask = NSTitledWindowMask |
196 NSMiniaturizableWindowMask |
197 NSClosableWindowMask |
198 NSResizableWindowMask;
203 s_rect.size.width = p_vout->i_window_width;
204 s_rect.size.height = p_vout->i_window_height;
211 [self initWithContentRect: s_rect
212 styleMask: i_stylemask
213 backing: NSBackingStoreBuffered
214 defer: YES screen: o_screen];
216 [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
218 if( var_GetBool( p_real_vout, "video-on-top" ) )
220 [self setLevel: NSStatusWindowLevel];
230 [self makeKeyAndOrderFront: nil];
231 [self setReleasedWhenClosed: YES];
233 /* We'll catch mouse events */
234 [self setAcceptsMouseMovedEvents: YES];
235 [self makeFirstResponder: self];
243 if( p_fullscreen_state )
245 EndFullScreen( p_fullscreen_state, 0 );
250 - (void)setOnTop:(BOOL)b_on_top
254 [self setLevel: NSStatusWindowLevel];
258 [self setLevel: NSNormalWindowLevel];
262 - (void)hideMouse:(BOOL)b_hide
266 NSView *o_contents = [self contentView];
268 ml = [self convertScreenToBase:[NSEvent mouseLocation]];
269 ml = [o_contents convertPoint:ml fromView:nil];
270 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
272 if( b_hide && b_inside )
274 [NSCursor setHiddenUntilMouseMoves: YES];
278 [NSCursor setHiddenUntilMouseMoves: NO];
284 if( p_fullscreen_state )
286 if( mdate() - i_time_mouse_last_moved > 3000000 )
288 [self hideMouse: YES];
293 [self hideMouse: NO];
296 /* Disable screensaver */
297 UpdateSystemActivity( UsrActivity );
300 - (void)scaleWindowWithFactor: (float)factor
303 int i_corrected_height, i_corrected_width;
305 NSPoint topleftscreen;
307 if ( !p_vout->b_fullscreen )
310 topleftbase.y = [self frame].size.height;
311 topleftscreen = [self convertBaseToScreen: topleftbase];
313 if( p_vout->render.i_height * p_vout->render.i_aspect >
314 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
316 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
318 newsize.width = (int) ( i_corrected_width * factor );
319 newsize.height = (int) ( p_vout->render.i_height * factor );
323 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
324 p_vout->render.i_aspect;
325 newsize.width = (int) ( p_vout->render.i_width * factor );
326 newsize.height = (int) ( i_corrected_height * factor );
329 [self setContentSize: newsize];
331 [self setFrameTopLeftPoint: topleftscreen];
332 p_vout->i_changes |= VOUT_SIZE_CHANGE;
336 - (void)toggleFloatOnTop
340 if( var_Get( p_vout, "video-on-top", &val )>=0 && val.b_bool)
342 val.b_bool = VLC_FALSE;
346 val.b_bool = VLC_TRUE;
348 var_Set( p_vout, "video-on-top", val );
351 - (void)toggleFullscreen
354 val.b_bool = !p_real_vout->b_fullscreen;
355 var_Set( p_real_vout, "fullscreen", val );
360 return( p_vout->b_fullscreen );
365 vout_Control( p_vout, VOUT_SNAPSHOT );
368 - (BOOL)canBecomeKeyWindow
373 /* Sometimes crashes VLC....
374 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
376 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
379 - (void)keyDown:(NSEvent *)o_event
383 unsigned int i_pressed_modifiers = 0;
386 i_pressed_modifiers = [o_event modifierFlags];
388 if( i_pressed_modifiers & NSShiftKeyMask )
389 val.i_int |= KEY_MODIFIER_SHIFT;
390 if( i_pressed_modifiers & NSControlKeyMask )
391 val.i_int |= KEY_MODIFIER_CTRL;
392 if( i_pressed_modifiers & NSAlternateKeyMask )
393 val.i_int |= KEY_MODIFIER_ALT;
394 if( i_pressed_modifiers & NSCommandKeyMask )
395 val.i_int |= KEY_MODIFIER_COMMAND;
397 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
401 /* Escape should always get you out of fullscreen */
402 if( key == (unichar) 0x1b )
404 if( [self isFullscreen] )
406 [self toggleFullscreen];
409 else if ( key == ' ' )
412 val.i_int = config_GetInt( p_vout, "key-play-pause" );
413 var_Set( p_vout->p_vlc, "key-pressed", val );
417 val.i_int |= CocoaKeyToVLC( key );
418 var_Set( p_vout->p_vlc, "key-pressed", val );
423 [super keyDown: o_event];
429 NSMutableString * o_title,* o_mrl;
430 input_thread_t * p_input;
437 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
440 if( p_input == NULL )
445 if( p_input->input.p_item->psz_name != NULL )
446 o_title = [NSMutableString stringWithUTF8String:
447 p_input->input.p_item->psz_name];
448 if( p_input->input.p_item->psz_uri != NULL )
449 o_mrl = [NSMutableString stringWithUTF8String:
450 p_input->input.p_item->psz_uri];
454 vlc_object_release( p_input );
457 if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
459 NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
460 if( prefix_range.location != NSNotFound )
461 [o_mrl deleteCharactersInRange: prefix_range];
462 [self setRepresentedFilename: o_mrl];
464 [self setTitle: o_title];
468 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
472 /* This is actually the same as VLCControls::stop. */
473 - (BOOL)windowShouldClose:(id)sender
475 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
477 if( p_playlist == NULL )
482 playlist_Stop( p_playlist );
483 vlc_object_release( p_playlist );
485 /* The window will be closed by the intf later. */
489 - (BOOL)acceptsFirstResponder
494 - (BOOL)becomeFirstResponder
499 - (BOOL)resignFirstResponder
501 /* We need to stay the first responder or we'll miss some
506 - (void)mouseDown:(NSEvent *)o_event
510 switch( [o_event type] )
512 case NSLeftMouseDown:
514 var_Get( p_vout, "mouse-button-down", &val );
516 var_Set( p_vout, "mouse-button-down", val );
521 [super mouseDown: o_event];
526 - (void)otherMouseDown:(NSEvent *)o_event
530 switch( [o_event type] )
532 case NSOtherMouseDown:
534 var_Get( p_vout, "mouse-button-down", &val );
536 var_Set( p_vout, "mouse-button-down", val );
541 [super mouseDown: o_event];
546 - (void)rightMouseDown:(NSEvent *)o_event
550 switch( [o_event type] )
552 case NSRightMouseDown:
554 var_Get( p_vout, "mouse-button-down", &val );
556 var_Set( p_vout, "mouse-button-down", val );
561 [super mouseDown: o_event];
566 - (void)mouseUp:(NSEvent *)o_event
570 switch( [o_event type] )
575 b_val.b_bool = VLC_TRUE;
576 var_Set( p_vout, "mouse-clicked", b_val );
578 var_Get( p_vout, "mouse-button-down", &val );
580 var_Set( p_vout, "mouse-button-down", val );
585 [super mouseUp: o_event];
590 - (void)otherMouseUp:(NSEvent *)o_event
594 switch( [o_event type] )
598 var_Get( p_vout, "mouse-button-down", &val );
600 var_Set( p_vout, "mouse-button-down", val );
605 [super mouseUp: o_event];
610 - (void)rightMouseUp:(NSEvent *)o_event
614 switch( [o_event type] )
618 var_Get( p_vout, "mouse-button-down", &val );
620 var_Set( p_vout, "mouse-button-down", val );
625 [super mouseUp: o_event];
630 - (void)mouseDragged:(NSEvent *)o_event
632 [self mouseMoved: o_event];
635 - (void)otherMouseDragged:(NSEvent *)o_event
637 [self mouseMoved: o_event];
640 - (void)rightMouseDragged:(NSEvent *)o_event
642 [self mouseMoved: o_event];
645 - (void)mouseMoved:(NSEvent *)o_event
652 i_time_mouse_last_moved = mdate();
654 o_view = [self contentView];
655 s_rect = [o_view bounds];
656 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
657 b_inside = [o_view mouse: ml inRect: s_rect];
662 int i_width, i_height, i_x, i_y;
664 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
665 (unsigned int)s_rect.size.height,
666 &i_x, &i_y, &i_width, &i_height );
668 val.i_int = ( ((int)ml.x) - i_x ) *
669 p_vout->render.i_width / i_width;
670 var_Set( p_vout, "mouse-x", val );
672 if( [[o_view className] isEqualToString: @"VLCGLView"] )
674 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
675 p_vout->render.i_height / i_height;
679 val.i_int = ( ((int)ml.y) - i_y ) *
680 p_vout->render.i_height / i_height;
682 var_Set( p_vout, "mouse-y", val );
684 val.b_bool = VLC_TRUE;
685 var_Set( p_vout, "mouse-moved", val );
688 [super mouseMoved: o_event];