]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
* better support for multiple display selection on OS X. (Remembers last selection...
[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_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_vout, "video-on-top", val );
377 }
378
379 - (void)toggleFullscreen
380 {
381     vlc_value_t val;
382     val.b_bool = !p_real_vout->b_fullscreen;
383     var_Set( p_real_vout, "fullscreen", val );
384 }
385
386 - (BOOL)isFullscreen
387 {
388     return( p_vout->b_fullscreen );
389 }
390
391 - (void)snapshot
392 {
393     vout_Control( p_vout, VOUT_SNAPSHOT );
394 }
395
396 - (BOOL)canBecomeKeyWindow
397 {
398     return YES;
399 }
400
401 /* Sometimes crashes VLC....
402 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
403 {
404         return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
405 }*/
406
407 - (void)keyDown:(NSEvent *)o_event
408 {
409     unichar key = 0;
410     vlc_value_t val;
411     unsigned int i_pressed_modifiers = 0;
412     val.i_int = 0;
413     
414     i_pressed_modifiers = [o_event modifierFlags];
415
416     if( i_pressed_modifiers & NSShiftKeyMask )
417         val.i_int |= KEY_MODIFIER_SHIFT;
418     if( i_pressed_modifiers & NSControlKeyMask )
419         val.i_int |= KEY_MODIFIER_CTRL;
420     if( i_pressed_modifiers & NSAlternateKeyMask )
421         val.i_int |= KEY_MODIFIER_ALT;
422     if( i_pressed_modifiers & NSCommandKeyMask )
423         val.i_int |= KEY_MODIFIER_COMMAND;
424
425     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
426
427     if( key )
428     {
429         /* Escape should always get you out of fullscreen */
430         if( key == (unichar) 0x1b )
431         {
432              if( [self isFullscreen] )
433              {
434                  [self toggleFullscreen];
435              }
436         }
437         else if ( key == ' ' )
438         {
439             vlc_value_t val;
440             val.i_int = config_GetInt( p_vout, "key-play-pause" );
441             var_Set( p_vout->p_vlc, "key-pressed", val );
442         }
443         else
444         {
445             val.i_int |= CocoaKeyToVLC( key );
446             var_Set( p_vout->p_vlc, "key-pressed", val );
447         }
448     }
449     else
450     {
451         [super keyDown: o_event];
452     }
453 }
454
455 - (void)updateTitle
456 {
457     NSMutableString * o_title,* o_mrl;
458     input_thread_t * p_input;
459     
460     if( p_vout == NULL )
461     {
462         return;
463     }
464     
465     p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
466                                                 FIND_PARENT );
467     
468     if( p_input == NULL )
469     {
470         return;
471     }
472
473     if( p_input->input.p_item->psz_name != NULL )
474         o_title = [NSMutableString stringWithUTF8String:
475             p_input->input.p_item->psz_name];
476     if( p_input->input.p_item->psz_uri != NULL )
477         o_mrl = [NSMutableString stringWithUTF8String:
478             p_input->input.p_item->psz_uri];
479     if( o_title == nil )
480         o_title = o_mrl;
481
482     vlc_object_release( p_input );
483     if( o_mrl != nil )
484     {
485         if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
486         {
487             NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
488             if( prefix_range.location != NSNotFound )
489                 [o_mrl deleteCharactersInRange: prefix_range];
490             [self setRepresentedFilename: o_mrl];
491         }
492         [self setTitle: o_title];
493     }
494     else
495     {
496         [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
497     }
498 }
499
500 /* This is actually the same as VLCControls::stop. */
501 - (BOOL)windowShouldClose:(id)sender
502 {
503     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
504                                                        FIND_ANYWHERE );
505     if( p_playlist == NULL )      
506     {
507         return NO;
508     }
509
510     playlist_Stop( p_playlist );
511     vlc_object_release( p_playlist );
512
513     /* The window will be closed by the intf later. */
514     return NO;
515 }
516
517 - (BOOL)acceptsFirstResponder
518 {
519     return YES;
520 }
521
522 - (BOOL)becomeFirstResponder
523 {
524     return YES;
525 }
526
527 - (BOOL)resignFirstResponder
528 {
529     /* We need to stay the first responder or we'll miss some
530        events */
531     return NO;
532 }
533
534 - (void)mouseDown:(NSEvent *)o_event
535 {
536     vlc_value_t val;
537
538     switch( [o_event type] )
539     {
540         case NSLeftMouseDown:
541         {
542             var_Get( p_vout, "mouse-button-down", &val );
543             val.i_int |= 1;
544             var_Set( p_vout, "mouse-button-down", val );
545         }
546         break;
547
548         default:
549             [super mouseDown: o_event];
550         break;
551     }
552 }
553
554 - (void)otherMouseDown:(NSEvent *)o_event
555 {
556     vlc_value_t val;
557
558     switch( [o_event type] )
559     {
560         case NSOtherMouseDown:
561         {
562             var_Get( p_vout, "mouse-button-down", &val );
563             val.i_int |= 2;
564             var_Set( p_vout, "mouse-button-down", val );
565         }
566         break;
567
568         default:
569             [super mouseDown: o_event];
570         break;
571     }
572 }
573
574 - (void)rightMouseDown:(NSEvent *)o_event
575 {
576     vlc_value_t val;
577
578     switch( [o_event type] )
579     {
580         case NSRightMouseDown:
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 mouseDown: o_event];
590         break;
591     }
592 }
593
594 - (void)mouseUp:(NSEvent *)o_event
595 {
596     vlc_value_t val;
597
598     switch( [o_event type] )
599     {
600         case NSLeftMouseUp:
601         {
602             vlc_value_t b_val;
603             b_val.b_bool = VLC_TRUE;
604             var_Set( p_vout, "mouse-clicked", b_val );
605
606             var_Get( p_vout, "mouse-button-down", &val );
607             val.i_int &= ~1;
608             var_Set( p_vout, "mouse-button-down", val );
609         }
610         break;
611
612         default:
613             [super mouseUp: o_event];
614         break;
615     }
616 }
617
618 - (void)otherMouseUp:(NSEvent *)o_event
619 {
620     vlc_value_t val;
621
622     switch( [o_event type] )
623     {
624         case NSOtherMouseUp:
625         {
626             var_Get( p_vout, "mouse-button-down", &val );
627             val.i_int &= ~2;
628             var_Set( p_vout, "mouse-button-down", val );
629         }
630         break;
631
632         default:
633             [super mouseUp: o_event];
634         break;
635     }
636 }
637
638 - (void)rightMouseUp:(NSEvent *)o_event
639 {
640     vlc_value_t val;
641
642     switch( [o_event type] )
643     {
644         case NSRightMouseUp:
645         {
646             var_Get( p_vout, "mouse-button-down", &val );
647             val.i_int &= ~4;
648             var_Set( p_vout, "mouse-button-down", val );
649         }
650         break;
651
652         default:
653             [super mouseUp: o_event];
654         break;
655     }
656 }
657
658 - (void)mouseDragged:(NSEvent *)o_event
659 {
660     [self mouseMoved: o_event];
661 }
662
663 - (void)otherMouseDragged:(NSEvent *)o_event
664 {
665     [self mouseMoved: o_event];
666 }
667
668 - (void)rightMouseDragged:(NSEvent *)o_event
669 {
670     [self mouseMoved: o_event];
671 }
672
673 - (void)mouseMoved:(NSEvent *)o_event
674 {   
675     NSPoint ml;
676     NSRect s_rect;
677     BOOL b_inside;
678     NSView * o_view;
679
680     i_time_mouse_last_moved = mdate();
681
682     o_view = [self contentView];
683     s_rect = [o_view bounds];
684     ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
685     b_inside = [o_view mouse: ml inRect: s_rect];
686
687     if( b_inside )
688     {
689         vlc_value_t val;
690         int i_width, i_height, i_x, i_y;
691
692         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
693                                    (unsigned int)s_rect.size.height,
694                                    &i_x, &i_y, &i_width, &i_height );
695
696         val.i_int = ( ((int)ml.x) - i_x ) *  
697                     p_vout->render.i_width / i_width;
698         var_Set( p_vout, "mouse-x", val );
699
700         if( [[o_view className] isEqualToString: @"VLCGLView"] )
701         {
702             val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
703                         p_vout->render.i_height / i_height;
704         }
705         else
706         {
707             val.i_int = ( ((int)ml.y) - i_y ) * 
708                         p_vout->render.i_height / i_height;
709         }
710         var_Set( p_vout, "mouse-y", val );
711             
712         val.b_bool = VLC_TRUE;
713         var_Set( p_vout, "mouse-moved", val ); 
714     }
715
716     [super mouseMoved: o_event];
717 }
718
719 @end