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