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