1 /*****************************************************************************
2 * vout.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2001-2003 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
50 [self setReleasedWhenClosed: YES];
53 p_fullscreen_state = NULL;
54 i_time_mouse_last_moved = mdate();
57 vlc_bool_t b_main_screen;
59 /* Find out on which screen to open the window */
60 int i_device = var_GetInteger( p_vout, "video-device" );
63 /* No preference specified. Use the main screen */
64 o_screen = [NSScreen mainScreen];
69 NSArray *o_screens = [NSScreen screens];
71 if( [o_screens count] < (unsigned) i_device )
73 o_screen = [NSScreen mainScreen];
79 o_screen = [o_screens objectAtIndex: i_device];
80 var_SetInteger( p_vout, "macosx-vdev", i_device );
81 b_main_screen = ( i_device == 0 );
85 NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
87 if( p_vout->b_fullscreen )
89 NSRect screen_rect = [o_screen frame];
90 screen_rect.origin.x = screen_rect.origin.y = 0;
92 /* Creates a window with size: screen_rect on o_screen */
93 [self initWithContentRect: screen_rect
94 styleMask: NSBorderlessWindowMask
95 backing: NSBackingStoreBuffered
96 defer: YES screen: o_screen];
100 BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
101 NULL, NULL, fullScreenAllowEvents );
106 unsigned int i_stylemask = NSTitledWindowMask |
107 NSMiniaturizableWindowMask |
108 NSClosableWindowMask |
109 NSResizableWindowMask;
114 s_rect.size.width = p_vout->i_window_width;
115 s_rect.size.height = p_vout->i_window_height;
122 [self initWithContentRect: s_rect
123 styleMask: i_stylemask
124 backing: NSBackingStoreBuffered
125 defer: YES screen: o_screen];
127 [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
129 if( var_GetBool( p_vout, "video-on-top" ) )
131 [self setLevel: NSStatusWindowLevel];
141 [self makeKeyAndOrderFront: nil];
143 /* We'll catch mouse events */
144 [self setAcceptsMouseMovedEvents: YES];
145 [self makeFirstResponder: self];
153 if( p_fullscreen_state )
155 EndFullScreen( p_fullscreen_state, NULL );
160 - (void)setOnTop:(bool)b_on_top
164 [self setLevel: NSStatusWindowLevel];
168 [self setLevel: NSNormalWindowLevel];
172 - (void)hideMouse:(bool)b_hide
176 NSView *o_contents = [self contentView];
178 ml = [self convertScreenToBase:[NSEvent mouseLocation]];
179 ml = [o_contents convertPoint:ml fromView:nil];
180 b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
182 if( b_hide && b_inside )
184 [NSCursor setHiddenUntilMouseMoves: YES];
188 [NSCursor setHiddenUntilMouseMoves: NO];
194 if( p_fullscreen_state )
196 if( mdate() - i_time_mouse_last_moved > 3000000 )
198 [self hideMouse: YES];
203 [self hideMouse: NO];
206 /* Disable screensaver */
207 UpdateSystemActivity( UsrActivity );
210 - (void)scaleWindowWithFactor: (float)factor
213 int i_corrected_height, i_corrected_width;
215 NSPoint topleftscreen;
217 if ( !p_vout->b_fullscreen )
220 topleftbase.y = [self frame].size.height;
221 topleftscreen = [self convertBaseToScreen: topleftbase];
223 if( p_vout->render.i_height * p_vout->render.i_aspect >
224 p_vout->render.i_width * VOUT_ASPECT_FACTOR )
226 i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
228 newsize.width = (int) ( i_corrected_width * factor );
229 newsize.height = (int) ( p_vout->render.i_height * factor );
233 i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
234 p_vout->render.i_aspect;
235 newsize.width = (int) ( p_vout->render.i_width * factor );
236 newsize.height = (int) ( i_corrected_height * factor );
239 [self setContentSize: newsize];
241 [self setFrameTopLeftPoint: topleftscreen];
242 p_vout->i_changes |= VOUT_SIZE_CHANGE;
246 - (void)toggleFloatOnTop
249 if( var_Get( p_vout, "video-on-top", &val )>=0 && val.b_bool)
251 val.b_bool = VLC_FALSE;
252 var_Set( p_vout, "video-on-top", val );
256 val.b_bool = VLC_TRUE;
257 var_Set( p_vout, "video-on-top", val );
261 - (void)toggleFullscreen
264 val.b_bool = !p_vout->b_fullscreen;
265 var_Set( p_vout, "fullscreen", val );
270 return( p_vout->b_fullscreen );
273 - (BOOL)canBecomeKeyWindow
278 /* Sometimes crashes VLC....
279 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
281 return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
284 - (void)keyDown:(NSEvent *)o_event
288 unsigned int i_pressed_modifiers = 0;
291 i_pressed_modifiers = [o_event modifierFlags];
293 if( i_pressed_modifiers & NSShiftKeyMask )
294 val.i_int |= KEY_MODIFIER_SHIFT;
295 if( i_pressed_modifiers & NSControlKeyMask )
296 val.i_int |= KEY_MODIFIER_CTRL;
297 if( i_pressed_modifiers & NSAlternateKeyMask )
298 val.i_int |= KEY_MODIFIER_ALT;
299 if( i_pressed_modifiers & NSCommandKeyMask )
300 val.i_int |= KEY_MODIFIER_COMMAND;
302 key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
306 /* Escape should always get you out of fullscreen */
307 if( key == (unichar) 0x1b )
309 if( [self isFullscreen] )
311 [self toggleFullscreen];
314 else if ( key == ' ' )
317 val.i_int = config_GetInt( p_vout, "key-play-pause" );
318 var_Set( p_vout->p_vlc, "key-pressed", val );
322 val.i_int |= CocoaKeyToVLC( key );
323 var_Set( p_vout->p_vlc, "key-pressed", val );
328 [super keyDown: o_event];
334 NSMutableString * o_title;
335 playlist_t * p_playlist;
342 p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
345 if( p_playlist == NULL )
350 vlc_mutex_lock( &p_playlist->object_lock );
351 o_title = [NSMutableString stringWithUTF8String:
352 p_playlist->p_input.psz_uri];
353 vlc_mutex_unlock( &p_playlist->object_lock );
354 vlc_object_release( p_playlist );
358 NSRange prefix_range = [o_title rangeOfString: @"file:"];
359 if( prefix_range.location != NSNotFound )
361 [o_title deleteCharactersInRange: prefix_range];
364 [self setTitleWithRepresentedFilename: o_title];
368 [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
372 /* This is actually the same as VLCControls::stop. */
373 - (BOOL)windowShouldClose:(id)sender
375 playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
377 if( p_playlist == NULL )
382 playlist_Stop( p_playlist );
383 vlc_object_release( p_playlist );
385 /* The window will be closed by the intf later. */
389 - (BOOL)acceptsFirstResponder
394 - (BOOL)becomeFirstResponder
399 - (BOOL)resignFirstResponder
401 /* We need to stay the first responder or we'll miss some
406 - (void)mouseDown:(NSEvent *)o_event
410 switch( [o_event type] )
412 case NSLeftMouseDown:
414 var_Get( p_vout, "mouse-button-down", &val );
416 var_Set( p_vout, "mouse-button-down", val );
421 [super mouseDown: o_event];
426 - (void)otherMouseDown:(NSEvent *)o_event
430 switch( [o_event type] )
432 case NSOtherMouseDown:
434 var_Get( p_vout, "mouse-button-down", &val );
436 var_Set( p_vout, "mouse-button-down", val );
441 [super mouseDown: o_event];
446 - (void)rightMouseDown:(NSEvent *)o_event
450 switch( [o_event type] )
452 case NSRightMouseDown:
454 var_Get( p_vout, "mouse-button-down", &val );
456 var_Set( p_vout, "mouse-button-down", val );
461 [super mouseDown: o_event];
466 - (void)mouseUp:(NSEvent *)o_event
470 switch( [o_event type] )
475 b_val.b_bool = VLC_TRUE;
476 var_Set( p_vout, "mouse-clicked", b_val );
478 var_Get( p_vout, "mouse-button-down", &val );
480 var_Set( p_vout, "mouse-button-down", val );
485 [super mouseUp: o_event];
490 - (void)otherMouseUp:(NSEvent *)o_event
494 switch( [o_event type] )
498 var_Get( p_vout, "mouse-button-down", &val );
500 var_Set( p_vout, "mouse-button-down", val );
505 [super mouseUp: o_event];
510 - (void)rightMouseUp:(NSEvent *)o_event
514 switch( [o_event type] )
518 var_Get( p_vout, "mouse-button-down", &val );
520 var_Set( p_vout, "mouse-button-down", val );
525 [super mouseUp: o_event];
530 - (void)mouseDragged:(NSEvent *)o_event
532 [self mouseMoved: o_event];
535 - (void)otherMouseDragged:(NSEvent *)o_event
537 [self mouseMoved: o_event];
540 - (void)rightMouseDragged:(NSEvent *)o_event
542 [self mouseMoved: o_event];
545 - (void)mouseMoved:(NSEvent *)o_event
552 i_time_mouse_last_moved = mdate();
554 o_view = [self contentView];
555 s_rect = [o_view bounds];
556 ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
557 b_inside = [o_view mouse: ml inRect: s_rect];
562 int i_width, i_height, i_x, i_y;
564 vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
565 (unsigned int)s_rect.size.height,
566 &i_x, &i_y, &i_width, &i_height );
568 val.i_int = ( ((int)ml.x) - i_x ) *
569 p_vout->render.i_width / i_width;
570 var_Set( p_vout, "mouse-x", val );
572 if( [[o_view className] isEqualToString: @"VLCGLView"] )
574 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
575 p_vout->render.i_height / i_height;
579 val.i_int = ( ((int)ml.y) - i_y ) *
580 p_vout->render.i_height / i_height;
582 var_Set( p_vout, "mouse-y", val );
584 val.b_bool = VLC_TRUE;
585 var_Set( p_vout, "mouse-moved", val );
588 [super mouseMoved: o_event];