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