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;
125 input_thread_t * p_input;
126 p_input = pl_CurrentInput( p_intf );
129 var_ToggleBool( p_input, "record" );
130 vlc_object_release( p_input );
134 - (void)setPlaybackRate:(int)i_value
136 playlist_t * p_playlist = pl_Get( VLCIntf );
138 double speed = pow( 2, (double)i_value / 17 );
139 int rate = INPUT_RATE_DEFAULT / speed;
140 if( i_currentPlaybackRate != rate )
141 var_SetFloat( p_playlist, "rate", (float)INPUT_RATE_DEFAULT / (float)rate );
142 i_currentPlaybackRate = rate;
149 intf_thread_t *p_intf = VLCIntf;
153 input_thread_t * p_input;
154 p_input = pl_CurrentInput( p_intf );
157 f_rate = var_GetFloat( p_input, "rate" );
158 vlc_object_release( p_input );
162 playlist_t * p_playlist = pl_Get( VLCIntf );
163 f_rate = var_GetFloat( p_playlist, "rate" );
166 double value = 17 * log( f_rate ) / log( 2. );
167 int returnValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
169 if( returnValue < -34 )
171 else if( returnValue > 34 )
174 i_currentPlaybackRate = returnValue;
180 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PREV );
185 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_NEXT );
190 intf_thread_t *p_intf = VLCIntf;
194 input_thread_t * p_input = pl_CurrentInput( p_intf );
196 if (!p_input) return NO;
198 input_state_e i_state = ERROR_S;
199 input_Control( p_input, INPUT_GET_STATE, &i_state);
200 vlc_object_release( p_input );
202 return ((i_state == OPENING_S) || (i_state == PLAYING_S));
207 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
208 int64_t i_currentTime = -1;
210 if (!p_input) return i_currentTime;
212 input_Control( p_input, INPUT_GET_TIME, &i_currentTime);
213 vlc_object_release( p_input );
215 return (int)( i_currentTime / 1000000 );
218 - (void)setCurrentTime:(int)i_value
220 int64_t i64_value = (int64_t)i_value;
221 input_thread_t * p_input = pl_CurrentInput( VLCIntf );
223 if (!p_input) return;
225 input_Control( p_input, INPUT_SET_TIME, (int64_t)(i64_value * 1000000));
226 vlc_object_release( p_input );
229 - (int)durationOfCurrentPlaylistItem
231 intf_thread_t *p_intf = VLCIntf;
235 input_thread_t * p_input = pl_CurrentInput( p_intf );
236 int64_t i_duration = -1;
237 if (!p_input) return i_duration;
240 input_Control( p_input, INPUT_GET_LENGTH, &i_duration);
241 vlc_object_release( p_input );
243 return (int)(i_duration / 1000000);
246 - (NSURL*)URLOfCurrentPlaylistItem
248 intf_thread_t *p_intf = VLCIntf;
252 input_thread_t *p_input = pl_CurrentInput( p_intf );
253 if (!p_input) return nil;
255 input_item_t *p_item = input_GetItem( p_input );
258 vlc_object_release( p_input );
262 char *psz_uri = input_item_GetURI( p_item );
265 vlc_object_release( p_input );
270 o_url = [NSURL URLWithString:[NSString stringWithUTF8String:psz_uri]];
271 vlc_object_release( p_input );
276 - (NSString*)nameOfCurrentPlaylistItem
278 intf_thread_t *p_intf = VLCIntf;
282 input_thread_t *p_input = pl_CurrentInput( p_intf );
283 if (!p_input) return nil;
285 input_item_t *p_item = input_GetItem( p_input );
288 vlc_object_release( p_input );
292 char *psz_uri = input_item_GetURI( p_item );
295 vlc_object_release( p_input );
300 char *format = var_InheritString( VLCIntf, "input-title-format" );
301 char *formated = str_format_meta( p_input, format );
303 o_name = [NSString stringWithUTF8String:formated];
306 NSURL * o_url = [NSURL URLWithString: [NSString stringWithUTF8String: psz_uri]];
309 if ([o_name isEqualToString:@""])
311 if ([o_url isFileURL])
312 o_name = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
314 o_name = [o_url absoluteString];
316 vlc_object_release( p_input );
329 [self backwardShort];
332 - (void)forwardExtraShort
334 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
337 - (void)backwardExtraShort
339 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
344 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_SHORT );
347 - (void)backwardShort
349 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_SHORT );
352 - (void)forwardMedium
354 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_MEDIUM );
357 - (void)backwardMedium
359 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_MEDIUM );
364 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_LONG );
369 var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_LONG );
374 intf_thread_t *p_intf = VLCIntf;
379 playlist_t * p_playlist = pl_Get( p_intf );
380 vout_thread_t *p_vout = getVout();
382 var_Get( p_playlist, "random", &val );
383 val.b_bool = !val.b_bool;
384 var_Set( p_playlist, "random", val );
389 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Random On" ) );
390 vlc_object_release( p_vout );
392 config_PutInt( p_playlist, "random", 1 );
398 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Random Off" ) );
399 vlc_object_release( p_vout );
401 config_PutInt( p_playlist, "random", 0 );
407 intf_thread_t *p_intf = VLCIntf;
411 playlist_t * p_playlist = pl_Get( p_intf );
413 var_SetBool( p_playlist, "repeat", NO );
414 var_SetBool( p_playlist, "loop", YES );
415 config_PutInt( p_playlist, "repeat", NO );
416 config_PutInt( p_playlist, "loop", YES );
418 vout_thread_t *p_vout = getVout();
421 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat All" ) );
422 vlc_object_release( p_vout );
428 intf_thread_t *p_intf = VLCIntf;
432 playlist_t * p_playlist = pl_Get( p_intf );
434 var_SetBool( p_playlist, "repeat", YES );
435 var_SetBool( p_playlist, "loop", NO );
436 config_PutInt( p_playlist, "repeat", YES );
437 config_PutInt( p_playlist, "loop", NO );
439 vout_thread_t *p_vout = getVout();
442 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat One" ) );
443 vlc_object_release( p_vout );
449 intf_thread_t *p_intf = VLCIntf;
453 playlist_t * p_playlist = pl_Get( p_intf );
455 var_SetBool( p_playlist, "repeat", NO );
456 var_SetBool( p_playlist, "loop", NO );
457 config_PutInt( p_playlist, "repeat", NO );
458 config_PutInt( p_playlist, "loop", NO );
460 vout_thread_t *p_vout = getVout();
463 vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat Off" ) );
464 vlc_object_release( p_vout );
470 intf_thread_t *p_intf = VLCIntf;
474 aout_VolumeUp( pl_Get( p_intf ), 1, NULL );
479 intf_thread_t *p_intf = VLCIntf;
483 aout_VolumeDown( pl_Get( p_intf ), 1, NULL );
488 intf_thread_t *p_intf = VLCIntf;
492 aout_ToggleMute( pl_Get( p_intf ), NULL );
497 intf_thread_t *p_intf = VLCIntf;
501 BOOL b_is_muted = NO;
502 b_is_muted = aout_IsMuted( VLC_OBJECT(pl_Get( p_intf )) );
509 intf_thread_t *p_intf = VLCIntf;
513 audio_volume_t i_volume = aout_VolumeGet( pl_Get( p_intf ) );
515 return (int)i_volume;
518 - (void)setVolume: (int)i_value
520 intf_thread_t *p_intf = VLCIntf;
524 aout_VolumeSet( pl_Get( p_intf ), i_value );
528 #pragma mark video output stuff
530 - (void)setAspectRatioLocked:(BOOL)b_value
532 b_lockAspectRatio = b_value;
535 - (BOOL)aspectRatioIsLocked
537 return b_lockAspectRatio;
540 - (void)toggleFullscreen
542 intf_thread_t *p_intf = VLCIntf;
546 var_ToggleBool( pl_Get( p_intf ), "fullscreen" );