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