1 /*****************************************************************************
2 * CoreInteraction.m: MacOS X interface module
3 *****************************************************************************
4 * Copyright (C) 2011-2012 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"
27 #import <vlc_playlist.h>
31 #import <vlc_aout_intf.h>
33 #import <vlc_strings.h>
35 @implementation VLCCoreInteraction
36 static VLCCoreInteraction *_o_sharedInstance = nil;
38 + (VLCCoreInteraction *)sharedInstance
40 return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
44 #pragma mark Initialization
48 if( _o_sharedInstance)
51 return _o_sharedInstance;
55 _o_sharedInstance = [super init];
56 b_lockAspectRatio = YES;
59 return _o_sharedInstance;
64 [[NSNotificationCenter defaultCenter] removeObserver: self];
70 [[NSNotificationCenter defaultCenter] addObserver: self
71 selector: @selector(applicationWillFinishLaunching:)
72 name: NSApplicationWillFinishLaunchingNotification
77 #pragma mark Playback Controls
81 playlist_t * p_playlist = pl_Get( VLCIntf );
85 empty = playlist_IsEmpty( p_playlist );
89 [[[VLCMain sharedInstance] open] openFileGeneric];
91 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PLAY_PAUSE );
96 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PAUSE );
101 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_STOP );
106 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_FASTER );
111 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_SLOWER );
116 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_RATE_NORMAL );
121 intf_thread_t *p_intf = VLCIntf;
123 input_thread_t * p_input;
124 p_input = pl_CurrentInput( p_intf );
127 var_ToggleBool( p_input, "record" );
128 vlc_object_release( p_input );
132 - (void)setPlaybackRate:(int)i_value
134 playlist_t * p_playlist = pl_Get( VLCIntf );
136 double speed = pow( 2, (double)i_value / 17 );
137 int rate = INPUT_RATE_DEFAULT / speed;
138 if( i_currentPlaybackRate != rate )
139 var_SetFloat( p_playlist, "rate", (float)INPUT_RATE_DEFAULT / (float)rate );
140 i_currentPlaybackRate = rate;
147 intf_thread_t *p_intf = VLCIntf;
149 input_thread_t * p_input;
150 p_input = pl_CurrentInput( p_intf );
153 f_rate = var_GetFloat( p_input, "rate" );
154 vlc_object_release( p_input );
158 playlist_t * p_playlist = pl_Get( VLCIntf );
159 f_rate = var_GetFloat( p_playlist, "rate" );
162 double value = 17 * log( f_rate ) / log( 2. );
163 int returnValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
165 if( returnValue < -34 )
167 else if( returnValue > 34 )
170 i_currentPlaybackRate = returnValue;
176 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PREV );
181 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_NEXT );
186 intf_thread_t *p_intf = VLCIntf;
188 input_thread_t * p_input = pl_CurrentInput( p_intf );
190 if (!p_input) return NO;
192 input_state_e i_state = ERROR_S;
193 input_Control( p_input, INPUT_GET_STATE, &i_state);
194 vlc_object_release( p_input );
196 return ((i_state == OPENING_S) || (i_state == PLAYING_S));
201 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
202 int64_t i_currentTime = -1;
204 if (!p_input) return i_currentTime;
206 input_Control( p_input, INPUT_GET_TIME, &i_currentTime);
207 vlc_object_release( p_input );
209 return (int)( i_currentTime / 1000000 );
212 - (void)setCurrentTime:(int)i_value
214 int64_t i64_value = (int64_t)i_value;
215 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
217 if (!p_input) return;
219 input_Control( p_input, INPUT_SET_TIME, (int64_t)(i64_value * 1000000));
220 vlc_object_release( p_input );
223 - (int)durationOfCurrentPlaylistItem
225 intf_thread_t *p_intf = VLCIntf;
227 input_thread_t * p_input = pl_CurrentInput( p_intf );
228 int64_t i_duration = -1;
229 if (!p_input) return i_duration;
232 input_Control( p_input, INPUT_GET_LENGTH, &i_duration);
233 vlc_object_release( p_input );
235 return (int)(i_duration / 1000000);
238 - (NSURL*)URLOfCurrentPlaylistItem
240 intf_thread_t *p_intf = VLCIntf;
242 input_thread_t *p_input = pl_CurrentInput( p_intf );
243 if (!p_input) return nil;
245 input_item_t *p_item = input_GetItem( p_input );
248 vlc_object_release( p_input );
252 char *psz_uri = input_item_GetURI( p_item );
255 vlc_object_release( p_input );
260 o_url = [NSURL URLWithString:[NSString stringWithUTF8String:psz_uri]];
261 vlc_object_release( p_input );
266 - (NSString*)nameOfCurrentPlaylistItem
268 intf_thread_t *p_intf = VLCIntf;
270 input_thread_t *p_input = pl_CurrentInput( p_intf );
271 if (!p_input) return nil;
273 input_item_t *p_item = input_GetItem( p_input );
276 vlc_object_release( p_input );
280 char *psz_uri = input_item_GetURI( p_item );
283 vlc_object_release( p_input );
288 char *format = var_InheritString( VLCIntf, "input-title-format" );
289 char *formated = str_format_meta( p_input, format );
291 o_name = [NSString stringWithUTF8String:formated];
294 NSURL * o_url = [NSURL URLWithString: [NSString stringWithUTF8String: psz_uri]];
297 if ([o_name isEqualToString:@""])
299 if ([o_url isFileURL])
300 o_name = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
302 o_name = [o_url absoluteString];
304 vlc_object_release( p_input );
317 [self backwardShort];
320 - (void)forwardExtraShort
322 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
325 - (void)backwardExtraShort
327 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
332 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_SHORT );
335 - (void)backwardShort
337 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_SHORT );
340 - (void)forwardMedium
342 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_MEDIUM );
345 - (void)backwardMedium
347 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_MEDIUM );
352 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_LONG );
357 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_LONG );
362 intf_thread_t *p_intf = VLCIntf;
365 playlist_t * p_playlist = pl_Get( p_intf );
366 vout_thread_t *p_vout = getVout();
368 var_Get( p_playlist, "random", &val );
369 val.b_bool = !val.b_bool;
370 var_Set( p_playlist, "random", val );
375 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Random On" ) );
376 vlc_object_release( p_vout );
378 config_PutInt( p_playlist, "random", 1 );
384 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Random Off" ) );
385 vlc_object_release( p_vout );
387 config_PutInt( p_playlist, "random", 0 );
393 intf_thread_t *p_intf = VLCIntf;
395 playlist_t * p_playlist = pl_Get( p_intf );
397 var_SetBool( p_playlist, "repeat", NO );
398 var_SetBool( p_playlist, "loop", YES );
399 config_PutInt( p_playlist, "repeat", NO );
400 config_PutInt( p_playlist, "loop", YES );
402 vout_thread_t *p_vout = getVout();
405 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat All" ) );
406 vlc_object_release( p_vout );
412 intf_thread_t *p_intf = VLCIntf;
414 playlist_t * p_playlist = pl_Get( p_intf );
416 var_SetBool( p_playlist, "repeat", YES );
417 var_SetBool( p_playlist, "loop", NO );
418 config_PutInt( p_playlist, "repeat", YES );
419 config_PutInt( p_playlist, "loop", NO );
421 vout_thread_t *p_vout = getVout();
424 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat One" ) );
425 vlc_object_release( p_vout );
431 intf_thread_t *p_intf = VLCIntf;
433 playlist_t * p_playlist = pl_Get( p_intf );
435 var_SetBool( p_playlist, "repeat", NO );
436 var_SetBool( p_playlist, "loop", NO );
437 config_PutInt( p_playlist, "repeat", NO );
438 config_PutInt( p_playlist, "loop", NO );
440 vout_thread_t *p_vout = getVout();
443 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat Off" ) );
444 vlc_object_release( p_vout );
450 intf_thread_t *p_intf = VLCIntf;
452 aout_VolumeUp( pl_Get( p_intf ), 1, NULL );
457 intf_thread_t *p_intf = VLCIntf;
459 aout_VolumeDown( pl_Get( p_intf ), 1, NULL );
464 intf_thread_t *p_intf = VLCIntf;
466 aout_ToggleMute( pl_Get( p_intf ), NULL );
471 intf_thread_t *p_intf = VLCIntf;
473 BOOL b_is_muted = NO;
474 b_is_muted = aout_IsMuted( VLC_OBJECT(pl_Get( p_intf )) );
481 intf_thread_t *p_intf = VLCIntf;
483 audio_volume_t i_volume = aout_VolumeGet( pl_Get( p_intf ) );
485 return (int)i_volume;
488 - (void)setVolume: (int)i_value
490 intf_thread_t *p_intf = VLCIntf;
492 aout_VolumeSet( pl_Get( p_intf ), i_value );
496 #pragma mark video output stuff
498 - (void)setAspectRatioLocked:(BOOL)b_value
500 b_lockAspectRatio = b_value;
503 - (BOOL)aspectRatioIsLocked
505 return b_lockAspectRatio;
508 - (void)toggleFullscreen
510 intf_thread_t *p_intf = VLCIntf;
512 var_ToggleBool( pl_Get( p_intf ), "fullscreen" );