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