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