macosx: prepared fullscreen support and clean-up
[vlc.git] / modules / gui / macosx / CoreInteraction.m
1 /*****************************************************************************
2  * CoreInteraction.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2011 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 "vout.h"
27 #import "open.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
34 @implementation VLCCoreInteraction
35 static VLCCoreInteraction *_o_sharedInstance = nil;
36
37 + (VLCCoreInteraction *)sharedInstance
38 {
39     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
40 }
41
42 #pragma mark -
43 #pragma mark Initialization
44
45 - (id)init
46 {
47     if( _o_sharedInstance)
48     {
49         [self dealloc];
50         return _o_sharedInstance;
51     }
52     else
53     {
54         _o_sharedInstance = [super init];
55         b_lockAspectRatio = YES;
56     }
57     
58     return _o_sharedInstance;
59 }
60
61 - (void)dealloc
62 {
63     [[NSNotificationCenter defaultCenter] removeObserver: self];
64     [super dealloc];
65 }
66
67 - (void)awakeFromNib
68 {
69     [[NSNotificationCenter defaultCenter] addObserver: self 
70                                              selector: @selector(applicationWillFinishLaunching:)
71                                                  name: NSApplicationWillFinishLaunchingNotification
72                                                object: nil];
73 }
74
75 #pragma mark -
76 #pragma mark Playback Controls
77
78 - (void)play
79 {
80     playlist_t * p_playlist = pl_Get( VLCIntf );
81     bool empty;
82     
83     PL_LOCK;
84     empty = playlist_IsEmpty( p_playlist );
85     PL_UNLOCK;
86     
87     if( empty )
88         [[[VLCMain sharedInstance] open] openFileGeneric];
89     
90     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PLAY_PAUSE );
91 }
92
93 - (void)stop
94 {
95     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_STOP );
96     /* Close the window directly, because we do know that there
97      * won't be anymore video. It's currently waiting a bit. */
98     [[[self voutView] window] orderOut:self];
99 }
100
101 - (void)faster
102 {
103     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_FASTER );
104 }
105
106 - (void)slower
107 {
108     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_SLOWER );
109 }
110
111 - (void)normalSpeed
112 {
113     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_RATE_NORMAL );
114 }
115
116 - (void)setPlaybackRate:(int)i_value
117 {
118     playlist_t * p_playlist = pl_Get( VLCIntf );
119
120     double speed = pow( 2, (double)i_value / 17 );
121     int rate = INPUT_RATE_DEFAULT / speed;
122     if( i_currentPlaybackRate != rate )
123         var_SetFloat( p_playlist, "rate", (float)INPUT_RATE_DEFAULT / (float)rate );
124     i_currentPlaybackRate = rate;
125 }
126
127 - (int)playbackRate
128 {
129     playlist_t * p_playlist = pl_Get( VLCIntf );
130
131     float rate = var_GetFloat( p_playlist, "rate" );
132     double value = 17 * log( rate ) / log( 2. );
133     int returnValue = (int) ( ( value > 0 ) ? value + .5 : value - .5 );
134
135     if( returnValue < -34 )
136         returnValue = -34;
137     else if( returnValue > 34 )
138         returnValue = 34;
139
140     i_currentPlaybackRate = returnValue;
141     return returnValue;
142 }
143
144 - (void)previous
145 {
146     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_PREV );
147 }
148
149 - (void)next
150 {
151     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_NEXT );
152 }
153
154 - (void)forward
155 {
156     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_SHORT );
157 }
158
159 - (void)backward
160 {
161     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_SHORT );
162 }
163
164 - (void)shuffle
165 {
166     vlc_value_t val;
167     playlist_t * p_playlist = pl_Get( VLCIntf );
168     
169     var_Get( p_playlist, "random", &val );
170     val.b_bool = !val.b_bool;
171     var_Set( p_playlist, "random", val );
172     if( val.b_bool )
173     {
174         //vout_OSDMessage( VLCIntf, SPU_DEFAULT_CHANNEL, "%s", _( "Random On" ) );
175         config_PutInt( p_playlist, "random", 1 );
176     }
177     else
178     {
179         //vout_OSDMessage( VLCIntf, SPU_DEFAULT_CHANNEL, "%s", _( "Random Off" ) );
180         config_PutInt( p_playlist, "random", 0 );
181     }
182 }
183
184 - (void)repeatAll
185 {
186     playlist_t * p_playlist = pl_Get( VLCIntf );
187
188     var_SetBool( p_playlist, "repeat", NO );
189     var_SetBool( p_playlist, "loop", YES );
190     config_PutInt( p_playlist, "repeat", NO );
191     config_PutInt( p_playlist, "loop", YES );
192
193     //vout_OSDMessage( VLCIntf, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat All" ) );
194 }
195
196 - (void)repeatOne
197 {
198     playlist_t * p_playlist = pl_Get( VLCIntf );
199
200     var_SetBool( p_playlist, "repeat", YES );
201     var_SetBool( p_playlist, "loop", NO );
202     config_PutInt( p_playlist, "repeat", YES );
203     config_PutInt( p_playlist, "loop", NO );
204
205     //vout_OSDMessage( VLCIntf, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat One" ) );
206 }
207
208 - (void)repeatOff
209 {
210     playlist_t * p_playlist = pl_Get( VLCIntf );
211
212     var_SetBool( p_playlist, "repeat", NO );
213     var_SetBool( p_playlist, "loop", NO );
214     config_PutInt( p_playlist, "repeat", NO );
215     config_PutInt( p_playlist, "loop", NO );
216
217     //vout_OSDMessage( VLCIntf, SPU_DEFAULT_CHANNEL, "%s", _( "Repeat Off" ) );
218 }
219
220 // CAVE: [o_main manageVolumeSlider]
221
222 - (void)volumeUp
223 {
224     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_VOL_UP );
225 }
226
227 - (void)volumeDown
228 {
229     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_VOL_DOWN );
230 }
231
232 - (void)mute
233 {
234     var_SetInteger( VLCIntf->p_libvlc, "key-action", ACTIONID_VOL_MUTE );
235 }
236
237 - (void)setVolume: (int)i_value
238 {
239     intf_thread_t * p_intf = VLCIntf;
240     playlist_t * p_playlist = pl_Get( VLCIntf );
241     audio_volume_t i_volume = (audio_volume_t)i_value;
242     int i_volume_step;
243
244     i_volume_step = config_GetInt( VLCIntf->p_libvlc, "volume-step" );
245     aout_VolumeSet( p_playlist, i_volume * i_volume_step );
246 }
247
248 #pragma mark -
249 #pragma mark video output stuff
250
251 - (void)setAspectRatioLocked:(BOOL)b_value
252 {
253     b_lockAspectRatio = b_value;
254 }
255
256 - (BOOL)aspectRatioIsLocked
257 {
258     return b_lockAspectRatio;
259 }
260
261 - (void)toggleFullscreen
262 {
263     input_thread_t * p_input = pl_CurrentInput( VLCIntf );
264
265     if( p_input != NULL )
266     {
267         playlist_t * p_playlist = pl_Get( VLCIntf );
268         var_ToggleBool( p_playlist, "fullscreen" );
269
270         vlc_object_release( p_input );
271     }
272 }
273
274 - (id)voutView
275 {
276     id o_window;
277     id o_voutView = nil;
278     id o_embeddedViewList = [[VLCMain sharedInstance] embeddedList];
279     NSEnumerator *o_enumerator = [[NSApp orderedWindows] objectEnumerator];
280     while( !o_voutView && ( o_window = [o_enumerator nextObject] ) )
281     {
282         /* We have an embedded vout */
283         if( [o_embeddedViewList windowContainsEmbedded: o_window] )
284         {
285             o_voutView = [o_embeddedViewList viewForWindow: o_window];
286         }
287         /* We have a detached vout */
288         else if( [[o_window className] isEqualToString: @"VLCVoutWindow"] )
289         {
290             o_voutView = [o_window voutView];
291         }
292     }
293     return [[o_voutView retain] autorelease];
294 }
295 @end