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