]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
macosx/vout*: also destroy the window and attach/detach the view on the
[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 performSelectorOnMainThread: @selector(closeReal:)
295         withObject: NULL waitUntilDone: NO];
296 }
297
298 - (id) closeReal: (id) sender
299 {
300     [super close];
301     if( p_fullscreen_state )
302     {
303         EndFullScreen( p_fullscreen_state, 0 );
304     }
305     return NULL;
306 }
307
308 - (void)setOnTop:(BOOL)b_on_top
309 {
310     if( b_on_top )
311     {
312         [self setLevel: NSStatusWindowLevel];
313     }
314     else
315     {
316         [self setLevel: NSNormalWindowLevel];
317     }
318 }
319
320 - (void)hideMouse:(BOOL)b_hide
321 {
322     BOOL b_inside;
323     NSPoint ml;
324     NSView *o_contents = [self contentView];
325     
326     ml = [self convertScreenToBase:[NSEvent mouseLocation]];
327     ml = [o_contents convertPoint:ml fromView:nil];
328     b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
329     
330     if( b_hide && b_inside )
331     {
332         [NSCursor setHiddenUntilMouseMoves: YES];
333     }
334     else if( !b_hide )
335     {
336         [NSCursor setHiddenUntilMouseMoves: NO];
337     }
338 }
339
340 - (void)manage
341 {
342     if( p_fullscreen_state )
343     {
344         if( mdate() - i_time_mouse_last_moved > 3000000 )
345         {
346             [self hideMouse: YES];
347         }
348     }
349     else
350     {
351         [self hideMouse: NO];
352     }
353
354     /* Disable screensaver */
355     UpdateSystemActivity( UsrActivity );
356 }
357
358 - (void)scaleWindowWithFactor: (float)factor
359 {
360     NSSize newsize;
361     int i_corrected_height, i_corrected_width;
362     NSPoint topleftbase;
363     NSPoint topleftscreen;
364     
365     if ( !p_vout->b_fullscreen )
366     {
367         topleftbase.x = 0;
368         topleftbase.y = [self frame].size.height;
369         topleftscreen = [self convertBaseToScreen: topleftbase];
370         
371         if( p_vout->render.i_height * p_vout->render.i_aspect > 
372                         p_vout->render.i_width * VOUT_ASPECT_FACTOR )
373         {
374             i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
375                                             VOUT_ASPECT_FACTOR;
376             newsize.width = (int) ( i_corrected_width * factor );
377             newsize.height = (int) ( p_vout->render.i_height * factor );
378         }
379         else
380         {
381             i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
382                                             p_vout->render.i_aspect;
383             newsize.width = (int) ( p_vout->render.i_width * factor );
384             newsize.height = (int) ( i_corrected_height * factor );
385         }
386     
387         [self setContentSize: newsize];
388         
389         [self setFrameTopLeftPoint: topleftscreen];
390         p_vout->i_changes |= VOUT_SIZE_CHANGE;
391     }
392 }
393
394 - (void)toggleFloatOnTop
395 {
396     vlc_value_t val;
397
398     if( var_Get( p_real_vout, "video-on-top", &val )>=0 && val.b_bool)
399     {
400         val.b_bool = VLC_FALSE;
401     }
402     else
403     {
404         val.b_bool = VLC_TRUE;
405     }
406     var_Set( p_real_vout, "video-on-top", val );
407 }
408
409 - (void)toggleFullscreen
410 {
411     vlc_value_t val;
412     var_Get( p_real_vout, "fullscreen", &val );
413     val.b_bool = !val.b_bool;
414     var_Set( p_real_vout, "fullscreen", val );
415 }
416
417 - (BOOL)isFullscreen
418 {
419     vlc_value_t val;
420     var_Get( p_real_vout, "fullscreen", &val );
421     return( val.b_bool );
422 }
423
424 - (void)snapshot
425 {
426     vout_Control( p_real_vout, VOUT_SNAPSHOT );
427 }
428
429 - (BOOL)canBecomeKeyWindow
430 {
431     return YES;
432 }
433
434 /* Sometimes crashes VLC....
435 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
436 {
437         return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
438 }*/
439
440 - (void)keyDown:(NSEvent *)o_event
441 {
442     unichar key = 0;
443     vlc_value_t val;
444     unsigned int i_pressed_modifiers = 0;
445     val.i_int = 0;
446     
447     i_pressed_modifiers = [o_event modifierFlags];
448
449     if( i_pressed_modifiers & NSShiftKeyMask )
450         val.i_int |= KEY_MODIFIER_SHIFT;
451     if( i_pressed_modifiers & NSControlKeyMask )
452         val.i_int |= KEY_MODIFIER_CTRL;
453     if( i_pressed_modifiers & NSAlternateKeyMask )
454         val.i_int |= KEY_MODIFIER_ALT;
455     if( i_pressed_modifiers & NSCommandKeyMask )
456         val.i_int |= KEY_MODIFIER_COMMAND;
457
458     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
459
460     if( key )
461     {
462         /* Escape should always get you out of fullscreen */
463         if( key == (unichar) 0x1b )
464         {
465              if( [self isFullscreen] )
466              {
467                  [self toggleFullscreen];
468              }
469         }
470         else if ( key == ' ' )
471         {
472             vlc_value_t val;
473             val.i_int = config_GetInt( p_vout, "key-play-pause" );
474             var_Set( p_vout->p_vlc, "key-pressed", val );
475         }
476         else
477         {
478             val.i_int |= CocoaKeyToVLC( key );
479             var_Set( p_vout->p_vlc, "key-pressed", val );
480         }
481     }
482     else
483     {
484         [super keyDown: o_event];
485     }
486 }
487
488 - (void)updateTitle
489 {
490     NSMutableString * o_title = NULL, * o_mrl = NULL;
491     input_thread_t * p_input;
492     
493     if( p_vout == NULL )
494     {
495         return;
496     }
497     
498     p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT,
499                                                 FIND_PARENT );
500     
501     if( p_input == NULL )
502     {
503         return;
504     }
505
506     if( p_input->input.p_item->psz_name != NULL )
507         o_title = [NSMutableString stringWithUTF8String:
508             p_input->input.p_item->psz_name];
509     if( p_input->input.p_item->psz_uri != NULL )
510         o_mrl = [NSMutableString stringWithUTF8String:
511             p_input->input.p_item->psz_uri];
512     if( o_title == nil )
513         o_title = o_mrl;
514
515     vlc_object_release( p_input );
516     if( o_mrl != nil )
517     {
518         if( p_input->input.p_access && !strcmp( p_input->input.p_access->p_module->psz_shortname, "File" ) )
519         {
520             NSRange prefix_range = [o_mrl rangeOfString: @"file:"];
521             if( prefix_range.location != NSNotFound )
522                 [o_mrl deleteCharactersInRange: prefix_range];
523             [self setRepresentedFilename: o_mrl];
524         }
525         [self setTitle: o_title];
526     }
527     else
528     {
529         [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
530     }
531 }
532
533 /* This is actually the same as VLCControls::stop. */
534 - (BOOL)windowShouldClose:(id)sender
535 {
536     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
537                                                        FIND_ANYWHERE );
538     if( p_playlist == NULL )      
539     {
540         return NO;
541     }
542
543     playlist_Stop( p_playlist );
544     vlc_object_release( p_playlist );
545
546     /* The window will be closed by the intf later. */
547     return NO;
548 }
549
550 - (BOOL)acceptsFirstResponder
551 {
552     return YES;
553 }
554
555 - (BOOL)becomeFirstResponder
556 {
557     return YES;
558 }
559
560 - (BOOL)resignFirstResponder
561 {
562     /* We need to stay the first responder or we'll miss some
563        events */
564     return NO;
565 }
566
567 - (void)mouseDown:(NSEvent *)o_event
568 {
569     vlc_value_t val;
570
571     switch( [o_event type] )
572     {
573         case NSLeftMouseDown:
574         {
575             var_Get( p_vout, "mouse-button-down", &val );
576             val.i_int |= 1;
577             var_Set( p_vout, "mouse-button-down", val );
578         }
579         break;
580
581         default:
582             [super mouseDown: o_event];
583         break;
584     }
585 }
586
587 - (void)otherMouseDown:(NSEvent *)o_event
588 {
589     vlc_value_t val;
590
591     switch( [o_event type] )
592     {
593         case NSOtherMouseDown:
594         {
595             var_Get( p_vout, "mouse-button-down", &val );
596             val.i_int |= 2;
597             var_Set( p_vout, "mouse-button-down", val );
598         }
599         break;
600
601         default:
602             [super mouseDown: o_event];
603         break;
604     }
605 }
606
607 - (void)rightMouseDown:(NSEvent *)o_event
608 {
609     vlc_value_t val;
610
611     switch( [o_event type] )
612     {
613         case NSRightMouseDown:
614         {
615             var_Get( p_vout, "mouse-button-down", &val );
616             val.i_int |= 4;
617             var_Set( p_vout, "mouse-button-down", val );
618         }
619         break;
620
621         default:
622             [super mouseDown: o_event];
623         break;
624     }
625 }
626
627 - (void)mouseUp:(NSEvent *)o_event
628 {
629     vlc_value_t val;
630
631     switch( [o_event type] )
632     {
633         case NSLeftMouseUp:
634         {
635             vlc_value_t b_val;
636             b_val.b_bool = VLC_TRUE;
637             var_Set( p_vout, "mouse-clicked", b_val );
638
639             var_Get( p_vout, "mouse-button-down", &val );
640             val.i_int &= ~1;
641             var_Set( p_vout, "mouse-button-down", val );
642         }
643         break;
644
645         default:
646             [super mouseUp: o_event];
647         break;
648     }
649 }
650
651 - (void)otherMouseUp:(NSEvent *)o_event
652 {
653     vlc_value_t val;
654
655     switch( [o_event type] )
656     {
657         case NSOtherMouseUp:
658         {
659             var_Get( p_vout, "mouse-button-down", &val );
660             val.i_int &= ~2;
661             var_Set( p_vout, "mouse-button-down", val );
662         }
663         break;
664
665         default:
666             [super mouseUp: o_event];
667         break;
668     }
669 }
670
671 - (void)rightMouseUp:(NSEvent *)o_event
672 {
673     vlc_value_t val;
674
675     switch( [o_event type] )
676     {
677         case NSRightMouseUp:
678         {
679             var_Get( p_vout, "mouse-button-down", &val );
680             val.i_int &= ~4;
681             var_Set( p_vout, "mouse-button-down", val );
682         }
683         break;
684
685         default:
686             [super mouseUp: o_event];
687         break;
688     }
689 }
690
691 - (void)mouseDragged:(NSEvent *)o_event
692 {
693     [self mouseMoved: o_event];
694 }
695
696 - (void)otherMouseDragged:(NSEvent *)o_event
697 {
698     [self mouseMoved: o_event];
699 }
700
701 - (void)rightMouseDragged:(NSEvent *)o_event
702 {
703     [self mouseMoved: o_event];
704 }
705
706 - (void)mouseMoved:(NSEvent *)o_event
707 {   
708     NSPoint ml;
709     NSRect s_rect;
710     BOOL b_inside;
711
712     i_time_mouse_last_moved = mdate();
713
714     s_rect = [o_view bounds];
715     ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
716     b_inside = [o_view mouse: ml inRect: s_rect];
717
718     if( b_inside )
719     {
720         vlc_value_t val;
721         unsigned int i_width, i_height, i_x, i_y;
722
723         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
724                                    (unsigned int)s_rect.size.height,
725                                    &i_x, &i_y, &i_width, &i_height );
726
727         val.i_int = ( ((int)ml.x) - i_x ) *  
728                     p_vout->render.i_width / i_width;
729         var_Set( p_vout, "mouse-x", val );
730
731         if( [[o_view className] isEqualToString: @"VLCGLView"] )
732         {
733             val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
734                         p_vout->render.i_height / i_height;
735         }
736         else
737         {
738             val.i_int = ( ((int)ml.y) - i_y ) * 
739                         p_vout->render.i_height / i_height;
740         }
741         var_Set( p_vout, "mouse-y", val );
742             
743         val.b_bool = VLC_TRUE;
744         var_Set( p_vout, "mouse-moved", val ); 
745     }
746
747     [super mouseMoved: o_event];
748 }
749
750 @end