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