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