1 /*****************************************************************************
2 * fspanel.m: MacOS X full screen panel
3 *****************************************************************************
4 * Copyright (C) 2006-2013 VLC authors and VideoLAN
7 * Authors: Jérôme Decoodt <djc at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
9 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
30 #import "CoreInteraction.h"
31 #import "MainWindow.h"
34 #import "CompatibilityFixes.h"
36 @interface VLCFSPanel ()
40 /*****************************************************************************
42 *****************************************************************************/
43 @implementation VLCFSPanel
44 /* We override this initializer so we can set the NSBorderlessWindowMask styleMask, and set a few other important settings */
45 - (id)initWithContentRect:(NSRect)contentRect
46 styleMask:(NSUInteger)aStyle
47 backing:(NSBackingStoreType)bufferingType
50 id win = [super initWithContentRect:contentRect styleMask:NSTexturedBackgroundWindowMask backing:bufferingType defer:flag];
52 [win setHasShadow: NO];
53 [win setBackgroundColor:[NSColor clearColor]];
54 if (!OSX_SNOW_LEOPARD)
55 [win setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
57 /* let the window sit on top of everything else and start out completely transparent */
58 [win setLevel:NSModalPanelWindowLevel];
59 i_device = config_GetInt(VLCIntf, "macosx-vdev");
60 hideAgainTimer = fadeTimer = nil;
61 [self setNonActive:nil];
67 [self setContentView:[[VLCFSPanelView alloc] initWithFrame: [self frame]]];
68 BOOL isInside = (NSPointInRect([NSEvent mouseLocation],[self frame]));
69 [[self contentView] addTrackingRect:[[self contentView] bounds] owner:self userData:nil assumeInside:isInside];
71 [self mouseEntered:NULL];
73 [self mouseExited:NULL];
75 if (!OSX_SNOW_LEOPARD)
76 [self setAnimationBehavior:NSWindowAnimationBehaviorNone];
78 /* get a notification if VLC isn't the active app anymore */
79 [[NSNotificationCenter defaultCenter]
81 selector: @selector(setNonActive:)
82 name: NSApplicationDidResignActiveNotification
85 /* Get a notification if VLC is the active app again.
86 Needed as becomeKeyWindow does not get called when window is activated by clicking */
87 [[NSNotificationCenter defaultCenter]
89 selector: @selector(setActive:)
90 name: NSApplicationDidBecomeActiveNotification
94 /* make sure that we don't become key, since we can't handle hotkeys */
95 - (BOOL)canBecomeKeyWindow
100 - (BOOL)mouseDownCanMoveWindow
107 [[NSNotificationCenter defaultCenter] removeObserver: self];
109 if (hideAgainTimer) {
110 [hideAgainTimer invalidate];
111 [hideAgainTimer release];
115 [o_vout_window release];
117 [self setFadeTimer:nil];
123 /* centre the panel in the lower third of the screen */
124 NSPoint theCoordinate;
125 NSRect theScreensFrame;
126 NSRect theWindowsFrame;
129 /* user-defined screen */
130 screen = [NSScreen screenWithDisplayID: (CGDirectDisplayID)i_device];
133 /* invalid preferences or none specified, using main screen */
134 screen = [NSScreen mainScreen];
136 theScreensFrame = [screen frame];
137 theWindowsFrame = [self frame];
139 theCoordinate.x = (theScreensFrame.size.width - theWindowsFrame.size.width) / 2 + theScreensFrame.origin.x;
140 theCoordinate.y = (theScreensFrame.size.height / 3) - theWindowsFrame.size.height + theScreensFrame.origin.y;
141 [self setFrameTopLeftPoint: theCoordinate];
146 [[self contentView] setPlay];
151 [[self contentView] setPause];
154 - (void)setStreamTitle:(NSString *)o_title
156 [[self contentView] setStreamTitle: o_title];
159 - (void)updatePositionAndTime
161 [[self contentView] updatePositionAndTime];
164 - (void)setSeekable:(BOOL) b_seekable
166 [[self contentView] setSeekable: b_seekable];
169 - (void)setVolumeLevel: (int)i_volumeLevel
171 [[self contentView] setVolumeLevel: i_volumeLevel];
174 - (void)setNonActive:(id)noData
178 /* here's fadeOut, just without visibly fading */
180 [self setAlphaValue:0.0];
181 [self setFadeTimer:nil];
185 [self orderOut: self];
188 - (void)setActive:(id)noData
192 [[VLCMain sharedInstance] showFullscreenController];
195 /* This routine is called repeatedly to fade in the window */
196 - (void)focus:(NSTimer *)timer
198 /* we need to push ourselves to front if the vout window was closed since our last display */
199 if (b_voutWasUpdated) {
200 [self orderFront: self];
201 b_voutWasUpdated = NO;
204 if ([self alphaValue] < 1.0) {
205 [self setAlphaValue:[self alphaValue]+0.1];
207 if ([self alphaValue] >= 1.0) {
209 [self setAlphaValue: 1.0];
210 [self setFadeTimer:nil];
213 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(unfocus:) userInfo:NULL repeats:YES]];
218 /* This routine is called repeatedly to hide the window */
219 - (void)unfocus:(NSTimer *)timer
224 [self setFadeTimer: NULL];
228 if ([self alphaValue] > 0.0) {
229 [self setAlphaValue:[self alphaValue]-0.05];
231 if ([self alphaValue] <= 0.05) {
233 [self setAlphaValue:0.0];
234 [self setFadeTimer:nil];
238 [NSTimer scheduledTimerWithTimeInterval:0.1
240 selector:@selector(focus:)
247 - (void)mouseExited:(NSEvent *)theEvent
249 /* give up our focus, so the vout may show us again without letting the user clicking it */
250 if (o_vout_window && var_GetBool(pl_Get(VLCIntf), "fullscreen"))
251 [o_vout_window makeKeyWindow];
256 [NSCursor setHiddenUntilMouseMoves: YES];
261 /* in case that the user don't want us to appear, make sure we hide the mouse */
263 if (!config_GetInt(VLCIntf, "macosx-fspanel")) {
264 float time = (float)var_CreateGetInteger(VLCIntf, "mouse-hide-timeout") / 1000.;
265 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(hideMouse) userInfo:nil repeats:NO]];
272 [self orderFront: nil];
274 if ([self alphaValue] < 1.0 || b_displayed != YES) {
275 if (![self fadeTimer])
276 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(focus:) userInfo:@1 repeats:YES]];
277 else if ([[[self fadeTimer] userInfo] shortValue]==0)
285 if (NSPointInRect([NSEvent mouseLocation],[self frame]))
288 if (([self alphaValue] > 0.0)) {
289 if (![self fadeTimer])
290 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(unfocus:) userInfo:@0 repeats:YES]];
291 else if ([[[self fadeTimer] userInfo] shortValue]==1)
296 /* triggers a timer to autoHide us again after some seconds of no activity */
299 /* this will tell the timer to start over again or to start at all */
302 /* get us a valid timer */
303 if (!b_alreadyCounting) {
304 i_timeToKeepVisibleInSec = var_CreateGetInteger(VLCIntf, "mouse-hide-timeout") / 500;
305 if (hideAgainTimer) {
306 [hideAgainTimer invalidate];
307 [hideAgainTimer autorelease];
309 /* released in -autoHide and -dealloc */
310 hideAgainTimer = [[NSTimer scheduledTimerWithTimeInterval: 0.5
312 selector: @selector(keepVisible:)
314 repeats: YES] retain];
315 b_alreadyCounting = YES;
319 - (void)keepVisible:(NSTimer *)timer
321 /* if the user triggered an action, start over again */
325 /* count down until we hide ourselfes again and do so if necessary */
326 if (--i_timeToKeepVisibleInSec < 1) {
329 [hideAgainTimer invalidate]; /* released in -autoHide and -dealloc */
330 b_alreadyCounting = NO;
334 /* A getter and setter for our main timer that handles window fading */
335 - (NSTimer *)fadeTimer
340 - (void)setFadeTimer:(NSTimer *)timer
343 [fadeTimer invalidate];
344 [fadeTimer autorelease];
348 - (void)mouseDown:(NSEvent *)theEvent
350 mouseClic = [theEvent locationInWindow];
353 - (void)mouseDragged:(NSEvent *)theEvent
355 NSPoint point = [NSEvent mouseLocation];
356 point.x -= mouseClic.x;
357 point.y -= mouseClic.y;
358 [self setFrameOrigin:point];
361 - (void)setVoutWasUpdated: (VLCWindow *)o_window
363 b_voutWasUpdated = YES;
365 [o_vout_window release];
366 o_vout_window = [o_window retain];
367 int i_newdevice = (int)[[o_vout_window screen] displayID];
368 if ((i_newdevice != i_device && i_device != 0) || i_newdevice != [[self screen] displayID]) {
369 i_device = i_newdevice;
372 i_device = i_newdevice;
376 /*****************************************************************************
378 *****************************************************************************/
379 @implementation VLCFSPanelView
381 #define addButton(o_button, imageOff, imageOn, _x, _y, action, AXDesc, ToolTip) \
382 s_rc.origin.x = _x; \
383 s_rc.origin.y = _y; \
384 o_button = [[NSButton alloc] initWithFrame: s_rc]; \
385 [o_button setButtonType: NSMomentaryChangeButton]; \
386 [o_button setBezelStyle: NSRegularSquareBezelStyle]; \
387 [o_button setBordered: NO]; \
388 [o_button setFont:[NSFont systemFontOfSize:0]]; \
389 [o_button setImage:[NSImage imageNamed:imageOff]]; \
390 [o_button setAlternateImage:[NSImage imageNamed:imageOn]]; \
391 [o_button sizeToFit]; \
392 [o_button setTarget: self]; \
393 [o_button setAction: @selector(action:)]; \
394 [[o_button cell] accessibilitySetOverrideValue:AXDesc forAttribute:NSAccessibilityDescriptionAttribute]; \
395 [[o_button cell] accessibilitySetOverrideValue:ToolTip forAttribute:NSAccessibilityTitleAttribute]; \
396 [o_button setToolTip: ToolTip]; \
397 [self addSubview:o_button];
399 #define addTextfield(class, o_text, align, font, color) \
400 o_text = [[class alloc] initWithFrame: s_rc]; \
401 [o_text setDrawsBackground: NO]; \
402 [o_text setBordered: NO]; \
403 [o_text setEditable: NO]; \
404 [o_text setSelectable: NO]; \
405 [o_text setStringValue: _NS("(no item is being played)")]; \
406 [o_text setAlignment: align]; \
407 [o_text setTextColor: [NSColor color]]; \
408 [o_text setFont:[NSFont font:[NSFont smallSystemFontSize]]]; \
409 [self addSubview:o_text];
411 - (id)initWithFrame:(NSRect)frameRect
413 id view = [super initWithFrame:frameRect];
414 fillColor = [[NSColor clearColor] retain];
415 NSRect s_rc = [self frame];
416 addButton(o_prev, @"fs_skip_previous_highlight" , @"fs_skip_previous", 174, 15, prev, _NS("Click to go to the previous playlist item."), _NS("Previous"));
417 addButton(o_bwd, @"fs_rewind_highlight" , @"fs_rewind" , 211, 14, backward, _NS("Click and hold to skip backward through the current media."), _NS("Backward"));
418 addButton(o_play, @"fs_play_highlight" , @"fs_play" , 265, 10, play, _NS("Click to play or pause the current media."), _NS("Play/Pause"));
419 addButton(o_fwd, @"fs_forward_highlight" , @"fs_forward" , 313, 14, forward, _NS("Click and hold to skip forward through the current media."), _NS("Forward"));
420 addButton(o_next, @"fs_skip_next_highlight" , @"fs_skip_next" , 365, 15, next, _NS("Click to go to the next playlist item."), _NS("Next"));
421 addButton(o_fullscreen, @"fs_exit_fullscreen_highlight", @"fs_exit_fullscreen", 507, 13, toggleFullscreen, _NS("Click to exit fullscreen playback."), _NS("Toggle Fullscreen mode"));
423 addButton(o_button, @"image (off state)", @"image (on state)", 38, 51, something, accessibility help string, usual tool tip);
425 [o_fwd setContinuous:YES];
426 [o_bwd setContinuous:YES];
429 // (surrounding progress view for swipe behaviour)
432 s_rc.size.width = 518;
433 s_rc.size.height = 13;
434 o_progress_view = [[VLCProgressView alloc] initWithFrame: s_rc];
437 o_fs_timeSlider = [[VLCFSTimeSlider alloc] initWithFrame: s_rc];
438 [o_fs_timeSlider setMinValue:0];
439 [o_fs_timeSlider setMaxValue:10000];
440 [o_fs_timeSlider setFloatValue: 0];
441 [o_fs_timeSlider setContinuous: YES];
442 [o_fs_timeSlider setTarget: self];
443 [o_fs_timeSlider setAction: @selector(fsTimeSliderUpdate:)];
444 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Position") forAttribute:NSAccessibilityTitleAttribute];
445 [[o_fs_timeSlider cell] accessibilitySetOverrideValue:_NS("Click and move the mouse while keeping the button pressed to use this slider to change current playback position.") forAttribute:NSAccessibilityDescriptionAttribute];
446 [self addSubview: o_progress_view];
447 [o_progress_view addSubview: o_fs_timeSlider];
453 s_rc.size.width = 95;
454 s_rc.size.height = 12;
455 o_fs_volumeSlider = [[VLCFSVolumeSlider alloc] initWithFrame: s_rc];
456 [o_fs_volumeSlider setMinValue:0];
457 [o_fs_volumeSlider setMaxValue:AOUT_VOLUME_MAX];
458 [o_fs_volumeSlider setIntValue:AOUT_VOLUME_DEFAULT];
459 [o_fs_volumeSlider setContinuous: YES];
460 [o_fs_volumeSlider setTarget: self];
461 [o_fs_volumeSlider setAction: @selector(fsVolumeSliderUpdate:)];
462 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Volume") forAttribute:NSAccessibilityTitleAttribute];
463 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Click and move the mouse while keeping the button pressed to use this slider to change the volume.") forAttribute:NSAccessibilityDescriptionAttribute];
464 [self addSubview: o_fs_volumeSlider];
466 /* time counter and stream title output fields */
468 // 10 px gap between time fields
471 s_rc.size.width = 361;
472 s_rc.size.height = 14;
473 addTextfield(NSTextField, o_streamTitle_txt, NSCenterTextAlignment, systemFontOfSize, whiteColor);
476 s_rc.size.width = 65;
477 addTextfield(VLCTimeField, o_streamPosition_txt, NSLeftTextAlignment, systemFontOfSize, whiteColor);
480 s_rc.size.width = 65;
481 addTextfield(VLCTimeField, o_streamLength_txt, NSRightTextAlignment, systemFontOfSize, whiteColor);
482 [o_streamLength_txt setRemainingIdentifier: @"DisplayFullscreenTimeAsTimeRemaining"];
484 o_background_img = [[NSImage imageNamed:@"fs_background"] retain];
485 o_vol_sld_img = [[NSImage imageNamed:@"fs_volume_slider_bar"] retain];
486 o_vol_mute_img = [[NSImage imageNamed:@"fs_volume_mute_highlight"] retain];
487 o_vol_max_img = [[NSImage imageNamed:@"fs_volume_max_highlight"] retain];
488 o_time_sld_img = [[NSImage imageNamed:@"fs_time_slider"] retain];
495 [o_background_img release];
496 [o_vol_sld_img release];
497 [o_vol_mute_img release];
498 [o_vol_max_img release];
499 [o_time_sld_img release];
500 [o_fs_timeSlider release];
501 [o_fs_volumeSlider release];
507 [o_fullscreen release];
508 [o_streamTitle_txt release];
509 [o_streamPosition_txt release];
515 [o_play setImage:[NSImage imageNamed:@"fs_play_highlight"]];
516 [o_play setAlternateImage: [NSImage imageNamed:@"fs_play"]];
521 [o_play setImage: [NSImage imageNamed:@"fs_pause_highlight"]];
522 [o_play setAlternateImage: [NSImage imageNamed:@"fs_pause"]];
525 - (void)setStreamTitle:(NSString *)o_title
527 [o_streamTitle_txt setStringValue: o_title];
530 - (void)updatePositionAndTime
532 input_thread_t * p_input;
533 p_input = pl_CurrentInput(VLCIntf);
539 var_Get(p_input, "position", &pos);
540 f_updated = 10000. * pos.f_float;
541 [o_fs_timeSlider setFloatValue: f_updated];
544 char psz_time[MSTRTIME_MAX_SIZE];
546 var_Get(p_input, "time", &time);
547 mtime_t dur = input_item_GetDuration(input_GetItem(p_input));
549 // update total duration (right field)
551 [o_streamLength_txt setHidden: YES];
553 [o_streamLength_txt setHidden: NO];
555 NSString *o_total_time;
556 if ([o_streamLength_txt timeRemaining]) {
557 mtime_t remaining = 0;
558 if (dur > time.i_time)
559 remaining = dur - time.i_time;
560 o_total_time = [NSString stringWithFormat: @"-%s", secstotimestr(psz_time, (remaining / 1000000))];
562 o_total_time = @(secstotimestr(psz_time, (dur / 1000000)));
564 [o_streamLength_txt setStringValue: o_total_time];
567 // update current position (left field)
568 NSString *o_playback_pos = @(secstotimestr(psz_time, (time.i_time / 1000000)));
570 [o_streamPosition_txt setStringValue: o_playback_pos];
571 vlc_object_release(p_input);
573 [o_fs_timeSlider setFloatValue: 0.0];
574 [o_streamPosition_txt setStringValue: @"00:00"];
575 [o_streamLength_txt setHidden: YES];
580 - (void)setSeekable:(BOOL)b_seekable
582 [o_bwd setEnabled: b_seekable];
583 [o_fwd setEnabled: b_seekable];
584 [o_fs_timeSlider setEnabled: b_seekable];
587 - (void)setVolumeLevel: (int)i_volumeLevel
589 [o_fs_volumeSlider setIntValue: i_volumeLevel];
592 - (IBAction)play:(id)sender
594 [[VLCCoreInteraction sharedInstance] playOrPause];
597 - (IBAction)forward:(id)sender
599 if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) > 0.16) {
600 // we just skipped 4 "continous" events, otherwise we are too fast
601 [[VLCCoreInteraction sharedInstance] forwardExtraShort];
602 last_fwd_event = [NSDate timeIntervalSinceReferenceDate];
606 - (IBAction)backward:(id)sender
608 if (([NSDate timeIntervalSinceReferenceDate] - last_bwd_event) > 0.16) {
609 // we just skipped 4 "continous" events, otherwise we are too fast
610 [[VLCCoreInteraction sharedInstance] backwardExtraShort];
611 last_bwd_event = [NSDate timeIntervalSinceReferenceDate];
615 - (IBAction)prev:(id)sender
617 [[VLCCoreInteraction sharedInstance] previous];
620 - (IBAction)next:(id)sender
622 [[VLCCoreInteraction sharedInstance] next];
625 - (IBAction)toggleFullscreen:(id)sender
627 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
630 - (IBAction)fsTimeSliderUpdate:(id)sender
632 input_thread_t * p_input;
633 p_input = pl_CurrentInput(VLCIntf);
634 if (p_input != NULL) {
637 pos.f_float = [o_fs_timeSlider floatValue] / 10000.;
638 var_Set(p_input, "position", pos);
639 vlc_object_release(p_input);
641 [[VLCMain sharedInstance] updatePlaybackPosition];
644 - (IBAction)fsVolumeSliderUpdate:(id)sender
646 [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
649 #define addImage(image, _x, _y, mode) \
650 image_size = [image size]; \
651 image_rect.size = image_size; \
652 image_rect.origin.x = 0; \
653 image_rect.origin.y = 0; \
654 frame.origin.x = _x; \
655 frame.origin.y = _y; \
656 frame.size = image_size; \
657 [image drawInRect:frame fromRect:image_rect operation:mode fraction:1];
659 - (void)drawRect:(NSRect)rect
661 NSRect frame = [self frame];
665 addImage(o_background_img, 0, 0, NSCompositeCopy);
666 addImage(o_vol_sld_img, 26, 23, NSCompositeSourceOver);
667 addImage(o_vol_mute_img, 16, 18, NSCompositeSourceOver);
668 addImage(o_vol_max_img, 124, 18, NSCompositeSourceOver);
669 addImage(o_time_sld_img, 15, 45, NSCompositeSourceOver);
674 /*****************************************************************************
676 *****************************************************************************/
677 @implementation VLCFSTimeSlider
678 - (void)drawKnobInRect:(NSRect)knobRect
681 NSImage *img = [NSImage imageNamed:@"fs_time_slider_knob_highlight"];
682 image_rect.size = [img size];
683 image_rect.origin.x = 0;
684 image_rect.origin.y = 0;
685 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
686 knobRect.size.width = image_rect.size.width;
687 knobRect.size.height = image_rect.size.height;
688 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
691 - (void)drawRect:(NSRect)rect
693 /* Draw default to make sure the slider behaves correctly */
694 [[NSGraphicsContext currentContext] saveGraphicsState];
695 NSRectClip(NSZeroRect);
696 [super drawRect:rect];
697 [[NSGraphicsContext currentContext] restoreGraphicsState];
699 NSRect knobRect = [[self cell] knobRectFlipped:NO];
700 knobRect.origin.y+=4;
701 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
702 [self drawKnobInRect: knobRect];
707 /*****************************************************************************
709 *****************************************************************************/
710 @implementation VLCFSVolumeSlider
711 - (void)drawKnobInRect:(NSRect) knobRect
714 NSImage *img = [NSImage imageNamed:@"fs_volume_slider_knob_highlight"];
715 image_rect.size = [img size];
716 image_rect.origin.x = 0;
717 image_rect.origin.y = 0;
718 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
719 knobRect.size.width = image_rect.size.width;
720 knobRect.size.height = image_rect.size.height;
721 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
724 - (void)drawRect:(NSRect)rect
726 /* Draw default to make sure the slider behaves correctly */
727 [[NSGraphicsContext currentContext] saveGraphicsState];
728 NSRectClip(NSZeroRect);
729 [super drawRect:rect];
730 [[NSGraphicsContext currentContext] restoreGraphicsState];
732 NSRect knobRect = [[self cell] knobRectFlipped:NO];
733 knobRect.origin.y+=7.5;
734 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
735 [self drawKnobInRect: knobRect];