]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
* vout.m: small codefixes that were not 100% problems.
[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 = [[NSAutoreleasePool alloc] init];
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     if( p_vout->b_fullscreen )
203     {
204         NSRect screen_rect = [o_screen frame];
205         screen_rect.origin.x = screen_rect.origin.y = 0;
206
207         /* Creates a window with size: screen_rect on o_screen */
208         [self initWithContentRect: screen_rect
209               styleMask: NSBorderlessWindowMask
210               backing: NSBackingStoreBuffered
211               defer: YES screen: o_screen];
212
213         if( b_menubar_screen )
214         {
215             BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
216                              NULL, NULL, fullScreenAllowEvents );
217         }
218     }
219     else
220     {
221         unsigned int i_stylemask = NSTitledWindowMask |
222                                    NSMiniaturizableWindowMask |
223                                    NSClosableWindowMask |
224                                    NSResizableWindowMask;
225
226         NSRect s_rect;
227         if( !s_frame )
228         {
229             s_rect.size.width  = p_vout->i_window_width;
230             s_rect.size.height = p_vout->i_window_height;
231         }
232         else
233         {
234             s_rect = *s_frame;
235         }
236        
237         [self initWithContentRect: s_rect
238               styleMask: i_stylemask
239               backing: NSBackingStoreBuffered
240               defer: YES screen: o_screen];
241
242         [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
243
244         if( var_GetBool( p_real_vout, "video-on-top" ) )
245         {
246             [self setLevel: NSStatusWindowLevel];
247         }
248
249         if( !s_frame )
250         {
251             [self center];
252         }
253     }
254
255     [self updateTitle];
256     [self makeKeyAndOrderFront: nil];
257     [self setReleasedWhenClosed: YES];
258
259     /* We'll catch mouse events */
260     [self setAcceptsMouseMovedEvents: YES];
261     [self makeFirstResponder: self];
262     
263     [o_pool release];
264     return self;
265 }
266
267 - (void)close
268 {
269     [super close];
270     if( p_fullscreen_state )
271     {
272         EndFullScreen( p_fullscreen_state, 0 );
273     }
274 }
275
276 - (void)setOnTop:(BOOL)b_on_top
277 {
278     if( b_on_top )
279     {
280         [self setLevel: NSStatusWindowLevel];
281     }
282     else
283     {
284         [self setLevel: NSNormalWindowLevel];
285     }
286 }
287
288 - (void)hideMouse:(BOOL)b_hide
289 {
290     BOOL b_inside;
291     NSPoint ml;
292     NSView *o_contents = [self contentView];
293     
294     ml = [self convertScreenToBase:[NSEvent mouseLocation]];
295     ml = [o_contents convertPoint:ml fromView:nil];
296     b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
297     
298     if( b_hide && b_inside )
299     {
300         [NSCursor setHiddenUntilMouseMoves: YES];
301     }
302     else if( !b_hide )
303     {
304         [NSCursor setHiddenUntilMouseMoves: NO];
305     }
306 }
307
308 - (void)manage
309 {
310     if( p_fullscreen_state )
311     {
312         if( mdate() - i_time_mouse_last_moved > 3000000 )
313         {
314             [self hideMouse: YES];
315         }
316     }
317     else
318     {
319         [self hideMouse: NO];
320     }
321
322     /* Disable screensaver */
323     UpdateSystemActivity( UsrActivity );
324 }
325
326 - (void)scaleWindowWithFactor: (float)factor
327 {
328     NSSize newsize;
329     int i_corrected_height, i_corrected_width;
330     NSPoint topleftbase;
331     NSPoint topleftscreen;
332     
333     if ( !p_vout->b_fullscreen )
334     {
335         topleftbase.x = 0;
336         topleftbase.y = [self frame].size.height;
337         topleftscreen = [self convertBaseToScreen: topleftbase];
338         
339         if( p_vout->render.i_height * p_vout->render.i_aspect > 
340                         p_vout->render.i_width * VOUT_ASPECT_FACTOR )
341         {
342             i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
343                                             VOUT_ASPECT_FACTOR;
344             newsize.width = (int) ( i_corrected_width * factor );
345             newsize.height = (int) ( p_vout->render.i_height * factor );
346         }
347         else
348         {
349             i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
350                                             p_vout->render.i_aspect;
351             newsize.width = (int) ( p_vout->render.i_width * factor );
352             newsize.height = (int) ( i_corrected_height * factor );
353         }
354     
355         [self setContentSize: newsize];
356         
357         [self setFrameTopLeftPoint: topleftscreen];
358         p_vout->i_changes |= VOUT_SIZE_CHANGE;
359     }
360 }
361
362 - (void)toggleFloatOnTop
363 {
364     vlc_value_t val;
365
366     if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
367     {
368         val.b_bool = VLC_FALSE;
369     }
370     else
371     {
372         val.b_bool = VLC_TRUE;
373     }
374     var_Set( p_real_vout, "video-on-top", val );
375 }
376
377 - (void)toggleFullscreen
378 {
379     vlc_value_t val;
380     var_Get( p_real_vout, "fullscreen", &val );
381     val.b_bool = !val.b_bool;
382     var_Set( p_real_vout, "fullscreen", val );
383 }
384
385 - (BOOL)isFullscreen
386 {
387     vlc_value_t val;
388     var_Get( p_real_vout, "fullscreen", &val );
389     return( val.b_bool );
390 }
391
392 - (void)snapshot
393 {
394     vout_Control( p_real_vout, VOUT_SNAPSHOT );
395 }
396
397 - (BOOL)canBecomeKeyWindow
398 {
399     return YES;
400 }
401
402 /* Sometimes crashes VLC....
403 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
404 {
405         return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
406 }*/
407
408 - (void)keyDown:(NSEvent *)o_event
409 {
410     unichar key = 0;
411     vlc_value_t val;
412     unsigned int i_pressed_modifiers = 0;
413     val.i_int = 0;
414     
415     i_pressed_modifiers = [o_event modifierFlags];
416
417     if( i_pressed_modifiers & NSShiftKeyMask )
418         val.i_int |= KEY_MODIFIER_SHIFT;
419     if( i_pressed_modifiers & NSControlKeyMask )
420         val.i_int |= KEY_MODIFIER_CTRL;
421     if( i_pressed_modifiers & NSAlternateKeyMask )
422         val.i_int |= KEY_MODIFIER_ALT;
423     if( i_pressed_modifiers & NSCommandKeyMask )
424         val.i_int |= KEY_MODIFIER_COMMAND;
425
426     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
427
428     if( key )
429     {
430         /* Escape should always get you out of fullscreen */
431         if( key == (unichar) 0x1b )
432         {
433              if( [self isFullscreen] )
434              {
435                  [self toggleFullscreen];
436              }
437         }
438         else if ( key == ' ' )
439         {
440             vlc_value_t val;
441             val.i_int = config_GetInt( p_vout, "key-play-pause" );
442             var_Set( p_vout->p_vlc, "key-pressed", val );
443         }
444         else
445         {
446             val.i_int |= CocoaKeyToVLC( key );
447             var_Set( p_vout->p_vlc, "key-pressed", val );
448         }
449     }
450     else
451     {
452         [super keyDown: o_event];
453     }
454 }
455
456 - (void)updateTitle
457 {
458     NSMutableString * o_title,* o_mrl;
459     input_thread_t * p_input;
460     
461     if( p_vout == NULL )
462     {
463         return;
464     }
465     
466     p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
467                                                 FIND_PARENT );
468     
469     if( p_input == NULL )
470     {
471         return;
472     }
473
474     if( p_input->input.p_item->psz_name != NULL )
475         o_title = [NSMutableString stringWithUTF8String:
476             p_input->input.p_item->psz_name];
477     if( p_input->input.p_item->psz_uri != NULL )
478         o_mrl = [NSMutableString stringWithUTF8String:
479             p_input->input.p_item->psz_uri];
480     if( o_title == nil )
481         o_title = o_mrl;
482
483     vlc_object_release( p_input );
484     if( o_mrl != nil )
485     {
486         if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
487         {
488             NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
489             if( prefix_range.location != NSNotFound )
490                 [o_mrl deleteCharactersInRange: prefix_range];
491             [self setRepresentedFilename: o_mrl];
492         }
493         [self setTitle: o_title];
494     }
495     else
496     {
497         [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
498     }
499 }
500
501 /* This is actually the same as VLCControls::stop. */
502 - (BOOL)windowShouldClose:(id)sender
503 {
504     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
505                                                        FIND_ANYWHERE );
506     if( p_playlist == NULL )      
507     {
508         return NO;
509     }
510
511     playlist_Stop( p_playlist );
512     vlc_object_release( p_playlist );
513
514     /* The window will be closed by the intf later. */
515     return NO;
516 }
517
518 - (BOOL)acceptsFirstResponder
519 {
520     return YES;
521 }
522
523 - (BOOL)becomeFirstResponder
524 {
525     return YES;
526 }
527
528 - (BOOL)resignFirstResponder
529 {
530     /* We need to stay the first responder or we'll miss some
531        events */
532     return NO;
533 }
534
535 - (void)mouseDown:(NSEvent *)o_event
536 {
537     vlc_value_t val;
538
539     switch( [o_event type] )
540     {
541         case NSLeftMouseDown:
542         {
543             var_Get( p_vout, "mouse-button-down", &val );
544             val.i_int |= 1;
545             var_Set( p_vout, "mouse-button-down", val );
546         }
547         break;
548
549         default:
550             [super mouseDown: o_event];
551         break;
552     }
553 }
554
555 - (void)otherMouseDown:(NSEvent *)o_event
556 {
557     vlc_value_t val;
558
559     switch( [o_event type] )
560     {
561         case NSOtherMouseDown:
562         {
563             var_Get( p_vout, "mouse-button-down", &val );
564             val.i_int |= 2;
565             var_Set( p_vout, "mouse-button-down", val );
566         }
567         break;
568
569         default:
570             [super mouseDown: o_event];
571         break;
572     }
573 }
574
575 - (void)rightMouseDown:(NSEvent *)o_event
576 {
577     vlc_value_t val;
578
579     switch( [o_event type] )
580     {
581         case NSRightMouseDown:
582         {
583             var_Get( p_vout, "mouse-button-down", &val );
584             val.i_int |= 4;
585             var_Set( p_vout, "mouse-button-down", val );
586         }
587         break;
588
589         default:
590             [super mouseDown: o_event];
591         break;
592     }
593 }
594
595 - (void)mouseUp:(NSEvent *)o_event
596 {
597     vlc_value_t val;
598
599     switch( [o_event type] )
600     {
601         case NSLeftMouseUp:
602         {
603             vlc_value_t b_val;
604             b_val.b_bool = VLC_TRUE;
605             var_Set( p_vout, "mouse-clicked", b_val );
606
607             var_Get( p_vout, "mouse-button-down", &val );
608             val.i_int &= ~1;
609             var_Set( p_vout, "mouse-button-down", val );
610         }
611         break;
612
613         default:
614             [super mouseUp: o_event];
615         break;
616     }
617 }
618
619 - (void)otherMouseUp:(NSEvent *)o_event
620 {
621     vlc_value_t val;
622
623     switch( [o_event type] )
624     {
625         case NSOtherMouseUp:
626         {
627             var_Get( p_vout, "mouse-button-down", &val );
628             val.i_int &= ~2;
629             var_Set( p_vout, "mouse-button-down", val );
630         }
631         break;
632
633         default:
634             [super mouseUp: o_event];
635         break;
636     }
637 }
638
639 - (void)rightMouseUp:(NSEvent *)o_event
640 {
641     vlc_value_t val;
642
643     switch( [o_event type] )
644     {
645         case NSRightMouseUp:
646         {
647             var_Get( p_vout, "mouse-button-down", &val );
648             val.i_int &= ~4;
649             var_Set( p_vout, "mouse-button-down", val );
650         }
651         break;
652
653         default:
654             [super mouseUp: o_event];
655         break;
656     }
657 }
658
659 - (void)mouseDragged:(NSEvent *)o_event
660 {
661     [self mouseMoved: o_event];
662 }
663
664 - (void)otherMouseDragged:(NSEvent *)o_event
665 {
666     [self mouseMoved: o_event];
667 }
668
669 - (void)rightMouseDragged:(NSEvent *)o_event
670 {
671     [self mouseMoved: o_event];
672 }
673
674 - (void)mouseMoved:(NSEvent *)o_event
675 {   
676     NSPoint ml;
677     NSRect s_rect;
678     BOOL b_inside;
679     NSView * o_view;
680
681     i_time_mouse_last_moved = mdate();
682
683     o_view = [self contentView];
684     s_rect = [o_view bounds];
685     ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
686     b_inside = [o_view mouse: ml inRect: s_rect];
687
688     if( b_inside )
689     {
690         vlc_value_t val;
691         unsigned int i_width, i_height, i_x, i_y;
692
693         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
694                                    (unsigned int)s_rect.size.height,
695                                    &i_x, &i_y, &i_width, &i_height );
696
697         val.i_int = ( ((int)ml.x) - i_x ) *  
698                     p_vout->render.i_width / i_width;
699         var_Set( p_vout, "mouse-x", val );
700
701         if( [[o_view className] isEqualToString: @"VLCGLView"] )
702         {
703             val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
704                         p_vout->render.i_height / i_height;
705         }
706         else
707         {
708             val.i_int = ( ((int)ml.y) - i_y ) * 
709                         p_vout->render.i_height / i_height;
710         }
711         var_Set( p_vout, "mouse-y", val );
712             
713         val.b_bool = VLC_TRUE;
714         var_Set( p_vout, "mouse-moved", val ); 
715     }
716
717     [super mouseMoved: o_event];
718 }
719
720 @end