]> git.sesse.net Git - vlc/blob - modules/gui/macosx/CoreInteraction.m
OSX GUI: store the interface pointer to avoid multiple accesses.
[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 <vlc_playlist.h>
28 #import <vlc_input.h>
29 #import <vlc_keys.h>
30 #import <vlc_osd.h>
31 #import <vlc_aout_intf.h>
32 #import <vlc/vlc.h>
33 #import <vlc_strings.h>
34
35 @implementation VLCCoreInteraction
36 static VLCCoreInteraction *_o_sharedInstance = nil;
37
38 + (VLCCoreInteraction *)sharedInstance
39 {
40     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
41 }
42
43 #pragma mark -
44 #pragma mark Initialization
45
46 - (id)init
47 {
48     if( _o_sharedInstance)
49     {
50         [self dealloc];
51         return _o_sharedInstance;
52     }
53     else
54     {
55         _o_sharedInstance = [super init];
56         b_lockAspectRatio = YES;
57     }
58     
59     return _o_sharedInstance;
60 }
61
62 - (void)dealloc
63 {
64     [[NSNotificationCenter defaultCenter] removeObserver: self];
65     [super dealloc];
66 }
67
68 - (void)awakeFromNib
69 {
70     [[NSNotificationCenter defaultCenter] addObserver: self 
71                                              selector: @selector(applicationWillFinishLaunching:)
72                                                  name: NSApplicationWillFinishLaunchingNotification
73                                                object: nil];
74 }
75
76 #pragma mark -
77 #pragma mark Playback Controls
78
79 - (void)play
80 {
81     playlist_t * p_playlist = pl_Get( VLCIntf );
82     bool empty;
83     
84     PL_LOCK;
85     empty = playlist_IsEmpty( p_playlist );
86     PL_UNLOCK;
87     
88     if( empty )
89         [[[VLCMain sharedInstance] open] openFileGeneric];
90     
91     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PLAY_PAUSE );
92 }
93
94 - (void)pause
95 {
96     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PAUSE );
97 }
98
99 - (void)stop
100 {
101     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_STOP );
102 }
103
104 - (void)faster
105 {
106     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_FASTER );
107 }
108
109 - (void)slower
110 {
111     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_SLOWER );
112 }
113
114 - (void)normalSpeed
115 {
116     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_RATE_NORMAL );
117 }
118
119 - (void)toggleRecord
120 {
121     intf_thread_t *p_intf = VLCIntf;
122
123     input_thread_t * p_input;
124     p_input = pl_CurrentInput( p_intf );
125     if( p_input )
126     {
127         var_ToggleBool( p_input, "record" );
128         vlc_object_release( p_input );
129     }
130 }
131
132 - (void)setPlaybackRate:(int)i_value
133 {
134     playlist_t * p_playlist = pl_Get( VLCIntf );
135
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;
141 }
142
143 - (int)playbackRate
144 {
145     float f_rate;
146
147     intf_thread_t *p_intf = VLCIntf;
148
149     input_thread_t * p_input;
150     p_input = pl_CurrentInput( p_intf );
151     if (p_input)
152     {
153         f_rate = var_GetFloat( p_input, "rate" );
154         vlc_object_release( p_input );
155     }
156     else
157     {
158         playlist_t * p_playlist = pl_Get( VLCIntf );
159         f_rate = var_GetFloat( p_playlist, "rate" );
160     }
161
162     double value = 17 * log( f_rate ) / log( 2. );
163     int returnValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
164
165     if( returnValue < -34 )
166         returnValue = -34;
167     else if( returnValue > 34 )
168         returnValue = 34;
169
170     i_currentPlaybackRate = returnValue;
171     return returnValue;
172 }
173
174 - (void)previous
175 {
176     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PREV );
177 }
178
179 - (void)next
180 {
181     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_NEXT );
182 }
183
184 - (BOOL)isPlaying
185 {
186     intf_thread_t *p_intf = VLCIntf;
187
188     input_thread_t * p_input = pl_CurrentInput( p_intf );
189
190     if (!p_input) return NO;
191
192     input_state_e i_state = ERROR_S;
193     input_Control( p_input, INPUT_GET_STATE, &i_state);
194     vlc_object_release( p_input );
195
196     return ((i_state == OPENING_S) || (i_state == PLAYING_S));
197 }
198
199 - (int)currentTime
200 {
201     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
202     int64_t i_currentTime = -1;
203
204     if (!p_input) return i_currentTime;
205
206     input_Control( p_input, INPUT_GET_TIME, &i_currentTime);
207     vlc_object_release( p_input );
208
209     return (int)( i_currentTime / 1000000 );
210 }
211
212 - (void)setCurrentTime:(int)i_value
213 {
214     int64_t i64_value = (int64_t)i_value;
215     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
216
217     if (!p_input) return;
218
219     input_Control( p_input, INPUT_SET_TIME, (int64_t)(i64_value * 1000000));
220     vlc_object_release( p_input );
221 }
222
223 - (int)durationOfCurrentPlaylistItem
224 {
225     intf_thread_t *p_intf = VLCIntf;
226
227     input_thread_t * p_input = pl_CurrentInput( p_intf );
228     int64_t i_duration = -1;
229     if (!p_input) return i_duration;
230
231
232     input_Control( p_input, INPUT_GET_LENGTH, &i_duration);
233     vlc_object_release( p_input );
234
235     return (int)(i_duration / 1000000);
236 }
237
238 - (NSURL*)URLOfCurrentPlaylistItem
239 {
240     intf_thread_t *p_intf = VLCIntf;
241
242     input_thread_t *p_input = pl_CurrentInput( p_intf );
243     if (!p_input) return nil;
244
245     input_item_t *p_item = input_GetItem( p_input );
246     if (!p_item)
247     {
248         vlc_object_release( p_input );
249         return nil;
250     }
251
252     char *psz_uri = input_item_GetURI( p_item );
253     if (!psz_uri)
254     {
255         vlc_object_release( p_input );
256         return nil;
257     }
258
259     NSURL *o_url;
260     o_url = [NSURL URLWithString:[NSString stringWithUTF8String:psz_uri]];
261     vlc_object_release( p_input );
262
263     return o_url;
264 }
265
266 - (NSString*)nameOfCurrentPlaylistItem
267 {
268     intf_thread_t *p_intf = VLCIntf;
269
270     input_thread_t *p_input = pl_CurrentInput( p_intf );
271     if (!p_input) return nil;
272
273     input_item_t *p_item = input_GetItem( p_input );
274     if (!p_item)
275     {
276         vlc_object_release( p_input );
277         return nil;
278     }
279
280     char *psz_uri = input_item_GetURI( p_item );
281     if (!psz_uri)
282     {
283         vlc_object_release( p_input );
284         return nil;
285     }
286
287     NSString *o_name;
288     char *format = var_InheritString( VLCIntf, "input-title-format" );
289     char *formated = str_format_meta( p_input, format );
290     free( format );
291     o_name = [NSString stringWithUTF8String:formated];
292     free( formated );
293
294     NSURL * o_url = [NSURL URLWithString: [NSString stringWithUTF8String: psz_uri]];
295     free( psz_uri );
296
297     if ([o_name isEqualToString:@""])
298     {
299         if ([o_url isFileURL]) 
300             o_name = [[NSFileManager defaultManager] displayNameAtPath: [o_url path]];
301         else
302             o_name = [o_url absoluteString];
303     }
304     vlc_object_release( p_input );
305     return o_name;
306 }
307
308 - (void)forward
309 {
310     //LEGACY SUPPORT
311     [self forwardShort];
312 }
313
314 - (void)backward
315 {
316     //LEGACY SUPPORT
317     [self backwardShort];
318 }
319
320 - (void)forwardExtraShort
321 {
322     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_EXTRASHORT );
323 }
324
325 - (void)backwardExtraShort
326 {
327     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_EXTRASHORT );
328 }
329
330 - (void)forwardShort
331 {
332     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_SHORT );
333 }
334
335 - (void)backwardShort
336 {
337     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_SHORT );
338 }
339
340 - (void)forwardMedium
341 {
342     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_MEDIUM );
343 }
344
345 - (void)backwardMedium
346 {
347     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_MEDIUM );
348 }
349
350 - (void)forwardLong
351 {
352     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_LONG );
353 }
354
355 - (void)backwardLong
356 {
357     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_LONG );
358 }
359
360 - (void)shuffle
361 {
362     intf_thread_t *p_intf = VLCIntf;
363
364     vlc_value_t val;
365     playlist_t * p_playlist = pl_Get( p_intf );
366     vout_thread_t *p_vout = getVout();
367
368     var_Get( p_playlist, "random", &val );
369     val.b_bool = !val.b_bool;
370     var_Set( p_playlist, "random", val );
371     if( val.b_bool )
372     {
373         if (p_vout)
374         {
375             vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Random On" ) );
376             vlc_object_release( p_vout );
377         }
378         config_PutInt( p_playlist, "random", 1 );
379     }
380     else
381     {
382         if (p_vout)
383         {
384             vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Random Off" ) );
385             vlc_object_release( p_vout );
386         }
387         config_PutInt( p_playlist, "random", 0 );
388     }
389 }
390
391 - (void)repeatAll
392 {
393     intf_thread_t *p_intf = VLCIntf;
394
395     playlist_t * p_playlist = pl_Get( p_intf );
396
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 );
401
402     vout_thread_t *p_vout = getVout();
403     if (p_vout)
404     {
405         vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat All" ) );
406         vlc_object_release( p_vout );
407     }
408 }
409
410 - (void)repeatOne
411 {
412     intf_thread_t *p_intf = VLCIntf;
413
414     playlist_t * p_playlist = pl_Get( p_intf );
415
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 );
420
421     vout_thread_t *p_vout = getVout();
422     if (p_vout)
423     {
424         vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat One" ) );
425         vlc_object_release( p_vout );
426     }
427 }
428
429 - (void)repeatOff
430 {
431     intf_thread_t *p_intf = VLCIntf;
432
433     playlist_t * p_playlist = pl_Get( p_intf );
434
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 );
439
440     vout_thread_t *p_vout = getVout();
441     if (p_vout)
442     {
443         vout_OSDMessage( p_vout, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat Off" ) );
444         vlc_object_release( p_vout );
445     }
446 }
447
448 - (void)volumeUp
449 {
450     intf_thread_t *p_intf = VLCIntf;
451
452     aout_VolumeUp( pl_Get( p_intf ), 1, NULL );
453 }
454
455 - (void)volumeDown
456 {
457     intf_thread_t *p_intf = VLCIntf;
458
459     aout_VolumeDown( pl_Get( p_intf ), 1, NULL );
460 }
461
462 - (void)mute
463 {
464     intf_thread_t *p_intf = VLCIntf;
465
466     aout_ToggleMute( pl_Get( p_intf ), NULL );
467 }
468
469 - (BOOL)isMuted
470 {
471     intf_thread_t *p_intf = VLCIntf;
472
473     BOOL b_is_muted = NO;
474     b_is_muted = aout_IsMuted( VLC_OBJECT(pl_Get( p_intf )) );
475
476     return b_is_muted;
477 }
478
479 - (int)volume
480 {
481     intf_thread_t *p_intf = VLCIntf;
482
483     audio_volume_t i_volume = aout_VolumeGet( pl_Get( p_intf ) );
484
485     return (int)i_volume;
486 }
487
488 - (void)setVolume: (int)i_value
489 {
490     intf_thread_t *p_intf = VLCIntf;
491
492     aout_VolumeSet( pl_Get( p_intf ), i_value );
493 }
494
495 #pragma mark -
496 #pragma mark video output stuff
497
498 - (void)setAspectRatioLocked:(BOOL)b_value
499 {
500     b_lockAspectRatio = b_value;
501 }
502
503 - (BOOL)aspectRatioIsLocked
504 {
505     return b_lockAspectRatio;
506 }
507
508 - (void)toggleFullscreen
509 {
510     intf_thread_t *p_intf = VLCIntf;
511
512     var_ToggleBool( pl_Get( p_intf ), "fullscreen" );
513 }
514
515 @end