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"
34 #import <vlc_aout_intf.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];
108 [hideAgainTimer invalidate];
109 [hideAgainTimer release];
111 [self setFadeTimer:nil];
117 /* centre the panel in the lower third of the screen */
118 NSPoint theCoordinate;
119 NSRect theScreensFrame;
120 NSRect theWindowsFrame;
123 /* user-defined screen */
124 screen = [NSScreen screenWithDisplayID: (CGDirectDisplayID)i_device];
127 /* invalid preferences or none specified, using main screen */
128 screen = [NSScreen mainScreen];
130 theScreensFrame = [screen frame];
131 theWindowsFrame = [self frame];
133 theCoordinate.x = (theScreensFrame.size.width - theWindowsFrame.size.width) / 2 + theScreensFrame.origin.x;
134 theCoordinate.y = (theScreensFrame.size.height / 3) - theWindowsFrame.size.height + theScreensFrame.origin.y;
135 [self setFrameTopLeftPoint: theCoordinate];
140 [[self contentView] setPlay];
145 [[self contentView] setPause];
148 - (void)setStreamTitle:(NSString *)o_title
150 [[self contentView] setStreamTitle: o_title];
153 - (void)setStreamPos:(float) f_pos andTime:(NSString *)o_time
155 [[self contentView] setStreamPos:f_pos andTime: o_time];
158 - (void)setSeekable:(BOOL) b_seekable
160 [[self contentView] setSeekable: b_seekable];
163 - (void)setVolumeLevel: (int)i_volumeLevel
165 [[self contentView] setVolumeLevel: i_volumeLevel];
168 - (void)setNonActive:(id)noData
171 [self orderOut: self];
173 /* here's fadeOut, just without visibly fading */
175 [self setAlphaValue:0.0];
176 [self setFadeTimer:nil];
180 - (void)setActive:(id)noData
183 [[VLCMain sharedInstance] showFullscreenController];
186 /* This routine is called repeatedly to fade in the window */
187 - (void)focus:(NSTimer *)timer
189 /* we need to push ourselves to front if the vout window was closed since our last display */
190 if(b_voutWasUpdated) {
191 [self orderFront: self];
192 b_voutWasUpdated = NO;
195 if([self alphaValue] < 1.0)
196 [self setAlphaValue:[self alphaValue]+0.1];
197 if([self alphaValue] >= 1.0) {
199 [self setAlphaValue: 1.0];
200 [self setFadeTimer:nil];
203 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(unfocus:) userInfo:NULL repeats:YES]];
208 /* This routine is called repeatedly to hide the window */
209 - (void)unfocus:(NSTimer *)timer
214 [self setFadeTimer: NULL];
218 if([self alphaValue] > 0.0)
219 [self setAlphaValue:[self alphaValue]-0.05];
220 if([self alphaValue] <= 0.05) {
222 [self setAlphaValue:0.0];
223 [self setFadeTimer:nil];
227 [NSTimer scheduledTimerWithTimeInterval:0.1
229 selector:@selector(focus:)
236 - (void)mouseExited:(NSEvent *)theEvent
238 /* give up our focus, so the vout may show us again without letting the user clicking it */
239 vout_thread_t *p_vout = getVout();
241 if (var_GetBool(p_vout, "fullscreen"))
242 [[[[VLCMainWindow sharedInstance] videoView] window] makeKeyWindow];
243 vlc_object_release(p_vout);
249 [NSCursor setHiddenUntilMouseMoves: YES];
254 /* in case that the user don't want us to appear, make sure we hide the mouse */
256 if(!config_GetInt(VLCIntf, "macosx-fspanel")) {
257 float time = (float)var_CreateGetInteger(VLCIntf, "mouse-hide-timeout") / 1000.;
258 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(hideMouse) userInfo:nil repeats:NO]];
265 [self orderFront: nil];
267 if([self alphaValue] < 1.0 || b_displayed != YES) {
268 if (![self fadeTimer])
269 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(focus:) userInfo:[NSNumber numberWithShort:1] repeats:YES]];
270 else if ([[[self fadeTimer] userInfo] shortValue]==0)
278 if(NSPointInRect([NSEvent mouseLocation],[self frame]))
281 if(([self alphaValue] > 0.0)) {
282 if (![self fadeTimer])
283 [self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(unfocus:) userInfo:[NSNumber numberWithShort:0] repeats:YES]];
284 else if ([[[self fadeTimer] userInfo] shortValue]==1)
289 /* triggers a timer to autoHide us again after some seconds of no activity */
292 /* this will tell the timer to start over again or to start at all */
295 /* get us a valid timer */
296 if(! b_alreadyCounting) {
297 i_timeToKeepVisibleInSec = var_CreateGetInteger(VLCIntf, "mouse-hide-timeout") / 500;
299 [hideAgainTimer invalidate];
300 [hideAgainTimer autorelease];
302 /* released in -autoHide and -dealloc */
303 hideAgainTimer = [[NSTimer scheduledTimerWithTimeInterval: 0.5
305 selector: @selector(keepVisible:)
307 repeats: YES] retain];
308 b_alreadyCounting = YES;
312 - (void)keepVisible:(NSTimer *)timer
314 /* if the user triggered an action, start over again */
318 /* count down until we hide ourselfes again and do so if necessary */
319 if(--i_timeToKeepVisibleInSec < 1) {
322 [hideAgainTimer invalidate]; /* released in -autoHide and -dealloc */
323 b_alreadyCounting = NO;
327 /* A getter and setter for our main timer that handles window fading */
328 - (NSTimer *)fadeTimer
333 - (void)setFadeTimer:(NSTimer *)timer
336 [fadeTimer invalidate];
337 [fadeTimer autorelease];
341 - (void)mouseDown:(NSEvent *)theEvent
343 mouseClic = [theEvent locationInWindow];
346 - (void)mouseDragged:(NSEvent *)theEvent
348 NSPoint point = [NSEvent mouseLocation];
349 point.x -= mouseClic.x;
350 point.y -= mouseClic.y;
351 [self setFrameOrigin:point];
354 - (void)setVoutWasUpdated: (int)i_newdevice;
356 b_voutWasUpdated = YES;
357 if(i_newdevice != i_device) {
358 i_device = i_newdevice;
364 /*****************************************************************************
366 *****************************************************************************/
367 @implementation VLCFSPanelView
369 #define addButton(o_button, imageOff, imageOn, _x, _y, action, AXDesc, ToolTip) \
370 s_rc.origin.x = _x; \
371 s_rc.origin.y = _y; \
372 o_button = [[NSButton alloc] initWithFrame: s_rc]; \
373 [o_button setButtonType: NSMomentaryChangeButton]; \
374 [o_button setBezelStyle: NSRegularSquareBezelStyle]; \
375 [o_button setBordered: NO]; \
376 [o_button setFont:[NSFont systemFontOfSize:0]]; \
377 [o_button setImage:[NSImage imageNamed:imageOff]]; \
378 [o_button setAlternateImage:[NSImage imageNamed:imageOn]]; \
379 [o_button sizeToFit]; \
380 [o_button setTarget: self]; \
381 [o_button setAction: @selector(action:)]; \
382 [[o_button cell] accessibilitySetOverrideValue:AXDesc forAttribute:NSAccessibilityDescriptionAttribute]; \
383 [[o_button cell] accessibilitySetOverrideValue:ToolTip forAttribute:NSAccessibilityTitleAttribute]; \
384 [o_button setToolTip: ToolTip]; \
385 [self addSubview:o_button];
387 #define addTextfield(class, o_text, align, font, color) \
388 o_text = [[class alloc] initWithFrame: s_rc]; \
389 [o_text setDrawsBackground: NO]; \
390 [o_text setBordered: NO]; \
391 [o_text setEditable: NO]; \
392 [o_text setSelectable: NO]; \
393 [o_text setStringValue: _NS("(no item is being played)")]; \
394 [o_text setAlignment: align]; \
395 [o_text setTextColor: [NSColor color]]; \
396 [o_text setFont:[NSFont font:[NSFont smallSystemFontSize]]]; \
397 [self addSubview:o_text];
399 - (id)initWithFrame:(NSRect)frameRect
401 id view = [super initWithFrame:frameRect];
402 fillColor = [[NSColor clearColor] retain];
403 NSRect s_rc = [self frame];
404 addButton(o_prev, @"fs_skip_previous" , @"fs_skip_previous_highlight", 174, 15, prev, _NS("Click to go to the previous playlist item."), _NS("Previous"));
405 addButton(o_bwd, @"fs_rewind" , @"fs_rewind_highlight" , 211, 14, backward, _NS("Click and hold to skip backward through the current media."), _NS("Backward"));
406 addButton(o_play, @"fs_play" , @"fs_play_highlight" , 267, 10, play, _NS("Click to play or pause the current media."), _NS("Play/Pause"));
407 addButton(o_fwd, @"fs_forward" , @"fs_forward_highlight" , 313, 14, forward, _NS("Click and hold to skip forward through the current media."), _NS("Forward"));
408 addButton(o_next, @"fs_skip_next" , @"fs_skip_next_highlight" , 365, 15, next, _NS("Click to go to the next playlist item."), _NS("Next"));
409 addButton(o_fullscreen, @"fs_exit_fullscreen", @"fs_exit_fullscreen_hightlight", 507, 13, toggleFullscreen, _NS("Click to exit fullscreen playback."), _NS("Toggle Fullscreen mode"));
411 addButton(o_button, @"image (off state)", @"image (on state)", 38, 51, something, accessibility help string, usual tool tip);
413 [o_fwd setContinuous:YES];
414 [o_bwd setContinuous:YES];
420 s_rc.size.width = 518;
421 s_rc.size.height = 9;
422 o_fs_timeSlider = [[VLCFSTimeSlider alloc] initWithFrame: s_rc];
423 [o_fs_timeSlider setMinValue:0];
424 [o_fs_timeSlider setMaxValue:10000];
425 [o_fs_timeSlider setFloatValue: 0];
426 [o_fs_timeSlider setContinuous: YES];
427 [o_fs_timeSlider setTarget: self];
428 [o_fs_timeSlider setAction: @selector(fsTimeSliderUpdate:)];
429 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Position") forAttribute:NSAccessibilityTitleAttribute];
430 [[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];
431 [self addSubview: o_fs_timeSlider];
437 s_rc.size.width = 95;
438 s_rc.size.height = 10;
439 o_fs_volumeSlider = [[VLCFSVolumeSlider alloc] initWithFrame: s_rc];
440 [o_fs_volumeSlider setMinValue:0];
441 [o_fs_volumeSlider setMaxValue:AOUT_VOLUME_MAX];
442 [o_fs_volumeSlider setIntValue:AOUT_VOLUME_DEFAULT];
443 [o_fs_volumeSlider setContinuous: YES];
444 [o_fs_volumeSlider setTarget: self];
445 [o_fs_volumeSlider setAction: @selector(fsVolumeSliderUpdate:)];
446 [[o_fs_volumeSlider cell] accessibilitySetOverrideValue:_NS("Volume") forAttribute:NSAccessibilityTitleAttribute];
447 [[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];
448 [self addSubview: o_fs_volumeSlider];
450 /* time counter and stream title output fields */
454 s_rc.size.width = 352;
455 s_rc.size.height = 14;
456 addTextfield(NSTextField, o_streamTitle_txt, NSCenterTextAlignment, systemFontOfSize, whiteColor);
459 s_rc.size.width = 65;
460 addTextfield(VLCTimeField, o_streamPosition_txt, NSRightTextAlignment, systemFontOfSize, whiteColor);
462 o_background_img = [[NSImage imageNamed:@"fs_background"] retain];
463 o_vol_sld_img = [[NSImage imageNamed:@"fs_volume_slider_bar"] retain];
464 o_vol_mute_img = [[NSImage imageNamed:@"fs_volume_mute"] retain];
465 o_vol_max_img = [[NSImage imageNamed:@"fs_volume_max"] retain];
466 o_time_sld_img = [[NSImage imageNamed:@"fs_time_slider"] retain];
473 [o_background_img release];
474 [o_vol_sld_img release];
475 [o_vol_mute_img release];
476 [o_vol_max_img release];
477 [o_time_sld_img release];
478 [o_fs_timeSlider release];
479 [o_fs_volumeSlider release];
485 [o_fullscreen release];
486 [o_streamTitle_txt release];
487 [o_streamPosition_txt release];
493 [o_play setImage:[NSImage imageNamed:@"fs_play"]];
494 [o_play setAlternateImage: [NSImage imageNamed:@"fs_play_highlight"]];
499 [o_play setImage: [NSImage imageNamed:@"fs_pause"]];
500 [o_play setAlternateImage: [NSImage imageNamed:@"fs_pause_highlight"]];
503 - (void)setStreamTitle:(NSString *)o_title
505 [o_streamTitle_txt setStringValue: o_title];
508 - (void)setStreamPos:(float) f_pos andTime:(NSString *)o_time
510 [o_streamPosition_txt setStringValue: o_time];
511 [o_fs_timeSlider setFloatValue: f_pos];
514 - (void)setSeekable:(BOOL)b_seekable
516 [o_bwd setEnabled: b_seekable];
517 [o_fwd setEnabled: b_seekable];
518 [o_fs_timeSlider setEnabled: b_seekable];
521 - (void)setVolumeLevel: (int)i_volumeLevel
523 [o_fs_volumeSlider setIntValue: i_volumeLevel];
526 - (IBAction)play:(id)sender
528 [[VLCCoreInteraction sharedInstance] play];
531 - (IBAction)forward:(id)sender
533 if (([NSDate timeIntervalSinceReferenceDate] - last_fwd_event) > 0.16) {
534 // we just skipped 4 "continous" events, otherwise we are too fast
535 [[VLCCoreInteraction sharedInstance] forwardExtraShort];
536 last_fwd_event = [NSDate timeIntervalSinceReferenceDate];
540 - (IBAction)backward:(id)sender
542 if (([NSDate timeIntervalSinceReferenceDate] - last_bwd_event) > 0.16) {
543 // we just skipped 4 "continous" events, otherwise we are too fast
544 [[VLCCoreInteraction sharedInstance] backwardExtraShort];
545 last_bwd_event = [NSDate timeIntervalSinceReferenceDate];
549 - (IBAction)prev:(id)sender
551 [[VLCCoreInteraction sharedInstance] previous];
554 - (IBAction)next:(id)sender
556 [[VLCCoreInteraction sharedInstance] next];
559 - (IBAction)toggleFullscreen:(id)sender
561 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
564 - (IBAction)fsTimeSliderUpdate:(id)sender
566 input_thread_t * p_input;
567 p_input = pl_CurrentInput(VLCIntf);
568 if(p_input != NULL) {
571 pos.f_float = [o_fs_timeSlider floatValue] / 10000.;
572 var_Set(p_input, "position", pos);
573 vlc_object_release(p_input);
575 [[VLCMain sharedInstance] updatePlaybackPosition];
578 - (IBAction)fsVolumeSliderUpdate:(id)sender
580 [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
583 #define addImage(image, _x, _y, mode) \
584 image_size = [image size]; \
585 image_rect.size = image_size; \
586 image_rect.origin.x = 0; \
587 image_rect.origin.y = 0; \
588 frame.origin.x = _x; \
589 frame.origin.y = _y; \
590 frame.size = image_size; \
591 [image drawInRect:frame fromRect:image_rect operation:mode fraction:1];
593 - (void)drawRect:(NSRect)rect
595 NSRect frame = [self frame];
599 addImage(o_background_img, 0, 0, NSCompositeCopy);
600 addImage(o_vol_sld_img, 26, 23, NSCompositeSourceOver);
601 addImage(o_vol_mute_img, 16, 18, NSCompositeSourceOver);
602 addImage(o_vol_max_img, 124, 18, NSCompositeSourceOver);
603 addImage(o_time_sld_img, 15, 53, NSCompositeSourceOver);
608 /*****************************************************************************
610 *****************************************************************************/
611 @implementation VLCFSTimeSlider
612 - (void)drawKnobInRect:(NSRect)knobRect
615 NSImage *img = [NSImage imageNamed:@"fs_time_slider_knob_highlight"];
616 image_rect.size = [img size];
617 image_rect.origin.x = 0;
618 image_rect.origin.y = 0;
619 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
620 knobRect.size.width = image_rect.size.width;
621 knobRect.size.height = image_rect.size.height;
622 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
625 - (void)drawRect:(NSRect)rect
627 /* Draw default to make sure the slider behaves correctly */
628 [[NSGraphicsContext currentContext] saveGraphicsState];
629 NSRectClip(NSZeroRect);
630 [super drawRect:rect];
631 [[NSGraphicsContext currentContext] restoreGraphicsState];
633 NSRect knobRect = [[self cell] knobRectFlipped:NO];
634 knobRect.origin.y+=7.5;
635 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
636 [self drawKnobInRect: knobRect];
641 /*****************************************************************************
643 *****************************************************************************/
644 @implementation VLCFSVolumeSlider
645 - (void)drawKnobInRect:(NSRect) knobRect
648 NSImage *img = [NSImage imageNamed:@"fs_volume_slider_knob"];
649 image_rect.size = [img size];
650 image_rect.origin.x = 0;
651 image_rect.origin.y = 0;
652 knobRect.origin.x += (knobRect.size.width - image_rect.size.width) / 2;
653 knobRect.size.width = image_rect.size.width;
654 knobRect.size.height = image_rect.size.height;
655 [img drawInRect:knobRect fromRect:image_rect operation:NSCompositeSourceOver fraction:1];
658 - (void)drawRect:(NSRect)rect
660 /* Draw default to make sure the slider behaves correctly */
661 [[NSGraphicsContext currentContext] saveGraphicsState];
662 NSRectClip(NSZeroRect);
663 [super drawRect:rect];
664 [[NSGraphicsContext currentContext] restoreGraphicsState];
666 NSRect knobRect = [[self cell] knobRectFlipped:NO];
667 knobRect.origin.y+=6;
668 [[[NSColor blackColor] colorWithAlphaComponent:0.6] set];
669 [self drawKnobInRect: knobRect];