]> git.sesse.net Git - vlc/blob - modules/gui/macosx/VideoView.m
ed0f0050699b91859cb5a26ab897776c6ff02dcc
[vlc] / modules / gui / macosx / VideoView.m
1 /*****************************************************************************
2  * VideoView.m: MacOS X video output module
3  *****************************************************************************
4  * Copyright (C) 2002-2012 VLC authors and VideoLAN
5  * $Id$
6  *
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  *
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.
17  * 
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.
22  *
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  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #import <stdlib.h>                                                 /* free() */
32 #import <string.h>
33
34 #import "intf.h"
35 #import "VideoView.h"
36 #import "CoreInteraction.h"
37 #import "MainMenu.h"
38 #import "MainWindow.h"
39
40 #import <vlc_common.h>
41 #import <vlc_vout_window.h>
42 #import <vlc_vout_display.h>
43 #import <vlc_keys.h>
44 #import <vlc_mouse.h>
45 /*****************************************************************************
46  * DeviceCallback: Callback triggered when the video-device variable is changed
47  *****************************************************************************/
48 int DeviceCallback( vlc_object_t *p_this, const char *psz_variable,
49                      vlc_value_t old_val, vlc_value_t new_val, void *param )
50 {
51     vlc_value_t val;
52     vout_thread_t *p_vout = (vout_thread_t *)p_this;
53
54     msg_Dbg( p_vout, "set %"PRId64, new_val.i_int );
55     var_Create( p_vout->p_libvlc, "video-device", VLC_VAR_INTEGER );
56     var_Set( p_vout->p_libvlc, "video-device", new_val );
57
58     val.b_bool = true;
59     var_Set( p_vout, "intf-change", val );
60     return VLC_SUCCESS;
61 }
62
63 /*****************************************************************************
64  * VLCOpenGLVideoView interface excerpt
65  * full implementation in modules/video_output/macosx.m:95
66  *****************************************************************************/
67 @interface VLCOpenGLVideoView : NSOpenGLView
68 {
69 }
70 - (vout_display_t *)voutDisplay;
71 @end
72
73 /*****************************************************************************
74  * VLCVoutView implementation
75  *****************************************************************************/
76 @implementation VLCVoutView
77
78 #pragma mark -
79 #pragma mark drag & drop support
80
81 - (void)dealloc
82 {
83     [self unregisterDraggedTypes];
84     [super dealloc];
85 }
86
87 - (void)awakeFromNib
88 {
89     [self registerForDraggedTypes:[NSArray arrayWithObject: NSFilenamesPboardType]];
90 }
91
92 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
93 {
94     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
95         return NSDragOperationGeneric;
96     return NSDragOperationNone;
97 }
98
99 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
100 {
101     return YES;
102 }
103
104 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
105 {
106     BOOL b_returned;
107     b_returned = [[VLCCoreInteraction sharedInstance] performDragOperation: sender];
108
109     [self setNeedsDisplay:YES];
110     return b_returned;
111 }
112
113 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
114 {
115     [self setNeedsDisplay:YES];
116 }
117
118 #pragma mark -
119 #pragma mark vout actions
120
121 - (void)closeVout
122 {
123     vout_thread_t * p_vout = getVout();
124     if( !p_vout )
125     {
126         var_DelCallback( p_vout, "video-device", DeviceCallback, NULL );
127         vlc_object_release( p_vout );
128     }
129 }
130
131 - (void)scrollWheel:(NSEvent *)theEvent
132 {
133     VLCControls * o_controls = (VLCControls *)[[NSApp delegate] controls];
134     [o_controls scrollWheel: theEvent];
135 }
136
137 - (void)keyDown:(NSEvent *)o_event
138 {
139     unichar key = 0;
140     vlc_value_t val;
141     unsigned int i_pressed_modifiers = 0;
142     val.i_int = 0;
143
144     i_pressed_modifiers = [o_event modifierFlags];
145
146     if( i_pressed_modifiers & NSShiftKeyMask )
147         val.i_int |= KEY_MODIFIER_SHIFT;
148     if( i_pressed_modifiers & NSControlKeyMask )
149         val.i_int |= KEY_MODIFIER_CTRL;
150     if( i_pressed_modifiers & NSAlternateKeyMask )
151         val.i_int |= KEY_MODIFIER_ALT;
152     if( i_pressed_modifiers & NSCommandKeyMask )
153         val.i_int |= KEY_MODIFIER_COMMAND;
154
155     key = [[[o_event charactersIgnoringModifiers] lowercaseString] characterAtIndex: 0];
156
157     if( key )
158     {
159         vout_thread_t * p_vout = getVout();
160         /* Escape should always get you out of fullscreen */
161         if( key == (unichar) 0x1b )
162         {
163             playlist_t * p_playlist = pl_Get( VLCIntf );
164              if( var_GetBool( p_playlist, "fullscreen") )
165                  [[VLCCoreInteraction sharedInstance] toggleFullscreen];
166         }
167         /* handle Lion's default key combo for fullscreen-toggle in addition to our own hotkeys */
168         else if( key == 'f' && i_pressed_modifiers & NSControlKeyMask && i_pressed_modifiers & NSCommandKeyMask )
169             [[VLCCoreInteraction sharedInstance] toggleFullscreen];
170         else if ( p_vout )
171         {
172             if( key == ' ' )
173             {
174                 [[VLCCoreInteraction sharedInstance] play];
175             }
176             else
177             {
178                 val.i_int |= (int)CocoaKeyToVLC( key );
179                 var_Set( p_vout->p_libvlc, "key-pressed", val );
180             }
181             vlc_object_release( p_vout );
182         }
183         else
184             msg_Dbg( VLCIntf, "could not send keyevent to VLC core" );
185     }
186     else
187         [super keyDown: o_event];
188 }
189
190 - (void)mouseDown:(NSEvent *)o_event
191 {
192     if( ( [o_event type] == NSLeftMouseDown ) &&
193        ( ! ( [o_event modifierFlags] &  NSControlKeyMask ) ) )
194     {
195         if( [o_event clickCount] > 1 )
196         {
197             /* multiple clicking */
198             [[VLCCoreInteraction sharedInstance] toggleFullscreen];
199         }
200     }
201     else if( ( [o_event type] == NSRightMouseDown ) ||
202             ( ( [o_event type] == NSLeftMouseDown ) &&
203              ( [o_event modifierFlags] &  NSControlKeyMask ) ) )
204     {
205         [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
206     }
207
208     [super mouseDown: o_event];
209 }
210
211 - (void)rightMouseDown:(NSEvent *)o_event
212 {
213     if( [o_event type] == NSRightMouseDown )
214         [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
215
216     [super mouseDown: o_event];
217 }
218
219 - (void)rightMouseUp:(NSEvent *)o_event
220 {
221     if( [o_event type] == NSRightMouseUp )
222         [NSMenu popUpContextMenu: [[VLCMainMenu sharedInstance] voutMenu] withEvent: o_event forView: self];
223
224     [super mouseUp: o_event];
225 }
226
227 - (void)mouseMoved:(NSEvent *)o_event
228 {
229     [[VLCMain sharedInstance] showFullscreenController];
230     [super mouseMoved: o_event];
231 }
232
233 - (BOOL)mouseDownCanMoveWindow
234 {
235     return YES;
236 }
237
238 - (BOOL)acceptsFirstResponder
239 {
240     return YES;
241 }
242
243 - (BOOL)becomeFirstResponder
244 {
245     return YES;
246 }
247
248 - (BOOL)resignFirstResponder
249 {
250     /* while we need to be the first responder most of the time, we need to give up that status when toggling the playlist */
251     return YES;
252 }
253
254 -(void)didAddSubview:(NSView *)subview
255 {
256     [[self window] makeFirstResponder: subview];
257 }
258
259 @end