]> git.sesse.net Git - vlc/blob - modules/gui/macosx/applescript.m
macosx: fix resume playback guards, do not resume for folder urls
[vlc] / modules / gui / macosx / applescript.m
1 /*****************************************************************************
2  * applescript.m: MacOS X AppleScript support
3  *****************************************************************************
4  * Copyright (C) 2002-2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <thedj@users.sourceforge.net>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #import "intf.h"
29 #import "applescript.h"
30 #import "CoreInteraction.h"
31 #import "playlist.h"
32 #import <vlc_url.h>
33
34 /*****************************************************************************
35  * VLGetURLScriptCommand implementation
36  *****************************************************************************/
37 @implementation VLGetURLScriptCommand
38
39 - (id)performDefaultImplementation {
40     NSString *o_command = [[self commandDescription] commandName];
41     NSString *o_urlString = [self directParameter];
42
43     if ([o_command isEqualToString:@"GetURL"] || [o_command isEqualToString:@"OpenURL"]) {
44         if (o_urlString) {
45             BOOL b_autoplay = config_GetInt(VLCIntf, "macosx-autoplay");
46             NSURL * o_url = [NSURL fileURLWithPath: o_urlString];
47             if (o_url != nil)
48                 [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_url];
49
50             input_thread_t * p_input = pl_CurrentInput(VLCIntf);
51             BOOL b_returned = NO;
52
53             if (p_input) {
54                 b_returned = input_AddSubtitle(p_input, [o_urlString UTF8String], true);
55                 vlc_object_release(p_input);
56                 if (!b_returned)
57                     return nil;
58             }
59
60             NSDictionary *o_dic;
61             NSArray *o_array;
62             o_dic = [NSDictionary dictionaryWithObject:o_urlString forKey:@"ITEM_URL"];
63             o_array = [NSArray arrayWithObject: o_dic];
64
65             if (b_autoplay)
66                 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue: NO];
67             else
68                 [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue: YES];
69         }
70     }
71     return nil;
72 }
73
74 @end
75
76
77 /*****************************************************************************
78  * VLControlScriptCommand implementation
79  *****************************************************************************/
80 /*
81  * This entire control command needs a better design. more object oriented.
82  * Applescript developers would be very welcome (hartman)
83  */
84 @implementation VLControlScriptCommand
85
86 - (id)performDefaultImplementation {
87     NSString *o_command = [[self commandDescription] commandName];
88     NSString *o_parameter = [self directParameter];
89
90     intf_thread_t * p_intf = VLCIntf;
91     playlist_t * p_playlist = pl_Get(p_intf);
92
93     if ([o_command isEqualToString:@"play"])
94         [[VLCCoreInteraction sharedInstance] playOrPause];
95     else if ([o_command isEqualToString:@"stop"])
96         [[VLCCoreInteraction sharedInstance] stop];
97     else if ([o_command isEqualToString:@"previous"])
98         [[VLCCoreInteraction sharedInstance] previous];
99     else if ([o_command isEqualToString:@"next"])
100         [[VLCCoreInteraction sharedInstance] next];
101     else if ([o_command isEqualToString:@"fullscreen"])
102         [[VLCCoreInteraction sharedInstance] toggleFullscreen];
103     else if ([o_command isEqualToString:@"mute"])
104         [[VLCCoreInteraction sharedInstance] toggleMute];
105     else if ([o_command isEqualToString:@"volumeUp"])
106         [[VLCCoreInteraction sharedInstance] volumeUp];
107     else if ([o_command isEqualToString:@"volumeDown"])
108         [[VLCCoreInteraction sharedInstance] volumeDown];
109     else if ([o_command isEqualToString:@"stepForward"]) {
110         //default: forwardShort
111         if (o_parameter) {
112             int i_parameter = [o_parameter intValue];
113             switch (i_parameter) {
114                 case 1:
115                     [[VLCCoreInteraction sharedInstance] forwardExtraShort];
116                     break;
117                 case 2:
118                     [[VLCCoreInteraction sharedInstance] forwardShort];
119                     break;
120                 case 3:
121                     [[VLCCoreInteraction sharedInstance] forwardMedium];
122                     break;
123                 case 4:
124                     [[VLCCoreInteraction sharedInstance] forwardLong];
125                     break;
126                 default:
127                     [[VLCCoreInteraction sharedInstance] forwardShort];
128                     break;
129             }
130         } else
131             [[VLCCoreInteraction sharedInstance] forwardShort];
132     } else if ([o_command isEqualToString:@"stepBackward"]) {
133         //default: backwardShort
134         if (o_parameter) {
135             int i_parameter = [o_parameter intValue];
136             switch (i_parameter) {
137                 case 1:
138                     [[VLCCoreInteraction sharedInstance] backwardExtraShort];
139                     break;
140                 case 2:
141                     [[VLCCoreInteraction sharedInstance] backwardShort];
142                     break;
143                 case 3:
144                     [[VLCCoreInteraction sharedInstance] backwardMedium];
145                     break;
146                 case 4:
147                     [[VLCCoreInteraction sharedInstance] backwardLong];
148                     break;
149                 default:
150                     [[VLCCoreInteraction sharedInstance] backwardShort];
151                     break;
152             }
153         } else
154             [[VLCCoreInteraction sharedInstance] backwardShort];
155     }
156    return nil;
157 }
158
159 @end
160
161 /*****************************************************************************
162  * Category that adds AppleScript support to NSApplication
163  *****************************************************************************/
164 @implementation NSApplication(ScriptSupport)
165
166 - (BOOL)scriptFullscreenMode {
167     vout_thread_t * p_vout = getVoutForActiveWindow();
168     if (!p_vout)
169         return NO;
170     BOOL b_value = var_GetBool(p_vout, "fullscreen");
171     vlc_object_release(p_vout);
172     return b_value;
173 }
174
175 - (void)setScriptFullscreenMode:(BOOL)mode {
176     vout_thread_t * p_vout = getVoutForActiveWindow();
177     if (!p_vout)
178         return;
179     if (var_GetBool(p_vout, "fullscreen") == mode) {
180         vlc_object_release(p_vout);
181         return;
182     }
183     vlc_object_release(p_vout);
184     [[VLCCoreInteraction sharedInstance] toggleFullscreen];
185 }
186
187 - (BOOL) muted {
188     return [[VLCCoreInteraction sharedInstance] mute];
189 }
190
191 - (BOOL) playing {
192     intf_thread_t *p_intf = VLCIntf;
193     if (!p_intf)
194         return NO;
195
196     input_thread_t * p_input = pl_CurrentInput(p_intf);
197     if (!p_input)
198         return NO;
199
200     input_state_e i_state = ERROR_S;
201     input_Control(p_input, INPUT_GET_STATE, &i_state);
202     vlc_object_release(p_input);
203
204     return ((i_state == OPENING_S) || (i_state == PLAYING_S));
205 }
206
207 - (int) audioVolume {
208     return ([[VLCCoreInteraction sharedInstance] volume]);
209 }
210
211 - (void) setAudioVolume:(int)i_audioVolume {
212     [[VLCCoreInteraction sharedInstance] setVolume:(int)i_audioVolume];
213 }
214
215 - (int) audioDesync {
216     input_thread_t * p_input = pl_CurrentInput(VLCIntf);
217     int i_delay = -1;
218
219     if(!p_input)
220         return i_delay;
221
222     i_delay = var_GetTime(p_input, "audio-delay");
223     vlc_object_release(p_input);
224
225     return (i_delay / 1000);
226 }
227
228 - (void) setAudioDesync:(int)i_audioDesync {
229     input_thread_t * p_input = pl_CurrentInput(VLCIntf);
230     if(!p_input)
231         return;
232
233     var_SetTime(p_input, "audio-delay", i_audioDesync * 1000);
234     vlc_object_release(p_input);
235 }
236
237 - (int) currentTime {
238     input_thread_t * p_input = pl_CurrentInput(VLCIntf);
239     int64_t i_currentTime = -1;
240
241     if (!p_input)
242         return i_currentTime;
243
244     input_Control(p_input, INPUT_GET_TIME, &i_currentTime);
245     vlc_object_release(p_input);
246
247     return (int)(i_currentTime / 1000000);
248 }
249
250 - (void) setCurrentTime:(int)i_currentTime {
251     if (i_currentTime) {
252         int64_t i64_value = (int64_t)i_currentTime;
253         input_thread_t * p_input = pl_CurrentInput(VLCIntf);
254
255         if (!p_input)
256             return;
257
258         input_Control(p_input, INPUT_SET_TIME, (int64_t)(i64_value * 1000000));
259         vlc_object_release(p_input);
260     }
261 }
262
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