]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
Removes trailing spaces. Removes tabs.
[vlc] / modules / gui / macosx / vout.m
1 /*****************************************************************************
2  * vout.m: MacOS X video output module
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
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>
12  *          Benjamin Pracht <bigben at videolan dot org>
13  *          Felix K\9fhne <fkuehne at videolan dot org>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <stdlib.h>                                                /* free() */
35 #include <string.h>                                            /* strerror() */
36
37 /* BeginFullScreen, EndFullScreen */
38 #include <QuickTime/QuickTime.h>
39
40 #include <vlc_keys.h>
41
42 #include "intf.h"
43 #include "fspanel.h"
44 #include "vout.h"
45 #import "controls.h"
46 #import "embeddedwindow.h"
47
48 /*****************************************************************************
49  * DeviceCallback: Callback triggered when the video-device variable is changed
50  *****************************************************************************/
51 int DeviceCallback( vlc_object_t *p_this, const char *psz_variable,
52                      vlc_value_t old_val, vlc_value_t new_val, void *param )
53 {
54     vlc_value_t val;
55     vout_thread_t *p_vout = (vout_thread_t *)p_this;
56
57     msg_Dbg( p_vout, "set %d", new_val.i_int );
58     var_Create( p_vout->p_libvlc, "video-device", VLC_VAR_INTEGER );
59     var_Set( p_vout->p_libvlc, "video-device", new_val );
60
61     val.b_bool = VLC_TRUE;
62     var_Set( p_vout, "intf-change", val );
63     return VLC_SUCCESS;
64 }
65
66
67 /*****************************************************************************
68  * VLCEmbeddedList implementation
69  *****************************************************************************/
70 @implementation VLCEmbeddedList
71
72 - (id)init
73 {
74     [super init];
75     o_embedded_array = [NSMutableArray array];
76     return self;
77 }
78
79 - (id)getEmbeddedVout
80 {
81     unsigned int i;
82
83     for( i = 0; i < [o_embedded_array count]; i++ )
84     {
85         id o_vout_view = [o_embedded_array objectAtIndex: i];
86         if( ![o_vout_view isUsed] )
87         {
88             [o_vout_view setUsed: YES];
89             return o_vout_view;
90         }
91     }
92     return nil;
93 }
94
95 - (void)releaseEmbeddedVout: (id)o_vout_view
96 {
97     if( [o_embedded_array containsObject: o_vout_view] )
98     {
99         [o_vout_view setUsed: NO];
100     }
101     else
102     {
103         msg_Warn( VLCIntf, "cannot find Video Output");
104     }
105 }
106
107 - (void)addEmbeddedVout: (id)o_vout_view
108 {
109     if( ![o_embedded_array containsObject: o_vout_view] )
110     {
111         [o_embedded_array addObject: o_vout_view];
112     }
113 }
114
115 - (BOOL)windowContainsEmbedded: (id)o_window
116 {
117 /*    if( ![[o_window className] isEqualToString: @"VLCVoutWindow"] )
118     {
119         NSLog( @"We were not given a VLCVoutWindow" );
120     }*/
121     return ([self getViewForWindow: o_window] == nil ? NO : YES );
122 }
123
124 - (id)getViewForWindow: (id)o_window
125 {
126     id o_enumerator = [o_embedded_array objectEnumerator];
127     id o_current_embedded;
128
129     while( (o_current_embedded = [o_enumerator nextObject]) )
130     {
131         if( [o_current_embedded getWindow] == o_window )
132         {
133             return o_current_embedded;
134         }
135     }
136     return nil;
137 }
138
139 @end
140
141 /*****************************************************************************
142  * VLCVoutView implementation
143  *****************************************************************************/
144 @implementation VLCVoutView
145
146 - (id)initWithFrame:(NSRect)frameRect
147 {
148     [super initWithFrame: frameRect];
149     p_vout = NULL;
150     o_view = nil;
151     s_frame = &frameRect;
152
153     p_real_vout = NULL;
154     o_window = nil;
155     return self;
156 }
157
158 - (BOOL)setVout: (vout_thread_t *) vout subView: (NSView *) view
159                      frame: (NSRect *) frame
160 {
161     int i_device;
162     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
163     NSArray *o_screens = [NSScreen screens];
164
165     p_vout  = vout;
166     o_view  = view;
167     s_frame = frame;
168
169     if( [o_screens count] <= 0 )
170     {
171         msg_Err( p_vout, "no OSX screens available" );
172         return NO;
173     }
174
175     p_real_vout = [VLCVoutView getRealVout: p_vout];
176
177     /* Get the pref value when this is the first time, otherwise retrieve the device from the top level video-device var */
178     if( var_Type( p_real_vout->p_libvlc, "video-device" ) == 0 )
179     {
180         i_device = var_GetInteger( p_vout, "macosx-vdev" );
181     }
182     else
183     {
184         i_device = var_GetInteger( p_real_vout->p_libvlc, "video-device" );
185     }
186
187     /* Setup the menuitem for the multiple displays. */
188     if( var_Type( p_real_vout, "video-device" ) == 0 )
189     {
190         int i = 1;
191         vlc_value_t val2, text;
192         NSScreen * o_screen;
193
194         var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER |
195                                             VLC_VAR_HASCHOICE );
196         text.psz_string = _("Video Device");
197         var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
198
199         NSEnumerator * o_enumerator = [o_screens objectEnumerator];
200
201         val2.i_int = 0;
202         text.psz_string = _("Default");
203         var_Change( p_real_vout, "video-device",
204                         VLC_VAR_ADDCHOICE, &val2, &text );
205         var_Set( p_real_vout, "video-device", val2 );
206
207         while( (o_screen = [o_enumerator nextObject]) != NULL )
208         {
209             char psz_temp[255];
210             NSRect s_rect = [o_screen frame];
211
212             snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
213                       "%s %d (%dx%d)", _("Screen"), i,
214                       (int)s_rect.size.width, (int)s_rect.size.height );
215
216             text.psz_string = psz_temp;
217             val2.i_int = (int)[o_screen displayID];
218             var_Change( p_real_vout, "video-device",
219                         VLC_VAR_ADDCHOICE, &val2, &text );
220             if( (int)[o_screen displayID] == i_device )
221             {
222                 var_Set( p_real_vout, "video-device", val2 );
223             }
224             i++;
225         }
226
227         var_AddCallback( p_real_vout, "video-device", DeviceCallback,
228                          NULL );
229
230         val2.b_bool = VLC_TRUE;
231         var_Set( p_real_vout, "intf-change", val2 );
232     }
233
234     /* Add the view. It's automatically resized to fit the window */
235     [self addSubview: o_view];
236     [self setAutoresizesSubviews: YES];
237     [o_pool release];
238
239     return YES;
240 }
241
242 - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize
243 {
244     [super resizeSubviewsWithOldSize: oldBoundsSize];
245     [o_view setFrameSize: [self frame].size];
246 }
247
248 - (void)drawRect:(NSRect)rect
249 {
250     /* When there is no subview we draw a black background */
251     [self lockFocus];
252     [[NSColor blackColor] set];
253     NSRectFill(rect);
254     [self unlockFocus];
255 }
256
257 - (void)closeVout
258 {
259     [[[[VLCMain sharedInstance] getControls] getFSPanel] fadeOut];
260
261     /* Make sure we don't see a white flash */
262     [[self window] disableScreenUpdatesUntilFlush];
263     [o_view removeFromSuperview];
264     o_view = nil;
265     p_vout = NULL;
266     s_frame = nil;
267     o_window = nil;
268     p_real_vout = NULL;
269 }
270
271 - (void)updateTitle
272 {
273     NSMutableString * o_title = nil, * o_mrl = nil;
274     input_thread_t * p_input;
275
276     if( p_vout == NULL )
277     {
278         return;
279     }
280
281     p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
282
283     if( p_input == NULL )
284     {
285         return;
286     }
287
288     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
289     char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
290     if( psz_name != NULL )
291         o_title = [NSMutableString stringWithUTF8String: psz_name];
292     if( psz_uri != NULL )
293         o_mrl = [NSMutableString stringWithUTF8String: psz_uri];
294     free( psz_name );
295     free( psz_uri );
296     if( o_title == nil )
297         o_title = o_mrl;
298
299     if( o_mrl != nil )
300     {
301         /* FIXME once psz_access is exported, we could check if we are
302          * reading from a file in a smarter way. */
303
304         NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
305         if( prefix_range.location != NSNotFound )
306             [o_mrl deleteCharactersInRange: prefix_range];
307
308         if( [o_mrl characterAtIndex:0] == '/' )
309         {
310             /* it's a local file */
311             [o_window setRepresentedFilename: o_mrl];
312         }
313         else
314         {
315             /* it's from the network or somewhere else,
316              * we clear the previous path */
317             [o_window setRepresentedFilename: @""];
318         }
319         [o_window setTitle: o_title];
320     }
321     else
322     {
323         [o_window setTitle: [NSString stringWithCString: VOUT_TITLE]];
324     }
325     vlc_object_release( p_input );
326 }
327
328
329 - (void)setOnTop:(BOOL)b_on_top
330 {
331     if( b_on_top )
332     {
333         [o_window setLevel: NSStatusWindowLevel];
334     }
335     else
336     {
337         [o_window setLevel: NSNormalWindowLevel];
338     }
339 }
340
341 - (void)scaleWindowWithFactor: (float)factor animate: (BOOL)animate
342 {
343     NSSize newsize;
344     int i_corrected_height, i_corrected_width;
345     NSPoint topleftbase;
346     NSPoint topleftscreen;
347
348     if ( !p_vout->b_fullscreen )
349     {
350         NSView *mainView;
351         NSRect new_frame;
352         topleftbase.x = 0;
353         topleftbase.y = [o_window frame].size.height;
354         topleftscreen = [o_window convertBaseToScreen: topleftbase];
355
356         if( p_vout->render.i_height * p_vout->render.i_aspect >
357                         p_vout->render.i_width * VOUT_ASPECT_FACTOR )
358         {
359             i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
360                                             VOUT_ASPECT_FACTOR;
361             newsize.width = (int) ( i_corrected_width * factor );
362             newsize.height = (int) ( p_vout->render.i_height * factor );
363         }
364         else
365         {
366             i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
367                                             p_vout->render.i_aspect;
368             newsize.width = (int) ( p_vout->render.i_width * factor );
369             newsize.height = (int) ( i_corrected_height * factor );
370         }
371
372         /* In fullscreen mode we need to use a view that is different from
373          * ourselves, with the VLCEmbeddedWindow */
374         if([o_window isKindOfClass:[VLCEmbeddedWindow class]])
375             mainView = [o_window mainView];
376         else
377             mainView = self;
378
379         /* Calculate the window's new size */
380         new_frame.size.width = [o_window frame].size.width -
381                                     [mainView frame].size.width + newsize.width;
382         new_frame.size.height = [o_window frame].size.height -
383                                     [mainView frame].size.height + newsize.height;
384
385         new_frame.origin.x = topleftscreen.x;
386         new_frame.origin.y = topleftscreen.y - new_frame.size.height;
387
388         [o_window setFrame: new_frame display: animate animate: animate];
389
390         p_vout->i_changes |= VOUT_SIZE_CHANGE;
391     }
392 }
393
394 - (void)toggleFloatOnTop
395 {
396     vlc_value_t val;
397
398     if( !p_real_vout ) return;
399     if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
400     {
401         val.b_bool = VLC_FALSE;
402     }
403     else
404     {
405         val.b_bool = VLC_TRUE;
406     }
407     var_Set( p_real_vout, "video-on-top", val );
408 }
409
410 - (void)toggleFullscreen
411 {
412     vlc_value_t val;
413     if( !p_real_vout ) return;
414     var_Get( p_real_vout, "fullscreen", &val );
415     val.b_bool = !val.b_bool;
416     var_Set( p_real_vout, "fullscreen", val );
417 }
418
419 - (BOOL)isFullscreen
420 {
421     vlc_value_t val;
422     if( !p_real_vout ) return NO;
423     var_Get( p_real_vout, "fullscreen", &val );
424     return( val.b_bool );
425 }
426
427 - (void)snapshot
428 {
429     vout_Control( p_real_vout, VOUT_SNAPSHOT );
430 }
431
432 - (void)manage
433 {
434     /* Disable Screensaver, when we're playing something, but allow it on pause */
435     if( VLCIntf->p_sys->i_play_status == PLAYING_S )
436         UpdateSystemActivity( UsrActivity );
437 }
438
439 - (id)getWindow
440 {
441     return o_window;
442 }
443
444 - (void)scrollWheel:(NSEvent *)theEvent
445 {
446     VLCControls * o_controls = (VLCControls *)[[NSApp delegate] getControls];
447     [o_controls scrollWheel: theEvent];
448 }
449
450 - (void)keyDown:(NSEvent *)o_event
451 {
452     unichar key = 0;
453     vlc_value_t val;
454     unsigned int i_pressed_modifiers = 0;
455     val.i_int = 0;
456
457     i_pressed_modifiers = [o_event modifierFlags];
458
459     if( i_pressed_modifiers & NSShiftKeyMask )
460         val.i_int |= KEY_MODIFIER_SHIFT;
461     if( i_pressed_modifiers & NSControlKeyMask )
462         val.i_int |= KEY_MODIFIER_CTRL;
463     if( i_pressed_modifiers & NSAlternateKeyMask )
464         val.i_int |= KEY_MODIFIER_ALT;
465     if( i_pressed_modifiers & NSCommandKeyMask )
466         val.i_int |= KEY_MODIFIER_COMMAND;
467
468     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
469
470     if( key )
471     {
472         /* Escape should always get you out of fullscreen */
473         if( key == (unichar) 0x1b )
474         {
475              if( p_real_vout && [self isFullscreen] )
476              {
477                  [self toggleFullscreen];
478              }
479         }
480         else if ( key == ' ' )
481         {
482             vlc_value_t val;
483             val.i_int = config_GetInt( p_vout, "key-play-pause" );
484             var_Set( p_vout->p_libvlc, "key-pressed", val );
485         }
486         else
487         {
488             val.i_int |= CocoaKeyToVLC( key );
489             var_Set( p_vout->p_libvlc, "key-pressed", val );
490         }
491     }
492     else
493     {
494         [super keyDown: o_event];
495     }
496 }
497
498 - (void)mouseDown:(NSEvent *)o_event
499 {
500     vlc_value_t val;
501
502     if( p_vout )
503     {
504         if( ( [o_event type] == NSLeftMouseDown ) &&
505           ( ! ( [o_event modifierFlags] &  NSControlKeyMask ) ) )
506         {
507             if( [o_event clickCount] <= 1 )
508             {
509                 /* single clicking */
510                 var_Get( p_vout, "mouse-button-down", &val );
511                 val.i_int |= 1;
512                 var_Set( p_vout, "mouse-button-down", val );
513             }
514             else
515             {
516                 /* multiple clicking */
517                 [self toggleFullscreen];
518             }
519         }
520         else if( ( [o_event type] == NSRightMouseDown ) ||
521                ( ( [o_event type] == NSLeftMouseDown ) &&
522                  ( [o_event modifierFlags] &  NSControlKeyMask ) ) )
523         {
524             msg_Dbg( p_vout, "received NSRightMouseDown (generic method) or Ctrl clic" );
525             [NSMenu popUpContextMenu: [[VLCMain sharedInstance] getVoutMenu] withEvent: o_event forView: [[[VLCMain sharedInstance] getControls] getVoutView]];
526         }
527     }
528
529     [super mouseDown: o_event];
530 }
531
532 - (void)otherMouseDown:(NSEvent *)o_event
533 {
534     vlc_value_t val;
535
536     if( p_vout && [o_event type] == NSOtherMouseDown )
537     {
538         var_Get( p_vout, "mouse-button-down", &val );
539         val.i_int |= 2;
540         var_Set( p_vout, "mouse-button-down", val );
541     }
542
543     [super mouseDown: o_event];
544 }
545
546 - (void)rightMouseDown:(NSEvent *)o_event
547 {
548     if( p_vout && [o_event type] == NSRightMouseDown )
549     {
550         msg_Dbg( p_vout, "received NSRightMouseDown (specific method)" );
551         [NSMenu popUpContextMenu: [[VLCMain sharedInstance] getVoutMenu] withEvent: o_event forView: [[[VLCMain sharedInstance] getControls] getVoutView]];
552     }
553
554     [super mouseDown: o_event];
555 }
556
557 - (void)mouseUp:(NSEvent *)o_event
558 {
559     vlc_value_t val;
560
561     if( p_vout && [o_event type] == NSLeftMouseUp )
562     {
563         vlc_value_t b_val;
564         b_val.b_bool = VLC_TRUE;
565         var_Set( p_vout, "mouse-clicked", b_val );
566
567         var_Get( p_vout, "mouse-button-down", &val );
568         val.i_int &= ~1;
569         var_Set( p_vout, "mouse-button-down", val );
570     }
571
572     [super mouseUp: o_event];
573 }
574
575 - (void)otherMouseUp:(NSEvent *)o_event
576 {
577     vlc_value_t val;
578
579     if( p_vout && [o_event type] == NSOtherMouseUp )
580     {
581         var_Get( p_vout, "mouse-button-down", &val );
582         val.i_int &= ~2;
583         var_Set( p_vout, "mouse-button-down", val );
584     }
585
586     [super mouseUp: o_event];
587 }
588
589 - (void)rightMouseUp:(NSEvent *)o_event
590 {
591     if( p_vout && [o_event type] == NSRightMouseUp )
592     {
593         /* FIXME: this isn't the appropriate place, but we can't receive
594          * NSRightMouseDown some how */
595         msg_Dbg( p_vout, "received NSRightMouseUp" );
596         [NSMenu popUpContextMenu: [[VLCMain sharedInstance] getVoutMenu] withEvent: o_event forView: [[[VLCMain sharedInstance] getControls] getVoutView]];
597     }
598
599     [super mouseUp: o_event];
600 }
601
602 - (void)mouseDragged:(NSEvent *)o_event
603 {
604     [self mouseMoved: o_event];
605 }
606
607 - (void)otherMouseDragged:(NSEvent *)o_event
608 {
609     [self mouseMoved: o_event];
610 }
611
612 - (void)rightMouseDragged:(NSEvent *)o_event
613 {
614     [self mouseMoved: o_event];
615 }
616
617 - (void)mouseMoved:(NSEvent *)o_event
618 {
619     NSPoint ml;
620     NSRect s_rect;
621     BOOL b_inside;
622
623     if( p_vout )
624     {
625         s_rect = [o_view bounds];
626         ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
627         b_inside = [o_view mouse: ml inRect: s_rect];
628
629         if( b_inside )
630         {
631             vlc_value_t val;
632             unsigned int i_width, i_height, i_x, i_y;
633
634             vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
635                                        (unsigned int)s_rect.size.height,
636                                        &i_x, &i_y, &i_width, &i_height );
637
638             val.i_int = ( ((int)ml.x) - i_x ) *
639                         p_vout->render.i_width / i_width;
640             var_Set( p_vout, "mouse-x", val );
641
642             if( [[o_view className] isEqualToString: @"VLCGLView"] )
643             {
644                 val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
645                             p_vout->render.i_height / i_height;
646             }
647             else
648             {
649                 val.i_int = ( ((int)ml.y) - i_y ) *
650                             p_vout->render.i_height / i_height;
651             }
652             var_Set( p_vout, "mouse-y", val );
653
654             val.b_bool = VLC_TRUE;
655             var_Set( p_vout, "mouse-moved", val );
656         }
657         if( [self isFullscreen] )
658             [[[[VLCMain sharedInstance] getControls] getFSPanel] fadeIn];
659     }
660
661     [super mouseMoved: o_event];
662 }
663
664 - (BOOL)acceptsFirstResponder
665 {
666     return YES;
667 }
668
669 - (BOOL)becomeFirstResponder
670 {
671     return YES;
672 }
673
674 - (BOOL)resignFirstResponder
675 {
676     /* We need to stay the first responder or we'll miss some
677        events */
678     return NO;
679 }
680
681 /* Class methods used by the different vout modules */
682
683 + (vout_thread_t *)getRealVout: (vout_thread_t *)p_vout
684 {
685     /* p_real_vout: the vout we have to use to check for video-on-top
686        and a few other things. If we are the QuickTime output, it's us.
687        It we are the OpenGL provider, it is our parent. */
688     if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
689     {
690         return (vout_thread_t *) p_vout->p_parent;
691     }
692     else
693     {
694         return p_vout;
695     }
696
697 }
698
699 + (id)getVoutView: (vout_thread_t *)p_vout subView: (NSView *)view
700                                     frame: (NSRect *)s_frame
701 {
702     vlc_value_t value_drawable;
703     int i_timeout;
704     id o_return = nil;
705
706     var_Get( p_vout->p_libvlc, "drawable", &value_drawable );
707
708     var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
709     var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
710     var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
711     var_Create( p_vout, "macosx-background", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
712     var_Create( p_vout, "macosx-black", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
713     var_Create( p_vout, "macosx-embedded", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
714
715
716     /* We only wait for NSApp to initialise if we're not embedded (as in the
717      * case of the Mozilla plugin).  We can tell whether we're embedded or not
718      * by examining the "drawable" value: if it's zero, we're running in the
719      * main Mac intf; if it's non-zero, we're embedded. */
720     if( value_drawable.i_int == 0 )
721     {
722         /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
723         for( i_timeout = 20 ; i_timeout-- ; )
724         {
725             if( NSApp == NULL )
726             {
727                 msleep( INTF_IDLE_SLEEP );
728             }
729         }
730
731         if( NSApp == NULL )
732         {
733             /* No MacOS X intf, unable to communicate with MT */
734             msg_Err( p_vout, "no MacOS X interface present" );
735             return nil;
736         }
737         else
738         {
739             if ( VLCIntf && !(p_vout->b_fullscreen) &&
740                         !(var_GetBool( p_vout, "macosx-background" )) &&
741                         var_GetBool( p_vout, "macosx-embedded") )
742             {
743                 o_return = [[[VLCMain sharedInstance] getEmbeddedList]
744                                                             getEmbeddedVout];
745             }
746         }
747     }
748
749     /* No embedded vout is available */
750     if( o_return == nil )
751     {
752         NSRect null_rect;
753         bzero( &null_rect, sizeof( NSRect ) );
754         o_return = [[VLCDetachedVoutView alloc] initWithFrame: null_rect ];
755     }
756     [o_return setVout: p_vout subView: view frame: s_frame];
757     return o_return;
758 }
759
760 - (void)enterFullscreen
761 {
762     [[o_view class] performSelectorOnMainThread:@selector(resetVout:) withObject:[NSValue valueWithPointer:p_vout] waitUntilDone:YES];
763     [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil];
764 }
765
766 - (void)leaveFullscreen
767 {
768     [[o_view class] performSelectorOnMainThread:@selector(resetVout:) withObject:[NSValue valueWithPointer:p_vout] waitUntilDone:YES];
769     [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
770 }
771
772 @end
773
774 /*****************************************************************************
775  * VLCDetachedVoutView implementation
776  *****************************************************************************/
777 @implementation VLCDetachedVoutView
778
779 - (id)initWithFrame: (NSRect)frameRect
780 {
781     [super initWithFrame: frameRect];
782     i_time_mouse_last_moved = 0;
783     return self;
784 }
785
786 - (bool)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view
787                      frame: (NSRect *) s_arg_frame
788 {
789     BOOL b_return = [super setVout: p_arg_vout subView: view frame:s_arg_frame];
790     i_time_mouse_last_moved = mdate();
791     o_window = [[VLCVoutWindow alloc] initWithVout: p_arg_vout view: self
792                                                     frame: s_arg_frame];
793     [self updateTitle];
794     [view setFrame: [self frame]];
795
796     if( var_GetBool( p_real_vout, "video-on-top" ) )
797     {
798         [o_window setLevel: NSStatusWindowLevel];
799     }
800
801
802     [o_window setAcceptsMouseMovedEvents: TRUE];
803     return b_return;
804 }
805
806 - (void)closeVout
807 {
808     [o_window closeWindow];
809     [o_window setAcceptsMouseMovedEvents: NO];
810     i_time_mouse_last_moved = 0;
811     [super closeVout];
812 }
813
814 - (void)mouseMoved:(NSEvent *)o_event
815 {
816     i_time_mouse_last_moved = mdate();
817     [super mouseMoved: o_event];
818 }
819
820 - (void)hideMouse:(BOOL)b_hide
821 {
822     BOOL b_inside;
823     NSPoint ml;
824     NSView *o_contents = [o_window contentView];
825
826     ml = [o_window convertScreenToBase:[NSEvent mouseLocation]];
827     ml = [o_contents convertPoint:ml fromView:nil];
828     b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
829
830     if( b_hide && b_inside )
831     {
832         [NSCursor setHiddenUntilMouseMoves: YES];
833     }
834     else if( !b_hide )
835     {
836         [NSCursor setHiddenUntilMouseMoves: NO];
837     }
838 }
839
840 - (void)manage
841 {
842     [super manage];
843     if( p_vout->b_fullscreen )
844     {
845         if( mdate() - i_time_mouse_last_moved > 3000000 )
846         {
847             [self hideMouse: YES];
848         }
849     }
850     else
851     {
852         [self hideMouse: NO];
853     }
854 }
855
856 @end
857
858 /*****************************************************************************
859  * VLCEmbeddedVoutView implementation
860  *****************************************************************************/
861
862 @implementation VLCEmbeddedVoutView
863
864 - (id)initWithFrame: (NSRect)frameRect
865 {
866     [super initWithFrame: frameRect];
867     b_used = NO;
868     [[[VLCMain sharedInstance] getEmbeddedList] addEmbeddedVout: self];
869     return self;
870 }
871
872 - (BOOL)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view
873                  frame: (NSRect *)s_arg_frame showWindow: (BOOL)b_show_window
874 {
875     BOOL b_return;
876     b_return = [super setVout: p_arg_vout subView: view frame: s_arg_frame];
877     if( b_return )
878     {
879         o_window = [self window];
880         if (b_show_window)
881             [o_window makeKeyAndOrderFront: self];
882         [o_window setAcceptsMouseMovedEvents: TRUE];
883
884         if( var_GetBool( p_real_vout, "video-on-top" ) )
885         {
886             [o_window setLevel: NSStatusWindowLevel];
887         }
888
889         [view setFrameSize: [self frame].size];
890     }
891     return b_return;
892 }
893
894 - (BOOL)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view
895                      frame: (NSRect *) s_arg_frame
896
897 {
898     return [self setVout: p_arg_vout subView: view frame:s_arg_frame showWindow: YES];
899 }
900
901 - (void)setUsed: (BOOL)b_new_used
902 {
903     b_used = b_new_used;
904 }
905
906 - (BOOL)isUsed
907 {
908     return b_used;
909 }
910
911 - (void)closeVout
912 {
913     [super closeVout];
914     [o_window setAcceptsMouseMovedEvents: NO];
915     [[[VLCMain sharedInstance] getEmbeddedList] releaseEmbeddedVout: self];
916 }
917
918
919 @end
920
921 @implementation VLCDetachedEmbeddedVoutView
922 - (void)awakeFromNib
923 {
924     o_embeddedwindow = [self window];
925 }
926
927 - (BOOL)setVout: (vout_thread_t *) p_arg_vout subView: (NSView *) view
928                      frame: (NSRect *) s_arg_frame
929 {
930     BOOL b_return = [super setVout: p_arg_vout subView: view frame: s_arg_frame showWindow: NO];
931
932     /* o_window needs to point to our o_embeddedwindow, super might have set it
933      * to the fullscreen window that o_embeddedwindow setups during fullscreen */
934     o_window = o_embeddedwindow;
935  
936     if( b_return )
937     {
938         [o_window lockFullscreenAnimation];
939
940         [o_window setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
941
942         [self updateTitle];
943
944         /* Make the window the front and key window before animating */
945         if ([o_window isVisible] && (![o_window isFullscreen]))
946             [o_window makeKeyAndOrderFront: self];
947
948         [self scaleWindowWithFactor: 1.0 animate: [o_window isVisible] && (![o_window isFullscreen])];
949
950         [o_window setAspectRatio:NSMakeSize([o_window frame].size.width, [o_window frame].size.height)];
951
952         /* Make sure our window is visible, if we are not in fullscreen */
953         if (![o_window isFullscreen])
954             [o_window makeKeyAndOrderFront: self];
955         [o_window unlockFullscreenAnimation];
956
957     }
958     return b_return;
959 }
960
961 - (void)closeVout
962 {
963     playlist_t * p_playlist = pl_Yield( VLCIntf );
964
965     if(!playlist_IsPlaying( p_playlist ))
966         [o_window performSelectorOnMainThread: @selector(orderOut:) withObject: self waitUntilDone: YES];
967  
968     vlc_object_release( p_playlist );
969
970     [super closeVout];
971 }
972
973 - (void)enterFullscreen
974 {
975     /* We are in a VLCEmbeddedWindow */
976     [o_embeddedwindow performSelectorOnMainThread: @selector(enterFullscreen) withObject: NULL waitUntilDone: YES];
977 }
978
979 - (void)leaveFullscreen
980 {
981     /* We are in a VLCEmbeddedWindow */
982     [o_embeddedwindow performSelectorOnMainThread: @selector(leaveFullscreen) withObject: NULL waitUntilDone: YES];
983 }
984 @end
985
986 /*****************************************************************************
987  * VLCVoutWindow implementation
988  *****************************************************************************/
989 @implementation VLCVoutWindow
990
991 - (id) initWithVout: (vout_thread_t *) vout view: (VLCVoutView *) view
992                      frame: (NSRect *) frame
993 {
994     p_vout  = vout;
995     o_view  = view;
996     s_frame = frame;
997
998     [self performSelectorOnMainThread: @selector(initReal:)
999         withObject: NULL waitUntilDone: YES];
1000
1001     if( !b_init_ok )
1002     {
1003         return NULL;
1004     }
1005
1006     return self;
1007 }
1008
1009 - (id)initReal: (id) sender
1010 {
1011     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
1012     NSArray *o_screens = [NSScreen screens];
1013     NSScreen *o_screen;
1014     vlc_bool_t b_menubar_screen = VLC_FALSE;
1015     int i_device;
1016
1017     b_init_ok = VLC_FALSE;
1018
1019     p_real_vout = [VLCVoutView getRealVout: p_vout];
1020     i_device = var_GetInteger( p_real_vout->p_libvlc, "video-device" );
1021     b_black = NO;
1022     b_embedded = var_GetBool( p_vout, "macosx-embedded" );
1023
1024     /* Find out on which screen to open the window */
1025     o_screen = [NSScreen screenWithDisplayID: (CGDirectDisplayID)i_device];
1026
1027     if( !o_screen )
1028         o_screen = [NSScreen mainScreen];
1029
1030     if( [o_screen isMainScreen] )
1031         b_menubar_screen = VLC_TRUE;
1032
1033     if( p_vout->b_fullscreen )
1034     {
1035         CGDisplayFadeReservationToken token;
1036         NSRect screen_rect = [o_screen frame];
1037         screen_rect.origin.x = screen_rect.origin.y = 0;
1038
1039         b_black = var_GetBool( p_vout, "macosx-black" );
1040
1041         /* move the FSPanel to front in case that it is currently shown
1042          * this won't and is not supposed to work when it's fading right now */
1043         if( [[[[VLCMain sharedInstance] getControls] getFSPanel] isDisplayed] )
1044             [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil];
1045  
1046         /* tell the fspanel to move itself to front next time it's triggered */
1047         [[[[VLCMain sharedInstance] getControls] getFSPanel] setVoutWasUpdated: i_device];
1048
1049         /* Creates a window with size: screen_rect on o_screen */
1050         [self initWithContentRect: screen_rect
1051               styleMask: NSBorderlessWindowMask
1052               backing: NSBackingStoreBuffered
1053               defer: YES screen: o_screen];
1054  
1055         if( b_menubar_screen )
1056         {
1057             SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
1058         }
1059         if( b_black == VLC_TRUE )
1060         {
1061             CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
1062             CGDisplayFade( token, 0.6 , kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
1063
1064             [o_screen blackoutOtherScreens];
1065
1066             CGDisplayFade( token, 0.3 , kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
1067             CGReleaseDisplayFadeReservation( token);
1068         }
1069     }
1070     else if( var_GetBool( p_vout, "macosx-background" ) )
1071     {
1072         NSRect screen_rect = [o_screen frame];
1073         screen_rect.origin.x = screen_rect.origin.y = 0;
1074
1075         /* Creates a window with size: screen_rect on o_screen */
1076         [self initWithContentRect: screen_rect
1077               styleMask: NSBorderlessWindowMask
1078               backing: NSBackingStoreBuffered
1079               defer: YES screen: o_screen];
1080
1081         [self setLevel: CGWindowLevelForKey(kCGDesktopWindowLevelKey)];
1082     }
1083     else
1084     {
1085         unsigned int i_stylemask = NSTitledWindowMask |
1086                                    NSMiniaturizableWindowMask |
1087                                    NSClosableWindowMask |
1088                                    NSResizableWindowMask;
1089
1090         NSRect s_rect;
1091         if( !s_frame )
1092         {
1093             s_rect.size.width  = p_vout->i_window_width;
1094             s_rect.size.height = p_vout->i_window_height;
1095         }
1096         else
1097         {
1098             s_rect = *s_frame;
1099         }
1100
1101         [self initWithContentRect: s_rect
1102               styleMask: i_stylemask
1103               backing: NSBackingStoreBuffered
1104               defer: YES screen: o_screen];
1105
1106         [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
1107
1108         if( !s_frame )
1109         {
1110             [self center];
1111         }
1112     }
1113
1114     [self makeKeyAndOrderFront: nil];
1115     [self setReleasedWhenClosed: YES];
1116
1117     /* We'll catch mouse events */
1118     [self makeFirstResponder: o_view];
1119
1120     /* Add the view. It's automatically resized to fit the window */
1121     [self setContentView: o_view];
1122
1123     [o_pool release];
1124
1125     b_init_ok = VLC_TRUE;
1126     return self;
1127 }
1128
1129 - (void)close
1130 {
1131     [o_view closeVout];
1132 }
1133
1134 - (void)closeWindow
1135 {
1136     /* XXX waitUntilDone = NO to avoid a possible deadlock when hitting
1137        Command-Q */
1138     [self performSelectorOnMainThread: @selector(closeReal:)
1139         withObject: NULL waitUntilDone: NO];
1140 }
1141
1142 - (id)closeReal:(id)sender
1143 {
1144     if( b_black == VLC_TRUE )
1145     {
1146         CGDisplayFadeReservationToken token;
1147         CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
1148         CGDisplayFade( token, 0.3 , kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
1149  
1150         [self disableScreenUpdatesUntilFlush];
1151         [self orderOut: self];
1152
1153         CGDisplayFade( token, 0.6 , kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, YES );
1154         CGReleaseDisplayFadeReservation( token);
1155         CGDisplayRestoreColorSyncSettings();
1156     }
1157     [NSScreen unblackoutScreens];
1158
1159     SetSystemUIMode( kUIModeNormal, 0);
1160     [super close];
1161     /* this does only work in embedded mode */
1162     if( b_embedded == VLC_TRUE )
1163         [[[[VLCMain sharedInstance] getControls] getFSPanel] orderOut: self];
1164
1165     return NULL;
1166 }
1167
1168 - (id)getVoutView
1169 {
1170     return o_view;
1171 }
1172
1173 - (BOOL)canBecomeKeyWindow
1174 {
1175     return YES;
1176 }
1177
1178 /* Sometimes crashes VLC....
1179 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
1180 {
1181         return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
1182 }*/
1183
1184 /* This is actually the same as VLCControls::stop. */
1185
1186 - (BOOL)windowShouldClose:(id)sender
1187 {
1188     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
1189                                                        FIND_ANYWHERE );
1190     if( p_playlist == NULL )
1191     {
1192         return NO;
1193     }
1194
1195     playlist_Stop( p_playlist );
1196     vlc_object_release( p_playlist );
1197
1198     /* The window will be closed by the intf later. */
1199     return NO;
1200 }
1201
1202
1203 @end