1 /*****************************************************************************
2 * VideoView.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2002-2013 VLC authors and VideoLAN
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8 * Eric Petit <titer@m0k.org>
9 * Benjamin Pracht <bigben at videolan dot org>
10 * Pierre d'Herbemont <pdherbemont # videolan org>
11 * Felix Paul Kühne <fkuehne at videolan dot org>
12 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
32 #import <stdlib.h> /* free() */
37 #import "CoreInteraction.h"
39 #import "MainWindow.h"
41 #import <vlc_common.h>
45 /*****************************************************************************
46 * VLCVoutView implementation
47 *****************************************************************************/
48 @implementation VLCVoutView
51 #pragma mark drag & drop support
56 vlc_object_release(p_vout);
58 [self unregisterDraggedTypes];
62 -(id)initWithFrame:(NSRect)frameRect
64 if (self = [super initWithFrame:frameRect]) {
65 [self registerForDraggedTypes:[NSArray arrayWithObject: NSFilenamesPboardType]];
68 i_lastScrollWheelDirection = 0;
69 f_cumulated_magnification = 0.0;
74 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
76 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
77 return NSDragOperationGeneric;
78 return NSDragOperationNone;
81 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
86 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
89 b_returned = [[VLCCoreInteraction sharedInstance] performDragOperation: sender];
91 [self setNeedsDisplay:YES];
95 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
97 [self setNeedsDisplay:YES];
101 #pragma mark vout actions
103 - (void)keyDown:(NSEvent *)o_event
107 unsigned int i_pressed_modifiers = 0;
110 i_pressed_modifiers = [o_event modifierFlags];
112 if (i_pressed_modifiers & NSShiftKeyMask)
113 val.i_int |= KEY_MODIFIER_SHIFT;
114 if (i_pressed_modifiers & NSControlKeyMask)
115 val.i_int |= KEY_MODIFIER_CTRL;
116 if (i_pressed_modifiers & NSAlternateKeyMask)
117 val.i_int |= KEY_MODIFIER_ALT;
118 if (i_pressed_modifiers & NSCommandKeyMask)
119 val.i_int |= KEY_MODIFIER_COMMAND;
121 NSString * characters = [o_event charactersIgnoringModifiers];
122 if ([characters length] > 0) {
123 key = [[characters lowercaseString] characterAtIndex: 0];
126 /* Escape should always get you out of fullscreen */
127 if (key == (unichar) 0x1b) {
128 playlist_t * p_playlist = pl_Get(VLCIntf);
129 if (var_GetBool(p_playlist, "fullscreen"))
130 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
132 /* handle Lion's default key combo for fullscreen-toggle in addition to our own hotkeys */
133 else if (key == 'f' && i_pressed_modifiers & NSControlKeyMask && i_pressed_modifiers & NSCommandKeyMask)
134 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
136 val.i_int |= (int)CocoaKeyToVLC(key);
137 var_Set(p_vout->p_libvlc, "key-pressed", val);
140 msg_Dbg(VLCIntf, "could not send keyevent to VLC core");
145 [super keyDown: o_event];
148 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
150 return [[VLCMainWindow sharedInstance] performKeyEquivalent: o_event];
153 - (void)mouseDown:(NSEvent *)o_event
155 if (([o_event type] == NSLeftMouseDown) && (! ([o_event modifierFlags] & NSControlKeyMask))) {
156 if ([o_event clickCount] > 1)
157 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
158 } else if (([o_event type] == NSRightMouseDown) ||
159 (([o_event type] == NSLeftMouseDown) &&
160 ([o_event modifierFlags] & NSControlKeyMask)))
161 [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
163 [super mouseDown: o_event];
166 - (void)rightMouseDown:(NSEvent *)o_event
168 if ([o_event type] == NSRightMouseDown)
169 [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
171 [super mouseDown: o_event];
174 - (void)rightMouseUp:(NSEvent *)o_event
176 if ([o_event type] == NSRightMouseUp)
177 [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
179 [super mouseUp: o_event];
182 - (void)mouseMoved:(NSEvent *)o_event
184 NSPoint ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
185 if ([self mouse: ml inRect: [self bounds]])
186 [[VLCMain sharedInstance] showFullscreenController];
188 [super mouseMoved: o_event];
191 - (void)resetScrollWheelDirection
193 /* release the scroll direction 0.8 secs after the last event */
194 if (([NSDate timeIntervalSinceReferenceDate] - t_lastScrollEvent) >= 0.80)
195 i_lastScrollWheelDirection = 0;
198 - (void)scrollWheel:(NSEvent *)theEvent
200 intf_thread_t * p_intf = VLCIntf;
201 CGFloat f_deltaX = [theEvent deltaX];
202 CGFloat f_deltaY = [theEvent deltaY];
204 if (!OSX_SNOW_LEOPARD && [theEvent isDirectionInvertedFromDevice]) {
205 f_deltaX = -f_deltaX;
206 f_deltaY = -f_deltaY;
209 CGFloat f_yabsvalue = f_deltaY > 0.0f ? f_deltaY : -f_deltaY;
210 CGFloat f_xabsvalue = f_deltaX > 0.0f ? f_deltaX : -f_deltaX;
212 int i_yvlckey, i_xvlckey = 0;
214 i_yvlckey = KEY_MOUSEWHEELDOWN;
216 i_yvlckey = KEY_MOUSEWHEELUP;
219 i_xvlckey = KEY_MOUSEWHEELRIGHT;
221 i_xvlckey = KEY_MOUSEWHEELLEFT;
223 /* in the following, we're forwarding either a x or a y event */
224 /* Multiple key events are send depending on the intensity of the event */
225 /* the opposite direction is being blocked for 0.8 secs */
226 if (f_yabsvalue > 0.05) {
227 if (i_lastScrollWheelDirection < 0) // last was a X
230 i_lastScrollWheelDirection = 1; // Y
231 for (NSUInteger i = 0; i < (int)(f_yabsvalue/4.+1.); i++)
232 var_SetInteger(p_intf->p_libvlc, "key-pressed", i_yvlckey);
234 t_lastScrollEvent = [NSDate timeIntervalSinceReferenceDate];
235 [self performSelector:@selector(resetScrollWheelDirection)
240 if (f_xabsvalue > 0.05) {
241 if (i_lastScrollWheelDirection > 0) // last was a Y
244 i_lastScrollWheelDirection = -1; // X
245 for (NSUInteger i = 0; i < (int)(f_xabsvalue/6.+1.); i++)
246 var_SetInteger(p_intf->p_libvlc, "key-pressed", i_xvlckey);
248 t_lastScrollEvent = [NSDate timeIntervalSinceReferenceDate];
249 [self performSelector:@selector(resetScrollWheelDirection)
256 #pragma mark Handling of vout related actions
258 - (void)setVoutThread:(vout_thread_t *)p_vout_thread
260 assert(p_vout == NULL);
261 p_vout = p_vout_thread;
262 vlc_object_hold(p_vout);
265 - (vout_thread_t *)voutThread
268 vlc_object_hold(p_vout);
275 - (void)releaseVoutThread
278 vlc_object_release(p_vout);
284 #pragma mark Basic view behaviour and touch events handling
286 - (BOOL)mouseDownCanMoveWindow
291 - (BOOL)acceptsFirstResponder
296 - (BOOL)becomeFirstResponder
301 - (BOOL)resignFirstResponder
303 /* while we need to be the first responder most of the time, we need to give up that status when toggling the playlist */
307 -(void)didAddSubview:(NSView *)subview
309 [[self window] makeFirstResponder: subview];
312 - (void)magnifyWithEvent:(NSEvent *)event
314 f_cumulated_magnification += [event magnification];
316 // This is the result of [NSEvent standardMagnificationThreshold].
317 // Unfortunately, this is a private API, currently.
318 CGFloat f_threshold = 0.3;
319 BOOL b_fullscreen = [(VLCVideoWindowCommon *)[self window] fullscreen];
321 if ((f_cumulated_magnification > f_threshold && !b_fullscreen) || (f_cumulated_magnification < -f_threshold && b_fullscreen)) {
322 f_cumulated_magnification = 0.0;
323 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
327 - (void)beginGestureWithEvent:(NSEvent *)event
329 f_cumulated_magnification = 0.0;