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];
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];
77 /* get a notification if VLC isn't the active app anymore */
78 [[NSNotificationCenter defaultCenter]
80 selector: @selector(setNonActive:)
81 name: NSApplicationDidResignActiveNotification
84 /* get a notification if VLC is the active app again */
85 [[NSNotificationCenter defaultCenter]
87 selector: @selector(setActive:)
88 name: NSApplicationDidBecomeActiveNotification
92 /* make sure that we don't become key, since we can't handle hotkeys */
93 - (BOOL)canBecomeKeyWindow
98 - (BOOL)mouseDownCanMoveWindow
105 [[NSNotificationCenter defaultCenter] removeObserver: self];
107 if (hideAgainTimer) {
108 [hideAgainTimer invalidate];
109 [hideAgainTimer release];
113 [o_vout_window release];
115 [self setFadeTimer:nil];
121 /* centre the panel in the lower third of the screen */
122 NSPoint theCoordinate;
123 NSRect theScreensFrame;
124 NSRect theWindowsFrame;
127 /* user-defined screen */
128 screen = [NSScreen screenWithDisplayID: (CGDirectDisplayID)i_device];
131 /* invalid preferences or none specified, using main screen */
132 screen = [NSScreen mainScreen];
134 theScreensFrame = [screen frame];
135 theWindowsFrame = [self frame];
137 theCoordinate.x = (theScreensFrame.size.width - theWindowsFrame.size.width) / 2 + theScreensFrame.origin.x;
138 theCoordinate.y = (theScreensFrame.size.height / 3) - theWindowsFrame.size.height + theScreensFrame.origin.y;
139 [self setFrameTopLeftPoint: theCoordinate];
144 [[self contentView] setPlay];
149 [[self contentView] setPause];
152 - (void)setStreamTitle:(NSString *)o_title
154 [[self contentView] setStreamTitle: o_title];
157 - (void)updatePositionAndTime
159 [[self contentView] updatePositionAndTime];
162 - (void)setSeekable:(BOOL) b_seekable
164 [[self contentView] setSeekable: b_seekable];
167 - (void)setVolumeLevel: (int)i_volumeLevel
169 [[self contentView] setVolumeLevel: i_volumeLevel];
172 - (void)setNonActive:(id)noData
175 [self orderOut: self];
177 /* here's fadeOut, just without visibly fading */
179 [self setAlphaValue:0.0];
180 [self setFadeTimer:nil];
184 - (void)setActive:(id)noData
188 [[VLCMain sharedInstance] showFullscreenController];
191 /* This routine is called repeatedly to fade in the window */
192 - (void)focus:(NSTimer *)timer
194 /* we need to push ourselves to front if the vout window was closed since our last display */
195 if (b_voutWasUpdated) {
196 [self orderFront: self];
197 b_voutWasUpdated = NO;
200 if ([self alphaValue] < 1.0)
201 [self setAlphaValue:[self alphaValue]+0.1];
202 if ([self alphaValue] >= 1.0) {
204 [self setAlphaValue: 1.0];
205 [self setFadeTimer:nil];
208 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(unfocus:) userInfo:NULL repeats:YES]];
213 /* This routine is called repeatedly to hide the window */
214 - (void)unfocus:(NSTimer *)timer
219 [self setFadeTimer: NULL];
223 if ([self alphaValue] > 0.0)
224 [self setAlphaValue:[self alphaValue]-0.05];
225 if ([self alphaValue] <= 0.05) {
227 [self setAlphaValue:0.0];
228 [self setFadeTimer:nil];
232 [NSTimer scheduledTimerWithTimeInterval:0.1
234 selector:@selector(focus:)
241 - (void)mouseExited:(NSEvent *)theEvent
243 /* give up our focus, so the vout may show us again without letting the user clicking it */
244 if (o_vout_window && var_GetBool(pl_Get(VLCIntf), "fullscreen"))
245 [o_vout_window makeKeyWindow];
250 [NSCursor setHiddenUntilMouseMoves: YES];
255 /* in case that the user don't want us to appear, make sure we hide the mouse */
257 if (!config_GetInt(VLCIntf, "macosx-fspanel")) {
258 float time = (float)var_CreateGetInteger(VLCIntf, "mouse-hide-timeout") / 1000.;
259 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(hideMouse) userInfo:nil repeats:NO]];
266 [self orderFront: nil];
268 if ([self alphaValue] < 1.0 || b_displayed != YES) {
269 if (![self fadeTimer])
270 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(focus:) userInfo:[NSNumber numberWithShort:1] repeats:YES]];
271 else if ([[[self fadeTimer] userInfo] shortValue]==0)
279 if (NSPointInRect([NSEvent mouseLocation],[self frame]))
282 if (([self alphaValue] > 0.0)) {
283 if (![self fadeTimer])
284 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(unfocus:) userInfo:[NSNumber numberWithShort:0] repeats:YES]];
285 else if ([[[self fadeTimer] userInfo] shortValue]==1)
290 /* triggers a timer to autoHide us again after some seconds of no activity */
293 /* this will tell the timer to start over again or to start at all */
296 /* get us a valid timer */
297 if (! b_alreadyCounting) {
298 i_timeToKeepVisibleInSec = var_CreateGetInteger(VLCIntf, "mouse-hide-timeout") / 500;
299 if (hideAgainTimer) {
300 [hideAgainTimer invalidate];
301 [hideAgainTimer autorelease];
303 /* released in -autoHide and -dealloc */
304 hideAgainTimer = [[NSTimer scheduledTimerWithTimeInterval: 0.5
306 selector: @selector(keepVisible:)
308 repeats: YES] retain];
309 b_alreadyCounting = YES;
313 - (void)keepVisible:(NSTimer *)timer
315 /* if the user triggered an action, start over again */
319 /* count down until we hide ourselfes again and do so if necessary */
320 if (--i_timeToKeepVisibleInSec < 1) {
323 [hideAgainTimer invalidate]; /* released in -autoHide and -dealloc */
324 b_alreadyCounting = NO;
328 /* A getter and setter for our main timer that handles window fading */
329 - (NSTimer *)fadeTimer
334 - (void)setFadeTimer:(NSTimer *)timer
337 [fadeTimer invalidate];
338 [fadeTimer autorelease];
342 - (void)mouseDown:(NSEvent *)theEvent
344 mouseClic = [theEvent locationInWindow];
347 - (void)mouseDragged:(NSEvent *)theEvent
349 NSPoint point = [NSEvent mouseLocation];
350 point.x -= mouseClic.x;
351 point.y -= mouseClic.y;
352 [self setFrameOrigin:point];
355 - (void)setVoutWasUpdated: (VLCWindow *)o_window
357 b_voutWasUpdated = YES;
359 [o_vout_window release];
360 o_vout_window = [o_window retain];
361 int i_newdevice = (int)[[o_vout_window screen] displayID];
362 if (i_newdevice != i_device) {
363 i_device = i_newdevice;
369 /*****************************************************************************
371 *****************************************************************************/
372 @implementation VLCFSPanelView
374 #define addButton(o_button, imageOff, imageOn, _x, _y, action, AXDesc, ToolTip) \
375 s_rc.origin.x = _x; \
376 s_rc.origin.y = _y; \
377 o_button = [[NSButton alloc] initWithFrame: s_rc]; \
378 [o_button setButtonType: NSMomentaryChangeButton]; \
379 [o_button setBezelStyle: NSRegularSquareBezelStyle]; \
380 [o_button setBordered: NO]; \
381 [o_button setFont:[NSFont systemFontOfSize:0]]; \
382 [o_button setImage:[NSImage imageNamed:imageOff]]; \
383 [o_button setAlternateImage:[NSImage imageNamed:imageOn]]; \
384 [o_button sizeToFit]; \
385 [o_button setTarget: self]; \
386 [o_button setAction: @selector(action:)]; \
387 [[o_button cell] accessibilitySetOverrideValue:AXDesc forAttribute:NSAccessibilityDescriptionAttribute]; \
388 [[o_button cell] accessibilitySetOverrideValue:ToolTip forAttribute:NSAccessibilityTitleAttribute]; \
389 [o_button setToolTip: ToolTip]; \
390 [self addSubview:o_button];
392 #define addTextfield(class, o_text, align, font, color) \
393 o_text = [[class alloc] initWithFrame: s_rc]; \
394 [o_text setDrawsBackground: NO]; \
395 [o_text setBordered: NO]; \
396 [o_text setEditable: NO]; \
397 [o_text setSelectable: NO]; \
398 [o_text setStringValue: _NS("(no item is being played)")]; \
399 [o_text setAlignment: align]; \
400 [o_text setTextColor: [NSColor color]]; \
401 [o_text setFont:[NSFont font:[NSFont smallSystemFontSize]]]; \
402 [self addSubview:o_text];
404 - (id)initWithFrame:(NSRect)frameRect
406 id view = [super initWithFrame:frameRect];
407 fillColor = [[NSColor clearColor] retain];
408 NSRect s_rc = [self frame];
409 addButton(o_prev, @"fs_skip_previous_highlight" , @"fs_skip_previous", 174, 15, prev, _NS("Click to go to the previous playlist item."), _NS("Previous"));
410 addButton(o_bwd, @"fs_rewind_highlight" , @"fs_rewind" , 211, 14, backward, _NS("Click and hold to skip backward through the current media."), _NS("Backward"));
411 addButton(o_play, @"fs_play_highlight" , @"fs_play" , 265, 10, play, _NS("Click to play or pause the current media."), _NS("Play/Pause"));
412 addButton(o_fwd, @"fs_forward_highlight" , @"fs_forward" , 313, 14, forward, _NS("Click and hold to skip forward through the current media."), _NS("Forward"));
413 addButton(o_next, @"fs_skip_next_highlight" , @"fs_skip_next" , 365, 15, next, _NS("Click to go to the next playlist item."), _NS("Next"));
414 addButton(o_fullscreen, @"fs_exit_fullscreen_highlight", @"fs_exit_fullscreen", 507, 13, toggleFullscreen, _NS("Click to exit fullscreen playback."), _NS("Toggle Fullscreen mode"));
416 addButton(o_button, @"image (off state)", @"image (on state)", 38, 51, something, accessibility help string, usual tool tip);
418 [o_fwd setContinuous:YES];
419 [o_bwd setContinuous:YES];
422 // (surrounding progress view for swipe behaviour)
425 s_rc.size.width = 518;
426 s_rc.size.height = 13;
427 o_progress_view = [[VLCProgressView alloc] initWithFrame: s_rc];
430 o_fs_timeSlider = [[VLCFSTimeSlider alloc] initWithFrame: s_rc];
431 [o_fs_timeSlider setMinValue:0];
432 [o_fs_timeSlider setMaxValue:10000];
433 [o_fs_timeSlider setFloatValue: 0];
434 [o_fs_timeSlider setContinuous: YES];
435 [o_fs_timeSlider setTarget: self];
436 [o_fs_timeSlider setAction: @selector(fsTimeSliderUpdate:)];
437 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Position") forAttribute:NSAccessibilityTitleAttribute];
438 [[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];
439 [self addSubview: o_progress_view];
440 [o_progress_view addSubview: o_fs_timeSlider];
446 s_rc.size.width = 95;
447 s_rc.size.height = 10;
448 o_fs_volumeSlider = [[VLCFSVolumeSlider alloc] initWithFrame: s_rc];
449 [o_fs_volumeSlider setMinValue:0];
450 [o_fs_volumeSlider setMaxValue:AOUT_VOLUME_MAX];
451 [o_fs_volumeSlider setIntValue:AOUT_VOLUME_DEFAULT];
452 [o_fs_volumeSlider setContinuous: YES];
453 [o_fs_volumeSlider setTarget: self];
454 [o_fs_volumeSlider setAction: @selector(fsVolumeSliderUpdate:)];
455 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Volume") forAttribute:NSAccessibilityTitleAttribute];
456 [[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];
457 [self addSubview: o_fs_volumeSlider];
459 /* time counter and stream title output fields */
461 // 10 px gap between time fields
464 s_rc.size.width = 361;
465 s_rc.size.height = 14;
466 addTextfield(NSTextField, o_streamTitle_txt, NSCenterTextAlignment, systemFontOfSize, whiteColor);
469 s_rc.size.width = 65;
470 addTextfield(VLCTimeField, o_streamPosition_txt, NSLeftTextAlignment, systemFontOfSize, whiteColor);
473 s_rc.size.width = 65;
474 addTextfield(VLCTimeField, o_streamLength_txt, NSRightTextAlignment, systemFontOfSize, whiteColor);
475 [o_streamLength_txt setRemainingIdentifier: @"DisplayFullscreenTimeAsTimeRemaining"];
477 o_background_img = [[NSImage imageNamed:@"fs_background"] retain];
478 o_vol_sld_img = [[NSImage imageNamed:@"fs_volume_slider_bar"] retain];
479 o_vol_mute_img = [[NSImage imageNamed:@"fs_volume_mute_highlight"] retain];
480 o_vol_max_img = [[NSImage imageNamed:@"fs_volume_max_highlight"] retain];
481 o_time_sld_img = [[NSImage imageNamed:@"fs_time_slider"] retain];
488 [o_background_img release];
489 [o_vol_sld_img release];
490 [o_vol_mute_img release];
491 [o_vol_max_img release];
492 [o_time_sld_img release];
493 [o_fs_timeSlider release];
494 [o_fs_volumeSlider release];
500 [o_fullscreen release];
501 [o_streamTitle_txt release];
502 [o_streamPosition_txt release];
508 [o_play setImage:[NSImage imageNamed:@"fs_play_highlight"]];
509 [o_play setAlternateImage: [NSImage imageNamed:@"fs_play"]];
514 [o_play setImage: [NSImage imageNamed:@"fs_pause_highlight"]];
515 [o_play setAlternateImage: [NSImage imageNamed:@"fs_pause"]];
518 - (void)setStreamTitle:(NSString *)o_title
520 [o_streamTitle_txt setStringValue: o_title];
523 - (void)updatePositionAndTime
525 input_thread_t * p_input;
526 p_input = pl_CurrentInput(VLCIntf);
532 var_Get(p_input, "position", &pos);
533 f_updated = 10000. * pos.f_float;
534 [o_fs_timeSlider setFloatValue: f_updated];
537 char psz_time[MSTRTIME_MAX_SIZE];
539 var_Get(p_input, "time", &time);
540 mtime_t dur = input_item_GetDuration(input_GetItem(p_input));
542 // update total duration (right field)
544 [o_streamLength_txt setHidden: YES];
546 [o_streamLength_txt setHidden: NO];
548 NSString *o_total_time;
549 if ([o_streamLength_txt timeRemaining]) {
550 mtime_t remaining = 0;
551 if (dur > time.i_time)
552 remaining = dur - time.i_time;
553 o_total_time = [NSString stringWithFormat: @"-%s", secstotimestr(psz_time, (remaining / 1000000))];
555 o_total_time = [NSString stringWithUTF8String: secstotimestr(psz_time, (dur / 1000000))];
557 [o_streamLength_txt setStringValue: o_total_time];
560 // update current position (left field)
561 NSString *o_playback_pos = [NSString stringWithUTF8String: secstotimestr(psz_time, (time.i_time / 1000000))];
563 [o_streamPosition_txt setStringValue: o_playback_pos];
564 vlc_object_release(p_input);
566 [o_fs_timeSlider setFloatValue: 0.0];
567 [o_streamPosition_txt setStringValue: @"00:00"];
568 [o_streamLength_txt setHidden: YES];
573 - (void)setSeekable:(BOOL)b_seekable
575 [o_bwd setEnabled: b_seekable];
576 [o_fwd setEnabled: b_seekable];
577 [o_fs_timeSlider setEnabled: b_seekable];
580 - (void)setVolumeLevel: (int)i_volumeLevel
582 [o_fs_volumeSlider setIntValue: i_volumeLevel];
585 - (IBAction)play:(id)sender
587 [[VLCCoreInteraction sharedInstance] playOrPause];
590 - (IBAction)forward:(id)sender
592 if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) > 0.16) {
593 // we just skipped 4 "continous" events, otherwise we are too fast
594 [[VLCCoreInteraction sharedInstance] forwardExtraShort];
595 last_fwd_event = [NSDate timeIntervalSinceReferenceDate];
599 - (IBAction)backward:(id)sender
601 if (([NSDate timeIntervalSinceReferenceDate] - last_bwd_event) > 0.16) {
602 // we just skipped 4 "continous" events, otherwise we are too fast
603 [[VLCCoreInteraction sharedInstance] backwardExtraShort];
604 last_bwd_event = [NSDate timeIntervalSinceReferenceDate];
608 - (IBAction)prev:(id)sender
610 [[VLCCoreInteraction sharedInstance] previous];
613 - (IBAction)next:(id)sender
615 [[VLCCoreInteraction sharedInstance] next];
618 - (IBAction)toggleFullscreen:(id)sender
620 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
623 - (IBAction)fsTimeSliderUpdate:(id)sender
625 input_thread_t * p_input;
626 p_input = pl_CurrentInput(VLCIntf);
627 if (p_input != NULL) {
630 pos.f_float = [o_fs_timeSlider floatValue] / 10000.;
631 var_Set(p_input, "position", pos);
632 vlc_object_release(p_input);
634 [[VLCMain sharedInstance] updatePlaybackPosition];
637 - (IBAction)fsVolumeSliderUpdate:(id)sender
639 [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
642 #define addImage(image, _x, _y, mode) \
643 image_size = [image size]; \
644 image_rect.size = image_size; \
645 image_rect.origin.x = 0; \
646 image_rect.origin.y = 0; \
647 frame.origin.x = _x; \
648 frame.origin.y = _y; \
649 frame.size = image_size; \
650 [image drawInRect:frame fromRect:image_rect operation:mode fraction:1];
652 - (void)drawRect:(NSRect)rect
654 NSRect frame = [self frame];
658 addImage(o_background_img, 0, 0, NSCompositeCopy);
659 addImage(o_vol_sld_img, 26, 23, NSCompositeSourceOver);
660 addImage(o_vol_mute_img, 16, 18, NSCompositeSourceOver);
661 addImage(o_vol_max_img, 124, 18, NSCompositeSourceOver);
662 addImage(o_time_sld_img, 15, 45, NSCompositeSourceOver);
667 /*****************************************************************************
669 *****************************************************************************/
670 @implementation VLCFSTimeSlider
671 - (void)drawKnobInRect:(NSRect)knobRect
674 NSImage *img = [NSImage imageNamed:@"fs_time_slider_knob_highlight"];
675 image_rect.size = [img size];
676 image_rect.origin.x = 0;
677 image_rect.origin.y = 0;
678 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
679 knobRect.size.width = image_rect.size.width;
680 knobRect.size.height = image_rect.size.height;
681 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
684 - (void)drawRect:(NSRect)rect
686 /* Draw default to make sure the slider behaves correctly */
687 [[NSGraphicsContext currentContext] saveGraphicsState];
688 NSRectClip(NSZeroRect);
689 [super drawRect:rect];
690 [[NSGraphicsContext currentContext] restoreGraphicsState];
692 NSRect knobRect = [[self cell] knobRectFlipped:NO];
693 knobRect.origin.y+=4;
694 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
695 [self drawKnobInRect: knobRect];
700 /*****************************************************************************
702 *****************************************************************************/
703 @implementation VLCFSVolumeSlider
704 - (void)drawKnobInRect:(NSRect) knobRect
707 NSImage *img = [NSImage imageNamed:@"fs_volume_slider_knob_highlight"];
708 image_rect.size = [img size];
709 image_rect.origin.x = 0;
710 image_rect.origin.y = 0;
711 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
712 knobRect.size.width = image_rect.size.width;
713 knobRect.size.height = image_rect.size.height;
714 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
717 - (void)drawRect:(NSRect)rect
719 /* Draw default to make sure the slider behaves correctly */
720 [[NSGraphicsContext currentContext] saveGraphicsState];
721 NSRectClip(NSZeroRect);
722 [super drawRect:rect];
723 [[NSGraphicsContext currentContext] restoreGraphicsState];
725 NSRect knobRect = [[self cell] knobRectFlipped:NO];
726 knobRect.origin.y+=7.5;
727 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
728 [self drawKnobInRect: knobRect];