]> git.sesse.net Git - vlc/blob - modules/gui/macosx/embeddedwindow.m
macosx-port: removed compatibility code for outdated versions of OS X
[vlc] / modules / gui / macosx / embeddedwindow.m
1 /*****************************************************************************
2  * embeddedwindow.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2005-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Benjamin Pracht <bigben at videolan dot org>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 /* DisableScreenUpdates, SetSystemUIMode, ... */
30 #import <QuickTime/QuickTime.h>
31
32 #import "intf.h"
33 #import "controls.h"
34 #import "vout.h"
35 #import "embeddedwindow.h"
36 #import "fspanel.h"
37
38 /*****************************************************************************
39  * VLCEmbeddedWindow Implementation
40  *****************************************************************************/
41
42 @implementation VLCEmbeddedWindow
43
44 - (void)awakeFromNib
45 {
46     [self setDelegate: self];
47
48     [o_btn_backward setToolTip: _NS("Rewind")];
49     [o_btn_forward setToolTip: _NS("Fast Forward")];
50     [o_btn_fullscreen setToolTip: _NS("Fullscreen")];
51     [o_btn_play setToolTip: _NS("Play")];
52     [o_slider setToolTip: _NS("Position")];
53
54     o_img_play = [NSImage imageNamed: @"play_embedded"];
55     o_img_pause = [NSImage imageNamed: @"pause_embedded"];
56     [self controlTintChanged];
57     [[NSNotificationCenter defaultCenter] addObserver: self
58                                              selector: @selector( controlTintChanged )
59                                                  name: NSControlTintDidChangeNotification
60                                                object: nil];
61
62     /* Useful to save o_view frame in fullscreen mode */
63     o_temp_view = [[NSView alloc] init];
64     [o_temp_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
65
66     o_fullscreen_window = nil;
67     o_fullscreen_anim1 = o_fullscreen_anim2 = nil;
68
69     /* Not fullscreen when we wake up */
70     [o_btn_fullscreen setState: NO];
71     b_fullscreen = NO;
72
73     [self setMovableByWindowBackground:YES];
74
75     [self setDelegate:self];
76
77     /* Make sure setVisible: returns NO */
78     [self orderOut:self];
79     b_window_is_invisible = YES;
80     videoRatio = NSMakeSize( 0., 0. );
81 }
82
83 - (void)controlTintChanged
84 {
85     BOOL b_playing = NO;
86     if( [o_btn_play alternateImage] == o_img_play_pressed )
87         b_playing = YES;
88     
89     if( [NSColor currentControlTint] == NSGraphiteControlTint )
90     {
91         o_img_play_pressed = [NSImage imageNamed: @"play_embedded_graphite"];
92         o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_graphite"];
93         [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_graphite"]];
94         [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_graphite"]];
95         [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_graphite"]];
96     }
97     else
98     {
99         o_img_play_pressed = [NSImage imageNamed: @"play_embedded_blue"];
100         o_img_pause_pressed = [NSImage imageNamed: @"pause_embedded_blue"];
101         [o_btn_backward setAlternateImage: [NSImage imageNamed: @"skip_previous_embedded_blue"]];
102         [o_btn_forward setAlternateImage: [NSImage imageNamed: @"skip_forward_embedded_blue"]];
103         [o_btn_fullscreen setAlternateImage: [NSImage imageNamed: @"fullscreen_blue"]];
104     }
105     
106     if( b_playing )
107         [o_btn_play setAlternateImage: o_img_play_pressed];
108     else
109         [o_btn_play setAlternateImage: o_img_pause_pressed];
110 }
111
112 - (void)dealloc
113 {
114     [[NSNotificationCenter defaultCenter] removeObserver: self];
115     [o_img_play release];
116     [o_img_play_pressed release];
117     [o_img_pause release];
118     [o_img_pause_pressed release];
119     
120     [super dealloc];
121 }
122
123 - (void)setTime:(NSString *)o_arg_time position:(float)f_position
124 {
125     [o_time setStringValue: o_arg_time];
126     [o_slider setFloatValue: f_position];
127 }
128
129 - (void)playStatusUpdated:(int)i_status
130 {
131     if( i_status == PLAYING_S )
132     {
133         [o_btn_play setImage: o_img_pause];
134         [o_btn_play setAlternateImage: o_img_pause_pressed];
135         [o_btn_play setToolTip: _NS("Pause")];
136     }
137     else
138     {
139         [o_btn_play setImage: o_img_play];
140         [o_btn_play setAlternateImage: o_img_play_pressed];
141         [o_btn_play setToolTip: _NS("Play")];
142     }
143 }
144
145 - (void)setSeekable:(BOOL)b_seekable
146 {
147     [o_btn_forward setEnabled: b_seekable];
148     [o_btn_backward setEnabled: b_seekable];
149     [o_slider setEnabled: b_seekable];
150 }
151
152 - (BOOL)windowShouldZoom:(NSWindow *)sender toFrame:(NSRect)newFrame
153 {
154     [self setFrame: newFrame display: YES animate: YES];
155     return NO;
156 }
157
158 - (BOOL)windowShouldClose:(id)sender
159 {
160     playlist_t * p_playlist = pl_Hold( VLCIntf );
161
162     playlist_Stop( p_playlist );
163     vlc_object_release( p_playlist );
164     return YES;
165 }
166
167 - (NSView *)mainView
168 {
169     if (o_fullscreen_window)
170         return o_temp_view;
171     else
172         return o_view;
173 }
174
175 - (void)setVideoRatio:(NSSize)ratio
176 {
177     videoRatio = ratio;
178 }
179
180 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
181 {
182     if( videoRatio.height == 0. || videoRatio.width == 0. )
183         return proposedFrameSize;
184
185     NSRect viewRect = [o_view convertRect:[o_view bounds] toView: nil];
186     NSRect contentRect = [self contentRectForFrameRect:[self frame]];
187     float marginy = viewRect.origin.y + [self frame].size.height - contentRect.size.height;
188     float marginx = contentRect.size.width - viewRect.size.width;
189     proposedFrameSize.height = (proposedFrameSize.width - marginx) * videoRatio.height / videoRatio.width + marginy;
190
191     return proposedFrameSize;
192 }
193
194 /*****************************************************************************
195  * Fullscreen support
196  */
197
198 - (BOOL)isFullscreen
199 {
200     return b_fullscreen;
201 }
202
203 - (void)lockFullscreenAnimation
204 {
205     [o_animation_lock lock];
206 }
207
208 - (void)unlockFullscreenAnimation
209 {
210     [o_animation_lock unlock];
211 }
212
213 - (void)enterFullscreen
214 {
215     NSMutableDictionary *dict1, *dict2;
216     NSScreen *screen;
217     NSRect screen_rect;
218     NSRect rect;
219     vout_thread_t *p_vout = vlc_object_find( VLCIntf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
220     BOOL blackout_other_displays = config_GetInt( VLCIntf, "macosx-black" );
221
222     screen = [NSScreen screenWithDisplayID:(CGDirectDisplayID)var_GetInteger( p_vout, "video-device" )]; 
223  
224     [self lockFullscreenAnimation];
225
226     if (!screen)
227     {
228         msg_Dbg( p_vout, "chosen screen isn't present, using current screen for fullscreen mode" );
229         screen = [self screen];
230     }
231     if (!screen)
232     {
233         msg_Dbg( p_vout, "Using deepest screen" );
234         screen = [NSScreen deepestScreen];
235     }
236
237     vlc_object_release( p_vout );
238
239     screen_rect = [screen frame];
240
241     [o_btn_fullscreen setState: YES];
242
243     [NSCursor setHiddenUntilMouseMoves: YES];
244  
245     if( blackout_other_displays )        
246         [screen blackoutOtherScreens];
247
248     /* Make sure we don't see the window flashes in float-on-top mode */
249     originalLevel = [self level];
250     [self setLevel:NSNormalWindowLevel];
251
252     /* Only create the o_fullscreen_window if we are not in the middle of the zooming animation */
253     if (!o_fullscreen_window)
254     {
255         /* We can't change the styleMask of an already created NSWindow, so we create an other window, and do eye catching stuff */
256
257         rect = [[o_view superview] convertRect: [o_view frame] toView: nil]; /* Convert to Window base coord */
258         rect.origin.x += [self frame].origin.x;
259         rect.origin.y += [self frame].origin.y;
260         o_fullscreen_window = [[VLCWindow alloc] initWithContentRect:rect styleMask: NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
261         [o_fullscreen_window setBackgroundColor: [NSColor blackColor]];
262         [o_fullscreen_window setCanBecomeKeyWindow: YES];
263
264         if (![self isVisible] || [self alphaValue] == 0.0)
265         {
266             /* We don't animate if we are not visible, instead we
267              * simply fade the display */
268             CGDisplayFadeReservationToken token;
269  
270             CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
271             CGDisplayFade( token, 0.5, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
272  
273             if ([screen isMainScreen])
274                 SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
275  
276             [[self contentView] replaceSubview:o_view with:o_temp_view];
277             [o_temp_view setFrame:[o_view frame]];
278             [o_fullscreen_window setContentView:o_view];
279
280             [o_fullscreen_window makeKeyAndOrderFront:self];
281             [o_fullscreen_window orderFront:self animate:YES];
282
283             [o_fullscreen_window setFrame:screen_rect display:YES];
284
285             CGDisplayFade( token, 0.3, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
286             CGReleaseDisplayFadeReservation( token);
287
288             /* Will release the lock */
289             [self hasBecomeFullscreen];
290
291             return;
292         }
293  
294         /* Make sure we don't see the o_view disappearing of the screen during this operation */
295         DisableScreenUpdates();
296         [[self contentView] replaceSubview:o_view with:o_temp_view];
297         [o_temp_view setFrame:[o_view frame]];
298         [o_fullscreen_window setContentView:o_view];
299         [o_fullscreen_window makeKeyAndOrderFront:self];
300         EnableScreenUpdates();
301     }
302
303     /* We are in fullscreen (and no animation is running) */
304     if (b_fullscreen)
305     {
306         /* Make sure we are hidden */
307         [super orderOut: self];
308         [self unlockFullscreenAnimation];
309         return;
310     }
311
312     if (o_fullscreen_anim1)
313     {
314         [o_fullscreen_anim1 stopAnimation];
315         [o_fullscreen_anim1 release];
316     }
317     if (o_fullscreen_anim2)
318     {
319         [o_fullscreen_anim2 stopAnimation];
320         [o_fullscreen_anim2 release];
321     }
322  
323     if ([screen isMainScreen])
324         SetSystemUIMode( kUIModeAllHidden, kUIOptionAutoShowMenuBar);
325
326     dict1 = [[NSMutableDictionary alloc] initWithCapacity:2];
327     dict2 = [[NSMutableDictionary alloc] initWithCapacity:3];
328
329     [dict1 setObject:self forKey:NSViewAnimationTargetKey];
330     [dict1 setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];
331
332     [dict2 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
333     [dict2 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
334     [dict2 setObject:[NSValue valueWithRect:screen_rect] forKey:NSViewAnimationEndFrameKey];
335
336     /* Strategy with NSAnimation allocation:
337         - Keep at most 2 animation at a time
338         - leaveFullscreen/enterFullscreen are the only responsible for releasing and alloc-ing
339     */
340     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
341     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
342
343     [dict1 release];
344     [dict2 release];
345
346     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
347     [o_fullscreen_anim1 setDuration: 0.3];
348     [o_fullscreen_anim1 setFrameRate: 30];
349     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
350     [o_fullscreen_anim2 setDuration: 0.2];
351     [o_fullscreen_anim2 setFrameRate: 30];
352
353     [o_fullscreen_anim2 setDelegate: self];
354     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
355
356     [o_fullscreen_anim1 startAnimation];
357     /* fullscreenAnimation will be unlocked when animation ends */
358 }
359
360 - (void)hasBecomeFullscreen
361 {
362     [o_fullscreen_window makeFirstResponder: [[[VLCMain sharedInstance] getControls] voutView]];
363
364     [o_fullscreen_window makeKeyWindow];
365     [o_fullscreen_window setAcceptsMouseMovedEvents: TRUE];
366
367     /* tell the fspanel to move itself to front next time it's triggered */
368     [[[[VLCMain sharedInstance] getControls] getFSPanel] setVoutWasUpdated: (int)[[o_fullscreen_window screen] displayID]];
369
370     if([self isVisible])
371         [super orderOut: self];
372
373     [[[[VLCMain sharedInstance] getControls] getFSPanel] setActive: nil];
374
375     b_fullscreen = YES;
376     [self unlockFullscreenAnimation];
377 }
378
379 - (void)leaveFullscreen
380 {
381     [self leaveFullscreenAndFadeOut: NO];
382 }
383
384 - (void)leaveFullscreenAndFadeOut: (BOOL)fadeout
385 {
386     NSMutableDictionary *dict1, *dict2;
387     NSRect frame;
388
389     [self lockFullscreenAnimation];
390
391     b_fullscreen = NO;
392     [o_btn_fullscreen setState: NO];
393
394     /* We always try to do so */
395     [NSScreen unblackoutScreens];
396
397     /* Don't do anything if o_fullscreen_window is already closed */
398     if (!o_fullscreen_window)
399     {
400         [self unlockFullscreenAnimation];
401         return;
402     }
403
404     if (fadeout)
405     {
406         /* We don't animate if we are not visible, instead we
407         * simply fade the display */
408         CGDisplayFadeReservationToken token;
409
410         CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval, &token);
411         CGDisplayFade( token, 0.3, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, 0, 0, 0, YES );
412
413         [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
414         SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
415
416         /* Will release the lock */
417         [self hasEndedFullscreen];
418
419         /* Our window is hidden, and might be faded. We need to workaround that, so note it
420          * here */
421         b_window_is_invisible = YES;
422
423         CGDisplayFade( token, 0.5, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal, 0, 0, 0, NO );
424         CGReleaseDisplayFadeReservation( token);
425         return;
426     }
427
428     [self setAlphaValue: 0.0];
429     [self orderFront: self];
430
431     [[[[VLCMain sharedInstance] getControls] getFSPanel] setNonActive: nil];
432     SetSystemUIMode( kUIModeNormal, kUIOptionAutoShowMenuBar);
433
434     if (o_fullscreen_anim1)
435     {
436         [o_fullscreen_anim1 stopAnimation];
437         [o_fullscreen_anim1 release];
438     }
439     if (o_fullscreen_anim2)
440     {
441         [o_fullscreen_anim2 stopAnimation];
442         [o_fullscreen_anim2 release];
443     }
444
445     frame = [[o_temp_view superview] convertRect: [o_temp_view frame] toView: nil]; /* Convert to Window base coord */
446     frame.origin.x += [self frame].origin.x;
447     frame.origin.y += [self frame].origin.y;
448
449     dict2 = [[NSMutableDictionary alloc] initWithCapacity:2];
450     [dict2 setObject:self forKey:NSViewAnimationTargetKey];
451     [dict2 setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
452
453     o_fullscreen_anim2 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict2, nil]];
454     [dict2 release];
455
456     [o_fullscreen_anim2 setAnimationBlockingMode: NSAnimationNonblocking];
457     [o_fullscreen_anim2 setDuration: 0.3];
458     [o_fullscreen_anim2 setFrameRate: 30];
459
460     [o_fullscreen_anim2 setDelegate: self];
461
462     dict1 = [[NSMutableDictionary alloc] initWithCapacity:3];
463
464     [dict1 setObject:o_fullscreen_window forKey:NSViewAnimationTargetKey];
465     [dict1 setObject:[NSValue valueWithRect:[o_fullscreen_window frame]] forKey:NSViewAnimationStartFrameKey];
466     [dict1 setObject:[NSValue valueWithRect:frame] forKey:NSViewAnimationEndFrameKey];
467
468     o_fullscreen_anim1 = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict1, nil]];
469     [dict1 release];
470
471     [o_fullscreen_anim1 setAnimationBlockingMode: NSAnimationNonblocking];
472     [o_fullscreen_anim1 setDuration: 0.2];
473     [o_fullscreen_anim1 setFrameRate: 30];
474     [o_fullscreen_anim2 startWhenAnimation: o_fullscreen_anim1 reachesProgress: 1.0];
475
476     /* Make sure o_fullscreen_window is the frontmost window */
477     [o_fullscreen_window orderFront: self];
478
479     [o_fullscreen_anim1 startAnimation];
480     /* fullscreenAnimation will be unlocked when animation ends */
481 }
482
483 - (void)hasEndedFullscreen
484 {
485     /* This function is private and should be only triggered at the end of the fullscreen change animation */
486     /* Make sure we don't see the o_view disappearing of the screen during this operation */
487     DisableScreenUpdates();
488     [o_view retain];
489     [o_view removeFromSuperviewWithoutNeedingDisplay];
490     [[self contentView] replaceSubview:o_temp_view with:o_view];
491     [o_view release];
492     [o_view setFrame:[o_temp_view frame]];
493     [self makeFirstResponder: o_view];
494     if ([self isVisible])
495         [super makeKeyAndOrderFront:self]; /* our version contains a workaround */
496     [o_fullscreen_window orderOut: self];
497     EnableScreenUpdates();
498
499     [o_fullscreen_window release];
500     o_fullscreen_window = nil;
501     [self setLevel:originalLevel];
502
503     [self unlockFullscreenAnimation];
504 }
505
506 - (void)animationDidEnd:(NSAnimation*)animation
507 {
508     NSArray *viewAnimations;
509
510     if ([animation currentValue] < 1.0)
511         return;
512
513     /* Fullscreen ended or started (we are a delegate only for leaveFullscreen's/enterFullscren's anim2) */
514     viewAnimations = [o_fullscreen_anim2 viewAnimations];
515     if ([viewAnimations count] >=1 &&
516         [[[viewAnimations objectAtIndex: 0] objectForKey: NSViewAnimationEffectKey] isEqualToString:NSViewAnimationFadeInEffect])
517     {
518         /* Fullscreen ended */
519         [self hasEndedFullscreen];
520     }
521     else
522     {
523         /* Fullscreen started */
524         [self hasBecomeFullscreen];
525     }
526 }
527
528 - (void)orderOut: (id)sender
529 {
530     [super orderOut: sender];
531
532     /* Make sure we leave fullscreen */
533     [self leaveFullscreenAndFadeOut: YES];
534 }
535
536 - (void)makeKeyAndOrderFront: (id)sender
537 {
538     /* Hack
539      * when we exit fullscreen and fade out, we may endup in
540      * having a window that is faded. We can't have it fade in unless we
541      * animate again. */
542
543     if(!b_window_is_invisible)
544     {
545         /* Make sure we don't do it too much */
546         [super makeKeyAndOrderFront: sender];
547         return;
548     }
549
550     [super setAlphaValue:0.0f];
551     [super makeKeyAndOrderFront: sender];
552
553     NSMutableDictionary * dict = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
554     [dict setObject:self forKey:NSViewAnimationTargetKey];
555     [dict setObject:NSViewAnimationFadeInEffect forKey:NSViewAnimationEffectKey];
556
557     NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:dict]];
558
559     [anim setAnimationBlockingMode: NSAnimationNonblocking];
560     [anim setDuration: 0.1];
561     [anim setFrameRate: 30];
562
563     [anim startAnimation];
564     b_window_is_invisible = NO;
565
566     /* fullscreenAnimation will be unlocked when animation ends */
567 }
568
569
570
571 /* Make sure setFrame gets executed on main thread especially if we are animating.
572  * (Thus we won't block the video output thread) */
573 - (void)setFrame:(NSRect)frame display:(BOOL)display animate:(BOOL)animate
574 {
575     struct { NSRect frame; BOOL display; BOOL animate;} args;
576     NSData *packedargs;
577
578     args.frame = frame;
579     args.display = display;
580     args.animate = animate;
581
582     packedargs = [NSData dataWithBytes:&args length:sizeof(args)];
583
584     [self performSelectorOnMainThread:@selector(setFrameOnMainThread:)
585                     withObject: packedargs waitUntilDone: YES];
586 }
587
588 - (void)setFrameOnMainThread:(NSData*)packedargs
589 {
590     struct args { NSRect frame; BOOL display; BOOL animate; } * args = (struct args*)[packedargs bytes];
591
592     if( args->animate )
593     {
594         /* Make sure we don't block too long and set up a non blocking animation */
595         NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
596             self, NSViewAnimationTargetKey,
597             [NSValue valueWithRect:[self frame]], NSViewAnimationStartFrameKey,
598             [NSValue valueWithRect:args->frame], NSViewAnimationEndFrameKey, nil];
599
600         NSViewAnimation * anim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]];
601
602         [anim setAnimationBlockingMode: NSAnimationNonblocking];
603         [anim setDuration: 0.4];
604         [anim setFrameRate: 30];
605         [anim startAnimation];
606     }
607     else {
608         [super setFrame:args->frame display:args->display animate:args->animate];
609     }
610
611 }
612 @end