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