1 /*****************************************************************************
2 * controls.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2002-2012 VLC authors and VideoLAN
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * Benjamin Pracht <bigben at videolan doit 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 #include <stdlib.h> /* malloc(), free() */
32 #include <sys/param.h> /* for MAXPATHLEN */
41 #import "CoreInteraction.h"
45 /*****************************************************************************
46 * VLCControls implementation
47 *****************************************************************************/
48 @implementation VLCControls
52 [o_specificTime_mi setTitle: _NS("Jump To Time")];
53 [o_specificTime_cancel_btn setTitle: _NS("Cancel")];
54 [o_specificTime_ok_btn setTitle: _NS("OK")];
55 [o_specificTime_sec_lbl setStringValue: _NS("sec.")];
56 [o_specificTime_goTo_lbl setStringValue: _NS("Jump to time")];
58 i_lastScrollWheelDirection = 0;
64 [[NSNotificationCenter defaultCenter] removeObserver: self];
69 - (IBAction)play:(id)sender
71 [[VLCCoreInteraction sharedInstance] play];
74 - (IBAction)stop:(id)sender
76 [[VLCCoreInteraction sharedInstance] stop];
79 - (IBAction)prev:(id)sender
81 [[VLCCoreInteraction sharedInstance] previous];
84 - (IBAction)next:(id)sender
86 [[VLCCoreInteraction sharedInstance] next];
89 - (IBAction)random:(id)sender
91 [[VLCCoreInteraction sharedInstance] shuffle];
94 - (IBAction)repeat:(id)sender
97 intf_thread_t * p_intf = VLCIntf;
98 playlist_t * p_playlist = pl_Get(p_intf);
100 var_Get(p_playlist, "repeat", &val);
102 [[VLCCoreInteraction sharedInstance] repeatOne];
104 [[VLCCoreInteraction sharedInstance] repeatOff];
107 - (IBAction)loop:(id)sender
110 intf_thread_t * p_intf = VLCIntf;
111 playlist_t * p_playlist = pl_Get(p_intf);
113 var_Get(p_playlist, "loop", &val);
115 [[VLCCoreInteraction sharedInstance] repeatAll];
117 [[VLCCoreInteraction sharedInstance] repeatOff];
120 - (IBAction)quitAfterPlayback:(id)sender
123 playlist_t * p_playlist = pl_Get(VLCIntf);
124 var_ToggleBool(p_playlist, "play-and-exit");
127 - (IBAction)forward:(id)sender
129 [[VLCCoreInteraction sharedInstance] forward];
132 - (IBAction)backward:(id)sender
134 [[VLCCoreInteraction sharedInstance] backward];
137 - (IBAction)volumeUp:(id)sender
139 [[VLCCoreInteraction sharedInstance] volumeUp];
142 - (IBAction)volumeDown:(id)sender
144 [[VLCCoreInteraction sharedInstance] volumeDown];
147 - (IBAction)mute:(id)sender
149 [[VLCCoreInteraction sharedInstance] setMute: YES];
152 - (IBAction)volumeSliderUpdated:(id)sender
154 [[VLCCoreInteraction sharedInstance] setVolume: [sender intValue]];
157 - (IBAction)showPosition: (id)sender
159 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
160 if (p_input != NULL) {
161 vout_thread_t *p_vout = input_GetVout(p_input);
162 if (p_vout != NULL) {
163 var_SetInteger(VLCIntf->p_libvlc, "key-action", ACTIONID_POSITION);
164 vlc_object_release(p_vout);
166 vlc_object_release(p_input);
170 - (IBAction)telxTransparent:(id)sender
173 p_vbi = (vlc_object_t *) vlc_object_find_name(pl_Get(VLCIntf), "zvbi");
175 var_SetBool(p_vbi, "vbi-opaque", [sender state]);
176 [sender setState: ![sender state]];
177 vlc_object_release(p_vbi);
181 - (IBAction)telxNavLink:(id)sender
183 intf_thread_t * p_intf = VLCIntf;
187 if ([[sender title] isEqualToString: _NS("Index")])
189 else if ([[sender title] isEqualToString: _NS("Red")])
191 else if ([[sender title] isEqualToString: _NS("Green")])
193 else if ([[sender title] isEqualToString: _NS("Yellow")])
195 else if ([[sender title] isEqualToString: _NS("Blue")])
197 if (i_page == 0) return;
199 p_vbi = (vlc_object_t *) vlc_object_find_name(pl_Get(VLCIntf), "zvbi");
201 var_SetInteger(p_vbi, "vbi-page", i_page);
202 vlc_object_release(p_vbi);
206 - (IBAction)lockVideosAspectRatio:(id)sender
208 [[VLCCoreInteraction sharedInstance] setAspectRatioIsLocked: ![sender state]];
209 [sender setState: [[VLCCoreInteraction sharedInstance] aspectRatioIsLocked]];
212 - (IBAction)addSubtitleFile:(id)sender
214 NSInteger i_returnValue = 0;
215 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
219 input_item_t *p_item = input_GetItem(p_input);
221 vlc_object_release(p_input);
225 char *path = input_item_GetURI(p_item);
229 NSOpenPanel * openPanel = [NSOpenPanel openPanel];
230 [openPanel setCanChooseFiles: YES];
231 [openPanel setCanChooseDirectories: NO];
232 [openPanel setAllowsMultipleSelection: YES];
233 [openPanel setAllowedFileTypes: [NSArray arrayWithObjects: @"cdg",@"@idx",@"srt",@"sub",@"utf",@"ass",@"ssa",@"aqt",@"jss",@"psb",@"rt",@"smi",@"txt",@"smil", nil]];
234 [openPanel setDirectoryURL:[NSURL fileURLWithPath:[[NSString stringWithUTF8String:path] stringByExpandingTildeInPath]]];
235 i_returnValue = [openPanel runModal];
238 if (i_returnValue == NSOKButton) {
243 c = [[openPanel URLs] count];
245 for (int i = 0; i < c ; i++) {
246 msg_Dbg(VLCIntf, "loading subs from %s", [[[[openPanel URLs] objectAtIndex: i] path] UTF8String]);
247 if (input_AddSubtitle(p_input, [[[[openPanel URLs] objectAtIndex: i] path] UTF8String], TRUE))
248 msg_Warn(VLCIntf, "unable to load subtitles from '%s'",
249 [[[[openPanel URLs] objectAtIndex: i] path] UTF8String]);
252 vlc_object_release(p_input);
255 - (void)resetScrollWheelDirection
257 /* release the scroll direction 0.8 secs after the last event */
258 if (([NSDate timeIntervalSinceReferenceDate] - t_lastScrollEvent) >= 0.80)
259 i_lastScrollWheelDirection = 0;
262 - (void)scrollWheel:(NSEvent *)theEvent
264 intf_thread_t * p_intf = VLCIntf;
265 BOOL b_invertedEventFromDevice = NO;
266 CGFloat f_deltaY, f_deltaX = .0;
268 if (!OSX_SNOW_LEOPARD) {
269 if ([theEvent isDirectionInvertedFromDevice])
270 b_invertedEventFromDevice = YES;
273 f_deltaY = [theEvent deltaY];
274 f_deltaX = [theEvent deltaX];
276 CGFloat f_yabsvalue = f_deltaY > 0.0f ? f_deltaY : -f_deltaY;
277 CGFloat f_xabsvalue = f_deltaX > 0.0f ? f_deltaX : -f_deltaX;
279 int i_yvlckey, i_xvlckey = 0;
281 if (b_invertedEventFromDevice) {
283 i_yvlckey = KEY_MOUSEWHEELDOWN;
285 i_yvlckey = KEY_MOUSEWHEELUP;
288 i_xvlckey = KEY_MOUSEWHEELRIGHT;
290 i_xvlckey = KEY_MOUSEWHEELLEFT;
293 i_yvlckey = KEY_MOUSEWHEELDOWN;
295 i_yvlckey = KEY_MOUSEWHEELUP;
298 i_xvlckey = KEY_MOUSEWHEELRIGHT;
300 i_xvlckey = KEY_MOUSEWHEELLEFT;
303 /* in the following, we're forwarding either a x or a y event */
304 /* Multiple key events are send depending on the intensity of the event */
305 /* the opposite direction is being blocked for 0.8 secs */
306 if (f_yabsvalue > 0.05)
308 if (i_lastScrollWheelDirection < 0) // last was a X
311 i_lastScrollWheelDirection = 1; // Y
312 for (NSUInteger i = 0; i < (int)(f_yabsvalue/4.+1.) && f_yabsvalue > 0.05 ; i++)
313 var_SetInteger(p_intf->p_libvlc, "key-pressed", i_yvlckey);
315 t_lastScrollEvent = [NSDate timeIntervalSinceReferenceDate];
316 [self performSelector:@selector(resetScrollWheelDirection)
321 if (f_xabsvalue > 0.05)
323 if (i_lastScrollWheelDirection > 0) // last was a Y
326 i_lastScrollWheelDirection = -1; // X
327 for (NSUInteger i = 0; i < (int)(f_xabsvalue/6.+1.) && f_xabsvalue > 0.05; i++)
328 var_SetInteger(p_intf->p_libvlc, "key-pressed", i_xvlckey);
330 t_lastScrollEvent = [NSDate timeIntervalSinceReferenceDate];
331 [self performSelector:@selector(resetScrollWheelDirection)
337 - (BOOL)keyEvent:(NSEvent *)o_event
339 BOOL eventHandled = NO;
340 NSString * characters = [o_event charactersIgnoringModifiers];
341 if ([characters length] > 0) {
342 unichar key = [characters characterAtIndex: 0];
345 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
346 if (p_input != NULL) {
347 vout_thread_t *p_vout = input_GetVout(p_input);
349 if (p_vout != NULL) {
351 if (key == (unichar) 0x1b) {
352 if (var_GetBool(p_vout, "fullscreen")) {
353 [[VLCCoreInteraction sharedInstance] toggleFullscreen];
357 else if (key == ' ') {
361 vlc_object_release(p_vout);
363 vlc_object_release(p_input);
370 - (IBAction)goToSpecificTime:(id)sender
372 if (sender == o_specificTime_cancel_btn)
374 [NSApp endSheet: o_specificTime_win];
375 [o_specificTime_win close];
376 } else if (sender == o_specificTime_ok_btn) {
377 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
379 int64_t timeInSec = 0;
380 NSString * fieldContent = [o_specificTime_enter_fld stringValue];
381 if ([[fieldContent componentsSeparatedByString: @":"] count] > 1 &&
382 [[fieldContent componentsSeparatedByString: @":"] count] <= 3) {
383 NSArray * ourTempArray = \
384 [fieldContent componentsSeparatedByString: @":"];
386 if ([[fieldContent componentsSeparatedByString: @":"] count] == 3) {
387 timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 3600); //h
388 timeInSec += ([[ourTempArray objectAtIndex: 1] intValue] * 60); //m
389 timeInSec += [[ourTempArray objectAtIndex: 2] intValue]; //s
391 timeInSec += ([[ourTempArray objectAtIndex: 0] intValue] * 60); //m
392 timeInSec += [[ourTempArray objectAtIndex: 1] intValue]; //s
396 timeInSec = [fieldContent intValue];
398 input_Control(p_input, INPUT_SET_TIME, (int64_t)(timeInSec * 1000000));
399 vlc_object_release(p_input);
402 [NSApp endSheet: o_specificTime_win];
403 [o_specificTime_win close];
405 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
407 /* we can obviously only do that if an input is available */
408 vlc_value_t pos, length;
409 var_Get(p_input, "time", &pos);
410 [o_specificTime_enter_fld setIntValue: (pos.i_time / 1000000)];
411 var_Get(p_input, "length", &length);
412 [o_specificTime_stepper setMaxValue: (length.i_time / 1000000)];
414 [NSApp beginSheet: o_specificTime_win modalForWindow: \
415 [NSApp mainWindow] modalDelegate: self didEndSelector: nil \
417 [o_specificTime_win makeKeyWindow];
418 vlc_object_release(p_input);
425 @implementation VLCControls (NSMenuValidation)
427 - (BOOL)validateMenuItem:(NSMenuItem *)o_mi
429 return [[VLCMainMenu sharedInstance] validateMenuItem:o_mi];