]> git.sesse.net Git - vlc/blob - modules/gui/macosx/applescript.m
4806edfeadc966b6bb469e8c3a0e84f1ee048584
[vlc] / modules / gui / macosx / applescript.m
1 /*****************************************************************************
2  * applescript.m: MacOS X AppleScript support
3  *****************************************************************************
4  * Copyright (C) 2002-2012 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 #include "intf.h"
29 #include "applescript.h"
30 #include "CoreInteraction.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         intf_thread_t * p_intf = VLCIntf;
43         playlist_t * p_playlist = pl_Get(p_intf);
44
45         if (o_urlString) {
46             NSURL * o_url;
47             input_item_t *p_input;
48             int returnValue;
49
50             p_input = input_item_New([o_urlString fileSystemRepresentation],
51                                     [[[NSFileManager defaultManager]
52                                     displayNameAtPath: o_urlString] UTF8String]);
53             if (!p_input)
54                 return nil;
55
56             returnValue = playlist_AddInput(p_playlist, p_input, PLAYLIST_INSERT,
57                                PLAYLIST_END, true, pl_Unlocked);
58             vlc_gc_decref(p_input);
59
60             if (returnValue != VLC_SUCCESS)
61                 return nil;
62
63             o_url = [NSURL fileURLWithPath: o_urlString];
64             if (o_url != nil)
65                 [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_url];
66         }
67     }
68     return nil;
69 }
70
71 @end
72
73
74 /*****************************************************************************
75  * VLControlScriptCommand implementation
76  *****************************************************************************/
77 /*
78  * This entire control command needs a better design. more object oriented.
79  * Applescript developers would be very welcome (hartman)
80  */
81 @implementation VLControlScriptCommand
82
83 - (id)performDefaultImplementation {
84     NSString *o_command = [[self commandDescription] commandName];
85     NSString *o_parameter = [self directParameter];
86
87     intf_thread_t * p_intf = VLCIntf;
88     playlist_t * p_playlist = pl_Get(p_intf);
89
90     if ([o_command isEqualToString:@"play"])
91         [[VLCCoreInteraction sharedInstance] play];
92     else if ([o_command isEqualToString:@"stop"])
93         [[VLCCoreInteraction sharedInstance] stop];
94     else if ([o_command isEqualToString:@"previous"])
95         [[VLCCoreInteraction sharedInstance] previous];
96     else if ([o_command isEqualToString:@"next"])
97         [[VLCCoreInteraction sharedInstance] next];
98     else if ([o_command isEqualToString:@"fullscreen"])
99         [[VLCCoreInteraction sharedInstance] toggleFullscreen];
100     else if ([o_command isEqualToString:@"mute"])
101         [[VLCCoreInteraction sharedInstance] setMute: YES];
102     else if ([o_command isEqualToString:@"volumeUp"])
103         [[VLCCoreInteraction sharedInstance] volumeUp];
104     else if ([o_command isEqualToString:@"volumeDown"])
105         [[VLCCoreInteraction sharedInstance] volumeDown];
106     else if ([o_command isEqualToString:@"stepForward"]) {
107         //default: forwardShort
108         if (o_parameter) {
109             int i_parameter = [o_parameter intValue];
110             switch (i_parameter) {
111                 case 1:
112                     [[VLCCoreInteraction sharedInstance] forwardExtraShort];
113                     break;
114                 case 2:
115                     [[VLCCoreInteraction sharedInstance] forwardShort];
116                     break;
117                 case 3:
118                     [[VLCCoreInteraction sharedInstance] forwardMedium];
119                     break;
120                 case 4:
121                     [[VLCCoreInteraction sharedInstance] forwardLong];
122                     break;
123                 default:
124                     [[VLCCoreInteraction sharedInstance] forwardShort];
125                     break;
126             }
127         } else
128             [[VLCCoreInteraction sharedInstance] forwardShort];
129     } else if ([o_command isEqualToString:@"stepBackward"]) {
130         //default: backwardShort
131         if (o_parameter) {
132             int i_parameter = [o_parameter intValue];
133             switch (i_parameter) {
134                 case 1:
135                     [[VLCCoreInteraction sharedInstance] backwardExtraShort];
136                     break;
137                 case 2:
138                     [[VLCCoreInteraction sharedInstance] backwardShort];
139                     break;
140                 case 3:
141                     [[VLCCoreInteraction sharedInstance] backwardMedium];
142                     break;
143                 case 4:
144                     [[VLCCoreInteraction sharedInstance] backwardLong];
145                     break;
146                 default:
147                     [[VLCCoreInteraction sharedInstance] backwardShort];
148                     break;
149             }
150         } else
151             [[VLCCoreInteraction sharedInstance] backwardShort];
152     }
153    return nil;
154 }
155
156 @end
157
158 /*****************************************************************************
159  * Category that adds AppleScript support to NSApplication
160  *****************************************************************************/
161 @implementation NSApplication(ScriptSupport)
162
163 - (BOOL)scriptFullscreenMode {
164     vout_thread_t * p_vout = getVoutForActiveWindow();
165     if (!p_vout)
166         return NO;
167     BOOL b_value = var_GetBool(p_vout, "fullscreen");
168     vlc_object_release(p_vout);
169     return b_value;
170 }
171
172 - (void)setScriptFullscreenMode:(BOOL)mode {
173     vout_thread_t * p_vout = getVoutForActiveWindow();
174     if (!p_vout)
175         return;
176     if (var_GetBool(p_vout, "fullscreen") == mode) {
177         vlc_object_release(p_vout);
178         return;
179     }
180     vlc_object_release(p_vout);
181     [[VLCCoreInteraction sharedInstance] toggleFullscreen];
182 }
183
184 - (BOOL) muted {
185     return [[VLCCoreInteraction sharedInstance] mute];
186 }
187
188 - (BOOL) playing {
189     intf_thread_t *p_intf = VLCIntf;
190     if (!p_intf)
191         return NO;
192
193     input_thread_t * p_input = pl_CurrentInput(p_intf);
194     if (!p_input)
195         return NO;
196
197     input_state_e i_state = ERROR_S;
198     input_Control(p_input, INPUT_GET_STATE, &i_state);
199     vlc_object_release(p_input);
200
201     return ((i_state == OPENING_S) || (i_state == PLAYING_S));
202 }
203
204 - (int) audioVolume {
205     return ([[VLCCoreInteraction sharedInstance] volume]);
206 }
207
208 - (void) setAudioVolume:(int)i_audioVolume {
209     [[VLCCoreInteraction sharedInstance] setVolume:(int)i_audioVolume];
210 }
211
212 - (int) currentTime {
213     input_thread_t * p_input = pl_CurrentInput(VLCIntf);
214     int64_t i_currentTime = -1;
215
216     if (!p_input)
217         return i_currentTime;
218
219     input_Control(p_input, INPUT_GET_TIME, &i_currentTime);
220     vlc_object_release(p_input);
221
222     return (int)(i_currentTime / 1000000);
223 }
224
225 - (void) setCurrentTime:(int)i_currentTime {
226     if (i_currentTime) {
227         int64_t i64_value = (int64_t)i_currentTime;
228         input_thread_t * p_input = pl_CurrentInput(VLCIntf);
229
230         if (!p_input)
231             return;
232
233         input_Control(p_input, INPUT_SET_TIME, (int64_t)(i64_value * 1000000));
234         vlc_object_release(p_input);
235     }
236 }
237
238 - (int) durationOfCurrentItem {
239     return [[VLCCoreInteraction sharedInstance] durationOfCurrentPlaylistItem];
240 }
241
242 - (NSString*) pathOfCurrentItem {
243     return [[[VLCCoreInteraction sharedInstance] URLOfCurrentPlaylistItem] path];
244 }
245
246 - (NSString*) nameOfCurrentItem {
247     return [[VLCCoreInteraction sharedInstance] nameOfCurrentPlaylistItem];
248 }
249
250 @end