]> git.sesse.net Git - vlc/blob - modules/gui/macosx/applescript.m
macosx: copyright fixes
[vlc] / modules / gui / macosx / applescript.m
1 /*****************************************************************************
2  * applescript.m: MacOS X AppleScript support
3  *****************************************************************************
4  * Copyright (C) 2002-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "intf.h"
28 #include "applescript.h"
29 #include "CoreInteraction.h"
30 #include "vlc_aout_intf.h"
31
32 /*****************************************************************************
33  * VLGetURLScriptCommand implementation
34  *****************************************************************************/
35 @implementation VLGetURLScriptCommand
36
37 - (id)performDefaultImplementation {
38     NSString *o_command = [[self commandDescription] commandName];
39     NSString *o_urlString = [self directParameter];
40
41     if ( [o_command isEqualToString:@"GetURL"] || [o_command isEqualToString:@"OpenURL"] )
42     {
43         intf_thread_t * p_intf = VLCIntf;
44         playlist_t * p_playlist = pl_Get( p_intf );
45         if( p_playlist == NULL )
46         {
47             return nil;
48         }
49
50         if ( o_urlString )
51         {
52             NSURL * o_url;
53             input_item_t *p_input;
54
55             p_input = input_item_New( [o_urlString fileSystemRepresentation],
56                                     [[[NSFileManager defaultManager]
57                                     displayNameAtPath: o_urlString] UTF8String] );
58             /* FIXME: playlist_AddInput() can fail */
59             playlist_AddInput( p_playlist, p_input, PLAYLIST_INSERT,
60                                PLAYLIST_END, true, pl_Unlocked );
61
62             vlc_gc_decref( p_input );
63
64             o_url = [NSURL fileURLWithPath: o_urlString];
65             if( o_url != nil )
66             {
67                 [[NSDocumentController sharedDocumentController]
68                     noteNewRecentDocumentURL: o_url];
69             }
70         }
71     }
72     return nil;
73 }
74
75 @end
76
77
78 /*****************************************************************************
79  * VLControlScriptCommand implementation
80  *****************************************************************************/
81 /*
82  * This entire control command needs a better design. more object oriented.
83  * Applescript developers would be very welcome (hartman)
84  */
85 @implementation VLControlScriptCommand
86
87 - (id)performDefaultImplementation {
88     NSString *o_command = [[self commandDescription] commandName];
89     NSString *o_parameter = [self directParameter];
90
91     intf_thread_t * p_intf = VLCIntf;
92     playlist_t * p_playlist = pl_Get( p_intf );
93     if( p_playlist == NULL )
94     {
95         return nil;
96     }
97  
98     if ( [o_command isEqualToString:@"play"] )
99     {
100         [[VLCCoreInteraction sharedInstance] play];
101     }
102     else if ( [o_command isEqualToString:@"stop"] )
103     {
104         [[VLCCoreInteraction sharedInstance] stop];
105     }
106     else if ( [o_command isEqualToString:@"previous"] )
107     {
108         [[VLCCoreInteraction sharedInstance] previous];
109     }
110     else if ( [o_command isEqualToString:@"next"] )
111     {
112         [[VLCCoreInteraction sharedInstance] next];
113     }
114     else if ( [o_command isEqualToString:@"fullscreen"] )
115     {
116         [[VLCCoreInteraction sharedInstance] toggleFullscreen];
117     }
118     else if ( [o_command isEqualToString:@"mute"] )
119     {
120         [[VLCCoreInteraction sharedInstance] mute];
121     }
122     else if ( [o_command isEqualToString:@"volumeUp"] )
123     {
124         [[VLCCoreInteraction sharedInstance] volumeUp];
125     }
126     else if ( [o_command isEqualToString:@"volumeDown"] )
127     {
128         [[VLCCoreInteraction sharedInstance] volumeDown];
129     }
130         else if ( [o_command isEqualToString:@"stepForward"] )
131     {
132         //default: forwardShort
133         if (o_parameter)
134         {
135             int i_parameter = [o_parameter intValue];
136             switch (i_parameter)
137             {
138                 case 1:
139                     [[VLCCoreInteraction sharedInstance] forwardExtraShort];
140                     break;
141                 case 2:
142                     [[VLCCoreInteraction sharedInstance] forwardShort];
143                     break;
144                 case 3:
145                     [[VLCCoreInteraction sharedInstance] forwardMedium];
146                     break;
147                 case 4:
148                     [[VLCCoreInteraction sharedInstance] forwardLong];
149                     break;
150                 default:
151                     [[VLCCoreInteraction sharedInstance] forwardShort];
152                     break;
153             }
154         }
155         else
156         {
157             [[VLCCoreInteraction sharedInstance] forwardShort];
158         }
159     }
160     else if ( [o_command isEqualToString:@"stepBackward"] )
161     {
162         //default: backwardShort
163         if (o_parameter)
164         {
165             int i_parameter = [o_parameter intValue];
166             switch (i_parameter)
167             {
168                 case 1:
169                     [[VLCCoreInteraction sharedInstance] backwardExtraShort];
170                     break;
171                 case 2:
172                     [[VLCCoreInteraction sharedInstance] backwardShort];
173                     break;
174                 case 3:
175                     [[VLCCoreInteraction sharedInstance] backwardMedium];
176                     break;
177                 case 4:
178                     [[VLCCoreInteraction sharedInstance] backwardLong];
179                     break;
180                 default:
181                     [[VLCCoreInteraction sharedInstance] backwardShort];
182                     break;
183             }
184         }
185         else
186         {
187             [[VLCCoreInteraction sharedInstance] backwardShort];
188         }
189     }
190    return nil;
191 }
192
193 @end
194
195 /*****************************************************************************
196  * Category that adds AppleScript support to NSApplication
197  *****************************************************************************/
198 @implementation NSApplication(ScriptSupport)
199
200 - (BOOL) scriptFullscreenMode {    
201     vout_thread_t * p_vout = getVout();
202     if( !p_vout )
203         return NO;
204     BOOL b_value = var_GetBool( p_vout, "fullscreen");
205     vlc_object_release( p_vout );
206     return b_value;
207 }
208 - (void) setScriptFullscreenMode: (BOOL) mode {
209     vout_thread_t * p_vout = getVout();
210     if( !p_vout )
211         return;
212     if (var_GetBool( p_vout, "fullscreen") == mode)
213     {
214         vlc_object_release( p_vout );
215         return;
216     }
217     vlc_object_release( p_vout );
218     [[VLCCoreInteraction sharedInstance] toggleFullscreen];
219 }
220
221 - (BOOL) muted {
222     return [[VLCCoreInteraction sharedInstance] isMuted];
223 }
224
225 - (BOOL) playing {
226     return [[VLCCoreInteraction sharedInstance] isPlaying];
227 }
228
229 - (double) audioVolume {
230     return ( (double)[[VLCCoreInteraction sharedInstance] volume] / (double)AOUT_VOLUME_DEFAULT );
231 }
232
233 - (void) setAudioVolume: (double) d_audioVolume {
234     //1 = 100%, 4 = 400%; 0 <= d_audioVolume  <= 4
235     //0-1024 (but AOUT_VOLUME_MAX == 512)???
236     //AOUT_VOLUME_DEFAULT = 256 = 100%
237     //somehow [[VLCCoreInteraction sharedInstance] setVolume:i_parameter] has 0-32 steps with 32 as stepWidth (0 - 1024)
238     if (d_audioVolume < 0)
239             d_audioVolume = 0;
240
241     if (d_audioVolume > 4)
242             d_audioVolume = 4;
243
244     intf_thread_t * p_intf = VLCIntf;
245     playlist_t * p_playlist = pl_Get( VLCIntf );
246     int i_volume_step = config_GetInt( VLCIntf->p_libvlc, "volume-step" );
247
248     int i_parameter = (int) ( d_audioVolume * i_volume_step / 4 );
249     [[VLCCoreInteraction sharedInstance] setVolume:i_parameter];
250 }
251
252 - (int) currentTime {
253     return [[VLCCoreInteraction sharedInstance] currentTime];
254 }
255
256 - (void) setCurrentTime: (int) i_currentTime {
257     if (i_currentTime)
258         [[VLCCoreInteraction sharedInstance] setCurrentTime:i_currentTime];
259 }
260
261 #pragma mark -
262 //TODO:whenever VLC should implement NSDocument, the methods below should move or be additionaly implemented in the NSDocument category
263 - (int) durationOfCurrentItem {
264     return [[VLCCoreInteraction sharedInstance] durationOfCurrentPlaylistItem];
265 }
266
267 - (NSString*) pathOfCurrentItem {
268     return [[[VLCCoreInteraction sharedInstance] URLOfCurrentPlaylistItem] path];
269 }
270
271 - (NSString*) nameOfCurrentItem {
272     return [[VLCCoreInteraction sharedInstance] nameOfCurrentPlaylistItem];
273 }
274
275 @end