applescript.m 10.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * applescript.m: MacOS X AppleScript support
 *****************************************************************************
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 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29
#import "intf.h"
#import "applescript.h"
30
#import "VLCCoreInteraction.h"
31
#import "VLCPlaylist.h"
32
#import <vlc_url.h>
33 34

/*****************************************************************************
35
 * VLGetURLScriptCommand implementation
36 37 38 39 40 41 42
 *****************************************************************************/
@implementation VLGetURLScriptCommand

- (id)performDefaultImplementation {
    NSString *o_command = [[self commandDescription] commandName];
    NSString *o_urlString = [self directParameter];

43 44
    if ([o_command isEqualToString:@"GetURL"] || [o_command isEqualToString:@"OpenURL"]) {
        if (o_urlString) {
45
            NSURL * o_url = [NSURL fileURLWithPath: o_urlString];
46
            if (o_url != nil)
47
                [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: o_url];
48

49
            input_thread_t * p_input = pl_CurrentInput(getIntf());
50 51 52 53 54 55 56 57 58 59 60
            BOOL b_returned = NO;

            if (p_input) {
                b_returned = input_AddSubtitle(p_input, [o_urlString UTF8String], true);
                vlc_object_release(p_input);
                if (!b_returned)
                    return nil;
            }

            NSDictionary *o_dic;
            NSArray *o_array;
61
            o_dic = [NSDictionary dictionaryWithObject:o_urlString forKey:@"ITEM_URL"];
62
            o_array = [NSArray arrayWithObject: o_dic];
63

64
            [[[VLCMain sharedInstance] playlist] addPlaylistItems:o_array];
65 66 67 68 69 70 71 72 73
        }
    }
    return nil;
}

@end


/*****************************************************************************
74
 * VLControlScriptCommand implementation
75 76 77 78 79 80 81 82 83
 *****************************************************************************/
/*
 * This entire control command needs a better design. more object oriented.
 * Applescript developers would be very welcome (hartman)
 */
@implementation VLControlScriptCommand

- (id)performDefaultImplementation {
    NSString *o_command = [[self commandDescription] commandName];
84 85
    NSString *o_parameter = [self directParameter];

86
    intf_thread_t * p_intf = getIntf();
87
    playlist_t * p_playlist = pl_Get(p_intf);
88

89
    if ([o_command isEqualToString:@"play"])
90
        [[VLCCoreInteraction sharedInstance] playOrPause];
91
    else if ([o_command isEqualToString:@"stop"])
92
        [[VLCCoreInteraction sharedInstance] stop];
93
    else if ([o_command isEqualToString:@"previous"])
94
        [[VLCCoreInteraction sharedInstance] previous];
95
    else if ([o_command isEqualToString:@"next"])
96
        [[VLCCoreInteraction sharedInstance] next];
97
    else if ([o_command isEqualToString:@"fullscreen"])
98
        [[VLCCoreInteraction sharedInstance] toggleFullscreen];
99
    else if ([o_command isEqualToString:@"mute"])
100
        [[VLCCoreInteraction sharedInstance] toggleMute];
101
    else if ([o_command isEqualToString:@"volumeUp"])
102
        [[VLCCoreInteraction sharedInstance] volumeUp];
103
    else if ([o_command isEqualToString:@"volumeDown"])
104
        [[VLCCoreInteraction sharedInstance] volumeDown];
105 106 107 108 109 110 111 112 113 114
    else if ([o_command isEqualToString:@"moveMenuFocusUp"])
        [[VLCCoreInteraction sharedInstance] moveMenuFocusUp];
    else if ([o_command isEqualToString:@"moveMenuFocusDown"])
        [[VLCCoreInteraction sharedInstance] moveMenuFocusDown];
    else if ([o_command isEqualToString:@"moveMenuFocusLeft"])
        [[VLCCoreInteraction sharedInstance] moveMenuFocusLeft];
    else if ([o_command isEqualToString:@"moveMenuFocusRight"])
        [[VLCCoreInteraction sharedInstance] moveMenuFocusRight];
    else if ([o_command isEqualToString:@"menuFocusActivate"])
        [[VLCCoreInteraction sharedInstance] menuFocusActivate];
115
    else if ([o_command isEqualToString:@"stepForward"]) {
116
        //default: forwardShort
117
        if (o_parameter) {
118
            int i_parameter = [o_parameter intValue];
119
            switch (i_parameter) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                case 1:
                    [[VLCCoreInteraction sharedInstance] forwardExtraShort];
                    break;
                case 2:
                    [[VLCCoreInteraction sharedInstance] forwardShort];
                    break;
                case 3:
                    [[VLCCoreInteraction sharedInstance] forwardMedium];
                    break;
                case 4:
                    [[VLCCoreInteraction sharedInstance] forwardLong];
                    break;
                default:
                    [[VLCCoreInteraction sharedInstance] forwardShort];
                    break;
            }
136
        } else
137
            [[VLCCoreInteraction sharedInstance] forwardShort];
138
    } else if ([o_command isEqualToString:@"stepBackward"]) {
139
        //default: backwardShort
140
        if (o_parameter) {
141
            int i_parameter = [o_parameter intValue];
142
            switch (i_parameter) {
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
                case 1:
                    [[VLCCoreInteraction sharedInstance] backwardExtraShort];
                    break;
                case 2:
                    [[VLCCoreInteraction sharedInstance] backwardShort];
                    break;
                case 3:
                    [[VLCCoreInteraction sharedInstance] backwardMedium];
                    break;
                case 4:
                    [[VLCCoreInteraction sharedInstance] backwardLong];
                    break;
                default:
                    [[VLCCoreInteraction sharedInstance] backwardShort];
                    break;
            }
159
        } else
160
            [[VLCCoreInteraction sharedInstance] backwardShort];
161 162
    }
   return nil;
