]> git.sesse.net Git - vlc/blob - modules/gui/macosx/vout.m
macosx/vout*: fixed escape key not exiting fullscreen and video-on-top
[vlc] / modules / gui / macosx / vout.m
1 /*****************************************************************************
2  * vout.m: MacOS X video output module
3  *****************************************************************************
4  * Copyright (C) 2001-2003 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  * VLCWindow implementation
45  *****************************************************************************/
46 @implementation VLCWindow
47
48 - (id)initWithVout:(vout_thread_t *)_p_vout frame:(NSRect *)s_frame
49 {
50     [self setReleasedWhenClosed: YES];
51
52     p_vout = _p_vout;
53
54     /* p_real_vout: the vout we have to use to check for video-on-top
55        and a few other things. If we are the QuickTime output, it's us.
56        It we are the OpenGL provider, it is our parent. */
57     if( p_vout->i_object_type == VLC_OBJECT_OPENGL )
58     {
59         p_real_vout = (vout_thread_t *) p_vout->p_parent;
60     }
61     else
62     {
63         p_real_vout = p_vout;
64     }
65
66     p_fullscreen_state = NULL;
67     i_time_mouse_last_moved = mdate();
68
69     NSScreen * o_screen;
70     vlc_bool_t b_main_screen;
71
72     /* Find out on which screen to open the window */
73     int i_device = var_GetInteger( p_vout, "video-device" );
74     if( i_device < 0 )
75     {
76          /* No preference specified. Use the main screen */
77         o_screen = [NSScreen mainScreen];
78         b_main_screen = 1;
79     }
80     else
81     {
82         NSArray *o_screens = [NSScreen screens];
83         
84         if( [o_screens count] < (unsigned) i_device )
85         {
86             o_screen = [NSScreen mainScreen];
87             b_main_screen = 1;
88         }
89         else
90         {
91             i_device--;
92             o_screen = [o_screens objectAtIndex: i_device];
93             var_SetInteger( p_vout, "macosx-vdev", i_device );
94             b_main_screen = ( i_device == 0 );
95         }
96     }
97
98     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
99
100     if( p_vout->b_fullscreen )
101     {
102         NSRect screen_rect = [o_screen frame];
103         screen_rect.origin.x = screen_rect.origin.y = 0;
104
105         /* Creates a window with size: screen_rect on o_screen */
106         [self initWithContentRect: screen_rect
107               styleMask: NSBorderlessWindowMask
108               backing: NSBackingStoreBuffered
109               defer: YES screen: o_screen];
110
111         if( b_main_screen )
112         {
113             BeginFullScreen( &p_fullscreen_state, NULL, 0, 0,
114                              NULL, NULL, fullScreenAllowEvents );
115         }
116     }
117     else
118     {
119         unsigned int i_stylemask = NSTitledWindowMask |
120                                    NSMiniaturizableWindowMask |
121                                    NSClosableWindowMask |
122                                    NSResizableWindowMask;
123
124         NSRect s_rect;
125         if( !s_frame )
126         {
127             s_rect.size.width  = p_vout->i_window_width;
128             s_rect.size.height = p_vout->i_window_height;
129         }
130         else
131         {
132             s_rect = *s_frame;
133         }
134        
135         [self initWithContentRect: s_rect
136               styleMask: i_stylemask
137               backing: NSBackingStoreBuffered
138               defer: YES screen: o_screen];
139
140         [self setAlphaValue: var_GetFloat( p_vout, "macosx-opaqueness" )];
141
142         if( var_GetBool( p_real_vout, "video-on-top" ) )
143         {
144             [self setLevel: NSStatusWindowLevel];
145         }
146
147         if( !s_frame )
148         {
149             [self center];
150         }
151     }
152
153     [self updateTitle];
154     [self makeKeyAndOrderFront: nil];
155
156     /* We'll catch mouse events */
157     [self setAcceptsMouseMovedEvents: YES];
158     [self makeFirstResponder: self];
159     
160     [o_pool release];
161     return self;
162 }
163
164 - (void)close
165 {
166     if( p_fullscreen_state )
167     {
168         EndFullScreen( p_fullscreen_state, NULL );
169     }
170     [super close];
171 }
172
173 - (void)setOnTop:(bool)b_on_top
174 {
175     if( b_on_top )
176     {
177         [self setLevel: NSStatusWindowLevel];
178     }
179     else
180     {
181         [self setLevel: NSNormalWindowLevel];
182     }
183 }
184
185 - (void)hideMouse:(bool)b_hide
186 {
187     BOOL b_inside;
188     NSPoint ml;
189     NSView *o_contents = [self contentView];
190     
191     ml = [self convertScreenToBase:[NSEvent mouseLocation]];
192     ml = [o_contents convertPoint:ml fromView:nil];
193     b_inside = [o_contents mouse: ml inRect: [o_contents bounds]];
194     
195     if( b_hide && b_inside )
196     {
197         [NSCursor setHiddenUntilMouseMoves: YES];
198     }
199     else if( !b_hide )
200     {
201         [NSCursor setHiddenUntilMouseMoves: NO];
202     }
203 }
204
205 - (void)manage
206 {
207     if( p_fullscreen_state )
208     {
209         if( mdate() - i_time_mouse_last_moved > 3000000 )
210         {
211             [self hideMouse: YES];
212         }
213     }
214     else
215     {
216         [self hideMouse: NO];
217     }
218
219     /* Disable screensaver */
220     UpdateSystemActivity( UsrActivity );
221 }
222
223 - (void)scaleWindowWithFactor: (float)factor
224 {
225     NSSize newsize;
226     int i_corrected_height, i_corrected_width;
227     NSPoint topleftbase;
228     NSPoint topleftscreen;
229     
230     if ( !p_vout->b_fullscreen )
231     {
232         topleftbase.x = 0;
233         topleftbase.y = [self frame].size.height;
234         topleftscreen = [self convertBaseToScreen: topleftbase];
235         
236         if( p_vout->render.i_height * p_vout->render.i_aspect > 
237                         p_vout->render.i_width * VOUT_ASPECT_FACTOR )
238         {
239             i_corrected_width = p_vout->render.i_height * p_vout->render.i_aspect /
240                                             VOUT_ASPECT_FACTOR;
241             newsize.width = (int) ( i_corrected_width * factor );
242             newsize.height = (int) ( p_vout->render.i_height * factor );
243         }
244         else
245         {
246             i_corrected_height = p_vout->render.i_width * VOUT_ASPECT_FACTOR /
247                                             p_vout->render.i_aspect;
248             newsize.width = (int) ( p_vout->render.i_width * factor );
249             newsize.height = (int) ( i_corrected_height * factor );
250         }
251     
252         [self setContentSize: newsize];
253         
254         [self setFrameTopLeftPoint: topleftscreen];
255         p_vout->i_changes |= VOUT_SIZE_CHANGE;
256     }
257 }
258
259 - (void)toggleFloatOnTop
260 {
261     vlc_value_t val;
262     if( var_Get( p_vout, "video-on-top", &val )>=0 && val.b_bool)
263     {
264         val.b_bool = VLC_FALSE;
265         var_Set( p_vout, "video-on-top", val );
266     }
267     else
268     {
269         val.b_bool = VLC_TRUE;
270         var_Set( p_vout, "video-on-top", val );
271     }
272 }
273
274 - (void)toggleFullscreen
275 {
276     vlc_value_t val;
277     val.b_bool = !p_real_vout->b_fullscreen;
278     var_Set( p_real_vout, "fullscreen", val );
279 }
280
281 - (BOOL)isFullscreen
282 {
283     return( p_vout->b_fullscreen );
284 }
285
286 - (BOOL)canBecomeKeyWindow
287 {
288     return YES;
289 }
290
291 /* Sometimes crashes VLC....
292 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
293 {
294         return [[VLCMain sharedInstance] hasDefinedShortcutKey:o_event];
295 }*/
296
297 - (void)keyDown:(NSEvent *)o_event
298 {
299     unichar key = 0;
300     vlc_value_t val;
301     unsigned int i_pressed_modifiers = 0;
302     val.i_int = 0;
303     
304     i_pressed_modifiers = [o_event modifierFlags];
305
306     if( i_pressed_modifiers & NSShiftKeyMask )
307         val.i_int |= KEY_MODIFIER_SHIFT;
308     if( i_pressed_modifiers & NSControlKeyMask )
309         val.i_int |= KEY_MODIFIER_CTRL;
310     if( i_pressed_modifiers & NSAlternateKeyMask )
311         val.i_int |= KEY_MODIFIER_ALT;
312     if( i_pressed_modifiers & NSCommandKeyMask )
313         val.i_int |= KEY_MODIFIER_COMMAND;
314
315     key = [[o_event charactersIgnoringModifiers] characterAtIndex: 0];
316
317     if( key )
318     {
319         /* Escape should always get you out of fullscreen */
320         if( key == (unichar) 0x1b )
321         {
322              if( [self isFullscreen] )
323              {
324                  [self toggleFullscreen];
325              }
326         }
327         else if ( key == ' ' )
328         {
329             vlc_value_t val;
330             val.i_int = config_GetInt( p_vout, "key-play-pause" );
331             var_Set( p_vout->p_vlc, "key-pressed", val );
332         }
333         else
334         {
335             val.i_int |= CocoaKeyToVLC( key );
336             var_Set( p_vout->p_vlc, "key-pressed", val );
337         }
338     }
339     else
340     {
341         [super keyDown: o_event];
342     }
343 }
344
345 - (void)updateTitle
346 {
347     NSMutableString * o_title;
348     playlist_t * p_playlist;
349     
350     if( p_vout == NULL )
351     {
352         return;
353     }
354     
355     p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
356                                                 FIND_ANYWHERE );
357     
358     if( p_playlist == NULL )
359     {
360         return;
361     }
362
363     vlc_mutex_lock( &p_playlist->object_lock );
364     o_title = [NSMutableString stringWithUTF8String: 
365         p_playlist->status.p_item->input.psz_uri]; 
366     vlc_mutex_unlock( &p_playlist->object_lock );
367     vlc_object_release( p_playlist );
368
369     if( o_title != nil )
370     {
371         NSRange prefix_range = [o_title rangeOfString: @"file:"];
372         if( prefix_range.location != NSNotFound )
373         {
374             [o_title deleteCharactersInRange: prefix_range];
375         }
376
377         [self setTitleWithRepresentedFilename: o_title];
378     }
379     else
380     {
381         [self setTitle: [NSString stringWithCString: VOUT_TITLE]];
382     }
383 }
384
385 /* This is actually the same as VLCControls::stop. */
386 - (BOOL)windowShouldClose:(id)sender
387 {
388     playlist_t * p_playlist = vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
389                                                        FIND_ANYWHERE );
390     if( p_playlist == NULL )      
391     {
392         return NO;
393     }
394
395     playlist_Stop( p_playlist );
396     vlc_object_release( p_playlist );
397
398     /* The window will be closed by the intf later. */
399     return NO;
400 }
401
402 - (BOOL)acceptsFirstResponder
403 {
404     return YES;
405 }
406
407 - (BOOL)becomeFirstResponder
408 {
409     return YES;
410 }
411
412 - (BOOL)resignFirstResponder
413 {
414     /* We need to stay the first responder or we'll miss some
415        events */
416     return NO;
417 }
418
419 - (void)mouseDown:(NSEvent *)o_event
420 {
421     vlc_value_t val;
422
423     switch( [o_event type] )
424     {
425         case NSLeftMouseDown:
426         {
427             var_Get( p_vout, "mouse-button-down", &val );
428             val.i_int |= 1;
429             var_Set( p_vout, "mouse-button-down", val );
430         }
431         break;
432
433         default:
434             [super mouseDown: o_event];
435         break;
436     }
437 }
438
439 - (void)otherMouseDown:(NSEvent *)o_event
440 {
441     vlc_value_t val;
442
443     switch( [o_event type] )
444     {
445         case NSOtherMouseDown:
446         {
447             var_Get( p_vout, "mouse-button-down", &val );
448             val.i_int |= 2;
449             var_Set( p_vout, "mouse-button-down", val );
450         }
451         break;
452
453         default:
454             [super mouseDown: o_event];
455         break;
456     }
457 }
458
459 - (void)rightMouseDown:(NSEvent *)o_event
460 {
461     vlc_value_t val;
462
463     switch( [o_event type] )
464     {
465         case NSRightMouseDown:
466         {
467             var_Get( p_vout, "mouse-button-down", &val );
468             val.i_int |= 4;
469             var_Set( p_vout, "mouse-button-down", val );
470         }
471         break;
472
473         default:
474             [super mouseDown: o_event];
475         break;
476     }
477 }
478
479 - (void)mouseUp:(NSEvent *)o_event
480 {
481     vlc_value_t val;
482
483     switch( [o_event type] )
484     {
485         case NSLeftMouseUp:
486         {
487             vlc_value_t b_val;
488             b_val.b_bool = VLC_TRUE;
489             var_Set( p_vout, "mouse-clicked", b_val );
490
491             var_Get( p_vout, "mouse-button-down", &val );
492             val.i_int &= ~1;
493             var_Set( p_vout, "mouse-button-down", val );
494         }
495         break;
496
497         default:
498             [super mouseUp: o_event];
499         break;
500     }
501 }
502
503 - (void)otherMouseUp:(NSEvent *)o_event
504 {
505     vlc_value_t val;
506
507     switch( [o_event type] )
508     {
509         case NSOtherMouseUp:
510         {
511             var_Get( p_vout, "mouse-button-down", &val );
512             val.i_int &= ~2;
513             var_Set( p_vout, "mouse-button-down", val );
514         }
515         break;
516
517         default:
518             [super mouseUp: o_event];
519         break;
520     }
521 }
522
523 - (void)rightMouseUp:(NSEvent *)o_event
524 {
525     vlc_value_t val;
526
527     switch( [o_event type] )
528     {
529         case NSRightMouseUp:
530         {
531             var_Get( p_vout, "mouse-button-down", &val );
532             val.i_int &= ~4;
533             var_Set( p_vout, "mouse-button-down", val );
534         }
535         break;
536
537         default:
538             [super mouseUp: o_event];
539         break;
540     }
541 }
542
543 - (void)mouseDragged:(NSEvent *)o_event
544 {
545     [self mouseMoved: o_event];
546 }
547
548 - (void)otherMouseDragged:(NSEvent *)o_event
549 {
550     [self mouseMoved: o_event];
551 }
552
553 - (void)rightMouseDragged:(NSEvent *)o_event
554 {
555     [self mouseMoved: o_event];
556 }
557
558 - (void)mouseMoved:(NSEvent *)o_event
559 {   
560     NSPoint ml;
561     NSRect s_rect;
562     BOOL b_inside;
563     NSView * o_view;
564
565     i_time_mouse_last_moved = mdate();
566
567     o_view = [self contentView];
568     s_rect = [o_view bounds];
569     ml = [o_view convertPoint: [o_event locationInWindow] fromView: nil];
570     b_inside = [o_view mouse: ml inRect: s_rect];
571
572     if( b_inside )
573     {
574         vlc_value_t val;
575         int i_width, i_height, i_x, i_y;
576
577         vout_PlacePicture( p_vout, (unsigned int)s_rect.size.width,
578                                    (unsigned int)s_rect.size.height,
579                                    &i_x, &i_y, &i_width, &i_height );
580
581         val.i_int = ( ((int)ml.x) - i_x ) *  
582                     p_vout->render.i_width / i_width;
583         var_Set( p_vout, "mouse-x", val );
584
585         if( [[o_view className] isEqualToString: @"VLCGLView"] )
586         {
587             val.i_int = ( ((int)(s_rect.size.height - ml.y)) - i_y ) *
588                         p_vout->render.i_height / i_height;
589         }
590         else
591         {
592             val.i_int = ( ((int)ml.y) - i_y ) * 
593                         p_vout->render.i_height / i_height;
594         }
595         var_Set( p_vout, "mouse-y", val );
596             
597         val.b_bool = VLC_TRUE;
598         var_Set( p_vout, "mouse-moved", val ); 
599     }
600
601     [super mouseMoved: o_event];
602 }
603
604 @end