]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
* fixed fullscreen on osx with multiple vouts.
[vlc] / modules / gui / macosx / vout.m
1 /*****************************************************************************
2  * vout.m: MacOS X video output module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 VideoLAN
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  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <errno.h>                                                 /* ENOMEM */
32 #include <stdlib.h>                                                /* free() */
33 #include <string.h>                                            /* strerror() */
34
35 /* BeginFullScreen, EndFullScreen */
36 #include <QuickTime/QuickTime.h>
37
38 #include <vlc_keys.h>
39
40 #include "intf.h"
41 #include "vout.h"
42
43
44 /*****************************************************************************
45  * DeviceCallback: Callback triggered when the video-device variable is changed
46  *****************************************************************************/
47 int DeviceCallback( vlc_object_t *p_this, const char *psz_variable,
48                      vlc_value_t old_val, vlc_value_t new_val, void *param )
49 {
50     vlc_value_t val;
51     vout_thread_t *p_vout = (vout_thread_t *)p_this;
52     
53     msg_Dbg( p_vout, "set %d", new_val.i_int );
54     var_Create( p_vout->p_vlc, "video-device", VLC_VAR_INTEGER );
55     var_Set( p_vout->p_vlc, "video-device", new_val );
56     
57     val.b_bool = VLC_TRUE;
58     var_Set( p_vout, "intf-change", val );
59     return VLC_SUCCESS;
60 }
61
62
63 /*****************************************************************************
64  * VLCWindow implementation
65  *****************************************************************************/
66 @implementation VLCWindow
67
68 - (id)initWithVout:(vout_thread_t *)_p_vout frame:(NSRect *)s_frame
69 {
70     NSAutoreleasePool *o_pool;
71     NSArray *o_screens = [NSScreen screens];
72     NSScreen *o_screen;
73     vlc_bool_t b_menubar_screen = VLC_FALSE;
74     int i_timeout, i_device;
75     vlc_value_t value_drawable;
76
77     p_vout = _p_vout;
78
79     var_Get( p_vout->p_vlc, "drawable", &value_drawable );
80
81     /* We only wait for NSApp to initialise if we're not embedded (as in the
82      * case of the Mozilla plugin).  We can tell whether we're embedded or not
83      * by examining the "drawable" value: if it's zero, we're running in the
84      * main Mac intf; if it's non-zero, we're embedded. */
85     if( value_drawable.i_int == 0 )
86     {
87         /* Wait for a MacOS X interface to appear. Timeout is 2 seconds. */
88         for( i_timeout = 20 ; i_timeout-- ; )
89         {
90             if( NSApp == NULL )
91             {
92                 msleep( INTF_IDLE_SLEEP );
93             }
94         }
95
96         if( NSApp == NULL )
97         {
98             /* No MacOS X intf, unable to communicate with MT */
99             msg_Err( p_vout, "no MacOS X interface present" );
100             return NULL;
101         }
102     }
103     
104     if( [o_screens count] <= 0 )
105     {
106         msg_Err( p_vout, "no OSX screens available" );
107         return NULL;
108     }
109
110     /* p_real_vout: the vout we have to use to check for video-on-top
111        and a few other things. If we are the QuickTime output, it's us.
112        It we are the OpenGL provider, it is our parent. */
113     if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
114     {
115         p_real_vout = (vout_thread_t *) p_vout->p_parent;
116     }
117     else
118     {
119         p_real_vout = p_vout;
120     }
121
122     p_fullscreen_state = NULL;
123     i_time_mouse_last_moved = mdate();
124
125     var_Create( p_vout, "macosx-vdev", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
126     var_Create( p_vout, "macosx-fill", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
127     var_Create( p_vout, "macosx-stretch", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
128     var_Create( p_vout, "macosx-opaqueness", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
129
130     /* Get the pref value when this is the first time, otherwise retrieve the device from the top level video-device var */
131     if( var_Type( p_real_vout->p_vlc, "video-device" ) == 0 )
132     {
133         i_device = var_GetInteger( p_vout, "macosx-vdev" );
134     }
135     else
136     {
137         i_device = var_GetInteger( p_real_vout->p_vlc, "video-device" );
138     }
139
140     /* Setup the menuitem for the multiple displays. */
141     if( var_Type( p_real_vout, "video-device" ) == 0 )
142     {
143         int i = 1;
144         vlc_value_t val2, text;
145         NSScreen * o_screen;
146
147         var_Create( p_real_vout, "video-device", VLC_VAR_INTEGER |
148                                             VLC_VAR_HASCHOICE );
149         text.psz_string = _("Video Device");
150         var_Change( p_real_vout, "video-device", VLC_VAR_SETTEXT, &text, NULL );
151
152         NSEnumerator * o_enumerator = [o_screens objectEnumerator];
153
154         val2.i_int = 0;
155         text.psz_string = _("Default");
156         var_Change( p_real_vout, "video-device",
157                         VLC_VAR_ADDCHOICE, &val2, &text );
158         var_Set( p_real_vout, "video-device", val2 );
159
160         while( (o_screen = [o_enumerator nextObject]) != NULL )
161         {
162             char psz_temp[255];
163             NSRect s_rect = [o_screen frame];
164
165             snprintf( psz_temp, sizeof(psz_temp)/sizeof(psz_temp[0])-1,
166                       "%s %d (%dx%d)", _("Screen"), i,
167                       (int)s_rect.size.width, (int)s_rect.size.height );
168
169             text.psz_string = psz_temp;
170             val2.i_int = i;
171             var_Change( p_real_vout, "video-device",
172                         VLC_VAR_ADDCHOICE, &val2, &text );
173             if( i == i_device )
174             {
175                 var_Set( p_real_vout, "video-device", val2 );
176             }
177             i++;
178         }
179
180         var_AddCallback( p_real_vout, "video-device", DeviceCallback,
181                          NULL );
182
183         val2.b_bool = VLC_TRUE;
184         var_Set( p_real_vout, "intf-change", val2 );
185     }
186
187     /* Find out on which screen to open the window */
188     if( i_device <= 0 || i_device > (int)[o_screens count] )
189     {
190          /* No preference specified. Use the main screen */
191         o_screen = [NSScreen mainScreen];
192         if( o_screen == [o_screens objectAtIndex: 0] )
193             b_menubar_screen = VLC_TRUE;
194     }
195     else
196     {
197         i_device--;
198         o_screen = [o_screens objectAtIndex: i_device];
199         b_menubar_screen = ( i_device == 0 );
200     }
201
202     o_pool = [[NSAutoreleasePool alloc] init];
203
204     if( p_vout->b_fullscreen )
205     {
206         NSRect screen_rect = [o_screen frame];
207         screen_rect.origin.x = screen_rect.origin.y = 0;
208
209         /* Creates a window with size: screen_rect on o_screen */
210         [self initWithContentRect: screen_rect
211               styleMask: NSBorderlessWindowMask
212               backing: NSBackingStoreBuffered
213               defer: YES screen: o_screen];
214
215         if( b_menubar_screen )
216         {
217             BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
218                              NULL, NULL, fullScreenAllowEvents );
219         }
220     }
221     else
222     {
223         unsigned int i_stylemask = NSTitledWindowMask |
224                                    NSMiniaturizableWindowMask |
225                                    NSClosableWindowMask |
226                                    NSResizableWindowMask;
227
228         NSRect s_rect;
229         if( !s_frame )
230         {
231             s_rect.size.width  = p_vout->i_window_width;
232             s_rect.size.height = p_vout->i_window_height;
233         }
234         else
235         {
236             s_rect = *s_frame;
237         }
238        
239         [self initWithContentRect: s_rect
240               styleMask: i_stylemask
241               backing: NSBackingStoreBuffered
242               defer: YES screen: o_screen];
243
244         [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
245
246         if( var_GetBool( p_real_vout, "video-on-top" ) )
247         {
248             [self setLevel: NSStatusWindowLevel];
249         }
250
251         if( !s_frame )
252         {
253             [self center];
254         }
255     }
256
257     [self updateTitle];
258     [self makeKeyAndOrderFront: nil];
259     [self setReleasedWhenClosed: YES];
260
261     /* We'll catch mouse events */
262     [self setAcceptsMouseMovedEvents: YES];
263     [self makeFirstResponder: self];
264     
265     [o_pool release];
266     return self;
267 }
268
269 - (void)close
270 {
271     if( p_fullscreen_state )
272     {
273         EndFullScreen( p_fullscreen_state, 0 );
274     }
275     [super close];
276 }
277
278 - (void)setOnTop:(BOOL)b_on_top
279 {
280     if( b_on_top )
281     {
282         [self setLevel: NSStatusWindowLevel];
283     }
284     else
285     {
286         [self setLevel: NSNormalWindowLevel];
287     }
288 }
289
290 - (void)hideMouse:(BOOL)b_hide
291 {
292     BOOL b_inside;
293     NSPoint ml;
294     NSView *o_contents = [self contentView];
295     
296     ml = [self convertScreenToBase:[NSEvent mouseLocation]];
297     ml = [o_contents convertPoint:ml fromView:nil];
298     b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
299     
300     if( b_hide && b_inside )
301     {
302         [NSCursor setHiddenUntilMouseMoves: YES];
303     }
304     else if( !b_hide )
305     {
306         [NSCursor setHiddenUntilMouseMoves: NO];
307     }
308 }
309
310 - (void)manage
311 {
312     if( p_fullscreen_state )
313     {
314         if( mdate() - i_time_mouse_last_moved > 3000000 )
315         {
316             [self hideMouse: YES];
317         }
318     }
319     else
320     {
321         [self hideMouse: NO];
322     }
323
324     /* Disable screensaver */
325     UpdateSystemActivity( UsrActivity );
326 }
327
328 - (void)scaleWindowWithFactor: (float)factor
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         topleftbase.x = 0;
338         topleftbase.y = [self frame].size.height;
339         topleftscreen = [self convertBaseToScreen: topleftbase];
340         
341         if( p_vout->render.i_height * p_vout->render.i_aspect > 
342                         p_vout->render.i_width * VOUT_ASPECT_FACTOR )
343         {
344             i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
345                                             VOUT_ASPECT_FACTOR;
346             newsize.width = (int) ( i_corrected_width * factor );
347             newsize.height = (int) ( p_vout->render.i_height * factor );
348         }
349         else
350         {
351             i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
352                                             p_vout->render.i_aspect;
353             newsize.width = (int) ( p_vout->render.i_width * factor );
354             newsize.height = (int) ( i_corrected_height * factor );
355         }
356     
357         [self setContentSize: newsize];
358         
359         [self setFrameTopLeftPoint: topleftscreen];
360         p_vout->i_changes |= VOUT_SIZE_CHANGE;
361     }
362 }
363
364 - (void)toggleFloatOnTop
365 {
366     vlc_value_t val;
367
368     if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
369     {
370         val.b_bool = VLC_FALSE;
371     }
372     else
373     {
374         val.b_bool = VLC_TRUE;
375     }
376     var_Set( p_real_vout, "video-on-top", val );
377 }
378
379 - (void)toggleFullscreen
380 {
381     vlc_value_t val;
382     var_Get( p_real_vout, "fullscreen", &val );
383     val.b_bool = !val.b_bool;
384     var_Set( p_real_vout, "fullscreen", val );
385 }
386
387 - (BOOL)isFullscreen
388 {
389     vlc_value_t val;
390     var_Get( p_real_vout, "fullscreen", &val );
391     return( val.b_bool );
392 }
393
394 - (void)snapshot
395 {
396     vout_Control( p_real_vout, VOUT_SNAPSHOT );
397 }
398
399 - (BOOL)canBecomeKeyWindow
400 {
401     return YES;
402 }
403
404 /* Sometimes crashes VLC....
405 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
406 {
407         return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
408 }*/
409
410 - (void)keyDown:(NSEvent *)o_event
411 {
412     unichar key = 0;
413     vlc_value_t val;
414     unsigned int i_pressed_modifiers = 0;
415     val.i_int = 0;
416     
417     i_pressed_modifiers = [o_event modifierFlags];
418
419     if( i_pressed_modifiers & NSShiftKeyMask )
420         val.i_int |= KEY_MODIFIER_SHIFT;
421     if( i_pressed_modifiers & NSControlKeyMask )
422         val.i_int |= KEY_MODIFIER_CTRL;
423     if( i_pressed_modifiers & NSAlternateKeyMask )
424         val.i_int |= KEY_MODIFIER_ALT;
425     if( i_pressed_modifiers & NSCommandKeyMask )
426         val.i_int |= KEY_MODIFIER_COMMAND;
427
428     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
429
430     if( key )
431     {
432         /* Escape should always get you out of fullscreen */
433         if( key == (unichar) 0x1b )
434         {
435              if( [self isFullscreen] )
436              {
437                  [self toggleFullscreen];
438              }
439         }
440         else if ( key == ' ' )
441         {
442             vlc_value_t val;
443             val.i_int = config_GetInt( p_vout, "key-play-pause" );
444             var_Set( p_vout->p_vlc, "key-pressed", val );
445         }
446         else
447         {
448             val.i_int |= CocoaKeyToVLC( key );
449             var_Set( p_vout->p_vlc, "key-pressed", val );
450         }
451     }
452     else
453     {
454         [super keyDown: o_event];
455     }
456 }
457
458 - (void)updateTitle
459 {
460     NSMutableString * o_title,* o_mrl;
461     input_thread_t * p_input;
462     
463     if( p_vout == NULL )
464     {
465         return;
466     }
467     
468     p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
469                                                 FIND_PARENT );
470     
471     if( p_input == NULL )
472     {
473         return;
474     }
475
476     if( p_input->input.p_item->psz_name != NULL )
477         o_title = [NSMutableString stringWithUTF8String:
478             p_input->input.p_item->psz_name];
479     if( p_input->input.p_item->psz_uri != NULL )
480         o_mrl = [NSMutableString stringWithUTF8String:
481             p_input->input.p_item->psz_uri];
482     if( o_title == nil )
483         o_title = o_mrl;
484
485     vlc_object_release( p_input );
486     if( o_mrl != nil )
487     {
488         if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
489         {
490             NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
491             if( prefix_range.location != NSNotFound )
492                 [o_mrl deleteCharactersInRange: prefix_range];
493             [self setRepresentedFilename: o_mrl];
494         }
495         [self setTitle: o_title];
496     }
497     else
498     {
499         [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
500     }
501 }
502
503 /* This is actually the same as VLCControls::stop. */
504 - (BOOL)windowShouldClose:(id)sender
505 {
506     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
507                                                        FIND_ANYWHERE );
508     if( p_playlist == NULL )      
509     {
510         return NO;
511     }
512
513     playlist_Stop( p_playlist );
514     vlc_object_release( p_playlist );
515
516     /* The window will be closed by the intf later. */
517     return NO;
518 }
519
520 - (BOOL)acceptsFirstResponder
521 {
522     return YES;
523 }
524
525 - (BOOL)becomeFirstResponder
526 {
527     return YES;
528 }
529
530 - (BOOL)resignFirstResponder
531 {
532     /* We need to stay the first responder or we'll miss some
533        events */
534     return NO;
535 }
536
537 - (void)mouseDown:(NSEvent *)o_event
538 {
539     vlc_value_t val;
540
541     switch( [o_event type] )
542     {
543         case NSLeftMouseDown:
544         {
545             var_Get( p_vout, "mouse-button-down", &val );
546             val.i_int |= 1;
547             var_Set( p_vout, "mouse-button-down", val );
548         }
549         break;
550
551         default:
552             [super mouseDown: o_event];
553         break;
554     }
555 }
556
557 - (void)otherMouseDown:(NSEvent *)o_event
558 {
559     vlc_value_t val;
560
561     switch( [o_event type] )
562     {
563         case NSOtherMouseDown:
564         {
565             var_Get( p_vout, "mouse-button-down", &val );
566             val.i_int |= 2;
567             var_Set( p_vout, "mouse-button-down", val );
568         }
569         break;
570
571         default:
572             [super mouseDown: o_event];
573         break;
574     }
575 }
576
577 - (void)rightMouseDown:(NSEvent *)o_event
578 {
579     vlc_value_t val;
580
581     switch( [o_event type] )
582     {
583         case NSRightMouseDown:
584         {
585             var_Get( p_vout, "mouse-button-down", &val );
586             val.i_int |= 4;
587             var_Set( p_vout, "mouse-button-down", val );
588         }
589         break;
590
591         default:
592             [super mouseDown: o_event];
593         break;
594     }
595 }
596
597 - (void)mouseUp:(NSEvent *)o_event
598 {
599     vlc_value_t val;
600
601     switch( [o_event type] )
602     {
603         case NSLeftMouseUp:
604         {
605             vlc_value_t b_val;
606             b_val.b_bool = VLC_TRUE;
607             var_Set( p_vout, "mouse-clicked", b_val );
608
609             var_Get( p_vout, "mouse-button-down", &val );
610             val.i_int &= ~1;
611             var_Set( p_vout, "mouse-button-down", val );
612         }
613         break;
614
615         default:
616             [super mouseUp: o_event];
617         break;
618     }
619 }
620
621 - (void)otherMouseUp:(NSEvent *)o_event
622 {
623     vlc_value_t val;
624
625     switch( [o_event type] )
626     {
627         case NSOtherMouseUp:
628         {
629             var_Get( p_vout, "mouse-button-down", &val );
630             val.i_int &= ~2;
631             var_Set( p_vout, "mouse-button-down", val );
632         }
633         break;
634
635         default:
636             [super mouseUp: o_event];
637         break;
638     }
639 }
640
641 - (void)rightMouseUp:(NSEvent *)o_event
642 {
643     vlc_value_t val;
644
645     switch( [o_event type] )
646     {
647         case NSRightMouseUp:
648         {
649             var_Get( p_vout, "mouse-button-down", &val );
650             val.i_int &= ~4;
651             var_Set( p_vout, "mouse-button-down", val );
652         }
653         break;
654
655         default:
656             [super mouseUp: o_event];
657         break;
658     }
659 }
660
661 - (void)mouseDragged:(NSEvent *)o_event
662 {
663     [self mouseMoved: o_event];
664 }
665
666 - (void)otherMouseDragged:(NSEvent *)o_event
667 {
668     [self mouseMoved: o_event];
669 }
670
671 - (void)rightMouseDragged:(NSEvent *)o_event
672 {
673     [self mouseMoved: o_event];
674 }
675
676 - (void)mouseMoved:(NSEvent *)o_event
677 {   
678     NSPoint ml;
679     NSRect s_rect;
680     BOOL b_inside;
681     NSView * o_view;
682
683     i_time_mouse_last_moved = mdate();
684
685     o_view = [self contentView];
686     s_rect = [o_view bounds];
687     ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
688     b_inside = [o_view mouse: ml inRect: s_rect];
689
690     if( b_inside )
691     {
692         vlc_value_t val;
693         int i_width, i_height, i_x, i_y;
694
695         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
696                                    (unsigned int)s_rect.size.height,
697                                    &i_x, &i_y, &i_width, &i_height );
698
699         val.i_int = ( ((int)ml.x) - i_x ) *  
700                     p_vout->render.i_width / i_width;
701         var_Set( p_vout, "mouse-x", val );
702
703         if( [[o_view className] isEqualToString: @"VLCGLView"] )
704         {
705             val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
706                         p_vout->render.i_height / i_height;
707         }
708         else
709         {
710             val.i_int = ( ((int)ml.y) - i_y ) * 
711                         p_vout->render.i_height / i_height;
712         }
713         var_Set( p_vout, "mouse-y", val );
714             
715         val.b_bool = VLC_TRUE;
716         var_Set( p_vout, "mouse-moved", val ); 
717     }
718
719     [super mouseMoved: o_event];
720 }
721
722 @end