1 /*****************************************************************************
2 * fspanel.m: MacOS X full screen panel
3 *****************************************************************************
4 * Copyright (C) 2006-2012 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>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #import "CoreInteraction.h"
30 #import "MainWindow.h"
33 #import "CompatibilityFixes.h"
35 @interface VLCFSPanel ()
39 /*****************************************************************************
41 *****************************************************************************/
42 @implementation VLCFSPanel
43 /* We override this initializer so we can set the NSBorderlessWindowMask styleMask, and set a few other important settings */
44 - (id)initWithContentRect:(NSRect)contentRect
45 styleMask:(NSUInteger)aStyle
46 backing:(NSBackingStoreType)bufferingType
49 id win = [super initWithContentRect:contentRect styleMask:NSTexturedBackgroundWindowMask backing:bufferingType defer:flag];
51 [win setHasShadow: NO];
52 [win setBackgroundColor:[NSColor clearColor]];
53 if (!OSX_SNOW_LEOPARD)
54 [win setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
56 /* let the window sit on top of everything else and start out completely transparent */
57 [win setLevel:NSModalPanelWindowLevel];
59 hideAgainTimer = fadeTimer = nil;
60 [self setNonActive:nil];
66 [self setContentView:[[VLCFSPanelView alloc] initWithFrame: [self frame]]];
67 BOOL isInside = (NSPointInRect([NSEvent mouseLocation],[self frame]));
68 [[self contentView] addTrackingRect:[[self contentView] bounds] owner:self userData:nil assumeInside:isInside];
70 [self mouseEntered:NULL];
72 [self mouseExited:NULL];
76 /* get a notification if VLC isn't the active app anymore */
77 [[NSNotificationCenter defaultCenter]
79 selector: @selector(setNonActive:)
80 name: NSApplicationDidResignActiveNotification
83 /* get a notification if VLC is the active app again */
84 [[NSNotificationCenter defaultCenter]
86 selector: @selector(setActive:)
87 name: NSApplicationDidBecomeActiveNotification
91 /* make sure that we don't become key, since we can't handle hotkeys */
92 - (BOOL)canBecomeKeyWindow
97 - (BOOL)mouseDownCanMoveWindow
104 [[NSNotificationCenter defaultCenter] removeObserver: self];
106 if (hideAgainTimer) {
107 [hideAgainTimer invalidate];
108 [hideAgainTimer release];
112 [o_vout_window release];
114 [self setFadeTimer:nil];
120 /* centre the panel in the lower third of the screen */
121 NSPoint theCoordinate;
122 NSRect theScreensFrame;
123 NSRect theWindowsFrame;
126 /* user-defined screen */
127 screen = [NSScreen screenWithDisplayID: (CGDirectDisplayID)i_device];
130 /* invalid preferences or none specified, using main screen */
131 screen = [NSScreen mainScreen];
133 theScreensFrame = [screen frame];
134 theWindowsFrame = [self frame];
136 theCoordinate.x = (theScreensFrame.size.width - theWindowsFrame.size.width) / 2 + theScreensFrame.origin.x;
137 theCoordinate.y = (theScreensFrame.size.height / 3) - theWindowsFrame.size.height + theScreensFrame.origin.y;
138 [self setFrameTopLeftPoint: theCoordinate];
143 [[self contentView] setPlay];
148 [[self contentView] setPause];
151 - (void)setStreamTitle:(NSString *)o_title
153 [[self contentView] setStreamTitle: o_title];
156 - (void)setStreamPos:(float) f_pos andTime:(NSString *)o_time
158 [[self contentView] setStreamPos:f_pos andTime: o_time];
161 - (void)setSeekable:(BOOL) b_seekable
163 [[self contentView] setSeekable: b_seekable];
166 - (void)setVolumeLevel: (int)i_volumeLevel
168 [[self contentView] setVolumeLevel: i_volumeLevel];
171 - (void)setNonActive:(id)noData
174 [self orderOut: self];
176 /* here's fadeOut, just without visibly fading */
178 [self setAlphaValue:0.0];
179 [self setFadeTimer:nil];
183 - (void)setActive:(id)noData
186 [[VLCMain sharedInstance] showFullscreenController];
189 /* This routine is called repeatedly to fade in the window */
190 - (void)focus:(NSTimer *)timer
192 /* we need to push ourselves to front if the vout window was closed since our last display */
193 if (b_voutWasUpdated) {
194 [self orderFront: self];
195 b_voutWasUpdated = NO;
198 if ([self alphaValue] < 1.0)
199 [self setAlphaValue:[self alphaValue]+0.1];
200 if ([self alphaValue] >= 1.0) {
202 [self setAlphaValue: 1.0];
203 [self setFadeTimer:nil];
206 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(unfocus:) userInfo:NULL repeats:YES]];
211 /* This routine is called repeatedly to hide the window */
212 - (void)unfocus:(NSTimer *)timer
217 [self setFadeTimer: NULL];
221 if ([self alphaValue] > 0.0)
222 [self setAlphaValue:[self alphaValue]-0.05];
223 if ([self alphaValue] <= 0.05) {
225 [self setAlphaValue:0.0];
226 [self setFadeTimer:nil];
230 [NSTimer scheduledTimerWithTimeInterval:0.1
232 selector:@selector(focus:)
239 - (void)mouseExited:(NSEvent *)theEvent
241 /* give up our focus, so the vout may show us again without letting the user clicking it */
242 if (o_vout_window && var_GetBool(pl_Get(VLCIntf), "fullscreen"))
243 [o_vout_window makeKeyWindow];
248 [NSCursor setHiddenUntilMouseMoves: YES];
253 /* in case that the user don't want us to appear, make sure we hide the mouse */
255 if (!config_GetInt(VLCIntf, "macosx-fspanel")) {
256 float time = (float)var_CreateGetInteger(VLCIntf, "mouse-hide-timeout") / 1000.;
257 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(hideMouse) userInfo:nil repeats:NO]];
264 [self orderFront: nil];
266 if ([self alphaValue] < 1.0 || b_displayed != YES) {
267 if (![self fadeTimer])
268 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(focus:) userInfo:[NSNumber numberWithShort:1] repeats:YES]];
269 else if ([[[self fadeTimer] userInfo] shortValue]==0)
277 if (NSPointInRect([NSEvent mouseLocation],[self frame]))
280 if (([self alphaValue] > 0.0)) {
281 if (![self fadeTimer])
282 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(unfocus:) userInfo:[NSNumber numberWithShort:0] repeats:YES]];
283 else if ([[[self fadeTimer] userInfo] shortValue]==1)
288 /* triggers a timer to autoHide us again after some seconds of no activity */
291 /* this will tell the timer to start over again or to start at all */
294 /* get us a valid timer */
295 if (! b_alreadyCounting) {
296 i_timeToKeepVisibleInSec = var_CreateGetInteger(VLCIntf, "mouse-hide-timeout") / 500;
297 if (hideAgainTimer) {
298 [hideAgainTimer invalidate];
299 [hideAgainTimer autorelease];
301 /* released in -autoHide and -dealloc */
302 hideAgainTimer = [[NSTimer scheduledTimerWithTimeInterval: 0.5
304 selector: @selector(keepVisible:)
306 repeats: YES] retain];
307 b_alreadyCounting = YES;
311 - (void)keepVisible:(NSTimer *)timer
313 /* if the user triggered an action, start over again */
317 /* count down until we hide ourselfes again and do so if necessary */
318 if (--i_timeToKeepVisibleInSec < 1) {
321 [hideAgainTimer invalidate]; /* released in -autoHide and -dealloc */
322 b_alreadyCounting = NO;
326 /* A getter and setter for our main timer that handles window fading */
327 - (NSTimer *)fadeTimer
332 - (void)setFadeTimer:(NSTimer *)timer
335 [fadeTimer invalidate];
336 [fadeTimer autorelease];
340 - (void)mouseDown:(NSEvent *)theEvent
342 mouseClic = [theEvent locationInWindow];
345 - (void)mouseDragged:(NSEvent *)theEvent
347 NSPoint point = [NSEvent mouseLocation];
348 point.x -= mouseClic.x;
349 point.y -= mouseClic.y;
350 [self setFrameOrigin:point];
353 - (void)setVoutWasUpdated: (VLCWindow *)o_window
355 b_voutWasUpdated = YES;
357 [o_vout_window release];
358 o_vout_window = [o_window retain];
359 int i_newdevice = (int)[[o_vout_window screen] displayID];
360 if (i_newdevice != i_device) {
361 i_device = i_newdevice;
367 /*****************************************************************************
369 *****************************************************************************/
370 @implementation VLCFSPanelView
372 #define addButton(o_button, imageOff, imageOn, _x, _y, action, AXDesc, ToolTip) \
373 s_rc.origin.x = _x; \
374 s_rc.origin.y = _y; \
375 o_button = [[NSButton alloc] initWithFrame: s_rc]; \
376 [o_button setButtonType: NSMomentaryChangeButton]; \
377 [o_button setBezelStyle: NSRegularSquareBezelStyle]; \
378 [o_button setBordered: NO]; \
379 [o_button setFont:[NSFont systemFontOfSize:0]]; \
380 [o_button setImage:[NSImage imageNamed:imageOff]]; \
381 [o_button setAlternateImage:[NSImage imageNamed:imageOn]]; \
382 [o_button sizeToFit]; \
383 [o_button setTarget: self]; \
384 [o_button setAction: @selector(action:)]; \
385 [[o_button cell] accessibilitySetOverrideValue:AXDesc forAttribute:NSAccessibilityDescriptionAttribute]; \
386 [[o_button cell] accessibilitySetOverrideValue:ToolTip forAttribute:NSAccessibilityTitleAttribute]; \
387 [o_button setToolTip: ToolTip]; \
388 [self addSubview:o_button];
390 #define addTextfield(class, o_text, align, font, color) \
391 o_text = [[class alloc] initWithFrame: s_rc]; \
392 [o_text setDrawsBackground: NO]; \
393 [o_text setBordered: NO]; \
394 [o_text setEditable: NO]; \
395 [o_text setSelectable: NO]; \
396 [o_text setStringValue: _NS("(no item is being played)")]; \
397 [o_text setAlignment: align]; \
398 [o_text setTextColor: [NSColor color]]; \
399 [o_text setFont:[NSFont font:[NSFont smallSystemFontSize]]]; \
400 [self addSubview:o_text];
402 - (id)initWithFrame:(NSRect)frameRect
404 id view = [super initWithFrame:frameRect];
405 fillColor = [[NSColor clearColor] retain];
406 NSRect s_rc = [self frame];
407 addButton(o_prev, @"fs_skip_previous" , @"fs_skip_previous_highlight", 174, 15, prev, _NS("Click to go to the previous playlist item."), _NS("Previous"));
408 addButton(o_bwd, @"fs_rewind" , @"fs_rewind_highlight" , 211, 14, backward, _NS("Click and hold to skip backward through the current media."), _NS("Backward"));
409 addButton(o_play, @"fs_play" , @"fs_play_highlight" , 267, 10, play, _NS("Click to play or pause the current media."), _NS("Play/Pause"));
410 addButton(o_fwd, @"fs_forward" , @"fs_forward_highlight" , 313, 14, forward, _NS("Click and hold to skip forward through the current media."), _NS("Forward"));
411 addButton(o_next, @"fs_skip_next" , @"fs_skip_next_highlight" , 365, 15, next, _NS("Click to go to the next playlist item."), _NS("Next"));
412 addButton(o_fullscreen, @"fs_exit_fullscreen", @"fs_exit_fullscreen_hightlight", 507, 13, toggleFullscreen, _NS("Click to exit fullscreen playback."), _NS("Toggle Fullscreen mode"));
414 addButton(o_button, @"image (off state)", @"image (on state)", 38, 51, something, accessibility help string, usual tool tip);
416 [o_fwd setContinuous:YES];
417 [o_bwd setContinuous:YES];
423 s_rc.size.width = 518;
424 s_rc.size.height = 13;
425 o_fs_timeSlider = [[VLCFSTimeSlider alloc] initWithFrame: s_rc];
426 [o_fs_timeSlider setMinValue:0];
427 [o_fs_timeSlider setMaxValue:10000];
428 [o_fs_timeSlider setFloatValue: 0];
429 [o_fs_timeSlider setContinuous: YES];
430 [o_fs_timeSlider setTarget: self];
431 [o_fs_timeSlider setAction: @selector(fsTimeSliderUpdate:)];
432 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Position") forAttribute:NSAccessibilityTitleAttribute];
433 [[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];
434 [self addSubview: o_fs_timeSlider];
440 s_rc.size.width = 95;
441 s_rc.size.height = 10;
442 o_fs_volumeSlider = [[VLCFSVolumeSlider alloc] initWithFrame: s_rc];
443 [o_fs_volumeSlider setMinValue:0];
444 [o_fs_volumeSlider setMaxValue:AOUT_VOLUME_MAX];
445 [o_fs_volumeSlider setIntValue:AOUT_VOLUME_DEFAULT];
446 [o_fs_volumeSlider setContinuous: YES];
447 [o_fs_volumeSlider setTarget: self];
448 [o_fs_volumeSlider setAction: @selector(fsVolumeSliderUpdate:)];
449 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Volume") forAttribute:NSAccessibilityTitleAttribute];
450 [[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];
451 [self addSubview: o_fs_volumeSlider];
453 /* time counter and stream title output fields */
457 s_rc.size.width = 352;
458 s_rc.size.height = 14;
459 addTextfield(NSTextField, o_streamTitle_txt, NSCenterTextAlignment, systemFontOfSize, whiteColor);
462 s_rc.size.width = 65;
463 addTextfield(VLCTimeField, o_streamPosition_txt, NSRightTextAlignment, systemFontOfSize, whiteColor);
465 o_background_img = [[NSImage imageNamed:@"fs_background"] retain];
466 o_vol_sld_img = [[NSImage imageNamed:@"fs_volume_slider_bar"] retain];
467 o_vol_mute_img = [[NSImage imageNamed:@"fs_volume_mute"] retain];
468 o_vol_max_img = [[NSImage imageNamed:@"fs_volume_max"] retain];
469 o_time_sld_img = [[NSImage imageNamed:@"fs_time_slider"] retain];
476 [o_background_img release];
477 [o_vol_sld_img release];
478 [o_vol_mute_img release];
479 [o_vol_max_img release];
480 [o_time_sld_img release];
481 [o_fs_timeSlider release];
482 [o_fs_volumeSlider release];
488 [o_fullscreen release];
489 [o_streamTitle_txt release];
490 [o_streamPosition_txt release];
496 [o_play setImage:[NSImage imageNamed:@"fs_play"]];
497 [o_play setAlternateImage: [NSImage imageNamed:@"fs_play_highlight"]];
502 [o_play setImage: [NSImage imageNamed:@"fs_pause"]];
503 [o_play setAlternateImage: [NSImage imageNamed:@"fs_pause_highlight"]];
506 - (void)setStreamTitle:(NSString *)o_title
508 [o_streamTitle_txt setStringValue: o_title];
511 - (void)setStreamPos:(float) f_pos andTime:(NSString *)o_time
513 [o_streamPosition_txt setStringValue: o_time];
514 [o_fs_timeSlider setFloatValue: f_pos];
517 - (void)setSeekable:(BOOL)b_seekable
519 [o_bwd setEnabled: b_seekable];
520 [o_fwd setEnabled: b_seekable];
521 [o_fs_timeSlider setEnabled: b_seekable];
524 - (void)setVolumeLevel: (int)i_volumeLevel
526 [o_fs_volumeSlider setIntValue: i_volumeLevel];
529 - (IBAction)play:(id)sender
531 [[VLCCoreInteraction sharedInstance] play];
534 - (IBAction)forward:(id)sender
536 if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) > 0.16) {
537 // we just skipped 4 "continous" events, otherwise we are too fast
538 [[VLCCoreInteraction sharedInstance] forwardExtraShort];
539 last_fwd_event = [NSDate timeIntervalSinceReferenceDate];
543 - (IBAction)backward:(id)sender
545 if (([NSDate timeIntervalSinceReferenceDate] - last_bwd_event) > 0.16) {
546 // we just skipped 4 "continous" events, otherwise we are too fast
547 [[VLCCoreInteraction sharedInstance] backwardExtraShort];
548 last_bwd_event = [NSDate timeIntervalSinceReferenceDate];
552 - (IBAction)prev:(id)sender
554 [[VLCCoreInteraction sharedInstance] previous];
557 - (IBAction)next:(id)sender
559 [[VLCCoreInteraction sharedInstance] next];
562 - (IBAction)toggleFullscreen:(id)sender
564 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
567 - (IBAction)fsTimeSliderUpdate:(id)sender
569 input_thread_t * p_input;
570 p_input = pl_CurrentInput(VLCIntf);
571 if (p_input != NULL) {
574 pos.f_float = [o_fs_timeSlider floatValue] / 10000.;
575 var_Set(p_input, "position", pos);
576 vlc_object_release(p_input);
578 [[VLCMain sharedInstance] updatePlaybackPosition];
581 - (IBAction)fsVolumeSliderUpdate:(id)sender
583 [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
586 #define addImage(image, _x, _y, mode) \
587 image_size = [image size]; \
588 image_rect.size = image_size; \
589 image_rect.origin.x = 0; \
590 image_rect.origin.y = 0; \
591 frame.origin.x = _x; \
592 frame.origin.y = _y; \
593 frame.size = image_size; \
594 [image drawInRect:frame fromRect:image_rect operation:mode fraction:1];
596 - (void)drawRect:(NSRect)rect
598 NSRect frame = [self frame];
602 addImage(o_background_img, 0, 0, NSCompositeCopy);
603 addImage(o_vol_sld_img, 26, 23, NSCompositeSourceOver);
604 addImage(o_vol_mute_img, 16, 18, NSCompositeSourceOver);
605 addImage(o_vol_max_img, 124, 18, NSCompositeSourceOver);
606 addImage(o_time_sld_img, 15, 45, NSCompositeSourceOver);
611 /*****************************************************************************
613 *****************************************************************************/
614 @implementation VLCFSTimeSlider
615 - (void)drawKnobInRect:(NSRect)knobRect
618 NSImage *img = [NSImage imageNamed:@"fs_time_slider_knob_highlight"];
619 image_rect.size = [img size];
620 image_rect.origin.x = 0;
621 image_rect.origin.y = 0;
622 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
623 knobRect.size.width = image_rect.size.width;
624 knobRect.size.height = image_rect.size.height;
625 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
628 - (void)drawRect:(NSRect)rect
630 /* Draw default to make sure the slider behaves correctly */
631 [[NSGraphicsContext currentContext] saveGraphicsState];
632 NSRectClip(NSZeroRect);
633 [super drawRect:rect];
634 [[NSGraphicsContext currentContext] restoreGraphicsState];
636 NSRect knobRect = [[self cell] knobRectFlipped:NO];
637 knobRect.origin.y+=4;
638 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
639 [self drawKnobInRect: knobRect];
644 /*****************************************************************************
646 *****************************************************************************/
647 @implementation VLCFSVolumeSlider
648 - (void)drawKnobInRect:(NSRect) knobRect
651 NSImage *img = [NSImage imageNamed:@"fs_volume_slider_knob"];
652 image_rect.size = [img size];
653 image_rect.origin.x = 0;
654 image_rect.origin.y = 0;
655 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
656 knobRect.size.width = image_rect.size.width;
657 knobRect.size.height = image_rect.size.height;
658 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
661 - (void)drawRect:(NSRect)rect
663 /* Draw default to make sure the slider behaves correctly */
664 [[NSGraphicsContext currentContext] saveGraphicsState];
665 NSRectClip(NSZeroRect);
666 [super drawRect:rect];
667 [[NSGraphicsContext currentContext] restoreGraphicsState];
669 NSRect knobRect = [[self cell] knobRectFlipped:NO];
670 knobRect.origin.y+=6;
671 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
672 [self drawKnobInRect: knobRect];