]> git.sesse.net Git - vlc/blob - modules/gui/macosx/CoreInteraction.m
macosx: fixed white spacing errors
[vlc] / modules / gui / macosx / CoreInteraction.m
1 /*****************************************************************************
2  * CoreInteraction.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2011-2012 Felix Paul Kühne
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #import "CoreInteraction.h"
25 #import "intf.h"
26 #import "open.h"
27 #import "playlist.h"
28 #import <vlc_playlist.h>
29 #import <vlc_input.h>
30 #import <vlc_keys.h>
31 #import <vlc_osd.h>
32 #import <vlc_aout_intf.h>
33 #import <vlc/vlc.h>
34 #import <vlc_strings.h>
35 #import <vlc_url.h>
36
37 @implementation VLCCoreInteraction
38 static VLCCoreInteraction *_o_sharedInstance = nil;
39
40 + (VLCCoreInteraction *)sharedInstance
41 {
42     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
43 }
44
45 #pragma mark -
46 #pragma mark Initialization
47
48 - (id)init
49 {
50     if( _o_sharedInstance )
51     {
52         [self dealloc];
53         return _o_sharedInstance;
54     }
55     else
56     {
57         _o_sharedInstance = [super init];
58     }
59
60     return _o_sharedInstance;
61 }
62
63 - (void)dealloc
64 {
65     [[NSNotificationCenter defaultCenter] removeObserver: self];
66     [super dealloc];
67 }
68
69 - (void)awakeFromNib
70 {
71     [[NSNotificationCenter defaultCenter] addObserver: self
72                                              selector: @selector(applicationWillFinishLaunching:)
73                                                  name: NSApplicationWillFinishLaunchingNotification
74                                                object: nil];
75 }
76
77 #pragma mark -
78 #pragma mark Playback Controls
79
80 - (void)play
81 {
82     playlist_t * p_playlist = pl_Get( VLCIntf );
83     bool empty;
84
85     PL_LOCK;
86     empty = playlist_IsEmpty( p_playlist );
87     PL_UNLOCK;
88
89     if( empty )
90         [[[VLCMain sharedInstance] open] openFileGeneric];
91
92     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PLAY_PAUSE );
93 }
94
95 - (void)pause
96 {
97     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PAUSE );
98 }
99
100 - (void)stop
101 {
102     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_STOP );
103 }
104
105 - (void)faster
106 {
107     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_FASTER );
108 }
109
110 - (void)slower
111 {
112     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_SLOWER );
113 }
114
115 - (void)normalSpeed
116 {
117     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_RATE_NORMAL );
118 }
119
120 - (void)toggleRecord
121 {
122     intf_thread_t *p_intf = VLCIntf;
123     if( !p_intf )
124         return;
125
126     input_thread_t * p_input;
127     p_input = pl_CurrentInput( p_intf );
128     if( p_input )
129     {
130         var_ToggleBool( p_input, "record" );
131         vlc_object_release( p_input );
132     }
133 }
134
135 - (void)setPlaybackRate:(int)i_value
136 {
137     playlist_t * p_playlist = pl_Get( VLCIntf );
138
139     double speed = pow( 2, (double)i_value / 17 );
140     int rate = INPUT_RATE_DEFAULT / speed;
141     if( i_currentPlaybackRate != rate )
142         var_SetFloat( p_playlist, "rate", (float)INPUT_RATE_DEFAULT / (float)rate );
143     i_currentPlaybackRate = rate;
144 }
145
146 - (int)playbackRate
147 {
148     float f_rate;
149
150     intf_thread_t *p_intf = VLCIntf;
151     if( !p_intf )
152         return 0;
153
154     input_thread_t * p_input;
155     p_input = pl_CurrentInput( p_intf );
156     if( p_input )
157     {
158         f_rate = var_GetFloat( p_input, "rate" );
159         vlc_object_release( p_input );
160     }
161     else
162     {
163         playlist_t * p_playlist = pl_Get( VLCIntf );
164         f_rate = var_GetFloat( p_playlist, "rate" );
165     }
166
167     double value = 17 * log( f_rate ) / log( 2. );
168     int returnValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
169
170     if( returnValue < -34 )
171         returnValue = -34;
172     else if( returnValue > 34 )
173         returnValue = 34;
174
175     i_currentPlaybackRate = returnValue;
176     return returnValue;
177 }
178
179 - (void)previous
180 {
181     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PREV );
182 }
183
184 - (void)next
185 {
186     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_NEXT );
187 }
188
189 - (BOOL)isPlaying
190 {
191     intf_thread_t *p_intf = VLCIntf;
192     if( !p_intf )
193         return NO;
194
195     input_thread_t * p_input = pl_CurrentInput( p_intf );
196     if( !p_input )
197         return NO;
198
199     input_state_e i_state = ERROR_S;
200     input_Control( p_input, INPUT_GET_STATE, &i_state );
201     vlc_object_release( p_input );
202
203     return ( ( i_state == OPENING_S ) || ( i_state == PLAYING_S ) );
204 }
205
206 - (int)currentTime
207 {
208     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
209     int64_t i_currentTime = -1;
210
211     if( !p_input )
212         return i_currentTime;
213
214     input_Control( p_input, INPUT_GET_TIME, &i_currentTime );
215     vlc_object_release( p_input );
216
217     return (int)( i_currentTime / 1000000 );
218 }
219
220 - (void)setCurrentTime:(int)i_value
221 {
222     int64_t i64_value = (int64_t)i_value;
223     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
224
225     if ( !p_input )
226         return;
227
228     input_Control( p_input, INPUT_SET_TIME, (int64_t)(i64_value * 1000000) );
229     vlc_object_release( p_input );
230 }
231
232 - (int)durationOfCurrentPlaylistItem
233 {
234     intf_thread_t *p_intf = VLCIntf;
235     if( !p_intf )
236         return 0;
237
238     input_thread_t * p_input = pl_CurrentInput( p_intf );
239     int64_t i_duration = -1;
240     if( !p_input )
241         return i_duration;
242
243     input_Control( p_input, INPUT_GET_LENGTH, &i_duration );
244     vlc_object_release( p_input );
245
246     return (int)( i_duration / 1000000 );
247 }
248
249 - (NSURL*)URLOfCurrentPlaylistItem
250 {
251     intf_thread_t *p_intf = VLCIntf;
252     if( !p_intf )
253         return nil;
254
255     input_thread_t *p_input = pl_CurrentInput( p_intf );
256     if( !p_input )
257         return nil;
258
259     input_item_t *p_item = input_GetItem( p_input );
260     if( !p_item )
261     {
262         vlc_object_release( p_input );
263         return nil;
264     }
265
266     char *psz_uri = input_item_GetURI( p_item );
267     if( !psz_uri )
268     {
269         vlc_object_release( p_input );
270         return nil;
271     }
272
273     NSURL *o_url;
274     o_url = [NSURL URLWithString:[NSString stringWithUTF8String:psz_uri]];
275     vlc_object_release( p_input );
276
277     return o_url;
278 }
279
280 - (NSString*)nameOfCurrentPlaylistItem
281 {
282     intf_thread_t *p_intf = VLCIntf;
283     if( !p_intf )
284         return nil;
285
286     input_thread_t *p_input = pl_CurrentInput( p_intf );
287     if( !p_input )
288         return nil;
289
290     input_item_t *p_item = input_GetItem( p_input );
291     if( !p_item )
292     {
293         vlc_object_release( p_input );
294         return nil;
295     }
296
297     char *psz_uri = input_item_GetURI( p_item );
298     if( !psz_uri )
299     {
300         vlc_object_release( p_input );
301         return nil;
302     }
303
304     NSString *o_name;
305     char *format = var_InheritString( VLCIntf, "input-title-format" );
306     char *formated = str_format_meta( p_input, format );
307     free( format );
308     o_name = [NSString stringWithUTF8String:formated];
309     free( formated );
310
311     NSURL * o_url = [NSURL URLWithString: [NSString stringWithUTF8String: psz_uri]];
312     free( psz_uri );
313
314     if( [o_name isEqualToString:@""] )
315     {
316         if( [o_url isFileURL] )
317             o_name = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
318         else
319             o_name = [o_url absoluteString];
320     }
321     vlc_object_release( p_input );
322     return o_name;
323 }
324
325 - (void)forward
326 {
327     //LEGACY SUPPORT
328     [self forwardShort];
329 }
330
331 - (void)backward
332 {
333     //LEGACY SUPPORT
334     [self backwardShort];
335 }
336
337 - (void)forwardExtraShort
338 {
339     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
340 }
341
342 - (void)backwardExtraShort
343 {
344     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
345 }
346
347 - (void)forwardShort
348 {
349     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_SHORT );
350 }
351
352 - (void)backwardShort
353 {
354     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_SHORT );
355 }
356
357 - (void)forwardMedium
358 {
359     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_MEDIUM );
360 }
361
362 - (void)backwardMedium
363 {
364     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_MEDIUM );
365 }
366
367 - (void)forwardLong
368 {
369     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_LONG );
370 }
371
372 - (void)backwardLong
373 {
374     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_LONG );
375 }
376
377 - (void)shuffle
378 {
379     intf_thread_t *p_intf = VLCIntf;
380     if( !p_intf )
381         return;
382
383     vlc_value_t val;
384     playlist_t * p_playlist = pl_Get( p_intf );
385     vout_thread_t *p_vout = getVout();
386
387     var_Get( p_playlist, "random", &val );
388     val.b_bool = !val.b_bool;
389     var_Set( p_playlist, "random", val );
390     if( val.b_bool )
391     {
392         if( p_vout )
393         {
394             vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Random On" ) );
395             vlc_object_release( p_vout );
396         }
397         config_PutInt( p_playlist, "random", 1 );
398     }
399     else
400     {
401         if( p_vout )
402         {
403             vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Random Off" ) );
404             vlc_object_release( p_vout );
405         }
406         config_PutInt( p_playlist, "random", 0 );
407     }
408 }
409
410 - (void)repeatAll
411 {
412     intf_thread_t *p_intf = VLCIntf;
413     if( !p_intf )
414         return;
415
416     playlist_t * p_playlist = pl_Get( p_intf );
417
418     var_SetBool( p_playlist, "repeat", NO );
419     var_SetBool( p_playlist, "loop", YES );
420     config_PutInt( p_playlist, "repeat", NO );
421     config_PutInt( p_playlist, "loop", YES );
422
423     vout_thread_t *p_vout = getVout();
424     if( p_vout )
425     {
426         vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat All" ) );
427         vlc_object_release( p_vout );
428     }
429 }
430
431 - (void)repeatOne
432 {
433     intf_thread_t *p_intf = VLCIntf;
434     if( !p_intf )
435         return;
436
437     playlist_t * p_playlist = pl_Get( p_intf );
438
439     var_SetBool( p_playlist, "repeat", YES );
440     var_SetBool( p_playlist, "loop", NO );
441     config_PutInt( p_playlist, "repeat", YES );
442     config_PutInt( p_playlist, "loop", NO );
443
444     vout_thread_t *p_vout = getVout();
445     if( p_vout )
446     {
447         vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat One" ) );
448         vlc_object_release( p_vout );
449     }
450 }
451
452 - (void)repeatOff
453 {
454     intf_thread_t *p_intf = VLCIntf;
455     if( !p_intf )
456         return;
457
458     playlist_t * p_playlist = pl_Get( p_intf );
459
460     var_SetBool( p_playlist, "repeat", NO );
461     var_SetBool( p_playlist, "loop", NO );
462     config_PutInt( p_playlist, "repeat", NO );
463     config_PutInt( p_playlist, "loop", NO );
464
465     vout_thread_t *p_vout = getVout();
466     if( p_vout )
467     {
468         vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat Off" ) );
469         vlc_object_release( p_vout );
470     }
471 }
472
473 - (void)volumeUp
474 {
475     intf_thread_t *p_intf = VLCIntf;
476     if( !p_intf )
477         return;
478
479     aout_VolumeUp( pl_Get( p_intf ), 1, NULL );
480 }
481
482 - (void)volumeDown
483 {
484     intf_thread_t *p_intf = VLCIntf;
485     if( !p_intf )
486         return;
487
488     aout_VolumeDown( pl_Get( p_intf ), 1, NULL );
489 }
490
491 - (void)mute
492 {
493     intf_thread_t *p_intf = VLCIntf;
494     if( !p_intf )
495         return;
496
497     aout_ToggleMute( pl_Get( p_intf ), NULL );
498 }
499
500 - (BOOL)isMuted
501 {
502     intf_thread_t *p_intf = VLCIntf;
503     if( !p_intf )
504         return NO;
505
506     BOOL b_is_muted = NO;
507     b_is_muted = aout_IsMuted( VLC_OBJECT(pl_Get( p_intf )) );
508
509     return b_is_muted;
510 }
511
512 - (int)volume
513 {
514     intf_thread_t *p_intf = VLCIntf;
515     if( !p_intf )
516         return 0;
517
518     audio_volume_t i_volume = aout_VolumeGet( pl_Get( p_intf ) );
519
520     return (int)i_volume;
521 }
522
523 - (void)setVolume: (int)i_value
524 {
525     intf_thread_t *p_intf = VLCIntf;
526     if( !p_intf )
527         return;
528
529     aout_VolumeSet( pl_Get( p_intf ), i_value );
530 }
531
532 #pragma mark -
533 #pragma mark drag and drop support for VLCVoutView, VLBrushedMetalImageView and VLCThreePartDropView
534 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
535 {
536     NSPasteboard *o_paste = [sender draggingPasteboard];
537     NSArray *o_types = [NSArray arrayWithObject: NSFilenamesPboardType];
538     NSString *o_desired_type = [o_paste availableTypeFromArray:o_types];
539     NSData *o_carried_data = [o_paste dataForType:o_desired_type];
540     BOOL b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
541
542     if( o_carried_data )
543     {
544         if( [o_desired_type isEqualToString:NSFilenamesPboardType] )
545         {
546             NSArray *o_array = [NSArray array];
547             NSArray *o_values = [[o_paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
548             NSUInteger count = [o_values count];
549
550             input_thread_t * p_input = pl_CurrentInput( VLCIntf );
551             BOOL b_returned = NO;
552
553             if( count == 1 && p_input )
554             {
555                 b_returned = input_AddSubtitle( p_input, make_URI([[o_values objectAtIndex:0] UTF8String], NULL), true );
556                 vlc_object_release( p_input );
557                 if( !b_returned )
558                     return YES;
559             }
560             else if( p_input )
561                 vlc_object_release( p_input );
562
563             for( NSUInteger i = 0; i < count; i++)
564             {
565                 NSDictionary *o_dic;
566                 char *psz_uri = make_URI([[o_values objectAtIndex:i] UTF8String], NULL);
567                 if( !psz_uri )
568                     continue;
569
570                 o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
571                 free( psz_uri );
572
573                 o_array = [o_array arrayByAddingObject: o_dic];
574             }
575             if( b_autoplay )
576                 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
577             else
578                 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:YES];
579
580             return YES;
581         }
582     }
583     return NO;
584 }
585
586 #pragma mark -
587 #pragma mark video output stuff
588
589 - (void)setAspectRatioLocked:(BOOL)b_value
590 {
591     config_PutInt( VLCIntf, "macosx-lock-aspect-ratio", b_value );
592 }
593
594 - (BOOL)aspectRatioIsLocked
595 {
596     return config_GetInt( VLCIntf, "macosx-lock-aspect-ratio" );
597 }
598
599 - (void)toggleFullscreen
600 {
601     intf_thread_t *p_intf = VLCIntf;
602     if( !p_intf )
603         return;
604
605     BOOL b_fs = var_ToggleBool( pl_Get( p_intf ), "fullscreen" );
606
607     vout_thread_t *p_vout = getVout();
608     if( p_vout )
609     {
610         var_SetBool( p_vout, "fullscreen", b_fs );
611         vlc_object_release( p_vout );
612     }
613 }
614
615 #pragma mark -
616 #pragma mark uncommon stuff
617
618 - (BOOL)fixPreferences
619 {
620     NSMutableString * o_workString;
621     NSRange returnedRange;
622     NSRange fullRange;
623     BOOL b_needsRestart = NO;
624
625     #define fixpref( pref ) \
626     o_workString = [[NSMutableString alloc] initWithFormat:@"%s", config_GetPsz( VLCIntf, pref )]; \
627     if ([o_workString length] > 0) \
628     { \
629         returnedRange = [o_workString rangeOfString:@"macosx" options: NSCaseInsensitiveSearch]; \
630         if (returnedRange.location != NSNotFound) \
631         { \
632             if ([o_workString isEqualToString:@"macosx"]) \
633                 [o_workString setString:@""]; \
634             fullRange = NSMakeRange( 0, [o_workString length] ); \
635             [o_workString replaceOccurrencesOfString:@":macosx" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
636             fullRange = NSMakeRange( 0, [o_workString length] ); \
637             [o_workString replaceOccurrencesOfString:@"macosx:" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
638             \
639             config_PutPsz( VLCIntf, pref, [o_workString UTF8String] ); \
640             b_needsRestart = YES; \
641         } \
642     } \
643     [o_workString release]
644
645     fixpref( "control" );
646     fixpref( "extraintf" );
647     #undef fixpref
648
649     return b_needsRestart;
650 }
651
652 @end