1 /*****************************************************************************
2 * VideoView.m: MacOS X video output module
3 *****************************************************************************
4 * Copyright (C) 2002-2012 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>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 #import <stdlib.h> /* free() */
36 #import "CoreInteraction.h"
38 #import "MainWindow.h"
40 #import <vlc_common.h>
44 /*****************************************************************************
45 * VLCVoutView implementation
46 *****************************************************************************/
47 @implementation VLCVoutView
50 #pragma mark drag & drop support
55 vlc_object_release(p_vout);
57 [self unregisterDraggedTypes];
61 -(id)initWithFrame:(NSRect)frameRect
63 if (self = [super initWithFrame:frameRect]) {
64 [self registerForDraggedTypes:[NSArray arrayWithObject: NSFilenamesPboardType]];
67 i_lastScrollWheelDirection = 0;
68 f_cumulated_magnification = 0.0;
73 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
75 if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
76 return NSDragOperationGeneric;
77 return NSDragOperationNone;
80 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
85 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
88 b_returned = [[VLCCoreInteraction sharedInstance] performDragOperation: sender];
90 [self setNeedsDisplay:YES];
94 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
96 [self setNeedsDisplay:YES];
100 #pragma mark vout actions
102 - (void)keyDown:(NSEvent *)o_event
106 unsigned int i_pressed_modifiers = 0;
109 i_pressed_modifiers = [o_event modifierFlags];
111 if (i_pressed_modifiers & NSShiftKeyMask)
112 val.i_int |= KEY_MODIFIER_SHIFT;
113 if (i_pressed_modifiers & NSControlKeyMask)
114 val.i_int |= KEY_MODIFIER_CTRL;
115 if (i_pressed_modifiers & NSAlternateKeyMask)
116 val.i_int |= KEY_MODIFIER_ALT;
117 if (i_pressed_modifiers & NSCommandKeyMask)
118 val.i_int |= KEY_MODIFIER_COMMAND;
120 NSString * characters = [o_event charactersIgnoringModifiers];
121 if ([characters length] > 0) {
122 key = [[characters lowercaseString] characterAtIndex: 0];
125 /* Escape should always get you out of fullscreen */
126 if (key == (unichar) 0x1b) {
127 playlist_t * p_playlist = pl_Get(VLCIntf);
128 if (var_GetBool(p_playlist, "fullscreen"))
129 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
131 /* handle Lion's default key combo for fullscreen-toggle in addition to our own hotkeys */
132 else if (key == 'f' && i_pressed_modifiers & NSControlKeyMask && i_pressed_modifiers & NSCommandKeyMask)
133 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
136 [[VLCCoreInteraction sharedInstance] play];
138 val.i_int |= (int)CocoaKeyToVLC(key);
139 var_Set(p_vout->p_libvlc, "key-pressed", val);
143 msg_Dbg(VLCIntf, "could not send keyevent to VLC core");
148 [super keyDown: o_event];
151 - (BOOL)performKeyEquivalent:(NSEvent *)o_event
153 return [[VLCMainWindow sharedInstance] performKeyEquivalent: o_event];
156 - (void)mouseDown:(NSEvent *)o_event
158 if (([o_event type] == NSLeftMouseDown) && (! ([o_event modifierFlags] & NSControlKeyMask))) {
159 if ([o_event clickCount] > 1)
160 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
161 } else if (([o_event type] == NSRightMouseDown) ||
162 (([o_event type] == NSLeftMouseDown) &&
163 ([o_event modifierFlags] & NSControlKeyMask)))
164 [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
166 [super mouseDown: o_event];
169 - (void)rightMouseDown:(NSEvent *)o_event
171 if ([o_event type] == NSRightMouseDown)
172 [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
174 [super mouseDown: o_event];
177 - (void)rightMouseUp:(NSEvent *)o_event
179 if ([o_event type] == NSRightMouseUp)
180 [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
182 [super mouseUp: o_event];
185 - (void)mouseMoved:(NSEvent *)o_event
187 NSPoint ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
188 if ([self mouse: ml inRect: [self bounds]])
189 [[VLCMain sharedInstance] showFullscreenController];
191 [super mouseMoved: o_event];
194 - (void)resetScrollWheelDirection
196 /* release the scroll direction 0.8 secs after the last event */
197 if (([NSDate timeIntervalSinceReferenceDate] - t_lastScrollEvent) >= 0.80)
198 i_lastScrollWheelDirection = 0;
201 - (void)scrollWheel:(NSEvent *)theEvent
203 intf_thread_t * p_intf = VLCIntf;
204 CGFloat f_deltaX = [theEvent deltaX];
205 CGFloat f_deltaY = [theEvent deltaY];
207 if (!OSX_SNOW_LEOPARD && [theEvent isDirectionInvertedFromDevice]) {
208 f_deltaX = -f_deltaX;
209 f_deltaY = -f_deltaY;
212 CGFloat f_yabsvalue = f_deltaY > 0.0f ? f_deltaY : -f_deltaY;
213 CGFloat f_xabsvalue = f_deltaX > 0.0f ? f_deltaX : -f_deltaX;
215 int i_yvlckey, i_xvlckey = 0;
217 i_yvlckey = KEY_MOUSEWHEELDOWN;
219 i_yvlckey = KEY_MOUSEWHEELUP;
222 i_xvlckey = KEY_MOUSEWHEELRIGHT;
224 i_xvlckey = KEY_MOUSEWHEELLEFT;
226 /* in the following, we're forwarding either a x or a y event */
227 /* Multiple key events are send depending on the intensity of the event */
228 /* the opposite direction is being blocked for 0.8 secs */
229 if (f_yabsvalue > 0.05) {
230 if (i_lastScrollWheelDirection < 0) // last was a X
233 i_lastScrollWheelDirection = 1; // Y
234 for (NSUInteger i = 0; i < (int)(f_yabsvalue/4.+1.); i++)
235 var_SetInteger(p_intf->p_libvlc, "key-pressed", i_yvlckey);
237 t_lastScrollEvent = [NSDate timeIntervalSinceReferenceDate];
238 [self performSelector:@selector(resetScrollWheelDirection)
243 if (f_xabsvalue > 0.05) {
244 if (i_lastScrollWheelDirection > 0) // last was a Y
247 i_lastScrollWheelDirection = -1; // X
248 for (NSUInteger i = 0; i < (int)(f_xabsvalue/6.+1.); i++)
249 var_SetInteger(p_intf->p_libvlc, "key-pressed", i_xvlckey);
251 t_lastScrollEvent = [NSDate timeIntervalSinceReferenceDate];
252 [self performSelector:@selector(resetScrollWheelDirection)
259 #pragma mark Handling of vout related actions
261 - (void)setVoutThread:(vout_thread_t *)p_vout_thread
263 assert(p_vout == NULL);
264 p_vout = p_vout_thread;
265 vlc_object_hold(p_vout);
268 - (vout_thread_t *)voutThread
271 vlc_object_hold(p_vout);
278 - (void)releaseVoutThread
281 vlc_object_release(p_vout);
287 #pragma mark Basic view behaviour and touch events handling
289 - (BOOL)mouseDownCanMoveWindow
294 - (BOOL)acceptsFirstResponder
299 - (BOOL)becomeFirstResponder
304 - (BOOL)resignFirstResponder
306 /* while we need to be the first responder most of the time, we need to give up that status when toggling the playlist */
310 -(void)didAddSubview:(NSView *)subview
312 [[self window] makeFirstResponder: subview];
315 - (void)magnifyWithEvent:(NSEvent *)event
317 f_cumulated_magnification += [event magnification];
319 // This is the result of [NSEvent standardMagnificationThreshold].
320 // Unfortunately, this is a private API, currently.
321 CGFloat f_threshold = 0.3;
322 BOOL b_fullscreen = [[VLCMainWindow sharedInstance] fullscreen];
324 if ((f_cumulated_magnification > f_threshold && !b_fullscreen) || (f_cumulated_magnification < -f_threshold && b_fullscreen)) {
325 f_cumulated_magnification = 0.0;
326 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
330 - (void)beginGestureWithEvent:(NSEvent *)event
332 f_cumulated_magnification = 0.0;