1 /*****************************************************************************
2 * CoreInteraction.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2011-2013 Felix Paul Kühne
7 * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #import "CoreInteraction.h"
29 #import <vlc_playlist.h>
33 #import <vlc_vout_osd.h>
35 #import <vlc_strings.h>
38 @interface VLCMainWindow (Internal)
39 - (void)jumpWithValue:(char *)p_value forward:(BOOL)b_value;
42 @implementation VLCCoreInteraction
43 static VLCCoreInteraction *_o_sharedInstance = nil;
45 + (VLCCoreInteraction *)sharedInstance
47 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
51 #pragma mark Initialization
55 if (_o_sharedInstance) {
57 return _o_sharedInstance;
59 _o_sharedInstance = [super init];
61 return _o_sharedInstance;
66 [[NSNotificationCenter defaultCenter] removeObserver: self];
72 [[NSNotificationCenter defaultCenter] addObserver: self
73 selector: @selector(applicationWillFinishLaunching:)
74 name: NSApplicationWillFinishLaunchingNotification
79 #pragma mark Playback Controls
83 input_thread_t * p_input;
84 p_input = pl_CurrentInput(VLCIntf);
85 playlist_t * p_playlist = pl_Get(VLCIntf);
88 playlist_Pause(p_playlist);
89 vlc_object_release(p_input);
94 empty = playlist_IsEmpty(p_playlist);
97 if ([[[VLCMain sharedInstance] playlist] isSelectionEmpty] && ([[[VLCMain sharedInstance] playlist] currentPlaylistRoot] == p_playlist->p_local_category || [[[VLCMain sharedInstance] playlist] currentPlaylistRoot] == p_playlist->p_ml_category))
98 [[[VLCMain sharedInstance] open] openFileGeneric];
100 [[[VLCMain sharedInstance] playlist] playItem:nil];
106 playlist_Pause(pl_Get(VLCIntf));
111 playlist_Stop(pl_Get(VLCIntf));
116 var_TriggerCallback(pl_Get(VLCIntf), "rate-faster");
121 var_TriggerCallback(pl_Get(VLCIntf), "rate-slower");
126 var_SetFloat(pl_Get(VLCIntf), "rate", 1.);
131 intf_thread_t *p_intf = VLCIntf;
135 input_thread_t * p_input;
136 p_input = pl_CurrentInput(p_intf);
138 var_ToggleBool(p_input, "record");
139 vlc_object_release(p_input);
143 - (void)setPlaybackRate:(int)i_value
145 playlist_t * p_playlist = pl_Get(VLCIntf);
147 double speed = pow(2, (double)i_value / 17);
148 int rate = INPUT_RATE_DEFAULT / speed;
149 if (i_currentPlaybackRate != rate)
150 var_SetFloat(p_playlist, "rate", (float)INPUT_RATE_DEFAULT / (float)rate);
151 i_currentPlaybackRate = rate;
158 intf_thread_t *p_intf = VLCIntf;
162 input_thread_t * p_input;
163 p_input = pl_CurrentInput(p_intf);
165 f_rate = var_GetFloat(p_input, "rate");
166 vlc_object_release(p_input);
170 playlist_t * p_playlist = pl_Get(VLCIntf);
171 f_rate = var_GetFloat(p_playlist, "rate");
174 double value = 17 * log(f_rate) / log(2.);
175 int returnValue = (int) ((value > 0) ? value + .5 : value - .5);
177 if (returnValue < -34)
179 else if (returnValue > 34)
182 i_currentPlaybackRate = returnValue;
188 playlist_Prev(pl_Get(VLCIntf));
193 playlist_Next(pl_Get(VLCIntf));
196 - (int)durationOfCurrentPlaylistItem
198 intf_thread_t *p_intf = VLCIntf;
202 input_thread_t * p_input = pl_CurrentInput(p_intf);
203 int64_t i_duration = -1;
207 input_Control(p_input, INPUT_GET_LENGTH, &i_duration);
208 vlc_object_release(p_input);
210 return (int)(i_duration / 1000000);
213 - (NSURL*)URLOfCurrentPlaylistItem
215 intf_thread_t *p_intf = VLCIntf;
219 input_thread_t *p_input = pl_CurrentInput(p_intf);
223 input_item_t *p_item = input_GetItem(p_input);
225 vlc_object_release(p_input);
229 char *psz_uri = input_item_GetURI(p_item);
231 vlc_object_release(p_input);
236 o_url = [NSURL URLWithString:[NSString stringWithUTF8String:psz_uri]];
237 vlc_object_release(p_input);
242 - (NSString*)nameOfCurrentPlaylistItem
244 intf_thread_t *p_intf = VLCIntf;
248 input_thread_t *p_input = pl_CurrentInput(p_intf);
252 input_item_t *p_item = input_GetItem(p_input);
254 vlc_object_release(p_input);
258 char *psz_uri = input_item_GetURI(p_item);
260 vlc_object_release(p_input);
265 char *format = var_InheritString(VLCIntf, "input-title-format");
266 char *formated = str_format_meta(pl_Get(VLCIntf), format);
268 o_name = [NSString stringWithUTF8String:formated];
271 NSURL * o_url = [NSURL URLWithString: [NSString stringWithUTF8String: psz_uri]];
274 if ([o_name isEqualToString:@""]) {
275 if ([o_url isFileURL])
276 o_name = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
278 o_name = [o_url absoluteString];
280 vlc_object_release(p_input);
293 [self backwardShort];
296 - (void)jumpWithValue:(char *)p_value forward:(BOOL)b_value
298 input_thread_t *p_input = pl_CurrentInput(VLCIntf);
302 int i_interval = var_InheritInteger( p_input, p_value );
303 if (i_interval > 0) {
304 mtime_t val = CLOCK_FREQ * i_interval;
307 var_SetTime( p_input, "time-offset", val );
309 vlc_object_release(p_input);
312 - (void)forwardExtraShort
314 [self jumpWithValue:"extrashort-jump-size" forward:YES];
317 - (void)backwardExtraShort
319 [self jumpWithValue:"extrashort-jump-size" forward:NO];
324 [self jumpWithValue:"short-jump-size" forward:YES];
327 - (void)backwardShort
329 [self jumpWithValue:"short-jump-size" forward:NO];
332 - (void)forwardMedium
334 [self jumpWithValue:"medium-jump-size" forward:YES];
337 - (void)backwardMedium
339 [self jumpWithValue:"medium-jump-size" forward:NO];
344 [self jumpWithValue:"long-jump-size" forward:YES];
349 [self jumpWithValue:"long-jump-size" forward:NO];
354 intf_thread_t *p_intf = VLCIntf;
359 playlist_t * p_playlist = pl_Get(p_intf);
360 vout_thread_t *p_vout = getVout();
362 var_Get(p_playlist, "random", &val);
363 val.b_bool = !val.b_bool;
364 var_Set(p_playlist, "random", val);
367 vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Random On"));
368 vlc_object_release(p_vout);
370 config_PutInt(p_playlist, "random", 1);
375 vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Random Off"));
376 vlc_object_release(p_vout);
378 config_PutInt(p_playlist, "random", 0);
384 intf_thread_t *p_intf = VLCIntf;
388 playlist_t * p_playlist = pl_Get(p_intf);
390 var_SetBool(p_playlist, "repeat", NO);
391 var_SetBool(p_playlist, "loop", YES);
392 config_PutInt(p_playlist, "repeat", NO);
393 config_PutInt(p_playlist, "loop", YES);
395 vout_thread_t *p_vout = getVout();
397 vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Repeat All"));
398 vlc_object_release(p_vout);
404 intf_thread_t *p_intf = VLCIntf;
408 playlist_t * p_playlist = pl_Get(p_intf);
410 var_SetBool(p_playlist, "repeat", YES);
411 var_SetBool(p_playlist, "loop", NO);
412 config_PutInt(p_playlist, "repeat", YES);
413 config_PutInt(p_playlist, "loop", NO);
415 vout_thread_t *p_vout = getVout();
417 vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Repeat One"));
418 vlc_object_release(p_vout);
424 intf_thread_t *p_intf = VLCIntf;
428 playlist_t * p_playlist = pl_Get(p_intf);
430 var_SetBool(p_playlist, "repeat", NO);
431 var_SetBool(p_playlist, "loop", NO);
432 config_PutInt(p_playlist, "repeat", NO);
433 config_PutInt(p_playlist, "loop", NO);
435 vout_thread_t *p_vout = getVout();
437 vout_OSDMessage(p_vout, SPU_DEFAULT_CHANNEL, "%s", _("Repeat Off"));
438 vlc_object_release(p_vout);
445 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
447 timeA = var_GetTime(p_input, "time");
448 vlc_object_release(p_input);
451 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
453 timeB = var_GetTime(p_input, "time");
454 vlc_object_release(p_input);
469 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
471 mtime_t currentTime = var_GetTime(p_input, "time");
472 if ( currentTime >= timeB || currentTime < timeA)
473 var_SetTime(p_input, "time", timeA);
474 vlc_object_release(p_input);
481 intf_thread_t *p_intf = VLCIntf;
485 playlist_VolumeUp(pl_Get(p_intf), 1, NULL);
490 intf_thread_t *p_intf = VLCIntf;
494 playlist_VolumeDown(pl_Get(p_intf), 1, NULL);
499 intf_thread_t *p_intf = VLCIntf;
503 playlist_MuteToggle(pl_Get(p_intf));
506 - (void)setMute:(BOOL)b_value
508 intf_thread_t *p_intf = VLCIntf;
512 playlist_MuteSet(pl_Get(p_intf), b_value);
517 intf_thread_t *p_intf = VLCIntf;
521 BOOL b_is_muted = NO;
522 b_is_muted = playlist_MuteGet(pl_Get(p_intf)) > 0;
529 intf_thread_t *p_intf = VLCIntf;
533 float volume = playlist_VolumeGet(pl_Get(p_intf));
535 return lroundf(volume * AOUT_VOLUME_DEFAULT);
538 - (void)setVolume: (int)i_value
540 intf_thread_t *p_intf = VLCIntf;
544 float f_value = i_value / (float)AOUT_VOLUME_DEFAULT;
546 playlist_VolumeSet(pl_Get(p_intf), f_value);
550 #pragma mark drag and drop support for VLCVoutView, VLBrushedMetalImageView and VLCThreePartDropView
551 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
553 NSPasteboard *o_paste = [sender draggingPasteboard];
554 NSArray *o_types = @[NSFilenamesPboardType];
555 NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
556 NSData *o_carried_data = [o_paste dataForType:o_desired_type];
557 BOOL b_autoplay = config_GetInt(VLCIntf, "macosx-autoplay");
559 if (o_carried_data) {
560 if ([o_desired_type isEqualToString:NSFilenamesPboardType]) {
561 NSArray *o_array = [NSArray array];
562 NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
563 NSUInteger count = [o_values count];
565 input_thread_t * p_input = pl_CurrentInput(VLCIntf);
566 BOOL b_returned = NO;
568 if (count == 1 && p_input) {
569 b_returned = input_AddSubtitle(p_input, [[o_values objectAtIndex:0] UTF8String], true);
570 vlc_object_release(p_input);
575 vlc_object_release(p_input);
577 for (NSUInteger i = 0; i < count; i++) {
579 char *psz_uri = vlc_path2uri([[o_values objectAtIndex:i] UTF8String], NULL);
583 o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
586 o_array = [o_array arrayByAddingObject: o_dic];
589 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
591 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:YES];
600 #pragma mark video output stuff
602 - (void)setAspectRatioIsLocked:(BOOL)b_value
604 config_PutInt(VLCIntf, "macosx-lock-aspect-ratio", b_value);
607 - (BOOL)aspectRatioIsLocked
609 return config_GetInt(VLCIntf, "macosx-lock-aspect-ratio");
612 - (void)toggleFullscreen
614 intf_thread_t *p_intf = VLCIntf;
618 vout_thread_t *p_vout = getVoutForActiveWindow();
620 BOOL b_fs = var_ToggleBool(p_vout, "fullscreen");
621 var_SetBool(pl_Get(p_intf), "fullscreen", b_fs);
622 vlc_object_release(p_vout);
623 } else { // e.g. lion fullscreen toggle
624 BOOL b_fs = var_ToggleBool(pl_Get(p_intf), "fullscreen");
625 [[[VLCMain sharedInstance] voutController] setFullscreen:b_fs forWindow:nil];
630 #pragma mark uncommon stuff
632 - (BOOL)fixPreferences
634 NSMutableString * o_workString;
635 NSRange returnedRange;
637 BOOL b_needsRestart = NO;
639 #define fixpref(pref) \
640 o_workString = [[NSMutableString alloc] initWithFormat:@"%s", config_GetPsz(VLCIntf, pref)]; \
641 if ([o_workString length] > 0) \
643 returnedRange = [o_workString rangeOfString:@"macosx" options: NSCaseInsensitiveSearch]; \
644 if (returnedRange.location != NSNotFound) \
646 if ([o_workString isEqualToString:@"macosx"]) \
647 [o_workString setString:@""]; \
648 fullRange = NSMakeRange(0, [o_workString length]); \
649 [o_workString replaceOccurrencesOfString:@":macosx" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
650 fullRange = NSMakeRange(0, [o_workString length]); \
651 [o_workString replaceOccurrencesOfString:@"macosx:" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
653 config_PutPsz(VLCIntf, pref, [o_workString UTF8String]); \
654 b_needsRestart = YES; \
657 [o_workString release]
660 fixpref("extraintf");
663 return b_needsRestart;