163 164
}

Gildas Bazin's avatar
 
Gildas Bazin committed
165
@end
166 167 168 169 170 171

/*****************************************************************************
 * Category that adds AppleScript support to NSApplication
 *****************************************************************************/
@implementation NSApplication(ScriptSupport)

172
- (BOOL)scriptFullscreenMode {
173
    vout_thread_t * p_vout = getVoutForActiveWindow();
174
    if (!p_vout)
175
        return NO;
176 177
    BOOL b_value = var_GetBool(p_vout, "fullscreen");
    vlc_object_release(p_vout);
178
    return b_value;
179
}
180 181

- (void)setScriptFullscreenMode:(BOOL)mode {
182
    vout_thread_t * p_vout = getVoutForActiveWindow();
183
    if (!p_vout)
184
        return;
185 186
    if (var_GetBool(p_vout, "fullscreen") == mode) {
        vlc_object_release(p_vout);
187 188
        return;
    }
189
    vlc_object_release(p_vout);
190
    [[VLCCoreInteraction sharedInstance] toggleFullscreen];
191 192
}

193
- (BOOL) muted {
194
    return [[VLCCoreInteraction sharedInstance] mute];
195 196 197
}

- (BOOL) playing {
198
    intf_thread_t *p_intf = getIntf();
199
    if (!p_intf)
200 201
        return NO;

202 203
    input_thread_t * p_input = pl_CurrentInput(p_intf);
    if (!p_input)
204 205 206
        return NO;

    input_state_e i_state = ERROR_S;
207 208
    input_Control(p_input, INPUT_GET_STATE, &i_state);
    vlc_object_release(p_input);
209

210
    return ((i_state == OPENING_S) || (i_state == PLAYING_S));
211 212
}

213
- (int) audioVolume {
214
    return ([[VLCCoreInteraction sharedInstance] volume]);
215 216
}

217
- (void) setAudioVolume:(int)i_audioVolume {
218
    [[VLCCoreInteraction sharedInstance] setVolume:(int)i_audioVolume];
219 220
}

221
- (int) audioDesync {
222
    input_thread_t * p_input = pl_CurrentInput(getIntf());
223 224 225 226 227
    int i_delay = -1;

    if(!p_input)
        return i_delay;

228
    i_delay = var_GetInteger(p_input, "audio-delay");
229 230 231 232 233 234
    vlc_object_release(p_input);

    return (i_delay / 1000);
}

- (void) setAudioDesync:(int)i_audioDesync {
235
    input_thread_t * p_input = pl_CurrentInput(getIntf());
236 237 238
    if(!p_input)
        return;

239
    var_SetInteger(p_input, "audio-delay", i_audioDesync * 1000);
240 241 242
    vlc_object_release(p_input);
}

243
- (int) currentTime {
244
    input_thread_t * p_input = pl_CurrentInput(getIntf());
245 246
    int64_t i_currentTime = -1;

247
    if (!p_input)
248 249
        return i_currentTime;

250 251
    input_Control(p_input, INPUT_GET_TIME, &i_currentTime);
    vlc_object_release(p_input);
252

253
    return (int)(i_currentTime / 1000000);
254 255
}

256
- (void) setCurrentTime:(int)i_currentTime {
257 258
    if (i_currentTime) {
        int64_t i64_value = (int64_t)i_currentTime;
259
        input_thread_t * p_input = pl_CurrentInput(getIntf());
260

261
        if (!p_input)
262 263
            return;

264 265
        input_Control(p_input, INPUT_SET_TIME, (int64_t)(i64_value * 1000000));
        vlc_object_release(p_input);
266
    }
267 268 269
}

- (int) durationOfCurrentItem {
270
    return [[VLCCoreInteraction sharedInstance] durationOfCurrentPlaylistItem];
271 272 273
}

- (NSString*) pathOfCurrentItem {
274
    return [[[VLCCoreInteraction sharedInstance] URLOfCurrentPlaylistItem] path];
275 276 277
}

- (NSString*) nameOfCurrentItem {
278
    return [[VLCCoreInteraction sharedInstance] nameOfCurrentPlaylistItem];
279 280
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
- (BOOL)playbackShowsMenu {
    input_thread_t *p_input_thread = pl_CurrentInput(getIntf());

    if (!p_input_thread)
        return NO;

    int i_current_title = var_GetInteger(p_input_thread, "title");

    input_title_t **p_input_title;
    int count;

    /* fetch data */
    int coreret = input_Control(p_input_thread, INPUT_GET_FULL_TITLE_INFO,
                                &p_input_title, &count);
    vlc_object_release(p_input_thread);

    if (coreret != VLC_SUCCESS)
        return NO;

    BOOL ret = NO;

    if (count > 0 && i_current_title < count) {
        ret = p_input_title[i_current_title]->i_flags & INPUT_TITLE_MENU;
    }

    /* free array */
    for (int i = 0; i < count; i++) {
        vlc_input_title_Delete(p_input_title[i]);
    }
    free(p_input_title);

    return ret;
}

315
@end