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