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)setStreamPos:(float) f_pos andTime:(NSString *)o_time
159 [[self contentView] setStreamPos:f_pos andTime: o_time];
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" , @"fs_skip_previous_highlight", 174, 15, prev, _NS("Click to go to the previous playlist item."), _NS("Previous"));
410 addButton(o_bwd, @"fs_rewind" , @"fs_rewind_highlight" , 211, 14, backward, _NS("Click and hold to skip backward through the current media."), _NS("Backward"));
411 addButton(o_play, @"fs_play" , @"fs_play_highlight" , 267, 10, play, _NS("Click to play or pause the current media."), _NS("Play/Pause"));
412 addButton(o_fwd, @"fs_forward" , @"fs_forward_highlight" , 313, 14, forward, _NS("Click and hold to skip forward through the current media."), _NS("Forward"));
413 addButton(o_next, @"fs_skip_next" , @"fs_skip_next_highlight" , 365, 15, next, _NS("Click to go to the next playlist item."), _NS("Next"));
414 addButton(o_fullscreen, @"fs_exit_fullscreen", @"fs_exit_fullscreen_hightlight", 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 */
463 s_rc.size.width = 352;
464 s_rc.size.height = 14;
465 addTextfield(NSTextField, o_streamTitle_txt, NSCenterTextAlignment, systemFontOfSize, whiteColor);
468 s_rc.size.width = 65;
469 addTextfield(VLCTimeField, o_streamPosition_txt, NSRightTextAlignment, systemFontOfSize, whiteColor);
471 o_background_img = [[NSImage imageNamed:@"fs_background"] retain];
472 o_vol_sld_img = [[NSImage imageNamed:@"fs_volume_slider_bar"] retain];
473 o_vol_mute_img = [[NSImage imageNamed:@"fs_volume_mute"] retain];
474 o_vol_max_img = [[NSImage imageNamed:@"fs_volume_max"] retain];
475 o_time_sld_img = [[NSImage imageNamed:@"fs_time_slider"] retain];
482 [o_background_img release];
483 [o_vol_sld_img release];
484 [o_vol_mute_img release];
485 [o_vol_max_img release];
486 [o_time_sld_img release];
487 [o_fs_timeSlider release];
488 [o_fs_volumeSlider release];
494 [o_fullscreen release];
495 [o_streamTitle_txt release];
496 [o_streamPosition_txt release];
502 [o_play setImage:[NSImage imageNamed:@"fs_play"]];
503 [o_play setAlternateImage: [NSImage imageNamed:@"fs_play_highlight"]];
508 [o_play setImage: [NSImage imageNamed:@"fs_pause"]];
509 [o_play setAlternateImage: [NSImage imageNamed:@"fs_pause_highlight"]];
512 - (void)setStreamTitle:(NSString *)o_title
514 [o_streamTitle_txt setStringValue: o_title];
517 - (void)setStreamPos:(float) f_pos andTime:(NSString *)o_time
519 [o_streamPosition_txt setStringValue: o_time];
520 [o_fs_timeSlider setFloatValue: f_pos];
523 - (void)setSeekable:(BOOL)b_seekable
525 [o_bwd setEnabled: b_seekable];
526 [o_fwd setEnabled: b_seekable];
527 [o_fs_timeSlider setEnabled: b_seekable];
530 - (void)setVolumeLevel: (int)i_volumeLevel
532 [o_fs_volumeSlider setIntValue: i_volumeLevel];
535 - (IBAction)play:(id)sender
537 [[VLCCoreInteraction sharedInstance] playOrPause];
540 - (IBAction)forward:(id)sender
542 if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) > 0.16) {
543 // we just skipped 4 "continous" events, otherwise we are too fast
544 [[VLCCoreInteraction sharedInstance] forwardExtraShort];
545 last_fwd_event = [NSDate timeIntervalSinceReferenceDate];
549 - (IBAction)backward:(id)sender
551 if (([NSDate timeIntervalSinceReferenceDate] - last_bwd_event) > 0.16) {
552 // we just skipped 4 "continous" events, otherwise we are too fast
553 [[VLCCoreInteraction sharedInstance] backwardExtraShort];
554 last_bwd_event = [NSDate timeIntervalSinceReferenceDate];
558 - (IBAction)prev:(id)sender
560 [[VLCCoreInteraction sharedInstance] previous];
563 - (IBAction)next:(id)sender
565 [[VLCCoreInteraction sharedInstance] next];
568 - (IBAction)toggleFullscreen:(id)sender
570 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
573 - (IBAction)fsTimeSliderUpdate:(id)sender
575 input_thread_t * p_input;
576 p_input = pl_CurrentInput(VLCIntf);
577 if (p_input != NULL) {
580 pos.f_float = [o_fs_timeSlider floatValue] / 10000.;
581 var_Set(p_input, "position", pos);
582 vlc_object_release(p_input);
584 [[VLCMain sharedInstance] updatePlaybackPosition];
587 - (IBAction)fsVolumeSliderUpdate:(id)sender
589 [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
592 #define addImage(image, _x, _y, mode) \
593 image_size = [image size]; \
594 image_rect.size = image_size; \
595 image_rect.origin.x = 0; \
596 image_rect.origin.y = 0; \
597 frame.origin.x = _x; \
598 frame.origin.y = _y; \
599 frame.size = image_size; \
600 [image drawInRect:frame fromRect:image_rect operation:mode fraction:1];
602 - (void)drawRect:(NSRect)rect
604 NSRect frame = [self frame];
608 addImage(o_background_img, 0, 0, NSCompositeCopy);
609 addImage(o_vol_sld_img, 26, 23, NSCompositeSourceOver);
610 addImage(o_vol_mute_img, 16, 18, NSCompositeSourceOver);
611 addImage(o_vol_max_img, 124, 18, NSCompositeSourceOver);
612 addImage(o_time_sld_img, 15, 45, NSCompositeSourceOver);
617 /*****************************************************************************
619 *****************************************************************************/
620 @implementation VLCFSTimeSlider
621 - (void)drawKnobInRect:(NSRect)knobRect
624 NSImage *img = [NSImage imageNamed:@"fs_time_slider_knob_highlight"];
625 image_rect.size = [img size];
626 image_rect.origin.x = 0;
627 image_rect.origin.y = 0;
628 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
629 knobRect.size.width = image_rect.size.width;
630 knobRect.size.height = image_rect.size.height;
631 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
634 - (void)drawRect:(NSRect)rect
636 /* Draw default to make sure the slider behaves correctly */
637 [[NSGraphicsContext currentContext] saveGraphicsState];
638 NSRectClip(NSZeroRect);
639 [super drawRect:rect];
640 [[NSGraphicsContext currentContext] restoreGraphicsState];
642 NSRect knobRect = [[self cell] knobRectFlipped:NO];
643 knobRect.origin.y+=4;
644 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
645 [self drawKnobInRect: knobRect];
650 /*****************************************************************************
652 *****************************************************************************/
653 @implementation VLCFSVolumeSlider
654 - (void)drawKnobInRect:(NSRect) knobRect
657 NSImage *img = [NSImage imageNamed:@"fs_volume_slider_knob"];
658 image_rect.size = [img size];
659 image_rect.origin.x = 0;
660 image_rect.origin.y = 0;
661 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
662 knobRect.size.width = image_rect.size.width;
663 knobRect.size.height = image_rect.size.height;
664 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
667 - (void)drawRect:(NSRect)rect
669 /* Draw default to make sure the slider behaves correctly */
670 [[NSGraphicsContext currentContext] saveGraphicsState];
671 NSRectClip(NSZeroRect);
672 [super drawRect:rect];
673 [[NSGraphicsContext currentContext] restoreGraphicsState];
675 NSRect knobRect = [[self cell] knobRectFlipped:NO];
676 knobRect.origin.y+=6;
677 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
678 [self drawKnobInRect: knobRect];